我正在尝试使用扩展名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工具后进行更新:
虚拟卡的内容为:
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的对象。不幸的是,Virtual Key Explorer不允许我删除任何对象,所以我切换到了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密钥对一起使用,但没有与证书一起使用。
最佳答案
传递给Net.Pkcs11Interop.PDF.Pkcs11RsaSignature类的构造函数的参数标识以下内容:
应该使用哪个PKCS#11库(libraryPath)
哪个令牌/智能卡存储了私钥(tokenSerial和/或tokenLabel)
应该使用哪个私钥进行签名(ckaLabel和/或ckaId)
在签名创建期间应使用哪种哈希算法(hashAlgorithm)
如果您知道应使用哪个PKCS#11库来访问智能卡,则可以为其余参数确定正确的值,即运行与OpenSC middleware捆绑在一起的pkcs11-tool实用程序。请在下面找到确切的命令以及为我的测试卡生成的输出(重要部分以粗体突出显示):
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
Based on this output these are the correct values of individual parameters for this card:
- libraryPath="cardos11.dll"
- tokenSerial="7BFF2737350B262C" and/or tokenLabel="Pkcs11Interop"
- ckaLabel="John Doe" and/or ckaId="ec5e50a889b888d600c6e13cb0fdf0c1"
Hope this helps.
Update for ObjectNotFoundException:
You are getting ObjectNotFoundException because there are two private keys with the exactly same label and ID stored in your token and therefore Pkcs11RsaSignature class cannot be sure which one should be used for signature creation. Just delete or rename one of them and it should be working.
Update for SoftHSM:
You can import PKCS#8 private key to SoftHSM with softhsm.exe tool:
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。
关于c# - Pkcs11Interop.PDF库中私钥的标识,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22883775/