Sub topic 7
Securing the Connection with TLS
This chapter assumes you know what a TCP connection is and how the three-way handshake opens one. You should also know what a host name is and have a rough idea of public and private keys. After the TCP handshake from the previous chapter, the browser has a reliable byte stream to the server. That stream is not private. Anyone on the path can read it or change it. TLS (Transport Layer Security) adds three properties on top of TCP:
- Confidentiality. Only the two endpoints can read the data.
- Integrity. Any change to the data in transit is detected.
- Authentication. The browser knows it is talking to the real owner of the host name, not an impostor.
TLS is what turns http into https. It runs between TCP and HTTP, so HTTP itself does not change.
Where TLS Fits
For https://example.com/, the browser knows from the scheme that it must use TLS. The default port for https is 443. After TCP connects to port 443, the browser starts the TLS handshake. Only after the handshake finishes does it send the HTTP request.
What the Handshake Has to Solve
The two sides must agree on three things without having met before, over a network an attacker may watch.
- Parameters. Which TLS version, which cipher suite, and which application protocol to use.
- A shared secret. Both sides need the same symmetric key, and an eavesdropper must not be able to compute it.
- Identity. The server must prove it controls the certificate for the requested host name.
Key exchange
TLS uses asymmetric cryptography to agree on a key, then switches to faster symmetric encryption for the data. Modern TLS uses an ephemeral Diffie-Hellman exchange, usually over the X25519 elliptic curve. Each side generates a temporary key pair and sends its public half. Each side combines its own private half with the other's public half. Both get the same shared secret, and the secret never crosses the network.
Because the key pairs are temporary and discarded after the session, a later theft of the server's long-term private key does not reveal past traffic. This property is called forward secrecy. TLS 1.3 requires it.
Cipher suites
A cipher suite names the algorithms used for encryption and integrity. In TLS 1.3 the suite covers only the bulk encryption and hash, for example TLS_AES_128_GCM_SHA256 or TLS_CHACHA20_POLY1305_SHA256. These are AEAD ciphers (authenticated encryption with associated data). They encrypt and check integrity in one step. Key exchange and signatures are negotiated separately.
The Handshake Step by Step
ClientHello
The browser sends the first message. It contains:
- The TLS versions it supports.
- The cipher suites it supports.
- Its key share, the public half of a Diffie-Hellman key for one or more guessed groups.
- SNI (Server Name Indication), the host name it wants. One IP address often serves many sites, and the server needs the name to pick the right certificate.
- ALPN (Application-Layer Protocol Negotiation), a list such as
h2andhttp/1.1. The server's choice here decides the HTTP version, which a later chapter covers.
ServerHello and the encrypted rest
The server picks a version and cipher suite and returns its own key share. At this point both sides can compute the shared secret. Everything the server sends after ServerHello is encrypted. That includes the certificate.
The server then sends:
- Certificate. Its certificate chain.
- CertificateVerify. A signature over the handshake transcript made with the certificate's private key. This proves the server holds the key.
- Finished. A message authentication code over the whole handshake, which detects tampering with earlier messages.
The client checks the certificate (see below), verifies the signature, and sends its own Finished. The handshake is complete. The browser can send the HTTP request in the same flight as Finished.
Round trips
TLS 1.3 needs one round trip. TLS 1.2 needed two, because the key exchange happened after the hellos. Including TCP, a first HTTPS request costs two round trips before the first HTTP byte under TLS 1.3, and three under TLS 1.2. On a 100 ms link, that difference is 100 ms.
| Property | TLS 1.2 | TLS 1.3 |
|---|---|---|
| Round trips for a full handshake | 2 | 1 |
| Forward secrecy | Optional | Required |
| Certificate visible to observers | Yes | No, it is encrypted |
| Legacy ciphers (RC4, CBC modes, RSA key exchange) | Possible | Removed |
| Resumption with 0-RTT | No | Optional |
Certificates and Trust
A certificate binds a public key to a name. It is signed by a certificate authority (CA). The server sends a chain: its own leaf certificate, then one or more intermediate certificates. The root certificate is not sent. Browsers and operating systems ship with a trust store of root CAs.
To validate a certificate, the browser checks that:
- The leaf is signed by an intermediate, and each intermediate is signed by the next, up to a root in the trust store.
- Every certificate in the chain is inside its validity dates.
- The requested host name matches an entry in the certificate's Subject Alternative Name (SAN) list. Wildcards such as
*.example.comcover one label only, soa.b.example.comdoes not match. - The certificate has not been revoked. Browsers use mechanisms such as OCSP stapling, where the server attaches a recent signed status, or built-in revocation lists.
- The certificate is permitted for server authentication, and is accepted under Certificate Transparency policy.
If any check fails, the browser shows a full-page warning and does not send the request.
Session Resumption and 0-RTT
A full handshake is wasteful if you reconnect often. After the first handshake, the server can send a session ticket, an encrypted blob holding the session state. On the next connection the client presents the ticket in its ClientHello, and both sides derive new keys without repeating certificate checks.
TLS 1.3 also allows 0-RTT ("early data"). The client sends the HTTP request along with the ClientHello, using a key from the previous session. This saves a full round trip. The cost is that early data can be replayed by an attacker who records and resends it. Servers should accept 0-RTT only for idempotent requests such as GET and should not use it for actions that change state.
HSTS Connects Back to the Cache Check
Earlier in this tutorial, the browser checks local state before going to the network. One item is HSTS (HTTP Strict Transport Security). A server sends Strict-Transport-Security: max-age=31536000; includeSubDomains over HTTPS. The browser remembers it. For that period, the browser rewrites any http:// request for that host to https:// before connecting, so an attacker cannot downgrade the first request. A preload list ships with browsers to cover the very first visit.
Observing TLS
curl -v shows the handshake summary. Run it and look for the lines that begin with *.
curl -v https://example.com/ -o /dev/null 2>&1 | grep -E "TLS|SSL|subject|issuer|expire|ALPN"You should see the negotiated version (for example TLSv1.3), the cipher suite, the certificate subject and issuer, the expiry date, and the ALPN result. For the full certificate chain, use OpenSSL:
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/nullThe -servername flag sets SNI. Without it, a server hosting many sites may return a default certificate with the wrong name. To force a version and see whether the server supports it:
openssl s_client -connect example.com:443 -servername example.com -tls1_2 </dev/null 2>&1 | grep -E "Protocol|Cipher"In Chrome DevTools, the Security panel shows the protocol, key exchange, cipher and certificate details. In the Network panel, the Timing tab of a request splits out an "SSL" segment. It sits inside "Initial connection" and shows how long the handshake took. On a reused connection this segment is absent, because the connection and its keys already exist.
Common Errors
| Symptom | Likely cause |
|---|---|
| `NET::ERR_CERT_DATE_INVALID` | The certificate expired, or the client clock is wrong. |
| `NET::ERR_CERT_COMMON_NAME_INVALID` | The host name is not in the certificate's SAN list, or you connected by IP address. |
| `NET::ERR_CERT_AUTHORITY_INVALID` | The chain does not reach a trusted root, often a self-signed certificate or a missing intermediate. |
| Works in browser, fails in `curl` or an app | The server omitted an intermediate certificate. Browsers sometimes fetch it. Other clients do not. |
| `ERR_SSL_VERSION_OR_CIPHER_MISMATCH` | The server only offers protocols or ciphers the client has dropped. |
| Wrong certificate returned | The client sent no SNI, or the server is misconfigured for that name. |
| Mixed content warnings | An `https` page loads a subresource over `http`. The browser blocks it or flags it. |
What TLS Does Not Hide
TLS encrypts the HTTP request and response, including the path, query and headers. It does not hide everything. The destination IP address is visible. The host name is visible in the SNI field unless Encrypted Client Hello is in use. DNS queries are separate and are unencrypted unless DNS over HTTPS or DNS over TLS is used. The size and timing of traffic also remain visible.
Exercise
A browser connects to a server over TCP and TLS 1.3 with no session ticket. The round-trip time is 80 ms. About how long does it take, counting only network round trips, before the browser can send the HTTP request in the same flight as its final handshake message? How much longer would TLS 1.2 take, and why?
Show answer
TCP takes one round trip (80 ms). The TLS 1.3 handshake takes one more (80 ms), so about 160 ms before the request goes out with the client's Finished message. TLS 1.2 needs two round trips for its handshake, so the total is about 240 ms, which is 80 ms longer. The extra round trip exists because TLS 1.2 delays the key exchange until after the hello messages, while TLS 1.3 sends the client's key share in the ClientHello.
Exercise
A site works in Chrome but curl reports "unable to get local issuer certificate". The certificate is valid and not expired. Name the most likely cause and a command that shows it.
Show answer
The server probably sends only its leaf certificate and omits the intermediate. Chrome can fetch the missing intermediate itself, while curl cannot. Running openssl s_client -connect HOST:443 -servername HOST -showcerts shows the chain the server actually sends. If only one certificate appears, the fix is to configure the server to send the full chain.