1--TEST--
2TLS server rate-limits client-initiated renegotiation
3--SKIPIF--
4<?php
5if (!extension_loaded("openssl")) die("skip openssl not loaded");
6if (!function_exists("proc_open")) die("skip no proc_open");
7exec('openssl help', $out, $code);
8if ($code > 0) die("skip couldn't locate openssl binary");
9if(substr(PHP_OS, 0, 3) == 'WIN') {
10    die('skip not suitable for Windows');
11}
12?>
13--FILE--
14<?php
15$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_server_reneg_limit.pem.tmp';
16
17/**
18 * This test uses the openssl binary directly to initiate renegotiation. At this time it's not
19 * possible renegotiate the TLS handshake in PHP userland, so using the openssl s_client binary
20 * command is the only feasible way to test renegotiation limiting functionality. It's not an ideal
21 * solution, but it's really the only way to get test coverage on the rate-limiting functionality
22 * given current limitations.
23 */
24
25$serverCode = <<<'CODE'
26    $printed = false;
27    $serverUri = "ssl://127.0.0.1:64321";
28    $serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
29    $serverCtx = stream_context_create(['ssl' => [
30        'local_cert' => '%s',
31        'reneg_limit' => 0,
32        'reneg_window' => 30,
33        'reneg_limit_callback' => function($stream) use (&$printed) {
34            if (!$printed) {
35                $printed = true;
36                var_dump($stream);
37            }
38        }
39    ]]);
40
41    $server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
42    phpt_notify();
43
44    $clients = [];
45    while (1) {
46        $r = array_merge([$server], $clients);
47        $w = $e = [];
48
49        stream_select($r, $w, $e, $timeout=42);
50
51        foreach ($r as $sock) {
52            if ($sock === $server && ($client = stream_socket_accept($server, $timeout = 42))) {
53                $clientId = (int) $client;
54                $clients[$clientId] = $client;
55            } elseif ($sock !== $server) {
56                $clientId = (int) $sock;
57                $buffer = fread($sock, 1024);
58                if (strlen($buffer)) {
59                    continue;
60                } elseif (!is_resource($sock) || feof($sock)) {
61                    unset($clients[$clientId]);
62                    break 2;
63                }
64            }
65        }
66    }
67CODE;
68$serverCode = sprintf($serverCode, $certFile);
69
70$clientCode = <<<'CODE'
71    $cmd = 'openssl s_client -connect 127.0.0.1:64321';
72    $descriptorSpec = [["pipe", "r"], ["pipe", "w"], ["pipe", "w"]];
73    $process = proc_open($cmd, $descriptorSpec, $pipes);
74
75    list($stdin, $stdout, $stderr) = $pipes;
76
77    // Trigger renegotiation twice
78    // Server settings only allow one per second (should result in disconnection)
79    fwrite($stdin, "R\nR\nR\nR\n");
80
81    $lines = [];
82    while(!feof($stderr)) {
83        fgets($stderr);
84    }
85
86    fclose($stdin);
87    fclose($stdout);
88    fclose($stderr);
89    proc_terminate($process);
90CODE;
91
92include 'CertificateGenerator.inc';
93$certificateGenerator = new CertificateGenerator();
94$certificateGenerator->saveNewCertAsFileWithKey('stream_security_level', $certFile);
95
96include 'ServerClientTestCase.inc';
97ServerClientTestCase::getInstance()->run($serverCode, $clientCode);
98?>
99--CLEAN--
100<?php
101@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_server_reneg_limit.pem.tmp');
102?>
103--EXPECTF--
104resource(%d) of type (stream)
105