--- /dev/null
+<?php
+
+class CertificateGenerator
+{
+ const CONFIG = __DIR__. DIRECTORY_SEPARATOR . 'openssl.cnf';
+
+ /** @var resource */
+ private $ca;
+
+ /** @var resource */
+ private $caKey;
+
+ /** @var resource|null */
+ private $lastCert;
+
+ /** @var resource|null */
+ private $lastKey;
+
+ public function __construct()
+ {
+ if (!extension_loaded('openssl')) {
+ throw new RuntimeException(
+ 'openssl extension must be loaded to generate certificates'
+ );
+ }
+ $this->generateCa();
+ }
+
+ /**
+ * @param int|null $keyLength
+ * @return resource
+ */
+ private static function generateKey($keyLength = null)
+ {
+ if (null === $keyLength) {
+ $keyLength = 2048;
+ }
+
+ return openssl_pkey_new([
+ 'private_key_bits' => $keyLength,
+ 'private_key_type' => OPENSSL_KEYTYPE_RSA,
+ 'encrypt_key' => false,
+ ]);
+ }
+
+ private function generateCa()
+ {
+ $this->caKey = self::generateKey();
+ $dn = [
+ 'countryName' => 'GB',
+ 'stateOrProvinceName' => 'Berkshire',
+ 'localityName' => 'Newbury',
+ 'organizationName' => 'Example Certificate Authority',
+ 'commonName' => 'CA for PHP Tests'
+ ];
+
+ $this->ca = openssl_csr_sign(
+ openssl_csr_new(
+ $dn,
+ $this->caKey,
+ [
+ 'x509_extensions' => 'v3_ca',
+ 'config' => self::CONFIG,
+ ]
+ ),
+ null,
+ $this->caKey,
+ 2
+ );
+ }
+
+ public function getCaCert()
+ {
+ $output = '';
+ openssl_x509_export($this->ca, $output);
+
+ return $output;
+ }
+
+ public function saveCaCert($file)
+ {
+ openssl_x509_export_to_file($this->ca, $file);
+ }
+
+ public function saveNewCertAsFileWithKey($commonNameForCert, $file, $keyLength = null)
+ {
+ $dn = [
+ 'countryName' => 'BY',
+ 'stateOrProvinceName' => 'Minsk',
+ 'localityName' => 'Minsk',
+ 'organizationName' => 'Example Org',
+ 'commonName' => $commonNameForCert,
+ ];
+
+ $this->lastKey = self::generateKey($keyLength);
+ $this->lastCert = openssl_csr_sign(
+ openssl_csr_new($dn, $this->lastKey, ['req_extensions' => 'v3_req']),
+ $this->ca,
+ $this->caKey,
+ 2
+ );
+
+ $certText = '';
+ openssl_x509_export($this->lastCert, $certText);
+
+ $keyText = '';
+ openssl_pkey_export($this->lastKey, $keyText);
+
+ file_put_contents($file, $certText . PHP_EOL . $keyText);
+ }
+
+ public function getCertDigest($algo)
+ {
+ return openssl_x509_fingerprint($this->lastCert, $algo);
+ }
+}
+++ /dev/null
------BEGIN CERTIFICATE-----
-MIIC5jCCAk+gAwIBAgIBADANBgkqhkiG9w0BAQQFADBcMQswCQYDVQQGEwJBVTET
-MBEGA1UECBMKUXVlZW5zbGFuZDEaMBgGA1UEChMRQ3J5cHRTb2Z0IFB0eSBMdGQx
-HDAaBgNVBAMTE1Rlc3QgUENBICgxMDI0IGJpdCkwHhcNOTkxMjAyMjEzNTQ4WhcN
-MDUwNzExMjEzNTQ4WjBcMQswCQYDVQQGEwJBVTETMBEGA1UECBMKUXVlZW5zbGFu
-ZDEaMBgGA1UEChMRQ3J5cHRTb2Z0IFB0eSBMdGQxHDAaBgNVBAMTE1Rlc3QgUENB
-ICgxMDI0IGJpdCkwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAJ2haT/f5Zwy
-V+MiuSDjSR62adBoSiBB7Usty44lXqsp9RICw+DCCxpsn/CfxPEDXLLd4olsWXc6
-JRcxGynbYmnzk+Z6aIPPJQhK3CTvaqGnWKZsA1m+WaUIUqJCuNTK4N+7hMAGaf6S
-S3e9HVgEQ4a34gXJ7VQFVIBNV1EnZRWHAgMBAAGjgbcwgbQwHQYDVR0OBBYEFE0R
-aEcrj18q1dw+G6nJbsTWR213MIGEBgNVHSMEfTB7gBRNEWhHK49fKtXcPhupyW7E
-1kdtd6FgpF4wXDELMAkGA1UEBhMCQVUxEzARBgNVBAgTClF1ZWVuc2xhbmQxGjAY
-BgNVBAoTEUNyeXB0U29mdCBQdHkgTHRkMRwwGgYDVQQDExNUZXN0IFBDQSAoMTAy
-NCBiaXQpggEA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7NPCUF9nhU3jh60qWWO8mkc=
------END RSA PRIVATE KEY-----
--TEST--
-#46127, openssl_sign/verify: accept different algos
+#46127 php_openssl_tcp_sockop_accept forgets to set context on accepted stream
--SKIPIF--
<?php
if (!extension_loaded("openssl")) die("skip openssl not loaded");
?>
--FILE--
<?php
+$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug46127.pem.tmp';
+
$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
- 'local_cert' => __DIR__ . '/bug46127.pem',
+ 'local_cert' => '%s',
]]);
$sock = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
$link = stream_socket_accept($sock);
fwrite($link, "Sending bug 46127\n");
CODE;
+$serverCode = sprintf($serverCode, $certFile);
$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
echo fgets($sock);
CODE;
+include 'CertificateGenerator.inc';
+$certificateGenerator = new CertificateGenerator();
+$certificateGenerator->saveNewCertAsFileWithKey('bug46127', $certFile);
+
include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug46127.pem.tmp');
+?>
--EXPECT--
Sending bug 46127
?>
--FILE--
<?php
+$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug48182.pem.tmp';
+$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug48182-ca.pem.tmp';
+
$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
- 'local_cert' => __DIR__ . '/bug54992.pem'
+ 'local_cert' => '%s'
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
$data = "Sending bug48182\n" . fread($client, 8192);
fwrite($client, $data);
CODE;
+$serverCode = sprintf($serverCode, $certFile);
+$peerName = 'bug48182';
$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$clientFlags = STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT;
$clientCtx = stream_context_create(['ssl' => [
- 'cafile' => __DIR__ . '/bug54992-ca.pem',
- 'peer_name' => 'bug54992.local'
+ 'cafile' => '%s',
+ 'peer_name' => '%s'
]]);
phpt_wait();
fwrite($client, $data);
echo fread($client, 1024);
CODE;
+$clientCode = sprintf($clientCode, $cacertFile, $peerName);
echo "Running bug48182\n";
+include 'CertificateGenerator.inc';
+$certificateGenerator = new CertificateGenerator();
+$certificateGenerator->saveCaCert($cacertFile);
+$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
+
include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
---EXPECTF--
+--CLEAN--
+<?php
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug48182.pem.tmp');
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug48182-ca.pem.tmp');
+?>
+--EXPECT--
Running bug48182
Sending bug48182
Sending data over to SSL server in async mode with contents like Hello World
+++ /dev/null
------BEGIN CERTIFICATE-----
-MIIGAzCCA+ugAwIBAgIUZ7ZvvfVqSEf1EswMT9LfMIPc/U8wDQYJKoZIhvcNAQEL
-BQAwgZAxCzAJBgNVBAYTAlBUMQ8wDQYDVQQIDAZMaXNib2ExDzANBgNVBAcMBkxp
-c2JvYTEXMBUGA1UECgwOUEhQIEZvdW5kYXRpb24xHjAcBgNVBAMMFVJvb3QgQ0Eg
-Zm9yIFBIUCBUZXN0czEmMCQGCSqGSIb3DQEJARYXaW50ZXJuYWxzQGxpc3RzLnBo
-cC5uZXQwHhcNMTgxMjMxMDg0NDU3WhcNMjAwMjA0MDg0NDU3WjCBkDELMAkGA1UE
-BhMCUFQxDzANBgNVBAgMBkxpc2JvYTEPM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EGIN RSA PRIVATE KEY-----
-MIICXgIBAAKBgQC1QBVApOClQ+C0U4nfDeQMkRNLjm1Z5P3qRSDU15gspb6M1sWH
-E+g8m/teF96YJqqHa1urVx1hb3TpanUv541C91NgDYhAbgRAFSWZ/IhqQ4gyw39G
-LNWQXT2+U1DBOloympO/vAHy+/BvFv2NsLZmFBQESDo8ifXx1Ky1sz0VnQIDAQAB
-AoGBALUEnHUkdgv4P7o5WJACAomedqPWSlYmgoVvpvuLmrq0ihuFAGAIvL+TlTgD
-JNfWfiejTDlSVtCSDTR1kzZVztitfXDxRkWEjGtFjMhk/DJkql3w10SUtcqCiWqw
-/XknyPHZ7A+w7Fu5KRO2LoSIze2ZLKvCfP/M/pLR2fTKGTHtAkEA2NreT1GUnvzj
-u1lb2J0nTZbSQHvEkfpEej9akl0Bc5UkskenEsiXE3cJYA1TbEGSqYCmt23x3Rd2
-FYxm6MwV6wJBANX34ZuUOllsS0FJPbkEAps3M4s59daQSFiEkQc5XjPgVB0xVV7s
-OEBlGkM3eqcCUOMnMI8L9wfBk49sELZCeJcCQQC/y/TL2q/EXo9c6I/faj+U1Exp
-VA5rvhpKtTX6NeBOxh6Kv+z5JAja4nGcTqz2FpkM6giKO+erUFDUhjWOuNK5AkEA
-xkmHnCRLxp8jRodXWeQrfigz7ixydLsVMGL5+9XgRPb5PGyBjwwePR70raH2Wls9
-FqU0zPvrnBZ6Zwlgm2cSVQJAPLYA51Z9piajbTuggpioQ5qbUEDkJjmYHbm8eJnK
-h5NW/EtCk4SBxAc+8ElPrvJjtZyOPWfm4vZF5sDKtC3Fkg==
------END RSA PRIVATE KEY-----
?>
--FILE--
<?php
-/*
- How to generate bug54992.pem and bug54992-ca.pem and all dependants:
+$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug54992.pem.tmp';
+$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug54992-ca.pem.tmp';
- All the commands below assume you're in the root of php sources
-
- Generate new key for CA:
- $ openssl genrsa -out ./ext/openssl/tests/bug54992-ca.key 4096
-
- Create new CA:
- $ openssl req -new -x509 -key ./ext/openssl/tests/bug54992-ca.key \
- -out ext/openssl/tests/bug54992-ca.pem \
- -subj '/C=PT/ST=Lisboa/L=Lisboa/O=PHP Foundation/CN=Root CA for PHP Tests/emailAddress=internals@lists.php.net' \
- -days 400
-
- Extract private key from the bundle:
- $ openssl rsa -in ext/openssl/tests/bug54992.pem > ext/openssl/tests/bug54992.key
-
- Extract CSR from existing certificate:
- $ openssl x509 -x509toreq -in ext/openssl/tests/bug54992.pem -out ext/openssl/tests/bug54992.csr -signkey ext/openssl/tests/bug54992.key
-
- Sign the CSR:
- $ openssl x509 -CA ext/openssl/tests/bug54992-ca.pem \
- -CAcreateserial \
- -CAkey ./ext/openssl/tests/bug54992-ca.key \
- -req \
- -in ext/openssl/tests/bug54992.csr \
- -sha256 \
- -days 400 \
- -out ./ext/openssl/tests/bug54992.pem
-
- Bundle certificate's private key with the certificate:
- $ cat ext/openssl/tests/bug54992.key >> ext/openssl/tests/bug54992.pem\
-
-
- Dependants:
-
- 1. ext/openssl/tests/bug65538_003.phpt
- Run the following to generate required phar:
- php -d phar.readonly=Off -r '$phar = new Phar("ext/openssl/tests/bug65538.phar"); $phar->addFile("ext/openssl/tests/bug54992.pem", "bug54992.pem"); $phar->addFile("ext/openssl/tests/bug54992-ca.pem", "bug54992-ca.pem");'
-
- 2. Update ext/openssl/tests/openssl_peer_fingerprint_basic.phpt (see instructions in there)
- */
$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
- 'local_cert' => __DIR__ . '/bug54992.pem',
+ 'local_cert' => '%s',
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
@stream_socket_accept($server, 1);
CODE;
+$serverCode = sprintf($serverCode, $certFile);
+$peerName = 'bug54992_actual_peer_name';
+$wrongPeerName = 'bug54992_expected_peer_name';
$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$clientFlags = STREAM_CLIENT_CONNECT;
$clientCtx = stream_context_create(['ssl' => [
'verify_peer' => true,
- 'cafile' => __DIR__ . '/bug54992-ca.pem',
- 'peer_name' => 'buga_buga',
+ 'cafile' => '%s',
+ 'peer_name' => '%s',
]]);
phpt_wait();
var_dump($client);
CODE;
+$clientCode = sprintf($clientCode, $cacertFile, $wrongPeerName);
+
+include 'CertificateGenerator.inc';
+$certificateGenerator = new CertificateGenerator();
+$certificateGenerator->saveCaCert($cacertFile);
+$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug54992.pem.tmp');
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug54992-ca.pem.tmp');
+?>
--EXPECTF--
-Warning: stream_socket_client(): Peer certificate CN=`bug54992.local' did not match expected CN=`buga_buga' in %s on line %d
+Warning: stream_socket_client(): Peer certificate CN=`bug54992_actual_peer_name' did not match expected CN=`bug54992_expected_peer_name' in %s on line %d
Warning: stream_socket_client(): Failed to enable crypto in %s on line %d
?>
--FILE--
<?php
+$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug65538_001.pem.tmp';
+$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug65538_001-ca.pem.tmp';
+
$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
- 'local_cert' => __DIR__ . '/bug54992.pem',
+ 'local_cert' => '%s',
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
fclose($client);
}
CODE;
+$serverCode = sprintf($serverCode, $certFile);
+$peerName = 'bug65538_001';
$clientCode = <<<'CODE'
$serverUri = "https://127.0.0.1:64321/";
$clientCtx = stream_context_create(['ssl' => [
- 'cafile' => 'file://' . __DIR__ . '/bug54992-ca.pem',
- 'peer_name' => 'bug54992.local',
+ 'cafile' => 'file://%s',
+ 'peer_name' => '%s',
]]);
phpt_wait();
var_dump($html);
CODE;
+$clientCode = sprintf($clientCode, $cacertFile, $peerName);
+
+include 'CertificateGenerator.inc';
+$certificateGenerator = new CertificateGenerator();
+$certificateGenerator->saveCaCert($cacertFile);
+$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug65538_001.pem.tmp');
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug65538_001-ca.pem.tmp');
+?>
--EXPECT--
string(12) "Hello World!"
if (!extension_loaded("phar")) die("skip phar not loaded");
if (!function_exists("proc_open")) die("skip no proc_open");
?>
+--INI--
+phar.readonly=0
--FILE--
<?php
+$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug65538_003.pem.tmp';
+
+$cacertFile = 'bug65538_003-ca.pem';
+$cacertPhar = __DIR__ . DIRECTORY_SEPARATOR . 'bug65538_003-ca.phar.tmp';
+
$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
- 'local_cert' => __DIR__ . '/bug54992.pem',
+ 'local_cert' => '%s',
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
fclose($client);
}
CODE;
+$serverCode = sprintf($serverCode, $certFile);
+$peerName = 'bug65538_003';
$clientCode = <<<'CODE'
$serverUri = "https://127.0.0.1:64321/";
$clientCtx = stream_context_create(['ssl' => [
- 'cafile' => 'phar://' . __DIR__ . '/bug65538.phar/bug54992-ca.pem',
- 'peer_name' => 'bug54992.local',
+ 'cafile' => 'phar://%s/%s',
+ 'peer_name' => '%s',
]]);
phpt_wait();
var_dump($html);
CODE;
+$clientCode = sprintf($clientCode, $cacertPhar, $cacertFile, $peerName);
+
+include 'CertificateGenerator.inc';
+$certificateGenerator = new CertificateGenerator();
+$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
+
+$phar = new Phar($cacertPhar);
+$phar->addFromString($cacertFile, $certificateGenerator->getCaCert());
include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
---EXPECTF--
+--CLEAN--
+<?php
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug65538_003.pem.tmp');
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug65538_003-ca.phar.tmp');
+?>
+--EXPECT--
string(12) "Hello World!"
?>
--FILE--
<?php
+$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug72333.pem.tmp';
+
$serverCode = <<<'CODE'
- $context = stream_context_create(['ssl' => ['local_cert' => __DIR__ . '/bug54992.pem']]);
+ $context = stream_context_create(['ssl' => ['local_cert' => '%s']]);
$flags = STREAM_SERVER_BIND|STREAM_SERVER_LISTEN;
$fp = stream_socket_server("ssl://127.0.0.1:10011", $errornum, $errorstr, $flags, $context);
}
phpt_wait();
CODE;
+$serverCode = sprintf($serverCode, $certFile);
+$peerName = 'bug72333';
$clientCode = <<<'CODE'
- $context = stream_context_create(['ssl' => ['verify_peer' => false, 'peer_name' => 'bug54992.local']]);
-
+ $context = stream_context_create(['ssl' => ['verify_peer' => false, 'peer_name' => '%s']]);
+
phpt_wait();
$fp = stream_socket_client("ssl://127.0.0.1:10011", $errornum, $errorstr, 3000, STREAM_CLIENT_CONNECT, $context);
stream_set_blocking($fp, false);
-
+
function blocking_fwrite($fp, $buf) {
$write = [$fp];
$total = 0;
phpt_notify();
echo "done";
CODE;
+$clientCode = sprintf($clientCode, $peerName);
+
+include 'CertificateGenerator.inc';
+$certificateGenerator = new CertificateGenerator();
+$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug72333.pem.tmp');
+?>
--EXPECT--
done
?>
--FILE--
<?php
+$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug74159.pem.tmp';
+$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug74159-ca.pem.tmp';
+
// the server code is doing many readings in a short interval which is
// not really reliable on more powerful machine but cover different
// scenarios which might be useful. More reliable test is bug72333.phpt
$serverUri = "ssl://127.0.0.1:10012";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
- 'local_cert' => __DIR__ . '/bug54992.pem',
+ 'local_cert' => '%s',
'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_SERVER,
]]);
fclose($client);
CODE;
+$serverCode = sprintf($serverCode, $certFile);
+$peerName = 'bug74159';
$clientCode = <<<'CODE'
function streamRead($stream) : int {
return strlen(fread($stream, 8192));
$clientFlags = STREAM_CLIENT_CONNECT;
$clientCtx = stream_context_create(['ssl' => [
'verify_peer' => true,
- 'cafile' => __DIR__ . '/bug54992-ca.pem',
- 'peer_name' => 'bug54992.local',
+ 'cafile' => '%s',
+ 'peer_name' => '%s',
]]);
phpt_wait();
$data = substr($data, $written);
waitForWrite($fp);
}
- printf("Written %d bytes\n", $total);
+ printf("Written %%d bytes\n", $total);
while(waitForRead($fp)) {
streamRead($fp);
exit("DONE\n");
CODE;
+$clientCode = sprintf($clientCode, $cacertFile, $peerName);
+
+include 'CertificateGenerator.inc';
+$certificateGenerator = new CertificateGenerator();
+$certificateGenerator->saveCaCert($cacertFile);
+$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
---EXPECTF--
+--CLEAN--
+<?php
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug74159.pem.tmp');
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug74159-ca.pem.tmp');
+?>
+--EXPECT--
Written 1048575 bytes
DONE
?>
--FILE--
<?php
+$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'capture_peer_cert_001.pem.tmp';
+$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'capture_peer_cert_001-ca.pem.tmp';
+
$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
- 'local_cert' => __DIR__ . '/bug54992.pem'
+ 'local_cert' => '%s'
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
@stream_socket_accept($server, 1);
CODE;
+$serverCode = sprintf($serverCode, $certFile);
+$peerName = 'capture_peer_cert_001';
$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$clientFlags = STREAM_CLIENT_CONNECT;
$clientCtx = stream_context_create(['ssl' => [
'capture_peer_cert' => true,
- 'cafile' => __DIR__ . '/bug54992-ca.pem'
+ 'cafile' => '%s'
]]);
phpt_wait();
$cert = stream_context_get_options($clientCtx)['ssl']['peer_certificate'];
var_dump(openssl_x509_parse($cert)['subject']['CN']);
CODE;
+$clientCode = sprintf($clientCode, $cacertFile);
+
+include 'CertificateGenerator.inc';
+$certificateGenerator = new CertificateGenerator();
+$certificateGenerator->saveCaCert($cacertFile);
+$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'capture_peer_cert_001.pem.tmp');
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'capture_peer_cert_001-ca.pem.tmp');
+?>
--EXPECTF--
-string(%d) "bug54992.local"
+string(%d) "capture_peer_cert_001"
?>
--FILE--
<?php
+$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'openssl_peer_fingerprint_basic.pem.tmp';
+$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'openssl_peer_fingerprint_basic-ca.pem.tmp';
+
$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
- 'local_cert' => __DIR__ . '/bug54992.pem'
+ 'local_cert' => '%s'
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
@stream_socket_accept($server, 1);
@stream_socket_accept($server, 1);
CODE;
+$serverCode = sprintf($serverCode, $certFile);
+$peerName = 'openssl_peer_fingerprint_basic';
$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$clientFlags = STREAM_CLIENT_CONNECT;
$clientCtx = stream_context_create(['ssl' => [
'verify_peer' => true,
- 'cafile' => __DIR__ . '/bug54992-ca.pem',
- 'capture_peer_cert' => true,
- 'peer_name' => 'bug54992.local',
+ 'cafile' => '%s',
+ 'capture_peer_cert' => true,
+ 'peer_name' => '%s',
]]);
phpt_wait();
- // Run the following to get actual md5 (from sources root):
- // openssl x509 -noout -fingerprint -md5 -inform pem -in ext/openssl/tests/bug54992.pem | cut -d '=' -f 2 | tr -d ':' | tr 'A-F' 'a-f'
- // Currently it's 4edbbaf40a6a4b6af22b6d6d9818378f
- // One below is intentionally broken (compare the last character):
- stream_context_set_option($clientCtx, 'ssl', 'peer_fingerprint', '4edbbaf40a6a4b6af22b6d6d98183780');
+ stream_context_set_option($clientCtx, 'ssl', 'peer_fingerprint', '%s');
var_dump(stream_socket_client($serverUri, $errno, $errstr, 2, $clientFlags, $clientCtx));
- // Run the following to get actual sha256 (from sources root):
- // openssl x509 -noout -fingerprint -sha256 -inform pem -in ext/openssl/tests/bug54992.pem | cut -d '=' -f 2 | tr -d ':' | tr 'A-F' 'a-f'
stream_context_set_option($clientCtx, 'ssl', 'peer_fingerprint', [
- 'sha256' => 'b1d480a2f83594fa243d26378cf611f334d369e59558d87e3de1abe8f36cb997',
+ 'sha256' => '%s',
]);
var_dump(stream_socket_client($serverUri, $errno, $errstr, 2, $clientFlags, $clientCtx));
CODE;
+include 'CertificateGenerator.inc';
+$certificateGenerator = new CertificateGenerator();
+$certificateGenerator->saveCaCert($cacertFile);
+$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
+
+$actualMd5 = $certificateGenerator->getCertDigest('md5');
+$lastCharacter = substr($actualMd5, -1, 1);
+$brokenLastCharacter = dechex(hexdec($lastCharacter) ^ 1);
+$brokenMd5 = substr($actualMd5, 0, -1) . $brokenLastCharacter;
+$actualSha256 = $certificateGenerator->getCertDigest('sha256');
+
+$clientCode = sprintf($clientCode, $cacertFile, $peerName, $brokenMd5, $actualSha256);
+
+
include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'openssl_peer_fingerprint_basic.pem.tmp');
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'openssl_peer_fingerprint_basic-ca.pem.tmp');
+?>
--EXPECTF--
Warning: stream_socket_client(): peer_fingerprint match failure in %s on line %d
?>
--FILE--
<?php
+$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'peer_verification.pem.tmp';
+$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'peer_verification-ca.pem.tmp';
+
$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
- 'local_cert' => __DIR__ . '/bug54992.pem'
+ 'local_cert' => '%s'
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
@stream_socket_accept($server, 1);
}
CODE;
+$serverCode = sprintf($serverCode, $certFile);
+$peerName = 'peer_verification';
$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$clientFlags = STREAM_CLIENT_CONNECT;
- $caFile = __DIR__ . '/bug54992-ca.pem';
+ $caFile = '%s';
phpt_wait();
// Should succeed with CA file specified in context
$clientCtx = stream_context_create(['ssl' => [
'cafile' => $caFile,
- 'peer_name' => 'bug54992.local',
+ 'peer_name' => '%s',
]]);
var_dump(stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx));
CODE;
+$clientCode = sprintf($clientCode, $cacertFile, $peerName);
+
+include 'CertificateGenerator.inc';
+$certificateGenerator = new CertificateGenerator();
+$certificateGenerator->saveCaCert($cacertFile);
+$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'peer_verification.pem.tmp');
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'peer_verification-ca.pem.tmp');
+?>
--EXPECTF--
bool(false)
bool(false)
?>
--FILE--
<?php
+$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'session_meta_capture.pem.tmp';
+$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'session_meta_capture-ca.pem.tmp';
+
$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
- 'local_cert' => __DIR__ . '/bug54992.pem'
+ 'local_cert' => '%s'
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
@stream_socket_accept($server, 1);
@stream_socket_accept($server, 1);
CODE;
+$serverCode = sprintf($serverCode, $certFile);
+$peerName = 'session_meta_capture';
$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$clientFlags = STREAM_CLIENT_CONNECT;
$clientCtx = stream_context_create(['ssl' => [
'verify_peer' => true,
- 'cafile' => __DIR__ . '/bug54992-ca.pem',
- 'peer_name' => 'bug54992.local',
+ 'cafile' => '%s',
+ 'peer_name' => '%s',
'capture_session_meta' => true,
]]);
$meta = stream_context_get_options($clientCtx)['ssl']['session_meta'];
var_dump($meta['protocol']);
CODE;
+$clientCode = sprintf($clientCode, $cacertFile, $peerName);
+
+include 'CertificateGenerator.inc';
+$certificateGenerator = new CertificateGenerator();
+$certificateGenerator->saveCaCert($cacertFile);
+$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
---EXPECTF--
+--CLEAN--
+<?php
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'session_meta_capture.pem.tmp');
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'session_meta_capture-ca.pem.tmp');
+?>
+--EXPECT--
string(5) "TLSv1"
string(7) "TLSv1.1"
string(7) "TLSv1.2"
?>
--FILE--
<?php
+$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_001.pem.tmp';
+$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_001-ca.pem.tmp';
+
$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
- 'local_cert' => __DIR__ . '/bug54992.pem'
+ 'local_cert' => '%s'
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
@stream_socket_accept($server, 1);
@stream_socket_accept($server, 1);
CODE;
+$serverCode = sprintf($serverCode, $certFile);
+$peerName = 'stream_crypto_flags_001';
$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$clientFlags = STREAM_CLIENT_CONNECT;
$clientCtx = stream_context_create(['ssl' => [
'verify_peer' => true,
- 'cafile' => __DIR__ . '/bug54992-ca.pem',
- 'peer_name' => 'bug54992.local',
+ 'cafile' => '%s',
+ 'peer_name' => '%s',
]]);
phpt_wait();
stream_context_set_option($clientCtx, 'ssl', 'crypto_method', STREAM_CRYPTO_METHOD_TLS_CLIENT);
var_dump(stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx));
CODE;
+$clientCode = sprintf($clientCode, $cacertFile, $peerName);
+
+include 'CertificateGenerator.inc';
+$certificateGenerator = new CertificateGenerator();
+$certificateGenerator->saveCaCert($cacertFile);
+$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_001.pem.tmp');
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_001-ca.pem.tmp');
+?>
--EXPECTF--
resource(%d) of type (stream)
resource(%d) of type (stream)
?>
--FILE--
<?php
+$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_002.pem.tmp';
+$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_002-ca.pem.tmp';
+
$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
- 'local_cert' => __DIR__ . '/bug54992.pem'
+ 'local_cert' => '%s'
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
@stream_socket_accept($server, 1);
@stream_socket_accept($server, 1);
CODE;
+$serverCode = sprintf($serverCode, $certFile);
+$peerName = 'stream_crypto_flags_002';
$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$clientFlags = STREAM_CLIENT_CONNECT;
$clientCtx = stream_context_create(['ssl' => [
'verify_peer' => true,
- 'cafile' => __DIR__ . '/bug54992-ca.pem',
- 'peer_name' => 'bug54992.local',
+ 'cafile' => '%s',
+ 'peer_name' => '%s',
]]);
phpt_wait();
stream_context_set_option($clientCtx, 'ssl', 'crypto_method', STREAM_CRYPTO_METHOD_TLS_CLIENT);
var_dump(stream_socket_client($serverUri, $errno, $errstr, 2, $clientFlags, $clientCtx));
CODE;
+$clientCode = sprintf($clientCode, $cacertFile, $peerName);
+
+include 'CertificateGenerator.inc';
+$certificateGenerator = new CertificateGenerator();
+$certificateGenerator->saveCaCert($cacertFile);
+$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_002.pem.tmp');
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_002-ca.pem.tmp');
+?>
--EXPECTF--
resource(%d) of type (stream)
resource(%d) of type (stream)
if (!extension_loaded("openssl")) die("skip openssl not loaded");
if (!function_exists("proc_open")) die("skip no proc_open");
if (OPENSSL_VERSION_NUMBER < 0x10001001) die("skip OpenSSLv1.0.1 required");
+?>
--FILE--
<?php
+$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_003.pem.tmp';
+$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_003-ca.pem.tmp';
+
$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
- 'local_cert' => __DIR__ . '/bug54992.pem',
+ 'local_cert' => '%s',
// Only accept TLSv1.2 connections
'crypto_method' => STREAM_CRYPTO_METHOD_SSLv3_SERVER | STREAM_CRYPTO_METHOD_TLSv1_2_SERVER,
@stream_socket_accept($server, 1);
@stream_socket_accept($server, 1);
CODE;
+$serverCode = sprintf($serverCode, $certFile);
+$peerName = 'stream_crypto_flags_003';
$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$clientFlags = STREAM_CLIENT_CONNECT;
$clientCtx = stream_context_create(['ssl' => [
'verify_peer' => true,
- 'cafile' => __DIR__ . '/bug54992-ca.pem',
- 'peer_name' => 'bug54992.local',
+ 'cafile' => '%s',
+ 'peer_name' => '%s',
]]);
phpt_wait();
stream_context_set_option($clientCtx, 'ssl', 'crypto_method', STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT);
var_dump(@stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx));
CODE;
+$clientCode = sprintf($clientCode, $cacertFile, $peerName);
+
+include 'CertificateGenerator.inc';
+$certificateGenerator = new CertificateGenerator();
+$certificateGenerator->saveCaCert($cacertFile);
+$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_003.pem.tmp');
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_003-ca.pem.tmp');
+?>
--EXPECTF--
resource(%d) of type (stream)
bool(false)
?>
--FILE--
<?php
+$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_004.pem.tmp';
+$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_004-ca.pem.tmp';
+
$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
- 'local_cert' => __DIR__ . '/bug54992.pem',
+ 'local_cert' => '%s',
'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_0_SERVER,
]]);
@stream_socket_accept($server, 1);
@stream_socket_accept($server, 1);
CODE;
+$serverCode = sprintf($serverCode, $certFile);
+$peerName = 'stream_crypto_flags_004';
$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$clientFlags = STREAM_CLIENT_CONNECT;
$clientCtx = stream_context_create(['ssl' => [
'verify_peer' => true,
- 'cafile' => __DIR__ . '/bug54992-ca.pem',
- 'peer_name' => 'bug54992.local',
+ 'cafile' => '%s',
+ 'peer_name' => '%s',
]]);
phpt_wait();
stream_context_set_option($clientCtx, 'ssl', 'crypto_method', STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT);
var_dump(stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx));
CODE;
+$clientCode = sprintf($clientCode, $cacertFile, $peerName);
+
+include 'CertificateGenerator.inc';
+$certificateGenerator = new CertificateGenerator();
+$certificateGenerator->saveCaCert($cacertFile);
+$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_004.pem.tmp');
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_crypto_flags_004-ca.pem.tmp');
+?>
--EXPECTF--
resource(%d) of type (stream)
bool(false)
?>
--FILE--
<?php
+// https://www.openssl.org/docs/man1.1.0/ssl/SSL_CTX_get_security_level.html
+$securityLevel = 2;
+
+// Security level 2 refuses certs signed by keys with length of less than 2048 bits
+$keyLength = 1024;
+
+$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_security_level.pem.tmp';
+$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_security_level-ca.pem.tmp';
+
$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64322";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
- 'local_cert' => __DIR__ . '/bug54992.pem'
+ 'local_cert' => '%s'
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
@stream_socket_accept($server, 1);
CODE;
+$serverCode = sprintf($serverCode, $certFile);
$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64322";
$clientFlags = STREAM_CLIENT_CONNECT;
$clientCtx = stream_context_create(['ssl' => [
- 'security_level' => 2,
+ 'security_level' => %d,
'verify_peer' => true,
- 'cafile' => __DIR__ . '/bug54992-ca.pem',
+ 'cafile' => '%s',
'verify_peer_name' => false
]]);
var_dump($client);
CODE;
+$clientCode = sprintf($clientCode, $securityLevel, $cacertFile);
+
+include 'CertificateGenerator.inc';
+$certificateGenerator = new CertificateGenerator();
+$certificateGenerator->saveCaCert($cacertFile);
+$certificateGenerator->saveNewCertAsFileWithKey('stream_security_level', $certFile, $keyLength);
include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_security_level.pem.tmp');
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_security_level-ca.pem.tmp');
+?>
--EXPECTF--
Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in %s : eval()'d code on line %d
?>
--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
$serverUri = "ssl://127.0.0.1:64321";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
- 'local_cert' => __DIR__ . '/bug54992.pem',
+ 'local_cert' => '%s',
'reneg_limit' => 0,
'reneg_window' => 30,
'reneg_limit_callback' => function($stream) use (&$printed) {
}
}
CODE;
+$serverCode = sprintf($serverCode, $certFile);
$clientCode = <<<'CODE'
$cmd = 'openssl s_client -connect 127.0.0.1:64321';
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)
?>
--FILE--
<?php
+$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_verify_peer_name_001.pem.tmp';
+
$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
- 'local_cert' => __DIR__ . '/bug54992.pem'
+ 'local_cert' => '%s'
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
@stream_socket_accept($server, 1);
CODE;
+$serverCode = sprintf($serverCode, $certFile);
+$peerName = 'stream_verify_peer_name_001';
$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$clientFlags = STREAM_CLIENT_CONNECT;
$clientCtx = stream_context_create(['ssl' => [
'verify_peer' => false,
- 'peer_name' => 'bug54992.local'
+ 'peer_name' => '%s'
]]);
phpt_wait();
var_dump($client);
CODE;
+$clientCode = sprintf($clientCode, $peerName);
+
+include 'CertificateGenerator.inc';
+$certificateGenerator = new CertificateGenerator();
+$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_verify_peer_name_001.pem.tmp');
+?>
--EXPECTF--
resource(%d) of type (stream)
?>
--FILE--
<?php
+$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_verify_peer_name_002.pem.tmp';
+$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_verify_peer_name_002-ca.pem.tmp';
+
$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
- 'local_cert' => __DIR__ . '/bug54992.pem'
+ 'local_cert' => '%s'
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
@stream_socket_accept($server, 1);
CODE;
+$serverCode = sprintf($serverCode, $certFile);
+$actualPeerName = 'stream_verify_peer_name_002';
$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$clientFlags = STREAM_CLIENT_CONNECT;
$clientCtx = stream_context_create(['ssl' => [
'verify_peer' => true,
- 'cafile' => __DIR__ . '/bug54992-ca.pem',
+ 'cafile' => '%s',
'verify_peer_name' => false
]]);
var_dump($client);
CODE;
+$clientCode = sprintf($clientCode, $cacertFile);
+
+include 'CertificateGenerator.inc';
+$certificateGenerator = new CertificateGenerator();
+$certificateGenerator->saveCaCert($cacertFile);
+$certificateGenerator->saveNewCertAsFileWithKey($actualPeerName, $certFile);
include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_verify_peer_name_002.pem.tmp');
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_verify_peer_name_002-ca.pem.tmp');
+?>
--EXPECTF--
resource(%d) of type (stream)
?>
--FILE--
<?php
+$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_verify_peer_name_003.pem.tmp';
+$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'stream_verify_peer_name_003-ca.pem.tmp';
+
$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
- 'local_cert' => __DIR__ . '/bug54992.pem'
+ 'local_cert' => '%s'
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
@stream_socket_accept($server, 1);
CODE;
+$serverCode = sprintf($serverCode, $certFile);
+$actualPeerName = 'stream_verify_peer_name_003';
$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$clientFlags = STREAM_CLIENT_CONNECT;
$clientCtx = stream_context_create(['ssl' => [
'verify_peer' => true,
- 'cafile' => __DIR__ . '/bug54992-ca.pem'
+ 'cafile' => '%s'
]]);
phpt_wait();
var_dump($client);
CODE;
+$clientCode = sprintf($clientCode, $cacertFile);
+
+include 'CertificateGenerator.inc';
+$certificateGenerator = new CertificateGenerator();
+$certificateGenerator->saveCaCert($cacertFile);
+$certificateGenerator->saveNewCertAsFileWithKey($actualPeerName, $certFile);
include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_verify_peer_name_003.pem.tmp');
+@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_verify_peer_name_003-ca.pem.tmp');
+?>
--EXPECTF--
-Warning: stream_socket_client(): Peer certificate CN=`bug54992.local' did not match expected CN=`127.0.0.1' in %s on line %d
+Warning: stream_socket_client(): Peer certificate CN=`stream_verify_peer_name_003' did not match expected CN=`127.0.0.1' in %s on line %d
Warning: stream_socket_client(): Failed to enable crypto in %s on line %d