Repository navigation
fix(encryption): read "key_ops" according to the key management mode - #797
Merged
Merged
Conversation
KeyChecker accepted any of "encrypt", "wrapKey" and "deriveKey" to build a JWE, and any of "decrypt", "unwrapKey" and "deriveBits" to decrypt one, whatever the key management mode. A key restricted to key wrapping, such as an A128GCMKW key with ["wrapKey", "unwrapKey"], could therefore decrypt "dir" content: "key_ops" did not prevent the confusion of #760, which returns the CEK of a key wrapping token. Key agreement accepted "deriveKey" to build only and "deriveBits" to decrypt only. With "dir", the key encrypts the content. A key whose "key_ops" lacks "encrypt" (to build) or "decrypt" (to decrypt) still works through the operations accepted so far, but triggers a deprecation and will be refused in 5.0.0. The decrypter only reports the key that decrypted the token. With a key agreement algorithm, with or without key wrapping, "deriveKey" and "deriveBits" are both accepted to build and to decrypt, for the recipient key and, when building, for the sender key. The other key management modes are unchanged. Reported by Anthropic's OSS Scanner (ANT-2026-Z8KOO4J9). Fixes #761
This was referenced Nov 8, 2026
Spomky
added a commit
that referenced
this pull request
Nov 8, 2026
#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
Spomky
added a commit
that referenced
this pull request
Nov 8, 2026
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.
This was referenced Nov 8, 2026
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.
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.
The defect
KeyChecker::checkKeyUsage()readkey_opsthe same way for every key management mode: any ofencrypt,wrapKeyandderiveKeyallowed a key to build a JWE, and any ofdecrypt,unwrapKeyandderiveBitsallowed it to decrypt one. Two consequences:dir, the key is the CEK and encrypts the content, yet a key restricted to key wrapping is accepted. An A128GCMKW key with["wrapKey", "unwrapKey"]and noalgdecrypts the JSONdirJWE described in Hardening: refuse a JSON "dir" JWE without protected header and "aad", whose content can be an A*GCMKW encrypted key #760 and returns the CEK of the A128GCMKW token, which then opens that token.key_opsdid not prevent that confusion.deriveBitscould decrypt with ECDH-ES but not encrypt, and a key restricted toderiveKeycould encrypt but not decrypt. WebCrypto exports an ECDH key with either value.The fix
dir: the key must allowencryptto build a token anddecryptto decrypt one. As no BC break is allowed in 4.3.0, a key that only allows the other operations accepted so far (wrapKey,deriveKeyto build;unwrapKey,deriveBitsto decrypt) still works, and triggers a deprecation. It will be refused in 5.0.0; thetrigger_deprecationgrep of 5.0.0: remove what 4.3 deprecated, seal what it announced #717 covers that. The decrypter only reports the key that decrypted the token, as for the size of adirkey, because a key set may hold keys meant for other algorithms. This differs from the issue, which proposed refusing those keys in 4.3.0: in 4.3, the confusion of Hardening: refuse a JSON "dir" JWE without protected header and "aad", whose content can be an A*GCMKW encrypted key #760 is reported, not prevented.+A*KWvariants):deriveKeyandderiveBitsare both accepted to build and to decrypt, for the recipient key and, when building, for the sender key.encrypt/decryptused for A*GCMKW, for instance), stays out of scope, as the issue says.KeyCheckeris@internalandfinal readonly. It gets two static methods,checkKeyAgreementKeyUsage()andallowsOperation(). The changes inJWEBuilderandJWEDecrypterare private.Behaviour changes
dir,key_opswithoutencryptbut withwrapKeyorderiveKeydir,key_opswithoutdecryptbut withunwrapKeyorderiveBitskey_opswithderiveBitsbut none ofencrypt,wrapKey,deriveKeyInvalidKeyException"Key cannot be used to encrypt"key_opswithderiveKeybut none ofdecrypt,unwrapKey,deriveBitsEverything else is unchanged: keys without
key_ops, theuseparameter, the other modes, and every key refused today. Nothing that was accepted is refused. One message changes: a key agreement key that hasderiveKeyorderiveBitsand is refused for itsalgnow reports "Key is only allowed for algorithm …" instead of "Key cannot be used to encrypt/decrypt". The exception class (InvalidKeyException) is the same.Upgrade note
Proposed text for the "From v4.2 to v4.3" guide:
Verification
KeyOperationsTest, 30 cases. 19 fail on the base branch:dirkey operation sets withoutencryptand the five withoutdecrypt: built or decrypted without any deprecation;["wrapKey", "unwrapKey"]decrypts the forgeddirJWE (16-byte payload) without any deprecation;deriveBitsto build andderiveKeyto decrypt, with ECDH-ES, ECDH-ES+A128KW, ECDH-SS and ECDH-SS+A128KW, including the sender key of the static agreements: refused on the base branch.The 11 other cases pass on both branches and pin what must not change:
dirkeys that allow the content operations (or have nokey_ops) are not reported, adirkey that is tried but does not decrypt the token is not reported, keys without any encryption operation stay refused, AES-KW and AES-GCM-KW keys gain neither derivation operation, and an AES-GCM-KW key withencrypt/decryptstays accepted.BC, against the base branch:
src/(445 types), dumped by reflection, is identical apart from the two new methods ofKeyChecker;key_opsvalues, built and decrypted (plus the sender key for ECDH-SS and ECDH-SS+A128KW), and the Hardening: refuse a JSON "dir" JWE without protected header and "aad", whose content can be an A*GCMKW encrypted key #760 token: 217 outcomes. 198 are identical; the 19 others are exactly the four rows above, plus the deprecation on the Hardening: refuse a JSON "dir" JWE without protected header and "aad", whose content can be an A*GCMKW encrypted key #760 token. Before and after, the CEK recovered from that token opens the A128GCMKW token;key_opsas a string,null, a map, nested arrays, booleans, integers, other casing, duplicates, padded values;useandalgcombined withkey_ops) × 5 algorithms, 275 outcomes. Nothing goes from accepted to refused, and no exception class changes. The differences are the rows above and the message change;trigger_deprecation(): of 3,539 notices, the 11 of the new deprecation all come fromKeyOperationsTest.The whole suite on PHP 8.4 (
phpqa:8.4,phpunit-11): 2,538 tests green, with the 4 skipped and 2 incomplete tests of the base branch. ECS, Rector, PHPStan (baseline unchanged) and Deptrac are clean with the current image.Mutation testing on the diff: 48 mutants. With the 3-second timeout of
infection.json.dist, as in CI, 13 are killed and 35 are skipped, because the covering test files take longer. With a 60-second timeout, all 48 are killed (MSI 100%).Open pull requests: no conflict with fix(sd-jwt): keep only the verified signature in the verified Issuer-signed JWT #793, fix(nested-token): deprecate a "cty" outside the protected header of the JWE #794 or fix(sd-jwt): bound the depth of the processed payload #795. Draft feat(encryption): HPKE for JWE — Integrated Encryption and Key Encryption (draft-ietf-jose-hpke-encrypt) #740 already conflicts with 4.3.x in
JWEDecrypter.php, and this branch adds no other conflict.Merge-up into 5.0.x: one conflict in
JWEDecrypter.php, where the deprecation of longerdirkeys has already become a refusal. The resolution there is to refuse the key as well.Fixes #761
Found by the Anthropic OSS Scanner (ANT-2026-Z8KOO4J9).