Private keys 15
Password options such as -pass, -passin and -passout also accept env:VAR, file:PATHNAME and stdin; the pass: form shows the password to other users in ps.
| Command | What it does |
|---|---|
openssl genpkey -algorithm RSA -out key.pem | Generate an RSA keyThe key is 2048 bits unless an rsa_keygen_bits option says otherwise; without -out it goes to standard output. |
openssl genpkey -algorithm RSA -out key.pem -pkeyopt rsa_keygen_bits:2048 | Set the RSA key sizeEach -pkeyopt passes one algorithm setting and must come after -algorithm; the manual example also sets the public exponent. |
openssl genpkey -algorithm RSA -out key.pem -aes-128-cbc -pass pass:hello | Generate a passphrase-protected keyAny cipher name accepted by OpenSSL can replace -aes-128-cbc; -pass names where the passphrase comes from. |
openssl genpkey -algorithm EC -out eckey.pem -pkeyopt ec_paramgen_curve:P-384 -pkeyopt ec_param_enc:named_curve | Generate an EC keyNIST names such as P-256 or P-384 pick the curve; named_curve is also the default parameter encoding. |
openssl genpkey -algorithm X25519 -out xkey.pem | Generate an X25519 keyX25519, X448, ED25519 and ED448 take no key generation options, so the algorithm name is all that is needed. |
openssl genpkey -algorithm ML-DSA-65 -out ml-dsa-key.pem | Generate an ML-DSA keyML-DSA and ML-KEM key generation is available since OpenSSL 3.5. |
openssl genpkey -algorithm ML-KEM-768 -out ml-kem-key.pem | Generate an ML-KEM key |
openssl genrsa -out key.pem 2048 | Generate an RSA key with genrsaMakes RSA keys only; the req manual pairs it with req -new -key to build a CSR in two steps. |
openssl pkey -in key.pem -out keyout.pem | Remove a key passphraseNo cipher option is given, so the copy written to keyout.pem is not encrypted. |
openssl pkey -in key.pem -des3 -out keyout.pem | Encrypt an existing keyAny cipher name, such as aes128, can stand in for des3; DER output cannot be encrypted. |
openssl pkey -in key.pem -text -noout | Print key componentsShows the key parameters as plain text; -noout leaves out the encoded PEM copy of the key. |
openssl pkey -in key.pem -pubout -out pubkey.pem | Extract the public keyOnly the public half is written; together with -text this gives the same output as -text_pub. |
openssl pkey -pubin -in FILE -text -noout | Read a public key fileWith -pubin the input is read as a public key instead of a private one. |
openssl pkey -in FILE -check -noout | Check a key pairTests the public and private parts of any key type for consistency. |
openssl rsa -in FILE -check -noout | Check an RSA private keyRuns an RSA consistency test; -noout keeps the key itself from being printed again. |
Certificate requests and signing 8
In x509, -signkey was renamed -key in OpenSSL 3.0; the old name still works as an alias.
| Command | What it does |
|---|---|
openssl req -new -key key.pem -out req.pem | Create a CSR from a keyPrompts for the subject fields set up in the configuration file; -subj supplies them on the command line instead. |
openssl req -newkey rsa:2048 -keyout key.pem -out req.pem | New key and CSR togetherThe key written to the -keyout file is encrypted with AES-256-CBC unless -noenc is added. |
openssl req -new -subj "/C=GB/CN=foo" -addext "subjectAltName = DNS:foo.co.uk" -newkey rsa:2048 -keyout key.pem -out req.pem | CSR with subject and SANNo prompts: -subj sets the name, and each -addext adds one extension written as a config file key=value pair. |
openssl req -in req.pem -text -verify -noout | Examine and verify a CSRThe self-signature is checked first, and nothing is printed if that check fails. |
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out req.pem | Self-signed certificateWrites a certificate instead of a request, valid 30 days unless -days says otherwise; -noenc skips key encryption. |
openssl x509 -req -in careq.pem -extfile openssl.cnf -extensions v3_ca -key key.pem -out cacert.pem | Self-sign a CSR as a CA-key does the self-signing (-signkey is its old alias); -extfile and -extensions add the v3_ca section from openssl.cnf. |
openssl x509 -req -in req.pem -extfile openssl.cnf -extensions v3_usr -CA cacert.pem -CAkey key.pem -CAcreateserial | Sign a CSR with your CAThe CA subject becomes the issuer; -CAcreateserial creates the serial number file when it does not exist yet. |
openssl x509 -x509toreq -in cert.pem -out req.pem -key key.pem | Turn a certificate into a CSRExtensions of the certificate are dropped unless -copy_extensions or -extfile brings them across. |
Reading certificates 13
| Command | What it does |
|---|---|
openssl x509 -in cert.pem -noout -text | Show certificate detailsIncludes subject, issuer, serial, public key and extensions; -noout drops the encoded certificate from the output. |
openssl x509 -in cert.pem -noout -ext subjectAltName | Show the SAN extensionTakes a comma-separated list, so subjectAltName,nsCertType prints both extensions. |
openssl x509 -in cert.pem -noout -subject | Show the subject nameAdd -nameopt RFC2253 to print the name in RFC 2253 form. |
openssl x509 -in FILE -noout -issuer | Show the issuer nameAccepts the same -nameopt display options as -subject. |
openssl x509 -in cert.pem -noout -serial | Show the serial number |
openssl x509 -in FILE -noout -dates | Show validity datesPrints notBefore and notAfter; -startdate or -enddate prints only one of them. |
openssl x509 -in FILE -noout -enddate | Show the expiry dateAdd -dateopt iso_8601 for an ISO 8601 date instead of the default RFC 822 format. |
openssl x509 -in chain.pem -multi -checkend $[3600*24*30] | Check for coming expiryExit status is nonzero when any certificate in the file expires within 30 days; without -multi only one is checked. |
openssl x509 -sha1 -in cert.pem -noout -fingerprint | Show the fingerprintHashes the DER form of the certificate; use -sha256 in place of -sha1 for a SHA-256 fingerprint. |
openssl x509 -in FILE -noout -pubkey | Extract the certificate public keyThe SubjectPublicKeyInfo block is printed in PEM form. |
openssl x509 -in FILE -noout -checkhost HOST | Check the host nameReports whether the certificate matches that host; -checkip and -checkemail test an address or email instead. |
openssl x509 -in FILE -noout -purpose | List certificate purposesRuns the extension checks for each predefined purpose, such as SSL server, and prints the result of each. |
openssl x509 -in FILE -noout -ocsp_uri | Show the OCSP responder URL |
Converting formats 5
| Command | What it does |
|---|---|
openssl x509 -in cert.pem -inform PEM -out cert.der -outform DER | Convert a certificate to DERSwap the -inform and -outform values to turn a DER certificate back into PEM. |
openssl pkey -in key.pem -outform DER -out keyout.der | Convert a private key to DERDER output cannot be encrypted, so do not combine it with a cipher option. |
openssl pkcs7 -in file.pem -print_certs -out certs.pem | Extract certificates from PKCS#7Also prints any CRLs in the file, each item preceded by its subject and issuer names. |
openssl base64 -in file.bin -out file.b64 | Base64-encode a fileOutput lines break after 64 characters unless -A is added. |
openssl base64 -d -in file.b64 -out file.bin | Decode a Base64 fileAdd -A when the Base64 input is a single line without newlines. |
PKCS#12 bundles 8
| Command | What it does |
|---|---|
openssl pkcs12 -export -in cert.pem -inkey key.pem -out file.p12 | Bundle a key and certificateProtected with AES-256-CBC and PBKDF2 by default; -passout gives the export password. |
openssl pkcs12 -export -in cert.pem -inkey key.pem -out file.p12 -legacy | Bundle with legacy algorithmsUses the older defaults of the legacy provider instead of AES-256-CBC. |
openssl pkcs12 -export -in file.pem -out file.p12 -name "My PSE" -certfile othercerts.pem | Add a friendly name and extra certificates-name sets the label that importing software displays; -certfile adds extra certificates to the bundle. |
openssl pkcs12 -in file.p12 -out file.pem | Unpack a PKCS#12 fileKey and certificates come out as PEM, with the key encrypted unless -noenc is added. |
openssl pkcs12 -in file.p12 -clcerts -out file.pem | Extract only the client certificate-cacerts keeps only CA certificates instead; -nokeys and -nocerts leave out keys or certificates. |
openssl pkcs12 -in file.p12 -out file.pem -noenc | Unpack with an unencrypted key-noenc replaces -nodes, which is deprecated since OpenSSL 3.0. |
openssl pkcs12 -in file.p12 -info -noout | Inspect a PKCS#12 fileShows the structure, algorithms and iteration counts; -noout only verifies the contents without writing them. |
openssl pkcs12 -in file.p12 -info -noout -legacy | Inspect a legacy PKCS#12 fileNeeded for older files protected with algorithms such as RC2-40-CBC. |
Verifying certificates 6
OpenSSL ships no trusted CA certificates of its own; the default store is whatever the operating system or distribution configured.
| Command | What it does |
|---|---|
openssl verify CERTIFICATE | Verify a certificateSeveral files can be named, one certificate each; with none, a single certificate is read from standard input. |
openssl verify -CAfile CAFILE CERTIFICATE | Verify against a CA fileTrusts the certificates in CAFILE for this check; -no-CAfile, -no-CApath and -no-CAstore switch the defaults off. |
openssl verify -CAfile CAFILE -untrusted FILENAME CERTIFICATE | Supply intermediate certificatesUntrusted certificates only help build the chain; trust still has to come from -CAfile or the default store. |
openssl verify -show_chain -CAfile CAFILE CERTIFICATE | Show the built chainCertificates taken from the -untrusted list are flagged as untrusted in the output. |
openssl verify -verify_hostname HOSTNAME -CAfile CAFILE CERTIFICATE | Verify for a host nameFails unless the name matches a DNS entry in the Subject Alternative Name or the Common Name. |
openssl verify -crl_check -CRLfile FILENAME -CAfile CAFILE CERTIFICATE | Check revocation with a CRLFails when no valid CRL covers the leaf certificate; -crl_check_all extends the check to the whole chain. |
Testing TLS connections 18
| Command | What it does |
|---|---|
openssl s_client -connect servername:443 | Open a TLS connectionPrints certificate and session information, then sends what you type, such as GET /, to the server. |
openssl s_client -brief -starttls smtp -connect smtp.example.com:25 | Test STARTTLS on a mail serverOther -starttls keywords include pop3, imap, ftp, xmpp, postgres, mysql and ldap. |
-servername NAME | Set the SNI nameWithout it, the -connect host is sent as SNI when it looks like a DNS name. |
-showcerts | Show the certificates sentLists the server certificates in the order received, which is not a verified chain. |
-brief | Print a short summaryReplaces the long session printout with a few lines of connection parameters. |
-tls1_2 | Require TLS 1.2TLS 1.2 becomes the only version offered, so a server without it fails the handshake. |
-tls1_3 | Require TLS 1.3TLS 1.3 becomes the only version offered; -no_tls1_3 does the opposite and leaves it out. |
-alpn PROTOCOLS | Offer ALPN protocolsA comma-separated list with the preferred protocol first, for example http/1.1. |
-status | Ask for OCSP staplingPrints the stapled OCSP response if the server sends one. |
-verify_return_error | Stop on verification errorsBy default s_client reports certificate errors but carries on with the handshake. |
-CAfile FILE | Trust a specific CA fileAdds the certificates in that file to the trusted set used to check the server. |
-cert FILENAME | Send a client certificateUsed only when the server asks for a client certificate; pair it with -key. |
-key FILENAME | Client certificate private keyWhen it is left out, the key is read from the -cert file. |
-keylogfile FILE | Log TLS secretsAppends the session secrets so Wireshark or a similar tool can decrypt a capture of the connection. |
-ign_eof | Keep going at end of inputKeeps a scripted run with no terminal input from closing the connection early. |
-crlf | Send CR+LF line endingsTurns each typed line feed into CR+LF for servers that require it. |
-msg | Show protocol messages-trace prints a more verbose decode of the same handshake messages. |
openssl s_server -port PORT -cert CERT -key KEY -www | Run a test TLS serverListens on 4433 when no port is given; -www answers a browser with an HTML page about the session. |
Inside an s_client session 5
These letters only work in an interactive session started without -quiet, -ign_eof, -nocommands or -adv.
| Command | What it does |
|---|---|
| Q | Quit the sessionTyped as the first letter of a line; closes the connection and exits s_client. |
| R | Renegotiate the sessionWorks up to TLS 1.2 only. |
| C | Reconnect with resumptionTests whether the server accepts a resumed session. |
| k | Send a key updateTLS 1.3 only; the server is not asked to update its own keys. |
| K | Key update with a replyTLS 1.3 only; asks the server to send its own key update back. |
Hashes and signatures 5
| Command | What it does |
|---|---|
openssl dgst -sha256 FILE | SHA-256 checksumSHA-256 is also the default digest, and a digest name works as the command too: openssl sha256. |
openssl dgst -list | List digest names |
openssl dgst -sha256 -sign privatekey.pem -out signature.sign file.txt | Sign a fileWrites a binary signature; Ed25519, ED448 and ML-DSA keys sign without a digest option. |
openssl dgst -sha256 -verify publickey.pem -signature signature.sign file.txt | Verify a signaturePrints Verified OK or Verification Failure. |
openssl dgst -hmac KEY FILE | Compute an HMACUses SHA-256 unless another digest is named; the manual recommends openssl mac for this job. |
Encrypting files 5
| Command | What it does |
|---|---|
openssl enc -aes128 -pbkdf2 -in file.txt -out file.aes128 | Encrypt a file with a passwordPrompts for the password; -pbkdf2 runs 10,000 iterations unless -iter sets another count. |
openssl enc -aes128 -pbkdf2 -d -in file.aes128 -out file.txt | Decrypt a fileThe manual example adds -pass with a pass: source to give the password on the command line. |
openssl enc -aes-256-ctr -pbkdf2 -a -in file.txt -out file.aes256 | Encrypt to Base64 text-a Base64-encodes the ciphertext so it can be pasted into mail or other text. |
openssl enc -aes-256-ctr -pbkdf2 -d -a -in file.aes256 -out file.txt | Decrypt Base64 ciphertextDecodes the Base64 before decrypting; the manual reads the password from a file with -pass. |
openssl enc -list | List cipher namesGCM and CCM never appear because enc does not support authenticated modes. |
Random data and passwords 4
| Command | What it does |
|---|---|
openssl rand -base64 NUM | Random Base64 stringNUM counts random bytes, not output characters; a K, M, G or T suffix scales it. |
openssl rand -hex NUM | Random hex string |
openssl rand -out FILE NUM | Random bytes to a fileWrites raw binary bytes; without -out they go to standard output. |
openssl passwd -6 -salt STRING PASSWORD | Hash a password for cryptBuilds a SHA-512 crypt hash, -5 gives SHA-256; leave out the password to be asked for it. |
Version and algorithm lists 11
| Command | What it does |
|---|---|
openssl version -a | Show full version detailsAdds build date, compiler flags, platform and the OPENSSLDIR directory to the version string. |
openssl version -d | Show the OPENSSLDIRThe default openssl.cnf configuration file lives in this directory. |
openssl help | Get help on commandsBare openssl does the same since OpenSSL 3.0 removed the interactive mode; every command also accepts -help. |
openssl list -commands | List standard commandsPut -1 first to print the names in a single column. |
openssl list -digest-algorithms | List digest algorithms |
openssl list -cipher-algorithms | List cipher algorithms |
openssl list -public-key-algorithms | List public key algorithms |
openssl list -providers | List loaded providersShows name, version and status of each; -verbose adds every provider parameter. |
openssl list -disabled | List disabled featuresShows what was compiled out of this build. |
openssl ciphers -v | List TLS cipher suitesWith no cipher list argument the default list is used; -v adds the details of each suite. |
openssl ciphers -s -v 'ALL:@SECLEVEL=2' | Suites allowed at a security level-s keeps only suites usable at that level and within the configured protocol versions. |
No row on this page matches that filter.
Notes
From key to certificate
openssl genpkey -algorithm RSA -out key.pem makes a private key; the same command takes EC, X25519 and, since OpenSSL 3.5, ML-DSA and ML-KEM. openssl req -new -key key.pem -out req.pem turns the key into a certificate signing request for a CA, and openssl req -x509 -newkey rsa:2048 -keyout key.pem -out req.pem skips the CA for a test certificate, valid for 30 days unless -days is given; add -noenc if the key must not be encrypted. openssl pkcs12 -export -in cert.pem -inkey key.pem -out file.p12 bundles key and certificate for software that imports PKCS#12 (PFX) files.
Reading and debugging certificates
openssl x509 -in cert.pem -noout -text prints everything in a certificate, and openssl x509 -in FILE -noout -enddate just the expiry; openssl x509 -in chain.pem -multi -checkend $[3600*24*30] turns that into an exit status a script can test. openssl verify -CAfile CAFILE CERTIFICATE checks a chain against a CA you name; without -CAfile it relies on the trust store the system configured, because OpenSSL has none of its own. openssl s_client -connect servername:443 shows what a server actually sends: add -servername for SNI, -showcerts for every certificate it presents and -verify_return_error to stop at the first verification failure.
Hashing, random data and encryption
openssl dgst -sha256 FILE prints a checksum for comparing downloads, and openssl rand -base64 NUM generates a random secret from NUM bytes. openssl enc -aes128 -pbkdf2 -in file.txt -out file.aes128 encrypts a file with a password and openssl enc -aes128 -pbkdf2 -d -in file.aes128 -out file.txt decrypts it; use the same cipher and -pbkdf2 on both sides. enc does not support GCM, CCM or any other authenticated mode.
Questions
How do I check when a certificate expires?
openssl x509 -in FILE -noout -enddate prints the notAfter date. In a script, openssl x509 -in chain.pem -multi -checkend $[3600*24*30] exits nonzero if any certificate in the file expires within 30 days.
How do I see the certificate chain a server sends?
Run openssl s_client -connect servername:443 with -showcerts added. It lists the certificates in the order the server sent them, which is not the same as a verified chain. Add -servername NAME when the -connect target is an IP address or not the name the server expects.
Should I use -nodes or -noenc?
Use -noenc. The -nodes option of req and pkcs12 is deprecated since OpenSSL 3.0; openssl pkcs12 -in file.p12 -out file.pem -noenc, for example, unpacks a bundle with the key left unencrypted.
Where does openssl verify get its trusted CA certificates?
From the store your operating system or distribution configured; OpenSSL itself ships none. openssl verify -CAfile CAFILE CERTIFICATE names the CA explicitly, and openssl verify -CAfile CAFILE -untrusted FILENAME CERTIFICATE adds intermediates kept in a separate file. openssl version -d shows the OPENSSLDIR directory of the build.
Can openssl enc encrypt with AES-GCM?
No. enc does not support authenticated modes such as GCM and CCM, and the manual says it will not, which is why openssl enc -list does not show them.
Sources and checks
- openssl-genpkey(1) manual page, OpenSSL 3.6 documentation (private key generation) EXAMPLES for RSA, EC, X25519, ML-DSA and ML-KEM keys
- openssl-req(1) manual page, OpenSSL 3.6 documentation (certificate requests) EXAMPLES for CSRs, self-signed certificates and the genrsa two-step
- openssl-pkey(1) manual page, OpenSSL 3.6 documentation (key processing) EXAMPLES and SYNOPSIS for converting, encrypting and checking keys
- openssl-rsa(1) manual page, OpenSSL 3.6 documentation (RSA key processing) SYNOPSIS and the -check option
- openssl-x509(1) manual page, OpenSSL 3.6 documentation (certificate display and signing) EXAMPLES and SYNOPSIS for reading, converting and signing certificates
- openssl-pkcs7(1) manual page, OpenSSL 3.6 documentation (PKCS#7 files) The -print_certs example
14 more source pages
- openssl-enc(1) manual page, OpenSSL 3.6 documentation (symmetric ciphers and Base64) EXAMPLES for encryption, decryption and Base64
- openssl-pkcs12(1) manual page, OpenSSL 3.6 documentation (PKCS#12 files) EXAMPLES for creating, unpacking and inspecting PKCS#12 bundles
- openssl-verify(1) manual page, OpenSSL 3.6 documentation (certificate verification) SYNOPSIS of the verify command and its options
- openssl-s_client(1) manual page, OpenSSL 3.6 documentation (TLS client) NOTES example, OPTIONS and connected-mode commands
- openssl-s_server(1) manual page, OpenSSL 3.6 documentation (TLS test server) SYNOPSIS of the test server
- openssl-dgst(1) manual page, OpenSSL 3.6 documentation (digests and signatures) EXAMPLES and SYNOPSIS for hashing, signing and HMAC
- openssl-rand(1) manual page, OpenSSL 3.6 documentation (random bytes) SYNOPSIS of the rand command
- openssl-passwd(1) manual page, OpenSSL 3.6 documentation (password hashes) SYNOPSIS of the passwd command
- openssl-version(1) manual page, OpenSSL 3.6 documentation (version information) SYNOPSIS of the version command
- openssl(1) manual page, OpenSSL 3.6 documentation (command overview) The openssl help command
- openssl-list(1) manual page, OpenSSL 3.6 documentation (algorithm and command lists) SYNOPSIS of the list command
- openssl-ciphers(1) manual page, OpenSSL 3.6 documentation (TLS cipher lists) SYNOPSIS and the security-level example
- openssl-verification-options(1) manual page, OpenSSL 3.6 documentation (trust and verification options) Note on default trust anchors
- openssl-passphrase-options(1) manual page, OpenSSL 3.6 documentation (password arguments) Forms of the pass phrase argument
Every row on this page was matched to a line in the documentation above on . A binding that could not be found there is not listed.
Found a wrong or missing key? Tell us and it gets checked against the source. How we verify describes the process and its limits.