Мой код для переноса и развертывания открытого и закрытого ключа
public void BasicWrapAndUnwrapKeyTest()
{
using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath, Settings.AppType))
{
// Find first slot with token present
Slot slot = Helpers.GetUsableSlot(pkcs11);
// Open RW session
using (Session session = slot.OpenSession (SessionType.ReadWrite))
{
// Login as normal user
session.Login(CKU.CKU_USER, Settings.NormalUserPin);
// Generate asymetric key pair
ObjectHandle publicKey = null;
ObjectHandle privateKey = null;
GenerateKeyPair(session, out publicKey, out privateKey);
// Generate wrapping key
ObjectHandle secretKey = GenerateKey(session);
// Generate random initialization vector
byte[] iv = session.GenerateRandom(8);
// Specify wrapping mechanism
Mechanism mechanism = new Mechanism(CKM.CKM_DES3_CBC, iv);
// Wrap private key
byte[] wrappedKey = session.WrapKey(mechanism, secretKey, privateKey);
// Define attributes for unwrapped key
List<ObjectAttribute> objectAttributes = new List<ObjectAttribute>();
objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_RSA));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_PRIVATE, true));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, "unwrapped_private"));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_SENSITIVE, true));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_DECRYPT, true));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_SIGN, true));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_SIGN_RECOVER, true));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_UNWRAP, true));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_EXTRACTABLE, true));
// Unwrap private key
ObjectHandle unwrappedKey = session.UnwrapKey(mechanism, secretKey, wrappedKey, objectAttributes);
session.DestroyObject(privateKey);
session.DestroyObject(publicKey);
session.DestroyObject(secretKey);
session.DestroyObject(unwrappedKey);
session.Logout();
}
}
}
После запуска этого кода я получил следующую ошибку:
Message = "Метод C_WrapKey возвратил CKR_MECHANISM_INVALID"