I am creating a certificate distribution system to track clients and more.
What's happening:
- Client sends CSR to server
- Server verifies and signs the certificate
- The server sends the signed certificate to the client.
- The client puts the signed certificate plus the private key in the Windows store.
So, on the client this happens:
//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
Good. So, I will skip the server. Just trust me that the server signs the certificate and sends it to the client. That's where I get the action.
PemReader pr = new PemReader(new StringReader(b.ToString())); X509Certificate cert = (X509Certificate)pr.ReadObject();
Now you probably think that there should be something wrong on the server side. Well, this is what I thought, too, but when I made a pfx file from this certificate and imported it manually, it worked fine ....
Somehow there is a difference between the .NET RSA private key and the BouncyCastle RSA private key, and I can't put it on it.
You probably suggest importing pfx, and then get the private key to it through the X509Store. I tried .: S And failed. As soon as I try ExportParameters(true) , true means the inclusion of private parameters. He says: "The key is not valid for use in the specified state." See Complete Exclusion at the End.
I hope some of you killed this pig earlier or could help me.
***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& hKey) at System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters parameters) InnerException:
albertjan Jun 04 '09 at 10:28 2009-06-04 10:28
source share