Skip to main content

Command Palette

Search for a command to run...

The TLS Handshake: Behind the Scenes of Secure Internet Communication

Updated
7 min read
The TLS Handshake: Behind the Scenes of Secure Internet Communication

It's a typical Wednesday afternoon. You visit your bank's website, enter your credentials, and check your account balance. This seemingly simple interaction involves a complex security dance happening behind the scenes—one that protects your sensitive information from prying eyes. At the heart of this security lies the TLS handshake.

What is TLS, Anyway?

Transport Layer Security (TLS)—the successor to the now-deprecated Secure Sockets Layer (SSL)—is a cryptographic protocol that enables secure communication over a computer network. When you see that little padlock icon in your browser's address bar or the "https://" prefix, you're witnessing TLS in action.

Born in 1999 as TLS 1.0 (essentially SSL 3.1), the protocol has evolved through several iterations, with TLS 1.3 (finalized in 2018) being the latest and most secure version. Each iteration has addressed vulnerabilities and improved performance—particularly by streamlining the handshake process we're about to explore.

The TLS Handshake: A Cryptographic Ballet

The TLS handshake is where the magic happens. It's a series of carefully choreographed steps between client and server that accomplishes three critical goals:

  1. Authentication (verifying who's who)

  2. Agreement on encryption algorithms

  3. Establishment of shared secret keys

Let's break down this complex process into digestible pieces.

Phase 1: Hello Messages - The Introduction

Client Hello

The client (your browser) kicks things off by sending a "Client Hello" message to the server. Think of this as saying, "Hey there, I'd like to talk securely with you. Here's what I can work with."

This message includes:

  • The highest TLS version the client supports

  • A client random (a generated string of random bytes)

  • A list of cipher suites the client can use (combinations of encryption and hashing algorithms)

  • Compression methods supported

  • Extensions (additional features like Server Name Indication or SNI, which helps with virtual hosting)

For example, your browser might say, "I can speak up to TLS 1.3, here's my random value 0x4C7...A26F, and I can work with TLS_AES_256_GCM_SHA384 or several other cipher suites."

Server Hello

The server receives the Client Hello and responds with its own "Server Hello" message: "Nice to meet you. Here's how we'll communicate."

This message includes:

  • The TLS version both can use (the highest common version)

  • A server random (another string of random bytes)

  • The chosen cipher suite from the client's list

  • The chosen compression method

  • Any relevant extensions that will be used

For instance: "We'll use TLS 1.2, here's my random value 0xD8E...47B, and I've selected TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 for our communications."

Phase 2: Certificate Exchange and Validation - The Credential Check

After the introductions, the server presents its digital certificate in the "Certificate" message.

A TLS certificate is essentially an ID card for websites, containing:

  • The server's public key

  • Domain name(s) the certificate is valid for

  • The certificate's validity period

  • The digital signature of a Certificate Authority (CA)

Your browser checks this certificate against its built-in list of trusted CAs. It verifies that:

  1. The certificate isn't expired

  2. It's issued for the domain you're connecting to

  3. It's signed by a trusted authority

  4. It hasn't been revoked

If any of these checks fail, you'll see one of those scary "Your Connection Is Not Private" warnings.

Phase 3: Key Exchange - The Secret Handshake

This is where things get particularly interesting. The client and server need to establish a shared secret without revealing it to anyone who might be eavesdropping. There are several key exchange methods, but let's focus on the most common ones:

For TLS 1.2 and earlier:

RSA Key Exchange:

  • The client generates a pre-master secret

  • Encrypts it with the server's public key from the certificate

  • Sends it to the server

  • Only the server, with its private key, can decrypt this message

Diffie-Hellman (DH) or Elliptic Curve Diffie-Hellman (ECDH):

  • The server sends DH parameters or its ECDH public key

  • The client generates its own DH/ECDH key pair

  • Both sides can mathematically derive the same secret without ever transmitting it

For TLS 1.3:
TLS 1.3 streamlined this process by eliminating RSA key exchange (which doesn't provide forward secrecy) and supporting only ephemeral Diffie-Hellman variants. The key exchange parameters are now included in the Hello messages, reducing the handshake to a single round-trip in most cases.

Phase 4: Verification and Finished - Crossing the T's

Client Verification (TLS 1.2)

The client sends a "Change Cipher Spec" message, indicating all future messages will be encrypted. It then sends a "Finished" message—the first encrypted message using the new keys—containing a hash of the entire handshake conversation.

Server Verification (TLS 1.2)

The server sends its own "Change Cipher Spec" followed by a "Finished" message. If both sides can decrypt and verify these messages, the handshake is successful.

TLS 1.3 Verification

In TLS 1.3, the "Change Cipher Spec" is eliminated, and the "Finished" messages contain HMAC values derived from the handshake transcript, providing stronger authentication.

Inside the Master Secret Generation

I've mentioned keys being generated, but I haven't explained how. This is crucial to understanding the TLS handshake's security.

In TLS 1.2, both parties use the pre-master secret (exchanged earlier) plus both random values to derive the "master secret" using a pseudorandom function (PRF):

master_secret = PRF(pre_master_secret, "master secret", 
                    client_random + server_random)

From this master secret, they derive several session keys:

  • Client write key (encryption key for client → server)

  • Server write key (encryption key for server → client)

  • Client write MAC key (for message authentication)

  • Server write MAC key

  • Client write IV (initialization vector)

  • Server write IV

TLS 1.3 improves this with a more robust key derivation process using HKDF (HMAC-based Key Derivation Function) and creates separate keys for handshake traffic and application data.

Resuming Sessions: The Fast-Track

Performing a full handshake for every connection would be inefficient. TLS includes mechanisms to resume sessions:

Session IDs (TLS 1.2):

  • Server issues a session ID during the initial handshake

  • Client includes this ID in subsequent connections

  • If the server recognizes it and the session hasn't expired, many handshake steps are skipped

Session Tickets (TLS 1.2):

  • Server encrypts session information and sends it to the client as a "ticket"

  • Client presents this ticket when reconnecting

  • Server decrypts it to retrieve session parameters

PSK and 0-RTT (TLS 1.3):

  • TLS 1.3 introduces Pre-Shared Keys (PSK) for session resumption

  • With 0-RTT (zero round-trip time), clients can even send application data in the first message

Common TLS Handshake Issues

Certificate Problems

One of the most frequent issues is certificate validation failures:

  • Expired certificates

  • Self-signed certificates

  • Wrong domain name

  • Missing intermediate certificates

I once spent three hours troubleshooting a connection issue only to discover the server's clock was set incorrectly, causing it to think a perfectly valid certificate had expired!

Cipher Suite Mismatches

If a client and server don't share any supported cipher suites, the handshake fails. This commonly happens when:

  • Servers are configured to use only secure ciphers

  • Clients (particularly old browsers) only support older, less secure ciphers

Protocol Version Incompatibility

As older TLS versions become deprecated for security reasons, some clients may be unable to connect to servers requiring newer versions. TLS 1.0 and 1.1 are now considered insecure, with many servers requiring at least TLS 1.2.

Performance Considerations

A full TLS handshake adds latency to connections. For high-traffic websites, this overhead can be significant. Solutions include:

  • Efficient session resumption

  • TLS 1.3 (reduces handshake to 1-RTT)

  • HTTP/2 and HTTP/3 multiplexing

  • CDN services that terminate TLS closer to users

The Evolution: TLS 1.3 Handshake

TLS 1.3 represents a dramatic improvement in both security and performance:

Simplified Handshake:

  • Reduced from 2-RTT to 1-RTT

  • Client can guess the key exchange parameters in its first message

  • Server can respond with its certificate, key exchange, and "Finished" message all at once

Stronger Security:

  • Removed support for obsolete and insecure algorithms

  • Eliminated static RSA and DH key exchange (no forward secrecy)

  • Encrypted more of the handshake to protect against downgrade attacks

Privacy Improvements:

  • Server certificates are now encrypted

  • Less information exposed to passive network observers

Conclusion

The TLS handshake is a masterpiece of cryptographic engineering—a carefully designed process that allows two parties who have never met to establish a secure communication channel over an insecure medium. Understanding its inner workings not only satisfies technical curiosity but also helps in troubleshooting security issues and making informed decisions about system design.

As web traffic continues to shift toward encrypted communications (with over 95% of Google traffic now encrypted), the importance of efficient and secure TLS implementations will only grow. Whether you're a developer implementing secure communications, a system administrator configuring web servers, or simply a curious internet user, appreciating the complexity and elegance of the TLS handshake gives you insight into one of the fundamental security mechanisms enabling our digital world.

Networking Decoded

Part 1 of 1

In this series, I will break down complex networking concepts into digestible, easy-to-understand lessons.