2013-07-29 5 views
3

이것은 .NET/C#과 관련이 있습니다. PFX 또는 PKCS # 12 파일 내에 인증서 + 개인 키 (P521 ECC 1)가 있다고 가정합니다. 이 인증서를로드하고 Windows 인증서 저장소에 개인 키를 설치합니다 (PFX를 두 번 클릭하거나 certutil -f -p myPfxPassword -importPFX MY SomeEcCert.pfx을 실행). 필자는 인증서가 호환 가능한 경우 (예 : p521 곡선) CNG 인증서/키로 자동 설치된다는 점에 유의했습니다.DiffieHellman (ECDiffieHellmanCng 클래스)과 함께 사용할 CngKey 클래스에 인증서 키로드

이제클래스 내부에서 사용할 수 있도록 개인 키를 CngKey에로드하려면 어떻게해야합니까? 또한 X509 (CNG) 인증서를 직렬 #, 발급자, 일반 이름 등 일부 부기를 위해 읽으려고합니다.

var myCngKey = SomehowLoadTheCngKey("my ecc certificate"); // <== ?? 
var myDH = new ECDiffieHellmanCng(myCngKey); 

답변

1

글쎄, .NET은 CNG에 좋은 API를 가지고 있지 않습니다. API의 표면을 긁어 본다면, 특히 MS와 CNG가 전체 Windows 플랫폼에서 모든 Crypto API 중 가장 심각한 API라는 점을 고려하면 다소 우스꽝 스럽습니다.

따라서 C++ CNG API에 C# 인터페이스 (P/Invoke를 통해)를 제공하는 CLRSecurity을 사용해야합니다. 그것으로도 API 디자인이 가장 좋지 않습니다. 하지만 도움이됩니다.

// Load the cert, many ways, one implementation 
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine); 
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly); 
var certs = store.Certificates.Find(X509FindType.FindBySubjectName, "My cert subject name", true); 
store.Close(); 

if (certs.Count > 0) 
    cert = certs[0]; 
else 
    return; 

// Magic happens here! We load the private CngKey (if it exists) 
// You need CLR Security for this, it manages the P/Invoke 
// into the C++ api behind the scenes. 
var pvtCngKey = cert.GetCngPrivateKey(); 

// Create the DiffieHellman helper 
var ecDh = new ECDiffieHellmanCng(ourPvtEcCngKey) 
{ 
    KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash, 
    HashAlgorithm = CngAlgorithm.Sha256 
}; 

ECDiffieHellmanCngPublicKey theirPubCngKey = LoadOtherPartiesCngPublicKey(theirCert); 
byte[] symKey = ecDh.DeriveKeyMaterial(theirPubCngKey); 
+1

'LoadOtherPartiesCngPublicKey (theirCert)'의 구현은 어떻게 생겼습니까? –