@@ -13,73 +13,73 @@ |
||
| 13 | 13 | * Base class for algorithms usable for signing and validating JWS's. |
| 14 | 14 | */ |
| 15 | 15 | abstract class SignatureAlgorithm implements |
| 16 | - AlgorithmParameterValue, |
|
| 17 | - HeaderParameters |
|
| 16 | + AlgorithmParameterValue, |
|
| 17 | + HeaderParameters |
|
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * ID of the key used by the algorithm. |
|
| 21 | - * |
|
| 22 | - * If set, KeyID parameter shall be automatically inserted into JWS's |
|
| 23 | - * header. |
|
| 24 | - * |
|
| 25 | - * @var string|null $_keyID |
|
| 26 | - */ |
|
| 27 | - protected $_keyID; |
|
| 19 | + /** |
|
| 20 | + * ID of the key used by the algorithm. |
|
| 21 | + * |
|
| 22 | + * If set, KeyID parameter shall be automatically inserted into JWS's |
|
| 23 | + * header. |
|
| 24 | + * |
|
| 25 | + * @var string|null $_keyID |
|
| 26 | + */ |
|
| 27 | + protected $_keyID; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Compute signature. |
|
| 31 | - * |
|
| 32 | - * @param string $data Data for which the signature is computed |
|
| 33 | - * @return string |
|
| 34 | - */ |
|
| 35 | - abstract public function computeSignature($data); |
|
| 29 | + /** |
|
| 30 | + * Compute signature. |
|
| 31 | + * |
|
| 32 | + * @param string $data Data for which the signature is computed |
|
| 33 | + * @return string |
|
| 34 | + */ |
|
| 35 | + abstract public function computeSignature($data); |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Validate signature. |
|
| 39 | - * |
|
| 40 | - * @param string $data Data to validate |
|
| 41 | - * @param string $signature Signature to compare |
|
| 42 | - * @return bool |
|
| 43 | - */ |
|
| 44 | - abstract public function validateSignature($data, $signature); |
|
| 37 | + /** |
|
| 38 | + * Validate signature. |
|
| 39 | + * |
|
| 40 | + * @param string $data Data to validate |
|
| 41 | + * @param string $signature Signature to compare |
|
| 42 | + * @return bool |
|
| 43 | + */ |
|
| 44 | + abstract public function validateSignature($data, $signature); |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Initialize signature algorithm from a JWK and a header. |
|
| 48 | - * |
|
| 49 | - * @param JWK $jwk JSON Web Key |
|
| 50 | - * @param Header $header Header |
|
| 51 | - * @return SignatureAlgorithm |
|
| 52 | - */ |
|
| 53 | - public static function fromJWK(JWK $jwk, Header $header) |
|
| 54 | - { |
|
| 55 | - $factory = new SignatureAlgorithmFactory($header); |
|
| 56 | - return $factory->algoByKey($jwk); |
|
| 57 | - } |
|
| 46 | + /** |
|
| 47 | + * Initialize signature algorithm from a JWK and a header. |
|
| 48 | + * |
|
| 49 | + * @param JWK $jwk JSON Web Key |
|
| 50 | + * @param Header $header Header |
|
| 51 | + * @return SignatureAlgorithm |
|
| 52 | + */ |
|
| 53 | + public static function fromJWK(JWK $jwk, Header $header) |
|
| 54 | + { |
|
| 55 | + $factory = new SignatureAlgorithmFactory($header); |
|
| 56 | + return $factory->algoByKey($jwk); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Get self with key ID. |
|
| 61 | - * |
|
| 62 | - * @param string|null $id Key ID or null to remove |
|
| 63 | - * @return self |
|
| 64 | - */ |
|
| 65 | - public function withKeyID($id) |
|
| 66 | - { |
|
| 67 | - $obj = clone $this; |
|
| 68 | - $obj->_keyID = $id; |
|
| 69 | - return $obj; |
|
| 70 | - } |
|
| 59 | + /** |
|
| 60 | + * Get self with key ID. |
|
| 61 | + * |
|
| 62 | + * @param string|null $id Key ID or null to remove |
|
| 63 | + * @return self |
|
| 64 | + */ |
|
| 65 | + public function withKeyID($id) |
|
| 66 | + { |
|
| 67 | + $obj = clone $this; |
|
| 68 | + $obj->_keyID = $id; |
|
| 69 | + return $obj; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * |
|
| 74 | - * @see \JWX\JWT\Header\HeaderParameters::headerParameters() |
|
| 75 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
| 76 | - */ |
|
| 77 | - public function headerParameters() |
|
| 78 | - { |
|
| 79 | - $params = array(); |
|
| 80 | - if (isset($this->_keyID)) { |
|
| 81 | - $params[] = new KeyIDParameter($this->_keyID); |
|
| 82 | - } |
|
| 83 | - return $params; |
|
| 84 | - } |
|
| 72 | + /** |
|
| 73 | + * |
|
| 74 | + * @see \JWX\JWT\Header\HeaderParameters::headerParameters() |
|
| 75 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
| 76 | + */ |
|
| 77 | + public function headerParameters() |
|
| 78 | + { |
|
| 79 | + $params = array(); |
|
| 80 | + if (isset($this->_keyID)) { |
|
| 81 | + $params[] = new KeyIDParameter($this->_keyID); |
|
| 82 | + } |
|
| 83 | + return $params; |
|
| 84 | + } |
|
| 85 | 85 | } |
@@ -19,277 +19,277 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class JWS |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * Protected header. |
|
| 24 | - * |
|
| 25 | - * @var Header $_protectedHeader |
|
| 26 | - */ |
|
| 27 | - protected $_protectedHeader; |
|
| 22 | + /** |
|
| 23 | + * Protected header. |
|
| 24 | + * |
|
| 25 | + * @var Header $_protectedHeader |
|
| 26 | + */ |
|
| 27 | + protected $_protectedHeader; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Payload. |
|
| 31 | - * |
|
| 32 | - * @var string $_payload |
|
| 33 | - */ |
|
| 34 | - protected $_payload; |
|
| 29 | + /** |
|
| 30 | + * Payload. |
|
| 31 | + * |
|
| 32 | + * @var string $_payload |
|
| 33 | + */ |
|
| 34 | + protected $_payload; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Input value for the signature computation. |
|
| 38 | - * |
|
| 39 | - * @var string $_signatureInput |
|
| 40 | - */ |
|
| 41 | - protected $_signatureInput; |
|
| 36 | + /** |
|
| 37 | + * Input value for the signature computation. |
|
| 38 | + * |
|
| 39 | + * @var string $_signatureInput |
|
| 40 | + */ |
|
| 41 | + protected $_signatureInput; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Signature. |
|
| 45 | - * |
|
| 46 | - * @var string $_signature |
|
| 47 | - */ |
|
| 48 | - protected $_signature; |
|
| 43 | + /** |
|
| 44 | + * Signature. |
|
| 45 | + * |
|
| 46 | + * @var string $_signature |
|
| 47 | + */ |
|
| 48 | + protected $_signature; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Constructor. |
|
| 52 | - * |
|
| 53 | - * @param Header $protected_header JWS Protected Header |
|
| 54 | - * @param string $payload JWS Payload |
|
| 55 | - * @param string $signature_input Input value for the signature computation |
|
| 56 | - * @param string $signature JWS Signature |
|
| 57 | - */ |
|
| 58 | - protected function __construct(Header $protected_header, $payload, |
|
| 59 | - $signature_input, $signature) |
|
| 60 | - { |
|
| 61 | - $this->_protectedHeader = $protected_header; |
|
| 62 | - $this->_payload = $payload; |
|
| 63 | - $this->_signatureInput = $signature_input; |
|
| 64 | - $this->_signature = $signature; |
|
| 65 | - } |
|
| 50 | + /** |
|
| 51 | + * Constructor. |
|
| 52 | + * |
|
| 53 | + * @param Header $protected_header JWS Protected Header |
|
| 54 | + * @param string $payload JWS Payload |
|
| 55 | + * @param string $signature_input Input value for the signature computation |
|
| 56 | + * @param string $signature JWS Signature |
|
| 57 | + */ |
|
| 58 | + protected function __construct(Header $protected_header, $payload, |
|
| 59 | + $signature_input, $signature) |
|
| 60 | + { |
|
| 61 | + $this->_protectedHeader = $protected_header; |
|
| 62 | + $this->_payload = $payload; |
|
| 63 | + $this->_signatureInput = $signature_input; |
|
| 64 | + $this->_signature = $signature; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Initialize from a compact serialization. |
|
| 69 | - * |
|
| 70 | - * @param string $data |
|
| 71 | - * @return self |
|
| 72 | - */ |
|
| 73 | - public static function fromCompact($data) |
|
| 74 | - { |
|
| 75 | - return self::fromParts(explode(".", $data)); |
|
| 76 | - } |
|
| 67 | + /** |
|
| 68 | + * Initialize from a compact serialization. |
|
| 69 | + * |
|
| 70 | + * @param string $data |
|
| 71 | + * @return self |
|
| 72 | + */ |
|
| 73 | + public static function fromCompact($data) |
|
| 74 | + { |
|
| 75 | + return self::fromParts(explode(".", $data)); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Initialize from the parts of a compact serialization. |
|
| 80 | - * |
|
| 81 | - * @param array $parts |
|
| 82 | - * @throws \UnexpectedValueException |
|
| 83 | - * @return self |
|
| 84 | - */ |
|
| 85 | - public static function fromParts(array $parts) |
|
| 86 | - { |
|
| 87 | - if (count($parts) != 3) { |
|
| 88 | - throw new \UnexpectedValueException( |
|
| 89 | - "Invalid JWS compact serialization."); |
|
| 90 | - } |
|
| 91 | - $header = Header::fromJSON(Base64::urlDecode($parts[0])); |
|
| 92 | - $b64 = $header->hasB64Payload() ? $header->B64Payload()->value() : true; |
|
| 93 | - $payload = $b64 ? Base64::urlDecode($parts[1]) : $parts[1]; |
|
| 94 | - $signature_input = $parts[0] . "." . $parts[1]; |
|
| 95 | - $signature = Base64::urlDecode($parts[2]); |
|
| 96 | - return new self($header, $payload, $signature_input, $signature); |
|
| 97 | - } |
|
| 78 | + /** |
|
| 79 | + * Initialize from the parts of a compact serialization. |
|
| 80 | + * |
|
| 81 | + * @param array $parts |
|
| 82 | + * @throws \UnexpectedValueException |
|
| 83 | + * @return self |
|
| 84 | + */ |
|
| 85 | + public static function fromParts(array $parts) |
|
| 86 | + { |
|
| 87 | + if (count($parts) != 3) { |
|
| 88 | + throw new \UnexpectedValueException( |
|
| 89 | + "Invalid JWS compact serialization."); |
|
| 90 | + } |
|
| 91 | + $header = Header::fromJSON(Base64::urlDecode($parts[0])); |
|
| 92 | + $b64 = $header->hasB64Payload() ? $header->B64Payload()->value() : true; |
|
| 93 | + $payload = $b64 ? Base64::urlDecode($parts[1]) : $parts[1]; |
|
| 94 | + $signature_input = $parts[0] . "." . $parts[1]; |
|
| 95 | + $signature = Base64::urlDecode($parts[2]); |
|
| 96 | + return new self($header, $payload, $signature_input, $signature); |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Initialize by signing the payload with given algorithm. |
|
| 101 | - * |
|
| 102 | - * @param string $payload JWS Payload |
|
| 103 | - * @param SignatureAlgorithm $algo Signature algorithm |
|
| 104 | - * @param Header|null $header Desired header. Algorithm specific |
|
| 105 | - * parameters are added automatically. |
|
| 106 | - * @throws \RuntimeException If signature computation fails |
|
| 107 | - * @return self |
|
| 108 | - */ |
|
| 109 | - public static function sign($payload, SignatureAlgorithm $algo, |
|
| 110 | - Header $header = null) |
|
| 111 | - { |
|
| 112 | - if (!isset($header)) { |
|
| 113 | - $header = new Header(); |
|
| 114 | - } |
|
| 115 | - $header = $header->withParameters(...$algo->headerParameters()); |
|
| 116 | - // ensure that if b64 parameter is used, it's marked critical |
|
| 117 | - if ($header->hasB64Payload()) { |
|
| 118 | - if (!$header->hasCritical()) { |
|
| 119 | - $crit = new CriticalParameter(JWTParameter::P_B64); |
|
| 120 | - } else { |
|
| 121 | - $crit = $header->critical()->withParamName(JWTParameter::P_B64); |
|
| 122 | - } |
|
| 123 | - $header = $header->withParameters($crit); |
|
| 124 | - } |
|
| 125 | - $signature_input = self::_generateSignatureInput($payload, $header); |
|
| 126 | - $signature = $algo->computeSignature($signature_input); |
|
| 127 | - return new self($header, $payload, $signature_input, $signature); |
|
| 128 | - } |
|
| 99 | + /** |
|
| 100 | + * Initialize by signing the payload with given algorithm. |
|
| 101 | + * |
|
| 102 | + * @param string $payload JWS Payload |
|
| 103 | + * @param SignatureAlgorithm $algo Signature algorithm |
|
| 104 | + * @param Header|null $header Desired header. Algorithm specific |
|
| 105 | + * parameters are added automatically. |
|
| 106 | + * @throws \RuntimeException If signature computation fails |
|
| 107 | + * @return self |
|
| 108 | + */ |
|
| 109 | + public static function sign($payload, SignatureAlgorithm $algo, |
|
| 110 | + Header $header = null) |
|
| 111 | + { |
|
| 112 | + if (!isset($header)) { |
|
| 113 | + $header = new Header(); |
|
| 114 | + } |
|
| 115 | + $header = $header->withParameters(...$algo->headerParameters()); |
|
| 116 | + // ensure that if b64 parameter is used, it's marked critical |
|
| 117 | + if ($header->hasB64Payload()) { |
|
| 118 | + if (!$header->hasCritical()) { |
|
| 119 | + $crit = new CriticalParameter(JWTParameter::P_B64); |
|
| 120 | + } else { |
|
| 121 | + $crit = $header->critical()->withParamName(JWTParameter::P_B64); |
|
| 122 | + } |
|
| 123 | + $header = $header->withParameters($crit); |
|
| 124 | + } |
|
| 125 | + $signature_input = self::_generateSignatureInput($payload, $header); |
|
| 126 | + $signature = $algo->computeSignature($signature_input); |
|
| 127 | + return new self($header, $payload, $signature_input, $signature); |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * Get JOSE header. |
|
| 132 | - * |
|
| 133 | - * @return JOSE |
|
| 134 | - */ |
|
| 135 | - public function header() |
|
| 136 | - { |
|
| 137 | - return new JOSE($this->_protectedHeader); |
|
| 138 | - } |
|
| 130 | + /** |
|
| 131 | + * Get JOSE header. |
|
| 132 | + * |
|
| 133 | + * @return JOSE |
|
| 134 | + */ |
|
| 135 | + public function header() |
|
| 136 | + { |
|
| 137 | + return new JOSE($this->_protectedHeader); |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * Get the signature algorithm name. |
|
| 142 | - * |
|
| 143 | - * @return string |
|
| 144 | - */ |
|
| 145 | - public function algorithmName() |
|
| 146 | - { |
|
| 147 | - return $this->header() |
|
| 148 | - ->algorithm() |
|
| 149 | - ->value(); |
|
| 150 | - } |
|
| 140 | + /** |
|
| 141 | + * Get the signature algorithm name. |
|
| 142 | + * |
|
| 143 | + * @return string |
|
| 144 | + */ |
|
| 145 | + public function algorithmName() |
|
| 146 | + { |
|
| 147 | + return $this->header() |
|
| 148 | + ->algorithm() |
|
| 149 | + ->value(); |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - /** |
|
| 153 | - * Check whether JWS is unsecured, that is, contains no signature. |
|
| 154 | - * |
|
| 155 | - * @return bool |
|
| 156 | - */ |
|
| 157 | - public function isUnsecured() |
|
| 158 | - { |
|
| 159 | - return $this->algorithmName() == JWA::ALGO_NONE; |
|
| 160 | - } |
|
| 152 | + /** |
|
| 153 | + * Check whether JWS is unsecured, that is, contains no signature. |
|
| 154 | + * |
|
| 155 | + * @return bool |
|
| 156 | + */ |
|
| 157 | + public function isUnsecured() |
|
| 158 | + { |
|
| 159 | + return $this->algorithmName() == JWA::ALGO_NONE; |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | - /** |
|
| 163 | - * Get the payload. |
|
| 164 | - * |
|
| 165 | - * @return string |
|
| 166 | - */ |
|
| 167 | - public function payload() |
|
| 168 | - { |
|
| 169 | - return $this->_payload; |
|
| 170 | - } |
|
| 162 | + /** |
|
| 163 | + * Get the payload. |
|
| 164 | + * |
|
| 165 | + * @return string |
|
| 166 | + */ |
|
| 167 | + public function payload() |
|
| 168 | + { |
|
| 169 | + return $this->_payload; |
|
| 170 | + } |
|
| 171 | 171 | |
| 172 | - /** |
|
| 173 | - * Get the signature. |
|
| 174 | - * |
|
| 175 | - * @return string |
|
| 176 | - */ |
|
| 177 | - public function signature() |
|
| 178 | - { |
|
| 179 | - return $this->_signature; |
|
| 180 | - } |
|
| 172 | + /** |
|
| 173 | + * Get the signature. |
|
| 174 | + * |
|
| 175 | + * @return string |
|
| 176 | + */ |
|
| 177 | + public function signature() |
|
| 178 | + { |
|
| 179 | + return $this->_signature; |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | - /** |
|
| 183 | - * Get the payload encoded for serialization. |
|
| 184 | - * |
|
| 185 | - * @return string |
|
| 186 | - */ |
|
| 187 | - protected function _encodedPayload() |
|
| 188 | - { |
|
| 189 | - $b64 = true; |
|
| 190 | - if ($this->_protectedHeader->hasB64Payload()) { |
|
| 191 | - $b64 = $this->_protectedHeader->B64Payload()->value(); |
|
| 192 | - } |
|
| 193 | - return $b64 ? Base64::urlEncode($this->_payload) : $this->_payload; |
|
| 194 | - } |
|
| 182 | + /** |
|
| 183 | + * Get the payload encoded for serialization. |
|
| 184 | + * |
|
| 185 | + * @return string |
|
| 186 | + */ |
|
| 187 | + protected function _encodedPayload() |
|
| 188 | + { |
|
| 189 | + $b64 = true; |
|
| 190 | + if ($this->_protectedHeader->hasB64Payload()) { |
|
| 191 | + $b64 = $this->_protectedHeader->B64Payload()->value(); |
|
| 192 | + } |
|
| 193 | + return $b64 ? Base64::urlEncode($this->_payload) : $this->_payload; |
|
| 194 | + } |
|
| 195 | 195 | |
| 196 | - /** |
|
| 197 | - * Validate the signature using explicit algorithm. |
|
| 198 | - * |
|
| 199 | - * @param SignatureAlgorithm $algo |
|
| 200 | - * @throws \UnexpectedValueException If using different signature algorithm |
|
| 201 | - * then specified by the header |
|
| 202 | - * @throws \RuntimeException If signature computation fails |
|
| 203 | - * @return bool True if signature is valid |
|
| 204 | - */ |
|
| 205 | - public function validate(SignatureAlgorithm $algo) |
|
| 206 | - { |
|
| 207 | - if ($algo->algorithmParamValue() != $this->algorithmName()) { |
|
| 208 | - throw new \UnexpectedValueException("Invalid signature algorithm."); |
|
| 209 | - } |
|
| 210 | - return $algo->validateSignature($this->_signatureInput, |
|
| 211 | - $this->_signature); |
|
| 212 | - } |
|
| 196 | + /** |
|
| 197 | + * Validate the signature using explicit algorithm. |
|
| 198 | + * |
|
| 199 | + * @param SignatureAlgorithm $algo |
|
| 200 | + * @throws \UnexpectedValueException If using different signature algorithm |
|
| 201 | + * then specified by the header |
|
| 202 | + * @throws \RuntimeException If signature computation fails |
|
| 203 | + * @return bool True if signature is valid |
|
| 204 | + */ |
|
| 205 | + public function validate(SignatureAlgorithm $algo) |
|
| 206 | + { |
|
| 207 | + if ($algo->algorithmParamValue() != $this->algorithmName()) { |
|
| 208 | + throw new \UnexpectedValueException("Invalid signature algorithm."); |
|
| 209 | + } |
|
| 210 | + return $algo->validateSignature($this->_signatureInput, |
|
| 211 | + $this->_signature); |
|
| 212 | + } |
|
| 213 | 213 | |
| 214 | - /** |
|
| 215 | - * Validate the signature using the given JWK. |
|
| 216 | - * |
|
| 217 | - * Signature algorithm is determined from the header. |
|
| 218 | - * |
|
| 219 | - * @param JWK $jwk JSON Web Key |
|
| 220 | - * @throws \RuntimeException If algorithm initialization fails |
|
| 221 | - * @return bool True if signature is valid |
|
| 222 | - */ |
|
| 223 | - public function validateWithJWK(JWK $jwk) |
|
| 224 | - { |
|
| 225 | - $algo = SignatureAlgorithm::fromJWK($jwk, $this->header()); |
|
| 226 | - return $this->validate($algo); |
|
| 227 | - } |
|
| 214 | + /** |
|
| 215 | + * Validate the signature using the given JWK. |
|
| 216 | + * |
|
| 217 | + * Signature algorithm is determined from the header. |
|
| 218 | + * |
|
| 219 | + * @param JWK $jwk JSON Web Key |
|
| 220 | + * @throws \RuntimeException If algorithm initialization fails |
|
| 221 | + * @return bool True if signature is valid |
|
| 222 | + */ |
|
| 223 | + public function validateWithJWK(JWK $jwk) |
|
| 224 | + { |
|
| 225 | + $algo = SignatureAlgorithm::fromJWK($jwk, $this->header()); |
|
| 226 | + return $this->validate($algo); |
|
| 227 | + } |
|
| 228 | 228 | |
| 229 | - /** |
|
| 230 | - * Validate the signature using a key from the given JWK set. |
|
| 231 | - * |
|
| 232 | - * Correct key shall be sought by the key ID indicated by the header. |
|
| 233 | - * |
|
| 234 | - * @param JWKSet $set Set of JSON Web Keys |
|
| 235 | - * @throws \RuntimeException If algorithm initialization fails |
|
| 236 | - * @return bool True if signature is valid |
|
| 237 | - */ |
|
| 238 | - public function validateWithJWKSet(JWKSet $set) |
|
| 239 | - { |
|
| 240 | - if (!count($set)) { |
|
| 241 | - throw new \RuntimeException("No keys."); |
|
| 242 | - } |
|
| 243 | - $factory = new SignatureAlgorithmFactory($this->header()); |
|
| 244 | - $algo = $factory->algoByKeys($set); |
|
| 245 | - return $this->validate($algo); |
|
| 246 | - } |
|
| 229 | + /** |
|
| 230 | + * Validate the signature using a key from the given JWK set. |
|
| 231 | + * |
|
| 232 | + * Correct key shall be sought by the key ID indicated by the header. |
|
| 233 | + * |
|
| 234 | + * @param JWKSet $set Set of JSON Web Keys |
|
| 235 | + * @throws \RuntimeException If algorithm initialization fails |
|
| 236 | + * @return bool True if signature is valid |
|
| 237 | + */ |
|
| 238 | + public function validateWithJWKSet(JWKSet $set) |
|
| 239 | + { |
|
| 240 | + if (!count($set)) { |
|
| 241 | + throw new \RuntimeException("No keys."); |
|
| 242 | + } |
|
| 243 | + $factory = new SignatureAlgorithmFactory($this->header()); |
|
| 244 | + $algo = $factory->algoByKeys($set); |
|
| 245 | + return $this->validate($algo); |
|
| 246 | + } |
|
| 247 | 247 | |
| 248 | - /** |
|
| 249 | - * Convert to compact serialization. |
|
| 250 | - * |
|
| 251 | - * @return string |
|
| 252 | - */ |
|
| 253 | - public function toCompact() |
|
| 254 | - { |
|
| 255 | - return Base64::urlEncode($this->_protectedHeader->toJSON()) . "." . |
|
| 256 | - $this->_encodedPayload() . "." . |
|
| 257 | - Base64::urlEncode($this->_signature); |
|
| 258 | - } |
|
| 248 | + /** |
|
| 249 | + * Convert to compact serialization. |
|
| 250 | + * |
|
| 251 | + * @return string |
|
| 252 | + */ |
|
| 253 | + public function toCompact() |
|
| 254 | + { |
|
| 255 | + return Base64::urlEncode($this->_protectedHeader->toJSON()) . "." . |
|
| 256 | + $this->_encodedPayload() . "." . |
|
| 257 | + Base64::urlEncode($this->_signature); |
|
| 258 | + } |
|
| 259 | 259 | |
| 260 | - /** |
|
| 261 | - * Convert to compact serialization with payload detached. |
|
| 262 | - * |
|
| 263 | - * @return string |
|
| 264 | - */ |
|
| 265 | - public function toCompactDetached() |
|
| 266 | - { |
|
| 267 | - return Base64::urlEncode($this->_protectedHeader->toJSON()) . ".." . |
|
| 268 | - Base64::urlEncode($this->_signature); |
|
| 269 | - } |
|
| 260 | + /** |
|
| 261 | + * Convert to compact serialization with payload detached. |
|
| 262 | + * |
|
| 263 | + * @return string |
|
| 264 | + */ |
|
| 265 | + public function toCompactDetached() |
|
| 266 | + { |
|
| 267 | + return Base64::urlEncode($this->_protectedHeader->toJSON()) . ".." . |
|
| 268 | + Base64::urlEncode($this->_signature); |
|
| 269 | + } |
|
| 270 | 270 | |
| 271 | - /** |
|
| 272 | - * Generate input for the signature computation. |
|
| 273 | - * |
|
| 274 | - * @param string $payload Payload |
|
| 275 | - * @param Header $header Protected header |
|
| 276 | - * @return string |
|
| 277 | - */ |
|
| 278 | - protected static function _generateSignatureInput($payload, Header $header) |
|
| 279 | - { |
|
| 280 | - $b64 = $header->hasB64Payload() ? $header->B64Payload()->value() : true; |
|
| 281 | - $data = Base64::urlEncode($header->toJSON()) . "."; |
|
| 282 | - $data .= $b64 ? Base64::urlEncode($payload) : $payload; |
|
| 283 | - return $data; |
|
| 284 | - } |
|
| 271 | + /** |
|
| 272 | + * Generate input for the signature computation. |
|
| 273 | + * |
|
| 274 | + * @param string $payload Payload |
|
| 275 | + * @param Header $header Protected header |
|
| 276 | + * @return string |
|
| 277 | + */ |
|
| 278 | + protected static function _generateSignatureInput($payload, Header $header) |
|
| 279 | + { |
|
| 280 | + $b64 = $header->hasB64Payload() ? $header->B64Payload()->value() : true; |
|
| 281 | + $data = Base64::urlEncode($header->toJSON()) . "."; |
|
| 282 | + $data .= $b64 ? Base64::urlEncode($payload) : $payload; |
|
| 283 | + return $data; |
|
| 284 | + } |
|
| 285 | 285 | |
| 286 | - /** |
|
| 287 | - * Convert JWS to string. |
|
| 288 | - * |
|
| 289 | - * @return string |
|
| 290 | - */ |
|
| 291 | - public function __toString() |
|
| 292 | - { |
|
| 293 | - return $this->toCompact(); |
|
| 294 | - } |
|
| 286 | + /** |
|
| 287 | + * Convert JWS to string. |
|
| 288 | + * |
|
| 289 | + * @return string |
|
| 290 | + */ |
|
| 291 | + public function __toString() |
|
| 292 | + { |
|
| 293 | + return $this->toCompact(); |
|
| 294 | + } |
|
| 295 | 295 | } |
@@ -12,30 +12,30 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class ES512Algorithm extends ECDSAAlgorithm |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - protected function _curveName() |
|
| 20 | - { |
|
| 21 | - return CurveParameter::CURVE_P521; |
|
| 22 | - } |
|
| 15 | + /** |
|
| 16 | + * |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + protected function _curveName() |
|
| 20 | + { |
|
| 21 | + return CurveParameter::CURVE_P521; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * |
|
| 26 | - * {@inheritdoc} |
|
| 27 | - */ |
|
| 28 | - protected function _mdMethod() |
|
| 29 | - { |
|
| 30 | - return "sha512"; |
|
| 31 | - } |
|
| 24 | + /** |
|
| 25 | + * |
|
| 26 | + * {@inheritdoc} |
|
| 27 | + */ |
|
| 28 | + protected function _mdMethod() |
|
| 29 | + { |
|
| 30 | + return "sha512"; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * |
|
| 35 | - * {@inheritdoc} |
|
| 36 | - */ |
|
| 37 | - public function algorithmParamValue() |
|
| 38 | - { |
|
| 39 | - return JWA::ALGO_ES512; |
|
| 40 | - } |
|
| 33 | + /** |
|
| 34 | + * |
|
| 35 | + * {@inheritdoc} |
|
| 36 | + */ |
|
| 37 | + public function algorithmParamValue() |
|
| 38 | + { |
|
| 39 | + return JWA::ALGO_ES512; |
|
| 40 | + } |
|
| 41 | 41 | } |
@@ -11,21 +11,21 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class HS512Algorithm extends HMACAlgorithm |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * |
|
| 16 | - * {@inheritdoc} |
|
| 17 | - */ |
|
| 18 | - protected function _hashAlgo() |
|
| 19 | - { |
|
| 20 | - return "sha512"; |
|
| 21 | - } |
|
| 14 | + /** |
|
| 15 | + * |
|
| 16 | + * {@inheritdoc} |
|
| 17 | + */ |
|
| 18 | + protected function _hashAlgo() |
|
| 19 | + { |
|
| 20 | + return "sha512"; |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - public function algorithmParamValue() |
|
| 28 | - { |
|
| 29 | - return JWA::ALGO_HS512; |
|
| 30 | - } |
|
| 23 | + /** |
|
| 24 | + * |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + public function algorithmParamValue() |
|
| 28 | + { |
|
| 29 | + return JWA::ALGO_HS512; |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -12,89 +12,89 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class SignatureAlgorithmFactory |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Header. |
|
| 17 | - * |
|
| 18 | - * @var Header $_header |
|
| 19 | - */ |
|
| 20 | - protected $_header; |
|
| 15 | + /** |
|
| 16 | + * Header. |
|
| 17 | + * |
|
| 18 | + * @var Header $_header |
|
| 19 | + */ |
|
| 20 | + protected $_header; |
|
| 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 = array( |
|
| 30 | - /* @formatter:off */ |
|
| 31 | - JWA::ALGO_HS256 => HS256Algorithm::class, |
|
| 32 | - JWA::ALGO_HS384 => HS384Algorithm::class, |
|
| 33 | - JWA::ALGO_HS512 => HS512Algorithm::class, |
|
| 34 | - JWA::ALGO_RS256 => RS256Algorithm::class, |
|
| 35 | - JWA::ALGO_RS384 => RS384Algorithm::class, |
|
| 36 | - JWA::ALGO_RS512 => RS512Algorithm::class, |
|
| 37 | - JWA::ALGO_ES256 => ES256Algorithm::class, |
|
| 38 | - JWA::ALGO_ES384 => ES384Algorithm::class, |
|
| 39 | - JWA::ALGO_ES512 => ES512Algorithm::class |
|
| 40 | - /* @formatter:on */ |
|
| 41 | - ); |
|
| 22 | + /** |
|
| 23 | + * Mapping from algorithm name to class name. |
|
| 24 | + * |
|
| 25 | + * @internal |
|
| 26 | + * |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + const MAP_ALGO_TO_CLASS = array( |
|
| 30 | + /* @formatter:off */ |
|
| 31 | + JWA::ALGO_HS256 => HS256Algorithm::class, |
|
| 32 | + JWA::ALGO_HS384 => HS384Algorithm::class, |
|
| 33 | + JWA::ALGO_HS512 => HS512Algorithm::class, |
|
| 34 | + JWA::ALGO_RS256 => RS256Algorithm::class, |
|
| 35 | + JWA::ALGO_RS384 => RS384Algorithm::class, |
|
| 36 | + JWA::ALGO_RS512 => RS512Algorithm::class, |
|
| 37 | + JWA::ALGO_ES256 => ES256Algorithm::class, |
|
| 38 | + JWA::ALGO_ES384 => ES384Algorithm::class, |
|
| 39 | + JWA::ALGO_ES512 => ES512Algorithm::class |
|
| 40 | + /* @formatter:on */ |
|
| 41 | + ); |
|
| 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 | - * @return \JWX\JWS\SignatureAlgorithm |
|
| 58 | - */ |
|
| 59 | - public function algoByKey(JWK $jwk) |
|
| 60 | - { |
|
| 61 | - $alg = JWA::deriveAlgorithmName($this->_header, $jwk); |
|
| 62 | - $cls = self::_algoClassByName($alg); |
|
| 63 | - return $cls::fromJWK($jwk, $this->_header); |
|
| 64 | - } |
|
| 53 | + /** |
|
| 54 | + * Get signature algorithm by given JWK. |
|
| 55 | + * |
|
| 56 | + * @param JWK $jwk |
|
| 57 | + * @return \JWX\JWS\SignatureAlgorithm |
|
| 58 | + */ |
|
| 59 | + public function algoByKey(JWK $jwk) |
|
| 60 | + { |
|
| 61 | + $alg = JWA::deriveAlgorithmName($this->_header, $jwk); |
|
| 62 | + $cls = self::_algoClassByName($alg); |
|
| 63 | + return $cls::fromJWK($jwk, $this->_header); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Get signature algorithm using a matching key from given JWK set. |
|
| 68 | - * |
|
| 69 | - * @param JWKSet $set |
|
| 70 | - * @throws \UnexpectedValueException If a key cannot be found |
|
| 71 | - * @return \JWX\JWS\SignatureAlgorithm |
|
| 72 | - */ |
|
| 73 | - public function algoByKeys(JWKSet $set) |
|
| 74 | - { |
|
| 75 | - if (!$this->_header->hasKeyID()) { |
|
| 76 | - throw new \UnexpectedValueException("No key ID paremeter."); |
|
| 77 | - } |
|
| 78 | - $id = $this->_header->keyID()->value(); |
|
| 79 | - if (!$set->hasKeyID($id)) { |
|
| 80 | - throw new \UnexpectedValueException("No key for ID '$id'."); |
|
| 81 | - } |
|
| 82 | - return $this->algoByKey($set->keyByID($id)); |
|
| 83 | - } |
|
| 66 | + /** |
|
| 67 | + * Get signature algorithm using a matching key from given JWK set. |
|
| 68 | + * |
|
| 69 | + * @param JWKSet $set |
|
| 70 | + * @throws \UnexpectedValueException If a key cannot be found |
|
| 71 | + * @return \JWX\JWS\SignatureAlgorithm |
|
| 72 | + */ |
|
| 73 | + public function algoByKeys(JWKSet $set) |
|
| 74 | + { |
|
| 75 | + if (!$this->_header->hasKeyID()) { |
|
| 76 | + throw new \UnexpectedValueException("No key ID paremeter."); |
|
| 77 | + } |
|
| 78 | + $id = $this->_header->keyID()->value(); |
|
| 79 | + if (!$set->hasKeyID($id)) { |
|
| 80 | + throw new \UnexpectedValueException("No key for ID '$id'."); |
|
| 81 | + } |
|
| 82 | + return $this->algoByKey($set->keyByID($id)); |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Get the algorithm implementation class name by an algorithm name. |
|
| 87 | - * |
|
| 88 | - * @param string $alg Algorithm name |
|
| 89 | - * @throws \UnexpectedValueException |
|
| 90 | - * @return string Class name |
|
| 91 | - */ |
|
| 92 | - private static function _algoClassByName($alg) |
|
| 93 | - { |
|
| 94 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 95 | - throw new \UnexpectedValueException( |
|
| 96 | - "Algorithm '$alg' not supported."); |
|
| 97 | - } |
|
| 98 | - return self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 99 | - } |
|
| 85 | + /** |
|
| 86 | + * Get the algorithm implementation class name by an algorithm name. |
|
| 87 | + * |
|
| 88 | + * @param string $alg Algorithm name |
|
| 89 | + * @throws \UnexpectedValueException |
|
| 90 | + * @return string Class name |
|
| 91 | + */ |
|
| 92 | + private static function _algoClassByName($alg) |
|
| 93 | + { |
|
| 94 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 95 | + throw new \UnexpectedValueException( |
|
| 96 | + "Algorithm '$alg' not supported."); |
|
| 97 | + } |
|
| 98 | + return self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 99 | + } |
|
| 100 | 100 | } |
@@ -12,30 +12,30 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class ES256Algorithm extends ECDSAAlgorithm |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - protected function _curveName() |
|
| 20 | - { |
|
| 21 | - return CurveParameter::CURVE_P256; |
|
| 22 | - } |
|
| 15 | + /** |
|
| 16 | + * |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + protected function _curveName() |
|
| 20 | + { |
|
| 21 | + return CurveParameter::CURVE_P256; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * |
|
| 26 | - * {@inheritdoc} |
|
| 27 | - */ |
|
| 28 | - protected function _mdMethod() |
|
| 29 | - { |
|
| 30 | - return "sha256"; |
|
| 31 | - } |
|
| 24 | + /** |
|
| 25 | + * |
|
| 26 | + * {@inheritdoc} |
|
| 27 | + */ |
|
| 28 | + protected function _mdMethod() |
|
| 29 | + { |
|
| 30 | + return "sha256"; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * |
|
| 35 | - * {@inheritdoc} |
|
| 36 | - */ |
|
| 37 | - public function algorithmParamValue() |
|
| 38 | - { |
|
| 39 | - return JWA::ALGO_ES256; |
|
| 40 | - } |
|
| 33 | + /** |
|
| 34 | + * |
|
| 35 | + * {@inheritdoc} |
|
| 36 | + */ |
|
| 37 | + public function algorithmParamValue() |
|
| 38 | + { |
|
| 39 | + return JWA::ALGO_ES256; |
|
| 40 | + } |
|
| 41 | 41 | } |
@@ -11,21 +11,21 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class RS512Algorithm extends RSASSAPKCS1Algorithm |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * |
|
| 16 | - * {@inheritdoc} |
|
| 17 | - */ |
|
| 18 | - protected function _mdMethod() |
|
| 19 | - { |
|
| 20 | - return "sha512WithRSAEncryption"; |
|
| 21 | - } |
|
| 14 | + /** |
|
| 15 | + * |
|
| 16 | + * {@inheritdoc} |
|
| 17 | + */ |
|
| 18 | + protected function _mdMethod() |
|
| 19 | + { |
|
| 20 | + return "sha512WithRSAEncryption"; |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - public function algorithmParamValue() |
|
| 28 | - { |
|
| 29 | - return JWA::ALGO_RS512; |
|
| 30 | - } |
|
| 23 | + /** |
|
| 24 | + * |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + public function algorithmParamValue() |
|
| 28 | + { |
|
| 29 | + return JWA::ALGO_RS512; |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -14,41 +14,41 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class NoneAlgorithm extends SignatureAlgorithm |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * |
|
| 19 | - * {@inheritdoc} |
|
| 20 | - */ |
|
| 21 | - public function algorithmParamValue() |
|
| 22 | - { |
|
| 23 | - return JWA::ALGO_NONE; |
|
| 24 | - } |
|
| 17 | + /** |
|
| 18 | + * |
|
| 19 | + * {@inheritdoc} |
|
| 20 | + */ |
|
| 21 | + public function algorithmParamValue() |
|
| 22 | + { |
|
| 23 | + return JWA::ALGO_NONE; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * |
|
| 28 | - * {@inheritdoc} |
|
| 29 | - */ |
|
| 30 | - public function computeSignature($data) |
|
| 31 | - { |
|
| 32 | - return ""; |
|
| 33 | - } |
|
| 26 | + /** |
|
| 27 | + * |
|
| 28 | + * {@inheritdoc} |
|
| 29 | + */ |
|
| 30 | + public function computeSignature($data) |
|
| 31 | + { |
|
| 32 | + return ""; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * |
|
| 37 | - * {@inheritdoc} |
|
| 38 | - */ |
|
| 39 | - public function validateSignature($data, $signature) |
|
| 40 | - { |
|
| 41 | - return $signature === ""; |
|
| 42 | - } |
|
| 35 | + /** |
|
| 36 | + * |
|
| 37 | + * {@inheritdoc} |
|
| 38 | + */ |
|
| 39 | + public function validateSignature($data, $signature) |
|
| 40 | + { |
|
| 41 | + return $signature === ""; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * |
|
| 46 | - * @see \JWX\JWS\SignatureAlgorithm::headerParameters() |
|
| 47 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
| 48 | - */ |
|
| 49 | - public function headerParameters() |
|
| 50 | - { |
|
| 51 | - return array_merge(parent::headerParameters(), |
|
| 52 | - array(AlgorithmParameter::fromAlgorithm($this))); |
|
| 53 | - } |
|
| 44 | + /** |
|
| 45 | + * |
|
| 46 | + * @see \JWX\JWS\SignatureAlgorithm::headerParameters() |
|
| 47 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
| 48 | + */ |
|
| 49 | + public function headerParameters() |
|
| 50 | + { |
|
| 51 | + return array_merge(parent::headerParameters(), |
|
| 52 | + array(AlgorithmParameter::fromAlgorithm($this))); |
|
| 53 | + } |
|
| 54 | 54 | } |
@@ -11,21 +11,21 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class HS384Algorithm extends HMACAlgorithm |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * |
|
| 16 | - * {@inheritdoc} |
|
| 17 | - */ |
|
| 18 | - protected function _hashAlgo() |
|
| 19 | - { |
|
| 20 | - return "sha384"; |
|
| 21 | - } |
|
| 14 | + /** |
|
| 15 | + * |
|
| 16 | + * {@inheritdoc} |
|
| 17 | + */ |
|
| 18 | + protected function _hashAlgo() |
|
| 19 | + { |
|
| 20 | + return "sha384"; |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - public function algorithmParamValue() |
|
| 28 | - { |
|
| 29 | - return JWA::ALGO_HS384; |
|
| 30 | - } |
|
| 23 | + /** |
|
| 24 | + * |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + public function algorithmParamValue() |
|
| 28 | + { |
|
| 29 | + return JWA::ALGO_HS384; |
|
| 30 | + } |
|
| 31 | 31 | } |