diff --git a/NEWS b/NEWS index 4eed5c34ec1..767b35b0048 100644 --- a/NEWS +++ b/NEWS @@ -13,6 +13,10 @@ PHP NEWS . Fixed bug GH-21052 (Preloaded constant erroneously propagated to file-cached script). (ilutov) +- OpenSSL: + . Fixed bug GH-21083 (Skip private_key_bits validation for EC/curve-based + keys). (iliaal) + - PCNTL: . Fixed signal handler installation on AIX by bumping the storage size of the num_signals global. (Calvin Buckley) diff --git a/ext/openssl/openssl_backend_common.c b/ext/openssl/openssl_backend_common.c index 78da71b53d9..84e19dbea09 100644 --- a/ext/openssl/openssl_backend_common.c +++ b/ext/openssl/openssl_backend_common.c @@ -1444,7 +1444,10 @@ static const char *php_openssl_get_evp_pkey_name(int key_type) { EVP_PKEY *php_openssl_generate_private_key(struct php_x509_request * req) { - if (req->priv_key_bits < MIN_KEY_LENGTH) { + if ((req->priv_key_type == OPENSSL_KEYTYPE_RSA || + req->priv_key_type == OPENSSL_KEYTYPE_DH || + req->priv_key_type == OPENSSL_KEYTYPE_DSA) && + req->priv_key_bits < MIN_KEY_LENGTH) { php_error_docref(NULL, E_WARNING, "Private key length must be at least %d bits, configured to %d", MIN_KEY_LENGTH, req->priv_key_bits); return NULL; diff --git a/ext/openssl/tests/gh21083.phpt b/ext/openssl/tests/gh21083.phpt new file mode 100644 index 00000000000..31afb7ca084 --- /dev/null +++ b/ext/openssl/tests/gh21083.phpt @@ -0,0 +1,61 @@ +--TEST-- +GH-21083 (openssl_pkey_new() fails for EC keys when private_key_bits is not set) +--EXTENSIONS-- +openssl +--SKIPIF-- + +--ENV-- +OPENSSL_CONF= +--FILE-- + $conf, + 'private_key_type' => OPENSSL_KEYTYPE_EC, + 'curve_name' => 'prime256v1', +]); +var_dump($key !== false); +$details = openssl_pkey_get_details($key); +var_dump($details['bits']); +var_dump($details['type'] === OPENSSL_KEYTYPE_EC); +echo "EC OK\n"; + +// X25519 - fixed size key, private_key_bits should not be required +if (defined('OPENSSL_KEYTYPE_X25519')) { + $key = openssl_pkey_new([ + 'config' => $conf, + 'private_key_type' => OPENSSL_KEYTYPE_X25519, + ]); + var_dump($key !== false); + echo "X25519 OK\n"; +} else { + echo "bool(true)\nX25519 OK\n"; +} + +// Ed25519 - fixed size key, private_key_bits should not be required +if (defined('OPENSSL_KEYTYPE_ED25519')) { + $key = openssl_pkey_new([ + 'config' => $conf, + 'private_key_type' => OPENSSL_KEYTYPE_ED25519, + ]); + var_dump($key !== false); + echo "Ed25519 OK\n"; +} else { + echo "bool(true)\nEd25519 OK\n"; +} + +unlink($conf); +?> +--EXPECT-- +bool(true) +int(256) +bool(true) +EC OK +bool(true) +X25519 OK +bool(true) +Ed25519 OK