Dev.to Security 🔐 Cybersecurity 👁 2 📖 11 min read

HTTPS and the TLS Handshake: How Two Strangers Agree on a Secret in a Crowded Room

You're Sitting in a Café, Typing Your Password Let's start with a scene you know well. You're in a café. The coffee is decent, the Wi-Fi is free, and you open your laptop to log into your bank. You type your password,

You're Sitting in a Café, Typing Your Password

Let's start with a scene you know well.

You're in a café. The coffee is decent, the Wi-Fi is free, and you open your laptop to log into your bank. You type your password, press Enter, and the page loads. Easy.

Now here's the part nobody thinks about: that Wi-Fi network is shared with everyone else in the room. The person two tables away, the stranger by the window, the "free café Wi-Fi" that may or may not be the café's Wi-Fi. Your data is travelling through the same air as theirs.

So why can't any of them read your password?

The answer is a little padlock in your browser, and the quiet bit of cleverness behind it. That's what this post is about: HTTPS, and the TLS handshake that makes it work.

In Part 7 we followed a request through DNS to find a server's address. Now you've found the server. The next question is: how do you talk to it without the whole room listening in?

The Postcard Problem

Plain old HTTP is like sending a postcard.

Everything you write is right there on the back, in plain sight. The postal worker can read it. The sorting office can read it. Anyone who handles it along the way can read it, and even change it before it arrives. For a note saying "wish you were here," that's fine. For your password or card number, it's a disaster.

HTTPS is like putting that message in a sealed, tamper-proof envelope. The postal workers still carry it, but they can't read it, and they can't change it without you noticing.

(The "S" stands for "Secure." HTTPS is just HTTP, wrapped in a layer of protection called TLS. You might also hear "SSL," which is the older name for the same idea. Technically we've all moved on to TLS, but the old name stuck, like calling every tissue a Kleenex.)

The Three Promises of HTTPS

When you see that padlock, three separate promises are being kept. It helps to name them, because people often only think about the first one.

1. Privacy (encryption). Nobody in the middle can read what you send. Your password turns into scrambled nonsense while it travels.

2. Integrity (tamper-proofing). Nobody in the middle can quietly change the message. If someone tries to alter even one character, the receiver detects it and rejects the whole thing.

3. Identity (authentication). You're really talking to the real yourbank.com, not a convincing impostor. This one is easy to forget, but it's just as important. A perfectly encrypted conversation with a scammer is still a conversation with a scammer.

Keep those three in your head: private, untampered, and the right person. Everything in the handshake exists to deliver them.

The Puzzle: How Do Strangers Share a Secret in Public?

Here's the genuinely hard problem, and it's why this topic is so fascinating.

To encrypt a conversation, both sides need to share a secret key, a kind of password only they know. But your browser and the server have never met. And everything they say to each other travels through a room full of potential eavesdroppers.

How do you agree on a secret when every word you say can be overheard?

It sounds impossible. It's like two people shouting across a crowded stadium and somehow ending up with a secret that nobody else in the stadium knows. Yet that's exactly what TLS does, thousands of times a second, all over the world. Two clever ideas make it work.

Idea 1: The Padlock Anyone Can Close

Imagine the server hands out open padlocks to anyone who asks. Anyone can snap one shut. But only the server holds the key that opens it.

So you can put a message in a box, lock it with the server's padlock, and send it. Anyone can see the box, but only the server can open it. This is called public-key (asymmetric) cryptography. There's a public part everyone can see (the padlock) and a private part only the server keeps (the key).

It solves the stadium problem beautifully. But it has a catch: it's slow. Great for a small, important message, impractical for encrypting every image and video on a page.

Idea 2: The Shared Secret Key

For the bulk of the conversation, it's far faster to use a single shared key, where the same key locks and unlocks. This is symmetric encryption. It's very fast, perfect for streaming real data.

But it brings back the original problem: how do both sides get that same key without anyone else seeing it?

The Trick: Use the Slow One to Set Up the Fast One

TLS combines both ideas. It uses the clever (slower) public-key method just long enough to agree on a shared key, then switches to the fast (symmetric) method for everything else.

Think of it like this: you use a fancy, slow, ultra-secure process to quietly agree on a secret handshake. After that, you just use the handshake.

The Paint Mixing Trick (How the Key Gets Agreed)

Here's a way to picture how two people create a shared secret in public. It's called a key exchange, and the classic explanation uses paint.

Imagine you and the server agree, out loud, on a common starting color, say yellow. Everyone in the stadium hears that. It's public.

  1. Secretly, you each pick a private color that you tell nobody. You pick red. The server picks blue.
  2. You each mix your private color into the shared yellow. Yours becomes orange. The server's becomes green.
  3. You swap the mixed buckets in public. Eavesdroppers see orange and green going back and forth.
  4. Now you each add your own private color to the bucket you received. You add red to the server's green. The server adds blue to your orange.

You both end up with the same final color: a muddy brownish mix of yellow, red, and blue. But an eavesdropper only saw yellow, orange, and green. Unmixing paint is nearly impossible, so they can't work out the final shade.

The real version uses math instead of paint, but the idea is identical: you can build a shared secret out of things that are safe to say out loud. Modern TLS does this in a way that creates a fresh secret for every single session, so even if someone records your traffic today and steals a key next year, they still can't read the old conversation. (That bonus has a fancy name, forward secrecy, and it's a really nice property.)

The Handshake, Step by Step

Now let's watch a real connection form. Think of it as a polite, quick introduction between two strangers. Here's the modern version (TLS 1.3), simplified:

  1. "Hello, here's what I can do." Your browser sends a ClientHello: the encryption methods it supports, plus its half of the key exchange (the "orange bucket").

  2. "Hello back, and here's my ID." The server replies with a ServerHello: it picks one of the methods, sends its half of the key exchange (the "green bucket"), and presents its certificate, its digital ID card.

  3. "Let me check your ID." Your browser inspects the certificate. Is it for the right website? Is it still valid? Was it issued by someone I trust? (More on this in a moment.)

  4. "We both have the secret now." Using the exchanged halves, both sides independently work out the same shared key. It never travelled across the network.

  5. "Ready when you are." Both sides confirm, and from here on, everything is encrypted with that shared key.

  6. The real conversation begins. Your request for the page, your password, the response: all sealed in the envelope.

All of that happens in roughly one round trip in TLS 1.3, a fraction of a second, before the first byte of real content is sent. (Older TLS versions needed more back-and-forth, which is one reason upgrading matters.)

The ID Card: What a Certificate Really Is

Step 3 is where the identity promise gets kept, so it's worth slowing down.

A certificate is a digital ID card for a website. It says, roughly: "This public key belongs to example.com, it's valid between these dates, and it was vouched for by this authority."

But anyone could write a card that says "I'm your bank." So why should you believe it? Because of a chain of trust.

Your device ships with a list of Certificate Authorities (CAs), organizations that are trusted to check who a website really is before issuing a certificate. Think of them like a passport office. A passport isn't trusted because the holder says "I'm me." It's trusted because a government body verified it and put an official stamp on it.

So when a server presents its certificate, your browser checks:

  • Does the name match? A certificate for example.com shouldn't be shown on evil-example.com.
  • Is it still valid? Certificates expire, usually in a matter of months.
  • Was it signed by someone I trust? Often through a short chain: your site's certificate was signed by an "intermediate" authority, which was signed by a "root" authority your device already trusts.
  • Has it been tampered with? The digital signature proves it hasn't.

If anything is off, your browser stops and shows that big scary warning page. And that warning is the system doing its job.

A happy modern detail: certificates used to cost money and be a hassle. Services like Let's Encrypt now issue them for free and automatically, which is a big reason most of the web is now HTTPS.

What the Padlock Does NOT Mean

This one trips up a lot of people, so let's be clear.

The padlock means: "Your connection to this site is private and untampered, and the site owns the domain name shown."

It does not mean: "This site is honest, safe, or trustworthy."

Scammers can get certificates too. A fake bank page at a lookalike address can have a perfectly valid padlock. The padlock protects the road, not the destination's intentions. Always check the actual address, not just the lock.

Where TLS Actually Happens (Connecting the Dots)

Remember the earlier parts of this series? HTTPS doesn't live in a vacuum:

  • Reverse proxy (Part 3): This is where TLS termination usually happens. The proxy handles all the encryption work at the edge, then forwards requests to your app over the internal network. Your app code never has to touch a certificate.
  • CDN (Part 1): CDNs terminate TLS at the edge server nearest the user, so the handshake travels a short distance instead of crossing an ocean. A big speed win.
  • Load balancer (Part 2): Often handles TLS too, so the servers behind it are free to focus on their real job.
  • API gateway (Part 4): Another common spot for terminating TLS and then applying auth and rate limits.
  • DNS (Part 7): Certificates are tied to domain names, and the whole system relies on DNS pointing to the right place first.

So when someone says "just put it behind a proxy and turn on HTTPS," now you know exactly what they mean.

When It Goes Wrong (and What the Error Means)

HTTPS errors are some of the most alarming messages a user ever sees, but they're usually very simple underneath:

Expired certificate. The ID card passed its date. The #1 cause of "the site suddenly broke." Fix: renew it, and set up auto-renewal so you never have to remember.

Name mismatch. The certificate is for example.com but the visitor came to www.example.com, or a subdomain it doesn't cover. Fix: make sure the certificate covers every name you use.

Self-signed or untrusted certificate. You made your own ID card instead of getting one from a trusted authority. Fine for local development, never for a real site.

Mixed content. The page itself loads over HTTPS, but some image or script is still requested over plain HTTP. Browsers flag or block it, because that one postcard undermines the whole sealed envelope.

Wrong device clock. Believe it or not, if a device's clock is way off, every certificate looks "expired" or "not yet valid." A confusing one to debug.

Old protocol versions. Very outdated TLS versions are insecure and get rejected. Keep your server's TLS configuration modern.

Your Detective Kit

You can peek at a connection yourself from the terminal:

# See the whole handshake and certificate details
curl -v https://example.com

# Look at a site's certificate directly
openssl s_client -connect example.com:443 -servername example.com

In the output, look for the certificate's expiry date, the issuer (who vouched for it), and the TLS version (aim for 1.2 or 1.3). Your browser can show the same info if you click the padlock and look at the certificate details.

How to Set Up HTTPS (A Simple Checklist)

Same spirit as the earlier checklists. When you put a site on HTTPS, walk through these:

  1. Get a certificate. Use a free, automated one (like Let's Encrypt), or the one your host or CDN provides.
  2. Cover every name. Include the root domain, www, and any subdomains you use.
  3. Install it where TLS ends. Usually at your reverse proxy, load balancer, CDN, or hosting platform.
  4. Redirect HTTP to HTTPS. Anyone arriving on plain HTTP should be sent to the secure version automatically.
  5. Turn on HSTS. This tells browsers "only ever use HTTPS for this site," so they never even try the insecure version again.
  6. Fix mixed content. Make sure every image, script, and stylesheet loads over HTTPS.
  7. Automate renewal and monitor expiry. The most common outage is a forgotten certificate.

Get, cover, install, redirect, HSTS, mixed, renew. Seven steps, and your site is wearing its sealed envelope.

What to Take Away

  1. HTTP is a postcard, HTTPS is a sealed envelope. The "S" is TLS wrapped around HTTP.
  2. HTTPS keeps three promises: private (encryption), untampered (integrity), and the right person (authentication).
  3. The big puzzle is sharing a secret in public. TLS solves it by using clever slow math to agree on a key, then fast encryption for the real traffic.
  4. The handshake is a quick introduction: hello, here's my ID, let me check it, we now share a key, let's talk privately.
  5. A certificate is a digital ID card, trusted through a chain that ends at an authority your device already knows.
  6. The padlock protects the road, not the destination. It doesn't prove a site is honest.
  7. Most HTTPS problems are boring: an expired certificate, a name mismatch, or mixed content. Automate renewal.

So the next time you're in a café, typing a password on questionable Wi-Fi, take a second to appreciate the little padlock. In the time it took your page to load, two strangers met, checked each other's ID, agreed on a secret in a crowded room, and sealed the envelope. And it happened so fast that you never noticed.

Next in this series: HTTP/1.1 vs HTTP/2 vs HTTP/3, or how the web learned to stop loading things one at a time and why the newest version quietly walked away from TCP.

What was the first HTTPS error you ever had to deal with? Tell me in the comments. Everyone remembers their first scary certificate warning.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.