Repository navigation
fix(core): bound the RSA public exponent to 64 bits whatever the modulus - #828
Merged
Merged
Conversation
RSAKey bounded the public exponent "e" to 64 bits above a modulus of 3072 bits only, as OpenSSL does, and to the length of the modulus below it. A key of 3072 bits whose exponent was as long as its modulus was therefore accepted, and the public operation PS*, RSA-OAEP and RSA1_5 compute in PHP costs as many squarings as "e" has bits: with GMP, a PS256 signature under such a key, which the sender of a DPoP proof chooses, took 30.9 ms to reject, against 0.7 ms with a 64-bit exponent; with BCMath, 1.48 s against 52 ms. The exponent is now refused beyond 64 bits whatever the modulus, and beyond the length of the modulus as before. The dearest key left is one of 16384 bits with a 64-bit exponent: 8.6 ms with GMP, 1.18 s with BCMath, as before this change. A key with a larger exponent is refused although FIPS 186-5 allows exponents below 2^256: generators draw 65537, and Go and BoringSSL already refuse exponents above 2^31 - 1 and 33 bits.
…ports on 4.2.x The phpqa image rebuilt on 2026-11-20 ships PHPStan 2.3.7, which reports a mixed value as the condition of the ternaries of the processHeaders() methods of JSONFlattenedSerializer and JSONGeneralSerializer and of the if of MixedPublicAndPrivateKeys: the Static Analysis job failed on 4.2.x whatever the change. The baseline, generated by castor phpstan-baseline, gains those three entries and nothing else.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Target branch: 4.2.x
Includes:
The defect
RSAKeytakes OpenSSL's limits (e2e7248, 4.2.6): a modulus of 16384 bits at most, and a public exponent of 64 bits at most above a 3072-bit modulus, or no longer than the modulus below that. So a 3072-bit key whoseeis as long as itsnis accepted. PS256/384/512, RSA-OAEP, RSA-OAEP-256 and RSA1_5 compute the public operation in PHP. That costs one squaring per bit ofe, and whoever supplies the key choosese(for instance the sender of a DPoP proof, whose key is in itsjwkheader).PS256 verification of a signature under a key of all-
0xFFnande, measured on 4.2.x (ghcr.io/spomky-labs/phpqa:8.5, PHP 8.5, brick/math calculator set explicitly):The fix
eis refused beyond 64 bits whatever the modulus, and beyond the length of the modulus as before (min($modulusLength, MAX_PUBLIC_EXPONENT_LENGTH)).SMALL_MODULUS_LENGTHgoes.What it buys, and what it does not:
e.Compatibility
A key whose public exponent is longer than 64 bits, with a modulus of 3072 bits or less, is now refused. FIPS 186-5 (Appendix A.1.1) allows exponents below 2^256, so such a key is valid. But no common generator draws one (65537 is the norm), and Go (
crypto/rsa, exponents above 2^31 − 1) and BoringSSL (above 33 bits) already refuse it.This is in the patch line, as the bound it narrows was in 4.2.6. If it should wait for a minor because it refuses keys OpenSSL accepts, the commit applies as is to 4.3.x.
Tests
RSAKeyTest:theLimitsOfOpenSSLAreAcceptedbecomestheLargestKeysAreAccepted: 16384, 3080, 3072 and 2048-bit moduli with a 64-bit exponent, and a 32-bit modulus with a 32-bit exponent.aKeyBeyondTheLimitsOfOpenSSLIsRejectedbecomesaKeyBeyondTheLimitsIsRejected. It gains three cases: a 72-bit exponent under 3072 and 2048-bit moduli, and an exponent as long as a 3072-bit modulus. The two cases of an exponent longer than a 3072-bit or a 2048-bit modulus become an exponent longer than a 32-bit modulus, since the 72-bit cases now cover the 2048-bit modulus.--filter 'RSA|Rsa|PS256|PS384|PS512|RS256|RS384|RS512|KeyAnalyzer|RSAKey|OAEP'(phpunit-11,.ci-tools/phpunit.xml.dist): OK, 161 tests, 564 assertions, 1 incomplete. The incomplete one isJWKFactoryTest::iCanLoadAP12CertificateThatContainsARSAKey("Unable to run this test using the last OpenSSL versions"); it was incomplete before this change.ECS and Rector on both files: nothing to change. PHPStan on
RSAKey.php: no error. The whole suite and Infection are left to CI.The second commit
The Static Analysis job is red on 4.2.x whatever the change. The phpqa image rebuilt on 2026-11-20 ships PHPStan 2.3.7, which reports three conditions on a mixed value, none of them in
RSAKey: the ternaries ofprocessHeaders()inJSONFlattenedSerializer(line 105) andJSONGeneralSerializer(line 124), and theifofMixedPublicAndPrivateKeys(line 27). The last green run on 4.2.x is #825, on 2026-11-10. The second commit adds those three entries to the baseline (castor phpstan-baseline, nothing else moves), as was done on the 5.3.x line of webauthn-framework for the same image. It can be dropped if the baseline is fixed on 4.2.x first.