1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\JWX\JWK\Parameter; |
6
|
|
|
|
7
|
|
|
use Sop\JWX\Parameter\Parameter; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Represents a single JWK parameter. |
11
|
|
|
* |
12
|
|
|
* @see https://tools.ietf.org/html/rfc7517#section-4 |
13
|
|
|
* @see http://www.iana.org/assignments/jose/jose.xhtml#web-key-parameters |
14
|
|
|
*/ |
15
|
|
|
class JWKParameter extends Parameter |
16
|
|
|
{ |
17
|
|
|
// registered parameter names |
18
|
|
|
public const PARAM_KEY_TYPE = 'kty'; |
19
|
|
|
public const PARAM_PUBLIC_KEY_USE = 'use'; |
20
|
|
|
public const PARAM_KEY_OPERATIONS = 'key_ops'; |
21
|
|
|
public const PARAM_ALGORITHM = 'alg'; |
22
|
|
|
public const PARAM_KEY_ID = 'kid'; |
23
|
|
|
public const PARAM_X509_URL = 'x5u'; |
24
|
|
|
public const PARAM_X509_CERTIFICATE_CHAIN = 'x5c'; |
25
|
|
|
public const PARAM_X509_CERTIFICATE_SHA1_THUMBPRINT = 'x5t'; |
26
|
|
|
public const PARAM_X509_CERTIFICATE_SHA256_THUMBPRINT = 'x5t#S256'; |
27
|
|
|
public const PARAM_CURVE = 'crv'; |
28
|
|
|
public const PARAM_X_COORDINATE = 'x'; |
29
|
|
|
public const PARAM_Y_COORDINATE = 'y'; |
30
|
|
|
public const PARAM_ECC_PRIVATE_KEY = 'd'; |
31
|
|
|
public const PARAM_MODULUS = 'n'; |
32
|
|
|
public const PARAM_EXPONENT = 'e'; |
33
|
|
|
public const PARAM_PRIVATE_EXPONENT = 'd'; |
34
|
|
|
public const PARAM_FIRST_PRIME_FACTOR = 'p'; |
35
|
|
|
public const PARAM_SECOND_PRIME_FACTOR = 'q'; |
36
|
|
|
public const PARAM_FIRST_FACTOR_CRT_EXPONENT = 'dp'; |
37
|
|
|
public const PARAM_SECOND_FACTOR_CRT_EXPONENT = 'dq'; |
38
|
|
|
public const PARAM_FIRST_CRT_COEFFICIENT = 'qi'; |
39
|
|
|
public const PARAM_OTHER_PRIMES_INFO = 'oth'; |
40
|
|
|
public const PARAM_KEY_VALUE = 'k'; |
41
|
|
|
|
42
|
|
|
// shorthand aliases for parameter names |
43
|
|
|
public const P_KTY = self::PARAM_KEY_TYPE; |
44
|
|
|
public const P_USE = self::PARAM_PUBLIC_KEY_USE; |
45
|
|
|
public const P_KEY_OPS = self::PARAM_KEY_OPERATIONS; |
46
|
|
|
public const P_ALG = self::PARAM_ALGORITHM; |
47
|
|
|
public const P_KID = self::PARAM_KEY_ID; |
48
|
|
|
public const P_X5U = self::PARAM_X509_URL; |
49
|
|
|
public const P_X5C = self::PARAM_X509_CERTIFICATE_CHAIN; |
50
|
|
|
public const P_X5T = self::PARAM_X509_CERTIFICATE_SHA1_THUMBPRINT; |
51
|
|
|
public const P_X5TS256 = self::PARAM_X509_CERTIFICATE_SHA256_THUMBPRINT; |
52
|
|
|
public const P_CRV = self::PARAM_CURVE; |
53
|
|
|
public const P_X = self::PARAM_X_COORDINATE; |
54
|
|
|
public const P_Y = self::PARAM_Y_COORDINATE; |
55
|
|
|
public const P_ECC_D = self::PARAM_ECC_PRIVATE_KEY; |
56
|
|
|
public const P_N = self::PARAM_MODULUS; |
57
|
|
|
public const P_E = self::PARAM_EXPONENT; |
58
|
|
|
public const P_RSA_D = self::PARAM_PRIVATE_EXPONENT; |
59
|
|
|
public const P_P = self::PARAM_FIRST_PRIME_FACTOR; |
60
|
|
|
public const P_Q = self::PARAM_SECOND_PRIME_FACTOR; |
61
|
|
|
public const P_DP = self::PARAM_FIRST_FACTOR_CRT_EXPONENT; |
62
|
|
|
public const P_DQ = self::PARAM_SECOND_FACTOR_CRT_EXPONENT; |
63
|
|
|
public const P_QI = self::PARAM_FIRST_CRT_COEFFICIENT; |
64
|
|
|
public const P_OTH = self::PARAM_OTHER_PRIMES_INFO; |
65
|
|
|
public const P_K = self::PARAM_KEY_VALUE; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Mapping from registered JWK parameter name to class name. |
69
|
|
|
* |
70
|
|
|
* Note that ECC private key and RSA private key cannot be mapped since |
71
|
|
|
* they share the same parameter name 'd'. |
72
|
|
|
* |
73
|
|
|
* @internal |
74
|
|
|
* |
75
|
|
|
* @var array |
76
|
|
|
*/ |
77
|
|
|
public const MAP_NAME_TO_CLASS = [ |
78
|
|
|
self::P_KTY => KeyTypeParameter::class, |
79
|
|
|
self::P_USE => PublicKeyUseParameter::class, |
80
|
|
|
self::P_KEY_OPS => KeyOperationsParameter::class, |
81
|
|
|
self::P_ALG => AlgorithmParameter::class, |
82
|
|
|
self::P_KID => KeyIDParameter::class, |
83
|
|
|
self::P_X5U => X509URLParameter::class, |
84
|
|
|
self::P_X5C => X509CertificateChainParameter::class, |
85
|
|
|
self::P_X5T => X509CertificateSHA1ThumbprintParameter::class, |
86
|
|
|
self::P_X5TS256 => X509CertificateSHA256ThumbprintParameter::class, |
87
|
|
|
self::P_CRV => CurveParameter::class, |
88
|
|
|
self::P_X => XCoordinateParameter::class, |
89
|
|
|
self::P_Y => YCoordinateParameter::class, |
90
|
|
|
self::P_N => ModulusParameter::class, |
91
|
|
|
self::P_E => ExponentParameter::class, |
92
|
|
|
self::P_P => FirstPrimeFactorParameter::class, |
93
|
|
|
self::P_Q => SecondPrimeFactorParameter::class, |
94
|
|
|
self::P_DP => FirstFactorCRTExponentParameter::class, |
95
|
|
|
self::P_DQ => SecondFactorCRTExponentParameter::class, |
96
|
|
|
self::P_QI => FirstCRTCoefficientParameter::class, |
97
|
|
|
self::P_OTH => OtherPrimesInfoParameter::class, |
98
|
|
|
self::P_K => KeyValueParameter::class, |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Constructor. |
103
|
|
|
* |
104
|
|
|
* @param string $name Parameter name |
105
|
|
|
* @param mixed $value Parameter value |
106
|
|
|
*/ |
107
|
150 |
|
public function __construct(string $name, $value) |
108
|
|
|
{ |
109
|
150 |
|
$this->_name = $name; |
110
|
150 |
|
$this->_value = $value; |
111
|
150 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Initialize from a name and a value. |
115
|
|
|
* |
116
|
|
|
* Returns a parameter specific object if one is implemented. |
117
|
|
|
* |
118
|
|
|
* @param string $name Parameter name |
119
|
|
|
* @param mixed $value Parameter value |
120
|
|
|
*/ |
121
|
54 |
|
public static function fromNameAndValue(string $name, $value): self |
122
|
|
|
{ |
123
|
54 |
|
if (array_key_exists($name, self::MAP_NAME_TO_CLASS)) { |
124
|
52 |
|
$cls = self::MAP_NAME_TO_CLASS[$name]; |
125
|
52 |
|
return $cls::fromJSONValue($value); |
126
|
|
|
} |
127
|
12 |
|
return new self($name, $value); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Initialize from a JSON value. |
132
|
|
|
* |
133
|
|
|
* @param mixed $value |
134
|
|
|
* |
135
|
|
|
* @return JWKParameter |
136
|
|
|
*/ |
137
|
1 |
|
public static function fromJSONValue($value): Parameter |
138
|
|
|
{ |
139
|
1 |
|
throw new \BadMethodCallException( |
140
|
1 |
|
__FUNCTION__ . ' must be implemented in a derived class.'); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|