|
4 | 4 |
|
5 | 5 | namespace Jose\Tests\Component\KeyManagement; |
6 | 6 |
|
| 7 | +use Brick\Math\BigInteger; |
7 | 8 | use Jose\Component\Core\JWK; |
| 9 | +use Jose\Component\Core\Util\Base64UrlSafe; |
| 10 | +use Jose\Component\Core\Util\Ecc\NistCurve; |
8 | 11 | use Jose\Component\KeyManagement\Analyzer\AlgorithmAnalyzer; |
9 | 12 | use Jose\Component\KeyManagement\Analyzer\ES256KeyAnalyzer; |
10 | 13 | use Jose\Component\KeyManagement\Analyzer\ES384KeyAnalyzer; |
|
14 | 17 | use Jose\Component\KeyManagement\Analyzer\HS512KeyAnalyzer; |
15 | 18 | use Jose\Component\KeyManagement\Analyzer\KeyAnalyzerManager; |
16 | 19 | use Jose\Component\KeyManagement\Analyzer\KeyIdentifierAnalyzer; |
| 20 | +use Jose\Component\KeyManagement\Analyzer\Message; |
17 | 21 | use Jose\Component\KeyManagement\Analyzer\NoneAnalyzer; |
18 | 22 | use Jose\Component\KeyManagement\Analyzer\OctAnalyzer; |
19 | 23 | use Jose\Component\KeyManagement\Analyzer\RsaAnalyzer; |
20 | 24 | use Jose\Component\KeyManagement\Analyzer\UsageAnalyzer; |
21 | 25 | use Jose\Component\KeyManagement\Analyzer\ZxcvbnKeyAnalyzer; |
22 | 26 | use Jose\Component\KeyManagement\JWKFactory; |
| 27 | +use PHPUnit\Framework\Attributes\DataProvider; |
23 | 28 | use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; |
24 | 29 | use PHPUnit\Framework\Attributes\Test; |
25 | 30 | use PHPUnit\Framework\TestCase; |
| 31 | +use function in_array; |
| 32 | +use const STR_PAD_LEFT; |
26 | 33 |
|
27 | 34 | /** |
28 | 35 | * @internal |
@@ -74,6 +81,130 @@ public function theRsaKeyHasALowExponent(): void |
74 | 81 | static::fail('The low exponent should be catched'); |
75 | 82 | } |
76 | 83 |
|
| 84 | + #[Test] |
| 85 | + public function theRsaKeyWithLeadingZeroOctetsIsAnalyzedByItsValue(): void |
| 86 | + { |
| 87 | + $key = new JWK([ |
| 88 | + 'kty' => 'RSA', |
| 89 | + 'n' => Base64UrlSafe::encodeUnpadded(str_repeat("\0", 255) . "\x0f"), |
| 90 | + 'e' => Base64UrlSafe::encodeUnpadded("\0\0\0\1"), |
| 91 | + ]); |
| 92 | + $messages = $this->getKeyAnalyzer() |
| 93 | + ->analyze($key); |
| 94 | + |
| 95 | + $texts = array_map(static fn (Message $message): string => $message->getMessage(), $messages->all()); |
| 96 | + static::assertContains('The key length is less than 2048 bits.', $texts); |
| 97 | + static::assertContains('The exponent is too low. It should be at least 65537.', $texts); |
| 98 | + } |
| 99 | + |
| 100 | + #[Test] |
| 101 | + public function theEcKeyWithACoordinateAboveTheFieldPrimeIsRejected(): void |
| 102 | + { |
| 103 | + $key = JWKFactory::createECKey('P-521'); |
| 104 | + $prime = NistCurve::curve521()->getPrime(); |
| 105 | + $y = BigInteger::fromBase(bin2hex(Base64UrlSafe::decodeNoPadding($key->get('y'))), 16)->plus($prime); |
| 106 | + $y = str_pad(hex2bin(str_pad($y->toBase(16), 132, '0', STR_PAD_LEFT)), 66, "\0", STR_PAD_LEFT); |
| 107 | + $key = new JWK([ |
| 108 | + 'kty' => 'EC', |
| 109 | + 'crv' => 'P-521', |
| 110 | + 'x' => $key->get('x'), |
| 111 | + 'y' => Base64UrlSafe::encodeUnpadded($y), |
| 112 | + ]); |
| 113 | + $messages = $this->getKeyAnalyzer() |
| 114 | + ->analyze($key); |
| 115 | + |
| 116 | + $texts = array_map(static fn (Message $message): string => $message->getMessage(), $messages->all()); |
| 117 | + static::assertContains( |
| 118 | + 'Invalid key. The components "x" and "y" shall be lower than the field prime.', |
| 119 | + $texts |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + #[Test] |
| 124 | + public function theEcKeyWithCoordinatesOfTheWrongSizeIsNotAnalyzedFurther(): void |
| 125 | + { |
| 126 | + $key = new JWK([ |
| 127 | + 'kty' => 'EC', |
| 128 | + 'crv' => 'P-256', |
| 129 | + 'x' => Base64UrlSafe::encodeUnpadded(str_repeat("\xff", 33)), |
| 130 | + 'y' => Base64UrlSafe::encodeUnpadded(str_repeat("\xff", 33)), |
| 131 | + ]); |
| 132 | + $messages = $this->getKeyAnalyzer() |
| 133 | + ->analyze($key); |
| 134 | + |
| 135 | + $texts = array_map(static fn (Message $message): string => $message->getMessage(), $messages->all()); |
| 136 | + static::assertContains('Invalid key. The components "x" and "y" size shall be 256 bits.', $texts); |
| 137 | + static::assertNotContains( |
| 138 | + 'Invalid key. The components "x" and "y" shall be lower than the field prime.', |
| 139 | + $texts |
| 140 | + ); |
| 141 | + static::assertNotContains('Invalid key. The point is not on the curve.', $texts); |
| 142 | + } |
| 143 | + |
| 144 | + #[Test] |
| 145 | + #[DataProvider('rsaModuli')] |
| 146 | + public function theRsaKeyLengthIsTheBitLengthOfTheModulus(string $n, bool $tooShort): void |
| 147 | + { |
| 148 | + $key = new JWK([ |
| 149 | + 'kty' => 'RSA', |
| 150 | + 'n' => Base64UrlSafe::encodeUnpadded($n), |
| 151 | + 'e' => 'AQAB', |
| 152 | + ]); |
| 153 | + $messages = $this->getKeyAnalyzer() |
| 154 | + ->analyze($key); |
| 155 | + |
| 156 | + $texts = array_map(static fn (Message $message): string => $message->getMessage(), $messages->all()); |
| 157 | + static::assertSame($tooShort, in_array('The key length is less than 2048 bits.', $texts, true)); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * @return iterable<string, array{string, bool}> |
| 162 | + */ |
| 163 | + public static function rsaModuli(): iterable |
| 164 | + { |
| 165 | + yield '2048 bits' => ["\x80" . str_repeat("\xff", 255), false]; |
| 166 | + yield '2048 bits with a leading zero octet' => ["\0\x80" . str_repeat("\xff", 255), false]; |
| 167 | + yield '2049 bits' => ["\x01" . str_repeat("\xff", 256), false]; |
| 168 | + yield '2047 bits on 256 octets' => ["\x7f" . str_repeat("\xff", 255), true]; |
| 169 | + yield '2041 bits on 256 octets' => ["\x01" . str_repeat("\xff", 255), true]; |
| 170 | + yield '2040 bits' => [str_repeat("\xff", 255), true]; |
| 171 | + } |
| 172 | + |
| 173 | + #[Test] |
| 174 | + #[DataProvider('rsaExponents')] |
| 175 | + public function theRsaExponentIsComparedByItsValue(string $e, bool $tooLow): void |
| 176 | + { |
| 177 | + $key = new JWK([ |
| 178 | + 'kty' => 'RSA', |
| 179 | + 'n' => Base64UrlSafe::encodeUnpadded("\x80" . str_repeat("\xff", 255)), |
| 180 | + 'e' => Base64UrlSafe::encodeUnpadded($e), |
| 181 | + ]); |
| 182 | + $messages = $this->getKeyAnalyzer() |
| 183 | + ->analyze($key); |
| 184 | + |
| 185 | + $texts = array_map(static fn (Message $message): string => $message->getMessage(), $messages->all()); |
| 186 | + static::assertSame( |
| 187 | + $tooLow, |
| 188 | + in_array('The exponent is too low. It should be at least 65537.', $texts, true) |
| 189 | + ); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * @return iterable<string, array{string, bool}> |
| 194 | + */ |
| 195 | + public static function rsaExponents(): iterable |
| 196 | + { |
| 197 | + yield '65537' => ["\x01\x00\x01", false]; |
| 198 | + yield '65537 with leading zero octets' => ["\0\0\x01\x00\x01", false]; |
| 199 | + yield '65536' => ["\x01\x00\x00", true]; |
| 200 | + yield '3' => ["\x03", true]; |
| 201 | + yield 'zero' => ["\0", true]; |
| 202 | + yield '2^24 - 1' => ["\xff\xff\xff", false]; |
| 203 | + yield '2^24' => ["\x01\x00\x00\x00", false]; |
| 204 | + yield '2^32 + 1' => ["\x01\x00\x00\x00\x01", false]; |
| 205 | + yield '2^32 - 1' => ["\xff\xff\xff\xff", false]; |
| 206 | + } |
| 207 | + |
77 | 208 | #[Test] |
78 | 209 | public function iCanAnalyzeAnOctKeyAndGetMessages(): void |
79 | 210 | { |
|
0 commit comments