TLS
What TLS guarantees, how certificates and the TLS 1.3 handshake work, where to terminate it in a design, and when to use mutual TLS between services.
The problem
HTTP was designed as plain text. Anyone on the path between a browser and a server, such as another person on the same café Wi-Fi, an ISP or a compromised router, could read every request, change every response, or pretend to be the server.
For years the common compromise was to encrypt only the login page, since that is where the password travels. In October 2010 a Firefox extension called Firesheep showed why that fails. Sites that encrypted the login still sent the session cookie over plain HTTP on every later request. Firesheep listened on open Wi-Fi, listed the people nearby who were logged in to sites such as Facebook and Twitter, and let its user take over a session with a double click. No password was needed, because the cookie is as good as the password.
The fix was to encrypt everything, always. The protocol that does it is TLS, the successor to Netscape’s SSL from the mid-1990s. The current version, TLS 1.3, was published in 2018. Free automated certificates from Let’s Encrypt, from 2015, removed the last excuse.
What TLS provides
- Confidentiality. Nobody on the path can read the data.
- Integrity. Nobody can change it without detection.
- Authentication. The server proves it holds the private key for the name you asked for. The client can optionally prove its identity too.
It is worth knowing what TLS does not do. An observer still sees the IP addresses, the timing and size of the traffic, and normally the server name, which the client sends unencrypted so that a server hosting many sites can pick the right certificate. And TLS protects data in transit only. Once a load balancer or server decrypts it, protecting it is a different job, covered in encryption.
Certificates and chains
A certificate is a signed statement: “this public key belongs to shop.example.com”. The signer is a certificate authority (CA), which checked that the requester controls the domain.
Clients do not trust your certificate directly. They ship with a set of root certificates. A root signs an intermediate CA certificate, and the intermediate signs your leaf certificate. During the handshake the server sends the leaf and the intermediates, and the client checks that:
- each certificate is signed by the next, ending at a root it already trusts;
- the hostname matches a name in the leaf;
- every certificate is within its validity dates and has not been revoked, as far as the client can tell.
Two things break in production. One is a server that sends only the leaf, which works in some browsers and fails in stricter clients. The other is expiry. An expired certificate takes a service down as surely as a crashed server, and it has caused long outages at very large companies.
The TLS 1.3 handshake
Step 1 of 8
Two things happen long before any connection. A certificate authority signs the server's certificate, which binds a public key to the name shop.example.com. And the client ships with that authority's root certificate in its trust store.
Read the steps as text
- Two things happen long before any connection. A certificate authority signs the server's certificate, which binds a public key to the name shop.example.com. And the client ships with that authority's root certificate in its trust store.
- The TCP connection is open. The client sends ClientHello with the TLS versions and ciphers it supports, the server name it wants, and a fresh one-time public key. It sends that key share without waiting to be asked, which saves a round trip.
- The server replies with ServerHello and its own one-time key share. Each side combines its private half with the other's public half and arrives at the same secret. An eavesdropper who saw both public halves cannot.
- In the same flight, now encrypted, the server sends its certificate chain, a signature over the whole handshake made with the certificate's private key, and a Finished message.
- The client checks the evidence. The chain must lead to a root in its trust store, the name must match, the dates must be valid, and the signature must verify. The signature proves the server holds the private key, so a copied certificate is useless.
- The client sends its own Finished message with the first HTTP request right behind it. The handshake cost one round trip. TLS 1.2 needed two, and sent the certificate in the clear.
- On a later visit the client presents a session ticket from last time. Both sides skip the certificate and signature. With zero round trip mode the request rides in the very first packet, but an attacker can replay that packet, so it is only safe for idempotent requests.
- The expensive public-key maths happens only in the handshake. Everything after it uses fast symmetric encryption. So the cost of TLS is mostly round trips at connection setup, which is why you keep connections alive and resume sessions.
The handshake does two jobs: agree on keys that nobody else can learn, and prove who the server is.
The key agreement is an ephemeral Diffie-Hellman exchange. Each side generates a one-time key pair, sends the public half, and combines the other’s public half with its own private half. Both arrive at the same secret, and the one-time keys are thrown away afterwards. This gives forward secrecy: someone who records the traffic today and steals the server’s private key next year still cannot decrypt it, because that key was only ever used to sign.
The proof of identity is that signature. The server signs the handshake transcript with the certificate’s private key. A copied certificate is not enough to impersonate the site.
| TLS 1.2 | TLS 1.3 | |
|---|---|---|
| Round trips for a full handshake | 2 | 1 |
| Key exchange | RSA or Diffie-Hellman | Ephemeral Diffie-Hellman only |
| Forward secrecy | Depends on the cipher chosen | Always |
| Server certificate on the wire | Visible | Encrypted |
TLS 1.3 saves the round trip because the client guesses which key exchange the server will accept and sends its key share in the first message. It also deleted the old, weak options, so there is far less to misconfigure.
Resumption and 0-RTT
Public-key operations happen only in the handshake. After it, data is protected with symmetric encryption, which modern CPUs do at very high speed. So the cost of TLS is mostly the setup round trip and the signature, and both are worth avoiding.
After a handshake the server gives the client a session ticket. On the next connection the client presents it, and both sides skip the certificate and signature. With 0-RTT, the client also sends its first request encrypted under a key derived from the ticket, before hearing from the server at all.
There is a catch. An attacker can record that first flight and send it again, and the server may process the request twice. Early data is acceptable for an idempotent GET and not for “transfer £100”. Many deployments leave 0-RTT off, or allow it only for safe methods.
Where to terminate TLS
“Terminating” TLS means being the endpoint that decrypts. In a design with a load balancer there are three choices.
| Option | What the balancer can do | Cost |
|---|---|---|
| Terminate at the edge, plain HTTP inside | Everything: routing by path, caching, WAF, retries | Internal traffic is readable by anyone who gets onto the network |
| Terminate at the edge, re-encrypt to backends | Everything | A second handshake per backend connection, and certificates on every service |
| Passthrough to the service | Only L4 balancing, plus routing on the unencrypted server name | No L7 features at the balancer; each service manages its own public certificate |
Terminate and re-encrypt is the common choice for systems that carry anything sensitive. Connection pooling between the balancer and the backends keeps the cost of the second handshake small. If a CDN sits in front, the same question applies between the CDN and your origin: use TLS there too, and have the CDN verify the origin’s certificate.
Mutual TLS between services
In ordinary TLS only the server presents a certificate. In mutual TLS (mTLS) the server asks for one from the client as well, and verifies it against a CA it trusts. Between services this replaces “I trust this request because it came from inside the network” with “I trust it because the caller proved it is the orders service”.
To run it you need an internal CA, a way to give every workload a certificate, and automatic rotation, usually with lifetimes of hours or days. Service meshes such as Istio and Linkerd do this in a sidecar proxy, so application code does not change.
Know its limits. mTLS authenticates the calling service. It does not say whether that service may perform this operation, and it does not carry the end user’s identity. Those need policy and tokens, covered in authentication and authorisation.
When to choose what
- TLS on every public endpoint: always. There is no reason left to serve plain HTTP.
- TLS 1.3: wherever clients support it, with 1.2 as a fallback for old clients.
- 0-RTT: only for idempotent requests, and only if the saved round trip matters to you.
- Passthrough: when the service itself must see the client’s certificate, or policy forbids any intermediary from decrypting. Do not choose it if you need L7 routing.
- mTLS: when you have many services and want identity that does not depend on network position. For three services in one private subnet, the certificate machinery may cost more than it returns, so say so.
In the interview
TLS appears in “what happens when you type a URL”, in any question about securing service-to-service calls, and whenever you draw a load balancer and are asked “is that link encrypted?”.
A strong answer in a minute: TLS gives confidentiality, integrity and server authentication through a certificate chain. TLS 1.3 needs one round trip, with forward secrecy from ephemeral keys, and resumption makes repeat connections cheaper. I would terminate at the load balancer to get L7 routing, re-encrypt to the backends, and use mTLS between services so that each has a verifiable identity. Then name one operational risk, such as certificate expiry, and how you would automate it away.
Key takeaways
- TLS gives confidentiality, integrity and authentication of the server. It does not hide who is talking to whom, or protect data after it is decrypted.
- A certificate binds a public key to a name and is trusted through a chain of signatures ending at a root the client already holds.
- TLS 1.3 completes a full handshake in one round trip, always uses ephemeral keys for forward secrecy, and encrypts the certificate.
- 0-RTT resumption saves a round trip but its data can be replayed, so restrict it to idempotent requests.
- Where you terminate TLS decides what the load balancer can see. Terminating at the edge and re-encrypting to backends is the common middle ground.
- Mutual TLS gives each service a cryptographic identity. It answers who is calling, not whether the call is allowed.
Check yourself
0 of 5 answered
Flashcards
These 6 cards are in your review deck. Tap a card to see the answer.
What three guarantees does TLS provide?
Confidentiality (others cannot read the data), integrity (it cannot be altered unnoticed) and authentication (the server proves it holds the key for the name you asked for).
What is a certificate chain?
The server's leaf certificate is signed by an intermediate CA, which is signed by a root CA. Clients ship with the roots. The server must send the leaf and the intermediates.
How many round trips does a full handshake take in TLS 1.2 and in TLS 1.3?
Two in TLS 1.2 and one in TLS 1.3, because the 1.3 client sends its key share in the first message without waiting to negotiate.
What is forward secrecy?
Session keys come from one-time key exchanges, so stealing the server's long-term private key later does not let anyone decrypt traffic recorded earlier. It is mandatory in TLS 1.3.
What are the three places to terminate TLS, and the main trade-off?
At the edge with plain HTTP inside (simple, internal traffic exposed), at the edge with re-encryption (L7 features plus encryption, more certificates), or passthrough to the service (end to end, but the balancer cannot read HTTP).
What does mutual TLS add over ordinary TLS?
The client also presents a certificate, so both sides are authenticated. Between services this gives each workload a verifiable identity that does not depend on its IP address.