在Pkcs11Interop.PDF库中标识私钥

时间:2014-04-05 16:47:18

标签: c# pdf itext pkcs#11 pkcs11interop

我正在尝试使用Pkcs11Interop.PDF扩展程序设置示例应用程序。我很遗憾地获得了System.ArgumentNullException设置ckaId。什么会在这里工作?我在这里尝试了不同的数字,这些数字都给了我一个System.ArgumentOutOfRangeException

安装程序使用VirtualCryptoki-64-1.0.6.7.exe应用程序来模拟智能卡。

令人不安的电话是:

pkcs11RsaSignature = new Pkcs11RsaSignature(libraryPath, tokenSerial, tokenLabel, pin, ckaLabel, ckaId, hashAlgorithm);

我使用的哈希算法是:

HashAlgorithm hashAlgorithm = HashAlgorithm.SHA256;

使用pkcs11-tool后更新:

虚拟卡的内容是:

    C:\Program Files\OpenSC Project\OpenSC\tools>pkcs11-tool.exe --module "C:\windows\System32\vcki.dll" --list-slots --list-objects --login --pin 1234
Available slots:
Slot 0 (0xd47db04d): Virtual Smart Card Reader
  token label:   Virtual SC-A0101010101
  token manuf:   Cryptware
  token model:   VirtualSmartCard
  token flags:   rng, login required, PIN initialized, token initialized, other flags=0x200
  serial num  :
Using slot 0 with a present token (0xd47db04d)
Certificate Object, type = X.509 cert
  label:      ibisit
  ID:         4a656e73204b6175666d616e6e
Public Key Object; RSA 1024 bits
  label:      ibisit
  ID:         4a656e73204b6175666d616e6e
  Usage:      encrypt, verify
Private Key Object; RSA
  label:      ibisit
  ID:         4a656e73204b6175666d616e6e
  Usage:      decrypt, sign
warning: PKCS11 function C_GetAttributeValue(ALWAYS_AUTHENTICATE) failed: rv = CKR_ATTRIBUTE_TYPE_INVALID (0x12)

Public Key Object; RSA 1024 bits
  label:      ibisit
  ID:         4a656e73204b6175666d616e6e
  Usage:      encrypt, verify
Private Key Object; RSA
  label:      ibisit
  ID:         4a656e73204b6175666d616e6e
  Usage:      decrypt, sign
warning: PKCS11 function C_GetAttributeValue(ALWAYS_AUTHENTICATE) failed: rv = CKR_ATTRIBUTE_TYPE_INVALID (0x12)

我使用的参数是:

string libraryPath = @"C:\Windows\System32\vcki.dll";
string tokenSerial = null;
string tokenLabel = @"Virtual SC-A0101010101";
string pin = @"1234";
string ckaLabel = @"ibisit";
string ckaId = "4a656e73204b6175666d616e6e";
HashAlgorithm hashAlgorithm = HashAlgorithm.SHA256;

我不幸地得到了Net.Pkcs11Interop.PDF.ObjectNotFoundException设置ckaId。更改引脚给了我一个不同的例外,所以我在这里定义访问正确的设备。

切换到SoftHSM后更新: jariq说这里有太多具有相同id的对象存在问题。不幸的是,虚拟密钥资源管理器不允许我删除任何对象,所以我切换到SoftHSM(我在开始时使用)。我正在获得带有标签的证书" ibis-it key"和id" A1B2"找不到"。我想这里的问题是这个虚拟卡只持有RSA密钥对而不是证书。

pkcs11工具的输出是:

C:\Program Files (x86)\OpenSC Project\OpenSC\tools>pkcs11-tool.exe --module "C:\SoftHSM\lib\libsofthsm.dll" --list-slots --list-objects --login --pin smart
Available slots:
Slot 0 (0x0): SoftHSM
  token label:   SoftHSM
  token manuf:   SoftHSM
  token model:   SoftHSM
  token flags:   rng, login required, PIN initialized, token initialized, other flags=0x40
  serial num  :  1
Using slot 0 with a present token (0x0)
Public Key Object; RSA 2048 bits
  label:      ibis-it key
  ID:         a1b2
  Usage:      verify
Private Key Object; RSA
  label:      ibis-it key
  ID:         a1b2
  Usage:      sign

问题是SoftHSM只导入PKCS#8(RSA)密钥对,因此这里永远不会有证书。我想你一直在使用RSA密钥对而不是证书。

1 个答案:

答案 0 :(得分:2)

传递给Net.Pkcs11Interop.PDF.Pkcs11RsaSignature类构造函数的参数标识以下内容:

  • 应使用哪个PKCS#11库(libraryPath)
  • 哪个令牌/智能卡存储私钥(tokenSerial和/或tokenLabel)
  • 应使用哪个私钥进行签名(ckaLabel和/或ckaId)
  • 在签名创建期间应使用哪种哈希算法(hashAlgorithm)

如果您知道应该使用哪个PKCS#11库来访问智能卡,那么您可以确定其余参数的正确值,即运行与{{捆绑在一起的 pkcs11-tool 实用程序3}}。请在下面找到确切的命令和为我的测试卡生成的输出(重要部分用粗体文字突出显示):

C:\Program Files (x86)\OpenSC Project\OpenSC\tools>pkcs11-tool.exe --module cardos11.dll --list-slots --list-objects --login --pin 11111111
Available slots:
Slot 0 (0x1): SCM Microsystems Inc. SCR33x USB Smart Card Reader 0
  token label        : Pkcs11Interop
  token manufacturer : www.atos.net/cardos
  token model        : CardOS V4.3B
  token flags        : rng, login required, PIN initialized, token initialized, other flags=0x800
  hardware version   : 102.63
  firmware version   : 200.8
  serial num         : 7BFF2737350B262C
Using slot 0 with a present token (0x1)
Private Key Object; RSA
  label:      John Doe
  ID:         ec5e50a889b888d600c6e13cb0fdf0c1
  Usage:      sign
Certificate Object, type = X.509 cert
  label:      John Doe
  ID:         ec5e50a889b888d600c6e13cb0fdf0c1

根据此输出,这些是此卡的各个参数的正确值:

  • libraryPath = “cardos11.dll”
  • tokenSerial =“7BFF2737350B262C”和/或tokenLabel =“Pkcs11Interop”
  • ckaLabel =“John Doe”和/或ckaId =“ec5e50a889b888d600c6e13cb0fdf0c1”

希望这有帮助。

更新ObjectNotFoundException:

您正在获取ObjectNotFoundException,因为有两个私钥,您的令牌中存储了完全相同的标签和ID,因此Pkcs11RsaSignature类无法确定应该使用哪个私钥来创建签名。只需删除或重命名其中一个就可以了。

SoftHSM的更新:

您可以使用softhsm.exe工具将PKCS#8私钥导入SoftHSM:

C:\SoftHSM\bin>softhsm.exe --import doe.key --slot 0 --label "John Doe" --pin 11111111 --id "ec5e50a889b888d600c6e13cb0fdf0c1"
The key pair has been imported to the token in slot 0.

您可以使用pkcs11-tool.exe工具将DER编码的X.509证书导入SoftHSM:

C:\SoftHSM\bin>"c:\Program Files (x86)\OpenSC Project\OpenSC\tools\pkcs11-tool.exe" --module libsofthsm.dll --login --pin 11111111 --write-object doe.der --type cert --label "John Doe" --id "ec5e50a889b888d600c6e13cb0fdf0c1"
Using slot 0 with a present token (0x0)
Created certificate:
Certificate Object, type = X.509 cert
  label:      John Doe
  ID:         ec5e50a889b888d600c6e13cb0fdf0c1

只需确保您将使用与私钥ID相同的ID导入证书。