亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 PKCS#11 導入私鑰

使用 PKCS#11 導入私鑰

C#
慕的地8271018 2022-06-12 11:08:11
我們正在嘗試使用 C# 和 PKCS#11 將 RSA 密鑰對導入我們的 HSM。使用以下方法導入私鑰:    var privateKeyAttributes = new List<ObjectAttribute>();    privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ID, ckaId));    privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, ckaId));    privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY));    privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_RSA));    privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));    privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_PRIVATE, true));    privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_SENSITIVE, true));    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_UNWRAP, true));    privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_MODULUS, privateKeyParams.Modulus));    privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_PRIVATE_EXPONENT, privateKeyParams.D));    var privateKeyHandle = session.CreateObject(privateKeyAttributes);失敗并顯示錯誤代碼CKR_TEMPLATE_INCONSISTENT。不幸的是,它沒有說明不一致的地方。我嘗試了各種其他屬性組合,但總是失敗:-(如何通過 PKCS#11 正確導入私鑰?注意:使用非常相似的代碼導入公鑰的工作原理:    var publicKeyAttributes = new List<ObjectAttribute>();    publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ID, ckaId));    publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, ckaId));    publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PUBLIC_KEY));    publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_RSA));    publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));    publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_PRIVATE, true));    publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ENCRYPT, true));    publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_VERIFY, true));
查看完整描述

3 回答

?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

答案是:您不能直接將私鑰導入 SafeNet Luna HSM。您必須首先加密(包裝)私鑰,然后才能將其傳輸到 HSM。有關如何執行此操作的答案,請參閱PKCS#11 unwrap private key to HSM 。


查看完整回答
反對 回復 2022-06-12
?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

嘗試“CKA_PRIVATE = false”:新 ObjectAttribute(CKA.CKA_PRIVATE, false)



查看完整回答
反對 回復 2022-06-12
?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

不幸的是,PKCS#11 API 沒有提供來自提供的模板的確切屬性導致錯誤的詳細信息,但是許多 PKCS#11 庫支持某種內部日志記錄機制,這可能會揭示錯誤的真正原因。啟用日志記錄所需的確切步驟應包含在 PKCS#11 庫供應商提供的文檔中。


我的猜測是你收到CKR_TEMPLATE_INCONSISTENT是因為你設置CKA_SENSITIVE為true. 以明文形式導入的私鑰已經失去了“敏感性”,因為它暴露在外部環境中。我在 Pkcs11Interop.X509Store 項目中成功使用了以下模板:


var privateKeyAttributes = new List<ObjectAttribute>()

{

    new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY),

    new ObjectAttribute(CKA.CKA_TOKEN, true),

    new ObjectAttribute(CKA.CKA_PRIVATE, true),

    new ObjectAttribute(CKA.CKA_MODIFIABLE, true),

    new ObjectAttribute(CKA.CKA_LABEL, ...),

    new ObjectAttribute(CKA.CKA_ID, ...),

    new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_RSA),

    new ObjectAttribute(CKA.CKA_MODULUS, rsaPrivKeyParams.Modulus.ToByteArrayUnsigned()),

    new ObjectAttribute(CKA.CKA_PUBLIC_EXPONENT, rsaPrivKeyParams.PublicExponent.ToByteArrayUnsigned()),

    new ObjectAttribute(CKA.CKA_PRIVATE_EXPONENT, rsaPrivKeyParams.Exponent.ToByteArrayUnsigned()),

    new ObjectAttribute(CKA.CKA_PRIME_1, rsaPrivKeyParams.P.ToByteArrayUnsigned()),

    new ObjectAttribute(CKA.CKA_PRIME_2, rsaPrivKeyParams.Q.ToByteArrayUnsigned()),

    new ObjectAttribute(CKA.CKA_EXPONENT_1, rsaPrivKeyParams.DP.ToByteArrayUnsigned()),

    new ObjectAttribute(CKA.CKA_EXPONENT_2, rsaPrivKeyParams.DQ.ToByteArrayUnsigned()),

    new ObjectAttribute(CKA.CKA_COEFFICIENT, rsaPrivKeyParams.QInv.ToByteArrayUnsigned())

};


查看完整回答
反對 回復 2022-06-12
  • 3 回答
  • 0 關注
  • 460 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號