OpenSSL Commands Reference
Essential OpenSSL commands for certificate management and troubleshooting
📋 Inspecting Certificates
View certificate details (PEM format)
openssl x509 -in certificate.crt -text -nooutDisplays all certificate information including subject, issuer, validity dates, and extensions.
View certificate details (DER format)
openssl x509 -in certificate.der -inform DER -text -nooutCheck certificate expiration dates
openssl x509 -in certificate.crt -noout -datesShows notBefore and notAfter dates.
Check if certificate will expire within N seconds
openssl x509 -in certificate.crt -noout -checkend 86400Returns exit code 0 if the certificate is valid for at least 86400 seconds (24 hours), or exit code 1 if it will expire sooner. Use 0 to check if it has already expired.
View certificate subject and issuer
openssl x509 -in certificate.crt -noout -subject -issuerCheck certificate fingerprint (SHA-256)
openssl x509 -in certificate.crt -noout -fingerprint -sha256View certificate serial number
openssl x509 -in certificate.crt -noout -serialView Subject Alternative Names (SANs)
openssl x509 -in certificate.crt -noout -ext subjectAltNameRequires OpenSSL 1.1.1 or later. Shows all DNS names and IP addresses the certificate covers.
View key usage and extended key usage
openssl x509 -in certificate.crt -noout -ext keyUsage,extendedKeyUsageRequires OpenSSL 1.1.1 or later. Shows what the certificate is authorized to do (e.g., digital signature, key encipherment, server auth, client auth).
Decode certificate purpose
openssl x509 -in certificate.crt -noout -purposeLists whether the certificate is suitable for SSL client, SSL server, S/MIME, CRL signing, and other purposes.
View certificate public key
openssl x509 -in certificate.crt -noout -pubkeyView certificate modulus (RSA certificates)
openssl x509 -in certificate.crt -noout -modulusOutputs the RSA modulus. Compare this with the modulus of a private key to verify they match.
📝 Certificate Signing Request (CSR) Operations
Generate private key and CSR (RSA 2048-bit)
openssl req -newkey rsa:2048 -keyout private.key -out request.csrCreates both a new private key and CSR in one step. You will be prompted for a passphrase and the certificate subject fields.
Generate private key and CSR without passphrase
openssl req -newkey rsa:2048 -nodes -keyout private.key -out request.csr \
-subj "/C=US/ST=California/L=San Francisco/O=Example Inc/CN=example.com"Non-interactive generation with subject provided on the command line. The -nodes flag skips key encryption. In OpenSSL 3.x, you can use -noenc instead.
Generate CSR from existing private key
openssl req -new -key private.key -out request.csrGenerate CSR with Subject Alternative Names (SANs)
openssl req -new -key private.key -out request.csr \
-subj "/CN=example.com" \
-addext "subjectAltName=DNS:example.com,DNS:www.example.com,DNS:api.example.com"Requires OpenSSL 1.1.1 or later. Use IP:192.168.1.1 syntax for IP address SANs.
Generate CSR using a configuration file
openssl req -new -key private.key -out request.csr -config openssl.cnfUses a custom OpenSSL config file for complex CSR options including SANs, custom extensions, and policy constraints. Required for SANs on OpenSSL versions older than 1.1.1.
View CSR details
openssl req -in request.csr -text -nooutVerify CSR signature
openssl req -in request.csr -verify -nooutConfirms the CSR was signed with the corresponding private key and has not been tampered with.
Generate EC key and CSR in one step (P-256)
openssl req -new -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 \
-nodes -keyout private.key -out request.csr🔑 Private Key Operations
Generate RSA private key (2048-bit)
openssl genrsa -out private.key 2048Generate RSA private key (4096-bit, encrypted)
openssl genrsa -aes256 -out private.key 4096Encrypts the key with AES-256. You will be prompted for a passphrase.
Generate EC private key (P-256)
openssl ecparam -genkey -name prime256v1 -noout -out private.keyP-256 (also known as prime256v1 or secp256r1) is the most widely supported elliptic curve. The -noout flag suppresses the EC parameters from the output.
Generate EC private key (P-384)
openssl ecparam -genkey -name secp384r1 -noout -out private.keyGenerate Ed25519 private key
openssl genpkey -algorithm Ed25519 -out private.keyRequires OpenSSL 1.1.1 or later. Ed25519 keys are always 256 bits and do not require parameter selection.
View RSA private key details
openssl rsa -in private.key -text -nooutView EC private key details
openssl ec -in private.key -text -nooutView any private key details (generic)
openssl pkey -in private.key -text -nooutWorks with RSA, EC, and Ed25519 keys. Use this when the key type is unknown.
Check RSA key modulus
openssl rsa -in private.key -noout -modulusCompare with the certificate modulus to verify the key and certificate match.
Remove passphrase from private key
openssl rsa -in encrypted.key -out decrypted.keyAdd passphrase to private key
openssl rsa -in private.key -aes256 -out encrypted.keyConvert private key to DER format
openssl pkey -in private.key -outform DER -out private.derConvert private key to PKCS#8 format
openssl pkcs8 -topk8 -in private.key -out private-pkcs8.key -nocryptPKCS#8 is the modern key format used by Java and many other platforms. Remove -nocrypt to encrypt the output key.
Extract public key from private key
openssl pkey -in private.key -pubout -out public.keyList available EC curves
openssl ecparam -list_curves🔄 Format Conversion
Convert PEM to DER
openssl x509 -in certificate.crt -outform DER -out certificate.derConvert DER to PEM
openssl x509 -in certificate.der -inform DER -out certificate.crtConvert PEM to PKCS#12/PFX (certificate and key)
openssl pkcs12 -export -out certificate.pfx -inkey private.key -in certificate.crtYou will be prompted for an export password to protect the PFX file.
Convert PEM to PKCS#12/PFX (with certificate chain)
openssl pkcs12 -export -out certificate.pfx -inkey private.key \
-in certificate.crt -certfile ca-chain.crtIncludes intermediate and root CA certificates in the PFX bundle.
Convert PKCS#12/PFX to PEM (certificate and key)
openssl pkcs12 -in certificate.pfx -out certificate.pem -nodesExtracts both the certificate and private key into a single file. The -nodes flag outputs the key unencrypted. In OpenSSL 3.x, use -noenc instead.
Extract certificate only from PKCS#12/PFX
openssl pkcs12 -in certificate.pfx -clcerts -nokeys -out certificate.crtExtract private key only from PKCS#12/PFX
openssl pkcs12 -in certificate.pfx -nocerts -nodes -out private.keyExtract CA certificates from PKCS#12/PFX
openssl pkcs12 -in certificate.pfx -cacerts -nokeys -out ca-certs.crtConvert PEM to PKCS#7
openssl crl2pkcs7 -nocrl -certfile certificate.crt -out certificate.p7bPKCS#7 (.p7b) is commonly used in Windows and Java environments. It contains only certificates and chain, not private keys.
Convert PEM to PKCS#7 (with chain)
openssl crl2pkcs7 -nocrl -certfile certificate.crt \
-certfile ca-chain.crt -out certificate.p7bConvert PKCS#7 to PEM
openssl pkcs7 -in certificate.p7b -print_certs -out certificates.crtExtracts all certificates from the PKCS#7 bundle into PEM format.
Convert PKCS#7 (DER) to PEM
openssl pkcs7 -in certificate.p7b -inform DER -print_certs -out certificates.crt🛠️ Self-Signed Certificates and CA Operations
Generate self-signed certificate (valid 365 days)
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365You will be prompted for a passphrase and certificate subject fields.
Generate self-signed certificate without passphrase
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 \
-nodes -subj "/CN=example.com"Generate self-signed certificate with SANs
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes \
-subj "/CN=example.com" \
-addext "subjectAltName=DNS:example.com,DNS:www.example.com"Requires OpenSSL 1.1.1 or later. Modern browsers require SANs and ignore the CN field for domain validation.
Generate self-signed certificate from existing key
openssl req -x509 -key private.key -out cert.pem -days 365 \
-subj "/CN=example.com"Create a CA root certificate
openssl req -x509 -newkey rsa:4096 -keyout ca.key -out ca.crt -days 3650 -nodes \
-subj "/C=US/O=My Organization/CN=My Root CA" \
-addext "basicConstraints=critical,CA:TRUE" \
-addext "keyUsage=critical,keyCertSign,cRLSign"Creates a root CA certificate valid for 10 years. The basicConstraints extension marks it as a CA, and keyUsage restricts it to certificate and CRL signing. Requires OpenSSL 1.1.1 or later.
Sign a CSR with your CA
openssl x509 -req -in request.csr -CA ca.crt -CAkey ca.key \
-CAcreateserial -out certificate.crt -days 365 -sha256Issues a certificate from the CSR using your CA. The -CAcreateserial flag generates a serial number file automatically.
Sign a CSR with your CA (preserving SANs from CSR)
openssl x509 -req -in request.csr -CA ca.crt -CAkey ca.key \
-CAcreateserial -out certificate.crt -days 365 -sha256 \
-copy_extensions copyallRequires OpenSSL 3.x. The -copy_extensions copyall flag copies all extensions (including SANs) from the CSR to the signed certificate.
✅ Verification and Validation
Verify certificate against CA bundle
openssl verify -CAfile ca-bundle.crt certificate.crtVerify certificate chain with intermediate certificates
openssl verify -CAfile root.crt -untrusted intermediate.crt certificate.crtThe -untrusted flag provides intermediate certificates that are not directly trusted but may complete the chain.
Verify certificate with verbose output
openssl verify -verbose -CAfile ca-bundle.crt certificate.crtCheck if certificate matches private key (RSA)
# Compare the modulus hashes - they should match
openssl x509 -noout -modulus -in certificate.crt | openssl sha256
openssl rsa -noout -modulus -in private.key | openssl sha256Both commands should output the same SHA-256 hash if the certificate and key are a matching pair.
Check if CSR matches private key (RSA)
# Compare the modulus hashes - they should match
openssl req -noout -modulus -in request.csr | openssl sha256
openssl rsa -noout -modulus -in private.key | openssl sha256Check if certificate, key, and CSR all match (RSA)
openssl x509 -noout -modulus -in certificate.crt | openssl sha256
openssl rsa -noout -modulus -in private.key | openssl sha256
openssl req -noout -modulus -in request.csr | openssl sha256All three commands should produce the same hash. This verifies the entire set is consistent.
Verify certificate purpose
openssl verify -purpose sslserver -CAfile ca-bundle.crt certificate.crtValidates the certificate is suitable for the specified purpose. Other values: sslclient, smimesign, smimeencrypt.
🔒 Testing SSL/TLS Connections
Connect to server and show certificate details
echo | openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -text -nooutConnects, retrieves the server certificate, and displays its full details. The echo | prefix closes the connection immediately after the handshake.
Show full certificate chain
openssl s_client -connect example.com:443 -showcertsDisplays all certificates sent by the server, including intermediate certificates in the chain.
Connect with specific TLS version
openssl s_client -connect example.com:443 -tls1_2Forces a specific TLS version. Available options: -tls1, -tls1_1, -tls1_2, -tls1_3. Useful for testing if a server supports or rejects specific protocol versions.
Connect with specific cipher suite
openssl s_client -connect example.com:443 -cipher ECDHE-RSA-AES256-GCM-SHA384Tests whether the server accepts a specific cipher suite. For TLS 1.3 ciphers, use -ciphersuites instead (e.g., -ciphersuites TLS_AES_256_GCM_SHA384).
Test with SNI (Server Name Indication)
openssl s_client -connect example.com:443 -servername example.comRequired when a server hosts multiple certificates on the same IP address. Without SNI, you may receive the default certificate instead.
Check OCSP stapling
openssl s_client -connect example.com:443 -statusRequests the OCSP staple response from the server. Look for "OCSP Response Status: successful" in the output.
Test client certificate authentication
openssl s_client -connect example.com:443 -cert client.crt -key client.keyShow TLS protocol messages and trusted CA DNs
openssl s_client -connect example.com:443 -msgDisplays all TLS protocol messages. Useful for debugging handshake failures and seeing which CAs the server trusts for client authentication.
Download server certificate to file
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null \
| openssl x509 -out server.crtTest SMTP STARTTLS
openssl s_client -connect mail.example.com:587 -starttls smtpTest IMAP STARTTLS
openssl s_client -connect mail.example.com:143 -starttls imapTest FTP STARTTLS
openssl s_client -connect ftp.example.com:21 -starttls ftpOther supported STARTTLS protocols: pop3, xmpp, ldap, postgres, mysql.
Check if a specific TLS version is supported
# Check if TLS 1.3 is supported
echo | openssl s_client -connect example.com:443 -tls1_3 2>&1 | head -5
# Check if TLS 1.1 is still enabled (it should not be)
echo | openssl s_client -connect example.com:443 -tls1_1 2>&1 | head -5A successful handshake means the protocol is supported. An error indicates the server rejects that version.
Connect with custom CA trust store
openssl s_client -connect example.com:443 -CAfile ca-bundle.crtVerifies the server certificate against a specific CA bundle instead of the system default trust store.
🔗 Certificate Chain Operations
Display the full certificate chain from a server
echo | openssl s_client -connect example.com:443 -showcerts 2>/dev/nullSave all certificates from a server chain to a file
echo | openssl s_client -connect example.com:443 -showcerts 2>/dev/null \
| sed -n '/BEGIN CERTIFICATE/,/END CERTIFICATE/p' > chain.pemVerify chain order in a bundle file
openssl crl2pkcs7 -nocrl -certfile chain.pem | openssl pkcs7 -print_certs -nooutLists all certificates in order with their subject and issuer. The leaf certificate should be first, followed by intermediates, ending with the root.
Build a certificate bundle from separate files
# Concatenate in order: leaf, intermediate(s), root
cat certificate.crt intermediate.crt root.crt > bundle.pemWeb servers typically need the leaf certificate followed by intermediates. The root certificate is usually omitted since clients already have it in their trust store.
Find the CA Issuers URL (AIA extension)
openssl x509 -in certificate.crt -noout -text | grep "CA Issuers"The Authority Information Access (AIA) extension contains a URL where the issuer certificate can be downloaded to build the chain.
📜 CRL (Certificate Revocation List) Operations
View CRL details (PEM format)
openssl crl -in crl.pem -text -nooutDisplays the CRL issuer, last update, next update, and all revoked certificate serial numbers.
View CRL details (DER format)
openssl crl -in crl.der -inform DER -text -nooutConvert CRL from DER to PEM
openssl crl -in crl.der -inform DER -out crl.pem -outform PEMVerify a certificate against a CRL
# Combine the CA certificate and CRL, then verify
cat ca.crt crl.pem > ca-crl.pem
openssl verify -crl_check -CAfile ca-crl.pem certificate.crtThe CA file must contain both the CA certificate and the CRL for revocation checking to work.
Verify checking all CRLs in the chain
openssl verify -crl_check_all -CAfile ca-crl-bundle.pem certificate.crtFind CRL distribution point in a certificate
openssl x509 -in certificate.crt -noout -text | grep -A 4 "CRL Distribution"Generate a CRL (as a CA)
openssl ca -gencrl -keyfile ca.key -cert ca.crt -out crl.pem -config openssl.cnfRequires a properly configured openssl.cnf file with CA directory structure and database files.
🌐 OCSP (Online Certificate Status Protocol)
Find the OCSP responder URL
openssl x509 -in certificate.crt -noout -ocsp_uriCheck certificate revocation status via OCSP
openssl ocsp -issuer issuer.crt -cert certificate.crt \
-url http://ocsp.example.com -resp_textSends an OCSP request and displays the response. Look for "Cert Status: good" in the output. A status of "revoked" means the certificate has been revoked.
Create an OCSP request file
openssl ocsp -issuer issuer.crt -cert certificate.crt \
-reqout ocsp-request.derVerify an OCSP response with CA validation
openssl ocsp -issuer issuer.crt -cert certificate.crt \
-url http://ocsp.example.com -CAfile ca-bundle.crt -resp_textThe -CAfile flag enables full verification of the OCSP response signature chain.
Check OCSP stapling on a live server
echo | openssl s_client -connect example.com:443 -status 2>/dev/null \
| grep -A 20 "OCSP Response"If the server supports OCSP stapling, you will see the stapled OCSP response. If not, the section will be empty or absent.
🔐 Encryption, Decryption, and Signing
Encrypt a file with AES-256-CBC
openssl enc -aes-256-cbc -salt -pbkdf2 -in plaintext.txt -out encrypted.encYou will be prompted for a password. The -pbkdf2 flag uses the modern key derivation function (OpenSSL 1.1.1+). The -salt flag adds randomness to prevent dictionary attacks.
Decrypt a file
openssl enc -d -aes-256-cbc -pbkdf2 -in encrypted.enc -out decrypted.txtEncrypt a file with Base64 output
openssl enc -aes-256-cbc -salt -pbkdf2 -a -in plaintext.txt -out encrypted.txtThe -a flag Base64-encodes the output, making it safe for text transport.
Sign a file with a private key
openssl dgst -sha256 -sign private.key -out signature.bin file.txtVerify a file signature
openssl dgst -sha256 -verify public.key -signature signature.bin file.txtReturns "Verified OK" if the signature matches the file contents.
Generate SHA-256 hash of a file
openssl dgst -sha256 file.txt🧰 Utility Commands
Check OpenSSL version
openssl version -aShows version, build date, compiler flags, and default directories.
Generate random bytes (hex)
openssl rand -hex 32Generates 32 random bytes as hexadecimal (64 hex characters). Uses cryptographically secure randomness.
Generate random bytes (Base64)
openssl rand -base64 32Useful for generating random passwords, tokens, and secrets.
List available cipher suites
openssl ciphers -vList TLS 1.3 cipher suites
openssl ciphers -v -tls1_3List available digest algorithms
openssl list -digest-algorithmsList available cipher algorithms
openssl list -cipher-algorithmsList available public key algorithms
openssl list -public-key-algorithmsBenchmark cryptographic performance
openssl speedBenchmarks all algorithms. Test a specific algorithm with openssl speed aes-256-cbc or openssl speed rsa2048.
Base64 encode a file
openssl base64 -in file.bin -out file.b64Base64 decode a file
openssl base64 -d -in file.b64 -out file.binParse ASN.1 structure of a certificate
openssl asn1parse -in certificate.crtUseful for debugging certificate encoding issues. Works with any PEM or DER encoded file.