Certificate formats: PEM, DER, PFX and when to convert

14 min readUpdated

Certificate formats such as PEM, DER and PFX are different ways of storing the same SSL certificate on disk. The certificate itself is one ASN.1 structure, encoded with the binary Distinguished Encoding Rules, DER [1] [5]. What changes from file to file is whether that binary is wrapped in text, and what else travels with it.

PEM
text: base64 between a -----BEGIN line and an -----END line [1]
DER
the same certificate as raw binary [1] [5]
PFX, P12
PKCS #12: certificate, chain and private key in one password-protected file [3]
P7B
PKCS #7 certificates-only file: certificates and chain, no private key [4]
.crt, .cer
either encoding; the extension does not decide [1] [2]
Conversion
re-encodes; names, dates and signature stay the same [11]

The formats you will meet

Two questions place any certificate file:

  • How is it encoded? As raw binary (DER), or as that binary turned into base64 text between a header line and a footer line (PEM) [1].

  • What is in the file? A certificate, a certificate with its chain, a certificate with its private key, or a key or a signing request alone.

The formats you will meet
NameEncodingContains
PEMtextcertificates, keys or requests, one block per item [1]
DERbinaryone certificate per .cer file [2]
PKCS #12 (PFX, P12)binarycertificate, chain and private key, password-protected [3] [6]
PKCS #7 (P7B)text or binarycertificates and chain, no private key [4] [6]

PEM is text, so several blocks can follow one another in one file; that is how a certificate and its intermediate certificates travel together [1]. Its encoding is described in RFC 7468. PKCS #12, defined in RFC 7292, is a transfer syntax for private keys and certificates [3]. A P7B file uses what RFC 2315 calls the degenerate case of PKCS #7, a message with no signers that serves to distribute certificates and revocation lists [4]. The parts of a PEM block and what each label means are in the glossary entry on PEM files.

.crt, .cer and .pem: what the extension tells you

RFC 7468 asks for .crt on PEM certificates and notes that many tools still write PEM text with .cer [1]. RFC 2585 registers .cer for a file holding exactly one certificate in DER [2]. For a certificate exported without its key, the Windows export wizard offers a DER-encoded file, a Base64-encoded file or a PKCS #7 file; Microsoft documents the wizard for Windows 7 and Windows Server 2008 R2 [9]. So a request for "a CER file" does not say which one the receiver wants.

Why there is a text form at all

RFC 7468 names the drawback of a binary format: it cannot be passed through textual transports such as email or text documents. Text can, and it can be edited: certificates can be joined into a chain with copy and paste [1]. PKCS #12 answers a different need, moving a private key and its certificates between systems as one unit [3].

Telling them apart

Open the file in a text editor, or print its first line. Readable lines that start with five hyphens mean PEM, and the label says what follows: CERTIFICATE, PRIVATE KEY or CERTIFICATE REQUEST [1]. Binary noise means DER or a PKCS #12 (PFX) file. Counting the BEGIN CERTIFICATE lines tells you whether the chain is in the file:

$ head -1 cert.pem
-----BEGIN CERTIFICATE-----
$ grep -c "BEGIN CERTIFICATE" fullchain.pem
2

To read what a certificate says, decode it:

$ openssl x509 -in cert.pem -noout -text
$ openssl x509 -in cert.der -inform DER -noout -text

Since OpenSSL 3.0, a single certificate is read from DER, PEM or P12 without naming the format [6]. OpenSSL 1.1.1 assumes PEM, so a DER file needs -inform DER there [7].

Which format do you need

The software that reads the file decides, and its documentation is the authority:

Which format do you need
SoftwareFormat
nginx, ssl_certificatePEM; the server certificate first, then the intermediates, in one file [8]
Apache 2.4.8 and later, SSLCertificateFilePEM; intermediates may follow in the same file, from leaf to root [8]
Windows export wizard, certificate without its keyPKCS #7 preferred for import on Windows; DER or Base64 for other systems [9]
Windows export wizard, certificate with its private keya PKCS #12 file protected by a password [9]

Where a certification authority or provider gives you several files without explaining them, open each in a text editor and read the labels.

Converting with OpenSSL

Conversion re-encodes, and nothing else: PEM is the base64 encoding of the DER bytes with header and footer lines added [7]. In the lab, the test certificate converted from PEM to DER, from DER back to PEM and out of a PFX kept the same SHA-256 fingerprint, and the round trip to DER and back gave a byte-identical file [11]. The fingerprint is a digest of the certificate's DER encoding [6], so it is the check to run before and after: openssl x509 -in <file> -noout -fingerprint -sha256.

Converting cannot fix an expired certificate, add a name or repair a mismatched key; it helps only when the software cannot read the file.

What conversion can change is what is bundled. openssl x509 handles one certificate: converting a PEM chain to DER with it kept only the first certificate, the site's own, and dropped the intermediate [11].

CRT or CER to PEM

If the first line reads -----BEGIN CERTIFICATE-----, the file is PEM already: copy or rename it to .pem. If it is binary, it is DER, and one command converts it:

$ openssl x509 -in cert.crt -inform DER -out cert.pem

On OpenSSL 3.0 the command without -inform converted a DER .crt, a PEM .crt and a DER .cer to the same PEM file [11]. A format given with -inform is enforced [6], and a PEM file read with -inform DER failed with Could not read certificate [11]. On OpenSSL 1.1.1, keep -inform DER for binary files [7].

PEM to CER

For a receiver that wants a binary .cer, write DER:

$ openssl x509 -in cert.pem -outform DER -out cert.cer

-outform DER writes binary; without it the output is PEM [6]. A .cer in text form is a PEM file under another name, and many tools write it that way [1]. When a receiver asks for Base64, its documentation says whether it means this text. A DER .cer holds exactly one certificate [2], so the intermediates go in a separate file or in a P7B.

PFX to PEM, and PEM to PFX

$ openssl pkcs12 -in bundle.pfx -out bundle.pem -noenc
$ openssl pkcs12 -export -in cert.pem -inkey key.pem -certfile chain.pem -out bundle.pfx

A PFX file is a PKCS #12 object encoded in DER [6]. The first command asks for the PFX password and writes the certificates and the private key into one PEM file; in the lab that file held two CERTIFICATE blocks and one PRIVATE KEY block [11]. -noenc leaves the key unencrypted, so the output needs the same protection as the key itself; without -noenc, OpenSSL asks for a pass phrase and writes ENCRYPTED PRIVATE KEY [6] [11]. -noenc replaces -nodes, which is deprecated since OpenSSL 3.0 [6]; OpenSSL 1.1.1 has no -noenc, so use -nodes there [7]. The second command builds a PFX from the certificate, its private key and the intermediate certificates in chain.pem [6]. Splitting a PFX into separate files for nginx or Apache, and moving one between Windows servers, is covered in the guide to exporting and importing a certificate in the installation section.

How to get a PFX file

A PFX file is built around a private key, so it cannot be made from the certificate alone. The certificate carries the public key [5]; the private key is a separate input, and OpenSSL requires it for the export [6]. There are two ways to end up with one. Export it from the system that already holds the key, which is what the Windows wizard does for a certificate with its private key (last row of the table above) [9]. Or build it from files with OpenSSL, using three inputs, shown here as PEM files: the certificate, its private key and the intermediate certificates.

The second command in the previous section does the building. Two details decide whether it works. -inkey names the key file; without it, the file given to -in must already contain the key [6]. That allows one PEM file holding the certificate and the key, in any order [6]:

$ openssl pkcs12 -export -in cert-and-key.pem -certfile chain.pem -out bundle.pfx

-certfile adds the intermediate certificates to the output [6].

Check and protect the PFX file you made

Open the new file once before it goes anywhere:

$ openssl pkcs12 -in bundle.pfx -info -noout

OpenSSL asks for the password [12]. -info prints the structure of the file, the algorithms and the iteration counts, and -noout suppresses all output of the credentials, so the file is only verified [6].

The export password is typed at the prompt [12]. Giving it as -passout pass:... puts it on the command line, where utilities such as ps can see it. The manual allows that form only where security is not important [12].

OpenSSL 3.0 protects a new PFX with AES-256-CBC and PBKDF2 by default. -legacy switches to legacy mode and loads the legacy provider, where the default is RC2_CBC or 3DES_CBC for certificates and 3DES_CBC for the private key [6]. Use it only when the importing software documents that it needs them.

A PFX file contains the private key, so store it as you would a private key.

P7B to PEM, and PEM to P7B

$ openssl pkcs7 -print_certs -in chain.p7b -out chain.pem
$ openssl crl2pkcs7 -nocrl -certfile fullchain.pem -out chain.p7b

-print_certs writes every certificate in the file, each preceded by its subject and issuer; crl2pkcs7 builds the certificates-only structure, and -nocrl leaves the revocation list out [6]. In the lab the round trip kept both certificates of the chain and no key [11]. Other OpenSSL tasks, from expiry dates to chain checks, are in the OpenSSL commands reference.

Mistakes that produce broken installations

  • A bundle in the wrong order. nginx expects the server certificate first, then the intermediates [8], and TLS 1.3 requires the sender's own certificate to come first [10]. In the lab, openssl s_server given a file with the intermediate first refused to start with key values mismatch: it paired the key with the first certificate it read [11].

  • Including the root. Not needed. TLS 1.3 allows the sender to omit the certificate of a trust anchor if the clients already have that certificate, because trust anchors reach clients independently [10]. See certificate chains.

  • Pasting with damage. In the lab, OpenSSL still read a certificate whose base64 had been joined into one line or had a space inserted. It refused a copy where an em dash replaced the first hyphen of the BEGIN line, and one with four characters cut from a line [11]. RFC 7468 tells parsers to ignore whitespace [1]; the boundary lines and the base64 itself have to survive. Transfer the file as a file, not as pasted text.

  • Losing the chain in a conversion. A DER .cer file holds one certificate [2], and a PEM chain converted to DER kept only the first [11]. A server that then sends no intermediate leaves clients unable to build the chain to a trusted root, the case covered on ERR_CERT_AUTHORITY_INVALID.

FAQ

What is a PEM file?

A PEM file is a certificate, key or request stored as text, with a header line such as -----BEGIN CERTIFICATE----- and a matching footer. One file can hold several items, such as a certificate followed by its chain. nginx and Apache read certificates in this format. [1] [8]

What is the difference between PEM and DER?

Encoding, and nothing else. PEM is the text form with labelled header and footer lines; DER is the same certificate as raw binary. Converting between them changes how the bytes are written, not what the certificate says. [1] [11]

What is a PFX file?

A PFX (PKCS #12) file is a password-protected bundle that holds the certificate, its chain and the private key together. Because the key travels inside, handle it like the key itself: do not email it or leave it in shared storage. [3]

How do I open a PEM file?

In any text editor, to see which blocks it contains. To read the certificate's names and dates, decode it with openssl x509 -in file.pem -noout -text. [6]

How do I convert a CRT or CER file to PEM?

Open it in a text editor first. If it begins with -----BEGIN CERTIFICATE-----, it is already PEM and only needs renaming. If it is binary, it is DER: convert it with openssl x509 -in file.cer -inform DER -out file.pem. [6] [7]

How do I convert PEM to CER?

Ask whether the receiver wants binary or text. For binary DER, run openssl x509 -in cert.pem -outform DER -out cert.cer. A .cer in text form is the PEM file under another name. [1] [2] [6]

Does converting a certificate change it?

No. Conversion re-encodes identical content: the names, the validity period, the signature and the fingerprint stay the same. An expired certificate stays expired and a mismatched key stays mismatched. [6] [11]

Should the root certificate be in the bundle?

No. The client either trusts that root already or it does not, and a copy from your server changes neither case. TLS 1.3 allows the sender to leave the trust anchor out if the clients already have that certificate. [10]

How do I create a PFX file?

The command is openssl pkcs12 -export -in cert.pem -inkey key.pem -certfile chain.pem -out bundle.pfx, which combines the certificate, its private key and the intermediate certificates. OpenSSL prompts for the export password. The private key is required: -inkey supplies it unless the -in file already holds it, and the certificate itself carries the public key.

How do I convert PEM to CRT?

A PEM certificate can already be a .crt file: RFC 7468 asks for the extension .crt on the textual encoding of a certificate, so renaming cert.pem to cert.crt gives a receiver that wants PEM text the name that rule asks for. Open the file first; a first line of -----BEGIN CERTIFICATE----- means it is PEM. If the receiver asks for a binary .crt, the command in "PEM to CER" above produces it with -out cert.crt. The name does not set the encoding; -outform does.

Can I convert a CSR to a PFX file?

No. A certificate signing request (CSR) is not a certificate. RFC 2986 defines it as a distinguished name, a public key and optional attributes, signed by the requester, and it is sent to a certification authority, which transforms it into an X.509 public-key certificate. A PFX file carries private keys and certificates, and openssl pkcs12 -export expects one private key and its corresponding certificate. So send the CSR to the authority, wait for the issued certificate, then build the PFX from that certificate and its private key, as in "How to get a PFX file" above.

Sources

  1. RFC 7468, Textual Encodings of PKIX, PKCS, and CMS Structures: sections 1, 2, 5, 5.3, 7 and 10.

  2. RFC 2585, Internet X.509 Public Key Infrastructure Operational Protocols: FTP and HTTP, section 2.

  3. RFC 7292, PKCS #12: Personal Information Exchange Syntax v1.1.

  4. RFC 2315, PKCS #7: Cryptographic Message Syntax Version 1.5.

  5. RFC 5280, Internet X.509 Public Key Infrastructure Certificate and CRL Profile, section 4.1.

  6. OpenSSL 3.0 manual: openssl-format-options, openssl-x509, openssl-pkcs12, openssl-pkcs7 and openssl-crl2pkcs7.

  7. OpenSSL 1.1.1 manual: x509 and pkcs12.

  8. nginx, ngx_http_ssl_module: ssl_certificate; Apache HTTP Server 2.4, mod_ssl: SSLCertificateFile.

  9. Microsoft Learn, Export a Certificate and Export a Certificate with the Private Key, for Windows 7 and Windows Server 2008 R2.

  10. RFC 9846, The Transport Layer Security (TLS) Protocol Version 1.3, section 4.5.1.

  11. Lab runs for this page: OpenSSL 3.0.13 on Linux Mint 22.1; test chain Example Test Root, Example Test Intermediate, example.com.

  12. OpenSSL 3.0 manual: openssl-passphrase-options.

  13. RFC 2986, PKCS #10: Certification Request Syntax Specification, section 1; OpenSSL 3.0 manual: openssl-req.