openssl (s_client, s_server, x509, req, verify, ciphers, ocsp, s_time, genpkey, pkcs12) (2/5)
openssl x509 reads, prints, converts, checks and even signs certificates like a small CA, and openssl s_server is a test TLS server you run with your own cert and key. Together they let you build a full lab PKI and debug chain and mTLS problems.
171.1 What it does, where it runs
| Tool / OS | Details |
|---|---|
| x509 | Read, print, convert, check and sign a certificate |
| s_server | Test TLS server: -accept, -cert, -key |
| Windows | Comes with Git for Windows, or winget ShiningLight.OpenSSL.Light |
| Linux / macOS | apt/dnf openssl; macOS ships LibreSSL, so brew install openssl@3 |
- Lab: n171-srv 192.168.191.10 (root + issuing CA, s_server) and n171-client 192.168.191.20 (s_client), OpenSSL 3.3.0.
- Private keys were generated with genpkey inside the containers and never left them; only CSRs and public certs moved.
- Step 1
1.n171-srv 192.168.191.10: s_server listens on TCP 4433
- Step 2
2.n171-client .20 sends ClientHello (SNI srv.lab)
- Step 3
3.Server sends leaf CN=srv.lab + chain CN=N171 Issuing CA (TLSv1.3)
- Step 4
4.Client verifies the whole chain against root.crt
- Step 5
5.Verify return code: 0 (ok)
The intermediate must come from the server; the client only holds the root
171.2 x509: creating and signing certificates
- -new -key: create a brand-new self-signed cert (here the root CA); -extfile/-extensions add the CA extensions.
- -req -signkey: sign a CSR with its own key, so subject = issuer = srv.lab.
- -days 1 -set_serial: a one-day cert with your own serial (1001).
cd /lab# root CA: naya cert, apni key se self-signedopenssl x509 -new -key root.key \ -subj "/CN=N171 Root CA" -days 3650 \ -extfile ext.cnf -extensions v3_ca -out root.crt# CSR ko apni hi key se sign karoopenssl x509 -req -in srv.csr -signkey srv.key \ -days 30 -out self.crt# sirf 1 din valid, apna serialopenssl x509 -req -in short.csr -key short.key \ -days 1 -set_serial 0x1001 -out short.crtopenssl x509 -in self.crt -noout -subject -issueropenssl x509 -in short.crt -noout -serial -enddate$ sh make.shCertificate request self-signature oksubject=CN=srv.labCertificate request self-signature oksubject=CN=short.labsubject=CN=srv.labissuer=CN=srv.labserial=1001notAfter=Nov 10 09:09:22 2026 GMT171.3 x509 as a micro-CA (-CA, -CAkey, -CAcreateserial)
- -CA and -CAkey pick the signing CA; the new cert's issuer becomes that CA's subject.
- Root signed the Issuing CA, the Issuing CA signed srv.lab and srvec (EC).
- -CAcreateserial creates inter.srl; the next signing increments it (...801 then ...802).
cd /labopenssl x509 -req -in inter.csr -days 1825 \ -CA root.crt -CAkey root.key -CAcreateserial \ -extfile ext.cnf -extensions v3_inter -out inter.crtopenssl x509 -req -in srv.csr -days 90 \ -CA inter.crt -CAkey inter.key -CAcreateserial \ -extfile ext.cnf -extensions srv -out srv.crtopenssl x509 -req -in srvec.csr -days 90 \ -CA inter.crt -CAkey inter.key \ -extfile ext.cnf -extensions srv -out srvec.crtls *.srl; cat inter.srlopenssl x509 -in srv.crt -noout -serial -issueropenssl x509 -in srvec.crt -noout -serialopenssl verify -CAfile root.crt \ -untrusted inter.crt srv.crt srvec.crtinter.srlroot.srl296AF06761C163D814AC1C37B182EF3A3DD802serial=296AF06761C163D814AC1C37B182EF3A3DD801issuer=CN=N171 Issuing CAserial=296AF06761C163D814AC1C37B182EF3A3DD802$ openssl verify -CAfile root.crt -untrusted inter.crt srv.crt srvec.crtsrv.crt: OKsrvec.crt: OK| Option | What it does |
|---|---|
| -next_serial | Input serial plus one |
| -preserve_dates | Keep the old notBefore/notAfter when re-signing |
| -force_pubkey F | Issue the cert for this public key |
| -clrext | Drop old extensions |
| -sigopt nm:v | Signing option (e.g. PSS) |
| -badsig | Corrupt the signature (testing) |
| -CAform / -CAkeyform DER | CA cert / key in DER |
| -CAserial F | Serial file (default .srl) |
171.4 x509: input, output and format conversion
- -outform DER writes binary DER: 1103 bytes PEM became 773 bytes DER.
- -inform DER says the input is binary; -out writes PEM again (PEM is the default).
- -noout prints nothing and exits 0: handy to just validate a file.
- Others: -passin (encrypted input password), -x509toreq (cert to CSR), -copy_extensions, -keyform.
cd /labopenssl x509 -in srv.crt -outform DER -out srv.derls -l srv.crt srv.deropenssl x509 -in srv.der -inform DER -noout -subjectopenssl x509 -in srv.der -inform DER -out back.pemhead -1 back.pem; tail -1 back.pemopenssl x509 -in srv.crt -noout; echo "(noout: exit $?)"-rw-r--r-- 1 root root 1103 Nov 9 09:09 srv.crt-rw-r--r-- 1 root root 773 Nov 9 09:09 srv.dersubject=CN=srv.lab-----BEGIN CERTIFICATE-----<hidden>-----END CERTIFICATE-----(noout: exit 0)171.5 x509: printing certificate information
cd /labX="openssl x509 -in srv.crt -noout"$X -subject -issuer -dates -serial$X -fingerprint -sha256$X -ext subjectAltName$X -ocsp_uri$X -email$X -hash -subject_hash -issuer_hash$X -pubkey | sed -n '1p;$p'$X -purpose | head -3subject=CN=srv.labissuer=CN=N171 Issuing CAnotBefore=Nov 9 09:09:24 2026 GMTnotAfter=Feb 7 09:09:24 2027 GMTserial=296AF06761C163D814AC1C37B182EF3A3DD801sha256 Fingerprint=12:6A:3C:05:...:5A:B0:EC:CAX509v3 Subject Alternative Name: DNS:srv.lab, IP Address:192.168.191.10, email:admin@srv.labhttp://192.168.191.10:8888admin@srv.lab094da2b81def3801-----BEGIN PUBLIC KEY-----<hidden>-----END PUBLIC KEY-----Certificate purposes:SSL client : NoSSL client CA : No- -fingerprint -sha256 matches the fingerprint a browser shows (plain -fingerprint defaults to SHA-1).
- -hash and -subject_hash are the same value, so only two hash lines came.
- -purpose shows this is not an SSL client cert (it is a server cert).
- Also useful: -dateopt iso_8601, -startdate/-enddate, -nameopt RFC2253, -modulus to match cert and key.
171.6 x509: -certopt text flags
- -text prints the whole cert (very long); -certopt hides parts.
- no_pubkey,no_sigdump,no_extensions leaves version, serial, issuer, dates and subject.
- ca_default is the short openssl ca format: issuer is removed too. ext_default/ext_error/ext_parse/ext_dump decide what happens to unknown extensions.
openssl x509 -in srv.crt -noout -text -certopt \ no_pubkey,no_sigdump,no_extensionsCertificate: Data: Version: 3 (0x2) Serial Number: 29:6a:f0:67:61:c1:63:d8:14:ac:1c:37:b1:82:ef:3a:3d:d8:01 Signature Algorithm: ecdsa-with-SHA256 Issuer: CN=N171 Issuing CA Validity Not Before: Nov 9 09:09:24 2026 GMT Not After : Feb 7 09:09:24 2027 GMT Subject: CN=srv.lab171.7 x509: checking certificates (monitoring)
cd /labdate -uopenssl x509 -in srv.crt -noout -checkend 86400echo "exit $?"openssl x509 -in short.crt -noout -checkend 86400echo "exit $?"openssl x509 -in srv.crt -noout -checkhost srv.labopenssl x509 -in srv.crt -noout -checkhost shop.labopenssl x509 -in srv.crt -noout -checkip 192.168.191.10openssl x509 -in srv.crt -noout -checkemail admin@srv.labMon Nov 9 09:09:38 UTC 2026Certificate will not expireexit 0Certificate will expireexit 1Hostname srv.lab does match certificateHostname shop.lab does NOT match certificateIP 192.168.191.10 does match certificateEmail admin@srv.lab does match certificate- -checkend N: exit 0 if still valid after N seconds, exit 1 if it expires: perfect for cron alerts.
- -checkhost / -checkip / -checkemail match against the SAN.
171.8 x509: trust settings, providers and engines
openssl x509 -in root.crt -addtrust serverAuth \ -addtrust clientAuth -addreject emailProtection \ -setalias "N171 lab root" -trustout -out trusted.pemopenssl x509 -in trusted.pem -noout -text \ | grep -A1 -E 'Trusted Uses|Rejected Uses|Alias'Trusted Uses: TLS Web Server Authentication, TLS Web Client AuthenticationRejected Uses: E-mail ProtectionAlias: N171 lab root- -trustout writes BEGIN TRUSTED CERTIFICATE; -clrtrust/-clrreject empty the lists (No Trusted Uses.).
- -provider default / -propquery provider=default choose the algorithm provider; -writerand seed.bin saved a 1024-byte RNG state.
- -engine is deprecated: -engine pkcs11 gave Invalid engine "pkcs11" in this build.
171.9 s_server: listening and transport
| Option | Meaning |
|---|---|
| -accept [host:]port | Listen address, default *:4433 |
| -port N | Port only |
| -4 / -6 | IPv4 only / IPv6 only |
| -naccept N | Exit after N connections |
| -unix /tmp/s.sock | Accept on a Unix socket |
| -www / -WWW / -rev | Status page / serve files / echo line reversed |
| -crlf / -ign_eof | Terminal input behaviour |
# srvs() { sleep 15 | openssl s_server -naccept 1 \ -cert srv.crt -key srv.key "$@"; }s -accept 192.168.191.10:4433 -4 -www &s -port 8443 -WWW &s -port 9443 -rev &netstat -tln | grep -E ':(4433|8443|9443) '# clientecho "hello from client" \ | timeout 3 openssl s_client -quiet -connect srv.lab:9443 2>/dev/nulltcp 0 0 192.168.191.10:4433 0.0.0.0:* LISTENtcp 0 0 :::9443 :::* LISTENtcp 0 0 :::8443 :::* LISTENtneilc morf olleh171.10 s_server: server certificate, chain and dual certs
# srvs -accept 4441 > /tmp/s4441.log 2>&1 &s -accept 4442 -cert_chain inter.crt > /tmp/s4442.log 2>&1 &# clientfor p in 4441 4442; do echo "--- port $p" openssl s_client -connect srv.lab:$p \ -CAfile root.crt </dev/null 2>&1 \ | grep -E '^depth|Verify return'done--- port 4441depth=0 CN=srv.labVerify return code: 21 (unable to verify the first certificate)--- port 4442depth=2 CN=N171 Root CAdepth=1 CN=N171 Issuing CAdepth=0 CN=srv.labVerify return code: 0 (ok)- -dcert/-dkey add a second cert on the same port: the client asking rsa_pss_rsae_sha256 got the 2048-bit RSA cert, ecdsa_secp256r1_sha256 got the 256-bit EC cert.
- -build_chain / -chainCAfile let the server build the chain; -cert2/-key2 serve a second SNI host.
- -xkey/-xcert/-xchain (extended certs): EC client got Overall Validity: OK, RSA-PSS client failed with no suitable signature algorithm; the main -cert was not a fallback, so use -dcert normally.
171.11 s_server: client certificate verification (mTLS) and CRL
- Step 1
1.Server (-Verify 1) sends CertificateRequest with CA names
- Step 2
2.Client sends an empty Certificate
- Step 3
3.Server: peer did not return a certificate, alert 116 (certificate required)
- Step 4
4.Client retries with CN=billing-service (EKU clientAuth)
- Step 5
5.Server verifies depth 2, 1, 0 with -CAfile cas.pem: verify return:1
| Port / setup | Client sends | Result |
|---|---|---|
| 5001 -verify 1 | No cert | Accepted (cert only requested) |
| 5002 -Verify 1 | No cert | alert certificate required (116) |
| 5003 -Verify 1 | billing-service | Accepted, chain verified |
| 5004 -CRL -crl_check | Revoked old-service | verify error:num=23 logged, but still accepted |
| 5005 + -verify_return_error | Revoked old-service | alert certificate revoked (44) |
| 5006 + -verify_return_error | billing-service | Accepted |
# srvs() { sleep 15 | openssl s_server -naccept 1 \ -cert srv.crt -key srv.key -cert_chain inter.crt \ -CAfile cas.pem -Verify 1 -CRL inter.crl -crl_check "$@"; }s -accept 5005 -verify_return_error > /tmp/c5005.log 2>&1 &# clientsleep 1 | openssl s_client -CAfile root.crt -connect srv.lab:5005 \ -cert old.crt -key old.key 2>&1 | grep -E 'alert'...:ssl/tls alert certificate revoked:...:SSL alert number 44[srv /tmp/c5005.log]depth=0 CN=old-serviceverify error:num=23:certificate revokedERRORverify error:certificate revoked- -verifyCAfile keeps client-CA trust separate from the server chain; -crl_download fetches the CRL from the CDP; -no_ca_names hides the CA list.
- Shared verify options (also in s_client and openssl verify): -attime, -crl_check_all, -partial_chain, -verify_depth, -verify_hostname, -purpose.
171.12 Common mistakes
- Thinking -verify makes the client cert required: it only asks; use -Verify.
- Using -crl_check without -verify_return_error: the revoked cert is logged but accepted.
- Serving only the leaf cert: clients get Verify return code 21.
- Using plain -fingerprint and comparing with a browser's SHA-256 value.
171.13 Interview tips
- Verify return code 21 = server did not send the intermediate; fix with -cert_chain / a full-chain file.
- Expiry in 7 days from cron: openssl x509 -noout -checkend 604800 (exit 1 = alert).
- PEM vs DER: same cert, base64 text vs binary; convert with -inform/-outform.
- Strict mTLS on s_server: -Verify + -crl_check + -verify_return_error.