xref: /PHP-8.1/ext/openssl/tests/sni_server.phpt (revision 74859783)
1--TEST--
2sni_server
3--EXTENSIONS--
4openssl
5--SKIPIF--
6<?php
7if (!function_exists("proc_open")) die("skip no proc_open");
8?>
9--FILE--
10<?php
11$serverCode = <<<'CODE'
12    $flags = STREAM_SERVER_BIND|STREAM_SERVER_LISTEN;
13    $ctx = stream_context_create(['ssl' => [
14        'local_cert' => __DIR__ . '/domain1.pem',
15        'SNI_server_certs' => [
16            "cs.php.net" => __DIR__ . "/sni_server_cs.pem",
17            "uk.php.net" => __DIR__ . "/sni_server_uk.pem",
18            "us.php.net" => __DIR__ . "/sni_server_us.pem"
19        ]
20    ]]);
21
22    $server = stream_socket_server('tls://127.0.0.1:64321', $errno, $errstr, $flags, $ctx);
23    phpt_notify();
24
25    for ($i=0; $i < 3; $i++) {
26        @stream_socket_accept($server, 3);
27    }
28CODE;
29
30$clientCode = <<<'CODE'
31    $flags = STREAM_CLIENT_CONNECT;
32    $ctxArr = [
33        'cafile' => __DIR__ . '/sni_server_ca.pem',
34        'capture_peer_cert' => true
35    ];
36
37    phpt_wait();
38
39    $ctxArr['peer_name'] = 'cs.php.net';
40    $ctx = stream_context_create(['ssl' => $ctxArr]);
41    $client = stream_socket_client("tls://127.0.0.1:64321", $errno, $errstr, 1, $flags, $ctx);
42    $cert = stream_context_get_options($ctx)['ssl']['peer_certificate'];
43    var_dump(openssl_x509_parse($cert)['subject']['CN']);
44
45    $ctxArr['peer_name'] = 'uk.php.net';
46    $ctx = stream_context_create(['ssl' => $ctxArr]);
47    $client = @stream_socket_client("tls://127.0.0.1:64321", $errno, $errstr, 1, $flags, $ctx);
48    $cert = stream_context_get_options($ctx)['ssl']['peer_certificate'];
49    var_dump(openssl_x509_parse($cert)['subject']['CN']);
50
51    $ctxArr['peer_name'] = 'us.php.net';
52    $ctx = stream_context_create(['ssl' => $ctxArr]);
53    $client = @stream_socket_client("tls://127.0.0.1:64321", $errno, $errstr, 1, $flags, $ctx);
54    $cert = stream_context_get_options($ctx)['ssl']['peer_certificate'];
55    var_dump(openssl_x509_parse($cert)['subject']['CN']);
56CODE;
57
58include 'ServerClientTestCase.inc';
59ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
60?>
61--EXPECTF--
62string(%d) "cs.php.net"
63string(%d) "uk.php.net"
64string(%d) "us.php.net"
65