@@ -18,89 +18,89 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | abstract class HMACAlgorithm extends SignatureAlgorithm |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Mapping from algorithm name to class name. |
|
| 23 | - * |
|
| 24 | - * @internal |
|
| 25 | - * |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - const MAP_ALGO_TO_CLASS = [ |
|
| 29 | - JWA::ALGO_HS256 => HS256Algorithm::class, |
|
| 30 | - JWA::ALGO_HS384 => HS384Algorithm::class, |
|
| 31 | - JWA::ALGO_HS512 => HS512Algorithm::class, |
|
| 32 | - ]; |
|
| 21 | + /** |
|
| 22 | + * Mapping from algorithm name to class name. |
|
| 23 | + * |
|
| 24 | + * @internal |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + const MAP_ALGO_TO_CLASS = [ |
|
| 29 | + JWA::ALGO_HS256 => HS256Algorithm::class, |
|
| 30 | + JWA::ALGO_HS384 => HS384Algorithm::class, |
|
| 31 | + JWA::ALGO_HS512 => HS512Algorithm::class, |
|
| 32 | + ]; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Shared secret key. |
|
| 36 | - * |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - protected $_key; |
|
| 34 | + /** |
|
| 35 | + * Shared secret key. |
|
| 36 | + * |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + protected $_key; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Constructor. |
|
| 43 | - * |
|
| 44 | - * @param string $key Shared secret key |
|
| 45 | - */ |
|
| 46 | - public function __construct(string $key) |
|
| 47 | - { |
|
| 48 | - $this->_key = $key; |
|
| 49 | - } |
|
| 41 | + /** |
|
| 42 | + * Constructor. |
|
| 43 | + * |
|
| 44 | + * @param string $key Shared secret key |
|
| 45 | + */ |
|
| 46 | + public function __construct(string $key) |
|
| 47 | + { |
|
| 48 | + $this->_key = $key; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * {@inheritdoc} |
|
| 53 | - * |
|
| 54 | - * @return self |
|
| 55 | - */ |
|
| 56 | - public static function fromJWK(JWK $jwk, Header $header): SignatureAlgorithm |
|
| 57 | - { |
|
| 58 | - $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
| 59 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 60 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 61 | - throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
| 62 | - } |
|
| 63 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 64 | - return new $cls($jwk->key()); |
|
| 65 | - } |
|
| 51 | + /** |
|
| 52 | + * {@inheritdoc} |
|
| 53 | + * |
|
| 54 | + * @return self |
|
| 55 | + */ |
|
| 56 | + public static function fromJWK(JWK $jwk, Header $header): SignatureAlgorithm |
|
| 57 | + { |
|
| 58 | + $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
| 59 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 60 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 61 | + throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
| 62 | + } |
|
| 63 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 64 | + return new $cls($jwk->key()); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * {@inheritdoc} |
|
| 69 | - * |
|
| 70 | - * @throws \RuntimeException For generic errors |
|
| 71 | - */ |
|
| 72 | - public function computeSignature(string $data): string |
|
| 73 | - { |
|
| 74 | - $result = @hash_hmac($this->_hashAlgo(), $data, $this->_key, true); |
|
| 75 | - if (false === $result) { |
|
| 76 | - $err = error_get_last(); |
|
| 77 | - $msg = isset($err) && __FILE__ === $err['file'] ? $err['message'] : null; |
|
| 78 | - throw new \RuntimeException($msg ?? 'hash_hmac() failed.'); |
|
| 79 | - } |
|
| 80 | - return $result; |
|
| 81 | - } |
|
| 67 | + /** |
|
| 68 | + * {@inheritdoc} |
|
| 69 | + * |
|
| 70 | + * @throws \RuntimeException For generic errors |
|
| 71 | + */ |
|
| 72 | + public function computeSignature(string $data): string |
|
| 73 | + { |
|
| 74 | + $result = @hash_hmac($this->_hashAlgo(), $data, $this->_key, true); |
|
| 75 | + if (false === $result) { |
|
| 76 | + $err = error_get_last(); |
|
| 77 | + $msg = isset($err) && __FILE__ === $err['file'] ? $err['message'] : null; |
|
| 78 | + throw new \RuntimeException($msg ?? 'hash_hmac() failed.'); |
|
| 79 | + } |
|
| 80 | + return $result; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * {@inheritdoc} |
|
| 85 | - */ |
|
| 86 | - public function validateSignature(string $data, string $signature): bool |
|
| 87 | - { |
|
| 88 | - return $this->computeSignature($data) === $signature; |
|
| 89 | - } |
|
| 83 | + /** |
|
| 84 | + * {@inheritdoc} |
|
| 85 | + */ |
|
| 86 | + public function validateSignature(string $data, string $signature): bool |
|
| 87 | + { |
|
| 88 | + return $this->computeSignature($data) === $signature; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * {@inheritdoc} |
|
| 93 | - */ |
|
| 94 | - public function headerParameters(): array |
|
| 95 | - { |
|
| 96 | - return array_merge(parent::headerParameters(), |
|
| 97 | - [AlgorithmParameter::fromAlgorithm($this)]); |
|
| 98 | - } |
|
| 91 | + /** |
|
| 92 | + * {@inheritdoc} |
|
| 93 | + */ |
|
| 94 | + public function headerParameters(): array |
|
| 95 | + { |
|
| 96 | + return array_merge(parent::headerParameters(), |
|
| 97 | + [AlgorithmParameter::fromAlgorithm($this)]); |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * Get algorithm name recognized by the Hash extension. |
|
| 102 | - * |
|
| 103 | - * @return string |
|
| 104 | - */ |
|
| 105 | - abstract protected function _hashAlgo(): string; |
|
| 100 | + /** |
|
| 101 | + * Get algorithm name recognized by the Hash extension. |
|
| 102 | + * |
|
| 103 | + * @return string |
|
| 104 | + */ |
|
| 105 | + abstract protected function _hashAlgo(): string; |
|
| 106 | 106 | } |
@@ -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\JWS\Algorithm; |
| 6 | 6 | |
@@ -14,27 +14,27 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class ES512Algorithm extends ECDSAAlgorithm |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * {@inheritdoc} |
|
| 19 | - */ |
|
| 20 | - public function algorithmParamValue(): string |
|
| 21 | - { |
|
| 22 | - return JWA::ALGO_ES512; |
|
| 23 | - } |
|
| 17 | + /** |
|
| 18 | + * {@inheritdoc} |
|
| 19 | + */ |
|
| 20 | + public function algorithmParamValue(): string |
|
| 21 | + { |
|
| 22 | + return JWA::ALGO_ES512; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * {@inheritdoc} |
|
| 27 | - */ |
|
| 28 | - protected function _curveName(): string |
|
| 29 | - { |
|
| 30 | - return CurveParameter::CURVE_P521; |
|
| 31 | - } |
|
| 25 | + /** |
|
| 26 | + * {@inheritdoc} |
|
| 27 | + */ |
|
| 28 | + protected function _curveName(): string |
|
| 29 | + { |
|
| 30 | + return CurveParameter::CURVE_P521; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * {@inheritdoc} |
|
| 35 | - */ |
|
| 36 | - protected function _mdMethod(): int |
|
| 37 | - { |
|
| 38 | - return OPENSSL_ALGO_SHA512; |
|
| 39 | - } |
|
| 33 | + /** |
|
| 34 | + * {@inheritdoc} |
|
| 35 | + */ |
|
| 36 | + protected function _mdMethod(): int |
|
| 37 | + { |
|
| 38 | + return OPENSSL_ALGO_SHA512; |
|
| 39 | + } |
|
| 40 | 40 | } |
@@ -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\JWS\Algorithm; |
| 6 | 6 | |
@@ -13,19 +13,19 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class HS256Algorithm extends HMACAlgorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - public function algorithmParamValue(): string |
|
| 20 | - { |
|
| 21 | - return JWA::ALGO_HS256; |
|
| 22 | - } |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + public function algorithmParamValue(): string |
|
| 20 | + { |
|
| 21 | + return JWA::ALGO_HS256; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - protected function _hashAlgo(): string |
|
| 28 | - { |
|
| 29 | - return 'sha256'; |
|
| 30 | - } |
|
| 24 | + /** |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + protected function _hashAlgo(): string |
|
| 28 | + { |
|
| 29 | + return 'sha256'; |
|
| 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\JWS\Algorithm; |
| 6 | 6 | |
@@ -13,19 +13,19 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class RS256Algorithm extends RSASSAPKCS1Algorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - public function algorithmParamValue(): string |
|
| 20 | - { |
|
| 21 | - return JWA::ALGO_RS256; |
|
| 22 | - } |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + public function algorithmParamValue(): string |
|
| 20 | + { |
|
| 21 | + return JWA::ALGO_RS256; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - protected function _mdMethod(): int |
|
| 28 | - { |
|
| 29 | - return OPENSSL_ALGO_SHA256; |
|
| 30 | - } |
|
| 24 | + /** |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + protected function _mdMethod(): int |
|
| 28 | + { |
|
| 29 | + return OPENSSL_ALGO_SHA256; |
|
| 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\JWS\Algorithm; |
| 6 | 6 | |
@@ -28,40 +28,40 @@ discard block |
||
| 28 | 28 | */ |
| 29 | 29 | class UUIDv4 |
| 30 | 30 | { |
| 31 | - /** |
|
| 32 | - * UUID. |
|
| 33 | - * |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 36 | - protected $_uuid; |
|
| 31 | + /** |
|
| 32 | + * UUID. |
|
| 33 | + * |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | + protected $_uuid; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Constructor. |
|
| 40 | - * |
|
| 41 | - * @param string $uuid UUIDv4 in canonical hexadecimal format |
|
| 42 | - */ |
|
| 43 | - public function __construct(string $uuid) |
|
| 44 | - { |
|
| 45 | - // @todo Check that UUID is version 4 |
|
| 46 | - $this->_uuid = $uuid; |
|
| 47 | - } |
|
| 38 | + /** |
|
| 39 | + * Constructor. |
|
| 40 | + * |
|
| 41 | + * @param string $uuid UUIDv4 in canonical hexadecimal format |
|
| 42 | + */ |
|
| 43 | + public function __construct(string $uuid) |
|
| 44 | + { |
|
| 45 | + // @todo Check that UUID is version 4 |
|
| 46 | + $this->_uuid = $uuid; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * @return string |
|
| 51 | - */ |
|
| 52 | - public function __toString(): string |
|
| 53 | - { |
|
| 54 | - return $this->canonical(); |
|
| 55 | - } |
|
| 49 | + /** |
|
| 50 | + * @return string |
|
| 51 | + */ |
|
| 52 | + public function __toString(): string |
|
| 53 | + { |
|
| 54 | + return $this->canonical(); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Create new random UUIDv4. |
|
| 59 | - * |
|
| 60 | - * @return self |
|
| 61 | - */ |
|
| 62 | - public static function createRandom(): self |
|
| 63 | - { |
|
| 64 | - /* |
|
| 57 | + /** |
|
| 58 | + * Create new random UUIDv4. |
|
| 59 | + * |
|
| 60 | + * @return self |
|
| 61 | + */ |
|
| 62 | + public static function createRandom(): self |
|
| 63 | + { |
|
| 64 | + /* |
|
| 65 | 65 | 1. Set the two most significant bits (bits 6 and 7) of |
| 66 | 66 | the clock_seq_hi_and_reserved to zero and one, respectively. |
| 67 | 67 | |
@@ -72,29 +72,29 @@ discard block |
||
| 72 | 72 | 3. Set all the other bits to randomly (or pseudo-randomly) |
| 73 | 73 | chosen values. |
| 74 | 74 | */ |
| 75 | - $uuid = sprintf('%04x%04x-%04x-%04x-%02x%02x-%04x%04x%04x', |
|
| 76 | - // time_low |
|
| 77 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
| 78 | - // time_mid |
|
| 79 | - mt_rand(0, 0xffff), |
|
| 80 | - // time_hi_and_version |
|
| 81 | - mt_rand(0, 0x0fff) | 0x4000, |
|
| 82 | - // clk_seq_hi_res |
|
| 83 | - mt_rand(0, 0x3f) | 0x80, |
|
| 84 | - // clk_seq_low |
|
| 85 | - mt_rand(0, 0xff), |
|
| 86 | - // node |
|
| 87 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
|
| 88 | - return new self($uuid); |
|
| 89 | - } |
|
| 75 | + $uuid = sprintf('%04x%04x-%04x-%04x-%02x%02x-%04x%04x%04x', |
|
| 76 | + // time_low |
|
| 77 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
| 78 | + // time_mid |
|
| 79 | + mt_rand(0, 0xffff), |
|
| 80 | + // time_hi_and_version |
|
| 81 | + mt_rand(0, 0x0fff) | 0x4000, |
|
| 82 | + // clk_seq_hi_res |
|
| 83 | + mt_rand(0, 0x3f) | 0x80, |
|
| 84 | + // clk_seq_low |
|
| 85 | + mt_rand(0, 0xff), |
|
| 86 | + // node |
|
| 87 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
|
| 88 | + return new self($uuid); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Get UUIDv4 in canonical form. |
|
| 93 | - * |
|
| 94 | - * @return string |
|
| 95 | - */ |
|
| 96 | - public function canonical(): string |
|
| 97 | - { |
|
| 98 | - return $this->_uuid; |
|
| 99 | - } |
|
| 91 | + /** |
|
| 92 | + * Get UUIDv4 in canonical form. |
|
| 93 | + * |
|
| 94 | + * @return string |
|
| 95 | + */ |
|
| 96 | + public function canonical(): string |
|
| 97 | + { |
|
| 98 | + return $this->_uuid; |
|
| 99 | + } |
|
| 100 | 100 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\Util; |
| 6 | 6 | |
@@ -78,9 +78,9 @@ discard block |
||
| 78 | 78 | // time_mid |
| 79 | 79 | mt_rand(0, 0xffff), |
| 80 | 80 | // time_hi_and_version |
| 81 | - mt_rand(0, 0x0fff) | 0x4000, |
|
| 81 | + mt_rand(0, 0x0fff)|0x4000, |
|
| 82 | 82 | // clk_seq_hi_res |
| 83 | - mt_rand(0, 0x3f) | 0x80, |
|
| 83 | + mt_rand(0, 0x3f)|0x80, |
|
| 84 | 84 | // clk_seq_low |
| 85 | 85 | mt_rand(0, 0xff), |
| 86 | 86 | // node |
@@ -9,87 +9,87 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class BigInt |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Number. |
|
| 14 | - * |
|
| 15 | - * @var \GMP |
|
| 16 | - */ |
|
| 17 | - protected $_num; |
|
| 12 | + /** |
|
| 13 | + * Number. |
|
| 14 | + * |
|
| 15 | + * @var \GMP |
|
| 16 | + */ |
|
| 17 | + protected $_num; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Constructor. |
|
| 21 | - * |
|
| 22 | - * @param \GMP $num GMP number |
|
| 23 | - */ |
|
| 24 | - protected function __construct(\GMP $num) |
|
| 25 | - { |
|
| 26 | - $this->_num = $num; |
|
| 27 | - } |
|
| 19 | + /** |
|
| 20 | + * Constructor. |
|
| 21 | + * |
|
| 22 | + * @param \GMP $num GMP number |
|
| 23 | + */ |
|
| 24 | + protected function __construct(\GMP $num) |
|
| 25 | + { |
|
| 26 | + $this->_num = $num; |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * @return string |
|
| 31 | - */ |
|
| 32 | - public function __toString(): string |
|
| 33 | - { |
|
| 34 | - return $this->base10(); |
|
| 35 | - } |
|
| 29 | + /** |
|
| 30 | + * @return string |
|
| 31 | + */ |
|
| 32 | + public function __toString(): string |
|
| 33 | + { |
|
| 34 | + return $this->base10(); |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Initialize from a base10 number. |
|
| 39 | - * |
|
| 40 | - * @param int|string $number |
|
| 41 | - * |
|
| 42 | - * @return self |
|
| 43 | - */ |
|
| 44 | - public static function fromBase10($number): self |
|
| 45 | - { |
|
| 46 | - $num = gmp_init($number, 10); |
|
| 47 | - return new self($num); |
|
| 48 | - } |
|
| 37 | + /** |
|
| 38 | + * Initialize from a base10 number. |
|
| 39 | + * |
|
| 40 | + * @param int|string $number |
|
| 41 | + * |
|
| 42 | + * @return self |
|
| 43 | + */ |
|
| 44 | + public static function fromBase10($number): self |
|
| 45 | + { |
|
| 46 | + $num = gmp_init($number, 10); |
|
| 47 | + return new self($num); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Initialize from a base256 number. |
|
| 52 | - * |
|
| 53 | - * Base64 number is an octet string of big endian, most significant word |
|
| 54 | - * first integer. |
|
| 55 | - * |
|
| 56 | - * @param string $octets |
|
| 57 | - * |
|
| 58 | - * @return self |
|
| 59 | - */ |
|
| 60 | - public static function fromBase256(string $octets): self |
|
| 61 | - { |
|
| 62 | - $num = gmp_import($octets, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 63 | - return new self($num); |
|
| 64 | - } |
|
| 50 | + /** |
|
| 51 | + * Initialize from a base256 number. |
|
| 52 | + * |
|
| 53 | + * Base64 number is an octet string of big endian, most significant word |
|
| 54 | + * first integer. |
|
| 55 | + * |
|
| 56 | + * @param string $octets |
|
| 57 | + * |
|
| 58 | + * @return self |
|
| 59 | + */ |
|
| 60 | + public static function fromBase256(string $octets): self |
|
| 61 | + { |
|
| 62 | + $num = gmp_import($octets, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 63 | + return new self($num); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Convert to base10 string. |
|
| 68 | - * |
|
| 69 | - * @return string |
|
| 70 | - */ |
|
| 71 | - public function base10(): string |
|
| 72 | - { |
|
| 73 | - return gmp_strval($this->_num, 10); |
|
| 74 | - } |
|
| 66 | + /** |
|
| 67 | + * Convert to base10 string. |
|
| 68 | + * |
|
| 69 | + * @return string |
|
| 70 | + */ |
|
| 71 | + public function base10(): string |
|
| 72 | + { |
|
| 73 | + return gmp_strval($this->_num, 10); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * Convert to base16 string. |
|
| 78 | - * |
|
| 79 | - * @return string |
|
| 80 | - */ |
|
| 81 | - public function base16(): string |
|
| 82 | - { |
|
| 83 | - return gmp_strval($this->_num, 16); |
|
| 84 | - } |
|
| 76 | + /** |
|
| 77 | + * Convert to base16 string. |
|
| 78 | + * |
|
| 79 | + * @return string |
|
| 80 | + */ |
|
| 81 | + public function base16(): string |
|
| 82 | + { |
|
| 83 | + return gmp_strval($this->_num, 16); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Convert to base256 string. |
|
| 88 | - * |
|
| 89 | - * @return string |
|
| 90 | - */ |
|
| 91 | - public function base256(): string |
|
| 92 | - { |
|
| 93 | - return gmp_export($this->_num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 94 | - } |
|
| 86 | + /** |
|
| 87 | + * Convert to base256 string. |
|
| 88 | + * |
|
| 89 | + * @return string |
|
| 90 | + */ |
|
| 91 | + public function base256(): string |
|
| 92 | + { |
|
| 93 | + return gmp_export($this->_num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 94 | + } |
|
| 95 | 95 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\JWX\Util; |
| 6 | 6 | |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | public static function fromBase256(string $octets): self |
| 61 | 61 | { |
| 62 | - $num = gmp_import($octets, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 62 | + $num = gmp_import($octets, 1, GMP_MSW_FIRST|GMP_BIG_ENDIAN); |
|
| 63 | 63 | return new self($num); |
| 64 | 64 | } |
| 65 | 65 | |
@@ -90,6 +90,6 @@ discard block |
||
| 90 | 90 | */ |
| 91 | 91 | public function base256(): string |
| 92 | 92 | { |
| 93 | - return gmp_export($this->_num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 93 | + return gmp_export($this->_num, 1, GMP_MSW_FIRST|GMP_BIG_ENDIAN); |
|
| 94 | 94 | } |
| 95 | 95 | } |
@@ -9,111 +9,111 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Base64 |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Encode a string using base64url variant. |
|
| 14 | - * |
|
| 15 | - * @see https://en.wikipedia.org/wiki/Base64#URL_applications |
|
| 16 | - * |
|
| 17 | - * @param string $data |
|
| 18 | - * |
|
| 19 | - * @return string |
|
| 20 | - */ |
|
| 21 | - public static function urlEncode(string $data): string |
|
| 22 | - { |
|
| 23 | - return strtr(rtrim(self::encode($data), '='), '+/', '-_'); |
|
| 24 | - } |
|
| 12 | + /** |
|
| 13 | + * Encode a string using base64url variant. |
|
| 14 | + * |
|
| 15 | + * @see https://en.wikipedia.org/wiki/Base64#URL_applications |
|
| 16 | + * |
|
| 17 | + * @param string $data |
|
| 18 | + * |
|
| 19 | + * @return string |
|
| 20 | + */ |
|
| 21 | + public static function urlEncode(string $data): string |
|
| 22 | + { |
|
| 23 | + return strtr(rtrim(self::encode($data), '='), '+/', '-_'); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Decode a string using base64url variant. |
|
| 28 | - * |
|
| 29 | - * @see https://en.wikipedia.org/wiki/Base64#URL_applications |
|
| 30 | - * |
|
| 31 | - * @param string $data |
|
| 32 | - * |
|
| 33 | - * @throws \UnexpectedValueException |
|
| 34 | - * |
|
| 35 | - * @return string |
|
| 36 | - */ |
|
| 37 | - public static function urlDecode(string $data): string |
|
| 38 | - { |
|
| 39 | - $data = strtr($data, '-_', '+/'); |
|
| 40 | - switch (strlen($data) % 4) { |
|
| 41 | - case 0: |
|
| 42 | - break; |
|
| 43 | - case 2: |
|
| 44 | - $data .= '=='; |
|
| 45 | - break; |
|
| 46 | - case 3: |
|
| 47 | - $data .= '='; |
|
| 48 | - break; |
|
| 49 | - default: |
|
| 50 | - throw new \UnexpectedValueException( |
|
| 51 | - 'Malformed base64url encoding.'); |
|
| 52 | - } |
|
| 53 | - return self::decode($data); |
|
| 54 | - } |
|
| 26 | + /** |
|
| 27 | + * Decode a string using base64url variant. |
|
| 28 | + * |
|
| 29 | + * @see https://en.wikipedia.org/wiki/Base64#URL_applications |
|
| 30 | + * |
|
| 31 | + * @param string $data |
|
| 32 | + * |
|
| 33 | + * @throws \UnexpectedValueException |
|
| 34 | + * |
|
| 35 | + * @return string |
|
| 36 | + */ |
|
| 37 | + public static function urlDecode(string $data): string |
|
| 38 | + { |
|
| 39 | + $data = strtr($data, '-_', '+/'); |
|
| 40 | + switch (strlen($data) % 4) { |
|
| 41 | + case 0: |
|
| 42 | + break; |
|
| 43 | + case 2: |
|
| 44 | + $data .= '=='; |
|
| 45 | + break; |
|
| 46 | + case 3: |
|
| 47 | + $data .= '='; |
|
| 48 | + break; |
|
| 49 | + default: |
|
| 50 | + throw new \UnexpectedValueException( |
|
| 51 | + 'Malformed base64url encoding.'); |
|
| 52 | + } |
|
| 53 | + return self::decode($data); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Check whether string is validly base64url encoded. |
|
| 58 | - * |
|
| 59 | - * @see https://en.wikipedia.org/wiki/Base64#URL_applications |
|
| 60 | - * |
|
| 61 | - * @param string $data |
|
| 62 | - * |
|
| 63 | - * @return bool |
|
| 64 | - */ |
|
| 65 | - public static function isValidURLEncoding(string $data): bool |
|
| 66 | - { |
|
| 67 | - return 1 === preg_match('#^[A-Za-z0-9\-_]*$#', $data); |
|
| 68 | - } |
|
| 56 | + /** |
|
| 57 | + * Check whether string is validly base64url encoded. |
|
| 58 | + * |
|
| 59 | + * @see https://en.wikipedia.org/wiki/Base64#URL_applications |
|
| 60 | + * |
|
| 61 | + * @param string $data |
|
| 62 | + * |
|
| 63 | + * @return bool |
|
| 64 | + */ |
|
| 65 | + public static function isValidURLEncoding(string $data): bool |
|
| 66 | + { |
|
| 67 | + return 1 === preg_match('#^[A-Za-z0-9\-_]*$#', $data); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * Encode a string in base64. |
|
| 72 | - * |
|
| 73 | - * @see https://tools.ietf.org/html/rfc4648#section-4 |
|
| 74 | - * |
|
| 75 | - * @param string $data |
|
| 76 | - * |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - public static function encode(string $data): string |
|
| 80 | - { |
|
| 81 | - return base64_encode($data); |
|
| 82 | - } |
|
| 70 | + /** |
|
| 71 | + * Encode a string in base64. |
|
| 72 | + * |
|
| 73 | + * @see https://tools.ietf.org/html/rfc4648#section-4 |
|
| 74 | + * |
|
| 75 | + * @param string $data |
|
| 76 | + * |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + public static function encode(string $data): string |
|
| 80 | + { |
|
| 81 | + return base64_encode($data); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Decode a string from base64 encoding. |
|
| 86 | - * |
|
| 87 | - * @see https://tools.ietf.org/html/rfc4648#section-4 |
|
| 88 | - * |
|
| 89 | - * @param string $data |
|
| 90 | - * |
|
| 91 | - * @throws \RuntimeException If decoding fails |
|
| 92 | - * |
|
| 93 | - * @return string |
|
| 94 | - */ |
|
| 95 | - public static function decode(string $data): string |
|
| 96 | - { |
|
| 97 | - $ret = base64_decode($data, true); |
|
| 98 | - if (!is_string($ret)) { |
|
| 99 | - $err = error_get_last(); |
|
| 100 | - $msg = isset($err) && __FILE__ === $err['file'] ? $err['message'] : null; |
|
| 101 | - throw new \RuntimeException($msg ?? 'base64_decode() failed.'); |
|
| 102 | - } |
|
| 103 | - return $ret; |
|
| 104 | - } |
|
| 84 | + /** |
|
| 85 | + * Decode a string from base64 encoding. |
|
| 86 | + * |
|
| 87 | + * @see https://tools.ietf.org/html/rfc4648#section-4 |
|
| 88 | + * |
|
| 89 | + * @param string $data |
|
| 90 | + * |
|
| 91 | + * @throws \RuntimeException If decoding fails |
|
| 92 | + * |
|
| 93 | + * @return string |
|
| 94 | + */ |
|
| 95 | + public static function decode(string $data): string |
|
| 96 | + { |
|
| 97 | + $ret = base64_decode($data, true); |
|
| 98 | + if (!is_string($ret)) { |
|
| 99 | + $err = error_get_last(); |
|
| 100 | + $msg = isset($err) && __FILE__ === $err['file'] ? $err['message'] : null; |
|
| 101 | + throw new \RuntimeException($msg ?? 'base64_decode() failed.'); |
|
| 102 | + } |
|
| 103 | + return $ret; |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * Check whether string is validly base64 encoded. |
|
| 108 | - * |
|
| 109 | - * @see https://tools.ietf.org/html/rfc4648#section-4 |
|
| 110 | - * |
|
| 111 | - * @param string $data |
|
| 112 | - * |
|
| 113 | - * @return bool |
|
| 114 | - */ |
|
| 115 | - public static function isValid(string $data): bool |
|
| 116 | - { |
|
| 117 | - return 1 === preg_match('#^[A-Za-z0-9+/]*={0,2}$#', $data); |
|
| 118 | - } |
|
| 106 | + /** |
|
| 107 | + * Check whether string is validly base64 encoded. |
|
| 108 | + * |
|
| 109 | + * @see https://tools.ietf.org/html/rfc4648#section-4 |
|
| 110 | + * |
|
| 111 | + * @param string $data |
|
| 112 | + * |
|
| 113 | + * @return bool |
|
| 114 | + */ |
|
| 115 | + public static function isValid(string $data): bool |
|
| 116 | + { |
|
| 117 | + return 1 === preg_match('#^[A-Za-z0-9+/]*={0,2}$#', $data); |
|
| 118 | + } |
|
| 119 | 119 | } |
@@ -38,17 +38,17 @@ |
||
| 38 | 38 | { |
| 39 | 39 | $data = strtr($data, '-_', '+/'); |
| 40 | 40 | switch (strlen($data) % 4) { |
| 41 | - case 0: |
|
| 42 | - break; |
|
| 43 | - case 2: |
|
| 44 | - $data .= '=='; |
|
| 45 | - break; |
|
| 46 | - case 3: |
|
| 47 | - $data .= '='; |
|
| 48 | - break; |
|
| 49 | - default: |
|
| 50 | - throw new \UnexpectedValueException( |
|
| 51 | - 'Malformed base64url encoding.'); |
|
| 41 | + case 0: |
|
| 42 | + break; |
|
| 43 | + case 2: |
|
| 44 | + $data .= '=='; |
|
| 45 | + break; |
|
| 46 | + case 3: |
|
| 47 | + $data .= '='; |
|
| 48 | + break; |
|
| 49 | + default: |
|
| 50 | + throw new \UnexpectedValueException( |
|
| 51 | + 'Malformed base64url encoding.'); |
|
| 52 | 52 | } |
| 53 | 53 | return self::decode($data); |
| 54 | 54 | } |
@@ -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\Util; |
| 6 | 6 | |
@@ -15,221 +15,221 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | abstract class JWA |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * HMAC using SHA-256. |
|
| 20 | - */ |
|
| 21 | - const ALGO_HS256 = 'HS256'; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * HMAC using SHA-384. |
|
| 25 | - */ |
|
| 26 | - const ALGO_HS384 = 'HS384'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * HMAC using SHA-512. |
|
| 30 | - */ |
|
| 31 | - const ALGO_HS512 = 'HS512'; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * RSASSA-PKCS1-v1_5 using SHA-256. |
|
| 35 | - */ |
|
| 36 | - const ALGO_RS256 = 'RS256'; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * RSASSA-PKCS1-v1_5 using SHA-384. |
|
| 40 | - */ |
|
| 41 | - const ALGO_RS384 = 'RS384'; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * RSASSA-PKCS1-v1_5 using SHA-512. |
|
| 45 | - */ |
|
| 46 | - const ALGO_RS512 = 'RS512'; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * ECDSA using P-256 and SHA-256. |
|
| 50 | - */ |
|
| 51 | - const ALGO_ES256 = 'ES256'; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * ECDSA using P-384 and SHA-384. |
|
| 55 | - */ |
|
| 56 | - const ALGO_ES384 = 'ES384'; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * ECDSA using P-521 and SHA-512. |
|
| 60 | - */ |
|
| 61 | - const ALGO_ES512 = 'ES512'; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * RSASSA-PSS using SHA-256 and MGF1 with SHA-256. |
|
| 65 | - */ |
|
| 66 | - const ALGO_PS256 = 'PS256'; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * RSASSA-PSS using SHA-384 and MGF1 with SHA-384. |
|
| 70 | - */ |
|
| 71 | - const ALGO_PS384 = 'PS384'; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * RSASSA-PSS using SHA-512 and MGF1 with SHA-512. |
|
| 75 | - */ |
|
| 76 | - const ALGO_PS512 = 'PS512'; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * No digital signature or MAC performed. |
|
| 80 | - */ |
|
| 81 | - const ALGO_NONE = 'none'; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * RSAES-PKCS1-v1_5. |
|
| 85 | - */ |
|
| 86 | - const ALGO_RSA1_5 = 'RSA1_5'; |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * RSAES OAEP using default parameters. |
|
| 90 | - */ |
|
| 91 | - const ALGO_RSA_OAEP = 'RSA-OAEP'; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * RSAES OAEP using SHA-256 and MGF1 with SHA-256. |
|
| 95 | - */ |
|
| 96 | - const ALGO_RSA_OAEP256 = 'RSA-OAEP-256'; |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * AES Key Wrap using 128-bit key. |
|
| 100 | - */ |
|
| 101 | - const ALGO_A128KW = 'A128KW'; |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * AES Key Wrap using 192-bit key. |
|
| 105 | - */ |
|
| 106 | - const ALGO_A192KW = 'A192KW'; |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * AES Key Wrap using 256-bit key. |
|
| 110 | - */ |
|
| 111 | - const ALGO_A256KW = 'A256KW'; |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Direct use of a shared symmetric key. |
|
| 115 | - */ |
|
| 116 | - const ALGO_DIR = 'dir'; |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * ECDH-ES using Concat KDF. |
|
| 120 | - */ |
|
| 121 | - const ALGO_ECDH_ES = 'ECDH-ES'; |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * ECDH-ES using Concat KDF and "A128KW" wrapping. |
|
| 125 | - */ |
|
| 126 | - const ALGO_ECDH_ES_A128KW = 'ECDH-ES+A128KW'; |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * ECDH-ES using Concat KDF and "A192KW" wrapping. |
|
| 130 | - */ |
|
| 131 | - const ALGO_ECDH_ES_A192KW = 'ECDH-ES+A192KW'; |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * ECDH-ES using Concat KDF and "A256KW" wrapping. |
|
| 135 | - */ |
|
| 136 | - const ALGO_ECDH_ES_A256KW = 'ECDH-ES+A256KW'; |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Key wrapping with AES GCM using 128-bit key. |
|
| 140 | - */ |
|
| 141 | - const ALGO_A128GCMKW = 'A128GCMKW'; |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Key wrapping with AES GCM using 192-bit key. |
|
| 145 | - */ |
|
| 146 | - const ALGO_A192GCMKW = 'A192GCMKW'; |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Key wrapping with AES GCM using 256-bit key. |
|
| 150 | - */ |
|
| 151 | - const ALGO_A256GCMKW = 'A256GCMKW'; |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * PBES2 with HMAC SHA-256 and "A128KW" wrapping. |
|
| 155 | - */ |
|
| 156 | - const ALGO_PBES2_HS256_A128KW = 'PBES2-HS256+A128KW'; |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * PBES2 with HMAC SHA-384 and "A192KW" wrapping. |
|
| 160 | - */ |
|
| 161 | - const ALGO_PBES2_HS384_A192KW = 'PBES2-HS384+A192KW'; |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * PBES2 with HMAC SHA-512 and "A256KW" wrapping. |
|
| 165 | - */ |
|
| 166 | - const ALGO_PBES2_HS512_A256KW = 'PBES2-HS512+A256KW'; |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * AES_128_CBC_HMAC_SHA_256 authenticated encryption algorithm. |
|
| 170 | - */ |
|
| 171 | - const ALGO_A128CBC_HS256 = 'A128CBC-HS256'; |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * AES_192_CBC_HMAC_SHA_384 authenticated encryption algorithm. |
|
| 175 | - */ |
|
| 176 | - const ALGO_A192CBC_HS384 = 'A192CBC-HS384'; |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * AES_256_CBC_HMAC_SHA_512 authenticated encryption algorithm. |
|
| 180 | - */ |
|
| 181 | - const ALGO_A256CBC_HS512 = 'A256CBC-HS512'; |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * AES GCM using 128-bit key. |
|
| 185 | - */ |
|
| 186 | - const ALGO_A128GCM = 'A128GCM'; |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * AES GCM using 192-bit key. |
|
| 190 | - */ |
|
| 191 | - const ALGO_A192GCM = 'A192GCM'; |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * AES GCM using 256-bit key. |
|
| 195 | - */ |
|
| 196 | - const ALGO_A256GCM = 'A256GCM'; |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * DEFLATE compression. |
|
| 200 | - */ |
|
| 201 | - const ALGO_DEFLATE = 'DEF'; |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * Derive algorithm name from the header and optionally from the given JWK. |
|
| 205 | - * |
|
| 206 | - * @param Header $header Header |
|
| 207 | - * @param JWK $jwk Optional JWK |
|
| 208 | - * |
|
| 209 | - * @throws \UnexpectedValueException if algorithm parameter is not present |
|
| 210 | - * or header and JWK algorithms differ |
|
| 211 | - * |
|
| 212 | - * @return string Algorithm name |
|
| 213 | - */ |
|
| 214 | - public static function deriveAlgorithmName(Header $header, ?JWK $jwk = null): string |
|
| 215 | - { |
|
| 216 | - if ($header->hasAlgorithm()) { |
|
| 217 | - $alg = $header->algorithm()->value(); |
|
| 218 | - } |
|
| 219 | - // if JWK is set, and has an algorithm parameter |
|
| 220 | - if (isset($jwk) && $jwk->hasAlgorithmParameter()) { |
|
| 221 | - $jwk_alg = $jwk->algorithmParameter()->value(); |
|
| 222 | - // check that algorithms match |
|
| 223 | - if (isset($alg) && $alg !== $jwk_alg) { |
|
| 224 | - throw new \UnexpectedValueException( |
|
| 225 | - "JWK algorithm '{$jwk_alg}' doesn't match" . |
|
| 226 | - " the header's algorithm '{$alg}'."); |
|
| 227 | - } |
|
| 228 | - $alg = $jwk_alg; |
|
| 229 | - } |
|
| 230 | - if (!isset($alg)) { |
|
| 231 | - throw new \UnexpectedValueException('No algorithm parameter.'); |
|
| 232 | - } |
|
| 233 | - return $alg; |
|
| 234 | - } |
|
| 18 | + /** |
|
| 19 | + * HMAC using SHA-256. |
|
| 20 | + */ |
|
| 21 | + const ALGO_HS256 = 'HS256'; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * HMAC using SHA-384. |
|
| 25 | + */ |
|
| 26 | + const ALGO_HS384 = 'HS384'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * HMAC using SHA-512. |
|
| 30 | + */ |
|
| 31 | + const ALGO_HS512 = 'HS512'; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * RSASSA-PKCS1-v1_5 using SHA-256. |
|
| 35 | + */ |
|
| 36 | + const ALGO_RS256 = 'RS256'; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * RSASSA-PKCS1-v1_5 using SHA-384. |
|
| 40 | + */ |
|
| 41 | + const ALGO_RS384 = 'RS384'; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * RSASSA-PKCS1-v1_5 using SHA-512. |
|
| 45 | + */ |
|
| 46 | + const ALGO_RS512 = 'RS512'; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * ECDSA using P-256 and SHA-256. |
|
| 50 | + */ |
|
| 51 | + const ALGO_ES256 = 'ES256'; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * ECDSA using P-384 and SHA-384. |
|
| 55 | + */ |
|
| 56 | + const ALGO_ES384 = 'ES384'; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * ECDSA using P-521 and SHA-512. |
|
| 60 | + */ |
|
| 61 | + const ALGO_ES512 = 'ES512'; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * RSASSA-PSS using SHA-256 and MGF1 with SHA-256. |
|
| 65 | + */ |
|
| 66 | + const ALGO_PS256 = 'PS256'; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * RSASSA-PSS using SHA-384 and MGF1 with SHA-384. |
|
| 70 | + */ |
|
| 71 | + const ALGO_PS384 = 'PS384'; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * RSASSA-PSS using SHA-512 and MGF1 with SHA-512. |
|
| 75 | + */ |
|
| 76 | + const ALGO_PS512 = 'PS512'; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * No digital signature or MAC performed. |
|
| 80 | + */ |
|
| 81 | + const ALGO_NONE = 'none'; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * RSAES-PKCS1-v1_5. |
|
| 85 | + */ |
|
| 86 | + const ALGO_RSA1_5 = 'RSA1_5'; |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * RSAES OAEP using default parameters. |
|
| 90 | + */ |
|
| 91 | + const ALGO_RSA_OAEP = 'RSA-OAEP'; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * RSAES OAEP using SHA-256 and MGF1 with SHA-256. |
|
| 95 | + */ |
|
| 96 | + const ALGO_RSA_OAEP256 = 'RSA-OAEP-256'; |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * AES Key Wrap using 128-bit key. |
|
| 100 | + */ |
|
| 101 | + const ALGO_A128KW = 'A128KW'; |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * AES Key Wrap using 192-bit key. |
|
| 105 | + */ |
|
| 106 | + const ALGO_A192KW = 'A192KW'; |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * AES Key Wrap using 256-bit key. |
|
| 110 | + */ |
|
| 111 | + const ALGO_A256KW = 'A256KW'; |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Direct use of a shared symmetric key. |
|
| 115 | + */ |
|
| 116 | + const ALGO_DIR = 'dir'; |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * ECDH-ES using Concat KDF. |
|
| 120 | + */ |
|
| 121 | + const ALGO_ECDH_ES = 'ECDH-ES'; |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * ECDH-ES using Concat KDF and "A128KW" wrapping. |
|
| 125 | + */ |
|
| 126 | + const ALGO_ECDH_ES_A128KW = 'ECDH-ES+A128KW'; |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * ECDH-ES using Concat KDF and "A192KW" wrapping. |
|
| 130 | + */ |
|
| 131 | + const ALGO_ECDH_ES_A192KW = 'ECDH-ES+A192KW'; |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * ECDH-ES using Concat KDF and "A256KW" wrapping. |
|
| 135 | + */ |
|
| 136 | + const ALGO_ECDH_ES_A256KW = 'ECDH-ES+A256KW'; |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Key wrapping with AES GCM using 128-bit key. |
|
| 140 | + */ |
|
| 141 | + const ALGO_A128GCMKW = 'A128GCMKW'; |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Key wrapping with AES GCM using 192-bit key. |
|
| 145 | + */ |
|
| 146 | + const ALGO_A192GCMKW = 'A192GCMKW'; |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Key wrapping with AES GCM using 256-bit key. |
|
| 150 | + */ |
|
| 151 | + const ALGO_A256GCMKW = 'A256GCMKW'; |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * PBES2 with HMAC SHA-256 and "A128KW" wrapping. |
|
| 155 | + */ |
|
| 156 | + const ALGO_PBES2_HS256_A128KW = 'PBES2-HS256+A128KW'; |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * PBES2 with HMAC SHA-384 and "A192KW" wrapping. |
|
| 160 | + */ |
|
| 161 | + const ALGO_PBES2_HS384_A192KW = 'PBES2-HS384+A192KW'; |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * PBES2 with HMAC SHA-512 and "A256KW" wrapping. |
|
| 165 | + */ |
|
| 166 | + const ALGO_PBES2_HS512_A256KW = 'PBES2-HS512+A256KW'; |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * AES_128_CBC_HMAC_SHA_256 authenticated encryption algorithm. |
|
| 170 | + */ |
|
| 171 | + const ALGO_A128CBC_HS256 = 'A128CBC-HS256'; |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * AES_192_CBC_HMAC_SHA_384 authenticated encryption algorithm. |
|
| 175 | + */ |
|
| 176 | + const ALGO_A192CBC_HS384 = 'A192CBC-HS384'; |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * AES_256_CBC_HMAC_SHA_512 authenticated encryption algorithm. |
|
| 180 | + */ |
|
| 181 | + const ALGO_A256CBC_HS512 = 'A256CBC-HS512'; |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * AES GCM using 128-bit key. |
|
| 185 | + */ |
|
| 186 | + const ALGO_A128GCM = 'A128GCM'; |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * AES GCM using 192-bit key. |
|
| 190 | + */ |
|
| 191 | + const ALGO_A192GCM = 'A192GCM'; |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * AES GCM using 256-bit key. |
|
| 195 | + */ |
|
| 196 | + const ALGO_A256GCM = 'A256GCM'; |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * DEFLATE compression. |
|
| 200 | + */ |
|
| 201 | + const ALGO_DEFLATE = 'DEF'; |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * Derive algorithm name from the header and optionally from the given JWK. |
|
| 205 | + * |
|
| 206 | + * @param Header $header Header |
|
| 207 | + * @param JWK $jwk Optional JWK |
|
| 208 | + * |
|
| 209 | + * @throws \UnexpectedValueException if algorithm parameter is not present |
|
| 210 | + * or header and JWK algorithms differ |
|
| 211 | + * |
|
| 212 | + * @return string Algorithm name |
|
| 213 | + */ |
|
| 214 | + public static function deriveAlgorithmName(Header $header, ?JWK $jwk = null): string |
|
| 215 | + { |
|
| 216 | + if ($header->hasAlgorithm()) { |
|
| 217 | + $alg = $header->algorithm()->value(); |
|
| 218 | + } |
|
| 219 | + // if JWK is set, and has an algorithm parameter |
|
| 220 | + if (isset($jwk) && $jwk->hasAlgorithmParameter()) { |
|
| 221 | + $jwk_alg = $jwk->algorithmParameter()->value(); |
|
| 222 | + // check that algorithms match |
|
| 223 | + if (isset($alg) && $alg !== $jwk_alg) { |
|
| 224 | + throw new \UnexpectedValueException( |
|
| 225 | + "JWK algorithm '{$jwk_alg}' doesn't match" . |
|
| 226 | + " the header's algorithm '{$alg}'."); |
|
| 227 | + } |
|
| 228 | + $alg = $jwk_alg; |
|
| 229 | + } |
|
| 230 | + if (!isset($alg)) { |
|
| 231 | + throw new \UnexpectedValueException('No algorithm parameter.'); |
|
| 232 | + } |
|
| 233 | + return $alg; |
|
| 234 | + } |
|
| 235 | 235 | } |
@@ -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\JWA; |
| 6 | 6 | |
@@ -20,362 +20,362 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | class JWE |
| 22 | 22 | { |
| 23 | - /** |
|
| 24 | - * Protected header. |
|
| 25 | - * |
|
| 26 | - * @var Header |
|
| 27 | - */ |
|
| 28 | - protected $_protectedHeader; |
|
| 23 | + /** |
|
| 24 | + * Protected header. |
|
| 25 | + * |
|
| 26 | + * @var Header |
|
| 27 | + */ |
|
| 28 | + protected $_protectedHeader; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Encrypted key. |
|
| 32 | - * |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - protected $_encryptedKey; |
|
| 30 | + /** |
|
| 31 | + * Encrypted key. |
|
| 32 | + * |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + protected $_encryptedKey; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Initialization vector. |
|
| 39 | - * |
|
| 40 | - * @var string |
|
| 41 | - */ |
|
| 42 | - protected $_iv; |
|
| 37 | + /** |
|
| 38 | + * Initialization vector. |
|
| 39 | + * |
|
| 40 | + * @var string |
|
| 41 | + */ |
|
| 42 | + protected $_iv; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Additional authenticated data. |
|
| 46 | - * |
|
| 47 | - * @var null|string |
|
| 48 | - */ |
|
| 49 | - protected $_aad; |
|
| 44 | + /** |
|
| 45 | + * Additional authenticated data. |
|
| 46 | + * |
|
| 47 | + * @var null|string |
|
| 48 | + */ |
|
| 49 | + protected $_aad; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Ciphertext. |
|
| 53 | - * |
|
| 54 | - * @var string |
|
| 55 | - */ |
|
| 56 | - protected $_ciphertext; |
|
| 51 | + /** |
|
| 52 | + * Ciphertext. |
|
| 53 | + * |
|
| 54 | + * @var string |
|
| 55 | + */ |
|
| 56 | + protected $_ciphertext; |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Authentication tag. |
|
| 60 | - * |
|
| 61 | - * @var string |
|
| 62 | - */ |
|
| 63 | - protected $_authenticationTag; |
|
| 58 | + /** |
|
| 59 | + * Authentication tag. |
|
| 60 | + * |
|
| 61 | + * @var string |
|
| 62 | + */ |
|
| 63 | + protected $_authenticationTag; |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Constructor. |
|
| 67 | - * |
|
| 68 | - * @param Header $protected_header JWE Protected Header |
|
| 69 | - * @param string $encrypted_key Encrypted key |
|
| 70 | - * @param string $iv Initialization vector |
|
| 71 | - * @param string $ciphertext Ciphertext |
|
| 72 | - * @param string $auth_tag Authentication tag |
|
| 73 | - * @param null|string $aad Additional authenticated data |
|
| 74 | - */ |
|
| 75 | - public function __construct(Header $protected_header, string $encrypted_key, |
|
| 76 | - string $iv, string $ciphertext, string $auth_tag, ?string $aad = null) |
|
| 77 | - { |
|
| 78 | - $this->_protectedHeader = $protected_header; |
|
| 79 | - $this->_encryptedKey = $encrypted_key; |
|
| 80 | - $this->_iv = $iv; |
|
| 81 | - $this->_aad = $aad; |
|
| 82 | - $this->_ciphertext = $ciphertext; |
|
| 83 | - $this->_authenticationTag = $auth_tag; |
|
| 84 | - } |
|
| 65 | + /** |
|
| 66 | + * Constructor. |
|
| 67 | + * |
|
| 68 | + * @param Header $protected_header JWE Protected Header |
|
| 69 | + * @param string $encrypted_key Encrypted key |
|
| 70 | + * @param string $iv Initialization vector |
|
| 71 | + * @param string $ciphertext Ciphertext |
|
| 72 | + * @param string $auth_tag Authentication tag |
|
| 73 | + * @param null|string $aad Additional authenticated data |
|
| 74 | + */ |
|
| 75 | + public function __construct(Header $protected_header, string $encrypted_key, |
|
| 76 | + string $iv, string $ciphertext, string $auth_tag, ?string $aad = null) |
|
| 77 | + { |
|
| 78 | + $this->_protectedHeader = $protected_header; |
|
| 79 | + $this->_encryptedKey = $encrypted_key; |
|
| 80 | + $this->_iv = $iv; |
|
| 81 | + $this->_aad = $aad; |
|
| 82 | + $this->_ciphertext = $ciphertext; |
|
| 83 | + $this->_authenticationTag = $auth_tag; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Convert JWE to string. |
|
| 88 | - * |
|
| 89 | - * @return string |
|
| 90 | - */ |
|
| 91 | - public function __toString(): string |
|
| 92 | - { |
|
| 93 | - return $this->toCompact(); |
|
| 94 | - } |
|
| 86 | + /** |
|
| 87 | + * Convert JWE to string. |
|
| 88 | + * |
|
| 89 | + * @return string |
|
| 90 | + */ |
|
| 91 | + public function __toString(): string |
|
| 92 | + { |
|
| 93 | + return $this->toCompact(); |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * Initialize from compact serialization. |
|
| 98 | - * |
|
| 99 | - * @param string $data |
|
| 100 | - * |
|
| 101 | - * @return self |
|
| 102 | - */ |
|
| 103 | - public static function fromCompact(string $data): self |
|
| 104 | - { |
|
| 105 | - return self::fromParts(explode('.', $data)); |
|
| 106 | - } |
|
| 96 | + /** |
|
| 97 | + * Initialize from compact serialization. |
|
| 98 | + * |
|
| 99 | + * @param string $data |
|
| 100 | + * |
|
| 101 | + * @return self |
|
| 102 | + */ |
|
| 103 | + public static function fromCompact(string $data): self |
|
| 104 | + { |
|
| 105 | + return self::fromParts(explode('.', $data)); |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Initialize from parts of compact serialization. |
|
| 110 | - * |
|
| 111 | - * @param array $parts |
|
| 112 | - * |
|
| 113 | - * @throws \UnexpectedValueException |
|
| 114 | - * |
|
| 115 | - * @return self |
|
| 116 | - */ |
|
| 117 | - public static function fromParts(array $parts): self |
|
| 118 | - { |
|
| 119 | - if (5 !== count($parts)) { |
|
| 120 | - throw new \UnexpectedValueException( |
|
| 121 | - 'Invalid JWE compact serialization.'); |
|
| 122 | - } |
|
| 123 | - $header = Header::fromJSON(Base64::urlDecode($parts[0])); |
|
| 124 | - $encrypted_key = Base64::urlDecode($parts[1]); |
|
| 125 | - $iv = Base64::urlDecode($parts[2]); |
|
| 126 | - $ciphertext = Base64::urlDecode($parts[3]); |
|
| 127 | - $auth_tag = Base64::urlDecode($parts[4]); |
|
| 128 | - return new self($header, $encrypted_key, $iv, $ciphertext, $auth_tag); |
|
| 129 | - } |
|
| 108 | + /** |
|
| 109 | + * Initialize from parts of compact serialization. |
|
| 110 | + * |
|
| 111 | + * @param array $parts |
|
| 112 | + * |
|
| 113 | + * @throws \UnexpectedValueException |
|
| 114 | + * |
|
| 115 | + * @return self |
|
| 116 | + */ |
|
| 117 | + public static function fromParts(array $parts): self |
|
| 118 | + { |
|
| 119 | + if (5 !== count($parts)) { |
|
| 120 | + throw new \UnexpectedValueException( |
|
| 121 | + 'Invalid JWE compact serialization.'); |
|
| 122 | + } |
|
| 123 | + $header = Header::fromJSON(Base64::urlDecode($parts[0])); |
|
| 124 | + $encrypted_key = Base64::urlDecode($parts[1]); |
|
| 125 | + $iv = Base64::urlDecode($parts[2]); |
|
| 126 | + $ciphertext = Base64::urlDecode($parts[3]); |
|
| 127 | + $auth_tag = Base64::urlDecode($parts[4]); |
|
| 128 | + return new self($header, $encrypted_key, $iv, $ciphertext, $auth_tag); |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - /** |
|
| 132 | - * Initialize by encrypting the given payload. |
|
| 133 | - * |
|
| 134 | - * @param string $payload Payload |
|
| 135 | - * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
| 136 | - * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
| 137 | - * @param null|CompressionAlgorithm $zip_algo Optional compression algorithm |
|
| 138 | - * @param null|Header $header Optional desired header. |
|
| 139 | - * Algorithm specific parameters are |
|
| 140 | - * automatically added. |
|
| 141 | - * @param null|string $cek Optional content encryption key. |
|
| 142 | - * Randomly enerated if not set. |
|
| 143 | - * @param null|string $iv Optional initialization vector. |
|
| 144 | - * Randomly generated if not set. |
|
| 145 | - * |
|
| 146 | - * @throws \RuntimeException If encrypt fails |
|
| 147 | - * |
|
| 148 | - * @return self |
|
| 149 | - */ |
|
| 150 | - public static function encrypt(string $payload, |
|
| 151 | - KeyManagementAlgorithm $key_algo, ContentEncryptionAlgorithm $enc_algo, |
|
| 152 | - ?CompressionAlgorithm $zip_algo = null, ?Header $header = null, |
|
| 153 | - ?string $cek = null, ?string $iv = null): self |
|
| 154 | - { |
|
| 155 | - // if header was not given, initialize empty |
|
| 156 | - if (!isset($header)) { |
|
| 157 | - $header = new Header(); |
|
| 158 | - } |
|
| 159 | - // generate random CEK |
|
| 160 | - if (!isset($cek)) { |
|
| 161 | - $cek = $key_algo->cekForEncryption($enc_algo->keySize()); |
|
| 162 | - } |
|
| 163 | - // generate random IV |
|
| 164 | - if (!isset($iv)) { |
|
| 165 | - $iv = openssl_random_pseudo_bytes($enc_algo->ivSize()); |
|
| 166 | - } |
|
| 167 | - // compress |
|
| 168 | - if (isset($zip_algo)) { |
|
| 169 | - $payload = $zip_algo->compress($payload); |
|
| 170 | - $header = $header->withParameters(...$zip_algo->headerParameters()); |
|
| 171 | - } |
|
| 172 | - return self::_encryptContent($payload, $cek, $iv, |
|
| 173 | - $key_algo, $enc_algo, $header); |
|
| 174 | - } |
|
| 131 | + /** |
|
| 132 | + * Initialize by encrypting the given payload. |
|
| 133 | + * |
|
| 134 | + * @param string $payload Payload |
|
| 135 | + * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
| 136 | + * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
| 137 | + * @param null|CompressionAlgorithm $zip_algo Optional compression algorithm |
|
| 138 | + * @param null|Header $header Optional desired header. |
|
| 139 | + * Algorithm specific parameters are |
|
| 140 | + * automatically added. |
|
| 141 | + * @param null|string $cek Optional content encryption key. |
|
| 142 | + * Randomly enerated if not set. |
|
| 143 | + * @param null|string $iv Optional initialization vector. |
|
| 144 | + * Randomly generated if not set. |
|
| 145 | + * |
|
| 146 | + * @throws \RuntimeException If encrypt fails |
|
| 147 | + * |
|
| 148 | + * @return self |
|
| 149 | + */ |
|
| 150 | + public static function encrypt(string $payload, |
|
| 151 | + KeyManagementAlgorithm $key_algo, ContentEncryptionAlgorithm $enc_algo, |
|
| 152 | + ?CompressionAlgorithm $zip_algo = null, ?Header $header = null, |
|
| 153 | + ?string $cek = null, ?string $iv = null): self |
|
| 154 | + { |
|
| 155 | + // if header was not given, initialize empty |
|
| 156 | + if (!isset($header)) { |
|
| 157 | + $header = new Header(); |
|
| 158 | + } |
|
| 159 | + // generate random CEK |
|
| 160 | + if (!isset($cek)) { |
|
| 161 | + $cek = $key_algo->cekForEncryption($enc_algo->keySize()); |
|
| 162 | + } |
|
| 163 | + // generate random IV |
|
| 164 | + if (!isset($iv)) { |
|
| 165 | + $iv = openssl_random_pseudo_bytes($enc_algo->ivSize()); |
|
| 166 | + } |
|
| 167 | + // compress |
|
| 168 | + if (isset($zip_algo)) { |
|
| 169 | + $payload = $zip_algo->compress($payload); |
|
| 170 | + $header = $header->withParameters(...$zip_algo->headerParameters()); |
|
| 171 | + } |
|
| 172 | + return self::_encryptContent($payload, $cek, $iv, |
|
| 173 | + $key_algo, $enc_algo, $header); |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | - /** |
|
| 177 | - * Decrypt the content using explicit algorithms. |
|
| 178 | - * |
|
| 179 | - * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
| 180 | - * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
| 181 | - * |
|
| 182 | - * @throws \RuntimeException If decrypt fails |
|
| 183 | - * |
|
| 184 | - * @return string Plaintext payload |
|
| 185 | - */ |
|
| 186 | - public function decrypt(KeyManagementAlgorithm $key_algo, |
|
| 187 | - ContentEncryptionAlgorithm $enc_algo): string |
|
| 188 | - { |
|
| 189 | - // check that key management algorithm matches |
|
| 190 | - if ($key_algo->algorithmParamValue() !== $this->algorithmName()) { |
|
| 191 | - throw new \UnexpectedValueException( |
|
| 192 | - 'Invalid key management algorithm.'); |
|
| 193 | - } |
|
| 194 | - // check that encryption algorithm matches |
|
| 195 | - if ($enc_algo->encryptionAlgorithmParamValue() !== $this->encryptionAlgorithmName()) { |
|
| 196 | - throw new \UnexpectedValueException('Invalid encryption algorithm.'); |
|
| 197 | - } |
|
| 198 | - $header = $this->header(); |
|
| 199 | - // decrypt content encryption key |
|
| 200 | - $cek = $key_algo->decrypt($this->_encryptedKey, $header); |
|
| 201 | - // decrypt payload |
|
| 202 | - $aad = Base64::urlEncode($this->_protectedHeader->toJSON()); |
|
| 203 | - $payload = $enc_algo->decrypt($this->_ciphertext, $cek, |
|
| 204 | - $this->_iv, $aad, $this->_authenticationTag); |
|
| 205 | - // decompress |
|
| 206 | - if ($header->hasCompressionAlgorithm()) { |
|
| 207 | - $payload = CompressionFactory::algoByHeader($header)->decompress($payload); |
|
| 208 | - } |
|
| 209 | - return $payload; |
|
| 210 | - } |
|
| 176 | + /** |
|
| 177 | + * Decrypt the content using explicit algorithms. |
|
| 178 | + * |
|
| 179 | + * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
| 180 | + * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
| 181 | + * |
|
| 182 | + * @throws \RuntimeException If decrypt fails |
|
| 183 | + * |
|
| 184 | + * @return string Plaintext payload |
|
| 185 | + */ |
|
| 186 | + public function decrypt(KeyManagementAlgorithm $key_algo, |
|
| 187 | + ContentEncryptionAlgorithm $enc_algo): string |
|
| 188 | + { |
|
| 189 | + // check that key management algorithm matches |
|
| 190 | + if ($key_algo->algorithmParamValue() !== $this->algorithmName()) { |
|
| 191 | + throw new \UnexpectedValueException( |
|
| 192 | + 'Invalid key management algorithm.'); |
|
| 193 | + } |
|
| 194 | + // check that encryption algorithm matches |
|
| 195 | + if ($enc_algo->encryptionAlgorithmParamValue() !== $this->encryptionAlgorithmName()) { |
|
| 196 | + throw new \UnexpectedValueException('Invalid encryption algorithm.'); |
|
| 197 | + } |
|
| 198 | + $header = $this->header(); |
|
| 199 | + // decrypt content encryption key |
|
| 200 | + $cek = $key_algo->decrypt($this->_encryptedKey, $header); |
|
| 201 | + // decrypt payload |
|
| 202 | + $aad = Base64::urlEncode($this->_protectedHeader->toJSON()); |
|
| 203 | + $payload = $enc_algo->decrypt($this->_ciphertext, $cek, |
|
| 204 | + $this->_iv, $aad, $this->_authenticationTag); |
|
| 205 | + // decompress |
|
| 206 | + if ($header->hasCompressionAlgorithm()) { |
|
| 207 | + $payload = CompressionFactory::algoByHeader($header)->decompress($payload); |
|
| 208 | + } |
|
| 209 | + return $payload; |
|
| 210 | + } |
|
| 211 | 211 | |
| 212 | - /** |
|
| 213 | - * Decrypt content using given JWK. |
|
| 214 | - * |
|
| 215 | - * Key management and content encryption algorithms are determined from the |
|
| 216 | - * header. |
|
| 217 | - * |
|
| 218 | - * @param JWK $jwk JSON Web Key |
|
| 219 | - * |
|
| 220 | - * @throws \RuntimeException If algorithm initialization fails |
|
| 221 | - * |
|
| 222 | - * @return string Plaintext payload |
|
| 223 | - */ |
|
| 224 | - public function decryptWithJWK(JWK $jwk): string |
|
| 225 | - { |
|
| 226 | - $header = $this->header(); |
|
| 227 | - $key_algo = KeyManagementAlgorithm::fromJWK($jwk, $header); |
|
| 228 | - $enc_algo = EncryptionAlgorithmFactory::algoByHeader($header); |
|
| 229 | - return $this->decrypt($key_algo, $enc_algo); |
|
| 230 | - } |
|
| 212 | + /** |
|
| 213 | + * Decrypt content using given JWK. |
|
| 214 | + * |
|
| 215 | + * Key management and content encryption algorithms are determined from the |
|
| 216 | + * header. |
|
| 217 | + * |
|
| 218 | + * @param JWK $jwk JSON Web Key |
|
| 219 | + * |
|
| 220 | + * @throws \RuntimeException If algorithm initialization fails |
|
| 221 | + * |
|
| 222 | + * @return string Plaintext payload |
|
| 223 | + */ |
|
| 224 | + public function decryptWithJWK(JWK $jwk): string |
|
| 225 | + { |
|
| 226 | + $header = $this->header(); |
|
| 227 | + $key_algo = KeyManagementAlgorithm::fromJWK($jwk, $header); |
|
| 228 | + $enc_algo = EncryptionAlgorithmFactory::algoByHeader($header); |
|
| 229 | + return $this->decrypt($key_algo, $enc_algo); |
|
| 230 | + } |
|
| 231 | 231 | |
| 232 | - /** |
|
| 233 | - * Decrypt content using a key from the given JWK set. |
|
| 234 | - * |
|
| 235 | - * Correct key shall be sought by the key ID indicated by the header. |
|
| 236 | - * |
|
| 237 | - * @param JWKSet $set Set of JSON Web Keys |
|
| 238 | - * |
|
| 239 | - * @throws \RuntimeException If algorithm initialization fails |
|
| 240 | - * |
|
| 241 | - * @return string Plaintext payload |
|
| 242 | - */ |
|
| 243 | - public function decryptWithJWKSet(JWKSet $set): string |
|
| 244 | - { |
|
| 245 | - if (!count($set)) { |
|
| 246 | - throw new \RuntimeException('No keys.'); |
|
| 247 | - } |
|
| 248 | - $header = $this->header(); |
|
| 249 | - $factory = new KeyAlgorithmFactory($header); |
|
| 250 | - $key_algo = $factory->algoByKeys($set); |
|
| 251 | - $enc_algo = EncryptionAlgorithmFactory::algoByHeader($header); |
|
| 252 | - return $this->decrypt($key_algo, $enc_algo); |
|
| 253 | - } |
|
| 232 | + /** |
|
| 233 | + * Decrypt content using a key from the given JWK set. |
|
| 234 | + * |
|
| 235 | + * Correct key shall be sought by the key ID indicated by the header. |
|
| 236 | + * |
|
| 237 | + * @param JWKSet $set Set of JSON Web Keys |
|
| 238 | + * |
|
| 239 | + * @throws \RuntimeException If algorithm initialization fails |
|
| 240 | + * |
|
| 241 | + * @return string Plaintext payload |
|
| 242 | + */ |
|
| 243 | + public function decryptWithJWKSet(JWKSet $set): string |
|
| 244 | + { |
|
| 245 | + if (!count($set)) { |
|
| 246 | + throw new \RuntimeException('No keys.'); |
|
| 247 | + } |
|
| 248 | + $header = $this->header(); |
|
| 249 | + $factory = new KeyAlgorithmFactory($header); |
|
| 250 | + $key_algo = $factory->algoByKeys($set); |
|
| 251 | + $enc_algo = EncryptionAlgorithmFactory::algoByHeader($header); |
|
| 252 | + return $this->decrypt($key_algo, $enc_algo); |
|
| 253 | + } |
|
| 254 | 254 | |
| 255 | - /** |
|
| 256 | - * Get JOSE header. |
|
| 257 | - * |
|
| 258 | - * @return JOSE |
|
| 259 | - */ |
|
| 260 | - public function header(): JOSE |
|
| 261 | - { |
|
| 262 | - return new JOSE($this->_protectedHeader); |
|
| 263 | - } |
|
| 255 | + /** |
|
| 256 | + * Get JOSE header. |
|
| 257 | + * |
|
| 258 | + * @return JOSE |
|
| 259 | + */ |
|
| 260 | + public function header(): JOSE |
|
| 261 | + { |
|
| 262 | + return new JOSE($this->_protectedHeader); |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - /** |
|
| 266 | - * Get the name of the key management algorithm. |
|
| 267 | - * |
|
| 268 | - * @return string |
|
| 269 | - */ |
|
| 270 | - public function algorithmName(): string |
|
| 271 | - { |
|
| 272 | - return $this->header()->algorithm()->value(); |
|
| 273 | - } |
|
| 265 | + /** |
|
| 266 | + * Get the name of the key management algorithm. |
|
| 267 | + * |
|
| 268 | + * @return string |
|
| 269 | + */ |
|
| 270 | + public function algorithmName(): string |
|
| 271 | + { |
|
| 272 | + return $this->header()->algorithm()->value(); |
|
| 273 | + } |
|
| 274 | 274 | |
| 275 | - /** |
|
| 276 | - * Get the name of the encryption algorithm. |
|
| 277 | - * |
|
| 278 | - * @return string |
|
| 279 | - */ |
|
| 280 | - public function encryptionAlgorithmName(): string |
|
| 281 | - { |
|
| 282 | - return $this->header()->encryptionAlgorithm()->value(); |
|
| 283 | - } |
|
| 275 | + /** |
|
| 276 | + * Get the name of the encryption algorithm. |
|
| 277 | + * |
|
| 278 | + * @return string |
|
| 279 | + */ |
|
| 280 | + public function encryptionAlgorithmName(): string |
|
| 281 | + { |
|
| 282 | + return $this->header()->encryptionAlgorithm()->value(); |
|
| 283 | + } |
|
| 284 | 284 | |
| 285 | - /** |
|
| 286 | - * Get encrypted CEK. |
|
| 287 | - * |
|
| 288 | - * @return string |
|
| 289 | - */ |
|
| 290 | - public function encryptedKey(): string |
|
| 291 | - { |
|
| 292 | - return $this->_encryptedKey; |
|
| 293 | - } |
|
| 285 | + /** |
|
| 286 | + * Get encrypted CEK. |
|
| 287 | + * |
|
| 288 | + * @return string |
|
| 289 | + */ |
|
| 290 | + public function encryptedKey(): string |
|
| 291 | + { |
|
| 292 | + return $this->_encryptedKey; |
|
| 293 | + } |
|
| 294 | 294 | |
| 295 | - /** |
|
| 296 | - * Get initialization vector. |
|
| 297 | - * |
|
| 298 | - * @return string |
|
| 299 | - */ |
|
| 300 | - public function initializationVector(): string |
|
| 301 | - { |
|
| 302 | - return $this->_iv; |
|
| 303 | - } |
|
| 295 | + /** |
|
| 296 | + * Get initialization vector. |
|
| 297 | + * |
|
| 298 | + * @return string |
|
| 299 | + */ |
|
| 300 | + public function initializationVector(): string |
|
| 301 | + { |
|
| 302 | + return $this->_iv; |
|
| 303 | + } |
|
| 304 | 304 | |
| 305 | - /** |
|
| 306 | - * Get ciphertext. |
|
| 307 | - * |
|
| 308 | - * @return string |
|
| 309 | - */ |
|
| 310 | - public function ciphertext(): string |
|
| 311 | - { |
|
| 312 | - return $this->_ciphertext; |
|
| 313 | - } |
|
| 305 | + /** |
|
| 306 | + * Get ciphertext. |
|
| 307 | + * |
|
| 308 | + * @return string |
|
| 309 | + */ |
|
| 310 | + public function ciphertext(): string |
|
| 311 | + { |
|
| 312 | + return $this->_ciphertext; |
|
| 313 | + } |
|
| 314 | 314 | |
| 315 | - /** |
|
| 316 | - * Get authentication tag. |
|
| 317 | - * |
|
| 318 | - * @return string |
|
| 319 | - */ |
|
| 320 | - public function authenticationTag(): string |
|
| 321 | - { |
|
| 322 | - return $this->_authenticationTag; |
|
| 323 | - } |
|
| 315 | + /** |
|
| 316 | + * Get authentication tag. |
|
| 317 | + * |
|
| 318 | + * @return string |
|
| 319 | + */ |
|
| 320 | + public function authenticationTag(): string |
|
| 321 | + { |
|
| 322 | + return $this->_authenticationTag; |
|
| 323 | + } |
|
| 324 | 324 | |
| 325 | - /** |
|
| 326 | - * Convert to compact serialization. |
|
| 327 | - * |
|
| 328 | - * @return string |
|
| 329 | - */ |
|
| 330 | - public function toCompact(): string |
|
| 331 | - { |
|
| 332 | - return Base64::urlEncode($this->_protectedHeader->toJSON()) . '.' . |
|
| 333 | - Base64::urlEncode($this->_encryptedKey) . '.' . |
|
| 334 | - Base64::urlEncode($this->_iv) . '.' . |
|
| 335 | - Base64::urlEncode($this->_ciphertext) . '.' . |
|
| 336 | - Base64::urlEncode($this->_authenticationTag); |
|
| 337 | - } |
|
| 325 | + /** |
|
| 326 | + * Convert to compact serialization. |
|
| 327 | + * |
|
| 328 | + * @return string |
|
| 329 | + */ |
|
| 330 | + public function toCompact(): string |
|
| 331 | + { |
|
| 332 | + return Base64::urlEncode($this->_protectedHeader->toJSON()) . '.' . |
|
| 333 | + Base64::urlEncode($this->_encryptedKey) . '.' . |
|
| 334 | + Base64::urlEncode($this->_iv) . '.' . |
|
| 335 | + Base64::urlEncode($this->_ciphertext) . '.' . |
|
| 336 | + Base64::urlEncode($this->_authenticationTag); |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | - /** |
|
| 340 | - * Encrypt content with explicit parameters. |
|
| 341 | - * |
|
| 342 | - * @param string $plaintext Plaintext content to encrypt |
|
| 343 | - * @param string $cek Content encryption key |
|
| 344 | - * @param string $iv Initialization vector |
|
| 345 | - * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
| 346 | - * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
| 347 | - * @param Header $header Header |
|
| 348 | - * |
|
| 349 | - * @throws \UnexpectedValueException |
|
| 350 | - * |
|
| 351 | - * @return self |
|
| 352 | - */ |
|
| 353 | - private static function _encryptContent(string $plaintext, string $cek, |
|
| 354 | - string $iv, KeyManagementAlgorithm $key_algo, |
|
| 355 | - ContentEncryptionAlgorithm $enc_algo, Header $header): self |
|
| 356 | - { |
|
| 357 | - // check that content encryption key has correct size |
|
| 358 | - if (strlen($cek) !== $enc_algo->keySize()) { |
|
| 359 | - throw new \UnexpectedValueException('Invalid key size.'); |
|
| 360 | - } |
|
| 361 | - // check that initialization vector has correct size |
|
| 362 | - if (strlen($iv) !== $enc_algo->ivSize()) { |
|
| 363 | - throw new \UnexpectedValueException('Invalid IV size.'); |
|
| 364 | - } |
|
| 365 | - // add key and encryption algorithm parameters to the header |
|
| 366 | - $header = $header->withParameters(...$key_algo->headerParameters()) |
|
| 367 | - ->withParameters(...$enc_algo->headerParameters()); |
|
| 368 | - // encrypt the content encryption key |
|
| 369 | - $encrypted_key = $key_algo->encrypt($cek, $header); |
|
| 370 | - // sanity check that header wasn't unset via reference |
|
| 371 | - if (!$header instanceof Header) { |
|
| 372 | - throw new \RuntimeException('Broken key algorithm.'); |
|
| 373 | - } |
|
| 374 | - // additional authenticated data |
|
| 375 | - $aad = Base64::urlEncode($header->toJSON()); |
|
| 376 | - // encrypt |
|
| 377 | - [$ciphertext, $auth_tag] = $enc_algo->encrypt($plaintext, $cek, $iv, $aad); |
|
| 378 | - // TODO: should aad be passed |
|
| 379 | - return new self($header, $encrypted_key, $iv, $ciphertext, $auth_tag); |
|
| 380 | - } |
|
| 339 | + /** |
|
| 340 | + * Encrypt content with explicit parameters. |
|
| 341 | + * |
|
| 342 | + * @param string $plaintext Plaintext content to encrypt |
|
| 343 | + * @param string $cek Content encryption key |
|
| 344 | + * @param string $iv Initialization vector |
|
| 345 | + * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
| 346 | + * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
| 347 | + * @param Header $header Header |
|
| 348 | + * |
|
| 349 | + * @throws \UnexpectedValueException |
|
| 350 | + * |
|
| 351 | + * @return self |
|
| 352 | + */ |
|
| 353 | + private static function _encryptContent(string $plaintext, string $cek, |
|
| 354 | + string $iv, KeyManagementAlgorithm $key_algo, |
|
| 355 | + ContentEncryptionAlgorithm $enc_algo, Header $header): self |
|
| 356 | + { |
|
| 357 | + // check that content encryption key has correct size |
|
| 358 | + if (strlen($cek) !== $enc_algo->keySize()) { |
|
| 359 | + throw new \UnexpectedValueException('Invalid key size.'); |
|
| 360 | + } |
|
| 361 | + // check that initialization vector has correct size |
|
| 362 | + if (strlen($iv) !== $enc_algo->ivSize()) { |
|
| 363 | + throw new \UnexpectedValueException('Invalid IV size.'); |
|
| 364 | + } |
|
| 365 | + // add key and encryption algorithm parameters to the header |
|
| 366 | + $header = $header->withParameters(...$key_algo->headerParameters()) |
|
| 367 | + ->withParameters(...$enc_algo->headerParameters()); |
|
| 368 | + // encrypt the content encryption key |
|
| 369 | + $encrypted_key = $key_algo->encrypt($cek, $header); |
|
| 370 | + // sanity check that header wasn't unset via reference |
|
| 371 | + if (!$header instanceof Header) { |
|
| 372 | + throw new \RuntimeException('Broken key algorithm.'); |
|
| 373 | + } |
|
| 374 | + // additional authenticated data |
|
| 375 | + $aad = Base64::urlEncode($header->toJSON()); |
|
| 376 | + // encrypt |
|
| 377 | + [$ciphertext, $auth_tag] = $enc_algo->encrypt($plaintext, $cek, $iv, $aad); |
|
| 378 | + // TODO: should aad be passed |
|
| 379 | + return new self($header, $encrypted_key, $iv, $ciphertext, $auth_tag); |
|
| 380 | + } |
|
| 381 | 381 | } |
@@ -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; |
| 6 | 6 | |