1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace X509\CertificationPath\PathValidation; |
4
|
|
|
|
5
|
|
|
use ASN1\Element; |
6
|
|
|
use CryptoUtil\ASN1\AlgorithmIdentifier; |
7
|
|
|
use CryptoUtil\ASN1\AlgorithmIdentifier\Feature\AlgorithmIdentifierType; |
8
|
|
|
use CryptoUtil\ASN1\PublicKeyInfo; |
9
|
|
|
use X501\ASN1\Name; |
10
|
|
|
use X509\Certificate\Certificate; |
11
|
|
|
use X509\CertificationPath\Policy\PolicyNode; |
12
|
|
|
use X509\CertificationPath\Policy\PolicyTree; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* State class for the certification path validation process. |
17
|
|
|
* |
18
|
|
|
* @link https://tools.ietf.org/html/rfc5280#section-6.1.1 |
19
|
|
|
* @link 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 $_pathLength |
27
|
|
|
*/ |
28
|
|
|
protected $_pathLength; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Current index in the certification path in the range of 1..n (i). |
32
|
|
|
* |
33
|
|
|
* @var int $_index |
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 PolicyTree|null $_validPolicyTree |
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 $_permittedSubtrees |
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 $_excludedSubtrees |
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 $_explicitPolicy |
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 $_inhibitAnyPolicy |
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 $_policyMapping |
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 $_workingPublicKeyAlgorithm |
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 $_workingPublicKey |
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 Element|null $_workingPublicKeyParameters |
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 $_workingIssuerName |
135
|
|
|
*/ |
136
|
|
|
protected $_workingIssuerName; |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Maximum certification path length (max_path_length). |
140
|
|
|
* |
141
|
|
|
* @var int $_maxPathLength |
142
|
|
|
*/ |
143
|
|
|
protected $_maxPathLength; |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Constructor. |
147
|
|
|
*/ |
148
|
|
|
protected function __constructor() {} |
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
|
46 |
|
public static function initialize(PathValidationConfig $config, |
160
|
|
|
Certificate $trust_anchor, $n) { |
161
|
46 |
|
$state = new self(); |
162
|
46 |
|
$state->_pathLength = $n; |
163
|
46 |
|
$state->_index = 1; |
164
|
46 |
|
$state->_validPolicyTree = new PolicyTree(PolicyNode::anyPolicyNode()); |
165
|
46 |
|
$state->_permittedSubtrees = null; |
166
|
46 |
|
$state->_excludedSubtrees = null; |
167
|
46 |
|
$state->_explicitPolicy = $config->explicitPolicy() ? 0 : $n + 1; |
168
|
46 |
|
$state->_inhibitAnyPolicy = $config->anyPolicyInhibit() ? 0 : $n + 1; |
169
|
46 |
|
$state->_policyMapping = $config->policyMappingInhibit() ? 0 : $n + 1; |
170
|
46 |
|
$state->_workingPublicKeyAlgorithm = $trust_anchor->signatureAlgorithm(); |
171
|
46 |
|
$tbsCert = $trust_anchor->tbsCertificate(); |
172
|
46 |
|
$state->_workingPublicKey = $tbsCert->subjectPublicKeyInfo(); |
173
|
46 |
|
$state->_workingPublicKeyParameters = self::getAlgorithmParameters( |
174
|
46 |
|
$state->_workingPublicKey->algorithmIdentifier()); |
175
|
46 |
|
$state->_workingIssuerName = $tbsCert->issuer(); |
176
|
46 |
|
$state->_maxPathLength = $config->maxLength(); |
177
|
46 |
|
return $state; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get self with current certification path index set. |
182
|
|
|
* |
183
|
|
|
* @param int $index |
184
|
|
|
* @return self |
185
|
|
|
*/ |
186
|
44 |
|
public function withIndex($index) { |
187
|
44 |
|
$state = clone $this; |
188
|
44 |
|
$state->_index = $index; |
189
|
44 |
|
return $state; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get self with valid_policy_tree. |
194
|
|
|
* |
195
|
|
|
* @param PolicyTree $policy_tree |
196
|
|
|
* @return self |
197
|
|
|
*/ |
198
|
14 |
|
public function withValidPolicyTree(PolicyTree $policy_tree) { |
199
|
14 |
|
$state = clone $this; |
200
|
14 |
|
$state->_validPolicyTree = $policy_tree; |
201
|
14 |
|
return $state; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get self with valid_policy_tree set to null. |
206
|
|
|
* |
207
|
|
|
* @return self |
208
|
|
|
*/ |
209
|
36 |
|
public function withoutValidPolicyTree() { |
210
|
36 |
|
$state = clone $this; |
211
|
36 |
|
$state->_validPolicyTree = null; |
212
|
36 |
|
return $state; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Get self with explicit_policy. |
217
|
|
|
* |
218
|
|
|
* @param int $num |
219
|
|
|
* @return self |
220
|
|
|
*/ |
221
|
30 |
|
public function withExplicitPolicy($num) { |
222
|
30 |
|
$state = clone $this; |
223
|
30 |
|
$state->_explicitPolicy = $num; |
224
|
30 |
|
return $state; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Get self with inhibit_anyPolicy. |
229
|
|
|
* |
230
|
|
|
* @param int $num |
231
|
|
|
* @return self |
232
|
|
|
*/ |
233
|
23 |
|
public function withInhibitAnyPolicy($num) { |
234
|
23 |
|
$state = clone $this; |
235
|
23 |
|
$state->_inhibitAnyPolicy = $num; |
236
|
23 |
|
return $state; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Get self with policy_mapping. |
241
|
|
|
* |
242
|
|
|
* @param int $num |
243
|
|
|
* @return self |
244
|
|
|
*/ |
245
|
21 |
|
public function withPolicyMapping($num) { |
246
|
21 |
|
$state = clone $this; |
247
|
21 |
|
$state->_policyMapping = $num; |
248
|
21 |
|
return $state; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Get self with working_public_key_algorithm. |
253
|
|
|
* |
254
|
|
|
* @param AlgorithmIdentifierType $algo |
255
|
|
|
* @return self |
256
|
|
|
*/ |
257
|
42 |
|
public function withWorkingPublicKeyAlgorithm(AlgorithmIdentifierType $algo) { |
258
|
42 |
|
$state = clone $this; |
259
|
42 |
|
$state->_workingPublicKeyAlgorithm = $algo; |
260
|
42 |
|
return $state; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Get self with working_public_key. |
265
|
|
|
* |
266
|
|
|
* @param PublicKeyInfo $pubkey_info |
267
|
|
|
* @return self |
268
|
|
|
*/ |
269
|
42 |
|
public function withWorkingPublicKey(PublicKeyInfo $pubkey_info) { |
270
|
42 |
|
$state = clone $this; |
271
|
42 |
|
$state->_workingPublicKey = $pubkey_info; |
272
|
42 |
|
return $state; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Get self with working_public_key_parameters. |
277
|
|
|
* |
278
|
|
|
* @param Element|null $params |
279
|
|
|
* @return self |
280
|
|
|
*/ |
281
|
42 |
|
public function withWorkingPublicKeyParameters(Element $params = null) { |
282
|
42 |
|
$state = clone $this; |
283
|
42 |
|
$state->_workingPublicKeyParameters = $params; |
284
|
42 |
|
return $state; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Get self with working_issuer_name. |
289
|
|
|
* |
290
|
|
|
* @param Name $issuer |
291
|
|
|
* @return self |
292
|
|
|
*/ |
293
|
42 |
|
public function withWorkingIssuerName(Name $issuer) { |
294
|
42 |
|
$state = clone $this; |
295
|
42 |
|
$state->_workingIssuerName = $issuer; |
296
|
42 |
|
return $state; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Get self with max_path_length. |
301
|
|
|
* |
302
|
|
|
* @param int $length |
303
|
|
|
* @return self |
304
|
|
|
*/ |
305
|
28 |
|
public function withMaxPathLength($length) { |
306
|
28 |
|
$state = clone $this; |
307
|
28 |
|
$state->_maxPathLength = $length; |
308
|
28 |
|
return $state; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Get the certification path length (n). |
313
|
|
|
* |
314
|
|
|
* @return int |
315
|
|
|
*/ |
316
|
1 |
|
public function pathLength() { |
317
|
1 |
|
return $this->_pathLength; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Get the current index in certification path in the range of 1..n. |
322
|
|
|
* |
323
|
|
|
* @return int |
324
|
|
|
*/ |
325
|
14 |
|
public function index() { |
326
|
14 |
|
return $this->_index; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Check whether valid_policy_tree is present. |
331
|
|
|
* |
332
|
|
|
* @return bool |
333
|
|
|
*/ |
334
|
35 |
|
public function hasValidPolicyTree() { |
335
|
35 |
|
return isset($this->_validPolicyTree); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Get valid_policy_tree. |
340
|
|
|
* |
341
|
|
|
* @throws \LogicException |
342
|
|
|
* @return PolicyTree |
343
|
|
|
*/ |
344
|
15 |
|
public function validPolicyTree() { |
345
|
15 |
|
if (!$this->hasValidPolicyTree()) { |
346
|
1 |
|
throw new \LogicException("valid_policy_tree not set."); |
347
|
|
|
} |
348
|
14 |
|
return $this->_validPolicyTree; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Get permitted_subtrees. |
353
|
|
|
* |
354
|
|
|
* @return mixed |
355
|
|
|
*/ |
356
|
35 |
|
public function permittedSubtrees() { |
357
|
35 |
|
return $this->_permittedSubtrees; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Get excluded_subtrees. |
362
|
|
|
* |
363
|
|
|
* @return mixed |
364
|
|
|
*/ |
365
|
35 |
|
public function excludedSubtrees() { |
366
|
35 |
|
return $this->_excludedSubtrees; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Get explicit_policy. |
371
|
|
|
* |
372
|
|
|
* @return int |
373
|
|
|
*/ |
374
|
43 |
|
public function explicitPolicy() { |
375
|
43 |
|
return $this->_explicitPolicy; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Get inhibit_anyPolicy. |
380
|
|
|
* |
381
|
|
|
* @return int |
382
|
|
|
*/ |
383
|
26 |
|
public function inhibitAnyPolicy() { |
384
|
26 |
|
return $this->_inhibitAnyPolicy; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Get policy_mapping. |
389
|
|
|
* |
390
|
|
|
* @return int |
391
|
|
|
*/ |
392
|
23 |
|
public function policyMapping() { |
393
|
23 |
|
return $this->_policyMapping; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Get working_public_key_algorithm. |
398
|
|
|
* |
399
|
|
|
* @return AlgorithmIdentifierType |
400
|
|
|
*/ |
401
|
1 |
|
public function workingPublicKeyAlgorithm() { |
402
|
1 |
|
return $this->_workingPublicKeyAlgorithm; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Get working_public_key. |
407
|
|
|
* |
408
|
|
|
* @return PublicKeyInfo |
409
|
|
|
*/ |
410
|
44 |
|
public function workingPublicKey() { |
411
|
44 |
|
return $this->_workingPublicKey; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Get working_public_key_parameters. |
416
|
|
|
* |
417
|
|
|
* @return Element|null |
418
|
|
|
*/ |
419
|
1 |
|
public function workingPublicKeyParameters() { |
420
|
1 |
|
return $this->_workingPublicKeyParameters; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* Get working_issuer_name. |
425
|
|
|
* |
426
|
|
|
* @return Name |
427
|
|
|
*/ |
428
|
43 |
|
public function workingIssuerName() { |
429
|
43 |
|
return $this->_workingIssuerName; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* Get maximum certification path length. |
434
|
|
|
* |
435
|
|
|
* @return int |
436
|
|
|
*/ |
437
|
31 |
|
public function maxPathLength() { |
438
|
31 |
|
return $this->_maxPathLength; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Check whether processing the final certificate of the certification path. |
443
|
|
|
* |
444
|
|
|
* @return bool |
445
|
|
|
*/ |
446
|
43 |
|
public function isFinal() { |
447
|
43 |
|
return $this->_index == $this->_pathLength; |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* Get the path validation result. |
452
|
|
|
* |
453
|
|
|
* @param Certificate[] $certificates Certificates in a certification path |
454
|
|
|
* @return PathValidationResult |
455
|
|
|
*/ |
456
|
28 |
|
public function getResult(array $certificates) { |
457
|
28 |
|
return new PathValidationResult($certificates, $this->_validPolicyTree, |
458
|
28 |
|
$this->_workingPublicKey, $this->_workingPublicKeyAlgorithm, |
459
|
28 |
|
$this->_workingPublicKeyParameters); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* Get ASN.1 parameters from algorithm identifier. |
464
|
|
|
* |
465
|
|
|
* @param AlgorithmIdentifierType $algo |
466
|
|
|
* @return Element|null ASN.1 element or null if parameters are omitted |
467
|
|
|
*/ |
468
|
46 |
|
public static function getAlgorithmParameters(AlgorithmIdentifierType $algo) { |
469
|
46 |
|
$seq = $algo->toASN1(); |
470
|
46 |
|
return $seq->has(1) ? $seq->at(1)->asElement() : null; |
471
|
|
|
} |
472
|
|
|
} |
473
|
|
|
|