Skip to content

fix(encryption): deprecate "dir" JWEs without protected header and without "aad" - #796

Merged
Spomky merged 3 commits into
4.3.xfrom
fix/dir-jwe-empty-aad
Nov 8, 2026
Merged

Spomky merged 3 commits into
4.3.xfrom
fix/dir-jwe-empty-aad

Conversation

@Spomky

@Spomky Spomky commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

The issue

AES-GCM key wrapping (A128GCMKW, A192GCMKW, A256GCMKW) encrypts the CEK with AES-GCM under the key and an empty AAD. In the JSON serializations, alg and enc may sit in the per-recipient header. A dir JWE that has neither a protected header nor an aad member then has its content encrypted with an empty AAD too, which is the same computation.

With a decrypter that registers dir, an AGCMKW algorithm and the AGCM algorithm of the same size, and a key without an alg parameter:

  • the encrypted key of an A*GCMKW token, with the iv and tag of its header, presented as the content of such a dir JWE, is decrypted to the CEK of that token. If the application shows decrypted payloads to the sender, that CEK opens the original token;
  • conversely, the content of a dir JWE built that way is a valid A*GCMKW encrypted key of its payload.

Compact JWEs, hence JWTs, always have a protected header and cannot take this shape.

The change

Refusing the shape would break tokens that the JSON serializations allow, so this PR only deprecates it:

  • JWEDecrypter triggers a deprecation when it decrypts a dir JWE whose encoded protected header is empty and that has no aad member. Only the key that decrypted the token reports it, as for the longer dir keys: a token that no key decrypts triggers nothing.
  • JWEBuilder triggers a deprecation when it builds such a JWE.

Both still work. Both will be refused in 5.0.0; the trigger_deprecation grep of #717 covers that. The two new methods are private, and no signature changes.

The rule follows the AAD of the content, not the decoded header: an empty protected member is reported, an e30 ({}) one is not; an empty aad member is enough, because the AAD then ends with a period. It applies whatever the content encryption algorithm. Only AES-GCM collides with a key wrapping algorithm of the library today, but the same collision would come back with any AEAD offered for both uses, such as a ChaCha20-Poly1305 content encryption next to the experimental chacha20-poly1305 key encryption, which also uses an empty AAD.

Behaviour changes

JWE before after
dir, JSON, no protected header, no aad: built built built, with a deprecation
the same: decrypted decrypted decrypted, with a deprecation
an A*GCMKW encrypted key presented as such a JWE decrypted (CEK returned) the same, with a deprecation
dir with a protected header (every compact JWE), or with an aad member, even empty unchanged unchanged
any other key management mode, with or without protected header unchanged unchanged

Upgrade note

Proposed text for the "From v4.2 to v4.3" guide:

"dir" JWEs without a protected header. In the JSON serializations, a dir JWE whose alg and enc are only in the per-recipient header, and that has no aad member, encrypts its content exactly as AES-GCM key wrapping encrypts a key. Under a key that serves both dir and an A*GCMKW algorithm, one can then pass for the other. Building or decrypting such a JWE now triggers a deprecation notice, and 5.0 will refuse it: put alg and enc in the shared protected header (withSharedProtectedHeader()). Compact JWEs always have a protected header and are not affected.

And for the key management pages, as the issue asks:

Give an alg parameter to every key of a key set used with several algorithms. A key with "alg": "A128GCMKW" is never used for dir, and the key analyzer reports the keys without it.

Mitigations available today

  • An alg parameter on every key.
  • An AlgorithmChecker created with protectedHeader: true, so that alg is read from the protected header only.
  • Not registering dir next to key wrapping algorithms in the same decrypter.

Verification

  • DirectEncryptionAdditionalAuthenticatedDataTest, 31 cases. 19 fail on the base branch, each on its deprecation assertion. The assertions before it pass there, which confirms the issue:
    • the encrypted key of an A128GCMKW, A192GCMKW or A256GCMKW token, presented as the content of a flattened or general dir JWE, is decrypted with the same key (no alg) to the CEK of that token, and that CEK decrypts the original content. One deprecation now (6 cases);
    • a dir JWE built without protected header and without aad: its content, IV and tag unwrap with A*GCMKW to its payload. One deprecation at build time now (3 cases); decrypting it, flattened and general, for the six content encryption algorithms, gives one deprecation per decryption (6 cases);
    • hand-made tokens pin the rule: no protected header, an empty protected member and a shared unprotected header only are reported; protected set to e30, a protected header with a parameter, an empty aad member and an aad member are not (7 cases);
    • a token that no key decrypts triggers nothing, and a key set with a wrong key first triggers exactly one deprecation (1 case);
    • not reported: dir with a protected header (compact and JSON), with enc protected and alg in the recipient header, with an empty or a non-empty aad (6 cases); A128KW, and A128GCMKW with two recipients and an empty shared protected header (2 cases).
  • BC, against the current base branch (ae49782e, which includes fix(nested-token): deprecate a "cty" outside the protected header of the JWE #794, fix(encryption): read "key_ops" according to the key management mode #797, fix(encryption): check the size of an AES key wrapped key before unwrapping it #799 and feat(key-mgmt): let JKUFactory and X5UFactory refuse HTTP redirects #800):
    • the public and protected API of src/ (445 types), dumped by reflection, is identical;
    • a probe decrypts a corpus of 96 tokens generated once, each with a key and with a key set, and runs 16 builder configurations, with both versions. The corpus covers dir with each content encryption algorithm and header layout, longer keys, a filled encrypted key, edited protected members, the forged AGCMKW tokens, A128KW, AGCMKW with two recipients, ECDH-ES and RSA-OAEP-256. All 208 cases have the same outcome and exception class. 72 of them gain exactly one of the two new deprecations (66 decryptions, 6 builds), and none loses one;
    • a run of the whole suite that logs trigger_deprecation() shows that only the new test class (9 builds, 22 decryptions) and KeyOperationsTest::aKeyWrappingKeyThatDecryptsDirectContentIsReported (1 decryption, see below) trigger the new deprecations. No other test, bundle path or nested token builds or decrypts this shape.
  • Whole suite with phpunit-11 in phpqa:8.4: 2,688 tests green (base: 2,657, the difference is the new class), with the same 3 skipped and 2 incomplete. ECS, Rector, PHPStan, Deptrac, the license check, the lint and composer validate --strict are clean with the current image.
  • Mutation testing on the diff: 22 mutants, all killed in a local run with a 60-second timeout (MSI 100%). With the 3-second timeout of infection.json.dist, the CI run of this PR kills 4 and skips 18 as "required more time than configured": they are not escaped, the covering test files are just slow.
  • 4.3.x is merged into this branch; the commit of the fix is unchanged (no rebase). fix(encryption): read "key_ops" according to the key management mode #797 (for KeyChecker: "key_ops" does not tell content decryption from key unwrapping, and "deriveKey"/"deriveBits" are not accepted on both sides #761) had added its own deprecation call on the line of JWEDecrypter::decryptRecipientKey() that this PR extends: both calls are kept, the one of fix(encryption): read "key_ops" according to the key management mode #797 first. fix(encryption): read "key_ops" according to the key management mode #797 also added KeyOperationsTest::aKeyWrappingKeyThatDecryptsDirectContentIsReported, which forges the token of this issue and decrypts it with a key restricted to wrapKey and unwrapKey. That token has the deprecated shape, so the test now expects both deprecations, the key_ops one first (separate commit).
  • Merge-up into 5.0.x: since fix(encryption): read "key_ops" according to the key management mode #797, merging 4.3.x into 5.0.x already conflicts in JWEDecrypter.php, on the calls after deprecateLongerDirectKey() (removed in 5.0.x) and on the deprecation methods, and in the four loader factories. This PR adds no conflict block: its call joins the first one (keep the 4.3.x calls except deprecateLongerDirectKey()), and its method, placed next to checkEncryptedKeyIsEmpty(), which 5.0.x did not change, merges cleanly. The HPKE draft feat(encryption): HPKE for JWE — Integrated Encryption and Key Encryption (draft-ietf-jose-hpke-encrypt) #740 conflicts with 4.3.x in one block of JWEDecrypter.php, with or without this PR.

Related: #761, fixed by #797 (key_ops read according to the key management mode); this PR does not touch KeyChecker.

Closes #760

Found by the Anthropic OSS Scanner (ANT-2026-35BMRM17).

…thout "aad"

AES-GCM key wrapping encrypts the CEK with AES-GCM and an empty AAD. A
JSON "dir" JWE that has neither a protected header nor an "aad" member
encrypts its content the same way. Under a key without "alg" that a
decrypter accepts for "dir" and for an A*GCMKW algorithm, the encrypted
key of an A*GCMKW token, presented as the content of such a JWE, is
decrypted to the CEK of that token. Conversely, the content of such a
JWE is a valid A*GCMKW encrypted key of its payload.

Refusing this shape would break tokens that the JSON serializations
allow, so 4.3.0 deprecates it on both sides. The decrypter triggers a
deprecation when it decrypts such a JWE, for the key that decrypted it
only, and the builder when it builds one. Both still work; they will be
refused in 5.0.0. Compact JWEs always have a protected header and are
not concerned.

Reported by Anthropic's OSS Scanner (ANT-2026-35BMRM17).
#797 added its own deprecation call after deprecateLongerDirectKey() in
JWEDecrypter::decryptRecipientKey(), on the line this branch also
extends. Both calls are kept, the one of 4.3.x first.

* origin/4.3.x:
  Place the wrapped key size check next to the CEK size check
  fix(encryption): check the size of an AES key wrapped key before unwrapping it
  feat(key-mgmt): let JKUFactory and X5UFactory refuse HTTP redirects
  fix(sd-jwt): refuse the reserved claim names in the objects of the claims
  fix(sd-jwt): bound the depth of the processed payload
  fix(encryption): read "key_ops" according to the key management mode
  fix(nested-token): deprecate a "cty" outside the protected header of the JWE
  fix(sd-jwt): keep only the verified signature in the verified Issuer-signed JWT

# Conflicts:
#	src/Library/Encryption/JWEDecrypter.php
The token that KeyOperationsTest forges from an A128GCMKW encrypted key
is a JSON "dir" JWE that has neither a protected header nor an "aad"
member, the shape this branch deprecates. Decrypting it now triggers
the "key_ops" deprecation of #797, then the empty AAD one.
@Spomky
Spomky merged commit 1d88a1a into 4.3.x Nov 8, 2026
18 checks passed
@Spomky
Spomky deleted the fix/dir-jwe-empty-aad branch November 8, 2026 19:12
Spomky added a commit that referenced this pull request Nov 29, 2026
5.0.x now holds #797 (c82460f) through the merge-up of 4.3.x, so this
branch is rebuilt as its description plans, through a merge instead of a
new history: the cherry-pick of #797 (8404b4e) gives way to the code of
5.0.x, and the refusal (c819887) is kept.

- JWEDecrypter: the deprecation of a "dir" key without "decrypt" goes
  with the refusal; the deprecation of a "dir" JWE with an empty AAD,
  from #796, stays.
- KeyOperationsTest: the refusal versions of the tests are kept, and the
  static key agreement test decrypts the token again, as 5.0.x can now
  decrypt it; the constant of the empty AAD deprecation is no longer
  used and goes.
Spomky added a commit that referenced this pull request Nov 29, 2026
5.0.x now holds #796 (1d88a1a) through the merge-up of 4.3.x, so 5.0.x
is merged into this branch as its description plans. The cherry-pick of
#796 (f4b6da5) gives way to the code of 5.0.x, and the refusal
(54617b0) is kept:

- JWEBuilder and JWEDecrypter keep checkDirectEncryptionAADIsNotEmpty(),
  and the call to deprecateDirectEncryptionWithEmptyAAD() goes; the
  deprecation of a "dir" key without "decrypt" (#797) stays, #806 turns
  it into a refusal;
- DirectEncryptionAdditionalAuthenticatedDataTest is the version of this
  branch;
- KeyOperationsTest::aKeyWrappingKeyThatDecryptsDirectContentIsReported
  (#797) forges the token of #760 and expected its CEK: the token is now
  refused, so the test, renamed aKeyWrappingKeyCannotDecryptDirectContent
  as in #806, expects a failed decryption without deprecation.
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.

Hardening: refuse a JSON "dir" JWE without protected header and "aad", whose content can be an A*GCMKW encrypted key

1 participant