|
| 1 | +--TEST-- |
| 2 | +stream_copy_to_stream() from a TLS stream copies decrypted data (no fd fast-path) |
| 3 | +--EXTENSIONS-- |
| 4 | +openssl |
| 5 | +--SKIPIF-- |
| 6 | +<?php |
| 7 | +if (!function_exists("proc_open")) die("skip no proc_open"); |
| 8 | +?> |
| 9 | +--FILE-- |
| 10 | +<?php |
| 11 | + |
| 12 | +$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_copy_ssl.pem.tmp'; |
| 13 | +$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_copy_ssl-ca.pem.tmp'; |
| 14 | + |
| 15 | +$serverCode = <<<'CODE' |
| 16 | + $serverCtx = stream_context_create(['ssl' => [ |
| 17 | + 'local_cert' => '%s', |
| 18 | + ]]); |
| 19 | + $flags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN; |
| 20 | + $server = stream_socket_server("ssl://127.0.0.1:0", $errno, $errstr, $flags, $serverCtx); |
| 21 | + phpt_notify_server_start($server); |
| 22 | +
|
| 23 | + $conn = stream_socket_accept($server, 5); |
| 24 | + fwrite($conn, str_repeat("secret-", 1000)); |
| 25 | + fclose($conn); |
| 26 | + fclose($server); |
| 27 | +CODE; |
| 28 | +$serverCode = sprintf($serverCode, $certFile); |
| 29 | + |
| 30 | +$peerName = 'stream_copy_ssl_peer'; |
| 31 | +$clientCode = <<<'CODE' |
| 32 | + $clientCtx = stream_context_create(['ssl' => [ |
| 33 | + 'verify_peer' => true, |
| 34 | + 'cafile' => '%s', |
| 35 | + 'peer_name' => '%s', |
| 36 | + ]]); |
| 37 | + $client = stream_socket_client("ssl://{{ ADDR }}", $errno, $errstr, 5, STREAM_CLIENT_CONNECT, $clientCtx); |
| 38 | +
|
| 39 | + $tmp = tmpfile(); |
| 40 | + /* If the copy offloaded the raw socket fd it would write ciphertext; the |
| 41 | + * decrypted plaintext proves it correctly fell back to the userspace loop. */ |
| 42 | + $copied = stream_copy_to_stream($client, $tmp); |
| 43 | + var_dump($copied); |
| 44 | +
|
| 45 | + fseek($tmp, 0, SEEK_SET); |
| 46 | + $content = stream_get_contents($tmp); |
| 47 | + var_dump(strlen($content)); |
| 48 | + var_dump($content === str_repeat("secret-", 1000)); |
| 49 | +
|
| 50 | + fclose($tmp); |
| 51 | + fclose($client); |
| 52 | +CODE; |
| 53 | +$clientCode = sprintf($clientCode, $cacertFile, $peerName); |
| 54 | + |
| 55 | +include 'CertificateGenerator.inc'; |
| 56 | +$certificateGenerator = new CertificateGenerator(); |
| 57 | +$certificateGenerator->saveCaCert($cacertFile); |
| 58 | +$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile); |
| 59 | + |
| 60 | +include 'ServerClientTestCase.inc'; |
| 61 | +ServerClientTestCase::getInstance()->run($clientCode, $serverCode); |
| 62 | +?> |
| 63 | +--CLEAN-- |
| 64 | +<?php |
| 65 | +@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_copy_ssl.pem.tmp'); |
| 66 | +@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_copy_ssl-ca.pem.tmp'); |
| 67 | +?> |
| 68 | +--EXPECT-- |
| 69 | +int(7000) |
| 70 | +int(7000) |
| 71 | +bool(true) |
0 commit comments