diff --git a/.ci-tools/phpstan-baseline.neon b/.ci-tools/phpstan-baseline.neon index ebcee86f..a7cae84f 100644 --- a/.ci-tools/phpstan-baseline.neon +++ b/.ci-tools/phpstan-baseline.neon @@ -5154,6 +5154,12 @@ parameters: count: 1 path: ../src/Library/Encryption/Serializer/JSONFlattenedSerializer.php + - + rawMessage: 'Only booleans are allowed in a ternary operator condition, mixed given.' + identifier: ternary.condNotBoolean + count: 1 + path: ../src/Library/Encryption/Serializer/JSONFlattenedSerializer.php + - rawMessage: 'Parameter #1 $encodedString of static method Jose\Component\Core\Util\Base64UrlSafe::decodeNoPadding() expects string, mixed given.' identifier: argument.type @@ -5220,6 +5226,12 @@ parameters: count: 1 path: ../src/Library/Encryption/Serializer/JSONGeneralSerializer.php + - + rawMessage: 'Only booleans are allowed in a ternary operator condition, mixed given.' + identifier: ternary.condNotBoolean + count: 1 + path: ../src/Library/Encryption/Serializer/JSONGeneralSerializer.php + - rawMessage: 'Parameter #1 $encodedString of static method Jose\Component\Core\Util\Base64UrlSafe::decodeNoPadding() expects string, mixed given.' identifier: argument.type @@ -5298,6 +5310,12 @@ parameters: count: 1 path: ../src/Library/KeyManagement/Analyzer/MixedPublicAndPrivateKeys.php + - + rawMessage: 'Only booleans are allowed in an if condition, mixed given.' + identifier: if.condNotBoolean + count: 1 + path: ../src/Library/KeyManagement/Analyzer/MixedPublicAndPrivateKeys.php + - rawMessage: 'Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.' identifier: argument.type diff --git a/src/Library/Core/Util/RSAKey.php b/src/Library/Core/Util/RSAKey.php index b7d44658..c84c0e61 100644 --- a/src/Library/Core/Util/RSAKey.php +++ b/src/Library/Core/Util/RSAKey.php @@ -18,6 +18,7 @@ use function array_key_exists; use function count; use function ltrim; +use function min; use function ord; use function sprintf; use function strlen; @@ -28,14 +29,12 @@ final class RSAKey { /** - * The limits of OpenSSL, in bytes: a modulus of 16384 bits at most, and a public exponent no longer than the - * modulus and of 64 bits at most above 3072 bits. RSA-PSS and RSAES compute the public operation in PHP, at a cost - * set by these lengths. + * In bytes: a modulus of 16384 bits at most, the limit of OpenSSL, and a public exponent no longer than the modulus + * and of 64 bits at most whatever the modulus, a limit OpenSSL applies above 3072 bits only. RSA-PSS and RSAES + * compute the public operation in PHP, at a cost set by these lengths. */ private const MAX_MODULUS_LENGTH = 2048; - private const SMALL_MODULUS_LENGTH = 384; - private const MAX_PUBLIC_EXPONENT_LENGTH = 8; private null|Sequence $sequence = null; @@ -71,8 +70,7 @@ private function __construct(JWK $data) if ($modulusLength > self::MAX_MODULUS_LENGTH) { throw new InvalidArgumentException('The key parameter "n" is too long.'); } - $limit = $modulusLength > self::SMALL_MODULUS_LENGTH ? self::MAX_PUBLIC_EXPONENT_LENGTH : $modulusLength; - if ($this->getMemberLength('e') > $limit) { + if ($this->getMemberLength('e') > min($modulusLength, self::MAX_PUBLIC_EXPONENT_LENGTH)) { throw new InvalidArgumentException('The key parameter "e" is too long.'); } $this->modulusLength = $modulusLength; diff --git a/tests/Component/Core/Util/RSAKeyTest.php b/tests/Component/Core/Util/RSAKeyTest.php index a65af1f0..37418fc4 100644 --- a/tests/Component/Core/Util/RSAKeyTest.php +++ b/tests/Component/Core/Util/RSAKeyTest.php @@ -32,17 +32,18 @@ protected function tearDown(): void } #[Test] - public function theLimitsOfOpenSSLAreAccepted(): void + public function theLargestKeysAreAccepted(): void { static::assertSame(2048, RSAKey::createFromJWK(self::key(2048, 8))->getModulusLength()); static::assertSame(385, RSAKey::createFromJWK(self::key(385, 8))->getModulusLength()); - static::assertSame(384, RSAKey::createFromJWK(self::key(384, 384))->getModulusLength()); - static::assertSame(256, RSAKey::createFromJWK(self::key(256, 9))->getModulusLength()); + static::assertSame(384, RSAKey::createFromJWK(self::key(384, 8))->getModulusLength()); + static::assertSame(256, RSAKey::createFromJWK(self::key(256, 8))->getModulusLength()); + static::assertSame(4, RSAKey::createFromJWK(self::key(4, 4))->getModulusLength()); } #[Test] #[DataProvider('oversizedKeys')] - public function aKeyBeyondTheLimitsOfOpenSSLIsRejected( + public function aKeyBeyondTheLimitsIsRejected( int $modulusLength, int $exponentLength, string $member @@ -61,8 +62,10 @@ public static function oversizedKeys(): iterable yield 'modulus of 16392 bits' => [2049, 3, 'n']; yield 'exponent of 72 bits, modulus of 16384 bits' => [2048, 9, 'e']; yield 'exponent of 72 bits, modulus of 3080 bits' => [385, 9, 'e']; - yield 'exponent longer than a modulus of 3072 bits' => [384, 385, 'e']; - yield 'exponent longer than a modulus of 2048 bits' => [256, 257, 'e']; + yield 'exponent of 72 bits, modulus of 3072 bits' => [384, 9, 'e']; + yield 'exponent of 72 bits, modulus of 2048 bits' => [256, 9, 'e']; + yield 'exponent as long as a modulus of 3072 bits' => [384, 384, 'e']; + yield 'exponent longer than a modulus of 32 bits' => [4, 5, 'e']; } #[Test]