Skip to content

Commit

Permalink
Make code in openssl ext tests more consistent
Browse files Browse the repository at this point in the history
Mainly use spaces for indent and fix some other CS issues. Also
drop checks for unsupported OpenSSL library versions.
  • Loading branch information
bukka committed Jun 21, 2018
1 parent 6531719 commit 3c42f64
Show file tree
Hide file tree
Showing 88 changed files with 708 additions and 629 deletions.
41 changes: 25 additions & 16 deletions ext/openssl/tests/001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,43 @@ if (!@openssl_pkey_new()) die("skip cannot create private key");
<?php
echo "Creating private key\n";

$conf = array('config' => dirname(__FILE__) . DIRECTORY_SEPARATOR . 'openssl.cnf');
$conf = array('config' => __DIR__ . DIRECTORY_SEPARATOR . 'openssl.cnf');
$privkey = openssl_pkey_new($conf);

if ($privkey === false)
die("failed to create private key");
if ($privkey === false) {
die("failed to create private key");
}

$passphrase = "banana";
$key_file_name = tempnam(sys_get_temp_dir(), "ssl");
if ($key_file_name === false)
die("failed to get a temporary filename!");
$key_file_name = __DIR__ . '/001-tmp.key';
if ($key_file_name === false) {
die("failed to get a temporary filename!");
}

echo "Export key to file\n";

openssl_pkey_export_to_file($privkey, $key_file_name, $passphrase, $conf) or die("failed to export to file $key_file_name");
if (!openssl_pkey_export_to_file($privkey, $key_file_name, $passphrase, $conf)) {
die("failed to export to file $key_file_name");
}
var_dump(is_resource($privkey));

echo "Load key from file - array syntax\n";

$loaded_key = openssl_pkey_get_private(array("file://$key_file_name", $passphrase));

if ($loaded_key === false)
die("failed to load key using array syntax");
if ($loaded_key === false) {
die("failed to load key using array syntax");
}

openssl_pkey_free($loaded_key);

echo "Load key using direct syntax\n";

$loaded_key = openssl_pkey_get_private("file://$key_file_name", $passphrase);

if ($loaded_key === false)
die("failed to load key using direct syntax");
if ($loaded_key === false) {
die("failed to load key using direct syntax");
}

openssl_pkey_free($loaded_key);

Expand All @@ -48,15 +54,13 @@ echo "Load key manually and use string syntax\n";
$key_content = file_get_contents($key_file_name);
$loaded_key = openssl_pkey_get_private($key_content, $passphrase);

if ($loaded_key === false)
die("failed to load key using string syntax");

if ($loaded_key === false) {
die("failed to load key using string syntax");
}
openssl_pkey_free($loaded_key);

echo "OK!\n";

@unlink($key_file_name);

?>
--EXPECT--
Creating private key
Expand All @@ -66,3 +70,8 @@ Load key from file - array syntax
Load key using direct syntax
Load key manually and use string syntax
OK!
--CLEAN--
<?php
$key_file_name = __DIR__ . DIRECTORY_SEPARATOR . '001-tmp.key';
@unlink($key_file_name);
?>
3 changes: 1 addition & 2 deletions ext/openssl/tests/bug28382.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
Bug #28382 (openssl_x509_parse extensions support)
--SKIPIF--
<?php
if (!extension_loaded("openssl")) die("skip");
if (OPENSSL_VERSION_NUMBER<0x009070af) die("skip");
if (!extension_loaded("openssl")) die("skip");
?>
--FILE--
<?php
Expand Down
21 changes: 10 additions & 11 deletions ext/openssl/tests/bug36732.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ Bug #36732 (add support for req_extensions in openss_csr_new and sign)
--SKIPIF--
<?php
if (!extension_loaded("openssl")) die("skip");
if (OPENSSL_VERSION_NUMBER < 0x009070af) die("skip");
?>
--FILE--
<?php
$configargs = array(
"req_extensions" => "v3_req",
"x509_extensions" => "usr_cert",
"config" => __DIR__. DIRECTORY_SEPARATOR . "openssl.cnf",
"req_extensions" => "v3_req",
"x509_extensions" => "usr_cert",
"config" => __DIR__. DIRECTORY_SEPARATOR . "openssl.cnf",
);

$dn = array(
"countryName" => "GB",
"stateOrProvinceName" => "Berkshire",
"localityName" => "Newbury",
"organizationName" => "My Company Ltd",
"commonName" => "Demo Cert"
"countryName" => "GB",
"stateOrProvinceName" => "Berkshire",
"localityName" => "Newbury",
"organizationName" => "My Company Ltd",
"commonName" => "Demo Cert"
);

$key = openssl_pkey_new();
Expand All @@ -29,11 +28,11 @@ $str = '';
openssl_csr_export($csr, $str, false);

if (strpos($str, 'Requested Extensions:')) {
echo "Ok\n";
echo "Ok\n";
}
openssl_x509_export($crt, $str, false);
if (strpos($str, 'X509v3 extensions:')) {
echo "Ok\n";
echo "Ok\n";
}
?>
--EXPECTF--
Expand Down
8 changes: 3 additions & 5 deletions ext/openssl/tests/bug37820.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ openssl_sign/verify: accept different algos
--SKIPIF--
<?php
if (!extension_loaded("openssl")) die("skip");
if (OPENSSL_VERSION_NUMBER < 0x009070af) die("skip");
?>
--FILE--
<?php
Expand All @@ -15,19 +14,18 @@ $priv_key = file_get_contents($file_key);
$priv_key_id = openssl_get_privatekey($priv_key);



$pub_key = file_get_contents($file_pub);
$pub_key_id = openssl_get_publickey($pub_key);
$data = "some custom data";
if (!openssl_sign($data, $signature, $priv_key_id, OPENSSL_ALGO_MD5)) {
echo "openssl_sign failed.";
echo "openssl_sign failed.";
}

$ok = openssl_verify($data, $signature, $pub_key_id, OPENSSL_ALGO_MD5);
if ($ok == 1) {
echo "Ok";
echo "Ok";
} elseif ($ok == 0) {
echo "openssl_verify failed.";
echo "openssl_verify failed.";
}


Expand Down
6 changes: 3 additions & 3 deletions ext/openssl/tests/bug38255.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ $signature = '';
$ok = openssl_verify("foo", $signature, $pub_key_id, OPENSSL_ALGO_MD5);

class test {
function __toString() {
return "test object";
}
function __toString() {
return "test object";
}
}
$t = new test;

Expand Down
6 changes: 3 additions & 3 deletions ext/openssl/tests/bug38261.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ if (!extension_loaded("openssl")) die("skip");
<?php
$cert = false;
class test {
function __toString() {
return "test object";
}
function __toString() {
return "test object";
}
}
$t = new test;

Expand Down
6 changes: 3 additions & 3 deletions ext/openssl/tests/bug39217.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ if (!extension_loaded("openssl")) die("skip");
$dir = dirname(__FILE__);
$certs = array('bug39217cert2.txt', 'bug39217cert1.txt');
foreach($certs as $cert) {
$res = openssl_x509_parse(file_get_contents($dir . '/' . $cert));
print_r($res['serialNumber']);
echo "\n";
$res = openssl_x509_parse(file_get_contents($dir . '/' . $cert));
print_r($res['serialNumber']);
echo "\n";
}
?>
--EXPECTF--
Expand Down
1 change: 0 additions & 1 deletion ext/openssl/tests/bug41033.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
--SKIPIF--
<?php
if (!extension_loaded("openssl")) die("skip, openssl required");
if (OPENSSL_VERSION_NUMBER < 0x009070af) die("skip");
?>
--FILE--
<?php
Expand Down
3 changes: 2 additions & 1 deletion ext/openssl/tests/bug46127.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<?php
if (!extension_loaded("openssl")) die("skip openssl not loaded");
if (!function_exists("proc_open")) die("skip no proc_open");
if (OPENSSL_VERSION_NUMBER < 0x009070af) die("skip openssl version too low");
?>
--FILE--
<?php
$serverCode = <<<'CODE'
Expand Down Expand Up @@ -38,5 +38,6 @@ CODE;

include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
--EXPECT--
Sending bug 46127
3 changes: 2 additions & 1 deletion ext/openssl/tests/bug48182.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Bug #48182: ssl handshake fails during asynchronous socket connection
<?php
if (!extension_loaded("openssl")) die("skip openssl not loaded");
if (!function_exists("proc_open")) die("skip no proc_open");
if (OPENSSL_VERSION_NUMBER < 0x009070af) die("skip openssl version too low");
?>
--FILE--
<?php
$serverCode = <<<'CODE'
Expand Down Expand Up @@ -44,6 +44,7 @@ echo "Running bug48182\n";

include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
--EXPECTF--
Running bug48182
Sending bug48182
Expand Down
2 changes: 2 additions & 0 deletions ext/openssl/tests/bug54992.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Bug #54992: Stream not closed and error not returned when SSL CN_match fails
<?php
if (!extension_loaded("openssl")) die("skip openssl not loaded");
if (!function_exists("proc_open")) die("skip no proc_open");
?>
--FILE--
<?php
$serverCode = <<<'CODE'
Expand Down Expand Up @@ -36,6 +37,7 @@ CODE;

include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
--EXPECTF--
Warning: stream_socket_client(): Peer certificate CN=`bug54992.local' did not match expected CN=`buga_buga' in %s on line %d

Expand Down
12 changes: 6 additions & 6 deletions ext/openssl/tests/bug55259.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ Bug #55259 (openssl extension does not get the DH parameters from DH key resourc
<?php

$phex = 'dcf93a0b883972ec0e19989ac5a2ce310e1d37717e8d9571bb7623731866e61e' .
'f75a2e27898b057f9891c2e27a639c3f29b60814581cd3b2ca3986d268370557' .
'7d45c2e7e52dc81c7a171876e5cea74b1448bfdfaf18828efd2519f14e45e382' .
'6634af1949e5b535cc829a483b8a76223e5d490a257f05bdff16f2fb22c583ab';
$dh_details = array( 'p' => $phex, 'g' => '2' );
$dh = openssl_pkey_new(array( 'dh'=> array( 'p' => $phex, 'g' => '2' )));
'f75a2e27898b057f9891c2e27a639c3f29b60814581cd3b2ca3986d268370557' .
'7d45c2e7e52dc81c7a171876e5cea74b1448bfdfaf18828efd2519f14e45e382' .
'6634af1949e5b535cc829a483b8a76223e5d490a257f05bdff16f2fb22c583ab';
$dh_details = array('p' => $phex, 'g' => '2');
$dh = openssl_pkey_new(array('dh'=> array('p' => $phex, 'g' => '2')));
var_dump($dh);
$dh = openssl_pkey_new(array( 'dh'=> array( 'p' => hex2bin($phex), 'g' => '2' )));
$dh = openssl_pkey_new(array('dh'=> array( 'p' => hex2bin($phex), 'g' => '2')));
$details = openssl_pkey_get_details($dh);
var_dump(bin2hex($details['dh']['p']));
var_dump($details['dh']['g']);
Expand Down
46 changes: 31 additions & 15 deletions ext/openssl/tests/bug55646.phpt
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
--TEST--
Bug #55646: textual input in openssl_csr_new() is not expected in UTF-8
--SKIPIF--
<?php
if (!function_exists('openssl_csr_new'))
die('skip no openssl extension');
<?php if (!extension_loaded("openssl")) die("skip"); ?>
--FILE--
<?php
function stringAsHex($string){$unpacked = unpack("H*", $string);return implode(" ", str_split($unpacked[1],2));}

$config = array("digest_alg" => "sha1","x509_extensions" => "v3_ca","req_extensions" => "v3_req","private_key_bits" => 2048,"private_key_type" => OPENSSL_KEYTYPE_RSA,"encrypt_key" => false,);
function stringAsHex($string) {
$unpacked = unpack("H*", $string);
return implode(" ", str_split($unpacked[1],2));
}

$config = array(
"digest_alg" => "sha1",
"x509_extensions" => "v3_ca",
"req_extensions" => "v3_req",
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
"encrypt_key" => false,
);
$csr_info = array(
"countryName" => "US",
"stateOrProvinceName" => "Utah",
"localityName" => "Lindon",
"organizationName" => "Chinese",
"organizationalUnitName" => "IT \xe4\xba\x92",
"commonName" => "www.example.com",);
"countryName" => "US",
"stateOrProvinceName" => "Utah",
"localityName" => "Lindon",
"organizationName" => "Chinese",
"organizationalUnitName" => "IT \xe4\xba\x92",
"commonName" => "www.example.com",
);
$private = openssl_pkey_new($config);
while (openssl_error_string()) {}
$csr_res = openssl_csr_new($csr_info, $private,
['config' => __DIR__. DIRECTORY_SEPARATOR . "openssl.cnf"]);
$csr_res = openssl_csr_new(
$csr_info,
$private,
['config' => __DIR__. DIRECTORY_SEPARATOR . "openssl.cnf"]
);
if (!$csr_res) {
while ($e = openssl_error_string()) { $err = $e; }
die("Failed; last error: $err");
while ($e = openssl_error_string()) {
$err = $e;
}
die("Failed; last error: $err");
}
openssl_csr_export($csr_res, $csr);
$output = openssl_csr_get_subject($csr);
Expand All @@ -31,6 +46,7 @@ echo "A: ".$csr_info["organizationalUnitName"]."\n";
echo "B: ".stringAsHex($csr_info["organizationalUnitName"])."\n";
echo "C: ".$output['OU']."\n";
echo "D: ".stringAsHex($output['OU'])."\n";
?>
--EXPECT--
A: IT 互
B: 49 54 20 e4 ba 92
Expand Down
15 changes: 11 additions & 4 deletions ext/openssl/tests/bug61124.phpt
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
--TEST--
Bug #61124: Segmentation fault with openssl_decrypt
--SKIPIF--
<?php
if (!extension_loaded("openssl")) die("skip");
<?php if (!extension_loaded("openssl")) die("skip"); ?>
--FILE--
<?php
var_dump(openssl_decrypt('kzo w2RMExUTYQXW2Xzxmg==', 'aes-128-cbc', 'pass', false, 'pass'));

var_dump(
openssl_decrypt(
'kzo w2RMExUTYQXW2Xzxmg==',
'aes-128-cbc',
'pass',
false,
'pass'
)
);
?>
--EXPECTF--
Warning: openssl_decrypt(): IV passed is only 4 bytes long, cipher expects an IV of precisely 16 bytes, padding with \0 in %s on line %d
bool(false)
4 changes: 3 additions & 1 deletion ext/openssl/tests/bug61930.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ $data = <<<DATA
Please verify me
DATA;

$sig = 'f9Gyb6NV/ENn7GUa37ygTLcF93XHf5fbFTnoYF/O+fXbq3iChGUbET0RuhOsptlAODi6JsDLnJO4ikcVZo0tC1fFTj3LyCuPy3ZdgJbbVxQ/rviROCmuMFTqUW/Xa2LQYiapeCCgLQeWTLg7TM/BoHEkKbKLG/XT5jHvep1758A=';
$sig = 'f9Gyb6NV/ENn7GUa37ygTLcF93XHf5fbFTnoYF/O+fXbq3iChGUbET0RuhOsptl' .
'AODi6JsDLnJO4ikcVZo0tC1fFTj3LyCuPy3ZdgJbbVxQ/rviROCmuMFTqUW/Xa2' .
'LQYiapeCCgLQeWTLg7TM/BoHEkKbKLG/XT5jHvep1758A=';

$key = openssl_get_publickey($cert);
var_dump(openssl_get_publickey($key));
Expand Down

0 comments on commit 3c42f64

Please sign in to comment.