OpenSSL Cheat Sheet: Certificates, CSRs, Keys and s_client
Checked against OpenSSL OpenSSL 3.5 LTS docs (3.5.9) on 28 November 2026. Each section links to the official docs it was checked against.
This sheet is for developers who touch certificates a few times a year and need the exact command fast: read a cert, test a server, convert a file, make a CSR. Flags follow the OpenSSL 3.5 manual pages, because 3.5 is the long-term support line (3.5.9, supported until April 2030) and it is what most current Linux distributions will carry. The openssl binary was not available in our test shell, so every command row is marked untested; the same operations were checked with Python's cryptography 48 library, which links OpenSSL 3.
When to use what
Start with x509 for anything that is already a certificate, req for anything that is a request or a fresh test cert, and pkey for keys of any type. The older per-algorithm commands like rsa still work, but the rsa man page itself points you to pkey, and pkey handles EC and Ed25519 keys too.
To debug a live server, use s_client, not a browser. A browser fills in missing intermediates from its cache and hides the exact problem your API client or curl will hit. Pair -showcerts with -verify_return_error so a broken chain fails loudly instead of scrolling past.
For file formats, ask what the consumer wants. Nginx and most Linux tools read PEM. Java keystores and Windows imports usually want PKCS#12 (.p12 or .pfx). DER shows up in Android and some embedded devices. If you only need to look inside a cert, paste it into the SSL certificate decoder instead; to check a public site's expiry and chain from the browser, the SSL checker is quicker. The SSL/TLS certificates guide explains chains and trust if the output still makes no sense.
-fingerprint uses SHA-1 unless you pass a digest such as -sha256.
openssl x509 -in cert.pem -noout -textPrint every field of a PEM certificate without echoing the PEM block back
openssl x509 -in cert.pem -noout -subject -issuerShow who the cert is for and who signed it; equal values mean self-signed
openssl x509 -in cert.pem -noout -datesPrint the notBefore and notAfter validity window
openssl x509 -in cert.pem -noout -checkend 2592000Exit non-zero if the cert expires within 30 days, handy in a cron or CI check
openssl x509 -in cert.pem -noout -ext subjectAltNameList only the DNS names and IPs the cert is valid for
openssl x509 -in cert.pem -noout -fingerprint -sha256Print the SHA-256 fingerprint for pinning or comparing two files
openssl x509 -in cert.pem -noout -serialPrint the serial number, which revocation lists and CA support ask for
SNI is sent by default since OpenSSL 1.1.1, using the -connect host name. Pass -servername only when you connect by IP or need a different name. -showcerts prints the certificates as the server sent them, which is not the same as a verified chain.
openssl s_client -connect example.com:443 -servername example.com </dev/nullOpen a TLS connection, print the chain summary and close at once instead of waiting for input
openssl s_client -connect example.com:443 -showcerts </dev/nullDump every certificate the server sends, to spot a missing intermediate
openssl s_client -connect example.com:443 -verify_return_error </dev/nullAbort the handshake on any verification error rather than carrying on
openssl s_client -connect example.com:443 -brief </dev/nullPrint just the protocol, cipher and peer summary
openssl s_client -connect example.com:443 </dev/null | openssl x509 -noout -datesRead the live leaf cert's expiry dates in one pipeline
openssl verify -CAfile root.pem -untrusted intermediate.pem cert.pemVerify a local leaf against a root, supplying intermediates for chain building
PEM is base64 text between -----BEGIN lines. DER is the same data in binary; a DER cert starts with the bytes 30 82.
openssl x509 -in cert.pem -outform DER -out cert.derConvert a PEM certificate to binary DER
openssl x509 -in cert.der -inform DER -out cert.pemConvert a DER certificate back to PEM
openssl pkcs12 -export -in cert.pem -inkey key.pem -certfile chain.pem -out bundle.p12Bundle a cert, its key and intermediates into one password-protected PKCS#12 file
openssl pkcs12 -in bundle.p12 -nokeys -out certs.pemExtract only the certificates from a .p12 or .pfx
openssl pkcs12 -in bundle.p12 -nocerts -noenc -out key.pemExtract the private key unencrypted; protect the output file
openssl pkcs12 -export -legacy -in cert.pem -inkey key.pem -out old.p12Use the old RC2/3DES encryption for importers that reject AES
Key generation flags are on the genpkey page (https://docs.openssl.org/3.5/man1/openssl-genpkey/). Without -days, req -x509 makes a cert valid for only 30 days.
openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out key.pemGenerate a P-256 private key
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:3072 -out key.pemGenerate an RSA key larger than the 2048-bit default
openssl genpkey -algorithm ED25519 -out key.pemGenerate an Ed25519 key
openssl req -new -key key.pem -subj "/CN=api.example.com" -addext "subjectAltName=DNS:api.example.com" -out req.csrCreate a CSR from an existing key with the SAN set on the command line
openssl req -in req.csr -noout -text -verifyCheck the CSR's self-signature and print what you are about to send to the CA
openssl req -x509 -newkey rsa -noenc -keyout key.pem -out cert.pem -days 365 -subj "/CN=localhost" -addext "subjectAltName=DNS:localhost"Make a self-signed test cert and new unencrypted key in one command
openssl pkey -in key.pem -pubout -out pub.pemWrite the public half of any private key type
openssl pkey -in key.pem -noout -textPrint the key's algorithm, size and components
openssl x509 -in cert.pem -noout -pubkey | openssl sha256Hash the cert's public key; compare it with the next row to prove a match
openssl pkey -in key.pem -pubout | openssl sha256Hash the key's public half; identical output means the key belongs to the cert
Worked example: Prove a key matches a certificate before a deploy
A classic outage: someone renews a cert, copies the new cert.pem but keeps the old key.pem, and Nginx refuses to start with a key mismatch error. Check the pair before you reload.
openssl x509 -in cert.pem -noout -pubkey | openssl sha256
openssl pkey -in key.pem -pubout | openssl sha256
openssl x509 -in cert.pem -noout -checkend 604800 && echo "valid for 7+ days"
The first two lines extract the public key from each file and hash it. If the two hashes are identical, the key belongs to the cert; if they differ, find the key that was generated with the CSR. This works for RSA, EC and Ed25519 because it compares the whole public key block rather than an RSA modulus. The last line exits 0 only when the cert is still valid a week from now, so you can put it in a deploy script. We could not run openssl here, so we checked the same logic with Python's cryptography library: the public key of a generated cert matched its own key and did not match a second, fresh P-256 key.
Common mistakes
Relying on the Common Name and leaving out subjectAltName
Browsers and modern clients check SAN, not CN. Add
-addext "subjectAltName=DNS:host"to every CSR and test cert.Serving only the leaf certificate without the intermediates
Concatenate the leaf and intermediates in the file your server reads, then confirm with
s_client -showcertsthat the chain is complete.Forgetting
-dayson a self-signed test certThe default is 30 days, so your local setup breaks next month. Pass an explicit
-days.Still typing
-nodesfrom old blog postsIt is deprecated since 3.0. Use
-noenc, which means the same thing: do not encrypt the private key.Leaving
s_clienthanging and assuming the server frozeIt waits for input after the handshake. Redirect
</dev/nullso it exits after printing.
Related cheat sheets
Sources
- https://openssl-library.org/source/
- https://docs.openssl.org/3.5/man1/openssl-x509/
- https://docs.openssl.org/3.5/man1/openssl-req/
- https://docs.openssl.org/3.5/man1/openssl-s_client/
- https://docs.openssl.org/3.5/man1/openssl-pkcs12/
- https://docs.openssl.org/3.5/man1/openssl-genpkey/
- https://docs.openssl.org/3.5/man1/openssl-pkey/
- https://docs.openssl.org/3.5/man1/openssl-verify/
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.