19: Resume a TLS Stream Session
Overview
Section titled “Overview”Connect twice to a local TLS server and inspect whether the second connection reuses the first session. Resumption can reduce the cost of opening another connection.
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-session-resumption.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/19-tls-session-resumption/tls-session-resumption.php<?php
declare(strict_types=1);
require __DIR__ . '/../support/tls-lab.php';
runTlsLab('resume');Expected Result
Section titled “Expected Result”Connection 1 resumed=noConnection 2 resumed=yesWhy It Works
Section titled “Why It Works”The client captures a session ticket via session_new_cb while reading application data. It gives that session to a subsequent connection via session_data. Inspect session_reused in the stream’s crypto metadata because the server can decline to resume.
In runTlsLabClient(), follow the first connection through fgets() before checking the captured session. Reading from the stream gives the client time to receive the TLS 1.3 ticket. The server uses the same listener and session ID context for both connections, and the client validates the same certificate each time.
Exercise
Section titled “Exercise”Set session_data to null for the second connection. Both connections should report resumed=no.
Wrap-up
Section titled “Wrap-up”Continue with Authenticate TLS Streams with External PSKs.