Repository navigation
fix(encryption): check the size of an AES-GCM wrapped key before unwrapping it - #830
Merged
Merged
Conversation
…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
force-pushed
the
fix/aes-gcm-wrapped-key-size
branch
from
November 29, 2026 09:46
04deba3 to
d65a08e
Compare
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.
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
024031b3with PHP 8.6, keys of 16 bytes: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()coversAESGCMKW, so its three subclasses too. AES-GCM key wrapping adds nothing to the key it wraps, because its tag is thetagheader 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$onErrorcallable ofdecrypt(), and chained by the loaders as the previous exception, becomesInvalidArgumentException: 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
AESKeyWrapEncryptedKeySizeTestgetsA128GCMKW,A192GCMKWandA256GCMKWrows: sizes off by one byte or one block, an empty key, the key wrapped for anotherenc, and 1 MiB rejected without being allocated. The 6 new cases fail on 4.3.x.phpqa:8.4after a cleancomposer install: 3046 tests on 4.3.x, 3052 here, all green.php -linphpqa:8.2.JWEDecrypteragainst this one, on 720 tokens (3 algorithms, 4encvalues, 12 token variants, 5 key sets), throughdecrypt()andJWELoader::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.