Skip to content

fix(core): bound the RSA public exponent to 64 bits whatever the modulus - #828

Merged
Spomky merged 2 commits into
4.2.xfrom
fix/rsa-public-exponent-64-bits
Nov 23, 2026
Merged

Spomky merged 2 commits into
4.2.xfrom
fix/rsa-public-exponent-64-bits

Conversation

@Spomky

@Spomky Spomky commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Target branch: 4.2.x

  • It is a Bug fix
  • It is a New feature
  • It is related to dependencies

Includes:

  • Breaks BC
  • Deprecations

The defect

RSAKey takes 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 whose e is as long as its n is accepted. PS256/384/512, RSA-OAEP, RSA-OAEP-256 and RSA1_5 compute the public operation in PHP. That costs one squaring per bit of e, and whoever supplies the key chooses e (for instance the sender of a DPoP proof, whose key is in its jwk header).

PS256 verification of a signature under a key of all-0xFF n and e, measured on 4.2.x (ghcr.io/spomky-labs/phpqa:8.5, PHP 8.5, brick/math calculator set explicitly):

Key GMP BCMath
n 3072 bits, e 3072 bits (accepted on 4.2.x) 30.9 ms 1479 ms
n 2048 bits, e 2048 bits (accepted on 4.2.x) 7.4 ms 502 ms
n 3072 bits, e 64 bits 0.7 ms 52 ms
n 8192 bits, e 64 bits 3.0 ms 286 ms
n 16384 bits, e 64 bits 8.6 ms 1177 ms
n 2048 bits, e 24 bits 0.3 ms 15 ms

The fix

e is refused beyond 64 bits whatever the modulus, and beyond the length of the modulus as before (min($modulusLength, MAX_PUBLIC_EXPONENT_LENGTH)). SMALL_MODULUS_LENGTH goes.

What it buys, and what it does not:

  • The keys of up to 3072 bits stop being the dearest. With GMP, the worst case falls from 30.9 ms to 8.6 ms.
  • The dearest key left is the one OpenSSL's limits already allowed above 3072 bits: 16384 bits with a 64-bit e.
  • With BCMath that key stays at about 1.2 s. Lowering it would mean bounding the modulus below 16384 bits, which refuses keys OpenSSL accepts. That is not done here.

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:

  • theLimitsOfOpenSSLAreAccepted becomes theLargestKeysAreAccepted: 16384, 3080, 3072 and 2048-bit moduli with a 64-bit exponent, and a 32-bit modulus with a 32-bit exponent.
  • aKeyBeyondTheLimitsOfOpenSSLIsRejected becomes aKeyBeyondTheLimitsIsRejected. 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.
  • On 4.2.x without the change, the three new cases fail.

--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 is JWKFactoryTest::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 of processHeaders() in JSONFlattenedSerializer (line 105) and JSONGeneralSerializer (line 124), and the if of MixedPublicAndPrivateKeys (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.

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.
@Spomky Spomky added this to the 4.2.8 milestone Nov 23, 2026
@Spomky Spomky added the bug label Nov 23, 2026
…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.
@Spomky
Spomky merged commit 284c2e2 into 4.2.x Nov 23, 2026
17 checks passed
@Spomky
Spomky deleted the fix/rsa-public-exponent-64-bits branch November 23, 2026 20:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant