Skip to content

Fix GH-21083: openssl_pkey_new() fails for EC keys when private_key_bits is not set#21211

Open
ThanhNguyxn wants to merge 1 commit intophp:PHP-8.3from
ThanhNguyxn:fix/gh-21083-openssl-ec-min-key-length
Open

Fix GH-21083: openssl_pkey_new() fails for EC keys when private_key_bits is not set#21211
ThanhNguyxn wants to merge 1 commit intophp:PHP-8.3from
ThanhNguyxn:fix/gh-21083-openssl-ec-min-key-length

Conversation

@ThanhNguyxn
Copy link

Summary

The MIN_KEY_LENGTH check in php_openssl_generate_private_key() was applied unconditionally to all key types, including EC where key size is determined by the curve (e.g., prime256v1 = 256 bits, secp384r1 = 384 bits) rather than private_key_bits. When private_key_bits is not explicitly set, it defaults to 0 (or the value from openssl.cnf's default_bits), which is below the 384-bit minimum — causing the function to fail with:

Private key length must be at least 384 bits, configured to 0

This is particularly visible with OpenSSL 3.6 due to stricter error handling, and affects multiple downstream projects (Laravel Passport, web-push-php, LibreNMS).

Changes

  • Moved the type determination (php_openssl_get_evp_pkey_type()) before the MIN_KEY_LENGTH check
  • Wrapped the minimum bits validation in a conditional that skips EC key types (EVP_PKEY_EC), since private_key_bits is not applicable to them
  • Added test ext/openssl/tests/gh21083.phpt that generates EC keys with prime256v1 and secp384r1 without setting private_key_bits
  • Added NEWS entry

Related

…y_bits is not set

The MIN_KEY_LENGTH check in php_openssl_generate_private_key() was
applied unconditionally to all key types, including EC where key size
is determined by the curve rather than private_key_bits. This caused
failures when private_key_bits defaulted to 0 (below the 384-bit
minimum), particularly visible with OpenSSL 3.6's stricter error
handling.

Skip the minimum bits validation for EC key types since the parameter
is not applicable to them.

Closes phpGH-21083
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant