Hello! Welcome to your fourth lesson in the "Networking for DevOps Engineers" module.
In our last lesson, we demystified DNS, the internet's phonebook, which allows us to find a server's IP address using its human-friendly name. Now that we have the address, our next task is to establish a connection and ensure our communication is private and secure. This is where TCP and TLS come into play.
Today’s lesson directly addresses the learning outcome: Describe the TCP three-way handshake and the role of TLS in securing HTTP traffic. This is a cornerstone of modern networking and cybersecurity. Given your interest in security and your identified knowledge gap in networking, this lesson will connect some crucial dots for you.
We will cover:
- How the TCP three-way handshake establishes a reliable connection.
- Why this connection is not inherently secure and where TLS fits in.
- The step-by-step process of the TLS handshake, which creates an encrypted tunnel for our data.
1. The Foundation: TCP's Three-Way Handshake
Before any application data (like an HTTP request) can be sent, the client and server must agree to communicate. For most of the web, this agreement is handled by the Transmission Control Protocol (TCP).
TCP is a connection-oriented protocol. It prioritizes reliability and ordered data delivery over raw speed. This is in contrast to a connectionless protocol like UDP, which is faster but offers no guarantees about delivery or order—making it suitable for services like DNS queries or video streaming where speed is paramount and losing a single packet isn't catastrophic.
This article provides a quick but clear comparison between TCP and UDP, setting the stage for why TCP's handshake is necessary for reliable communication.
Read the section 'TCP vs. UDP: How does the TCP 3-way handshake differ from a UDP connection?'. Focus on the table comparing their features.
The process TCP uses to establish a connection is famously known as the three-way handshake. Its primary goal is to synchronize the sequence numbers on both the client and server, which is essential for tracking data packets and ensuring they are reassembled in the correct order.
The handshake consists of three steps:
- SYN (Synchronize): The client initiates the connection by sending a TCP segment with the SYN flag set. This packet contains a randomly generated Initial Sequence Number (ISN). Think of this as the client saying, "I'd like to start a conversation, and my first sentence will be number X."
- SYN-ACK (Synchronize-Acknowledge): The server receives the SYN. If it's able to accept the connection, it responds with a segment that has both the SYN and ACK flags set.
- The SYN part contains the server's own randomly generated ISN. "I'm ready to talk too, and my first sentence will be number Y."
- The ACK part acknowledges the client's ISN by sending back the client's ISN + 1. "I'm expecting you to say sentence number X+1 next."
- ACK (Acknowledge): The client receives the SYN-ACK and sends a final segment with the ACK flag set. This acknowledges the server's ISN by sending the server's ISN + 1. "Got it. I'm expecting you to say sentence number Y+1."
Once the server receives this final ACK, the connection is moved to the ESTABLISHED state on both ends, and data transfer can begin.
TCP - 12 simple ideas to explain the Transmission Control Protocol
The video below from Practical Networking does an excellent job of visualizing this process, showing how the sequence numbers and TCP flags are set in each packet.
Watch the segment from 24:41 to 29:30. Pay close attention to how the sequence and acknowledgment numbers are incremented. The 'phantom byte' concept for SYN packets is a key detail that explains why the sequence number is bumped even though no data is sent.
From your computer science background, you'll appreciate that this is essentially a state-synchronization algorithm. Both client and server transition through specific states during this process:
| Phase | Client State | Server State |
|---|---|---|
| Before connection | CLOSED |
LISTEN |
| Client sends SYN | SYN-SENT |
LISTEN |
| Server sends SYN-ACK | SYN-SENT |
SYN-RECEIVED |
| Client sends ACK | ESTABLISHED |
ESTABLISHED |
2. The Missing Piece: Securing the Connection with TLS
We've established a reliable connection. The problem? It's not private. All data sent over this standard TCP connection, including HTTP requests and responses, is in plain text. Anyone intercepting the traffic can read it.
This is where Transport Layer Security (TLS) comes in. TLS is a cryptographic protocol that operates on top of an established TCP connection to create a secure, encrypted tunnel. When you see https in your browser's address bar, you're using HTTP over a TLS-secured TCP connection.
This layering is a critical concept. TLS does not replace the TCP handshake; it happens after the TCP handshake is complete.

The primary goals of the TLS handshake are:
- Authentication: The client verifies that the server is who it claims to be (using a digital certificate).
- Negotiation: The client and server agree on a set of cryptographic algorithms (a "cipher suite") they both support.
- Key Exchange: They securely generate and exchange a symmetric session key that will be used to encrypt all further communication.
The genius of TLS is that it uses slow, computationally expensive asymmetric cryptography (public/private keys) only for the initial handshake to securely establish a shared secret. Then, it uses that secret to generate fast and efficient symmetric keys for encrypting the actual application data.
3. The TLS 1.2 Handshake in Detail
Let's walk through the steps of a common TLS 1.2 handshake using the RSA key exchange algorithm. This process might seem complex, but each step serves a specific security purpose.
TLS Handshake - EVERYTHING that happens when you visit an HTTPS website
The following video from Practical Networking provides an exceptionally detailed, record-by-record breakdown of the entire TLS handshake. We will watch it in segments to digest each part of the process.
Prepare to watch several clips from this video. It's dense, but it will give you a solid understanding of the mechanics.
Step 1: Hello and Negotiation (ClientHello, ServerHello)
The handshake begins with the client and server exchanging "Hello" messages to agree on the rules of engagement.
- The
ClientHellomessage includes the TLS versions the client supports, a list of supported cipher suites, and a random string of bytes (ClientRandom). - The
ServerHellomessage is the server's response. It selects the highest TLS version and the strongest cipher suite they both support, and sends its own random string (ServerRandom).
At the end of this exchange, both parties know the TLS version, the cipher suite to use, and both random numbers.
TLS Handshake - EVERYTHING that happens when you visit an HTTPS website
Let's watch the first part of the handshake where these initial parameters are negotiated.
Watch from 03:08 to 07:43. Focus on the five fields exchanged in the Hello messages: version, random number, session ID, cipher suites, and extensions.
Step 2: Server Authentication and Key Exchange (Certificate, ClientKeyExchange)
Next, the server proves its identity and the client provides the material for the shared secret.
- Server sends its Certificate: The server sends its SSL/TLS certificate to the client. The client's browser verifies this certificate against its list of trusted Certificate Authorities (CAs) to confirm the server's identity. This certificate contains the server's public key.
- Client generates and encrypts the Pre-Master Secret: The client generates a third random string called the
Pre-Master Secret. It then encrypts this secret using the server's public key (from the certificate) and sends it to the server in aClientKeyExchangemessage. - Server decrypts the Pre-Master Secret: The server receives the encrypted message and uses its private key to decrypt it. Only the true owner of the certificate has the matching private key. If the decryption is successful, the server has proven its identity, and now both the client and server possess the same
Pre-Master Secret.
TLS Handshake - EVERYTHING that happens when you visit an HTTPS website
This next segment covers the core of the asymmetric key exchange.
Watch from 07:43 to 11:56. This explains how the server sends its certificate and, critically, how the client uses the server's public key to securely transmit the pre-master secret.
Step 3: Generating Symmetric Session Keys
The Pre-Master Secret itself is not used for encryption. Instead, both the client and server use it—along with the ClientRandom and ServerRandom from the Hello messages—to independently run the same algorithm and generate the exact same set of symmetric session keys.
These session keys (one for encrypting client-to-server traffic, one for server-to-client) are what will be used for the actual encryption of HTTP data.
TLS Handshake - EVERYTHING that happens when you visit an HTTPS website
This clip shows how the shared secrets are derived from the exchanged random values.
Watch from 11:56 to 17:39. The key concept here is that both sides can calculate the identical session keys without ever transmitting them over the network.
Step 4: Final Verification (ChangeCipherSpec, Finished)
The final step is for both parties to confirm that the handshake was successful and that they have the correct keys.
- The client sends a
ChangeCipherSpecmessage, indicating it will now start encrypting with the new session key. - It immediately follows with an encrypted
Finishedmessage. This message contains a hash of all the previous handshake messages. - The server decrypts the
Finishedmessage with its session key and verifies the hash. If it matches, it proves the client has the correct key and the handshake wasn't tampered with. - The server then does the exact same thing: sends its own
ChangeCipherSpecand encryptedFinishedmessage to the client. - The client decrypts and verifies.
Once this final verification is complete, the TLS handshake is over, and the secure tunnel is open. Encrypted application data can now flow freely.
TLS Handshake - EVERYTHING that happens when you visit an HTTPS website
This last segment shows the final verification step that completes the handshake.
Watch from 17:39 to 24:44. Understand that the Finished message is the ultimate proof that both parties have the correct keys and saw the exact same handshake conversation.
Conclusion
Congratulations on completing this deep dive into connection establishment and security! While the details are complex, the core ideas are elegant and powerful.
Key Takeaways:
- The TCP three-way handshake (SYN, SYN-ACK, ACK) creates a reliable, but unencrypted, connection by synchronizing sequence numbers between a client and server.
- TLS is a separate protocol that runs on top of TCP to provide encryption, authentication, and integrity. HTTPS is simply HTTP over a TLS-secured connection.
- The TLS handshake uses asymmetric cryptography (public/private keys) to authenticate the server and securely establish a shared
pre-master secret. - This secret is then used by both parties to independently generate identical symmetric session keys, which are used to efficiently encrypt the actual application data.
Preview of the Next Lesson:
We now understand how to find a server (DNS) and establish a secure connection to it (TCP/TLS). But what happens when things don't work? In our next lesson, we will get hands-on and learn how to troubleshoot network connectivity and name resolution using essential command-line tools like ping, traceroute, dig, and curl.