Introduction
PEM is the usual text form of keys and certificates. It takes binary data in
DER encoding, writes it as base64 and wraps it between a
-----BEGIN …----- line and an -----END …----- line that say
what it is. The name comes from Privacy Enhanced Mail, an early standard for secure
email; the mail standard faded but its text encoding became universal. RFC 7468
describes it as it is used today.
The same encoding is saved under several extensions: .pem for anything,
.crt and .cer for certificates, .key for private
keys and .csr for certificate signing requests. A .crt or
.cer file can also hold the raw binary DER form instead, which starts with
the byte 30 rather than with text.
Structure
A PEM file is plain text holding one or more blocks. Each block is a BEGIN line with a label, the base64 encoding of the DER data in lines of 64 characters, and an END line with the same label. Text outside the blocks is ignored, so files often carry a description above each block. A certificate chain is several CERTIFICATE blocks one after another.
The label tells software what the data is. Common labels are CERTIFICATE,
PRIVATE KEY for a PKCS #8 private key, ENCRYPTED PRIVATE KEY,
RSA PRIVATE KEY and EC PRIVATE KEY for older key formats,
PUBLIC KEY, CERTIFICATE REQUEST and X509 CRL.
-----BEGIN CERTIFICATE-----
MIIDdzCCAl+gAwIBAgIEAgAAuTANBgkqhkiG9w0BAQUFADBaMQswCQYDVQQGEwJJ
…
-----END CERTIFICATE-----
Not described
These pages follow RFC 7468 and OpenSSL’s pem.h. The explorer has no PEM viewer, so a dropped
file shows as bytes.
Known, not yet covered
- Reference: RFC 7468’s ABNF in its standard, lax and strict forms, its notes on file
extensions, and its rule about
PRIVATE KEYagainst OneAsymmetricKey versions. - Viewer: there is no PEM plugin: the labels, headers and base64 body are not decoded into the DER structures inside.
Unknown
- Reference: the contents behind several labels that the OpenSSL manual pages read do not
describe:
DSA PUBLIC KEY,ECDSA PUBLIC KEY,SSL SESSION PARAMETERS,SM2 PRIVATE KEY,SM2 PARAMETERSandECHCONFIG.
Also used for
- Apple Keynote presentation – Apple Keynote presentation: a ZIP package of IWA files and media.
- Commodore 64 cartridge image – Commodore 64 cartridges: a C64 CARTRIDGE header, then one CHIP packet per bank.
- DER certificate – Certificates and keys in binary ASN.1: tag, length and value fields.
- DER certificate – Certificates and keys in binary ASN.1: tag, length and value fields.
Links
Software
- OpenSSL – command-line toolkit that makes, reads and converts PEM keys and certificates (Windows, macOS, Linux)
- GnuTLS certtool – command-line tool for keys and certificates (Linux, macOS, Windows)
- KeyStore Explorer – free key and certificate manager (Windows, macOS, Linux)
- XCA – certificate and key manager with a small certificate authority (Windows, macOS, Linux)
- Keychain Access – built into macOS, imports PEM certificates and keys (macOS)
- Windows certificate viewer – opens
.crtand.cerfiles (Windows)
Developers
- OpenSSL libcrypto – library that reads and writes PEM keys and certificates (C)
- cryptography – library that loads and writes PEM and DER (Python)
- encoding/pem – standard library package that encodes and decodes PEM blocks (Go)
- Bouncy Castle – libraries with PEM readers and writers (Java, C#)
Reference
- RFC 7468 – Textual Encodings of PKIX, PKCS and CMS Structures