Skip to content

Commit 4056edd

Browse files
committed
Update openssl_error_string to run and be more forgiving
1 parent 1f7cc24 commit 4056edd

File tree

1 file changed

+74
-89
lines changed

1 file changed

+74
-89
lines changed
Lines changed: 74 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
11
--TEST--
22
openssl_error_string() tests
33
--SKIPIF--
4-
<?php
5-
if (!extension_loaded("openssl")) print "skip";
6-
//if (OPENSSL_VERSION_NUMBER < 0x10001001) die("skip OpenSSLv1.0.1 required");
7-
?>
8-
--XFAIL--
9-
ot ready baked yet, fails different ways on different envs
4+
<?php if (!extension_loaded("openssl")) print "skip"; ?>
105
--FILE--
116
<?php
12-
// helper function to dump openssl errors
13-
function dump_openssl_errors() {
7+
// helper function to check openssl errors
8+
function expect_openssl_errors($name, $expected_error_codes) {
9+
$expected_errors = array_fill_keys($expected_error_codes, false);
10+
while (($error_string = openssl_error_string()) !== false) {
11+
if (strlen($error_string) > 14) {
12+
$error_code = substr($error_string, 6, 8);
13+
if (isset($expected_errors[$error_code])) {
14+
$expected_errors[$error_code] = true;
15+
}
16+
}
17+
}
18+
19+
$fail = false;
20+
foreach ($expected_errors as $error_code => $error_code_found) {
21+
if (!$error_code_found) {
22+
$fail = true;
23+
echo "$name: no error code $error_code\n";
24+
}
25+
}
26+
27+
if (!$fail) {
28+
echo "$name: ok\n";
29+
}
30+
}
31+
32+
// helper for debugging errors
33+
function dump_openssl_errors($name) {
34+
echo "\n$name\n";
1435
while (($error_string = openssl_error_string()) !== false) {
1536
var_dump($error_string);
1637
}
@@ -56,61 +77,59 @@ while (($enc_error_new = openssl_error_string()) !== false) {
5677
++$error_queue_size;
5778
}
5879
var_dump($error_queue_size);
80+
echo "\n";
5981

6082
// PKEY
6183
echo "PKEY errors\n";
6284
// file for pkey (file:///) fails when opennig (BIO_new_file)
63-
openssl_pkey_export_to_file("file://" . $invalid_file_for_read, $output_file);
64-
dump_openssl_errors();
85+
@openssl_pkey_export_to_file("file://" . $invalid_file_for_read, $output_file);
86+
expect_openssl_errors('openssl_pkey_export_to_file opening', ['02001002', '2006D080']);
6587
// file or private pkey is not correct PEM - failing PEM_read_bio_PrivateKey
66-
openssl_pkey_export_to_file($csr_file, $output_file);
67-
dump_openssl_errors();
88+
@openssl_pkey_export_to_file($csr_file, $output_file);
89+
expect_openssl_errors('openssl_pkey_export_to_file pem', ['0906D06C']);
6890
// file to export cannot be written
69-
openssl_pkey_export_to_file($private_key_file, $invalid_file_for_write);
70-
dump_openssl_errors();
91+
@openssl_pkey_export_to_file($private_key_file, $invalid_file_for_write);
92+
expect_openssl_errors('openssl_pkey_export_to_file write', ['2006D002', '09072007']);
7193
// succesful export
72-
openssl_pkey_export($private_key_file_with_pass, $out, 'wrong pwd');
73-
dump_openssl_errors();
94+
@openssl_pkey_export($private_key_file_with_pass, $out, 'wrong pwd');
95+
expect_openssl_errors('openssl_pkey_export', ['06065064', '0906A065']);
7496
// invalid x509 for getting public key
75-
openssl_pkey_get_public($private_key_file);
76-
dump_openssl_errors();
97+
@openssl_pkey_get_public($private_key_file);
98+
expect_openssl_errors('openssl_pkey_get_public', ['0906D06C']);
7799
// private encrypt with unknown padding
78-
openssl_private_encrypt("data", $crypted, $private_key_file, 1000);
79-
dump_openssl_errors();
100+
@openssl_private_encrypt("data", $crypted, $private_key_file, 1000);
101+
expect_openssl_errors('openssl_private_encrypt', ['04066076']);
80102
// private decrypt with failed padding check
81-
openssl_private_decrypt("data", $crypted, $private_key_file);
82-
dump_openssl_errors();
103+
@openssl_private_decrypt("data", $crypted, $private_key_file);
104+
expect_openssl_errors('openssl_private_decrypt', ['04065072']);
83105
// public encrypt and decrypt with failed padding check and padding
84-
openssl_public_encrypt("data", $crypted, $public_key_file, 1000);
85-
openssl_public_decrypt("data", $crypted, $public_key_file);
86-
dump_openssl_errors();
106+
@openssl_public_encrypt("data", $crypted, $public_key_file, 1000);
107+
@openssl_public_decrypt("data", $crypted, $public_key_file);
108+
expect_openssl_errors('openssl_private_(en|de)crypt padding', ['0906D06C', '04068076', '0407006A', '04067072']);
87109

88110
// X509
89111
echo "X509 errors\n";
90112
// file for x509 (file:///) fails when opennig (BIO_new_file)
91-
openssl_x509_export_to_file("file://" . $invalid_file_for_read, $output_file);
92-
dump_openssl_errors();
113+
@openssl_x509_export_to_file("file://" . $invalid_file_for_read, $output_file);
114+
expect_openssl_errors('openssl_x509_export_to_file open', ['02001002']);
93115
// file or str cert is not correct PEM - failing PEM_read_bio_X509 or PEM_ASN1_read_bio
94-
openssl_x509_export_to_file($csr_file, $output_file);
95-
dump_openssl_errors();
116+
@openssl_x509_export_to_file($csr_file, $output_file);
117+
expect_openssl_errors('openssl_x509_export_to_file pem', ['0906D06C']);
96118
// file to export cannot be written
97-
openssl_x509_export_to_file($crt_file, $invalid_file_for_write);
98-
dump_openssl_errors();
119+
@openssl_x509_export_to_file($crt_file, $invalid_file_for_write);
120+
expect_openssl_errors('openssl_x509_export_to_file write', ['2006D002']);
99121
// checking purpose fails because there is no such purpose 1000
100-
openssl_x509_checkpurpose($crt_file, 1000);
101-
dump_openssl_errors();
102-
// make sure that X509_STORE_add_lookup will not emmit any error (just PHP warning)
103-
openssl_x509_checkpurpose($crt_file, X509_PURPOSE_SSL_CLIENT, array( __DIR__ . "/cert.csr"));
104-
dump_openssl_errors();
122+
@openssl_x509_checkpurpose($crt_file, 1000);
123+
expect_openssl_errors('openssl_x509_checkpurpose purpose', ['0B086079']);
105124

106125
// CSR
107126
echo "CSR errors\n";
108127
// file for csr (file:///) fails when opennig (BIO_new_file)
109-
openssl_csr_get_subject("file://" . $invalid_file_for_read);
110-
dump_openssl_errors();
128+
@openssl_csr_get_subject("file://" . $invalid_file_for_read);
129+
expect_openssl_errors('openssl_csr_get_subject open', ['02001002', '2006D080', '20068079', '0906D06C']);
111130
// file or str csr is not correct PEM - failing PEM_read_bio_X509_REQ
112131
openssl_csr_get_subject($crt_file);
113-
dump_openssl_errors();
132+
@expect_openssl_errors('openssl_csr_get_subjec pem', ['0906D06C']);
114133

115134
// other possible cuases that are difficult to catch:
116135
// - ASN1_STRING_to_UTF8 fails in add_assoc_name_entry
@@ -124,59 +143,25 @@ if (is_file($output_file)) {
124143
unlink($output_file);
125144
}
126145
?>
127-
--EXPECTF--
146+
--EXPECT--
128147
string(89) "error:0607A082:digital envelope routines:EVP_CIPHER_CTX_set_key_length:invalid key length"
129148
bool(false)
130149
int(15)
131-
PKEY errors
132-
133-
Warning: openssl_pkey_export_to_file(): cannot get key from parameter 1 in %s on line %d
134-
string(61) "error:02001002:system library:fopen:No such file or directory"
135-
string(53) "error:2006D080:BIO routines:BIO_new_file:no such file"
136150

137-
Warning: openssl_pkey_export_to_file(): cannot get key from parameter 1 in %s on line %d
138-
string(54) "error:0906D06C:PEM routines:PEM_read_bio:no start line"
139-
string(68) "error:0E06D06C:configuration file routines:NCONF_get_string:no value"
140-
string(68) "error:0E06D06C:configuration file routines:NCONF_get_string:no value"
141-
string(68) "error:0E06D06C:configuration file routines:NCONF_get_string:no value"
142-
string(68) "error:0E06D06C:configuration file routines:NCONF_get_string:no value"
143-
string(68) "error:0E06D06C:configuration file routines:NCONF_get_string:no value"
144-
string(68) "error:0E06D06C:configuration file routines:NCONF_get_string:no value"
145-
string(50) "error:02001015:system library:fopen:Is a directory"
146-
string(51) "error:2006D002:BIO routines:BIO_new_file:system lib"
147-
string(49) "error:09072007:PEM routines:PEM_write_bio:BUF lib"
148-
149-
Warning: openssl_pkey_export(): cannot get key from parameter 1 in %s on line %d
150-
string(72) "error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt"
151-
string(53) "error:0906A065:PEM routines:PEM_do_header:bad decrypt"
152-
string(54) "error:0906D06C:PEM routines:PEM_read_bio:no start line"
153-
string(54) "error:0906D06C:PEM routines:PEM_read_bio:no start line"
154-
string(72) "error:04066076:rsa routines:RSA_EAY_PRIVATE_ENCRYPT:unknown padding type"
155-
string(78) "error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error"
156-
string(72) "error:04065072:rsa routines:RSA_EAY_PRIVATE_DECRYPT:padding check failed"
157-
string(54) "error:0906D06C:PEM routines:PEM_read_bio:no start line"
158-
string(71) "error:04068076:rsa routines:RSA_EAY_PUBLIC_ENCRYPT:unknown padding type"
159-
string(54) "error:0906D06C:PEM routines:PEM_read_bio:no start line"
160-
string(79) "error:0407006A:rsa routines:RSA_padding_check_PKCS1_type_1:block type is not 01"
161-
string(71) "error:04067072:rsa routines:RSA_EAY_PUBLIC_DECRYPT:padding check failed"
151+
PKEY errors
152+
openssl_pkey_export_to_file opening: ok
153+
openssl_pkey_export_to_file pem: ok
154+
openssl_pkey_export_to_file write: ok
155+
openssl_pkey_export: ok
156+
openssl_pkey_get_public: ok
157+
openssl_private_encrypt: ok
158+
openssl_private_decrypt: ok
159+
openssl_private_(en|de)crypt padding: ok
162160
X509 errors
163-
164-
Warning: openssl_x509_export_to_file(): cannot get cert from parameter 1 in %s on line %d
165-
string(61) "error:02001002:system library:fopen:No such file or directory"
166-
string(53) "error:2006D080:BIO routines:BIO_new_file:no such file"
167-
168-
Warning: openssl_x509_export_to_file(): cannot get cert from parameter 1 in %s on line %d
169-
string(54) "error:0906D06C:PEM routines:PEM_read_bio:no start line"
170-
171-
Warning: openssl_x509_export_to_file(): error opening file %s in %s on line %d
172-
string(50) "error:02001015:system library:fopen:Is a directory"
173-
string(51) "error:2006D002:BIO routines:BIO_new_file:system lib"
174-
string(90) "error:0B086079:x509 certificate routines:X509_STORE_CTX_purpose_inherit:unknown purpose id"
175-
176-
Warning: openssl_x509_checkpurpose(): error loading file %s in %s on line %d
161+
openssl_x509_export_to_file open: ok
162+
openssl_x509_export_to_file pem: ok
163+
openssl_x509_export_to_file write: ok
164+
openssl_x509_checkpurpose purpose: ok
177165
CSR errors
178-
string(61) "error:02001002:system library:fopen:No such file or directory"
179-
string(53) "error:2006D080:BIO routines:BIO_new_file:no such file"
180-
string(55) "error:20068079:BIO routines:BIO_gets:unsupported method"
181-
string(54) "error:0906D06C:PEM routines:PEM_read_bio:no start line"
182-
string(54) "error:0906D06C:PEM routines:PEM_read_bio:no start line"
166+
openssl_csr_get_subject open: ok
167+
openssl_csr_get_subjec pem: ok

0 commit comments

Comments
 (0)