OpenSC 常用实用工具操作说明

来源:互联网 发布:松溪县网络推广专员 编辑:程序博客网 时间:2024/05/17 05:05
################################################################################
以下内容整理自:http://www.gooze.eu/howto/smartcard-quickstarter-guide/smart-card-initialization
1.Install a smart card reader and initializea cryptographic card.
 安装智能卡读卡器并初始化一张加密卡。
  
  1)Plug-in your smartcardreader and insert the smartcard.
   or 
    Plug-inthe USB token.
    为你的电脑插上USB 型智能卡 或 智能卡读卡器(并插好智能卡)。
  
  2)You will need to runterminal on Linux.
    在 Linux上运行 终端程序。
   
  3)Smartcardrecognition
   智能卡识别
    Eachsmart card has a special footprint, which is called the ATR. Querythe ATR of the card:

    $opensc-tool --atr
    Usingreader with a card: Feitian SCR301 00 00
   3b:9f:95:81:31:fe:9f:00:65:46:53:05:30:06:71:df:00:00:00:80:6a:82:5e
    注:-a 或--atr -- 打印输出智能卡的复位应答字节值

    $opensc-tool --serial
    Usingreader with a card: Feitian SCR301 00 00
    29 2722 01 15 13 11 09 )'".....
   注:--serial -- 打印输出智能卡序列号

    $opensc-tool --reader 0 --name
   Entersafe
    注:-r 或--reader -- 指定要操作的读卡器编号
          --name   --鉴别智能卡且打印输出它的名字

  4)Erase your smartcard ifneeded:
   如果需要擦除你的智能卡:

    $pkcs15-init -E
    注:-E,--erase-card -- 擦除智能卡
   
  5)Initialize yoursmartcard with PIN 0000, 
    PUK111111 and name "NiuShengchao".
    用名字为NiuShengchao 和 PIN 码为 0000 和 PUK 码为 111111来初始化你的智能卡。
   注:NiuShengchao 是作者本人姓名,可以替换为你自己的姓名或任意你喜欢的字符串。

    Now wewill need to define:
   现在我们将需要定义:
   
    a PINcode: 
    Itgives access to your smartcard. 
    Youshould remember the PIN code by heart.
    一个PIN(个人识别码) 码:
   以便有使用你智能卡的权利。该 PIN码,你最好用心去记,以免密码泄露。 
   
    a PUKcode: 
    It isused to ublock your card if you lose the PINcode. 
    Note itdown somewhere and hide it.
    一个PUK(解锁码) 码:
   如果你丢失了你的 PIN 码,它是用来解锁的。
   注意把它放在某处藏起来。

    Choosea PIN code with 4 to 8 digits.
    In thebelow example, we use 0000 as PIN code.
    选择一个 4到 8 位的数字作为 PIN 码。
   在下面的例子中,我们使用 0000 作为 PIN 码。

    Choosea PUK code with 4 to 8 digits.
    in thebelow example, we use 111111 as PUK code.
    选择一个 4到 8 位的数字作为 PUK 码。
   在下面的例子中,我们使用 111111 作为 PUK 码。
   
    $pkcs15-init --create-pkcs15 --profile pkcs15+onepin--use-default-transport-key --pin 0000 --puk 111111 --label"NiuShengchao"
    注:-C,--create-pkcs15            -- 创建一个新的 PKCS#15结构
      -p,--profile                 -- 指定使用一个常规配置,pkcs15 为目前仅有的常规配置,onepin为该常规配置的参数选项,此两项要用 加号(+) 组合
      -T,--use-default-transport-keys --如果驱动认为它知道传输密钥的话可以不询问(使用默认的传输密钥)
      --pin                       -- 指定 PIN 码
      --puk                       -- 指定 PUK 码
      -l,--label                   -- 指定 PIN 或 密钥的标签
   
  6)Display cardinformation
   显示智能卡信息
    $pkcs15-tool -D
    注:-D,--dump -- 输出所有智能卡对象信息
   
    If youare using the Feitian PKI card, this displays:
    如果你使用的是飞天诚信 PKI 卡的话,显示如下信息:
   
    In thiscase, the PIN code has ID 01. 
   在这个案例中,PIN 码的 ID 是 01 。
   
    Youwill use this ID later on in the guide.
   在后面的指导中,你将用到这个 ID 。
   
    Usingreader with a card: Feitian SCR301 00 00
    PKCS#15Card [NiuShengchao]:
       Version       :0
       Serial number : 0391401113210812
       Manufacturer ID:EnterSafe
       Last update   :20130403090949Z
       Flags        : EID compliant

    PIN[User PIN]
       Object Flags  : [0x3], private, modifiable
       ID           : 01
       Flags        : [0x32], local, initialized,needs-padding
       Length        :min_len:4, max_len:16, stored_len:16
       Pad char      : 0x00
       Reference     : 1 (0x01)
       Type         : ascii-numeric
       Path         : 3f005015

 以下内容整理自:http://www.gooze.eu/howto/smartcard-quickstarter-guide/query-supported-mechanisms
  7)Query supportedmechanisms by your smart card
   查询你智能卡所支持的机制
  
    Toquery mechanisms supported by your smartcard/token, 
    you mayuse the following command:
   查询你智能卡所支持的机制,你可以使用如下命令:
  
    $pkcs11-tool --list-mechanisms --module/usr/local/lib/opensc-pkcs11.so
  
################################################################################
以下内容整理自:http://www.gooze.eu/howto/smartcard-quickstarter-guide/managing-pin-codes
2.Managing PIN codes
  管理 PIN 码
  
  1)Listing PINcodes
    列出 PIN码
    $pkcs15-tool --list-pins
   注:--list-pins -- 列出所有 PIN 码
   
  2)Changing PINcode
    修改 PIN码
    $pkcs15-tool --change-pin
   注:--change-pin -- 改变 PIN 或 PUK 码
   
   注:如果旧密码输出错误,将显示可尝试的剩余次数:
    PINcode incorrect; tries left: 2
   
  3)Unlock pincode
    解锁 PIN码
    $pkcs15-tool --unblock-pin
    注:-u,--unblock-pin - 解锁 PIN 码
   
  4)PIN code and PUK codelost
    PIN 码和PUK 码全忘了
    Usingthe Feitian PKI, there is nothing you can do.
    使用 飞天诚信PKI 智能卡,对于这种情况,没有什么办法解决的。
    Youwill need to erase and initialize your smart card.
   你将要重新擦除并初始化你的智能卡。


以下内容整理自:http://www.gooze.eu/howto/smartcard-quickstarter-guide/what-you-should-know-about-security
Warning: generating an RSA key pair on smartcard may result in losing data
警告:在智能卡上产生 RSA 密钥对可能导致丢失数据

If you generate the RSA key on the smart card(embedeed processor) and 
the smart card becomes unusable (electricalshock or any physicaldeterioration), 
your encrypted data may becomeunreadable. 
如果你在智能卡上(嵌入式处理器)产生 RSA密钥,之后智能卡变得不可用(受到电击或物理老损),
你加密的数据可能将无法读取。

This may have dramaticconsequences, 
like loosing content of an encrypted disc orbeing unable to read encrypted emails.
这可能会有戏剧性的后果,像 加密盘中不精确的内容 或存在加密的邮件不可读。

Wheneverpossible, 
you should always generate the RSA privatekey on your computer and transfer it to smartcard. 
你尽可能的在你的计算机上产生 RSA私钥并将它传递到智能卡上。

Back-up the private key to a non removablemedia. 
并在一个不可清除的存储媒介(如:只读光盘)上备份该私钥。

Store the media in a safeplace.
然后将该媒介放到一个安全的地方保存。

################################################################################
以下内容整理自:http://www.gooze.eu/howto/smartcard-quickstarter-guide/scenario-2-creating-a-self-signed-certificate-on-computer
3.Generate and transfer RSA keys to thecryptographic card.
  产生并且传递 RSA密钥到加密卡中。
  
  1)Generate an RSA key onyour computer
   在你的计算机上产生一个 RSA 密钥
    $openssl genrsa 2048 > key.pem
   注:genrsa    -- 生成 RSA密钥
      >key.pem -- > 为 Linux 的标准输出重定向到一个文件,文件名中的 key是可以任意起的,但要方便理解。
                 key.pem文件中包含了公钥与私钥,用法标明为:公钥 -> 验证,私钥 -> 签名
   
  2)Transfer the key tosmartcard/token:
   传递密钥到你的智能卡或 USB 型智能卡中:
    $pkcs15-init --store-private-key key.pem --auth-id 01 --pin0000
    注:-S,--store-private-key -- 存储指定文件中的私钥
      -a,--auth-id          -- 使用或创建指定 ID 的 PIN码
      --pin                -- 指定 PIN码
   
  3)Display cardinformation
   显示智能卡信息
    $pkcs15-tool -D
    注:-D,--dump -- 输出所有智能卡对象信息
   
   显示的信息内容中会多出如下两项:
    PrivateRSA Key [Private Key]
       Object Flags  : [0x3], private, modifiable
       Usage        : [0x4], sign
       Access Flags  : [0xD], sensitive, alwaysSensitive,neverExtract
       ModLength     : 2048
       Key ref       : 2(0x2)
       Native        :yes
       Path         : 3f005015
       Auth ID       :01
       ID           :d2521ca08e20331c970de35bb5cbedd6853c6b8a
       GUID         :{999ede21-3c25-b85f-0568-dc1fe972440d}

    PublicRSA Key [Public Key]
       Object Flags  : [0x2], modifiable
       Usage        : [0x40], verify
       Access Flags  : [0x0]
       ModLength     : 2048
       Key ref       : 0(0x0)
       Native        :no
       Path         : 3f0050153000
       ID           :d2521ca08e20331c970de35bb5cbedd6853c6b8a
       DirectValue   :

################################################################################
以下内容整理自:http://www.gooze.eu/howto/smartcard-quickstarter-guide/scenario-1-importing-certificates-from-pkcs12-file
4.Generate and transfer X.509 certificates tothe cryptographic card.
  产生并且传递 X.509证书到加密卡中。

  importing certificatesfrom PKCS#12 file
  从 PKCS#12文件中导入证书

  PKCS#12 format (.p12 or.pfx extension) is the standard for exchanging RSA keys and X.509certificates.
  PKCS#12 格式(采用 .p12 或 .pfx扩展名)是交换 RSA 密钥和 X.509 证书的标准。
  In Firefox (NSS) orWindows Certificate Manager, 
  it is possible to exportcertificates to PKCS#12 format.
  在 火狐浏览器(NSS) 或 Windows证书管理器中,是可以从 PKCS#12 格式中导出证书的。

  A PKCS#12 file maybundle:
  一个 PKCS#12文件可以捆绑:

  RSA key pairs (private andpublic keys).
  RSA密钥对(公私钥)。
  
  X.509 usercertificates.
  X.509用户证书。
  
  Root CA X.509certificates.
  首个 CA 的 X.509证书。
  
  1)To create a ca'scertificate file, use OpenSSL command:
    使用OpenSSL 命令来创建一个 CA 证书文件:
   注:由于 CSR(CertificateSigning Request) 文件要交给 CA 签名后,方可成为证书。
     一般让 CA做签名是要交钱的,我们这里只是演示,所以就自己来做 CA 生成 CA 证书。

    $openssl req -new -x509 -keyout CACert.key -out CACert.crt -configopenssl.cnf
    注:req    -- 用于生成和处理 PKCS#10证书请求
      -new   -- 生成新的证书请求以及私钥,默认为 1024比特(bits)
      -x509  -- 生成自签名证书
     -keyout -- 指定生成的私钥文件名称
      -out   --指定输出的文件名,如:key-file.pkcs12
     -config -- 指定证书请求模板文件,如:OpenSSL 自带的 openssl.cnf ,一般在/usr/local/ssl 目录下

  2)To create a customer'sRSA key file, use OpenSSLcommand: 
    使用OpenSSL 命令来创建一个用户 RSA 密钥文件:
    
    $openssl genrsa -out privateKey.key 1024
   注:genrsa -- 生成 RSA 密钥
      -out  -- 指定输出的文件名,如:privateKey.key
      1024  -- 指定密钥长度,默认为:512

  3)To create a customer'sCSR file, use OpenSSL command: 
    使用OpenSSL 命令来创建一个用户 CSR 证书签名请求文件:
    
    $openssl req -new -key privateKey.key -out certificate.csr -configopenssl.cnf
   要输入以下信息方可产生证书:
    CountryName (2 letter code) [AU]:CN
   国家名
    Stateor Province Name (full name) [Some-State]:beijing
   省份名
   Locality Name (eg, city) []:beijing
   城市名
   Organization Name (eg, company) [Internet Widgits Pty Ltd]:Feitian 
   公司名
   Organizational Unit Name (eg, section)[]:EnterSafe
   部门名
    CommonName (eg, YOUR name) []:NiuShengchao
   个人名
    EmailAddress []:shengchao@feitian.com
   邮件名
    
    注:req    -- 用于生成和处理 PKCS#10证书请求
      -new   -- 生成新的证书请求以及私钥,默认为 1024比特(bits)
      -key   -- 由 2) 生成的 RSA密钥文件
      -out   --指定输出的文件名,如:certificate.csr
     -config -- 指定证书请求模板文件,如:OpenSSL 自带的 openssl.cnf ,一般在/usr/local/ssl 目录下

  4)To create a customer'scertificate file, use OpenSSLcommand: 
    使用OpenSSL 命令来创建一个用户证书文件:
    注:由于我们用-config 指定的是 OpenSSL 自带的 openssl.cnf文件,所以我们还得按这个文件的要求创建一个文件夹及一些文件:
     1>要在 openssl.cnf 文件当前目录下创建 demoCA 文件夹;
     2>要在 demoCA 文件夹内创建一个空白的 index.txt 文本文件;
     3>要在 demoCA 文件夹内创建一个空白的 serial 文本文件,并用文本编辑器添加 01两个字符做为生成证书的起始序列号,以后会自动加壹增长;
     以上三点要求可详见 openssl.cnf 文件内容。
      
     产生的用户证书文件(*.crt)可以和用户 RSA 密钥文件(*.key)合并到一个文件里,
     如:把两个文件的内容(均是文本信息)拷贝到一个扩展名为 *.pem 的文件中。
    
    $openssl ca -in certificate.csr -out certificate.crt -certCACert.crt -keyfile CACert.key -config openssl.cnf

    注:ca      -- 这是一个小型CA 系统,它能签发证书请求和生成 CRL ,维护一个已签发证书状态的文本数据库
      -in     --指定输入的证书请求文件
      -out    --指定输出的文件名,如:certificate.crt
      -cert   -- 指定 CA 证书文件
     -keyfile -- 指定 CA 签发证书的 RSA 密钥文件
     -config  -- 指定证书请求模板文件,如:OpenSSL 自带的 openssl.cnf,一般在 /usr/local/ssl 目录下
    
  5)To create a PKCS#12 filewith ca's certificate, use OpenSSL command:
    使用OpenSSL 命令来创建一个包含 CA 证书的 PKCS#12 文件:
    $openssl pkcs12 -export -out key-file.pkcs12 -inkey privateKey.key-in certificate.crt -CAfile CACert.crt
   注:pkcs12  -- pkcs12 文件工具,能生成和分析 pkcs12文件
     -export -- 输出 pkcs12 文件
      -out   --指定输出的文件名,如:key-file.pkcs12
     -inkey  -- 指定用户密钥文件,如果不用此选项,密钥必须在用 -in参数指定的文件中
      -in    -- 指定私钥和证书读取的文件,必须为 PEM格式
     -CAfile -- 指定 CA 证书文件
   
    使用OpenSSL 命令的另一种方式来创建一个不包含 CA 证书的 PKCS#12 文件:
    $openssl pkcs12 -export -out key-file2.pkcs12 -inkey customer.pem-in customer.pem
   注:customer.pem = certificate.crt + privateKey.key
    
  6)To show PKCS#12 fileinformation, use OpenSSL command:
    使用OpenSSL 命令来显示 PKCS#12 文件信息:
    $openssl pkcs12 -in key-file.pkcs12 -info
   注:pkcs12 -- pkcs12 文件工具,能生成和分析 pkcs12 文件
      -in   -- 指定私钥和证书读取的文件,必须为 PEM格式
      -info -- 输出 pkcs12 结构信息
   
  7)To import a PKCS#12file, type:
    导入PKCS#12 文件,敲入如下命令:
    $pkcs15-init --store-private-key key-file.pkcs12 --format pkcs12--id 85746946F3463ED0158B5B6BF9922C09F3D3C9A4 --auth-id 01 --pin0000
    注:-S,--store-private-key -- 存储指定文件中的私钥
      -f,--format           --指定密钥或证书文件格式:PEM(默认),DER 或 PKCS12
      -i,--id              -- 指定 密钥 或 证书ID,如果忽略此参数,则创建一个默认的 ID
                            85746946F3463ED0158B5B6BF9922C09F3D3C9A4 是执行 6)时得到的 localKeyID: 信息
      -a,--auth-id          -- 使用或创建指定 ID 的 PIN码
      --pin                -- 指定 PIN码
    另:-u,--key-usage        --可以添加此参数选项来指定添加的密钥用法

  6)Display cardinformation
   显示智能卡信息
    $pkcs15-tool -D
    注:-D,--dump -- 输出所有智能卡对象信息
   
   显示的信息内容中会多出如下两项:
    PrivateRSA Key [NiuShengchao]
       Object Flags  : [0x3], private, modifiable
       Usage        : [0x10E], decrypt, sign, signRecover,derive
       Access Flags  : [0xD], sensitive, alwaysSensitive,neverExtract
       ModLength     : 1024
       Key ref       : 1(0x1)
       Native        :yes
       Path         : 3f005015
       Auth ID       :01
       ID           :85746946f3463ed0158b5b6bf9922c09f3d3c9a4
       GUID         :{8d73029a-473e-2d4d-593e-e66a106ca9ca}

    X.509Certificate [NiuShengchao]
       Object Flags  : [0x2], modifiable
       Authority     : no
       Path         : 3f0050153100
       ID           :85746946f3463ed0158b5b6bf9922c09f3d3c9a4
       GUID         :{8d73029a-473e-2d4d-593e-e66a106ca9ca}
       Encoded serial : 02 0101
      
    另:Asyou notice, this command transfers private RSA key, X.509certificates.
     同样地你可以注意到,7) 传递了私钥和证书到智能卡中。
     
      Thereis no need to import the RSA public key as it can be derived fromRSA public key.
      不需要导入RSA 公钥的原因是导的这个私钥具有 衍生(derive)用法,即可以衍生出公钥来。 

################################################################################
以下内容整理自:http://www.gooze.eu/howto/smartcard-quickstarter-guide/scenario-3-creating-a-self-signed-certificate-using-embedded
5.creating a RSA keypair using embeddedprocessor
  使用智能卡的嵌入式处理器来创建一个 RSA密钥对
  
  Warning: using thismethod, there is no way to backup yourkeys. 
  警告:   使用该方法产生的密钥,将不能导出备份。
         
         Therefore, it is not usable for encryption ofsensitive data. 
         因此,由该方法产生的密钥不能用于需慎重处理的数据加密。
         
         If you lose the smartcard ortoken, 
         you will not be able to recover yourdata, 
         which will be lost FOREVER.
         如果你丢失了该智能卡或 USB型智能卡,你将无法解密你的数据,意味着此数据永远丢失!
         
  1)Creating an RSA keypairusing the embedded processor
   使用智能卡的嵌入式处理器来创建一个 RSA 密钥对
   
    $pkcs15-init --generate-key rsa/2048 --auth-id 01 --pin 0000 -usign,decrypt
    注:-G,--generate-key -- 在智能卡上产生并存储一个新密钥,rsa/2048 指出产生 2048 比特(bits) rsa密钥对
      -a,--auth-id     -- 使用或创建指定 ID 的 PIN 码
      --pin           -- 指定 PIN 码
      -u,--key-usage    --指定智能卡上产生新密钥的用法

  2)Display cardinformation
   显示智能卡信息
    $pkcs15-tool -D
    注:-D,--dump -- 输出所有智能卡对象信息
   
   显示的信息内容中会多出如下两项:
    PrivateRSA Key [Private Key]
       Object Flags  : [0x3], private, modifiable
       Usage        : [0x2E], decrypt, sign, signRecover,unwrap
       Access Flags  : [0x1D], sensitive, alwaysSensitive,neverExtract, local
       ModLength     : 2048
       Key ref       : 3(0x3)
       Native        :yes
       Path         : 3f005015
       Auth ID       :01
       ID           :cf3b3c42b14f3ff59710d564933becdaf0a432e8
       GUID         :{3b6a24b4-c316-6514-1dce-19b054ae89b4}

    PublicRSA Key [Private Key]
       Object Flags  : [0x2], modifiable
       Usage        : [0xD1], encrypt, wrap, verify,verifyRecover
       Access Flags  : [0x0]
       ModLength     : 2048
       Key ref       : 0(0x0)
       Native        :no
       Path         : 3f0050153001
       ID           :cf3b3c42b14f3ff59710d564933becdaf0a432e8
       DirectValue   :

################################################################################
以下内容整理自:http://www.gooze.eu/howto/smartcard-quickstarter-guide/signing-crypting-and-verifying
6.Signing and verifying usingMD5
  用 MD5签名与验签
  
  1)Erase your smartcard ifneeded:
   如果需要擦除你的智能卡:

    $pkcs15-init -E
    注:-E,--erase-card -- 擦除智能卡

  2)Initialize yoursmartcard with PIN 0000, 
    PUK111111 and name "NiuShengchao".
    用名字为NiuShengchao 和 PIN 码为 0000 和 PUK 码为 111111来初始化你的智能卡。
   注:NiuShengchao 是作者本人姓名,可以替换为你自己的姓名或任意你喜欢的字符串。
   
    $pkcs15-init --create-pkcs15 --profile pkcs15+onepin--use-default-transport-key --pin 0000 --puk 111111 --label"NiuShengchao"
    注:-C,--create-pkcs15            -- 创建一个新的 PKCS#15结构
      -p,--profile                 -- 指定使用一个常规配置,pkcs15 为目前仅有的常规配置,onepin为该常规配置的参数选项,此两项要用 加号(+) 组合
      -T,--use-default-transport-keys --如果驱动认为它知道传输密钥的话可以不询问(使用默认的传输密钥)
      --pin                       -- 指定 PIN 码
      --puk                       -- 指定 PUK 码
      -l,--label                   -- 指定 PIN 或 密钥的标签

  3)Creating an RSA keypairusing the embedded processor
   使用智能卡的嵌入式处理器来创建一个 RSA 密钥对
   
    $pkcs15-init --generate-key rsa/1024 --auth-id 01 --pin 0000 -usign,decrypt
    注:-G,--generate-key -- 在智能卡上产生并存储一个新密钥,rsa/2048 指出产生 2048 比特(bits) rsa密钥对
      -a,--auth-id     -- 使用或创建指定 ID 的 PIN 码
      --pin           -- 指定 PIN 码
      -u,--key-usage    --指定智能卡上产生新密钥的用法

  4)Query available publicRSA key:
   查询所有可得到的公钥:
  
   $pkcs15-tool --list-public-keys
  注:--list-public-keys - 列出所有公钥
  
  执行成功显示下列信息:(注意公钥ID)
   Usingreader with a card: Feitian SCR301 00 00
   PublicRSA Key [Private Key]
      Object Flags  : [0x2], modifiable
      Usage        : [0xD1], encrypt, wrap, verify,verifyRecover
      Access Flags  : [0x0]
      ModLength     : 1024
      Key ref       : 0(0x0)
      Native        :no
      Path         : 3f0050153000
    ID           :90da6b57300e07606bfffbc73e944ca43f9138d0
      DirectValue   :

  5)读出 RSA公钥
    $pkcs15-tool --read-public-key90da6b57300e07606bfffbc73e944ca43f9138d0 -opuk.pem
   注:--read-public-key -- 读出指定 ID 的公钥
      -o,--output      --输出结果到指定文件

  6)先创建一个名为 somemessage的文本文件,test text 为里面的内容:
   
    $ echotest text > somemessage

  7)次用 somemessage 文本文件做 MD5运算:
  
    $openssl dgst -md5 -binary -out hashed <somemessage
    注:dgst-- 数据摘要
      -md5-- 用 md5 进行摘要,默认选项
      -out-- 输出结果到指定文件
      <   --重定向标准输入到指定的文件
   
   另:hashed      --文件名,包含 MD5 值,上述指令的输出物
     somemessage -- 文件名,包含将要做数据摘要的信息
     
  8)再用 pkcs15-crypt对数据摘要做签名:
   
    $pkcs15-crypt -vvvvvvvvv -s --md5 --pkcs1 -i hashed -osigned
    注:-vv        --将同时显示每条命令的描述
      -vvv       -- 将同样为每条命令添加输入标志
      -vvvv      -- 将同样显示内部输入标志
      -s,--sign   -- 执行数字签名操作
      --md5      -- 输入文件是一个 MD5 散列数据
     -pkcs1      -- 使用 PKCS#1 v1.5 填充方式
      -i,--input  -- 使用指定的输入文件
      -o,--output -- 输到指定的文件
     
  9)最后用 openssl对签名做验签:
  
    $openssl dgst -verify puk.pem -md5 -signature signed <somemessage
    注:dgst      --数据摘要
     -verify    --用密钥文件中的公钥验证签名
      -md5      -- 用 md5进行摘要,默认选项
     -signature -- 指定一个签名文件,即 3) 的输出文件
      <        --重定向标准输入到指定的文件
     
  执行成功显示下列信息:
   VerifiedOK

################################################################################
以下内容整理自:http://www.gooze.eu/howto/smartcard-quickstarter-guide/signing-crypting-and-verifying
7.Encrypting message (pkcs1padding)
  加密消息(使用 PKCS#1 v1.5填充方式)

  *以下操作是基于 6. 操作成功之上的。

 1)用公钥对消息数据做加密:
  
    $openssl rsautl -pubin -inkey puk.pem -encrypt -in somemessage -outencrypted
   注:rsautl   -- RSA 工具,能使用 RSA算法签名,验证身份,加解密数据
     -pubin   --表明输入的是一个公钥文件,默认输入为私钥文件
     -inkey   -- 指定密钥文件
     -encrypt -- 用公钥对输入数据加密
      -in     --指定要加密的数据文件名
      -out    --指定加密后的数据文件名

   另:encrypted   --文件名,包含加密后的数据,上述指令的输出物
     somemessage -- 文件名,包含将要做数据加密的信息
     puk.pem     --文件名,包含公钥
  
  2)使用 pkcs15-crypt工具
  
    $pkcs15-crypt -vvvvvvvvv -c --pkcs1 -i encrypted -odecrypted
   
    注:-vv        --将同时显示每条命令的描述
      -vvv       -- 将同样为每条命令添加输入标志
      -vvvv      -- 将同样显示内部输入标志
     -pkcs1      -- 使用 PKCS#1 v1.5 填充方式
      -i,--input  -- 使用指定的输入文件
      -o,--output -- 输到指定的文件

   另:encrypted -- 文件名,包含加密后的数据
     decrypted -- 文件名,包含解密后的数据,上述指令的输出物

################################################################################
以下内容整理自:http://www.gooze.eu/howto/smartcard-quickstarter-guide/scenario-5-importing-3des-key
8.Importing 3DES key
  OpenSC is able to generateRSA keys using the embedded processor, not 3DESkeys.
  OpenSC 可以使用智能卡的嵌入式处理器来产生RSA 密钥,而非 3DES 密钥。
  
  To use 3DES keys, we needto generate and import them to smartcard/token.
  要使用 3DES密钥,我们需要先产生,然后导入它们到知能卡或 USB 型智能卡中。
  
  1)Generate a 3des keyusing OpenSSL:
    用OpenSSL 产生一个 3DES 密钥:
   
    $openssl rand 24 -out 3des.key
    注:rand-- 生成随机数
      -out-- 输出结果到指定文件,文件名任意起,但要便于理解
     
  2)Query availableslots:
   查询可用的槽:
   
    $pkcs11-tool --list-slots --module/usr/local/lib/opensc-pkcs11.so
    注:-L,--list-slots -- 得到可用的槽
     --module        -- 指定要加载的共享库(受委托的)
   
   执行成功显示下列信息:
   Available slots:
    Slot 0(0xffffffff): Virtual hotplug slot
     (empty)
    Slot 1(0x1): Feitian SCR301 00 00
     token label       :NiuShengchao (User PIN)
     token manufacturer : EnterSafe
     token model       :PKCS#15
     token flags       : rng,login required, PIN initialized, token initialized
     hardware version   :0.0
     firmware version   :0.0
     serial num        :0391401113210812

  3)Import the 3DES key asan independant object and write private flag to forbid the userfrom reading it back:
    导入 3DES密钥作一个独立对象并且标记私有标志,以防止用户把它从智能卡中读出来:
   
    $pkcs11-tool -v --module /usr/local/lib/opensc-pkcs11.so --slot 1--label "3deskey" --write-object 3des.key --type data --private--login --pin 0000
    注:-v,--verbose     -- 详述操作
     --module          --指定要加载的共享库(受委托的)
     --slot            -- 使用指定 ID的槽
      -a,--label       -- 指定对象的标签
      -w,--write-object --向智能卡中写入一个对象(例如:cert(证书),privkey(私钥),pubkey(公钥),data(数据))
      -y,--type        --指定对象的类型(例如:cert(证书),privkey(私钥),pubkey(公钥),data(数据))
     --private         -- 设置CKA_PRIVATE 属性(在登录后对象仅为可见,以防止用户把它从智能卡中读出来)
      -l,--login       -- 首次登录智能卡
      -p,--pin         -- 在命令行上补充用户 PIN码(如果是用在脚本文件:要当心泄密!)
     
   显示下列信息表示执行成功:
    Usingslot with ID 0x1
    CreatedData Object:
    Dataobject 135014352
     label:        '3deskey'
     application:   '3deskey'
     app_id:       
     flags:          modifiableprivate

  4)Display cardinformation
   显示智能卡信息
    $pkcs15-tool -D
    注:-D,--dump -- 输出所有智能卡对象信息
   
   显示的信息内容中会多出如下一项:
    Dataobject '3deskey'
       applicationName:3deskey
       Path:          3f0050153400
       Auth ID:       01
   
   *The3DES key is installed and usable by the smartcard and OpenSSLcrytoengine.
    至此 3DES密钥已被安装且可被智能卡和 OpenSSL 加密引擎使用。
   
  5)It can also be retrievedusing a PIN code and therefore is not completelyhidden:
    此 3DES密钥使用 PIN 码可以取出,因此不是完全隐藏的:
   
    $pkcs15-tool --read-data-object "3deskey" -owelcome-back.txt
    注:-R,--read-data-object -- 读出指定 OID ,应用名 或 标签 的数据对象
      -o,--output          -- 输出结果到指定文件
     
   
    $pkcs15-tool --read-public-keyd2521ca08e20331c970de35bb5cbedd6853c6b8a -od2521ca08e20331c970de35bb5cbedd6853c6b8a.txt
   注:--read-public-key -- 读出指定 ID 的公钥
      -o,--output      --输出结果到指定文件

################################################################################
以下内容整理自:http://www.gooze.eu/howto/smartcard-quickstarter-guide/storing-objects-on-smartcard
9.Storing objects onsmartcard
  存储对象到智能卡中
  
  1)先创建一个名为 thismessage的文本文件,test text 为里面的内容:
   
    $ echotest text > thismessage

  2)Listing PINcodes
    列出 PIN码
    $pkcs15-tool --list-pins
   注:--list-pins -- 列出所有 PIN 码

   显示下列信息表示执行成功:
    Usingreader with a card: Feitian SCR301 00 00
    PIN[User PIN]
       Object Flags  : [0x3], private, modifiable
       ID           : 01
       Flags        : [0x32], local, initialized,needs-padding
       Length        :min_len:4, max_len:16, stored_len:16
       Pad char      : 0x00
       Reference     : 1 (0x01)
       Type         : ascii-numeric
       Path         : 3f005015

  3)To store the file onsmart card with auth ID 01:
    用 ID 为01 的 PIN 码来存储此文本文件:
   
    $pkcs15-init -W thismessage -a 01 -l "thismessage"
    注:-W,--store-data -- 存储一个数据对象
      -a,--auth-id    -- 使用或创建指定 ID 的 PIN码
      -l,--label      --指定 PIN 或 密钥 或 数据对象 的标签
   
  4)To list files oncard:
   列出智能卡的所有数据对象:
   
    $pkcs15-tool --list-data-objects
    注:-C,--list-data-objects -- 列出所有的数据对象
   
   显示的信息内容中会多出如下一项:
    Dataobject 'thismessage'
       applicationName:pkcs15-init
       Path:          3f0050153400
       Auth ID:       01

  5)To readout thefile:
   读出这个文件:
   
    $pkcs15-tool --read-data-object "thismessage" -othatmessage
    注:-R,--read-data-object -- 读出指定 OID ,应用名 或 标签 的数据对象
      -o,--output          -- 输出结果到指定文件

################################################################################
以下内容整理自:http://www.gooze.eu/howto/smartcard-quickstarter-guide/scenario-2-creating-a-self-signed-certificate-on-computer
A.Configure OpenSSL to use smartcardcryptographic engine:
  配置 OpenSSL使用智能卡加密引擎:
  
  Forsafety, 
  we will be using OpenSSLwith engine_pkcs11 to generate certificate using the smart cardcryptographic engine.
  为了保险起见,
  我们将让 OpenSSL使用 engine_pkcs11 智能卡加密引擎来创建证书。
  
  1)Enter OpenSSL commandprompt:
    进入OpenSSL 命令提示行状态:
   
    $openssl
   
  2)Copy the following lineand paste it in OpenSSL command prompt:
    拷贝如下命令到OpenSSL 命令提示行:(不含 OpenSSL> )
   
   OpenSSL> engine dynamic -preSO_PATH:/usr/local/lib/engines/engine_pkcs11.so -pre ID:pkcs11 -preLIST_ADD:1 -pre LOAD -preMODULE_PATH:/usr/local/lib/opensc-pkcs11.so
   注:engine  -- 通过 Engine 机制使用第三方算法库或硬件设备而不是 OpenSSL本身的 RSA 算法
     dynamic -- 动态加载 Engine 设备的接口
      $openssl engine -h
      usage: engine opts [engine...]
       -v[v[v[v]]] - verbose mode,for each engine, list its 'control commands'
                  开启每一个引擎的详述模式,列出它们的控制命令
                  
                   -vv willadditionally display each command's description
                   -vv将同时显示每条命令的描述
                  
                   -vvv willalso add the input flags for each command
                   -vvv将同样为每条命令添加输入标志
                  
                   -vvvv willalso show internal input flags
                   -vvvv将同样显示内部输入标志
                  
       -c        - for each engine, also list thecapabilities
                  同样为每个引擎列出它的性能
                  
       -t[t]      - for each engine, check thatthey are really available
                  检测可用的引擎
                  
                   -tt willdisplay error trace for unavailable engines
                   -tt将为不可用的引擎显示错误跟踪
                  
       -pre  - runscommand 'cmd' against the ENGINE before any attempts to load it (if-t is used)
                  在任何尝试加载引擎之前运行 指令 以供引擎之用(如果 -t 参数选项被使用的话)
                  
       -post - runs command 'cmd'against the ENGINE after loading it(only used if -t is alsoprovided)
                   在加载引擎之后运行指令 以供引擎之用(如果 -t 参数选项同样被提供的话,那么它刚刚使用过)
                  
       NB: -pre and -post will beapplied to all ENGINEs supplied on the commandline, 
          or all supported ENGINEs if none arespecified.
       注意:-pre 和 -post将被应用于全部在命令行上提供的引擎,或如果没有指定引擎的话,即是应用于全部被支持的引擎。
          
       E.g. '-pre"SO_PATH:/lib/libdriver.so"' calls command "SO_PATH" with argument"/lib/libdriver.so".
       例如:-pre"SO_PATH:/lib/libdriver.so 调用参数为 /lib/libdriver.so 的指令SO_PATH

     显示下列信息表示执行成功:
     (dynamic) Dynamic engine loading support
     [Success]:SO_PATH:/usr/local/lib/engines/engine_pkcs11.so
     [Success]: ID:pkcs11
     [Success]: LIST_ADD:1
     [Success]: LOAD
     [Success]:MODULE_PATH:/usr/local/lib/opensc-pkcs11.so
     Loaded: (pkcs11) pkcs11 engine

   *Do notexit the OpenSSL command prompt.
    执行 2)后不要退出 OpenSSL 命令提示行。
    
  3)Query availableslots:
   查询可用的槽:
   
    $pkcs11-tool --list-slots --module/usr/local/lib/opensc-pkcs11.so
    注:-L,--list-slots -- 得到可用的槽
     --module        -- 指定要加载的共享库(受委托的)
   
   执行成功显示下列信息:
   Available slots:
    Slot 0(0xffffffff): Virtual hotplug slot
     (empty)
    Slot 1(0x1): Feitian SCR301 00 00
     token label       :NiuShengchao (User PIN)
     token manufacturer : EnterSafe
     token model       :PKCS#15
     token flags       : rng,login required, PIN initialized, token initialized
     hardware version   :0.0
     firmware version   :0.0
     serial num        :0391401113210812

  4)Query the ID of the RSAkey:
    $pkcs15-tool --list-keys
    注:-k,--list-keys -- 列出所有私钥
   
   执行成功显示下列信息:
    PrivateRSA Key [Private Key]
       Object Flags  : [0x3], private, modifiable
       Usage        : [0x4], sign
       Access Flags  : [0xD], sensitive, alwaysSensitive,neverExtract
       ModLength     : 2048
       Key ref       : 2(0x2)
       Native        :yes
       Path         : 3f005015
       Auth ID       :01
       ID           :d2521ca08e20331c970de35bb5cbedd6853c6b8a
       GUID         :{999ede21-3c25-b85f-0568-dc1fe972440d}
   
  5)Creating a self-signedcertificate
   创建一个自签名的证书
      
    We aregoing to generate a certificate using this RSAkey.
    我们要使用RSA 密钥产生一个证书。
   
   OpenSSL> req -engine pkcs11 -new -keyslot_1-id_d2521ca08e20331c970de35bb5cbedd6853c6b8a -keyform engine-x509 -out cert.pem -text
    注:req     -- 用于生成和处理PKCS#10 证书请求
     -engine  -- 指定硬件引擎
      -new    -- 生成新的证书请求以及私钥,默认为 1024比特(bits)
      -key    -- 密钥 ID ,格式:slot_数字-id_密钥 ID<-- 槽ID见 (3 输出结果 和 秘钥ID见 (4 输出结果
     -keyform -- 密钥来源,engine - 来自引擎
      -x509   -- 生成自签名证书
      -out    -- 指定文件名输出
      -text   --打印证书请求或自签名证书信息

  6)Your self-signedcertificate was saved to cert.pem. The certificate can be verifiedagainst itself:
   你的自签名证书是被保存为 cert.pem ,该证书可以自验证:
   
   OpenSSL> verify -CAfile cert.pem cert.pem
   注:verify  -- 证书验证工具
     -CAfile -- CA 证书,当其格式为 PEM 格式时,里面可以有多个 CA 证书。
    另:倒数第一个cert.pem 是作为 CA 证书的自身,倒第一个才是要验证的自身。
   
   显示下列信息表示验证成功:
   cert.pem: OK

  7)We store certificate oncard:
   我们存储证书到智能卡上去:
   
    $pkcs15-init --store-certificate cert.pem --auth-id 01 --idd2521ca08e20331c970de35bb5cbedd6853c6b8a --formatpem
    注:-X,--store-certificate -- 存储一个指定的 x.509 证书
      -a,--auth-id          -- 使用或创建指定 ID 的 PIN码
      -i,--id              -- 指定 密钥 或 证书 ID,此处指定将存储的证书 ID与产生它的密钥 ID 相同
      -f,--format           --指定密钥或证书文件格式:PEM(默认),DER 或 PKCS12
     
  6)Display cardinformation
   显示智能卡信息
    $pkcs15-tool -D
    注:-D,--dump -- 输出所有智能卡对象信息
   
   显示的信息内容中会多出如下一项:
    X.509Certificate [Certificate]
       Object Flags  : [0x2], modifiable
       Authority     : no
       Path         : 3f0050153101
       ID           :d2521ca08e20331c970de35bb5cbedd6853c6b8a
       GUID         :{999ede21-3c25-b85f-0568-dc1fe972440d}
       Encoded serial : 02 0900938F37138D3734D9
      
################################################################################
以下内容整理自:http://www.gooze.eu/howto/smartcard-quickstarter-guide/pkcs11-testing-suite
B.OpenSC pkcs11 testingsuite
  OpenSC pkcs11测试套件
  
  It will test each RSA keyand X.509 certificate. 
  将测试每一个 RSA 密钥和 X.509证书。
  
  Of course, this is anon-destructive test.
 当然,这不是破坏性的测试。
  
  $ pkcs11-tool --login--test --module /usr/local/lib/opensc-pkcs11.so
原创地址:http://blog.sina.com.cn/s/blog_4c451e0e0101765e.html
0 0
原创粉丝点击