我跟着这个例子:https://pkcs11interop.net/doc/_high_level_a_p_i_2_24__wrap_and_unwrap_key_test_8cs-example.html
使用rsa键包装对称密钥,它工作。
我想要实现的是包装非对称密钥(rsa私钥)。我所做的只是用私钥的ObjectHandle替换“secretKey”变量。但是,每次调用Wrapkey函数时,我都会得到CKR_GENERAL_ERROR。
有人可以解释为什么这不起作用?我在规范中找不到任何阻止包装非对称密钥的东西。
使用以下属性生成私钥:
List<ObjectAttribute> privateKeyAttributes = new List<ObjectAttribute>();
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_PRIVATE, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ID, ConvertUtils.HexStringToBytes(id)));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_SENSITIVE, false));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_DECRYPT, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_SIGN, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_SIGN_RECOVER, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_EXTRACTABLE, true));
最好的祝福,
您似乎尝试使用CKM_RSA_PKCS
mechanim来包装RSA私钥。 PKCS#11 v2.20 specification国家的第12.1.6章:
PKCS#1 v1.5 RSA机制,表示为
CKM_RSA_PKCS
,是一种基于RSA公钥密码系统的多用途机制,以及最初在PKCS#1 v1.5中定义的块格式。...
此机制可以包装和解包任何适当长度的密钥。当然,特定令牌可能无法打包/解包它支持的每个适当长度的密钥。对于包装,加密操作的“输入”是包装的密钥的
CKA_VALUE
属性的值;同样适用于展开。
PKCS#11规范仅对对称密钥使用术语密钥。此外,CKA_VALUE
属性对RSA私钥无效。这不行。
您最好的选择是查阅设备/库的文档,并选择适合您需求的不同包装机制。