xref: /PHP-8.0/ext/openssl/tests/bug65729.phpt (revision dd7d161c)
1--TEST--
2Bug #65729: CN_match gives false positive when wildcard is used
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 . 'bug65729.pem.tmp';
11$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug65729-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    $expected_names = ['foo.test.com.sg', 'foo.test.com', 'FOO.TEST.COM', 'foo.bar.test.com'];
24    foreach ($expected_names as $name) {
25        @stream_socket_accept($server, 1);
26    }
27CODE;
28$serverCode = sprintf($serverCode, $certFile);
29
30$clientCode = <<<'CODE'
31    $serverUri = "ssl://127.0.0.1:64321";
32    $clientFlags = STREAM_CLIENT_CONNECT;
33
34    phpt_wait();
35
36    $expected_names = ['foo.test.com.sg', 'foo.test.com', 'FOO.TEST.COM', 'foo.bar.test.com'];
37    foreach ($expected_names as $expected_name) {
38        $clientCtx = stream_context_create(['ssl' => [
39            'verify_peer'        => true,
40            'peer_name'          => $expected_name,
41            'cafile'             => '%s',
42        ]]);
43
44        var_dump(stream_socket_client($serverUri, $errno, $errstr, 2, $clientFlags, $clientCtx));
45    }
46CODE;
47$clientCode = sprintf($clientCode, $cacertFile);
48
49include 'CertificateGenerator.inc';
50$certificateGenerator = new CertificateGenerator();
51$certificateGenerator->saveCaCert($cacertFile);
52$certificateGenerator->saveNewCertAsFileWithKey('*.test.com', $certFile);
53
54include 'ServerClientTestCase.inc';
55ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
56?>
57--CLEAN--
58<?php
59@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug65729.pem.tmp');
60@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug65729-ca.pem.tmp');
61?>
62--EXPECTF--
63Warning: stream_socket_client(): Peer certificate CN=`*.test.com' did not match expected CN=`foo.test.com.sg' in %s on line %d
64
65Warning: stream_socket_client(): Failed to enable crypto in %s on line %d
66
67Warning: stream_socket_client(): Unable to connect to ssl://127.0.0.1:64321 (Unknown error) in %s on line %d
68bool(false)
69resource(%d) of type (stream)
70resource(%d) of type (stream)
71
72Warning: stream_socket_client(): Peer certificate CN=`*.test.com' did not match expected CN=`foo.bar.test.com' in %s on line %d
73
74Warning: stream_socket_client(): Failed to enable crypto in %s on line %d
75
76Warning: stream_socket_client(): Unable to connect to ssl://127.0.0.1:64321 (Unknown error) in %s on line %d
77bool(false)
78