1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JWX\JWK\Parameter; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Implements 'Curve' parameter. |
8
|
|
|
* |
9
|
|
|
* @link https://tools.ietf.org/html/rfc7518#section-6.2.1.1 |
10
|
|
|
*/ |
11
|
|
|
class CurveParameter extends RegisteredJWKParameter |
12
|
|
|
{ |
13
|
|
|
const CURVE_P256 = "P-256"; |
14
|
|
|
const CURVE_P384 = "P-384"; |
15
|
|
|
const CURVE_P521 = "P-521"; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Mapping from curve OID to curve name. |
19
|
|
|
* |
20
|
|
|
* @internal |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
const MAP_OID_TO_CURVE = array( |
25
|
|
|
/* @formatter:off */ |
26
|
|
|
"1.2.840.10045.3.1.7" => self::CURVE_P256, |
27
|
|
|
"1.3.132.0.34" => self::CURVE_P384, |
28
|
|
|
"1.3.132.0.35" => self::CURVE_P521 |
29
|
|
|
/* @formatter:on */ |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Mapping from curve name to bit size. |
34
|
|
|
* |
35
|
|
|
* @internal |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
const MAP_CURVE_TO_SIZE = array( |
40
|
|
|
/* @formatter:off */ |
41
|
|
|
self::CURVE_P256 => 256, |
42
|
|
|
self::CURVE_P384 => 384, |
43
|
|
|
self::CURVE_P521 => 521 |
44
|
|
|
/* @formatter:on */ |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Constructor |
49
|
|
|
* |
50
|
|
|
* @param string $curve Curve name |
51
|
|
|
*/ |
52
|
14 |
|
public function __construct($curve) { |
53
|
14 |
|
parent::__construct(self::PARAM_CURVE, $curve); |
54
|
14 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Initialize from curve OID. |
58
|
|
|
* |
59
|
|
|
* @param string $oid Object identifier in dotted format |
60
|
|
|
* @throws \UnexpectedValueException If the curve is not supported |
61
|
|
|
* @return self |
62
|
|
|
*/ |
63
|
5 |
View Code Duplication |
public static function fromOID($oid) { |
|
|
|
|
64
|
5 |
|
if (!array_key_exists($oid, self::MAP_OID_TO_CURVE)) { |
65
|
1 |
|
throw new \UnexpectedValueException("OID $oid not supported."); |
66
|
|
|
} |
67
|
4 |
|
$curve = self::MAP_OID_TO_CURVE[$oid]; |
68
|
4 |
|
return new self($curve); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get key size in bits for the curve. |
73
|
|
|
* |
74
|
|
|
* @throws \UnexpectedValueException |
75
|
|
|
* @return int |
76
|
|
|
*/ |
77
|
12 |
|
public function keySizeBits() { |
78
|
12 |
|
if (!array_key_exists($this->_value, self::MAP_CURVE_TO_SIZE)) { |
79
|
1 |
|
throw new \UnexpectedValueException( |
80
|
1 |
|
"Curve " . $this->_value . " not supported."); |
81
|
|
|
} |
82
|
11 |
|
return self::MAP_CURVE_TO_SIZE[$this->_value]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the curve OID by curve name. |
87
|
|
|
* |
88
|
|
|
* @param string $name Curve parameter name |
89
|
|
|
* @throws \UnexpectedValueException If the curve is not supported |
90
|
|
|
* @return string OID in dotted format |
91
|
|
|
*/ |
92
|
12 |
|
public static function nameToOID($name) { |
93
|
12 |
|
static $reverseMap; |
94
|
12 |
|
if (!isset($reverseMap)) { |
95
|
1 |
|
$reverseMap = array_flip(self::MAP_OID_TO_CURVE); |
96
|
1 |
|
} |
97
|
12 |
|
if (!isset($reverseMap[$name])) { |
98
|
1 |
|
throw new \UnexpectedValueException("Curve $name not supported."); |
99
|
|
|
} |
100
|
11 |
|
return $reverseMap[$name]; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.