Converting PFX to PEM made easy: A step-by-step guide for .NET Core developers

Alex Maher
5 min readJan 20, 2023

--

Converting a PFX certificate to a PEM certificate is a common task when working with .NET Core and other technologies that use the Secure Sockets Layer (SSL) and Transport Layer Security (TLS) protocols to secure network communications. PEM (Privacy-Enhanced Mail) is a widely-used format for storing X.509 certificates and private keys, while PFX (PKCS#12) is a container format that can store multiple certificates and private keys in a single file.

In this article, we will explain how to convert a PFX certificate to a PEM certificate using the OpenSSL tool, which is a widely-used open-source library for working with SSL and TLS. We will also show how to use the PEM certificate in a .NET Core application to secure network communications.

Step 1: Install OpenSSL

The first step is to install OpenSSL on your system. You can download the OpenSSL binaries for your operating system from the official website (https://www.openssl.org/source/). You can also install OpenSSL using a package manager, such as apt on Ubuntu or yum on Red Hat.

Step 2: Extract the private key

The next step is to extract the private key from the PFX certificate. You can use the following command to do this:

openssl pkcs12 -in [pfx_file] -nocerts -out [key_file] -nodes

This command will extract the private key from the PFX certificate and save it in a separate file. The -nocerts option tells OpenSSL to extract only the private key, and the -nodes option tells OpenSSL not to encrypt the private key.

Step 3: Extract the certificate

Now that we have extracted the private key, we need to extract the certificate as well. You can use the following command to do this:

openssl pkcs12 -in [pfx_file] -clcerts -nokeys -out [cert_file]

This command will extract the certificate from the PFX certificate and save it in a separate file. The -clcerts option tells OpenSSL to extract only the certificate, and the -nokeys option tells OpenSSL not to extract the private key.

Step 4: Convert the certificate to PEM format

Now that we have extracted the certificate, we need to convert it to PEM format. You can use the following command to do this:

openssl x509 -in [cert_file] -out [pem_file] -outform PEM

This command will convert the certificate to PEM format and save it in a separate file.

Step 5: Use the PEM certificate in .NET Core

Now that we have the PEM certificate and private key, we can use them in our .NET Core application to secure network communications. To do this, we need to load the PEM certificate and private key into an X509Certificate2 object and configure our application to use it.

You can use the following code to load the PEM certificate and private key:

var certificate = new X509Certificate2
(pemFile, "", X509KeyStorageFlags.Exportable);

Once you have the certificate object, you can configure your application to use it in the Startup.cs file:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseHttps(certificate);
}

This will configure your application to use HTTPS and the certificate you loaded for secure network communications.

Another way of using the certificate is by using it in Kestrel server, which is a cross-platform web server for ASP.NET Core. You can configure Kestrel to use the PEM certificate and private key by adding the following code to your Program.cs file:

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseKestrel(options =>
{
options.ConfigureHttpsDefaults(o =>
{
o.ServerCertificate = certificate;
});
});
webBuilder.UseStartup<Startup>();
});

And that’s it! You have successfully converted a PFX certificate to a PEM certificate and used it in your .NET Core application to secure network communications. Keep in mind that this is just one example of how you can use PEM certificates in .NET Core, and there are many other ways to do this depending on your specific use case and requirements.

Configuring client

When using a PEM certificate on the server side to secure network communications, it’s important to also consider how the client will interact with the server. One way to use the PEM certificate from the client is by including the certificate in the client’s trust store.

Here are the steps to use the PEM certificate from the client to make requests to the server:

  1. Extract the public key from the server’s PEM certificate:
openssl x509 -in [pem_file] -pubkey -noout > [pubkey_file]
  1. Add the public key to the client’s trust store. The location of the trust store and the format of the key will depend on the operating system and application you are using. For example, in Windows, the trust store is located in the “Certificates — Current User\Trusted Root Certification Authorities” store, and the key should be in the DER format.
  2. Configure the client application to use the trust store and the certificate. In .NET Core, you can use the HttpClientHandler class to configure the trust store and the certificate. Here is an example of how you can do this:
var handler = new HttpClientHandler();
handler.ClientCertificates.Add(new X509Certificate2(pubkey_file));
var client = new HttpClient(handler);

This way, when the client makes a request to the server, it will include the certificate in the request and the server will be able to verify the certificate and establish a secure connection with the client.

Troubleshooting

Troubleshooting issues with a .NET Core application that uses PEM certificates can be a bit tricky, but there are a few things you can check to help identify and resolve the problem.

  1. Check the certificate: Make sure that the PEM certificate and private key are valid and have not expired. You can use the OpenSSL tool to check the validity of the certificate:
openssl x509 -in [pem_file] -noout -text
  1. Check the trust store: Make sure that the client’s trust store contains the server’s PEM certificate. Also, check that the client’s trust store is properly configured and accessible.
  2. Check the network: Make sure that the client and server are able to communicate over the network. This includes checking that the firewall is configured to allow traffic on the necessary ports and that the client and server are on the same network.
  3. Check the logs: Check the application logs on both the client and server for error messages. The logs can provide valuable information about what’s causing the problem.
  4. Check the configuration: Make sure that the client and server are configured correctly to use the PEM certificate. This includes checking that the certificate is being loaded correctly and that the certificate is being used for the appropriate network communications.
  5. Check the version of the certificate: Make sure that the certificate is of the correct version, for example X509 version 3, and that the certificate is not outdated.
  6. Check the date and time: Make sure that the date and time on both the client and server are correct. Incorrect date and time can cause issues with certificate validation.

Thanks for reading! I hope you found this information helpful. If you have any further questions, feel free to ask.

--

--

Alex Maher
Alex Maher

Written by Alex Maher

.NET C# dev with 10+ yrs exp, self-taught & passionate web developer. Sharing tips & experiences in C# and web dev.

No responses yet