1

I have a .der certificate that is binary encoded which needs to be converted to a .pem file programatically in .net

This line gives the correct output using OpenSSL on OSX:

openssl x509 -inform der -in cert.crt -out cert.pem

But we need to do the same in .net

We have tried many solutions but are completely stuck.

Would something like this work:

var oc = OpenSSL.X509.X509Certificate.FromDER(bio); 

Any advice very welcome :)

Marcus
  • 675
  • 2
  • 8
  • 24

2 Answers2

0

In the end we were able to use this to import the DER into a string which we could then export to a PEM:

var oc = OpenSSL.X509.X509Certificate.FromDER(bio);

These pages were useful:

https://github.com/openssl-net/openssl-net/blob/master/ManagedOpenSsl/X509/X509Certificate.cs

https://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509certificate2.rawdata

Thanks all for your help :)

Marcus
  • 675
  • 2
  • 8
  • 24
  • What is the "bio" parameter? I have a list of .crl files in my folder that's in DER format that I need to convert to PEM format. How do I load up all my .crl files as the bio parameter? – Harvey Lin Oct 24 '16 at 20:54
  • Can you please provide more detailed steps in how you did it? I am confused by how this can be done, thanks. – Harvey Lin Oct 25 '16 at 00:07
  • `var bio = new OpenSSL.Core.BIO(privateKeyBytes);` Has an overload for strings as well. Or directly from file: `var bio = OpenSSL.Core.BIO.File(filePath);` – Timo Aug 01 '17 at 13:15
0

Still use OpenSSL? We are coming to you!

No, seriously, it is done in just 1 line:

String pem = "-----BEGIN CERTIFICATE-----\r\n" + Convert.ToBase64String(cert.RawData, InsertLineBreaks) + "-----END CERTIFICATE-----";

where cert is an X509Certificate2object.

Crypt32
  • 12,850
  • 2
  • 41
  • 70
  • Does the X509Certificate2 object includes anything coded in DER format? I am trying to convert a bunch of certificate revocation list files in .crl extension in DER format to PEM format, but when I wrote the code to get those CRL files as X509Certificate2 object, it is throwing me an error in visual studio, it seems X509Certificate2 does not like my .crl files even though they are in DER format. – Harvey Lin Oct 24 '16 at 22:30
  • 1
    I think you mean: var pem = "-----BEGIN CERTIFICATE-----\n" + Convert.ToBase64String(File.ReadAllBytes("certificate.der"), Base64FormattingOptions.InsertLineBreaks) + "\n-----END CERTIFICATE-----"; – Carlo Bos May 31 '18 at 00:35