1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2019 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Jose\Component\KeyManagement\Analyzer; |
15
|
|
|
|
16
|
|
|
use Base64Url\Base64Url; |
17
|
|
|
use Jose\Component\Core\JWK; |
18
|
|
|
use Jose\Component\Core\Util\Ecc\NistCurve; |
19
|
|
|
use RuntimeException; |
20
|
|
|
|
21
|
|
|
final class ES384KeyAnalyzer implements KeyAnalyzer |
22
|
|
|
{ |
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
if (!class_exists(NistCurve::class)) { |
26
|
|
|
throw new RuntimeException('Please install web-token/jwt-util-ecc to use this key analyzer'); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function analyze(JWK $jwk, MessageBag $bag): void |
31
|
|
|
{ |
32
|
|
|
if ('EC' !== $jwk->get('kty')) { |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
if (!$jwk->has('crv')) { |
36
|
|
|
$bag->add(Message::high('Invalid key. The components "crv" is missing.')); |
37
|
|
|
|
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
if ('P-384' !== $jwk->get('crv')) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
$x = Base64Url::decode($jwk->get('x')); |
44
|
|
|
$xLength = 8 * mb_strlen($x, '8bit'); |
45
|
|
|
$y = Base64Url::decode($jwk->get('y')); |
46
|
|
|
$yLength = 8 * mb_strlen($y, '8bit'); |
47
|
|
|
if ($yLength !== $xLength || 384 !== $yLength) { |
48
|
|
|
$bag->add(Message::high('Invalid key. The components "x" and "y" size shall be 384 bits.')); |
49
|
|
|
} |
50
|
|
|
$xGmp = gmp_init(bin2hex($x), 16); |
51
|
|
|
$yGmp = gmp_init(bin2hex($y), 16); |
52
|
|
|
$curve = NistCurve::curve384(); |
53
|
|
|
if (!$curve->contains($xGmp, $yGmp)) { |
54
|
|
|
$bag->add(Message::high('Invalid key. The point is not on the curve.')); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|