Half of all “my certificate does not work” tickets are really “wrong file format”. The same certificate can be stored in several different file formats, each tool wants a particular one, and the file extension often tells you nothing. This page is your decoder ring: what the formats are, how to tell them apart, and how to convert between them.
Encoding: DER and PEM
Keys and certificates are ASN.1 structures, the strict data-description format from the previous page. There are two ways to write that structure to a file, and almost every format question is “which one is this file?”:
| Encoding | What it is | Typical extensions |
|---|---|---|
| DER | Raw binary | .der, .cer, .crt (sometimes) |
| PEM | DER, base64-encoded, between -----BEGIN ...----- lines | .pem, .crt, .key, .csr |
Binary DER cannot be pasted into an email or a YAML file, which is why PEM
exists: it is the same bytes made printable. The -----BEGIN ...----- line
tells you what is inside. File extensions mean little: .crt may be PEM or
DER, .key is just a convention.
| BEGIN line | Content |
|---|---|
CERTIFICATE | An X.509 certificate |
CERTIFICATE REQUEST | A CSR (PKCS#10) |
PRIVATE KEY | Unencrypted private key, PKCS#8 |
ENCRYPTED PRIVATE KEY | Passphrase-protected PKCS#8 |
RSA PRIVATE KEY | Old RSA-only PKCS#1 format |
EC PRIVATE KEY | Old EC-only SEC1 format |
PUBLIC KEY | Public key (SubjectPublicKeyInfo) |
PKCS7 | A bundle of certificates (P7B) |
Convert between them:
$ openssl x509 -in web.crt -outform DER -out web.der
$ file web.der
web.der: Certificate, Version=3
$ openssl x509 -inform DER -in web.der -out web-again.pem
$ openssl pkey -in ec.key -outform DER -out ec.der
$ xxd ec.der | head -2
00000000: 3081 8702 0100 3013 0607 2a86 48ce 3d02 0.....0...*.H.=.
00000010: 0106 082a 8648 ce3d 0301 0704 6d30 6b02 ...*.H.=....m0k.
(30 is the ASN.1 SEQUENCE tag: every DER structure starts with it, so a
file whose first byte is 30 is probably DER. A text file starting with
-----BEGIN is PEM.)
A PEM file can hold several objects. A certificate bundle is just certificates pasted one after the other, leaf first:
$ cat web.crt int.crt > fullchain.pem
$ grep -c BEGIN fullchain.pem
2
The PKCS family
PKCS (Public-Key Cryptography Standards) are a numbered series of documents written by RSA Labs in the 1990s that defined how to store and exchange keys, certificates and requests. Many have since been taken over by the IETF as RFCs (PKCS#7 became CMS, for example). They are not competing formats: each one covers a different job, which is why you meet several in one day. The ones you meet:
| Standard | Name | What it is | You see it as |
|---|---|---|---|
| PKCS#1 | RSA cryptography | RSA key format and RSA signature/encryption padding | BEGIN RSA PRIVATE KEY; the RSASSA-PSS and OAEP names |
| PKCS#5 | Password-based crypto | PBKDF2 and PBES2: turning a password into a key | ENCRYPTED PRIVATE KEY |
| PKCS#7 / CMS | Cryptographic Message Syntax | Signed and encrypted messages; certificate bundles | .p7b, .p7c, S/MIME email, SCEP messages |
| PKCS#8 | Private key info | A generic wrapper for any private key type | BEGIN PRIVATE KEY |
| PKCS#10 | Certification request | The CSR | BEGIN CERTIFICATE REQUEST |
| PKCS#11 | Cryptoki | The C API for talking to HSMs, smart cards and tokens | .so / .dll “module” files; see Protecting keys |
| PKCS#12 | Personal information exchange | An encrypted archive of key + certificate + chain | .p12, .pfx |
PKCS#1 vs PKCS#8
PKCS#1 only holds RSA keys, and its header (BEGIN RSA PRIVATE KEY) reveals
that. PKCS#8 holds any kind of key (RSA, EC, Ed25519) and records which
algorithm it is, so it is the modern default and shows up as
BEGIN PRIVATE KEY. OpenSSL 3+ writes PKCS#8 by default. You usually only
need the old form when a legacy tool rejects the new one:
$ head -1 r2.key
-----BEGIN PRIVATE KEY-----
$ openssl rsa -in r2.key -traditional -out r2.pkcs1.key
writing RSA key
$ head -1 r2.pkcs1.key
-----BEGIN RSA PRIVATE KEY-----
Convert the other way (old tool wants PKCS#8, or the reverse) with
openssl pkcs8 -topk8 -nocrypt -in old.key -out new.key.
PKCS#10: the CSR
You created one on the previous page. A CSR holds a public key, the requested names, and a signature made with the matching private key. It never contains the private key. A CSR is the thing you send to a CA, so it is safe to email, but always inspect one before you sign it (if you run a CA), and before you submit it (if you do not): the names in it become the names in the certificate.
$ openssl req -in web.csr -noout -text | grep -E "Subject:|DNS:"
Subject: CN=www.example.com
DNS:www.example.com, DNS:example.com
PKCS#7: a bundle of CA certificates
$ openssl crl2pkcs7 -nocrl -certfile int.crt -certfile root.crt -out ca.p7b
$ openssl pkcs7 -in ca.p7b -print_certs -noout
subject=O=Example Corp, CN=Example Issuing CA
issuer=O=Example Corp, CN=Example Root CA
subject=O=Example Corp, CN=Example Root CA
issuer=O=Example Corp, CN=Example Root CA
This is the format to use when a vendor says “send us the CA bundle in P7B”: it holds only certificates, never private keys. PKCS#7 (now called CMS) can also carry signed or encrypted data, and that is how S/MIME email signatures work. SCEP uses it for the whole request and response (see enrollment).
PKCS#12: one file for key, certificate and chain
Unlike the formats above, a PKCS#12 file contains the private key along with the certificate and its chain, all encrypted with a password. It exists to move a complete identity from one place to another: this is what Windows, macOS keychains, Java and many VPN clients import. Treat it as a private key: anyone with the file and its password is that identity.
$ openssl pkcs12 -export -inkey web.key -in web.crt -certfile int.crt \
-name web -passout pass:changeit -out web.p12
$ openssl pkcs12 -in web.p12 -passin pass:changeit -info -nokeys -nodes 2>&1 | grep -E "MAC|PKCS7|Cert"
MAC: sha256, Iteration 2048
PKCS7 Encrypted data: PBES2, PBKDF2, AES-256-CBC, Iteration 2048, PRF hmacWithSHA256
Certificate bag
Certificate bag
(-name web sets a friendly label inside the archive. The MAC line is an
integrity check on the whole file, and PBES2, PBKDF2, AES-256-CBC shows the
encryption the password produces: the same password-to-key technique from the
cryptography page.)
Extract the parts again:
$ openssl pkcs12 -in web.p12 -passin pass:changeit -nodes -nokeys -clcerts -out leaf.pem
$ openssl pkcs12 -in web.p12 -passin pass:changeit -nodes -nocerts -out key.pem
.p12 made years ago with RC2 or 3DES encryption may fail on OpenSSL 3+
with unsupported. Add -legacy to read it, then re-export it with the
modern defaults above. On the other side, some old Java or Windows
versions cannot read modern .p12 files; fix by upgrading, not by
weakening the encryption.Why a file “does not load”: a checklist
Work down this list in order. It is roughly the order in which things go wrong.
- Wrong format.
file xandhead -1 xtell you PEM or DER, and what. - Key and certificate do not match. Compare the public-key hashes (see above).
- Missing intermediate.
openssl verify -untrusted. - Wrong order in the bundle. Leaf first, then each issuer.
- Windows line endings or stray text before
-----BEGIN. - Encrypted key, and the program cannot prompt. Servers cannot type a
passphrase on boot; they need an unencrypted key (protected with
0600permissions), a secret store, or an HSM/KMS. - Wrong usage. The certificate says
serverAuthonly and you are using it for client authentication.
Names for the same key in other tools
| Tool | Where it keeps keys |
|---|---|
| OpenSSL, nginx, Apache, curl, Go, Python | PEM files |
| Java | Keystore (.jks or .p12), via keytool |
| Windows | Certificate store (certmgr.msc), imports .pfx |
| macOS | Keychain, imports .p12 |
| Kubernetes | Secret of type kubernetes.io/tls (PEM) |
| HSM / smart card | Not exportable; addressed with a pkcs11: URI |
About BastionXP
BastionXP is a cloud-native private PKI CA and device identity management platform that eliminates passwords and shared secrets by issuing hardware-attested certificates to mobile devices and laptops. Secure access to your Wi-Fi, VPN, and SaaS applications in your enterprise network. Replace legacy SCEP with automated ACME Device Attestation. No shared secrets. Zero credential theft. Learn more about BastionXP →