c# - Load certificate keys into CngKey class for use with DiffieHellman (ECDiffieHellmanCng class) -
this related .net / c#. lets assume there certificate + private key (p521 ecc one) inside pfx or pkcs#12 file. have loaded certificate , it's private key windows certificate store installing (either double clicking pfx or running certutil -f -p mypfxpassword -importpfx someeccert.pfx
). have noted if certificate compatible (eg. p521 curve), automatically installed cng certificate/key.
now, how can load private key cngkey can use inside ecdiffiehellmancng
class? load x509 (cng) certificate read it's serial #, issuer, common name etc bookkeeping.
var mycngkey = somehowloadthecngkey("my ecc certificate"); // <== ?? var mydh = new ecdiffiehellmancng(mycngkey);
well, .net's doesn't have api cng. if scratch surface of api see it's kinda ridiculous, considering both microsoft , cng serious of crypto apis on entire windows platform.
so need use clrsecurity provides c# interfaces (via p/invoke) c++ cng api. it's not nicest of api designs; helps.
// load cert, many ways, 1 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! load private cngkey (if exists) // need clr security this, manages p/invoke // c++ api behind scenes. var pvtcngkey = cert.getcngprivatekey(); // create diffiehellman helper var ecdh = new ecdiffiehellmancng(ourpvteccngkey) { keyderivationfunction = ecdiffiehellmankeyderivationfunction.hash, hashalgorithm = cngalgorithm.sha256 }; ecdiffiehellmancngpublickey theirpubcngkey = loadotherpartiescngpublickey(theircert); byte[] symkey = ecdh.derivekeymaterial(theirpubcngkey);
Comments
Post a Comment