HTTPS and TLS 1.3 Handshake: Keys, Certificates, and RTT in Practice
HTTPS and TLS 1.3 Handshake: Keys, Certificates, and RTT in Practice

HTTPS and TLS 1.3 Handshake: Keys, Certificates, and RTT in Practice

HTTPS is fundamentally more than “HTTP over an encrypted channel.” It is the cornerstone of modern network security architecture. In TLS 1.3, the handshake is a heavily optimized state machine designed to minimize round-trips while maximizing cryptographic resilience. It rigorously negotiates cryptographic parameters, computes ephemeral shared secrets via elliptic curves, authenticates the server’s X.509 identity, and verifies the transcript integrity. Below, we dissect TLS 1.3 at an extreme engineering depth—ranging from Galois field mathematics to OpenSSL’s C implementation and eBPF kernel-level profiling.

1. The Architecture of TLS 1.3: 1-RTT and State Machine Overhaul

TLS 1.3 drastically reduced the handshake latency from 2-RTT to 1-RTT by aggressively deprecating obsolete primitives (RSA key transport, SHA-1) and enforcing Forward Secrecy (FS). In the OpenSSL C implementation, this is driven by the state machine inside statem_clnt.c and statem_srvr.c. A client sends a ClientHello containing pre-computed key_share extensions. The server immediately transitions to TLS_ST_SW_SRVR_HELLO, computing the shared secret, and responds with a ServerHello, EncryptedExtensions, Certificate, CertificateVerify, and Finished messages in a single flight.

TLS 1.3 message flights in a one round-trip teaching model
With modeled RTT set to 36 ms, the client sends the application request at exactly 36 ms, optimizing the physical limit of latency.

2. Advanced State Machine & HKDF Visualization

The following diagram expands the 1-RTT flow to highlight the exact stages where the HMAC-based Extract-and-Expand Key Derivation Function (HKDF) is applied to generate the Handshake and Application Traffic Secrets.


sequenceDiagram
    autonumber
    participant Client
    participant Server
    
    Client->>Server: ClientHello + Key Share (X25519) + ALPN
    Note right of Server: statem_srvr.c: tls_process_client_hello()
HKDF-Extract(0, ECDHE Shared Secret)
HKDF-Expand(Handshake Secret) Server->>Client: ServerHello + Key Share Server->>Client: {EncryptedExtensions + Certificate + CertVerify + Finished} Note left of Client: statem_clnt.c: tls_process_server_hello()
Derive keys, Verify Signature, Verify Finished MAC Client->>Server: {Finished} + [Application Data (HTTP Request)] Note right of Server: HKDF-Expand(Master Secret) -> Application Traffic Keys Server->>Client: [Application Data (HTTP Response)]

3. Mathematical Rigor: ECDLP and AES-GCM Polynomials

Modern TLS 1.3 deployments rely heavily on Curve25519 for the key exchange and AES-GCM for Authenticated Encryption with Associated Data (AEAD).

Elliptic Curve Discrete Logarithm Problem (ECDLP)

The key exchange leverages the Curve25519 Montgomery curve defined by the equation over a prime field (mathbb{F}_p):

[ v^2 = u^3 + 486662u^2 + u pmod{2^{255} – 19} ]

The security relies on the intractability of the ECDLP: given a base point ( G ) and a public key ( P = dG ), it is computationally infeasible to find the private scalar ( d ). The shared secret is computed as ( S = d_{client} P_{server} = d_{client} d_{server} G = d_{server} P_{client} ). OpenSSL implements this using highly optimized, constant-time Montgomery ladders to prevent timing side-channel attacks.

Galois/Counter Mode (GCM) Mathematics

Once keys are established, AES-GCM encrypts the application records. The authentication tag in GCM is computed using a universal hash function GHASH over the Galois field ( text{GF}(2^{128}) ). The field is defined by the irreducible polynomial:

[ P(x) = x^{128} + x^7 + x^2 + x + 1 ]

Elements of the field are 128-bit blocks. The GHASH function evaluates a polynomial where the coefficients are the ciphertext blocks and the variable is the hash subkey ( H ). In production, this multiplication is massively accelerated using CPU hardware instructions like Intel’s AES-NI (specifically the vpclmulqdq instruction for carry-less multiplication).

4. Production Engineering: OpenSSL and Hardware Acceleration

In high-throughput architectures (e.g., terminating 100k+ TLS connections/sec), CPU overhead is the primary bottleneck. Modern edge load balancers bypass generic C implementations and invoke raw assembly. For example, in OpenSSL’s evp_cipher API, AES-GCM is routed directly to the AES-NI vector units. Engineers tuning Envoy or Nginx for TLS termination must ensure that the OS CPU flags expose aes, pclmulqdq, and avx512f to the user-space process.

Furthermore, OpenSSL’s EVP_DigestSign for ECDSA/RSA certificate signatures is often offloaded to asynchronous cryptographic engines (e.g., Intel QAT) via engine modules, allowing the Nginx event loop to continue serving other epoll events while the hardware computes the ECDLP scalar multiplication.

5. Advanced Tooling: eBPF Traffic Interception

Debugging production TLS issues (like handshake latency spikes or cipher negotiation failures) using TCP dumps is useless since the payload is encrypted. Instead, elite engineers deploy eBPF (Extended Berkeley Packet Filter) to trace OpenSSL dynamically in user-space.

#include <uapi/linux/ptrace.h>
BPF_HASH(start, u32);
BPF_HISTOGRAM(dist);

int probe_ssl_handshake_start(struct pt_regs *ctx) {
    u32 pid = bpf_get_current_pid_tgid();
    u64 ts = bpf_ktime_get_ns();
    start.update(&pid, &ts);
    return 0;
}

int probe_ssl_handshake_return(struct pt_regs *ctx) {
    u32 pid = bpf_get_current_pid_tgid();
    u64 *tsp = start.lookup(&pid);
    if (tsp != 0) {
        u64 delta = bpf_ktime_get_ns() - *tsp;
        dist.increment(bpf_log2l(delta / 1000000)); // Log2 histogram in ms
        start.delete(&pid);
    }
    return 0;
}

By injecting this eBPF program into the libssl.so text segment, SREs can visualize TLS handshake percentiles (p99 latency) without modifying the application code or suffering tcpdump context-switch overhead.

6. Post-mortem: Side-Channel Attack Mitigations

Production environments must harden against side-channel vulnerabilities such as Bleichenbacher attacks, Lucky Thirteen, or cache-timing attacks (e.g., Flush+Reload). TLS 1.3 eliminates many of these by removing RSA encryption (PKCS#1 v1.5 padding) and MAC-then-Encrypt CBC modes. To defend against remaining scalar multiplication timing leaks, libraries use Montgomery Ladders and ensure that branch instructions and memory access patterns are strictly independent of the secret scalar bits.

FAQ

Does the certificate encrypt application records?

No. The X.509 certificate mathematically authenticates the server via a digital signature (e.g., ECDSA over P-256) over the transcript hash, proving ownership of the public key. Encryption of the application layer is exclusively handled by the symmetric AEAD keys derived from the ephemeral ECDHE exchange via HKDF, guaranteeing Forward Secrecy.

Why not implement TLS from scratch?

Production TLS requires constant-time arithmetic to prevent microarchitectural side-channel data leaks. Rolling your own crypto in high-level languages often introduces cache-timing vulnerabilities, branch-prediction leaks, and memory-safety flaws. Always rely on battle-tested, hardware-accelerated libraries like OpenSSL, BoringSSL, or Rustls.

References

Search questions

FAQ

Who is this article for?

This article is for readers who want a professional-level guide to HTTPS and TLS 1.3 Handshake: Keys, Certificates, and RTT in Practice. It takes about 13 min and focuses on TLS 1.3, HTTPS, Key Agreement, Python.

What should I read next?

The recommended next step is HTTP/2, HTTP/3, and CDN Caching: Read Page Speed from a Waterfall, so the article connects into a longer learning route instead of ending as an isolated note.

Does this article include runnable code or companion resources?

Yes. Use the run notes, resource cards, and download links on the page to reproduce the example or inspect the companion files.

How does this article fit into the larger site?

It is connected to the article context block, learning routes, resources, and project timeline so readers can move from concept to implementation.

Article context

Network Fundamentals

A reproducible route through DNS, TCP, TLS, HTTP/3, proxy tunnels, load balancing, and shared caches with code and figures.

Level: Professional Reading time: 13 min
  • TLS 1.3
  • HTTPS
  • Key Agreement
  • Python
Other language version HTTPS 与 TLS 1.3 握手原理:密钥交换、证书和 RTT 实验
Share summary HTTPS and TLS 1.3 Handshake: Keys, Certificates, and RTT in Practice

Understand TLS 1.3 message flights, certificate authentication, ephemeral key agreement, and handshake latency with a safe teaching model.

Download share card Open share center

Companion resources

Leave a Reply

Project timeline

Published posts

  1. DNS Resolution Explained: Build a TTL Cache and Packet Parser in Python A runnable DNS guide covering resolution paths, response headers, TTL cache latency, and deterministic Python/C experiments.
  2. CIDR, Longest Prefix Match, and MTU: Calculate IP Routing Step by Step Calculate CIDR ranges, longest-prefix route choice, and MTU/MSS payload segmentation with runnable Python and C examples.
  3. TCP Reliability and Congestion Window: A Runnable Sequence Number Experiment Track TCP sequence numbers, cumulative ACKs, loss, retransmission, and congestion-window changes with safe local experiments.
  4. HTTPS and TLS 1.3 Handshake: Keys, Certificates, and RTT in Practice Understand TLS 1.3 message flights, certificate authentication, ephemeral key agreement, and handshake latency with a safe teaching model.
  5. HTTP/2, HTTP/3, and CDN Caching: Read Page Speed from a Waterfall A deterministic browser-waterfall model for HTTP/2, HTTP/3, QUIC streams, and CDN cache hits or misses.
  6. Forward Proxy vs Reverse Proxy: Connection Paths, Trust Boundaries, and Latency A reproducible guide to forward proxies, reverse proxies, tunnels, TLS boundaries, and latency segments.
  7. HTTP CONNECT and HTTPS Proxy Tunnels: TLS Boundaries and Handshake Latency An RFC-based explanation of CONNECT tunnels, encrypted HTTPS payloads, and modeled first-request latency.
  8. SOCKS5 Proxy Explained: Protocol Bytes, DNS Resolution Boundaries, and Leakage Risk Decode safe SOCKS5 CONNECT bytes and compare local-DNS and proxy-side hostname resolution boundaries.
  9. Reverse Proxy Load Balancing: Queues, Health Checks, and a Reproducible Scheduler Compare round robin and load-aware queue selection while reasoning about health checks and retry boundaries.
  10. Proxy Cache Revalidation: Cache-Control, ETag, and Observable Correctness Use an RFC 9111 shared-cache model to calculate MISS, HIT, and 304 revalidation latency and correctness boundaries.

Published resources

  1. Network Fundamentals Lab README Setup, no-privilege safety boundary, ten Python experiments, and three C examples.
  2. Network fundamentals full lab bundle Bundles Python/C source, fixed scenarios, ten result CSVs, and protocol/proxy figures.
  3. DNS TTL results CSV HIT/MISS state, expiry, and latency for four fixed lookups.
  4. CIDR and MTU results CSV Longest-prefix route and 3600-byte payload segmentation results.
  5. TCP cwnd events CSV Per-round ACK, window, and deterministic retransmission events.
  6. TLS 1.3 flight results CSV Message direction, timing, and teaching shared value in a fixed RTT model.
  7. HTTP/CDN waterfall results CSV Phase timing for HTTP/2 and HTTP/3 in cold and warm cache models.
  8. Proxy path latency results CSV Phase timing for direct access, forward-proxy tunneling, and reverse-proxy cache paths.
  9. CONNECT/TLS timeline CSV Records CONNECT authority, tunnel establishment, and the encrypted HTTPS-request boundary.
  10. SOCKS5 DNS boundary CSV Stores ATYP, destination bytes, request length, and modeled local DNS counts.
  11. Proxy load-balancing queue CSV Compares backend selection and queue waiting for round robin and least queue.
  12. Proxy cache revalidation CSV Records MISS, HIT, 304 revalidation, object age, and response latency.
  13. Network request path visualizer Adjust TTL, prefixes, loss, handshake RTT, and cache paths in the browser.
  14. Network fundamentals topic share card A 1200x630 SVG card for the DNS, TLS, HTTP/3, proxy tunnel, and caching topic hub.

Next notes

  1. Add IPv6 and QUIC observation notes
  2. Review caching and protocol benefits with real-user metrics
Scroll down