Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/KeyManagement/Analyzer/ESKeyAnalyzer.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jose\Component\KeyManagement\Analyzer;
6
7 use Brick\Math\BigInteger;
8 use Jose\Component\Core\JWK;
9 use Jose\Component\Core\Util\Base64UrlSafe;
10 use Jose\Component\Core\Util\Ecc\Curve;
11 use function is_string;
12
13 abstract class ESKeyAnalyzer implements KeyAnalyzer
14 {
15 public function analyze(JWK $jwk, MessageBag $bag): void
16 {
17 if ($jwk->get('kty') !== 'EC') {
18 return;
19 }
20 if (! $jwk->has('crv')) {
21 $bag->add(Message::high('Invalid key. The components "crv" is missing.'));
22
23 return;
24 }
25 if ($jwk->get('crv') !== $this->getCurveName()) {
26 return;
27 }
28 $x = $jwk->get('x');
29 if (! is_string($x)) {
30 $bag->add(Message::high('Invalid key. The components "x" shall be a string.'));
31
32 return;
33 }
34 $x = Base64UrlSafe::decodeNoPadding($x);
35 $xLength = 8 * mb_strlen($x, '8bit');
36 $y = $jwk->get('y');
37 if (! is_string($y)) {
38 $bag->add(Message::high('Invalid key. The components "y" shall be a string.'));
39
40 return;
41 }
42 $y = Base64UrlSafe::decodeNoPadding($y);
43 $yLength = 8 * mb_strlen($y, '8bit');
44 if ($yLength !== $xLength || $yLength !== $this->getKeySize()) {
45 $bag->add(
46 Message::high(sprintf(
47 'Invalid key. The components "x" and "y" size shall be %d bits.',
48 $this->getKeySize()
49 ))
50 );
51 }
52 $xBI = BigInteger::fromBase(bin2hex($x), 16);
53 $yBI = BigInteger::fromBase(bin2hex($y), 16);
54 if (! $this->getCurve()->contains($xBI, $yBI)) {
55 $bag->add(Message::high('Invalid key. The point is not on the curve.'));
56 }
57 }
58
59 abstract protected function getAlgorithmName(): string;
60
61 abstract protected function getCurveName(): string;
62
63 abstract protected function getCurve(): Curve;
64
65 abstract protected function getKeySize(): int;
66 }