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