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.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
- Supported TLS versions
- Supported cipher suites (encryption algorithms)
- Random data for key generation
- Supported compression methods
- Server Name Indication (SNI)
Server Hello
- Selected TLS version
- Selected cipher suite
- Random data for key generation
- Session ID (for session resumption)
Certificate exchange
- Server’s public key
- Certificate issuer information
- Validity period
- Domain name(s) the certificate covers
Server Hello Done
Key exchange
- 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
Finished messages
Certificate verification
Verifying the server’s certificate is crucial to prevent man-in-the-middle attacks.Check certificate validity period
Verify domain name
Validate certificate chain
- 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
Check revocation status
- Certificate Revocation Lists (CRL)
- Online Certificate Status Protocol (OCSP)
- OCSP Stapling (server provides OCSP response)
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
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
- 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
Connection established
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
Opcode (4 bits)
Opcode (4 bits)
0x0- Continuation frame0x1- Text frame (UTF-8 data)0x2- Binary frame0x8- Connection close0x9- Ping0xA- Pong
MASK bit
MASK bit
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
Simple frame handling
Read frame header
Read extended length (if needed)
Read masking key (if present)
Read and unmask payload
Process based on opcode
- Text/Binary: Deliver the message to the application
- Ping: Respond with a Pong frame
- Pong: Update connection health status
- Close: Initiate connection closure
Connection management
Ping/Pong
Close handshake
Network debugging
Debugging TLS and WebSocket connections:tcpdump
Wireshark
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