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
28 changes: 22 additions & 6 deletions src/Library/Core/Util/KeyChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@ public static function checkKeyAgreementKeyUsage(JWK $key, string $usage): void
}
}

public static function allowsOperation(JWK $key, string $operation): bool
/**
* With direct encryption, the key encrypts the content: on top of the checks of checkKeyUsage(), its "key_ops"
* parameter must contain "encrypt" to build a token and "decrypt" to decrypt one.
*/
public static function checkDirectEncryptionKeyUsage(JWK $key, string $usage): void
{
if (! $key->has('key_ops')) {
return true;
self::checkKeyUsage($key, $usage);
$operation = $usage === 'encryption' ? 'encrypt' : 'decrypt';
if (! self::allowsOperation($key, $operation)) {
throw new InvalidKeyException(sprintf(
'Key cannot be used to %1$s the content: its "key_ops" parameter does not contain "%1$s".',
$operation
));
}
$ops = $key->get('key_ops');

return is_array($ops) && in_array($operation, $ops, true);
}

public static function checkKeyAlgorithm(JWK $key, string $algorithm): void
Expand All @@ -60,6 +66,16 @@ public static function checkKeyAlgorithm(JWK $key, string $algorithm): void
}
}

private static function allowsOperation(JWK $key, string $operation): bool
{
if (! $key->has('key_ops')) {
return true;
}
$ops = $key->get('key_ops');

return is_array($ops) && in_array($operation, $ops, true);
}

/**
* @param list<string> $additionalOperations Operations that allow the usage besides those listed for it
*/
Expand Down
13 changes: 3 additions & 10 deletions src/Library/Encryption/JWEBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,9 @@ private function checkKey(
JWK $recipientKey,
ContentEncryptionAlgorithm $contentEncryptionAlgorithm
): void {
if ($keyEncryptionAlgorithm instanceof KeyAgreement
if ($keyEncryptionAlgorithm instanceof DirectEncryption) {
KeyChecker::checkDirectEncryptionKeyUsage($recipientKey, 'encryption');
} elseif ($keyEncryptionAlgorithm instanceof KeyAgreement
|| $keyEncryptionAlgorithm instanceof KeyAgreementWithKeyWrapping) {
KeyChecker::checkKeyAgreementKeyUsage($recipientKey, 'encryption');
} else {
Expand All @@ -614,15 +616,6 @@ private function checkKey(
} else {
KeyChecker::checkKeyAlgorithm($recipientKey, $contentEncryptionAlgorithm->name());
}
if ($keyEncryptionAlgorithm instanceof DirectEncryption
&& ! KeyChecker::allowsOperation($recipientKey, 'encrypt')) {
trigger_deprecation(
'web-token/jwt-library',
'4.3.0',
'Using a "%s" key whose "key_ops" parameter does not contain "encrypt" is deprecated and will not be possible in 5.0.0: the key encrypts the content.',
$keyEncryptionAlgorithm->name()
);
}
}

/**
Expand Down
23 changes: 3 additions & 20 deletions src/Library/Encryption/JWEDecrypter.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ private function decryptRecipientKey(

foreach ($jwkset->all() as $recipientKey) {
try {
if ($key_encryption_algorithm instanceof KeyAgreement
if ($key_encryption_algorithm instanceof DirectEncryption) {
KeyChecker::checkDirectEncryptionKeyUsage($recipientKey, 'decryption');
} elseif ($key_encryption_algorithm instanceof KeyAgreement
|| $key_encryption_algorithm instanceof KeyAgreementWithKeyWrapping) {
KeyChecker::checkKeyAgreementKeyUsage($recipientKey, 'decryption');
} else {
Expand Down Expand Up @@ -251,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 All @@ -260,24 +261,6 @@ private function decryptRecipientKey(
return null;
}

/**
* Only the key that decrypted the token is reported: a key set may hold keys restricted to other algorithms.
*/
private function deprecateDirectKeyNotAllowedToDecrypt(
JWK $key,
KeyEncryptionAlgorithm $keyEncryptionAlgorithm
): void {
if (! $keyEncryptionAlgorithm instanceof DirectEncryption || KeyChecker::allowsOperation($key, 'decrypt')) {
return;
}
trigger_deprecation(
'web-token/jwt-library',
'4.3.0',
'Using a "%s" key whose "key_ops" parameter does not contain "decrypt" is deprecated and will not be possible in 5.0.0: the key decrypts the content.',
$keyEncryptionAlgorithm->name()
);
}

/**
* Every key management mode is checked, the direct one included: the content encryption algorithms pad a short key
* with zero bytes and ignore the end of a long one.
Expand Down
Loading
Loading