Skip to content

fix(encryption): deprecate deriving the passwords of a key set beyond the PBES2 cap - #831

Merged
Spomky merged 1 commit into
4.3.xfrom
fix/pbes2-key-set-iterations
Nov 29, 2026
Merged

Spomky merged 1 commit into
4.3.xfrom
fix/pbes2-key-set-iterations

Conversation

@Spomky

@Spomky Spomky commented Nov 29, 2026

Copy link
Copy Markdown
Member

Follow-up of GHSA-3vqw-452c-g726, reported by inanna-malick.

The defect

JWEDecrypter tries the keys of a key set one after the other, and derives each PBES2 password with the iteration count of the token. Since GHSA-3vqw, the recipients of a token share the cap: each one may request at most max_count / N iterations. One recipient tried with a key set of K passwords still costs K derivations at that share.

A compact token at the default cap (1,000,000), with a password that is not in the key set:

Passwords in the key set 1 2 4 8
Derivations 1 2 4 8
Time 1.6 s 3.3 s 8.9 s 20.1 s

The application chooses the key set, so a sender cannot push the cost beyond what the application configured: K tokens sent to an application holding one password cost the same. But max_count reads like a bound per token, and it is one only with a single password.

The fix

JWEDecrypter counts the iterations derived for one recipient over the keys it tries. When the next derivation would take the total past the share of the recipient, it triggers a deprecation, once, and carries on:

Trying more than %d iterations of "%s" in total with the keys of a key set for one recipient is deprecated: from 5.0.0, the keys share the maximum iteration count of the algorithm and the next ones are not tried. Raise that maximum to keep trying them.

Only the keys that are derived count. Keys of another type do not count, and neither do keys rejected for their usage or their algorithm (alg, use, key_ops). So a key set holding one password next to signature keys or RSA keys keeps the whole share. Nothing is rejected in 4.3.

For 5.0, the rule announced is a budget over the keys actually tried, rather than a division of the cap by the number of keys. Both bound a token to max_count. The division would reject, upfront, tokens that are decrypted today with a single derivation. With the default cap:

Token Key set Derivations today Dividing by count() Budget (5.0)
p2c = 600,000 2 passwords, right one first 1 rejected decrypted
p2c = 600,000 2 passwords, right one last 2 rejected rejected, unless max_count is raised
p2c = 300,000 3 RSA keys + 1 password 1 rejected decrypted
p2c = 300,000 3 oct keys for HS256, A128KW or use: sig + 1 password 1 rejected decrypted

600,000 is the iteration count OWASP recommends for PBKDF2-HMAC-SHA256, so the first two rows are ordinary key rotations.

Behaviour changes

No token changes outcome, and no failure changes: the only difference is the new deprecation. The default JWEBuilder count (4,096) stays below it for up to 244 passwords at the default cap.

Verification

  • PBES2IterationCountTest::thePasswordsOfAKeySetTriedBeyondTheCapAreDeprecated has 9 cases. They cover one and two recipients, passwords within the share, past it, and exactly at it before another one, the right password first, and keys that are not derived. The 4 cases past the share fail on 4.3.x. The other 5 check that the token is decrypted without a deprecation, before and after.
  • Full suite in phpqa:8.4 after a clean composer install: 3046 tests on 4.3.x, 3055 here, all green.
  • PHPStan reports the same as on 4.3.x. ECS, Rector, Deptrac and parallel-lint are clean, and so is php -l in phpqa:8.2.
  • Infection on the changed lines, run locally with a 120 s timeout: every mutant is killed.
  • Behaviour probe: the 4.3.x JWEDecrypter against this one, on 588 PBES2 tokens. They combine PBES2-HS256 and HS512, 7 iteration counts, compact and JSON General with up to 3 recipients, a p2c of 0, −5 or a string, and 7 key sets. Calls go through decrypt() for each recipient and through JWELoader::loadAndDecrypt().
    • The outcome and the failures never change.
    • The deprecation appears in 74 decrypt() cases, all of them past the share. It never appears with a single password, and never because of keys that are not derived.
  • One private method is added; no public or protected signature changes.

Not in this PR

  • The 5.0.0 refusal (the budget above, without the deprecation).
  • Documentation: what a PBES2 token costs with a key set, and how to size max_count.

… the PBES2 cap

JWEDecrypter tries the keys of a key set one after the other and derives
each password with the iteration count of the token. The PBES2 cap bounds
one derivation, and since the recipients of a token share it, one
recipient tried with one key: a recipient at the cap tried with a key set
holding N passwords costs N derivations at the cap.

Deriving more iterations in total than the share of a recipient with the
keys of a key set is now deprecated. From 5.0.0, the passwords tried for a
recipient share that part of the cap, and the ones beyond it are not
tried. Only the keys that are derived count: keys of another type, and
keys rejected for their usage or their algorithm, are left out, so a key
set holding one password next to other keys keeps the whole share.
Nothing is rejected: the tokens decrypted before are still decrypted.

Reported by inanna-malick, as a follow-up of ANT-2026-P0RDGIIJ.
@Spomky
Spomky force-pushed the fix/pbes2-key-set-iterations branch from b6d2427 to 144a8e7 Compare November 29, 2026 09:46
@Spomky
Spomky merged commit a5eea10 into 4.3.x Nov 29, 2026
18 checks passed
@Spomky
Spomky deleted the fix/pbes2-key-set-iterations branch November 29, 2026 09:52
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