Explainer · Certificates
What is a PEM file and how do you read it
If you have opened a .pem file and seen a line reading -----BEGIN CERTIFICATE-----, that is PEM. Here is what those lines mean, how PEM differs from DER and PKCS#12, and how to read a certificate without any external tools.
PEM in one sentence
PEM is a textual wrapper around a binary certificate. A binary DER structure is encoded as Base64 text and surrounded by a header and footer line. The name comes from history (Privacy-Enhanced Mail); today the format is described by RFC 7468.
The practical advantage: PEM can be sent by email, pasted into a text editor or read on the command line without the binary data being corrupted.
What PEM looks like
-----BEGIN CERTIFICATE-----
MIICkjCCAjigAwIBAgIRAPejXciB/OqJDsnGrmHcdOMwCgYIKoZIzj0EAwIwOzEL
MAkGA1UEBhMCVVMxHjAcBgNVBAoTFUdvb2dsZSBUcnVzdCBTZXJ2aWNlczEMMAoG
A1UEAxMDV0UxMB4XDTI2MTExNjE1NDYyMFoXDTI3MDIxNDE2NDYwM1owGTEXMBUG
A1UEAxMOY2xvdWRmbGFyZS5jb20wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAT...
-----END CERTIFICATE-----
Three things are worth noticing:
- The header and footer define the content type. A certificate always starts with
BEGIN CERTIFICATE. - The Base64 lines are usually 64 characters long and may contain upper and lower case letters, digits,
+,/and=. - Interleaved text (comments, for example) is not part of PEM and may confuse a parser.
PEM, DER, PKCS#12 and CRT/KEY
The file extension does not determine the content — the content does. The same .crt file can hold PEM or DER.
| Format | What it is | Typical use |
|---|---|---|
| PEM | Base64 text with header and footer lines | Nginx, Apache, Let's Encrypt, most APIs |
| DER | Binary ASN.1 encoding | Windows certificate store, Java, some devices |
| PKCS#12 / PFX | Binary container: certificate + private key (+ chain), usually password-protected | Windows/IIS, macOS, import/export |
| CRT / CER | A name that may mean a PEM or DER certificate | Generic certificate file |
| KEY | Private key (PEM or DER) | Server configuration |
One or several certificates in one file
A PEM file can contain several blocks in sequence. A typical server chain is:
- Leaf certificate — the certificate for your domain.
- Intermediate certificate — the CA certificate proving who issued the leaf.
- Root certificate — usually left out of the chain, because clients already know it from their trust store.
Order matters: the leaf always comes first. In Nginx such a file is called fullchain.pem:
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
In Apache (2.4.8 and newer) a single file containing both the leaf and the chain is often enough:
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Why this matters: if the server sends only the leaf certificate, clients have to find the intermediate themselves. Some clients cope, some do not — the result is “unable to get local issuer certificate” or a broken chain on older devices.
How to read PEM
The fastest route is the ssl.ee PEM decoder: paste one or several certificates and you will see the subject, issuer, validity period, public key, SHA-256 fingerprint and SAN names. Decoding happens entirely in your browser — no request is sent anywhere.
On the command line, use OpenSSL:
# Everything in a human-readable form
openssl x509 -in cert.pem -noout -text
# Just the basics
openssl x509 -in cert.pem -noout -subject -issuer -dates
# SAN names and fingerprint
openssl x509 -in cert.pem -noout -ext subjectAltName
openssl x509 -in cert.pem -noout -fingerprint -sha256
# Check whether the leaf matches the chain
openssl verify -untrusted chain.pem cert.pem
On Windows you can do the same with a built-in tool:
certutil -dump cert.pem
Converting between formats
# DER → PEM
openssl x509 -inform DER -in cert.der -out cert.pem
# PEM → DER
openssl x509 -in cert.pem -outform DER -out cert.der
# PKCS#12 → PEM (certificate + private key)
openssl pkcs12 -in bundle.p12 -out bundle.pem -nodes
# PEM → PKCS#12
openssl pkcs12 -export -out bundle.p12 \
-inkey key.pem -in cert.pem -certfile chain.pem
-nodes means “no DES”, so the private key is written without an additional passphrase. If you do that, guard the output file permissions carefully.
Private keys and security
A private key starts with BEGIN PRIVATE KEY (PKCS#8), BEGIN RSA PRIVATE KEY (PKCS#1) or BEGIN EC PRIVATE KEY. The rules are simple:
- Never send a private key by email, in a chat or to a third party.
- Keep the file at
chmod 600, owned only by the service user. - If you suspect a leak, issue a new certificate — revoking the old one alone does not help once the key is out.
- Check that the certificate and key match (for RSA):
openssl x509 -noout -modulus -in cert.pem | openssl sha256
openssl rsa -noout -modulus -in key.pem | openssl sha256
If the hashes match, they belong together. For ECDSA keys, use openssl pkey -pubout and compare the public keys.
Common errors
| Symptom | Cause |
|---|---|
| The browser says “certificate is not trusted” | The chain is missing an intermediate, or the order is wrong. |
no start line | The file is not PEM (it may be DER) or the header lines are damaged. |
bad base64 decode | Whitespace, line breaks or truncated lines were introduced while copying. |
| The private key does not match the certificate | The wrong key was used, or only one of the two was renewed. |
| The certificate has expired | Renewal was skipped or the server was not reloaded. |
Paste a PEM and read every field instantly — no file upload.
Frequently asked questions
Does a PEM file contain the private key?
Sometimes yes, sometimes no. A certificate PEM contains only the public part. If the file has a separate BEGIN PRIVATE KEY block, that part is sensitive and must not be shared.
Can I put several certificates in one file?
Yes. That is the normal form of a chain. The order must be leaf → intermediate → (optionally) root.
Why is the content of .crt and .pem sometimes identical?
Because the extension is only a convention. Many CAs hand out PEM content in a file ending in .crt. Check the content, not the name.
Does decoding PEM send the data to a server?
Not with the ssl.ee decoder. All work happens in your browser in JavaScript and no network request is made.