C# 如何在网络Luna HSM上设置/创建AES密钥的密钥值?

C# 如何在网络Luna HSM上设置/创建AES密钥的密钥值?,c#,cryptography,pkcs#11,hsm,pkcs11interop,C#,Cryptography,Pkcs#11,Hsm,Pkcs11interop,试图创建密钥时;我被告知不可能创建自己的键值,它们必须生成/展开到HSM上。如果这不是真的,我已经附加了两种尝试/方法,以防我遗漏了什么。任何帮助都将不胜感激 环境:网络Luna K6 5.1 库:PKCS11Interop.Net 尝试1:使用创建对象 错误:CKR\u模板不一致 string keyLabel = "CustomSecretKey" byte[] Value = HexStringToByteArray(value); privateKey = new List<Ob

试图创建密钥时;我被告知不可能创建自己的键值,它们必须生成/展开到HSM上。如果这不是真的,我已经附加了两种尝试/方法,以防我遗漏了什么。任何帮助都将不胜感激

环境:网络Luna K6 5.1

库:PKCS11Interop.Net

尝试1:使用创建对象 错误:CKR\u模板不一致

string keyLabel = "CustomSecretKey"
byte[] Value = HexStringToByteArray(value);

privateKey = new List<ObjectAttribute>
                                {
                                   //PKCS11V2.20 Common Attributes Defined @ 12.12.2
                                   new ObjectAttribute(CKA.CKA_LABEL, keyLabel),
                                   new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_SECRET_KEY),
                                   new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_AES),
                                   new ObjectAttribute(CKA.CKA_TOKEN, true),
                                   new ObjectAttribute(CKA.CKA_ENCRYPT, true),
                                   new ObjectAttribute(CKA.CKA_PRIVATE, true), 
                                   new ObjectAttribute(CKA.CKA_DECRYPT, true), 
                                   new ObjectAttribute(CKA.CKA_VALUE_LEN, 32),
                                   new ObjectAttribute(CKA.CKA_VALUE, Value)    

                               };

session.CreateObject(privateKey);

我是否应该生成另一个密钥并使用该密钥进行解包?

显然,在与Gemalto进行了交谈并发表了一些评论之后,这在非protectserver系列网络HSM上是无法实现的。该过程对于protect server HSM非常有效(他们现在有一个protect server网络HSM)。更新我的解包裹过程以使用带IV的CBC模式后,我能够创建密钥,但不能使用指定的密钥值

更新:在示例中添加了GenerateKeyPair函数

我能够使用以下代码将AES密钥“加载”到HSM中:

//创建Pkcs11Interop库使用的工厂
PKCS11InteropFactorys工厂=新的PKCS11InteropFactorys();
//加载非托管PKCS#11库
使用(IPkcs11Library pkcs11Library=Factorys.Pkcs11LibraryFactory.LoadPkcs11Library(Factorys,options.PKCS11LibraryPath,AppType.SingleThreaded))
{
//查找存在令牌的第一个插槽
ISlot slot=pkcs11Library.GetSlotList(SlotsType.WithTokenPresent)[0];
//开放RW会话
使用(ISession session=slot.OpenSession(SessionType.ReadWrite))
{
//以普通用户身份登录
登录(CKU.CKU_用户,options.Pin);
//生成非对称密钥加密密钥对
IObjectHandle ASYMKEYENCPYTIONPublicKey=null;
IObjectHandle asymKeyEncryptionPrivateKey=null;
生成密钥对(会话,out asymkeyencryptionpublickey,out asymKeyEncryptionPrivateKey,“KEK”);
//从文件中加载aes密钥
//使用以下命令生成密钥:“openssl rand-out aes256-forImport.key 32”
byte[]sourceKey=File.ReadAllBytes(options.aesKeyPath);
WriteLine($“加载的AES密钥:{BitConverter.ToString(sourceKey)}”);
//指定包装机制
IMechanism asymMechanism=session.Factories.MechanismFactory.Create(CKM.CKM\u RSA\u PKCS);
//加密文件中的密钥
byte[]encryptedKey=session.Encrypt(非对称机制、非对称加密公钥、源密钥);
//定义展开关键点的属性
List objectAttributes=new List();
Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_类,CKO.CKO_密钥));
Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_KEY_TYPE,CKK.CKK_AES));
Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_ENCRYPT,true));
Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_DECRYPT,true));
Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_EXTRACTABLE,options.markExtractable));
Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_标记,true));
Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_标签,$“在{DateTime.Now}打开”);
//对称展开密钥
IOObjectHandle SYUNWRAPDKEY=会话.UnwrapKey(非对称机制、非对称加密私钥、加密密钥、对象属性);
}
}
-----
/// 
///生成不对称密钥对。
/// 
///用户已登录的读写会话
///公钥对象句柄的输出参数
///私钥对象句柄的输出参数
public static void GenerateKeyPair(ISession会话,out IObjectHandle publicKeyHandle,out IObjectHandle privateKeyHandle,字符串标签=“”)
{
//CKA_ID属性用于区分同一主题持有的多个密钥对
字节[]ckaId=session.generateradom(20);
//准备新公钥的属性模板
List publicKeyAttributes=新列表();
////Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_标记,true));
Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_PRIVATE,false));
Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_LABEL,$“{LABEL}public”);
Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_ID,ckaId));
Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_ENCRYPT,true));
Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_VERIFY,true));
Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA\u VERIFY\u RECOVER,true));
Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_WRAP,true));
Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_module_BITS,2048));
Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_PUBLIC_index,新字节[]{0x01,0x00,0x01}));
//准备新私钥的属性模板
List privateKeyAttributes=新列表();
////添加(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_标记,true));
using (Pkcs11 pkcs11 = new Pkcs11(HSM.Instance.vendorDLLPath, AppType.MultiThreaded))
            {
                List<Slot> slots = pkcs11.GetSlotList(SlotsType.WithTokenPresent); //Checks to see what slots have token on them.
                Slot slot = slots[HSM.Instance.activeSlot];
                using (Session session = slot.OpenSession(SessionType.ReadWrite)) // Open RW session
                {
                    session.Login(CKU.CKU_USER, HSM.Instance.loginPass); // Login as normal user   
                    try
                    {
                        byte[] Value = HexStringToByteArray(value);

                        if (!string.IsNullOrEmpty(keyLabel))
                        {

                            List<ObjectAttribute> wrapKey = new List<ObjectAttribute>
                            {
                            new ObjectAttribute(CKA.CKA_LABEL, "TempWrapKey"),
                            new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_SECRET_KEY),
                            new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_AES),
                            new ObjectAttribute(CKA.CKA_TOKEN, true),
                            new ObjectAttribute(CKA.CKA_PRIVATE, true),
                            new ObjectAttribute(CKA.CKA_ENCRYPT, true),
                            new ObjectAttribute(CKA.CKA_DECRYPT, true),
                            new ObjectAttribute(CKA.CKA_SENSITIVE, true),
                            new ObjectAttribute(CKA.CKA_VERIFY, true),
                            new ObjectAttribute(CKA.CKA_SIGN, true),
                            new ObjectAttribute(CKA.CKA_WRAP, true),
                            new ObjectAttribute(CKA.CKA_UNWRAP, true),
                            new ObjectAttribute(CKA.CKA_DERIVE, false),
                            new ObjectAttribute(CKA.CKA_EXTRACTABLE, false),
                            new ObjectAttribute(CKA.CKA_VALUE_LEN, 32)

                            };
                            ObjectHandle WrappingKey = session.GenerateKey(new Mechanism(CKM.CKM_AES_KEY_GEN), wrapKey);

                            byte[] keyValueBytes = session.Encrypt(new Mechanism(CKM.CKM_AES_ECB), WrappingKey, Value);

                            List<ObjectAttribute> privateKeyAttributes = new List<ObjectAttribute>
                            {

                                new ObjectAttribute(CKA.CKA_LABEL, keyLabel),
                                new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_SECRET_KEY),
                                new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_AES),
                                new ObjectAttribute(CKA.CKA_TOKEN, true),
                                new ObjectAttribute(CKA.CKA_PRIVATE, true),
                                new ObjectAttribute(CKA.CKA_ENCRYPT, true),
                                new ObjectAttribute(CKA.CKA_DECRYPT, true),
                                new ObjectAttribute(CKA.CKA_SENSITIVE, true),
                                new ObjectAttribute(CKA.CKA_VERIFY, true),
                                new ObjectAttribute(CKA.CKA_SIGN, true),
                                new ObjectAttribute(CKA.CKA_WRAP, true),
                                new ObjectAttribute(CKA.CKA_UNWRAP, true),
                                new ObjectAttribute(CKA.CKA_DERIVE, false),
                                new ObjectAttribute(CKA.CKA_EXTRACTABLE, false),
                                new ObjectAttribute(CKA.CKA_VALUE_LEN, 32)

                            };


                            session.UnwrapKey(new Mechanism(CKM.CKM_AES_ECB), WrappingKey, keyValueBytes, privateKeyAttributes);
                            session.DestroyObject(WrappingKey);
                            response.ErrorText = "Key Created?";
12:02:28 03696-5152:STRTOpenSession {Slot=1 Flgs=6 }
12:02:28 03696-5152:FINIOpenSession CKR_OK(1609ms) {Sesn=1 }

12:02:28 03696-5152:STRTLogin {Sesn=1 User=1 PIN="********" }
12:02:28 03696-5152:FINILogin CKR_OK(1734ms) {}

12:02:28 03696-5152:STRTGenerateKey {Sesn=1 Mech=(CKM_AES_KEY_GEN,"") AttrList={CKA_CLASS="04000000" CKA_KEY_TYPE="1f000000" CKA_TOKEN="01" CKA_ENCRYPT="01" CKA_LABEL="TempWrapKey" CKA_CLASS="04000000" CKA_KEY_TYPE="1f000000" CKA_TOKEN="01" CKA_ENCRYPT="01" CKA_PRIVATE="01" CKA_DECRYPT="01" CKA_VALUE_LEN="20000000" CKA_WRAP="01" CKA_UNWRAP="01" CKA_SENSITIVE="01" CKA_SIGN="01" CKA_DERIVE="00" CKA_EXTRACTABLE="00" } }
12:02:29 03696-5152:FINIGenerateKey CKR_OK(1750ms) {Obj=2000001 }

12:02:29 03696-5152:STRTEncrypt_Init {Sesn=1 Mech=(CKM_AES_ECB,"") Obj=2000001 }
12:02:29 03696-5152:FINIEncrypt_Init CKR_OK(1750ms) {}

12:02:29 03696-5152:STRTEncrypt {Sesn=1 "23724cf301e85a53fc3f5aea6384c16e" pusEncryptedDataLen=0xf91ddca0 *pusEncryptedDataLen=0 }
12:02:29 03696-5152:FINIEncrypt CKR_OK(1750ms) {"" pusEncryptedDataLen=0xf91ddca0 *pusEncryptedDataLen=16 }

12:02:29 03696-5152:STRTEncrypt {Sesn=1 "23724cf301e85a53fc3f5aea6384c16e" pusEncryptedDataLen=0xf91ddca0 *pusEncryptedDataLen=16 }
12:02:29 03696-5152:FINIEncrypt CKR_OK(1750ms) {"2e151cc889470864b5fb5a24146fecb2" pusEncryptedDataLen=0xf91ddca0 *pusEncryptedDataLen=16 }

12:02:29 03696-5152:STRTUnwrapKey {Sesn=1 Mech=(CKM_AES_ECB,"") Obj=2000001 "2e151cc889470864b5fb5a24146fecb2" AttrList={CKA_LABEL="Key2372" CKA_CLASS="04000000" CKA_KEY_TYPE="1f000000" CKA_TOKEN="01" CKA_PRIVATE="01" CKA_ENCRYPT="01" CKA_DECRYPT="01" CKA_SENSITIVE="01" CKA_VERIFY="01" CKA_SIGN="01" CKA_WRAP="01" CKA_UNWRAP="01" CKA_DERIVE="00" CKA_EXTRACTABLE="00" CKA_VALUE_LEN="20000000" } }
12:02:29 03696-5152:FINIUnwrapKey ***CKR_WRAPPED_KEY_INVALID***(1750ms) {Obj=0 }

12:02:29 03696-5152:STRTCloseSession {Sesn=1 }
12:02:29 03696-5152:FINICloseSession CKR_OK(1765ms) {}