| 1 | <?php |
||
| 21 | abstract class AESKWAlgorithm extends KeyManagementAlgorithm |
||
| 22 | { |
||
| 23 | use RandomCEK; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Mapping from algorithm name to class name. |
||
| 27 | * |
||
| 28 | * @internal |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | const MAP_ALGO_TO_CLASS = [ |
||
| 33 | JWA::ALGO_A128KW => A128KWAlgorithm::class, |
||
| 34 | JWA::ALGO_A192KW => A192KWAlgorithm::class, |
||
| 35 | JWA::ALGO_A256KW => A256KWAlgorithm::class, |
||
| 36 | ]; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Key encryption key. |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $_kek; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Key wrapping algorithm. |
||
| 47 | * |
||
| 48 | * Lazily initialized. |
||
| 49 | * |
||
| 50 | * @var null|AESKeyWrapAlgorithm |
||
| 51 | */ |
||
| 52 | protected $_kw; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Constructor. |
||
| 56 | * |
||
| 57 | * @param string $kek Key encryption key |
||
| 58 | */ |
||
| 59 | 24 | public function __construct(string $kek) |
|
| 66 | 23 | } |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Initialize from JWK. |
||
| 70 | * |
||
| 71 | * @param JWK $jwk |
||
| 72 | * @param Header $header |
||
| 73 | * |
||
| 74 | * @throws \UnexpectedValueException |
||
| 75 | * |
||
| 76 | * @return self |
||
| 77 | */ |
||
| 78 | 9 | public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm |
|
| 79 | { |
||
| 80 | 9 | $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
| 81 | 9 | $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 82 | 8 | if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 83 | 1 | throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
| 84 | } |
||
| 85 | 7 | $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 86 | 7 | return new $cls($jwk->key()); |
|
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritdoc} |
||
| 91 | */ |
||
| 92 | 3 | public function headerParameters(): array |
|
| 93 | { |
||
| 94 | 3 | return array_merge(parent::headerParameters(), |
|
| 95 | 3 | [AlgorithmParameter::fromAlgorithm($this)]); |
|
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get the size of the key encryption key in bytes. |
||
| 100 | * |
||
| 101 | * @return int |
||
| 102 | */ |
||
| 103 | abstract protected function _kekSize(): int; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get key wrapping algorithm instance. |
||
| 107 | * |
||
| 108 | * @return AESKeyWrapAlgorithm |
||
| 109 | */ |
||
| 110 | abstract protected function _AESKWAlgo(): AESKeyWrapAlgorithm; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get key wrapping algorithm. |
||
| 114 | * |
||
| 115 | * @return AESKeyWrapAlgorithm |
||
| 116 | */ |
||
| 117 | 24 | protected function _kw(): AESKeyWrapAlgorithm |
|
| 118 | { |
||
| 119 | 24 | if (!isset($this->_kw)) { |
|
| 120 | 20 | $this->_kw = $this->_AESKWAlgo(); |
|
| 121 | } |
||
| 122 | 24 | return $this->_kw; |
|
|
1 ignored issue
–
show
|
|||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritdoc} |
||
| 127 | */ |
||
| 128 | 18 | protected function _encryptKey(string $key, Header &$header): string |
|
| 129 | { |
||
| 130 | 18 | return $this->_kw()->wrap($key, $this->_kek); |
|
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | 15 | protected function _decryptKey(string $ciphertext, Header $header): string |
|
| 139 | } |
||
| 140 | } |
||
| 141 |