|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License (MIT) |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2014-2016 Spomky-Labs |
|
7
|
|
|
* |
|
8
|
|
|
* This software may be modified and distributed under the terms |
|
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Jose\Behaviour; |
|
13
|
|
|
|
|
14
|
|
|
use Assert\Assertion; |
|
15
|
|
|
use Jose\Object\JWKInterface; |
|
16
|
|
|
|
|
17
|
|
|
trait HasKeyChecker |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @param \Jose\Object\JWKInterface $key |
|
21
|
|
|
* @param string $usage |
|
22
|
|
|
* |
|
23
|
|
|
* @throws \InvalidArgumentException |
|
24
|
|
|
* |
|
25
|
|
|
* @return bool |
|
26
|
|
|
*/ |
|
27
|
|
|
private function checkKeyUsage(JWKInterface $key, $usage) |
|
28
|
|
|
{ |
|
29
|
|
|
if ($key->has('use')) { |
|
30
|
|
|
return $this->checkUsage($key, $usage); |
|
31
|
|
|
} |
|
32
|
|
|
if ($key->has('key_ops')) { |
|
33
|
|
|
return $this->checkOperation($key, $usage); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return true; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param \Jose\Object\JWKInterface $key |
|
41
|
|
|
* @param string $usage |
|
42
|
|
|
* |
|
43
|
|
|
* @return bool |
|
44
|
|
|
*/ |
|
45
|
|
|
private function checkOperation(JWKInterface $key, $usage) |
|
46
|
|
|
{ |
|
47
|
|
|
$ops = $key->get('key_ops'); |
|
48
|
|
|
if (!is_array($ops)) { |
|
49
|
|
|
$ops = [$ops]; |
|
50
|
|
|
} |
|
51
|
|
|
switch ($usage) { |
|
52
|
|
|
case 'verification': |
|
53
|
|
|
Assertion::inArray('verify', $ops, 'Key cannot be used to verify a signature'); |
|
54
|
|
|
return true; |
|
55
|
|
|
case 'signature': |
|
56
|
|
|
Assertion::inArray('sign', $ops, 'Key cannot be used to sign'); |
|
57
|
|
|
return true; |
|
58
|
|
|
case 'encryption': |
|
59
|
|
|
Assertion::true(in_array('encrypt', $ops) || in_array('wrapKey', $ops), 'Key cannot be used to encrypt'); |
|
60
|
|
|
return true; |
|
61
|
|
|
case 'decryption': |
|
62
|
|
|
Assertion::true(in_array('decrypt', $ops) || in_array('unwrapKey', $ops), 'Key cannot be used to decrypt'); |
|
63
|
|
|
return true; |
|
64
|
|
|
default: |
|
65
|
|
|
throw new \InvalidArgumentException('Unsupported key usage.'); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param \Jose\Object\JWKInterface $key |
|
71
|
|
|
* @param string $usage |
|
72
|
|
|
* |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
|
|
private function checkUsage(JWKInterface $key, $usage) |
|
76
|
|
|
{ |
|
77
|
|
|
$use = $key->get('use'); |
|
78
|
|
|
switch ($usage) { |
|
79
|
|
|
case 'verification': |
|
80
|
|
|
case 'signature': |
|
81
|
|
|
Assertion::eq('sig', $use, 'Key cannot be used to sign or verify a signature'); |
|
82
|
|
|
return true; |
|
83
|
|
|
case 'encryption': |
|
84
|
|
|
case 'decryption': |
|
85
|
|
|
Assertion::eq('enc', $use, 'Key cannot be used to encrypt or decrypt'); |
|
86
|
|
|
return true; |
|
87
|
|
|
default: |
|
88
|
|
|
throw new \InvalidArgumentException('Unsupported key usage.'); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param \Jose\Object\JWKInterface $key |
|
94
|
|
|
* @param string $algorithm |
|
95
|
|
|
*/ |
|
96
|
|
|
private function checkKeyAlgorithm(JWKInterface $key, $algorithm) |
|
97
|
|
|
{ |
|
98
|
|
|
if (!$key->has('alg')) { |
|
99
|
|
|
return; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
Assertion::eq($key->get('alg'), $algorithm, sprintf('Key is only allowed for algorithm "%s".', $key->get('alg'))); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|