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
|
|
|
public 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
|
12 |
|
public function __construct(Header $header) |
50
|
|
|
{ |
51
|
12 |
|
$this->_header = $header; |
52
|
12 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get key management algorithm by given JWK. |
56
|
|
|
*/ |
57
|
9 |
|
public function algoByKey(JWK $jwk): KeyManagementAlgorithm |
58
|
|
|
{ |
59
|
9 |
|
$alg = JWA::deriveAlgorithmName($this->_header, $jwk); |
60
|
9 |
|
$cls = self::_algoClassByName($alg); |
61
|
8 |
|
return $cls::fromJWK($jwk, $this->_header); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get key management algorithm using a matching key from given JWK set. |
66
|
|
|
* |
67
|
|
|
* @throws \UnexpectedValueException If a key cannot be found |
68
|
|
|
*/ |
69
|
7 |
|
public function algoByKeys(JWKSet $set): KeyManagementAlgorithm |
70
|
|
|
{ |
71
|
7 |
|
if (!$this->_header->hasKeyID()) { |
72
|
1 |
|
throw new \UnexpectedValueException('No key ID paremeter.'); |
73
|
|
|
} |
74
|
6 |
|
$id = $this->_header->keyID()->value(); |
75
|
6 |
|
if (!$set->hasKeyID($id)) { |
76
|
2 |
|
throw new \UnexpectedValueException("No key for ID '{$id}'."); |
77
|
|
|
} |
78
|
4 |
|
return $this->algoByKey($set->keyByID($id)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the algorithm implementation class name by an algorithm name. |
83
|
|
|
* |
84
|
|
|
* @param string $alg Algorithm name |
85
|
|
|
* |
86
|
|
|
* @throws \UnexpectedValueException |
87
|
|
|
* |
88
|
|
|
* @return string Class name |
89
|
|
|
*/ |
90
|
9 |
|
private static function _algoClassByName(string $alg): string |
91
|
|
|
{ |
92
|
9 |
|
if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
93
|
1 |
|
throw new \UnexpectedValueException( |
94
|
1 |
|
"Algorithm '{$alg}' not supported."); |
95
|
|
|
} |
96
|
8 |
|
return self::MAP_ALGO_TO_CLASS[$alg]; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|