xref: /PHP-8.0/ext/openssl/tests/bug65538_001.phpt (revision 1fab01be)
1--TEST--
2Bug #65538: SSL context "cafile" supports stream wrappers
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 . 'bug65538_001.pem.tmp';
11$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug65538_001-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    $client = @stream_socket_accept($server);
24    if ($client) {
25        $in = '';
26        while (!preg_match('/\r?\n\r?\n/', $in)) {
27            $in .= fread($client, 2048);
28        }
29        $response = "HTTP/1.0 200 OK\r\n"
30                  . "Content-Type: text/plain\r\n"
31                  . "Content-Length: 12\r\n"
32                  . "Connection: close\r\n"
33                  . "\r\n"
34                  . "Hello World!";
35        fwrite($client, $response);
36        fclose($client);
37    }
38CODE;
39$serverCode = sprintf($serverCode, $certFile);
40
41$peerName = 'bug65538_001';
42$clientCode = <<<'CODE'
43    $serverUri = "https://127.0.0.1:64321/";
44    $clientCtx = stream_context_create(['ssl' => [
45        'cafile' => 'file://%s',
46        'peer_name' => '%s',
47    ]]);
48
49    phpt_wait();
50    $html = file_get_contents($serverUri, false, $clientCtx);
51
52    var_dump($html);
53CODE;
54$clientCode = sprintf($clientCode, $cacertFile, $peerName);
55
56include 'CertificateGenerator.inc';
57$certificateGenerator = new CertificateGenerator();
58$certificateGenerator->saveCaCert($cacertFile);
59$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
60
61include 'ServerClientTestCase.inc';
62ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
63?>
64--CLEAN--
65<?php
66@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug65538_001.pem.tmp');
67@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug65538_001-ca.pem.tmp');
68?>
69--EXPECT--
70string(12) "Hello World!"
71