1--TEST--
2Capture SSL session meta array in stream context
3--SKIPIF--
4<?php
5if (!extension_loaded("openssl")) die("skip openssl not loaded");
6if (!function_exists("proc_open")) die("skip no proc_open");
7?>
8--FILE--
9<?php
10$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'session_meta_capture.pem.tmp';
11$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'session_meta_capture-ca.pem.tmp';
12
13$serverCode = <<<'CODE'
14    $serverUri = "ssl://127.0.0.1:64321";
15    $serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
16    $serverCtx = stream_context_create(['ssl' => [
17        'local_cert' => '%s',
18        'security_level' => 1,
19    ]]);
20
21    $server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
22    phpt_notify();
23
24    @stream_socket_accept($server, 1);
25    @stream_socket_accept($server, 1);
26    @stream_socket_accept($server, 1);
27    @stream_socket_accept($server, 1);
28CODE;
29$serverCode = sprintf($serverCode, $certFile);
30
31$peerName = 'session_meta_capture';
32$clientCode = <<<'CODE'
33    $serverUri = "ssl://127.0.0.1:64321";
34    $clientFlags = STREAM_CLIENT_CONNECT;
35    $clientCtx = stream_context_create(['ssl' => [
36        'verify_peer' => true,
37        'cafile' => '%s',
38        'peer_name' => '%s',
39        'capture_session_meta' => true,
40        'security_level' => 1,
41    ]]);
42
43    phpt_wait();
44
45    stream_context_set_option($clientCtx, 'ssl', 'crypto_method', STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT);
46    @stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx);
47    $meta = stream_context_get_options($clientCtx)['ssl']['session_meta'];
48    var_dump($meta['protocol']);
49
50    stream_context_set_option($clientCtx, 'ssl', 'crypto_method', STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT);
51    @stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx);
52    $meta = stream_context_get_options($clientCtx)['ssl']['session_meta'];
53    var_dump($meta['protocol']);
54
55    stream_context_set_option($clientCtx, 'ssl', 'crypto_method', STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT);
56    @stream_socket_client($serverUri, $errno, $errstr, 2, $clientFlags, $clientCtx);
57    $meta = stream_context_get_options($clientCtx)['ssl']['session_meta'];
58    var_dump($meta['protocol']);
59CODE;
60$clientCode = sprintf($clientCode, $cacertFile, $peerName);
61
62include 'CertificateGenerator.inc';
63$certificateGenerator = new CertificateGenerator();
64$certificateGenerator->saveCaCert($cacertFile);
65$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
66
67include 'ServerClientTestCase.inc';
68ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
69?>
70--CLEAN--
71<?php
72@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'session_meta_capture.pem.tmp');
73@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'session_meta_capture-ca.pem.tmp');
74?>
75--EXPECT--
76string(5) "TLSv1"
77string(7) "TLSv1.1"
78string(7) "TLSv1.2"
79