Skip to content
Sign inGet all access
Chapter 10·10 minFree

TLS

What the handshake exchanges, and how to read s_client output.

Not a cryptography chapter. The goal is reading s_client and diagnosing four errors.

What TLS provides

Three separate things, and conflating them causes the confusion:

Means Fails as
Encryption nobody between can read it nothing — it just works
Integrity nobody can change it a connection reset
Identity the server is who it claims a certificate error

Only the third asks you a question, which is why every TLS problem looks like a certificate problem.

A server to practise against

A certificate and key, then nginx in front:

terminal
openssl req -x509 -newkey rsa:2048 -nodes -days 1 \
    -keyout k.pem -out c.pem -subj '/CN=localhost'
Output$ openssl req -x509 -newkey rsa:2048 -nodes -days 1 \ -keyout k.pem -out c.pem -subj '/CN=localhost'$ docker run -d --name tls -p 8443:443 \ -v $PWD/tls.conf:/etc/nginx/conf.d/default.conf:ro \ -v $PWD/c.pem:/etc/nginx/certs/c.pem:ro \ -v $PWD/k.pem:/etc/nginx/certs/k.pem:ro nginx:1.27-alpine$ curl -sk https://localhost:8443/secure

-k skips verification, which a self-signed certificate needs. The rest of the chapter is why, and the alternative.

tls.conf
server {
    listen 443 ssl;
    http2 on;
    ssl_certificate     /etc/nginx/certs/c.pem;
    ssl_certificate_key /etc/nginx/certs/k.pem;
    location / { return 200 "secure\n"; default_type text/plain; }
}

openssl s_client

It completes a handshake and prints what was agreed. Five lines matter:

terminal
echo | openssl s_client -connect localhost:8443 2>/dev/null \
  | grep -E 'subject=|issuer=|Protocol|Cipher is|Verify return code'
Outputsubject=CN=localhostissuer=CN=localhostNew, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384Protocol: TLSv1.3Verify return code: 18 (self-signed certificate)

The handshake happens before any HTTP — version and cipher are settled before a request is written.

Certificates

A certificate asserts a name and is signed by an issuer. Here they match:

terminal
openssl x509 -in c.pem -noout -subject -issuer
Outputsubject=CN=localhostissuer=CN=localhost

Subject equal to issuer is self-signed. It also carries a validity window, and expiry is the commonest TLS failure:

terminal
openssl x509 -in c.pem -noout -dates
OutputnotBefore=Nov 27 08:56:30 2026 GMTnotAfter=Nov 28 08:56:30 2026 GMT

The chain of trust

curl rejects the certificate because it does not recognise the issuer:

terminal
curl -sS https://localhost:8443/ 2>&1 | head -1
Outputcurl: (60) SSL certificate problem: self signed certificate

Hand it the issuer and the same request succeeds:

terminal
curl -sS --cacert c.pem https://localhost:8443/
Outputsecure

That is the mechanism: a leaf, signed by an issuer, up to a root the client trusts. A real chain has an intermediate between them.

ALPN

The protocol is chosen inside the handshake, not by the URL:

terminal
echo | openssl s_client -connect localhost:8443 -alpn h2 2>/dev/null \
  | grep -i 'ALPN protocol'
OutputALPN protocol: h2

Ask for HTTP/1.1 instead and the same server agrees to that:

terminal
echo | openssl s_client -connect localhost:8443 -alpn http/1.1 2>/dev/null \
  | grep -i 'ALPN protocol'
OutputALPN protocol: http/1.1

That is why HTTP/2 needs no new port and no new scheme — chapter 12.

TLS 1.3 versus 1.2

Both are available here, and the cipher names differ:

terminal
echo | openssl s_client -connect localhost:8443 -tls1_2 2>/dev/null \
  | grep '^New,'
OutputNew, TLSv1.2, Cipher is ECDHE-RSA-AES256-GCM-SHA384
terminal
echo | openssl s_client -connect localhost:8443 -tls1_3 2>/dev/null \
  | grep '^New,'
OutputNew, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384

1.3 dropped the broken options and cut the handshake to one round trip. Its cipher names omit the key exchange — there is only one choice left.

SNI

One IP serves many names, so the client states which it wants, in the clear:

terminal
echo | openssl s_client -connect localhost:8443 -servername localhost \
  2>/dev/null | grep 'subject='
Outputsubject=CN=localhost

Reading a TLS error

Four failures, and s_client names each one:

Message Means
self signed certificate the issuer is not trusted
certificate has expired past notAfter
Hostname mismatch the name does not match the certificate
unable to get local issuer certificate the intermediate is missing

Cleartext to a TLS port is a different failure, answered in HTTP:

terminal
printf '%s\r\n' 'GET / HTTP/1.1' 'Host: localhost' \
    'Connection: close' '' | nc -w 2 localhost 8443 | head -1
OutputHTTP/1.1 400 Bad Request