Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,16 @@ public static function sign(
if (!\is_string($key)) {
throw new InvalidArgumentException('key must be a string when using hmac');
}
self::validateHmacKeyLength($key, $algorithm);
return \hash_hmac($algorithm, $msg, $key, true);
case 'openssl':
$signature = '';
if (!\is_resource($key) && !openssl_pkey_get_private($key)) {
throw new DomainException('OpenSSL unable to validate key');
}
if (str_starts_with($algorithm, 'RS')) {
self::validateRsaKeyLength($key);
}
$success = \openssl_sign($msg, $signature, $key, $algorithm); // @phpstan-ignore-line
if (!$success) {
throw new DomainException('OpenSSL unable to sign data');
Expand Down Expand Up @@ -324,6 +328,9 @@ private static function verify(
list($function, $algorithm) = static::$supported_algs[$alg];
switch ($function) {
case 'openssl':
if (str_starts_with($algorithm, 'RS')) {
self::validateRsaKeyLength($keyMaterial);
}
$success = \openssl_verify($msg, $signature, $keyMaterial, $algorithm); // @phpstan-ignore-line
if ($success === 1) {
return true;
Expand Down Expand Up @@ -361,6 +368,7 @@ private static function verify(
if (!\is_string($keyMaterial)) {
throw new InvalidArgumentException('key must be a string when using hmac');
}
self::validateHmacKeyLength($keyMaterial, $algorithm);
$hash = \hash_hmac($algorithm, $msg, $keyMaterial, true);
return self::constantTimeEquals($hash, $signature);
}
Expand Down Expand Up @@ -675,4 +683,38 @@ private static function readDER(string $der, int $offset = 0): array

return [$pos, $data];
}

/**
* Validate HMAC key length
*
* @param string $key HMAC key material
* @param string $algorithm The algorithm
*
* @throws DomainException Provided key is too short
*/
private static function validateHmacKeyLength(string $key, string $algorithm): void
{
$keyLength = strlen($key) * 8;
$minKeyLength = (int)str_replace($algorithm, 'SHA', '');
if ($keyLength < $minKeyLength) {
throw new DomainException('Provided key is too short');
}
}

/**
* Validate RSA key length
*
* @param OpenSSLAsymmetricKey|OpenSSLCertificate $key RSA key material
*
* @throws DomainException Provided key is too short
*/
private static function validateRsaKeyLength(OpenSSLAsymmetricKey|OpenSSLCertificate $key): void
{
$keyDetails = openssl_pkey_get_details(openssl_pkey_get_private($key));
$keyLength = $keyDetails['bits'];
$minKeyLength = 2048;
if ($keyLength < $minKeyLength) {
throw new DomainException('Provided key is too short');
}
}
}
Loading