@@ -18,92 +18,92 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | abstract class HMACAlgorithm extends SignatureAlgorithm |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Shared secret key. |
|
| 23 | - * |
|
| 24 | - * @var string $_key |
|
| 25 | - */ |
|
| 26 | - protected $_key; |
|
| 21 | + /** |
|
| 22 | + * Shared secret key. |
|
| 23 | + * |
|
| 24 | + * @var string $_key |
|
| 25 | + */ |
|
| 26 | + protected $_key; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Mapping from algorithm name to class name. |
|
| 30 | - * |
|
| 31 | - * @internal |
|
| 32 | - * |
|
| 33 | - * @var array |
|
| 34 | - */ |
|
| 35 | - const MAP_ALGO_TO_CLASS = array( |
|
| 36 | - /* @formatter:off */ |
|
| 37 | - JWA::ALGO_HS256 => HS256Algorithm::class, |
|
| 38 | - JWA::ALGO_HS384 => HS384Algorithm::class, |
|
| 39 | - JWA::ALGO_HS512 => HS512Algorithm::class |
|
| 40 | - /* @formatter:on */ |
|
| 41 | - ); |
|
| 28 | + /** |
|
| 29 | + * Mapping from algorithm name to class name. |
|
| 30 | + * |
|
| 31 | + * @internal |
|
| 32 | + * |
|
| 33 | + * @var array |
|
| 34 | + */ |
|
| 35 | + const MAP_ALGO_TO_CLASS = array( |
|
| 36 | + /* @formatter:off */ |
|
| 37 | + JWA::ALGO_HS256 => HS256Algorithm::class, |
|
| 38 | + JWA::ALGO_HS384 => HS384Algorithm::class, |
|
| 39 | + JWA::ALGO_HS512 => HS512Algorithm::class |
|
| 40 | + /* @formatter:on */ |
|
| 41 | + ); |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Get algorithm name recognized by the Hash extension. |
|
| 45 | - * |
|
| 46 | - * @return string |
|
| 47 | - */ |
|
| 48 | - abstract protected function _hashAlgo(): string; |
|
| 43 | + /** |
|
| 44 | + * Get algorithm name recognized by the Hash extension. |
|
| 45 | + * |
|
| 46 | + * @return string |
|
| 47 | + */ |
|
| 48 | + abstract protected function _hashAlgo(): string; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Constructor. |
|
| 52 | - * |
|
| 53 | - * @param string $key Shared secret key |
|
| 54 | - */ |
|
| 55 | - public function __construct(string $key) |
|
| 56 | - { |
|
| 57 | - $this->_key = $key; |
|
| 58 | - } |
|
| 50 | + /** |
|
| 51 | + * Constructor. |
|
| 52 | + * |
|
| 53 | + * @param string $key Shared secret key |
|
| 54 | + */ |
|
| 55 | + public function __construct(string $key) |
|
| 56 | + { |
|
| 57 | + $this->_key = $key; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * |
|
| 62 | - * {@inheritdoc} |
|
| 63 | - */ |
|
| 64 | - public static function fromJWK(JWK $jwk, Header $header): self |
|
| 65 | - { |
|
| 66 | - $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
| 67 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 68 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 69 | - throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
| 70 | - } |
|
| 71 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 72 | - return new $cls($jwk->key()); |
|
| 73 | - } |
|
| 60 | + /** |
|
| 61 | + * |
|
| 62 | + * {@inheritdoc} |
|
| 63 | + */ |
|
| 64 | + public static function fromJWK(JWK $jwk, Header $header): self |
|
| 65 | + { |
|
| 66 | + $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
| 67 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 68 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 69 | + throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
| 70 | + } |
|
| 71 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 72 | + return new $cls($jwk->key()); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * |
|
| 77 | - * {@inheritdoc} |
|
| 78 | - */ |
|
| 79 | - public function computeSignature(string $data): string |
|
| 80 | - { |
|
| 81 | - $result = @hash_hmac($this->_hashAlgo(), $data, $this->_key, true); |
|
| 82 | - if (false === $result) { |
|
| 83 | - $err = error_get_last(); |
|
| 84 | - $msg = isset($err) && __FILE__ == $err['file'] ? $err['message'] : null; |
|
| 85 | - throw new \RuntimeException($msg ?? 'hash_hmac() failed.'); |
|
| 86 | - } |
|
| 87 | - return $result; |
|
| 88 | - } |
|
| 75 | + /** |
|
| 76 | + * |
|
| 77 | + * {@inheritdoc} |
|
| 78 | + */ |
|
| 79 | + public function computeSignature(string $data): string |
|
| 80 | + { |
|
| 81 | + $result = @hash_hmac($this->_hashAlgo(), $data, $this->_key, true); |
|
| 82 | + if (false === $result) { |
|
| 83 | + $err = error_get_last(); |
|
| 84 | + $msg = isset($err) && __FILE__ == $err['file'] ? $err['message'] : null; |
|
| 85 | + throw new \RuntimeException($msg ?? 'hash_hmac() failed.'); |
|
| 86 | + } |
|
| 87 | + return $result; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * |
|
| 92 | - * {@inheritdoc} |
|
| 93 | - */ |
|
| 94 | - public function validateSignature(string $data, string $signature): bool |
|
| 95 | - { |
|
| 96 | - return $this->computeSignature($data) === $signature; |
|
| 97 | - } |
|
| 90 | + /** |
|
| 91 | + * |
|
| 92 | + * {@inheritdoc} |
|
| 93 | + */ |
|
| 94 | + public function validateSignature(string $data, string $signature): bool |
|
| 95 | + { |
|
| 96 | + return $this->computeSignature($data) === $signature; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * |
|
| 101 | - * @see \JWX\JWS\SignatureAlgorithm::headerParameters() |
|
| 102 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
| 103 | - */ |
|
| 104 | - public function headerParameters(): array |
|
| 105 | - { |
|
| 106 | - return array_merge(parent::headerParameters(), |
|
| 107 | - array(AlgorithmParameter::fromAlgorithm($this))); |
|
| 108 | - } |
|
| 99 | + /** |
|
| 100 | + * |
|
| 101 | + * @see \JWX\JWS\SignatureAlgorithm::headerParameters() |
|
| 102 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
| 103 | + */ |
|
| 104 | + public function headerParameters(): array |
|
| 105 | + { |
|
| 106 | + return array_merge(parent::headerParameters(), |
|
| 107 | + array(AlgorithmParameter::fromAlgorithm($this))); |
|
| 108 | + } |
|
| 109 | 109 | } |
@@ -13,21 +13,21 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class HS256Algorithm extends HMACAlgorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * |
|
| 18 | - * {@inheritdoc} |
|
| 19 | - */ |
|
| 20 | - protected function _hashAlgo(): string |
|
| 21 | - { |
|
| 22 | - return "sha256"; |
|
| 23 | - } |
|
| 16 | + /** |
|
| 17 | + * |
|
| 18 | + * {@inheritdoc} |
|
| 19 | + */ |
|
| 20 | + protected function _hashAlgo(): string |
|
| 21 | + { |
|
| 22 | + return "sha256"; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * |
|
| 27 | - * {@inheritdoc} |
|
| 28 | - */ |
|
| 29 | - public function algorithmParamValue(): string |
|
| 30 | - { |
|
| 31 | - return JWA::ALGO_HS256; |
|
| 32 | - } |
|
| 25 | + /** |
|
| 26 | + * |
|
| 27 | + * {@inheritdoc} |
|
| 28 | + */ |
|
| 29 | + public function algorithmParamValue(): string |
|
| 30 | + { |
|
| 31 | + return JWA::ALGO_HS256; |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -12,90 +12,90 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | abstract class OpenSSLSignatureAlgorithm extends SignatureAlgorithm |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Public key. |
|
| 17 | - * |
|
| 18 | - * @var \JWX\JWK\Asymmetric\PublicKeyJWK $_publicKey |
|
| 19 | - */ |
|
| 20 | - protected $_publicKey; |
|
| 15 | + /** |
|
| 16 | + * Public key. |
|
| 17 | + * |
|
| 18 | + * @var \JWX\JWK\Asymmetric\PublicKeyJWK $_publicKey |
|
| 19 | + */ |
|
| 20 | + protected $_publicKey; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Private key. |
|
| 24 | - * |
|
| 25 | - * @var \JWX\JWK\Asymmetric\PrivateKeyJWK|null $_privateKey |
|
| 26 | - */ |
|
| 27 | - protected $_privateKey; |
|
| 22 | + /** |
|
| 23 | + * Private key. |
|
| 24 | + * |
|
| 25 | + * @var \JWX\JWK\Asymmetric\PrivateKeyJWK|null $_privateKey |
|
| 26 | + */ |
|
| 27 | + protected $_privateKey; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Get the message digest method name supported by OpenSSL. |
|
| 31 | - * |
|
| 32 | - * @return string |
|
| 33 | - */ |
|
| 34 | - abstract protected function _mdMethod(): string; |
|
| 29 | + /** |
|
| 30 | + * Get the message digest method name supported by OpenSSL. |
|
| 31 | + * |
|
| 32 | + * @return string |
|
| 33 | + */ |
|
| 34 | + abstract protected function _mdMethod(): string; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * |
|
| 38 | - * @see \JWX\JWS\SignatureAlgorithm::computeSignature() |
|
| 39 | - * @throws \LogicException If private key was not provided |
|
| 40 | - * @throws \RuntimeException For generic errors |
|
| 41 | - * @return string |
|
| 42 | - */ |
|
| 43 | - public function computeSignature(string $data): string |
|
| 44 | - { |
|
| 45 | - /** |
|
| 46 | - * NOTE: OpenSSL uses PKCS #1 v1.5 padding by default, so no explicit |
|
| 47 | - * padding is required by sign and verify operations. |
|
| 48 | - */ |
|
| 49 | - if (!isset($this->_privateKey)) { |
|
| 50 | - throw new \LogicException("Private key not set."); |
|
| 51 | - } |
|
| 52 | - $key = openssl_pkey_get_private($this->_privateKey->toPEM()->string()); |
|
| 53 | - if (!$key) { |
|
| 54 | - throw new \RuntimeException( |
|
| 55 | - "openssl_pkey_get_private() failed: " . |
|
| 56 | - $this->_getLastOpenSSLError()); |
|
| 57 | - } |
|
| 58 | - $result = @openssl_sign($data, $signature, $key, $this->_mdMethod()); |
|
| 59 | - if (!$result) { |
|
| 60 | - throw new \RuntimeException( |
|
| 61 | - "openssl_sign() failed: " . $this->_getLastOpenSSLError()); |
|
| 62 | - } |
|
| 63 | - return $signature; |
|
| 64 | - } |
|
| 36 | + /** |
|
| 37 | + * |
|
| 38 | + * @see \JWX\JWS\SignatureAlgorithm::computeSignature() |
|
| 39 | + * @throws \LogicException If private key was not provided |
|
| 40 | + * @throws \RuntimeException For generic errors |
|
| 41 | + * @return string |
|
| 42 | + */ |
|
| 43 | + public function computeSignature(string $data): string |
|
| 44 | + { |
|
| 45 | + /** |
|
| 46 | + * NOTE: OpenSSL uses PKCS #1 v1.5 padding by default, so no explicit |
|
| 47 | + * padding is required by sign and verify operations. |
|
| 48 | + */ |
|
| 49 | + if (!isset($this->_privateKey)) { |
|
| 50 | + throw new \LogicException("Private key not set."); |
|
| 51 | + } |
|
| 52 | + $key = openssl_pkey_get_private($this->_privateKey->toPEM()->string()); |
|
| 53 | + if (!$key) { |
|
| 54 | + throw new \RuntimeException( |
|
| 55 | + "openssl_pkey_get_private() failed: " . |
|
| 56 | + $this->_getLastOpenSSLError()); |
|
| 57 | + } |
|
| 58 | + $result = @openssl_sign($data, $signature, $key, $this->_mdMethod()); |
|
| 59 | + if (!$result) { |
|
| 60 | + throw new \RuntimeException( |
|
| 61 | + "openssl_sign() failed: " . $this->_getLastOpenSSLError()); |
|
| 62 | + } |
|
| 63 | + return $signature; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * |
|
| 68 | - * @see \JWX\JWS\SignatureAlgorithm::validateSignature() |
|
| 69 | - * @throws \RuntimeException For generic errors |
|
| 70 | - * @return bool |
|
| 71 | - */ |
|
| 72 | - public function validateSignature(string $data, string $signature): bool |
|
| 73 | - { |
|
| 74 | - $key = openssl_pkey_get_public($this->_publicKey->toPEM()->string()); |
|
| 75 | - if (!$key) { |
|
| 76 | - throw new \RuntimeException( |
|
| 77 | - "openssl_pkey_get_public() failed: " . |
|
| 78 | - $this->_getLastOpenSSLError()); |
|
| 79 | - } |
|
| 80 | - $result = @openssl_verify($data, $signature, $key, $this->_mdMethod()); |
|
| 81 | - if (false === $result || -1 == $result) { |
|
| 82 | - throw new \RuntimeException( |
|
| 83 | - "openssl_verify() failed: " . $this->_getLastOpenSSLError()); |
|
| 84 | - } |
|
| 85 | - return $result == 1; |
|
| 86 | - } |
|
| 66 | + /** |
|
| 67 | + * |
|
| 68 | + * @see \JWX\JWS\SignatureAlgorithm::validateSignature() |
|
| 69 | + * @throws \RuntimeException For generic errors |
|
| 70 | + * @return bool |
|
| 71 | + */ |
|
| 72 | + public function validateSignature(string $data, string $signature): bool |
|
| 73 | + { |
|
| 74 | + $key = openssl_pkey_get_public($this->_publicKey->toPEM()->string()); |
|
| 75 | + if (!$key) { |
|
| 76 | + throw new \RuntimeException( |
|
| 77 | + "openssl_pkey_get_public() failed: " . |
|
| 78 | + $this->_getLastOpenSSLError()); |
|
| 79 | + } |
|
| 80 | + $result = @openssl_verify($data, $signature, $key, $this->_mdMethod()); |
|
| 81 | + if (false === $result || -1 == $result) { |
|
| 82 | + throw new \RuntimeException( |
|
| 83 | + "openssl_verify() failed: " . $this->_getLastOpenSSLError()); |
|
| 84 | + } |
|
| 85 | + return $result == 1; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Get the last OpenSSL error message. |
|
| 90 | - * |
|
| 91 | - * @return string|null |
|
| 92 | - */ |
|
| 93 | - protected function _getLastOpenSSLError() |
|
| 94 | - { |
|
| 95 | - $msg = null; |
|
| 96 | - while (false !== ($err = openssl_error_string())) { |
|
| 97 | - $msg = $err; |
|
| 98 | - } |
|
| 99 | - return $msg; |
|
| 100 | - } |
|
| 88 | + /** |
|
| 89 | + * Get the last OpenSSL error message. |
|
| 90 | + * |
|
| 91 | + * @return string|null |
|
| 92 | + */ |
|
| 93 | + protected function _getLastOpenSSLError() |
|
| 94 | + { |
|
| 95 | + $msg = null; |
|
| 96 | + while (false !== ($err = openssl_error_string())) { |
|
| 97 | + $msg = $err; |
|
| 98 | + } |
|
| 99 | + return $msg; |
|
| 100 | + } |
|
| 101 | 101 | } |
@@ -13,21 +13,21 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class HS384Algorithm extends HMACAlgorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * |
|
| 18 | - * {@inheritdoc} |
|
| 19 | - */ |
|
| 20 | - protected function _hashAlgo(): string |
|
| 21 | - { |
|
| 22 | - return "sha384"; |
|
| 23 | - } |
|
| 16 | + /** |
|
| 17 | + * |
|
| 18 | + * {@inheritdoc} |
|
| 19 | + */ |
|
| 20 | + protected function _hashAlgo(): string |
|
| 21 | + { |
|
| 22 | + return "sha384"; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * |
|
| 27 | - * {@inheritdoc} |
|
| 28 | - */ |
|
| 29 | - public function algorithmParamValue(): string |
|
| 30 | - { |
|
| 31 | - return JWA::ALGO_HS384; |
|
| 32 | - } |
|
| 25 | + /** |
|
| 26 | + * |
|
| 27 | + * {@inheritdoc} |
|
| 28 | + */ |
|
| 29 | + public function algorithmParamValue(): string |
|
| 30 | + { |
|
| 31 | + return JWA::ALGO_HS384; |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -16,41 +16,41 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class NoneAlgorithm extends SignatureAlgorithm |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * |
|
| 21 | - * {@inheritdoc} |
|
| 22 | - */ |
|
| 23 | - public function algorithmParamValue(): string |
|
| 24 | - { |
|
| 25 | - return JWA::ALGO_NONE; |
|
| 26 | - } |
|
| 19 | + /** |
|
| 20 | + * |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + public function algorithmParamValue(): string |
|
| 24 | + { |
|
| 25 | + return JWA::ALGO_NONE; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * |
|
| 30 | - * {@inheritdoc} |
|
| 31 | - */ |
|
| 32 | - public function computeSignature(string $data): string |
|
| 33 | - { |
|
| 34 | - return ""; |
|
| 35 | - } |
|
| 28 | + /** |
|
| 29 | + * |
|
| 30 | + * {@inheritdoc} |
|
| 31 | + */ |
|
| 32 | + public function computeSignature(string $data): string |
|
| 33 | + { |
|
| 34 | + return ""; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * |
|
| 39 | - * {@inheritdoc} |
|
| 40 | - */ |
|
| 41 | - public function validateSignature(string $data, string $signature): bool |
|
| 42 | - { |
|
| 43 | - return $signature === ""; |
|
| 44 | - } |
|
| 37 | + /** |
|
| 38 | + * |
|
| 39 | + * {@inheritdoc} |
|
| 40 | + */ |
|
| 41 | + public function validateSignature(string $data, string $signature): bool |
|
| 42 | + { |
|
| 43 | + return $signature === ""; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * |
|
| 48 | - * @see \JWX\JWS\SignatureAlgorithm::headerParameters() |
|
| 49 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
| 50 | - */ |
|
| 51 | - public function headerParameters(): array |
|
| 52 | - { |
|
| 53 | - return array_merge(parent::headerParameters(), |
|
| 54 | - array(AlgorithmParameter::fromAlgorithm($this))); |
|
| 55 | - } |
|
| 46 | + /** |
|
| 47 | + * |
|
| 48 | + * @see \JWX\JWS\SignatureAlgorithm::headerParameters() |
|
| 49 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
| 50 | + */ |
|
| 51 | + public function headerParameters(): array |
|
| 52 | + { |
|
| 53 | + return array_merge(parent::headerParameters(), |
|
| 54 | + array(AlgorithmParameter::fromAlgorithm($this))); |
|
| 55 | + } |
|
| 56 | 56 | } |
@@ -20,138 +20,138 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | abstract class ECDSAAlgorithm extends OpenSSLSignatureAlgorithm |
| 22 | 22 | { |
| 23 | - /** |
|
| 24 | - * Mapping from algorithm name to class name. |
|
| 25 | - * |
|
| 26 | - * @internal |
|
| 27 | - * |
|
| 28 | - * @var array |
|
| 29 | - */ |
|
| 30 | - const MAP_ALGO_TO_CLASS = array( |
|
| 31 | - /* @formatter:off */ |
|
| 32 | - JWA::ALGO_ES256 => ES256Algorithm::class, |
|
| 33 | - JWA::ALGO_ES384 => ES384Algorithm::class, |
|
| 34 | - JWA::ALGO_ES512 => ES512Algorithm::class |
|
| 35 | - /* @formatter:on */ |
|
| 36 | - ); |
|
| 23 | + /** |
|
| 24 | + * Mapping from algorithm name to class name. |
|
| 25 | + * |
|
| 26 | + * @internal |
|
| 27 | + * |
|
| 28 | + * @var array |
|
| 29 | + */ |
|
| 30 | + const MAP_ALGO_TO_CLASS = array( |
|
| 31 | + /* @formatter:off */ |
|
| 32 | + JWA::ALGO_ES256 => ES256Algorithm::class, |
|
| 33 | + JWA::ALGO_ES384 => ES384Algorithm::class, |
|
| 34 | + JWA::ALGO_ES512 => ES512Algorithm::class |
|
| 35 | + /* @formatter:on */ |
|
| 36 | + ); |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Signature size in bytes. |
|
| 40 | - * |
|
| 41 | - * @var int |
|
| 42 | - */ |
|
| 43 | - private $_signatureSize; |
|
| 38 | + /** |
|
| 39 | + * Signature size in bytes. |
|
| 40 | + * |
|
| 41 | + * @var int |
|
| 42 | + */ |
|
| 43 | + private $_signatureSize; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Get the name of the curve used by this algorithm. |
|
| 47 | - * |
|
| 48 | - * @return string |
|
| 49 | - */ |
|
| 50 | - abstract protected function _curveName(): string; |
|
| 45 | + /** |
|
| 46 | + * Get the name of the curve used by this algorithm. |
|
| 47 | + * |
|
| 48 | + * @return string |
|
| 49 | + */ |
|
| 50 | + abstract protected function _curveName(): string; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Constructor. |
|
| 54 | - * |
|
| 55 | - * @param ECPublicKeyJWK $pub_key |
|
| 56 | - * @param ECPrivateKeyJWK $priv_key |
|
| 57 | - */ |
|
| 58 | - protected function __construct(ECPublicKeyJWK $pub_key, |
|
| 59 | - ECPrivateKeyJWK $priv_key = null) |
|
| 60 | - { |
|
| 61 | - $curve = $pub_key->curveParameter()->value(); |
|
| 62 | - if ($this->_curveName() != $curve) { |
|
| 63 | - throw new \InvalidArgumentException( |
|
| 64 | - "Key with " . $this->_curveName() . |
|
| 65 | - " curve expected, got $curve."); |
|
| 66 | - } |
|
| 67 | - $this->_publicKey = $pub_key; |
|
| 68 | - $this->_privateKey = $priv_key; |
|
| 69 | - $key_size = $pub_key->curveParameter()->keySizeBits(); |
|
| 70 | - $this->_signatureSize = intval(ceil($key_size / 8) * 2); |
|
| 71 | - } |
|
| 52 | + /** |
|
| 53 | + * Constructor. |
|
| 54 | + * |
|
| 55 | + * @param ECPublicKeyJWK $pub_key |
|
| 56 | + * @param ECPrivateKeyJWK $priv_key |
|
| 57 | + */ |
|
| 58 | + protected function __construct(ECPublicKeyJWK $pub_key, |
|
| 59 | + ECPrivateKeyJWK $priv_key = null) |
|
| 60 | + { |
|
| 61 | + $curve = $pub_key->curveParameter()->value(); |
|
| 62 | + if ($this->_curveName() != $curve) { |
|
| 63 | + throw new \InvalidArgumentException( |
|
| 64 | + "Key with " . $this->_curveName() . |
|
| 65 | + " curve expected, got $curve."); |
|
| 66 | + } |
|
| 67 | + $this->_publicKey = $pub_key; |
|
| 68 | + $this->_privateKey = $priv_key; |
|
| 69 | + $key_size = $pub_key->curveParameter()->keySizeBits(); |
|
| 70 | + $this->_signatureSize = intval(ceil($key_size / 8) * 2); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Initialize from a public key. |
|
| 75 | - * |
|
| 76 | - * @param ECPublicKeyJWK $jwk |
|
| 77 | - * @return self |
|
| 78 | - */ |
|
| 79 | - public static function fromPublicKey(ECPublicKeyJWK $jwk): self |
|
| 80 | - { |
|
| 81 | - return new static($jwk); |
|
| 82 | - } |
|
| 73 | + /** |
|
| 74 | + * Initialize from a public key. |
|
| 75 | + * |
|
| 76 | + * @param ECPublicKeyJWK $jwk |
|
| 77 | + * @return self |
|
| 78 | + */ |
|
| 79 | + public static function fromPublicKey(ECPublicKeyJWK $jwk): self |
|
| 80 | + { |
|
| 81 | + return new static($jwk); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Initialize from a private key. |
|
| 86 | - * |
|
| 87 | - * @param ECPrivateKeyJWK $jwk |
|
| 88 | - * @return self |
|
| 89 | - */ |
|
| 90 | - public static function fromPrivateKey(ECPrivateKeyJWK $jwk): self |
|
| 91 | - { |
|
| 92 | - return new static($jwk->publicKey(), $jwk); |
|
| 93 | - } |
|
| 84 | + /** |
|
| 85 | + * Initialize from a private key. |
|
| 86 | + * |
|
| 87 | + * @param ECPrivateKeyJWK $jwk |
|
| 88 | + * @return self |
|
| 89 | + */ |
|
| 90 | + public static function fromPrivateKey(ECPrivateKeyJWK $jwk): self |
|
| 91 | + { |
|
| 92 | + return new static($jwk->publicKey(), $jwk); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * |
|
| 97 | - * {@inheritdoc} |
|
| 98 | - */ |
|
| 99 | - public static function fromJWK(JWK $jwk, Header $header): self |
|
| 100 | - { |
|
| 101 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 102 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 103 | - throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
| 104 | - } |
|
| 105 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 106 | - if ($jwk->has(...ECPrivateKeyJWK::MANAGED_PARAMS)) { |
|
| 107 | - return $cls::fromPrivateKey(ECPrivateKeyJWK::fromJWK($jwk)); |
|
| 108 | - } |
|
| 109 | - return $cls::fromPublicKey(ECPublicKeyJWK::fromJWK($jwk)); |
|
| 110 | - } |
|
| 95 | + /** |
|
| 96 | + * |
|
| 97 | + * {@inheritdoc} |
|
| 98 | + */ |
|
| 99 | + public static function fromJWK(JWK $jwk, Header $header): self |
|
| 100 | + { |
|
| 101 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 102 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 103 | + throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
| 104 | + } |
|
| 105 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 106 | + if ($jwk->has(...ECPrivateKeyJWK::MANAGED_PARAMS)) { |
|
| 107 | + return $cls::fromPrivateKey(ECPrivateKeyJWK::fromJWK($jwk)); |
|
| 108 | + } |
|
| 109 | + return $cls::fromPublicKey(ECPublicKeyJWK::fromJWK($jwk)); |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * |
|
| 114 | - * @see \JWX\JWS\Algorithm\OpenSSLSignatureAlgorithm::computeSignature() |
|
| 115 | - * @return string |
|
| 116 | - */ |
|
| 117 | - public function computeSignature(string $data): string |
|
| 118 | - { |
|
| 119 | - // OpenSSL returns ECDSA signature as a DER encoded ECDSA-Sig-Value |
|
| 120 | - $der = parent::computeSignature($data); |
|
| 121 | - $sig = ECSignature::fromDER($der); |
|
| 122 | - $mlen = intval(floor($this->_signatureSize / 2)); |
|
| 123 | - $signature = ECConversion::numberToOctets($sig->r(), $mlen) . |
|
| 124 | - ECConversion::numberToOctets($sig->s(), $mlen); |
|
| 125 | - return $signature; |
|
| 126 | - } |
|
| 112 | + /** |
|
| 113 | + * |
|
| 114 | + * @see \JWX\JWS\Algorithm\OpenSSLSignatureAlgorithm::computeSignature() |
|
| 115 | + * @return string |
|
| 116 | + */ |
|
| 117 | + public function computeSignature(string $data): string |
|
| 118 | + { |
|
| 119 | + // OpenSSL returns ECDSA signature as a DER encoded ECDSA-Sig-Value |
|
| 120 | + $der = parent::computeSignature($data); |
|
| 121 | + $sig = ECSignature::fromDER($der); |
|
| 122 | + $mlen = intval(floor($this->_signatureSize / 2)); |
|
| 123 | + $signature = ECConversion::numberToOctets($sig->r(), $mlen) . |
|
| 124 | + ECConversion::numberToOctets($sig->s(), $mlen); |
|
| 125 | + return $signature; |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - /** |
|
| 129 | - * |
|
| 130 | - * @see \JWX\JWS\Algorithm\OpenSSLSignatureAlgorithm::validateSignature() |
|
| 131 | - * @return bool |
|
| 132 | - */ |
|
| 133 | - public function validateSignature(string $data, string $signature): bool |
|
| 134 | - { |
|
| 135 | - if (strlen($signature) != $this->_signatureSize) { |
|
| 136 | - throw new \UnexpectedValueException("Invalid signature length."); |
|
| 137 | - } |
|
| 138 | - list($r_octets, $s_octets) = str_split($signature, |
|
| 139 | - intval(floor($this->_signatureSize / 2))); |
|
| 140 | - // convert signature to DER sequence for OpenSSL |
|
| 141 | - $r = ECConversion::octetsToNumber($r_octets); |
|
| 142 | - $s = ECConversion::octetsToNumber($s_octets); |
|
| 143 | - $sig = new ECSignature($r, $s); |
|
| 144 | - return parent::validateSignature($data, $sig->toDER()); |
|
| 145 | - } |
|
| 128 | + /** |
|
| 129 | + * |
|
| 130 | + * @see \JWX\JWS\Algorithm\OpenSSLSignatureAlgorithm::validateSignature() |
|
| 131 | + * @return bool |
|
| 132 | + */ |
|
| 133 | + public function validateSignature(string $data, string $signature): bool |
|
| 134 | + { |
|
| 135 | + if (strlen($signature) != $this->_signatureSize) { |
|
| 136 | + throw new \UnexpectedValueException("Invalid signature length."); |
|
| 137 | + } |
|
| 138 | + list($r_octets, $s_octets) = str_split($signature, |
|
| 139 | + intval(floor($this->_signatureSize / 2))); |
|
| 140 | + // convert signature to DER sequence for OpenSSL |
|
| 141 | + $r = ECConversion::octetsToNumber($r_octets); |
|
| 142 | + $s = ECConversion::octetsToNumber($s_octets); |
|
| 143 | + $sig = new ECSignature($r, $s); |
|
| 144 | + return parent::validateSignature($data, $sig->toDER()); |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - /** |
|
| 148 | - * |
|
| 149 | - * @see \JWX\JWS\SignatureAlgorithm::headerParameters() |
|
| 150 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
| 151 | - */ |
|
| 152 | - public function headerParameters(): array |
|
| 153 | - { |
|
| 154 | - return array_merge(parent::headerParameters(), |
|
| 155 | - array(AlgorithmParameter::fromAlgorithm($this))); |
|
| 156 | - } |
|
| 147 | + /** |
|
| 148 | + * |
|
| 149 | + * @see \JWX\JWS\SignatureAlgorithm::headerParameters() |
|
| 150 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
| 151 | + */ |
|
| 152 | + public function headerParameters(): array |
|
| 153 | + { |
|
| 154 | + return array_merge(parent::headerParameters(), |
|
| 155 | + array(AlgorithmParameter::fromAlgorithm($this))); |
|
| 156 | + } |
|
| 157 | 157 | } |
@@ -13,21 +13,21 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class RS256Algorithm extends RSASSAPKCS1Algorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * |
|
| 18 | - * {@inheritdoc} |
|
| 19 | - */ |
|
| 20 | - protected function _mdMethod(): string |
|
| 21 | - { |
|
| 22 | - return "sha256WithRSAEncryption"; |
|
| 23 | - } |
|
| 16 | + /** |
|
| 17 | + * |
|
| 18 | + * {@inheritdoc} |
|
| 19 | + */ |
|
| 20 | + protected function _mdMethod(): string |
|
| 21 | + { |
|
| 22 | + return "sha256WithRSAEncryption"; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * |
|
| 27 | - * {@inheritdoc} |
|
| 28 | - */ |
|
| 29 | - public function algorithmParamValue(): string |
|
| 30 | - { |
|
| 31 | - return JWA::ALGO_RS256; |
|
| 32 | - } |
|
| 25 | + /** |
|
| 26 | + * |
|
| 27 | + * {@inheritdoc} |
|
| 28 | + */ |
|
| 29 | + public function algorithmParamValue(): string |
|
| 30 | + { |
|
| 31 | + return JWA::ALGO_RS256; |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -14,30 +14,30 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class ES256Algorithm extends ECDSAAlgorithm |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * |
|
| 19 | - * {@inheritdoc} |
|
| 20 | - */ |
|
| 21 | - protected function _curveName(): string |
|
| 22 | - { |
|
| 23 | - return CurveParameter::CURVE_P256; |
|
| 24 | - } |
|
| 17 | + /** |
|
| 18 | + * |
|
| 19 | + * {@inheritdoc} |
|
| 20 | + */ |
|
| 21 | + protected function _curveName(): string |
|
| 22 | + { |
|
| 23 | + return CurveParameter::CURVE_P256; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * |
|
| 28 | - * {@inheritdoc} |
|
| 29 | - */ |
|
| 30 | - protected function _mdMethod(): string |
|
| 31 | - { |
|
| 32 | - return "sha256"; |
|
| 33 | - } |
|
| 26 | + /** |
|
| 27 | + * |
|
| 28 | + * {@inheritdoc} |
|
| 29 | + */ |
|
| 30 | + protected function _mdMethod(): string |
|
| 31 | + { |
|
| 32 | + return "sha256"; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * |
|
| 37 | - * {@inheritdoc} |
|
| 38 | - */ |
|
| 39 | - public function algorithmParamValue(): string |
|
| 40 | - { |
|
| 41 | - return JWA::ALGO_ES256; |
|
| 42 | - } |
|
| 35 | + /** |
|
| 36 | + * |
|
| 37 | + * {@inheritdoc} |
|
| 38 | + */ |
|
| 39 | + public function algorithmParamValue(): string |
|
| 40 | + { |
|
| 41 | + return JWA::ALGO_ES256; |
|
| 42 | + } |
|
| 43 | 43 | } |
@@ -14,30 +14,30 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class ES384Algorithm extends ECDSAAlgorithm |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * |
|
| 19 | - * {@inheritdoc} |
|
| 20 | - */ |
|
| 21 | - protected function _curveName(): string |
|
| 22 | - { |
|
| 23 | - return CurveParameter::CURVE_P384; |
|
| 24 | - } |
|
| 17 | + /** |
|
| 18 | + * |
|
| 19 | + * {@inheritdoc} |
|
| 20 | + */ |
|
| 21 | + protected function _curveName(): string |
|
| 22 | + { |
|
| 23 | + return CurveParameter::CURVE_P384; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * |
|
| 28 | - * {@inheritdoc} |
|
| 29 | - */ |
|
| 30 | - protected function _mdMethod(): string |
|
| 31 | - { |
|
| 32 | - return "sha384"; |
|
| 33 | - } |
|
| 26 | + /** |
|
| 27 | + * |
|
| 28 | + * {@inheritdoc} |
|
| 29 | + */ |
|
| 30 | + protected function _mdMethod(): string |
|
| 31 | + { |
|
| 32 | + return "sha384"; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * |
|
| 37 | - * {@inheritdoc} |
|
| 38 | - */ |
|
| 39 | - public function algorithmParamValue(): string |
|
| 40 | - { |
|
| 41 | - return JWA::ALGO_ES384; |
|
| 42 | - } |
|
| 35 | + /** |
|
| 36 | + * |
|
| 37 | + * {@inheritdoc} |
|
| 38 | + */ |
|
| 39 | + public function algorithmParamValue(): string |
|
| 40 | + { |
|
| 41 | + return JWA::ALGO_ES384; |
|
| 42 | + } |
|
| 43 | 43 | } |