xref: /PHP-7.3/ext/openssl/tests/bug54992.phpt (revision 1fab01be)
1--TEST--
2Bug #54992: Stream not closed and error not returned when SSL CN_match fails
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 . 'bug54992.pem.tmp';
11$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug54992-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    ]]);
19
20    $server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
21    phpt_notify();
22
23    @stream_socket_accept($server, 1);
24CODE;
25$serverCode = sprintf($serverCode, $certFile);
26
27$peerName = 'bug54992_actual_peer_name';
28$wrongPeerName = 'bug54992_expected_peer_name';
29$clientCode = <<<'CODE'
30    $serverUri = "ssl://127.0.0.1:64321";
31    $clientFlags = STREAM_CLIENT_CONNECT;
32    $clientCtx = stream_context_create(['ssl' => [
33        'verify_peer' => true,
34        'cafile' => '%s',
35        'peer_name' => '%s',
36    ]]);
37
38    phpt_wait();
39    $client = stream_socket_client($serverUri, $errno, $errstr, 2, $clientFlags, $clientCtx);
40
41    var_dump($client);
42CODE;
43$clientCode = sprintf($clientCode, $cacertFile, $wrongPeerName);
44
45include 'CertificateGenerator.inc';
46$certificateGenerator = new CertificateGenerator();
47$certificateGenerator->saveCaCert($cacertFile);
48$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
49
50include 'ServerClientTestCase.inc';
51ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
52?>
53--CLEAN--
54<?php
55@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug54992.pem.tmp');
56@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug54992-ca.pem.tmp');
57?>
58--EXPECTF--
59Warning: stream_socket_client(): Peer certificate CN=`bug54992_actual_peer_name' did not match expected CN=`bug54992_expected_peer_name' in %s on line %d
60
61Warning: stream_socket_client(): Failed to enable crypto in %s on line %d
62
63Warning: stream_socket_client(): unable to connect to ssl://127.0.0.1:64321 (Unknown error) in %s on line %d
64bool(false)
65