|
| 1 | +--TEST-- |
| 2 | +TLS 1.3 early data (0-RTT) basic client/server round-trip |
| 3 | +--EXTENSIONS-- |
| 4 | +openssl |
| 5 | +--SKIPIF-- |
| 6 | +<?php |
| 7 | +if (!function_exists("proc_open")) die("skip no proc_open"); |
| 8 | +if (!defined('STREAM_CRYPTO_METHOD_TLSv1_3_SERVER')) die("skip TLS 1.3 not available"); |
| 9 | +?> |
| 10 | +--FILE-- |
| 11 | +<?php |
| 12 | +$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'tls_early_data_basic.pem.tmp'; |
| 13 | + |
| 14 | +$serverCode = <<<'CODE' |
| 15 | + $early = ''; |
| 16 | + $ctx = stream_context_create(['ssl' => [ |
| 17 | + 'local_cert' => '%s', |
| 18 | + 'session_id_context' => 'early-data-test', |
| 19 | + 'max_early_data' => 16384, |
| 20 | + 'early_data_cb' => function ($stream, string $data) use (&$early) { |
| 21 | + $early .= $data; |
| 22 | + }, |
| 23 | + ]]); |
| 24 | +
|
| 25 | + $server = stream_socket_server('tls://127.0.0.1:0', $errno, $errstr, |
| 26 | + STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $ctx); |
| 27 | + phpt_notify_server_start($server); |
| 28 | +
|
| 29 | + for ($i = 0; $i < 2; $i++) { |
| 30 | + $early = ''; |
| 31 | + $client = @stream_socket_accept($server, 30); |
| 32 | + if ($client) { |
| 33 | + $meta = stream_get_meta_data($client); |
| 34 | + $status = $meta['crypto']['early_data'] ?? 'none'; |
| 35 | + fwrite($client, "early=[$early] status=$status\n"); |
| 36 | + fclose($client); |
| 37 | + } |
| 38 | + } |
| 39 | +CODE; |
| 40 | +$serverCode = sprintf($serverCode, $certFile); |
| 41 | + |
| 42 | +$clientCode = <<<'CODE' |
| 43 | + $sessionData = null; |
| 44 | +
|
| 45 | + $ctx = stream_context_create(['ssl' => [ |
| 46 | + 'verify_peer' => false, |
| 47 | + 'verify_peer_name' => false, |
| 48 | + 'session_new_cb' => function ($stream, $session) use (&$sessionData) { |
| 49 | + $sessionData = $session; |
| 50 | + }, |
| 51 | + ]]); |
| 52 | +
|
| 53 | + /* First connection: full handshake, capture the resumable session */ |
| 54 | + $client1 = stream_socket_client("tls://{{ ADDR }}", $errno, $errstr, |
| 55 | + 30, STREAM_CLIENT_CONNECT, $ctx); |
| 56 | + echo trim(fgets($client1)) . "\n"; |
| 57 | + fclose($client1); |
| 58 | +
|
| 59 | + /* Second connection: resume and send 0-RTT early data */ |
| 60 | + $ctx2 = stream_context_create(['ssl' => [ |
| 61 | + 'verify_peer' => false, |
| 62 | + 'verify_peer_name' => false, |
| 63 | + 'session_data' => $sessionData, |
| 64 | + 'early_data' => 'ping-0rtt', |
| 65 | + ]]); |
| 66 | +
|
| 67 | + $client2 = stream_socket_client("tls://{{ ADDR }}", $errno, $errstr, |
| 68 | + 30, STREAM_CLIENT_CONNECT, $ctx2); |
| 69 | + echo trim(fgets($client2)) . "\n"; |
| 70 | + $meta2 = stream_get_meta_data($client2); |
| 71 | + echo "client early_data: " . ($meta2['crypto']['early_data'] ?? 'none') . "\n"; |
| 72 | + fclose($client2); |
| 73 | +CODE; |
| 74 | + |
| 75 | +include 'CertificateGenerator.inc'; |
| 76 | +$certificateGenerator = new CertificateGenerator(); |
| 77 | +$certificateGenerator->saveNewCertAsFileWithKey('tls_early_data_basic', $certFile); |
| 78 | + |
| 79 | +include 'ServerClientTestCase.inc'; |
| 80 | +ServerClientTestCase::getInstance()->run($clientCode, $serverCode); |
| 81 | +?> |
| 82 | +--CLEAN-- |
| 83 | +<?php |
| 84 | +@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'tls_early_data_basic.pem.tmp'); |
| 85 | +?> |
| 86 | +--EXPECT-- |
| 87 | +early=[] status=not_sent |
| 88 | +early=[ping-0rtt] status=accepted |
| 89 | +client early_data: accepted |
0 commit comments