@@ -63,13 +63,13 @@ |
||
63 | 63 | * @throws JWEInvalidPayloadException |
64 | 64 | * @throws JWEInvalidRecipientKeyException |
65 | 65 | */ |
66 | - public function __construct(IJWK $key, StringOrURI $alg, StringOrURI $enc, $payload, JsonValue $zip = null) |
|
66 | + public function __construct(IJWK $key, StringOrURI $alg, StringOrURI $enc, $payload, JsonValue $zip = null) |
|
67 | 67 | { |
68 | 68 | |
69 | - if(is_null($key)) |
|
69 | + if (is_null($key)) |
|
70 | 70 | throw new JWEInvalidRecipientKeyException(); |
71 | 71 | |
72 | - if(is_null($payload)) |
|
72 | + if (is_null($payload)) |
|
73 | 73 | throw new JWEInvalidPayloadException('missing payload'); |
74 | 74 | |
75 | 75 | $this->key = $key; |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | * The CEK MUST have a length equal to that required for the |
58 | 58 | * content encryption algorithm. |
59 | 59 | */ |
60 | - $rnd = $generator->invoke($enc->getMinKeyLen()/8); |
|
60 | + $rnd = $generator->invoke($enc->getMinKeyLen() / 8); |
|
61 | 61 | $cek = new _ContentEncryptionKey($enc->getName(), 'RAW', $rnd); |
62 | 62 | } |
63 | 63 | break; |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | * @param EncryptionAlgorithm $alg |
86 | 86 | * @return Key |
87 | 87 | */ |
88 | - static public function fromRaw($value, EncryptionAlgorithm $alg){ |
|
88 | + static public function fromRaw($value, EncryptionAlgorithm $alg) { |
|
89 | 89 | return new _ContentEncryptionKey($alg->getName(), 'RAW', $value); |
90 | 90 | } |
91 | 91 | } |
92 | 92 | \ No newline at end of file |
@@ -44,7 +44,7 @@ |
||
44 | 44 | * @param string $format |
45 | 45 | * @param string $value |
46 | 46 | */ |
47 | - public function __construct($alg, $format, $value){ |
|
47 | + public function __construct($alg, $format, $value) { |
|
48 | 48 | $this->alg = $alg; |
49 | 49 | $this->format = $format; |
50 | 50 | $this->value = $value; |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | protected function __construct(IJWEJOSEHeader $header, IJWSPayloadSpec $payload = null) |
99 | 99 | { |
100 | 100 | $this->header = $header; |
101 | - if(!is_null($payload)) |
|
101 | + if (!is_null($payload)) |
|
102 | 102 | $this->setPayload($payload); |
103 | 103 | } |
104 | 104 | |
@@ -184,7 +184,7 @@ discard block |
||
184 | 184 | * the result be the JWE Encrypted Key. |
185 | 185 | */ |
186 | 186 | $key_management_mode = $this->getKeyManagementMode($alg); |
187 | - switch($key_management_mode){ |
|
187 | + switch ($key_management_mode) { |
|
188 | 188 | case KeyManagementModeValues::KeyEncryption: |
189 | 189 | case KeyManagementModeValues::KeyWrapping: |
190 | 190 | case KeyManagementModeValues::KeyAgreementWithKeyWrapping: |
@@ -212,15 +212,15 @@ discard block |
||
212 | 212 | */ |
213 | 213 | private function getKeyManagementMode(EncryptionAlgorithm $alg) |
214 | 214 | { |
215 | - if($alg instanceof KeyEncryption) |
|
215 | + if ($alg instanceof KeyEncryption) |
|
216 | 216 | return KeyManagementModeValues::KeyEncryption; |
217 | - if($alg instanceof KeyWrapping) |
|
217 | + if ($alg instanceof KeyWrapping) |
|
218 | 218 | return KeyManagementModeValues::KeyWrapping; |
219 | - if($alg instanceof DirectKeyAgreement) |
|
219 | + if ($alg instanceof DirectKeyAgreement) |
|
220 | 220 | return KeyManagementModeValues::DirectKeyAgreement; |
221 | - if($alg instanceof KeyAgreementWithKeyWrapping) |
|
221 | + if ($alg instanceof KeyAgreementWithKeyWrapping) |
|
222 | 222 | return KeyManagementModeValues::KeyAgreementWithKeyWrapping; |
223 | - if($alg instanceof DirectEncryption) |
|
223 | + if ($alg instanceof DirectEncryption) |
|
224 | 224 | return KeyManagementModeValues::DirectEncryption; |
225 | 225 | } |
226 | 226 | |
@@ -239,11 +239,9 @@ discard block |
||
239 | 239 | if (is_null($this->jwk)) |
240 | 240 | throw new JWEInvalidRecipientKeyException; |
241 | 241 | |
242 | - if($this->jwk->getAlgorithm()->getValue()!== $this->header->getAlgorithm()->getString()) |
|
243 | - throw new InvalidJWKAlgorithm |
|
244 | - ( |
|
245 | - sprintf |
|
246 | - ( |
|
242 | + if ($this->jwk->getAlgorithm()->getValue() !== $this->header->getAlgorithm()->getString()) |
|
243 | + throw new InvalidJWKAlgorithm( |
|
244 | + sprintf( |
|
247 | 245 | 'mismatch between algorithm intended for use with the key %s and the cryptographic algorithm used to encrypt or determine the value of the CEK %s', |
248 | 246 | $this->jwk->getAlgorithm()->getValue(), |
249 | 247 | $this->header->getAlgorithm()->getString() |
@@ -257,27 +255,22 @@ discard block |
||
257 | 255 | if (is_null($key_management_algorithm)) |
258 | 256 | throw new JWEUnsupportedKeyManagementAlgorithmException(sprintf('alg %s', $this->header->getAlgorithm()->getString())); |
259 | 257 | |
260 | - if($key_management_algorithm->getKeyType() !== $recipient_public_key->getAlgorithm()) |
|
261 | - throw new InvalidKeyTypeAlgorithmException |
|
262 | - ( |
|
263 | - sprintf |
|
264 | - ( |
|
258 | + if ($key_management_algorithm->getKeyType() !== $recipient_public_key->getAlgorithm()) |
|
259 | + throw new InvalidKeyTypeAlgorithmException( |
|
260 | + sprintf( |
|
265 | 261 | 'key should be for alg %s, %s instead.', |
266 | 262 | $key_management_algorithm->getKeyType(), |
267 | 263 | $recipient_public_key->getAlgorithm() |
268 | 264 | ) |
269 | 265 | ); |
270 | 266 | |
271 | - $content_encryption_algorithm = ContentEncryptionAlgorithms_Registry::getInstance()->get |
|
272 | - ( |
|
267 | + $content_encryption_algorithm = ContentEncryptionAlgorithms_Registry::getInstance()->get( |
|
273 | 268 | $this->header->getEncryptionAlgorithm()->getString() |
274 | 269 | ); |
275 | 270 | |
276 | 271 | if (is_null($content_encryption_algorithm)) |
277 | - throw new JWEUnsupportedContentEncryptionAlgorithmException |
|
278 | - ( |
|
279 | - sprintf |
|
280 | - ( |
|
272 | + throw new JWEUnsupportedContentEncryptionAlgorithmException( |
|
273 | + sprintf( |
|
281 | 274 | 'enc %s', |
282 | 275 | $this->header->getEncryptionAlgorithm()->getString() |
283 | 276 | ) |
@@ -285,8 +278,7 @@ discard block |
||
285 | 278 | |
286 | 279 | $key_management_mode = $this->getKeyManagementMode($key_management_algorithm); |
287 | 280 | |
288 | - $this->cek = ContentEncryptionKeyFactory::build |
|
289 | - ( |
|
281 | + $this->cek = ContentEncryptionKeyFactory::build( |
|
290 | 282 | $recipient_public_key, |
291 | 283 | $key_management_mode, |
292 | 284 | $content_encryption_algorithm |
@@ -300,7 +292,7 @@ discard block |
||
300 | 292 | * algorithm); otherwise, let the JWE Initialization Vector be the |
301 | 293 | * empty octet sequence. |
302 | 294 | */ |
303 | - $this->iv = ''; |
|
295 | + $this->iv = ''; |
|
304 | 296 | |
305 | 297 | if (!is_null($iv_size = $content_encryption_algorithm->getIVSize())) |
306 | 298 | { |
@@ -317,10 +309,10 @@ discard block |
||
317 | 309 | * sequence representing the compressed plaintext; otherwise, let M |
318 | 310 | * be the octet sequence representing the plaintext. |
319 | 311 | */ |
320 | - if(!is_null($zip)) |
|
312 | + if (!is_null($zip)) |
|
321 | 313 | { |
322 | 314 | $compression__algorithm = CompressionAlgorithms_Registry::getInstance()->get($zip->getValue()); |
323 | - $payload = $compression__algorithm->compress($payload); |
|
315 | + $payload = $compression__algorithm->compress($payload); |
|
324 | 316 | } |
325 | 317 | |
326 | 318 | /** |
@@ -330,8 +322,7 @@ discard block |
||
330 | 322 | * JWE Authentication Tag (which is the Authentication Tag output |
331 | 323 | * from the encryption operation). |
332 | 324 | */ |
333 | - list($this->cipher_text, $this->tag) = $content_encryption_algorithm->encrypt |
|
334 | - ( |
|
325 | + list($this->cipher_text, $this->tag) = $content_encryption_algorithm->encrypt( |
|
335 | 326 | $payload, |
336 | 327 | $this->cek->getEncoded(), |
337 | 328 | $this->iv, |
@@ -349,23 +340,21 @@ discard block |
||
349 | 340 | * @throws InvalidKeyTypeAlgorithmException |
350 | 341 | * @throws \Exception |
351 | 342 | */ |
352 | - private function decryptJWEEncryptedKey(EncryptionAlgorithm $alg){ |
|
343 | + private function decryptJWEEncryptedKey(EncryptionAlgorithm $alg) { |
|
353 | 344 | |
354 | 345 | $key_management_mode = $this->getKeyManagementMode($alg); |
355 | 346 | $recipient_private_key = $this->jwk->getKey(JSONWebKeyKeyOperationsValues::DecryptContentAndValidateDecryption); |
356 | 347 | |
357 | - if($alg->getKeyType() !== $recipient_private_key->getAlgorithm()) |
|
358 | - throw new InvalidKeyTypeAlgorithmException |
|
359 | - ( |
|
360 | - sprintf |
|
361 | - ( |
|
348 | + if ($alg->getKeyType() !== $recipient_private_key->getAlgorithm()) |
|
349 | + throw new InvalidKeyTypeAlgorithmException( |
|
350 | + sprintf( |
|
362 | 351 | 'key should be for alg %s, %s instead.', |
363 | 352 | $alg->getKeyType(), |
364 | 353 | $recipient_private_key->getAlgorithm() |
365 | 354 | ) |
366 | 355 | ); |
367 | 356 | |
368 | - switch($key_management_mode){ |
|
357 | + switch ($key_management_mode) { |
|
369 | 358 | /** |
370 | 359 | * When Key Wrapping, Key Encryption, or Key Agreement with Key |
371 | 360 | * Wrapping are employed, decrypt the JWE Encrypted Key to produce |
@@ -387,12 +376,12 @@ discard block |
||
387 | 376 | * symmetric key. |
388 | 377 | */ |
389 | 378 | case KeyManagementModeValues::DirectEncryption: |
390 | - if(!empty($this->enc_cek)) |
|
379 | + if (!empty($this->enc_cek)) |
|
391 | 380 | throw new JWEInvalidCompactFormatException('JWE Encrypted Key value is not an empty octetsequence.'); |
392 | 381 | return $recipient_private_key; |
393 | 382 | break; |
394 | 383 | case KeyManagementModeValues::DirectKeyAgreement: |
395 | - if(!empty($this->enc_cek)) |
|
384 | + if (!empty($this->enc_cek)) |
|
396 | 385 | throw new JWEInvalidCompactFormatException('JWE Encrypted Key value is not an empty octetsequence.'); |
397 | 386 | throw new \Exception('unsupported Key Management Mode!'); |
398 | 387 | break; |
@@ -417,42 +406,34 @@ discard block |
||
417 | 406 | |
418 | 407 | if (!$this->should_decrypt) return $this; |
419 | 408 | |
420 | - if($this->jwk->getAlgorithm()->getValue()!== $this->header->getAlgorithm()->getString()) |
|
421 | - throw new InvalidJWKAlgorithm |
|
422 | - ( |
|
423 | - sprintf |
|
424 | - ( |
|
409 | + if ($this->jwk->getAlgorithm()->getValue() !== $this->header->getAlgorithm()->getString()) |
|
410 | + throw new InvalidJWKAlgorithm( |
|
411 | + sprintf( |
|
425 | 412 | 'mismatch between algorithm intended for use with the key %s and the cryptographic algorithm used to encrypt or determine the value of the CEK %s', |
426 | 413 | $this->jwk->getAlgorithm()->getValue(), |
427 | 414 | $this->header->getAlgorithm()->getString() |
428 | 415 | ) |
429 | 416 | ); |
430 | 417 | |
431 | - $key_management_algorithm = KeyManagementAlgorithms_Registry::getInstance()->get |
|
432 | - ( |
|
418 | + $key_management_algorithm = KeyManagementAlgorithms_Registry::getInstance()->get( |
|
433 | 419 | $this->header->getAlgorithm()->getString() |
434 | 420 | ); |
435 | 421 | |
436 | 422 | if (is_null($key_management_algorithm)) |
437 | - throw new JWEUnsupportedKeyManagementAlgorithmException |
|
438 | - ( |
|
439 | - sprintf |
|
440 | - ( |
|
423 | + throw new JWEUnsupportedKeyManagementAlgorithmException( |
|
424 | + sprintf( |
|
441 | 425 | 'alg %s', |
442 | 426 | $this->header->getAlgorithm()->getString() |
443 | 427 | ) |
444 | 428 | ); |
445 | 429 | |
446 | - $content_encryption_algorithm = ContentEncryptionAlgorithms_Registry::getInstance()->get |
|
447 | - ( |
|
430 | + $content_encryption_algorithm = ContentEncryptionAlgorithms_Registry::getInstance()->get( |
|
448 | 431 | $this->header->getEncryptionAlgorithm()->getString() |
449 | 432 | ); |
450 | 433 | |
451 | 434 | if (is_null($content_encryption_algorithm)) |
452 | - throw new JWEUnsupportedContentEncryptionAlgorithmException |
|
453 | - ( |
|
454 | - sprintf |
|
455 | - ( |
|
435 | + throw new JWEUnsupportedContentEncryptionAlgorithmException( |
|
436 | + sprintf( |
|
456 | 437 | 'enc %s', |
457 | 438 | $this->header->getEncryptionAlgorithm()->getString() |
458 | 439 | ) |
@@ -473,8 +454,7 @@ discard block |
||
473 | 454 | * rejecting the input without emitting any decrypted output if the |
474 | 455 | * JWE Authentication Tag is incorrect. |
475 | 456 | */ |
476 | - $plain_text = $content_encryption_algorithm->decrypt |
|
477 | - ( |
|
457 | + $plain_text = $content_encryption_algorithm->decrypt( |
|
478 | 458 | $this->cipher_text, |
479 | 459 | $this->cek->getEncoded(), |
480 | 460 | $this->iv, |
@@ -482,12 +462,12 @@ discard block |
||
482 | 462 | $this->tag |
483 | 463 | ); |
484 | 464 | |
485 | - $zip = $this->header->getCompressionAlgorithm(); |
|
465 | + $zip = $this->header->getCompressionAlgorithm(); |
|
486 | 466 | /** |
487 | 467 | * If a "zip" parameter was included, uncompress the decrypted |
488 | 468 | * plaintext using the specified compression algorithm. |
489 | 469 | */ |
490 | - if(!is_null($zip)) |
|
470 | + if (!is_null($zip)) |
|
491 | 471 | { |
492 | 472 | $compression__algorithm = CompressionAlgorithms_Registry::getInstance()->get($zip->getValue()); |
493 | 473 | $plain_text = $compression__algorithm->uncompress($plain_text); |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | const CompressionAlgorithm = 'zip'; |
65 | 65 | |
66 | 66 | |
67 | - public static $registered_basic_headers_set = array ( |
|
67 | + public static $registered_basic_headers_set = array( |
|
68 | 68 | self::Algorithm, |
69 | 69 | self::EncryptionAlgorithm, |
70 | 70 | self::KeyID, |
@@ -73,8 +73,8 @@ discard block |
||
73 | 73 | self::ContentType, |
74 | 74 | ); |
75 | 75 | |
76 | - public static $registered_basic_headers_set_types = array ( |
|
77 | - self::Algorithm => JsonTypes::StringOrURI , |
|
76 | + public static $registered_basic_headers_set_types = array( |
|
77 | + self::Algorithm => JsonTypes::StringOrURI, |
|
78 | 78 | self::Type => JsonTypes::StringOrURI, |
79 | 79 | self::ContentType => JsonTypes::StringOrURI, |
80 | 80 | self::KeyID => JsonTypes::JsonValue, |
@@ -24,9 +24,9 @@ |
||
24 | 24 | |
25 | 25 | const KeyWrapping = 'wrap'; |
26 | 26 | |
27 | - const DirectKeyAgreement ='agree'; |
|
27 | + const DirectKeyAgreement = 'agree'; |
|
28 | 28 | |
29 | - const KeyAgreementWithKeyWrapping ='agree_wrap'; |
|
29 | + const KeyAgreementWithKeyWrapping = 'agree_wrap'; |
|
30 | 30 | |
31 | 31 | const DirectEncryption = 'dir'; |
32 | 32 | } |
33 | 33 | \ No newline at end of file |
@@ -37,41 +37,36 @@ discard block |
||
37 | 37 | static public function build(IJWE_Specification $spec) |
38 | 38 | { |
39 | 39 | |
40 | - if($spec instanceof IJWE_ParamsSpecification) |
|
40 | + if ($spec instanceof IJWE_ParamsSpecification) |
|
41 | 41 | { |
42 | 42 | |
43 | - if($spec->getRecipientKey()->getKeyUse()->getString() !== JSONWebKeyPublicKeyUseValues::Encryption) |
|
44 | - throw new InvalidJWKType |
|
45 | - ( |
|
46 | - sprintf |
|
47 | - ( |
|
43 | + if ($spec->getRecipientKey()->getKeyUse()->getString() !== JSONWebKeyPublicKeyUseValues::Encryption) |
|
44 | + throw new InvalidJWKType( |
|
45 | + sprintf( |
|
48 | 46 | 'use %s not supported (should be "enc")', |
49 | 47 | $spec->getRecipientKey()->getKeyUse()->getString() |
50 | 48 | ) |
51 | 49 | ); |
52 | 50 | |
53 | - if($spec->getAlg()->getString() !== $spec->getRecipientKey()->getAlgorithm()->getString()) |
|
54 | - throw new InvalidJWKAlgorithm |
|
55 | - ( |
|
56 | - sprintf |
|
57 | - ( |
|
51 | + if ($spec->getAlg()->getString() !== $spec->getRecipientKey()->getAlgorithm()->getString()) |
|
52 | + throw new InvalidJWKAlgorithm( |
|
53 | + sprintf( |
|
58 | 54 | 'mismatch between algorithm intended for use with the key %s and the cryptographic algorithm used to encrypt or determine the value of the CEK %s', |
59 | 55 | $spec->getAlg()->getString(), |
60 | 56 | $spec->getRecipientKey()->getAlgorithm()->getString() |
61 | 57 | ) |
62 | 58 | ); |
63 | 59 | |
64 | - $header = new JWEJOSEHeader |
|
65 | - ( |
|
60 | + $header = new JWEJOSEHeader( |
|
66 | 61 | $spec->getAlg(), |
67 | 62 | $spec->getEnc(), |
68 | 63 | $spec->getRecipientKey()->getId() |
69 | 64 | ); |
70 | 65 | |
71 | 66 | //set zip alg |
72 | - $zip = $spec->getZip(); |
|
67 | + $zip = $spec->getZip(); |
|
73 | 68 | |
74 | - if(!is_null($zip)) |
|
69 | + if (!is_null($zip)) |
|
75 | 70 | $header->setCompressionAlgorithm($zip); |
76 | 71 | |
77 | 72 | $jwe = JWE::fromHeaderAndPayload($header, $spec->getPayload()); |
@@ -81,7 +76,7 @@ discard block |
||
81 | 76 | return $jwe; |
82 | 77 | } |
83 | 78 | |
84 | - if($spec instanceof IJWE_CompactFormatSpecification) |
|
79 | + if ($spec instanceof IJWE_CompactFormatSpecification) |
|
85 | 80 | { |
86 | 81 | return JWE::fromCompactSerialization($spec->getCompactFormat()); |
87 | 82 | } |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | |
32 | 32 | private $algorithms = array(); |
33 | 33 | |
34 | - private function __construct(){ |
|
34 | + private function __construct() { |
|
35 | 35 | |
36 | 36 | $this->algorithms[CompressionAlgorithmsNames::Deflate] = new Deflate; |
37 | 37 | $this->algorithms[CompressionAlgorithmsNames::GZip] = new GZip; |
@@ -39,13 +39,13 @@ discard block |
||
39 | 39 | |
40 | 40 | } |
41 | 41 | |
42 | - private function __clone(){} |
|
42 | + private function __clone() {} |
|
43 | 43 | |
44 | 44 | /** |
45 | 45 | * @return CompressionAlgorithms_Registry |
46 | 46 | */ |
47 | - public static function getInstance(){ |
|
48 | - if(!is_object(self::$instance)){ |
|
47 | + public static function getInstance() { |
|
48 | + if (!is_object(self::$instance)) { |
|
49 | 49 | self::$instance = new CompressionAlgorithms_Registry(); |
50 | 50 | } |
51 | 51 | return self::$instance; |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | * @param string $alg |
56 | 56 | * @return bool |
57 | 57 | */ |
58 | - public function isSupported($alg){ |
|
58 | + public function isSupported($alg) { |
|
59 | 59 | return array_key_exists($alg, $this->algorithms); |
60 | 60 | } |
61 | 61 | |
@@ -63,8 +63,8 @@ discard block |
||
63 | 63 | * @param $alg |
64 | 64 | * @return null|CompressionAlgorithm |
65 | 65 | */ |
66 | - public function get($alg){ |
|
67 | - if(!$this->isSupported($alg)) return null; |
|
66 | + public function get($alg) { |
|
67 | + if (!$this->isSupported($alg)) return null; |
|
68 | 68 | return $this->algorithms[$alg]; |
69 | 69 | } |
70 | 70 | } |
71 | 71 | \ No newline at end of file |
@@ -27,7 +27,7 @@ |
||
27 | 27 | /** |
28 | 28 | * @param string $secret |
29 | 29 | */ |
30 | - public function __construct($secret){ |
|
30 | + public function __construct($secret) { |
|
31 | 31 | $this->secret = $secret; |
32 | 32 | } |
33 | 33 |