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