| 1 |
<?php |
| 2 |
|
| 3 |
namespace Firebase\JWT; |
| 4 |
|
| 5 |
use DomainException; |
| 6 |
use InvalidArgumentException; |
| 7 |
use UnexpectedValueException; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class JWK |
| 22 |
{ |
| 23 |
private const OID = '1.2.840.10045.2.1'; |
| 24 |
private const ASN1_OBJECT_IDENTIFIER = 0x06; |
| 25 |
private const ASN1_SEQUENCE = 0x10; |
| 26 |
private const ASN1_BIT_STRING = 0x03; |
| 27 |
private const EC_CURVES = [ |
| 28 |
'P-256' => '1.2.840.10045.3.1.7', |
| 29 |
'secp256k1' => '1.3.132.0.10', |
| 30 |
'P-384' => '1.3.132.0.34', |
| 31 |
|
| 32 |
]; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
private const OKP_SUBTYPES = [ |
| 37 |
'Ed25519' => true, |
| 38 |
]; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public static function parseKeySet(#[\SensitiveParameter] array $jwks, ?string $defaultAlg = null): array |
| 56 |
{ |
| 57 |
$keys = []; |
| 58 |
|
| 59 |
if (!isset($jwks['keys'])) { |
| 60 |
throw new UnexpectedValueException('"keys" member must exist in the JWK Set'); |
| 61 |
} |
| 62 |
|
| 63 |
if (empty($jwks['keys'])) { |
| 64 |
throw new InvalidArgumentException('JWK Set did not contain any keys'); |
| 65 |
} |
| 66 |
|
| 67 |
foreach ($jwks['keys'] as $k => $v) { |
| 68 |
$kid = isset($v['kid']) ? $v['kid'] : $k; |
| 69 |
if ($key = self::parseKey($v, $defaultAlg)) { |
| 70 |
$keys[(string) $kid] = $key; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
if (0 === \count($keys)) { |
| 75 |
throw new UnexpectedValueException('No supported algorithms found in JWK Set'); |
| 76 |
} |
| 77 |
|
| 78 |
return $keys; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public static function parseKey(#[\SensitiveParameter] array $jwk, ?string $defaultAlg = null): ?Key |
| 97 |
{ |
| 98 |
if (empty($jwk)) { |
| 99 |
throw new InvalidArgumentException('JWK must not be empty'); |
| 100 |
} |
| 101 |
|
| 102 |
if (!isset($jwk['kty'])) { |
| 103 |
throw new UnexpectedValueException('JWK must contain a "kty" parameter'); |
| 104 |
} |
| 105 |
|
| 106 |
if (!isset($jwk['alg'])) { |
| 107 |
if (\is_null($defaultAlg)) { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
throw new UnexpectedValueException('JWK must contain an "alg" parameter'); |
| 113 |
} |
| 114 |
$jwk['alg'] = $defaultAlg; |
| 115 |
} |
| 116 |
|
| 117 |
switch ($jwk['kty']) { |
| 118 |
case 'RSA': |
| 119 |
if (!empty($jwk['d'])) { |
| 120 |
throw new UnexpectedValueException('RSA private keys are not supported'); |
| 121 |
} |
| 122 |
if (!isset($jwk['n']) || !isset($jwk['e'])) { |
| 123 |
throw new UnexpectedValueException('RSA keys must contain values for both "n" and "e"'); |
| 124 |
} |
| 125 |
|
| 126 |
$pem = self::createPemFromModulusAndExponent($jwk['n'], $jwk['e']); |
| 127 |
$publicKey = \openssl_pkey_get_public($pem); |
| 128 |
if (false === $publicKey) { |
| 129 |
throw new DomainException( |
| 130 |
'OpenSSL error: ' . \openssl_error_string() |
| 131 |
); |
| 132 |
} |
| 133 |
return new Key($publicKey, $jwk['alg']); |
| 134 |
case 'EC': |
| 135 |
if (isset($jwk['d'])) { |
| 136 |
|
| 137 |
throw new UnexpectedValueException('Key data must be for a public key'); |
| 138 |
} |
| 139 |
|
| 140 |
if (empty($jwk['crv'])) { |
| 141 |
throw new UnexpectedValueException('crv not set'); |
| 142 |
} |
| 143 |
|
| 144 |
if (!isset(self::EC_CURVES[$jwk['crv']])) { |
| 145 |
throw new DomainException('Unrecognised or unsupported EC curve'); |
| 146 |
} |
| 147 |
|
| 148 |
if (empty($jwk['x']) || empty($jwk['y'])) { |
| 149 |
throw new UnexpectedValueException('x and y not set'); |
| 150 |
} |
| 151 |
|
| 152 |
$publicKey = self::createPemFromCrvAndXYCoordinates($jwk['crv'], $jwk['x'], $jwk['y']); |
| 153 |
return new Key($publicKey, $jwk['alg']); |
| 154 |
case 'OKP': |
| 155 |
if (isset($jwk['d'])) { |
| 156 |
|
| 157 |
throw new UnexpectedValueException('Key data must be for a public key'); |
| 158 |
} |
| 159 |
|
| 160 |
if (!isset($jwk['crv'])) { |
| 161 |
throw new UnexpectedValueException('crv not set'); |
| 162 |
} |
| 163 |
|
| 164 |
if (empty(self::OKP_SUBTYPES[$jwk['crv']])) { |
| 165 |
throw new DomainException('Unrecognised or unsupported OKP key subtype'); |
| 166 |
} |
| 167 |
|
| 168 |
if (empty($jwk['x'])) { |
| 169 |
throw new UnexpectedValueException('x not set'); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
$publicKey = JWT::convertBase64urlToBase64($jwk['x']); |
| 174 |
return new Key($publicKey, $jwk['alg']); |
| 175 |
case 'oct': |
| 176 |
if (!isset($jwk['k'])) { |
| 177 |
throw new UnexpectedValueException('k not set'); |
| 178 |
} |
| 179 |
|
| 180 |
return new Key(JWT::urlsafeB64Decode($jwk['k']), $jwk['alg']); |
| 181 |
default: |
| 182 |
break; |
| 183 |
} |
| 184 |
|
| 185 |
return null; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
private static function createPemFromCrvAndXYCoordinates(string $crv, string $x, string $y): string |
| 198 |
{ |
| 199 |
$pem = |
| 200 |
self::encodeDER( |
| 201 |
self::ASN1_SEQUENCE, |
| 202 |
self::encodeDER( |
| 203 |
self::ASN1_SEQUENCE, |
| 204 |
self::encodeDER( |
| 205 |
self::ASN1_OBJECT_IDENTIFIER, |
| 206 |
self::encodeOID(self::OID) |
| 207 |
) |
| 208 |
. self::encodeDER( |
| 209 |
self::ASN1_OBJECT_IDENTIFIER, |
| 210 |
self::encodeOID(self::EC_CURVES[$crv]) |
| 211 |
) |
| 212 |
) . |
| 213 |
self::encodeDER( |
| 214 |
self::ASN1_BIT_STRING, |
| 215 |
\chr(0x00) . \chr(0x04) |
| 216 |
. JWT::urlsafeB64Decode($x) |
| 217 |
. JWT::urlsafeB64Decode($y) |
| 218 |
) |
| 219 |
); |
| 220 |
|
| 221 |
return \sprintf( |
| 222 |
"-----BEGIN PUBLIC KEY-----\n%s\n-----END PUBLIC KEY-----\n", |
| 223 |
wordwrap(base64_encode($pem), 64, "\n", true) |
| 224 |
); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
private static function createPemFromModulusAndExponent( |
| 238 |
string $n, |
| 239 |
string $e |
| 240 |
): string { |
| 241 |
$mod = JWT::urlsafeB64Decode($n); |
| 242 |
$exp = JWT::urlsafeB64Decode($e); |
| 243 |
|
| 244 |
|
| 245 |
if (\strlen($mod) > 0 && \ord($mod[0]) >= 128) { |
| 246 |
$mod = \chr(0) . $mod; |
| 247 |
} |
| 248 |
if (\strlen($exp) > 0 && \ord($exp[0]) >= 128) { |
| 249 |
$exp = \chr(0) . $exp; |
| 250 |
} |
| 251 |
|
| 252 |
$modulus = \pack('Ca*a*', 2, self::encodeLength(\strlen($mod)), $mod); |
| 253 |
$publicExponent = \pack('Ca*a*', 2, self::encodeLength(\strlen($exp)), $exp); |
| 254 |
|
| 255 |
$rsaPublicKey = \pack( |
| 256 |
'Ca*a*a*', |
| 257 |
48, |
| 258 |
self::encodeLength(\strlen($modulus) + \strlen($publicExponent)), |
| 259 |
$modulus, |
| 260 |
$publicExponent |
| 261 |
); |
| 262 |
|
| 263 |
|
| 264 |
$rsaOID = \pack('H*', '300d06092a864886f70d0101010500'); |
| 265 |
$rsaPublicKey = \chr(0) . $rsaPublicKey; |
| 266 |
$rsaPublicKey = \chr(3) . self::encodeLength(\strlen($rsaPublicKey)) . $rsaPublicKey; |
| 267 |
|
| 268 |
$rsaPublicKey = \pack( |
| 269 |
'Ca*a*', |
| 270 |
48, |
| 271 |
self::encodeLength(\strlen($rsaOID . $rsaPublicKey)), |
| 272 |
$rsaOID . $rsaPublicKey |
| 273 |
); |
| 274 |
|
| 275 |
return "-----BEGIN PUBLIC KEY-----\r\n" . |
| 276 |
\chunk_split(\base64_encode($rsaPublicKey), 64) . |
| 277 |
'-----END PUBLIC KEY-----'; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
private static function encodeLength(int $length): string |
| 290 |
{ |
| 291 |
if ($length <= 0x7F) { |
| 292 |
return \chr($length); |
| 293 |
} |
| 294 |
|
| 295 |
$temp = \ltrim(\pack('N', $length), \chr(0)); |
| 296 |
|
| 297 |
return \pack('Ca*', 0x80 | \strlen($temp), $temp); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
private static function encodeDER(int $type, string $value): string |
| 309 |
{ |
| 310 |
$tag_header = 0; |
| 311 |
if ($type === self::ASN1_SEQUENCE) { |
| 312 |
$tag_header |= 0x20; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
$der = \chr($tag_header | $type); |
| 317 |
|
| 318 |
|
| 319 |
$der .= \chr(\strlen($value)); |
| 320 |
|
| 321 |
return $der . $value; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
private static function encodeOID(string $oid): string |
| 331 |
{ |
| 332 |
$octets = explode('.', $oid); |
| 333 |
|
| 334 |
|
| 335 |
$first = (int) array_shift($octets); |
| 336 |
$second = (int) array_shift($octets); |
| 337 |
$oid = \chr($first * 40 + $second); |
| 338 |
|
| 339 |
|
| 340 |
foreach ($octets as $octet) { |
| 341 |
if ($octet == 0) { |
| 342 |
$oid .= \chr(0x00); |
| 343 |
continue; |
| 344 |
} |
| 345 |
$bin = ''; |
| 346 |
|
| 347 |
while ($octet) { |
| 348 |
$bin .= \chr(0x80 | ($octet & 0x7f)); |
| 349 |
$octet >>= 7; |
| 350 |
} |
| 351 |
$bin[0] = $bin[0] & \chr(0x7f); |
| 352 |
|
| 353 |
|
| 354 |
if (pack('V', 65534) == pack('L', 65534)) { |
| 355 |
$oid .= strrev($bin); |
| 356 |
} else { |
| 357 |
$oid .= $bin; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
return $oid; |
| 362 |
} |
| 363 |
} |