summaryrefslogtreecommitdiff
path: root/ext/openssl/tests/stream_server_reneg_limit.phpt
blob: 04d1dc1f7a2fb0f6700aa7831ae67b097f733489 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
--TEST--
TLS server rate-limits client-initiated renegotiation
--SKIPIF--
<?php
if (!extension_loaded("openssl")) die("skip openssl not loaded");
if (!function_exists("proc_open")) die("skip no proc_open");
exec('openssl help', $out, $code);
if ($code > 0) die("skip couldn't locate openssl binary");
if(substr(PHP_OS, 0, 3) == 'WIN') {
    die('skip not suitable for Windows');
}
?>
--FILE--
<?php
$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_server_reneg_limit.pem.tmp';

/**
 * This test uses the openssl binary directly to initiate renegotiation. At this time it's not
 * possible renegotiate the TLS handshake in PHP userland, so using the openssl s_client binary
 * command is the only feasible way to test renegotiation limiting functionality. It's not an ideal
 * solution, but it's really the only way to get test coverage on the rate-limiting functionality
 * given current limitations.
 */

$serverCode = <<<'CODE'
    $printed = false;
    $serverUri = "ssl://127.0.0.1:64321";
    $serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
    $serverCtx = stream_context_create(['ssl' => [
        'local_cert' => '%s',
        'reneg_limit' => 0,
        'reneg_window' => 30,
        'reneg_limit_callback' => function($stream) use (&$printed) {
            if (!$printed) {
                $printed = true;
                var_dump($stream);
            }
        }
    ]]);

    $server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
    phpt_notify();

    $clients = [];
    while (1) {
        $r = array_merge([$server], $clients);
        $w = $e = [];

        stream_select($r, $w, $e, $timeout=42);

        foreach ($r as $sock) {
            if ($sock === $server && ($client = stream_socket_accept($server, $timeout = 42))) {
                $clientId = (int) $client;
                $clients[$clientId] = $client;
            } elseif ($sock !== $server) {
                $clientId = (int) $sock;
                $buffer = fread($sock, 1024);
                if (strlen($buffer)) {
                    continue;
                } elseif (!is_resource($sock) || feof($sock)) {
                    unset($clients[$clientId]);
                    break 2;
                }
            }
        }
    }
CODE;
$serverCode = sprintf($serverCode, $certFile);

$clientCode = <<<'CODE'
    $cmd = 'openssl s_client -connect 127.0.0.1:64321';
    $descriptorSpec = [["pipe", "r"], ["pipe", "w"], ["pipe", "w"]];
    $process = proc_open($cmd, $descriptorSpec, $pipes);

    list($stdin, $stdout, $stderr) = $pipes;

    // Trigger renegotiation twice
    // Server settings only allow one per second (should result in disconnection)
    fwrite($stdin, "R\nR\nR\nR\n");

    $lines = [];
    while(!feof($stderr)) {
        fgets($stderr);
    }

    fclose($stdin);
    fclose($stdout);
    fclose($stderr);
    proc_terminate($process);
CODE;

include 'CertificateGenerator.inc';
$certificateGenerator = new CertificateGenerator();
$certificateGenerator->saveNewCertAsFileWithKey('stream_security_level', $certFile);

include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($serverCode, $clientCode);
?>
--CLEAN--
<?php
@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_server_reneg_limit.pem.tmp');
?>
--EXPECTF--
resource(%d) of type (stream)