Sub topic 9
HTTPS and TLS
This chapter assumes you can read an HTTP request and response, you know how a TCP connection is opened, and you can run commands in a terminal. It covers what TLS adds, how the handshake works, how certificates establish trust, and which errors you will meet in practice.
Why plain HTTP is not enough
HTTP messages are plain text. Every device between you and the server can read them: Wi-Fi access points, your internet provider, and any proxy on the path. These devices can also change them. A request carrying a Cookie header would hand your session to anyone who watches the network.
TLS (Transport Layer Security) is a protocol that wraps a TCP connection and provides three guarantees:
- Confidentiality. Data is encrypted, so observers see only scrambled bytes.
- Integrity. Any change to the data in transit is detected and the connection is dropped.
- Authentication. The client verifies that it is talking to the real owner of the hostname, not an impostor.
HTTPS is simply HTTP carried over TLS. The HTTP messages are unchanged. Only the layer beneath them differs.
Where TLS sits
TLS runs on top of TCP and below HTTP. The browser first opens a TCP connection, usually to port 443 for https:// URLs. It then performs a TLS handshake. Only after that does it send the HTTP request, now encrypted.
| Layer | Plain HTTP | HTTPS |
|---|---|---|
| Application | HTTP messages | HTTP messages |
| Security | none | TLS |
| Transport | TCP, port 80 | TCP, port 443 |
| Network | IP | IP |
Because TLS sits below HTTP, a server needs no special request format for HTTPS. The request line, headers, and body are the same. They are encrypted before they leave the client.
The TLS 1.3 handshake
The handshake lets two parties who have never met agree on a secret key over an open network. TLS 1.3 needs one round trip after TCP is established. TLS 1.2 needed two.
The steps are:
- ClientHello. The client lists the TLS versions and cipher suites it supports. It adds a key share, which is its half of a Diffie-Hellman key exchange. It also sends the hostname it wants (SNI, described below) and the application protocols it speaks (ALPN, such as
h2orhttp/1.1). - ServerHello. The server picks a cipher suite and returns its own key share. Both sides now compute the same shared secret without ever sending it. From it they derive symmetric session keys.
- Certificate. The server sends its certificate and proves it holds the matching private key by signing the handshake transcript. These messages are already encrypted.
- Finished. Each side sends a message that authenticates the whole handshake. If an attacker altered anything, the check fails.
The key exchange uses fresh, temporary keys for every connection. This property is called forward secrecy. If the server's private key leaks later, recorded traffic from past sessions still cannot be decrypted.
Public-key operations are slow, so TLS uses them only for the handshake. The bulk of the traffic is encrypted with a fast symmetric cipher such as AES-GCM or ChaCha20-Poly1305.
Certificates and the chain of trust
A certificate binds a public key to one or more hostnames. It is signed by a certificate authority (CA). The browser checks four things:
- Chain. The server's certificate is signed by an intermediate CA, which is signed by a root CA. The root must be in the trust store of the operating system or browser.
- Hostname. The requested host must match a name in the certificate's Subject Alternative Name (SAN) list. Wildcards such as
*.example.commatch one label only, so they coverwww.example.combut nota.b.example.com. - Validity period. The current time must fall between the certificate's start and end dates.
- Revocation. The certificate must not have been revoked. Browsers check this with varying strictness.
If any check fails, the browser shows a warning page or the client library returns an error. A valid certificate proves control of a name. It does not prove that the site is trustworthy or safe.
Watching TLS with curl
The -v flag from the earlier curl example also shows the TLS details.
curl -v https://example.com/Look for lines that begin with * before the request is printed. They show the negotiated protocol and cipher, such as TLSv1.3, the ALPN result, and the certificate subject, issuer, and expiry date. A line such as SSL certificate verify ok means all four checks passed.
For the raw handshake and the full chain, use OpenSSL:
openssl s_client -connect example.com:443 -servername example.comThe -servername option sends the SNI value. To see a failure, request a site with a deliberately expired certificate:
curl -v https://expired.badssl.com/Curl exits with code 60 and explains that it could not verify the certificate. The -k option disables verification and makes the error disappear. Use it only for throwaway tests against your own servers, never in scripts that talk to real services.
Edge cases and gotchas
SNI. One IP address often serves many sites. The server needs to know which certificate to present before it can decrypt anything. SNI (Server Name Indication) puts the hostname in the ClientHello in plain text. Observers can therefore see which host you connect to, even though they cannot see the path, headers, or body. Encrypted ClientHello is an extension that hides it, but deployment is still partial.
What stays visible. An observer still sees IP addresses, timing, and the approximate size of each message. HTTPS hides content, not the fact that you communicated.
Mixed content. An HTTPS page that loads a script or image over http:// weakens the whole page. Browsers block active mixed content such as scripts and may upgrade or block images. Fix it by using https:// URLs or relative URLs.
HSTS. A user who types example.com may first reach http://, where an attacker can intercept the redirect. The server can answer over HTTPS with this header:
Strict-Transport-Security: max-age=31536000; includeSubDomainsThe browser then refuses plain HTTP to that host for a year. Test with a short max-age first, because a mistake is hard to undo.
TLS termination. Load balancers and CDNs often decrypt TLS and forward plain HTTP to the origin server. The traffic is protected on the public internet but not on the internal hop. Re-encrypt that hop if the network between them is not fully trusted.
Common errors.
| Symptom | Likely cause |
|---|---|
| Name mismatch error | Certificate does not list the requested hostname in its SAN |
| Certificate expired | Renewal failed, or the client clock is wrong |
| Unknown issuer | Server omitted the intermediate certificate, or the client lacks the root |
| Protocol or handshake failure | Client and server share no TLS version or cipher suite |
| Works in a browser but fails in a script | Script uses an old library or a trust store missing the CA |
An incomplete chain is the most common server-side mistake. Browsers can sometimes fetch missing intermediates themselves, so the site appears fine in a browser while curl and API clients fail.
Exercise
A site works in your browser, but curl https://api.example.com/ fails with an "unable to get local issuer certificate" error. The certificate is not expired and the hostname matches. What is the most likely cause, and how would you confirm it?
Show answer
The server probably sends only its own certificate and omits the intermediate CA certificate, so curl cannot build a chain to a trusted root. Browsers often fetch the missing intermediate and hide the problem. Confirm it with openssl s_client -connect api.example.com:443 -servername api.example.com and check whether the printed chain contains the intermediate. The fix is to configure the server to send the full chain.
Exercise
An observer on a coffee-shop network watches your connection to https://example.com/private/report.pdf. Name two pieces of information they can still see and two they cannot.
Show answer
They can see the destination IP address and the hostname (through SNI), and also the timing and approximate size of the traffic. They cannot see the path /private/report.pdf, the headers (including cookies), or the content of the file.