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