Note that there are some explanatory texts on larger screens.

plurals
  1. POBouncyCastle RSAPrivateKey to .NET RSAPrivateKey
    primarykey
    data
    text
    <p>I'm creating a certificate distribution system to keep track of clients and stuff.</p> <p>What happens is:</p> <ul> <li>Client send CSR to Server</li> <li>Server checks and signs certificate</li> <li>Server sends Signed certificate to Client</li> <li>Client puts Signed certificate plus Private key in Windows store.</li> </ul> <p>So on the client this happens:</p> <pre><code>//Pseudo Server Object: Server s = new Server(); //Requested Certificate Name and things X509Name name = new X509Name("CN=Client Cert, C=NL"); //Key generation 2048bits RsaKeyPairGenerator rkpg = new RsaKeyPairGenerator(); rkpg.Init(new KeyGenerationParameters(new SecureRandom(), 2048)); AsymmetricCipherKeyPair ackp = rkpg.GenerateKeyPair(); //PKCS #10 Certificate Signing Request Pkcs10CertificationRequest csr = new Pkcs10CertificationRequest("SHA1WITHRSA", name, ackp.Public, null, ackp.Private); //Make it a nice PEM thingie StringBuilder sb = new StringBuilder(); PemWriter pemwrit = new PemWriter(new StringWriter(b)); pemwrit.WriteObject(csr); pemwrit.Writer.Flush(); s.SendRequest(sb.ToSting()); </code></pre> <p>Ok So I'll skip serverside Just trust me the server signs the cert and send it back to the client. Thats where I'll pick up the action.</p> <pre><code>PemReader pr = new PemReader(new StringReader(b.ToString())); X509Certificate cert = (X509Certificate)pr.ReadObject(); //So lets asume I saved the AsymmetricCipherKeyPair (ackp) from before //I have now the certificate and my private key; //first I make it a "Microsoft" x509cert. //This however does not have a PrivateKey thats in the AsymmetricCipherKeyPair (ackp) System.Security.Cryptography.X509Certificates.X509Certificate2 netcert = DotNetUtilities.ToX509Certificate(cert); //So here comes the RSACryptoServerProvider: System.Security.Cryptography.RSACryptoServiceProvider rcsp = new System.Security.Cryptography.RSACryptoServiceProvider(); //And the privateKeyParameters System.Security.Cryptography.RSAParameters parms = new System.Security.Cryptography.RSAParameters(); //now I have to translate ackp.PrivateKey to parms; RsaPrivateCrtKeyParameters BCKeyParms = ((RsaPrivateCrtKeyParameters)ackp1.Private); //D is the private exponent parms.Modulus = BCKeyParms.Modulus.ToByteArray(); parms.P = BCKeyParms.P.ToByteArray(); parms.Q = BCKeyParms.Q.ToByteArray(); parms.DP = BCKeyParms.DP.ToByteArray(); parms.DQ = BCKeyParms.DQ.ToByteArray(); parms.InverseQ = BCKeyParms.QInv.ToByteArray(); parms.D = BCKeyParms.Exponent.ToByteArray(); parms.Exponent = BCKeyParms.PublicExponent.ToByteArray(); //Now I should be able to import the RSAParameters into the RSACryptoServiceProvider rcsp.ImportParameters(parms); //&lt;em&gt;&lt;b&gt;not really&lt;/b&gt;&lt;/em&gt; This breaks says "Bad Data" and not much more. I'll Post the //stacktrace at the end //I open up the windows cert store because thats where I want to save it. //Add it and save it this works fine without the privkey. X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.MaxAllowed); store.Add(netcert); store.Close(); </code></pre> <p>Now you're probably thinking there must be something going wrong at the server side. Well thats what I thought too but When I made a pfx file from this cert and imported it by hand it worked fine .... </p> <p>Somehow there's a diference bewteen a .NET RSA privatekey and a BouncyCastle RSA privatekey and I can't put my finger on it.</p> <p>You will probably suggest to import the pfx and then get the private key from it via the X509Store. I tried. :S And failed. As soon as I try to <code>ExportParameters(true)</code> the true stands for including privateparameters. It says "Key not valid for use in specified state.". See for complete exception at the end.</p> <p>I hope some of you have slain this pig before or might be able to help me.</p> <pre><code>***Exceptions:*** System.Security.Cryptography.CryptographicException was unhandled Message="Key not valid for use in specified state.\r\n" Source="mscorlib" StackTrace: at System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr) at System.Security.Cryptography.Utils._ExportKey(SafeKeyHandle hKey, Int32 blobType, Object cspObject) at System.Security.Cryptography.RSACryptoServiceProvider.ExportParameters(Boolean includePrivateParameters) InnerException: ***And the other one:*** System.Security.Cryptography.CryptographicException was unhandled Message="Bad Data.\r\n" Source="mscorlib" StackTrace: at System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr) at System.Security.Cryptography.Utils._ImportKey(SafeProvHandle hCSP, Int32 keyNumber, CspProviderFlags flags, Object cspObject, SafeKeyHandle&amp; hKey) at System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters parameters) InnerException: </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload