2016-09-30 2 views
1

pkcs11interopを使用して3DESキーを作成し、作成するキー値を指定することができるかどうか、キー値。基本的に私は秘密鍵を別のデバイスにエクスポートする必要があります。pkcs11Interopで3DESキーを作成し、キー値を出力するか、または作成するためにproivdeキー値を

私はCKA_VALUE属性を使用し、キーをbyte []配列として渡しましたが、成功しませんでした。

このようなことは可能ですか?誰かが私を助けてくれますか?

編集:私はこのコードは、それが使用して値です3DESキーを作成して取得することであるとしようとしているものをそう

public ObjectHandle generate3DESKey(string keyLabel) 
{ 
ObjectHandle key = null; 
// Generate symetric key 

// Prepare attribute template of new key 
List<ObjectAttribute> objectAttributes = new List<ObjectAttribute>(); 
objectAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true)); 
objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_SECRET_KEY)); 
objectAttributes.Add(new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_DES3)); 
objectAttributes.Add(new ObjectAttribute(CKA.CKA_ENCRYPT, true)); 
objectAttributes.Add(new ObjectAttribute(CKA.CKA_EXTRACTABLE, true)); 

objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, keyLabel)); 


// Specify key generation mechanism 
Mechanism mechanism = new Mechanism(CKM.CKM_DES3_KEY_GEN); 

// Generate key 
key = _session.GenerateKey(mechanism, objectAttributes); 

List<CKA> retrieveTemplate = new List<CKA>(); 
retrieveTemplate.Add(CKA.CKA_VALUE); 

var test = _session.GetAttributeValue(key, retrieveTemplate); 
var testval = test[0].GetValueAsString(); 
return key; 
} 

:ここ

は、私がこれまで運を持っているコードです。 GetAttributeValueは以下に示すとおりです。私はGetValueAsByteArrayとGetValueAsStringを試しましたが、すべて成功しませんでした。私が気づいたことは、作成時に抽出可能な属性を設定しても、取得された属性のcannotreadプロパティがtrueに設定されていることです。

3DESキーの生成にキー値を渡すことも考えましたが、CKA.CKA_VALUEで使用されるキー値は長さ24のバイト配列にする必要があります。 1616161616161616 1010101010101010

+0

私は私の答えに作業コードサンプルを追加しました。ライブラリ/デバイスで動作しない場合は、ライブラリ/デバイスベンダに問い合わせる必要があると思います。 BTW DES3キーは常に24バイトです。 16バイトしかない場合は、DES2キーを作成する必要があります。 – jariq

答えて

1

Session::CreateObject()メソッドでは、秘密鍵をインポートすることができます。この場合、私は作成する必要がある鍵は長さ16で、長さは24ではありません。 PKCS#11仕様で定義されている正しいオブジェクト属性を指定する必要があります。

平文の秘密鍵はSession::GetAttributeValue()メソッドでエクスポートできます。キーオブジェクトには、明示的な値を読み取れるように指定された正しい属性が必要です。

お読みください少なくとも- 「 - DES3秘密鍵オブジェクト章12.15.3」PKCS#11 v2.20 specificationと、あなたはまだあなたの問題を解決することができない可能性があなたのコードを投稿しとの「第10章オブジェクト」。コードサンプル後

SoftHSM 2.1.0と魔法のように私の作品:

using Net.Pkcs11Interop.Common; 
using Net.Pkcs11Interop.HighLevelAPI; 
using System; 
using System.Collections.Generic; 

namespace ExportTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      using (Pkcs11 pkcs11 = new Pkcs11(@"D:\SoftHSM2\lib\softhsm2.dll", false)) 
      { 
       Slot slot = pkcs11.GetSlotList(true)[0]; 

       using (Session session = slot.OpenSession(false)) 
       { 
        session.Login(CKU.CKU_USER, "11111111"); 

        // Generate exportable key 
        List<ObjectAttribute> objectAttributes = new List<ObjectAttribute>(); 
        objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, "Generated key")); 
        objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_SECRET_KEY)); 
        objectAttributes.Add(new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_DES3)); 
        objectAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true)); 
        objectAttributes.Add(new ObjectAttribute(CKA.CKA_ENCRYPT, true)); 
        objectAttributes.Add(new ObjectAttribute(CKA.CKA_DECRYPT, true)); 
        objectAttributes.Add(new ObjectAttribute(CKA.CKA_EXTRACTABLE, true)); 

        ObjectHandle generatedKey = null; 
        using (Mechanism mechanism = new Mechanism(CKM.CKM_DES3_KEY_GEN)) 
         generatedKey = session.GenerateKey(mechanism, objectAttributes); 

        // Export the key 
        byte[] plainKeyValue = null; 
        List<ObjectAttribute> readAttrs = session.GetAttributeValue(generatedKey, new List<CKA>() { CKA.CKA_VALUE }); 
        if (readAttrs[0].CannotBeRead) 
         throw new Exception("Key cannot be exported"); 
        else 
         plainKeyValue = readAttrs[0].GetValueAsByteArray(); 

        // Import the key 
        objectAttributes = new List<ObjectAttribute>(); 
        objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, "Imported key")); 
        objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_SECRET_KEY)); 
        objectAttributes.Add(new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_DES3)); 
        objectAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true)); 
        objectAttributes.Add(new ObjectAttribute(CKA.CKA_ENCRYPT, true)); 
        objectAttributes.Add(new ObjectAttribute(CKA.CKA_DECRYPT, true)); 
        objectAttributes.Add(new ObjectAttribute(CKA.CKA_VALUE, plainKeyValue)); 

        ObjectHandle importedKey = session.CreateObject(objectAttributes); 

        // Test encryption with generated key and decryption with imported key 
        using (Mechanism mechanism = new Mechanism(CKM.CKM_DES3_CBC, session.GenerateRandom(8))) 
        { 
         byte[] sourceData = ConvertUtils.Utf8StringToBytes("Our new password"); 
         byte[] encryptedData = session.Encrypt(mechanism, generatedKey, sourceData); 
         byte[] decryptedData = session.Decrypt(mechanism, importedKey, encryptedData); 
         if (Convert.ToBase64String(sourceData) != Convert.ToBase64String(decryptedData)) 
          throw new Exception("Encryption test failed"); 
        } 

        session.Logout(); 
       } 
      } 
     } 
    } 
} 
+0

この同じコードを実行することはできません。私がテストしているHSMはSafenet HSMです。それが何らかの設定と関係しているかどうかを調べて、このコードを実行すると、エクスポート時にcannotberead例外が発生し、インポート時にCKR_TEMPLATE_INCONSISTENTが発生します。ありがとう。 – Andre

+0

@Andreこの場合のニュースはありますか? – jariq

+0

これは、私たちが持っているHSMの限界であることが判明しました。セキュリティ上の理由から、キー値を抽出することはできません!実際、彼らはそれを可能にする異なる製品を持っています。私は現在、回避策を選択しました.AESキーを使用して暗号化されたセキュリティのために、generateRandomを使用してDESキーが生成され、データオブジェクトとして格納されます。 – Andre

関連する問題