Saturday, August 3, 2013

Use EC DiffieHellman Cng class through an existing PFX certificate + private key?


This is related to .NET / C#. Lets assume there is a certificate + private key (P521 ECC one) inside a PFX or a PKCS#12 file. Would like to be able to



  1. Import the certificate + private key into the CNG framework/windows cert store (the default Microsoft Software KSP and CSP)

  2. Load the private key into the CngKey class to then use it inside the ECDiffieHellmanCng class.

  3. Also read the certificate metadata like serial #, issuer, common name etc for some bookkeeping.


Basically would want to transform the following (toy) code to a more useful one by using the real certificate and keys instead of the random runtime generated ones.



// Current test/toy code, uses on-the-fly/non-persistent
// keys instead of pulling from PFX/cert store
var alice = new ECDiffieHellmanCng
{
KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash,
HashAlgorithm = CngAlgorithm.Sha256
};

var bob = new ECDiffieHellmanCng
{
KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash,
HashAlgorithm = CngAlgorithm.Sha256
};

byte[] sharedSymmetricKeyingBits = bob.DeriveKeyMaterial(alice.PublicKey);


No comments:

Post a Comment