SkillPilotAI

openssl (s_client, s_server, x509, req, verify, ciphers, ocsp, s_time, genpkey, pkcs12) (1/5)

openssl s_client is a generic TLS client: it connects to any TLS service and shows the certificate chain, protocol version, cipher, ALPN, OCSP status and the verification result. Part 1 of openssl covers all of s_client, the first tool to reach for when an HTTPS, SMTP or mTLS connection fails.

170.1 Why is it used?

  • See exactly what a server presents: the full chain, version, cipher, key exchange group and ALPN.
  • Find the cause of certificate errors through the Verify return code (incomplete chain, expired, self-signed, wrong name).
  • Test which TLS versions, ciphers, groups and signature algorithms a server accepts.
  • Test STARTTLS services (SMTP, IMAP, LDAP...), OCSP stapling, session resumption, mTLS client certs, DANE and PSK.
  • Windows: not built in (Git for Windows or winget for OpenSSL 3); Linux: usually preinstalled; macOS: /usr/bin/openssl is LibreSSL (fewer options), brew install openssl@3.

170.2 How it works

  • Lab: client 192.168.190.20 and srv.lab 192.168.190.10 (OpenSSL 3.3.0), lab Root CA + Issuing CA, nine s_server instances on different ports.
  • Ports: 443 (normal, ALPN, OCSP), 4433 (client renegotiation), 4434, 4435 (TLS 1.3 only), 8443 (expired), 8444 (self-signed), 8445 (no chain), 8446 (mTLS), 8447 (PSK, no cert), SMTP stub 25 (STARTTLS) and 2525 (no STARTTLS).
  • s_client prints its result but by default does not abort on a verify error: check "Verify return code".
  • Input from stdin is sent to the server; single letters at line start are commands (Q, R, k, K).
s_client and s_server: TLS 1.3 handshake
  1. 1.s_client 192.168.190.20 sends ClientHello with SNI srv.lab and ALPN

  2. 2.srv.lab 192.168.190.10:443 replies ServerHello + EncryptedExtensions

  3. 3.Server sends Certificate (leaf + Issuing CA) and CertificateVerify

  4. 4.Client verifies the chain against -CAfile root.crt

  5. 5.Both sides send Finished: New, TLSv1.3, TLS_AES_256_GCM_SHA384

  6. 6.Encrypted application data starts; Verify return code: 0 (ok)

170.3 Connection and target

  • -connect host:port (or host:port after the options), -servername sets SNI, -noservername sends none.
  • -4 / -6 force the address family; -verify_hostname checks the certificate name.
  • Output: depth 2 = root CA, depth 1 = intermediate, depth 0 = leaf.
  • Others: -bind ip:port, -proxy host:8080 (HTTP CONNECT), -proxy_user / -proxy_pass, -unix socket.
Terminal
openssl versionecho | openssl s_client -connect srv.lab:443 -servername srv.lab -CAfile root.crt
Output
OpenSSL 3.3.0 9 Apr 2024 (Library: OpenSSL 3.3.0 9 Apr 2024)Connecting to 192.168.190.10CONNECTED(00000003)depth=2 C=IN, O=N170 Lab, CN=N170 Lab Root CAdepth=1 C=IN, O=N170 Lab, CN=N170 Lab Issuing CAdepth=0 CN=srv.labCertificate chain 0 s:CN=srv.lab   i:C=IN, O=N170 Lab, CN=N170 Lab Issuing CA 1 s:C=IN, O=N170 Lab, CN=N170 Lab Issuing CA   i:C=IN, O=N170 Lab, CN=N170 Lab Root CAServer Temp Key: X25519, 253 bitsSSL handshake has read 1432 bytes and written 395 bytesVerification: OKNew, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384This TLS version forbids renegotiation.No ALPN negotiatedVerify return code: 0 (ok)DONE

170.4 Output, display and debugging

  • -brief: short summary (version, ciphersuite, peer cert, signature type, verification).
  • -showcerts prints every certificate the server sent; -state shows handshake states; -msg every TLS message.
  • -debug hex dumps raw reads/writes; -trace decodes the protocol; -tlsextdebug dumps extensions.
  • -keylogfile writes TLS secrets for Wireshark: protect it like a private key.
Terminal
s() { echo | openssl s_client -CAfile root.crt -connect srv.lab:443 "$@"; }s -briefs -msg | grep -E '^(>>>|<<<).*Handshake'
Output
CONNECTION ESTABLISHEDProtocol version: TLSv1.3Ciphersuite: TLS_AES_256_GCM_SHA384Peer certificate: CN=srv.labHash used: SHA256Signature type: ECDSAVerification: OKServer Temp Key: X25519, 253 bits>>> TLS 1.3, Handshake [length 0136], ClientHello<<< TLS 1.3, Handshake [length 007a], ServerHello<<< TLS 1.3, Handshake [length 0006], EncryptedExtensions<<< TLS 1.3, Handshake [length 0432], Certificate<<< TLS 1.3, Handshake [length 004e], CertificateVerify<<< TLS 1.3, Handshake [length 0034], Finished>>> TLS 1.3, Handshake [length 0034], Finished

170.5 Interactive input

  • With piped input s_client closes as soon as input ends (DONE before the HTTP reply).
  • -ign_eof keeps the connection open after EOF (got HTTP/1.0 200 ok); -quiet implies it; -no_ign_eof closes on EOF.
  • Commands at line start: Q quit, R renegotiate (TLS 1.2), k KeyUpdate (TLS 1.3), K KeyUpdate + ask the peer; -nocommands turns them into data.
  • -crlf converts LF to CR+LF (needed for SMTP/IMAP).
Terminal
s() { printf 'GET / HTTP/1.0\r\n\r\n' | openssl s_client -CAfile root.crt -connect srv.lab:443 "$@"; }s | grep -E '^HTTP|DONE's -ign_eof | grep -E '^HTTP|closed'
Output
DONEHTTP/1.0 200 okclosed

170.6 Protocol versions

  • -tls1_2 and -no_tls1_3 both negotiated TLS 1.2.
  • Port 4435 accepts only TLS 1.3: -tls1_2 there gives "tlsv1 alert protocol version" (alert 70).
  • -tls1_1 fails on the client itself ("no protocols available"): TLS 1.1 is off by default in OpenSSL 3.
  • -min_protocol / -max_protocol set a range; -dtls* are for UDP; -fallback_scsv tests downgrade protection.
Terminal
s() { echo | openssl s_client -brief -CAfile root.crt "$@"; }s -tls1_2 -connect srv.lab:443s -tls1_2 -connect srv.lab:4435s -tls1_1 -connect srv.lab:443
Output
CONNECTION ESTABLISHEDProtocol version: TLSv1.2Ciphersuite: ECDHE-ECDSA-AES256-GCM-SHA384...:error:0A00042E:SSL routines:ssl3_read_bytes:tlsv1 alert protocol version:...:SSL alert number 70...:error:0A0000BF:SSL routines:tls_setup_handshake:no protocols available:ssl/statem/statem_lib.c:153:

170.7 Ciphers, groups and signature algorithms

  • -cipher sets the TLS 1.2 list; ECDSA ciphers work because the certificate has an EC key.
  • RC4-MD5: "no cipher match" on the client; an RSA cipher: server sends handshake failure (alert 40).
  • -ciphersuites sets TLS 1.3 suites (ChaCha20 worked); -groups P-384 changed the key exchange; -sigalgs RSA-PSS+SHA256 failed with an EC cert.
Terminal
s() { echo | openssl s_client -brief -CAfile root.crt -connect srv.lab:443 "$@"; }s -tls1_2 -cipher ECDHE-ECDSA-AES128-GCM-SHA256s -cipher RC4-MD5s -tls1_2 -cipher ECDHE-RSA-AES128-GCM-SHA256s -ciphersuites TLS_CHACHA20_POLY1305_SHA256s -groups P-384
Output
Protocol version: TLSv1.2Ciphersuite: ECDHE-ECDSA-AES128-GCM-SHA256Signature type: ECDSACall to SSL_CONF_cmd(-cipher, RC4-MD5) failed...:error:0A0000B9:SSL routines:SSL_CTX_set_cipher_list:no cipher match:......:error:0A000410:SSL routines:ssl3_read_bytes:ssl/tls alert handshake failure:...:SSL alert number 40Protocol version: TLSv1.3Ciphersuite: TLS_CHACHA20_POLY1305_SHA256Ciphersuite: TLS_AES_256_GCM_SHA384Server Temp Key: ECDH, secp384r1, 384 bits
OptionApplies toExample
-cipherTLS 1.2 and belowECDHE-ECDSA-AES128-GCM-SHA256
-ciphersuitesTLS 1.3 onlyTLS_CHACHA20_POLY1305_SHA256
-groups (old: -curves)Key exchangeX25519, P-256, P-384
-sigalgsSignature algorithmsRSA-PSS+SHA256, ECDSA+SHA256

170.8 STARTTLS

  • Mail and similar protocols start in plain text; -starttls smtp sends EHLO, then STARTTLS, then does TLS on the same TCP connection.
  • -crlf is needed for the lab SMTP stub (it rejects bare LF: "500 5.5.2 bare LF").
  • Port 2525 has no STARTTLS: "Didn't find STARTTLS in server response" and "wrong version number".
  • Other protocols: pop3, imap, ftp, xmpp, irc, postgres, mysql, lmtp, nntp, sieve, ldap; -name sets the EHLO name.
STARTTLS: upgrading SMTP to TLS
  1. 1.s_client connects to 192.168.190.10:25 in plain text

  2. 2.Client sends EHLO client.lab

  3. 3.Server list includes 250-STARTTLS

  4. 4.Client sends STARTTLS, server replies Ready to start TLS

  5. 5.TLS handshake on the same TCP connection

  6. 6.EHLO and MAIL FROM now run inside TLS

Terminal
slow() { while read -r l; do echo "$l"; sleep 1; done; }s() { openssl s_client -starttls smtp -CAfile root.crt "$@"; }slow <<EOT | s -quiet -crlf -connect srv.lab:25EHLO client.labMAIL FROM:<test@client.lab>QUITEOTecho | s -brief -connect srv.lab:2525
Output
depth=0 CN=srv.lab250 8BITMIME250-srv.lab250-SIZE 1048576250 2.1.0 Ok (lab stub, nothing is relayed)221 2.0.0 ByeDidn't find STARTTLS in server response, trying anyway......:error:0A00010B:SSL routines:tls_validate_record_header:wrong version number:...

170.9 TLS extensions, sessions and status

  • -alpn h2,http/1.1: server selected h2; -alpn h3: alert 120 (no common protocol).
  • -status asks for OCSP stapling: port 443 "Cert Status: good", port 4434 "no response sent".
  • -sess_out saves the session (secret file), -sess_in reuses it ("Reused, TLSv1.3"); -reconnect reconnects 5 times.
  • Others: -no_ticket, -early_data (0-RTT), -enable_pha, -ct, -maxfraglen, -ignore_unexpected_eof.
Terminal
s() { echo | openssl s_client -CAfile root.crt "$@"; }s -connect srv.lab:443 -alpn h2,http/1.1 | grep ALPNs -connect srv.lab:443 -status | grep -E 'Response Status|Cert Status's -connect srv.lab:4434 -status | grep 'OCSP'echo | openssl s_client -CAfile root.crt -connect srv.lab:443 -reconnect -tls1_2 | grep -c '^Reus'
Output
ALPN protocol: h2    OCSP Response Status: successful (0x0)    Cert Status: goodOCSP response: no response sent5

170.10 Renegotiation and client certificates

  • Renegotiation exists only up to TLS 1.2: port 4433 (-client_renegotiation) allowed R; port 4434 gave "no renegotiation".
  • "Secure Renegotiation IS supported", so -no_legacy_server_connect worked.
  • Port 8446 demands a client cert: without one "certificate required" (alert 116).
  • -cert client.crt -key client.key -cert_chain inter.crt works; without the chain the server says "unknown ca" (alert 48).
Terminal
s() { sleep 1 | openssl s_client -brief -CAfile root.crt -connect srv.lab:8446 "$@"; }ss -cert client.crt -key client.key -cert_chain inter.crts -cert client.crt -key client.key
Output
CONNECTION ESTABLISHEDVerification: OK...:error:0A00045C:SSL routines:ssl3_read_bytes:tlsv13 alert certificate required:...:SSL alert number 116Protocol version: TLSv1.3DONE...:error:0A000418:SSL routines:ssl3_read_bytes:tlsv1 alert unknown ca:...:SSL alert number 48

170.11 Server certificate verification

  • Verification errors are only printed; -verify_return_error aborts the handshake (exit 1).
  • -no_check_time ignores dates (expired cert passed); -partial_chain trusts an intermediate as an anchor.
  • Other checks: -verify_ip, -verify_email, -verify_depth, -purpose sslserver, -crl_check, -attime, -x509_strict.
Terminal
r() { echo | openssl s_client "$@" | grep -E 'Verify return code|^[0-9A-F]+:error'; }r -connect srv.lab:443r -connect srv.lab:443 -CAfile root.crtr -connect srv.lab:8444 -CAfile root.crtr -connect srv.lab:8445 -CAfile root.crtr -connect srv.lab:8443 -CAfile root.crtr -connect srv.lab:443 -CAfile root.crt -verify_hostname wrong.labr -connect srv.lab:443 -CAfile root.crt -verify_hostname wrong.lab -verify_return_error
Output
Verify return code: 20 (unable to get local issuer certificate)Verify return code: 0 (ok)Verify return code: 18 (self-signed certificate)Verify return code: 21 (unable to verify the first certificate)Verify return code: 10 (certificate has expired)Verify return code: 62 (hostname mismatch)...:error:0A000086:SSL routines:tls_post_process_server_certificate:certificate verify failed:...
Verify codeMeaningLab port
0ok443 with -CAfile root.crt
2unable to get issuer certificate8445 with only inter.crt (no -partial_chain)
10certificate has expired8443
18self-signed certificate8444
20unable to get local issuer certificate443 without -CAfile
21unable to verify the first certificate8445 (server sends no chain)
62hostname mismatch-verify_hostname wrong.lab
65no matching DANE TLSA recordswrong TLSA hash

170.12 DANE, PSK and providers

  • DANE: a DNS TLSA record says which cert is valid; "3 1 1 HASH" = SHA-256 of the leaf public key.
  • Correct record: "DANE TLSA 3 1 1 ... matched the EE certificate at depth 0"; wrong hash: error 65.
  • PSK (port 8447, no certificate): both sides share a key; TLS 1.3 used ChaCha20, TLS 1.2 DHE-PSK-AES256-GCM-SHA384; a wrong key gives "illegal parameter" (binder does not verify).
  • SRP options are deprecated; engines are deprecated in favour of providers (-provider legacy).
Terminal
H=$(openssl x509 -in srv.crt -noout -pubkey | openssl pkey -pubin -outform DER | openssl dgst -sha256 -r | cut -d' ' -f1)echo | openssl s_client -brief -connect srv.lab:443 -dane_tlsa_domain srv.lab -dane_tlsa_rrdata "3 1 1 $H"echo | openssl s_client -brief -connect srv.lab:8447 -psk_identity lab-client -psk 1a2b3c4d5e6f7a8b
Output
TLSA 3 1 1 a121f516f449bcb1f5b4e0c3e648f940365d02c1c85e2ba219bbbb14a845b607CONNECTION ESTABLISHEDProtocol version: TLSv1.3Verification: OKVerified peername: srv.labDANE TLSA 3 1 1 ...c85e2ba219bbbb14a845b607 matched the EE certificate at depth 0CONNECTION ESTABLISHEDProtocol version: TLSv1.3Ciphersuite: TLS_CHACHA20_POLY1305_SHA256No peer certificate or raw public key

170.13 Common mistakes

  • Reading "CONNECTION ESTABLISHED" as "certificate OK": the expired cert connected with exit 0; only the Verification line tells you.
  • Connecting to an IP without -servername: the server may send the wrong certificate.
  • Forgetting -CAfile for a private CA: you get code 20 even though the server is fine.
  • Piping input without -ign_eof and wondering why the reply is missing.
  • Leaving -keylogfile or sess.pem files around: they let others decrypt or resume sessions.

170.14 Interview tips

  • Debugging TLS: openssl s_client -connect host:443 -servername host -CAfile ca.pem, then read the Verify return code (20/21 chain, 10 expired, 18 self-signed, 62 name).
  • Then check version, cipher and ALPN with -brief, -tls1_2 / -tls1_3, -alpn h2.
  • In scripts always add -verify_return_error so a bad certificate gives exit 1.
  • -cipher is for TLS 1.2, -ciphersuites for TLS 1.3; renegotiation does not exist in TLS 1.3 (KeyUpdate instead).