1 | <?php |
||
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) |
|
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 |
|
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 |
|
109 |