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:
openssl req -x509 -newkey rsa:2048 -nodes -days 1 \
-keyout k.pem -out c.pem -subj '/CN=localhost'-k skips verification, which a self-signed certificate needs. The rest of the chapter is
why, and the alternative.
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:
echo | openssl s_client -connect localhost:8443 2>/dev/null \
| grep -E 'subject=|issuer=|Protocol|Cipher is|Verify return code'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:
openssl x509 -in c.pem -noout -subject -issuerSubject equal to issuer is self-signed. It also carries a validity window, and expiry is the commonest TLS failure:
openssl x509 -in c.pem -noout -datesThe chain of trust
curl rejects the certificate because it does not recognise the issuer:
curl -sS https://localhost:8443/ 2>&1 | head -1Hand it the issuer and the same request succeeds:
curl -sS --cacert c.pem https://localhost:8443/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:
echo | openssl s_client -connect localhost:8443 -alpn h2 2>/dev/null \
| grep -i 'ALPN protocol'Ask for HTTP/1.1 instead and the same server agrees to that:
echo | openssl s_client -connect localhost:8443 -alpn http/1.1 2>/dev/null \
| grep -i 'ALPN protocol'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:
echo | openssl s_client -connect localhost:8443 -tls1_2 2>/dev/null \
| grep '^New,'echo | openssl s_client -connect localhost:8443 -tls1_3 2>/dev/null \
| grep '^New,'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:
echo | openssl s_client -connect localhost:8443 -servername localhost \
2>/dev/null | grep 'subject='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:
printf '%s\r\n' 'GET / HTTP/1.1' 'Host: localhost' \
'Connection: close' '' | nc -w 2 localhost 8443 | head -1