21: Send TLS 1.3 Early Data
Overview
Section titled “Overview”Send a read-only status probe when resuming a loopback TLS session. TLS 1.3 early data can reach the server before the handshake completes.
Prerequisites
Section titled “Prerequisites”- PHP 8.6 CLI. Confirm the version with
php --version. - The
opensslextension with TLS 1.3,proc_open(), and access to loopback sockets. Theopensslcommand generates a self-signed certificate trusted only by the lab client. - Review the previous chapter if you’re following the series in order.
Allow about 15 minutes for the walkthrough and exercise.
What You’ll Build
Section titled “What You’ll Build”A runnable tls-early-data.php example. This example calls the shared loopback TLS lab.
Step 1: Run the example (~10 min)
Section titled “Step 1: Run the example (~10 min)”Run the following from the root of the Code with PHP repository.
php code/php-8-6/21-tls-early-data/tls-early-data.php<?php
declare(strict_types=1);
require __DIR__ . '/../support/tls-lab.php';
runTlsLab('early-data');Expected Result
Section titled “Expected Result”Connection 1 resumed=noServer received 0 early bytesClient early data: not_sentConnection 2 resumed=yesServer received 6 early bytesClient early data: acceptedWhy It Works
Section titled “Why It Works”The server opts in with max_early_data and handles incoming bytes through early_data_cb. The client supplies early_data along with its saved session. Read the crypto status before deciding whether to resend. Early data can be replayed, so only send operations designed to tolerate replay.
In the lab, the first connection obtains a session ticket and sends no early data. The next connection uses that ticket and supplies the six bytes of status. The callback counts those bytes on the server side. The client’s accepted status confirms that the server accepted the early data.
Exercise
Section titled “Exercise”Remove the assignment to $options['early_data'] in the lab client and run the example again. The second connection should resume, the server should receive zero early bytes, and the client should report not_sent.
Wrap-up
Section titled “Wrap-up”Continue with Quote SQL String Literals with MySQLi.