1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\X509\CertificationPath\PathValidation; |
6
|
|
|
|
7
|
|
|
use Sop\ASN1\Element; |
8
|
|
|
use Sop\CryptoTypes\AlgorithmIdentifier\Feature\AlgorithmIdentifierType; |
9
|
|
|
use Sop\CryptoTypes\Asymmetric\PublicKeyInfo; |
10
|
|
|
use Sop\X501\ASN1\Name; |
11
|
|
|
use Sop\X509\Certificate\Certificate; |
12
|
|
|
use Sop\X509\CertificationPath\Policy\PolicyNode; |
13
|
|
|
use Sop\X509\CertificationPath\Policy\PolicyTree; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* State class for the certification path validation process. |
17
|
|
|
* |
18
|
|
|
* @see https://tools.ietf.org/html/rfc5280#section-6.1.1 |
19
|
|
|
* @see https://tools.ietf.org/html/rfc5280#section-6.1.2 |
20
|
|
|
*/ |
21
|
|
|
class ValidatorState |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Length of the certification path (n). |
25
|
|
|
* |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
protected $_pathLength; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Current index in the certification path in the range of 1..n (i). |
32
|
|
|
* |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
protected $_index; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Valid policy tree (valid_policy_tree). |
39
|
|
|
* |
40
|
|
|
* A tree of certificate policies with their optional qualifiers. |
41
|
|
|
* Each of the leaves of the tree represents a valid policy at this stage in |
42
|
|
|
* the certification path validation. |
43
|
|
|
* Once the tree is set to NULL, policy processing ceases. |
44
|
|
|
* |
45
|
|
|
* @var null|PolicyTree |
46
|
|
|
*/ |
47
|
|
|
protected $_validPolicyTree; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Permitted subtrees (permitted_subtrees). |
51
|
|
|
* |
52
|
|
|
* A set of root names for each name type defining a set of subtrees within |
53
|
|
|
* which all subject names in subsequent certificates in the certification |
54
|
|
|
* path must fall. |
55
|
|
|
* |
56
|
|
|
* @var mixed |
57
|
|
|
*/ |
58
|
|
|
protected $_permittedSubtrees; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Excluded subtrees (excluded_subtrees). |
62
|
|
|
* |
63
|
|
|
* A set of root names for each name type defining a set of subtrees within |
64
|
|
|
* which no subject name in subsequent certificates in the certification |
65
|
|
|
* path may fall. |
66
|
|
|
* |
67
|
|
|
* @var mixed |
68
|
|
|
*/ |
69
|
|
|
protected $_excludedSubtrees; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Explicit policy (explicit_policy). |
73
|
|
|
* |
74
|
|
|
* An integer that indicates if a non-NULL valid_policy_tree is required. |
75
|
|
|
* |
76
|
|
|
* @var int |
77
|
|
|
*/ |
78
|
|
|
protected $_explicitPolicy; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Inhibit anyPolicy (inhibit_anyPolicy). |
82
|
|
|
* |
83
|
|
|
* An integer that indicates whether the anyPolicy policy identifier is |
84
|
|
|
* considered a match. |
85
|
|
|
* |
86
|
|
|
* @var int |
87
|
|
|
*/ |
88
|
|
|
protected $_inhibitAnyPolicy; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Policy mapping (policy_mapping). |
92
|
|
|
* |
93
|
|
|
* An integer that indicates if policy mapping is permitted. |
94
|
|
|
* |
95
|
|
|
* @var int |
96
|
|
|
*/ |
97
|
|
|
protected $_policyMapping; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Working public key algorithm (working_public_key_algorithm). |
101
|
|
|
* |
102
|
|
|
* The digital signature algorithm used to verify the signature of a |
103
|
|
|
* certificate. |
104
|
|
|
* |
105
|
|
|
* @var AlgorithmIdentifierType |
106
|
|
|
*/ |
107
|
|
|
protected $_workingPublicKeyAlgorithm; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Working public key (working_public_key). |
111
|
|
|
* |
112
|
|
|
* The public key used to verify the signature of a certificate. |
113
|
|
|
* |
114
|
|
|
* @var PublicKeyInfo |
115
|
|
|
*/ |
116
|
|
|
protected $_workingPublicKey; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Working public key parameters (working_public_key_parameters). |
120
|
|
|
* |
121
|
|
|
* Parameters associated with the current public key that may be required to |
122
|
|
|
* verify a signature. |
123
|
|
|
* |
124
|
|
|
* @var null|Element |
125
|
|
|
*/ |
126
|
|
|
protected $_workingPublicKeyParameters; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Working issuer name (working_issuer_name). |
130
|
|
|
* |
131
|
|
|
* The issuer distinguished name expected in the next certificate in the |
132
|
|
|
* chain. |
133
|
|
|
* |
134
|
|
|
* @var Name |
135
|
|
|
*/ |
136
|
|
|
protected $_workingIssuerName; |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Maximum certification path length (max_path_length). |
140
|
|
|
* |
141
|
|
|
* @var int |
142
|
|
|
*/ |
143
|
|
|
protected $_maxPathLength; |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Constructor. |
147
|
|
|
*/ |
148
|
47 |
|
protected function __construct() |
149
|
|
|
{ |
150
|
47 |
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Initialize variables according to RFC 5280 6.1.2. |
154
|
|
|
* |
155
|
|
|
* @see https://tools.ietf.org/html/rfc5280#section-6.1.2 |
156
|
|
|
* |
157
|
|
|
* @param PathValidationConfig $config |
158
|
|
|
* @param Certificate $trust_anchor Trust anchor certificate |
159
|
|
|
* @param int $n Number of certificates |
160
|
|
|
* in the certification path |
161
|
|
|
* |
162
|
|
|
* @return self |
163
|
|
|
*/ |
164
|
47 |
|
public static function initialize(PathValidationConfig $config, |
165
|
|
|
Certificate $trust_anchor, int $n): self |
166
|
|
|
{ |
167
|
47 |
|
$state = new self(); |
168
|
47 |
|
$state->_pathLength = $n; |
169
|
47 |
|
$state->_index = 1; |
170
|
47 |
|
$state->_validPolicyTree = new PolicyTree(PolicyNode::anyPolicyNode()); |
171
|
47 |
|
$state->_permittedSubtrees = null; |
172
|
47 |
|
$state->_excludedSubtrees = null; |
173
|
47 |
|
$state->_explicitPolicy = $config->explicitPolicy() ? 0 : $n + 1; |
174
|
47 |
|
$state->_inhibitAnyPolicy = $config->anyPolicyInhibit() ? 0 : $n + 1; |
175
|
47 |
|
$state->_policyMapping = $config->policyMappingInhibit() ? 0 : $n + 1; |
176
|
47 |
|
$state->_workingPublicKeyAlgorithm = $trust_anchor->signatureAlgorithm(); |
177
|
47 |
|
$tbsCert = $trust_anchor->tbsCertificate(); |
178
|
47 |
|
$state->_workingPublicKey = $tbsCert->subjectPublicKeyInfo(); |
179
|
47 |
|
$state->_workingPublicKeyParameters = self::getAlgorithmParameters( |
180
|
47 |
|
$state->_workingPublicKey->algorithmIdentifier()); |
181
|
47 |
|
$state->_workingIssuerName = $tbsCert->issuer(); |
182
|
47 |
|
$state->_maxPathLength = $config->maxLength(); |
183
|
47 |
|
return $state; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get self with current certification path index set. |
188
|
|
|
* |
189
|
|
|
* @param int $index |
190
|
|
|
* |
191
|
|
|
* @return self |
192
|
|
|
*/ |
193
|
44 |
|
public function withIndex(int $index): self |
194
|
|
|
{ |
195
|
44 |
|
$state = clone $this; |
196
|
44 |
|
$state->_index = $index; |
197
|
44 |
|
return $state; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get self with valid_policy_tree. |
202
|
|
|
* |
203
|
|
|
* @param PolicyTree $policy_tree |
204
|
|
|
* |
205
|
|
|
* @return self |
206
|
|
|
*/ |
207
|
14 |
|
public function withValidPolicyTree(PolicyTree $policy_tree): self |
208
|
|
|
{ |
209
|
14 |
|
$state = clone $this; |
210
|
14 |
|
$state->_validPolicyTree = $policy_tree; |
211
|
14 |
|
return $state; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Get self with valid_policy_tree set to null. |
216
|
|
|
* |
217
|
|
|
* @return self |
218
|
|
|
*/ |
219
|
36 |
|
public function withoutValidPolicyTree(): self |
220
|
|
|
{ |
221
|
36 |
|
$state = clone $this; |
222
|
36 |
|
$state->_validPolicyTree = null; |
223
|
36 |
|
return $state; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Get self with explicit_policy. |
228
|
|
|
* |
229
|
|
|
* @param int $num |
230
|
|
|
* |
231
|
|
|
* @return self |
232
|
|
|
*/ |
233
|
30 |
|
public function withExplicitPolicy(int $num): self |
234
|
|
|
{ |
235
|
30 |
|
$state = clone $this; |
236
|
30 |
|
$state->_explicitPolicy = $num; |
237
|
30 |
|
return $state; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Get self with inhibit_anyPolicy. |
242
|
|
|
* |
243
|
|
|
* @param int $num |
244
|
|
|
* |
245
|
|
|
* @return self |
246
|
|
|
*/ |
247
|
23 |
|
public function withInhibitAnyPolicy(int $num): self |
248
|
|
|
{ |
249
|
23 |
|
$state = clone $this; |
250
|
23 |
|
$state->_inhibitAnyPolicy = $num; |
251
|
23 |
|
return $state; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Get self with policy_mapping. |
256
|
|
|
* |
257
|
|
|
* @param int $num |
258
|
|
|
* |
259
|
|
|
* @return self |
260
|
|
|
*/ |
261
|
21 |
|
public function withPolicyMapping(int $num): self |
262
|
|
|
{ |
263
|
21 |
|
$state = clone $this; |
264
|
21 |
|
$state->_policyMapping = $num; |
265
|
21 |
|
return $state; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Get self with working_public_key_algorithm. |
270
|
|
|
* |
271
|
|
|
* @param AlgorithmIdentifierType $algo |
272
|
|
|
* |
273
|
|
|
* @return self |
274
|
|
|
*/ |
275
|
42 |
|
public function withWorkingPublicKeyAlgorithm(AlgorithmIdentifierType $algo): self |
276
|
|
|
{ |
277
|
42 |
|
$state = clone $this; |
278
|
42 |
|
$state->_workingPublicKeyAlgorithm = $algo; |
279
|
42 |
|
return $state; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Get self with working_public_key. |
284
|
|
|
* |
285
|
|
|
* @param PublicKeyInfo $pubkey_info |
286
|
|
|
* |
287
|
|
|
* @return self |
288
|
|
|
*/ |
289
|
42 |
|
public function withWorkingPublicKey(PublicKeyInfo $pubkey_info): self |
290
|
|
|
{ |
291
|
42 |
|
$state = clone $this; |
292
|
42 |
|
$state->_workingPublicKey = $pubkey_info; |
293
|
42 |
|
return $state; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Get self with working_public_key_parameters. |
298
|
|
|
* |
299
|
|
|
* @param null|Element $params |
300
|
|
|
* |
301
|
|
|
* @return self |
302
|
|
|
*/ |
303
|
42 |
|
public function withWorkingPublicKeyParameters(?Element $params = null): self |
304
|
|
|
{ |
305
|
42 |
|
$state = clone $this; |
306
|
42 |
|
$state->_workingPublicKeyParameters = $params; |
307
|
42 |
|
return $state; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Get self with working_issuer_name. |
312
|
|
|
* |
313
|
|
|
* @param Name $issuer |
314
|
|
|
* |
315
|
|
|
* @return self |
316
|
|
|
*/ |
317
|
42 |
|
public function withWorkingIssuerName(Name $issuer): self |
318
|
|
|
{ |
319
|
42 |
|
$state = clone $this; |
320
|
42 |
|
$state->_workingIssuerName = $issuer; |
321
|
42 |
|
return $state; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Get self with max_path_length. |
326
|
|
|
* |
327
|
|
|
* @param int $length |
328
|
|
|
* |
329
|
|
|
* @return self |
330
|
|
|
*/ |
331
|
28 |
|
public function withMaxPathLength(int $length): self |
332
|
|
|
{ |
333
|
28 |
|
$state = clone $this; |
334
|
28 |
|
$state->_maxPathLength = $length; |
335
|
28 |
|
return $state; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Get the certification path length (n). |
340
|
|
|
* |
341
|
|
|
* @return int |
342
|
|
|
*/ |
343
|
1 |
|
public function pathLength(): int |
344
|
|
|
{ |
345
|
1 |
|
return $this->_pathLength; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Get the current index in certification path in the range of 1..n. |
350
|
|
|
* |
351
|
|
|
* @return int |
352
|
|
|
*/ |
353
|
15 |
|
public function index(): int |
354
|
|
|
{ |
355
|
15 |
|
return $this->_index; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Check whether valid_policy_tree is present. |
360
|
|
|
* |
361
|
|
|
* @return bool |
362
|
|
|
*/ |
363
|
35 |
|
public function hasValidPolicyTree(): bool |
364
|
|
|
{ |
365
|
35 |
|
return isset($this->_validPolicyTree); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Get valid_policy_tree. |
370
|
|
|
* |
371
|
|
|
* @throws \LogicException If not set |
372
|
|
|
* |
373
|
|
|
* @return PolicyTree |
374
|
|
|
*/ |
375
|
15 |
|
public function validPolicyTree(): PolicyTree |
376
|
|
|
{ |
377
|
15 |
|
if (!$this->hasValidPolicyTree()) { |
378
|
1 |
|
throw new \LogicException('valid_policy_tree not set.'); |
379
|
|
|
} |
380
|
14 |
|
return $this->_validPolicyTree; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Get permitted_subtrees. |
385
|
|
|
* |
386
|
|
|
* @return mixed |
387
|
|
|
*/ |
388
|
35 |
|
public function permittedSubtrees() |
389
|
|
|
{ |
390
|
35 |
|
return $this->_permittedSubtrees; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Get excluded_subtrees. |
395
|
|
|
* |
396
|
|
|
* @return mixed |
397
|
|
|
*/ |
398
|
35 |
|
public function excludedSubtrees() |
399
|
|
|
{ |
400
|
35 |
|
return $this->_excludedSubtrees; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Get explicit_policy. |
405
|
|
|
* |
406
|
|
|
* @return int |
407
|
|
|
*/ |
408
|
43 |
|
public function explicitPolicy(): int |
409
|
|
|
{ |
410
|
43 |
|
return $this->_explicitPolicy; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* Get inhibit_anyPolicy. |
415
|
|
|
* |
416
|
|
|
* @return int |
417
|
|
|
*/ |
418
|
26 |
|
public function inhibitAnyPolicy(): int |
419
|
|
|
{ |
420
|
26 |
|
return $this->_inhibitAnyPolicy; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* Get policy_mapping. |
425
|
|
|
* |
426
|
|
|
* @return int |
427
|
|
|
*/ |
428
|
23 |
|
public function policyMapping(): int |
429
|
|
|
{ |
430
|
23 |
|
return $this->_policyMapping; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Get working_public_key_algorithm. |
435
|
|
|
* |
436
|
|
|
* @return AlgorithmIdentifierType |
437
|
|
|
*/ |
438
|
1 |
|
public function workingPublicKeyAlgorithm(): AlgorithmIdentifierType |
439
|
|
|
{ |
440
|
1 |
|
return $this->_workingPublicKeyAlgorithm; |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* Get working_public_key. |
445
|
|
|
* |
446
|
|
|
* @return PublicKeyInfo |
447
|
|
|
*/ |
448
|
44 |
|
public function workingPublicKey(): PublicKeyInfo |
449
|
|
|
{ |
450
|
44 |
|
return $this->_workingPublicKey; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Get working_public_key_parameters. |
455
|
|
|
* |
456
|
|
|
* @return null|Element |
457
|
|
|
*/ |
458
|
1 |
|
public function workingPublicKeyParameters(): ?Element |
459
|
|
|
{ |
460
|
1 |
|
return $this->_workingPublicKeyParameters; |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* Get working_issuer_name. |
465
|
|
|
* |
466
|
|
|
* @return Name |
467
|
|
|
*/ |
468
|
43 |
|
public function workingIssuerName(): Name |
469
|
|
|
{ |
470
|
43 |
|
return $this->_workingIssuerName; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* Get maximum certification path length. |
475
|
|
|
* |
476
|
|
|
* @return int |
477
|
|
|
*/ |
478
|
31 |
|
public function maxPathLength(): int |
479
|
|
|
{ |
480
|
31 |
|
return $this->_maxPathLength; |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Check whether processing the final certificate of the certification path. |
485
|
|
|
* |
486
|
|
|
* @return bool |
487
|
|
|
*/ |
488
|
43 |
|
public function isFinal(): bool |
489
|
|
|
{ |
490
|
43 |
|
return $this->_index === $this->_pathLength; |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* Get the path validation result. |
495
|
|
|
* |
496
|
|
|
* @param Certificate[] $certificates Certificates in a certification path |
497
|
|
|
* |
498
|
|
|
* @return PathValidationResult |
499
|
|
|
*/ |
500
|
28 |
|
public function getResult(array $certificates): PathValidationResult |
501
|
|
|
{ |
502
|
28 |
|
return new PathValidationResult($certificates, $this->_validPolicyTree, |
503
|
28 |
|
$this->_workingPublicKey, $this->_workingPublicKeyAlgorithm, |
504
|
28 |
|
$this->_workingPublicKeyParameters); |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
/** |
508
|
|
|
* Get ASN.1 parameters from algorithm identifier. |
509
|
|
|
* |
510
|
|
|
* @param AlgorithmIdentifierType $algo |
511
|
|
|
* |
512
|
|
|
* @return null|Element ASN.1 element or null if parameters are omitted |
513
|
|
|
*/ |
514
|
47 |
|
public static function getAlgorithmParameters(AlgorithmIdentifierType $algo): ?Element |
515
|
|
|
{ |
516
|
47 |
|
$seq = $algo->toASN1(); |
517
|
47 |
|
return $seq->has(1) ? $seq->at(1)->asElement() : null; |
518
|
|
|
} |
519
|
|
|
} |
520
|
|
|
|