Lesson 3 of 9 · 23m
Handshakes, Capabilities, and ALPN
Design opening exchanges that establish identity, protocol selection, feature support, security parameters, and failure behavior without guessing.
A handshake is not ceremony. It is the only moment when two endpoints get to agree on what the connection means before bytes are interpreted under the wrong assumptions. Bad handshakes create version deadlock, downgrade attacks, silent feature loss, and middlebox confusion. Good ones are small, explicit, authenticated where necessary, and boring enough to debug at 2 a.m.
Decide what must be agreed before data flows
At minimum, define protocol identity, version or variant, roles, cryptographic parameters if applicable, and enough feature negotiation to interpret the first message. TLS solves channel security, not application semantics. ALPN identifies the application protocol inside TLS:
openssl s_client -connect api.example.net:443 -alpn h2,http/1.1If a server selects h2, HTTP/2 framing rules apply. If it selects http/1.1, completely different parsing rules apply. That is exactly the point: negotiated identity determines parser selection.
Do not negotiate by probing undefined behavior
This pattern looks clever and ages terribly:
conn.sendall(b"HELLO 2
")
if conn.recv(2) == b"OK":
use_v2()
else:
use_v1()What does silence mean? What does a syntax error mean? Can an intermediary strip 2? Is the server negotiating or merely echoing? Design explicit response types and error codes instead:
client: HELLO versions=[2,3] features=[compression,resumption]
server: SELECT version=2 features=[compression]Both sides can now compute the intersection once and store it in connection state.
Downgrade resistance must be intentional
If version or algorithm negotiation happens before authentication, an active attacker can weaken it. TLS transcript binding is the mature model: the exact handshake bytes are incorporated into authentication. In custom protocols, sign or MAC the negotiated parameter set once keys exist. If you cannot do that, at least make unsupported versions fatal rather than silently retrying forever.
Capabilities beat version arithmetic
Versions identify batches of behavior, but capabilities describe what is actually available:
version=3 capabilities=stream-reset,partial-reply,server-pushA peer can implement version 3 without server push. A deployment may disable compression for CPU or security reasons. Treat capabilities as a bitset or string registry with explicit unknown behavior.
Timeouts are part of the handshake
A handshake that waits forever is a resource leak. Bound each phase, not just the whole exchange: TCP connect, TLS negotiation, application hello, authentication, and first data. Log which phase timed out. "Connection failed" is operationally useless.
Field notes
- Never send unauthenticated plaintext that is parsed with the richest feature set before negotiation completes.
- Middleboxes often tolerate a few known patterns and destroy everything else; test through realistic proxies, not only localhost.
- Retry after a failed negotiation should not downgrade unless the threat model explicitly permits it.
- Capture the selected version and capability set in metrics labels; rollout failures hide in aggregate connection counts.
Was this lesson helpful?