1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
--TEST--
SNI 001
--SKIPIF--
<?php
if (!extension_loaded('openssl')) die("skip openssl extension not available");
if (!getenv('SNI_TESTS')) die("skip Set SNI_TESTS to enable this test (uses remote resources)");
?>
--FILE--
<?php
/* Server Name Indication (SNI) tests
*
* This test relies on https://sni.velox.ch/ and thus is disabled by default.
*
* sni.velox.ch uses 3 certificates :
* - CN=alice.sni.velox.ch (sent in response to server_name = alice.sni.velox.ch or not set)
* - CN=bob.sni.velox.ch (sent in response to server_name = bob.sni.velox.ch)
* - CN=*.sni.velox.ch (sent in response to server_name = mallory.sni.velox.ch or *.sni.velox.ch or sni.velox.ch)
*
* The test sends requests to the server, sending different names, and checks which certificate
* the server returned.
*/
function context($host = NULL) {
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'capture_peer_cert', true);
stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
if ($host) {
stream_context_set_option($ctx, 'ssl', 'peer_name', $host);
} else {
stream_context_set_option($ctx, 'ssl', 'verify_peer_name', false);
}
return $ctx;
}
function get_CN($context) {
$ary = stream_context_get_options($context);
assert($ary);
$cert = $ary['ssl']['peer_certificate'];
assert($cert);
$cert_ary = openssl_x509_parse($cert);
return $cert_ary['subject']['CN'];
}
function do_http_test($url, $context) {
$fh = fopen($url, 'r', false, $context);
assert($fh);
var_dump(get_CN($context));
}
function do_ssl_test($url, $context) {
$fh = stream_socket_client($url, $errno, $errstr,
ini_get("default_socket_timeout"), STREAM_CLIENT_CONNECT, $context);
assert($fh);
var_dump(get_CN($context));
}
function do_enable_crypto_test($url, $context) {
$fh = stream_socket_client($url, $errno, $errstr,
ini_get("default_socket_timeout"), STREAM_CLIENT_CONNECT, $context);
assert($fh);
$r = stream_socket_enable_crypto($fh, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
assert($r);
var_dump(get_CN($context));
}
/* Test https:// streams */
echo "-- auto host name (1) --\n";
do_http_test('https://alice.sni.velox.ch/', context('alice.sni.velox.ch'));
echo "-- auto host name (2) --\n";
do_http_test('https://bob.sni.velox.ch/', context('bob.sni.velox.ch'));
echo "-- auto host name (3) --\n";
do_http_test('https://bob.sni.velox.ch./', context('bob.sni.velox.ch'));
echo "-- user supplied server name --\n";
$context = context();
stream_context_set_option($context, 'ssl', 'peer_name', 'bob.sni.velox.ch');
stream_context_set_option($context, 'http', 'header', b'Host: bob.sni.velox.ch');
do_http_test('https://alice.sni.velox.ch/', $context);
echo "-- sni disabled --\n";
$context = context();
stream_context_set_option($context, 'ssl', 'SNI_enabled', false);
do_http_test('https://bob.sni.velox.ch/', $context);
/* Test ssl:// socket streams */
echo "-- raw SSL stream (1) --\n";
do_ssl_test('ssl://bob.sni.velox.ch:443', context('bob.sni.velox.ch'));
echo "-- raw SSL stream (2) --\n";
do_ssl_test('ssl://mallory.sni.velox.ch:443', context('mallory.sni.velox.ch'));
echo "-- raw SSL stream with user supplied sni --\n";
$context = context('bob.sni.velox.ch');
stream_context_set_option($context, 'ssl', 'peer_name', 'bob.sni.velox.ch');
do_ssl_test('ssl://mallory.sni.velox.ch:443', $context);
echo "-- raw SSL stream with sni disabled --\n";
$context = context();
stream_context_set_option($context, 'ssl', 'SNI_enabled', false);
do_ssl_test('ssl://mallory.sni.velox.ch:443', $context);
/* Test tcp:// socket streams with SSL enabled */
echo "-- stream_socket_enable_crypto (1) --\n";
do_enable_crypto_test('tcp://bob.sni.velox.ch:443', context());
echo "-- stream_socket_enable_crypto (2) --\n";
do_enable_crypto_test('tcp://mallory.sni.velox.ch:443', context());
echo "-- stream_socket_enable_crypto with user supplied sni --\n";
$context = context();
stream_context_set_option($context, 'ssl', 'peer_name', 'bob.sni.velox.ch');
do_enable_crypto_test('tcp://mallory.sni.velox.ch:443', $context);
echo "-- stream_socket_enable_crypto with sni disabled --\n";
$context = context();
stream_context_set_option($context, 'ssl', 'SNI_enabled', false);
do_enable_crypto_test('tcp://mallory.sni.velox.ch:443', $context);
echo "-- stream_socket_enable_crypto with long name --\n";
$context = context();
stream_context_set_option($context, 'ssl', 'peer_name', str_repeat('a.', 500) . '.sni.velox.ch');
do_enable_crypto_test('tcp://mallory.sni.velox.ch:443', $context);
?>
--EXPECTF--
-- auto host name (1) --
%unicode|string%(18) "alice.sni.velox.ch"
-- auto host name (2) --
%unicode|string%(16) "bob.sni.velox.ch"
-- auto host name (3) --
%unicode|string%(16) "bob.sni.velox.ch"
-- user supplied server name --
%unicode|string%(16) "bob.sni.velox.ch"
-- sni disabled --
%unicode|string%(18) "alice.sni.velox.ch"
-- raw SSL stream (1) --
%unicode|string%(16) "bob.sni.velox.ch"
-- raw SSL stream (2) --
%unicode|string%(14) "*.sni.velox.ch"
-- raw SSL stream with user supplied sni --
%unicode|string%(16) "bob.sni.velox.ch"
-- raw SSL stream with sni disabled --
%unicode|string%(18) "alice.sni.velox.ch"
-- stream_socket_enable_crypto (1) --
%unicode|string%(16) "bob.sni.velox.ch"
-- stream_socket_enable_crypto (2) --
%unicode|string%(14) "*.sni.velox.ch"
-- stream_socket_enable_crypto with user supplied sni --
%unicode|string%(16) "bob.sni.velox.ch"
-- stream_socket_enable_crypto with sni disabled --
%unicode|string%(18) "alice.sni.velox.ch"
-- stream_socket_enable_crypto with long name --
%unicode|string%(18) "alice.sni.velox.ch"
|