Skip to content

Commit 45e6dc7

Browse files
committed
Merge branch 'PHP-8.0'
* PHP-8.0: Suppress OpenSSL error on missing optional config
2 parents 353f7ff + 4fb8252 commit 45e6dc7

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

ext/openssl/openssl.c

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -719,16 +719,26 @@ static inline int php_openssl_config_check_syntax(const char * section_label, co
719719
}
720720
/* }}} */
721721

722+
static char *php_openssl_conf_get_string(
723+
LHASH_OF(CONF_VALUE) *conf, const char *group, const char *name) {
724+
char *str = CONF_get_string(conf, group, name);
725+
if (str == NULL) {
726+
/* OpenSSL reports an error if a configuration value is not found.
727+
* However, we don't want to generate errors for optional configuration. */
728+
ERR_clear_error();
729+
}
730+
return str;
731+
}
732+
722733
static int php_openssl_add_oid_section(struct php_x509_request * req) /* {{{ */
723734
{
724735
char * str;
725736
STACK_OF(CONF_VALUE) * sktmp;
726737
CONF_VALUE * cnf;
727738
int i;
728739

729-
str = CONF_get_string(req->req_config, NULL, "oid_section");
740+
str = php_openssl_conf_get_string(req->req_config, NULL, "oid_section");
730741
if (str == NULL) {
731-
php_openssl_store_errors();
732742
return SUCCESS;
733743
}
734744
sktmp = CONF_get_section(req->req_config, str);
@@ -813,10 +823,8 @@ static int php_openssl_parse_config(struct php_x509_request * req, zval * option
813823
}
814824

815825
/* read in the oids */
816-
str = CONF_get_string(req->req_config, NULL, "oid_file");
817-
if (str == NULL) {
818-
php_openssl_store_errors();
819-
} else if (!php_openssl_open_base_dir_chk(str)) {
826+
str = php_openssl_conf_get_string(req->req_config, NULL, "oid_file");
827+
if (str != NULL && !php_openssl_open_base_dir_chk(str)) {
820828
BIO *oid_bio = BIO_new_file(str, PHP_OPENSSL_BIO_MODE_R(PKCS7_BINARY));
821829
if (oid_bio) {
822830
OBJ_create_objects(oid_bio);
@@ -828,11 +836,11 @@ static int php_openssl_parse_config(struct php_x509_request * req, zval * option
828836
return FAILURE;
829837
}
830838
SET_OPTIONAL_STRING_ARG("digest_alg", req->digest_name,
831-
CONF_get_string(req->req_config, req->section_name, "default_md"));
839+
php_openssl_conf_get_string(req->req_config, req->section_name, "default_md"));
832840
SET_OPTIONAL_STRING_ARG("x509_extensions", req->extensions_section,
833-
CONF_get_string(req->req_config, req->section_name, "x509_extensions"));
841+
php_openssl_conf_get_string(req->req_config, req->section_name, "x509_extensions"));
834842
SET_OPTIONAL_STRING_ARG("req_extensions", req->request_extensions_section,
835-
CONF_get_string(req->req_config, req->section_name, "req_extensions"));
843+
php_openssl_conf_get_string(req->req_config, req->section_name, "req_extensions"));
836844
SET_OPTIONAL_LONG_ARG("private_key_bits", req->priv_key_bits,
837845
CONF_get_number(req->req_config, req->section_name, "default_bits"));
838846

@@ -841,11 +849,9 @@ static int php_openssl_parse_config(struct php_x509_request * req, zval * option
841849
if (optional_args && (item = zend_hash_str_find(Z_ARRVAL_P(optional_args), "encrypt_key", sizeof("encrypt_key")-1)) != NULL) {
842850
req->priv_key_encrypt = Z_TYPE_P(item) == IS_TRUE ? 1 : 0;
843851
} else {
844-
str = CONF_get_string(req->req_config, req->section_name, "encrypt_rsa_key");
852+
str = php_openssl_conf_get_string(req->req_config, req->section_name, "encrypt_rsa_key");
845853
if (str == NULL) {
846-
str = CONF_get_string(req->req_config, req->section_name, "encrypt_key");
847-
/* it is sure that there are some errors as str was NULL for encrypt_rsa_key */
848-
php_openssl_store_errors();
854+
str = php_openssl_conf_get_string(req->req_config, req->section_name, "encrypt_key");
849855
}
850856
if (str != NULL && strcmp(str, "no") == 0) {
851857
req->priv_key_encrypt = 0;
@@ -873,12 +879,10 @@ static int php_openssl_parse_config(struct php_x509_request * req, zval * option
873879

874880
/* digest alg */
875881
if (req->digest_name == NULL) {
876-
req->digest_name = CONF_get_string(req->req_config, req->section_name, "default_md");
882+
req->digest_name = php_openssl_conf_get_string(req->req_config, req->section_name, "default_md");
877883
}
878884
if (req->digest_name != NULL) {
879885
req->digest = req->md_alg = EVP_get_digestbyname(req->digest_name);
880-
} else {
881-
php_openssl_store_errors();
882886
}
883887
if (req->md_alg == NULL) {
884888
req->md_alg = req->digest = EVP_sha1();
@@ -900,10 +904,8 @@ static int php_openssl_parse_config(struct php_x509_request * req, zval * option
900904
#endif
901905

902906
/* set the string mask */
903-
str = CONF_get_string(req->req_config, req->section_name, "string_mask");
904-
if (str == NULL) {
905-
php_openssl_store_errors();
906-
} else if (!ASN1_STRING_set_default_mask_asc(str)) {
907+
str = php_openssl_conf_get_string(req->req_config, req->section_name, "string_mask");
908+
if (str != NULL && !ASN1_STRING_set_default_mask_asc(str)) {
907909
php_error_docref(NULL, E_WARNING, "Invalid global string mask setting %s", str);
908910
return FAILURE;
909911
}
@@ -2820,9 +2822,8 @@ static int php_openssl_make_REQ(struct php_x509_request * req, X509_REQ * csr, z
28202822
php_openssl_store_errors();
28212823
return FAILURE;
28222824
}
2823-
attr_sect = CONF_get_string(req->req_config, req->section_name, "attributes");
2825+
attr_sect = php_openssl_conf_get_string(req->req_config, req->section_name, "attributes");
28242826
if (attr_sect == NULL) {
2825-
php_openssl_store_errors();
28262827
attr_sk = NULL;
28272828
} else {
28282829
attr_sk = CONF_get_section(req->req_config, attr_sect);
@@ -3644,10 +3645,7 @@ static EVP_PKEY * php_openssl_generate_private_key(struct php_x509_request * req
36443645
return NULL;
36453646
}
36463647

3647-
randfile = CONF_get_string(req->req_config, req->section_name, "RANDFILE");
3648-
if (randfile == NULL) {
3649-
php_openssl_store_errors();
3650-
}
3648+
randfile = php_openssl_conf_get_string(req->req_config, req->section_name, "RANDFILE");
36513649
php_openssl_load_rand_file(randfile, &egdsocket, &seeded);
36523650

36533651
if ((req->priv_key = EVP_PKEY_new()) != NULL) {

ext/openssl/tests/bug80747.phpt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ $conf = array(
1313
'private_key_bits' => 511,
1414
);
1515
var_dump(openssl_pkey_new($conf));
16+
while ($e = openssl_error_string()) {
17+
echo $e, "\n";
18+
}
1619

1720
?>
18-
--EXPECT--
21+
--EXPECTF--
1922
bool(false)
23+
error:%s:key size too small

0 commit comments

Comments
 (0)