1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JWX\JWK; |
4
|
|
|
|
5
|
|
|
use JWX\JWK\Parameter\AlgorithmParameter; |
6
|
|
|
use JWX\JWK\Parameter\CurveParameter; |
7
|
|
|
use JWX\JWK\Parameter\ECCPrivateKeyParameter; |
8
|
|
|
use JWX\JWK\Parameter\ExponentParameter; |
9
|
|
|
use JWX\JWK\Parameter\FirstCRTCoefficientParameter; |
10
|
|
|
use JWX\JWK\Parameter\FirstFactorCRTExponentParameter; |
11
|
|
|
use JWX\JWK\Parameter\FirstPrimeFactorParameter; |
12
|
|
|
use JWX\JWK\Parameter\JWKParameter; |
13
|
|
|
use JWX\JWK\Parameter\KeyIDParameter; |
14
|
|
|
use JWX\JWK\Parameter\KeyOperationsParameter; |
15
|
|
|
use JWX\JWK\Parameter\KeyTypeParameter; |
16
|
|
|
use JWX\JWK\Parameter\KeyValueParameter; |
17
|
|
|
use JWX\JWK\Parameter\ModulusParameter; |
18
|
|
|
use JWX\JWK\Parameter\OtherPrimesInfoParameter; |
19
|
|
|
use JWX\JWK\Parameter\PrivateExponentParameter; |
20
|
|
|
use JWX\JWK\Parameter\PublicKeyUseParameter; |
21
|
|
|
use JWX\JWK\Parameter\RegisteredJWKParameter; |
22
|
|
|
use JWX\JWK\Parameter\SecondFactorCRTExponentParameter; |
23
|
|
|
use JWX\JWK\Parameter\SecondPrimeFactorParameter; |
24
|
|
|
use JWX\JWK\Parameter\XCoordinateParameter; |
25
|
|
|
use JWX\JWK\Parameter\YCoordinateParameter; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Trait for JWK to provide parameter accessor methods for typed return values. |
30
|
|
|
*/ |
31
|
|
|
trait TypedJWK |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Whether parameters are present. |
35
|
|
|
* |
36
|
|
|
* @param string ...$names Parameter names |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
|
|
abstract public function has(...$names); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get a parameter. |
43
|
|
|
* |
44
|
|
|
* @param string $name Parameter name |
45
|
|
|
* @return JWKParameter |
46
|
|
|
*/ |
47
|
|
|
abstract public function get($name); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Check whether the algorithm parameter is present. |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
22 |
|
public function hasAlgorithmParameter() { |
55
|
22 |
|
return $this->has(RegisteredJWKParameter::P_ALG); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the algorithm parameter. |
60
|
|
|
* |
61
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
62
|
|
|
* @throws \LogicException If the parameter is not present |
63
|
|
|
* @return AlgorithmParameter |
64
|
|
|
*/ |
65
|
19 |
|
public function algorithmParameter() { |
66
|
19 |
|
return self::_checkType($this->get(RegisteredJWKParameter::P_ALG), |
67
|
19 |
|
AlgorithmParameter::class); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Check whether the curve parameter is present. |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
1 |
|
public function hasCurveParameter() { |
76
|
1 |
|
return $this->has(RegisteredJWKParameter::P_CRV); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the curve parameter. |
81
|
|
|
* |
82
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
83
|
|
|
* @throws \LogicException If the parameter is not present |
84
|
|
|
* @return CurveParameter |
85
|
|
|
*/ |
86
|
1 |
|
public function curveParameter() { |
87
|
1 |
|
return self::_checkType($this->get(RegisteredJWKParameter::P_CRV), |
88
|
1 |
|
CurveParameter::class); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Check whether the ECC private key parameter is present. |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
1 |
|
public function hasECCPrivateKeyParameter() { |
97
|
1 |
|
return $this->has(RegisteredJWKParameter::P_ECC_D); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the ECC private key parameter. |
102
|
|
|
* |
103
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
104
|
|
|
* @throws \LogicException If the parameter is not present |
105
|
|
|
* @return ECCPrivateKeyParameter |
106
|
|
|
*/ |
107
|
1 |
|
public function ECCPrivateKeyParameter() { |
108
|
1 |
|
return self::_checkType($this->get(RegisteredJWKParameter::P_ECC_D), |
109
|
1 |
|
ECCPrivateKeyParameter::class); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Check whether the exponent parameter is present. |
114
|
|
|
* |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
1 |
|
public function hasExponentParameter() { |
118
|
1 |
|
return $this->has(RegisteredJWKParameter::P_E); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the exponent parameter. |
123
|
|
|
* |
124
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
125
|
|
|
* @throws \LogicException If the parameter is not present |
126
|
|
|
* @return ExponentParameter |
127
|
|
|
*/ |
128
|
41 |
|
public function exponentParameter() { |
129
|
41 |
|
return self::_checkType($this->get(RegisteredJWKParameter::P_E), |
130
|
41 |
|
ExponentParameter::class); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Check whether the first CRT coefficient parameter is present. |
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
1 |
|
public function hasFirstCRTCoefficientParameter() { |
139
|
1 |
|
return $this->has(RegisteredJWKParameter::P_QI); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get the first CRT coefficient parameter. |
144
|
|
|
* |
145
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
146
|
|
|
* @throws \LogicException If the parameter is not present |
147
|
|
|
* @return FirstCRTCoefficientParameter |
148
|
|
|
*/ |
149
|
17 |
|
public function firstCRTCoefficientParameter() { |
150
|
17 |
|
return self::_checkType($this->get(RegisteredJWKParameter::P_QI), |
151
|
17 |
|
FirstCRTCoefficientParameter::class); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Check whether the first factor CRT exponent parameter is present. |
156
|
|
|
* |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
1 |
|
public function hasFirstFactorCRTExponentParameter() { |
160
|
1 |
|
return $this->has(RegisteredJWKParameter::P_DP); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get the first factor CRT exponent parameter. |
165
|
|
|
* |
166
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
167
|
|
|
* @throws \LogicException If the parameter is not present |
168
|
|
|
* @return FirstFactorCRTExponentParameter |
169
|
|
|
*/ |
170
|
17 |
|
public function firstFactorCRTExponentParameter() { |
171
|
17 |
|
return self::_checkType($this->get(RegisteredJWKParameter::P_DP), |
172
|
17 |
|
FirstFactorCRTExponentParameter::class); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Check whether the first prime factor parameter is present. |
177
|
|
|
* |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
1 |
|
public function hasFirstPrimeFactorParameter() { |
181
|
1 |
|
return $this->has(RegisteredJWKParameter::P_P); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get the first prime factor parameter. |
186
|
|
|
* |
187
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
188
|
|
|
* @throws \LogicException If the parameter is not present |
189
|
|
|
* @return FirstPrimeFactorParameter |
190
|
|
|
*/ |
191
|
17 |
|
public function firstPrimeFactorParameter() { |
192
|
17 |
|
return self::_checkType($this->get(RegisteredJWKParameter::P_P), |
193
|
17 |
|
FirstPrimeFactorParameter::class); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Check whether the key ID parameter is present. |
198
|
|
|
* |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
1 |
|
public function hasKeyIDParameter() { |
202
|
1 |
|
return $this->has(RegisteredJWKParameter::P_KID); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get the key ID parameter. |
207
|
|
|
* |
208
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
209
|
|
|
* @throws \LogicException If the parameter is not present |
210
|
|
|
* @return KeyIDParameter |
211
|
|
|
*/ |
212
|
1 |
|
public function keyIDParameter() { |
213
|
1 |
|
return self::_checkType($this->get(RegisteredJWKParameter::P_KID), |
214
|
1 |
|
KeyIDParameter::class); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Check whether the key operations parameter is present. |
219
|
|
|
* |
220
|
|
|
* @return bool |
221
|
|
|
*/ |
222
|
1 |
|
public function hasKeyOperationsParameter() { |
223
|
1 |
|
return $this->has(RegisteredJWKParameter::P_KEY_OPS); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Get the key operations parameter. |
228
|
|
|
* |
229
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
230
|
|
|
* @throws \LogicException If the parameter is not present |
231
|
|
|
* @return KeyOperationsParameter |
232
|
|
|
*/ |
233
|
1 |
|
public function keyOperationsParameter() { |
234
|
1 |
|
return self::_checkType($this->get(RegisteredJWKParameter::P_KEY_OPS), |
235
|
1 |
|
KeyOperationsParameter::class); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Check whether the key type parameter is present. |
240
|
|
|
* |
241
|
|
|
* @return bool |
242
|
|
|
*/ |
243
|
1 |
|
public function hasKeyTypeParameter() { |
244
|
1 |
|
return $this->has(RegisteredJWKParameter::P_KTY); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Get the key type parameter. |
249
|
|
|
* |
250
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
251
|
|
|
* @throws \LogicException If the parameter is not present |
252
|
|
|
* @return KeyTypeParameter |
253
|
|
|
*/ |
254
|
78 |
|
public function keyTypeParameter() { |
255
|
78 |
|
return self::_checkType($this->get(RegisteredJWKParameter::P_KTY), |
256
|
78 |
|
KeyTypeParameter::class); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Check whether the key value parameter is present. |
261
|
|
|
* |
262
|
|
|
* @return bool |
263
|
|
|
*/ |
264
|
1 |
|
public function hasKeyValueParameter() { |
265
|
1 |
|
return $this->has(RegisteredJWKParameter::P_K); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Get the key value parameter. |
270
|
|
|
* |
271
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
272
|
|
|
* @throws \LogicException If the parameter is not present |
273
|
|
|
* @return KeyValueParameter |
274
|
|
|
*/ |
275
|
19 |
|
public function keyValueParameter() { |
276
|
19 |
|
return self::_checkType($this->get(RegisteredJWKParameter::P_K), |
277
|
19 |
|
KeyValueParameter::class); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Check whether the modulus parameter is present. |
282
|
|
|
* |
283
|
|
|
* @return bool |
284
|
|
|
*/ |
285
|
1 |
|
public function hasModulusParameter() { |
286
|
1 |
|
return $this->has(RegisteredJWKParameter::P_N); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Get the modulus parameter. |
291
|
|
|
* |
292
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
293
|
|
|
* @throws \LogicException If the parameter is not present |
294
|
|
|
* @return ModulusParameter |
295
|
|
|
*/ |
296
|
41 |
|
public function modulusParameter() { |
297
|
41 |
|
return self::_checkType($this->get(RegisteredJWKParameter::P_N), |
298
|
41 |
|
ModulusParameter::class); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Check whether the other primes info parameter is present. |
303
|
|
|
* |
304
|
|
|
* @return bool |
305
|
|
|
*/ |
306
|
1 |
|
public function hasOtherPrimesInfoParameter() { |
307
|
1 |
|
return $this->has(RegisteredJWKParameter::P_OTH); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Get the other primes info parameter. |
312
|
|
|
* |
313
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
314
|
|
|
* @throws \LogicException If the parameter is not present |
315
|
|
|
* @return OtherPrimesInfoParameter |
316
|
|
|
*/ |
317
|
1 |
|
public function otherPrimesInfoParameter() { |
318
|
1 |
|
return self::_checkType($this->get(RegisteredJWKParameter::P_OTH), |
319
|
1 |
|
OtherPrimesInfoParameter::class); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Check whether the private exponent parameter is present. |
324
|
|
|
* |
325
|
|
|
* @return bool |
326
|
|
|
*/ |
327
|
1 |
|
public function hasPrivateExponentParameter() { |
328
|
1 |
|
return $this->has(RegisteredJWKParameter::P_RSA_D); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Get the private exponent parameter. |
333
|
|
|
* |
334
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
335
|
|
|
* @throws \LogicException If the parameter is not present |
336
|
|
|
* @return PrivateExponentParameter |
337
|
|
|
*/ |
338
|
17 |
|
public function privateExponentParameter() { |
339
|
17 |
|
return self::_checkType($this->get(RegisteredJWKParameter::P_RSA_D), |
340
|
17 |
|
PrivateExponentParameter::class); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Check whether the public key use parameter is present. |
345
|
|
|
* |
346
|
|
|
* @return bool |
347
|
|
|
*/ |
348
|
1 |
|
public function hasPublicKeyUseParameter() { |
349
|
1 |
|
return $this->has(RegisteredJWKParameter::P_USE); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Get the public key use parameter. |
354
|
|
|
* |
355
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
356
|
|
|
* @throws \LogicException If the parameter is not present |
357
|
|
|
* @return PublicKeyUseParameter |
358
|
|
|
*/ |
359
|
1 |
|
public function publicKeyUseParameter() { |
360
|
1 |
|
return self::_checkType($this->get(RegisteredJWKParameter::P_USE), |
361
|
1 |
|
PublicKeyUseParameter::class); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Check whether the second factor CRT exponent parameter is present. |
366
|
|
|
* |
367
|
|
|
* @return bool |
368
|
|
|
*/ |
369
|
1 |
|
public function hasSecondFactorCRTExponentParameter() { |
370
|
1 |
|
return $this->has(RegisteredJWKParameter::P_DQ); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Get the second factor CRT exponent parameter. |
375
|
|
|
* |
376
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
377
|
|
|
* @throws \LogicException If the parameter is not present |
378
|
|
|
* @return SecondFactorCRTExponentParameter |
379
|
|
|
*/ |
380
|
17 |
|
public function secondFactorCRTExponentParameter() { |
381
|
17 |
|
return self::_checkType($this->get(RegisteredJWKParameter::P_DQ), |
382
|
17 |
|
SecondFactorCRTExponentParameter::class); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Check whether the second prime factor parameter is present. |
387
|
|
|
* |
388
|
|
|
* @return bool |
389
|
|
|
*/ |
390
|
1 |
|
public function hasSecondPrimeFactorParameter() { |
391
|
1 |
|
return $this->has(RegisteredJWKParameter::P_Q); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Get the second prime factor parameter. |
396
|
|
|
* |
397
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
398
|
|
|
* @throws \LogicException If the parameter is not present |
399
|
|
|
* @return SecondPrimeFactorParameter |
400
|
|
|
*/ |
401
|
17 |
|
public function secondPrimeFactorParameter() { |
402
|
17 |
|
return self::_checkType($this->get(RegisteredJWKParameter::P_Q), |
403
|
17 |
|
SecondPrimeFactorParameter::class); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Check whether the X coordinate parameter is present. |
408
|
|
|
* |
409
|
|
|
* @return bool |
410
|
|
|
*/ |
411
|
1 |
|
public function hasXCoordinateParameter() { |
412
|
1 |
|
return $this->has(RegisteredJWKParameter::P_X); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* Get the X coordinate parameter. |
417
|
|
|
* |
418
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
419
|
|
|
* @throws \LogicException If the parameter is not present |
420
|
|
|
* @return XCoordinateParameter |
421
|
|
|
*/ |
422
|
1 |
|
public function XCoordinateParameter() { |
423
|
1 |
|
return self::_checkType($this->get(RegisteredJWKParameter::P_X), |
424
|
1 |
|
XCoordinateParameter::class); |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Check whether the Y coordinate parameter is present. |
429
|
|
|
* |
430
|
|
|
* @return bool |
431
|
|
|
*/ |
432
|
1 |
|
public function hasYCoordinateParameter() { |
433
|
1 |
|
return $this->has(RegisteredJWKParameter::P_Y); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Get the Y coordinate parameter. |
438
|
|
|
* |
439
|
|
|
* @throws \UnexpectedValueException If the parameter has a wrong class |
440
|
|
|
* @throws \LogicException If the parameter is not present |
441
|
|
|
* @return YCoordinateParameter |
442
|
|
|
*/ |
443
|
1 |
|
public function YCoordinateParameter() { |
444
|
1 |
|
return self::_checkType($this->get(RegisteredJWKParameter::P_Y), |
445
|
1 |
|
YCoordinateParameter::class); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* Check that the parameter is an instance of the given class. |
450
|
|
|
* |
451
|
|
|
* @param JWKParameter $param Parameter |
452
|
|
|
* @param string $cls Class name |
453
|
|
|
* @throws \UnexpectedValueException |
454
|
|
|
* @return JWKParameter |
455
|
|
|
*/ |
456
|
111 |
|
private static function _checkType(JWKParameter $param, $cls) { |
457
|
111 |
|
if (!$param instanceof $cls) { |
458
|
1 |
|
throw new \UnexpectedValueException( |
459
|
1 |
|
"$cls expected, got " . get_class($param)); |
460
|
|
|
} |
461
|
110 |
|
return $param; |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
|