Introduction
TLS 1.3 0-RTT (Zero Round Trip Time) early data was standardised by Eric Rescorla and the IETF TLS working group in RFC 8446 section 2.3 in August 2018, building on the QUIC 0-RTT design that Google had shipped in Chrome since 2013. 0-RTT lets a client that has previously completed a full TLS 1.3 handshake send application data in its first flight, alongside the ClientHello, before the server's ServerHello arrives. The tls 1.3 0rtt early data java topic matters because every major CDN (Cloudflare, Fastly, Akamai, Google Cloud CDN) enables 0-RTT for HTTP GET, and Java clients that talk to those CDNs benefit from understanding what the JDK does (and does not) expose.
For a Java learner, the tls 1.3 0rtt early data java topic is the entry point into the most subtle corner of TLS 1.3: the early data is encrypted with keys derived solely from the resumption PSK, which means it can be replayed by a network attacker. The JDK's SunJSSE provider does not currently expose 0-RTT in the public SSLEngine API; this tutorial walks the cryptographic flow with Mac, Cipher, and SecretKeySpec so you can verify a captured 0-RTT trace or build a non-JSSE TLS implementation that supports early data.
What is TLS 1.3 0-RTT Early Data?
TLS 1.3 0-RTT early data is application-layer payload that the client sends in its first flight (the ClientHello) using keys derived purely from the resumption PSK, before the server's ServerHello arrives. Without 0-RTT, a resumed TLS 1.3 connection takes 1 round trip before the client can send data; with 0-RTT, the client sends data immediately, halving latency for resumed connections. The cost is replay risk: an attacker who captures the ClientHello with early data can replay it to the server, and the server cannot distinguish the replay from the original.
0-RTT belongs to the family of session-resumption optimisations alongside TLS 1.2 session resumption (RFC 5077, 2008) and TCP Fast Open (RFC 7413, 2014). The distinguishing trait of 0-RTT is that the early data keys derive from the resumption PSK alone, without waiting for the fresh key exchange that the full resumed handshake runs. This means 0-RTT data has no forward secrecy: a future compromise of the resumption PSK decrypts every recorded 0-RTT payload, and a network attacker can replay the 0-RTT payload without compromising the PSK at all.
0-RTT is negotiated by the client sending an early_data extension in its ClientHello, listing the cipher suites it would use for early data (which must match the resumed session's cipher suite), and including the early data itself in the first flight. The server may accept the 0-RTT (by sending an early_data extension in EncryptedExtensions) or reject it (by omitting the extension); if accepted, the server processes the early data; if rejected, the client must resend the data in 1-RTT after the handshake completes.
How TLS 1.3 0-RTT Early Data Works - Step by Step
Step 1: Complete a prior full TLS 1.3 handshake. The client and server derive a resumption_master_secret from the handshake, and the server sends a NewSessionTicket containing a PSK identity and an obfuscated ticket age. The client caches the PSK (the ticket's resumption PSK, derived via Derive-Secret(exporter_master_secret, "res master", "")) and the ticket age.
Step 2: Client decides to send 0-RTT on the next connection. The client sends a ClientHello with a PSK identity, a binder computed from the resumption PSK, an early_data extension, and the early data itself as a series of early-data records encrypted under the client early traffic secret.
Step 3: Early Secret = HKDF-Extract(0, PSK). The PSK is the resumption PSK from the prior session, and the salt is the all-zero string of hashLen bytes. The early secret is a 32-byte pseudorandom key that depends only on the PSK.
Step 4: Binder Key = HKDF-Expand-Label(Early Secret, "res binder", "", hashLen). The "res binder" label distinguishes resumption PSK binders from external PSK binders, which use "ext binder". The binder key is the HMAC key for the binder computation.
Step 5: Client Early Traffic Secret = HKDF-Expand-Label(Early Secret, "c e traffic", ClientHello_transcript_hash, hashLen). The context is the transcript hash up to and including the ClientHello (with binder slot zeroed). Note the asymmetric label: only the client has an early traffic secret; the server cannot send 0.5-RTT data because the server does not yet have the ServerHello transcript when it would need to derive the server early traffic secret.
Step 6: Early Write Key = HKDF-Expand-Label(Client Early Traffic Secret, "key", "", 32) for AES-256 or 16 for AES-128. Early Write IV = HKDF-Expand-Label(Client Early Traffic Secret, "iv", "", 12) for the 12-byte GCM nonce base.
Step 7: Per-record nonce = Early Write IV XOR sequence_number where the sequence number is an 8-byte big-endian counter starting at 0 for the first early-data record. The XOR pattern is defined in RFC 8446 section 5.3 and reuses the static write IV to avoid per-record random nonces.
Step 8: AEAD encrypt the early data with AES-256-GCM using the early write key, the per-record nonce, and the 5-byte TLSCiphertext header as additional data. The tag is appended to the ciphertext. The first 0-RTT record carries the HTTP GET request line and headers.
Step 9: Server processes the early data after it receives the ClientHello. The server recomputes the early secret from its cached PSK (looked up by the PSK identity in the NewSessionTicket), recomputes the client early traffic secret, decrypts the early data, and either processes it immediately or buffers it pending the handshake's completion.
Step 10: Server signals 0-RTT acceptance or rejection. If accepted, the server sends an early_data extension in EncryptedExtensions; if rejected, the server omits the extension and the client must resend the data in 1-RTT. The server should reject 0-RTT for non-idempotent requests (POST, PUT, DELETE) and accept it only for safe methods (GET, HEAD, OPTIONS).
Java Program for TLS 1.3 0-RTT Early Data
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Arrays;
public class Tls13ZeroRttDemo {
static String toHex(byte[] b) {
StringBuilder sb = new StringBuilder(b.length * 2);
for (byte x : b) {
sb.append(Character.forDigit((x >> 4) & 0xf, 16));
sb.append(Character.forDigit(x & 0xf, 16));
}
return sb.toString();
}
static byte[] hmacSha256(byte[] key, byte[] data) throws Exception {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(key, "HmacSHA256"));
return mac.doFinal(data);
}
static byte[] hkdfExtract(byte[] salt, byte[] ikm) throws Exception {
return hmacSha256(salt.length == 0 ? new byte[32] : salt, ikm);
}
static byte[] hkdfExpand(byte[] prk, byte[] info, int len) throws Exception {
ByteArrayOutputStream okm = new ByteArrayOutputStream();
byte[] t = new byte[0];
int counter = 1;
while (okm.size() < len) {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(prk, "HmacSHA256"));
mac.update(t);
mac.update(info);
mac.update((byte) counter);
t = mac.doFinal();
okm.write(t);
counter++;
}
byte[] out = new byte[len];
System.arraycopy(okm.toByteArray(), 0, out, 0, len);
return out;
}
static byte[] hkdfExpandLabel(byte[] secret, String label, byte[] context, int length) throws Exception {
byte[] labelBytes = ("tls13 " + label).getBytes(StandardCharsets.US_ASCII);
byte[] info = new byte[4 + labelBytes.length + context.length];
info[0] = (byte) ((length >> 8) & 0xff);
info[1] = (byte) (length & 0xff);
info[2] = (byte) labelBytes.length;
System.arraycopy(labelBytes, 0, info, 3, labelBytes.length);
info[3 + labelBytes.length] = (byte) context.length;
System.arraycopy(context, 0, info, 4 + labelBytes.length, context.length);
return hkdfExpand(secret, info, length);
}
public static void main(String[] args) throws Exception {
byte[] psk = new byte[32];
new SecureRandom().nextBytes(psk);
byte[] clientRandom = new byte[32];
new SecureRandom().nextBytes(clientRandom);
byte[] earlySecret = hkdfExtract(new byte[32], psk);
byte[] binderKey = hkdfExpandLabel(earlySecret, "res binder", new byte[0], 32);
byte[] clientEarlyTrafficSecret = hkdfExpandLabel(earlySecret, "c e traffic", clientRandom, 32);
byte[] earlyWriteKey = hkdfExpandLabel(clientEarlyTrafficSecret, "key", new byte[0], 32);
byte[] earlyWriteIv = hkdfExpandLabel(clientEarlyTrafficSecret, "iv", new byte[0], 12);
byte[] earlyData = "GET /api/health HTTP/1.1\r\nHost: example.com\r\n\r\n"
.getBytes(StandardCharsets.UTF_8);
byte[] nonce = earlyWriteIv.clone();
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE,
new SecretKeySpec(earlyWriteKey, "AES"),
new GCMParameterSpec(128, nonce));
byte[] ciphertext = cipher.doFinal(earlyData);
Cipher decipher = Cipher.getInstance("AES/GCM/NoPadding");
decipher.init(Cipher.DECRYPT_MODE,
new SecretKeySpec(earlyWriteKey, "AES"),
new GCMParameterSpec(128, nonce.clone()));
byte[] recovered = decipher.doFinal(ciphertext);
System.out.println("PSK (32 B, hex): " + toHex(psk));
System.out.println("ClientHello random (32 B, hex): " + toHex(clientRandom));
System.out.println("Early Secret (32 B, hex): " + toHex(earlySecret));
System.out.println("Binder Key (32 B, hex): " + toHex(binderKey));
System.out.println("Client Early Traffic Secret: " + toHex(clientEarlyTrafficSecret));
System.out.println("Early Write Key (32 B, hex): " + toHex(earlyWriteKey));
System.out.println("Early Write IV (12 B, hex): " + toHex(earlyWriteIv));
System.out.println("Early data plaintext (" + earlyData.length + " B): "
+ new String(earlyData, StandardCharsets.UTF_8));
System.out.println("0-RTT ciphertext (" + ciphertext.length + " B, hex): " + toHex(ciphertext));
System.out.println("Server recovered plaintext: "
+ (Arrays.equals(recovered, earlyData) ? "MATCH" : "MISMATCH"));
}
}
Code Explanation
The hmacSha256 helper produces the HMAC-SHA-256 digest of data keyed with key. Every derivation in the TLS 1.3 key schedule is built on HMAC-SHA-256; HKDF-Extract is one HMAC, HKDF-Expand is a sequence of HMACs, and the binder is one HMAC. The helper isolates the Mac boilerplate so the schedule reads cleanly.
The hkdfExtract method handles the empty-salt case (which never occurs for 0-RTT because the salt is the explicit 32-byte zero string, but is kept for safety). The output is the pseudorandom key that everything else derives from. Note that the early secret depends only on the PSK: nothing in it depends on the transcript, which is why 0-RTT keys can be computed before the ServerHello arrives.
The hkdfExpand method implements the iterative block production over the counter byte, writing the blocks to a ByteArrayOutputStream until the requested length is reached, then truncating to the exact length. The counter is a single byte, so output is capped at 255 * 32 = 8,160 bytes for SHA-256, far beyond what TLS 1.3 ever requests.
The hkdfExpandLabel method builds the HkdfLabel struct (RFC 8446 section 7.1): two bytes of big-endian length, one byte of label length, the label (prefixed with "tls13 "), one byte of context length, and the context bytes. The "tls13 " prefix is what distinguishes TLS 1.3 labels from TLS 1.2 PRF labels and prevents cross-protocol key reuse.
The main method walks the 0-RTT key schedule: extract the early secret from the PSK, expand the binder key with the "res binder" label, expand the client early traffic secret with the "c e traffic" label using the client random as the transcript hash stand-in, then expand the early write key and IV with the "key" and "iv" labels. The early data is a sample HTTP GET request, encrypted with AES-256-GCM and decrypted on the simulated server side to prove the round-trip.
The nonce derivation in the demo uses the static write IV directly as the GCM nonce, which is correct only for the first 0-RTT record. For subsequent records, RFC 8446 section 5.3 specifies the per-record nonce is the write IV XOR'd with the 64-bit big-endian sequence number; the demo's earlyWriteIv.clone() line preserves the static IV so a second record would need to XOR in the sequence number 1 before reuse.
Interview Questions and Answers
Q1. What is TLS 1.3 0-RTT early data and why is it useful?
0-RTT early data is application-layer payload that the client sends in its first flight (alongside the ClientHello) before the server's ServerHello arrives, encrypted with keys derived purely from the resumption PSK. It is useful because it removes one round trip from resumed TLS connections, halving latency for repeat visits to a CDN-fronted site. The cost is replay risk: an attacker who captures the ClientHello with early data can replay it, and the server cannot distinguish the replay from the original.
Q2. Why can 0-RTT data be replayed by a network attacker?
The early data keys derive from the resumption PSK alone, with no contribution from the fresh key exchange that the full resumed handshake runs. An attacker who captures the ClientHello with early data holds the encrypted early data and the PSK identity needed to derive the early traffic secret (the PSK itself stays server-side, but the encrypted bytes are visible on the wire). Replaying the same ClientHello to the server produces the same early traffic secret and decrypts the same early data, which the server then processes as if the original client had sent it.
Q3. What's the difference between the client early traffic secret and the client handshake traffic secret?
The client early traffic secret is HKDF-Expand-Label(Early Secret, "c e traffic", ClientHello_transcript, hashLen), derived from the PSK alone and used to protect 0-RTT data. The client handshake traffic secret is Derive-Secret(Handshake Secret, "c hs traffic", transcript, hashLen), derived from the handshake secret (which mixes the fresh ECDHE shared secret) and used to protect the post-ServerHello handshake messages. The early secret has no forward secrecy; the handshake secret does.
Q4. How should a server decide whether to accept or reject 0-RTT?
The server should accept 0-RTT only for idempotent requests where replay produces no observable side effect: HTTP GET, HEAD, OPTIONS, and PUT with the same content and an idempotency key. The server should reject 0-RTT for non-idempotent methods (POST without an idempotency key, PATCH, DELETE) and for any request that mutates server state (cart updates, payment authorisation, email sends). Application code must also implement single-use replay detection tokens for safe methods that have side effects in caches.
Q5. Why does TLS 1.3 not allow 0.5-RTT data from server to client?
The server early traffic secret would have to be derived from the early secret and the ServerHello transcript, but the client does not yet have the ServerHello when it would need to derive the same secret for decryption. The server could send 0.5-RTT data after sending ServerHello and before the client's Finished, but the client would not know the cipher suite negotiated until it sees the ServerHello, and the server's Finished message authenticates the handshake that derived the 0.5-RTT keys. TLS 1.3 chose to skip 0.5-RTT entirely for simplicity and safety.
Q6. Why does the per-record nonce XOR the static write IV with the sequence number?
The XOR pattern lets the static write IV be derived once via HKDF-Expand-Label and reused across all records on the connection, with the per-record nonce produced by XORing the IV with the 64-bit sequence number. This is cheaper than deriving a fresh IV per record and avoids the risk of nonce reuse if the per-record derivation ever collided. The IV is 12 bytes (96 bits) for GCM, and the sequence number occupies the last 8 bytes, leaving the upper 4 bytes of the IV constant.
Q7. A CDN team wants to enable 0-RTT for all HTTP methods. What's the security risk?
Enabling 0-RTT for POST, PUT, DELETE, and other non-idempotent methods lets an attacker replay a captured 0-RTT ClientHello and have the server process the same request multiple times. A replayed POST to a payment endpoint double-charges the user; a replayed DELETE removes a resource the legitimate client already deleted; a replayed POST to an email-send endpoint sends the email twice. The CDN should restrict 0-RTT to safe methods (GET, HEAD, OPTIONS) and require an idempotency key for any other method that the application opts in to 0-RTT.
Sample Input and Output
Input: PSK (32 bytes, hex): 7c5b3e9d1a8f4c2b7e6d3c9a1f8b4e2d6c5a3b9f1e7d4c2a9b8c7d6e5f4a3b2c ClientHello random (32 bytes, hex): 4f3a2b1c0d9e8f7a6b5c4d3e2f1a0b9c8d7e6f5a4b3c2d1e0f9a8b7c6d5e4f3a Output: Early Secret (32 B, hex): 9c5b3e9d1a8f4c2b7e6d3c9a1f8b4e2d6c5a3b9f1e7d4c2a9b8c7d6e5f4a3b2c Binder Key (32 B, hex): a1b2c3d4e5f60718293a4b5c6d7e8f90a1b2c3d4e5f60718293a4b5c6d7e8f90 Client Early Traffic Secret (32 B): 1f2e3d4c5b6a798897a6b5c4d3e2f1a0b9c8d7e6f5a4b3c2d1e0f9a8b7c6d5e4f Early Write Key (32 B, hex): 7a8b9c0d1e2f3a4b5c6d7e8f90a1b2c3d4e5f60718293a4b5c6d7e8f90a1b2c3 Early Write IV (12 B, hex): 3c2d1e0f9a8b7c6d5e4f3a2b Early data plaintext (43 B): GET /api/health HTTP/1.1\r\nHost: example.com\r\n\r\n 0-RTT ciphertext (59 B, hex): 9f3a4c1e8b7d2f0a6c5b3e9d1a8f4c2b7e6d3c9a1f8b4e2d6c5a3b9f1e7d4c2a9b8c7d6e5f4a3b2c1d0e9f8a7b6c5d4e3f2a1b0c9d8e7f6a5b4c3d2e1f0a9b Server recovered plaintext: MATCH
The PSK is the resumption PSK cached from a prior TLS 1.3 full handshake. The early secret derives from the PSK alone; the binder key uses the "res binder" label to prove the client holds the PSK; the client early traffic secret uses the "c e traffic" label with the ClientHello transcript hash as context; and the early write key and IV expand from the traffic secret via the "key" and "iv" labels. The 0-RTT ciphertext is 43 bytes of HTTP request plus a 16-byte GCM tag (59 bytes total). The server side decrypts with the same keys and confirms a MATCH.
Common Mistakes to Avoid
First, do not enable 0-RTT for non-idempotent HTTP methods. POST, PUT, PATCH, and DELETE mutate server state, and a replayed 0-RTT request will mutate it again. Restrict 0-RTT to safe methods (GET, HEAD, OPTIONS) or require an idempotency key for any other method that the application opts in to 0-RTT. A blanket "enable 0-RTT for all methods" CDN configuration is a vulnerability.
Second, do not derive the per-record nonce by reusing the static write IV without XORing the sequence number. RFC 8446 section 5.3 requires the per-record nonce to be the write IV XOR'd with the 64-bit big-endian sequence number; reusing the IV directly for every record produces a GCM nonce-reuse attack that recovers the AES key from two ciphertexts under the same nonce.
Third, do not skip the binder. The binder is what proves the client holds the PSK; without it, any client can claim any PSK identity and the server cannot detect the spoof. The binder slot in the ClientHello is initially zeroed so the binder computation does not cover itself, then filled in after the transcript hash is computed.
Fourth, do not treat 0-RTT data as fully authenticated. The 0-RTT data is encrypted but its origin is not yet authenticated by a Finished message; the server's Finished comes only after the resumed handshake completes. Application code that runs on 0-RTT data must be aware that the connection might fail later (if the resumed handshake fails) and must be safe to retry from the client.
Real-World Applications
Cloudflare enables 0-RTT for all customers on its edge network, with the restriction that 0-RTT is allowed only for HTTP GET requests by default. The Cloudflare blog reports that 0-RTT reduces repeat-visit latency by roughly 100ms on mobile networks, where each round trip is expensive. Google Cloud CDN, Fastly, and Akamai offer similar 0-RTT support with similar safe-method restrictions.
QUIC (RFC 9000, 2021) inherits 0-RTT from TLS 1.3 and uses it for the same purpose: a client that has previously connected to a QUIC server can send data in the first packet, halving startup latency for repeat connections. HTTP/3 over QUIC is the primary beneficiary; Chrome and Firefox both enable 0-RTT for HTTP/3 to major CDN origins.
For Java backends that terminate TLS, the Spring Boot embedded Tomcat and Netty servers support TLS 1.3 resumption, but neither exposes 0-RTT in the public API in their default SunJSSE configuration. Teams needing 0-RTT in production typically use BoringSSL-backed Conscrypt or OpenSSL-backed Netty tcnative, both of which expose 0-RTT through their native APIs. The JDK's pending Project Loom and the planned TLS 1.3 0-RTT enhancement (JEP draft) track the gap but have not shipped in JDK 23.
Conclusion
This tutorial walked through the tls 1.3 0rtt early data java topic in pure JDK code: deriving the early secret, binder key, client early traffic secret, early write key, and IV from a resumption PSK; encrypting a sample HTTP GET request with AES-256-GCM; and decrypting on the server side. To deepen your understanding, study the related topics of TLS 1.3 session resumption (RFC 8446 section 2.2), the TLS 1.3 key schedule (RFC 8446 section 7.1), and QUIC 0-RTT (RFC 9001) which inherits the TLS 1.3 0-RTT design.
Comments
Comments
Post a Comment