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 @@
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);

Check failure on line 270 in src/JWT.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Parameter #1 $key of static method Firebase\JWT\JWT::validateRsaKeyLength() expects OpenSSLAsymmetricKey|OpenSSLCertificate, OpenSSLAsymmetricKey|OpenSSLCertificate|resource|string given.
}
$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 @@
list($function, $algorithm) = static::$supported_algs[$alg];
switch ($function) {
case 'openssl':
if (str_starts_with($algorithm, 'RS')) {
self::validateRsaKeyLength($keyMaterial);

Check failure on line 332 in src/JWT.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Parameter #1 $key of static method Firebase\JWT\JWT::validateRsaKeyLength() expects OpenSSLAsymmetricKey|OpenSSLCertificate, OpenSSLAsymmetricKey|OpenSSLCertificate|resource|string given.
}
$success = \openssl_verify($msg, $signature, $keyMaterial, $algorithm); // @phpstan-ignore-line
if ($success === 1) {
return true;
Expand Down Expand Up @@ -361,6 +368,7 @@
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 @@

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));

Check failure on line 713 in src/JWT.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Parameter #1 $key of function openssl_pkey_get_details expects OpenSSLAsymmetricKey, OpenSSLAsymmetricKey|false given.
$keyLength = $keyDetails['bits'];

Check failure on line 714 in src/JWT.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Cannot access offset 'bits' on array|false.
$minKeyLength = 2048;
if ($keyLength < $minKeyLength) {
throw new DomainException('Provided key is too short');
}
}
}
Loading
Loading