1--TEST--
2Peer verification enabled for client streams
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 . 'peer_verification.pem.tmp';
11$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'peer_verification-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    for ($i = 0; $i < 5; $i++) {
24        @stream_socket_accept($server, 1);
25    }
26CODE;
27$serverCode = sprintf($serverCode, $certFile);
28
29$peerName = 'peer_verification';
30$clientCode = <<<'CODE'
31    $serverUri = "ssl://127.0.0.1:64321";
32    $clientFlags = STREAM_CLIENT_CONNECT;
33    $caFile = '%s';
34
35    phpt_wait();
36
37    // Expected to fail -- untrusted server cert and no CA File present
38    var_dump(@stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags));
39
40    // Expected to fail -- untrusted server cert and no CA File present
41    $clientCtx = stream_context_create(['ssl' => [
42        'verify_peer' => true,
43    ]]);
44    var_dump(@stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx));
45
46    // Should succeed with peer verification disabled in context
47    $clientCtx = stream_context_create(['ssl' => [
48        'verify_peer' => false,
49        'verify_peer_name' => false,
50    ]]);
51    var_dump(stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx));
52
53    // Should succeed with CA file specified in context
54    $clientCtx = stream_context_create(['ssl' => [
55        'cafile'   => $caFile,
56        'peer_name' => '%s',
57    ]]);
58    var_dump(stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx));
59CODE;
60$clientCode = sprintf($clientCode, $cacertFile, $peerName);
61
62include 'CertificateGenerator.inc';
63$certificateGenerator = new CertificateGenerator();
64$certificateGenerator->saveCaCert($cacertFile);
65$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
66
67include 'ServerClientTestCase.inc';
68ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
69?>
70--CLEAN--
71<?php
72@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'peer_verification.pem.tmp');
73@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'peer_verification-ca.pem.tmp');
74?>
75--EXPECTF--
76bool(false)
77bool(false)
78resource(%d) of type (stream)
79resource(%d) of type (stream)
80