@@ -97,8 +97,9 @@ discard block |
||
97 | 97 | protected function __construct(IJWEJOSEHeader $header, IJWSPayloadSpec $payload = null) |
98 | 98 | { |
99 | 99 | $this->header = $header; |
100 | - if(!is_null($payload)) |
|
101 | - $this->setPayload($payload); |
|
100 | + if(!is_null($payload)) { |
|
101 | + $this->setPayload($payload); |
|
102 | + } |
|
102 | 103 | } |
103 | 104 | |
104 | 105 | /** |
@@ -154,8 +155,9 @@ discard block |
||
154 | 155 | $this->decrypt(); |
155 | 156 | } |
156 | 157 | |
157 | - if (is_null($this->payload)) |
|
158 | - $this->payload = JWSPayloadFactory::build(''); |
|
158 | + if (is_null($this->payload)) { |
|
159 | + $this->payload = JWSPayloadFactory::build(''); |
|
160 | + } |
|
159 | 161 | |
160 | 162 | return ($this->payload instanceof IJWSPayloadRawSpec) ? $this->payload->getRaw():''; |
161 | 163 | } |
@@ -208,16 +210,21 @@ discard block |
||
208 | 210 | */ |
209 | 211 | private function getKeyManagementMode(EncryptionAlgorithm $alg) |
210 | 212 | { |
211 | - if($alg instanceof KeyEncryption) |
|
212 | - return KeyManagementModeValues::KeyEncryption; |
|
213 | - if($alg instanceof KeyWrapping) |
|
214 | - return KeyManagementModeValues::KeyWrapping; |
|
215 | - if($alg instanceof DirectKeyAgreement) |
|
216 | - return KeyManagementModeValues::DirectKeyAgreement; |
|
217 | - if($alg instanceof KeyAgreementWithKeyWrapping) |
|
218 | - return KeyManagementModeValues::KeyAgreementWithKeyWrapping; |
|
219 | - if($alg instanceof DirectEncryption) |
|
220 | - return KeyManagementModeValues::DirectEncryption; |
|
213 | + if($alg instanceof KeyEncryption) { |
|
214 | + return KeyManagementModeValues::KeyEncryption; |
|
215 | + } |
|
216 | + if($alg instanceof KeyWrapping) { |
|
217 | + return KeyManagementModeValues::KeyWrapping; |
|
218 | + } |
|
219 | + if($alg instanceof DirectKeyAgreement) { |
|
220 | + return KeyManagementModeValues::DirectKeyAgreement; |
|
221 | + } |
|
222 | + if($alg instanceof KeyAgreementWithKeyWrapping) { |
|
223 | + return KeyManagementModeValues::KeyAgreementWithKeyWrapping; |
|
224 | + } |
|
225 | + if($alg instanceof DirectEncryption) { |
|
226 | + return KeyManagementModeValues::DirectEncryption; |
|
227 | + } |
|
221 | 228 | } |
222 | 229 | |
223 | 230 | /** |
@@ -232,11 +239,12 @@ discard block |
||
232 | 239 | private function encrypt() |
233 | 240 | { |
234 | 241 | |
235 | - if (is_null($this->jwk)) |
|
236 | - throw new JWEInvalidRecipientKeyException; |
|
242 | + if (is_null($this->jwk)) { |
|
243 | + throw new JWEInvalidRecipientKeyException; |
|
244 | + } |
|
237 | 245 | |
238 | - if($this->jwk->getAlgorithm()->getValue()!== $this->header->getAlgorithm()->getString()) |
|
239 | - throw new InvalidJWKAlgorithm |
|
246 | + if($this->jwk->getAlgorithm()->getValue()!== $this->header->getAlgorithm()->getString()) { |
|
247 | + throw new InvalidJWKAlgorithm |
|
240 | 248 | ( |
241 | 249 | sprintf |
242 | 250 | ( |
@@ -245,16 +253,18 @@ discard block |
||
245 | 253 | $this->header->getAlgorithm()->getString() |
246 | 254 | ) |
247 | 255 | ); |
256 | + } |
|
248 | 257 | |
249 | 258 | $recipient_public_key = $this->jwk->getKey(JSONWebKeyKeyOperationsValues::EncryptContent); |
250 | 259 | |
251 | 260 | $key_management_algorithm = KeyManagementAlgorithms_Registry::getInstance()->get($this->header->getAlgorithm()->getString()); |
252 | 261 | |
253 | - if (is_null($key_management_algorithm)) |
|
254 | - throw new JWEUnsupportedKeyManagementAlgorithmException(sprintf('alg %s', $this->header->getAlgorithm()->getString())); |
|
262 | + if (is_null($key_management_algorithm)) { |
|
263 | + throw new JWEUnsupportedKeyManagementAlgorithmException(sprintf('alg %s', $this->header->getAlgorithm()->getString())); |
|
264 | + } |
|
255 | 265 | |
256 | - if($key_management_algorithm->getKeyType() !== $recipient_public_key->getAlgorithm()) |
|
257 | - throw new InvalidKeyTypeAlgorithmException |
|
266 | + if($key_management_algorithm->getKeyType() !== $recipient_public_key->getAlgorithm()) { |
|
267 | + throw new InvalidKeyTypeAlgorithmException |
|
258 | 268 | ( |
259 | 269 | sprintf |
260 | 270 | ( |
@@ -263,14 +273,15 @@ discard block |
||
263 | 273 | $recipient_public_key->getAlgorithm() |
264 | 274 | ) |
265 | 275 | ); |
276 | + } |
|
266 | 277 | |
267 | 278 | $content_encryption_algorithm = ContentEncryptionAlgorithms_Registry::getInstance()->get |
268 | 279 | ( |
269 | 280 | $this->header->getEncryptionAlgorithm()->getString() |
270 | 281 | ); |
271 | 282 | |
272 | - if (is_null($content_encryption_algorithm)) |
|
273 | - throw new JWEUnsupportedContentEncryptionAlgorithmException |
|
283 | + if (is_null($content_encryption_algorithm)) { |
|
284 | + throw new JWEUnsupportedContentEncryptionAlgorithmException |
|
274 | 285 | ( |
275 | 286 | sprintf |
276 | 287 | ( |
@@ -278,6 +289,7 @@ discard block |
||
278 | 289 | $this->header->getEncryptionAlgorithm()->getString() |
279 | 290 | ) |
280 | 291 | ); |
292 | + } |
|
281 | 293 | |
282 | 294 | $key_management_mode = $this->getKeyManagementMode($key_management_algorithm); |
283 | 295 | |
@@ -350,8 +362,8 @@ discard block |
||
350 | 362 | $key_management_mode = $this->getKeyManagementMode($alg); |
351 | 363 | $recipient_private_key = $this->jwk->getKey(JSONWebKeyKeyOperationsValues::DecryptContentAndValidateDecryption); |
352 | 364 | |
353 | - if($alg->getKeyType() !== $recipient_private_key->getAlgorithm()) |
|
354 | - throw new InvalidKeyTypeAlgorithmException |
|
365 | + if($alg->getKeyType() !== $recipient_private_key->getAlgorithm()) { |
|
366 | + throw new InvalidKeyTypeAlgorithmException |
|
355 | 367 | ( |
356 | 368 | sprintf |
357 | 369 | ( |
@@ -360,6 +372,7 @@ discard block |
||
360 | 372 | $recipient_private_key->getAlgorithm() |
361 | 373 | ) |
362 | 374 | ); |
375 | + } |
|
363 | 376 | |
364 | 377 | switch($key_management_mode){ |
365 | 378 | /** |
@@ -383,14 +396,16 @@ discard block |
||
383 | 396 | */ |
384 | 397 | case KeyManagementModeValues::DirectEncryption: |
385 | 398 | { |
386 | - if (!empty($this->enc_cek)) |
|
387 | - throw new JWEInvalidCompactFormatException('JWE Encrypted Key value is not an empty octetsequence.'); |
|
399 | + if (!empty($this->enc_cek)) { |
|
400 | + throw new JWEInvalidCompactFormatException('JWE Encrypted Key value is not an empty octetsequence.'); |
|
401 | + } |
|
388 | 402 | return $recipient_private_key; |
389 | 403 | } |
390 | 404 | case KeyManagementModeValues::DirectKeyAgreement: |
391 | 405 | { |
392 | - if (!empty($this->enc_cek)) |
|
393 | - throw new JWEInvalidCompactFormatException('JWE Encrypted Key value is not an empty octetsequence.'); |
|
406 | + if (!empty($this->enc_cek)) { |
|
407 | + throw new JWEInvalidCompactFormatException('JWE Encrypted Key value is not an empty octetsequence.'); |
|
408 | + } |
|
394 | 409 | throw new \Exception('unsupported Key Management Mode!'); |
395 | 410 | } |
396 | 411 | } |
@@ -409,13 +424,16 @@ discard block |
||
409 | 424 | */ |
410 | 425 | private function decrypt() |
411 | 426 | { |
412 | - if (is_null($this->jwk)) |
|
413 | - throw new JWEInvalidRecipientKeyException(); |
|
427 | + if (is_null($this->jwk)) { |
|
428 | + throw new JWEInvalidRecipientKeyException(); |
|
429 | + } |
|
414 | 430 | |
415 | - if (!$this->should_decrypt) return $this; |
|
431 | + if (!$this->should_decrypt) { |
|
432 | + return $this; |
|
433 | + } |
|
416 | 434 | |
417 | - if($this->jwk->getAlgorithm()->getValue()!== $this->header->getAlgorithm()->getString()) |
|
418 | - throw new InvalidJWKAlgorithm |
|
435 | + if($this->jwk->getAlgorithm()->getValue()!== $this->header->getAlgorithm()->getString()) { |
|
436 | + throw new InvalidJWKAlgorithm |
|
419 | 437 | ( |
420 | 438 | sprintf |
421 | 439 | ( |
@@ -424,14 +442,15 @@ discard block |
||
424 | 442 | $this->header->getAlgorithm()->getString() |
425 | 443 | ) |
426 | 444 | ); |
445 | + } |
|
427 | 446 | |
428 | 447 | $key_management_algorithm = KeyManagementAlgorithms_Registry::getInstance()->get |
429 | 448 | ( |
430 | 449 | $this->header->getAlgorithm()->getString() |
431 | 450 | ); |
432 | 451 | |
433 | - if (is_null($key_management_algorithm)) |
|
434 | - throw new JWEUnsupportedKeyManagementAlgorithmException |
|
452 | + if (is_null($key_management_algorithm)) { |
|
453 | + throw new JWEUnsupportedKeyManagementAlgorithmException |
|
435 | 454 | ( |
436 | 455 | sprintf |
437 | 456 | ( |
@@ -439,14 +458,15 @@ discard block |
||
439 | 458 | $this->header->getAlgorithm()->getString() |
440 | 459 | ) |
441 | 460 | ); |
461 | + } |
|
442 | 462 | |
443 | 463 | $content_encryption_algorithm = ContentEncryptionAlgorithms_Registry::getInstance()->get |
444 | 464 | ( |
445 | 465 | $this->header->getEncryptionAlgorithm()->getString() |
446 | 466 | ); |
447 | 467 | |
448 | - if (is_null($content_encryption_algorithm)) |
|
449 | - throw new JWEUnsupportedContentEncryptionAlgorithmException |
|
468 | + if (is_null($content_encryption_algorithm)) { |
|
469 | + throw new JWEUnsupportedContentEncryptionAlgorithmException |
|
450 | 470 | ( |
451 | 471 | sprintf |
452 | 472 | ( |
@@ -454,6 +474,7 @@ discard block |
||
454 | 474 | $this->header->getEncryptionAlgorithm()->getString() |
455 | 475 | ) |
456 | 476 | ); |
477 | + } |
|
457 | 478 | |
458 | 479 | $this->cek = $this->decryptJWEEncryptedKey($key_management_algorithm); |
459 | 480 |