Skip to content

fix(encryption): refuse "dir" keys without the content operation in "key_ops" - #806

Draft
Spomky wants to merge 3 commits into
5.0.xfrom
fix/key-ops-per-key-management-mode-5.0.x
Draft

Spomky wants to merge 3 commits into
5.0.xfrom
fix/key-ops-per-key-management-mode-5.0.x

Conversation

@Spomky

@Spomky Spomky commented Nov 8, 2026

Copy link
Copy Markdown
Member

Second part of #761, for 5.0.0. It follows #797 (4.3.x, merged as c82460f), which deprecated these keys in 4.3.0. Draft until 4.3.x is merged up into 5.0.x: this branch is then rebuilt on top of the merge-up.

Why the branch cherry-picks #797 instead of merging it

The other 5.0.x drafts (#783, #786, #790) merge the commit of their 4.3.x pull request, which starts from the last merge-up of 4.3.x into 5.0.x (7dfbe15). #797 starts from 6d4d2ce, 37 commits later. Merging its commit would bring those commits along (63 files) with five conflicts, which amounts to half a merge-up. The first commit is therefore a cherry-pick of #797 (4f9f233), adapted to 5.0.x:

The second commit turns the deprecations into refusals. Once the merge-up is done, the rebuild drops the cherry-pick, keeps the second commit, and brings back the decryption half of the static key agreement test.

The change

With #797 (4.3.0), a dir key whose key_ops parameter lacks encrypt (to build) or decrypt (to decrypt) triggers a deprecation if it contains another operation accepted so far: wrapKey or deriveKey to build, unwrapKey or deriveBits to decrypt. In 5.0.0, such a key is refused with an InvalidKeyException:

Key cannot be used to encrypt the content: its "key_ops" parameter does not contain "encrypt".

Key cannot be used to decrypt the content: its "key_ops" parameter does not contain "decrypt".

The net diff against 5.0.x adds no trigger_deprecation() call, so the "Done when" grep of #717 is not affected.

Behaviour changes, against 5.0.x

Key Call 5.0.x This branch
dir, key_ops without encrypt but with wrapKey or deriveKey build built InvalidKeyException
dir, key_ops without decrypt but with unwrapKey or deriveBits decrypt decrypted key skipped, InvalidKeyException passed to onError
key agreement, key_ops with deriveBits but none of encrypt, wrapKey, deriveKey build (recipient or sender key) InvalidKeyException built
key agreement, key_ops with deriveKey but none of decrypt, unwrapKey, deriveBits decrypt key skipped decrypted with ECDH-ES and ECDH-ES+A*KW

The first two rows are the BC break. The last two come from #797.

Three messages change:

  • A dir key with a wrong alg and without the content operation now reports the key_ops error instead of "Key is only allowed for algorithm …". The exception class is the same.
  • As in fix(encryption): read "key_ops" according to the key management mode #797, a key agreement key that has deriveKey or deriveBits and is refused for its alg now reports "Key is only allowed for algorithm …".
  • With ECDH-SS on 5.0.x, a key with deriveKey now passes the key check and then reaches the epk failure described above. That goes away with the merge-up.

Verification

  • KeyOperationsTest has 30 cases, all green.
    • On 5.0.x, 18 fail:
    • On the cherry-pick alone, the 12 refusal cases fail and nothing else does, so the second commit is exactly what they test.
    • The other cases pin what does not change. The message of the keys without any encryption operation is now matched exactly, since the new message starts with it.
  • BC against 5.0.x (b81a608):
  • The whole suite passes on PHP 8.4 and 8.5 (phpqa, phpunit-11): 2,283 tests, with the 4 skipped and 2 incomplete tests of the base. ECS, Rector, PHPStan and Deptrac are clean with the current phpqa:8.4 image, and the PHPStan baseline is unchanged.
  • Mutation testing on the diff generates 41 mutants. With the 3-second timeout of infection.json.dist, as in CI, 17 are killed and 24 are skipped. With 60 seconds, all 41 are killed. A first run let a PublicVisibility mutant of allowsOperation() escape, which is why the method is now private.

For the migration 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 must 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) triggered a deprecation in 4.3. In 5.0, the builder refuses it and the decrypter skips it. Add encrypt and decrypt to such keys, or remove their key_ops parameter.

Refs #761

Cherry-pick of #797 (4.3.x), so that the next commit turns its
deprecations into refusals. In 5.0.x, checkCekSize() already refuses
a longer "dir" key, and deprecateLongerDirectKey() no longer exists:
the deprecation of a "dir" key without "decrypt" is reported on its
own after the try block.

5.0.x cannot decrypt a static key agreement yet: the fix of #770
(#775, 4.2.7) comes with the merge-up of 4.3.x. The static key
agreement test therefore only builds the token, with the key of the
recipient and the key of the sender.

Refs #761

(cherry picked from commit 4f9f233)
…key_ops"

Since 4.3.0, a "dir" key whose "key_ops" parameter lacks "encrypt" (to
build) or "decrypt" (to decrypt), but contains another operation that
was accepted so far, triggers a deprecation. Such a key is now refused:
KeyChecker::checkDirectEncryptionKeyUsage() runs the check common to
every mode, then throws an InvalidKeyException when the content
operation is missing. The decrypter checks the key before using it, so
it skips a refused key and tries the next one of the key set, as for
the other key checks. A key restricted to key wrapping, such as an
A128GCMKW key with ["wrapKey", "unwrapKey"], can no longer decrypt the
"dir" content of #760 and return the CEK of the A128GCMKW token.

Keys without "key_ops", and those refused by the common check, behave
as before, with the same messages. KeyChecker::allowsOperation() is
only used by KeyChecker now and becomes private.

Refs #761
@Spomky Spomky added this to the 5.0.0 milestone Nov 8, 2026
@Spomky Spomky added enhancement BC Break Compliance Compliance with applicable specifications labels Nov 8, 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

BC Break Compliance Compliance with applicable specifications enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant