|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\JWX\JWE\EncryptionAlgorithm; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\JWX\JWA\JWA; |
|
8
|
|
|
use Sop\JWX\JWE\ContentEncryptionAlgorithm; |
|
9
|
|
|
use Sop\JWX\JWT\Header\Header; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Factory class to construct content encryption algorithm instances. |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class EncryptionAlgorithmFactory |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Mapping from algorithm name to class name. |
|
18
|
|
|
* |
|
19
|
|
|
* @internal |
|
20
|
|
|
* |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
const MAP_ALGO_TO_CLASS = [ |
|
24
|
|
|
JWA::ALGO_A128CBC_HS256 => A128CBCHS256Algorithm::class, |
|
25
|
|
|
JWA::ALGO_A192CBC_HS384 => A192CBCHS384Algorithm::class, |
|
26
|
|
|
JWA::ALGO_A256CBC_HS512 => A256CBCHS512Algorithm::class, |
|
27
|
|
|
JWA::ALGO_A128GCM => A128GCMAlgorithm::class, |
|
28
|
|
|
JWA::ALGO_A192GCM => A192GCMAlgorithm::class, |
|
29
|
|
|
JWA::ALGO_A256GCM => A256GCMAlgorithm::class, |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get the content encryption algorithm by algorithm name. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $name Algorithm name |
|
36
|
|
|
* |
|
37
|
|
|
* @throws \UnexpectedValueException if algorithm is not supported |
|
38
|
|
|
* |
|
39
|
|
|
* @return ContentEncryptionAlgorithm |
|
40
|
|
|
*/ |
|
41
|
22 |
|
public static function algoByName(string $name): ContentEncryptionAlgorithm |
|
42
|
|
|
{ |
|
43
|
22 |
|
if (!array_key_exists($name, self::MAP_ALGO_TO_CLASS)) { |
|
44
|
1 |
|
throw new \UnexpectedValueException( |
|
45
|
1 |
|
"No content encryption algorithm '{$name}'."); |
|
46
|
|
|
} |
|
47
|
21 |
|
$cls = self::MAP_ALGO_TO_CLASS[$name]; |
|
48
|
21 |
|
return new $cls(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get the content encryption algorithm as specified in the given header. |
|
53
|
|
|
* |
|
54
|
|
|
* @param Header $header Header |
|
55
|
|
|
* |
|
56
|
|
|
* @throws \UnexpectedValueException If content encryption algorithm |
|
57
|
|
|
* parameter is not present or algorithm |
|
58
|
|
|
* is not supported |
|
59
|
|
|
* |
|
60
|
|
|
* @return ContentEncryptionAlgorithm |
|
61
|
|
|
*/ |
|
62
|
7 |
|
public static function algoByHeader(Header $header): ContentEncryptionAlgorithm |
|
63
|
|
|
{ |
|
64
|
7 |
|
if (!$header->hasEncryptionAlgorithm()) { |
|
65
|
1 |
|
throw new \UnexpectedValueException( |
|
66
|
1 |
|
'No encryption algorithm parameter.'); |
|
67
|
|
|
} |
|
68
|
6 |
|
return self::algoByName($header->encryptionAlgorithm()->value()); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|