1--TEST--
2Server bitwise stream crypto flag assignment
3--SKIPIF--
4<?php
5if (!extension_loaded("openssl")) die("skip openssl not loaded");
6if (!function_exists("proc_open")) die("skip no proc_open");
7if (OPENSSL_VERSION_NUMBER < 0x10001001) die("skip OpenSSLv1.0.1 required");
8?>
9--FILE--
10<?php
11$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_003.pem.tmp';
12$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_003-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        // Only accept TLSv1.2 connections
21        'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_0_SERVER  | STREAM_CRYPTO_METHOD_TLSv1_2_SERVER,
22    ]]);
23
24    $server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
25    phpt_notify();
26
27    @stream_socket_accept($server, 1);
28    @stream_socket_accept($server, 1);
29    @stream_socket_accept($server, 1);
30    @stream_socket_accept($server, 1);
31CODE;
32$serverCode = sprintf($serverCode, $certFile);
33
34$peerName = 'stream_crypto_flags_003';
35$clientCode = <<<'CODE'
36    $serverUri = "ssl://127.0.0.1:64321";
37    $clientFlags = STREAM_CLIENT_CONNECT;
38    $clientCtx = stream_context_create(['ssl' => [
39        'verify_peer' => true,
40        'cafile' => '%s',
41        'peer_name' => '%s',
42    ]]);
43
44    phpt_wait();
45
46    stream_context_set_option($clientCtx, 'ssl', 'crypto_method', STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT);
47    var_dump(stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx));
48
49    stream_context_set_option($clientCtx, 'ssl', 'crypto_method', STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT);
50    var_dump(@stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx));
51
52    stream_context_set_option($clientCtx, 'ssl', 'crypto_method', STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT);
53    var_dump(@stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx));
54CODE;
55$clientCode = sprintf($clientCode, $cacertFile, $peerName);
56
57include 'CertificateGenerator.inc';
58$certificateGenerator = new CertificateGenerator();
59$certificateGenerator->saveCaCert($cacertFile);
60$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
61
62include 'ServerClientTestCase.inc';
63ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
64?>
65--CLEAN--
66<?php
67@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_003.pem.tmp');
68@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_003-ca.pem.tmp');
69?>
70--EXPECTF--
71resource(%d) of type (stream)
72resource(%d) of type (stream)
73resource(%d) of type (stream)
74