opensc工具的基本用法

################################################################################
以下内容整理自:http://www.gooze.eu/howto/smartcard-quickstarter-guide/smart-card-initialization
1.Install a smart card reader and initialize a cryptographic card.
  安装智能卡读卡器并初始化一张加密卡。
  
  1)Plug-in your smartcard reader and insert the smartcard.
    or 
    Plug-in the USB token.
    为你的电脑插上 USB 型智能卡 或 智能卡读卡器(并插好智能卡)。
  
  2)You will need to run terminal on Linux.
    在 Linux 上运行 终端程序。
    
  3)Smartcard recognition
    智能卡识别
    Each smart card has a special footprint, which is called the ATR. Query the ATR of the card:

    $ opensc-tool --atr
    Using reader 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
    Using reader with a card: Feitian SCR301 00 00
    29 27 22 01 15 13 11 09 )'".....
    注:--serial -- 打印输出智能卡序列号

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

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

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

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

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

    Choose a PUK code with 4 to 8 digits.
    in the below 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 card information
    显示智能卡信息
    $ pkcs15-tool -D
    注:-D, --dump -- 输出所有智能卡对象信息
    
    If you are using the Feitian PKI card, this displays:
    如果你使用的是 飞天诚信 PKI 卡的话,显示如下信息:
    
    In this case, the PIN code has ID 01. 
    在这个案例中,PIN 码的 ID 是 01 。
    
    You will use this ID later on in the guide.
    在后面的指导中,你将用到这个 ID 。
    
    Using reader with a card: Feitian SCR301 00 00
    PKCS#15 Card [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 supported mechanisms by your smart card
    查询你智能卡所支持的机制
  
    To query mechanisms supported by your smart card/token, 
    you may use 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 PIN codes
    列出 PIN 码
    $ pkcs15-tool --list-pins
    注:--list-pins -- 列出所有 PIN 码
    
  2)Changing PIN code
    修改 PIN 码
    $ pkcs15-tool --change-pin
    注:--change-pin -- 改变 PIN 或 PUK 码
    
    注:如果旧密码输出错误,将显示可尝试的剩余次数:
    PIN code incorrect; tries left: 2
    
  3)Unlock pin code
    解锁 PIN 码
    $ pkcs15-tool --unblock-pin
    注:-u, --unblock-pin - 解锁 PIN 码
    
  4)PIN code and PUK code lost
    PIN 码和 PUK 码全忘了
    Using the Feitian PKI, there is nothing you can do.
    使用 飞天诚信 PKI 智能卡,对于这种情况,没有什么办法解决的。
    You will 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 smart card may result in losing data
警告:在智能卡上产生 RSA 密钥对可能导致丢失数据

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

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

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

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

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

################################################################################
以下内容整理自:http://www.gooze.eu/howto/smartcard-quickstarter-guide/scenario-2-creating-a-self-signed-certificate-on-computer
3.Generate and transfer RSA keys to the cryptographic card.
  产生并且传递 RSA 密钥到加密卡中。
  
  1)Generate an RSA key on your computer
    在你的计算机上产生一个 RSA 密钥
    $ openssl genrsa 2048 > key.pem
    注:genrsa    -- 生成 RSA 密钥
      > key.pem -- > 为 Linux 的标准输出重定向到一个文件,文件名中的 key 是可以任意起的,但要方便理解。
                   key.pem 文件中包含了公钥与私钥,用法标明为:公钥 -> 验证,私钥 -> 签名
    
  2)Transfer the key to smartcard/token:
    传递密钥到你的智能卡或 USB 型智能卡中:
    $ pkcs15-init --store-private-key key.pem --auth-id 01 --pin 0000
    注:-S, --store-private-key -- 存储指定文件中的私钥
      -a, --auth-id           -- 使用或创建指定 ID 的 PIN 码
      --pin                   -- 指定 PIN 码
    
  3)Display card information
    显示智能卡信息
    $ pkcs15-tool -D
    注:-D, --dump -- 输出所有智能卡对象信息
    
    显示的信息内容中会多出如下两项:
    Private RSA 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}

    Public RSA 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 to the cryptographic card.
  产生并且传递 X.509 证书到加密卡中。

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

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

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

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

    $ openssl req -new -x509 -keyout CACert.key -out CACert.crt -config openssl.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's RSA key file, use OpenSSL command: 
    使用 OpenSSL 命令来创建一个用户 RSA 密钥文件:
     
    $ openssl genrsa -out privateKey.key 1024
    注:genrsa -- 生成 RSA 密钥
      -out   -- 指定输出的文件名,如:privateKey.key
      1024   -- 指定密钥长度,默认为:512

  3)To create a customer's CSR file, use OpenSSL command: 
    使用 OpenSSL 命令来创建一个用户 CSR 证书签名请求文件:
     
    $ openssl req -new -key privateKey.key -out certificate.csr -config openssl.cnf
    要输入以下信息方可产生证书:
    Country Name (2 letter code) [AU]:CN
    国家名
    State or 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
    部门名
    Common Name (eg, YOUR name) []:NiuShengchao
    个人名
    Email Address []:[email protected]
    邮件名
     
    注:req     -- 用于生成和处理 PKCS#10 证书请求
      -new    -- 生成新的证书请求以及私钥,默认为 1024 比特(bits)
      -key    -- 由 2) 生成的 RSA 密钥文件
      -out    -- 指定输出的文件名,如:certificate.csr
      -config -- 指定证书请求模板文件,如:OpenSSL 自带的 openssl.cnf ,一般在 /usr/local/ssl 目录下

  4)To create a customer's certificate file, use OpenSSL command: 
    使用 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 -cert CACert.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 file with 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 file information, 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#12 file, type:
    导入 PKCS#12 文件,敲入如下命令:
    $ pkcs15-init --store-private-key key-file.pkcs12 --format pkcs12 --id 85746946F3463ED0158B5B6BF9922C09F3D3C9A4 --auth-id 01 --pin 0000
    注:-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 card information
    显示智能卡信息
    $ pkcs15-tool -D
    注:-D, --dump -- 输出所有智能卡对象信息
    
    显示的信息内容中会多出如下两项:
    Private RSA 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.509 Certificate [NiuShengchao]
        Object Flags   : [0x2], modifiable
        Authority      : no
        Path           : 3f0050153100
        ID             : 85746946f3463ed0158b5b6bf9922c09f3d3c9a4
        GUID           : {8d73029a-473e-2d4d-593e-e66a106ca9ca}
        Encoded serial : 02 01 01
        
    另:As you notice, this command transfers private RSA key, X.509 certificates.
      同样地你可以注意到,7) 传递了私钥和证书到智能卡中。
      
      There is no need to import the RSA public key as it can be derived from RSA 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 embedded processor
  使用智能卡的嵌入式处理器来创建一个 RSA 密钥对
  
  Warning: using this method, there is no way to backup your keys. 
  警告:    使用该方法产生的密钥,将不能导出备份。
           
           Therefore, it is not usable for encryption of sensitive data. 
           因此,由该方法产生的密钥不能用于需慎重处理的数据加密。
           
           If you lose the smartcard or token, 
           you will not be able to recover your data, 
           which will be lost FOREVER.
           如果你丢失了该智能卡或 USB 型智能卡,你将无法解密你的数据,意味着此数据永远丢失!
           
  1)Creating an RSA keypair using the embedded processor
    使用智能卡的嵌入式处理器来创建一个 RSA 密钥对
    
    $ pkcs15-init --generate-key rsa/2048 --auth-id 01 --pin 0000 -u sign,decrypt
    注:-G, --generate-key -- 在智能卡上产生并存储一个新密钥,rsa/2048 指出产生 2048 比特(bits) rsa 密钥对
      -a, --auth-id      -- 使用或创建指定 ID 的 PIN 码
      --pin              -- 指定 PIN 码
      -u, --key-usage    -- 指定智能卡上产生新密钥的用法

  2)Display card information
    显示智能卡信息
    $ pkcs15-tool -D
    注:-D, --dump -- 输出所有智能卡对象信息
    
    显示的信息内容中会多出如下两项:
    Private RSA 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}

    Public RSA 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 using MD5
  用 MD5 签名与验签
  
  1)Erase your smartcard if needed:
    如果需要擦除你的智能卡:

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

  2)Initialize your smartcard with PIN 0000, 
    PUK 111111 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 keypair using the embedded processor
    使用智能卡的嵌入式处理器来创建一个 RSA 密钥对
    
    $ pkcs15-init --generate-key rsa/1024 --auth-id 01 --pin 0000 -u sign,decrypt
    注:-G, --generate-key -- 在智能卡上产生并存储一个新密钥,rsa/2048 指出产生 2048 比特(bits) rsa 密钥对
      -a, --auth-id      -- 使用或创建指定 ID 的 PIN 码
      --pin              -- 指定 PIN 码
      -u, --key-usage    -- 指定智能卡上产生新密钥的用法

  4)Query available public RSA key:
    查询所有可得到的公钥:
   
   $ pkcs15-tool --list-public-keys
   注:--list-public-keys - 列出所有公钥
   
   执行成功显示下列信息:(注意公钥ID)
   Using reader with a card: Feitian SCR301 00 00
   Public RSA 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-key 90da6b57300e07606bfffbc73e944ca43f9138d0 -o puk.pem
    注:--read-public-key -- 读出指定 ID 的公钥
      -o, --output      -- 输出结果到指定文件

  6)先创建一个名为 somemessage 的文本文件,test text 为里面的内容:
    
    $ echo test 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 -o signed
    注:-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) 的输出文件
      <          -- 重定向标准输入到指定的文件
      
   执行成功显示下列信息:
   Verified OK

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

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

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

    另:encrypted   -- 文件名,包含加密后的数据,上述指令的输出物
      somemessage -- 文件名,包含将要做数据加密的信息
      puk.pem     -- 文件名,包含公钥
  
  2)使用 pkcs15-crypt 工具
  
    $ pkcs15-crypt -vvvvvvvvv -c --pkcs1 -i encrypted -o decrypted
    
    注:-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 generate RSA keys using the embedded processor, not 3DES keys.
  OpenSC 可以使用智能卡的嵌入式处理器来产生 RSA 密钥,而非 3DES 密钥。
  
  To use 3DES keys, we need to generate and import them to smartcard/token.
  要使用 3DES 密钥,我们需要先产生,然后导入它们到知能卡或 USB 型智能卡中。
  
  1)Generate a 3des key using OpenSSL:
    用 OpenSSL 产生一个 3DES 密钥:
    
    $ openssl rand 24 -out 3des.key
    注:rand -- 生成随机数
      -out -- 输出结果到指定文件,文件名任意起,但要便于理解
      
  2)Query available slots:
    查询可用的槽:
    
    $ 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 as an independant object and write private flag to forbid the user from 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 码(如果是用在脚本文件:要当心泄密!)
      
    显示下列信息表示执行成功:
    Using slot with ID 0x1
    Created Data Object:
    Data object 135014352
      label:          '3deskey'
      application:    '3deskey'
      app_id:        
      flags:           modifiable private

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

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

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

    显示下列信息表示执行成功:
    Using reader 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 on smart 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 on card:
    列出智能卡的所有数据对象:
    
    $ pkcs15-tool --list-data-objects
    注:-C, --list-data-objects -- 列出所有的数据对象
    
    显示的信息内容中会多出如下一项:
    Data object 'thismessage'
        applicationName: pkcs15-init
        Path:            3f0050153400
        Auth ID:         01

  5)To readout the file:
    读出这个文件:
    
    $ pkcs15-tool --read-data-object "thismessage" -o thatmessage
    注:-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 smartcard cryptographic engine:
  配置 OpenSSL 使用智能卡加密引擎:
  
  For safety, 
  we will be using OpenSSL with engine_pkcs11 to generate certificate using the smart card cryptographic engine.
  为了保险起见,
  我们将让 OpenSSL 使用 engine_pkcs11 智能卡加密引擎来创建证书。
  
  1)Enter OpenSSL command prompt:
    进入 OpenSSL 命令提示行状态:
    
    $ openssl
    
  2)Copy the following line and paste it in OpenSSL command prompt:
    拷贝如下命令到 OpenSSL 命令提示行:(不含 OpenSSL> )
    
    OpenSSL> engine dynamic -pre SO_PATH:/usr/local/lib/engines/engine_pkcs11.so -pre ID:pkcs11 -pre LIST_ADD:1 -pre LOAD -pre MODULE_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 will additionally display each command's description
                      -vv 将同时显示每条命令的描述
                      
                      -vvv will also add the input flags for each command
                      -vvv 将同样为每条命令添加输入标志
                      
                      -vvvv will also show internal input flags
                      -vvvv 将同样显示内部输入标志
                      
        -c          - for each engine, also list the capabilities
                      同样为每个引擎列出它的性能
                      
        -t[t]       - for each engine, check that they are really available
                      检测可用的引擎
                      
                      -tt will display error trace for unavailable engines
                      -tt 将为不可用的引擎显示错误跟踪
                      
        -pre  - runs command '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 also provided)
                      在加载引擎之后运行 指令 以供引擎之用(如果 -t 参数选项同样被提供的话,那么它刚刚使用过)
                      
        NB: -pre and -post will be applied to all ENGINEs supplied on the command line, 
            or all supported ENGINEs if none are specified.
        注意:-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 not exit the OpenSSL command prompt.
    执行 2) 后不要退出 OpenSSL 命令提示行。
     
  3)Query available slots:
    查询可用的槽:
    
    $ 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 RSA key:
    $ pkcs15-tool --list-keys
    注:-k, --list-keys -- 列出所有私钥
    
    执行成功显示下列信息:
    Private RSA 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-signed certificate
    创建一个自签名的证书
        
    We are going to generate a certificate using this RSA key.
    我们要使用 RSA 密钥产生一个证书。
    
    OpenSSL> req -engine pkcs11 -new -key slot_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-signed certificate was saved to cert.pem. The certificate can be verified against 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 on card:
    我们存储证书到智能卡上去:
    
    $ pkcs15-init --store-certificate cert.pem --auth-id 01 --id d2521ca08e20331c970de35bb5cbedd6853c6b8a --format pem
    注:-X, --store-certificate -- 存储一个指定的 x.509 证书
      -a, --auth-id           -- 使用或创建指定 ID 的 PIN 码
      -i, --id                -- 指定 密钥 或 证书 ID,此处指定将存储的证书 ID 与产生它的密钥 ID 相同
      -f, --format            -- 指定密钥或证书文件格式:PEM(默认),DER 或 PKCS12
      
  6)Display card information
    显示智能卡信息
    $ pkcs15-tool -D
    注:-D, --dump -- 输出所有智能卡对象信息
    
    显示的信息内容中会多出如下一项:
    X.509 Certificate [Certificate]
        Object Flags   : [0x2], modifiable
        Authority      : no
        Path           : 3f0050153101
        ID             : d2521ca08e20331c970de35bb5cbedd6853c6b8a
        GUID           : {999ede21-3c25-b85f-0568-dc1fe972440d}
        Encoded serial : 02 09 00938F37138D3734D9
        
################################################################################
以下内容整理自:http://www.gooze.eu/howto/smartcard-quickstarter-guide/pkcs11-testing-suite
B.OpenSC pkcs11 testing suite
  OpenSC pkcs11 测试套件
  
  It will test each RSA key and X.509 certificate. 
  将测试每一个 RSA 密钥和 X.509 证书。
  
  Of course, this is a non-destructive test.
  当然,这不是破坏性的测试。
  
  $ pkcs11-tool --login --test --module /usr/local/lib/opensc-pkcs11.so


转载至:http://blog.sina.com.cn/s/blog_4c451e0e0101765e.html

你可能感兴趣的:(linux,网络,物联网)