@@ -20,484 +20,484 @@ |
||
20 | 20 | */ |
21 | 21 | class ValidatorState |
22 | 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 __construct() |
|
149 | - { |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Initialize variables according to RFC 5280 6.1.2. |
|
154 | - * |
|
155 | - * @link https://tools.ietf.org/html/rfc5280#section-6.1.2 |
|
156 | - * @param PathValidationConfig $config |
|
157 | - * @param Certificate $trust_anchor Trust anchor certificate |
|
158 | - * @param int $n Number of certificates in the certification path |
|
159 | - * @return self |
|
160 | - */ |
|
161 | - public static function initialize(PathValidationConfig $config, |
|
162 | - Certificate $trust_anchor, $n) |
|
163 | - { |
|
164 | - $state = new self(); |
|
165 | - $state->_pathLength = $n; |
|
166 | - $state->_index = 1; |
|
167 | - $state->_validPolicyTree = new PolicyTree(PolicyNode::anyPolicyNode()); |
|
168 | - $state->_permittedSubtrees = null; |
|
169 | - $state->_excludedSubtrees = null; |
|
170 | - $state->_explicitPolicy = $config->explicitPolicy() ? 0 : $n + 1; |
|
171 | - $state->_inhibitAnyPolicy = $config->anyPolicyInhibit() ? 0 : $n + 1; |
|
172 | - $state->_policyMapping = $config->policyMappingInhibit() ? 0 : $n + 1; |
|
173 | - $state->_workingPublicKeyAlgorithm = $trust_anchor->signatureAlgorithm(); |
|
174 | - $tbsCert = $trust_anchor->tbsCertificate(); |
|
175 | - $state->_workingPublicKey = $tbsCert->subjectPublicKeyInfo(); |
|
176 | - $state->_workingPublicKeyParameters = self::getAlgorithmParameters( |
|
177 | - $state->_workingPublicKey->algorithmIdentifier()); |
|
178 | - $state->_workingIssuerName = $tbsCert->issuer(); |
|
179 | - $state->_maxPathLength = $config->maxLength(); |
|
180 | - return $state; |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * Get self with current certification path index set. |
|
185 | - * |
|
186 | - * @param int $index |
|
187 | - * @return self |
|
188 | - */ |
|
189 | - public function withIndex($index) |
|
190 | - { |
|
191 | - $state = clone $this; |
|
192 | - $state->_index = $index; |
|
193 | - return $state; |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Get self with valid_policy_tree. |
|
198 | - * |
|
199 | - * @param PolicyTree $policy_tree |
|
200 | - * @return self |
|
201 | - */ |
|
202 | - public function withValidPolicyTree(PolicyTree $policy_tree) |
|
203 | - { |
|
204 | - $state = clone $this; |
|
205 | - $state->_validPolicyTree = $policy_tree; |
|
206 | - return $state; |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Get self with valid_policy_tree set to null. |
|
211 | - * |
|
212 | - * @return self |
|
213 | - */ |
|
214 | - public function withoutValidPolicyTree() |
|
215 | - { |
|
216 | - $state = clone $this; |
|
217 | - $state->_validPolicyTree = null; |
|
218 | - return $state; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Get self with explicit_policy. |
|
223 | - * |
|
224 | - * @param int $num |
|
225 | - * @return self |
|
226 | - */ |
|
227 | - public function withExplicitPolicy($num) |
|
228 | - { |
|
229 | - $state = clone $this; |
|
230 | - $state->_explicitPolicy = $num; |
|
231 | - return $state; |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * Get self with inhibit_anyPolicy. |
|
236 | - * |
|
237 | - * @param int $num |
|
238 | - * @return self |
|
239 | - */ |
|
240 | - public function withInhibitAnyPolicy($num) |
|
241 | - { |
|
242 | - $state = clone $this; |
|
243 | - $state->_inhibitAnyPolicy = $num; |
|
244 | - return $state; |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * Get self with policy_mapping. |
|
249 | - * |
|
250 | - * @param int $num |
|
251 | - * @return self |
|
252 | - */ |
|
253 | - public function withPolicyMapping($num) |
|
254 | - { |
|
255 | - $state = clone $this; |
|
256 | - $state->_policyMapping = $num; |
|
257 | - return $state; |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * Get self with working_public_key_algorithm. |
|
262 | - * |
|
263 | - * @param AlgorithmIdentifierType $algo |
|
264 | - * @return self |
|
265 | - */ |
|
266 | - public function withWorkingPublicKeyAlgorithm(AlgorithmIdentifierType $algo) |
|
267 | - { |
|
268 | - $state = clone $this; |
|
269 | - $state->_workingPublicKeyAlgorithm = $algo; |
|
270 | - return $state; |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * Get self with working_public_key. |
|
275 | - * |
|
276 | - * @param PublicKeyInfo $pubkey_info |
|
277 | - * @return self |
|
278 | - */ |
|
279 | - public function withWorkingPublicKey(PublicKeyInfo $pubkey_info) |
|
280 | - { |
|
281 | - $state = clone $this; |
|
282 | - $state->_workingPublicKey = $pubkey_info; |
|
283 | - return $state; |
|
284 | - } |
|
285 | - |
|
286 | - /** |
|
287 | - * Get self with working_public_key_parameters. |
|
288 | - * |
|
289 | - * @param Element|null $params |
|
290 | - * @return self |
|
291 | - */ |
|
292 | - public function withWorkingPublicKeyParameters(Element $params = null) |
|
293 | - { |
|
294 | - $state = clone $this; |
|
295 | - $state->_workingPublicKeyParameters = $params; |
|
296 | - return $state; |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * Get self with working_issuer_name. |
|
301 | - * |
|
302 | - * @param Name $issuer |
|
303 | - * @return self |
|
304 | - */ |
|
305 | - public function withWorkingIssuerName(Name $issuer) |
|
306 | - { |
|
307 | - $state = clone $this; |
|
308 | - $state->_workingIssuerName = $issuer; |
|
309 | - return $state; |
|
310 | - } |
|
311 | - |
|
312 | - /** |
|
313 | - * Get self with max_path_length. |
|
314 | - * |
|
315 | - * @param int $length |
|
316 | - * @return self |
|
317 | - */ |
|
318 | - public function withMaxPathLength($length) |
|
319 | - { |
|
320 | - $state = clone $this; |
|
321 | - $state->_maxPathLength = $length; |
|
322 | - return $state; |
|
323 | - } |
|
324 | - |
|
325 | - /** |
|
326 | - * Get the certification path length (n). |
|
327 | - * |
|
328 | - * @return int |
|
329 | - */ |
|
330 | - public function pathLength(): int |
|
331 | - { |
|
332 | - return $this->_pathLength; |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * Get the current index in certification path in the range of 1..n. |
|
337 | - * |
|
338 | - * @return int |
|
339 | - */ |
|
340 | - public function index(): int |
|
341 | - { |
|
342 | - return $this->_index; |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * Check whether valid_policy_tree is present. |
|
347 | - * |
|
348 | - * @return bool |
|
349 | - */ |
|
350 | - public function hasValidPolicyTree(): bool |
|
351 | - { |
|
352 | - return isset($this->_validPolicyTree); |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * Get valid_policy_tree. |
|
357 | - * |
|
358 | - * @throws \LogicException |
|
359 | - * @return PolicyTree |
|
360 | - */ |
|
361 | - public function validPolicyTree(): PolicyTree |
|
362 | - { |
|
363 | - if (!$this->hasValidPolicyTree()) { |
|
364 | - throw new \LogicException("valid_policy_tree not set."); |
|
365 | - } |
|
366 | - return $this->_validPolicyTree; |
|
367 | - } |
|
368 | - |
|
369 | - /** |
|
370 | - * Get permitted_subtrees. |
|
371 | - * |
|
372 | - * @return mixed |
|
373 | - */ |
|
374 | - public function permittedSubtrees() |
|
375 | - { |
|
376 | - return $this->_permittedSubtrees; |
|
377 | - } |
|
378 | - |
|
379 | - /** |
|
380 | - * Get excluded_subtrees. |
|
381 | - * |
|
382 | - * @return mixed |
|
383 | - */ |
|
384 | - public function excludedSubtrees() |
|
385 | - { |
|
386 | - return $this->_excludedSubtrees; |
|
387 | - } |
|
388 | - |
|
389 | - /** |
|
390 | - * Get explicit_policy. |
|
391 | - * |
|
392 | - * @return int |
|
393 | - */ |
|
394 | - public function explicitPolicy() |
|
395 | - { |
|
396 | - return $this->_explicitPolicy; |
|
397 | - } |
|
398 | - |
|
399 | - /** |
|
400 | - * Get inhibit_anyPolicy. |
|
401 | - * |
|
402 | - * @return int |
|
403 | - */ |
|
404 | - public function inhibitAnyPolicy(): int |
|
405 | - { |
|
406 | - return $this->_inhibitAnyPolicy; |
|
407 | - } |
|
408 | - |
|
409 | - /** |
|
410 | - * Get policy_mapping. |
|
411 | - * |
|
412 | - * @return int |
|
413 | - */ |
|
414 | - public function policyMapping(): int |
|
415 | - { |
|
416 | - return $this->_policyMapping; |
|
417 | - } |
|
418 | - |
|
419 | - /** |
|
420 | - * Get working_public_key_algorithm. |
|
421 | - * |
|
422 | - * @return AlgorithmIdentifierType |
|
423 | - */ |
|
424 | - public function workingPublicKeyAlgorithm(): AlgorithmIdentifierType |
|
425 | - { |
|
426 | - return $this->_workingPublicKeyAlgorithm; |
|
427 | - } |
|
428 | - |
|
429 | - /** |
|
430 | - * Get working_public_key. |
|
431 | - * |
|
432 | - * @return PublicKeyInfo |
|
433 | - */ |
|
434 | - public function workingPublicKey(): PublicKeyInfo |
|
435 | - { |
|
436 | - return $this->_workingPublicKey; |
|
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * Get working_public_key_parameters. |
|
441 | - * |
|
442 | - * @return Element|null |
|
443 | - */ |
|
444 | - public function workingPublicKeyParameters() |
|
445 | - { |
|
446 | - return $this->_workingPublicKeyParameters; |
|
447 | - } |
|
448 | - |
|
449 | - /** |
|
450 | - * Get working_issuer_name. |
|
451 | - * |
|
452 | - * @return Name |
|
453 | - */ |
|
454 | - public function workingIssuerName(): Name |
|
455 | - { |
|
456 | - return $this->_workingIssuerName; |
|
457 | - } |
|
458 | - |
|
459 | - /** |
|
460 | - * Get maximum certification path length. |
|
461 | - * |
|
462 | - * @return int |
|
463 | - */ |
|
464 | - public function maxPathLength(): int |
|
465 | - { |
|
466 | - return $this->_maxPathLength; |
|
467 | - } |
|
468 | - |
|
469 | - /** |
|
470 | - * Check whether processing the final certificate of the certification path. |
|
471 | - * |
|
472 | - * @return bool |
|
473 | - */ |
|
474 | - public function isFinal(): bool |
|
475 | - { |
|
476 | - return $this->_index == $this->_pathLength; |
|
477 | - } |
|
478 | - |
|
479 | - /** |
|
480 | - * Get the path validation result. |
|
481 | - * |
|
482 | - * @param Certificate[] $certificates Certificates in a certification path |
|
483 | - * @return PathValidationResult |
|
484 | - */ |
|
485 | - public function getResult(array $certificates): PathValidationResult |
|
486 | - { |
|
487 | - return new PathValidationResult($certificates, $this->_validPolicyTree, |
|
488 | - $this->_workingPublicKey, $this->_workingPublicKeyAlgorithm, |
|
489 | - $this->_workingPublicKeyParameters); |
|
490 | - } |
|
491 | - |
|
492 | - /** |
|
493 | - * Get ASN.1 parameters from algorithm identifier. |
|
494 | - * |
|
495 | - * @param AlgorithmIdentifierType $algo |
|
496 | - * @return Element|null ASN.1 element or null if parameters are omitted |
|
497 | - */ |
|
498 | - public static function getAlgorithmParameters(AlgorithmIdentifierType $algo) |
|
499 | - { |
|
500 | - $seq = $algo->toASN1(); |
|
501 | - return $seq->has(1) ? $seq->at(1)->asElement() : null; |
|
502 | - } |
|
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 __construct() |
|
149 | + { |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Initialize variables according to RFC 5280 6.1.2. |
|
154 | + * |
|
155 | + * @link https://tools.ietf.org/html/rfc5280#section-6.1.2 |
|
156 | + * @param PathValidationConfig $config |
|
157 | + * @param Certificate $trust_anchor Trust anchor certificate |
|
158 | + * @param int $n Number of certificates in the certification path |
|
159 | + * @return self |
|
160 | + */ |
|
161 | + public static function initialize(PathValidationConfig $config, |
|
162 | + Certificate $trust_anchor, $n) |
|
163 | + { |
|
164 | + $state = new self(); |
|
165 | + $state->_pathLength = $n; |
|
166 | + $state->_index = 1; |
|
167 | + $state->_validPolicyTree = new PolicyTree(PolicyNode::anyPolicyNode()); |
|
168 | + $state->_permittedSubtrees = null; |
|
169 | + $state->_excludedSubtrees = null; |
|
170 | + $state->_explicitPolicy = $config->explicitPolicy() ? 0 : $n + 1; |
|
171 | + $state->_inhibitAnyPolicy = $config->anyPolicyInhibit() ? 0 : $n + 1; |
|
172 | + $state->_policyMapping = $config->policyMappingInhibit() ? 0 : $n + 1; |
|
173 | + $state->_workingPublicKeyAlgorithm = $trust_anchor->signatureAlgorithm(); |
|
174 | + $tbsCert = $trust_anchor->tbsCertificate(); |
|
175 | + $state->_workingPublicKey = $tbsCert->subjectPublicKeyInfo(); |
|
176 | + $state->_workingPublicKeyParameters = self::getAlgorithmParameters( |
|
177 | + $state->_workingPublicKey->algorithmIdentifier()); |
|
178 | + $state->_workingIssuerName = $tbsCert->issuer(); |
|
179 | + $state->_maxPathLength = $config->maxLength(); |
|
180 | + return $state; |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * Get self with current certification path index set. |
|
185 | + * |
|
186 | + * @param int $index |
|
187 | + * @return self |
|
188 | + */ |
|
189 | + public function withIndex($index) |
|
190 | + { |
|
191 | + $state = clone $this; |
|
192 | + $state->_index = $index; |
|
193 | + return $state; |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Get self with valid_policy_tree. |
|
198 | + * |
|
199 | + * @param PolicyTree $policy_tree |
|
200 | + * @return self |
|
201 | + */ |
|
202 | + public function withValidPolicyTree(PolicyTree $policy_tree) |
|
203 | + { |
|
204 | + $state = clone $this; |
|
205 | + $state->_validPolicyTree = $policy_tree; |
|
206 | + return $state; |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Get self with valid_policy_tree set to null. |
|
211 | + * |
|
212 | + * @return self |
|
213 | + */ |
|
214 | + public function withoutValidPolicyTree() |
|
215 | + { |
|
216 | + $state = clone $this; |
|
217 | + $state->_validPolicyTree = null; |
|
218 | + return $state; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Get self with explicit_policy. |
|
223 | + * |
|
224 | + * @param int $num |
|
225 | + * @return self |
|
226 | + */ |
|
227 | + public function withExplicitPolicy($num) |
|
228 | + { |
|
229 | + $state = clone $this; |
|
230 | + $state->_explicitPolicy = $num; |
|
231 | + return $state; |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * Get self with inhibit_anyPolicy. |
|
236 | + * |
|
237 | + * @param int $num |
|
238 | + * @return self |
|
239 | + */ |
|
240 | + public function withInhibitAnyPolicy($num) |
|
241 | + { |
|
242 | + $state = clone $this; |
|
243 | + $state->_inhibitAnyPolicy = $num; |
|
244 | + return $state; |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * Get self with policy_mapping. |
|
249 | + * |
|
250 | + * @param int $num |
|
251 | + * @return self |
|
252 | + */ |
|
253 | + public function withPolicyMapping($num) |
|
254 | + { |
|
255 | + $state = clone $this; |
|
256 | + $state->_policyMapping = $num; |
|
257 | + return $state; |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * Get self with working_public_key_algorithm. |
|
262 | + * |
|
263 | + * @param AlgorithmIdentifierType $algo |
|
264 | + * @return self |
|
265 | + */ |
|
266 | + public function withWorkingPublicKeyAlgorithm(AlgorithmIdentifierType $algo) |
|
267 | + { |
|
268 | + $state = clone $this; |
|
269 | + $state->_workingPublicKeyAlgorithm = $algo; |
|
270 | + return $state; |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * Get self with working_public_key. |
|
275 | + * |
|
276 | + * @param PublicKeyInfo $pubkey_info |
|
277 | + * @return self |
|
278 | + */ |
|
279 | + public function withWorkingPublicKey(PublicKeyInfo $pubkey_info) |
|
280 | + { |
|
281 | + $state = clone $this; |
|
282 | + $state->_workingPublicKey = $pubkey_info; |
|
283 | + return $state; |
|
284 | + } |
|
285 | + |
|
286 | + /** |
|
287 | + * Get self with working_public_key_parameters. |
|
288 | + * |
|
289 | + * @param Element|null $params |
|
290 | + * @return self |
|
291 | + */ |
|
292 | + public function withWorkingPublicKeyParameters(Element $params = null) |
|
293 | + { |
|
294 | + $state = clone $this; |
|
295 | + $state->_workingPublicKeyParameters = $params; |
|
296 | + return $state; |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * Get self with working_issuer_name. |
|
301 | + * |
|
302 | + * @param Name $issuer |
|
303 | + * @return self |
|
304 | + */ |
|
305 | + public function withWorkingIssuerName(Name $issuer) |
|
306 | + { |
|
307 | + $state = clone $this; |
|
308 | + $state->_workingIssuerName = $issuer; |
|
309 | + return $state; |
|
310 | + } |
|
311 | + |
|
312 | + /** |
|
313 | + * Get self with max_path_length. |
|
314 | + * |
|
315 | + * @param int $length |
|
316 | + * @return self |
|
317 | + */ |
|
318 | + public function withMaxPathLength($length) |
|
319 | + { |
|
320 | + $state = clone $this; |
|
321 | + $state->_maxPathLength = $length; |
|
322 | + return $state; |
|
323 | + } |
|
324 | + |
|
325 | + /** |
|
326 | + * Get the certification path length (n). |
|
327 | + * |
|
328 | + * @return int |
|
329 | + */ |
|
330 | + public function pathLength(): int |
|
331 | + { |
|
332 | + return $this->_pathLength; |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * Get the current index in certification path in the range of 1..n. |
|
337 | + * |
|
338 | + * @return int |
|
339 | + */ |
|
340 | + public function index(): int |
|
341 | + { |
|
342 | + return $this->_index; |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * Check whether valid_policy_tree is present. |
|
347 | + * |
|
348 | + * @return bool |
|
349 | + */ |
|
350 | + public function hasValidPolicyTree(): bool |
|
351 | + { |
|
352 | + return isset($this->_validPolicyTree); |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * Get valid_policy_tree. |
|
357 | + * |
|
358 | + * @throws \LogicException |
|
359 | + * @return PolicyTree |
|
360 | + */ |
|
361 | + public function validPolicyTree(): PolicyTree |
|
362 | + { |
|
363 | + if (!$this->hasValidPolicyTree()) { |
|
364 | + throw new \LogicException("valid_policy_tree not set."); |
|
365 | + } |
|
366 | + return $this->_validPolicyTree; |
|
367 | + } |
|
368 | + |
|
369 | + /** |
|
370 | + * Get permitted_subtrees. |
|
371 | + * |
|
372 | + * @return mixed |
|
373 | + */ |
|
374 | + public function permittedSubtrees() |
|
375 | + { |
|
376 | + return $this->_permittedSubtrees; |
|
377 | + } |
|
378 | + |
|
379 | + /** |
|
380 | + * Get excluded_subtrees. |
|
381 | + * |
|
382 | + * @return mixed |
|
383 | + */ |
|
384 | + public function excludedSubtrees() |
|
385 | + { |
|
386 | + return $this->_excludedSubtrees; |
|
387 | + } |
|
388 | + |
|
389 | + /** |
|
390 | + * Get explicit_policy. |
|
391 | + * |
|
392 | + * @return int |
|
393 | + */ |
|
394 | + public function explicitPolicy() |
|
395 | + { |
|
396 | + return $this->_explicitPolicy; |
|
397 | + } |
|
398 | + |
|
399 | + /** |
|
400 | + * Get inhibit_anyPolicy. |
|
401 | + * |
|
402 | + * @return int |
|
403 | + */ |
|
404 | + public function inhibitAnyPolicy(): int |
|
405 | + { |
|
406 | + return $this->_inhibitAnyPolicy; |
|
407 | + } |
|
408 | + |
|
409 | + /** |
|
410 | + * Get policy_mapping. |
|
411 | + * |
|
412 | + * @return int |
|
413 | + */ |
|
414 | + public function policyMapping(): int |
|
415 | + { |
|
416 | + return $this->_policyMapping; |
|
417 | + } |
|
418 | + |
|
419 | + /** |
|
420 | + * Get working_public_key_algorithm. |
|
421 | + * |
|
422 | + * @return AlgorithmIdentifierType |
|
423 | + */ |
|
424 | + public function workingPublicKeyAlgorithm(): AlgorithmIdentifierType |
|
425 | + { |
|
426 | + return $this->_workingPublicKeyAlgorithm; |
|
427 | + } |
|
428 | + |
|
429 | + /** |
|
430 | + * Get working_public_key. |
|
431 | + * |
|
432 | + * @return PublicKeyInfo |
|
433 | + */ |
|
434 | + public function workingPublicKey(): PublicKeyInfo |
|
435 | + { |
|
436 | + return $this->_workingPublicKey; |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * Get working_public_key_parameters. |
|
441 | + * |
|
442 | + * @return Element|null |
|
443 | + */ |
|
444 | + public function workingPublicKeyParameters() |
|
445 | + { |
|
446 | + return $this->_workingPublicKeyParameters; |
|
447 | + } |
|
448 | + |
|
449 | + /** |
|
450 | + * Get working_issuer_name. |
|
451 | + * |
|
452 | + * @return Name |
|
453 | + */ |
|
454 | + public function workingIssuerName(): Name |
|
455 | + { |
|
456 | + return $this->_workingIssuerName; |
|
457 | + } |
|
458 | + |
|
459 | + /** |
|
460 | + * Get maximum certification path length. |
|
461 | + * |
|
462 | + * @return int |
|
463 | + */ |
|
464 | + public function maxPathLength(): int |
|
465 | + { |
|
466 | + return $this->_maxPathLength; |
|
467 | + } |
|
468 | + |
|
469 | + /** |
|
470 | + * Check whether processing the final certificate of the certification path. |
|
471 | + * |
|
472 | + * @return bool |
|
473 | + */ |
|
474 | + public function isFinal(): bool |
|
475 | + { |
|
476 | + return $this->_index == $this->_pathLength; |
|
477 | + } |
|
478 | + |
|
479 | + /** |
|
480 | + * Get the path validation result. |
|
481 | + * |
|
482 | + * @param Certificate[] $certificates Certificates in a certification path |
|
483 | + * @return PathValidationResult |
|
484 | + */ |
|
485 | + public function getResult(array $certificates): PathValidationResult |
|
486 | + { |
|
487 | + return new PathValidationResult($certificates, $this->_validPolicyTree, |
|
488 | + $this->_workingPublicKey, $this->_workingPublicKeyAlgorithm, |
|
489 | + $this->_workingPublicKeyParameters); |
|
490 | + } |
|
491 | + |
|
492 | + /** |
|
493 | + * Get ASN.1 parameters from algorithm identifier. |
|
494 | + * |
|
495 | + * @param AlgorithmIdentifierType $algo |
|
496 | + * @return Element|null ASN.1 element or null if parameters are omitted |
|
497 | + */ |
|
498 | + public static function getAlgorithmParameters(AlgorithmIdentifierType $algo) |
|
499 | + { |
|
500 | + $seq = $algo->toASN1(); |
|
501 | + return $seq->has(1) ? $seq->at(1)->asElement() : null; |
|
502 | + } |
|
503 | 503 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types=1); |
|
3 | +declare(strict_types = 1); |
|
4 | 4 | |
5 | 5 | namespace X509\CertificationPath\PathValidation; |
6 | 6 |
@@ -16,85 +16,85 @@ |
||
16 | 16 | */ |
17 | 17 | class PathValidationResult |
18 | 18 | { |
19 | - /** |
|
20 | - * Certificates in a certification path. |
|
21 | - * |
|
22 | - * @var \X509\Certificate\Certificate[] $_certificates |
|
23 | - */ |
|
24 | - protected $_certificates; |
|
19 | + /** |
|
20 | + * Certificates in a certification path. |
|
21 | + * |
|
22 | + * @var \X509\Certificate\Certificate[] $_certificates |
|
23 | + */ |
|
24 | + protected $_certificates; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Valid policy tree. |
|
28 | - * |
|
29 | - * @var \X509\CertificationPath\Policy\PolicyTree|null $_policyTree |
|
30 | - */ |
|
31 | - protected $_policyTree; |
|
26 | + /** |
|
27 | + * Valid policy tree. |
|
28 | + * |
|
29 | + * @var \X509\CertificationPath\Policy\PolicyTree|null $_policyTree |
|
30 | + */ |
|
31 | + protected $_policyTree; |
|
32 | 32 | |
33 | - /** |
|
34 | - * End-entity certificate's public key. |
|
35 | - * |
|
36 | - * @var PublicKeyInfo |
|
37 | - */ |
|
38 | - protected $_publicKeyInfo; |
|
33 | + /** |
|
34 | + * End-entity certificate's public key. |
|
35 | + * |
|
36 | + * @var PublicKeyInfo |
|
37 | + */ |
|
38 | + protected $_publicKeyInfo; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Public key algorithm. |
|
42 | - * |
|
43 | - * @var AlgorithmIdentifierType |
|
44 | - */ |
|
45 | - protected $_publicKeyAlgo; |
|
40 | + /** |
|
41 | + * Public key algorithm. |
|
42 | + * |
|
43 | + * @var AlgorithmIdentifierType |
|
44 | + */ |
|
45 | + protected $_publicKeyAlgo; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Public key parameters. |
|
49 | - * |
|
50 | - * @var Element|null $_publicKeyParameters |
|
51 | - */ |
|
52 | - protected $_publicKeyParameters; |
|
47 | + /** |
|
48 | + * Public key parameters. |
|
49 | + * |
|
50 | + * @var Element|null $_publicKeyParameters |
|
51 | + */ |
|
52 | + protected $_publicKeyParameters; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Constructor. |
|
56 | - * |
|
57 | - * @param \X509\Certificate\Certificate[] $certificates Certificates in a |
|
58 | - * certification path |
|
59 | - * @param \X509\CertificationPath\Policy\PolicyTree|null $policy_tree Valid |
|
60 | - * policy tree |
|
61 | - * @param PublicKeyInfo $pubkey_info Public key of the end-entity |
|
62 | - * certificate |
|
63 | - * @param AlgorithmIdentifierType $algo Public key algorithm of the |
|
64 | - * end-entity certificate |
|
65 | - * @param Element|null $params Algorithm parameters |
|
66 | - */ |
|
67 | - public function __construct(array $certificates, $policy_tree, |
|
68 | - PublicKeyInfo $pubkey_info, AlgorithmIdentifierType $algo, |
|
69 | - Element $params = null) |
|
70 | - { |
|
71 | - $this->_certificates = array_values($certificates); |
|
72 | - $this->_policyTree = $policy_tree; |
|
73 | - $this->_publicKeyInfo = $pubkey_info; |
|
74 | - $this->_publicKeyAlgo = $algo; |
|
75 | - $this->_publicKeyParameters = $params; |
|
76 | - } |
|
54 | + /** |
|
55 | + * Constructor. |
|
56 | + * |
|
57 | + * @param \X509\Certificate\Certificate[] $certificates Certificates in a |
|
58 | + * certification path |
|
59 | + * @param \X509\CertificationPath\Policy\PolicyTree|null $policy_tree Valid |
|
60 | + * policy tree |
|
61 | + * @param PublicKeyInfo $pubkey_info Public key of the end-entity |
|
62 | + * certificate |
|
63 | + * @param AlgorithmIdentifierType $algo Public key algorithm of the |
|
64 | + * end-entity certificate |
|
65 | + * @param Element|null $params Algorithm parameters |
|
66 | + */ |
|
67 | + public function __construct(array $certificates, $policy_tree, |
|
68 | + PublicKeyInfo $pubkey_info, AlgorithmIdentifierType $algo, |
|
69 | + Element $params = null) |
|
70 | + { |
|
71 | + $this->_certificates = array_values($certificates); |
|
72 | + $this->_policyTree = $policy_tree; |
|
73 | + $this->_publicKeyInfo = $pubkey_info; |
|
74 | + $this->_publicKeyAlgo = $algo; |
|
75 | + $this->_publicKeyParameters = $params; |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * Get end-entity certificate. |
|
80 | - * |
|
81 | - * @return \X509\Certificate\Certificate |
|
82 | - */ |
|
83 | - public function certificate(): Certificate |
|
84 | - { |
|
85 | - return $this->_certificates[count($this->_certificates) - 1]; |
|
86 | - } |
|
78 | + /** |
|
79 | + * Get end-entity certificate. |
|
80 | + * |
|
81 | + * @return \X509\Certificate\Certificate |
|
82 | + */ |
|
83 | + public function certificate(): Certificate |
|
84 | + { |
|
85 | + return $this->_certificates[count($this->_certificates) - 1]; |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Get certificate policies of the end-entity certificate. |
|
90 | - * |
|
91 | - * @return \X509\Certificate\Extension\CertificatePolicy\PolicyInformation[] |
|
92 | - */ |
|
93 | - public function policies(): array |
|
94 | - { |
|
95 | - if (!$this->_policyTree) { |
|
96 | - return array(); |
|
97 | - } |
|
98 | - return $this->_policyTree->policiesAtDepth(count($this->_certificates)); |
|
99 | - } |
|
88 | + /** |
|
89 | + * Get certificate policies of the end-entity certificate. |
|
90 | + * |
|
91 | + * @return \X509\Certificate\Extension\CertificatePolicy\PolicyInformation[] |
|
92 | + */ |
|
93 | + public function policies(): array |
|
94 | + { |
|
95 | + if (!$this->_policyTree) { |
|
96 | + return array(); |
|
97 | + } |
|
98 | + return $this->_policyTree->policiesAtDepth(count($this->_certificates)); |
|
99 | + } |
|
100 | 100 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types=1); |
|
3 | +declare(strict_types = 1); |
|
4 | 4 | |
5 | 5 | namespace X509\CertificationPath\PathValidation; |
6 | 6 |
@@ -17,584 +17,584 @@ |
||
17 | 17 | */ |
18 | 18 | class PathValidator |
19 | 19 | { |
20 | - /** |
|
21 | - * Crypto engine. |
|
22 | - * |
|
23 | - * @var Crypto $_crypto |
|
24 | - */ |
|
25 | - protected $_crypto; |
|
20 | + /** |
|
21 | + * Crypto engine. |
|
22 | + * |
|
23 | + * @var Crypto $_crypto |
|
24 | + */ |
|
25 | + protected $_crypto; |
|
26 | 26 | |
27 | - /** |
|
28 | - * Path validation configuration. |
|
29 | - * |
|
30 | - * @var PathValidationConfig $_config |
|
31 | - */ |
|
32 | - protected $_config; |
|
27 | + /** |
|
28 | + * Path validation configuration. |
|
29 | + * |
|
30 | + * @var PathValidationConfig $_config |
|
31 | + */ |
|
32 | + protected $_config; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Certification path. |
|
36 | - * |
|
37 | - * @var Certificate[] $_certificates |
|
38 | - */ |
|
39 | - protected $_certificates; |
|
34 | + /** |
|
35 | + * Certification path. |
|
36 | + * |
|
37 | + * @var Certificate[] $_certificates |
|
38 | + */ |
|
39 | + protected $_certificates; |
|
40 | 40 | |
41 | - /** |
|
42 | - * Certification path trust anchor. |
|
43 | - * |
|
44 | - * @var Certificate $_trustAnchor |
|
45 | - */ |
|
46 | - protected $_trustAnchor; |
|
41 | + /** |
|
42 | + * Certification path trust anchor. |
|
43 | + * |
|
44 | + * @var Certificate $_trustAnchor |
|
45 | + */ |
|
46 | + protected $_trustAnchor; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Constructor. |
|
50 | - * |
|
51 | - * @param Crypto $crypto Crypto engine |
|
52 | - * @param PathValidationConfig $config Validation config |
|
53 | - * @param Certificate ...$certificates Certificates from the trust anchor to |
|
54 | - * the end-entity certificate |
|
55 | - */ |
|
56 | - public function __construct(Crypto $crypto, PathValidationConfig $config, |
|
57 | - Certificate ...$certificates) |
|
58 | - { |
|
59 | - if (!count($certificates)) { |
|
60 | - throw new \LogicException("No certificates."); |
|
61 | - } |
|
62 | - $this->_crypto = $crypto; |
|
63 | - $this->_config = $config; |
|
64 | - $this->_certificates = $certificates; |
|
65 | - // if trust anchor is explicitly given in configuration |
|
66 | - if ($config->hasTrustAnchor()) { |
|
67 | - $this->_trustAnchor = $config->trustAnchor(); |
|
68 | - } else { |
|
69 | - $this->_trustAnchor = $certificates[0]; |
|
70 | - } |
|
71 | - } |
|
48 | + /** |
|
49 | + * Constructor. |
|
50 | + * |
|
51 | + * @param Crypto $crypto Crypto engine |
|
52 | + * @param PathValidationConfig $config Validation config |
|
53 | + * @param Certificate ...$certificates Certificates from the trust anchor to |
|
54 | + * the end-entity certificate |
|
55 | + */ |
|
56 | + public function __construct(Crypto $crypto, PathValidationConfig $config, |
|
57 | + Certificate ...$certificates) |
|
58 | + { |
|
59 | + if (!count($certificates)) { |
|
60 | + throw new \LogicException("No certificates."); |
|
61 | + } |
|
62 | + $this->_crypto = $crypto; |
|
63 | + $this->_config = $config; |
|
64 | + $this->_certificates = $certificates; |
|
65 | + // if trust anchor is explicitly given in configuration |
|
66 | + if ($config->hasTrustAnchor()) { |
|
67 | + $this->_trustAnchor = $config->trustAnchor(); |
|
68 | + } else { |
|
69 | + $this->_trustAnchor = $certificates[0]; |
|
70 | + } |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Validate certification path. |
|
75 | - * |
|
76 | - * @throws PathValidationException |
|
77 | - * @return PathValidationResult |
|
78 | - */ |
|
79 | - public function validate() |
|
80 | - { |
|
81 | - $n = count($this->_certificates); |
|
82 | - $state = ValidatorState::initialize($this->_config, $this->_trustAnchor, |
|
83 | - $n); |
|
84 | - for ($i = 0; $i < $n; ++$i) { |
|
85 | - $state = $state->withIndex($i + 1); |
|
86 | - $cert = $this->_certificates[$i]; |
|
87 | - // process certificate (section 6.1.3.) |
|
88 | - $state = $this->_processCertificate($state, $cert); |
|
89 | - if (!$state->isFinal()) { |
|
90 | - // prepare next certificate (section 6.1.4.) |
|
91 | - $state = $this->_prepareNext($state, $cert); |
|
92 | - } |
|
93 | - } |
|
94 | - if (!isset($cert)) { |
|
95 | - throw new \LogicException("No certificates."); |
|
96 | - } |
|
97 | - // wrap-up (section 6.1.5.) |
|
98 | - $state = $this->_wrapUp($state, $cert); |
|
99 | - // return outputs |
|
100 | - return $state->getResult($this->_certificates); |
|
101 | - } |
|
73 | + /** |
|
74 | + * Validate certification path. |
|
75 | + * |
|
76 | + * @throws PathValidationException |
|
77 | + * @return PathValidationResult |
|
78 | + */ |
|
79 | + public function validate() |
|
80 | + { |
|
81 | + $n = count($this->_certificates); |
|
82 | + $state = ValidatorState::initialize($this->_config, $this->_trustAnchor, |
|
83 | + $n); |
|
84 | + for ($i = 0; $i < $n; ++$i) { |
|
85 | + $state = $state->withIndex($i + 1); |
|
86 | + $cert = $this->_certificates[$i]; |
|
87 | + // process certificate (section 6.1.3.) |
|
88 | + $state = $this->_processCertificate($state, $cert); |
|
89 | + if (!$state->isFinal()) { |
|
90 | + // prepare next certificate (section 6.1.4.) |
|
91 | + $state = $this->_prepareNext($state, $cert); |
|
92 | + } |
|
93 | + } |
|
94 | + if (!isset($cert)) { |
|
95 | + throw new \LogicException("No certificates."); |
|
96 | + } |
|
97 | + // wrap-up (section 6.1.5.) |
|
98 | + $state = $this->_wrapUp($state, $cert); |
|
99 | + // return outputs |
|
100 | + return $state->getResult($this->_certificates); |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * Apply basic certificate processing according to RFC 5280 section 6.1.3. |
|
105 | - * |
|
106 | - * @link https://tools.ietf.org/html/rfc5280#section-6.1.3 |
|
107 | - * @param ValidatorState $state |
|
108 | - * @param Certificate $cert |
|
109 | - * @throws PathValidationException |
|
110 | - * @return ValidatorState |
|
111 | - */ |
|
112 | - private function _processCertificate(ValidatorState $state, Certificate $cert) |
|
113 | - { |
|
114 | - // (a.1) verify signature |
|
115 | - $this->_verifySignature($state, $cert); |
|
116 | - // (a.2) check validity period |
|
117 | - $this->_checkValidity($cert); |
|
118 | - // (a.3) check that certificate is not revoked |
|
119 | - $this->_checkRevocation($cert); |
|
120 | - // (a.4) check issuer |
|
121 | - $this->_checkIssuer($state, $cert); |
|
122 | - // (b)(c) if certificate is self-issued and it is not |
|
123 | - // the final certificate in the path, skip this step |
|
124 | - if (!($cert->isSelfIssued() && !$state->isFinal())) { |
|
125 | - // (b) check permitted subtrees |
|
126 | - $this->_checkPermittedSubtrees($state, $cert); |
|
127 | - // (c) check excluded subtrees |
|
128 | - $this->_checkExcludedSubtrees($state, $cert); |
|
129 | - } |
|
130 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
131 | - if ($extensions->hasCertificatePolicies()) { |
|
132 | - // (d) process policy information |
|
133 | - if ($state->hasValidPolicyTree()) { |
|
134 | - $state = $state->validPolicyTree()->processPolicies($state, |
|
135 | - $cert); |
|
136 | - } |
|
137 | - } else { |
|
138 | - // (e) certificate policies extension not present, |
|
139 | - // set the valid_policy_tree to NULL |
|
140 | - $state = $state->withoutValidPolicyTree(); |
|
141 | - } |
|
142 | - // (f) check that explicit_policy > 0 or valid_policy_tree is set |
|
143 | - if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) { |
|
144 | - throw new PathValidationException("No valid policies."); |
|
145 | - } |
|
146 | - return $state; |
|
147 | - } |
|
103 | + /** |
|
104 | + * Apply basic certificate processing according to RFC 5280 section 6.1.3. |
|
105 | + * |
|
106 | + * @link https://tools.ietf.org/html/rfc5280#section-6.1.3 |
|
107 | + * @param ValidatorState $state |
|
108 | + * @param Certificate $cert |
|
109 | + * @throws PathValidationException |
|
110 | + * @return ValidatorState |
|
111 | + */ |
|
112 | + private function _processCertificate(ValidatorState $state, Certificate $cert) |
|
113 | + { |
|
114 | + // (a.1) verify signature |
|
115 | + $this->_verifySignature($state, $cert); |
|
116 | + // (a.2) check validity period |
|
117 | + $this->_checkValidity($cert); |
|
118 | + // (a.3) check that certificate is not revoked |
|
119 | + $this->_checkRevocation($cert); |
|
120 | + // (a.4) check issuer |
|
121 | + $this->_checkIssuer($state, $cert); |
|
122 | + // (b)(c) if certificate is self-issued and it is not |
|
123 | + // the final certificate in the path, skip this step |
|
124 | + if (!($cert->isSelfIssued() && !$state->isFinal())) { |
|
125 | + // (b) check permitted subtrees |
|
126 | + $this->_checkPermittedSubtrees($state, $cert); |
|
127 | + // (c) check excluded subtrees |
|
128 | + $this->_checkExcludedSubtrees($state, $cert); |
|
129 | + } |
|
130 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
131 | + if ($extensions->hasCertificatePolicies()) { |
|
132 | + // (d) process policy information |
|
133 | + if ($state->hasValidPolicyTree()) { |
|
134 | + $state = $state->validPolicyTree()->processPolicies($state, |
|
135 | + $cert); |
|
136 | + } |
|
137 | + } else { |
|
138 | + // (e) certificate policies extension not present, |
|
139 | + // set the valid_policy_tree to NULL |
|
140 | + $state = $state->withoutValidPolicyTree(); |
|
141 | + } |
|
142 | + // (f) check that explicit_policy > 0 or valid_policy_tree is set |
|
143 | + if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) { |
|
144 | + throw new PathValidationException("No valid policies."); |
|
145 | + } |
|
146 | + return $state; |
|
147 | + } |
|
148 | 148 | |
149 | - /** |
|
150 | - * Apply preparation for the certificate i+1 according to rfc5280 section |
|
151 | - * 6.1.4. |
|
152 | - * |
|
153 | - * @link https://tools.ietf.org/html/rfc5280#section-6.1.4 |
|
154 | - * @param ValidatorState $state |
|
155 | - * @param Certificate $cert |
|
156 | - * @return ValidatorState |
|
157 | - */ |
|
158 | - private function _prepareNext(ValidatorState $state, Certificate $cert) |
|
159 | - { |
|
160 | - // (a)(b) if policy mappings extension is present |
|
161 | - $state = $this->_preparePolicyMappings($state, $cert); |
|
162 | - // (c) assign working_issuer_name |
|
163 | - $state = $state->withWorkingIssuerName( |
|
164 | - $cert->tbsCertificate() |
|
165 | - ->subject()); |
|
166 | - // (d)(e)(f) |
|
167 | - $state = $this->_setPublicKeyState($state, $cert); |
|
168 | - // (g) if name constraints extension is present |
|
169 | - $state = $this->_prepareNameConstraints($state, $cert); |
|
170 | - // (h) if certificate is not self-issued |
|
171 | - if (!$cert->isSelfIssued()) { |
|
172 | - $state = $this->_prepareNonSelfIssued($state); |
|
173 | - } |
|
174 | - // (i) if policy constraints extension is present |
|
175 | - $state = $this->_preparePolicyConstraints($state, $cert); |
|
176 | - // (j) if inhibit any policy extension is present |
|
177 | - $state = $this->_prepareInhibitAnyPolicy($state, $cert); |
|
178 | - // (k) check basic constraints |
|
179 | - $this->_processBasicContraints($cert); |
|
180 | - // (l) verify max_path_length |
|
181 | - $state = $this->_verifyMaxPathLength($state, $cert); |
|
182 | - // (m) check pathLenContraint |
|
183 | - $state = $this->_processPathLengthContraint($state, $cert); |
|
184 | - // (n) check key usage |
|
185 | - $this->_checkKeyUsage($cert); |
|
186 | - // (o) process relevant extensions |
|
187 | - $state = $this->_processExtensions($state, $cert); |
|
188 | - return $state; |
|
189 | - } |
|
149 | + /** |
|
150 | + * Apply preparation for the certificate i+1 according to rfc5280 section |
|
151 | + * 6.1.4. |
|
152 | + * |
|
153 | + * @link https://tools.ietf.org/html/rfc5280#section-6.1.4 |
|
154 | + * @param ValidatorState $state |
|
155 | + * @param Certificate $cert |
|
156 | + * @return ValidatorState |
|
157 | + */ |
|
158 | + private function _prepareNext(ValidatorState $state, Certificate $cert) |
|
159 | + { |
|
160 | + // (a)(b) if policy mappings extension is present |
|
161 | + $state = $this->_preparePolicyMappings($state, $cert); |
|
162 | + // (c) assign working_issuer_name |
|
163 | + $state = $state->withWorkingIssuerName( |
|
164 | + $cert->tbsCertificate() |
|
165 | + ->subject()); |
|
166 | + // (d)(e)(f) |
|
167 | + $state = $this->_setPublicKeyState($state, $cert); |
|
168 | + // (g) if name constraints extension is present |
|
169 | + $state = $this->_prepareNameConstraints($state, $cert); |
|
170 | + // (h) if certificate is not self-issued |
|
171 | + if (!$cert->isSelfIssued()) { |
|
172 | + $state = $this->_prepareNonSelfIssued($state); |
|
173 | + } |
|
174 | + // (i) if policy constraints extension is present |
|
175 | + $state = $this->_preparePolicyConstraints($state, $cert); |
|
176 | + // (j) if inhibit any policy extension is present |
|
177 | + $state = $this->_prepareInhibitAnyPolicy($state, $cert); |
|
178 | + // (k) check basic constraints |
|
179 | + $this->_processBasicContraints($cert); |
|
180 | + // (l) verify max_path_length |
|
181 | + $state = $this->_verifyMaxPathLength($state, $cert); |
|
182 | + // (m) check pathLenContraint |
|
183 | + $state = $this->_processPathLengthContraint($state, $cert); |
|
184 | + // (n) check key usage |
|
185 | + $this->_checkKeyUsage($cert); |
|
186 | + // (o) process relevant extensions |
|
187 | + $state = $this->_processExtensions($state, $cert); |
|
188 | + return $state; |
|
189 | + } |
|
190 | 190 | |
191 | - /** |
|
192 | - * Apply wrap-up procedure according to RFC 5280 section 6.1.5. |
|
193 | - * |
|
194 | - * @link https://tools.ietf.org/html/rfc5280#section-6.1.5 |
|
195 | - * @param ValidatorState $state |
|
196 | - * @param Certificate $cert |
|
197 | - * @throws PathValidationException |
|
198 | - */ |
|
199 | - private function _wrapUp(ValidatorState $state, Certificate $cert) |
|
200 | - { |
|
201 | - $tbs_cert = $cert->tbsCertificate(); |
|
202 | - $extensions = $tbs_cert->extensions(); |
|
203 | - // (a) |
|
204 | - if ($state->explicitPolicy() > 0) { |
|
205 | - $state = $state->withExplicitPolicy($state->explicitPolicy() - 1); |
|
206 | - } |
|
207 | - // (b) |
|
208 | - if ($extensions->hasPolicyConstraints()) { |
|
209 | - $ext = $extensions->policyConstraints(); |
|
210 | - if ($ext->hasRequireExplicitPolicy() && |
|
211 | - $ext->requireExplicitPolicy() == 0) { |
|
212 | - $state = $state->withExplicitPolicy(0); |
|
213 | - } |
|
214 | - } |
|
215 | - // (c)(d)(e) |
|
216 | - $state = $this->_setPublicKeyState($state, $cert); |
|
217 | - // (f) process relevant extensions |
|
218 | - $state = $this->_processExtensions($state, $cert); |
|
219 | - // (g) intersection of valid_policy_tree and the initial-policy-set |
|
220 | - $state = $this->_calculatePolicyIntersection($state); |
|
221 | - // check that explicit_policy > 0 or valid_policy_tree is set |
|
222 | - if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) { |
|
223 | - throw new PathValidationException("No valid policies."); |
|
224 | - } |
|
225 | - // path validation succeeded |
|
226 | - return $state; |
|
227 | - } |
|
191 | + /** |
|
192 | + * Apply wrap-up procedure according to RFC 5280 section 6.1.5. |
|
193 | + * |
|
194 | + * @link https://tools.ietf.org/html/rfc5280#section-6.1.5 |
|
195 | + * @param ValidatorState $state |
|
196 | + * @param Certificate $cert |
|
197 | + * @throws PathValidationException |
|
198 | + */ |
|
199 | + private function _wrapUp(ValidatorState $state, Certificate $cert) |
|
200 | + { |
|
201 | + $tbs_cert = $cert->tbsCertificate(); |
|
202 | + $extensions = $tbs_cert->extensions(); |
|
203 | + // (a) |
|
204 | + if ($state->explicitPolicy() > 0) { |
|
205 | + $state = $state->withExplicitPolicy($state->explicitPolicy() - 1); |
|
206 | + } |
|
207 | + // (b) |
|
208 | + if ($extensions->hasPolicyConstraints()) { |
|
209 | + $ext = $extensions->policyConstraints(); |
|
210 | + if ($ext->hasRequireExplicitPolicy() && |
|
211 | + $ext->requireExplicitPolicy() == 0) { |
|
212 | + $state = $state->withExplicitPolicy(0); |
|
213 | + } |
|
214 | + } |
|
215 | + // (c)(d)(e) |
|
216 | + $state = $this->_setPublicKeyState($state, $cert); |
|
217 | + // (f) process relevant extensions |
|
218 | + $state = $this->_processExtensions($state, $cert); |
|
219 | + // (g) intersection of valid_policy_tree and the initial-policy-set |
|
220 | + $state = $this->_calculatePolicyIntersection($state); |
|
221 | + // check that explicit_policy > 0 or valid_policy_tree is set |
|
222 | + if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) { |
|
223 | + throw new PathValidationException("No valid policies."); |
|
224 | + } |
|
225 | + // path validation succeeded |
|
226 | + return $state; |
|
227 | + } |
|
228 | 228 | |
229 | - /** |
|
230 | - * Update working_public_key, working_public_key_parameters and |
|
231 | - * working_public_key_algorithm state variables from certificate. |
|
232 | - * |
|
233 | - * @param ValidatorState $state |
|
234 | - * @param Certificate $cert |
|
235 | - * @return ValidatorState |
|
236 | - */ |
|
237 | - private function _setPublicKeyState(ValidatorState $state, Certificate $cert) |
|
238 | - { |
|
239 | - $pk_info = $cert->tbsCertificate()->subjectPublicKeyInfo(); |
|
240 | - // assign working_public_key |
|
241 | - $state = $state->withWorkingPublicKey($pk_info); |
|
242 | - // assign working_public_key_parameters |
|
243 | - $params = ValidatorState::getAlgorithmParameters( |
|
244 | - $pk_info->algorithmIdentifier()); |
|
245 | - if (null !== $params) { |
|
246 | - $state = $state->withWorkingPublicKeyParameters($params); |
|
247 | - } else { |
|
248 | - // if algorithms differ, set parameters to null |
|
249 | - if ($pk_info->algorithmIdentifier()->oid() !== |
|
250 | - $state->workingPublicKeyAlgorithm()->oid()) { |
|
251 | - $state = $state->withWorkingPublicKeyParameters(null); |
|
252 | - } |
|
253 | - } |
|
254 | - // assign working_public_key_algorithm |
|
255 | - $state = $state->withWorkingPublicKeyAlgorithm( |
|
256 | - $pk_info->algorithmIdentifier()); |
|
257 | - return $state; |
|
258 | - } |
|
229 | + /** |
|
230 | + * Update working_public_key, working_public_key_parameters and |
|
231 | + * working_public_key_algorithm state variables from certificate. |
|
232 | + * |
|
233 | + * @param ValidatorState $state |
|
234 | + * @param Certificate $cert |
|
235 | + * @return ValidatorState |
|
236 | + */ |
|
237 | + private function _setPublicKeyState(ValidatorState $state, Certificate $cert) |
|
238 | + { |
|
239 | + $pk_info = $cert->tbsCertificate()->subjectPublicKeyInfo(); |
|
240 | + // assign working_public_key |
|
241 | + $state = $state->withWorkingPublicKey($pk_info); |
|
242 | + // assign working_public_key_parameters |
|
243 | + $params = ValidatorState::getAlgorithmParameters( |
|
244 | + $pk_info->algorithmIdentifier()); |
|
245 | + if (null !== $params) { |
|
246 | + $state = $state->withWorkingPublicKeyParameters($params); |
|
247 | + } else { |
|
248 | + // if algorithms differ, set parameters to null |
|
249 | + if ($pk_info->algorithmIdentifier()->oid() !== |
|
250 | + $state->workingPublicKeyAlgorithm()->oid()) { |
|
251 | + $state = $state->withWorkingPublicKeyParameters(null); |
|
252 | + } |
|
253 | + } |
|
254 | + // assign working_public_key_algorithm |
|
255 | + $state = $state->withWorkingPublicKeyAlgorithm( |
|
256 | + $pk_info->algorithmIdentifier()); |
|
257 | + return $state; |
|
258 | + } |
|
259 | 259 | |
260 | - /** |
|
261 | - * Verify certificate signature. |
|
262 | - * |
|
263 | - * @param ValidatorState $state |
|
264 | - * @param Certificate $cert |
|
265 | - * @throws PathValidationException |
|
266 | - */ |
|
267 | - private function _verifySignature(ValidatorState $state, Certificate $cert) |
|
268 | - { |
|
269 | - try { |
|
270 | - $valid = $cert->verify($state->workingPublicKey(), $this->_crypto); |
|
271 | - } catch (\RuntimeException $e) { |
|
272 | - throw new PathValidationException( |
|
273 | - "Failed to verify signature: " . $e->getMessage(), 0, $e); |
|
274 | - } |
|
275 | - if (!$valid) { |
|
276 | - throw new PathValidationException( |
|
277 | - "Certificate signature doesn't match."); |
|
278 | - } |
|
279 | - } |
|
260 | + /** |
|
261 | + * Verify certificate signature. |
|
262 | + * |
|
263 | + * @param ValidatorState $state |
|
264 | + * @param Certificate $cert |
|
265 | + * @throws PathValidationException |
|
266 | + */ |
|
267 | + private function _verifySignature(ValidatorState $state, Certificate $cert) |
|
268 | + { |
|
269 | + try { |
|
270 | + $valid = $cert->verify($state->workingPublicKey(), $this->_crypto); |
|
271 | + } catch (\RuntimeException $e) { |
|
272 | + throw new PathValidationException( |
|
273 | + "Failed to verify signature: " . $e->getMessage(), 0, $e); |
|
274 | + } |
|
275 | + if (!$valid) { |
|
276 | + throw new PathValidationException( |
|
277 | + "Certificate signature doesn't match."); |
|
278 | + } |
|
279 | + } |
|
280 | 280 | |
281 | - /** |
|
282 | - * Check certificate validity. |
|
283 | - * |
|
284 | - * @param Certificate $cert |
|
285 | - * @throws PathValidationException |
|
286 | - */ |
|
287 | - private function _checkValidity(Certificate $cert) |
|
288 | - { |
|
289 | - $refdt = $this->_config->dateTime(); |
|
290 | - $validity = $cert->tbsCertificate()->validity(); |
|
291 | - if ($validity->notBefore() |
|
292 | - ->dateTime() |
|
293 | - ->diff($refdt)->invert) { |
|
294 | - throw new PathValidationException( |
|
295 | - "Certificate validity period has not started."); |
|
296 | - } |
|
297 | - if ($refdt->diff($validity->notAfter() |
|
298 | - ->dateTime())->invert) { |
|
299 | - throw new PathValidationException("Certificate has expired."); |
|
300 | - } |
|
301 | - } |
|
281 | + /** |
|
282 | + * Check certificate validity. |
|
283 | + * |
|
284 | + * @param Certificate $cert |
|
285 | + * @throws PathValidationException |
|
286 | + */ |
|
287 | + private function _checkValidity(Certificate $cert) |
|
288 | + { |
|
289 | + $refdt = $this->_config->dateTime(); |
|
290 | + $validity = $cert->tbsCertificate()->validity(); |
|
291 | + if ($validity->notBefore() |
|
292 | + ->dateTime() |
|
293 | + ->diff($refdt)->invert) { |
|
294 | + throw new PathValidationException( |
|
295 | + "Certificate validity period has not started."); |
|
296 | + } |
|
297 | + if ($refdt->diff($validity->notAfter() |
|
298 | + ->dateTime())->invert) { |
|
299 | + throw new PathValidationException("Certificate has expired."); |
|
300 | + } |
|
301 | + } |
|
302 | 302 | |
303 | - /** |
|
304 | - * Check certificate revocation. |
|
305 | - * |
|
306 | - * @param Certificate $cert |
|
307 | - */ |
|
308 | - private function _checkRevocation(Certificate $cert) |
|
309 | - { |
|
310 | - // @todo Implement CRL handling |
|
311 | - } |
|
303 | + /** |
|
304 | + * Check certificate revocation. |
|
305 | + * |
|
306 | + * @param Certificate $cert |
|
307 | + */ |
|
308 | + private function _checkRevocation(Certificate $cert) |
|
309 | + { |
|
310 | + // @todo Implement CRL handling |
|
311 | + } |
|
312 | 312 | |
313 | - /** |
|
314 | - * Check certificate issuer. |
|
315 | - * |
|
316 | - * @param ValidatorState $state |
|
317 | - * @param Certificate $cert |
|
318 | - * @throws PathValidationException |
|
319 | - */ |
|
320 | - private function _checkIssuer(ValidatorState $state, Certificate $cert) |
|
321 | - { |
|
322 | - if (!$cert->tbsCertificate() |
|
323 | - ->issuer() |
|
324 | - ->equals($state->workingIssuerName())) { |
|
325 | - throw new PathValidationException("Certification issuer mismatch."); |
|
326 | - } |
|
327 | - } |
|
313 | + /** |
|
314 | + * Check certificate issuer. |
|
315 | + * |
|
316 | + * @param ValidatorState $state |
|
317 | + * @param Certificate $cert |
|
318 | + * @throws PathValidationException |
|
319 | + */ |
|
320 | + private function _checkIssuer(ValidatorState $state, Certificate $cert) |
|
321 | + { |
|
322 | + if (!$cert->tbsCertificate() |
|
323 | + ->issuer() |
|
324 | + ->equals($state->workingIssuerName())) { |
|
325 | + throw new PathValidationException("Certification issuer mismatch."); |
|
326 | + } |
|
327 | + } |
|
328 | 328 | |
329 | - /** |
|
330 | - * |
|
331 | - * @param ValidatorState $state |
|
332 | - * @param Certificate $cert |
|
333 | - */ |
|
334 | - private function _checkPermittedSubtrees(ValidatorState $state, |
|
335 | - Certificate $cert) |
|
336 | - { |
|
337 | - // @todo Implement |
|
338 | - $state->permittedSubtrees(); |
|
339 | - } |
|
329 | + /** |
|
330 | + * |
|
331 | + * @param ValidatorState $state |
|
332 | + * @param Certificate $cert |
|
333 | + */ |
|
334 | + private function _checkPermittedSubtrees(ValidatorState $state, |
|
335 | + Certificate $cert) |
|
336 | + { |
|
337 | + // @todo Implement |
|
338 | + $state->permittedSubtrees(); |
|
339 | + } |
|
340 | 340 | |
341 | - /** |
|
342 | - * |
|
343 | - * @param ValidatorState $state |
|
344 | - * @param Certificate $cert |
|
345 | - */ |
|
346 | - private function _checkExcludedSubtrees(ValidatorState $state, |
|
347 | - Certificate $cert) |
|
348 | - { |
|
349 | - // @todo Implement |
|
350 | - $state->excludedSubtrees(); |
|
351 | - } |
|
341 | + /** |
|
342 | + * |
|
343 | + * @param ValidatorState $state |
|
344 | + * @param Certificate $cert |
|
345 | + */ |
|
346 | + private function _checkExcludedSubtrees(ValidatorState $state, |
|
347 | + Certificate $cert) |
|
348 | + { |
|
349 | + // @todo Implement |
|
350 | + $state->excludedSubtrees(); |
|
351 | + } |
|
352 | 352 | |
353 | - /** |
|
354 | - * Apply policy mappings handling for the preparation step. |
|
355 | - * |
|
356 | - * @param ValidatorState $state |
|
357 | - * @param Certificate $cert |
|
358 | - * @throws PathValidationException |
|
359 | - * @return ValidatorState |
|
360 | - */ |
|
361 | - private function _preparePolicyMappings(ValidatorState $state, |
|
362 | - Certificate $cert) |
|
363 | - { |
|
364 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
365 | - if ($extensions->hasPolicyMappings()) { |
|
366 | - // (a) verify that anyPolicy mapping is not used |
|
367 | - if ($extensions->policyMappings()->hasAnyPolicyMapping()) { |
|
368 | - throw new PathValidationException("anyPolicy mapping found."); |
|
369 | - } |
|
370 | - // (b) process policy mappings |
|
371 | - if ($state->hasValidPolicyTree()) { |
|
372 | - $state = $state->validPolicyTree()->processMappings($state, |
|
373 | - $cert); |
|
374 | - } |
|
375 | - } |
|
376 | - return $state; |
|
377 | - } |
|
353 | + /** |
|
354 | + * Apply policy mappings handling for the preparation step. |
|
355 | + * |
|
356 | + * @param ValidatorState $state |
|
357 | + * @param Certificate $cert |
|
358 | + * @throws PathValidationException |
|
359 | + * @return ValidatorState |
|
360 | + */ |
|
361 | + private function _preparePolicyMappings(ValidatorState $state, |
|
362 | + Certificate $cert) |
|
363 | + { |
|
364 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
365 | + if ($extensions->hasPolicyMappings()) { |
|
366 | + // (a) verify that anyPolicy mapping is not used |
|
367 | + if ($extensions->policyMappings()->hasAnyPolicyMapping()) { |
|
368 | + throw new PathValidationException("anyPolicy mapping found."); |
|
369 | + } |
|
370 | + // (b) process policy mappings |
|
371 | + if ($state->hasValidPolicyTree()) { |
|
372 | + $state = $state->validPolicyTree()->processMappings($state, |
|
373 | + $cert); |
|
374 | + } |
|
375 | + } |
|
376 | + return $state; |
|
377 | + } |
|
378 | 378 | |
379 | - /** |
|
380 | - * Apply name constraints handling for the preparation step. |
|
381 | - * |
|
382 | - * @param ValidatorState $state |
|
383 | - * @param Certificate $cert |
|
384 | - * @return ValidatorState |
|
385 | - */ |
|
386 | - private function _prepareNameConstraints(ValidatorState $state, |
|
387 | - Certificate $cert) |
|
388 | - { |
|
389 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
390 | - if ($extensions->hasNameConstraints()) { |
|
391 | - $state = $this->_processNameConstraints($state, $cert); |
|
392 | - } |
|
393 | - return $state; |
|
394 | - } |
|
379 | + /** |
|
380 | + * Apply name constraints handling for the preparation step. |
|
381 | + * |
|
382 | + * @param ValidatorState $state |
|
383 | + * @param Certificate $cert |
|
384 | + * @return ValidatorState |
|
385 | + */ |
|
386 | + private function _prepareNameConstraints(ValidatorState $state, |
|
387 | + Certificate $cert) |
|
388 | + { |
|
389 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
390 | + if ($extensions->hasNameConstraints()) { |
|
391 | + $state = $this->_processNameConstraints($state, $cert); |
|
392 | + } |
|
393 | + return $state; |
|
394 | + } |
|
395 | 395 | |
396 | - /** |
|
397 | - * Apply preparation for a non-self-signed certificate. |
|
398 | - * |
|
399 | - * @param ValidatorState $state |
|
400 | - * @return ValidatorState |
|
401 | - */ |
|
402 | - private function _prepareNonSelfIssued(ValidatorState $state) |
|
403 | - { |
|
404 | - // (h.1) |
|
405 | - if ($state->explicitPolicy() > 0) { |
|
406 | - $state = $state->withExplicitPolicy($state->explicitPolicy() - 1); |
|
407 | - } |
|
408 | - // (h.2) |
|
409 | - if ($state->policyMapping() > 0) { |
|
410 | - $state = $state->withPolicyMapping($state->policyMapping() - 1); |
|
411 | - } |
|
412 | - // (h.3) |
|
413 | - if ($state->inhibitAnyPolicy() > 0) { |
|
414 | - $state = $state->withInhibitAnyPolicy( |
|
415 | - $state->inhibitAnyPolicy() - 1); |
|
416 | - } |
|
417 | - return $state; |
|
418 | - } |
|
396 | + /** |
|
397 | + * Apply preparation for a non-self-signed certificate. |
|
398 | + * |
|
399 | + * @param ValidatorState $state |
|
400 | + * @return ValidatorState |
|
401 | + */ |
|
402 | + private function _prepareNonSelfIssued(ValidatorState $state) |
|
403 | + { |
|
404 | + // (h.1) |
|
405 | + if ($state->explicitPolicy() > 0) { |
|
406 | + $state = $state->withExplicitPolicy($state->explicitPolicy() - 1); |
|
407 | + } |
|
408 | + // (h.2) |
|
409 | + if ($state->policyMapping() > 0) { |
|
410 | + $state = $state->withPolicyMapping($state->policyMapping() - 1); |
|
411 | + } |
|
412 | + // (h.3) |
|
413 | + if ($state->inhibitAnyPolicy() > 0) { |
|
414 | + $state = $state->withInhibitAnyPolicy( |
|
415 | + $state->inhibitAnyPolicy() - 1); |
|
416 | + } |
|
417 | + return $state; |
|
418 | + } |
|
419 | 419 | |
420 | - /** |
|
421 | - * Apply policy constraints handling for the preparation step. |
|
422 | - * |
|
423 | - * @param ValidatorState $state |
|
424 | - * @param Certificate $cert |
|
425 | - * @return ValidatorState |
|
426 | - */ |
|
427 | - private function _preparePolicyConstraints(ValidatorState $state, |
|
428 | - Certificate $cert) |
|
429 | - { |
|
430 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
431 | - if (!$extensions->hasPolicyConstraints()) { |
|
432 | - return $state; |
|
433 | - } |
|
434 | - $ext = $extensions->policyConstraints(); |
|
435 | - // (i.1) |
|
436 | - if ($ext->hasRequireExplicitPolicy() && |
|
437 | - $ext->requireExplicitPolicy() < $state->explicitPolicy()) { |
|
438 | - $state = $state->withExplicitPolicy($ext->requireExplicitPolicy()); |
|
439 | - } |
|
440 | - // (i.2) |
|
441 | - if ($ext->hasInhibitPolicyMapping() && |
|
442 | - $ext->inhibitPolicyMapping() < $state->policyMapping()) { |
|
443 | - $state = $state->withPolicyMapping($ext->inhibitPolicyMapping()); |
|
444 | - } |
|
445 | - return $state; |
|
446 | - } |
|
420 | + /** |
|
421 | + * Apply policy constraints handling for the preparation step. |
|
422 | + * |
|
423 | + * @param ValidatorState $state |
|
424 | + * @param Certificate $cert |
|
425 | + * @return ValidatorState |
|
426 | + */ |
|
427 | + private function _preparePolicyConstraints(ValidatorState $state, |
|
428 | + Certificate $cert) |
|
429 | + { |
|
430 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
431 | + if (!$extensions->hasPolicyConstraints()) { |
|
432 | + return $state; |
|
433 | + } |
|
434 | + $ext = $extensions->policyConstraints(); |
|
435 | + // (i.1) |
|
436 | + if ($ext->hasRequireExplicitPolicy() && |
|
437 | + $ext->requireExplicitPolicy() < $state->explicitPolicy()) { |
|
438 | + $state = $state->withExplicitPolicy($ext->requireExplicitPolicy()); |
|
439 | + } |
|
440 | + // (i.2) |
|
441 | + if ($ext->hasInhibitPolicyMapping() && |
|
442 | + $ext->inhibitPolicyMapping() < $state->policyMapping()) { |
|
443 | + $state = $state->withPolicyMapping($ext->inhibitPolicyMapping()); |
|
444 | + } |
|
445 | + return $state; |
|
446 | + } |
|
447 | 447 | |
448 | - /** |
|
449 | - * Apply inhibit any-policy handling for the preparation step. |
|
450 | - * |
|
451 | - * @param ValidatorState $state |
|
452 | - * @param Certificate $cert |
|
453 | - * @return ValidatorState |
|
454 | - */ |
|
455 | - private function _prepareInhibitAnyPolicy(ValidatorState $state, |
|
456 | - Certificate $cert) |
|
457 | - { |
|
458 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
459 | - if ($extensions->hasInhibitAnyPolicy()) { |
|
460 | - $ext = $extensions->inhibitAnyPolicy(); |
|
461 | - if ($ext->skipCerts() < $state->inhibitAnyPolicy()) { |
|
462 | - $state = $state->withInhibitAnyPolicy($ext->skipCerts()); |
|
463 | - } |
|
464 | - } |
|
465 | - return $state; |
|
466 | - } |
|
448 | + /** |
|
449 | + * Apply inhibit any-policy handling for the preparation step. |
|
450 | + * |
|
451 | + * @param ValidatorState $state |
|
452 | + * @param Certificate $cert |
|
453 | + * @return ValidatorState |
|
454 | + */ |
|
455 | + private function _prepareInhibitAnyPolicy(ValidatorState $state, |
|
456 | + Certificate $cert) |
|
457 | + { |
|
458 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
459 | + if ($extensions->hasInhibitAnyPolicy()) { |
|
460 | + $ext = $extensions->inhibitAnyPolicy(); |
|
461 | + if ($ext->skipCerts() < $state->inhibitAnyPolicy()) { |
|
462 | + $state = $state->withInhibitAnyPolicy($ext->skipCerts()); |
|
463 | + } |
|
464 | + } |
|
465 | + return $state; |
|
466 | + } |
|
467 | 467 | |
468 | - /** |
|
469 | - * Verify maximum certification path length for the preparation step. |
|
470 | - * |
|
471 | - * @param ValidatorState $state |
|
472 | - * @param Certificate $cert |
|
473 | - * @throws PathValidationException |
|
474 | - * @return ValidatorState |
|
475 | - */ |
|
476 | - private function _verifyMaxPathLength(ValidatorState $state, |
|
477 | - Certificate $cert): ValidatorState |
|
478 | - { |
|
479 | - if (!$cert->isSelfIssued()) { |
|
480 | - if ($state->maxPathLength() <= 0) { |
|
481 | - throw new PathValidationException( |
|
482 | - "Certification path length exceeded."); |
|
483 | - } |
|
484 | - $state = $state->withMaxPathLength($state->maxPathLength() - 1); |
|
485 | - } |
|
486 | - return $state; |
|
487 | - } |
|
468 | + /** |
|
469 | + * Verify maximum certification path length for the preparation step. |
|
470 | + * |
|
471 | + * @param ValidatorState $state |
|
472 | + * @param Certificate $cert |
|
473 | + * @throws PathValidationException |
|
474 | + * @return ValidatorState |
|
475 | + */ |
|
476 | + private function _verifyMaxPathLength(ValidatorState $state, |
|
477 | + Certificate $cert): ValidatorState |
|
478 | + { |
|
479 | + if (!$cert->isSelfIssued()) { |
|
480 | + if ($state->maxPathLength() <= 0) { |
|
481 | + throw new PathValidationException( |
|
482 | + "Certification path length exceeded."); |
|
483 | + } |
|
484 | + $state = $state->withMaxPathLength($state->maxPathLength() - 1); |
|
485 | + } |
|
486 | + return $state; |
|
487 | + } |
|
488 | 488 | |
489 | - /** |
|
490 | - * Check key usage extension for the preparation step. |
|
491 | - * |
|
492 | - * @param Certificate $cert |
|
493 | - * @throws PathValidationException |
|
494 | - */ |
|
495 | - private function _checkKeyUsage(Certificate $cert) |
|
496 | - { |
|
497 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
498 | - if ($extensions->hasKeyUsage()) { |
|
499 | - $ext = $extensions->keyUsage(); |
|
500 | - if (!$ext->isKeyCertSign()) { |
|
501 | - throw new PathValidationException("keyCertSign usage not set."); |
|
502 | - } |
|
503 | - } |
|
504 | - } |
|
489 | + /** |
|
490 | + * Check key usage extension for the preparation step. |
|
491 | + * |
|
492 | + * @param Certificate $cert |
|
493 | + * @throws PathValidationException |
|
494 | + */ |
|
495 | + private function _checkKeyUsage(Certificate $cert) |
|
496 | + { |
|
497 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
498 | + if ($extensions->hasKeyUsage()) { |
|
499 | + $ext = $extensions->keyUsage(); |
|
500 | + if (!$ext->isKeyCertSign()) { |
|
501 | + throw new PathValidationException("keyCertSign usage not set."); |
|
502 | + } |
|
503 | + } |
|
504 | + } |
|
505 | 505 | |
506 | - /** |
|
507 | - * |
|
508 | - * @param ValidatorState $state |
|
509 | - * @param Certificate $cert |
|
510 | - * @return ValidatorState |
|
511 | - */ |
|
512 | - private function _processNameConstraints(ValidatorState $state, |
|
513 | - Certificate $cert): ValidatorState |
|
514 | - { |
|
515 | - // @todo Implement |
|
516 | - return $state; |
|
517 | - } |
|
506 | + /** |
|
507 | + * |
|
508 | + * @param ValidatorState $state |
|
509 | + * @param Certificate $cert |
|
510 | + * @return ValidatorState |
|
511 | + */ |
|
512 | + private function _processNameConstraints(ValidatorState $state, |
|
513 | + Certificate $cert): ValidatorState |
|
514 | + { |
|
515 | + // @todo Implement |
|
516 | + return $state; |
|
517 | + } |
|
518 | 518 | |
519 | - /** |
|
520 | - * Process basic constraints extension. |
|
521 | - * |
|
522 | - * @param Certificate $cert |
|
523 | - * @throws PathValidationException |
|
524 | - */ |
|
525 | - private function _processBasicContraints(Certificate $cert) |
|
526 | - { |
|
527 | - if ($cert->tbsCertificate()->version() == TBSCertificate::VERSION_3) { |
|
528 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
529 | - if (!$extensions->hasBasicConstraints()) { |
|
530 | - throw new PathValidationException( |
|
531 | - "v3 certificate must have basicConstraints extension."); |
|
532 | - } |
|
533 | - // verify that cA is set to TRUE |
|
534 | - if (!$extensions->basicConstraints()->isCA()) { |
|
535 | - throw new PathValidationException( |
|
536 | - "Certificate is not a CA certificate."); |
|
537 | - } |
|
538 | - } |
|
539 | - } |
|
519 | + /** |
|
520 | + * Process basic constraints extension. |
|
521 | + * |
|
522 | + * @param Certificate $cert |
|
523 | + * @throws PathValidationException |
|
524 | + */ |
|
525 | + private function _processBasicContraints(Certificate $cert) |
|
526 | + { |
|
527 | + if ($cert->tbsCertificate()->version() == TBSCertificate::VERSION_3) { |
|
528 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
529 | + if (!$extensions->hasBasicConstraints()) { |
|
530 | + throw new PathValidationException( |
|
531 | + "v3 certificate must have basicConstraints extension."); |
|
532 | + } |
|
533 | + // verify that cA is set to TRUE |
|
534 | + if (!$extensions->basicConstraints()->isCA()) { |
|
535 | + throw new PathValidationException( |
|
536 | + "Certificate is not a CA certificate."); |
|
537 | + } |
|
538 | + } |
|
539 | + } |
|
540 | 540 | |
541 | - /** |
|
542 | - * Process pathLenConstraint. |
|
543 | - * |
|
544 | - * @param ValidatorState $state |
|
545 | - * @param Certificate $cert |
|
546 | - * @return ValidatorState |
|
547 | - */ |
|
548 | - private function _processPathLengthContraint(ValidatorState $state, |
|
549 | - Certificate $cert): ValidatorState |
|
550 | - { |
|
551 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
552 | - if ($extensions->hasBasicConstraints()) { |
|
553 | - $ext = $extensions->basicConstraints(); |
|
554 | - if ($ext->hasPathLen()) { |
|
555 | - if ($ext->pathLen() < $state->maxPathLength()) { |
|
556 | - $state = $state->withMaxPathLength($ext->pathLen()); |
|
557 | - } |
|
558 | - } |
|
559 | - } |
|
560 | - return $state; |
|
561 | - } |
|
541 | + /** |
|
542 | + * Process pathLenConstraint. |
|
543 | + * |
|
544 | + * @param ValidatorState $state |
|
545 | + * @param Certificate $cert |
|
546 | + * @return ValidatorState |
|
547 | + */ |
|
548 | + private function _processPathLengthContraint(ValidatorState $state, |
|
549 | + Certificate $cert): ValidatorState |
|
550 | + { |
|
551 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
552 | + if ($extensions->hasBasicConstraints()) { |
|
553 | + $ext = $extensions->basicConstraints(); |
|
554 | + if ($ext->hasPathLen()) { |
|
555 | + if ($ext->pathLen() < $state->maxPathLength()) { |
|
556 | + $state = $state->withMaxPathLength($ext->pathLen()); |
|
557 | + } |
|
558 | + } |
|
559 | + } |
|
560 | + return $state; |
|
561 | + } |
|
562 | 562 | |
563 | - /** |
|
564 | - * |
|
565 | - * @param ValidatorState $state |
|
566 | - * @param Certificate $cert |
|
567 | - * @return ValidatorState |
|
568 | - */ |
|
569 | - private function _processExtensions(ValidatorState $state, Certificate $cert): ValidatorState |
|
570 | - { |
|
571 | - // @todo Implement |
|
572 | - return $state; |
|
573 | - } |
|
563 | + /** |
|
564 | + * |
|
565 | + * @param ValidatorState $state |
|
566 | + * @param Certificate $cert |
|
567 | + * @return ValidatorState |
|
568 | + */ |
|
569 | + private function _processExtensions(ValidatorState $state, Certificate $cert): ValidatorState |
|
570 | + { |
|
571 | + // @todo Implement |
|
572 | + return $state; |
|
573 | + } |
|
574 | 574 | |
575 | - /** |
|
576 | - * |
|
577 | - * @param ValidatorState $state |
|
578 | - * @return ValidatorState |
|
579 | - */ |
|
580 | - private function _calculatePolicyIntersection(ValidatorState $state): ValidatorState |
|
581 | - { |
|
582 | - // (i) If the valid_policy_tree is NULL, the intersection is NULL |
|
583 | - if (!$state->hasValidPolicyTree()) { |
|
584 | - return $state; |
|
585 | - } |
|
586 | - // (ii) If the valid_policy_tree is not NULL and |
|
587 | - // the user-initial-policy-set is any-policy, the intersection |
|
588 | - // is the entire valid_policy_tree |
|
589 | - $initial_policies = $this->_config->policySet(); |
|
590 | - if (in_array(PolicyInformation::OID_ANY_POLICY, $initial_policies)) { |
|
591 | - return $state; |
|
592 | - } |
|
593 | - // (iii) If the valid_policy_tree is not NULL and the |
|
594 | - // user-initial-policy-set is not any-policy, calculate |
|
595 | - // the intersection of the valid_policy_tree and the |
|
596 | - // user-initial-policy-set as follows |
|
597 | - return $state->validPolicyTree()->calculateIntersection($state, |
|
598 | - $initial_policies); |
|
599 | - } |
|
575 | + /** |
|
576 | + * |
|
577 | + * @param ValidatorState $state |
|
578 | + * @return ValidatorState |
|
579 | + */ |
|
580 | + private function _calculatePolicyIntersection(ValidatorState $state): ValidatorState |
|
581 | + { |
|
582 | + // (i) If the valid_policy_tree is NULL, the intersection is NULL |
|
583 | + if (!$state->hasValidPolicyTree()) { |
|
584 | + return $state; |
|
585 | + } |
|
586 | + // (ii) If the valid_policy_tree is not NULL and |
|
587 | + // the user-initial-policy-set is any-policy, the intersection |
|
588 | + // is the entire valid_policy_tree |
|
589 | + $initial_policies = $this->_config->policySet(); |
|
590 | + if (in_array(PolicyInformation::OID_ANY_POLICY, $initial_policies)) { |
|
591 | + return $state; |
|
592 | + } |
|
593 | + // (iii) If the valid_policy_tree is not NULL and the |
|
594 | + // user-initial-policy-set is not any-policy, calculate |
|
595 | + // the intersection of the valid_policy_tree and the |
|
596 | + // user-initial-policy-set as follows |
|
597 | + return $state->validPolicyTree()->calculateIntersection($state, |
|
598 | + $initial_policies); |
|
599 | + } |
|
600 | 600 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types=1); |
|
3 | +declare(strict_types = 1); |
|
4 | 4 | |
5 | 5 | namespace X509\CertificationPath\PathValidation; |
6 | 6 |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types=1); |
|
3 | +declare(strict_types = 1); |
|
4 | 4 | |
5 | 5 | namespace X509\CertificationPath\PathValidation; |
6 | 6 |
@@ -16,137 +16,137 @@ |
||
16 | 16 | */ |
17 | 17 | class CertificationPathBuilder |
18 | 18 | { |
19 | - /** |
|
20 | - * Trust anchors. |
|
21 | - * |
|
22 | - * @var CertificateBundle |
|
23 | - */ |
|
24 | - protected $_trustList; |
|
19 | + /** |
|
20 | + * Trust anchors. |
|
21 | + * |
|
22 | + * @var CertificateBundle |
|
23 | + */ |
|
24 | + protected $_trustList; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Constructor. |
|
28 | - * |
|
29 | - * @param CertificateBundle $trust_list List of trust anchors |
|
30 | - */ |
|
31 | - public function __construct(CertificateBundle $trust_list) |
|
32 | - { |
|
33 | - $this->_trustList = $trust_list; |
|
34 | - } |
|
26 | + /** |
|
27 | + * Constructor. |
|
28 | + * |
|
29 | + * @param CertificateBundle $trust_list List of trust anchors |
|
30 | + */ |
|
31 | + public function __construct(CertificateBundle $trust_list) |
|
32 | + { |
|
33 | + $this->_trustList = $trust_list; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * Get all certification paths to given target certificate from |
|
38 | - * any trust anchor. |
|
39 | - * |
|
40 | - * @param Certificate $target Target certificate |
|
41 | - * @param CertificateBundle|null $intermediate Optional intermediate |
|
42 | - * certificates |
|
43 | - * @return CertificationPath[] |
|
44 | - */ |
|
45 | - public function allPathsToTarget(Certificate $target, |
|
46 | - CertificateBundle $intermediate = null): array |
|
47 | - { |
|
48 | - $paths = $this->_resolvePathsToTarget($target, $intermediate); |
|
49 | - // map paths to CertificationPath objects |
|
50 | - return array_map( |
|
51 | - function ($certs) { |
|
52 | - return new CertificationPath(...$certs); |
|
53 | - }, $paths); |
|
54 | - } |
|
36 | + /** |
|
37 | + * Get all certification paths to given target certificate from |
|
38 | + * any trust anchor. |
|
39 | + * |
|
40 | + * @param Certificate $target Target certificate |
|
41 | + * @param CertificateBundle|null $intermediate Optional intermediate |
|
42 | + * certificates |
|
43 | + * @return CertificationPath[] |
|
44 | + */ |
|
45 | + public function allPathsToTarget(Certificate $target, |
|
46 | + CertificateBundle $intermediate = null): array |
|
47 | + { |
|
48 | + $paths = $this->_resolvePathsToTarget($target, $intermediate); |
|
49 | + // map paths to CertificationPath objects |
|
50 | + return array_map( |
|
51 | + function ($certs) { |
|
52 | + return new CertificationPath(...$certs); |
|
53 | + }, $paths); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Resolve all possible certification paths from any trust anchor to |
|
58 | - * the target certificate, using optional intermediate certificates. |
|
59 | - * |
|
60 | - * Helper method for allPathsToTarget to be called recursively. |
|
61 | - * |
|
62 | - * @todo Implement loop detection |
|
63 | - * @param Certificate $target |
|
64 | - * @param CertificateBundle $intermediate |
|
65 | - * @return array[] Array of arrays containing path certificates |
|
66 | - */ |
|
67 | - private function _resolvePathsToTarget(Certificate $target, |
|
68 | - CertificateBundle $intermediate = null): array |
|
69 | - { |
|
70 | - // array of possible paths |
|
71 | - $paths = array(); |
|
72 | - // signed by certificate in the trust list |
|
73 | - foreach ($this->_findIssuers($target, $this->_trustList) as $issuer) { |
|
74 | - // if target is self-signed, path consists of only |
|
75 | - // the target certificate |
|
76 | - if ($target->equals($issuer)) { |
|
77 | - $paths[] = array($target); |
|
78 | - } else { |
|
79 | - $paths[] = array($issuer, $target); |
|
80 | - } |
|
81 | - } |
|
82 | - if (isset($intermediate)) { |
|
83 | - // signed by intermediate certificate |
|
84 | - foreach ($this->_findIssuers($target, $intermediate) as $issuer) { |
|
85 | - // intermediate certificate must not be self-signed |
|
86 | - if ($issuer->isSelfIssued()) { |
|
87 | - continue; |
|
88 | - } |
|
89 | - // resolve paths to issuer |
|
90 | - $subpaths = $this->_resolvePathsToTarget($issuer, $intermediate); |
|
91 | - foreach ($subpaths as $path) { |
|
92 | - $paths[] = array_merge($path, array($target)); |
|
93 | - } |
|
94 | - } |
|
95 | - } |
|
96 | - return $paths; |
|
97 | - } |
|
56 | + /** |
|
57 | + * Resolve all possible certification paths from any trust anchor to |
|
58 | + * the target certificate, using optional intermediate certificates. |
|
59 | + * |
|
60 | + * Helper method for allPathsToTarget to be called recursively. |
|
61 | + * |
|
62 | + * @todo Implement loop detection |
|
63 | + * @param Certificate $target |
|
64 | + * @param CertificateBundle $intermediate |
|
65 | + * @return array[] Array of arrays containing path certificates |
|
66 | + */ |
|
67 | + private function _resolvePathsToTarget(Certificate $target, |
|
68 | + CertificateBundle $intermediate = null): array |
|
69 | + { |
|
70 | + // array of possible paths |
|
71 | + $paths = array(); |
|
72 | + // signed by certificate in the trust list |
|
73 | + foreach ($this->_findIssuers($target, $this->_trustList) as $issuer) { |
|
74 | + // if target is self-signed, path consists of only |
|
75 | + // the target certificate |
|
76 | + if ($target->equals($issuer)) { |
|
77 | + $paths[] = array($target); |
|
78 | + } else { |
|
79 | + $paths[] = array($issuer, $target); |
|
80 | + } |
|
81 | + } |
|
82 | + if (isset($intermediate)) { |
|
83 | + // signed by intermediate certificate |
|
84 | + foreach ($this->_findIssuers($target, $intermediate) as $issuer) { |
|
85 | + // intermediate certificate must not be self-signed |
|
86 | + if ($issuer->isSelfIssued()) { |
|
87 | + continue; |
|
88 | + } |
|
89 | + // resolve paths to issuer |
|
90 | + $subpaths = $this->_resolvePathsToTarget($issuer, $intermediate); |
|
91 | + foreach ($subpaths as $path) { |
|
92 | + $paths[] = array_merge($path, array($target)); |
|
93 | + } |
|
94 | + } |
|
95 | + } |
|
96 | + return $paths; |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * Get shortest path to given target certificate from any trust anchor. |
|
101 | - * |
|
102 | - * @param Certificate $target Target certificate |
|
103 | - * @param CertificateBundle|null $intermediate Optional intermediate |
|
104 | - * certificates |
|
105 | - * @throws PathBuildingException |
|
106 | - * @return CertificationPath |
|
107 | - */ |
|
108 | - public function shortestPathToTarget(Certificate $target, |
|
109 | - CertificateBundle $intermediate = null): CertificationPath |
|
110 | - { |
|
111 | - $paths = $this->allPathsToTarget($target, $intermediate); |
|
112 | - if (!count($paths)) { |
|
113 | - throw new PathBuildingException("No certification paths."); |
|
114 | - } |
|
115 | - usort($paths, |
|
116 | - function ($a, $b) { |
|
117 | - return count($a) < count($b) ? -1 : 1; |
|
118 | - }); |
|
119 | - return reset($paths); |
|
120 | - } |
|
99 | + /** |
|
100 | + * Get shortest path to given target certificate from any trust anchor. |
|
101 | + * |
|
102 | + * @param Certificate $target Target certificate |
|
103 | + * @param CertificateBundle|null $intermediate Optional intermediate |
|
104 | + * certificates |
|
105 | + * @throws PathBuildingException |
|
106 | + * @return CertificationPath |
|
107 | + */ |
|
108 | + public function shortestPathToTarget(Certificate $target, |
|
109 | + CertificateBundle $intermediate = null): CertificationPath |
|
110 | + { |
|
111 | + $paths = $this->allPathsToTarget($target, $intermediate); |
|
112 | + if (!count($paths)) { |
|
113 | + throw new PathBuildingException("No certification paths."); |
|
114 | + } |
|
115 | + usort($paths, |
|
116 | + function ($a, $b) { |
|
117 | + return count($a) < count($b) ? -1 : 1; |
|
118 | + }); |
|
119 | + return reset($paths); |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * Find all issuers of the target certificate from a given bundle. |
|
124 | - * |
|
125 | - * @param Certificate $target Target certificate |
|
126 | - * @param CertificateBundle $bundle Certificates to search |
|
127 | - * @return Certificate[] |
|
128 | - */ |
|
129 | - protected function _findIssuers(Certificate $target, |
|
130 | - CertificateBundle $bundle): array |
|
131 | - { |
|
132 | - $issuers = array(); |
|
133 | - $issuer_name = $target->tbsCertificate()->issuer(); |
|
134 | - $extensions = $target->tbsCertificate()->extensions(); |
|
135 | - // find by authority key identifier |
|
136 | - if ($extensions->hasAuthorityKeyIdentifier()) { |
|
137 | - $ext = $extensions->authorityKeyIdentifier(); |
|
138 | - if ($ext->hasKeyIdentifier()) { |
|
139 | - foreach ($bundle->allBySubjectKeyIdentifier( |
|
140 | - $ext->keyIdentifier()) as $issuer) { |
|
141 | - // check that issuer name matches |
|
142 | - if ($issuer->tbsCertificate() |
|
143 | - ->subject() |
|
144 | - ->equals($issuer_name)) { |
|
145 | - $issuers[] = $issuer; |
|
146 | - } |
|
147 | - } |
|
148 | - } |
|
149 | - } |
|
150 | - return $issuers; |
|
151 | - } |
|
122 | + /** |
|
123 | + * Find all issuers of the target certificate from a given bundle. |
|
124 | + * |
|
125 | + * @param Certificate $target Target certificate |
|
126 | + * @param CertificateBundle $bundle Certificates to search |
|
127 | + * @return Certificate[] |
|
128 | + */ |
|
129 | + protected function _findIssuers(Certificate $target, |
|
130 | + CertificateBundle $bundle): array |
|
131 | + { |
|
132 | + $issuers = array(); |
|
133 | + $issuer_name = $target->tbsCertificate()->issuer(); |
|
134 | + $extensions = $target->tbsCertificate()->extensions(); |
|
135 | + // find by authority key identifier |
|
136 | + if ($extensions->hasAuthorityKeyIdentifier()) { |
|
137 | + $ext = $extensions->authorityKeyIdentifier(); |
|
138 | + if ($ext->hasKeyIdentifier()) { |
|
139 | + foreach ($bundle->allBySubjectKeyIdentifier( |
|
140 | + $ext->keyIdentifier()) as $issuer) { |
|
141 | + // check that issuer name matches |
|
142 | + if ($issuer->tbsCertificate() |
|
143 | + ->subject() |
|
144 | + ->equals($issuer_name)) { |
|
145 | + $issuers[] = $issuer; |
|
146 | + } |
|
147 | + } |
|
148 | + } |
|
149 | + } |
|
150 | + return $issuers; |
|
151 | + } |
|
152 | 152 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types=1); |
|
3 | +declare(strict_types = 1); |
|
4 | 4 | |
5 | 5 | namespace X509\CertificationPath\PathBuilding; |
6 | 6 | |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | $paths = $this->_resolvePathsToTarget($target, $intermediate); |
49 | 49 | // map paths to CertificationPath objects |
50 | 50 | return array_map( |
51 | - function ($certs) { |
|
51 | + function($certs) { |
|
52 | 52 | return new CertificationPath(...$certs); |
53 | 53 | }, $paths); |
54 | 54 | } |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | throw new PathBuildingException("No certification paths."); |
114 | 114 | } |
115 | 115 | usort($paths, |
116 | - function ($a, $b) { |
|
116 | + function($a, $b) { |
|
117 | 117 | return count($a) < count($b) ? -1 : 1; |
118 | 118 | }); |
119 | 119 | return reset($paths); |