openssl (s_client, s_server, x509, req, verify, ciphers, ocsp, s_time, genpkey, pkcs12) (4/5)
Four openssl commands for the certificate lifecycle: verify checks a chain up to a trusted root (and its error codes tell you exactly what is wrong), s_time measures TLS connections per second, req creates CSRs and even micro-CA certificates, and genpkey generates keys and parameters.
173.1 What it does, where it runs
| Command | Job |
|---|---|
| verify | Check a certificate chain up to a trusted root |
| s_time | TLS speed test: connections per second |
| req | Create CSRs, self-signed and micro-CA certificates |
| genpkey | Generate keys and parameters (RSA, EC, Ed25519, DSA...) |
- Linux: usually preinstalled; Windows: Git for Windows or winget; macOS: brew install openssl@3 (LibreSSL has fewer options).
- Lab: n173-srv 192.168.193.10 (root CA, issuing CA, srv.lab cert, s_server) and n173-client 192.168.193.20, OpenSSL 3.3.7.
173.2 verify: building the chain
v() { openssl verify "$@"; echo "exit $?"; }v -CAfile root.crt srv.crtv -CAfile root.crt -untrusted inter.crt \ srv.crtv -CAfile root.crt -untrusted inter.crt \ -show_chain srv.crtCN=srv.laberror 20 at 0 depth lookup: unable to get local issuer certificateerror srv.crt: verification failedexit 2srv.crt: OKexit 0srv.crt: OKChain:depth=0: CN=srv.lab (untrusted)depth=1: C=IN, O=N173 Lab, CN=N173 Lab Issuing CA (untrusted)depth=2: C=IN, O=N173 Lab, CN=N173 Lab Root CAexit 0- -CAfile = trust anchor (root); -untrusted = intermediates used only to build the path.
- Failure exits with code 2, so scripts can check $?.
- Other trust sources: -trusted, -CApath (hashed dir), -CAstore (URI), -no-CAfile; -CRLfile and -crl_download for CRLs.
173.3 verify: error codes you must know
v() { openssl verify -CAfile root.crt \ -untrusted inter.crt "$@" 2>&1 \ | grep -E 'OK$|^error [0-9]'; }v -purpose sslserver srv.crtv -purpose sslserver client.crtv -verify_hostname wrong.lab srv.crtv expired.crtv -attime 1830297600 srv.crtsrv.crt: OKerror 26 at 0 depth lookup: unsuitable certificate purposeerror 62 at 0 depth lookup: hostname mismatcherror 10 at 0 depth lookup: certificate has expirederror 10 at 0 depth lookup: certificate has expired| Code | Meaning | Lab trigger |
|---|---|---|
| 20 | unable to get local issuer certificate | Intermediate missing (only root given) |
| 2 | unable to get issuer certificate | Only intermediate as CA file, chain cannot reach a root |
| 10 | certificate has expired | expired.crt, or -attime in 2028 |
| 23 | certificate revoked | old.crt with -CRLfile -crl_check |
| 26 | unsuitable certificate purpose | Client cert checked with -purpose sslserver |
| 62 | hostname mismatch | -verify_hostname wrong.lab |
| 85 | Missing Authority Key Identifier | loose.crt with -x509_strict |
- -partial_chain accepts a chain that stops at a trusted intermediate (error 2 becomes OK).
- -x509_strict rejects certs that plain verify accepts (loose.crt: OK vs error 85).
- Other controls: -no_check_time, -crl_check_all, -verify_depth, -verify_email/-verify_ip, -policy*, -suiteB_*; -issuer_checks has no effect in OpenSSL 3.
173.4 verify OK does not mean not revoked
C="-CAfile root.crt -untrusted inter.crt"openssl crl -in issuing.crl -noout -text \ | grep -E 'Serial Number|Key Compromise'openssl verify $C old.crt; echo "exit $?"openssl verify $C -CRLfile issuing.crl \ -crl_check old.crt; echo "exit $?" Serial Number: 1001 Key Compromiseold.crt: OKexit 0CN=srv.laberror 23 at 0 depth lookup: certificate revokederror old.crt: verification failedexit 2173.5 s_time: TLS speed test
- Step 1
1.s_time on 192.168.193.20 sends ClientHello
- Step 2
2.s_server -www on 192.168.193.10:4433 replies with full handshake + chain
- Step 3
3.With -www, client sends GET / and reads the page
- Step 4
4.Connection closed, loop repeats for -time seconds
- Step 5
5.s_time counts: 730 connections in 5 s (-new)
T="-connect srv.lab:4433 -time 5"openssl s_time $T -newopenssl s_time $T -reuseopenssl s_time $T -tls1_2 -reuseopenssl s_time $T -new -www / -CAfile root.crtCollecting connection statistics for 5 seconds****************************** ...730 connections in 2.63s; 277.57 connections/user sec, bytes read 0730 connections in 6 real seconds, 0 bytes read per connectionNow timing with session id reuse.****************************** ...754 connections in 2.50s; 301.60 connections/user sec, bytes read 0Now timing with session id reuse.rrrrrrrrrrrrrrrrrrrrrrrrrrrrrr ...1742 connections in 1.11s; 1569.37 connections/user sec, bytes read 0571 connections in 2.57s; 222.18 connections/user sec, bytes read 3063986571 connections in 6 real seconds, 5366 bytes read per connection- * = full handshake, r = resumed session.
- On TLS 1.3 -reuse still showed stars (session not reused); with -tls1_2 -reuse it showed r and 1742 connections.
- Other options: -cert/-key (client cert), -cipher/-ciphersuites to compare cipher speed, -tls1_3, -CAfile/-CApath.
173.6 req: creating a key and CSR
openssl req -new -newkey ec \ -pkeyopt ec_paramgen_curve:P-256 -nodes \ -keyout web.key -out web.csr \ -subj "/C=IN/O=N173 Lab/CN=web.lab" \ -addext "subjectAltName=DNS:web.lab" \ 2>/dev/nullopenssl req -in web.csr -text -noout \ -verify 2>&1 \ | grep -E 'verify|Subject:|Key Algo|DNS:'openssl req -in web.csr -outform DER \ -out web.derwc -c web.csr web.derCertificate request self-signature verify OK Subject: C=IN, O=N173 Lab, CN=web.lab Public Key Algorithm: id-ecPublicKey DNS:web.lab 448 web.csr 278 web.der 726 total- -new -newkey creates key and CSR in one go; -nodes (or -noenc) leaves the key unencrypted.
- -subj and -addext set subject and SAN on the command line (no prompts).
- -key signs with an existing key; -passout sets the new key's password; -newkey rsa:3072 / ed25519 pick the algorithm.
- Printing: -text, -noout, -verify, -pubkey, -inform/-outform DER, -reqopt, -batch for scripts.
173.7 req: configuration file
[ req ]default_bits = 2048default_md = sha256encrypt_key = noprompt = nostring_mask = utf8onlydistinguished_name = dnreq_extensions = v3_req[ dn ]O = N173 LabCN = api.lab[ v3_req ]subjectAltName = DNS:api.labextendedKeyUsage = serverAuth$ openssl req -new -newkey rsa -config req.cnf -keyout api.key -out api.csrexit 0Certificate request self-signature verify OKsubject=O=N173 Lab, CN=api.lab Public-Key: (2048 bit) DNS:api.lab TLS Web Server Authentication Signature Algorithm: sha256WithRSAEncryption- prompt = no takes the subject from the [dn] section; default_bits gave a 2048-bit RSA key.
- req_extensions puts SAN and EKU into the CSR; x509_extensions is used with -x509; commonName_default sets the prompt default.
173.8 req: self-signed and micro-CA certificates
openssl req -x509 -newkey ec \ -pkeyopt ec_paramgen_curve:P-256 -nodes \ -keyout mini.key -out mini.crt -days 30 \ -subj "/CN=N173 Mini CA" 2>/dev/nullopenssl req -in web.csr -CA mini.crt \ -CAkey mini.key -days 7 \ -copy_extensions copy -out web.crtopenssl x509 -in web.crt -noout -issuer \ -enddate -ext subjectAltNameopenssl verify -CAfile mini.crt web.crtissuer=CN=N173 Mini CAnotAfter=Nov 16 09:33:38 2026 GMTX509v3 Subject Alternative Name: DNS:web.labweb.crt: OK- -x509 outputs a self-signed cert instead of a CSR (subject = issuer).
- OpenSSL 3: req -CA -CAkey signs a CSR directly; -copy_extensions copy keeps the SAN (default is none).
- -set_serial sets your own serial; -precert makes a CT poison pre-cert.
173.9 genpkey: keys and parameters
g() { openssl genpkey -quiet "$@"; }g -algorithm RSA -out rsa.key \ -pkeyopt rsa_keygen_bits:2048g -algorithm EC -out ec.key \ -pkeyopt ec_paramgen_curve:P-256g -algorithm ED25519 -out ed.keyg -algorithm X25519 -out x.keyg -algorithm RSA-PSS -out pss.key \ -pkeyopt rsa_keygen_bits:2048 \ -pkeyopt rsa_pss_keygen_md:sha256for k in rsa ec ed x pss; do printf '%s: ' $k openssl pkey -in $k.key -noout -text_pub \ | head -1; donersa: Public-Key: (2048 bit)ec: Public-Key: (256 bit)ed: ED25519 Public-Key:x: X25519 Public-Key:pss: Public-Key: (2048 bit)| Algorithm | Use | Key -pkeyopt |
|---|---|---|
| RSA | Sign + encrypt | rsa_keygen_bits, rsa_keygen_pubexp, rsa_keygen_primes |
| RSA-PSS | Signing with a hash restriction (SHA2-256) | rsa_pss_keygen_md, _mgf1_md, _saltlen |
| EC | ECDSA / ECDH | ec_paramgen_curve, ec_param_enc:named_curve |
| ED25519 / ED448 | Signing | - |
| X25519 / X448 | Key agreement | - |
| DSA / DH / DHX | Needs parameters first (-genparam) | dsa_paramgen_bits, dsa_paramgen_q_bits |
g -genparam -algorithm DSA -text \ -pkeyopt dsa_paramgen_bits:2048 \ -pkeyopt dsa_paramgen_q_bits:256 \ -out dsa.paramgrep -E '^[A-Za-z-]+:' dsa.paramg -paramfile dsa.param -out dsa.keyDSA-Parameters: (2048 bit)P:Q:G:SEED:pcounter: 1560h: 2- -aes256 -pass encrypts the key (BEGIN ENCRYPTED PRIVATE KEY); a wrong -passin gives maybe wrong password.
- genpkey has no -noout (Unknown option or cipher: noout) and -text prints private values; use openssl pkey -noout -text_pub.
- ec_param_enc:named_curve (default) is more compatible than explicit; hexseed is for testing only.
- Step 1
1.genpkey -algorithm EC: private key stays on the server
- Step 2
2.req -new -addext SAN: CSR
- Step 3
3.CA (or req/x509 -CA -CAkey micro-CA) signs the CSR
- Step 4
4.verify -CAfile root -untrusted inter -purpose sslserver -verify_hostname
- Step 5
5.verify -CRLfile -crl_check: not revoked
173.10 Common mistakes
- Treating verify OK as not revoked: add -CRLfile and -crl_check.
- Forgetting -untrusted inter.crt and blaming the cert for error 20.
- Running genpkey -text and exposing private values in logs.
- Signing with req -CA but forgetting -copy_extensions copy: SAN is lost.
173.11 Interview tips
- New cert flow: genpkey EC key, req -new with SAN, CA signs, verify with -CAfile, -untrusted, -purpose, -verify_hostname and the CRL.
- Error codes: 20 incomplete chain, 10 expired, 23 revoked, 26 wrong purpose, 62 hostname mismatch.
- Why is -reuse faster? A resumed session skips the full handshake and certificate work (730 vs 1742).
- Ed25519 is for signing, X25519 for key agreement.