TLS/SSL
The TLS handshake, cipher suites, forward secrecy, and HTTPS
Skills taught: crypto.tls
How TLS Works
TLS (Transport Layer Security) encrypts data in transit between client and server. It provides confidentiality (eavesdropping prevention), integrity (tampering detection), and authentication (server identity verification). TLS 1.3 is the current version — TLS 1.0 and 1.1 are deprecated and must not be used.
The TLS 1.3 Handshake
TLS 1.3 Handshake (1 round-trip)
Client → Server: ClientHello
- Supported cipher suites: TLS_AES_256_GCM_SHA384, TLS_CHACHA20_POLY1305_SHA256
- Key share: X25519 public key (for ECDHE)
- Random nonce
Server → Client: ServerHello + Certificate + CertificateVerify + Finished
- Chosen cipher suite
- Key share: X25519 public key
- Certificate (signed by CA) → proves server identity
- CertificateVerify: signature over handshake using private key
- Finished: HMAC over entire handshake (tamper detection)
Both sides:
- Compute ECDHE shared secret from key shares
- Derive session keys using HKDF
- All further data encrypted with AES-256-GCM
Total: 1 round-trip before data flows
Forward secrecy is a critical TLS 1.3 feature. Each connection uses fresh ECDHE key pairs that are discarded after the session. Even if the server's private key is later compromised, past sessions cannot be decrypted because the session keys were derived from ephemeral keys that no longer exist.
- Forward Secrecy (PFS)
- Property that ensures past sessions cannot be decrypted even if long-term keys are later compromised. Achieved in TLS 1.3 by using ephemeral ECDHE for key exchange.
- Cipher Suite
- A named combination of algorithms for key exchange, authentication, encryption, and MAC. TLS 1.3 only permits 5 suites, all using AEAD and ephemeral key exchange.
Configuring TLS in Python
import ssl
import socket
# --- TLS Server ---
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.minimum_version = ssl.TLSVersion.TLSv1_2 # enforce TLS 1.2 minimum
context.load_cert_chain("cert.pem", "key.pem") # certificate + private key
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("0.0.0.0", 4443))
sock.listen(5)
with context.wrap_socket(sock, server_side=True) as ssock:
while True:
conn, addr = ssock.accept()
with conn:
data = conn.recv(4096)
conn.send(b"HTTP/1.1 200 OK\r\n\r\nHello over TLS!")
# --- TLS Client (verify certificate) ---
client_context = ssl.create_default_context() # loads system trust store
with socket.create_connection(("example.com", 443)) as raw:
with client_context.wrap_socket(raw, server_hostname="example.com") as tls:
tls.send(b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
print(tls.recv(4096))
# Check a server's TLS configuration
openssl s_client -connect example.com:443 -tls1_3 2>/dev/null | head -20
# See which protocols and cipher suites are supported
nmap --script ssl-enum-ciphers -p 443 example.com
# Test with curl (shows TLS version used)
curl -v https://example.com 2>&1 | grep -E 'TLS|SSL|cipher'
# Check certificate expiry
echo | openssl s_client -connect example.com:443 2>/dev/null \
| openssl x509 -noout -dates
TLS 1.2 vs TLS 1.3
- TLS 1.2: 2008 standard; widely supported; more flexible but weaker defaults
- TLS 1.3: 2018 standard; faster, more secure, forward secrecy required
Warning: Never disable TLS certificate verification in production (verify=False in requests, ssl.CERT_NONE). It makes the connection vulnerable to man-in-the-middle attacks. If you need to test with self-signed certificates, add the CA certificate to the trust store instead.
Tip: Use HSTS (HTTP Strict Transport Security) to tell browsers to always use HTTPS: Strict-Transport-Security: max-age=31536000; includeSubDomains. This prevents protocol-downgrade attacks.
Frequently Asked Questions
What is HSTS?
HTTP Strict Transport Security — a response header that tells browsers to only connect via HTTPS for a specified duration (max-age). It prevents protocol downgrade attacks where an attacker tries to redirect HTTPS traffic to HTTP.
What is a man-in-the-middle (MitM) attack on TLS?
An attacker intercepts the connection and presents their own certificate instead of the real server's. TLS prevents this because the attacker cannot create a valid certificate signed by a trusted CA for a domain they don't control — unless they compromise a CA.
Should I disable TLS 1.0 and 1.1?
Yes. TLS 1.0 is vulnerable to BEAST and POODLE attacks. TLS 1.1 is marginally better. Both are deprecated by RFC 8996 (2021) and browsers have removed support. Configure your server to require TLS 1.2 as the minimum.