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