Commands tested with OpenSSL 4.1 (they also work with 3.x unless noted). Replace the file names with your own.
Help and version
$ openssl version -a
$ openssl list -providers
$ openssl list -cipher-algorithms | head
$ openssl ecparam -list_curves | head
$ openssl <command> -help
Random and hashes
$ openssl rand -hex 32
$ openssl rand -base64 32
$ openssl dgst -sha256 file
$ openssl dgst -sha3-256 file
$ openssl mac -digest SHA256 -macopt hexkey:<hex> -in file HMAC
Keys
$ openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out ec.key
$ openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:3072 -out rsa.key
$ openssl genpkey -algorithm ED25519 -out ed.key
$ openssl genpkey -algorithm ML-DSA-65 -out ml.key # post-quantum
$ openssl pkey -in ec.key -pubout -out ec.pub # extract public key
$ openssl pkey -in ec.key -text -noout # inspect
$ openssl pkey -in ec.key -aes-256-cbc -out ec.enc.key # add passphrase
$ openssl pkey -in ec.enc.key -out ec.key # remove passphrase
$ openssl pkey -in ec.key -outform DER -out ec.der # PEM to DER
$ openssl rsa -in k.key -traditional -out k.pkcs1.key # PKCS#8 to PKCS#1
$ openssl pkcs8 -topk8 -nocrypt -in old.key -out new.key # to PKCS#8
Sign, verify, encrypt
$ openssl dgst -sha256 -sign ec.key -out sig file
$ openssl dgst -sha256 -verify ec.pub -signature sig file
$ openssl pkeyutl -encrypt -pubin -inkey rsa.pub -pkeyopt rsa_padding_mode:oaep -in f -out f.enc
$ openssl pkeyutl -decrypt -inkey rsa.key -pkeyopt rsa_padding_mode:oaep -in f.enc
$ openssl pkeyutl -derive -inkey a.key -peerkey b.pub # ECDH
$ openssl enc -aes-256-cbc -pbkdf2 -iter 600000 -salt -in f -out f.enc
$ openssl enc -d -aes-256-cbc -pbkdf2 -iter 600000 -in f.enc
CSRs
$ openssl req -new -key web.key -subj "/CN=www.example.com" \
-addext "subjectAltName=DNS:www.example.com" -out web.csr
$ openssl req -in web.csr -noout -text
$ openssl req -in web.csr -noout -verify
Certificates
$ openssl req -x509 -new -key root.key -sha384 -days 3650 -subj "/CN=Root" \
-addext "basicConstraints=critical,CA:TRUE" \
-addext "keyUsage=critical,keyCertSign,cRLSign" -out root.crt # self-signed
$ openssl x509 -req -in x.csr -CA ca.crt -CAkey ca.key -CAcreateserial \
-days 90 -sha256 -extfile x.ext -out x.crt # sign a CSR
$ openssl x509 -in x.crt -noout -text
$ openssl x509 -in x.crt -noout -subject -issuer -dates
$ openssl x509 -in x.crt -noout -ext subjectAltName
$ openssl x509 -in x.crt -noout -fingerprint -sha256
$ openssl x509 -in x.crt -noout -serial
$ openssl x509 -in x.crt -noout -checkend 2592000 # fails if < 30 days left
$ openssl x509 -in x.crt -pubkey -noout | openssl sha256 # key match check
$ openssl x509 -in x.crt -outform DER -out x.der
$ openssl x509 -inform DER -in x.der -out x.pem
$ openssl asn1parse -in x.crt
Chains and verification
$ openssl verify -CAfile root.crt -untrusted int.crt leaf.crt
$ openssl verify -CAfile bundle.pem leaf.crt
$ openssl verify -CAfile root.crt -untrusted int.crt -verify_hostname www.example.com leaf.crt
$ cat leaf.crt int.crt > fullchain.pem
Look at a live server
$ openssl s_client -connect host:443 -servername host -showcerts </dev/null
$ openssl s_client -connect host:443 -servername host </dev/null 2>/dev/null | openssl x509 -noout -dates -subject -issuer
$ openssl s_client -connect host:443 -tls1_3 </dev/null # force a version
$ openssl s_client -connect host:443 -cert c.crt -key c.key # mTLS client
$ openssl s_client -starttls smtp -connect mail:587 </dev/null # STARTTLS
$ openssl s_server -accept 8443 -cert leaf.crt -cert_chain int.crt -key leaf.key -www
PKCS bundles
$ openssl pkcs12 -export -inkey k.key -in c.crt -certfile int.crt -name web -out b.p12
$ openssl pkcs12 -in b.p12 -nodes -nocerts -out key.pem
$ openssl pkcs12 -in b.p12 -nokeys -clcerts -out leaf.pem
$ openssl pkcs12 -in b.p12 -nokeys -cacerts -out chain.pem
$ openssl pkcs12 -in old.p12 -legacy -nodes # very old files
$ openssl crl2pkcs7 -nocrl -certfile int.crt -certfile root.crt -out ca.p7b
$ openssl pkcs7 -in ca.p7b -print_certs -noout
Revocation
$ openssl crl -in ca.crl -noout -text
$ openssl crl -inform DER -in ca.crl -noout -nextupdate
$ openssl ocsp -issuer int.crt -cert leaf.crt -url http://ocsp.example.com -resp_text
JWT helpers
$ b64u() { openssl base64 -A | tr '+/' '-_' | tr -d '='; }
$ echo "$JWT" | cut -d. -f2 | tr '_-' '/+' | base64 -d # may need = padding
SSH
$ ssh-keygen -t ed25519 -f key # a key pair
$ ssh-keygen -s ca -I id -n user -V +8h key.pub # user certificate
$ ssh-keygen -s ca -I id -h -n host.example.com -V +52w host.pub # host certificate
$ ssh-keygen -L -f key-cert.pub # inspect
$ ssh-keygen -lf key.pub # fingerprint
PKCS#11 / HSM (OpenSC)
$ pkcs11-tool --module $MOD --list-slots
$ pkcs11-tool --module $MOD --login --list-objects
$ pkcs11-tool --module $MOD --login --keypairgen --key-type EC:prime256v1 --label k --id 01
$ pkcs11-tool --module $MOD --login --sign --mechanism ECDSA-SHA256 --label k -i f -o sig
$ pkcs11-tool --module $MOD --list-mechanisms
Gotchas
openssl req -x509andopenssl x509 -reqneed-extfile/-addextfor extensions; without it you get a certificate with no constraints and no SAN.- Always inspect the issued certificate; do not assume it matches the CSR.
- Passwords on the command line (
-pass pass:x) are visible inpsand shell history. Use-passin stdin,file:orenv:. openssl x509 -textprints times in GMT.- macOS’s built-in
opensslis LibreSSL; flags differ. Use Homebrew’s. openssl cais a full mini-CA with a database, useful for learning CRLs, but use a real CA product for production.
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 →