TLS Internals | Networking - Wyatt's Notes
flowchart TD A[Tls Internals] --> B[Key Concepts] A --> C[Core Principles] A --> D[Practical Applications] B --> E[Fundamental definitions] C --> F[Design patterns] D --> G[Real-world usage]Overview
Section titled “Overview”This document goes deeper into TLS internals than the TLS fundamentals document, covering the record Layer architecture, detailed handshake message formats for TLS 1.3, cipher suite construction, key Exchange mechanisms, and common implementation pitfalls. This is the material you need to understand When debugging TLS connections, configuring servers, or evaluating cryptographic strength.
TLS Architecture
Section titled “TLS Architecture”TLS is structured as a layered protocol with four sub-protocols operating over a reliable transport (TCP):
+-----------------------------------+| Application Data |+-----------------------------------+| Handshake Protocol |+-----------------------------------+| Change Cipher Spec |+-----------------------------------+| Alert Protocol |+-----------------------------------+| Record Layer |+-----------------------------------+| TCP |+-----------------------------------+Record Layer
Section titled “Record Layer”The TLS record layer fragments application data (and handshake messages) into records. Each record Has:
0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+| Content Type (8) | |+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Version (16) || +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+| | |+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Length (16) || +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+| | |+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+| Fragment (variable) |+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+Content types:
| Type | Value | Description |
|---|---|---|
| CHANGE_CIPHER_SPEC | 20 | Deprecated in TLS 1.3 (replaced by KeyUpdate) |
| ALERT | 21 | Error or warning notifications |
| HANDSHAKE | 22 | Handshake protocol messages |
| APPLICATION_DATA | 23 | Encrypted application data |
Handshake Protocol
Section titled “Handshake Protocol”The handshake protocol is responsible for authentication, key exchange, and negotiation of Cryptographic parameters. Handshake messages are carried inside TLS records with content type 22.
Alert Protocol
Section titled “Alert Protocol”Alert messages convey errors and state changes:
| Level | Description | Common Alerts |
|---|---|---|
| Warning (1) | Non-fatal; connection continues | close_notify, no_certificate, bad_certificate |
| Fatal (2) | Connection must be terminated | handshake_failure, decode_error, illegal_parameter |
Change Cipher Spec Protocol
Section titled “Change Cipher Spec Protocol”In TLS 1.2, this protocol signals the transition to encrypted communication. In TLS 1.3, it is Deprecated. Key changes are signaled within the handshake protocol itself.
TLS 1.2 vs TLS 1.3
Section titled “TLS 1.2 vs TLS 1.3”Removed in TLS 1.3
Section titled “Removed in TLS 1.3”| Feature | TLS 1.2 | TLS 1.3 | Reason |
|---|---|---|---|
| Renegotiation | Supported | Removed | Complex, caused RC4 injection attacks |
| Compression | Supported | Removed | CRIME attack (compression oracle) |
| Static RSA key exchange | Supported | Removed | No forward secrecy |
| Non-AEAD ciphers | Supported | Removed | CBC ciphers vulnerable to padding oracles |
| Custom DHE groups | Supported | Removed | Weak groups (e.g., export-grade) |
| SHA-1 in signatures | Supported | Removed | SHA-1 is cryptographically weak |
| MD5 in signatures | Supported | Removed | MD5 is broken |
Added in TLS 1.3
Section titled “Added in TLS 1.3”| Feature | Description |
|---|---|
| 0-RTT data | Send application data in the first flight (repeat connections) |
| Signature algorithms | Explicit negotiation of hash+signature pairs (RFC 8446 Section 4.2.3) |
| Key schedule | Derived key hierarchy using HKDF (RFC 5869) |
| Post-handshake auth | Server can request client certificate after the handshake |
| Encrypted Server Hello | Server Hello is encrypted (hides server identity from observers) |
| KeyUpdate | In-band key rotation without renegotiation |
Cipher Suite Simplification
Section titled “Cipher Suite Simplification”TLS 1.2 cipher suites are complex strings like TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256. TLS 1.3 Cipher suites only specify the AEAD algorithm:
| TLS 1.3 Cipher Suite | AEAD Algorithm | Hash (HKDF) |
|---|---|---|
| TLS_AES_128_GCM_SHA256 | AES-128-GCM | SHA-256 |
| TLS_AES_256_GCM_SHA384 | AES-256-GCM | SHA-384 |
| TLS_CHACHA20_POLY1305_SHA256 | ChaCha20-Poly1305 | SHA-256 |
| TLS_AES_128_CCM_SHA256 | AES-128-CCM | SHA-256 |
| TLS_AES_128_CCM_8_SHA256 | AES-128-CCM-8 | SHA-256 |
The key exchange algorithm is no longer part of the cipher suite. It is negotiated separately via The supported_groups extension.
TLS 1.3 Handshake in Detail
Section titled “TLS 1.3 Handshake in Detail”Full Handshake (1-RTT)
Section titled “Full Handshake (1-RTT)”Client Server | | |--- ClientHello -------------------------------->| Flight 1 | supported_versions, supported_groups, | | signature_algorithms, key_share | | | |<-- ServerHello ---------------------------------| Flight 2 | selected_version, selected_group, | | key_share | |<-- EncryptedExtensions ------------------------| |<-- Certificate ---------------------------------| |<-- CertificateVerify ---------------------------| |<-- Finished ------------------------------------| | | |--- Finished ----------------------------------->| Flight 3 | | |==== Application Data =========================|ClientHello Fields
Section titled “ClientHello Fields”The ClientHello carries the client”s capabilities and parameters:
| Field | Description |
|---|---|
| legacy_version | 0x0303 (TLS 1.2) for compatibility with middleboxes |
| random | 32 bytes of random (used in key derivation) |
| legacy_session_id | Session ID for compatibility (TLS 1.3 uses PSK) |
| cipher_suites | List of supported TLS 1.3 cipher suites |
| legacy_compression_methods | [0x00] (no compression) |
| extensions | supported_versions, supported_groups, key_share, |
| signature_algorithms, psk_key_exchange_modes, | |
| server_name (SNI), etc. |
ServerHello Fields
Section titled “ServerHello Fields”| Field | Description |
|---|---|
| legacy_version | 0x0303 (always, even for TLS 1.3) |
| random | 32 bytes of random |
| legacy_session_id_echo | Echo of client’s session_id |
| cipher_suite | Selected cipher suite |
| legacy_compression_method | 0x00 |
| extensions | supported_version (TLS 1.3), key_share, |
| pre_shared_key (if PSK selected) |
EncryptedExtensions
Section titled “EncryptedExtensions”After the ServerHello, all subsequent handshake messages are encrypted. EncryptedExtensions carries Server-side configuration that does not affect the cryptographic parameters:
server_nameindication (whether SNI was used)max_fragment_length(negotiate smaller records)application_layer_protocol_negotiation(ALPN)early_data(whether 0-RTT is accepted)
Certificate
Section titled “Certificate”The server sends its certificate chain. In TLS 1.3, the Certificate message is sent encrypted. The Certificate chain includes:
- Leaf certificate: The server’s end-entity certificate
- Intermediate certificates: One or more intermediate CA certificates
- Root certificate: NOT included (the client must already trust it)
CertificateVerify
Section titled “CertificateVerify”This message proves that the server holds the private key corresponding to the certificate’s public Key. It contains a digital signature over a transcript hash of all handshake messages so far.
Signature = Sign(private_key, Hash("TLS 1.3, server CertificateVerify" || 0x20...0x20 || transcript_hash))The 0x20...0x20 is 64 bytes of spaces (0x20), a context string that binds the signature to TLS 1.3 Specifically.
Finished
Section titled “Finished”Both sides send a Finished message, which contains a verify_data value derived from the handshake Transcript:
verify_data = HMAC(finished_key, Hash(transcript))The Finished message is the first message encrypted with the newly derived traffic keys. If the Verify_data does not match, the handshake has been tampered with and the connection is terminated.
Key Exchange Mechanisms
Section titled “Key Exchange Mechanisms”ECDHE (Elliptic Curve Diffie-Hellman Ephemeral)
Section titled “ECDHE (Elliptic Curve Diffie-Hellman Ephemeral)”The most widely used key exchange in TLS 1.3. Both sides generate an ephemeral (temporary) key pair On an elliptic curve, exchange public keys, and derive a shared secret.
Client generates: (priv_c, pub_c)Server generates: (priv_s, pub_s)
Shared secret = ECDH(priv_c, pub_s) = ECDH(priv_s, pub_c) = x-coordinate of (priv_c * pub_s)Supported curves (RFC 8446):
| Curve | Key Size | Security Level |
|---|---|---|
| X25519 | 256 bits | 128 bits |
| secp256r1 (P-256) | 256 bits | 128 bits |
| secp384r1 (P-384) | 384 bits | 192 bits |
| secp521r1 (P-521) | 521 bits | 256 bits |
X25519 is the recommended default. It is faster than NIST curves, has simpler implementation (fewer Edge cases), and uses a constant-time algorithm that is resistant to timing attacks.
DHE (Finite Field Diffie-Hellman Ephemeral)
Section titled “DHE (Finite Field Diffie-Hellman Ephemeral)”Traditional Diffie-Hellman over a finite field. Slower than ECDHE for equivalent security levels. Supported groups:
| Group (ffdhe) | Prime Size | Security Level |
|---|---|---|
| ffdhe2048 | 2048 bits | 112 bits |
| ffdhe3072 | 3072 bits | 128 bits |
| ffdhe4096 | 4096 bits | 150 bits |
| ffdhe6144 | 6144 bits | 175 bits |
| ffdhe8192 | 8192 bits | 200+ bits |
PSK (Pre-Shared Key)
Section titled “PSK (Pre-Shared Key)”TLS 1.3 supports PSK-based key exchange, which can be used alone or combined with (EC)DHE (called “PSK with (EC)DHE” or “psk_dhe_ke”).
| PSK Mode | Forward Secrecy | Use Case |
|---|---|---|
| PSK only | No | IoT devices, resumption tickets |
| PSK + (EC)DHE | Yes | Recommended for resumption (security) |
PSKs are established either externally (configured on both sides) or via a previous TLS handshake (session resumption via NewSessionTicket).
PSK Key Exchange Modes
Section titled “PSK Key Exchange Modes”The client indicates which PSK modes it supports in the psk_key_exchange_modes extension:
psk_ke: PSK-only key establishment (no forward secrecy)psk_dhe_ke: PSK combined with (EC)DHE (forward secrecy maintained)
Intuition
Section titled “Intuition”TLS is like a secure diplomatic pouch system. The handshake is like two ambassadors meeting, verifying each other’s credentials (certificates), and agreeing on a secret code (shared secret) for their correspondence. The record layer is like the pouch itself - it wraps messages so eavesdroppers cannot read them and tamperers cannot modify them. TLS 1.3 simplified the handshake from two round trips to one, like speeding up the credential verification process. The key insight is that TLS provides confidentiality (encryption), integrity (MAC), and authentication (certificates) - the three pillars of secure communication. Perfect forward secrecy ensures that even if a server’s private key is compromised later, past sessions remain secure.
Cross-References
Section titled “Cross-References”- TLS - Overview of TLS protocol versions and deployment best practices
- HTTP - How HTTPS combines HTTP with TLS for secure web communication
- Network Tools - Tools for testing TLS certificate configuration and cipher suites