toolhq.io

All posts
7 min readby Jameel Haider

Convert SSL certificate formats: PEM, DER, PKCS#12, and PKCS#7

Certificates come in a confusing pile of formats: .pem, .crt, .cer, .der, .p7b, .pfx. They often hold the same data in different wrappers, and picking the wrong one for your server is a common cause of failed installs.

The formats, demystified

  • PEM is the most common. It is Base64 text wrapped in -----BEGIN CERTIFICATE----- lines, and a single PEM file can hold a certificate, its chain, and even the private key stacked together. Apache, nginx, and most Linux tooling expect PEM. The .crt, .cer, and .key extensions are usually just PEM with a different name.
  • DER is the same information in raw binary instead of Base64. It is common in Java and some Windows contexts. A .cer file may be either PEM or DER, which is part of the confusion.
  • PKCS#7 (.p7b, .p7c) is a container for the certificate and its chain, but not the private key. Windows and Java import it to install a trust chain.
  • PKCS#12 (.pfx, .p12) is a single password-protected bundle holding the certificate, its chain, and the private key. This is what Windows and IIS and many load balancers want, and what you export to move a full identity between machines.

Extension is not format

The single biggest source of confusion is that the file extension does not reliably tell you the encoding. A .cer can be PEM or DER; a .crt is usually PEM but not always. The reliable test is to open the file: if it starts with -----BEGIN, it is PEM text; if it looks like binary, it is DER or a PKCS container. OpenSSL will also tell you, for example openssl x509 -in file.cer -inform PEM -noout -text succeeds only if the file really is PEM.

The conversion commands

Most conversions are one OpenSSL call:

# PEM to DER
openssl x509 -in cert.pem -outform DER -out cert.der

# DER to PEM
openssl x509 -in cert.der -inform DER -out cert.pem

# PEM (cert + key + chain) to PKCS#12
openssl pkcs12 -export -out bundle.pfx \
  -inkey key.pem -in cert.pem -certfile chain.pem

# PKCS#12 to PEM (extract everything)
openssl pkcs12 -in bundle.pfx -out all.pem -nodes

# PKCS#7 to PEM
openssl pkcs7 -print_certs -in chain.p7b -out chain.pem

Choosing the target format

The rule of thumb: nginx and Apache want PEM, Windows and IIS want PKCS#12, Java keystores often start from DER or PKCS#7, and moving a cert with its key between systems means PKCS#12. When an install fails, a format mismatch is one usual reason; the other is chain order inside a PEM file. Servers that read a combined PEM expect the leaf certificate first, then intermediates, then the root, and a bundle built in the wrong order produces the same "untrusted" symptom as a missing intermediate even though every certificate is present.

Converting safely

Conversions are lossless when done right, but PKCS#12 bundles contain the private key, so they should never be uploaded to a third-party service. A passwordless .pfx is especially dangerous since anyone who gets the file has the key outright; always set an export password. The SSL Converter runs in your browser, so the key in a .pfx stays on your machine. If your problem is really a broken chain rather than a format, the SSL certificate chain errors guide covers that specifically, and for bare keys rather than full bundles see RSA key formats.