SkillPilotAI

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 / OSDetails
x509Read, print, convert, check and sign a certificate
s_serverTest TLS server: -accept, -cert, -key
WindowsComes with Git for Windows, or winget ShiningLight.OpenSSL.Light
Linux / macOSapt/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.
s_server and s_client handshake
  1. 1.n171-srv 192.168.191.10: s_server listens on TCP 4433

  2. 2.n171-client .20 sends ClientHello (SNI srv.lab)

  3. 3.Server sends leaf CN=srv.lab + chain CN=N171 Issuing CA (TLSv1.3)

  4. 4.Client verifies the whole chain against root.crt

  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).
Terminal
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
Output
$ 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 GMT

171.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).
Terminal
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.crt
Output
inter.srlroot.srl296AF06761C163D814AC1C37B182EF3A3DD802serial=296AF06761C163D814AC1C37B182EF3A3DD801issuer=CN=N171 Issuing CAserial=296AF06761C163D814AC1C37B182EF3A3DD802$ openssl verify -CAfile root.crt -untrusted inter.crt srv.crt srvec.crtsrv.crt: OKsrvec.crt: OK
OptionWhat it does
-next_serialInput serial plus one
-preserve_datesKeep the old notBefore/notAfter when re-signing
-force_pubkey FIssue the cert for this public key
-clrextDrop old extensions
-sigopt nm:vSigning option (e.g. PSS)
-badsigCorrupt the signature (testing)
-CAform / -CAkeyform DERCA cert / key in DER
-CAserial FSerial 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.
Terminal
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 $?)"
Output
-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

Terminal
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 -3
Output
subject=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.
Terminal
openssl x509 -in srv.crt -noout -text -certopt \  no_pubkey,no_sigdump,no_extensions
Output
Certificate:    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.lab

171.7 x509: checking certificates (monitoring)

Terminal
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.lab
Output
Mon 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

Terminal
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'
Output
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

OptionMeaning
-accept [host:]portListen address, default *:4433
-port NPort only
-4 / -6IPv4 only / IPv6 only
-naccept NExit after N connections
-unix /tmp/s.sockAccept on a Unix socket
-www / -WWW / -revStatus page / serve files / echo line reversed
-crlf / -ign_eofTerminal input behaviour
Terminal
# 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/null
Output
tcp   0   0 192.168.191.10:4433   0.0.0.0:*   LISTENtcp   0   0 :::9443               :::*        LISTENtcp   0   0 :::8443               :::*        LISTENtneilc morf olleh

171.10 s_server: server certificate, chain and dual certs

Terminal
# 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
Output
--- 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

mTLS with -Verify
  1. 1.Server (-Verify 1) sends CertificateRequest with CA names

  2. 2.Client sends an empty Certificate

  3. 3.Server: peer did not return a certificate, alert 116 (certificate required)

  4. 4.Client retries with CN=billing-service (EKU clientAuth)

  5. 5.Server verifies depth 2, 1, 0 with -CAfile cas.pem: verify return:1

Port / setupClient sendsResult
5001 -verify 1No certAccepted (cert only requested)
5002 -Verify 1No certalert certificate required (116)
5003 -Verify 1billing-serviceAccepted, chain verified
5004 -CRL -crl_checkRevoked old-serviceverify error:num=23 logged, but still accepted
5005 + -verify_return_errorRevoked old-servicealert certificate revoked (44)
5006 + -verify_return_errorbilling-serviceAccepted
Terminal
# 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'
Output
...: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.