|
17 | 17 | use Jose\Component\Core\Util\KeyChecker; |
18 | 18 | use Jose\Component\Encryption\Algorithm\ContentEncryptionAlgorithm; |
19 | 19 | use Jose\Component\Encryption\Algorithm\KeyEncryption\AbstractECDHAESKW; |
| 20 | +use Jose\Component\Encryption\Algorithm\KeyEncryption\AESGCMKW; |
20 | 21 | use Jose\Component\Encryption\Algorithm\KeyEncryption\AESKW; |
21 | 22 | use Jose\Component\Encryption\Algorithm\KeyEncryption\DirectEncryption; |
22 | 23 | use Jose\Component\Encryption\Algorithm\KeyEncryption\KeyAgreement; |
@@ -321,21 +322,26 @@ private function checkCekSize( |
321 | 322 | } |
322 | 323 |
|
323 | 324 | /** |
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. |
326 | 328 | */ |
327 | 329 | private function checkWrappedKeySize( |
328 | 330 | KeyEncryptionAlgorithm $keyEncryptionAlgorithm, |
329 | 331 | Recipient $recipient, |
330 | 332 | ContentEncryptionAlgorithm $contentEncryptionAlgorithm |
331 | 333 | ): 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) { |
336 | 342 | return; |
337 | 343 | } |
338 | | - if (strlen($recipient->getEncryptedKey() ?? '') * 8 !== $contentEncryptionAlgorithm->getCEKSize() + 64) { |
| 344 | + if (strlen($recipient->getEncryptedKey() ?? '') * 8 !== $contentEncryptionAlgorithm->getCEKSize() + $addedSize) { |
339 | 345 | throw new InvalidArgumentException('Invalid encrypted key size'); |
340 | 346 | } |
341 | 347 | } |
|
0 commit comments