1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\JWX\JWK\Parameter; |
6
|
|
|
|
7
|
|
|
use Sop\JWX\Parameter\Feature\StringParameterValue; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Implements 'Curve' parameter. |
11
|
|
|
* |
12
|
|
|
* @see https://tools.ietf.org/html/rfc7518#section-6.2.1.1 |
13
|
|
|
*/ |
14
|
|
|
class CurveParameter extends JWKParameter |
15
|
|
|
{ |
16
|
|
|
use StringParameterValue; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* P-256 Curve. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
const CURVE_P256 = 'P-256'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* P-384 Curve. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
const CURVE_P384 = 'P-384'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* P-521 Curve. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
const CURVE_P521 = 'P-521'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Mapping from curve OID to curve name. |
41
|
|
|
* |
42
|
|
|
* @internal |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
const MAP_OID_TO_CURVE = [ |
47
|
|
|
'1.2.840.10045.3.1.7' => self::CURVE_P256, |
48
|
|
|
'1.3.132.0.34' => self::CURVE_P384, |
49
|
|
|
'1.3.132.0.35' => self::CURVE_P521, |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Mapping from curve name to bit size. |
54
|
|
|
* |
55
|
|
|
* @internal |
56
|
|
|
* |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
const MAP_CURVE_TO_SIZE = [ |
60
|
|
|
self::CURVE_P256 => 256, |
61
|
|
|
self::CURVE_P384 => 384, |
62
|
|
|
self::CURVE_P521 => 521, |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Constructor. |
67
|
|
|
* |
68
|
|
|
* @param string $curve Curve name |
69
|
|
|
*/ |
70
|
17 |
|
public function __construct(string $curve) |
71
|
|
|
{ |
72
|
17 |
|
parent::__construct(self::PARAM_CURVE, $curve); |
|
|
|
|
73
|
17 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Initialize from curve OID. |
77
|
|
|
* |
78
|
|
|
* @param string $oid Object identifier in dotted format |
79
|
|
|
* |
80
|
|
|
* @throws \UnexpectedValueException If the curve is not supported |
81
|
|
|
* |
82
|
|
|
* @return self |
83
|
|
|
*/ |
84
|
7 |
|
public static function fromOID(string $oid): self |
85
|
|
|
{ |
86
|
7 |
|
if (!array_key_exists($oid, self::MAP_OID_TO_CURVE)) { |
87
|
1 |
|
throw new \UnexpectedValueException("OID {$oid} not supported."); |
88
|
|
|
} |
89
|
6 |
|
$curve = self::MAP_OID_TO_CURVE[$oid]; |
90
|
6 |
|
return new self($curve); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get key size in bits for the curve. |
95
|
|
|
* |
96
|
|
|
* @throws \UnexpectedValueException |
97
|
|
|
* |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
11 |
|
public function keySizeBits(): int |
101
|
|
|
{ |
102
|
11 |
|
if (!array_key_exists($this->_value, self::MAP_CURVE_TO_SIZE)) { |
103
|
1 |
|
throw new \UnexpectedValueException( |
104
|
1 |
|
'Curve ' . $this->_value . ' not supported.'); |
105
|
|
|
} |
106
|
10 |
|
return self::MAP_CURVE_TO_SIZE[$this->_value]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the curve OID by curve name. |
111
|
|
|
* |
112
|
|
|
* @param string $name Curve parameter name |
113
|
|
|
* |
114
|
|
|
* @throws \UnexpectedValueException If the curve is not supported |
115
|
|
|
* |
116
|
|
|
* @return string OID in dotted format |
117
|
|
|
*/ |
118
|
12 |
|
public static function nameToOID(string $name): string |
119
|
|
|
{ |
120
|
12 |
|
static $reverseMap; |
121
|
12 |
|
if (!isset($reverseMap)) { |
122
|
1 |
|
$reverseMap = array_flip(self::MAP_OID_TO_CURVE); |
123
|
|
|
} |
124
|
12 |
|
if (!isset($reverseMap[$name])) { |
125
|
1 |
|
throw new \UnexpectedValueException("Curve {$name} not supported."); |
126
|
|
|
} |
127
|
11 |
|
return $reverseMap[$name]; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.