Learn Labs
Security

TLS

Certificates and chains, protocols, ALPN and HTTP/2, HSTS, and renewal

TLS gives three things: encryption (nobody on the path can read the traffic), integrity (nobody can change it), and authentication (the client knows it's talking to the real server, because the server proves it holds the private key for a certificate the client trusts).

The handshake, observed

docker compose -f compose_prod.yaml run --rm labs python tls_and_headers.py

TLS version:       TLSv1.3
cipher:            TLS_AES_256_GCM_SHA384
ALPN (protocol):   h2
certificate for:   ['localhost', 'nginx', '127.0.0.1']
issued by:         Infra Labs CA
expires:           Nov 13 16:04:55 2027 GMT
TLS 1.1 client:     refused (NO_PROTOCOLS_AVAILABLE)
TLS 1.2 client:     accepted (TLSv1.2)
SNI evil.example:  TLSV1_UNRECOGNIZED_NAME
without the lab CA: certificate verify failed: unable to get local issuer certif...
clientnginxClientHello: SNI nginx, ALPN h2, http/1.1ServerHello TLSv1.3, ALPN h2certificate (SAN: localhost, nginx)client checks it against ca.crtFinished, then HTTP/2 frames
  • SNI (Server Name Indication): the client says which name it wants before the server sends a certificate, so one IP can serve many sites.
  • ALPN: client and server agree on the application protocol inside TLS. h2 means HTTP/2; Nginx enables it with http2 on;.
  • The certificate lists the names it's valid for in SAN (Subject Alternative Name). Clients check SAN, not the old Common Name.
  • The chain: the client builds a path from the server certificate to a CA it trusts. A client without the lab CA refuses the connection: that's the system working.

Certificates and the lab CA

scripts/certs.sh plays both roles: it creates a CA, then signs a server certificate with it.

the CAopenssl req -x509 -newkey rsa:3072 -nodes -days 3650 -keyout ca.key -out ca.crt \-subj "/CN=Infra Labs CA" \-addext "basicConstraints=critical,CA:TRUE" \-addext "keyUsage=critical,keyCertSign,cRLSign"the server certificate, signed by the CA (san.ext)subjectAltName=DNS:localhost,DNS:nginx,IP:127.0.0.1basicConstraints=CA:FALSEkeyUsage=critical,digitalSignature,keyEnciphermentextendedKeyUsage=serverAuthauthorityKeyIdentifier=keyid,issuer

In production, certificates come from a public CA via ACME (Let's Encrypt with certbot, acme.sh, or lego), or from an internal CA for internal services. Public certificates have been capped at 200 days since March 2026, on a schedule down to 47 days by 2029, which makes automated renewal mandatory: certbot's --deploy-hook "nginx -s reload" loads new certificates without dropping connections.

Protocols and ciphers

ssl_protocols TLSv1.2 TLSv1.3;1.0 and 1.1 are broken and disabled everywheressl_prefer_server_ciphers off;TLS 1.3 suites are all good; let clients choosessl_session_cache shared:SSL:10m;resumption: skip the full handshake for returning clientsssl_session_timeout 1d;ssl_session_tickets off;tickets weaken forward secrecy unless keys rotate

The Mozilla SSL Configuration Generator is the usual starting point; "intermediate" matches this config.

HSTS

Strict-Transport-Security: max-age=63072000 tells browsers: for two years, never use plain HTTP for this host, even if a link says http://. It closes the window where the first request is unencrypted and could be redirected by an attacker.

Port 80

plain HTTP:        301 -> https://nginx:8443/api/whoami
HTTP /healthz:     200

Everything on port 80 redirects to HTTPS, except /healthz, kept on HTTP for load balancers that check plain HTTP.

Monitoring expiry

The Prometheus stack probes the site over HTTPS with the Blackbox exporter, which reports probe_ssl_earliest_cert_expiry. The alert TLSCertificateExpiringSoon fires when that's under 14 days away. An expired certificate is one of the most common full outages, and one of the easiest to prevent.

Lab

  1. Run openssl s_client -connect localhost:8443 -servername localhost -CAfile ca.crt </dev/null and find the chain and the protocol.
  2. Remove TLSv1.2 from ssl_protocols and run the lab again.
  3. Make a certificate without IP:127.0.0.1 in its SAN and request https://127.0.0.1:8443/. What error do you get?

On this page