PKCS#11 C_WrapKey returns CKR_GENERAL_ERROR

PKCS#11 C_WrapKey returns CKR_GENERAL_ERROR

我已按照示例进行操作: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 机制来包装 RSA 私钥。 PKCS#11 v2.20 specification 的第 12.1.6 章指出:

The PKCS#1 v1.5 RSA mechanism, denoted CKM_RSA_PKCS, is a multi-purpose mechanism based on the RSA public-key cryptosystem and the block formats initially defined in PKCS#1 v1.5.

...

This mechanism can wrap and unwrap any secret key of appropriate length. Of course, a particular token may not be able to wrap/unwrap every appropriate-length secret key that it supports. For wrapping, the "input" to the encryption operation is the value of the CKA_VALUE attribute of the key that is wrapped; similarly for unwrapping.

PKCS#11 规范仅对对称密钥使用术语秘密密钥。此外 CKA_VALUE 属性对 RSA 私钥无效。这行不通。

您最好的办法是参考您的 device/library 文档并选择适合您需要的不同包装机制。