using System.Collections.Generic;
using NUnit.Framework;
namespace Net.Pkcs11Interop.Tests.HighLevelAPI
{
[TestFixture()]
public class _24_WrapAndUnwrapKeyTest
{
[Test()]
public void _01_BasicWrapAndUnwrapKeyTest()
{
using (IPkcs11Library pkcs11Library = Settings.Factories.Pkcs11LibraryFactory.LoadPkcs11Library(Settings.Factories, Settings.Pkcs11LibraryPath, Settings.AppType))
{
ISlot slot = Helpers.GetUsableSlot(pkcs11Library);
using (ISession session = slot.OpenSession(
SessionType.ReadWrite))
{
session.Login(
CKU.CKU_USER, Settings.NormalUserPin);
IObjectHandle publicKey = null;
IObjectHandle privateKey = null;
Helpers.GenerateKeyPair(session, out publicKey, out privateKey);
IObjectHandle secretKey = Helpers.GenerateKey(session);
IMechanism mechanism = session.Factories.MechanismFactory.Create(
CKM.CKM_RSA_PKCS);
byte[] wrappedKey = session.WrapKey(mechanism, publicKey, secretKey);
Assert.IsNotNull(wrappedKey);
List<IObjectAttribute> objectAttributes = new List<IObjectAttribute>();
objectAttributes.Add(session.Factories.ObjectAttributeFactory.Create(
CKA.CKA_CLASS,
CKO.CKO_SECRET_KEY));
objectAttributes.Add(session.Factories.ObjectAttributeFactory.Create(
CKA.CKA_KEY_TYPE,
CKK.CKK_DES3));
objectAttributes.Add(session.Factories.ObjectAttributeFactory.Create(
CKA.CKA_ENCRYPT,
true));
objectAttributes.Add(session.Factories.ObjectAttributeFactory.Create(
CKA.CKA_DECRYPT,
true));
objectAttributes.Add(session.Factories.ObjectAttributeFactory.Create(
CKA.CKA_DERIVE,
true));
objectAttributes.Add(session.Factories.ObjectAttributeFactory.Create(
CKA.CKA_EXTRACTABLE,
true));
IObjectHandle unwrappedKey = session.UnwrapKey(mechanism, privateKey, wrappedKey, objectAttributes);
Assert.IsTrue(unwrappedKey.ObjectId != CK.CK_INVALID_HANDLE);
session.DestroyObject(privateKey);
session.DestroyObject(publicKey);
session.DestroyObject(secretKey);
session.Logout();
}
}
}
}
}