@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | protected function __construct(IJWEJOSEHeader $header, IJWSPayloadSpec $payload = null) |
| 98 | 98 | { |
| 99 | 99 | $this->header = $header; |
| 100 | - if(!is_null($payload)) |
|
| 100 | + if (!is_null($payload)) |
|
| 101 | 101 | $this->setPayload($payload); |
| 102 | 102 | } |
| 103 | 103 | |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | if (is_null($this->payload)) |
| 158 | 158 | $this->payload = JWSPayloadFactory::build(''); |
| 159 | 159 | |
| 160 | - return ($this->payload instanceof IJWSPayloadRawSpec) ? $this->payload->getRaw():''; |
|
| 160 | + return ($this->payload instanceof IJWSPayloadRawSpec) ? $this->payload->getRaw() : ''; |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | /** |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | * the result be the JWE Encrypted Key. |
| 183 | 183 | */ |
| 184 | 184 | $key_management_mode = $this->getKeyManagementMode($alg); |
| 185 | - switch($key_management_mode){ |
|
| 185 | + switch ($key_management_mode) { |
|
| 186 | 186 | case KeyManagementModeValues::KeyEncryption: |
| 187 | 187 | case KeyManagementModeValues::KeyWrapping: |
| 188 | 188 | case KeyManagementModeValues::KeyAgreementWithKeyWrapping: |
@@ -208,15 +208,15 @@ discard block |
||
| 208 | 208 | */ |
| 209 | 209 | private function getKeyManagementMode(EncryptionAlgorithm $alg) |
| 210 | 210 | { |
| 211 | - if($alg instanceof KeyEncryption) |
|
| 211 | + if ($alg instanceof KeyEncryption) |
|
| 212 | 212 | return KeyManagementModeValues::KeyEncryption; |
| 213 | - if($alg instanceof KeyWrapping) |
|
| 213 | + if ($alg instanceof KeyWrapping) |
|
| 214 | 214 | return KeyManagementModeValues::KeyWrapping; |
| 215 | - if($alg instanceof DirectKeyAgreement) |
|
| 215 | + if ($alg instanceof DirectKeyAgreement) |
|
| 216 | 216 | return KeyManagementModeValues::DirectKeyAgreement; |
| 217 | - if($alg instanceof KeyAgreementWithKeyWrapping) |
|
| 217 | + if ($alg instanceof KeyAgreementWithKeyWrapping) |
|
| 218 | 218 | return KeyManagementModeValues::KeyAgreementWithKeyWrapping; |
| 219 | - if($alg instanceof DirectEncryption) |
|
| 219 | + if ($alg instanceof DirectEncryption) |
|
| 220 | 220 | return KeyManagementModeValues::DirectEncryption; |
| 221 | 221 | } |
| 222 | 222 | |
@@ -235,11 +235,9 @@ discard block |
||
| 235 | 235 | if (is_null($this->jwk)) |
| 236 | 236 | throw new JWEInvalidRecipientKeyException; |
| 237 | 237 | |
| 238 | - if($this->jwk->getAlgorithm()->getValue()!== $this->header->getAlgorithm()->getString()) |
|
| 239 | - throw new InvalidJWKAlgorithm |
|
| 240 | - ( |
|
| 241 | - sprintf |
|
| 242 | - ( |
|
| 238 | + if ($this->jwk->getAlgorithm()->getValue() !== $this->header->getAlgorithm()->getString()) |
|
| 239 | + throw new InvalidJWKAlgorithm( |
|
| 240 | + sprintf( |
|
| 243 | 241 | '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', |
| 244 | 242 | $this->jwk->getAlgorithm()->getValue(), |
| 245 | 243 | $this->header->getAlgorithm()->getString() |
@@ -253,27 +251,22 @@ discard block |
||
| 253 | 251 | if (is_null($key_management_algorithm)) |
| 254 | 252 | throw new JWEUnsupportedKeyManagementAlgorithmException(sprintf('alg %s', $this->header->getAlgorithm()->getString())); |
| 255 | 253 | |
| 256 | - if($key_management_algorithm->getKeyType() !== $recipient_public_key->getAlgorithm()) |
|
| 257 | - throw new InvalidKeyTypeAlgorithmException |
|
| 258 | - ( |
|
| 259 | - sprintf |
|
| 260 | - ( |
|
| 254 | + if ($key_management_algorithm->getKeyType() !== $recipient_public_key->getAlgorithm()) |
|
| 255 | + throw new InvalidKeyTypeAlgorithmException( |
|
| 256 | + sprintf( |
|
| 261 | 257 | 'key should be for alg %s, %s instead.', |
| 262 | 258 | $key_management_algorithm->getKeyType(), |
| 263 | 259 | $recipient_public_key->getAlgorithm() |
| 264 | 260 | ) |
| 265 | 261 | ); |
| 266 | 262 | |
| 267 | - $content_encryption_algorithm = ContentEncryptionAlgorithms_Registry::getInstance()->get |
|
| 268 | - ( |
|
| 263 | + $content_encryption_algorithm = ContentEncryptionAlgorithms_Registry::getInstance()->get( |
|
| 269 | 264 | $this->header->getEncryptionAlgorithm()->getString() |
| 270 | 265 | ); |
| 271 | 266 | |
| 272 | 267 | if (is_null($content_encryption_algorithm)) |
| 273 | - throw new JWEUnsupportedContentEncryptionAlgorithmException |
|
| 274 | - ( |
|
| 275 | - sprintf |
|
| 276 | - ( |
|
| 268 | + throw new JWEUnsupportedContentEncryptionAlgorithmException( |
|
| 269 | + sprintf( |
|
| 277 | 270 | 'enc %s', |
| 278 | 271 | $this->header->getEncryptionAlgorithm()->getString() |
| 279 | 272 | ) |
@@ -281,8 +274,7 @@ discard block |
||
| 281 | 274 | |
| 282 | 275 | $key_management_mode = $this->getKeyManagementMode($key_management_algorithm); |
| 283 | 276 | |
| 284 | - $this->cek = ContentEncryptionKeyFactory::build |
|
| 285 | - ( |
|
| 277 | + $this->cek = ContentEncryptionKeyFactory::build( |
|
| 286 | 278 | $recipient_public_key, |
| 287 | 279 | $key_management_mode, |
| 288 | 280 | $content_encryption_algorithm |
@@ -296,7 +288,7 @@ discard block |
||
| 296 | 288 | * algorithm); otherwise, let the JWE Initialization Vector be the |
| 297 | 289 | * empty octet sequence. |
| 298 | 290 | */ |
| 299 | - $this->iv = ''; |
|
| 291 | + $this->iv = ''; |
|
| 300 | 292 | |
| 301 | 293 | if (!is_null($iv_size = $content_encryption_algorithm->getIVSize())) |
| 302 | 294 | { |
@@ -305,7 +297,7 @@ discard block |
||
| 305 | 297 | // We encrypt the payload and get the tag |
| 306 | 298 | $jwt_shared_protected_header = JOSEHeaderSerializer::serialize($this->header); |
| 307 | 299 | |
| 308 | - $payload = ($this->payload instanceof IJWSPayloadRawSpec) ? $this->payload->getRaw():''; |
|
| 300 | + $payload = ($this->payload instanceof IJWSPayloadRawSpec) ? $this->payload->getRaw() : ''; |
|
| 309 | 301 | $zip = $this->header->getCompressionAlgorithm(); |
| 310 | 302 | /** |
| 311 | 303 | * If a "zip" parameter was included, compress the plaintext using |
@@ -313,10 +305,10 @@ discard block |
||
| 313 | 305 | * sequence representing the compressed plaintext; otherwise, let M |
| 314 | 306 | * be the octet sequence representing the plaintext. |
| 315 | 307 | */ |
| 316 | - if(!is_null($zip)) |
|
| 308 | + if (!is_null($zip)) |
|
| 317 | 309 | { |
| 318 | 310 | $compression__algorithm = CompressionAlgorithms_Registry::getInstance()->get($zip->getValue()); |
| 319 | - $payload = $compression__algorithm->compress($payload); |
|
| 311 | + $payload = $compression__algorithm->compress($payload); |
|
| 320 | 312 | } |
| 321 | 313 | |
| 322 | 314 | /** |
@@ -326,8 +318,7 @@ discard block |
||
| 326 | 318 | * JWE Authentication Tag (which is the Authentication Tag output |
| 327 | 319 | * from the encryption operation). |
| 328 | 320 | */ |
| 329 | - list($this->cipher_text, $this->tag) = $content_encryption_algorithm->encrypt |
|
| 330 | - ( |
|
| 321 | + list($this->cipher_text, $this->tag) = $content_encryption_algorithm->encrypt( |
|
| 331 | 322 | $payload, |
| 332 | 323 | $this->cek->getEncoded(), |
| 333 | 324 | $this->iv, |
@@ -345,23 +336,21 @@ discard block |
||
| 345 | 336 | * @throws InvalidKeyTypeAlgorithmException |
| 346 | 337 | * @throws \Exception |
| 347 | 338 | */ |
| 348 | - private function decryptJWEEncryptedKey(EncryptionAlgorithm $alg){ |
|
| 339 | + private function decryptJWEEncryptedKey(EncryptionAlgorithm $alg) { |
|
| 349 | 340 | |
| 350 | 341 | $key_management_mode = $this->getKeyManagementMode($alg); |
| 351 | 342 | $recipient_private_key = $this->jwk->getKey(JSONWebKeyKeyOperationsValues::DecryptContentAndValidateDecryption); |
| 352 | 343 | |
| 353 | - if($alg->getKeyType() !== $recipient_private_key->getAlgorithm()) |
|
| 354 | - throw new InvalidKeyTypeAlgorithmException |
|
| 355 | - ( |
|
| 356 | - sprintf |
|
| 357 | - ( |
|
| 344 | + if ($alg->getKeyType() !== $recipient_private_key->getAlgorithm()) |
|
| 345 | + throw new InvalidKeyTypeAlgorithmException( |
|
| 346 | + sprintf( |
|
| 358 | 347 | 'key should be for alg %s, %s instead.', |
| 359 | 348 | $alg->getKeyType(), |
| 360 | 349 | $recipient_private_key->getAlgorithm() |
| 361 | 350 | ) |
| 362 | 351 | ); |
| 363 | 352 | |
| 364 | - switch($key_management_mode){ |
|
| 353 | + switch ($key_management_mode) { |
|
| 365 | 354 | /** |
| 366 | 355 | * When Key Wrapping, Key Encryption, or Key Agreement with Key |
| 367 | 356 | * Wrapping are employed, decrypt the JWE Encrypted Key to produce |
@@ -414,42 +403,34 @@ discard block |
||
| 414 | 403 | |
| 415 | 404 | if (!$this->should_decrypt) return $this; |
| 416 | 405 | |
| 417 | - if($this->jwk->getAlgorithm()->getValue()!== $this->header->getAlgorithm()->getString()) |
|
| 418 | - throw new InvalidJWKAlgorithm |
|
| 419 | - ( |
|
| 420 | - sprintf |
|
| 421 | - ( |
|
| 406 | + if ($this->jwk->getAlgorithm()->getValue() !== $this->header->getAlgorithm()->getString()) |
|
| 407 | + throw new InvalidJWKAlgorithm( |
|
| 408 | + sprintf( |
|
| 422 | 409 | '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', |
| 423 | 410 | $this->jwk->getAlgorithm()->getValue(), |
| 424 | 411 | $this->header->getAlgorithm()->getString() |
| 425 | 412 | ) |
| 426 | 413 | ); |
| 427 | 414 | |
| 428 | - $key_management_algorithm = KeyManagementAlgorithms_Registry::getInstance()->get |
|
| 429 | - ( |
|
| 415 | + $key_management_algorithm = KeyManagementAlgorithms_Registry::getInstance()->get( |
|
| 430 | 416 | $this->header->getAlgorithm()->getString() |
| 431 | 417 | ); |
| 432 | 418 | |
| 433 | 419 | if (is_null($key_management_algorithm)) |
| 434 | - throw new JWEUnsupportedKeyManagementAlgorithmException |
|
| 435 | - ( |
|
| 436 | - sprintf |
|
| 437 | - ( |
|
| 420 | + throw new JWEUnsupportedKeyManagementAlgorithmException( |
|
| 421 | + sprintf( |
|
| 438 | 422 | 'alg %s', |
| 439 | 423 | $this->header->getAlgorithm()->getString() |
| 440 | 424 | ) |
| 441 | 425 | ); |
| 442 | 426 | |
| 443 | - $content_encryption_algorithm = ContentEncryptionAlgorithms_Registry::getInstance()->get |
|
| 444 | - ( |
|
| 427 | + $content_encryption_algorithm = ContentEncryptionAlgorithms_Registry::getInstance()->get( |
|
| 445 | 428 | $this->header->getEncryptionAlgorithm()->getString() |
| 446 | 429 | ); |
| 447 | 430 | |
| 448 | 431 | if (is_null($content_encryption_algorithm)) |
| 449 | - throw new JWEUnsupportedContentEncryptionAlgorithmException |
|
| 450 | - ( |
|
| 451 | - sprintf |
|
| 452 | - ( |
|
| 432 | + throw new JWEUnsupportedContentEncryptionAlgorithmException( |
|
| 433 | + sprintf( |
|
| 453 | 434 | 'enc %s', |
| 454 | 435 | $this->header->getEncryptionAlgorithm()->getString() |
| 455 | 436 | ) |
@@ -470,8 +451,7 @@ discard block |
||
| 470 | 451 | * rejecting the input without emitting any decrypted output if the |
| 471 | 452 | * JWE Authentication Tag is incorrect. |
| 472 | 453 | */ |
| 473 | - $plain_text = $content_encryption_algorithm->decrypt |
|
| 474 | - ( |
|
| 454 | + $plain_text = $content_encryption_algorithm->decrypt( |
|
| 475 | 455 | $this->cipher_text, |
| 476 | 456 | $this->cek->getEncoded(), |
| 477 | 457 | $this->iv, |
@@ -479,12 +459,12 @@ discard block |
||
| 479 | 459 | $this->tag |
| 480 | 460 | ); |
| 481 | 461 | |
| 482 | - $zip = $this->header->getCompressionAlgorithm(); |
|
| 462 | + $zip = $this->header->getCompressionAlgorithm(); |
|
| 483 | 463 | /** |
| 484 | 464 | * If a "zip" parameter was included, uncompress the decrypted |
| 485 | 465 | * plaintext using the specified compression algorithm. |
| 486 | 466 | */ |
| 487 | - if(!is_null($zip)) |
|
| 467 | + if (!is_null($zip)) |
|
| 488 | 468 | { |
| 489 | 469 | $compression__algorithm = CompressionAlgorithms_Registry::getInstance()->get($zip->getValue()); |
| 490 | 470 | $plain_text = $compression__algorithm->uncompress($plain_text); |
@@ -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 | |
@@ -31,7 +31,7 @@ |
||
| 31 | 31 | /** |
| 32 | 32 | * @param string $compact_format |
| 33 | 33 | */ |
| 34 | - public function __construct($compact_format){ |
|
| 34 | + public function __construct($compact_format) { |
|
| 35 | 35 | $this->compact_format = $compact_format; |
| 36 | 36 | } |
| 37 | 37 | |
@@ -36,9 +36,9 @@ discard block |
||
| 36 | 36 | * @throws InvalidJWKType |
| 37 | 37 | * @throws InvalidJWKUseException |
| 38 | 38 | */ |
| 39 | - protected function __construct(array $headers = []){ |
|
| 39 | + protected function __construct(array $headers = []) { |
|
| 40 | 40 | |
| 41 | - if(count($headers) === 0 ) return; |
|
| 41 | + if (count($headers) === 0) return; |
|
| 42 | 42 | |
| 43 | 43 | $alg = @$headers[JSONWebKeyParameters::Algorithm]; |
| 44 | 44 | $this->setAlgorithm($alg); |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | */ |
| 81 | 81 | public function setId($kid) |
| 82 | 82 | { |
| 83 | - if(!empty($kid)) |
|
| 83 | + if (!empty($kid)) |
|
| 84 | 84 | $this->set[JSONWebKeyParameters::KeyId] = new JsonValue($kid); |
| 85 | 85 | return $this; |
| 86 | 86 | } |
@@ -92,8 +92,8 @@ discard block |
||
| 92 | 92 | */ |
| 93 | 93 | public function setAlgorithm($alg) |
| 94 | 94 | { |
| 95 | - if(!in_array($alg, JSONWebSignatureAndEncryptionAlgorithms::$header_location_alg)) |
|
| 96 | - throw new InvalidJWKAlgorithm (sprintf('alg %s', $alg)); |
|
| 95 | + if (!in_array($alg, JSONWebSignatureAndEncryptionAlgorithms::$header_location_alg)) |
|
| 96 | + throw new InvalidJWKAlgorithm(sprintf('alg %s', $alg)); |
|
| 97 | 97 | |
| 98 | 98 | $this->set[JSONWebKeyParameters::Algorithm] = new StringOrURI($alg); |
| 99 | 99 | return $this; |
@@ -106,8 +106,8 @@ discard block |
||
| 106 | 106 | */ |
| 107 | 107 | public function setKeyUse($use) |
| 108 | 108 | { |
| 109 | - if(empty($use)) return $this; |
|
| 110 | - if(!in_array($use, JSONWebKeyPublicKeyUseValues::$valid_uses)) |
|
| 109 | + if (empty($use)) return $this; |
|
| 110 | + if (!in_array($use, JSONWebKeyPublicKeyUseValues::$valid_uses)) |
|
| 111 | 111 | throw new InvalidJWKUseException(sprintf('use %s', $use)); |
| 112 | 112 | |
| 113 | 113 | $this->set[JSONWebKeyParameters::PublicKeyUse] = new StringOrURI($use); |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | */ |
| 122 | 122 | public function setType($type) |
| 123 | 123 | { |
| 124 | - if(!in_array($type, JSONWebKeyTypes::$valid_keys_set)) |
|
| 124 | + if (!in_array($type, JSONWebKeyTypes::$valid_keys_set)) |
|
| 125 | 125 | throw new InvalidJWKType(sprintf('use %s', $type)); |
| 126 | 126 | |
| 127 | 127 | $this->set[JSONWebKeyParameters::KeyType] = new StringOrURI($type); |
@@ -38,7 +38,9 @@ discard block |
||
| 38 | 38 | */ |
| 39 | 39 | protected function __construct(array $headers = []){ |
| 40 | 40 | |
| 41 | - if(count($headers) === 0 ) return; |
|
| 41 | + if(count($headers) === 0 ) { |
|
| 42 | + return; |
|
| 43 | + } |
|
| 42 | 44 | |
| 43 | 45 | $alg = @$headers[JSONWebKeyParameters::Algorithm]; |
| 44 | 46 | $this->setAlgorithm($alg); |
@@ -80,8 +82,9 @@ discard block |
||
| 80 | 82 | */ |
| 81 | 83 | public function setId($kid) |
| 82 | 84 | { |
| 83 | - if(!empty($kid)) |
|
| 84 | - $this->set[JSONWebKeyParameters::KeyId] = new JsonValue($kid); |
|
| 85 | + if(!empty($kid)) { |
|
| 86 | + $this->set[JSONWebKeyParameters::KeyId] = new JsonValue($kid); |
|
| 87 | + } |
|
| 85 | 88 | return $this; |
| 86 | 89 | } |
| 87 | 90 | |
@@ -92,8 +95,9 @@ discard block |
||
| 92 | 95 | */ |
| 93 | 96 | public function setAlgorithm($alg) |
| 94 | 97 | { |
| 95 | - if(!in_array($alg, JSONWebSignatureAndEncryptionAlgorithms::$header_location_alg)) |
|
| 96 | - throw new InvalidJWKAlgorithm (sprintf('alg %s', $alg)); |
|
| 98 | + if(!in_array($alg, JSONWebSignatureAndEncryptionAlgorithms::$header_location_alg)) { |
|
| 99 | + throw new InvalidJWKAlgorithm (sprintf('alg %s', $alg)); |
|
| 100 | + } |
|
| 97 | 101 | |
| 98 | 102 | $this->set[JSONWebKeyParameters::Algorithm] = new StringOrURI($alg); |
| 99 | 103 | return $this; |
@@ -106,9 +110,12 @@ discard block |
||
| 106 | 110 | */ |
| 107 | 111 | public function setKeyUse($use) |
| 108 | 112 | { |
| 109 | - if(empty($use)) return $this; |
|
| 110 | - if(!in_array($use, JSONWebKeyPublicKeyUseValues::$valid_uses)) |
|
| 111 | - throw new InvalidJWKUseException(sprintf('use %s', $use)); |
|
| 113 | + if(empty($use)) { |
|
| 114 | + return $this; |
|
| 115 | + } |
|
| 116 | + if(!in_array($use, JSONWebKeyPublicKeyUseValues::$valid_uses)) { |
|
| 117 | + throw new InvalidJWKUseException(sprintf('use %s', $use)); |
|
| 118 | + } |
|
| 112 | 119 | |
| 113 | 120 | $this->set[JSONWebKeyParameters::PublicKeyUse] = new StringOrURI($use); |
| 114 | 121 | return $this; |
@@ -121,8 +128,9 @@ discard block |
||
| 121 | 128 | */ |
| 122 | 129 | public function setType($type) |
| 123 | 130 | { |
| 124 | - if(!in_array($type, JSONWebKeyTypes::$valid_keys_set)) |
|
| 125 | - throw new InvalidJWKType(sprintf('use %s', $type)); |
|
| 131 | + if(!in_array($type, JSONWebKeyTypes::$valid_keys_set)) { |
|
| 132 | + throw new InvalidJWKType(sprintf('use %s', $type)); |
|
| 133 | + } |
|
| 126 | 134 | |
| 127 | 135 | $this->set[JSONWebKeyParameters::KeyType] = new StringOrURI($type); |
| 128 | 136 | return $this; |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | * @param int $byte_len |
| 23 | 23 | * @return int |
| 24 | 24 | */ |
| 25 | - static public function bitLength($byte_len){ |
|
| 25 | + static public function bitLength($byte_len) { |
|
| 26 | 26 | return $byte_len * 8; |
| 27 | 27 | } |
| 28 | 28 | |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | * @param int $byte_len |
| 31 | 31 | * @return String |
| 32 | 32 | */ |
| 33 | - static public function randomBytes($byte_len){ |
|
| 33 | + static public function randomBytes($byte_len) { |
|
| 34 | 34 | return Random::string($byte_len); |
| 35 | 35 | } |
| 36 | 36 | |
@@ -38,10 +38,10 @@ discard block |
||
| 38 | 38 | * @param array $oct |
| 39 | 39 | * @return string |
| 40 | 40 | */ |
| 41 | - static public function convertHalfWordArrayToBin(array $oct){ |
|
| 41 | + static public function convertHalfWordArrayToBin(array $oct) { |
|
| 42 | 42 | $hex = ''; |
| 43 | - foreach($oct as $b){ |
|
| 44 | - $hex .= str_pad(dechex($b),2,'0',STR_PAD_LEFT); |
|
| 43 | + foreach ($oct as $b) { |
|
| 44 | + $hex .= str_pad(dechex($b), 2, '0', STR_PAD_LEFT); |
|
| 45 | 45 | } |
| 46 | 46 | return self::hex2bin($hex); |
| 47 | 47 | } |
@@ -50,15 +50,15 @@ discard block |
||
| 50 | 50 | * @param int $nbr |
| 51 | 51 | * @return string |
| 52 | 52 | */ |
| 53 | - static public function convert2UnsignedLongBE($nbr){ |
|
| 54 | - $hex = str_pad(dechex($nbr),16,'0',STR_PAD_LEFT); |
|
| 53 | + static public function convert2UnsignedLongBE($nbr) { |
|
| 54 | + $hex = str_pad(dechex($nbr), 16, '0', STR_PAD_LEFT); |
|
| 55 | 55 | return self::hex2bin($hex); |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | - static public function hex2bin($hex_string){ |
|
| 59 | - if ( function_exists( 'hex2bin' ) ){ |
|
| 58 | + static public function hex2bin($hex_string) { |
|
| 59 | + if (function_exists('hex2bin')) { |
|
| 60 | 60 | return hex2bin($hex_string); |
| 61 | 61 | } |
| 62 | - return pack("H*" , $hex_string); |
|
| 62 | + return pack("H*", $hex_string); |
|
| 63 | 63 | } |
| 64 | 64 | } |
| 65 | 65 | \ No newline at end of file |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | /** |
| 23 | 23 | * @return BigInteger |
| 24 | 24 | */ |
| 25 | - public function toBigInt(){ |
|
| 25 | + public function toBigInt() { |
|
| 26 | 26 | $b64 = new Base64UrlRepresentation(); |
| 27 | 27 | $hex = bin2hex($b64->decode($this->value)); |
| 28 | 28 | return new BigInteger('0x'.$hex, 16); |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | * @param BigInteger $big_int |
| 33 | 33 | * @return Base64urlUInt |
| 34 | 34 | */ |
| 35 | - public static function fromBigInt(BigInteger $big_int){ |
|
| 35 | + public static function fromBigInt(BigInteger $big_int) { |
|
| 36 | 36 | $b64 = new Base64UrlRepresentation(); |
| 37 | 37 | $input = $big_int->toBytes(); |
| 38 | 38 | return new Base64urlUInt($b64->encode($input)); |
@@ -46,7 +46,7 @@ |
||
| 46 | 46 | * @return array |
| 47 | 47 | * @throws JWEInvalidCompactFormatException |
| 48 | 48 | */ |
| 49 | - static public function deserialize($input){ |
|
| 49 | + static public function deserialize($input) { |
|
| 50 | 50 | $parts = explode(IBasicJWT::SegmentSeparator, $input); |
| 51 | 51 | if (count($parts) !== 5) throw new JWEInvalidCompactFormatException; |
| 52 | 52 | |
@@ -48,7 +48,9 @@ |
||
| 48 | 48 | */ |
| 49 | 49 | static public function deserialize($input){ |
| 50 | 50 | $parts = explode(IBasicJWT::SegmentSeparator, $input); |
| 51 | - if (count($parts) !== 5) throw new JWEInvalidCompactFormatException; |
|
| 51 | + if (count($parts) !== 5) { |
|
| 52 | + throw new JWEInvalidCompactFormatException; |
|
| 53 | + } |
|
| 52 | 54 | |
| 53 | 55 | $header = JWEJOSEHeaderSerializer::deserialize($parts[0]); |
| 54 | 56 | $enc_cek = JWTRawSerializer::deserialize($parts[1]); |
@@ -24,6 +24,6 @@ |
||
| 24 | 24 | */ |
| 25 | 25 | public function getPaddingMode() |
| 26 | 26 | { |
| 27 | - return RSA::SIGNATURE_PKCS1 ; |
|
| 27 | + return RSA::SIGNATURE_PKCS1; |
|
| 28 | 28 | } |
| 29 | 29 | } |
| 30 | 30 | \ No newline at end of file |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | /** |
| 128 | 128 | * AES Key Wrap using 256-bit key |
| 129 | 129 | */ |
| 130 | - const A256KW= 'A256KW'; |
|
| 130 | + const A256KW = 'A256KW'; |
|
| 131 | 131 | |
| 132 | 132 | /** |
| 133 | 133 | * Direct use of a shared symmetric key |
@@ -222,29 +222,29 @@ discard block |
||
| 222 | 222 | self::RS256, |
| 223 | 223 | self::RS384, |
| 224 | 224 | self::RS512, |
| 225 | - self::ES256 , |
|
| 225 | + self::ES256, |
|
| 226 | 226 | self::ES384, |
| 227 | - self::ES512 , |
|
| 227 | + self::ES512, |
|
| 228 | 228 | self::PS256, |
| 229 | - self::PS384 , |
|
| 229 | + self::PS384, |
|
| 230 | 230 | self::PS512, |
| 231 | 231 | self::None, |
| 232 | 232 | self::RSA1_5, |
| 233 | - self::RSA_OAEP , |
|
| 233 | + self::RSA_OAEP, |
|
| 234 | 234 | self::RSA_OAEP_256, |
| 235 | - self::A128KW , |
|
| 235 | + self::A128KW, |
|
| 236 | 236 | self::A192KW, |
| 237 | 237 | self::A192KW, |
| 238 | 238 | self::A256KW, |
| 239 | 239 | self::Dir, |
| 240 | 240 | self::ECDH_ES, |
| 241 | - self::ECDH_ES_A128KW , |
|
| 241 | + self::ECDH_ES_A128KW, |
|
| 242 | 242 | self::ECDH_ES_A192KW, |
| 243 | - self::ECDH_ES_A256KW , |
|
| 244 | - self::A128GCMKW , |
|
| 243 | + self::ECDH_ES_A256KW, |
|
| 244 | + self::A128GCMKW, |
|
| 245 | 245 | self::A192GCMKW, |
| 246 | - self::A256GCMKW , |
|
| 247 | - self::PBES2_HS256_A128KW , |
|
| 246 | + self::A256GCMKW, |
|
| 247 | + self::PBES2_HS256_A128KW, |
|
| 248 | 248 | self::PBES2_HS384_A192KW, |
| 249 | 249 | self::PBES2_HS512_A256KW, |
| 250 | 250 | ]; |
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | /** |
| 24 | 24 | * @var array |
| 25 | 25 | */ |
| 26 | - private $info = []; |
|
| 26 | + private $info = []; |
|
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * @var X509|null |
@@ -35,11 +35,11 @@ discard block |
||
| 35 | 35 | */ |
| 36 | 36 | private $original_pem = null; |
| 37 | 37 | |
| 38 | - public function __construct($pem){ |
|
| 38 | + public function __construct($pem) { |
|
| 39 | 39 | |
| 40 | 40 | $this->file = new X509(); |
| 41 | 41 | $this->info = $this->file->loadX509($pem); |
| 42 | - if($this->info === false) throw new InvalidX509CertificateException($pem); |
|
| 42 | + if ($this->info === false) throw new InvalidX509CertificateException($pem); |
|
| 43 | 43 | $this->original_pem = $pem; |
| 44 | 44 | } |
| 45 | 45 | |
@@ -51,8 +51,8 @@ discard block |
||
| 51 | 51 | return $this->original_pem; |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | - private function calculateThumbprint($alg){ |
|
| 55 | - $pem = str_replace( array("\n","\r"), '', trim($this->original_pem)); |
|
| 54 | + private function calculateThumbprint($alg) { |
|
| 55 | + $pem = str_replace(array("\n", "\r"), '', trim($this->original_pem)); |
|
| 56 | 56 | return strtoupper(hash($alg, base64_decode($pem))); |
| 57 | 57 | } |
| 58 | 58 | |
@@ -78,9 +78,9 @@ discard block |
||
| 78 | 78 | public function getPublicKey() |
| 79 | 79 | { |
| 80 | 80 | $pem = (string)$this->file->getPublicKey(); |
| 81 | - $pem = preg_replace('/\-+BEGIN PUBLIC KEY\-+/','',$pem); |
|
| 82 | - $pem = preg_replace('/\-+END PUBLIC KEY\-+/','',$pem); |
|
| 83 | - $pem = str_replace( array("\n","\r","\t"), '', trim($pem)); |
|
| 81 | + $pem = preg_replace('/\-+BEGIN PUBLIC KEY\-+/', '', $pem); |
|
| 82 | + $pem = preg_replace('/\-+END PUBLIC KEY\-+/', '', $pem); |
|
| 83 | + $pem = str_replace(array("\n", "\r", "\t"), '', trim($pem)); |
|
| 84 | 84 | return $pem; |
| 85 | 85 | } |
| 86 | 86 | |
@@ -39,7 +39,9 @@ |
||
| 39 | 39 | |
| 40 | 40 | $this->file = new X509(); |
| 41 | 41 | $this->info = $this->file->loadX509($pem); |
| 42 | - if($this->info === false) throw new InvalidX509CertificateException($pem); |
|
| 42 | + if($this->info === false) { |
|
| 43 | + throw new InvalidX509CertificateException($pem); |
|
| 44 | + } |
|
| 43 | 45 | $this->original_pem = $pem; |
| 44 | 46 | } |
| 45 | 47 | |