Skip to content
Closed
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
7 changes: 7 additions & 0 deletions src/Library/KeyManagement/Analyzer/ESKeyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@
}
$xBI = BigInteger::fromBase(bin2hex($x), 16);
$yBI = BigInteger::fromBase(bin2hex($y), 16);
$prime = $this->getCurve()
->getPrime();
if ($xBI->isGreaterThanOrEqualTo($prime) || $yBI->isGreaterThanOrEqualTo($prime)) {
$bag->add(Message::high('Invalid key. The components "x" and "y" shall be lower than the field prime.'));

return;

Check warning on line 68 in src/Library/KeyManagement/Analyzer/ESKeyAnalyzer.php

View workflow job for this annotation

GitHub Actions / 🧬 Mutation Testing

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ if ($xBI->isGreaterThanOrEqualTo($prime) || $yBI->isGreaterThanOrEqualTo($prime)) { $bag->add(Message::high('Invalid key. The components "x" and "y" shall be lower than the field prime.')); - return; + } if (! $this->getCurve()->contains($xBI, $yBI)) { $bag->add(Message::high('Invalid key. The point is not on the curve.'));
}
if (! $this->getCurve()->contains($xBI, $yBI)) {
$bag->add(Message::high('Invalid key. The point is not on the curve.'));
}
Expand Down
26 changes: 16 additions & 10 deletions src/Library/KeyManagement/Analyzer/RsaAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

namespace Jose\Component\KeyManagement\Analyzer;

use Jose\Component\Core\Exception\InvalidKeyException;
use Jose\Component\Core\JWK;
use Jose\Component\Core\Util\Base64UrlSafe;
use Jose\Component\Core\Util\BigInteger;
use Override;
use function is_array;
use function is_string;
use function strlen;
use const STR_PAD_RIGHT;

final readonly class RsaAnalyzer implements KeyAnalyzer
{
Expand All @@ -34,11 +31,14 @@

return;
}
$exponent = unpack('l', str_pad(Base64UrlSafe::decodeNoPadding($e), 4, "\0", STR_PAD_RIGHT));
if (! is_array($exponent) || ! isset($exponent[1])) {
throw new InvalidKeyException('Unable to get the private key');
$e = Base64UrlSafe::decodeNoPadding($e);
if ($e === '') {
$bag->add(Message::high('The exponent is not valid.'));

return;
}
if ($exponent[1] < 65537) {
$exponent = BigInteger::createFromBinaryString($e);
if ($exponent->lowerThan(BigInteger::createFromDecimal(65537))) {

Check warning on line 41 in src/Library/KeyManagement/Analyzer/RsaAnalyzer.php

View workflow job for this annotation

GitHub Actions / 🧬 Mutation Testing

Escaped Mutant for Mutator "IncrementInteger": @@ @@ return; } $exponent = BigInteger::createFromBinaryString($e); - if ($exponent->lowerThan(BigInteger::createFromDecimal(65537))) { + if ($exponent->lowerThan(BigInteger::createFromDecimal(65538))) { $bag->add(Message::high('The exponent is too low. It should be at least 65537.')); } }

Check warning on line 41 in src/Library/KeyManagement/Analyzer/RsaAnalyzer.php

View workflow job for this annotation

GitHub Actions / 🧬 Mutation Testing

Escaped Mutant for Mutator "DecrementInteger": @@ @@ return; } $exponent = BigInteger::createFromBinaryString($e); - if ($exponent->lowerThan(BigInteger::createFromDecimal(65537))) { + if ($exponent->lowerThan(BigInteger::createFromDecimal(65536))) { $bag->add(Message::high('The exponent is too low. It should be at least 65537.')); } }
$bag->add(Message::high('The exponent is too low. It should be at least 65537.'));
}
}
Expand All @@ -51,8 +51,14 @@

return;
}
$n = 8 * strlen(Base64UrlSafe::decodeNoPadding($n));
if ($n < 2048) {
$n = Base64UrlSafe::decodeNoPadding($n);
if ($n === '') {
$bag->add(Message::high('The modulus is not valid.'));

return;
}
$modulus = BigInteger::createFromBinaryString($n);
if ($modulus->get()->getBitLength() < 2048) {

Check warning on line 61 in src/Library/KeyManagement/Analyzer/RsaAnalyzer.php

View workflow job for this annotation

GitHub Actions / 🧬 Mutation Testing

Escaped Mutant for Mutator "LessThan": @@ @@ return; } $modulus = BigInteger::createFromBinaryString($n); - if ($modulus->get()->getBitLength() < 2048) { + if ($modulus->get()->getBitLength() <= 2048) { $bag->add(Message::high('The key length is less than 2048 bits.')); } if ($jwk->has('d') && (! $jwk->has('p') || ! $jwk->has('q') || ! $jwk->has('dp') || ! $jwk->has(
$bag->add(Message::high('The key length is less than 2048 bits.'));
}
if ($jwk->has('d') && (! $jwk->has('p') || ! $jwk->has('q') || ! $jwk->has('dp') || ! $jwk->has(
Expand Down
44 changes: 44 additions & 0 deletions tests/Component/KeyManagement/JWKAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace Jose\Tests\Component\KeyManagement;

use Brick\Math\BigInteger;
use Jose\Component\Core\JWK;
use Jose\Component\Core\Util\Base64UrlSafe;
use Jose\Component\Core\Util\Ecc\NistCurve;
use Jose\Component\KeyManagement\Analyzer\AlgorithmAnalyzer;
use Jose\Component\KeyManagement\Analyzer\ES256KeyAnalyzer;
use Jose\Component\KeyManagement\Analyzer\ES384KeyAnalyzer;
Expand All @@ -14,6 +17,7 @@
use Jose\Component\KeyManagement\Analyzer\HS512KeyAnalyzer;
use Jose\Component\KeyManagement\Analyzer\KeyAnalyzerManager;
use Jose\Component\KeyManagement\Analyzer\KeyIdentifierAnalyzer;
use Jose\Component\KeyManagement\Analyzer\Message;
use Jose\Component\KeyManagement\Analyzer\NoneAnalyzer;
use Jose\Component\KeyManagement\Analyzer\OctAnalyzer;
use Jose\Component\KeyManagement\Analyzer\RsaAnalyzer;
Expand All @@ -23,6 +27,7 @@
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use const STR_PAD_LEFT;

/**
* @internal
Expand Down Expand Up @@ -74,6 +79,45 @@ public function theRsaKeyHasALowExponent(): void
static::fail('The low exponent should be catched');
}

#[Test]
public function theRsaKeyWithLeadingZeroOctetsIsAnalyzedByItsValue(): void
{
$key = new JWK([
'kty' => 'RSA',
'n' => Base64UrlSafe::encodeUnpadded(str_repeat("\0", 255) . "\x0f"),
'e' => Base64UrlSafe::encodeUnpadded("\0\0\0\1"),
]);
$messages = $this->getKeyAnalyzer()
->analyze($key);

$texts = array_map(static fn (Message $message): string => $message->getMessage(), $messages->all());
static::assertContains('The key length is less than 2048 bits.', $texts);
static::assertContains('The exponent is too low. It should be at least 65537.', $texts);
}

#[Test]
public function theEcKeyWithACoordinateAboveTheFieldPrimeIsRejected(): void
{
$key = JWKFactory::createECKey('P-521');
$prime = NistCurve::curve521()->getPrime();
$y = BigInteger::fromBase(bin2hex(Base64UrlSafe::decodeNoPadding($key->get('y'))), 16)->plus($prime);
$y = str_pad(hex2bin(str_pad($y->toBase(16), 132, '0', STR_PAD_LEFT)), 66, "\0", STR_PAD_LEFT);
$key = new JWK([
'kty' => 'EC',
'crv' => 'P-521',
'x' => $key->get('x'),
'y' => Base64UrlSafe::encodeUnpadded($y),
]);
$messages = $this->getKeyAnalyzer()
->analyze($key);

$texts = array_map(static fn (Message $message): string => $message->getMessage(), $messages->all());
static::assertContains(
'Invalid key. The components "x" and "y" shall be lower than the field prime.',
$texts
);
}

#[Test]
public function iCanAnalyzeAnOctKeyAndGetMessages(): void
{
Expand Down
Loading