Cryptography & Security

TLS & HTTPS

How the web secures data in transit -- from the TLS handshake and certificate chains to cipher suites, perfect forward secrecy, and modern protocols like TLS 1.3.

01 / TLS Overview

What TLS Provides

Transport Layer Security (TLS) is a cryptographic protocol that sits between the transport layer (TCP) and the application layer (HTTP). HTTPS is simply HTTP running over a TLS-secured connection. TLS delivers three fundamental guarantees:

Confidentiality

Data is encrypted so that only the intended recipient can read it. Even if an attacker intercepts packets on the wire, the payload is indecipherable without the session keys.

Integrity

Every TLS record includes a Message Authentication Code (MAC). Any tampering -- inserting, deleting, or modifying bytes -- is detected and the record is rejected.

Authentication

The server proves its identity with a digital certificate signed by a trusted Certificate Authority (CA). Optionally, the client can authenticate too (mTLS).

TLS vs SSL
SSL (Secure Sockets Layer) was the predecessor to TLS. SSL 3.0 was deprecated in 2015 due to the POODLE attack. When people say "SSL certificate" today, they almost always mean a certificate used with TLS.
02 / TLS 1.2 Handshake

The TLS 1.2 Handshake

TLS 1.2 requires two round-trips (2-RTT) before encrypted application data can flow. Here is the full message exchange:

TLS 1.2 Full Handshake (2-RTT)
Client
Server
ClientHello
 
 
ServerHello + Cert
 
ServerKeyExchange
 
ServerHelloDone
ClientKeyExchange
 
ChangeCipherSpec
 
Finished
 
 
ChangeCipherSpec
 
Finished

Step by Step

ClientHello: The client sends supported TLS versions, a random nonce, a list of cipher suites, and extensions (including SNI -- the hostname it wants to reach).

ServerHello: The server selects a TLS version and cipher suite, sends its own random nonce, and attaches its certificate chain.

ServerKeyExchange: When using ECDHE, the server sends its ephemeral public key, signed with its private key for authentication.

ClientKeyExchange: The client sends its ephemeral public key. Both sides now independently compute the same pre-master secret via the Diffie-Hellman algorithm.

Finished: Both sides derive session keys from the pre-master secret and the two random nonces, then send a Finished message encrypted with the new keys to verify the handshake was not tampered with.

RSA Key Exchange (Legacy)
In RSA key exchange (no "DHE" in the cipher suite), the client generates the pre-master secret and encrypts it with the server's public key. This works but does NOT provide forward secrecy -- if the server's private key is later compromised, all past sessions can be decrypted.
03 / TLS 1.3

TLS 1.3 Improvements

TLS 1.3 (RFC 8446, finalized 2018) is a major overhaul that reduces latency, removes insecure options, and simplifies the protocol.

TLS 1.3 Full Handshake (1-RTT)
Client
Server
ClientHello + KeyShare
 
 
ServerHello + KeyShare
 
{Cert, CertVerify, Finished}
{Finished}
 

{Braces} indicate encrypted messages. Application data can flow immediately after the client's Finished.

FeatureTLS 1.2TLS 1.3
Handshake round-trips2-RTT1-RTT (0-RTT with resumption)
RSA key exchangeSupportedRemoved entirely
Forward secrecyOptional (only with DHE/ECDHE)Mandatory (ECDHE only)
Cipher suites~37 options5 options
Server Finished encrypted?NoYes
0-RTT resumptionNoYes (with replay risk caveats)

0-RTT Resumption

If a client has connected to a server before, TLS 1.3 allows it to send encrypted application data in the very first flight (0-RTT) using a pre-shared key (PSK) from the previous session. This eliminates the handshake latency entirely for repeat connections.

0-RTT Replay Risk
0-RTT data is not protected against replay attacks. An attacker can capture and resend the first flight. Servers must ensure 0-RTT is only used for idempotent requests (e.g., GET) and implement anti-replay measures like single-use session tickets or time-window checks.
04 / Certificate Chain

Certificate Chain & X.509

Trust on the web is bootstrapped through a hierarchy of Certificate Authorities (CAs). A server does not just send its own certificate -- it sends a chain that lets the client trace trust back to a pre-installed root.

Certificate Chain of Trust
Root CA
signs
Intermediate CA
signs
Server Certificate

The root CA's certificate is self-signed and pre-installed in the OS/browser trust store. The server sends its leaf cert + intermediate(s); the client chains up to the root.

X.509 Certificate Fields

Certificates follow the X.509 v3 standard. Key fields include:

FieldPurpose
SubjectEntity the cert belongs to (CN, O, etc.)
IssuerCA that signed this certificate
Subject Alternative NameHostnames/IPs the cert is valid for (replaces CN matching)
Validity (Not Before / Not After)Time window during which the cert is valid
Public KeyThe server's public key (RSA or ECDSA)
SignatureDigital signature from the issuer, proving authenticity
ExtensionsKey Usage, Basic Constraints (is it a CA?), CRL/OCSP endpoints

Certificate Signing Request (CSR)

To get a certificate, the server operator generates a key pair, creates a CSR containing the public key and identity info, and sends it to a CA. The CA verifies domain ownership (for DV certs) or organization identity (for OV/EV certs), then signs the certificate with its private key.

Trust Stores
Operating systems and browsers ship with a curated list of ~100-150 root CA certificates. On macOS this is the System Keychain; on Linux it is typically /etc/ssl/certs; browsers like Firefox maintain their own store (Mozilla NSS). Any certificate chain that terminates at one of these roots is considered trusted.
05 / Validation & Revocation

Certificate Validation

When a TLS client receives a certificate, it performs several checks before trusting it:

Hostname Match

The requested hostname must match the cert's Subject Alternative Name (SAN) entries. Wildcard certs (*.example.com) match one subdomain level.

Expiry Check

The current time must fall within the cert's Not Before and Not After window. Expired certs are rejected.

Chain of Trust

Each cert in the chain must be validly signed by the next, terminating at a trusted root in the local trust store.

Signature Verification

The client verifies each certificate's digital signature using the issuer's public key to ensure the cert has not been forged.

Revocation Checking

Sometimes certificates need to be revoked before expiry (e.g., private key compromise). There are several mechanisms for this:

MechanismHow It WorksDownsides
CRLCA publishes a signed list of revoked cert serial numbers. Client downloads and checks it.Lists grow large; latency of downloading; infrequent updates.
OCSPClient queries the CA's OCSP responder in real-time with the cert's serial number.Privacy concern (CA sees which sites you visit); adds latency; single point of failure.
OCSP StaplingServer periodically fetches its own OCSP response and "staples" it to the TLS handshake.Best approach -- no privacy leak, no extra round-trip. But requires server support.

Certificate Transparency (CT)

CT logs are append-only public ledgers where CAs must log every certificate they issue. This allows domain owners and the community to detect mis-issued certificates. Browsers like Chrome require certificates to include Signed Certificate Timestamps (SCTs) proving they were logged.

06 / Cipher Suites & Advanced Topics

Cipher Suites, PFS, SNI, and mTLS

Cipher Suite Anatomy

A cipher suite specifies four algorithms. In TLS 1.2 notation, a suite like TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 breaks down as:

Cipher Suite Components
ECDHE
Key Exchange
+
RSA / ECDSA
Authentication
+
AES-GCM / ChaCha20
Bulk Encryption
+
SHA-256 / SHA-384
MAC / PRF

In TLS 1.3, key exchange is negotiated separately (always ECDHE), so suites are simpler: TLS_AES_128_GCM_SHA256 or TLS_CHACHA20_POLY1305_SHA256.

Perfect Forward Secrecy (PFS)

PFS means that session keys are not derived from the server's long-term private key. Instead, both sides generate ephemeral Diffie-Hellman keys for each session. Even if the server's private key is stolen later, past recorded traffic cannot be decrypted because the ephemeral keys were discarded after the session ended.

PFS in Practice
Any cipher suite containing "DHE" or "ECDHE" provides forward secrecy. TLS 1.3 mandates ECDHE, so all TLS 1.3 connections have PFS by default.

Server Name Indication (SNI)

SNI is a TLS extension where the client includes the requested hostname in the ClientHello (in plaintext). This allows a single IP address to host multiple HTTPS domains, each with its own certificate. Without SNI, the server would not know which certificate to present before the connection is encrypted.

SNI Privacy
Because SNI is sent in cleartext during the handshake, a network observer can see which hostname the client is connecting to. Encrypted Client Hello (ECH), formerly called ESNI, is a TLS 1.3 extension that encrypts the SNI field to address this privacy leak.

Mutual TLS (mTLS)

In standard TLS, only the server authenticates. In mTLS, the server also requests a certificate from the client during the handshake (CertificateRequest message). The client sends its certificate and proves possession of the private key. This is commonly used in service-to-service communication, zero-trust networks, and API authentication.

Let's Encrypt & ACME

Let's Encrypt is a free, automated CA that has issued billions of certificates. It uses the ACME (Automatic Certificate Management Environment) protocol, where the client proves domain control via an HTTP-01 or DNS-01 challenge, and the CA automatically issues a 90-day DV certificate. Tools like certbot handle the entire lifecycle -- issuance, installation, and renewal.

# Obtain a certificate with certbot
sudo certbot certonly --webroot -w /var/www/html -d example.com

# Auto-renewal (typically via cron or systemd timer)
sudo certbot renew --quiet

Test Yourself

Score: 0 / 10
Question 01
Which three properties does TLS provide?
TLS provides confidentiality (encryption), integrity (MAC on every record), and authentication (server proves identity via certificate). Non-repudiation is a property of digital signatures, not TLS itself.
Question 02
How many round-trips does a full TLS 1.3 handshake require?
A full TLS 1.3 handshake completes in 1-RTT. The client sends ClientHello + KeyShare, and the server responds with ServerHello + KeyShare + encrypted certificate and Finished. 0-RTT is available only for session resumption.
Question 03
Why was RSA key exchange removed in TLS 1.3?
RSA key exchange encrypts the pre-master secret with the server's long-term RSA key. If that key is later compromised, all past recorded sessions can be decrypted. TLS 1.3 mandates ECDHE to ensure forward secrecy.
Question 04
In a certificate chain, which certificate is self-signed?
Root CA certificates are self-signed -- the issuer and subject are the same entity. They are trusted because they are pre-installed in the operating system or browser trust store, not because of a signature chain.
Question 05
What is the main advantage of OCSP stapling over plain OCSP?
With OCSP stapling, the server fetches its own OCSP response and includes it in the TLS handshake. The client does not need to contact the CA's OCSP responder, so the CA cannot track which sites a user visits, and there is no extra round-trip latency.
Question 06
What does Perfect Forward Secrecy (PFS) guarantee?
PFS ensures that session keys are ephemeral. Each session uses a new Diffie-Hellman key pair that is discarded after use. Even if the server's long-term private key is stolen, previously recorded encrypted traffic cannot be decrypted.
Question 07
Why is SNI sent in plaintext during the TLS handshake?
The server must read the hostname before it can select which certificate to present. Since the ClientHello is sent before any encryption keys are established, SNI is necessarily in plaintext. Encrypted Client Hello (ECH) is a newer extension that addresses this by encrypting the SNI using a key published in DNS.
Question 08
In the cipher suite TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, what role does RSA play?
In this cipher suite, ECDHE handles key exchange and AES-128-GCM handles bulk encryption. RSA is used for authentication -- the server signs the ephemeral ECDHE parameters with its RSA private key to prove it owns the certificate.
Question 09
What is the main risk of TLS 1.3's 0-RTT resumption?
0-RTT data is encrypted but not protected against replay. An attacker who captures the initial flight can resend it, causing the server to process the same request again. This is why 0-RTT should only be used for idempotent operations like GET requests.
Question 10
What does mTLS add on top of standard TLS?
In mutual TLS (mTLS), both sides authenticate. The server sends a CertificateRequest message, and the client responds with its own certificate and a proof of possession of the corresponding private key. This is widely used for service-to-service authentication in microservice architectures.