Repository navigation
fix(encryption): check the size of an AES key wrapped key before unwrapping it - #799
Merged
Merged
Conversation
…apping it A128KW, A192KW, A256KW, PBES2-HS*+A*KW, ECDH-ES+A*KW and ECDH-SS+A*KW hand the whole encrypted key to spomky-labs/aes-key-wrap, which unwraps every 64-bit block before checking the integrity value. JWEDecrypter compared the size of the unwrapped key with the CEK size only afterwards, so a token was rejected after work and memory proportional to the size of its encrypted key, for every key tried: several seconds and 17 MB per megabyte, and a fatal error instead of an exception for a 9 MB encrypted key under a 128 MB memory limit. JWEDecrypter now compares the size of the encrypted key with the CEK size plus 64 bits before calling these algorithms, that is before the key derivation, the key agreement and the unwrapping. The tokens it rejects are the ones it already rejected: only the failure passed to the callable of decrypt() and chained by the loaders changes, and an empty encrypted key no longer triggers a PHP warning in the key wrapping library. The algorithms themselves are unchanged when called directly: unwrapAgreementKey() has always ignored the size it is given, and callers pass sizes that do not match the key they unwrap. Reported by Anthropic's OSS Scanner (ANT-2026-GFP863VS).
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
A128KW, A192KW, A256KW, PBES2-HS*+AKW, ECDH-ES+AKW and ECDH-SS+A*KW pass the whole JWE Encrypted Key to
spomky-labs/aes-key-wrap. Itsunwrap()splits the key into 64-bit blocks and runs six AES decryptions per block, from PHP, before it checks the integrity value. AES Key Wrap adds exactly 64 bits to the key it wraps, so the only size that can succeed is the CEK size plus 8 bytes.JWEDecrypterknows the CEK size, but compared it with the result only after the unwrapping, incheckCekSize().A token with an oversized encrypted key was therefore rejected only after work and memory proportional to that size, and again for every key tried. The PBES2 key derivation and the ECDH key agreement also ran before the size was looked at. Measured with
phpqa:8.4for a 1 MiB encrypted key and one key (PHP 8.5.12 gives the same order of magnitude):Under
memory_limit=128M, a 9 MiB encrypted key ends in a fatal error inAESKW.phpon the base branch. This branch rejects it in 2 ms.The fix
JWEDecrypter::decryptCEK()compares the size of the encrypted key with the CEK size plus 64 bits before it calls an algorithm built on AES Key Wrap (AESKW,PBES2AESKWandAbstractECDHAESKW, so their subclasses too). The check therefore comes before the key derivation, the key agreement and the unwrapping. A mismatch throws the sameInvalidArgumentExceptionclass as the unwrapping andcheckCekSize(), inside the loop over the keys, sodecrypt()still returns a failure.The issue suggested checking in the four algorithms. The check sits in the decrypter instead, for two reasons:
unwrapAgreementKey()of ECDH-ES+AKW and ECDH-SS+AKW has always ignored its$encryption_key_lengthargument, and callers pass sizes that do not match the key they unwrap: the library's ownECDHESKeyAgreementTestandECDHSSKeyAgreementTestpass 128 or 192 with a 256-bit CEK. A check there would turn those working calls into exceptions.unwrapKey()of A*KW and PBES2 only gets the CEK size as the undeclared fourth argument passed by the decrypter.The decrypter is the one place that knows the size for all four and already rejected every mismatch. Calling the algorithms directly behaves as before. Once #653 (in #717) declares the size on
KeyWrapping::unwrapKey()in 5.0, the check could move into the algorithms.There is no general bound on the encrypted key in
JWEDecrypter. With this check, the other key management modes are already bounded:dir, ECDH-ES and ECDH-SS require an empty encrypted key, RSA-OAEP and RSA1_5 a single block, and A128GCMKW to A256GCMKW decrypt it in one OpenSSL call (1 MiB: 11 ms, and 1 MB for the decrypted key itself).Behaviour changes
No token changes outcome: every token rejected now was rejected before, and every token decrypted before is still decrypted, with the same key. For the tokens that are rejected:
$onErrorcallable ofdecrypt()(or the sixth argument ofdecryptUsingKeySet()) is nowInvalidArgumentException: Invalid encrypted key size. So is the failure the loaders chain as the previous exception of "Unable to load and decrypt the token.". It used to beIntegrity check failed!,Bad data,Unable to decrypt the dataorInvalid CEK size, with the same class.InvalidKeyException. The same goes for an invalid PBES2 or ECDH header.aes-key-wrap.Verification
AESKeyWrapEncryptedKeySizeTesthas 21 cases, all of them failing on the base branch, which also emits the PHP warning:checkCekSize();CountingPBES2HS256A128KW);JWEDecrypter, dumped by reflection, is identical. The new method is private, and no other file ofsrc/changes;decrypt(),decryptUsingKeySet()andJWELoader. It covers the 12 algorithms built on AES Key Wrap plus a third-party subclass ofAESKW, and 9 others (dir, ECDH-ES, ECDH-SS, RSA-OAEP, RSA-OAEP-256, RSA1_5, A128GCMKW, A256GCMKW, and a third-party AES-KW algorithm that does not extendAESKW). Each comes with the six content encryption algorithms, the three serializations, the valid token and eight variants of its encrypted key, plus key sets with unusable keys and a token with two recipients. Outcomes, payloads and keys are identical. The only differences are the ones listed above. Nothing changes for the other algorithms, or for direct calls ofunwrapKey()andunwrapAgreementKey().phpqa:8.4image.infection.json.dist, the CI kills 3 and skips the other 18, because the slow encryption tests also cover these lines.5.0.xwithout conflict. Its tests pass there once their ECDH-SS cases use the key order that 5.0.x accepts today. The order they use (private key of the recipient, public key of the sender, 830a555) reaches 5.0.x with the next merge-up of 4.3.x.checkCekSize()for that reason, as fix(encryption): deprecate "dir" JWEs without protected header and without "aad" #796 adds its own method where this one was. The HPKE draft feat(encryption): HPKE for JWE — Integrated Encryption and Key Encryption (draft-ietf-jose-hpke-encrypt) #740 conflicts, as it already does with the current 4.3.x.Fixes #771
Found by the Anthropic OSS Scanner (ANT-2026-GFP863VS).