Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/Library/Encryption/JWEBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,13 @@ public function build(): JWE
$encodedSharedProtectedHeader = count($sharedProtectedHeader) === 0 ? '' : Base64UrlSafe::encodeUnpadded(
JsonConverter::encode($sharedProtectedHeader)
);
$this->checkDirectEncryptionAADIsNotEmpty($keyManagementMode, $encodedSharedProtectedHeader);

[$ciphertext, $iv, $tag] = $this->encryptJWE(
$contentEncryptionAlgorithm,
$cek,
$encodedSharedProtectedHeader
);
$this->deprecateDirectEncryptionWithEmptyAAD($keyManagementMode, $encodedSharedProtectedHeader);

return new JWE(
$ciphertext,
Expand All @@ -267,9 +267,10 @@ public function build(): JWE

/**
* Without a protected header and without "aad", the content of a "dir" JWE is encrypted with an empty AAD, as AES-GCM
* key wrapping encrypts a CEK: that content is then a valid A*GCMKW encrypted key of the payload under the same key.
* key wrapping encrypts a CEK: that content would then be a valid A*GCMKW encrypted key of the payload under the same
* key. The JWE is refused before its content is encrypted.
*/
private function deprecateDirectEncryptionWithEmptyAAD(
private function checkDirectEncryptionAADIsNotEmpty(
string $keyManagementMode,
string $encodedSharedProtectedHeader
): void {
Expand All @@ -278,10 +279,8 @@ private function deprecateDirectEncryptionWithEmptyAAD(
|| $this->aad !== null) {
return;
}
trigger_deprecation(
'web-token/jwt-library',
'4.3.0',
'Building a "dir" JWE that has neither a protected header nor additional authenticated data is deprecated and will not be possible in 5.0.0: set "alg" and "enc" in the shared protected header.'
throw new InvalidArgumentException(
'A "dir" JWE must have a protected header or additional authenticated data: set "alg" and "enc" in the shared protected header.'
);
}

Expand Down
13 changes: 6 additions & 7 deletions src/Library/Encryption/JWEDecrypter.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ private function decryptRecipientKey(
KeyChecker::checkKeyAlgorithm($recipientKey, $content_encryption_algorithm->name());
}
$this->checkIterationCount($key_encryption_algorithm, $completeHeader, $jwe->countRecipients());
$this->checkDirectEncryptionAADIsNotEmpty($jwe, $key_encryption_algorithm);
$cek = $this->decryptCEK(
$key_encryption_algorithm,
$content_encryption_algorithm,
Expand All @@ -252,7 +253,6 @@ private function decryptRecipientKey(
continue;
}
$this->deprecateDirectKeyNotAllowedToDecrypt($recipientKey, $key_encryption_algorithm);
$this->deprecateDirectEncryptionWithEmptyAAD($jwe, $key_encryption_algorithm);

return DecryptionResult::success($jwe->withPayload($payload), $i, $recipientKey);
}
Expand Down Expand Up @@ -422,19 +422,18 @@ private function checkEncryptedKeyIsEmpty(Recipient $recipient): void

/**
* Without a protected header and without "aad", the content of a "dir" JWE is encrypted with an empty AAD, as AES-GCM
* key wrapping encrypts a CEK: the encrypted key of an A*GCMKW token is then a valid content under the same key.
* key wrapping encrypts a CEK: the encrypted key of an A*GCMKW token would then be a valid content under the same
* key. The token is refused whatever the key, and every key reports it.
*/
private function deprecateDirectEncryptionWithEmptyAAD(JWE $jwe, KeyEncryptionAlgorithm $keyEncryptionAlgorithm): void
private function checkDirectEncryptionAADIsNotEmpty(JWE $jwe, KeyEncryptionAlgorithm $keyEncryptionAlgorithm): void
{
if (! $keyEncryptionAlgorithm instanceof DirectEncryption
|| $jwe->getEncodedSharedProtectedHeader() !== ''
|| $jwe->getAAD() !== null) {
return;
}
trigger_deprecation(
'web-token/jwt-library',
'4.3.0',
'Decrypting a "dir" JWE that has neither a protected header nor additional authenticated data is deprecated and will not be possible in 5.0.0: encrypt it again with "alg" and "enc" in the protected header.'
throw new InvalidArgumentException(
'A "dir" JWE must have a protected header or additional authenticated data.'
);
}

Expand Down
Loading
Loading