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