Certificate formats
Different servers require different certificate file formats when deploying SSL/TLS. Common formats include:
- PEM
- Used by web servers such as Apache, NGINX, and Caddy
- Common extensions: .pem, .crt, .cer, and .key
- Can contain certificates, private keys, or both
- The .key extension is normally used for private keys
- PFX
- Used by servers such as IIS
- Common extensions: .pfx and .p12
- Contains both the certificate and private key, usually protected by a password
- JKS
- Used by Java servers such as Tomcat, WebLogic, JBoss, and Jetty
- Common extension: .jks
A Let's Encrypt certificate download typically includes:
- cert.key (PEM): private key
- cert.cer (PEM): leaf certificate
- fullchain.cer (PEM): leaf certificate and intermediate certificates
The following commands generate cert.pfx and cert.jks from the PEM files and convert between these formats.
PEM ===> PFX
- Tool: openssl
- Command: create cert.pfx from cert.key and fullchain.cer
openssl pkcs12 -export -out cert.pfx -inkey cert.key -in fullchain.cerPFX ===> JKS
- Tool: keytool
- Command: create cert.jks from cert.pfx
keytool -importkeystore -srckeystore cert.pfx -destkeystore cert.jks -srcstoretype PKCS12 -deststoretype JKSPEM ===> JKS
- Use the two procedures above: first convert PEM to PFX, then convert PFX to JKS.
PFX ===> PEM
- Tool: openssl
- Step 1: export cert.pfx to a temporary temp.cer file containing the certificate and private key.
openssl pkcs12 -in cert.pfx -nodes -out temp.cer- Step 2: extract the private key from temp.cer into cert.key.
openssl rsa -in temp.cer -out cert.key- Step 3: extract the certificate from temp.cer into cert.cer.
openssl x509 -in temp.cer -out cert.cer- Step 4: extract intermediate certificates from cert.pfx into chain.cer. Combine cert.cer, a blank line, and chain.cer to create fullchain.cer.
openssl pkcs12 -in cert.pfx -cacerts -nokeys -chain | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > chain.cer
echo '\n' > emptyline.cer
cat cert.cer emptyline.cer chain.cer> fullchain.cerJKS ===> PFX
- Tool: keytool
- Command: create cert.pfx from cert.jks
keytool -importkeystore -srckeystore cert.jks -destkeystore cert.pfx -srcstoretype JKS -deststoretype PKCS12
An online certificate format converter is also available.