Skip to content

Commit a9eb44a

Browse files
committed
Add key size validation
1 parent a3edb39 commit a9eb44a

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/JWT.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,16 @@ public static function sign(
259259
if (!\is_string($key)) {
260260
throw new InvalidArgumentException('key must be a string when using hmac');
261261
}
262+
self::validateHmacKeyLength($key, $algorithm);
262263
return \hash_hmac($algorithm, $msg, $key, true);
263264
case 'openssl':
264265
$signature = '';
265266
if (!\is_resource($key) && !openssl_pkey_get_private($key)) {
266267
throw new DomainException('OpenSSL unable to validate key');
267268
}
269+
if (str_starts_with($algorithm, 'RS')) {
270+
self::validateRsaKeyLength($key);
271+
}
268272
$success = \openssl_sign($msg, $signature, $key, $algorithm); // @phpstan-ignore-line
269273
if (!$success) {
270274
throw new DomainException('OpenSSL unable to sign data');
@@ -324,6 +328,9 @@ private static function verify(
324328
list($function, $algorithm) = static::$supported_algs[$alg];
325329
switch ($function) {
326330
case 'openssl':
331+
if (str_starts_with($algorithm, 'RS')) {
332+
self::validateRsaKeyLength($keyMaterial);
333+
}
327334
$success = \openssl_verify($msg, $signature, $keyMaterial, $algorithm); // @phpstan-ignore-line
328335
if ($success === 1) {
329336
return true;
@@ -361,6 +368,7 @@ private static function verify(
361368
if (!\is_string($keyMaterial)) {
362369
throw new InvalidArgumentException('key must be a string when using hmac');
363370
}
371+
self::validateHmacKeyLength($keyMaterial, $algorithm);
364372
$hash = \hash_hmac($algorithm, $msg, $keyMaterial, true);
365373
return self::constantTimeEquals($hash, $signature);
366374
}
@@ -675,4 +683,38 @@ private static function readDER(string $der, int $offset = 0): array
675683

676684
return [$pos, $data];
677685
}
686+
687+
/**
688+
* Validate HMAC key length
689+
*
690+
* @param string $key HMAC key material
691+
* @param string $algorithm The algorithm
692+
*
693+
* @throws DomainException Provided key is too short
694+
*/
695+
private static function validateHmacKeyLength(string $key, string $algorithm): void
696+
{
697+
$keyLength = strlen($key) * 8;
698+
$minKeyLength = (int)str_replace($algorithm, 'SHA', '');
699+
if ($keyLength < $minKeyLength) {
700+
throw new DomainException('Provided key is too short');
701+
}
702+
}
703+
704+
/**
705+
* Validate RSA key length
706+
*
707+
* @param OpenSSLAsymmetricKey|OpenSSLCertificate $key RSA key material
708+
*
709+
* @throws DomainException Provided key is too short
710+
*/
711+
private static function validateRsaKeyLength(OpenSSLAsymmetricKey|OpenSSLCertificate $key): void
712+
{
713+
$keyDetails = openssl_pkey_get_details(openssl_pkey_get_private($key));
714+
$keyLength = $keyDetails['bits'];
715+
$minKeyLength = 2048;
716+
if ($keyLength < $minKeyLength) {
717+
throw new DomainException('Provided key is too short');
718+
}
719+
}
678720
}

0 commit comments

Comments
 (0)