Skip to content
Merged
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
18 changes: 18 additions & 0 deletions .ci-tools/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions src/Library/Core/Util/RSAKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 9 additions & 6 deletions tests/Component/Core/Util/RSAKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
Loading