2017-07-31 28 views
1

secp256r1をサポートするGemalto HSMに接続しています。私はPkcs11interopを使ってECDSA鍵ペアを作成する次のコードを持っています。私はBouncyCastle NistNamedCurvesとX962Parametersを使用してparamsBytesを取得しています。pkcs11interopでECDSA鍵ペアを作成する際にエラーが発生する

HSMは、CKR_ATTRIBUTE_TYPE_INVALIDに戻ってきます。私はECDSAの新人ですので、何かを見逃しているかもしれません。何か案は?

   X9ECParameters x9Ec = NistNamedCurves.GetByName("P-256"); 
       X962Parameters x962 = new X962Parameters(x9Ec); 
       byte[] paramsBytes = x962.GetDerEncoded(); 

       // The CKA_ID attribute is intended as a means of distinguishing multiple key pairs held by the same subject 
       byte[] ckaId = session.GenerateRandom(20); 

       // Prepare attribute template of new public key 
       List<ObjectAttribute> publicKeyAttributes = new List<ObjectAttribute>(); 
       publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_PRIVATE, false)); 
       publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, keyName)); 
       publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ID, ckaId)); 
       publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_VERIFY, true)); 
       publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ECDSA_PARAMS, paramsBytes)); 

       // Prepare attribute template of new private key 
       List<ObjectAttribute> privateKeyAttributes = new List<ObjectAttribute>(); 
       privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_PRIVATE, true)); 
       privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, keyName)); 
       privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ID, ckaId)); 
       privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_SENSITIVE, true)); 
       privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_SIGN, true)); 
       privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ECDSA_PARAMS, paramsBytes)); 

       // Generate key pair 
       Mechanism mechanism = new Mechanism(CKM.CKM_ECDSA_KEY_PAIR_GEN); 
       ObjectHandle publicKeyHandle = null; 
       ObjectHandle privateKeyHandle = null; 
       session.GenerateKeyPair(mechanism, publicKeyAttributes, privateKeyAttributes, out publicKeyHandle, 
        out privateKeyHandle); 

答えて

1

何が起こっていたかを発見しました。 HSMは非公開鍵の上に

privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ECDSA_PARAMS, paramsBytes)); 

がありませんでした。 PKCSは、ECDSAパラメータが公開鍵上にある必要があり、秘密鍵上に存在することができないと述べており、この実装によって強制されています。

+0

正解としてアップしてください。よくやった! – jariq

関連する問題