xref: /PHP-8.0/ext/openssl/tests/bug65538_003.phpt (revision 1fab01be)
1--TEST--
2Bug #65538: SSL context "cafile" supports phar wrapper
3--SKIPIF--
4<?php
5if (!extension_loaded("openssl")) die("skip openssl not loaded");
6if (!extension_loaded("phar")) die("skip phar not loaded");
7if (!function_exists("proc_open")) die("skip no proc_open");
8?>
9--INI--
10phar.readonly=0
11--FILE--
12<?php
13$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug65538_003.pem.tmp';
14
15$cacertFile = 'bug65538_003-ca.pem';
16$cacertPhar = __DIR__ . DIRECTORY_SEPARATOR . 'bug65538_003-ca.phar.tmp';
17
18$serverCode = <<<'CODE'
19    $serverUri = "ssl://127.0.0.1:64321";
20    $serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
21    $serverCtx = stream_context_create(['ssl' => [
22        'local_cert' => '%s',
23    ]]);
24
25    $server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
26    phpt_notify();
27
28    $client = @stream_socket_accept($server);
29    if ($client) {
30        $in = '';
31        while (!preg_match('/\r?\n\r?\n/', $in)) {
32            $in .= fread($client, 2048);
33        }
34        $response = "HTTP/1.0 200 OK\r\n"
35                  . "Content-Type: text/plain\r\n"
36                  . "Content-Length: 12\r\n"
37                  . "Connection: close\r\n"
38                  . "\r\n"
39                  . "Hello World!";
40        fwrite($client, $response);
41        fclose($client);
42    }
43CODE;
44$serverCode = sprintf($serverCode, $certFile);
45
46$peerName = 'bug65538_003';
47$clientCode = <<<'CODE'
48    $serverUri = "https://127.0.0.1:64321/";
49    $clientCtx = stream_context_create(['ssl' => [
50        'cafile' => 'phar://%s/%s',
51        'peer_name' => '%s',
52    ]]);
53
54    phpt_wait();
55    $html = file_get_contents($serverUri, false, $clientCtx);
56
57    var_dump($html);
58CODE;
59$clientCode = sprintf($clientCode, $cacertPhar, $cacertFile, $peerName);
60
61include 'CertificateGenerator.inc';
62$certificateGenerator = new CertificateGenerator();
63$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
64
65$phar = new Phar($cacertPhar);
66$phar->addFromString($cacertFile, $certificateGenerator->getCaCert());
67
68include 'ServerClientTestCase.inc';
69ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
70?>
71--CLEAN--
72<?php
73@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug65538_003.pem.tmp');
74@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug65538_003-ca.phar.tmp');
75?>
76--EXPECT--
77string(12) "Hello World!"
78