@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWE\KeyAlgorithm; |
6 | 6 |
@@ -21,202 +21,202 @@ |
||
21 | 21 | */ |
22 | 22 | abstract class RSAESKeyAlgorithm extends KeyManagementAlgorithm |
23 | 23 | { |
24 | - use RandomCEK; |
|
25 | - |
|
26 | - /** |
|
27 | - * Mapping from algorithm name to class name. |
|
28 | - * |
|
29 | - * @internal |
|
30 | - * |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - const MAP_ALGO_TO_CLASS = [ |
|
34 | - JWA::ALGO_RSA1_5 => RSAESPKCS1Algorithm::class, |
|
35 | - JWA::ALGO_RSA_OAEP => RSAESOAEPAlgorithm::class, |
|
36 | - ]; |
|
37 | - |
|
38 | - /** |
|
39 | - * Public key. |
|
40 | - * |
|
41 | - * @var RSAPublicKeyJWK |
|
42 | - */ |
|
43 | - protected $_publicKey; |
|
44 | - |
|
45 | - /** |
|
46 | - * Private key. |
|
47 | - * |
|
48 | - * @var null|RSAPrivateKeyJWK |
|
49 | - */ |
|
50 | - protected $_privateKey; |
|
51 | - |
|
52 | - /** |
|
53 | - * Constructor. |
|
54 | - * |
|
55 | - * Use `fromPublicKey` or `fromPrivateKey` instead! |
|
56 | - * |
|
57 | - * @param RSAPublicKeyJWK $pub_key RSA public key |
|
58 | - * @param RSAPrivateKeyJWK $priv_key Optional RSA private key |
|
59 | - */ |
|
60 | - protected function __construct(RSAPublicKeyJWK $pub_key, |
|
61 | - ?RSAPrivateKeyJWK $priv_key = null) |
|
62 | - { |
|
63 | - $this->_publicKey = $pub_key; |
|
64 | - $this->_privateKey = $priv_key; |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Initialize from JWK. |
|
69 | - * |
|
70 | - * @param JWK $jwk |
|
71 | - * @param Header $header |
|
72 | - * |
|
73 | - * @throws \UnexpectedValueException |
|
74 | - * |
|
75 | - * @return self |
|
76 | - */ |
|
77 | - public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm |
|
78 | - { |
|
79 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
80 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
81 | - throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
82 | - } |
|
83 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
84 | - if ($jwk->has(...RSAPrivateKeyJWK::MANAGED_PARAMS)) { |
|
85 | - return $cls::fromPrivateKey(RSAPrivateKeyJWK::fromJWK($jwk)); |
|
86 | - } |
|
87 | - return $cls::fromPublicKey(RSAPublicKeyJWK::fromJWK($jwk)); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Initialize from a public key. |
|
92 | - * |
|
93 | - * @param RSAPublicKeyJWK $jwk |
|
94 | - * |
|
95 | - * @return self |
|
96 | - */ |
|
97 | - public static function fromPublicKey(RSAPublicKeyJWK $jwk): self |
|
98 | - { |
|
99 | - return new static($jwk); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Initialize from a private key. |
|
104 | - * |
|
105 | - * @param RSAPrivateKeyJWK $jwk |
|
106 | - * |
|
107 | - * @return self |
|
108 | - */ |
|
109 | - public static function fromPrivateKey(RSAPrivateKeyJWK $jwk): self |
|
110 | - { |
|
111 | - return new static($jwk->publicKey(), $jwk); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Get the public key. |
|
116 | - * |
|
117 | - * @return RSAPublicKeyJWK |
|
118 | - */ |
|
119 | - public function publicKey(): RSAPublicKeyJWK |
|
120 | - { |
|
121 | - return $this->_publicKey; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Check whether the private key is present. |
|
126 | - * |
|
127 | - * @return bool |
|
128 | - */ |
|
129 | - public function hasPrivateKey(): bool |
|
130 | - { |
|
131 | - return isset($this->_privateKey); |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * Get the private key. |
|
136 | - * |
|
137 | - * @throws \LogicException |
|
138 | - * |
|
139 | - * @return RSAPrivateKeyJWK |
|
140 | - */ |
|
141 | - public function privateKey(): RSAPrivateKeyJWK |
|
142 | - { |
|
143 | - if (!$this->hasPrivateKey()) { |
|
144 | - throw new \LogicException('Private key not set.'); |
|
145 | - } |
|
146 | - return $this->_privateKey; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * {@inheritdoc} |
|
151 | - */ |
|
152 | - public function headerParameters(): array |
|
153 | - { |
|
154 | - return array_merge(parent::headerParameters(), |
|
155 | - [AlgorithmParameter::fromAlgorithm($this)]); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Get the padding scheme. |
|
160 | - * |
|
161 | - * @return int |
|
162 | - */ |
|
163 | - abstract protected function _paddingScheme(): int; |
|
164 | - |
|
165 | - /** |
|
166 | - * {@inheritdoc} |
|
167 | - */ |
|
168 | - protected function _encryptKey(string $key, Header &$header): string |
|
169 | - { |
|
170 | - $pubkey = openssl_pkey_get_public( |
|
171 | - $this->publicKey()->toPEM()->string()); |
|
172 | - if (false === $pubkey) { |
|
173 | - throw new \RuntimeException( |
|
174 | - 'openssl_pkey_get_public() failed: ' . |
|
175 | - $this->_getLastOpenSSLError()); |
|
176 | - } |
|
177 | - $result = openssl_public_encrypt($key, $crypted, $pubkey, |
|
178 | - $this->_paddingScheme()); |
|
179 | - if (!$result) { |
|
180 | - throw new \RuntimeException( |
|
181 | - 'openssl_public_encrypt() failed: ' . |
|
182 | - $this->_getLastOpenSSLError()); |
|
183 | - } |
|
184 | - return $crypted; |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * {@inheritdoc} |
|
189 | - */ |
|
190 | - protected function _decryptKey(string $ciphertext, Header $header): string |
|
191 | - { |
|
192 | - $privkey = openssl_pkey_get_private( |
|
193 | - $this->privateKey()->toPEM()->string()); |
|
194 | - if (false === $privkey) { |
|
195 | - throw new \RuntimeException( |
|
196 | - 'openssl_pkey_get_private() failed: ' . |
|
197 | - $this->_getLastOpenSSLError()); |
|
198 | - } |
|
199 | - $result = openssl_private_decrypt($ciphertext, $cek, $privkey, |
|
200 | - $this->_paddingScheme()); |
|
201 | - if (!$result) { |
|
202 | - throw new \RuntimeException( |
|
203 | - 'openssl_private_decrypt() failed: ' . |
|
204 | - $this->_getLastOpenSSLError()); |
|
205 | - } |
|
206 | - return $cek; |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Get last OpenSSL error message. |
|
211 | - * |
|
212 | - * @return null|string |
|
213 | - */ |
|
214 | - protected function _getLastOpenSSLError(): ?string |
|
215 | - { |
|
216 | - $msg = null; |
|
217 | - while (false !== ($err = openssl_error_string())) { |
|
218 | - $msg = $err; |
|
219 | - } |
|
220 | - return $msg; |
|
221 | - } |
|
24 | + use RandomCEK; |
|
25 | + |
|
26 | + /** |
|
27 | + * Mapping from algorithm name to class name. |
|
28 | + * |
|
29 | + * @internal |
|
30 | + * |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + const MAP_ALGO_TO_CLASS = [ |
|
34 | + JWA::ALGO_RSA1_5 => RSAESPKCS1Algorithm::class, |
|
35 | + JWA::ALGO_RSA_OAEP => RSAESOAEPAlgorithm::class, |
|
36 | + ]; |
|
37 | + |
|
38 | + /** |
|
39 | + * Public key. |
|
40 | + * |
|
41 | + * @var RSAPublicKeyJWK |
|
42 | + */ |
|
43 | + protected $_publicKey; |
|
44 | + |
|
45 | + /** |
|
46 | + * Private key. |
|
47 | + * |
|
48 | + * @var null|RSAPrivateKeyJWK |
|
49 | + */ |
|
50 | + protected $_privateKey; |
|
51 | + |
|
52 | + /** |
|
53 | + * Constructor. |
|
54 | + * |
|
55 | + * Use `fromPublicKey` or `fromPrivateKey` instead! |
|
56 | + * |
|
57 | + * @param RSAPublicKeyJWK $pub_key RSA public key |
|
58 | + * @param RSAPrivateKeyJWK $priv_key Optional RSA private key |
|
59 | + */ |
|
60 | + protected function __construct(RSAPublicKeyJWK $pub_key, |
|
61 | + ?RSAPrivateKeyJWK $priv_key = null) |
|
62 | + { |
|
63 | + $this->_publicKey = $pub_key; |
|
64 | + $this->_privateKey = $priv_key; |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Initialize from JWK. |
|
69 | + * |
|
70 | + * @param JWK $jwk |
|
71 | + * @param Header $header |
|
72 | + * |
|
73 | + * @throws \UnexpectedValueException |
|
74 | + * |
|
75 | + * @return self |
|
76 | + */ |
|
77 | + public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm |
|
78 | + { |
|
79 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
80 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
81 | + throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
82 | + } |
|
83 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
84 | + if ($jwk->has(...RSAPrivateKeyJWK::MANAGED_PARAMS)) { |
|
85 | + return $cls::fromPrivateKey(RSAPrivateKeyJWK::fromJWK($jwk)); |
|
86 | + } |
|
87 | + return $cls::fromPublicKey(RSAPublicKeyJWK::fromJWK($jwk)); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Initialize from a public key. |
|
92 | + * |
|
93 | + * @param RSAPublicKeyJWK $jwk |
|
94 | + * |
|
95 | + * @return self |
|
96 | + */ |
|
97 | + public static function fromPublicKey(RSAPublicKeyJWK $jwk): self |
|
98 | + { |
|
99 | + return new static($jwk); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Initialize from a private key. |
|
104 | + * |
|
105 | + * @param RSAPrivateKeyJWK $jwk |
|
106 | + * |
|
107 | + * @return self |
|
108 | + */ |
|
109 | + public static function fromPrivateKey(RSAPrivateKeyJWK $jwk): self |
|
110 | + { |
|
111 | + return new static($jwk->publicKey(), $jwk); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Get the public key. |
|
116 | + * |
|
117 | + * @return RSAPublicKeyJWK |
|
118 | + */ |
|
119 | + public function publicKey(): RSAPublicKeyJWK |
|
120 | + { |
|
121 | + return $this->_publicKey; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Check whether the private key is present. |
|
126 | + * |
|
127 | + * @return bool |
|
128 | + */ |
|
129 | + public function hasPrivateKey(): bool |
|
130 | + { |
|
131 | + return isset($this->_privateKey); |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * Get the private key. |
|
136 | + * |
|
137 | + * @throws \LogicException |
|
138 | + * |
|
139 | + * @return RSAPrivateKeyJWK |
|
140 | + */ |
|
141 | + public function privateKey(): RSAPrivateKeyJWK |
|
142 | + { |
|
143 | + if (!$this->hasPrivateKey()) { |
|
144 | + throw new \LogicException('Private key not set.'); |
|
145 | + } |
|
146 | + return $this->_privateKey; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * {@inheritdoc} |
|
151 | + */ |
|
152 | + public function headerParameters(): array |
|
153 | + { |
|
154 | + return array_merge(parent::headerParameters(), |
|
155 | + [AlgorithmParameter::fromAlgorithm($this)]); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Get the padding scheme. |
|
160 | + * |
|
161 | + * @return int |
|
162 | + */ |
|
163 | + abstract protected function _paddingScheme(): int; |
|
164 | + |
|
165 | + /** |
|
166 | + * {@inheritdoc} |
|
167 | + */ |
|
168 | + protected function _encryptKey(string $key, Header &$header): string |
|
169 | + { |
|
170 | + $pubkey = openssl_pkey_get_public( |
|
171 | + $this->publicKey()->toPEM()->string()); |
|
172 | + if (false === $pubkey) { |
|
173 | + throw new \RuntimeException( |
|
174 | + 'openssl_pkey_get_public() failed: ' . |
|
175 | + $this->_getLastOpenSSLError()); |
|
176 | + } |
|
177 | + $result = openssl_public_encrypt($key, $crypted, $pubkey, |
|
178 | + $this->_paddingScheme()); |
|
179 | + if (!$result) { |
|
180 | + throw new \RuntimeException( |
|
181 | + 'openssl_public_encrypt() failed: ' . |
|
182 | + $this->_getLastOpenSSLError()); |
|
183 | + } |
|
184 | + return $crypted; |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * {@inheritdoc} |
|
189 | + */ |
|
190 | + protected function _decryptKey(string $ciphertext, Header $header): string |
|
191 | + { |
|
192 | + $privkey = openssl_pkey_get_private( |
|
193 | + $this->privateKey()->toPEM()->string()); |
|
194 | + if (false === $privkey) { |
|
195 | + throw new \RuntimeException( |
|
196 | + 'openssl_pkey_get_private() failed: ' . |
|
197 | + $this->_getLastOpenSSLError()); |
|
198 | + } |
|
199 | + $result = openssl_private_decrypt($ciphertext, $cek, $privkey, |
|
200 | + $this->_paddingScheme()); |
|
201 | + if (!$result) { |
|
202 | + throw new \RuntimeException( |
|
203 | + 'openssl_private_decrypt() failed: ' . |
|
204 | + $this->_getLastOpenSSLError()); |
|
205 | + } |
|
206 | + return $cek; |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Get last OpenSSL error message. |
|
211 | + * |
|
212 | + * @return null|string |
|
213 | + */ |
|
214 | + protected function _getLastOpenSSLError(): ?string |
|
215 | + { |
|
216 | + $msg = null; |
|
217 | + while (false !== ($err = openssl_error_string())) { |
|
218 | + $msg = $err; |
|
219 | + } |
|
220 | + return $msg; |
|
221 | + } |
|
222 | 222 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWE\KeyAlgorithm; |
6 | 6 |
@@ -13,19 +13,19 @@ |
||
13 | 13 | */ |
14 | 14 | class A192GCMKWAlgorithm extends AESGCMKWAlgorithm |
15 | 15 | { |
16 | - /** |
|
17 | - * {@inheritdoc} |
|
18 | - */ |
|
19 | - public function algorithmParamValue(): string |
|
20 | - { |
|
21 | - return JWA::ALGO_A192GCMKW; |
|
22 | - } |
|
16 | + /** |
|
17 | + * {@inheritdoc} |
|
18 | + */ |
|
19 | + public function algorithmParamValue(): string |
|
20 | + { |
|
21 | + return JWA::ALGO_A192GCMKW; |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * {@inheritdoc} |
|
26 | - */ |
|
27 | - protected function _keySize(): int |
|
28 | - { |
|
29 | - return 24; |
|
30 | - } |
|
24 | + /** |
|
25 | + * {@inheritdoc} |
|
26 | + */ |
|
27 | + protected function _keySize(): int |
|
28 | + { |
|
29 | + return 24; |
|
30 | + } |
|
31 | 31 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWE\KeyAlgorithm; |
6 | 6 |
@@ -22,147 +22,147 @@ |
||
22 | 22 | */ |
23 | 23 | abstract class AESGCMKWAlgorithm extends KeyManagementAlgorithm |
24 | 24 | { |
25 | - use RandomCEK; |
|
26 | - |
|
27 | - /** |
|
28 | - * Mapping from algorithm name to class name. |
|
29 | - * |
|
30 | - * @internal |
|
31 | - * |
|
32 | - * @var array |
|
33 | - */ |
|
34 | - const MAP_ALGO_TO_CLASS = [ |
|
35 | - JWA::ALGO_A128GCMKW => A128GCMKWAlgorithm::class, |
|
36 | - JWA::ALGO_A192GCMKW => A192GCMKWAlgorithm::class, |
|
37 | - JWA::ALGO_A256GCMKW => A256GCMKWAlgorithm::class, |
|
38 | - ]; |
|
39 | - |
|
40 | - /** |
|
41 | - * Required IV size in bytes. |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - const IV_SIZE = 12; |
|
46 | - |
|
47 | - /** |
|
48 | - * Authentication tag size in bytes. |
|
49 | - * |
|
50 | - * @var int |
|
51 | - */ |
|
52 | - const AUTH_TAG_SIZE = 16; |
|
53 | - |
|
54 | - /** |
|
55 | - * Key encryption key. |
|
56 | - * |
|
57 | - * @var string |
|
58 | - */ |
|
59 | - protected $_kek; |
|
60 | - |
|
61 | - /** |
|
62 | - * Initialization vector. |
|
63 | - * |
|
64 | - * @var string |
|
65 | - */ |
|
66 | - protected $_iv; |
|
67 | - |
|
68 | - /** |
|
69 | - * Constructor. |
|
70 | - * |
|
71 | - * @param string $kek Key encryption key |
|
72 | - * @param string $iv Initialization vector |
|
73 | - */ |
|
74 | - public function __construct(string $kek, string $iv) |
|
75 | - { |
|
76 | - if (strlen($kek) !== $this->_keySize()) { |
|
77 | - throw new \LengthException('Invalid key size.'); |
|
78 | - } |
|
79 | - if (self::IV_SIZE !== strlen($iv)) { |
|
80 | - throw new \LengthException('Initialization vector must be 96 bits.'); |
|
81 | - } |
|
82 | - $this->_kek = $kek; |
|
83 | - $this->_iv = $iv; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Initialize from JWK. |
|
88 | - * |
|
89 | - * @param JWK $jwk |
|
90 | - * @param Header $header |
|
91 | - * |
|
92 | - * @throws \UnexpectedValueException |
|
93 | - * |
|
94 | - * @return self |
|
95 | - */ |
|
96 | - public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm |
|
97 | - { |
|
98 | - $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
99 | - if (!$header->hasInitializationVector()) { |
|
100 | - throw new \UnexpectedValueException('No initialization vector.'); |
|
101 | - } |
|
102 | - $iv = $header->initializationVector()->initializationVector(); |
|
103 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
104 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
105 | - throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
106 | - } |
|
107 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
108 | - return new $cls($jwk->key(), $iv); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Initialize from key encryption key with random IV. |
|
113 | - * |
|
114 | - * Key size must match the underlying cipher. |
|
115 | - * |
|
116 | - * @param string $key Key encryption key |
|
117 | - * |
|
118 | - * @return self |
|
119 | - */ |
|
120 | - public static function fromKey(string $key): self |
|
121 | - { |
|
122 | - $iv = openssl_random_pseudo_bytes(self::IV_SIZE); |
|
123 | - return new static($key, $iv); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * {@inheritdoc} |
|
128 | - */ |
|
129 | - public function headerParameters(): array |
|
130 | - { |
|
131 | - return array_merge(parent::headerParameters(), |
|
132 | - [AlgorithmParameter::fromAlgorithm($this), |
|
133 | - InitializationVectorParameter::fromString($this->_iv), ]); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Get the required key size. |
|
138 | - * |
|
139 | - * @return int |
|
140 | - */ |
|
141 | - abstract protected function _keySize(): int; |
|
142 | - |
|
143 | - /** |
|
144 | - * {@inheritdoc} |
|
145 | - */ |
|
146 | - protected function _encryptKey(string $key, Header &$header): string |
|
147 | - { |
|
148 | - [$ciphertext, $auth_tag] = AESGCM::encrypt($key, '', $this->_kek, |
|
149 | - $this->_iv, self::AUTH_TAG_SIZE); |
|
150 | - // insert authentication tag to the header |
|
151 | - $header = $header->withParameters( |
|
152 | - AuthenticationTagParameter::fromString($auth_tag)); |
|
153 | - return $ciphertext; |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * {@inheritdoc} |
|
158 | - */ |
|
159 | - protected function _decryptKey(string $ciphertext, Header $header): string |
|
160 | - { |
|
161 | - if (!$header->hasAuthenticationTag()) { |
|
162 | - throw new \RuntimeException( |
|
163 | - "Header doesn't contain authentication tag."); |
|
164 | - } |
|
165 | - $auth_tag = $header->authenticationTag()->authenticationTag(); |
|
166 | - return AESGCM::decrypt($ciphertext, $auth_tag, '', $this->_kek, $this->_iv); |
|
167 | - } |
|
25 | + use RandomCEK; |
|
26 | + |
|
27 | + /** |
|
28 | + * Mapping from algorithm name to class name. |
|
29 | + * |
|
30 | + * @internal |
|
31 | + * |
|
32 | + * @var array |
|
33 | + */ |
|
34 | + const MAP_ALGO_TO_CLASS = [ |
|
35 | + JWA::ALGO_A128GCMKW => A128GCMKWAlgorithm::class, |
|
36 | + JWA::ALGO_A192GCMKW => A192GCMKWAlgorithm::class, |
|
37 | + JWA::ALGO_A256GCMKW => A256GCMKWAlgorithm::class, |
|
38 | + ]; |
|
39 | + |
|
40 | + /** |
|
41 | + * Required IV size in bytes. |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + const IV_SIZE = 12; |
|
46 | + |
|
47 | + /** |
|
48 | + * Authentication tag size in bytes. |
|
49 | + * |
|
50 | + * @var int |
|
51 | + */ |
|
52 | + const AUTH_TAG_SIZE = 16; |
|
53 | + |
|
54 | + /** |
|
55 | + * Key encryption key. |
|
56 | + * |
|
57 | + * @var string |
|
58 | + */ |
|
59 | + protected $_kek; |
|
60 | + |
|
61 | + /** |
|
62 | + * Initialization vector. |
|
63 | + * |
|
64 | + * @var string |
|
65 | + */ |
|
66 | + protected $_iv; |
|
67 | + |
|
68 | + /** |
|
69 | + * Constructor. |
|
70 | + * |
|
71 | + * @param string $kek Key encryption key |
|
72 | + * @param string $iv Initialization vector |
|
73 | + */ |
|
74 | + public function __construct(string $kek, string $iv) |
|
75 | + { |
|
76 | + if (strlen($kek) !== $this->_keySize()) { |
|
77 | + throw new \LengthException('Invalid key size.'); |
|
78 | + } |
|
79 | + if (self::IV_SIZE !== strlen($iv)) { |
|
80 | + throw new \LengthException('Initialization vector must be 96 bits.'); |
|
81 | + } |
|
82 | + $this->_kek = $kek; |
|
83 | + $this->_iv = $iv; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Initialize from JWK. |
|
88 | + * |
|
89 | + * @param JWK $jwk |
|
90 | + * @param Header $header |
|
91 | + * |
|
92 | + * @throws \UnexpectedValueException |
|
93 | + * |
|
94 | + * @return self |
|
95 | + */ |
|
96 | + public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm |
|
97 | + { |
|
98 | + $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
99 | + if (!$header->hasInitializationVector()) { |
|
100 | + throw new \UnexpectedValueException('No initialization vector.'); |
|
101 | + } |
|
102 | + $iv = $header->initializationVector()->initializationVector(); |
|
103 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
104 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
105 | + throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
106 | + } |
|
107 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
108 | + return new $cls($jwk->key(), $iv); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Initialize from key encryption key with random IV. |
|
113 | + * |
|
114 | + * Key size must match the underlying cipher. |
|
115 | + * |
|
116 | + * @param string $key Key encryption key |
|
117 | + * |
|
118 | + * @return self |
|
119 | + */ |
|
120 | + public static function fromKey(string $key): self |
|
121 | + { |
|
122 | + $iv = openssl_random_pseudo_bytes(self::IV_SIZE); |
|
123 | + return new static($key, $iv); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * {@inheritdoc} |
|
128 | + */ |
|
129 | + public function headerParameters(): array |
|
130 | + { |
|
131 | + return array_merge(parent::headerParameters(), |
|
132 | + [AlgorithmParameter::fromAlgorithm($this), |
|
133 | + InitializationVectorParameter::fromString($this->_iv), ]); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Get the required key size. |
|
138 | + * |
|
139 | + * @return int |
|
140 | + */ |
|
141 | + abstract protected function _keySize(): int; |
|
142 | + |
|
143 | + /** |
|
144 | + * {@inheritdoc} |
|
145 | + */ |
|
146 | + protected function _encryptKey(string $key, Header &$header): string |
|
147 | + { |
|
148 | + [$ciphertext, $auth_tag] = AESGCM::encrypt($key, '', $this->_kek, |
|
149 | + $this->_iv, self::AUTH_TAG_SIZE); |
|
150 | + // insert authentication tag to the header |
|
151 | + $header = $header->withParameters( |
|
152 | + AuthenticationTagParameter::fromString($auth_tag)); |
|
153 | + return $ciphertext; |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * {@inheritdoc} |
|
158 | + */ |
|
159 | + protected function _decryptKey(string $ciphertext, Header $header): string |
|
160 | + { |
|
161 | + if (!$header->hasAuthenticationTag()) { |
|
162 | + throw new \RuntimeException( |
|
163 | + "Header doesn't contain authentication tag."); |
|
164 | + } |
|
165 | + $auth_tag = $header->authenticationTag()->authenticationTag(); |
|
166 | + return AESGCM::decrypt($ciphertext, $auth_tag, '', $this->_kek, $this->_iv); |
|
167 | + } |
|
168 | 168 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWE\KeyAlgorithm; |
6 | 6 |
@@ -13,19 +13,19 @@ |
||
13 | 13 | */ |
14 | 14 | class A128GCMKWAlgorithm extends AESGCMKWAlgorithm |
15 | 15 | { |
16 | - /** |
|
17 | - * {@inheritdoc} |
|
18 | - */ |
|
19 | - public function algorithmParamValue(): string |
|
20 | - { |
|
21 | - return JWA::ALGO_A128GCMKW; |
|
22 | - } |
|
16 | + /** |
|
17 | + * {@inheritdoc} |
|
18 | + */ |
|
19 | + public function algorithmParamValue(): string |
|
20 | + { |
|
21 | + return JWA::ALGO_A128GCMKW; |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * {@inheritdoc} |
|
26 | - */ |
|
27 | - protected function _keySize(): int |
|
28 | - { |
|
29 | - return 16; |
|
30 | - } |
|
24 | + /** |
|
25 | + * {@inheritdoc} |
|
26 | + */ |
|
27 | + protected function _keySize(): int |
|
28 | + { |
|
29 | + return 16; |
|
30 | + } |
|
31 | 31 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWE\KeyAlgorithm; |
6 | 6 |
@@ -13,19 +13,19 @@ |
||
13 | 13 | */ |
14 | 14 | class A256GCMKWAlgorithm extends AESGCMKWAlgorithm |
15 | 15 | { |
16 | - /** |
|
17 | - * {@inheritdoc} |
|
18 | - */ |
|
19 | - public function algorithmParamValue(): string |
|
20 | - { |
|
21 | - return JWA::ALGO_A256GCMKW; |
|
22 | - } |
|
16 | + /** |
|
17 | + * {@inheritdoc} |
|
18 | + */ |
|
19 | + public function algorithmParamValue(): string |
|
20 | + { |
|
21 | + return JWA::ALGO_A256GCMKW; |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * {@inheritdoc} |
|
26 | - */ |
|
27 | - protected function _keySize(): int |
|
28 | - { |
|
29 | - return 32; |
|
30 | - } |
|
24 | + /** |
|
25 | + * {@inheritdoc} |
|
26 | + */ |
|
27 | + protected function _keySize(): int |
|
28 | + { |
|
29 | + return 32; |
|
30 | + } |
|
31 | 31 | } |