Search code examples
c#ssl-certificatebouncycastlex509certificatepem

Using PEM file with multiple certificate and Private Key


I have a .PEM file that contains a private key and somehow it has 3 sets of certificates. I'm new to certificates and cant figure out how to import it to my code to be able to use it as authenticate to access clients web service.

Bag Attributes
    localKeyID: 94 7D DF 3D A7 A5 8D 3F FF 73 B5 CF 44 96 4F FC DC DC 51 BB 
    friendlyName: Company UAT Client SSL Certificate
Key Attributes: <No Attributes>
-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----
Bag Attributes
    localKeyID: 94 7D DF 3D A7 A5 8D 3F FF 73 B5 CF 44 96 4F FC DC DC 51 BB 
    friendlyName: Company UAT Client SSL Certificate
subject=/[email protected]/UID=002/CN=Company UAT Client SSL Certificate/OU=TCMG/O=AAAA/C=PH
issuer=/CN=AAAA Company API UAT CA/OU=Information Security/O=AAAA/C=PH
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
Bag Attributes
    friendlyName: AAAA Root CA
subject=/CN=AAAA Root CA/OU=Information Security/O=AAAA/C=PH
issuer=/CN=AAAA Root CA/OU=Information Security/O=AAAA/C=PH
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
Bag Attributes
    friendlyName: Name CA
subject=/CN=Subject CA/OU=Information Security/O=AAAA/C=PH
issuer=/CN=AAAA Root CA/OU=Information Security/O=AAAA/C=PH
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----

I have tried to use BouncyCastle - Org.BouncyCastle.X509 for C# but whenever i tried to access clients web service, I got Error (400) Bad Request.


Solution

  • I found a way to resolve my issue by converting PEM file into PFX/PKCS#12 format using openssl (or you can visit - https://www.sslshopper.com/ssl-converter.html if you dont have one installed) and then importing my certificate using X509Certificate2 in my code. And everything else I based here - https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509certificate2.import?view=netframework-4.8