| 1 |
<?php |
| 2 |
|
| 3 |
namespace Firebase\JWT; |
| 4 |
|
| 5 |
use ArrayAccess; |
| 6 |
use DateTime; |
| 7 |
use DomainException; |
| 8 |
use Exception; |
| 9 |
use InvalidArgumentException; |
| 10 |
use OpenSSLAsymmetricKey; |
| 11 |
use OpenSSLCertificate; |
| 12 |
use stdClass; |
| 13 |
use UnexpectedValueException; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class JWT |
| 29 |
{ |
| 30 |
private const ASN1_INTEGER = 0x02; |
| 31 |
private const ASN1_SEQUENCE = 0x10; |
| 32 |
private const ASN1_BIT_STRING = 0x03; |
| 33 |
|
| 34 |
private const RSA_KEY_MIN_LENGTH = 2048; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public static $leeway = 0; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public static $timestamp = null; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public static $supported_algs = [ |
| 58 |
'ES384' => ['openssl', 'SHA384'], |
| 59 |
'ES256' => ['openssl', 'SHA256'], |
| 60 |
'ES256K' => ['openssl', 'SHA256'], |
| 61 |
'HS256' => ['hash_hmac', 'SHA256'], |
| 62 |
'HS384' => ['hash_hmac', 'SHA384'], |
| 63 |
'HS512' => ['hash_hmac', 'SHA512'], |
| 64 |
'RS256' => ['openssl', 'SHA256'], |
| 65 |
'RS384' => ['openssl', 'SHA384'], |
| 66 |
'RS512' => ['openssl', 'SHA512'], |
| 67 |
'EdDSA' => ['sodium_crypto', 'EdDSA'], |
| 68 |
]; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public static function decode( |
| 99 |
string $jwt, |
| 100 |
#[\SensitiveParameter] $keyOrKeyArray, |
| 101 |
?stdClass &$headers = null |
| 102 |
): stdClass { |
| 103 |
|
| 104 |
$timestamp = \is_null(static::$timestamp) ? \time() : static::$timestamp; |
| 105 |
|
| 106 |
if (empty($keyOrKeyArray)) { |
| 107 |
throw new InvalidArgumentException('Key may not be empty'); |
| 108 |
} |
| 109 |
$tks = \explode('.', $jwt); |
| 110 |
if (\count($tks) !== 3) { |
| 111 |
throw new UnexpectedValueException('Wrong number of segments'); |
| 112 |
} |
| 113 |
list($headb64, $bodyb64, $cryptob64) = $tks; |
| 114 |
$headerRaw = static::urlsafeB64Decode($headb64); |
| 115 |
if (null === ($header = static::jsonDecode($headerRaw))) { |
| 116 |
throw new UnexpectedValueException('Invalid header encoding'); |
| 117 |
} |
| 118 |
if ($headers !== null) { |
| 119 |
$headers = $header; |
| 120 |
} |
| 121 |
$payloadRaw = static::urlsafeB64Decode($bodyb64); |
| 122 |
if (null === ($payload = static::jsonDecode($payloadRaw))) { |
| 123 |
throw new UnexpectedValueException('Invalid claims encoding'); |
| 124 |
} |
| 125 |
if (\is_array($payload)) { |
| 126 |
|
| 127 |
$payload = (object) $payload; |
| 128 |
} |
| 129 |
if (!$payload instanceof stdClass) { |
| 130 |
throw new UnexpectedValueException('Payload must be a JSON object'); |
| 131 |
} |
| 132 |
if (isset($payload->iat) && !\is_numeric($payload->iat)) { |
| 133 |
throw new UnexpectedValueException('Payload iat must be a number'); |
| 134 |
} |
| 135 |
if (isset($payload->nbf) && !\is_numeric($payload->nbf)) { |
| 136 |
throw new UnexpectedValueException('Payload nbf must be a number'); |
| 137 |
} |
| 138 |
if (isset($payload->exp) && !\is_numeric($payload->exp)) { |
| 139 |
throw new UnexpectedValueException('Payload exp must be a number'); |
| 140 |
} |
| 141 |
|
| 142 |
$sig = static::urlsafeB64Decode($cryptob64); |
| 143 |
if (empty($header->alg)) { |
| 144 |
throw new UnexpectedValueException('Empty algorithm'); |
| 145 |
} |
| 146 |
if (empty(static::$supported_algs[$header->alg])) { |
| 147 |
throw new UnexpectedValueException('Algorithm not supported'); |
| 148 |
} |
| 149 |
|
| 150 |
$key = self::getKey($keyOrKeyArray, property_exists($header, 'kid') ? $header->kid : null); |
| 151 |
|
| 152 |
|
| 153 |
if (!self::constantTimeEquals($key->getAlgorithm(), $header->alg)) { |
| 154 |
|
| 155 |
throw new UnexpectedValueException('Incorrect key for this algorithm'); |
| 156 |
} |
| 157 |
if (\in_array($header->alg, ['ES256', 'ES256K', 'ES384'], true)) { |
| 158 |
|
| 159 |
$sig = self::signatureToDER($sig); |
| 160 |
} |
| 161 |
if (!self::verify("{$headb64}.{$bodyb64}", $sig, $key->getKeyMaterial(), $header->alg)) { |
| 162 |
throw new SignatureInvalidException('Signature verification failed'); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
if (isset($payload->nbf) && floor($payload->nbf) > ($timestamp + static::$leeway)) { |
| 168 |
$ex = new BeforeValidException( |
| 169 |
'Cannot handle token with nbf prior to ' . \date(DateTime::ATOM, (int) floor($payload->nbf)) |
| 170 |
); |
| 171 |
$ex->setPayload($payload); |
| 172 |
throw $ex; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
if (!isset($payload->nbf) && isset($payload->iat) && floor($payload->iat) > ($timestamp + static::$leeway)) { |
| 179 |
$ex = new BeforeValidException( |
| 180 |
'Cannot handle token with iat prior to ' . \date(DateTime::ATOM, (int) floor($payload->iat)) |
| 181 |
); |
| 182 |
$ex->setPayload($payload); |
| 183 |
throw $ex; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
if (isset($payload->exp) && ($timestamp - static::$leeway) >= $payload->exp) { |
| 188 |
$ex = new ExpiredException('Expired token'); |
| 189 |
$ex->setPayload($payload); |
| 190 |
$ex->setTimestamp($timestamp); |
| 191 |
throw $ex; |
| 192 |
} |
| 193 |
|
| 194 |
return $payload; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public static function encode( |
| 213 |
array $payload, |
| 214 |
#[\SensitiveParameter] $key, |
| 215 |
string $alg, |
| 216 |
?string $keyId = null, |
| 217 |
?array $head = null |
| 218 |
): string { |
| 219 |
$header = ['typ' => 'JWT']; |
| 220 |
if (isset($head)) { |
| 221 |
$header = \array_merge($header, $head); |
| 222 |
} |
| 223 |
$header['alg'] = $alg; |
| 224 |
if ($keyId !== null) { |
| 225 |
$header['kid'] = $keyId; |
| 226 |
} |
| 227 |
$segments = []; |
| 228 |
$segments[] = static::urlsafeB64Encode((string) static::jsonEncode($header)); |
| 229 |
$segments[] = static::urlsafeB64Encode((string) static::jsonEncode($payload)); |
| 230 |
$signing_input = \implode('.', $segments); |
| 231 |
|
| 232 |
$signature = static::sign($signing_input, $key, $alg); |
| 233 |
$segments[] = static::urlsafeB64Encode($signature); |
| 234 |
|
| 235 |
return \implode('.', $segments); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public static function sign( |
| 251 |
string $msg, |
| 252 |
#[\SensitiveParameter] $key, |
| 253 |
string $alg |
| 254 |
): string { |
| 255 |
if (empty(static::$supported_algs[$alg])) { |
| 256 |
throw new DomainException('Algorithm not supported'); |
| 257 |
} |
| 258 |
list($function, $algorithm) = static::$supported_algs[$alg]; |
| 259 |
switch ($function) { |
| 260 |
case 'hash_hmac': |
| 261 |
if (!\is_string($key)) { |
| 262 |
throw new InvalidArgumentException('key must be a string when using hmac'); |
| 263 |
} |
| 264 |
self::validateHmacKeyLength($key, $algorithm); |
| 265 |
return \hash_hmac($algorithm, $msg, $key, true); |
| 266 |
case 'openssl': |
| 267 |
$signature = ''; |
| 268 |
if (!$key = openssl_pkey_get_private($key)) { |
| 269 |
throw new DomainException('OpenSSL unable to validate key'); |
| 270 |
} |
| 271 |
if (str_starts_with($alg, 'RS')) { |
| 272 |
self::validateRsaKeyLength($key); |
| 273 |
} elseif (str_starts_with($alg, 'ES')) { |
| 274 |
self::validateEcKeyLength($key, $alg); |
| 275 |
} |
| 276 |
$success = \openssl_sign($msg, $signature, $key, $algorithm); |
| 277 |
if (!$success) { |
| 278 |
throw new DomainException('OpenSSL unable to sign data'); |
| 279 |
} |
| 280 |
if ($alg === 'ES256' || $alg === 'ES256K') { |
| 281 |
$signature = self::signatureFromDER($signature, 256); |
| 282 |
} elseif ($alg === 'ES384') { |
| 283 |
$signature = self::signatureFromDER($signature, 384); |
| 284 |
} |
| 285 |
return $signature; |
| 286 |
case 'sodium_crypto': |
| 287 |
try { |
| 288 |
return sodium_crypto_sign_detached($msg, self::validateEdDSAKey($key)); |
| 289 |
} catch (Exception $e) { |
| 290 |
throw new DomainException($e->getMessage(), 0, $e); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
throw new DomainException('Algorithm not supported'); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
private static function verify( |
| 311 |
string $msg, |
| 312 |
string $signature, |
| 313 |
#[\SensitiveParameter] $keyMaterial, |
| 314 |
string $alg |
| 315 |
): bool { |
| 316 |
if (empty(static::$supported_algs[$alg])) { |
| 317 |
throw new DomainException('Algorithm not supported'); |
| 318 |
} |
| 319 |
|
| 320 |
list($function, $algorithm) = static::$supported_algs[$alg]; |
| 321 |
switch ($function) { |
| 322 |
case 'openssl': |
| 323 |
if (!$key = openssl_pkey_get_public($keyMaterial)) { |
| 324 |
throw new DomainException('OpenSSL unable to validate key'); |
| 325 |
} |
| 326 |
if (str_starts_with($alg, 'RS')) { |
| 327 |
self::validateRsaKeyLength($key); |
| 328 |
} elseif (str_starts_with($alg, 'ES')) { |
| 329 |
self::validateEcKeyLength($key, $alg); |
| 330 |
} |
| 331 |
$success = \openssl_verify($msg, $signature, $keyMaterial, $algorithm); |
| 332 |
if ($success === 1) { |
| 333 |
return true; |
| 334 |
} |
| 335 |
if ($success === 0) { |
| 336 |
return false; |
| 337 |
} |
| 338 |
|
| 339 |
throw new DomainException( |
| 340 |
'OpenSSL error: ' . \openssl_error_string() |
| 341 |
); |
| 342 |
case 'sodium_crypto': |
| 343 |
try { |
| 344 |
$key = self::validateEdDSAKey($keyMaterial); |
| 345 |
if (\strlen($signature) === 0) { |
| 346 |
throw new DomainException('Signature cannot be empty string'); |
| 347 |
} |
| 348 |
return sodium_crypto_sign_verify_detached($signature, $msg, $key); |
| 349 |
} catch (Exception $e) { |
| 350 |
throw new DomainException($e->getMessage(), 0, $e); |
| 351 |
} |
| 352 |
case 'hash_hmac': |
| 353 |
default: |
| 354 |
if (!\is_string($keyMaterial)) { |
| 355 |
throw new InvalidArgumentException('key must be a string when using hmac'); |
| 356 |
} |
| 357 |
self::validateHmacKeyLength($keyMaterial, $algorithm); |
| 358 |
$hash = \hash_hmac($algorithm, $msg, $keyMaterial, true); |
| 359 |
return self::constantTimeEquals($hash, $signature); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
public static function jsonDecode(string $input) |
| 373 |
{ |
| 374 |
$obj = \json_decode($input, false, 512, JSON_BIGINT_AS_STRING); |
| 375 |
|
| 376 |
if ($errno = \json_last_error()) { |
| 377 |
self::handleJsonError($errno); |
| 378 |
} elseif ($obj === null && $input !== 'null') { |
| 379 |
throw new DomainException('Null result with non-null input'); |
| 380 |
} |
| 381 |
return $obj; |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public static function jsonEncode(array $input): string |
| 394 |
{ |
| 395 |
$json = \json_encode($input, \JSON_UNESCAPED_SLASHES); |
| 396 |
if ($errno = \json_last_error()) { |
| 397 |
self::handleJsonError($errno); |
| 398 |
} elseif ($json === 'null') { |
| 399 |
throw new DomainException('Null result with non-null input'); |
| 400 |
} |
| 401 |
if ($json === false) { |
| 402 |
throw new DomainException('Provided object could not be encoded to valid JSON'); |
| 403 |
} |
| 404 |
return $json; |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
public static function urlsafeB64Decode(string $input): string |
| 417 |
{ |
| 418 |
return \base64_decode(self::convertBase64UrlToBase64($input)); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
public static function convertBase64UrlToBase64(string $input): string |
| 432 |
{ |
| 433 |
$remainder = \strlen($input) % 4; |
| 434 |
if ($remainder) { |
| 435 |
$padlen = 4 - $remainder; |
| 436 |
$input .= \str_repeat('=', $padlen); |
| 437 |
} |
| 438 |
return \strtr($input, '-_', '+/'); |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
public static function urlsafeB64Encode(string $input): string |
| 449 |
{ |
| 450 |
return \str_replace('=', '', \strtr(\base64_encode($input), '+/', '-_')); |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
private static function getKey( |
| 464 |
#[\SensitiveParameter] $keyOrKeyArray, |
| 465 |
?string $kid |
| 466 |
): Key { |
| 467 |
if ($keyOrKeyArray instanceof Key) { |
| 468 |
return $keyOrKeyArray; |
| 469 |
} |
| 470 |
|
| 471 |
if (empty($kid) && $kid !== '0') { |
| 472 |
throw new UnexpectedValueException('"kid" empty, unable to lookup correct key'); |
| 473 |
} |
| 474 |
|
| 475 |
if ($keyOrKeyArray instanceof CachedKeySet) { |
| 476 |
|
| 477 |
return $keyOrKeyArray[$kid]; |
| 478 |
} |
| 479 |
|
| 480 |
if (!isset($keyOrKeyArray[$kid])) { |
| 481 |
throw new UnexpectedValueException('"kid" invalid, unable to lookup correct key'); |
| 482 |
} |
| 483 |
|
| 484 |
return $keyOrKeyArray[$kid]; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
public static function constantTimeEquals(string $left, string $right): bool |
| 493 |
{ |
| 494 |
if (\function_exists('hash_equals')) { |
| 495 |
return \hash_equals($left, $right); |
| 496 |
} |
| 497 |
$len = \min(self::safeStrlen($left), self::safeStrlen($right)); |
| 498 |
|
| 499 |
$status = 0; |
| 500 |
for ($i = 0; $i < $len; $i++) { |
| 501 |
$status |= (\ord($left[$i]) ^ \ord($right[$i])); |
| 502 |
} |
| 503 |
$status |= (self::safeStrlen($left) ^ self::safeStrlen($right)); |
| 504 |
|
| 505 |
return ($status === 0); |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
private static function handleJsonError(int $errno): void |
| 518 |
{ |
| 519 |
$messages = [ |
| 520 |
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', |
| 521 |
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON', |
| 522 |
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', |
| 523 |
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', |
| 524 |
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters' |
| 525 |
]; |
| 526 |
throw new DomainException( |
| 527 |
isset($messages[$errno]) |
| 528 |
? $messages[$errno] |
| 529 |
: 'Unknown JSON error: ' . $errno |
| 530 |
); |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
private static function safeStrlen(string $str): int |
| 541 |
{ |
| 542 |
if (\function_exists('mb_strlen')) { |
| 543 |
return \mb_strlen($str, '8bit'); |
| 544 |
} |
| 545 |
return \strlen($str); |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
private static function signatureToDER(string $sig): string |
| 555 |
{ |
| 556 |
|
| 557 |
$length = max(1, (int) (\strlen($sig) / 2)); |
| 558 |
list($r, $s) = \str_split($sig, $length); |
| 559 |
|
| 560 |
|
| 561 |
$r = \ltrim($r, "\x00"); |
| 562 |
$s = \ltrim($s, "\x00"); |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
if (\ord($r[0]) > 0x7f) { |
| 567 |
$r = "\x00" . $r; |
| 568 |
} |
| 569 |
if (\ord($s[0]) > 0x7f) { |
| 570 |
$s = "\x00" . $s; |
| 571 |
} |
| 572 |
|
| 573 |
return self::encodeDER( |
| 574 |
self::ASN1_SEQUENCE, |
| 575 |
self::encodeDER(self::ASN1_INTEGER, $r) . |
| 576 |
self::encodeDER(self::ASN1_INTEGER, $s) |
| 577 |
); |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
private static function encodeDER(int $type, string $value): string |
| 589 |
{ |
| 590 |
$tag_header = 0; |
| 591 |
if ($type === self::ASN1_SEQUENCE) { |
| 592 |
$tag_header |= 0x20; |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
$der = \chr($tag_header | $type); |
| 597 |
|
| 598 |
|
| 599 |
$der .= \chr(\strlen($value)); |
| 600 |
|
| 601 |
return $der . $value; |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
private static function signatureFromDER(string $der, int $keySize): string |
| 613 |
{ |
| 614 |
|
| 615 |
list($offset, $_) = self::readDER($der); |
| 616 |
list($offset, $r) = self::readDER($der, $offset); |
| 617 |
list($offset, $s) = self::readDER($der, $offset); |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
$r = \ltrim($r, "\x00"); |
| 622 |
$s = \ltrim($s, "\x00"); |
| 623 |
|
| 624 |
|
| 625 |
$r = \str_pad($r, $keySize / 8, "\x00", STR_PAD_LEFT); |
| 626 |
$s = \str_pad($s, $keySize / 8, "\x00", STR_PAD_LEFT); |
| 627 |
|
| 628 |
return $r . $s; |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
private static function readDER(string $der, int $offset = 0): array |
| 641 |
{ |
| 642 |
$pos = $offset; |
| 643 |
$size = \strlen($der); |
| 644 |
$constructed = (\ord($der[$pos]) >> 5) & 0x01; |
| 645 |
$type = \ord($der[$pos++]) & 0x1f; |
| 646 |
|
| 647 |
|
| 648 |
$len = \ord($der[$pos++]); |
| 649 |
if ($len & 0x80) { |
| 650 |
$n = $len & 0x1f; |
| 651 |
$len = 0; |
| 652 |
while ($n-- && $pos < $size) { |
| 653 |
$len = ($len << 8) | \ord($der[$pos++]); |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
if ($type === self::ASN1_BIT_STRING) { |
| 659 |
$pos++; |
| 660 |
$data = \substr($der, $pos, $len - 1); |
| 661 |
$pos += $len - 1; |
| 662 |
} elseif (!$constructed) { |
| 663 |
$data = \substr($der, $pos, $len); |
| 664 |
$pos += $len; |
| 665 |
} else { |
| 666 |
$data = null; |
| 667 |
} |
| 668 |
|
| 669 |
return [$pos, $data]; |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
private static function validateHmacKeyLength(string $key, string $algorithm): void |
| 681 |
{ |
| 682 |
$keyLength = \strlen($key) * 8; |
| 683 |
$minKeyLength = (int) \str_replace('SHA', '', $algorithm); |
| 684 |
if ($keyLength < $minKeyLength) { |
| 685 |
throw new DomainException('Provided key is too short'); |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
private static function validateRsaKeyLength(#[\SensitiveParameter] OpenSSLAsymmetricKey $key): void |
| 696 |
{ |
| 697 |
if (!$keyDetails = openssl_pkey_get_details($key)) { |
| 698 |
throw new DomainException('Unable to validate key'); |
| 699 |
} |
| 700 |
if ($keyDetails['bits'] < self::RSA_KEY_MIN_LENGTH) { |
| 701 |
throw new DomainException('Provided key is too short'); |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
private static function validateEcKeyLength( |
| 713 |
#[\SensitiveParameter] OpenSSLAsymmetricKey $key, |
| 714 |
string $algorithm |
| 715 |
): void { |
| 716 |
if (!$keyDetails = openssl_pkey_get_details($key)) { |
| 717 |
throw new DomainException('Unable to validate key'); |
| 718 |
} |
| 719 |
$minKeyLength = (int) \str_replace('ES', '', $algorithm); |
| 720 |
if ($keyDetails['bits'] < $minKeyLength) { |
| 721 |
throw new DomainException('Provided key is too short'); |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
private static function validateEdDSAKey(#[\SensitiveParameter] $keyMaterial): string |
| 730 |
{ |
| 731 |
if (!\function_exists('sodium_crypto_sign_verify_detached')) { |
| 732 |
throw new DomainException('libsodium is not available'); |
| 733 |
} |
| 734 |
if (!\is_string($keyMaterial)) { |
| 735 |
throw new InvalidArgumentException('key must be a string when using EdDSA'); |
| 736 |
} |
| 737 |
|
| 738 |
$lines = array_filter(explode("\n", $keyMaterial)); |
| 739 |
$key = self::urlsafeB64Decode((string) end($lines)); |
| 740 |
if (\strlen($key) === 0) { |
| 741 |
throw new DomainException('Key cannot be empty string'); |
| 742 |
} |
| 743 |
return $key; |
| 744 |
} |
| 745 |
} |