I have an certificate which I can read using the X509Certificate2 class like this:
X509Certificate2 certificate = new X509Certificate2(@\"certificate.pem\");
I think I've got it... This uses the BouncyCastle NuGet package.
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;
using System.Security.Cryptography.X509Certificates;
using System;
using System.IO;
string pemKey = @"-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIKpAuZ/Wwp7FTSCNJ56fFM4Y/rf8ltXp3xnrooPxNc1UoAoGCCqGSM49
AwEHoUQDQgAEqiRaEw3ItPsRAqdDjJCyqxhfm8y3tVrxLBAGhPM0pVhHuqmPoQFA
zR5FA3IJZaWcopieEX5uZ4KMtDhLFu/FHw==
-----END EC PRIVATE KEY-----";
string pemCert = @"-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----";
var keyPair = (AsymmetricCipherKeyPair)new PemReader(new StringReader(pemKey)).ReadObject();
var cert = (Org.BouncyCastle.X509.X509Certificate)new PemReader(new StringReader(pemCert)).ReadObject();
var builder = new Pkcs12StoreBuilder();
builder.SetUseDerEncoding(true);
var store = builder.Build();
var certEntry = new X509CertificateEntry(cert);
store.SetCertificateEntry("", certEntry);
store.SetKeyEntry("", new AsymmetricKeyEntry(keyPair.Private), new[] { certEntry });
byte[] data;
using (var ms = new MemoryStream())
{
store.Save(ms, Array.Empty<char>(), new SecureRandom());
data = ms.ToArray();
}
var x509Cert = new X509Certificate2(data);
The trick seems to be to combine the cert and key together into a pkcs12 container, then feed that into X509Certificate2
in one go.
Combining the certificate and key with openssl, and feeding that into the x509certificate class works:
openssl pkcs12 -export -in certificate.pem -inkey privatekey.pem -out cert-and-key.pfx
And then using this to obtain a class with assigned private key:
X509Certificate2 certificate = new X509Certificate2("cert-and-key.pfx", "password");
And then the code in my question works.