Skip to main content

PEM

Privacy Enhanced Mail (PEM) is a Base64-encoded format for storing cryptographic keys, certificates, and other security-related data. Despite its name, PEM is widely used beyond email applications for various cryptographic purposes. The encoding is defined by RFC 7468.

Structure Format

ComponentDescriptionExample
Begin MarkerHeader identifying content type-----BEGIN CERTIFICATE-----
HeadersOptional key-value metadata pairsProc-Type: 4,ENCRYPTED
Encoded DataBase64-encoded binary contentMIIHzTCCBbWgAwIBAgIQaBYE3/M08XHYCnNVmcFBcjANBgkqhkiG9w0BAQsFADBy...
End MarkerFooter matching the begin marker-----END CERTIFICATE-----

Common PEM Types

RFC 7468 Standard Types

TypeBegin/End LabelDescriptionUse Case
CertificateCERTIFICATEX.509 public key certificateSSL/TLS, code signing
Certificate Revocation ListX509 CRLX.509 certificate revocation listCertificate validity checking
Certificate RequestCERTIFICATE REQUESTPKCS#10 certificate signing requestCertificate authority requests
Private KeyPRIVATE KEYPKCS#8 unencrypted private keyGeneral-purpose private key storage
Encrypted Private KeyENCRYPTED PRIVATE KEYPKCS#8 encrypted private keyPassword-protected private key storage
Public KeyPUBLIC KEYX.509 SubjectPublicKeyInfoPublic key distribution
PKCS#7 MessagePKCS7Legacy cryptographic message syntaxCertificate chains, signed data (legacy)
CMS MessageCMSCryptographic Message SyntaxCertificate chains, signed data (modern)
Attribute CertificateATTRIBUTE CERTIFICATEX.509 attribute certificate (RFC 5755)Authorization attributes

Algorithm-Specific Types (OpenSSL)

TypeBegin/End LabelDescriptionUse Case
RSA Private KeyRSA PRIVATE KEYPKCS#1 RSA private keyRSA-specific private keys
RSA Public KeyRSA PUBLIC KEYPKCS#1 RSA public keyRSA-specific public keys
DSA Private KeyDSA PRIVATE KEYDSA private keyDSA cryptography
EC Private KeyEC PRIVATE KEYElliptic Curve private keyEC cryptography
DH ParametersDH PARAMETERSDiffie-Hellman parametersKey exchange configuration

Encrypted PEM Format

The headers below predate RFC 7468 and are defined by RFC 1421. They appear on legacy OpenSSL-encrypted keys.

FieldDescriptionExample
Proc-TypeProcessing type and encryption flagProc-Type: 4,ENCRYPTED
DEK-InfoEncryption algorithm and IVDEK-Info: AES-256-CBC,A1B2C3D4E5F6...
Encrypted DataBase64-encoded encrypted contentEncrypted binary data...

Types Used by DataStream

Of the types listed above, the following are the ones DataStream reads and writes.

LabelReadWrittenContext
CERTIFICATEYYTLS certificates. The only label accepted when reading a certificate—other blocks in a bundle are skipped
PRIVATE KEYYYPKCS#8 keys. What -pfx2pem writes, and what a PKCS#8 encrypted key is re-emitted as once decrypted
ENCRYPTED PRIVATE KEYYNPKCS#8 encrypted keys, decrypted with the configured passphrase
RSA PRIVATE KEYYYPKCS#1 keys, including legacy Proc-Type/DEK-Info encrypted ones. A legacy-encrypted key is re-emitted under its own original label, not converted to PRIVATE KEY
EC PRIVATE KEYYYElliptic-curve keys. Written only for ephemeral self-signed TLS
ECDSA PRIVATE KEYYNAccepted as a synonym of EC PRIVATE KEY
PUBLIC KEYYYPKIX public keys, used for signature verification

The Director's -pfx2pem option converts a PFX bundle into exactly two files: key.pem, holding a PRIVATE KEY block, and cert.pem, holding the leaf CERTIFICATE. Both are written to the current directory. Intermediate and root certificates in the bundle are not written.

The conversion checks the leaf certificate first and refuses to write anything when it has expired or is not yet valid. An unknown signing authority is not a reason to refuse — a self-signed bundle converts normally.

Example Structure

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC,A1B2C3D4E5F67890A1B2C3D4E5F67890

MIIEpAIBAAKCAQEA2Z3QX0KZVE9I+sLlmEUKkYgJiEQSvfNF6JUVNBQdHPvs
kNkRFWGLQQEjLXPOCjGhvQZZLvbPjVZGKlnTJ1yJQvzjhvnP0zJhExFmKWz8
...
-----END RSA PRIVATE KEY-----

PEM files are text-based, human-readable, and can contain multiple objects separated by blank lines. They're commonly used in web servers, email systems, and various security applications.