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