Skip to content

fix(encryption): read "key_ops" according to the key management mode - #797

Merged
Spomky merged 1 commit into
4.3.xfrom
fix/key-ops-per-key-management-mode
Nov 8, 2026
Merged

Spomky merged 1 commit into
4.3.xfrom
fix/key-ops-per-key-management-mode

Conversation

@Spomky

@Spomky Spomky commented Nov 8, 2026

Copy link
Copy Markdown
Member

The defect

KeyChecker::checkKeyUsage() read key_ops the same way for every key management mode: any of encrypt, wrapKey and deriveKey allowed a key to build a JWE, and any of decrypt, unwrapKey and deriveBits allowed it to decrypt one. Two consequences:

  • With 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 no alg decrypts the JSON dir JWE 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_ops did not prevent that confusion.
  • Key agreement was lopsided: a key restricted to deriveBits could decrypt with ECDH-ES but not encrypt, and a key restricted to deriveKey could encrypt but not decrypt. WebCrypto exports an ECDH key with either value.

The fix

  • dir: the key must allow encrypt to build a token and decrypt to decrypt one. As no BC break is allowed in 4.3.0, a key that only allows the other operations accepted so far (wrapKey, deriveKey to build; unwrapKey, deriveBits to decrypt) still works, and triggers a deprecation. It will be refused in 5.0.0; the trigger_deprecation grep 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 a dir key, 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.
  • Key agreement (ECDH-ES, ECDH-SS and their +A*KW variants): 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 (RSA, AES-KW, AES-GCM-KW, PBES2) are unchanged. The strict mapping per mode, which would refuse keys exported by WebCrypto (AES-GCM keys with encrypt/decrypt used for A*GCMKW, for instance), stays out of scope, as the issue says.

KeyChecker is @internal and final readonly. It gets two static methods, checkKeyAgreementKeyUsage() and allowsOperation(). The changes in JWEBuilder and JWEDecrypter are private.

Behaviour changes

Key Call Before After
dir, key_ops without encrypt but with wrapKey or deriveKey build built built, with a deprecation
dir, key_ops without decrypt but with unwrapKey or deriveBits decrypt decrypted decrypted, with a deprecation
key agreement, key_ops with deriveBits but none of encrypt, wrapKey, deriveKey build (recipient or sender key) InvalidKeyException "Key cannot be used to encrypt" built
key agreement, key_ops with deriveKey but none of decrypt, unwrapKey, deriveBits decrypt key skipped, "Key cannot be used to decrypt" decrypted

Everything else is unchanged: keys without key_ops, the use parameter, the other modes, and every key refused today. Nothing that was accepted is refused. One message changes: a key agreement key that has deriveKey or deriveBits and is refused for its alg now 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:

key_ops of the keys used with dir. With direct encryption, the key encrypts the content. When its key_ops parameter is present, it should contain encrypt to build a token and decrypt to decrypt one. A key that only allows the other encryption operations (wrapKey, unwrapKey, deriveKey or deriveBits) still works, with a deprecation notice, and will be refused in 5.0. Add encrypt and decrypt to such keys, or remove their key_ops parameter. Keys used with ECDH-ES, ECDH-SS and their key wrapping variants now accept both deriveKey and deriveBits, as WebCrypto exports them, to encrypt and to decrypt.

Verification

Fixes #761

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

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
@Spomky Spomky added this to the 4.3.0 milestone Nov 8, 2026
@Spomky Spomky added enhancement Compliance Compliance with applicable specifications labels Nov 8, 2026
@Spomky
Spomky merged commit c82460f into 4.3.x Nov 8, 2026
18 checks passed
@Spomky
Spomky deleted the fix/key-ops-per-key-management-mode branch November 8, 2026 18:39
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.
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

Labels

Compliance Compliance with applicable specifications enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KeyChecker: "key_ops" does not tell content decryption from key unwrapping, and "deriveKey"/"deriveBits" are not accepted on both sides

1 participant