@@ -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 | } |
@@ -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 | } |
@@ -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 | } |
@@ -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 | } |
@@ -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 | } |
@@ -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 | } |
@@ -13,19 +13,19 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class RS384Algorithm extends RSASSAPKCS1Algorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - public function algorithmParamValue(): string |
|
| 20 | - { |
|
| 21 | - return JWA::ALGO_RS384; |
|
| 22 | - } |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + public function algorithmParamValue(): string |
|
| 20 | + { |
|
| 21 | + return JWA::ALGO_RS384; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - protected function _mdMethod(): int |
|
| 28 | - { |
|
| 29 | - return OPENSSL_ALGO_SHA384; |
|
| 30 | - } |
|
| 24 | + /** |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + protected function _mdMethod(): int |
|
| 28 | + { |
|
| 29 | + return OPENSSL_ALGO_SHA384; |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -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 | } |
@@ -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 | } |