Skip to content

Commit 75215eb

Browse files
authored
Merge pull request #830 from web-token/fix/aes-gcm-wrapped-key-size
fix(encryption): check the size of an AES-GCM wrapped key before unwrapping it
2 parents 011e764 + d65a08e commit 75215eb

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

‎src/Library/Encryption/JWEDecrypter.php‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Jose\Component\Core\Util\KeyChecker;
1818
use Jose\Component\Encryption\Algorithm\ContentEncryptionAlgorithm;
1919
use Jose\Component\Encryption\Algorithm\KeyEncryption\AbstractECDHAESKW;
20+
use Jose\Component\Encryption\Algorithm\KeyEncryption\AESGCMKW;
2021
use Jose\Component\Encryption\Algorithm\KeyEncryption\AESKW;
2122
use Jose\Component\Encryption\Algorithm\KeyEncryption\DirectEncryption;
2223
use Jose\Component\Encryption\Algorithm\KeyEncryption\KeyAgreement;
@@ -321,21 +322,26 @@ private function checkCekSize(
321322
}
322323

323324
/**
324-
* AES Key Wrap adds 64 bits to the key it wraps. Any other size is rejected before the key derivation, the key
325-
* agreement and the unwrapping, which processes the whole encrypted key before checking its integrity.
325+
* AES Key Wrap adds 64 bits to the key it wraps. AES-GCM key wrapping adds nothing, its authentication tag being
326+
* the "tag" header parameter. Any other size is rejected before the key derivation, the key agreement and the
327+
* unwrapping, which processes the whole encrypted key before checking its integrity, once per key of the key set.
326328
*/
327329
private function checkWrappedKeySize(
328330
KeyEncryptionAlgorithm $keyEncryptionAlgorithm,
329331
Recipient $recipient,
330332
ContentEncryptionAlgorithm $contentEncryptionAlgorithm
331333
): void {
332-
if (! $keyEncryptionAlgorithm instanceof AESKW
333-
&& ! $keyEncryptionAlgorithm instanceof PBES2AESKW
334-
&& ! $keyEncryptionAlgorithm instanceof AbstractECDHAESKW
335-
) {
334+
$addedSize = match (true) {
335+
$keyEncryptionAlgorithm instanceof AESGCMKW => 0,
336+
$keyEncryptionAlgorithm instanceof AESKW,
337+
$keyEncryptionAlgorithm instanceof PBES2AESKW,
338+
$keyEncryptionAlgorithm instanceof AbstractECDHAESKW => 64,
339+
default => null,
340+
};
341+
if ($addedSize === null) {
336342
return;
337343
}
338-
if (strlen($recipient->getEncryptedKey() ?? '') * 8 !== $contentEncryptionAlgorithm->getCEKSize() + 64) {
344+
if (strlen($recipient->getEncryptedKey() ?? '') * 8 !== $contentEncryptionAlgorithm->getCEKSize() + $addedSize) {
339345
throw new InvalidArgumentException('Invalid encrypted key size');
340346
}
341347
}

‎tests/Component/Encryption/AESKeyWrapEncryptedKeySizeTest.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ public static function dataAlgorithms(): iterable
166166
yield 'ECDH-ES+A256KW' => ['ECDH-ES+A256KW', 'A256CBC-HS512'];
167167
yield 'ECDH-SS+A128KW' => ['ECDH-SS+A128KW', 'A192GCM'];
168168
yield 'ECDH-SS+A256KW' => ['ECDH-SS+A256KW', 'A256GCM'];
169+
yield 'A128GCMKW' => ['A128GCMKW', 'A128CBC-HS256'];
170+
yield 'A192GCMKW' => ['A192GCMKW', 'A192GCM'];
171+
yield 'A256GCMKW' => ['A256GCMKW', 'A256CBC-HS512'];
169172
}
170173

171174
public static function dataKeyAgreementAlgorithms(): iterable
@@ -245,8 +248,8 @@ private static function decryptionKeys(string $alg): array
245248
private static function sharedKey(string $alg): JWK
246249
{
247250
$size = match ($alg) {
248-
'A128KW' => 16,
249-
'A192KW' => 24,
251+
'A128KW', 'A128GCMKW' => 16,
252+
'A192KW', 'A192GCMKW' => 24,
250253
default => 32,
251254
};
252255

0 commit comments

Comments
 (0)