美好的一天!我正在尝试根据来自客户端的 post 请求的十六进制字符串创建公钥 ObjectHandle
。
我正在根据文档进行操作,但它返回给我 CKR_ATTRIBUTE_TYPE_INVALID
异常。
完整的异常信息:Net.Pkcs11Interop.Common.Pkcs11Exception: 'Method C_CreateObject returned CKR_ATTRIBUTE_VALUE_INVALID'
内部异常为空
你能帮我弄清楚我做错了什么吗?
这是我的代码:
using (Pkcs11 pkcs11 = new Pkcs11(Settings.RutokenEcpDllDefaultPath, AppType.MultiThreaded))
{
Slot slot = GetSlot(pkcs11);
// This public key comes from client
// But i put it here to show value. Maybe value is a reason. I'm not sure,
// but i hope you will help me
var hexString = "1c:ec:2d:4a:b3:51:51:07:f7:c4:f6:d9:09:a3:06:73:c2:06:42:7f:b2:11:fd:be:ad:12:5c:22:b9:df:cb:e5:08:7c:7c:48:a6:af:92:67:d3:56:63:29:0c:9e:1a:4a:0e:d1:08:d8:7a:28:61:bd:da:ed:be:aa:49:84:f2:64";
hexString = hexString.Replace(":", string.Empty);
var publicKeyValue = ConvertUtils.HexStringToBytes(hexString);
using (Session session = slot.OpenSession(SessionType.ReadWrite))
{
session.Login(CKU.CKU_USER, Settings.TokenPin);
List<ObjectAttribute> objectAttributes = new List<ObjectAttribute>();
objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PUBLIC_KEY));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_GOSTR3410));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, "Verification Key"));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_VERIFY, true));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_VALUE, publicKeyValue));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_PRIVATE, false));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_GOSTR3410_PARAMS, new byte[] { 0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x23, 0x00 }));
// Create object
ObjectHandle objectHandle = session.CreateObject(objectAttributes);
}
}
GOST 公钥无法导入到 ruToken - 请参阅 docs在框中注明。
只是改变
objectAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, **true**));
到
objectAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, **false**));
作为 token 对象的公钥只能作为 C_GenerateKeyPair
调用的结果来“创建”。
我是一名优秀的程序员,十分优秀!