Skip to content

Menu

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 -text

Print every field of a PEM certificate without echoing the PEM block back

Try it: SSL Cert DecoderFrom the docs, not run by us
openssl x509 -in cert.pem -noout -subject -issuer

Show who the cert is for and who signed it; equal values mean self-signed

From the docs, not run by us
openssl x509 -in cert.pem -noout -dates

Print the notBefore and notAfter validity window

From the docs, not run by us
openssl x509 -in cert.pem -noout -checkend 2592000

Exit non-zero if the cert expires within 30 days, handy in a cron or CI check

From the docs, not run by us
openssl x509 -in cert.pem -noout -ext subjectAltName

List only the DNS names and IPs the cert is valid for

From the docs, not run by us
openssl x509 -in cert.pem -noout -fingerprint -sha256

Print the SHA-256 fingerprint for pinning or comparing two files

From the docs, not run by us
openssl x509 -in cert.pem -noout -serial

Print the serial number, which revocation lists and CA support ask for

From the docs, not run by us

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/null

Open a TLS connection, print the chain summary and close at once instead of waiting for input

Try it: SSL Certificate CheckerFrom the docs, not run by us
openssl s_client -connect example.com:443 -showcerts </dev/null

Dump every certificate the server sends, to spot a missing intermediate

From the docs, not run by us
openssl s_client -connect example.com:443 -verify_return_error </dev/null

Abort the handshake on any verification error rather than carrying on

From the docs, not run by us
openssl s_client -connect example.com:443 -brief </dev/null

Print just the protocol, cipher and peer summary

From the docs, not run by us
openssl s_client -connect example.com:443 </dev/null | openssl x509 -noout -dates

Read the live leaf cert's expiry dates in one pipeline

From the docs, not run by us
openssl verify -CAfile root.pem -untrusted intermediate.pem cert.pem

Verify a local leaf against a root, supplying intermediates for chain building

From the docs, not run by us

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.der

Convert a PEM certificate to binary DER

From the docs, not run by us
openssl x509 -in cert.der -inform DER -out cert.pem

Convert a DER certificate back to PEM

From the docs, not run by us
openssl pkcs12 -export -in cert.pem -inkey key.pem -certfile chain.pem -out bundle.p12

Bundle a cert, its key and intermediates into one password-protected PKCS#12 file

From the docs, not run by us
openssl pkcs12 -in bundle.p12 -nokeys -out certs.pem

Extract only the certificates from a .p12 or .pfx

From the docs, not run by us
openssl pkcs12 -in bundle.p12 -nocerts -noenc -out key.pem

Extract the private key unencrypted; protect the output file

From the docs, not run by us
openssl pkcs12 -export -legacy -in cert.pem -inkey key.pem -out old.p12

Use the old RC2/3DES encryption for importers that reject AES

From the docs, not run by us

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.pem

Generate a P-256 private key

From the docs, not run by us
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:3072 -out key.pem

Generate an RSA key larger than the 2048-bit default

Try it: RSA Key GeneratorFrom the docs, not run by us
openssl genpkey -algorithm ED25519 -out key.pem

Generate an Ed25519 key

From the docs, not run by us
openssl req -new -key key.pem -subj "/CN=api.example.com" -addext "subjectAltName=DNS:api.example.com" -out req.csr

Create a CSR from an existing key with the SAN set on the command line

From the docs, not run by us
openssl req -in req.csr -noout -text -verify

Check the CSR's self-signature and print what you are about to send to the CA

From the docs, not run by us
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

Try it: ssl-config-generatorFrom the docs, not run by us
openssl pkey -in key.pem -pubout -out pub.pem

Write the public half of any private key type

From the docs, not run by us
openssl pkey -in key.pem -noout -text

Print the key's algorithm, size and components

From the docs, not run by us
openssl x509 -in cert.pem -noout -pubkey | openssl sha256

Hash the cert's public key; compare it with the next row to prove a match

From the docs, not run by us
openssl pkey -in key.pem -pubout | openssl sha256

Hash the key's public half; identical output means the key belongs to the cert

From the docs, not run by us

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 -showcerts that the chain is complete.

  • Forgetting -days on a self-signed test cert

    The default is 30 days, so your local setup breaks next month. Pass an explicit -days.

  • Still typing -nodes from old blog posts

    It is deprecated since 3.0. Use -noenc, which means the same thing: do not encrypt the private key.

  • Leaving s_client hanging and assuming the server froze

    It waits for input after the handshake. Redirect </dev/null so it exits after printing.