【问题标题】:ECDH1_DERIVE issues with PCKS11interop and Safenet Network HSMPCKS11interop 和 Safenet Network HSM 的 ECDH1_DERIVE 问题
【发布时间】:2018-08-29 05:43:41
【问题描述】:

与金雅拓网络 HSM 斗争了一段时间,现在是我寻求专家帮助的时候了。我正在尝试从已知的公钥和存储在 HSM 上的私钥派生 ECDH1 密钥,并不断将 HSM 发送到某种恐慌模式,这需要我在每次调用派生密钥时重新开始通话之前重置它功能如下所述。有人指点吗?

static string PKCSLibraryPath = @"C:\Program Files (x86)\SafeNet\Protect Toolkit 5\Protect Toolkit C SDK\bin\hsm\cryptoki.dll";
    static Pkcs11 pkc = new Pkcs11(PKCSLibraryPath, AppType.SingleThreaded);

    public string HSM_Interaction(int SlotNumber, string KeyLabel, string Pubkey, string GUID)
    {

        List<Slot> slots = pkc.GetSlotList(SlotsType.WithTokenPresent);
        string pass = "1111";

        //convert putblic key to byte array
        byte[] data = Get_pub_Key(Pubkey);

        //convert password to byte array
        byte[] password = Encoding.ASCII.GetBytes(pass);

        //select correct HSM slot
        Slot S = slots[SlotNumber];

        using (Session Sesh = S.OpenSession(SessionType.ReadWrite))
        {

            Sesh.Login(CKU.CKU_USER, password);
            List<CKM> Mechs = S.GetMechanismList();
            ObjectHandle oPrivKeyObjectHandle;

            //setup search criteria for token
            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_EC));
            objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, KeyLabel));
            Sesh.FindObjectsInit(objectAttributes);
            List<ObjectHandle> oObjCollection = Sesh.FindObjects(1);
            Sesh.FindObjectsFinal();

            if (oObjCollection.Count > 0)
            {  
                oPrivKeyObjectHandle = oObjCollection[0];

                //set template for generated key
                var shared_secret_template = new List<ObjectAttribute>
                {
                    new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_SECRET_KEY),
                    new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_GENERIC_SECRET),
                    new ObjectAttribute(CKA.CKA_SENSITIVE, false),
                    new ObjectAttribute(CKA.CKA_EXTRACTABLE, true),
                    new ObjectAttribute(CKA.CKA_VALUE_LEN, (ulong)32)
                };


                var deriveAttributes = new List<ObjectAttribute>
                 {
                     new ObjectAttribute(CKA.CKA_TOKEN, false),
                     new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_SECRET_KEY),
                     new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_GENERIC_SECRET),
                     new ObjectAttribute(CKA.CKA_SENSITIVE, false),
                     new ObjectAttribute(CKA.CKA_EXTRACTABLE, true),
                     new ObjectAttribute(CKA.CKA_ENCRYPT, true),
                     new ObjectAttribute(CKA.CKA_DECRYPT, true),
                     new ObjectAttribute(CKA.CKA_WRAP, true),
                     new ObjectAttribute(CKA.CKA_UNWRAP, true),
                     new ObjectAttribute(CKA.CKA_VALUE_LEN, (ulong)32)
                };


                try
                {
                    //generate derived key

                    byte[] sd = null;
                    CkEcdh1DeriveParams par = new CkEcdh1DeriveParams((ulong)CKD.CKD_NULL, sd, data);
                    par.ToMarshalableStructure();
                    Mechanism m = new Mechanism(CKM.CKM_ECDH1_DERIVE, par);
                    Sesh.DeriveKey(m, oPrivKeyObjectHandle, deriveAttributes);
                    ObjectHandle SSOH = new ObjectHandle();

                    Sesh.GetAttributeValue(SSOH,deriveAttributes);

                }
                catch (Exception ex)
                {
                    string error = ex.Message;
                }
                finally
                {
                    Sesh.Logout();
                }

            }
        }
        return "";
    }
    private byte[] Get_SHA256(string text)
    {
        byte[] bytes = Encoding.UTF8.GetBytes(text);
        SHA256Managed hashstring = new SHA256Managed();
        byte[] hash = hashstring.ComputeHash(bytes);
        string hashString = string.Empty;
        foreach (byte x in hash)
        {
            hashString += String.Format("{0:x2}", x);
        }

        return hash;

    }

    private byte[] Get_pub_Key(string text)
    {
        byte[] Bytes = new byte[65];
        int startpos = 0;
        for (int i = 0; i < (text.Length / 2); i++)
        {
            byte b = Convert.ToByte(text.Substring(startpos, 2), 16);
            Bytes[i] = b;
            startpos += 2;
        }
        return Bytes;
    }
}

接收错误:

Net.Pkcs11Interop.Common.Pkcs11Exception: 'Method C_DeriveKey returned 2147484548'

来自 HSM 的消息尾日志:

Mar 21 07:59:10 hsm1 kernel: ERR:  viper0: _do_smachine: hsm kernel crashed
Mar 21 07:59:10 hsm1 kernel: ERR:  viper0: _do_smachine: device error
Mar 21 07:59:10 hsm1 kernel: NOTE: viper0: HSM is being shut down, discarding pending requests...
Mar 21 07:59:10 hsm1 kernel: NOTE: viper0:    DMA buffers:          0000
Mar 21 07:59:10 hsm1 kernel: NOTE: viper0:    HSM commands:         0001
Mar 21 07:59:10 hsm1 kernel: NOTE: viper0:    Callback requests:    0000
Mar 21 07:59:10 hsm1 etnetserver[990]: MDV2_SendReceiveCmd(): MD_SendReceive() 
error: Internal error - unknown error

【问题讨论】:

  • 我从供应商那里找到了一些更新版本的 PKCS11 dll 的发行说明,这表明这完全是他们的错... 问题:在 Cryptoki 中错误地定义了 CKM_ECDH1_DERIVE 机制。解决方案:已在 SafeNet ProtectServer/ProtectToolkit 5.3 和固件 5.00.06 中修复。

标签: pkcs#11 hsm pkcs11interop


【解决方案1】:

您得到的异常是低级 PKCS#11 函数 C_DeriveKey 返回供应商特定错误 0x80000384 (2147484548 dec),称为 CKR_SMS_ERROR。您需要讨论设备供应商提供的文档或联系供应商支持,以更好地了解如何处理或避免此特定错误。

这个确切的错误was also discussed in an older question 是由在多线程环境中错误使用 PKCS#11 API 引起的。

【讨论】:

  • 您好 Jariq,感谢您的回复。事后看来,我也应该包括 Pkcs 声明。 PKCS 设置为单线程模式。正如我之前看到你提到的帖子。我将更新上面的原始代码以包含声明广告并与供应商交谈以确定他们是否可以在问题中添加任何内容并在他们有帮助时进行报告。
  • 我认为错误代码是一种症状,而不是原因,因为 HSM 变得无响应,并且在遇到此错误后需要重置 HSM 模块才能接受更多命令。我有一张金雅拓的未结票,一旦他们回复我就会更新。奇怪的是,我们目前在 JCProv 下具有相同的功能,因此它不太可能是 HSM 的硬件/固件问题。
  • @JonBridger 你愿意将此对话转移到new GitHub issue吗?
  • 嗨 Jariq,新问题已提出here 非常感谢您一直以来的支持
猜你喜欢
  • 2019-04-22
  • 1970-01-01
  • 1970-01-01
  • 2015-11-15
  • 2020-12-29
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多