@@ -15,91 +15,91 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class SignatureAlgorithmFactory |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Mapping from algorithm name to class name. |
|
| 20 | - * |
|
| 21 | - * @internal |
|
| 22 | - * |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - const MAP_ALGO_TO_CLASS = [ |
|
| 26 | - JWA::ALGO_HS256 => HS256Algorithm::class, |
|
| 27 | - JWA::ALGO_HS384 => HS384Algorithm::class, |
|
| 28 | - JWA::ALGO_HS512 => HS512Algorithm::class, |
|
| 29 | - JWA::ALGO_RS256 => RS256Algorithm::class, |
|
| 30 | - JWA::ALGO_RS384 => RS384Algorithm::class, |
|
| 31 | - JWA::ALGO_RS512 => RS512Algorithm::class, |
|
| 32 | - JWA::ALGO_ES256 => ES256Algorithm::class, |
|
| 33 | - JWA::ALGO_ES384 => ES384Algorithm::class, |
|
| 34 | - JWA::ALGO_ES512 => ES512Algorithm::class, |
|
| 35 | - ]; |
|
| 36 | - /** |
|
| 37 | - * Header. |
|
| 38 | - * |
|
| 39 | - * @var Header |
|
| 40 | - */ |
|
| 41 | - protected $_header; |
|
| 18 | + /** |
|
| 19 | + * Mapping from algorithm name to class name. |
|
| 20 | + * |
|
| 21 | + * @internal |
|
| 22 | + * |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + const MAP_ALGO_TO_CLASS = [ |
|
| 26 | + JWA::ALGO_HS256 => HS256Algorithm::class, |
|
| 27 | + JWA::ALGO_HS384 => HS384Algorithm::class, |
|
| 28 | + JWA::ALGO_HS512 => HS512Algorithm::class, |
|
| 29 | + JWA::ALGO_RS256 => RS256Algorithm::class, |
|
| 30 | + JWA::ALGO_RS384 => RS384Algorithm::class, |
|
| 31 | + JWA::ALGO_RS512 => RS512Algorithm::class, |
|
| 32 | + JWA::ALGO_ES256 => ES256Algorithm::class, |
|
| 33 | + JWA::ALGO_ES384 => ES384Algorithm::class, |
|
| 34 | + JWA::ALGO_ES512 => ES512Algorithm::class, |
|
| 35 | + ]; |
|
| 36 | + /** |
|
| 37 | + * Header. |
|
| 38 | + * |
|
| 39 | + * @var Header |
|
| 40 | + */ |
|
| 41 | + protected $_header; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Constructor. |
|
| 45 | - * |
|
| 46 | - * @param Header $header |
|
| 47 | - */ |
|
| 48 | - public function __construct(Header $header) |
|
| 49 | - { |
|
| 50 | - $this->_header = $header; |
|
| 51 | - } |
|
| 43 | + /** |
|
| 44 | + * Constructor. |
|
| 45 | + * |
|
| 46 | + * @param Header $header |
|
| 47 | + */ |
|
| 48 | + public function __construct(Header $header) |
|
| 49 | + { |
|
| 50 | + $this->_header = $header; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Get signature algorithm by given JWK. |
|
| 55 | - * |
|
| 56 | - * @param JWK $jwk |
|
| 57 | - * |
|
| 58 | - * @return SignatureAlgorithm |
|
| 59 | - */ |
|
| 60 | - public function algoByKey(JWK $jwk): SignatureAlgorithm |
|
| 61 | - { |
|
| 62 | - $alg = JWA::deriveAlgorithmName($this->_header, $jwk); |
|
| 63 | - $cls = self::_algoClassByName($alg); |
|
| 64 | - return $cls::fromJWK($jwk, $this->_header); |
|
| 65 | - } |
|
| 53 | + /** |
|
| 54 | + * Get signature algorithm by given JWK. |
|
| 55 | + * |
|
| 56 | + * @param JWK $jwk |
|
| 57 | + * |
|
| 58 | + * @return SignatureAlgorithm |
|
| 59 | + */ |
|
| 60 | + public function algoByKey(JWK $jwk): SignatureAlgorithm |
|
| 61 | + { |
|
| 62 | + $alg = JWA::deriveAlgorithmName($this->_header, $jwk); |
|
| 63 | + $cls = self::_algoClassByName($alg); |
|
| 64 | + return $cls::fromJWK($jwk, $this->_header); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Get signature algorithm using a matching key from given JWK set. |
|
| 69 | - * |
|
| 70 | - * @param JWKSet $set |
|
| 71 | - * |
|
| 72 | - * @throws \UnexpectedValueException If a key cannot be found |
|
| 73 | - * |
|
| 74 | - * @return SignatureAlgorithm |
|
| 75 | - */ |
|
| 76 | - public function algoByKeys(JWKSet $set): SignatureAlgorithm |
|
| 77 | - { |
|
| 78 | - if (!$this->_header->hasKeyID()) { |
|
| 79 | - throw new \UnexpectedValueException('No key ID paremeter.'); |
|
| 80 | - } |
|
| 81 | - $id = $this->_header->keyID()->value(); |
|
| 82 | - if (!$set->hasKeyID($id)) { |
|
| 83 | - throw new \UnexpectedValueException("No key for ID '{$id}'."); |
|
| 84 | - } |
|
| 85 | - return $this->algoByKey($set->keyByID($id)); |
|
| 86 | - } |
|
| 67 | + /** |
|
| 68 | + * Get signature algorithm using a matching key from given JWK set. |
|
| 69 | + * |
|
| 70 | + * @param JWKSet $set |
|
| 71 | + * |
|
| 72 | + * @throws \UnexpectedValueException If a key cannot be found |
|
| 73 | + * |
|
| 74 | + * @return SignatureAlgorithm |
|
| 75 | + */ |
|
| 76 | + public function algoByKeys(JWKSet $set): SignatureAlgorithm |
|
| 77 | + { |
|
| 78 | + if (!$this->_header->hasKeyID()) { |
|
| 79 | + throw new \UnexpectedValueException('No key ID paremeter.'); |
|
| 80 | + } |
|
| 81 | + $id = $this->_header->keyID()->value(); |
|
| 82 | + if (!$set->hasKeyID($id)) { |
|
| 83 | + throw new \UnexpectedValueException("No key for ID '{$id}'."); |
|
| 84 | + } |
|
| 85 | + return $this->algoByKey($set->keyByID($id)); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Get the algorithm implementation class name by an algorithm name. |
|
| 90 | - * |
|
| 91 | - * @param string $alg Algorithm name |
|
| 92 | - * |
|
| 93 | - * @throws \UnexpectedValueException |
|
| 94 | - * |
|
| 95 | - * @return string Class name |
|
| 96 | - */ |
|
| 97 | - private static function _algoClassByName(string $alg): string |
|
| 98 | - { |
|
| 99 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 100 | - throw new \UnexpectedValueException( |
|
| 101 | - "Algorithm '{$alg}' not supported."); |
|
| 102 | - } |
|
| 103 | - return self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 104 | - } |
|
| 88 | + /** |
|
| 89 | + * Get the algorithm implementation class name by an algorithm name. |
|
| 90 | + * |
|
| 91 | + * @param string $alg Algorithm name |
|
| 92 | + * |
|
| 93 | + * @throws \UnexpectedValueException |
|
| 94 | + * |
|
| 95 | + * @return string Class name |
|
| 96 | + */ |
|
| 97 | + private static function _algoClassByName(string $alg): string |
|
| 98 | + { |
|
| 99 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 100 | + throw new \UnexpectedValueException( |
|
| 101 | + "Algorithm '{$alg}' not supported."); |
|
| 102 | + } |
|
| 103 | + return self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 104 | + } |
|
| 105 | 105 | } |
@@ -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 ES256Algorithm extends ECDSAAlgorithm |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * {@inheritdoc} |
|
| 19 | - */ |
|
| 20 | - public function algorithmParamValue(): string |
|
| 21 | - { |
|
| 22 | - return JWA::ALGO_ES256; |
|
| 23 | - } |
|
| 17 | + /** |
|
| 18 | + * {@inheritdoc} |
|
| 19 | + */ |
|
| 20 | + public function algorithmParamValue(): string |
|
| 21 | + { |
|
| 22 | + return JWA::ALGO_ES256; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * {@inheritdoc} |
|
| 27 | - */ |
|
| 28 | - protected function _curveName(): string |
|
| 29 | - { |
|
| 30 | - return CurveParameter::CURVE_P256; |
|
| 31 | - } |
|
| 25 | + /** |
|
| 26 | + * {@inheritdoc} |
|
| 27 | + */ |
|
| 28 | + protected function _curveName(): string |
|
| 29 | + { |
|
| 30 | + return CurveParameter::CURVE_P256; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * {@inheritdoc} |
|
| 35 | - */ |
|
| 36 | - protected function _mdMethod(): int |
|
| 37 | - { |
|
| 38 | - return OPENSSL_ALGO_SHA256; |
|
| 39 | - } |
|
| 33 | + /** |
|
| 34 | + * {@inheritdoc} |
|
| 35 | + */ |
|
| 36 | + protected function _mdMethod(): int |
|
| 37 | + { |
|
| 38 | + return OPENSSL_ALGO_SHA256; |
|
| 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 HS512Algorithm extends HMACAlgorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - public function algorithmParamValue(): string |
|
| 20 | - { |
|
| 21 | - return JWA::ALGO_HS512; |
|
| 22 | - } |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + public function algorithmParamValue(): string |
|
| 20 | + { |
|
| 21 | + return JWA::ALGO_HS512; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - protected function _hashAlgo(): string |
|
| 28 | - { |
|
| 29 | - return 'sha512'; |
|
| 30 | - } |
|
| 24 | + /** |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + protected function _hashAlgo(): string |
|
| 28 | + { |
|
| 29 | + return 'sha512'; |
|
| 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 | |
@@ -16,36 +16,36 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class NoneAlgorithm extends SignatureAlgorithm |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * {@inheritdoc} |
|
| 21 | - */ |
|
| 22 | - public function algorithmParamValue(): string |
|
| 23 | - { |
|
| 24 | - return JWA::ALGO_NONE; |
|
| 25 | - } |
|
| 19 | + /** |
|
| 20 | + * {@inheritdoc} |
|
| 21 | + */ |
|
| 22 | + public function algorithmParamValue(): string |
|
| 23 | + { |
|
| 24 | + return JWA::ALGO_NONE; |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * {@inheritdoc} |
|
| 29 | - */ |
|
| 30 | - public function computeSignature(string $data): string |
|
| 31 | - { |
|
| 32 | - return ''; |
|
| 33 | - } |
|
| 27 | + /** |
|
| 28 | + * {@inheritdoc} |
|
| 29 | + */ |
|
| 30 | + public function computeSignature(string $data): string |
|
| 31 | + { |
|
| 32 | + return ''; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * {@inheritdoc} |
|
| 37 | - */ |
|
| 38 | - public function validateSignature(string $data, string $signature): bool |
|
| 39 | - { |
|
| 40 | - return '' === $signature; |
|
| 41 | - } |
|
| 35 | + /** |
|
| 36 | + * {@inheritdoc} |
|
| 37 | + */ |
|
| 38 | + public function validateSignature(string $data, string $signature): bool |
|
| 39 | + { |
|
| 40 | + return '' === $signature; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * {@inheritdoc} |
|
| 45 | - */ |
|
| 46 | - public function headerParameters(): array |
|
| 47 | - { |
|
| 48 | - return array_merge(parent::headerParameters(), |
|
| 49 | - [AlgorithmParameter::fromAlgorithm($this)]); |
|
| 50 | - } |
|
| 43 | + /** |
|
| 44 | + * {@inheritdoc} |
|
| 45 | + */ |
|
| 46 | + public function headerParameters(): array |
|
| 47 | + { |
|
| 48 | + return array_merge(parent::headerParameters(), |
|
| 49 | + [AlgorithmParameter::fromAlgorithm($this)]); |
|
| 50 | + } |
|
| 51 | 51 | } |
@@ -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 | |
@@ -19,80 +19,80 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | abstract class RSASSAPKCS1Algorithm extends OpenSSLSignatureAlgorithm |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * Mapping from algorithm name to class name. |
|
| 24 | - * |
|
| 25 | - * @internal |
|
| 26 | - * |
|
| 27 | - * @var array |
|
| 28 | - */ |
|
| 29 | - const MAP_ALGO_TO_CLASS = [ |
|
| 30 | - JWA::ALGO_RS256 => RS256Algorithm::class, |
|
| 31 | - JWA::ALGO_RS384 => RS384Algorithm::class, |
|
| 32 | - JWA::ALGO_RS512 => RS512Algorithm::class, |
|
| 33 | - ]; |
|
| 22 | + /** |
|
| 23 | + * Mapping from algorithm name to class name. |
|
| 24 | + * |
|
| 25 | + * @internal |
|
| 26 | + * |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + const MAP_ALGO_TO_CLASS = [ |
|
| 30 | + JWA::ALGO_RS256 => RS256Algorithm::class, |
|
| 31 | + JWA::ALGO_RS384 => RS384Algorithm::class, |
|
| 32 | + JWA::ALGO_RS512 => RS512Algorithm::class, |
|
| 33 | + ]; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Constructor. |
|
| 37 | - * |
|
| 38 | - * @param RSAPublicKeyJWK $pub_key |
|
| 39 | - * @param RSAPrivateKeyJWK $priv_key |
|
| 40 | - */ |
|
| 41 | - protected function __construct(RSAPublicKeyJWK $pub_key, |
|
| 42 | - RSAPrivateKeyJWK $priv_key = null) |
|
| 43 | - { |
|
| 44 | - $this->_publicKey = $pub_key; |
|
| 45 | - $this->_privateKey = $priv_key; |
|
| 46 | - } |
|
| 35 | + /** |
|
| 36 | + * Constructor. |
|
| 37 | + * |
|
| 38 | + * @param RSAPublicKeyJWK $pub_key |
|
| 39 | + * @param RSAPrivateKeyJWK $priv_key |
|
| 40 | + */ |
|
| 41 | + protected function __construct(RSAPublicKeyJWK $pub_key, |
|
| 42 | + RSAPrivateKeyJWK $priv_key = null) |
|
| 43 | + { |
|
| 44 | + $this->_publicKey = $pub_key; |
|
| 45 | + $this->_privateKey = $priv_key; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Initialize from a public key. |
|
| 50 | - * |
|
| 51 | - * @param RSAPublicKeyJWK $jwk |
|
| 52 | - * |
|
| 53 | - * @return self |
|
| 54 | - */ |
|
| 55 | - public static function fromPublicKey(RSAPublicKeyJWK $jwk): self |
|
| 56 | - { |
|
| 57 | - return new static($jwk); |
|
| 58 | - } |
|
| 48 | + /** |
|
| 49 | + * Initialize from a public key. |
|
| 50 | + * |
|
| 51 | + * @param RSAPublicKeyJWK $jwk |
|
| 52 | + * |
|
| 53 | + * @return self |
|
| 54 | + */ |
|
| 55 | + public static function fromPublicKey(RSAPublicKeyJWK $jwk): self |
|
| 56 | + { |
|
| 57 | + return new static($jwk); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Initialize from a private key. |
|
| 62 | - * |
|
| 63 | - * @param RSAPrivateKeyJWK $jwk |
|
| 64 | - * |
|
| 65 | - * @return self |
|
| 66 | - */ |
|
| 67 | - public static function fromPrivateKey(RSAPrivateKeyJWK $jwk): self |
|
| 68 | - { |
|
| 69 | - return new static($jwk->publicKey(), $jwk); |
|
| 70 | - } |
|
| 60 | + /** |
|
| 61 | + * Initialize from a private key. |
|
| 62 | + * |
|
| 63 | + * @param RSAPrivateKeyJWK $jwk |
|
| 64 | + * |
|
| 65 | + * @return self |
|
| 66 | + */ |
|
| 67 | + public static function fromPrivateKey(RSAPrivateKeyJWK $jwk): self |
|
| 68 | + { |
|
| 69 | + return new static($jwk->publicKey(), $jwk); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * {@inheritdoc} |
|
| 74 | - * |
|
| 75 | - * @return self |
|
| 76 | - */ |
|
| 77 | - public static function fromJWK(JWK $jwk, Header $header): SignatureAlgorithm |
|
| 78 | - { |
|
| 79 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 80 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 81 | - throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
| 82 | - } |
|
| 83 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 84 | - if ($jwk->has(...RSAPrivateKeyJWK::MANAGED_PARAMS)) { |
|
| 85 | - return $cls::fromPrivateKey(RSAPrivateKeyJWK::fromJWK($jwk)); |
|
| 86 | - } |
|
| 87 | - return $cls::fromPublicKey(RSAPublicKeyJWK::fromJWK($jwk)); |
|
| 88 | - } |
|
| 72 | + /** |
|
| 73 | + * {@inheritdoc} |
|
| 74 | + * |
|
| 75 | + * @return self |
|
| 76 | + */ |
|
| 77 | + public static function fromJWK(JWK $jwk, Header $header): SignatureAlgorithm |
|
| 78 | + { |
|
| 79 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 80 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 81 | + throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
| 82 | + } |
|
| 83 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 84 | + if ($jwk->has(...RSAPrivateKeyJWK::MANAGED_PARAMS)) { |
|
| 85 | + return $cls::fromPrivateKey(RSAPrivateKeyJWK::fromJWK($jwk)); |
|
| 86 | + } |
|
| 87 | + return $cls::fromPublicKey(RSAPublicKeyJWK::fromJWK($jwk)); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * {@inheritdoc} |
|
| 92 | - */ |
|
| 93 | - public function headerParameters(): array |
|
| 94 | - { |
|
| 95 | - return array_merge(parent::headerParameters(), |
|
| 96 | - [AlgorithmParameter::fromAlgorithm($this)]); |
|
| 97 | - } |
|
| 90 | + /** |
|
| 91 | + * {@inheritdoc} |
|
| 92 | + */ |
|
| 93 | + public function headerParameters(): array |
|
| 94 | + { |
|
| 95 | + return array_merge(parent::headerParameters(), |
|
| 96 | + [AlgorithmParameter::fromAlgorithm($this)]); |
|
| 97 | + } |
|
| 98 | 98 | } |
@@ -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 RS512Algorithm extends RSASSAPKCS1Algorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - public function algorithmParamValue(): string |
|
| 20 | - { |
|
| 21 | - return JWA::ALGO_RS512; |
|
| 22 | - } |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + public function algorithmParamValue(): string |
|
| 20 | + { |
|
| 21 | + return JWA::ALGO_RS512; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - protected function _mdMethod(): int |
|
| 28 | - { |
|
| 29 | - return OPENSSL_ALGO_SHA512; |
|
| 30 | - } |
|
| 24 | + /** |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + protected function _mdMethod(): int |
|
| 28 | + { |
|
| 29 | + return OPENSSL_ALGO_SHA512; |
|
| 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 HS384Algorithm extends HMACAlgorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - public function algorithmParamValue(): string |
|
| 20 | - { |
|
| 21 | - return JWA::ALGO_HS384; |
|
| 22 | - } |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + public function algorithmParamValue(): string |
|
| 20 | + { |
|
| 21 | + return JWA::ALGO_HS384; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - protected function _hashAlgo(): string |
|
| 28 | - { |
|
| 29 | - return 'sha384'; |
|
| 30 | - } |
|
| 24 | + /** |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + protected function _hashAlgo(): string |
|
| 28 | + { |
|
| 29 | + return 'sha384'; |
|
| 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 | |
@@ -14,27 +14,27 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class ES384Algorithm extends ECDSAAlgorithm |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * {@inheritdoc} |
|
| 19 | - */ |
|
| 20 | - public function algorithmParamValue(): string |
|
| 21 | - { |
|
| 22 | - return JWA::ALGO_ES384; |
|
| 23 | - } |
|
| 17 | + /** |
|
| 18 | + * {@inheritdoc} |
|
| 19 | + */ |
|
| 20 | + public function algorithmParamValue(): string |
|
| 21 | + { |
|
| 22 | + return JWA::ALGO_ES384; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * {@inheritdoc} |
|
| 27 | - */ |
|
| 28 | - protected function _curveName(): string |
|
| 29 | - { |
|
| 30 | - return CurveParameter::CURVE_P384; |
|
| 31 | - } |
|
| 25 | + /** |
|
| 26 | + * {@inheritdoc} |
|
| 27 | + */ |
|
| 28 | + protected function _curveName(): string |
|
| 29 | + { |
|
| 30 | + return CurveParameter::CURVE_P384; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * {@inheritdoc} |
|
| 35 | - */ |
|
| 36 | - protected function _mdMethod(): int |
|
| 37 | - { |
|
| 38 | - return OPENSSL_ALGO_SHA384; |
|
| 39 | - } |
|
| 33 | + /** |
|
| 34 | + * {@inheritdoc} |
|
| 35 | + */ |
|
| 36 | + protected function _mdMethod(): int |
|
| 37 | + { |
|
| 38 | + return OPENSSL_ALGO_SHA384; |
|
| 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 | |
@@ -21,134 +21,134 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | abstract class ECDSAAlgorithm extends OpenSSLSignatureAlgorithm |
| 23 | 23 | { |
| 24 | - /** |
|
| 25 | - * Mapping from algorithm name to class name. |
|
| 26 | - * |
|
| 27 | - * @internal |
|
| 28 | - * |
|
| 29 | - * @var array |
|
| 30 | - */ |
|
| 31 | - const MAP_ALGO_TO_CLASS = [ |
|
| 32 | - JWA::ALGO_ES256 => ES256Algorithm::class, |
|
| 33 | - JWA::ALGO_ES384 => ES384Algorithm::class, |
|
| 34 | - JWA::ALGO_ES512 => ES512Algorithm::class, |
|
| 35 | - ]; |
|
| 24 | + /** |
|
| 25 | + * Mapping from algorithm name to class name. |
|
| 26 | + * |
|
| 27 | + * @internal |
|
| 28 | + * |
|
| 29 | + * @var array |
|
| 30 | + */ |
|
| 31 | + const MAP_ALGO_TO_CLASS = [ |
|
| 32 | + JWA::ALGO_ES256 => ES256Algorithm::class, |
|
| 33 | + JWA::ALGO_ES384 => ES384Algorithm::class, |
|
| 34 | + JWA::ALGO_ES512 => ES512Algorithm::class, |
|
| 35 | + ]; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Signature size in bytes. |
|
| 39 | - * |
|
| 40 | - * @var int |
|
| 41 | - */ |
|
| 42 | - private $_signatureSize; |
|
| 37 | + /** |
|
| 38 | + * Signature size in bytes. |
|
| 39 | + * |
|
| 40 | + * @var int |
|
| 41 | + */ |
|
| 42 | + private $_signatureSize; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Constructor. |
|
| 46 | - * |
|
| 47 | - * @param ECPublicKeyJWK $pub_key |
|
| 48 | - * @param ECPrivateKeyJWK $priv_key |
|
| 49 | - */ |
|
| 50 | - protected function __construct(ECPublicKeyJWK $pub_key, |
|
| 51 | - ?ECPrivateKeyJWK $priv_key = null) |
|
| 52 | - { |
|
| 53 | - $curve = $pub_key->curveParameter()->value(); |
|
| 54 | - if ($this->_curveName() !== $curve) { |
|
| 55 | - throw new \InvalidArgumentException( |
|
| 56 | - 'Key with ' . $this->_curveName() . |
|
| 57 | - " curve expected, got {$curve}."); |
|
| 58 | - } |
|
| 59 | - $this->_publicKey = $pub_key; |
|
| 60 | - $this->_privateKey = $priv_key; |
|
| 61 | - $key_size = $pub_key->curveParameter()->keySizeBits(); |
|
| 62 | - $this->_signatureSize = intval(ceil($key_size / 8) * 2); |
|
| 63 | - } |
|
| 44 | + /** |
|
| 45 | + * Constructor. |
|
| 46 | + * |
|
| 47 | + * @param ECPublicKeyJWK $pub_key |
|
| 48 | + * @param ECPrivateKeyJWK $priv_key |
|
| 49 | + */ |
|
| 50 | + protected function __construct(ECPublicKeyJWK $pub_key, |
|
| 51 | + ?ECPrivateKeyJWK $priv_key = null) |
|
| 52 | + { |
|
| 53 | + $curve = $pub_key->curveParameter()->value(); |
|
| 54 | + if ($this->_curveName() !== $curve) { |
|
| 55 | + throw new \InvalidArgumentException( |
|
| 56 | + 'Key with ' . $this->_curveName() . |
|
| 57 | + " curve expected, got {$curve}."); |
|
| 58 | + } |
|
| 59 | + $this->_publicKey = $pub_key; |
|
| 60 | + $this->_privateKey = $priv_key; |
|
| 61 | + $key_size = $pub_key->curveParameter()->keySizeBits(); |
|
| 62 | + $this->_signatureSize = intval(ceil($key_size / 8) * 2); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Initialize from a public key. |
|
| 67 | - * |
|
| 68 | - * @param ECPublicKeyJWK $jwk |
|
| 69 | - * |
|
| 70 | - * @return self |
|
| 71 | - */ |
|
| 72 | - public static function fromPublicKey(ECPublicKeyJWK $jwk): self |
|
| 73 | - { |
|
| 74 | - return new static($jwk); |
|
| 75 | - } |
|
| 65 | + /** |
|
| 66 | + * Initialize from a public key. |
|
| 67 | + * |
|
| 68 | + * @param ECPublicKeyJWK $jwk |
|
| 69 | + * |
|
| 70 | + * @return self |
|
| 71 | + */ |
|
| 72 | + public static function fromPublicKey(ECPublicKeyJWK $jwk): self |
|
| 73 | + { |
|
| 74 | + return new static($jwk); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Initialize from a private key. |
|
| 79 | - * |
|
| 80 | - * @param ECPrivateKeyJWK $jwk |
|
| 81 | - * |
|
| 82 | - * @return self |
|
| 83 | - */ |
|
| 84 | - public static function fromPrivateKey(ECPrivateKeyJWK $jwk): self |
|
| 85 | - { |
|
| 86 | - return new static($jwk->publicKey(), $jwk); |
|
| 87 | - } |
|
| 77 | + /** |
|
| 78 | + * Initialize from a private key. |
|
| 79 | + * |
|
| 80 | + * @param ECPrivateKeyJWK $jwk |
|
| 81 | + * |
|
| 82 | + * @return self |
|
| 83 | + */ |
|
| 84 | + public static function fromPrivateKey(ECPrivateKeyJWK $jwk): self |
|
| 85 | + { |
|
| 86 | + return new static($jwk->publicKey(), $jwk); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * {@inheritdoc} |
|
| 91 | - * |
|
| 92 | - * @return self |
|
| 93 | - */ |
|
| 94 | - public static function fromJWK(JWK $jwk, Header $header): SignatureAlgorithm |
|
| 95 | - { |
|
| 96 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 97 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 98 | - throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
| 99 | - } |
|
| 100 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 101 | - if ($jwk->has(...ECPrivateKeyJWK::MANAGED_PARAMS)) { |
|
| 102 | - return $cls::fromPrivateKey(ECPrivateKeyJWK::fromJWK($jwk)); |
|
| 103 | - } |
|
| 104 | - return $cls::fromPublicKey(ECPublicKeyJWK::fromJWK($jwk)); |
|
| 105 | - } |
|
| 89 | + /** |
|
| 90 | + * {@inheritdoc} |
|
| 91 | + * |
|
| 92 | + * @return self |
|
| 93 | + */ |
|
| 94 | + public static function fromJWK(JWK $jwk, Header $header): SignatureAlgorithm |
|
| 95 | + { |
|
| 96 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 97 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 98 | + throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
| 99 | + } |
|
| 100 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 101 | + if ($jwk->has(...ECPrivateKeyJWK::MANAGED_PARAMS)) { |
|
| 102 | + return $cls::fromPrivateKey(ECPrivateKeyJWK::fromJWK($jwk)); |
|
| 103 | + } |
|
| 104 | + return $cls::fromPublicKey(ECPublicKeyJWK::fromJWK($jwk)); |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * {@inheritdoc} |
|
| 109 | - */ |
|
| 110 | - public function computeSignature(string $data): string |
|
| 111 | - { |
|
| 112 | - // OpenSSL returns ECDSA signature as a DER encoded ECDSA-Sig-Value |
|
| 113 | - $der = parent::computeSignature($data); |
|
| 114 | - $sig = ECSignature::fromDER($der); |
|
| 115 | - $mlen = intval(floor($this->_signatureSize / 2)); |
|
| 116 | - return ECConversion::numberToOctets($sig->r(), $mlen) . |
|
| 117 | - ECConversion::numberToOctets($sig->s(), $mlen); |
|
| 118 | - } |
|
| 107 | + /** |
|
| 108 | + * {@inheritdoc} |
|
| 109 | + */ |
|
| 110 | + public function computeSignature(string $data): string |
|
| 111 | + { |
|
| 112 | + // OpenSSL returns ECDSA signature as a DER encoded ECDSA-Sig-Value |
|
| 113 | + $der = parent::computeSignature($data); |
|
| 114 | + $sig = ECSignature::fromDER($der); |
|
| 115 | + $mlen = intval(floor($this->_signatureSize / 2)); |
|
| 116 | + return ECConversion::numberToOctets($sig->r(), $mlen) . |
|
| 117 | + ECConversion::numberToOctets($sig->s(), $mlen); |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * {@inheritdoc} |
|
| 122 | - * |
|
| 123 | - * @throws \UnexpectedValueException If signature length is invalid |
|
| 124 | - */ |
|
| 125 | - public function validateSignature(string $data, string $signature): bool |
|
| 126 | - { |
|
| 127 | - if (strlen($signature) !== $this->_signatureSize) { |
|
| 128 | - throw new \UnexpectedValueException('Invalid signature length.'); |
|
| 129 | - } |
|
| 130 | - [$r_octets, $s_octets] = str_split($signature, |
|
| 131 | - intval(floor($this->_signatureSize / 2))); |
|
| 132 | - // convert signature to DER sequence for OpenSSL |
|
| 133 | - $r = ECConversion::octetsToNumber($r_octets); |
|
| 134 | - $s = ECConversion::octetsToNumber($s_octets); |
|
| 135 | - $sig = new ECSignature($r, $s); |
|
| 136 | - return parent::validateSignature($data, $sig->toDER()); |
|
| 137 | - } |
|
| 120 | + /** |
|
| 121 | + * {@inheritdoc} |
|
| 122 | + * |
|
| 123 | + * @throws \UnexpectedValueException If signature length is invalid |
|
| 124 | + */ |
|
| 125 | + public function validateSignature(string $data, string $signature): bool |
|
| 126 | + { |
|
| 127 | + if (strlen($signature) !== $this->_signatureSize) { |
|
| 128 | + throw new \UnexpectedValueException('Invalid signature length.'); |
|
| 129 | + } |
|
| 130 | + [$r_octets, $s_octets] = str_split($signature, |
|
| 131 | + intval(floor($this->_signatureSize / 2))); |
|
| 132 | + // convert signature to DER sequence for OpenSSL |
|
| 133 | + $r = ECConversion::octetsToNumber($r_octets); |
|
| 134 | + $s = ECConversion::octetsToNumber($s_octets); |
|
| 135 | + $sig = new ECSignature($r, $s); |
|
| 136 | + return parent::validateSignature($data, $sig->toDER()); |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * {@inheritdoc} |
|
| 141 | - */ |
|
| 142 | - public function headerParameters(): array |
|
| 143 | - { |
|
| 144 | - return array_merge(parent::headerParameters(), |
|
| 145 | - [AlgorithmParameter::fromAlgorithm($this)]); |
|
| 146 | - } |
|
| 139 | + /** |
|
| 140 | + * {@inheritdoc} |
|
| 141 | + */ |
|
| 142 | + public function headerParameters(): array |
|
| 143 | + { |
|
| 144 | + return array_merge(parent::headerParameters(), |
|
| 145 | + [AlgorithmParameter::fromAlgorithm($this)]); |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - /** |
|
| 149 | - * Get the name of the curve used by this algorithm. |
|
| 150 | - * |
|
| 151 | - * @return string |
|
| 152 | - */ |
|
| 153 | - abstract protected function _curveName(): string; |
|
| 148 | + /** |
|
| 149 | + * Get the name of the curve used by this algorithm. |
|
| 150 | + * |
|
| 151 | + * @return string |
|
| 152 | + */ |
|
| 153 | + abstract protected function _curveName(): string; |
|
| 154 | 154 | } |
@@ -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 | |