c# - How to create a X509 certificate from Bouncy Castle for use with AuthenticateAsServer? -
i've used bouncy castle create x509 certificate, can't use sslstream.authenticateasserver
or sslstream.authenticateasclient
since (of course) use .net version. although there converter ,dotnetutilities.tox509certificate()
, in bouncy castle takes a bc x509 , returns .net x509. problem seems authenticateasserver/authenticateasclient needs certificate private key included. @ least when try convert , use new certificate cryptographicexception: "key not exist"
when trying connect using sslstream.
so thought need create x509certificate2 bouncy castle, since can contain private key well. solution found seems bit...odd, , wondering if else better way use bc x509certificate sslstream.
this how create x509certificate2 bc certificate:
private static x509certificate createdotnetcertificate(org.bouncycastle.x509.x509certificate certificate, asymmetriccipherkeypair keypair) { var store = new pkcs12store(); string friendlyname = certificate.subjectdn.tostring(); var certificateentry = new x509certificateentry(certificate); store.setcertificateentry(friendlyname, certificateentry); store.setkeyentry(friendlyname, new asymmetrickeyentry(keypair.private), new[] { certificateentry }); var stream = new memorystream(); var password = "a password"; store.save(stream, password.tochararray(), new securerandom(randomgenerator)); return new x509certificate2(stream.toarray(), password, x509keystorageflags.persistkeyset | x509keystorageflags.exportable); }
it seems bit weird need take "detour" through pkcs12store able create x509certificate2.
the solution taken blog: http://blog.differentpla.net/post/20
windows works certificates stored in windows certificate storage . don't know if bouncycastle provides direct access windows certstorage if doesn't, option import certificate certstorage file (or other source), use it. pkcs#12 right format transfer certificate private key together, it's quite natural use intermediate medium.
Comments
Post a Comment