Blog

Using a Windows PFX with Nginx: splitting the certificate, intermediates and private key into PEM

How to split a PFX file exported from IIS into the fullchain.pem and key.pem that Nginx reads. Covers the key values mismatch error caused by certificate order, why OpenSSL 3 reports old PFX files as unsupported and how -legacy fixes it, and which format to pick when building a PFX from PEM that Windows Server 2016 can still import.

Diagram of a single locked box with arrows branching out to three items: a private key, a server certificate and a chain of intermediate certificates

I had to move a site that ran on IIS over to Nginx on Linux. The certificate had just been renewed, so buying a new one was out of the question. All I had was site.pfx, exported from the Windows certificate manager with “Yes, export the private key” selected, and the password I set at the time.

Nginx, however, only takes ssl_certificate and ssl_certificate_key, and both point to files in PEM format. There is no way to hand it the PFX directly. In this article I split one PFX into the two files Nginx can read, first with the browser-based PFX / P12 ⇄ PEM Converter and then with OpenSSL. At the end I also cover the opposite direction: taking Nginx’s PEM files back to Windows.

What’s inside a PFX, and what Nginx needs

A PFX (extension .pfx or .p12, standardized as PKCS#12) is a container that bundles certificates and a private key into one password-protected file. If you ticked “Include all certificates in the certification path if possible” during the Windows export, it holds three kinds of content.

Content Role How Nginx uses it
Server certificate The certificate carrying the domain name, such as www.example.com First entry in fullchain.pem
Intermediate certificate The certificate of the CA that signed the server certificate In fullchain.pem, after the server certificate
Private key The key paired with the server certificate key.pem

The root certificate is sometimes included too, but browsers and operating systems already ship their own root certificates, so the server does not need to send it. Sending it does not break the connection; it just adds a little traffic.

If you leave out the intermediate certificate, the site may look fine in a desktop browser while some phones or curl complain that the certificate cannot be verified. That happens because some browsers remember intermediate certificates they have seen before and fill in the gap themselves. If the server sends fullchain.pem, the difference disappears.

Splitting it with the browser tool

From here on I use the PFX / P12 ⇄ PEM Converter. Both reading and decrypting the PFX happen inside the browser, so the private key is never sent to any server.

  1. Under “Extract PEM from PFX / P12”, choose site.pfx.
  2. Enter the password you set during export and click “Convert to PEM”.
Result of loading site.pfx. The MAC is HMAC-SHA-1, the certificates are encrypted with pbeWithSHAAnd40BitRC2-CBC and the private key with pbeWithSHAAnd3-KeyTripleDES-CBC, and the key matches the certificate. The list shows the server certificate www.example.com, the intermediate Example Issuing CA and the root Example Root CA
With the correct password, the tool shows the PFX's encryption schemes, whether the key matches the certificate, and the list of certificates it contains.

The first row to check is “Key and certificate match”. If it says Match, the private key in the PFX and the server certificate at the top of the list belong together. The tool extracts each public key, compares them, and puts the matching certificate first as the server certificate. It then follows the “Issuer” fields and reorders the rest as intermediate certificate, then root certificate.

This is also a good moment to look at the expiry dates in the list. It keeps you from grabbing an old PFX during the migration and having the certificate expire the week after you switch over.

Below the result, the extracted files are listed. For Nginx you need two of them: site-fullchain.pem and site-key.pem.

Output panel for site-fullchain.pem with Copy and Save buttons, showing PEM text starting with BEGIN CERTIFICATE
Each file can be saved with Save or copied to the clipboard with Copy.

site-chain.pem contains only the intermediate certificates and site-cert.pem only the server certificate; they are meant for software that takes the intermediates separately, such as Apache 2.4.7 and earlier. site-combined.pem holds the private key and the fullchain in one file, for software like HAProxy that expects a single file. For RSA keys you also get site-key-rsa.pem in the older format (BEGIN RSA PRIVATE KEY). Current Nginx versions read either key file, but if in doubt, use site-key.pem.

Configuring Nginx

Put the two saved files on the server and make sure the private key cannot be read by anyone other than the user Nginx runs as.

sudo install -m 644 site-fullchain.pem /etc/nginx/ssl/www.example.com.fullchain.pem
sudo install -m 600 site-key.pem       /etc/nginx/ssl/www.example.com.key.pem

The configuration looks like this:

server {
    listen 443 ssl;
    server_name www.example.com example.com;

    ssl_certificate     /etc/nginx/ssl/www.example.com.fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/www.example.com.key.pem;
}

Check the syntax and the key pairing with sudo nginx -t, then apply it with sudo systemctl reload nginx. After that, running the following command from another machine shows the chain the server is sending.

openssl s_client -connect www.example.com:443 -servername www.example.com -showcerts </dev/null

If Certificate chain is followed by 0 s:CN = www.example.com and then 1 s:CN = Example Issuing CA, the intermediate certificate is being delivered as well.

When you are done, delete the copy of site-key.pem left on your own machine. Unlike the PFX it has no password, so anyone who gets hold of the file can use the key. If you really need to keep it, the tool can export it with “Encrypt the output private key with a password” turned on. Be aware, though, that an encrypted key makes Nginx ask for the password on every start, or requires you to point ssl_password_file at a file containing the password.

What happens when the fullchain is in the wrong order

The order of the certificates inside a PFX depends on the software that created it. When splitting with OpenSSL commands, for example, it is easy to end up with a fullchain that starts with the root or intermediate certificate.

Nginx treats the first certificate in the ssl_certificate file as its own and checks it against the private key. If the first one is an intermediate certificate, the public keys do not match and nginx -t stops with an error like this:

SSL_CTX_use_PrivateKey("/etc/nginx/ssl/www.example.com.key.pem") failed
(SSL: error:05800074:x509 certificate routines::key values mismatch)

The message makes it look as if the key is broken, but usually the order is the problem. Put the server certificate first, followed by its issuer, then that issuer’s issuer, and so on, and the check passes. The tool above does this reordering automatically. If you want to verify a fullchain you assembled yourself, load it into the X.509 / CSR Decoder to see the subject and issuer of each certificate in order.

Doing the same with OpenSSL

If the server has OpenSSL, you can split the file with commands as well. With OpenSSL 3 it goes like this:

# Private key (unencrypted)
openssl pkcs12 -in site.pfx -nocerts -noenc -out key.pem
# Server certificate only
openssl pkcs12 -in site.pfx -clcerts -nokeys -out cert.pem
# Intermediate and root certificates
openssl pkcs12 -in site.pfx -cacerts -nokeys -out chain.pem
cat cert.pem chain.pem > fullchain.pem

The output files contain extra lines such as Bag Attributes and subject=, but Nginx skips anything outside the PEM blocks, so they work as they are. The order inside chain.pem is simply the order in the PFX, so you need to check the ordering described in the previous section yourself.

With a PFX exported from an older Windows version, the first command may fail with this error:

Error outputting keys and certificates
40A7A457537F0000:error:0308010C:digital envelope routines:inner_evp_generic_fetch:unsupported:../crypto/evp/evp_fetch.c:386:Global default library context, Algorithm (RC2-40-CBC : 0), Properties ()

This is not a wrong password. Older Windows versions and OpenSSL 1.x wrapped the certificate part of a PFX in RC2 with a 40-bit key, an outdated cipher. OpenSSL 3 moved such old algorithms into a “legacy provider” that is not loaded by default, so decryption stops with “unsupported”. Adding -legacy to each command lets OpenSSL read it.

openssl pkcs12 -legacy -in site.pfx -nocerts -noenc -out key.pem

When the password really is wrong, you get Mac verify error: invalid password?, so the error text tells the two cases apart. The browser tool reads RC2 and 3DES PFX files without any extra settings. The “Certificate encryption” row in the earlier screenshot showed pbeWithSHAAnd40BitRC2-CBC precisely because that PFX had been exported in this old format.

The other direction: turning Nginx’s PEM into a PFX for Windows

If instead you want to move a certificate you used with Nginx, from Let’s Encrypt for example, into IIS, Azure or the Windows certificate store, you build a PFX from the PEM files. In the tool’s “Create PFX / P12 from PEM” section, select the server certificate, intermediate certificate and private key files together. They can be in any order.

Four files selected (ca.crt, site.key, site.crt, int.crt), the PFX password entered twice, Compatible: 3DES + SHA-1 chosen as the encryption format, and www.example.com.pfx created. The list below has been reordered as server certificate, intermediate certificate, root certificate
Even if the files are selected in random order, the tool puts the server certificate that matches the key first before building the PFX.

The setting that needs some thought here is “Encryption format”.

  • Standard (AES-256 + SHA-256) is the same as the OpenSSL 3 default and can be imported on Windows 10 version 1709, Windows Server 2019 and later.
  • Compatible (3DES + SHA-1) is for systems that cannot read AES-protected PFX files, such as Windows Server 2016 and earlier.

If you try to import an AES-format PFX on Windows Server 2016, it fails with “The password you entered is incorrect.” even when the password is correct. Retyping the password never helps, so the real cause is hard to spot. Choose Compatible only when you know the target is old; otherwise choose Standard. Because the compatible format uses weaker encryption, it is best to recreate the PFX in the standard format once the migration is done.

To create it with a command, use the following, adding -legacy if you need the compatible format.

openssl pkcs12 -export -inkey key.pem -in cert.pem -certfile chain.pem -name "www.example.com" -out site.pfx

Whether you use the tool or the command, no PFX is created unless the selected private key and server certificate belong together. If you mixed up an old certificate from before renewal or a key from another server, this is where you find out.

Summary

To use a PFX with Nginx, split it into two files: the private key, and a fullchain that starts with the server certificate followed by the intermediate certificates. The two common stumbling blocks are a fullchain that does not start with the server certificate, and the need for -legacy when reading an old PFX with OpenSSL 3. When going the other way, to Windows, choose the PFX format according to the Windows version you are importing into.

If you need a certificate for trying out HTTPS in a local development environment, you can also create a self-signed one with the X.509 Certificate Generator.