I have a .crt certificate and a .key private key file on a Linux machine. The private key is in encrypted PKCS#8 format (BEGIN ENCRYPTED PRIVATE KEY...). I would like to import these into an X509Certificate2 object for further use. Since we're on Linux, we're using .NET Core 2.2 (we cannot migrate to 3.0 yet).
I have explored a few possible solutions, detailed below:
- Use
openssl
to convert the files to a .pfx and import that using X509Certificate2- I do not want to use this option since I don't want to execute shell code from within C#. I would like the solution to be completely programmatically achieved in C#.
- Use the C# BouncyCastle libraries to do either:
- A conversion of both the certificate and the key to .pfx (as above), or
- Importing the certificate and private key separately and using
X509Certificate2.CopyWithPrivateKey()
to combine them. - However, I cannot find an API for the C# version of BouncyCastle, so I'm not sure what methods I could possibly use to do this.
- Some other programmatic method in C# that I'm missing here
Essentially, the end goal is to obtain an X509Certificate2 object from the .crt and .key files. Any help/insight into what approach to use, or even a pointer to helpful BouncyCastle documentation, would be much appreciated. Thanks!