All Posts

How SSL/TLS Works: The Handshake Explained

When you see the green lock icon, your data is safe. But how? We break down the SSL Handshake, Public/Private Keys, and Certificates.

Abstract AlgorithmsAbstract Algorithms
ยทยท6 min read
Share
Share on X / Twitter
Share on LinkedIn
Copy link

TLDR: SSL (now TLS) secures data between your browser and a server. It uses Asymmetric Encryption (Public/Private keys) once โ€” to safely exchange a fast Symmetric Session Key. Everything after the handshake is encrypted with the session key.


๐Ÿ“– The Locked Box Trick: Why Two Encryption Systems?

Alice wants to send Bob a secret letter, but Eve is intercepting all mail.

  • Symmetric encryption: Same key locks and unlocks. Fast โ€” but how do Alice and Bob share the key without Eve intercepting it?
  • Asymmetric encryption: Bob publishes an Open Padlock (public key) and keeps the only Key (private key). Alice locks her letter with Bob's padlock. Eve sees the locked box but can't open it. Only Bob can.
TypeSpeedKey distribution problem?
Symmetric (AES)Very fastYes โ€” how to share the key safely?
Asymmetric (RSA/ECDH)SlowNo โ€” public key can be shared openly

TLS Strategy: Use asymmetric encryption once (during the handshake) to securely exchange a symmetric session key. Then use the fast session key for everything else.


๐Ÿ”ข The TLS 1.3 Handshake: Step by Step

This happens in milliseconds every time you visit https://google.com.

sequenceDiagram
    participant B as Browser (Client)
    participant S as Server (google.com)

    B->>S: ClientHello (TLS version, cipher suites, random nonce)
    S->>B: ServerHello (chosen cipher, random nonce)
    S->>B: Certificate (server's public key, signed by CA)
    S->>B: Key Share (ECDH public value)
    Note over B: Verify certificate against trusted CAs
    B->>S: Key Share (browser's ECDH public value)
    Note over B,S: Both derive the same Session Key independently
    B->>S: Finished (encrypted with session key)
    S->>B: Finished (encrypted with session key)
    Note over B,S: Handshake complete โ€” all further data encrypted

TLS 1.3 improvement over TLS 1.2: The handshake completes in 1 RTT (round trip) instead of 2. Resumed sessions can use 0-RTT for even lower latency.

What the Certificate Tells the Browser

The server certificate contains:

  1. Subject: CN=google.com โ€” who owns this key.
  2. Public Key: The server's asymmetric key.
  3. Issuer: CN=DigiCert Global CA โ€” who vouches for it.
  4. Validity Period: Not before / not after dates.
  5. Signature: The CA's digital signature over all of the above.

โš™๏ธ Certificates and the Chain of Trust

Your browser doesn't know every website. It knows a small set of Root CAs (DigiCert, Let's Encrypt, GlobalSign) pre-installed in your OS/browser trust store.

flowchart TD
    Root["Root CA\n(Self-Signed โ€” in your OS trust store)"]
    Intermediate["Intermediate CA\n(Signed by Root)"]
    Leaf["Leaf Certificate\ngoogle.com\n(Signed by Intermediate)"]

    Root --> Intermediate --> Leaf

Validation chain:
Browser checks Leaf โ†’ Intermediate โ†’ Root โ†’ Root is in trust store โ†’ Valid.

If any link in the chain is missing or expired, the browser shows the red "Not Secure" warning.

Why not sign leaf certs with the Root directly?
Root private keys are stored offline in HSMs. Exposing them for every leaf cert would be a catastrophic security risk. Intermediate CAs handle day-to-day signing; if an Intermediate is compromised, only it needs to be revoked.


๐Ÿง  Session Keys, Forward Secrecy, and Performance

ECDH and Perfect Forward Secrecy (PFS)

In TLS 1.3, the session key is derived via ECDHE (Elliptic Curve Diffie-Hellman Ephemeral). "Ephemeral" means a fresh key pair is generated for every session and discarded after.

Perfect Forward Secrecy: Even if an attacker records encrypted traffic today and steals the server's private key tomorrow, they cannot decrypt past sessions โ€” because the ephemeral ECDH keys are long gone.

TLS 1.2 without PFS (RSA key exchange): steal the private key โ†’ decrypt all stored traffic. This is why PFS is critical.

TLS Performance Cost

StageCost
Full handshake~1โ€“2 ms additional latency
Session resumption (TLS 1.3)~0.5 ms (1-RTT)
0-RTT resumption~0 ms extra, but has replay attack risk
Symmetric encryption (AES-GCM)Negligible โ€” hardware-accelerated on modern CPUs

The performance cost is dominated by the handshake. Session caching and TLS 1.3's 1-RTT largely eliminate this concern in production.


โš–๏ธ Common Misconfigurations and mTLS

MisconfigurationRiskFix
Expired certificateUsers see security warning; browsers blockAutomate renewal (Let's Encrypt + certbot)
TLS 1.0/1.1 enabledVulnerable to BEAST, POODLE attacksDisable; enforce TLS 1.2 minimum, prefer 1.3
Weak cipher suites (RC4, 3DES)Decryptable with modern hardwareRestrict to ECDHE + AES-256-GCM
Missing HSTS headerAllows SSL-stripping downgrade attacksSet Strict-Transport-Security: max-age=31536000

mTLS (Mutual TLS): In standard TLS, only the server presents a certificate. In mTLS, both client and server present certificates โ€” each verifies the other. Used in zero-trust networks, service meshes (Istio), and B2B APIs where you need cryptographic proof of client identity.


๐Ÿ“Œ Summary

  • Two-phase encryption: Asymmetric (RSA/ECDH) for key exchange; Symmetric (AES-GCM) for data.
  • TLS 1.3 handshake: 1 RTT, ECDHE key exchange, mutual Finished verification.
  • Chain of Trust: Root CA โ†’ Intermediate CA โ†’ Leaf Certificate. Browser validates all links.
  • Perfect Forward Secrecy: Ephemeral ECDH keys mean past sessions stay safe even if the server key is later compromised.
  • mTLS: Both sides authenticate โ€” used in zero-trust and service mesh architectures.

๐Ÿ“ Practice Quiz

  1. Why does TLS use two types of encryption (asymmetric + symmetric) instead of just one?

    • A) Symmetric encryption is illegal in some countries.
    • B) Asymmetric is too slow for bulk data; it's used only to exchange the fast symmetric session key.
    • C) Symmetric encryption doesn't support certificates.
      Answer: B
  2. What is Perfect Forward Secrecy?

    • A) Certificates that never expire.
    • B) Using ephemeral key pairs per session so past sessions can't be decrypted even if the server's private key is stolen later.
    • C) Encrypting traffic twice with different keys.
      Answer: B
  3. Your API needs to verify that a calling microservice is exactly who it claims to be (not just any valid user). Which TLS feature do you use?

    • A) SNI (Server Name Indication).
    • B) mTLS (Mutual TLS) โ€” the client presents a certificate the server validates.
    • C) HSTS (HTTP Strict Transport Security).
      Answer: B

Abstract Algorithms

Written by

Abstract Algorithms

@abstractalgorithms