Handshakes, Explained: TCP, TLS and Wi-Fi
If you are reading this, you have probably seen "SYN, SYN-ACK, ACK" in a networking course, "TLS handshake" in a browser error, or "captured the handshake" in a Wi-Fi write-up, and you want to know what these really are and why they matter for security.
By the end you will be able to explain why computers do a handshake at all, walk through the TCP three-way handshake and the TLS handshake message by message, say what the WPA2 four-way handshake does, read a handshake in Wireshark or curl -v, and name the attacks and defences that sit around each one. It builds on Computer and Networking Basics, so read that first if "port" or "segment" is new.
1. What a handshake is
Before two computers can exchange useful data, they usually need to agree on a few things: Are you there? Can you hear me? What numbers will we use to keep track of the data? How will we protect it? A handshake is the short exchange of messages at the start of a connection that settles those questions.
It is like the start of a phone call:
"Hello?" → "Hi, it's me." → "Great, go ahead."
Nothing useful has been said yet, but both sides now know the other is there and ready.
Several different handshakes exist, each at a different layer and each with a different job:
| Handshake | Layer | What it sets up | Where you meet it |
|---|---|---|---|
| TCP three-way handshake | Transport | A reliable connection, and the starting sequence numbers for both directions | Every web page, email, SSH session, file transfer |
| TLS handshake | Runs on top of TCP | Encryption keys, and proof of who the server is | Every HTTPS address |
| WPA2 four-way handshake | Wi-Fi link | Fresh encryption keys for one device on one Wi-Fi network | Joining a home or office Wi-Fi network |
They stack. Opening https://example.com does a TCP handshake first, then a TLS handshake on that connection, and only then sends the HTTP request. This post takes them in that order.
2. The TCP three-way handshake
TCP is the protocol that gives applications a reliable, ordered stream of bytes (see TCP and UDP). To do that, each side numbers the bytes it sends so the other side can confirm receipt and put them back in order. The handshake is how the two sides announce where their numbering starts.
Two ideas you need first
Flags. Every TCP segment has a header with single-bit flags. The ones that matter here:
| Flag | Name | Meaning |
|---|---|---|
| SYN | Synchronize | "Here is my starting sequence number." Used to open a connection |
| ACK | Acknowledgement | "I have received everything up to this number." Set on almost every segment after the first |
| FIN | Finish | "I have no more data to send." Used to close politely |
| RST | Reset | "Stop. This connection is not valid." Used to abort or to refuse |
Sequence numbers. TCP numbers bytes, not packets. A segment carries the sequence number of its first byte. The receiver replies with an acknowledgement number: the sequence number of the next byte it expects. A SYN counts as one position in the numbering, even though it carries no data, so it is acknowledged like a byte.
The three steps
The standard, RFC 9293, describes it with a client (Peer A) starting at sequence number 100. Here is the same example, with a server number added:
Client Server
(CLOSED) (LISTEN)
1. SYN seq=100 ---->
(SYN-SENT) (SYN-RECEIVED)
2. <---- SYN, ACK seq=300 ack=101
3. ACK seq=101 ack=301 ---->
(ESTABLISHED) (ESTABLISHED)
- SYN. The client picks a starting number and sends a segment with SYN set: "I want to open a connection, and my numbering starts at 100."
- SYN-ACK. The server, which has been listening on that port, picks its own starting number (300 here) and replies with SYN and ACK both set. The ACK says "I got your SYN; the next byte I expect from you is 101." The SYN says "my numbering starts at 300."
- ACK. The client acknowledges the server's SYN: "the next byte I expect from you is 301." This segment carries no data, so it does not use up a sequence number. The client may start sending data right after it.
Both sides are now in the ESTABLISHED state and data can flow in both directions. The cost is one round trip (the time for a message to go to the server and the reply to come back) before the first byte of real data is sent.
Why three messages, not two?
RFC 9293 gives the reason in one sentence: "The principal reason for the three-way handshake is to prevent old duplicate connection initiations from causing confusion." A SYN that was delayed in the network could arrive long after the client gave up on it. With only two messages, the server would think a new connection had been requested. With the third message, the client can answer "no, I did not ask for that" (with a reset), and the server knows the connection is genuine. The exchange also lets each side confirm that the other can receive its messages, and each side's starting number is acknowledged by the other.
Where do the starting numbers come from?
They are not simply 0 or 1. RFC 9293 says an implementation should generate its initial sequence numbers (ISNs) from a clock plus a pseudorandom function of the connection's addresses, ports and a secret key. Two reasons matter for security: it avoids mixing up segments from an old connection that used the same addresses and ports, and it makes it hard for a stranger who cannot see the traffic to guess a valid number and inject packets into the connection.
What else is negotiated
The SYN and SYN-ACK segments can also carry options. Common ones are the maximum segment size (the largest chunk each side will accept), window scaling (to allow large amounts of data in flight on fast links), selective acknowledgement (SACK) and timestamps. RFC 9293 requires every implementation to support the maximum segment size option; it describes SACK, timestamps and window scaling (defined in separate standards) as recommended for high performance.
The states, as you will see them on a real machine
Commands such as ss -tan (Linux) or netstat -an show the state of each connection. During the handshake and shutdown you may see:
| State | Meaning |
|---|---|
LISTEN |
A server program is waiting for connections on this port |
SYN-SENT |
This machine sent a SYN and is waiting for the SYN-ACK |
SYN-RECEIVED (SYN-RECV) |
This machine received a SYN, sent a SYN-ACK and is waiting for the final ACK |
ESTABLISHED |
The handshake is complete; data can flow |
FIN-WAIT, CLOSE-WAIT, LAST-ACK, TIME-WAIT |
Stages of closing (next section) |
What happens when the port is closed
If a SYN arrives for a port where nothing is listening, RFC 9293 says a reset is sent in response. The connecting side sees "connection refused" immediately. If a firewall silently drops the SYN instead, nothing comes back and the client keeps retrying until it times out. This difference is how scanners tell a closed port from a filtered one.
3. Closing a connection
A TCP connection can end in two ways, and the RFC names both: the normal close using a FIN handshake, or an abort using one or more RST segments.
The polite close. Each direction is closed separately. One side sends FIN ("I am done sending"); the other acknowledges it. Later the other side sends its own FIN, which is acknowledged. That is usually written as four segments (FIN, ACK, FIN, ACK), although in practice the middle two are often sent as a single segment when the second side closes at once.
TIME-WAIT. The side that closes first lingers in a state called TIME-WAIT. RFC 9293 requires it to stay there for twice the maximum segment lifetime (2×MSL). The wait is there mainly so that delayed segments from the old connection cannot be mistaken for part of a new one, and it also leaves time to re-send the final ACK if it was lost. Seeing many TIME-WAIT entries on a busy server is usually normal.
The abort. A RST ends the connection at once, with no polite exchange. It is sent when a segment arrives for a connection that does not exist, when an application aborts the connection, or when something else (a firewall, or an attacker) wants to tear the connection down.
4. Seeing the TCP handshake yourself
The best way to make this real is to watch it.
In Wireshark (a free packet analyser; download it from wireshark.org, whose Windows installer includes Npcap, the component Windows needs for live capture), start a capture and open any website. Then type this in the display filter bar:
tcp.flags.syn == 1
You will see the SYN from your machine and the SYN-ACK from the server. Click a packet and open the Transmission Control Protocol section to see the flags, the sequence number, and the options (maximum segment size and others). Wireshark shows relative sequence numbers by default, so the first one reads 0; the real value is in the raw bytes.
With a port scanner. nmap is the standard open-source scanner (the Nmap site provides a Windows installer that includes Npcap and the Zenmap graphical interface, and a macOS disk image; on Linux install it with your package manager, as shown in Linux Fundamentals), to be used only on systems you own or have written permission to test. Its default scan for privileged users is a SYN scan (-sS): it sends a SYN and looks at the answer. A SYN-ACK means the port is open (the handshake is never completed, which is why Nmap's documentation calls it half-open); a RST means closed; no answer after retries, or an ICMP unreachable error, means filtered. The connect scan (-sT) completes the full handshake instead, using the operating system's normal connection call.
5. Attacks and defences around the TCP handshake
SYN flood
Remember that when the server receives a SYN, it must remember the half-finished connection until the final ACK arrives. RFC 4987 describes the attack: an attacker sends a large number of SYN segments, often with forged source addresses, and never completes the handshake. The server's table of half-open connections (the "backlog") fills with bogus entries, and real users can no longer connect. It is a denial-of-service attack: it attacks availability in the CIA triad.
Defences:
| Defence | How it helps |
|---|---|
| SYN cookies | The server stops keeping state for the half-open connection. It encodes the needed information in the sequence number of its SYN-ACK and rebuilds it from the final ACK. RFC 4987 notes the trade-offs: it can conflict with some TCP options and some protocols |
| A larger backlog and shorter timeouts | Gives more room and clears bogus entries faster |
| Rate limiting and filtering at the network edge | Drops obvious floods before they reach the server |
| Upstream DDoS protection (a provider that absorbs and filters traffic) | Handles floods bigger than one server's link |
Scanning and reconnaissance
Scanners use the handshake's behaviour to learn which ports are open (see Reconnaissance). Defences are to expose only the ports you need, use a firewall that drops traffic you do not expect, and alert on one source touching many ports.
Guessing sequence numbers
If an attacker could predict sequence numbers, they could inject data into someone else's connection. This is why the standard asks for unpredictable initial sequence numbers, and why modern operating systems use them.
6. The TLS handshake
TCP gives a reliable connection, but it is not private: anyone on the path can read it. TLS (Transport Layer Security) adds privacy on top. As covered in the basics post, it provides encryption, integrity and authentication of the server. The TLS handshake is where that is set up. (SSL is the older name; the old SSL versions are obsolete, and "SSL certificate" is just a leftover phrase.)
Which versions exist
| Version | Standard | Status |
|---|---|---|
| TLS 1.0 and 1.1 | Older | Formally deprecated by RFC 8996 (March 2021), which says they must not be negotiated |
| TLS 1.2 | RFC 5246 (2008) | Still widely used |
| TLS 1.3 | RFC 8446 (2018) | The current version, preferred wherever both sides support it |
What the handshake has to achieve
- Agree on the version and the algorithms to use.
- Authenticate the server: prove the server really is the website named in the address, by showing a certificate and proving it holds the matching private key.
- Create shared secret keys that only the two sides know, even though everything so far was sent in the open.
Step 3 sounds impossible, and it is the clever part. Both sides use ephemeral Diffie-Hellman key exchange (in modern TLS, usually the elliptic-curve version, written ECDHE). Each side makes a temporary secret number, sends only a derived public value, and combines the other side's public value with its own secret number. Both arrive at the same shared secret, while an eavesdropper who sees only the public values cannot work it out. Because the secret numbers are temporary and thrown away, recording today's traffic and later stealing the server's long-term key still does not let you decrypt it. That property is called forward secrecy.
The TLS 1.3 handshake, message by message
RFC 8446 describes it as one round trip. Here is the flow:
Client Server
ClientHello
+ key_share, supported_versions,
signature_algorithms, server_name ... ---->
ServerHello
+ key_share, supported_versions
{EncryptedExtensions}
{Certificate}
{CertificateVerify}
<---- {Finished}
{Finished} ---->
[Application data] <---> [Application data]
{ } = encrypted with handshake keys [ ] = encrypted with application keys
- ClientHello. The client says which TLS versions it supports (in the
supported_versionsextension), which cipher suites it will accept, which signature algorithms it will accept for certificates (signature_algorithms), the name of the site it wants (server_name, called SNI), and, crucially, its key share: its public value for the key exchange, sent in advance for the group it guesses the server will pick. TLS 1.3 sends it in the first message so the whole thing takes one round trip. (If the server wants a different group, it asks the client to try again, which costs an extra round trip.) - ServerHello. The server picks the version, the cipher suite and the key exchange group, and sends its key share. At this moment both sides can compute the shared secret. RFC 8446 states that everything after this phase is encrypted, so the rest of the server's reply is already protected.
- EncryptedExtensions. Further settings, now encrypted. One example is the agreed application protocol (negotiated with the ALPN extension), such as HTTP/2.
- Certificate. The server sends its certificate chain: its own certificate plus intermediates. The browser checks that the chain leads to a certificate authority it trusts, that the dates are valid, and that the name in the certificate matches the website address.
- CertificateVerify. A certificate is public, so anyone could copy it. To prove that this server holds the matching private key, it signs a hash of the handshake so far. The client verifies the signature with the public key from the certificate. This is the step that actually authenticates the server.
- Finished. A message authentication code over the whole handshake. If an attacker had tampered with any earlier message, the codes would not match. Each side sends one; when both check out, the handshake is complete.
After the client's Finished, application data (the HTTP request and response) flows, encrypted with fresh application keys.
Cipher suites in TLS 1.3 are short. They name only the symmetric encryption and the hash, for example TLS_AES_128_GCM_SHA256, TLS_AES_256_GCM_SHA384 and TLS_CHACHA20_POLY1305_SHA256. All use authenticated encryption, and the key exchange and signature choices are negotiated separately.
The TLS 1.2 handshake
RFC 5246 defines a longer flow, which takes two round trips:
| Step | Sender | Message |
|---|---|---|
| 1 | Client | ClientHello (versions, cipher suites, a random value) |
| 2 | Server | ServerHello, Certificate, ServerKeyExchange (when needed), ServerHelloDone |
| 3 | Client | ClientKeyExchange, ChangeCipherSpec, Finished |
| 4 | Server | ChangeCipherSpec, Finished |
In 1.2, the certificate and most messages travel unencrypted; encryption starts only at ChangeCipherSpec. The two sides combine the exchanged random values and a pre-master secret into a 48-byte master secret, from which the session keys are derived. Depending on the cipher suite, the pre-master secret comes from a Diffie-Hellman exchange (with forward secrecy) or is encrypted to the server's RSA key (without it).
TLS 1.2 against TLS 1.3
| TLS 1.2 | TLS 1.3 | |
|---|---|---|
| Round trips before data | 2 | 1 |
| Certificate visible to an observer | Yes (sent in the clear) | No (encrypted) |
| Forward secrecy | Only if an ephemeral key exchange is chosen | Yes in the normal handshake: static RSA and static Diffie-Hellman key exchange were removed |
| Cipher suites | Many, including old and weak ones | A short list, all authenticated encryption |
| Downgrade protection | Needs add-ons, such as a fallback signalling value | Built in: a server that was pushed to an older version signals it in its random value |
What an observer can still see
Even with TLS 1.3, someone watching the network can see the IP address you connect to, how much data is sent and when. The site name in the SNI field is also sent in the ClientHello, before encryption begins; a newer extension, Encrypted Client Hello (standardised in RFC 9849 in March 2026), is designed to hide it, along with the ALPN list, but it is only deployed on some sites so far. So HTTPS hides what you read and send, not that you visited the site.
Resuming and 0-RTT
Setting up a new connection costs a round trip, so TLS can resume a previous session using a pre-shared key from an earlier handshake. TLS 1.3 also allows 0-RTT early data: the client sends its first request in the very first flight. The cost is stated clearly in the standard: that data is not forward secret, and the protection against replay is weaker, so a server should accept 0-RTT only for requests that are safe to repeat (such as a plain page fetch), not ones that change something.
Where QUIC fits
HTTP/3 runs over QUIC (on UDP). QUIC does not use a separate TCP handshake and TLS handshake: it builds the TLS 1.3 handshake into its own connection set-up, so a new connection needs fewer round trips in total.
Seeing it yourself
Run this from a terminal (-v means verbose):
curl -v https://example.com
(macOS includes curl, and so do Windows 10 and 11. In Windows PowerShell type curl.exe, because there curl is an alias for a different command, Invoke-WebRequest.)
Among the output you will see the negotiated version and cipher (for example SSL connection using TLSv1.3 / …), the protocol the server accepted (ALPN: server accepted h2) and the certificate's subject, issuer and expiry date. Some builds of curl, depending on the TLS library they use, also print each handshake message, such as TLSv1.3 (OUT), TLS handshake, Client hello (1). The exact wording varies by version. To check which versions a server accepts, you can use openssl s_client -connect example.com:443 -tls1_2 and the same with -tls1_3. In Wireshark, filter with tls.handshake to see the ClientHello and ServerHello; in a TLS 1.3 capture the later messages show up as encrypted "Application Data" because they are.
What goes wrong, and what to do
| Problem | What it means | What to do |
|---|---|---|
| Certificate expired or name mismatch | The server's certificate is out of date or is for a different name | As a user, do not click through the warning. As an admin, renew it, or fix the name on the certificate |
| Untrusted issuer | The chain does not lead to a trusted authority | Install the missing intermediate certificate on the server; on a company network, a security proxy may be re-signing traffic |
| Protocol or cipher mismatch | The client and server share no version or suite | Enable TLS 1.2 and 1.3 on the server; do not turn old versions back on |
| Old versions still enabled | Exposes the server to known weaknesses and downgrade attempts | Disable TLS 1.0 and 1.1 (RFC 8996), and old SSL versions |
| Someone intercepting the connection | A man-in-the-middle, or a company proxy, presents a different certificate | Look at the certificate's issuer and chain and compare with what you see from another network or device. A different issuer alone does not prove interception, because sites change certificate authorities and many networks use security proxies; it is a reason to investigate, not a verdict. Do not click through the warning |
7. The WPA2 four-way handshake (Wi-Fi)
When you join a Wi-Fi network protected by a password (WPA2-Personal), your device and the access point already share a long-term secret derived from that password, called the pairwise master key (PMK). But using the same key for everything would be unsafe, so they run a four-way handshake to create fresh keys for this session.
In outline:
- The access point sends a random number, the ANonce.
- The device sends its own random number, the SNonce, plus a code proving it knows the PMK.
- The access point confirms and installs the keys.
- The device confirms, and encrypted traffic begins.
Both sides compute a pairwise transient key (PTK) from the PMK, the two nonces and the two devices' MAC addresses. Because the nonces are new each time, every session gets different keys. The password itself is never sent.
The security catch. Anyone within radio range can record the handshake messages when a device joins. The Aircrack-ng documentation explains that the handshake is what gives an attacker the information to start an attack: they can then try passphrases from a word list offline, on their own computer, with no lockout and no rate limit. The attack only works if the passphrase is in the list being tried, so a weak or common Wi-Fi password can be recovered from a recorded handshake and a long random one realistically cannot. This is what "capturing the handshake" means in Wi-Fi security write-ups. Do this only on networks you own or have written permission to test; doing it to anyone else's network is illegal in most places.
Defences:
| Defence | Why it helps |
|---|---|
| A long, random passphrase | Makes guessing impractical even with the handshake |
| WPA3-Personal | It replaces the pre-shared-key exchange with SAE (Simultaneous Authentication of Equals), which is based on the Dragonfly key exchange. RFC 7664 describes Dragonfly as resistant to active attack, passive attack and offline dictionary attack. The Wi-Fi Alliance says WPA3-Personal gives increased protection from password guessing attempts |
| WPA2/WPA3-Enterprise (802.1X) | Each user has their own credentials instead of one shared password |
8. Quick reference
| Handshake | Messages | Result |
|---|---|---|
| TCP | SYN → SYN-ACK → ACK | A connection with agreed starting sequence numbers |
| TLS 1.3 | ClientHello → ServerHello + encrypted Certificate, CertificateVerify, Finished → Finished | Server authenticated, shared keys, in 1 round trip |
| TLS 1.2 | ClientHello → ServerHello, Certificate, ServerHelloDone → ClientKeyExchange, ChangeCipherSpec, Finished → ChangeCipherSpec, Finished | The same result in 2 round trips |
| WPA2 four-way | ANonce → SNonce + proof → confirmation → confirmation | Fresh session keys for one Wi-Fi device |
9. Common mistakes
| Myth | The facts |
|---|---|
| "The TCP handshake proves who the other side is" | It only proves the other side can receive messages at the address it claimed. It does not verify identity. That is TLS's job |
| "A completed handshake means the connection is secure" | A TCP handshake alone is not encrypted. Only a TLS handshake adds encryption |
| "HTTPS hides everything" | It hides the content. The address, timing, amount of data and (usually) the site name are still visible |
| "A padlock means the site is safe" | It means the connection is encrypted to that site. A phishing site can have a valid certificate too (see Phishing) |
| "TLS 1.2 is broken" | It is still acceptable when configured properly with forward-secret cipher suites. TLS 1.3 is simply faster and removes the weak options |
| "SYN scans are invisible" | They never complete the connection, but firewalls and intrusion-detection systems log and detect them |
| "0-RTT is just a free speed-up" | It trades away replay protection, so it is only for requests that are safe to repeat |
Closing
A handshake is a short agreement at the start of a connection. TCP's three messages set up a reliable connection with numbered bytes. TLS builds on top of that connection to prove who the server is and to create shared keys, in one round trip in version 1.3. Wi-Fi's four-way handshake gives each device fresh keys, and it is also the reason a weak Wi-Fi password can be guessed offline.
Keep these points:
- TCP: SYN, SYN-ACK, ACK. One round trip, and both sides agree where their numbering starts.
- The TCP handshake is not encrypted and does not prove identity.
- A SYN flood fills the server's half-open table; SYN cookies are the classic answer.
- TLS 1.3 needs one round trip and encrypts the certificate; TLS 1.2 needs two. TLS 1.0 and 1.1 are deprecated.
- Forward secrecy comes from temporary key exchange, so a stolen server key does not unlock old recordings.
- A recorded WPA2 handshake allows offline password guessing, so use a long random passphrase or WPA3.