|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\JWX\JWE\KeyAlgorithm; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\JWX\JWA\JWA; |
|
8
|
|
|
use Sop\JWX\JWE\KeyManagementAlgorithm; |
|
9
|
|
|
use Sop\JWX\JWK\JWK; |
|
10
|
|
|
use Sop\JWX\JWK\JWKSet; |
|
11
|
|
|
use Sop\JWX\JWT\Header\Header; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Factory class to construct key management algorithm instances. |
|
15
|
|
|
*/ |
|
16
|
|
|
class KeyAlgorithmFactory |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Mapping from algorithm name to class name. |
|
20
|
|
|
* |
|
21
|
|
|
* @internal |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
const MAP_ALGO_TO_CLASS = [ |
|
26
|
|
|
JWA::ALGO_A128KW => A128KWAlgorithm::class, |
|
27
|
|
|
JWA::ALGO_A192KW => A192KWAlgorithm::class, |
|
28
|
|
|
JWA::ALGO_A128KW => A256KWAlgorithm::class, |
|
29
|
|
|
JWA::ALGO_A128GCMKW => A128GCMKWAlgorithm::class, |
|
30
|
|
|
JWA::ALGO_A192GCMKW => A192GCMKWAlgorithm::class, |
|
31
|
|
|
JWA::ALGO_A256GCMKW => A256GCMKWAlgorithm::class, |
|
32
|
|
|
JWA::ALGO_PBES2_HS256_A128KW => PBES2HS256A128KWAlgorithm::class, |
|
33
|
|
|
JWA::ALGO_PBES2_HS384_A192KW => PBES2HS384A192KWAlgorithm::class, |
|
34
|
|
|
JWA::ALGO_PBES2_HS512_A256KW => PBES2HS512A256KWAlgorithm::class, |
|
35
|
|
|
JWA::ALGO_DIR => DirectCEKAlgorithm::class, |
|
36
|
|
|
JWA::ALGO_RSA1_5 => RSAESPKCS1Algorithm::class, |
|
37
|
|
|
JWA::ALGO_RSA_OAEP => RSAESOAEPAlgorithm::class, |
|
38
|
|
|
]; |
|
39
|
|
|
/** |
|
40
|
|
|
* Header. |
|
41
|
|
|
* |
|
42
|
|
|
* @var Header |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $_header; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param Header $header |
|
50
|
|
|
*/ |
|
51
|
12 |
|
public function __construct(Header $header) |
|
52
|
|
|
{ |
|
53
|
12 |
|
$this->_header = $header; |
|
54
|
12 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get key management algorithm by given JWK. |
|
58
|
|
|
* |
|
59
|
|
|
* @param JWK $jwk |
|
60
|
|
|
* |
|
61
|
|
|
* @return KeyManagementAlgorithm |
|
62
|
|
|
*/ |
|
63
|
9 |
|
public function algoByKey(JWK $jwk): KeyManagementAlgorithm |
|
64
|
|
|
{ |
|
65
|
9 |
|
$alg = JWA::deriveAlgorithmName($this->_header, $jwk); |
|
66
|
9 |
|
$cls = self::_algoClassByName($alg); |
|
67
|
8 |
|
return $cls::fromJWK($jwk, $this->_header); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get key management algorithm using a matching key from given JWK set. |
|
72
|
|
|
* |
|
73
|
|
|
* @param JWKSet $set |
|
74
|
|
|
* |
|
75
|
|
|
* @throws \UnexpectedValueException If a key cannot be found |
|
76
|
|
|
* |
|
77
|
|
|
* @return KeyManagementAlgorithm |
|
78
|
|
|
*/ |
|
79
|
7 |
|
public function algoByKeys(JWKSet $set): KeyManagementAlgorithm |
|
80
|
|
|
{ |
|
81
|
7 |
|
if (!$this->_header->hasKeyID()) { |
|
82
|
1 |
|
throw new \UnexpectedValueException('No key ID paremeter.'); |
|
83
|
|
|
} |
|
84
|
6 |
|
$id = $this->_header->keyID()->value(); |
|
85
|
6 |
|
if (!$set->hasKeyID($id)) { |
|
86
|
2 |
|
throw new \UnexpectedValueException("No key for ID '{$id}'."); |
|
87
|
|
|
} |
|
88
|
4 |
|
return $this->algoByKey($set->keyByID($id)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get the algorithm implementation class name by an algorithm name. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $alg Algorithm name |
|
95
|
|
|
* |
|
96
|
|
|
* @throws \UnexpectedValueException |
|
97
|
|
|
* |
|
98
|
|
|
* @return string Class name |
|
99
|
|
|
*/ |
|
100
|
9 |
|
private static function _algoClassByName(string $alg): string |
|
101
|
|
|
{ |
|
102
|
9 |
|
if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
103
|
1 |
|
throw new \UnexpectedValueException( |
|
104
|
1 |
|
"Algorithm '{$alg}' not supported."); |
|
105
|
|
|
} |
|
106
|
8 |
|
return self::MAP_ALGO_TO_CLASS[$alg]; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|