Skip to content

Prevent unexpected array entry conversion when reading key #16693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions ext/openssl/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3571,19 +3571,21 @@ static EVP_PKEY *php_openssl_pkey_from_zval(
if (!(Z_TYPE_P(val) == IS_STRING || Z_TYPE_P(val) == IS_OBJECT)) {
TMP_CLEAN;
}
if (!try_convert_to_string(val)) {
zend_string *val_str = zval_try_get_string(val);
if (!val_str) {
TMP_CLEAN;
}

if (Z_STRLEN_P(val) > 7 && memcmp(Z_STRVAL_P(val), "file://", sizeof("file://") - 1) == 0) {
if (!php_openssl_check_path_str(Z_STR_P(val), file_path, arg_num)) {
if (ZSTR_LEN(val_str) > 7 && memcmp(ZSTR_VAL(val_str), "file://", sizeof("file://") - 1) == 0) {
if (!php_openssl_check_path_str(val_str, file_path, arg_num)) {
zend_string_release_ex(val_str, false);
TMP_CLEAN;
}
is_file = true;
}
/* it's an X509 file/cert of some kind, and we need to extract the data from that */
if (public_key) {
cert = php_openssl_x509_from_str(Z_STR_P(val), arg_num, false, NULL);
cert = php_openssl_x509_from_str(val_str, arg_num, false, NULL);

if (cert) {
free_cert = 1;
Expand All @@ -3593,10 +3595,11 @@ static EVP_PKEY *php_openssl_pkey_from_zval(
if (is_file) {
in = BIO_new_file(file_path, PHP_OPENSSL_BIO_MODE_R(PKCS7_BINARY));
} else {
in = BIO_new_mem_buf(Z_STRVAL_P(val), (int)Z_STRLEN_P(val));
in = BIO_new_mem_buf(ZSTR_VAL(val_str), (int)ZSTR_LEN(val_str));
}
if (in == NULL) {
php_openssl_store_errors();
zend_string_release_ex(val_str, false);
TMP_CLEAN;
}
key = PEM_read_bio_PUBKEY(in, NULL,NULL, NULL);
Expand All @@ -3609,10 +3612,11 @@ static EVP_PKEY *php_openssl_pkey_from_zval(
if (is_file) {
in = BIO_new_file(file_path, PHP_OPENSSL_BIO_MODE_R(PKCS7_BINARY));
} else {
in = BIO_new_mem_buf(Z_STRVAL_P(val), (int)Z_STRLEN_P(val));
in = BIO_new_mem_buf(ZSTR_VAL(val_str), (int)ZSTR_LEN(val_str));
}

if (in == NULL) {
zend_string_release_ex(val_str, false);
TMP_CLEAN;
}
if (passphrase == NULL) {
Expand All @@ -3625,6 +3629,8 @@ static EVP_PKEY *php_openssl_pkey_from_zval(
}
BIO_free(in);
}

zend_string_release_ex(val_str, false);
}

if (key == NULL) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
openssl_pkey_export_to_file object to string conversion
--EXTENSIONS--
openssl
--FILE--
<?php

class Test {
public function __toString(): string {
return "file://" . __DIR__ . "/private_rsa_1024.key";
}
}

$path = new Test;
$key = [$path, ""];
@openssl_pkey_export_to_file($key, str_repeat("a", 10000), passphrase: "");
var_dump($key);

?>
--EXPECT--
array(2) {
[0]=>
object(Test)#1 (0) {
}
[1]=>
string(0) ""
}
Loading