HTTPS & TLS Basics — Security

HTTPS is HTTP over TLS, the layer that guarantees confidentiality (the data is scrambled), integrity (the data can't be changed in secret) and authentication…

You open a site, and the browser shows a red warning page about the certificate, the site's proof of identity. Plenty of people click "proceed" right away. Behind that warning is one check that failed. That check is what keeps your password secret on its way from the browser to the server.

That check is part of HTTPS: HTTP wrapped in TLS (Transport Layer Security), the layer that encrypts data between the browser and the server. Without HTTPS, everything sent, passwords and cookies included, can be read by anyone on the same network as you, for example on cafe Wi-Fi. Picture mailing a postcard: the mail carrier and anyone who holds it can read it. HTTPS turns that postcard into a letter in a sealed envelope that only the right recipient can open. The postcard is the data sent over plain HTTP, the mail carrier is the network the data passes through, and the envelope is TLS encryption.

Three things TLS guarantees

TLS gives three guarantees at once on every connection:

Modern browser features like service workers (scripts that can make a site work offline) and geolocation (the user's location) also run only on HTTPS. Google also gives HTTPS sites a small boost when ranking search results.

The TLS 1.3 handshake: agreeing a key before exchanging data

Before the first data is sent, the browser and server do a handshake: a short introduction to agree on a shared secret key. The latest version, TLS 1.3, finishes it in a single round trip (1-RTT). In the Lab, the handshake to bank.example runs step by step:

Step Direction Contents
1. ClientHello browser to server TLS 1.3 version, a list of ciphers, and the browser's key share
2. ServerHello server to browser the chosen cipher, and the server's key share
3. Certificate + CertificateVerify server to browser the bank.example certificate (signed by a CA, a certificate issuer), then the server's signature over the handshake
4. Finished server then browser the server sends Finished, the browser checks the certificate and the signature, then sends Finished
5. Application data both ways all data encrypted with the session key

So TLS uses two kinds of cryptography: asymmetric (a pair of a public key and a private key) only at the start, to agree on a key and to prove the server's identity, then symmetric encryption for all the data after.

The keys used in the key exchange are made fresh for each connection and thrown away after the handshake (ephemeral, used once). This gives Perfect Forward Secrecy (PFS): if the server's private key leaks in the future, recorded data from past connections still can't be opened, because their keys are already gone.

The certificate and the chain of trust

A certificate is the server's digital ID card. It holds the domain name (bank.example), the server's public key, a validity period, and a signature from its issuer. That issuer is a Certificate Authority (CA): a party the browser trusts.

The browser doesn't trust the bank.example certificate on its own. It follows a chain of signatures up to a root it already trusts:

bank.example            (leaf: the site's certificate)
  signed by
intermediate CA         (an intermediate certificate)
  signed by
ISRG Root X1            (root CA, already in the browser)

The browser also refuses if the domain name doesn't match, the certificate has expired, or a signature is invalid. Free certificates come from Let's Encrypt, usually through Certbot (a program that requests and installs certificates), and renew automatically.

HSTS: forcing HTTPS

Even when a site is already HTTPS, a visit sometimes still starts on http://, for example when someone types the address without https://, and is then redirected to https://. That http:// request is not encrypted. The HSTS header closes that gap for later visits: it asks the browser to remember to always use HTTPS for this domain. The very first visit to the site is still exposed, because the header has not been received yet. To close that gap too, the domain is added to the HSTS preload list, a list of domains built into browsers (through hstspreload.org). One requirement: the header must also include preload (the example below doesn't).

Strict-Transport-Security: max-age=31536000; includeSubDomains

Recognising a safe URL

This is a simple check that tells three kinds of URL apart by their protocol, the start of the URL before ://:

function isSecureURL(url) {
  const lower = url.toLowerCase();         // check case-insensitively
  if (lower.startsWith("https://")) return "secure";
  if (lower.startsWith("http://")) return "insecure";
  return "invalid";                         // not http/https
}

console.log(isSecureURL("https://example.com")); // secure
console.log(isSecureURL("http://example.com"));  // insecure
console.log(isSecureURL("ftp://files.com"));     // invalid

In the exercise, your function is tested with these five calls:

Call Result
isSecureURL("https://example.com") "secure"
isSecureURL("http://example.com") "insecure"
isSecureURL("ftp://files.com") "invalid"
isSecureURL("HTTPS://SECURE.COM") "secure"
isSecureURL("mailto:[email protected]") "invalid"

⚠️ What gets missed about HTTPS

TL;DR: HTTPS is HTTP over TLS, the layer that guarantees confidentiality (the data is scrambled), integrity (the data can't be changed in secret) and authentication (the server proves who it is). The TLS 1.3 handshake, an introduction to agree on a key, finishes in one round trip. The browser and server exchange key shares (public numbers) in the ClientHello and ServerHello messages, to compute a shared session key, the key used to scramble the data. The server sends a certificate (the server's digital ID card) and a signature that proves it owns the certificate (CertificateVerify), then all data is encrypted symmetrically (the same single key on both sides) with the session key. The browser checks the certificate through a chain of signatures from the leaf (the site's certificate) to the intermediate (a go-between certificate) to a root the browser trusts. Free certificates come from Let's Encrypt. The HSTS header (Strict-Transport-Security) forces the browser to always use HTTPS from the second visit on. The first visit is protected by HSTS preload, a list of domains built into browsers. isSecureURL, the function from the exercise, returns "secure" for https://, "insecure" for http://, and "invalid" for anything else, after turning the URL into lower case.

⚡ A postcard becomes a sealed envelope

Meaning: Without HTTPS, the data you send is like a postcard: anyone on the same network can read it. HTTPS is HTTP wrapped in TLS (a layer of encryption), like a letter in a sealed envelope. TLS guarantees three things at once: confidentiality (the data is scrambled), integrity (it can't be changed in secret on the way), and authentication (the server proves it really is the site you meant to reach, such as bank.example).

⚡ A one-round-trip handshake

Guess first: before exchanging data, the browser and server do a handshake (an introduction) to agree on a session key, the key used to scramble the data. Is that session key sent over the network?

Never. The browser and server send each other key shares (public numbers), then each computes the same session key on its own. This method is called ECDHE.

Meaning: TLS 1.3 finishes the handshake in one round trip (one trip there and back). After that, all data is encrypted symmetrically (the same single key on both sides) with the session key, because that's far faster than the method used in the handshake.

⚡ The chain of trust

Write:

bank.example            (leaf: the site's certificate)
  signed by
intermediate CA         (an intermediate certificate)
  signed by
ISRG Root X1            (root CA, already in the browser)

Meaning: A certificate is the server's digital ID card. The browser follows this chain of signatures up to a root CA, an issuer the browser already trusts. If the chain is broken, the domain name doesn't match, or the certificate has expired, the browser refuses it and shows a warning.

⚡ HSTS and a safe URL

Write:

Strict-Transport-Security: max-age=31536000; includeSubDomains

Meaning: Once this HSTS header is received, the browser remembers to always use HTTPS for the domain that sent it, for one year (max-age is in seconds). A URL that starts with http:// is unsafe, so don't send a password through it. In the exercise, isSecureURL returns "insecure" for such a URL. Turn the URL into lower case first with toLowerCase().

What you will learn