Part 4 · The web and everyday protocols

TLS 1.3 and PKI: how encryption and trust work on the Internet

lesson 10 / 1917 min1 labpublished updated
Course contents
On this page
  1. The four cryptographic tools you need to know
  2. The TLS 1.3 handshake
  3. Certificates and the chain of trust
  4. Issuing certificates: domain validation and ACME
  5. Certificate Transparency
  6. SNI, ECH and ALPN
  7. mTLS: mutual authentication
  8. TLS for programmers

In lesson 2 you read an HTTP request straight out of a captured packet. Anyone who can see your packets can do the same: whoever runs the Wi-Fi you’re on, your ISP, every router on the way. TLS (Transport Layer Security) is what stops them. It sits between TCP and HTTP, and HTTPS is simply HTTP sent through TLS.

Encryption alone isn’t enough, though. If you set up a perfectly encrypted connection with an impostor, you’ve gained nothing. So TLS also has to prove that the server really is example.com. It does that with a certificate: a small document that says “this key belongs to example.com”, signed by a certificate authority (CA), a company that browsers and operating systems trust to check who owns a domain before signing. Think of a passport: you trust it not because of the person holding it, but because of the government that issued it. The whole system of CAs, certificates and trust is called PKI (public key infrastructure).

TLS (the current version is TLS 1.3, RFC 8446) guarantees three things: confidentiality (no one on the path can read your traffic), integrity (no one can change it without being noticed) and authentication (you are talking to the server you think you are). The third is the hardest, and the whole PKI exists to provide it.

The four cryptographic tools you need to know

You don’t need to know how these algorithms work inside, only what each one is for. In short: a shared secret key encrypts the data quickly; a key pair (one public, one private) proves identity with signatures; a key exchange lets two strangers agree on a shared key without ever sending it; and a hash makes a short fingerprint of any data.

  • Symmetric encryption (AES-GCM, ChaCha20-Poly1305): one shared key both encrypts and decrypts. It is very fast and is used for the data itself. The modes used today (called AEAD) encrypt and detect tampering at the same time.
  • Asymmetric cryptography (RSA, ECDSA, Ed25519): a public key and a private key. It is slow and is used for signatures (proving identity).
  • Key exchange (ECDHE, for example X25519): the two sides arrive at a shared secret without ever sending a key over the network. Because the keys are temporary (ephemeral), a leak of the server’s private key in the future doesn’t unlock traffic recorded in the past. This is forward secrecy.
  • Hashes (SHA-256): a one-way fingerprint of any data, used in signatures and to turn one shared secret into the several keys a connection needs (key derivation).
  • In recent years, hybrid post-quantum key exchange (such as X25519MLKEM768) has become the default in browsers and at Cloudflare and Google, so that today’s traffic stays safe even against future quantum computers.

The TLS 1.3 handshake

ClientServerClientHello: key_share, SNI, ALPNServerHello: key_share--- from here on, everything is encrypted ---EncryptedExtensionsCertificate (chain)CertificateVerify (signature)Finishedclient validates the chain and the nameFinished + HTTP requestHTTP response
The full TLS 1.3 handshake completes in a single round trip.
  1. ClientHello: the supported versions and cipher suites (the combinations of algorithms it can use), a key share (a temporary ECDHE public key), SNI (the site’s name, so the server knows which certificate to present) and ALPN (h2, http/1.1).
  2. ServerHello: the chosen cipher and the server’s own key share. From this moment, both sides have the handshake keys, and the rest of the handshake is encrypted.
  3. The server continues with EncryptedExtensions, Certificate (the chain), CertificateVerify (a signature over the whole handshake with its private key, proving it owns the certificate) and Finished.
  4. The client checks the certificate chain, sends its own Finished, and can immediately start sending application data.

The result is one RTT (TLS 1.2 needed two). With session resumption and 0-RTT (early data), the client sends data in its very first message, with the risk that an attacker can replay it. Use 0-RTT only for idempotent requests.

TLS 1.3 removed all the insecure old options: RSA key exchange (no forward secrecy), CBC, RC4, SHA-1, compression and renegotiation. TLS 1.0 and 1.1 are officially deprecated (RFC 8996).

Certificates and the chain of trust

An X.509 certificate (profiled in RFC 5280) is a signed statement: “this public key belongs to these names”, plus the signature of a CA. The important fields:

  • Subject Alternative Name (SAN): the names the certificate is valid for (example.com, *.example.com). Browsers look only at the SAN, not at the Common Name.
  • Issuer, Validity (Not Before and Not After), Public Key, Key Usage, and extensions.
  • A wildcard covers exactly one level: *.example.com covers neither a.b.example.com nor example.com itself.

The chain: the site’s certificate (the leaf) is signed by an intermediate CA, and the intermediate is signed by a root CA. Root CAs live in the trust store of the operating system or the browser (Mozilla, Apple, Microsoft and Google each run their own root program). The server must send the leaf and the intermediates, but not the root.

What does validation check?

  1. The signature on each certificate is valid under its issuer’s public key, and the chain ends at a trusted root.
  2. The certificate hasn’t expired. This is why the system clock matters: a wrong clock means TLS errors (see NTP).
  3. The hostname appears in the SAN.
  4. The certificate hasn’t been revoked, which is checked through CRLs or, in the past, OCSP. Let’s Encrypt dropped OCSP in 2025 and now offers only CRLs, and browsers use their own compressed lists (such as CRLite in Firefox). The industry trend is short-lived certificates instead of relying on revocation.

Issuing certificates: domain validation and ACME

Before it issues a certificate, a CA must make sure the requester controls the domain (DV, domain validation). The OV and EV levels also check the organization’s details, but browsers no longer show any difference between them today.

ACME (RFC 8555) is the automation protocol of Let’s Encrypt, and now of many other CAs:

  • HTTP-01: the CA fetches a file from http://domain/.well-known/acme-challenge/TOKEN on port 80.
  • DNS-01: a TXT record at _acme-challenge.domain. It is the only way to get a wildcard certificate, and a good fit for servers that aren’t reachable from the Internet.
  • TLS-ALPN-01: on port 443, using a temporary certificate.
  • Tools: certbot, acme.sh, Caddy (automatic), and cert-manager in Kubernetes.

Certificate Transparency

Every public certificate must be recorded in several public, append-only CT logs, and browsers reject a certificate that has no proof of being logged (an SCT). As a result, domain owners can see every certificate ever issued for their domain (for example on crt.sh), and unauthorized issuance can be detected. The flip side is that every subdomain that has a public certificate is public too.

SNI, ECH and ALPN

  • SNI is sent in plaintext inside the ClientHello. So even with HTTPS, anyone on the network path can see which site you are visiting, and censorship and filtering systems rely on exactly that.
  • ECH (Encrypted Client Hello, RFC 9849) encrypts the real ClientHello with a key that the server publishes in a DNS record of type HTTPS. An observer sees only the provider’s public name.
  • ALPN picks the upper-layer protocol (h2, http/1.1) inside the same handshake, at no extra RTT.

mTLS: mutual authentication

In ordinary TLS, only the server proves its identity. In mutual TLS, the server also asks the client for a certificate (CertificateRequest). It is used for service-to-service communication, service meshes (Istio and Linkerd set up mTLS automatically), zero trust, B2B APIs and IoT devices, usually with a private internal CA.

TLS for programmers

With Python’s standard library, a correctly verifying TLS client takes a few lines:

import socket, ssl
ctx = ssl.create_default_context()                 # system trust store, hostname check ON
with socket.create_connection(("example.com", 443), timeout=5) as raw:
    with ctx.wrap_socket(raw, server_hostname="example.com") as tls:   # sets SNI + hostname check
        print(tls.version(), tls.cipher(), tls.selected_alpn_protocol())
        cert = tls.getpeercert()
        print(cert["subject"], cert["notAfter"], cert["subjectAltName"][:3])