Skip to content

Commit c82460f

Browse files
authored
Merge pull request #797 from web-token/fix/key-ops-per-key-management-mode
fix(encryption): read "key_ops" according to the key management mode
2 parents 8ce8773 + 4f9f233 commit c82460f

4 files changed

Lines changed: 523 additions & 3 deletions

File tree

‎src/Library/Core/Util/KeyChecker.php‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@ public static function checkKeyUsage(JWK $key, string $usage): void
2525
}
2626
}
2727

28+
/**
29+
* A key agreement algorithm derives the key that encrypts the content or wraps the CEK: "deriveKey" and
30+
* "deriveBits" let the key encrypt and decrypt, as an ECDH key may carry either of them.
31+
*/
32+
public static function checkKeyAgreementKeyUsage(JWK $key, string $usage): void
33+
{
34+
if ($key->has('use')) {
35+
self::checkUsage($key, $usage);
36+
}
37+
if ($key->has('key_ops')) {
38+
self::checkOperation($key, $usage, ['deriveKey', 'deriveBits']);
39+
}
40+
}
41+
42+
public static function allowsOperation(JWK $key, string $operation): bool
43+
{
44+
if (! $key->has('key_ops')) {
45+
return true;
46+
}
47+
$ops = $key->get('key_ops');
48+
49+
return is_array($ops) && in_array($operation, $ops, true);
50+
}
51+
2852
public static function checkKeyAlgorithm(JWK $key, string $algorithm): void
2953
{
3054
$alg = $key->alg();
@@ -36,12 +60,20 @@ public static function checkKeyAlgorithm(JWK $key, string $algorithm): void
3660
}
3761
}
3862

39-
private static function checkOperation(JWK $key, string $usage): void
63+
/**
64+
* @param list<string> $additionalOperations Operations that allow the usage besides those listed for it
65+
*/
66+
private static function checkOperation(JWK $key, string $usage, array $additionalOperations = []): void
4067
{
4168
$ops = $key->get('key_ops');
4269
if (! is_array($ops)) {
4370
throw new InvalidKeyException('Invalid key parameter "key_ops". Should be a list of key operations');
4471
}
72+
foreach ($additionalOperations as $operation) {
73+
if (in_array($operation, $ops, true)) {
74+
return;
75+
}
76+
}
4577

4678
switch ($usage) {
4779
case 'verification':

‎src/Library/Encryption/JWEBuilder.php‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,12 +582,26 @@ private function checkKey(
582582
JWK $recipientKey,
583583
ContentEncryptionAlgorithm $contentEncryptionAlgorithm
584584
): void {
585-
KeyChecker::checkKeyUsage($recipientKey, 'encryption');
585+
if ($keyEncryptionAlgorithm instanceof KeyAgreement
586+
|| $keyEncryptionAlgorithm instanceof KeyAgreementWithKeyWrapping) {
587+
KeyChecker::checkKeyAgreementKeyUsage($recipientKey, 'encryption');
588+
} else {
589+
KeyChecker::checkKeyUsage($recipientKey, 'encryption');
590+
}
586591
if ($keyEncryptionAlgorithm->name() !== 'dir') {
587592
KeyChecker::checkKeyAlgorithm($recipientKey, $keyEncryptionAlgorithm->name());
588593
} else {
589594
KeyChecker::checkKeyAlgorithm($recipientKey, $contentEncryptionAlgorithm->name());
590595
}
596+
if ($keyEncryptionAlgorithm instanceof DirectEncryption
597+
&& ! KeyChecker::allowsOperation($recipientKey, 'encrypt')) {
598+
trigger_deprecation(
599+
'web-token/jwt-library',
600+
'4.3.0',
601+
'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.',
602+
$keyEncryptionAlgorithm->name()
603+
);
604+
}
591605
}
592606

593607
/**

‎src/Library/Encryption/JWEDecrypter.php‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,12 @@ private function decryptRecipientKey(
222222

223223
foreach ($jwkset->all() as $recipientKey) {
224224
try {
225-
KeyChecker::checkKeyUsage($recipientKey, 'decryption');
225+
if ($key_encryption_algorithm instanceof KeyAgreement
226+
|| $key_encryption_algorithm instanceof KeyAgreementWithKeyWrapping) {
227+
KeyChecker::checkKeyAgreementKeyUsage($recipientKey, 'decryption');
228+
} else {
229+
KeyChecker::checkKeyUsage($recipientKey, 'decryption');
230+
}
226231
if ($key_encryption_algorithm->name() !== 'dir') {
227232
KeyChecker::checkKeyAlgorithm($recipientKey, $key_encryption_algorithm->name());
228233
} else {
@@ -247,6 +252,7 @@ private function decryptRecipientKey(
247252
continue;
248253
}
249254
$this->deprecateLongerDirectKey($cek, $key_encryption_algorithm, $content_encryption_algorithm);
255+
$this->deprecateDirectKeyNotAllowedToDecrypt($recipientKey, $key_encryption_algorithm);
250256

251257
return DecryptionResult::success($jwe->withPayload($payload), $i, $recipientKey);
252258
}
@@ -276,6 +282,24 @@ private function deprecateLongerDirectKey(
276282
);
277283
}
278284

285+
/**
286+
* Only the key that decrypted the token is reported: a key set may hold keys restricted to other algorithms.
287+
*/
288+
private function deprecateDirectKeyNotAllowedToDecrypt(
289+
JWK $key,
290+
KeyEncryptionAlgorithm $keyEncryptionAlgorithm
291+
): void {
292+
if (! $keyEncryptionAlgorithm instanceof DirectEncryption || KeyChecker::allowsOperation($key, 'decrypt')) {
293+
return;
294+
}
295+
trigger_deprecation(
296+
'web-token/jwt-library',
297+
'4.3.0',
298+
'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.',
299+
$keyEncryptionAlgorithm->name()
300+
);
301+
}
302+
279303
/**
280304
* A key obtained by key agreement is checked like a decrypted or unwrapped one, and a direct key must be at least
281305
* that long: the content encryption algorithms pad a short key with zero bytes instead of rejecting it.

0 commit comments

Comments
 (0)