Skip to content

Commit

Permalink
Promote some OpenSSL warnings to Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Girgias committed Jan 24, 2020
1 parent 986da2a commit 0be74fd
Show file tree
Hide file tree
Showing 22 changed files with 448 additions and 296 deletions.
156 changes: 78 additions & 78 deletions ext/openssl/openssl.c

Large diffs are not rendered by default.

34 changes: 20 additions & 14 deletions ext/openssl/tests/bug38255.phpt
Expand Up @@ -8,7 +8,12 @@ if (!extension_loaded("openssl")) die("skip");
<?php
$pub_key_id = false;
$signature = '';
$ok = openssl_verify("foo", $signature, $pub_key_id, OPENSSL_ALGO_MD5);

try {
$ok = openssl_verify("foo", $signature, $pub_key_id, OPENSSL_ALGO_MD5);
} catch (\TypeError $e) {
echo $e->getMessage() . \PHP_EOL;
}

class test {
function __toString() {
Expand All @@ -18,18 +23,19 @@ class test {
$t = new test;


var_dump(openssl_verify("foo", $signature, $pub_key_id, OPENSSL_ALGO_MD5));
var_dump(openssl_verify("foo", $t, $pub_key_id, OPENSSL_ALGO_MD5));

echo "Done\n";
try {
var_dump(openssl_verify("foo", $signature, $pub_key_id, OPENSSL_ALGO_MD5));
} catch (\TypeError $e) {
echo $e->getMessage() . \PHP_EOL;
}
try {
var_dump(openssl_verify("foo", $t, $pub_key_id, OPENSSL_ALGO_MD5));
} catch (\TypeError $e) {
echo $e->getMessage() . \PHP_EOL;
}

?>
--EXPECTF--
Warning: openssl_verify(): Supplied key param cannot be coerced into a public key in %s on line %d

Warning: openssl_verify(): Supplied key param cannot be coerced into a public key in %s on line %d
bool(false)

Warning: openssl_verify(): Supplied key param cannot be coerced into a public key in %s on line %d
bool(false)
Done
--EXPECT--
Supplied key param cannot be coerced into a public key
Supplied key param cannot be coerced into a public key
Supplied key param cannot be coerced into a public key
13 changes: 8 additions & 5 deletions ext/openssl/tests/bug60632.phpt
Expand Up @@ -19,9 +19,12 @@ $test_pubkey = $details['key'];
$pubkey = openssl_pkey_get_public($test_pubkey);
$encrypted = null;
$ekeys = array();
$result = openssl_seal('test phrase', $encrypted, $ekeys, array($pubkey), 'AES-256-CBC');
echo "Done";

try {
$result = openssl_seal('test phrase', $encrypted, $ekeys, array($pubkey), 'AES-256-CBC');
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
?>
--EXPECTF--
Warning: openssl_seal(): Cipher algorithm requires an IV to be supplied as a sixth parameter in %s on line %d
Done
--EXPECT--
Cipher algorithm requires an IV to be supplied as a sixth parameter
20 changes: 12 additions & 8 deletions ext/openssl/tests/cve-2013-6420.phpt
Expand Up @@ -5,12 +5,16 @@ CVE-2013-6420
--FILE--
<?php
$crt = substr(__FILE__, 0, -4).'.crt';
$info = openssl_x509_parse("file://$crt");
var_dump($info['issuer']['emailAddress'], $info["validFrom_time_t"]);

try {
$info = openssl_x509_parse("file://$crt");
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

@var_dump($info['issuer']['emailAddress'], $info["validFrom_time_t"]);
?>
Done
--EXPECTF--
%s openssl_x509_parse(): Illegal length in timestamp in %s%ecve-2013-6420.php on line 3
string(27) "stefan.esser@sektioneins.de"
int(-1)
Done
--EXPECT--
Illegal length in timestamp
NULL
NULL
12 changes: 7 additions & 5 deletions ext/openssl/tests/ecc.phpt
Expand Up @@ -17,8 +17,12 @@ $argsFailed = array(
"private_key_type" => OPENSSL_KEYTYPE_EC,
);

$keyFailed = openssl_pkey_new($argsFailed);
var_dump($keyFailed);
try {
$keyFailed = openssl_pkey_new($argsFailed);
var_dump($keyFailed);
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

$d1 = openssl_pkey_get_details($key1);
var_dump($d1["bits"]);
Expand Down Expand Up @@ -82,9 +86,7 @@ foreach ($curve_names as $curve_name) {
--EXPECTF--
Testing openssl_pkey_new
resource(%d) of type (OpenSSL key)

Warning: openssl_pkey_new(): Unknown elliptic curve (short) name invalid_cuve_name in %s on line %d
bool(false)
Unknown elliptic curve (short) name invalid_cuve_name
int(384)
int(215)
string(9) "secp384r1"
Expand Down
15 changes: 8 additions & 7 deletions ext/openssl/tests/openssl_csr_new_basic.phpt
Expand Up @@ -8,7 +8,13 @@ openssl_csr_new() tests
$a = array();

$conf = array('config' => __DIR__ . DIRECTORY_SEPARATOR . 'openssl.cnf');
var_dump(openssl_csr_new(array(), $a, $conf, array()));

try {
var_dump(openssl_csr_new(array(), $a, $conf, array()));
var_dump($keyFailed);
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

// this leaks
$a = array(1,2);
Expand All @@ -19,14 +25,9 @@ var_dump(openssl_csr_new($a, $b, $conf));
$x = openssl_pkey_new($conf);
var_dump(openssl_csr_new(["countryName" => "DE"], $x, $conf + ["x509_extensions" => 0xDEADBEEF]));


echo "Done\n";
?>
--EXPECTF--
Warning: openssl_csr_new(): Key array must be of the form array(0 => key, 1 => phrase) in %s on line %d

Warning: openssl_csr_new(): add1_attr_by_txt challengePassword_min -> 4 (failed; check error queue and value of string_mask OpenSSL option if illegal characters are reported) in %s on line %d
bool(false)
Key array must be of the form array(0 => key, 1 => phrase)
resource(%d) of type (OpenSSL X.509 CSR)
resource(%d) of type (OpenSSL X.509 CSR)
Done
51 changes: 35 additions & 16 deletions ext/openssl/tests/openssl_csr_sign_basic.phpt
Expand Up @@ -33,12 +33,39 @@ var_dump(openssl_csr_sign($csr, null, $privkey, 365, $args));
var_dump(openssl_csr_sign($csr, null, $privkey, 365, $config_arg));
var_dump(openssl_csr_sign($csr, $cert, $priv, 365, $config_arg));
var_dump(openssl_csr_sign($csr, openssl_x509_read($cert), $priv, 365, $config_arg));
var_dump(openssl_csr_sign($csr, $wrong, $privkey, 365));
var_dump(openssl_csr_sign($csr, null, $wrong, 365));
var_dump(openssl_csr_sign($wrong, null, $privkey, 365));
var_dump(openssl_csr_sign(array(), null, $privkey, 365));

try {
var_dump(openssl_csr_sign($csr, $wrong, $privkey, 365));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

try {
var_dump(openssl_csr_sign($csr, null, $wrong, 365));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

try {
var_dump(openssl_csr_sign($wrong, null, $privkey, 365));
} catch (\TypeError $e) {
echo $e->getMessage() . \PHP_EOL;
}

try {
var_dump(openssl_csr_sign(array(), null, $privkey, 365));
} catch (\TypeError $e) {
echo $e->getMessage() . \PHP_EOL;
}

var_dump(openssl_csr_sign($csr, array(), $privkey, 365));
var_dump(openssl_csr_sign($csr, null, array(), 365));

try {
var_dump(openssl_csr_sign($csr, null, array(), 365));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

var_dump(openssl_csr_sign($csr, null, $privkey, 365, $config_arg));
?>
--EXPECTF--
Expand All @@ -52,18 +79,10 @@ bool(false)

Warning: openssl_csr_sign(): Cannot get private key from parameter 3 in %s on line %d
bool(false)

Warning: openssl_csr_sign(): Cannot get CSR from parameter 1 in %s on line %d
bool(false)

Warning: openssl_csr_sign(): Cannot get CSR from parameter 1 in %s on line %d
bool(false)
Supplied parameter cannot be coerced into an Certificate Signing Request (CSR)
Supplied parameter cannot be coerced into an Certificate Signing Request (CSR)

Warning: openssl_csr_sign(): Cannot get cert from parameter 2 in %s on line %d
bool(false)

Warning: openssl_csr_sign(): Key array must be of the form array(0 => key, 1 => phrase) in %s on line %d

Warning: openssl_csr_sign(): Cannot get private key from parameter 3 in %s on line %d
bool(false)
Key array must be of the form array(0 => key, 1 => phrase)
resource(%d) of type (OpenSSL X.509)
45 changes: 29 additions & 16 deletions ext/openssl/tests/openssl_decrypt_error.phpt
Expand Up @@ -12,13 +12,34 @@ $iv = str_repeat("\0", openssl_cipher_iv_length($method));

$encrypted = openssl_encrypt($data, $method, $password);
var_dump($encrypted); /* Not passing $iv should be the same as all-NULL iv, but with a warning */

var_dump(openssl_encrypt($data, $method, $password, 0, $iv));

var_dump(openssl_decrypt($encrypted, $method, $wrong));
var_dump(openssl_decrypt($encrypted, $wrong, $password));

try {
var_dump(openssl_decrypt($encrypted, $wrong, $password));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

var_dump(openssl_decrypt($wrong, $method, $password));
var_dump(openssl_decrypt($wrong, $wrong, $password));
var_dump(openssl_decrypt($encrypted, $wrong, $wrong));
var_dump(openssl_decrypt($wrong, $wrong, $wrong));

try {
var_dump(openssl_decrypt($wrong, $wrong, $password));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
try {
var_dump(openssl_decrypt($encrypted, $wrong, $wrong));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
try {
var_dump(openssl_decrypt($wrong, $wrong, $wrong));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

// invalid using of an authentication tag
var_dump(openssl_encrypt($data, $method, $password, 0, $iv, $wrong));
Expand All @@ -28,19 +49,11 @@ Warning: openssl_encrypt(): Using an empty Initialization Vector (iv) is potenti
string(44) "yof6cPPH4mLee6TOc0YQSrh4dvywMqxGUyjp0lV6+aM="
string(44) "yof6cPPH4mLee6TOc0YQSrh4dvywMqxGUyjp0lV6+aM="
bool(false)

Warning: openssl_decrypt(): Unknown cipher algorithm in %s on line %d
bool(false)
bool(false)

Warning: openssl_decrypt(): Unknown cipher algorithm in %s on line %d
bool(false)

Warning: openssl_decrypt(): Unknown cipher algorithm in %s on line %d
bool(false)

Warning: openssl_decrypt(): Unknown cipher algorithm in %s on line %d
Unknown cipher algorithm
bool(false)
Unknown cipher algorithm
Unknown cipher algorithm
Unknown cipher algorithm

Warning: openssl_encrypt(): The authenticated tag cannot be provided for cipher that doesn not support AEAD in %s on line %d
string(44) "yof6cPPH4mLee6TOc0YQSrh4dvywMqxGUyjp0lV6+aM="
9 changes: 6 additions & 3 deletions ext/openssl/tests/openssl_encrypt_error.phpt
Expand Up @@ -13,7 +13,11 @@ $object = new stdclass;
$arr = array(1);

// wrong parameters tests
var_dump(openssl_encrypt($data, $wrong, $password));
try {
var_dump(openssl_encrypt($data, $wrong, $password));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

// invalid using of an authentication tag
var_dump(openssl_encrypt($data, $method, $password, 0, $iv, $wrong));
Expand All @@ -22,8 +26,7 @@ var_dump(openssl_encrypt($data, $method, $password, 0, $iv, $wrong));
var_dump(openssl_encrypt($data, $method, $password, OPENSSL_DONT_ZERO_PAD_KEY, $iv));
?>
--EXPECTF--
Warning: openssl_encrypt(): Unknown cipher algorithm in %s on line %d
bool(false)
Unknown cipher algorithm

Warning: openssl_encrypt(): The authenticated tag cannot be provided for cipher that doesn not support AEAD in %s on line %d
string(44) "iPR4HulskuaP5Z6me5uImk6BqVyJG73+63tkPauVZYk="
Expand Down

0 comments on commit 0be74fd

Please sign in to comment.