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
|
|
|
|
55
|
|
|
return true; |
56
|
|
|
case 'signature': |
57
|
|
|
Assertion::inArray('sign', $ops, 'Key cannot be used to sign'); |
58
|
|
|
|
59
|
|
|
return true; |
60
|
|
|
case 'encryption': |
61
|
|
|
Assertion::true(in_array('encrypt', $ops) || in_array('wrapKey', $ops), 'Key cannot be used to encrypt'); |
62
|
|
|
|
63
|
|
|
return true; |
64
|
|
|
case 'decryption': |
65
|
|
|
Assertion::true(in_array('decrypt', $ops) || in_array('unwrapKey', $ops), 'Key cannot be used to decrypt'); |
66
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
default: |
69
|
|
|
throw new \InvalidArgumentException('Unsupported key usage.'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param \Jose\Object\JWKInterface $key |
75
|
|
|
* @param string $usage |
76
|
|
|
* |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
private function checkUsage(JWKInterface $key, $usage) |
80
|
|
|
{ |
81
|
|
|
$use = $key->get('use'); |
82
|
|
|
switch ($usage) { |
83
|
|
|
case 'verification': |
84
|
|
|
case 'signature': |
85
|
|
|
Assertion::eq('sig', $use, 'Key cannot be used to sign or verify a signature'); |
86
|
|
|
|
87
|
|
|
return true; |
88
|
|
|
case 'encryption': |
89
|
|
|
case 'decryption': |
90
|
|
|
Assertion::eq('enc', $use, 'Key cannot be used to encrypt or decrypt'); |
91
|
|
|
|
92
|
|
|
return true; |
93
|
|
|
default: |
94
|
|
|
throw new \InvalidArgumentException('Unsupported key usage.'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param \Jose\Object\JWKInterface $key |
100
|
|
|
* @param string $algorithm |
101
|
|
|
*/ |
102
|
|
|
private function checkKeyAlgorithm(JWKInterface $key, $algorithm) |
103
|
|
|
{ |
104
|
|
|
if (!$key->has('alg')) { |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
Assertion::eq($key->get('alg'), $algorithm, sprintf('Key is only allowed for algorithm "%s".', $key->get('alg'))); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|