Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 80 ยท Cryptography Basics

TLS, Certificates and PKI

Learn how the TLS handshake works, how certificate chains and certificate authorities establish trust, and how expiry, pinning and HSTS keep HTTPS safe.

When your browser shows a padlock, a lot of cryptography has just happened in a few milliseconds. TLS (Transport Layer Security) is the protocol that protects HTTPS, email transport, database connections and countless APIs. It gives you three guarantees: confidentiality (eavesdroppers see only ciphertext), integrity (changes are detected) and authentication (you are talking to the real server, not an impostor).

TLS combines everything from the previous lessons: a key exchange (Public-Key Cryptography), digital signatures to prove identity, symmetric authenticated encryption for the data (Symmetric Encryption and Modes) and hashes and MACs throughout. In this lesson you will follow the handshake, see how certificates and certificate authorities answer the question "whose public key is this?", and read and validate certificates with small Python tools. No network is needed, because the demos use fake certificates for example.test.

The TLS 1.3 handshake

Before TLS 1.3 the handshake took two round trips and offered many weak options. TLS 1.3 (2018) removed the old ciphers, key-exchange methods without forward secrecy and compression, and needs only one round trip. In simplified form:

sequenceDiagram
    participant C as Client (browser)
    participant S as Server
    C->>S: ClientHello (versions, ciphers, key share, server name)
    S->>C: ServerHello (chosen cipher, key share)
    Note over C,S: Both derive session keys. Everything below is encrypted.
    S->>C: Certificate chain + signature over the handshake
    S->>C: Finished (MAC of the whole handshake)
    C->>S: Finished
    C->>S: Application data (HTTP request)

The key shares are an ephemeral Diffie-Hellman exchange. The certificate and signature prove that the key share really came from the owner of the domain.

The important ideas:

  • Ephemeral key exchange. Client and server use fresh Diffie-Hellman keys (usually X25519) for each connection and throw them away. If the server's long-term key leaks next year, old recordings stay unreadable. This is forward secrecy.
  • Certificate and signature. The server sends a certificate that binds its name to a public key, and signs the handshake with the matching private key. This defeats the man-in-the-middle attack on plain Diffie-Hellman.
  • Finished messages. Each side sends a MAC over the entire handshake transcript. If an attacker altered anything, such as removing the strong ciphers from the client's offer (a downgrade attack), the MACs will not match and the connection is dropped.
  • SNI (server name). The client says which hostname it wants so one IP address can serve many sites. Encrypted Client Hello hides this name where supported.

The next demo makes the "Finished" idea concrete. It reuses a toy Diffie-Hellman exchange and derives a key from the shared secret and the transcript. An attacker rewrites the client's list of supported versions in transit, and the handshake fails:

Python Finished messages catch a downgrade attack

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

What a certificate contains

A certificate (X.509) is a signed document that says "this public key belongs to this name". The key fields are:

  • Subject and Subject Alternative Names (SAN): the domain names the certificate is valid for. Browsers look at the SAN list, not the old Common Name.
  • Issuer: who signed it.
  • Validity: Not Before and Not After dates.
  • Public key and its algorithm (RSA 2048, ECDSA P-256).
  • Extensions: whether the certificate belongs to a CA (CA:TRUE) or an ordinary site (CA:FALSE), what it may be used for, and pointers to revocation information.
  • The signature of the issuer over all of the above.

Tools such as openssl x509 -text -noout -in cert.pem print certificates in a readable form. This snippet is read-only because the runner has no openssl and no network:

Terminal
# Inspect a certificate file
openssl x509 -in cert.pem -noout -subject -issuer -dates

# Fetch and inspect what a live server presents
openssl s_client -connect example.org:443 -servername example.org </dev/null \
  | openssl x509 -noout -subject -issuer -dates -ext subjectAltName

The next demo parses the text output of a (fake) certificate. It extracts the important fields, computes the days left until expiry and checks hostnames, including the wildcard rule: *.cdn.example.test matches exactly one label, so img.cdn.example.test is covered but cdn.example.test and a.b.cdn.example.test are not.

Python Reading a certificate

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Chains of trust and certificate authorities

Your browser cannot know every website's key in advance. Instead it trusts a small set of certificate authorities (CAs) that verify who controls a domain and sign certificates. The trust is organized as a chain:

flowchart TB
    R["Root CA<br/>(self-signed, in the trust store)"] -->|"signs"| I["Intermediate CA"]
    I -->|"signs"| L["Leaf certificate<br/>www.example.test"]
    T["Operating system / browser trust store"] -.->|"contains"| R

The server sends the leaf and intermediate. The client already holds the root and checks every signature up the chain.

Root CA private keys are kept offline in hardened facilities. Day-to-day issuing is done by intermediate CAs, so a compromised intermediate can be revoked without discarding the root. Validation checks that:

  1. Each certificate is signed by the next one up, and the signature verifies.
  2. Every certificate is within its validity period.
  3. Intermediates have CA:TRUE and the chain ends at a root in the local trust store.
  4. The leaf's SAN covers the hostname you asked for.
  5. The certificate has not been revoked, and its use matches its purpose.

Public CAs prove domain control (a file served over HTTP or a DNS record) before issuing. Free CAs using the ACME protocol, such as Let's Encrypt, made certificates routine and short-lived. Certificate Transparency logs add public accountability: CAs must log certificates, so domain owners can monitor for certificates they did not request.

The demo below simulates this process. Real certificates carry asymmetric signatures checked with the issuer's public key. As a stand-in, our "signature" is an HMAC and the verifier looks the issuer's key up in a table, which is enough to show the logic of chain building:

Python Validating a certificate chain (stand-in signatures)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The "server forgot parent" case is one of the most common real-world mistakes. Some browsers can fetch missing intermediates themselves, but command-line tools and API clients usually fail, so always serve the full chain (leaf plus intermediates, not the root).

SSL/TLS protocol layers, versions and certificate types

The handshake above is one part of a layered protocol. Knowing the layers helps you read packet captures, interpret error messages and answer interview questions.

Two layers inside TLS

TLS has a lower Record protocol and several higher-level protocols that ride on it:

  • Record protocol: the bottom layer. It splits application data into records, and (once keys exist) encrypts and authenticates each one. Every record starts with a five-byte header: a content type, a legacy version and a length.
  • Handshake protocol: negotiates the version and cipher suite, authenticates the server (and optionally the client), and establishes the keys. Its messages include ClientHello, ServerHello, Certificate and Finished.
  • Change Cipher Spec protocol: a one-byte message that in TLS 1.2 and earlier tells the peer "everything from now on is encrypted with the keys we just agreed". TLS 1.3 removed its real function, but a dummy message may still be sent so that middleboxes are not confused.
  • Alert protocol: two-byte messages that report problems or closure, with a level (warning or fatal) and a description, such as close_notify, handshake_failure, bad_certificate, certificate_expired or unknown_ca. A fatal alert ends the connection.
  • Application Data protocol: the encrypted HTTP, SMTP or other traffic itself.
Content type value Meaning
20 (0x14) Change Cipher Spec
21 (0x15) Alert
22 (0x16) Handshake
23 (0x17) Application Data

In a packet capture, a TLS connection therefore starts with a record of type 22 (the ClientHello), and later records are mostly type 23. The demo builds and parses such headers.

Python Read TLS record headers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The Alert records explain many failures you see as browser errors: an unknown_ca alert means the peer does not trust the issuer, and certificate_expired means exactly what it says. Handshake and application records are encrypted (in TLS 1.3 most handshake messages after the ServerHello are too), so a capture shows only lengths and timing, plus the unencrypted ClientHello with its server name.

From SSL to TLS 1.3

The names are used interchangeably, but the protocols differ in age and safety:

Version Year Status
SSL 2.0 1995 Broken; prohibited
SSL 3.0 1996 Broken (POODLE attack); prohibited
TLS 1.0 1999 Deprecated and disabled by browsers
TLS 1.1 2006 Deprecated and disabled by browsers
TLS 1.2 2008 Still widely used; safe with modern cipher suites (AEAD, ECDHE)
TLS 1.3 2018 Current: fewer round trips, forward secrecy by default, old features removed

A good server configuration allows TLS 1.2 and 1.3 only, uses AEAD cipher suites and ephemeral key exchange, and disables compression and renegotiation features that enabled attacks in the past. "SSL certificate" is just a common name for a TLS certificate, since certificates are the same for both protocols.

Certificate validation levels and types

Certificates differ by how much the CA checked before issuing, and by what names they cover.

Type What the CA verifies Typical use
Domain Validated (DV) Only that the applicant controls the domain (a DNS record, a file on the site, or an email) Most websites; issued automatically in minutes (for example by ACME CAs)
Organization Validated (OV) Domain control plus the legal existence of the organization Business sites and portals that want the organization named in the certificate details
Extended Validation (EV) A stricter, standardized identity check of the legal entity Banks and large brands; browsers no longer show special green bars, so the user-visible benefit is small

All three give the same encryption strength; the difference is the identity assurance recorded in the certificate, which few users see. Note that a DV certificate on a phishing site is valid and shows a padlock, so the padlock means "encrypted to that domain", not "trustworthy".

By coverage:

  • Single-domain, multi-domain (SAN/UCC) certificates list several names, and wildcard certificates (*.example.com) cover all one-level subdomains. A wildcard key is more dangerous if stolen, because it protects many hosts, so keep its use narrow.
  • Client certificates authenticate users or devices to servers (mutual TLS).
  • Code-signing certificates sign software and drivers; S/MIME certificates sign and encrypt email (PGP and S/MIME: Encrypting Email).
  • Private (internal) CA certificates serve internal systems and are trusted only by devices configured to trust that CA.
Python Classify a certificate's validation level from its subject

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For most sites, a free automated DV certificate is the right choice; pay for OV or EV only when a customer or regulation requires the extra identity evidence. The more important controls are the ones listed under Expiry, revocation and automation and Pinning and HSTS below.

Expiry, revocation and automation

An expired certificate is not a minor warning: browsers block the site, and API clients fail. Outages from forgotten renewals have hit very large companies. Defences:

  • Automate renewal with ACME clients and monitor expiry dates from outside (alert at 30, 14 and 7 days).
  • Shorter lifetimes limit the damage from a stolen key; the maximum lifetime allowed for public certificates keeps being reduced, which makes automation mandatory.
  • Revocation is the way to cancel a certificate before it expires, but it is unreliable in practice. OCSP stapling (the server attaches a fresh signed "still good" response) and short lifetimes work better than relying on clients to download revocation lists.

Pinning and HSTS

Certificate pinning means a client accepts only a specific certificate or public key for a host, even if a trusted CA signs another one. It protects mobile apps and other clients you control against a rogue or compromised CA. It is risky: if the pinned key changes and you did not plan for it, your app locks itself out. Pin the public key of an intermediate or a backup key, and have a recovery plan. Browser-based pinning (HPKP) was removed; Certificate Transparency and CAA DNS records have replaced it for websites.

HSTS (HTTP Strict Transport Security) is an HTTP response header that tells browsers to use HTTPS for this site and never to fall back to HTTP. It defeats SSL-stripping, where an attacker quietly downgrades a first http:// request.

Python Auditing an HSTS header

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Roll HSTS out gradually: start with a short max-age, confirm that every subdomain really works over HTTPS, then raise it. Remember that a long max-age or the preload list is hard to undo. Other security headers are covered in HTTP Security Headers.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does forward secrecy protect against?

    With ephemeral Diffie-Hellman keys that are discarded after each session, a leak of the server's long-term key cannot unlock old recorded connections.
  2. 2. A server sends only its leaf certificate and some command-line clients fail with a verification error. What is the likely cause?

    Clients need a complete path from the leaf to a trusted root. Servers should send the leaf plus intermediates. Browsers sometimes hide the mistake by fetching missing intermediates.
  3. 3. Which certificate field do modern browsers use to decide whether a hostname matches?

    Hostnames are matched against the SAN entries. A wildcard such as *.example.test covers exactly one label, not the bare domain and not deeper subdomains.
  4. 4. What is the main purpose of the HSTS header?

    HSTS makes the browser upgrade every request to HTTPS for the stated time, which defeats SSL-stripping downgrades.
  5. 5. Why does the Finished message in the handshake matter?

    Each side sends a MAC over everything exchanged so far. If an attacker changed any part of the handshake, the MACs differ and the connection fails.

Summary

  • TLS 1.3 completes in one round trip: an ephemeral key exchange, a certificate with a signature to prove identity, and Finished MACs that cover the entire handshake.
  • A certificate binds names (SAN) to a public key and is signed by a CA. Clients validate the chain to a root in their trust store, check dates and hostname, and respect extensions like CA:FALSE.
  • Serve the full chain (leaf plus intermediates). Missing intermediates are a classic cause of failures.
  • Automate renewal and monitor expiry; short lifetimes and OCSP stapling beat depending on revocation lists.
  • Pin only in clients you control, with backup keys. Use HSTS (rolled out gradually) to stop HTTP downgrades.

Next: Randomness, Key Management and Secrets, the last lesson of this chapter, about where keys come from and how to keep them safe.