|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License (MIT) |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2014-2015 Spomky-Labs |
|
7
|
|
|
* |
|
8
|
|
|
* This software may be modified and distributed under the terms |
|
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Jose; |
|
13
|
|
|
|
|
14
|
|
|
use Base64Url\Base64Url; |
|
15
|
|
|
use Jose\Algorithm\ContentEncryptionAlgorithmInterface; |
|
16
|
|
|
use Jose\Algorithm\JWAManagerInterface; |
|
17
|
|
|
use Jose\Algorithm\KeyEncryption\DirectEncryptionInterface; |
|
18
|
|
|
use Jose\Algorithm\KeyEncryption\KeyAgreementInterface; |
|
19
|
|
|
use Jose\Algorithm\KeyEncryption\KeyAgreementWrappingInterface; |
|
20
|
|
|
use Jose\Algorithm\KeyEncryption\KeyEncryptionInterface; |
|
21
|
|
|
use Jose\Algorithm\KeyEncryption\KeyWrappingInterface; |
|
22
|
|
|
use Jose\Algorithm\KeyEncryptionAlgorithmInterface; |
|
23
|
|
|
use Jose\Behaviour\HasCompressionManager; |
|
24
|
|
|
use Jose\Behaviour\HasJWAManager; |
|
25
|
|
|
use Jose\Behaviour\HasKeyChecker; |
|
26
|
|
|
use Jose\Compression\CompressionManagerInterface; |
|
27
|
|
|
use Jose\Object\JWEInterface; |
|
28
|
|
|
use Jose\Object\JWKInterface; |
|
29
|
|
|
use Jose\Object\Recipient; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
*/ |
|
33
|
|
|
final class Encrypter implements EncrypterInterface |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
use HasKeyChecker; |
|
36
|
|
|
use HasJWAManager; |
|
37
|
|
|
use HasCompressionManager; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Encrypter constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param \Jose\Algorithm\JWAManagerInterface $jwa_manager |
|
43
|
|
|
* @param \Jose\Compression\CompressionManagerInterface $compression_manager |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct( |
|
46
|
|
|
JWAManagerInterface $jwa_manager, |
|
47
|
|
|
CompressionManagerInterface $compression_manager) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->setJWAManager($jwa_manager); |
|
50
|
|
|
$this->setCompressionManager($compression_manager); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param \Jose\Object\JWEInterface $jwe |
|
55
|
|
|
* @param \Jose\Object\JWKInterface $recipient_key |
|
56
|
|
|
* @param \Jose\Object\JWKInterface|null $sender_key |
|
57
|
|
|
* @param array $recipient_headers |
|
58
|
|
|
* |
|
59
|
|
|
* @return \Jose\Object\JWEInterface |
|
60
|
|
|
*/ |
|
61
|
|
|
public function addRecipient(JWEInterface $jwe, JWKInterface $recipient_key, JWKInterface $sender_key = null, array $recipient_headers = []) |
|
62
|
|
|
{ |
|
63
|
|
|
$complete_headers = array_merge( |
|
64
|
|
|
$jwe->getSharedProtectedHeaders(), |
|
65
|
|
|
$jwe->getSharedHeaders(), |
|
66
|
|
|
$recipient_headers |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
$key_encryption_algorithm = $this->findKeyEncryptionAlgorithm($complete_headers); |
|
70
|
|
|
|
|
71
|
|
|
// Content Encryption Algorithm |
|
72
|
|
|
$content_encryption_algorithm = $this->findContentEncryptionAlgorithm($complete_headers); |
|
73
|
|
|
|
|
74
|
|
|
if (null === $jwe->getCiphertext()) { |
|
75
|
|
|
// the content is not yet encrypted (no recipient) |
|
76
|
|
|
|
|
77
|
|
|
// CEK |
|
78
|
|
|
$content_encryption_key = $this->getCEK( |
|
79
|
|
|
$complete_headers, |
|
80
|
|
|
$key_encryption_algorithm, |
|
|
|
|
|
|
81
|
|
|
$content_encryption_algorithm, |
|
|
|
|
|
|
82
|
|
|
$recipient_key, |
|
|
|
|
|
|
83
|
|
|
$sender_key |
|
|
|
|
|
|
84
|
|
|
); |
|
85
|
|
|
$jwe = $jwe->withContentEncryptionKey($content_encryption_key); |
|
86
|
|
|
|
|
87
|
|
|
// IV |
|
88
|
|
|
if (null !== $iv_size = $content_encryption_algorithm->getIVSize()) { |
|
89
|
|
|
$iv = $this->createIV($iv_size); |
|
90
|
|
|
$jwe = $jwe->withIV($iv); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// We encrypt the payload and get the tag |
|
94
|
|
|
$tag = null; |
|
95
|
|
|
$payload = $this->compressPayloadIfNeeded($jwe->getPayload(), $complete_headers); |
|
|
|
|
|
|
96
|
|
|
$ciphertext = $content_encryption_algorithm->encryptContent( |
|
97
|
|
|
$payload, |
|
98
|
|
|
$content_encryption_key, |
|
99
|
|
|
$jwe->getIV(), |
|
100
|
|
|
$jwe->getAAD(), |
|
101
|
|
|
$jwe->getEncodedSharedProtectedHeaders(), |
|
102
|
|
|
$tag |
|
103
|
|
|
); |
|
104
|
|
|
$jwe = $jwe->withCiphertext($ciphertext); |
|
105
|
|
|
|
|
106
|
|
|
// Tag |
|
107
|
|
|
if (null !== $tag) { |
|
108
|
|
|
$jwe = $jwe->withTag($tag); |
|
109
|
|
|
} |
|
110
|
|
|
// JWT Ciphertext |
|
111
|
|
|
} else { |
|
|
|
|
|
|
112
|
|
|
// On vérifie le jey managmenet mode |
|
113
|
|
|
|
|
114
|
|
|
// On vérifie si le CEK est disponible. |
|
115
|
|
|
// - si dispo on quitte le if |
|
116
|
|
|
// - sinon on ne peut pas aller plus loin. Le JWE doit être décrypté avant |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$recipient = new Recipient(); |
|
120
|
|
|
$recipient = $recipient->withHeaders($recipient_headers); |
|
121
|
|
|
|
|
122
|
|
|
$encrypted_content_encryption_key = $this->getEncryptedKey( |
|
123
|
|
|
$complete_headers, |
|
124
|
|
|
$jwe->getContentEncryptionKey(), |
|
125
|
|
|
$key_encryption_algorithm, |
|
|
|
|
|
|
126
|
|
|
$content_encryption_algorithm, |
|
|
|
|
|
|
127
|
|
|
$recipient_key, |
|
|
|
|
|
|
128
|
|
|
$sender_key |
|
129
|
|
|
); |
|
130
|
|
|
if (null !== $encrypted_content_encryption_key) { |
|
131
|
|
|
$recipient = $recipient->withEncryptedKey($encrypted_content_encryption_key); |
|
132
|
|
|
} |
|
133
|
|
|
$jwe = $jwe->addRecipient($recipient); |
|
134
|
|
|
|
|
135
|
|
|
return $jwe; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param string $payload |
|
140
|
|
|
* @param array $complete_headers |
|
141
|
|
|
* |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
|
|
private function compressPayloadIfNeeded($payload, array $complete_headers) |
|
145
|
|
|
{ |
|
146
|
|
|
if (!array_key_exists('zip', $complete_headers)) { |
|
147
|
|
|
return; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$compression_method = $this->getCompressionManager()->getCompressionAlgorithm($complete_headers['zip']); |
|
151
|
|
|
if (null === $compression_method) { |
|
152
|
|
|
throw new \RuntimeException(sprintf('Compression method "%s" not supported', $complete_headers['zip'])); |
|
153
|
|
|
} |
|
154
|
|
|
$compressed_payload = $compression_method->compress($payload); |
|
155
|
|
|
if (!is_string($compressed_payload)) { |
|
156
|
|
|
throw new \RuntimeException('Compression failed.'); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $compressed_payload; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param array $complete_header |
|
164
|
|
|
* |
|
165
|
|
|
* @return \Jose\Algorithm\KeyEncryption\DirectEncryptionInterface|\Jose\Algorithm\KeyEncryption\KeyEncryptionInterface|\Jose\Algorithm\KeyEncryption\KeyAgreementInterface|\Jose\Algorithm\KeyEncryption\KeyAgreementWrappingInterface |
|
166
|
|
|
*/ |
|
167
|
|
|
private function getKeyEncryptionAlgorithm($complete_header) |
|
|
|
|
|
|
168
|
|
|
{ |
|
169
|
|
|
if (!array_key_exists('alg', $complete_header)) { |
|
170
|
|
|
throw new \InvalidArgumentException('Parameter "alg" is missing.'); |
|
171
|
|
|
} |
|
172
|
|
|
$key_encryption_algorithm = $this->getJWAManager()->getAlgorithm($complete_header['alg']); |
|
173
|
|
|
foreach ([ |
|
174
|
|
|
'\Jose\Algorithm\KeyEncryption\DirectEncryptionInterface', |
|
175
|
|
|
'\Jose\Algorithm\KeyEncryption\KeyEncryptionInterface', |
|
176
|
|
|
'\Jose\Algorithm\KeyEncryption\KeyAgreementInterface', |
|
177
|
|
|
'\Jose\Algorithm\KeyEncryption\KeyAgreementWrappingInterface', |
|
178
|
|
|
] as $class) { |
|
179
|
|
|
if ($key_encryption_algorithm instanceof $class) { |
|
180
|
|
|
return $key_encryption_algorithm; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
throw new \RuntimeException(sprintf('The key encryption algorithm "%s" is not supported or not a key encryption algorithm instance.', $complete_header['alg'])); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param array $complete_headers |
|
188
|
|
|
* |
|
189
|
|
|
* @return \Jose\Algorithm\ContentEncryptionAlgorithmInterface |
|
190
|
|
|
*/ |
|
191
|
|
|
private function getContentEncryptionAlgorithm($complete_headers) |
|
|
|
|
|
|
192
|
|
|
{ |
|
193
|
|
|
if (!array_key_exists('enc', $complete_headers)) { |
|
194
|
|
|
throw new \InvalidArgumentException('Parameter "enc" is missing.'); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$content_encryption_algorithm = $this->getJWAManager()->getAlgorithm($complete_headers['enc']); |
|
198
|
|
|
if (!$content_encryption_algorithm instanceof ContentEncryptionAlgorithmInterface) { |
|
199
|
|
|
throw new \RuntimeException(sprintf('The algorithm "%s" is not enabled or does not implement ContentEncryptionAlgorithmInterface.', $complete_headers['enc'])); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return $content_encryption_algorithm; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
private function getEncryptedKey(array $complete_headers, $cek, KeyEncryptionAlgorithmInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
|
206
|
|
|
{ |
|
207
|
|
|
if ($key_encryption_algorithm instanceof KeyEncryptionInterface) { |
|
208
|
|
|
return $key_encryption_algorithm->encryptKey( |
|
209
|
|
|
$recipient_key, |
|
210
|
|
|
$cek, |
|
211
|
|
|
$complete_headers |
|
212
|
|
|
); |
|
213
|
|
|
} elseif ($key_encryption_algorithm instanceof KeyWrappingInterface) { |
|
214
|
|
|
return $key_encryption_algorithm->wrapKey( |
|
215
|
|
|
$recipient_key, |
|
216
|
|
|
$cek, |
|
217
|
|
|
$complete_headers |
|
218
|
|
|
); |
|
219
|
|
|
} elseif ($key_encryption_algorithm instanceof KeyAgreementWrappingInterface) { |
|
220
|
|
|
if (!$sender_key instanceof JWKInterface) { |
|
221
|
|
|
throw new \RuntimeException('The sender key must be set using Key Agreement or Key Agreement with Wrapping algorithms.'); |
|
222
|
|
|
} |
|
223
|
|
|
$additional_header_values = []; |
|
224
|
|
|
$jwt_cek = $key_encryption_algorithm->wrapAgreementKey($sender_key, $recipient_key, $cek, $content_encryption_algorithm->getCEKSize(), $complete_headers, $additional_header_values); |
|
|
|
|
|
|
225
|
|
|
//$this->updateHeader($additional_header_values, $protected_header, $recipient_header, $serialization); |
|
226
|
|
|
} elseif ($key_encryption_algorithm instanceof KeyAgreementInterface) { |
|
227
|
|
|
$additional_header_values = []; |
|
228
|
|
|
$jwt_cek = $key_encryption_algorithm->getAgreementKey($content_encryption_algorithm->getCEKSize(), $sender_key, $recipient_key, $complete_headers, $additional_header_values); |
|
|
|
|
|
|
229
|
|
|
//$this->updateHeader($additional_header_values, $protected_header, $recipient_header, $serialization); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
private function getCEK(array $complete_headers, KeyEncryptionAlgorithmInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
|
234
|
|
|
{ |
|
235
|
|
|
if ($key_encryption_algorithm instanceof KeyEncryptionInterface) { |
|
236
|
|
|
return $this->createCEK($content_encryption_algorithm->getCEKSize()); |
|
237
|
|
|
} elseif ($key_encryption_algorithm instanceof KeyAgreementWrappingInterface) { |
|
238
|
|
|
return $this->createCEK($content_encryption_algorithm->getCEKSize()); |
|
239
|
|
|
} elseif ($key_encryption_algorithm instanceof KeyAgreementInterface) { |
|
240
|
|
|
return $this->calculateAgreementKey($complete_headers, $key_encryption_algorithm, $content_encryption_algorithm, $recipient_key, $sender_key); |
|
241
|
|
|
} elseif ($key_encryption_algorithm instanceof DirectEncryptionInterface) { |
|
242
|
|
|
return $key_encryption_algorithm->getCEK($recipient_key); |
|
243
|
|
|
} else { |
|
244
|
|
|
throw new \RuntimeException('Unable to get key management mode.'); |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @param array $complete_headers |
|
250
|
|
|
* |
|
251
|
|
|
* @return \Jose\Algorithm\KeyEncryptionAlgorithmInterface |
|
252
|
|
|
*/ |
|
253
|
|
|
private function findKeyEncryptionAlgorithm(array $complete_headers) |
|
254
|
|
|
{ |
|
255
|
|
|
if (!array_key_exists('alg', $complete_headers)) { |
|
256
|
|
|
throw new \InvalidArgumentException('Parameter "alg" is missing.'); |
|
257
|
|
|
} |
|
258
|
|
|
$key_encryption_algorithm = $this->getJWAManager()->getAlgorithm($complete_headers['alg']); |
|
259
|
|
|
if ($key_encryption_algorithm instanceof KeyEncryptionAlgorithmInterface) { |
|
260
|
|
|
return $key_encryption_algorithm; |
|
261
|
|
|
} |
|
262
|
|
|
throw new \RuntimeException(sprintf('The key encryption algorithm "%s" is not supported or not a key encryption algorithm instance.', $complete_headers['alg'])); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* @param array $complete_headers |
|
267
|
|
|
* |
|
268
|
|
|
* @return \Jose\Algorithm\ContentEncryptionAlgorithmInterface |
|
269
|
|
|
*/ |
|
270
|
|
|
private function findContentEncryptionAlgorithm(array $complete_headers) |
|
271
|
|
|
{ |
|
272
|
|
|
if (!array_key_exists('enc', $complete_headers)) { |
|
273
|
|
|
throw new \InvalidArgumentException('Parameter "enc" is missing.'); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
$content_encryption_algorithm = $this->getJWAManager()->getAlgorithm($complete_headers['enc']); |
|
277
|
|
|
if (!$content_encryption_algorithm instanceof ContentEncryptionAlgorithmInterface) { |
|
278
|
|
|
throw new \RuntimeException(sprintf('The algorithm "%s" is not enabled or does not implement ContentEncryptionInterface.', $complete_headers['enc'])); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
return $content_encryption_algorithm; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* @param array $complete_headers |
|
286
|
|
|
* @param \Jose\Algorithm\KeyEncryption\KeyAgreementInterface $key_encryption_algorithm |
|
287
|
|
|
* @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
|
288
|
|
|
* @param \Jose\Object\JWKInterface $recipient_key |
|
289
|
|
|
* @param \Jose\Object\JWKInterface|null $sender_key |
|
290
|
|
|
* |
|
291
|
|
|
* @return string |
|
292
|
|
|
*/ |
|
293
|
|
|
private function calculateAgreementKey(array $complete_headers, KeyAgreementInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key, JWKInterface $sender_key = null) |
|
294
|
|
|
{ |
|
295
|
|
|
if (!$sender_key instanceof JWKInterface) { |
|
296
|
|
|
throw new \RuntimeException('The sender key must be set using Key Agreement or Key Agreement with Wrapping algorithms.'); |
|
297
|
|
|
} |
|
298
|
|
|
$additional_header_values = []; |
|
299
|
|
|
$cek = $key_encryption_algorithm->getAgreementKey($content_encryption_algorithm->getCEKSize(), $sender_key, $recipient_key, $complete_headers, $additional_header_values); |
|
300
|
|
|
|
|
301
|
|
|
return $cek; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* @param int $size |
|
306
|
|
|
* |
|
307
|
|
|
* @return string |
|
308
|
|
|
*/ |
|
309
|
|
|
private function createCEK($size) |
|
310
|
|
|
{ |
|
311
|
|
|
return $this->generateRandomString($size / 8); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* @param int $size |
|
316
|
|
|
* |
|
317
|
|
|
* @return string |
|
318
|
|
|
*/ |
|
319
|
|
|
private function createIV($size) |
|
320
|
|
|
{ |
|
321
|
|
|
return $this->generateRandomString($size / 8); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* @param int $length |
|
326
|
|
|
* |
|
327
|
|
|
* @return string |
|
328
|
|
|
*/ |
|
329
|
|
|
private function generateRandomString($length) |
|
330
|
|
|
{ |
|
331
|
|
|
if (function_exists('random_bytes')) { |
|
332
|
|
|
return random_bytes($length); |
|
333
|
|
|
} else { |
|
334
|
|
|
return openssl_random_pseudo_bytes($length); |
|
335
|
|
|
} |
|
336
|
|
|
} |
|
337
|
|
|
} |
|
338
|
|
|
|
This check looks for classes that have been defined more than once.
If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.
This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.