Skip to content

fix(encryption): check the size of an AES-GCM wrapped key before unwrapping it - #830

Merged
Spomky merged 1 commit into
4.3.xfrom
fix/aes-gcm-wrapped-key-size
Nov 29, 2026
Merged

Spomky merged 1 commit into
4.3.xfrom
fix/aes-gcm-wrapped-key-size

Conversation

@Spomky

@Spomky Spomky commented Nov 29, 2026

Copy link
Copy Markdown
Member

Follow-up of #771 and #799, reported by inanna-malick.

The defect

#799 checks the size of the JWE Encrypted Key before the algorithms built on AES Key Wrap run, but A128GCMKW, A192GCMKW and A256GCMKW are not among them. These three hand the encrypted key to openssl_decrypt(), which decrypts all of it before it checks the tag, and does so again for every key of the key set. A token with an oversized encrypted key is therefore rejected only after work and memory proportional to that size.

The cost is low, because OpenSSL does the work and does it once: #799 already counted this path among the bounded ones. Measured on 024031b3 with PHP 8.6, keys of 16 bytes:

Encrypted key Token Parsing the token Decryption, 1 key Decryption, 4 keys
4 MiB 5.6 MB 147 ms 6 ms 21 ms
16 MiB 22 MB 402 ms 21 ms 80 ms

This PR is about consistency: every key wrapping algorithm of the library now rejects a wrong size before doing any work.

The fix

JWEDecrypter::checkWrappedKeySize() covers AESGCMKW, so its three subclasses too. AES-GCM key wrapping adds nothing to the key it wraps, because its tag is the tag header parameter, so the encrypted key must have exactly the CEK size. The AES Key Wrap algorithms still expect the CEK size plus 64 bits.

Behaviour changes

No token changes outcome. A token with an encrypted key of another size was already rejected, either by the tag or, when the tag matched, by checkCekSize(). For these tokens, the failure passed to the $onError callable of decrypt(), and chained by the loaders as the previous exception, becomes InvalidArgumentException: Invalid encrypted key size. It used to be one of these:

  • DecryptionFailedException: Unable to decrypt the CEK;
  • InvalidArgumentException: Invalid CEK size;
  • InvalidKeyException: Wrong key type., for a key of another type in the key set.

#799 made the same change for AES-KW.

Verification

  • AESKeyWrapEncryptedKeySizeTest gets A128GCMKW, A192GCMKW and A256GCMKW rows: sizes off by one byte or one block, an empty key, the key wrapped for another enc, and 1 MiB rejected without being allocated. The 6 new cases fail on 4.3.x.
  • Full suite in phpqa:8.4 after a clean composer install: 3046 tests on 4.3.x, 3052 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 because the encryption tests are slow: every mutant is killed.
  • Behaviour probe: the 4.3.x JWEDecrypter against this one, on 720 tokens (3 algorithms, 4 enc values, 12 token variants, 5 key sets), through decrypt() and JWELoader::loadAndDecrypt(). The outcome never changes (decrypted or not, payload, key used, exception thrown). The failures change as described above: 528, 18 and 78 cases respectively.

…apping it

A128GCMKW, A192GCMKW and A256GCMKW hand the whole encrypted key to
openssl_decrypt(), which decrypts it before checking the authentication
tag, for every key of the key set. The size check added for the AES Key
Wrap algorithms skipped them, so a token with an encrypted key of any
size was rejected only after work and memory proportional to that size.

AES-GCM key wrapping returns as many bytes as it wraps, its tag being the
"tag" header parameter: JWEDecrypter now compares the size of the
encrypted key with the CEK size before calling these algorithms. The
tokens it rejects are the ones it already rejected, either because the
tag did not match or because the unwrapped key had the wrong size: only
the failure passed to the callable of decrypt() and chained by the
loaders changes.

Reported by inanna-malick, as a follow-up of ANT-2026-GFP863VS.
@Spomky
Spomky force-pushed the fix/aes-gcm-wrapped-key-size branch from 04deba3 to d65a08e Compare November 29, 2026 09:46
@Spomky
Spomky merged commit 75215eb into 4.3.x Nov 29, 2026
18 checks passed
@Spomky
Spomky deleted the fix/aes-gcm-wrapped-key-size 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