SSL and its protocols
SSL (now TLS) provides a protocol stack (handshake + record) to create authenticated, confidential channels over untrusted networks; engineers use it to protect web, API and other TCP traffic.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Two or three sentences: where an engineer uses this.
Secure Sockets Layer (SSL) and its successor Transport Layer Security (TLS) are the standard protocols used by engineers to protect data in transit — for HTTPS, APIs, mail, VPNs and many client-server systems. An engineer must know how the handshake establishes keys, what the record protocol does, what a cipher suite contains, and which protocol versions / primitives to choose to avoid known attacks.
Key ideas
Start from first principles and define terms before use.
TLS (Transport Layer Security): the standardized protocol (successor to SSL) that provides confidentiality, integrity and authentication for data sent over a network. It runs on top of a reliable transport (typically TCP).
SSL: older versions (SSL 2.0, 3.0) now deprecated. When you see "SSL" in old texts, map it to the TLS family.
Handshake: a sequence of messages between client and server whose goal is to (1) agree protocol version and cipher suite, (2) authenticate one or both parties (usually server certificate), (3) establish shared secrets (keys) used for encryption and MAC.
Record protocol: the layer that fragments, optionally compresses, applies message authentication (MAC), and encrypts the application data. After processing, each fragment is sent with a small TLS header.
Cipher suite: a named set of algorithms used by a TLS connection. Typical form (TLS 1.2 era):
TLS_<KEX>[_<AUTH>]_WITH_<CIPHER>[_<MODE>]_<MAC-or-PRF>(e.g.,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256). Components:- Key exchange (KEX): how shared secret is established (RSA, ECDHE, DHE, PSK).
- Authentication: algorithm to verify server (and optionally client) identity; usually X.509 certificate signed by a CA.
- Bulk cipher: symmetric encryption (AES-GCM, AES-CBC, ChaCha20-Poly1305).
- MAC or AEAD: message integrity; AEAD ciphers combine encryption and integrity.
Pre-master secret and master secret: in TLS 1.2 and earlier the handshake yields a shared pre-master secret (for RSA it’s sent encrypted, for (EC)DHE it's the DH shared value). The master secret is derived from the pre-master secret and the two random nonces from client and server; session keys are then derived from the master secret.
Exact derivation commonly asked (TLS 1.2 era): master_secret = PRF(pre_master_secret, "master secret", ClientRandom || ServerRandom)[0..47], assuming PRF produces enough bytes. State assumptions: which PRF (TLS 1.0/1.1 used MD5+SHA1, TLS 1.2 uses a PRF based on the negotiated hash, TLS 1.3 uses HKDF — see TLS 1.3 differences).
HMAC: the HMAC construction with a hash function provides the MAC used by many TLS cipher suites. Definition: HMAC(key, message) = H((key⊕opad) || H((key⊕ipad) || message)).
Record processing order (TLS 1.2 typical with CBC): fragment → compress (usually null) → compute MAC → encrypt (MAC-then-encrypt). TLS 1.3 uses AEAD; processing is simpler: AEAD_encrypt(traffic_key, nonce, associated_data, plaintext).
Handshake message sequence (full handshake, ephemeral DH): describe what to draw: draw two vertical lifelines labelled Client (left) and Server (right). Label arrows in order:
- Client → Server: ClientHello (contains supported versions, cipher suites, ClientRandom)
- Server → Client: ServerHello (chosen version, cipher suite, ServerRandom)
- Server → Client: Certificate (server's X.509 cert)
- Server → Client: ServerKeyExchange (if needed, e.g., ECDHE parameters)
- Server → Client: ServerHelloDone
- Client → Server: ClientKeyExchange (client DH public value or RSA-encrypted pre-master)
- Client → Server: [ChangeCipherSpec]
- Client → Server: Finished (encrypted with new keys)
- Server → Client: [ChangeCipherSpec]
- Server → Client: Finished
Label where keys change: after a side sends its ChangeCipherSpec, it starts using the negotiated write keys for its Finished; upon receiving a ChangeCipherSpec, the peer switches read keys.
Session resumption: abbreviated handshake reuses a session (session id or session ticket) so both sides derive keys quickly.
TLS 1.3 differences (high level): handshake simplified, removed RSA key exchange, uses (EC)DHE and/or PSK (forward secrecy when using (EC)DHE or PSK with DHE), replaces older PRF with an HKDF-based key schedule, encrypts more handshake messages, adds optional 0-RTT early data (replayable), and uses AEAD-only cipher suites.
Vulnerabilities and mitigations (summary): SSL 2.0/3.0 disabled due to many issues (e.g., POODLE on SSL 3.0), CBC+MAC-then-encrypt vulnerabilities (BEAST, padding oracle attacks), misconfigurations and library bugs (Heartbleed is an OpenSSL bug). Mitigation: use TLS 1.2/1.3, use AEAD ciphers (AES-GCM, ChaCha20-Poly1305), prefer ECDHE for forward secrecy, validate certificates, keep libraries updated. Also disable TLS-level compression (to prevent CRIME) and be aware of HTTP-layer BREACH mitigations.
Formulas
HMAC(K, m) = H((K' xor opad) || H((K' xor ipad) || m)) — H is hash (e.g., SHA-256), K' is key padded to block size, opad/ipad are byte constants; output length = hash output length (bits: e.g., 256 bits for SHA-256). Applies to message authentication used in HMAC-based cipher suites.
PRF(secret, label, seed) -> output_bytes — PRF is the TLS pseudo-random function: in TLS 1.0/1.1 it is MD5+SHA1 XOR construction; in TLS 1.2 it is based on the negotiated hash (e.g., HMAC-SHA256). Used to expand secrets into key material.
master_secret = PRF(pre_master_secret, "master secret", ClientRandom || ServerRandom)[0..47] — outputs 48 bytes (384 bits) as defined in TLS 1.2 spec; ClientRandom and ServerRandom are 32-byte nonces.
key_block = PRF(master_secret, "key expansion", ServerRandom || ClientRandom) — produces enough bytes for client_write_MAC_key, server_write_MAC_key, client_write_key, server_write_key, and (if needed) client_write_IV, server_write_IV (in this exact order for TLS 1.2 and earlier); used to form symmetric keys.
record_header: ContentType(1 byte) || Version(2 bytes) || Length(2 bytes) — followed by fragment (<= 2^14 bytes normally).
AEAD_encrypt(key, nonce, aad, plaintext) -> ciphertext + tag — used by AES-GCM or ChaCha20-Poly1305 in TLS 1.2/1.3.
Note: TLS 1.3 key schedule uses HKDF and multiple stages (early secret, handshake secret, master secret) — see TLS 1.3 differences.
Worked examples
- Small Diffie–Hellman numeric example (exam-style with small primes). Show all steps and final secret.
Given: prime p = 23, base g = 5. Client choose a = 6, Server choose b = 15. Steps:
- Client computes A = g^a mod p = 5^6 mod 23. Compute: 5^2=25≡2, 5^4≡2^2=4, 5^6≡4·2=8. So A = 8.
- Server computes B = g^b mod p = 5^15 mod 23. From powers: 5^15 ≡ 19. So B = 19.
- Client computes shared = B^a mod p = 19^6 mod 23. Compute: 19^2≡16, 19^4≡16^2≡3, 19^6≡3·16≡48≡2. Shared secret = 2.
- Server computes shared = A^b mod p = 8^15 mod 23 (same result): shared = 2.
Final answer (bold): The DH shared secret is 2. This value is the basis for pre-master secret (in tutorial examples). In real TLS the secret is converted to a byte string and fed to the PRF/HKDF to derive master/session keys.
- TLS 1.2 full handshake trace (ephemeral DHE). Write the message sequence and say what is contained and when keys are derived.
Steps:
- ClientHello (client_random, supported ciphers)
- ServerHello (server_random, chosen cipher)
- Certificate (server's X.509 cert; client verifies chain)
- ServerKeyExchange (contains DHE params: p, g, server_pub)
- ServerHelloDone
- ClientKeyExchange (client_pub)
- Both compute pre-master via DH (shared = server_pub^client_priv mod p). From the pre-master, both compute master_secret = PRF(pre_master, "master secret", client_random||server_random).
- Client sends ChangeCipherSpec then Finished (encrypted with keys derived from master_secret)
- Server sends ChangeCipherSpec then Finished
The keys become active after the ChangeCipherSpec and the Finished messages confirm successful key derivation.
Common mistakes
- Saying TLS always uses RSA key exchange — TLS can use (EC)DHE which gives forward secrecy; RSA key exchange offers no forward secrecy.
- Confusing MAC-then-encrypt vs encrypt-then-MAC — TLS 1.0/1.1/1.2 with CBC historically used MAC-then-encrypt; many modern setups use AEAD (GCM) or explicit encrypt-then-MAC extension.
- Omitting that TLS 1.3 uses HKDF and removes many older constructs.
- For DH numeric questions: calculation errors in modular exponentiation (check repeated-squaring carefully).
For your exam
Kinds of questions and what full-marks answers include:
- Definitions: define TLS, handshake, record protocol, cipher suite; include where each term appears in the protocol stack.
- Short notes: explain TLS record format, handshake sequence, session resumption, and differences between TLS 1.2 and 1.3.
- Derivations: derive the master secret from pre-master in TLS 1.2; show PRF usage and seed order — include ClientRandom and ServerRandom sizes and the produced byte-length.
- Numericals / algorithms: small Diffie–Hellman computations (show modular exponent steps), or small HMAC calculation with a toy hash (show inner/outer pad steps).
- Diagrams: draw the client-server lifelines and label ClientHello, ServerHello, Certificate, (ServerKeyExchange), ClientKeyExchange, ChangeCipherSpec, Finished; mark where keys change and what data (randoms, public values) are exchanged.
A full-marks answer includes clear definitions of bolded terms, the exact handshake sequence (messages and contents), the formulas used to derive keys with correct ordering of concatenations and labels, and a diagram if asked.
Quick check
- What does a cipher suite name specify in TLS?
- In a (EC)DHE handshake when is forward secrecy achieved?
- What change to record processing did TLS 1.3 primarily bring (high level)?
Answers: 1. Key-exchange, authentication, bulk cipher and MAC/AEAD algorithm. 2. When ephemeral DH private keys are used for the handshake so the session keys cannot be recovered from long-term private keys later. 3. Mandatory AEAD-based encryption with a new HKDF-based key schedule and more handshake encryption (less plaintext metadata).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?