OpenSSL s_client and s_server
When a TLS connection fails, or when you need to know exactly what a server presents, the browser is the wrong tool: it hides details and caches decisions. openssl s_client is a bare TLS client that prints what was negotiated and why a certificate was accepted or rejected; openssl s_server is a bare TLS server for tests. Together with openssl x509 and openssl verify they answer most certificate questions in an incident or an audit: which certificate is served, who issued it, for which names, until when, and whether a client that checks properly would trust it. Every session here runs against servers started on 127.0.0.1 in the sandbox with the lab PKI in $SAMPLES/pki (lab-only keys derived from a fixed seed; never use such keys for anything real). The cryptography behind it is in Applied Cryptography.
Reading a certificate
P=$SAMPLES/pki
openssl x509 -in $P/lab.test.crt -noout -subject -issuer -dates -serial -ext subjectAltName
echo "--- chain check against the lab CA:"
openssl verify -CAfile $P/ca.crt $P/lab.test.crt
openssl verify -CAfile $P/ca.crt $P/expired.lab.test.crt 2>&1subject=C = XX, O = ExcelTAD Lab, OU = Web, CN = lab.test
issuer=C = XX, O = ExcelTAD Lab, CN = ExcelTAD Lab Root CA
notBefore=Jan 1 00:00:00 2026 GMT
notAfter=Jan 1 00:00:00 2028 GMT
serial=1002
X509v3 Subject Alternative Name:
DNS:lab.test, DNS:www.lab.test
--- chain check against the lab CA:
/opt/sec-samples/pki/lab.test.crt: OK
C = XX, O = ExcelTAD Lab, OU = Web, CN = expired.lab.test
error 10 at 0 depth lookup: certificate has expired
error /opt/sec-samples/pki/expired.lab.test.crt: verification failed
- subject / issuer: who the certificate is for and who signed it. The subject's CN is legacy; clients match names against the subjectAltName (
lab.test,www.lab.test). - notBefore / notAfter: the validity window. The second certificate expired in 2021.
openssl verifybuilds the chain from the leaf to a trusted CA and checks signatures and dates; it does not check host names.
A verified connection
The session starts s_server with the lab certificate and connects with s_client. -servername sends SNI, -CAfile says which CA to trust, -verify_hostname makes s_client check the name like a browser would, and -brief prints a short summary instead of the long default output.
P=$SAMPLES/pki
openssl s_server -accept 127.0.0.1:8443 -cert $P/lab.test.crt -key $P/lab.test.key -quiet >/dev/null 2>&1 &
for i in $(seq 50); do ss -Htln 'sport = :8443' | grep -q . && break; sleep 0.1; done
openssl s_client -connect 127.0.0.1:8443 -servername lab.test -CAfile $P/ca.crt -verify_hostname lab.test -brief </dev/null 2>&1
kill %1CONNECTION ESTABLISHED
Protocol version: TLSv1.3
Ciphersuite: TLS_AES_256_GCM_SHA384
Peer certificate: C = XX, O = ExcelTAD Lab, OU = Web, CN = lab.test
Hash used: SHA256
Signature type: RSA-PSS
Verification: OK
Verified peername: lab.test
Server Temp Key: X25519, 253 bits
TLS 1.3 with AES-256-GCM, an X25519 key exchange, an RSA-PSS signature by the server, and the two lines that matter for trust: Verification: OK (the chain leads to the CA we trust) and Verified peername: lab.test (the certificate is valid for the name we asked for).
Three ways it should fail
A client that checks properly must refuse each of the following. The session shows what s_client reports (only the first lines of each, without OpenSSL's internal error codes).
P=$SAMPLES/pki
openssl s_server -accept 127.0.0.1:8443 -cert $P/lab.test.crt -key $P/lab.test.key -quiet >/dev/null 2>&1 &
openssl s_server -accept 127.0.0.1:8444 -cert $P/expired.lab.test.crt -key $P/expired.lab.test.key -quiet >/dev/null 2>&1 &
for i in $(seq 50); do [ "$(ss -Htln | wc -l)" -ge 2 ] && break; sleep 0.1; done
echo "== 1. right CA, wrong name (we wanted intranet.lab.test):"
openssl s_client -connect 127.0.0.1:8443 -servername lab.test -CAfile $P/ca.crt -verify_hostname intranet.lab.test \
-verify_return_error -brief </dev/null 2>&1 | grep -v ':error:'
echo "== 2. no trusted CA given (the lab CA isn't in the system store):"
openssl s_client -connect 127.0.0.1:8443 -servername lab.test -verify_return_error -brief </dev/null 2>&1 | grep -v ':error:'
echo "== 3. expired certificate:"
openssl s_client -connect 127.0.0.1:8444 -servername expired.lab.test -CAfile $P/ca.crt -verify_return_error -brief </dev/null 2>&1 | grep -v ':error:'
echo "== 4. a client that offers only TLS 1.1:"
openssl s_client -connect 127.0.0.1:8443 -tls1_1 -brief </dev/null 2>&1 | grep -c 'no protocols available'
kill %1 %2== 1. right CA, wrong name (we wanted intranet.lab.test):
depth=0 C = XX, O = ExcelTAD Lab, OU = Web, CN = lab.test
verify error:num=62:hostname mismatch
== 2. no trusted CA given (the lab CA isn't in the system store):
depth=0 C = XX, O = ExcelTAD Lab, OU = Web, CN = lab.test
verify error:num=20:unable to get local issuer certificate
== 3. expired certificate:
depth=0 C = XX, O = ExcelTAD Lab, OU = Web, CN = expired.lab.test
verify error:num=10:certificate has expired
notAfter=Jan 1 00:00:00 2021 GMT
== 4. a client that offers only TLS 1.1:
1
- Hostname mismatch (error 62): the certificate is genuine but for other names. This is what a man-in-the-middle with a valid certificate for its own domain looks like.
- Unknown issuer (error 20): the chain doesn't lead to a trusted CA. Self-signed and private-CA certificates fail here unless the client is configured to trust that CA. This is what an interception proxy with its own CA looks like on a machine that doesn't trust it.
- Expired (error 10).
- No protocols available: OpenSSL 3 at its default security level (and Ubuntu's build, which also sets TLS 1.2 as the minimum) refuses TLS 1.1 before anything is sent, which is the behaviour you want from clients and servers (RFC 8996).
Without -verify_return_error, s_client reports the problem and continues anyway ("Verification error: …" followed by an established connection). Many scripts and libraries behave the same way when verification is switched off, which is the root of many man-in-the-middle findings.
Using it in an assessment
- Which certificate does each of my servers serve, for which names, and when does it expire?
s_client -connect host:443 -servername name </dev/null 2>/dev/null | openssl x509 -noout -subject -dates -ext subjectAltName(on systems with network access, within your scope). - Does a server still accept old protocols or weak ciphers?
-tls1,-tls1_1,-cipher 'DES-CBC3-SHA'(OpenSSL 3 may need-cipher 'DEFAULT:@SECLEVEL=0'to even try); dedicated scanners such as testssl.sh automate the checks. - Does STARTTLS work on mail or other services?
-starttls smtp|imap|pop3|ftp|ldap(SMTP, POP3, IMAP and STARTTLS). - Does the server send the full chain?
-showcertslists every certificate it sent; a missing intermediate breaks some clients but not others.
How problems are detected and fixed
| Problem | Detected by | Fix |
|---|---|---|
| expired or soon-expiring certificates | inventory plus x509 -checkend <seconds> in monitoring; Certificate Transparency monitoring for your domains |
automated renewal (ACME), alerts 30 days ahead |
| wrong names, missing SAN entries | -verify_hostname checks per service name |
issue certificates with every served name in SAN |
| clients that don't verify | code review, tests against a server with a wrong-name or self-signed certificate (as above) | never disable verification; pin CAs for internal services |
| old protocol versions | -tls1_1 probes, scanner reports |
TLS 1.2+ only; prefer 1.3 |
| interception or rogue CAs | unexpected issuers in s_client output from inside the network |
inventory trusted CAs; certificate pinning where justified |
Pitfalls
- Forgetting
-servername: servers that host several sites then return a default certificate, and you debug the wrong one. - Reading
Verification: OKas "the name matches". Without-verify_hostname(or-verify_ip), s_client doesn't check names. - Using the full s_client output in reports: it contains session tickets and random values;
-briefandx509extracts are clearer.
Practice
1. A colleague's script uses curl -k. Which of the four failures above would it still accept?
-k disables certificate verification entirely, so it accepts the wrong name, the unknown issuer and the expired certificate (1–3). Only the protocol-version refusal (4) still happens, because that is negotiated before certificates.
2. Which certificate field must contain www.lab.test for a browser to accept it for that name?
The subjectAltName extension (as a DNS name). Browsers ignore the subject CN for name matching.
3. How would you test whether a service at 10.10.0.10:8443 in your lab scope presents an expired certificate, without trusting the result of a browser?
openssl s_client -connect 10.10.0.10:8443 -servername <name> -CAfile <your CA> -verify_return_error -brief </dev/null and look for verify error:num=10:certificate has expired; or pipe the certificate into openssl x509 -noout -dates.
Test yourself
Which s_client option makes it check the certificate's name like a browser?
- -servername
- -verify_hostname
- -brief
- -CAfile
What does openssl verify -CAfile ca.crt leaf.crt NOT check?
- The signature chain
- The validity dates
- Whether the certificate matches a host name
- Whether the issuer is trusted
s_client reports verify error:num=20:unable to get local issuer certificate. What does it mean?
- The certificate expired
- The chain doesn't lead to a CA the client trusts
- The host name is wrong
- TLS 1.3 is not supported
Why is -verify_return_error important in tests?
- It speeds up the handshake
- Without it, s_client reports verification problems but continues the connection
- It enables TLS 1.1
- It prints the private key