Introduction
Secure communication and real-time bidirectional data transfer are essential for modern web applications. TLS (Transport Layer Security) provides encryption and authentication, while WebSockets enable low-latency, full-duplex communication.This is part of Week 1 – Networking from Scratch in the “From CPU to the Browser” course.
TLS basics
TLS is a cryptographic protocol that provides secure communication over networks. It operates above TCP and below application protocols like HTTP.TLS handshake overview
The TLS handshake establishes a secure connection before any application data is transmitted.Client Hello
The client initiates the handshake by sending a Client Hello message containing:
- Supported TLS versions
- Supported cipher suites (encryption algorithms)
- Random data for key generation
- Supported compression methods
- Server Name Indication (SNI)
Server Hello
The server responds with:
- Selected TLS version
- Selected cipher suite
- Random data for key generation
- Session ID (for session resumption)
Certificate exchange
The server sends its digital certificate containing:
- Server’s public key
- Certificate issuer information
- Validity period
- Domain name(s) the certificate covers
Key exchange
The client:
- Verifies the server certificate
- Generates a pre-master secret
- Encrypts it with the server’s public key
- Sends it to the server
Change Cipher Spec
Both client and server send Change Cipher Spec messages to activate the newly negotiated encryption.
Modern TLS 1.3 simplifies the handshake to 1-RTT (one round-trip time) compared to 2-RTT in TLS 1.2, improving performance.
Certificate verification
Verifying the server’s certificate is crucial to prevent man-in-the-middle attacks.Check certificate validity period
Ensure the current date/time falls within the certificate’s valid date range (notBefore to notAfter).
Verify domain name
Check that the certificate’s Subject Alternative Name (SAN) or Common Name (CN) matches the domain being accessed.
Validate certificate chain
Verify the certificate chain up to a trusted root Certificate Authority (CA):
- Check each certificate’s signature using its issuer’s public key
- Ensure none of the certificates have been revoked
- Verify the chain leads to a trusted root CA
Cipher suites
A cipher suite defines the algorithms used for:- Key exchange (e.g., RSA, ECDHE)
- Authentication (e.g., RSA, ECDSA)
- Encryption (e.g., AES, ChaCha20)
- Message authentication (e.g., SHA256, SHA384)
WebSocket basics
WebSockets provide full-duplex communication channels over a single TCP connection, enabling real-time bidirectional data flow.WebSocket upgrade handshake
WebSockets start as an HTTP connection and upgrade to the WebSocket protocol.Client sends upgrade request
The client initiates the WebSocket connection with an HTTP upgrade request:Key headers:
Upgrade: websocket- Requests protocol upgradeConnection: Upgrade- Indicates connection upgradeSec-WebSocket-Key- Random base64-encoded value for securitySec-WebSocket-Version- WebSocket protocol version (13 is current)
Server accepts upgrade
If the server supports WebSockets, it responds with:Sec-WebSocket-Accept calculation:
- Concatenate the client’s Sec-WebSocket-Key with the magic string “258EAFA5-E914-47DA-95CA-C5AB0DC85B11”
- Compute SHA-1 hash of the result
- Base64 encode the hash
The Sec-WebSocket-Key/Accept mechanism prevents non-WebSocket servers from accidentally accepting WebSocket connections.
WebSocket frame handling
Once the connection is established, data is transmitted in frames.Frame structure
Each WebSocket frame has this binary structure:Frame components
FIN bit
FIN bit
Indicates if this is the final fragment of a message. For unfragmented messages, this is always 1.
Opcode (4 bits)
Opcode (4 bits)
Defines frame type:
0x0- Continuation frame0x1- Text frame (UTF-8 data)0x2- Binary frame0x8- Connection close0x9- Ping0xA- Pong
MASK bit
MASK bit
Indicates if the payload is masked. Client-to-server frames MUST be masked. Server-to-client frames MUST NOT be masked.
Payload length
Payload length
- If 0-125: Actual payload length
- If 126: Next 2 bytes contain the length
- If 127: Next 8 bytes contain the length
Masking key
Masking key
If MASK bit is set, a 4-byte masking key is used to XOR the payload data for security.
Simple frame handling
Read and unmask payload
Read the payload data. If masked, XOR each byte with the corresponding masking key byte:
Connection management
Ping/Pong
Ping frames keep the connection alive and detect broken connections. The receiver must respond with a Pong frame.
Close handshake
Either party can initiate closure by sending a Close frame with an optional status code. The other party responds with a Close frame, then both close the TCP connection.
Network debugging
Debugging TLS and WebSocket connections:tcpdump
Capture encrypted TLS traffic (you’ll see encrypted data):
Wireshark
Decrypt TLS traffic by providing the server’s private key or using SSLKEYLOGFILE. View WebSocket frames with protocol dissection.
Common issues
Certificate errors
Certificate errors
- Expired certificates
- Hostname mismatch
- Untrusted certificate authority
- Revoked certificates
WebSocket connection failures
WebSocket connection failures
- Missing Upgrade headers
- Incorrect Sec-WebSocket-Accept calculation
- Proxy or firewall blocking WebSocket traffic
- Protocol mismatch
Frame parsing errors
Frame parsing errors
- Incorrect masking
- Fragmentation handling issues
- Buffer overflows with large payloads
Next steps
With networking fundamentals mastered, you’re ready to move on to browser engine implementation where these protocols come to life.Browser Engine Implementation
Learn how to build HTML parsers, CSS engines, and layout systems (Weeks 2-3)