cheat-sheets

OpenSSL

OpenSSL is a toolkit for TLS and general-purpose cryptography. See TLS (Transport Layer Security), TLS Handshake, SSL Certificates Cheat-Sheet and Certutil.

Private Keys and CSRs #

COMMANDDESCRIPTION
openssl genrsa -out key.pem 4096Generate a 4096-bit RSA key
openssl ecparam -genkey -name prime256v1 -noout -out key.pemGenerate an ECDSA P-256 key
openssl genpkey -algorithm ed25519 -out key.pemGenerate an Ed25519 key
openssl req -new -key key.pem -out req.csrCreate a CSR from a key
openssl req -new -newkey rsa:4096 -nodes -keyout key.pem -out req.csrCreate key and CSR in one go
openssl req -new -key key.pem -out req.csr -subj "/C=GB/O=Example/CN=example.com" -addext "subjectAltName=DNS:example.com,DNS:www.example.com"CSR with SANs, non-interactive

Self-Signed Certificates #

COMMANDDESCRIPTION
openssl req -x509 -newkey rsa:4096 -nodes -keyout key.pem -out cert.pem -days 365 -subj "/CN=localhost" -addext "subjectAltName=DNS:localhost,IP:127.0.0.1"Self-signed cert with SANs
openssl x509 -req -in req.csr -signkey key.pem -out cert.pem -days 365Self-sign an existing CSR

Inspecting Files #

COMMANDDESCRIPTION
openssl x509 -in cert.pem -noout -textFull certificate details
openssl x509 -in cert.pem -noout -subject -issuer -datesSubject, issuer and validity
openssl x509 -in cert.pem -noout -fingerprint -sha256SHA-256 fingerprint
openssl x509 -in cert.pem -noout -ext subjectAltNameShow SANs
openssl req -in req.csr -noout -text -verifyInspect and verify a CSR
openssl verify -CAfile ca.pem cert.pemVerify a cert against a CA
openssl crl2pkcs7 -nocrl -certfile chain.pem | openssl pkcs7 -print_certs -nooutList every cert in a bundle

Checking a Key Matches a Certificate #

COMMANDDESCRIPTION
openssl x509 -in cert.pem -noout -pubkey | openssl sha256Hash of the certificate’s public key
openssl pkey -in key.pem -pubout | openssl sha256Hash of the key’s public key. Must match the line above

Remote Servers #

COMMANDDESCRIPTION
openssl s_client -connect example.com:443 -servername example.comOpen a TLS connection and show the chain
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -datesExpiry of a remote cert
openssl s_client -connect example.com:443 -tls1_2Test a specific TLS version
openssl s_client -connect example.com:443 -showcertsPrint the full chain
openssl s_client -starttls smtp -connect mail.example.com:587TLS over STARTTLS (smtp, imap, pop3, ftp)
echo | openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -checkend 2592000Exit non-zero if expiring within 30 days

Format Conversion #

COMMANDDESCRIPTION
openssl x509 -in cert.der -inform der -out cert.pemDER to PEM
openssl x509 -in cert.pem -outform der -out cert.derPEM to DER
openssl pkcs12 -export -out bundle.pfx -inkey key.pem -in cert.pem -certfile chain.pemPEM to PKCS#12 (.pfx)
openssl pkcs12 -in bundle.pfx -nodes -out all.pemPKCS#12 to PEM (unencrypted)
openssl rsa -in key.pem -out key-nopass.pemRemove a key passphrase

Hashing, Encoding and Random Data #

COMMANDDESCRIPTION
openssl rand -base64 3232 random bytes, base64
openssl rand -hex 1616 random bytes, hex
openssl dgst -sha256 fileSHA-256 of a file
openssl base64 -in file -out file.b64Base64 encode (-d to decode)
openssl enc -aes-256-cbc -pbkdf2 -salt -in file -out file.encEncrypt a file with a password
openssl enc -d -aes-256-cbc -pbkdf2 -in file.enc -out fileDecrypt it
openssl passwd -6 <PASSWORD>SHA-512 crypt hash for /etc/shadow
openssl dhparam -out dhparams.pem 2048Generate DH parameters
Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.