1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace X509\CertificationPath\PathValidation; |
4
|
|
|
|
5
|
|
|
use CryptoUtil\Crypto\Crypto; |
6
|
|
|
use X509\Certificate\Certificate; |
7
|
|
|
use X509\Certificate\Extension\Extension; |
8
|
|
|
use X509\Certificate\TBSCertificate; |
9
|
|
|
use X509\CertificationPath\Exception\PathValidationException; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Implements certification path validation. |
14
|
|
|
* |
15
|
|
|
* @link https://tools.ietf.org/html/rfc5280#section-6 |
16
|
|
|
*/ |
17
|
|
|
class PathValidator |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Crypto engine. |
21
|
|
|
* |
22
|
|
|
* @var Crypto $_crypto |
23
|
|
|
*/ |
24
|
|
|
protected $_crypto; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Path validation configuration. |
28
|
|
|
* |
29
|
|
|
* @var PathValidationConfig $_config |
30
|
|
|
*/ |
31
|
|
|
protected $_config; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Certification path. |
35
|
|
|
* |
36
|
|
|
* @var Certificate[] $_certificates |
37
|
|
|
*/ |
38
|
|
|
protected $_certificates; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Certification path trust anchor. |
42
|
|
|
* |
43
|
|
|
* @var Certificate $_trustAnchor |
44
|
|
|
*/ |
45
|
|
|
protected $_trustAnchor; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Constructor |
49
|
|
|
* |
50
|
|
|
* @param Crypto $crypto |
51
|
|
|
* @param PathValidationConfig $config |
52
|
|
|
* @param Certificate ...$certificates |
53
|
|
|
*/ |
54
|
23 |
|
public function __construct(Crypto $crypto, PathValidationConfig $config, |
55
|
|
|
Certificate ...$certificates) { |
56
|
23 |
|
if (!count($certificates)) { |
57
|
|
|
throw new \LogicException("No certificates."); |
58
|
|
|
} |
59
|
23 |
|
$this->_crypto = $crypto; |
60
|
23 |
|
$this->_config = $config; |
61
|
23 |
|
$this->_certificates = $certificates; |
62
|
|
|
// if trust anchor is explicitly given in configuration |
63
|
23 |
|
if ($config->hasTrustAnchor()) { |
64
|
|
|
$this->_trustAnchor = $config->trustAnchor(); |
65
|
|
|
} else { |
66
|
23 |
|
$this->_trustAnchor = $certificates[0]; |
67
|
|
|
} |
68
|
23 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Validate certification path. |
72
|
|
|
* |
73
|
|
|
* @throws PathValidationException |
74
|
|
|
* @return PathValidationResult |
75
|
|
|
*/ |
76
|
23 |
|
public function validate() { |
77
|
23 |
|
$n = count($this->_certificates); |
78
|
23 |
|
$state = ValidatorState::initialize($this->_config, $this->_trustAnchor, |
79
|
23 |
|
$n); |
80
|
23 |
|
for ($i = 0; $i < $n; ++$i) { |
81
|
|
|
// whether processing final certificate |
82
|
23 |
|
$state = $state->withIsFinal($i === $n - 1); |
83
|
23 |
|
$cert = $this->_certificates[$i]; |
84
|
|
|
// process certificate (section 6.1.3.) |
85
|
23 |
|
$state = $this->_processCertificate($state, $cert); |
86
|
22 |
|
if (!$state->isFinal()) { |
87
|
|
|
// prepare next certificate (section 6.1.4.) |
88
|
22 |
|
$state = $this->_prepareNext($state, $cert); |
89
|
18 |
|
} |
90
|
18 |
|
} |
91
|
12 |
|
if (!isset($cert)) { |
92
|
|
|
throw new \LogicException("No certificates."); |
93
|
|
|
} |
94
|
|
|
// wrap-up (section 6.1.5.) |
95
|
12 |
|
$this->_wrapUp($state, $cert); |
96
|
|
|
// return outputs |
97
|
11 |
|
return new PathValidationResult($cert, $state->validPolicyTree(), |
98
|
11 |
|
$state->workingPublicKey(), $state->workingPublicKeyAlgorithm(), |
99
|
11 |
|
$state->workingPublicKeyParameters()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Apply basic certificate processing according to RFC 5280 section 6.1.3. |
104
|
|
|
* |
105
|
|
|
* @link https://tools.ietf.org/html/rfc5280#section-6.1.3 |
106
|
|
|
* @param ValidatorState $state |
107
|
|
|
* @param Certificate $cert |
108
|
|
|
* @throws PathValidationException |
109
|
|
|
* @return ValidatorState |
110
|
|
|
*/ |
111
|
23 |
|
private function _processCertificate(ValidatorState $state, |
112
|
|
|
Certificate $cert) { |
113
|
|
|
// (a.1) verify signature |
114
|
23 |
|
$this->_verifySignature($state, $cert); |
115
|
|
|
// (a.2) check validity period |
116
|
23 |
|
$this->_checkValidity($cert); |
117
|
|
|
// (a.3) check that certificate is not revoked |
118
|
22 |
|
$this->_checkRevocation($cert); |
|
|
|
|
119
|
|
|
// (a.4) check issuer |
120
|
22 |
|
$this->_checkIssuer($state, $cert); |
121
|
|
|
// (b)(c) if certificate is self-issued and it is not |
122
|
|
|
// the final certificate in the path, skip this step |
123
|
22 |
|
if (!($cert->isSelfIssued() && !$state->isFinal())) { |
124
|
|
|
// (b) check permitted subtrees |
125
|
15 |
|
$this->_checkPermittedSubtrees($state, $cert); |
|
|
|
|
126
|
|
|
// (c) check excluded subtrees |
127
|
15 |
|
$this->_checkExcludedSubtrees($state, $cert); |
|
|
|
|
128
|
15 |
|
} |
129
|
22 |
|
$extensions = $cert->tbsCertificate()->extensions(); |
130
|
22 |
|
if ($extensions->hasCertificatePolicies()) { |
131
|
|
|
// (d) process policy information |
132
|
7 |
|
if ($state->hasValidPolicyTree()) { |
133
|
6 |
|
$state = $this->_processPolicyInformation($state, $cert); |
134
|
6 |
|
} |
135
|
7 |
|
} else { |
136
|
|
|
// (e) certificate policies extension not present, |
137
|
|
|
// set the valid_policy_tree to NULL |
138
|
18 |
|
$state = $state->withValidPolicyTree(null); |
139
|
|
|
} |
140
|
|
|
// (f) check that explicit_policy > 0 or valid_policy_tree is set |
141
|
22 |
|
if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) { |
142
|
1 |
|
throw new PathValidationException("Policy error."); |
143
|
|
|
} |
144
|
22 |
|
return $state; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Apply preparation for the certificate i+1 according to rfc5280 section |
149
|
|
|
* 6.1.4. |
150
|
|
|
* |
151
|
|
|
* @link https://tools.ietf.org/html/rfc5280#section-6.1.4 |
152
|
|
|
* @param ValidatorState $state |
153
|
|
|
* @param Certificate $cert |
154
|
|
|
* @return ValidatorState |
155
|
|
|
*/ |
156
|
22 |
|
private function _prepareNext(ValidatorState $state, Certificate $cert) { |
157
|
22 |
|
$tbs_cert = $cert->tbsCertificate(); |
158
|
22 |
|
$extensions = $tbs_cert->extensions(); |
|
|
|
|
159
|
|
|
// (a)(b) if policy mappings extension is present |
160
|
22 |
|
$state = $this->_preparePolicyMappings($state, $cert); |
161
|
|
|
// (c) assign working_issuer_name |
162
|
21 |
|
$state = $state->withWorkingIssuerName($tbs_cert->subject()); |
163
|
|
|
// (d)(e)(f) |
|
|
|
|
164
|
21 |
|
$state = $this->_setPublicKeyState($state, $cert); |
165
|
|
|
// (g) if name constraints extension is present |
166
|
21 |
|
$state = $this->_prepareNameConstraints($state, $cert); |
167
|
|
|
// (h) if certificate is not self-issued |
168
|
21 |
|
if (!$cert->isSelfIssued()) { |
169
|
6 |
|
$state = $this->_prepareNonSelfIssued($state); |
170
|
6 |
|
} |
171
|
|
|
// (i) if policy constraints extension is present |
172
|
21 |
|
$state = $this->_preparePolicyConstraints($state, $cert); |
173
|
|
|
// (j) if inhibit any policy extension is present |
174
|
21 |
|
$state = $this->_prepareInhibitAnyPolicy($state, $cert); |
175
|
|
|
// (k) check basic constraints |
176
|
21 |
|
$this->_processBasicContraints($cert); |
177
|
|
|
// (l) verify max_path_length |
178
|
19 |
|
$state = $this->_verifyMaxPathLength($state, $cert); |
179
|
|
|
// (m) check pathLenContraint |
180
|
19 |
|
$state = $this->_processPathLengthContraint($state, $cert); |
181
|
|
|
// (n) check key usage |
182
|
19 |
|
$this->_checkKeyUsage($cert); |
183
|
|
|
// (o) process relevant extensions |
184
|
18 |
|
$state = $this->_processExtensions($state, $cert); |
185
|
18 |
|
return $state; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Apply wrap-up procedure according to RFC 5280 section 6.1.5. |
190
|
|
|
* |
191
|
|
|
* @link https://tools.ietf.org/html/rfc5280#section-6.1.5 |
192
|
|
|
* @param ValidatorState $state |
193
|
|
|
* @param Certificate $cert |
194
|
|
|
* @throws PathValidationException |
195
|
|
|
*/ |
196
|
12 |
|
private function _wrapUp(ValidatorState $state, Certificate $cert) { |
197
|
12 |
|
$tbs_cert = $cert->tbsCertificate(); |
198
|
12 |
|
$extensions = $tbs_cert->extensions(); |
199
|
|
|
// (a) |
200
|
12 |
|
if ($state->explicitPolicy() > 0) { |
201
|
9 |
|
$state = $state->withExplicitPolicy($state->explicitPolicy() - 1); |
202
|
9 |
|
} |
203
|
|
|
// (b) |
204
|
12 |
|
if ($extensions->hasPolicyConstraints()) { |
205
|
2 |
|
$ext = $extensions->policyConstraints(); |
206
|
2 |
|
if ($ext->hasRequireExplicitPolicy() && |
207
|
2 |
|
$ext->requireExplicitPolicy() == 0) { |
208
|
1 |
|
$state = $state->withExplicitPolicy(0); |
209
|
1 |
|
} |
210
|
2 |
|
} |
211
|
|
|
// (c)(d)(e) |
|
|
|
|
212
|
12 |
|
$state = $this->_setPublicKeyState($state, $cert); |
213
|
|
|
// (f) process relevant extensions |
214
|
12 |
|
$state = $this->_processExtensions($state, $cert); |
215
|
|
|
// (g) intersection of valid_policy_tree and the initial-policy-set |
216
|
12 |
|
$state = $this->_calculatePolicyIntersection($state); |
217
|
|
|
// check that explicit_policy > 0 or valid_policy_tree is set |
218
|
12 |
|
if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) { |
219
|
1 |
|
throw new PathValidationException("Policy error."); |
220
|
|
|
} |
221
|
|
|
// path validation succeeded |
222
|
11 |
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Update working_public_key, working_public_key_parameters and |
226
|
|
|
* working_public_key_algorithm state variables from certificate. |
227
|
|
|
* |
228
|
|
|
* @param ValidatorState $state |
229
|
|
|
* @param Certificate $cert |
230
|
|
|
* @return ValidatorState |
231
|
|
|
*/ |
232
|
21 |
|
private function _setPublicKeyState(ValidatorState $state, Certificate $cert) { |
233
|
21 |
|
$pk_info = $cert->tbsCertificate()->subjectPublicKeyInfo(); |
234
|
|
|
// assign working_public_key |
235
|
21 |
|
$state = $state->withWorkingPublicKey($pk_info); |
236
|
|
|
// assign working_public_key_parameters |
237
|
21 |
|
$params = ValidatorState::getAlgorithmParameters( |
238
|
21 |
|
$pk_info->algorithmIdentifier()); |
239
|
21 |
|
if (null !== $params) { |
240
|
21 |
|
$state = $state->withWorkingPublicKeyParameters($params); |
241
|
21 |
|
} else { |
242
|
|
|
// if algorithms differ, set parameters to null |
243
|
1 |
|
if ($pk_info->algorithmIdentifier()->oid() !== |
244
|
1 |
|
$state->workingPublicKeyAlgorithm()->oid()) { |
245
|
1 |
|
$state = $state->withWorkingPublicKeyParameters(null); |
246
|
1 |
|
} |
247
|
|
|
} |
248
|
|
|
// assign working_public_key_algorithm |
249
|
21 |
|
$state = $state->withWorkingPublicKeyAlgorithm( |
250
|
21 |
|
$pk_info->algorithmIdentifier()); |
251
|
21 |
|
return $state; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Verify certificate signature. |
256
|
|
|
* |
257
|
|
|
* @param ValidatorState $state |
258
|
|
|
* @param Certificate $cert |
259
|
|
|
* @throws PathValidationException |
260
|
|
|
*/ |
261
|
23 |
|
private function _verifySignature(ValidatorState $state, Certificate $cert) { |
262
|
23 |
|
if (!$cert->verify($this->_crypto, $state->workingPublicKey())) { |
263
|
1 |
|
throw new PathValidationException( |
264
|
1 |
|
"Certificate signature doesn't match."); |
265
|
|
|
} |
266
|
23 |
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Check certificate validity. |
270
|
|
|
* |
271
|
|
|
* @param Certificate $cert |
272
|
|
|
* @throws PathValidationException |
273
|
|
|
*/ |
274
|
23 |
|
private function _checkValidity(Certificate $cert) { |
275
|
23 |
|
$refdt = $this->_config->dateTime(); |
276
|
23 |
|
$validity = $cert->tbsCertificate()->validity(); |
277
|
23 |
|
if ($validity->notBefore() |
278
|
23 |
|
->dateTime() |
279
|
23 |
|
->diff($refdt)->invert) { |
280
|
1 |
|
throw new PathValidationException( |
281
|
1 |
|
"Certificate validity period has not started."); |
282
|
|
|
} |
283
|
22 |
|
if ($refdt->diff($validity->notAfter() |
284
|
22 |
|
->dateTime())->invert) { |
285
|
1 |
|
throw new PathValidationException("Certificate has expired."); |
286
|
|
|
} |
287
|
22 |
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Check certificate revocation. |
291
|
|
|
* |
292
|
|
|
* @param Certificate $cert |
293
|
|
|
*/ |
294
|
22 |
|
private function _checkRevocation(Certificate $cert) { |
|
|
|
|
295
|
|
|
// @todo Implement CRL handling |
296
|
22 |
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Check certificate issuer. |
300
|
|
|
* |
301
|
|
|
* @param ValidatorState $state |
302
|
|
|
* @param Certificate $cert |
303
|
|
|
* @throws PathValidationException |
304
|
|
|
*/ |
305
|
22 |
|
private function _checkIssuer(ValidatorState $state, Certificate $cert) { |
306
|
22 |
|
if (!$cert->tbsCertificate() |
307
|
22 |
|
->issuer() |
308
|
22 |
|
->equals($state->workingIssuerName())) { |
|
|
|
|
309
|
1 |
|
throw new PathValidationException("Certification issuer mismatch."); |
310
|
|
|
} |
311
|
22 |
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* |
315
|
|
|
* @param ValidatorState $state |
316
|
|
|
* @param Certificate $cert |
317
|
|
|
*/ |
318
|
15 |
|
private function _checkPermittedSubtrees(ValidatorState $state, |
319
|
|
|
Certificate $cert) { |
|
|
|
|
320
|
|
|
// @todo Implement |
321
|
15 |
|
$state->permittedSubtrees(); |
|
|
|
|
322
|
15 |
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* |
326
|
|
|
* @param ValidatorState $state |
327
|
|
|
* @param Certificate $cert |
328
|
|
|
*/ |
329
|
15 |
|
private function _checkExcludedSubtrees(ValidatorState $state, |
330
|
|
|
Certificate $cert) { |
|
|
|
|
331
|
|
|
// @todo Implement |
332
|
15 |
|
$state->excludedSubtrees(); |
|
|
|
|
333
|
15 |
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* |
337
|
|
|
* @param ValidatorState $state |
338
|
|
|
* @param Certificate $cert |
339
|
|
|
* @return ValidatorState |
340
|
|
|
*/ |
341
|
6 |
|
private function _processPolicyInformation(ValidatorState $state, |
342
|
|
|
Certificate $cert) { |
|
|
|
|
343
|
|
|
// @todo Implement |
344
|
6 |
|
return $state; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Apply policy mappings handling for the preparation step. |
349
|
|
|
* |
350
|
|
|
* @param ValidatorState $state |
351
|
|
|
* @param Certificate $cert |
352
|
|
|
* @throws PathValidationException |
353
|
|
|
* @return ValidatorState |
354
|
|
|
*/ |
355
|
22 |
|
private function _preparePolicyMappings(ValidatorState $state, |
356
|
|
|
Certificate $cert) { |
357
|
22 |
|
$extensions = $cert->tbsCertificate()->extensions(); |
358
|
22 |
|
if ($extensions->hasPolicyMappings()) { |
359
|
|
|
// (a) verify that anyPolicy mapping is not used |
360
|
3 |
|
if ($extensions->policyMappings()->hasAnyPolicyMapping()) { |
361
|
1 |
|
throw new PathValidationException("anyPolicy mapping found."); |
362
|
|
|
} |
363
|
|
|
// (b) process policy mappings |
364
|
2 |
|
$state = $this->_processPolicyMappings($state, $cert); |
365
|
2 |
|
} |
366
|
21 |
|
return $state; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Apply name constraints handling for the preparation step. |
371
|
|
|
* |
372
|
|
|
* @param ValidatorState $state |
373
|
|
|
* @param Certificate $cert |
374
|
|
|
* @return ValidatorState |
375
|
|
|
*/ |
376
|
21 |
|
private function _prepareNameConstraints(ValidatorState $state, |
377
|
|
|
Certificate $cert) { |
378
|
21 |
|
$extensions = $cert->tbsCertificate()->extensions(); |
379
|
21 |
|
if ($extensions->hasNameConstraints()) { |
380
|
1 |
|
$state = $this->_processNameConstraints($state, $cert); |
381
|
1 |
|
} |
382
|
21 |
|
return $state; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Apply preparation for a non-self-signed certificate. |
387
|
|
|
* |
388
|
|
|
* @param ValidatorState $state |
389
|
|
|
* @return ValidatorState |
390
|
|
|
*/ |
391
|
6 |
|
private function _prepareNonSelfIssued(ValidatorState $state) { |
392
|
|
|
// (h.1) |
393
|
6 |
|
if ($state->explicitPolicy() > 0) { |
394
|
5 |
|
$state = $state->withExplicitPolicy($state->explicitPolicy() - 1); |
395
|
5 |
|
} |
396
|
|
|
// (h.2) |
397
|
6 |
|
if ($state->policyMapping() > 0) { |
398
|
5 |
|
$state = $state->withPolicyMapping($state->policyMapping() - 1); |
399
|
5 |
|
} |
400
|
|
|
// (h.3) |
401
|
6 |
|
if ($state->inhibitAnyPolicy() > 0) { |
402
|
6 |
|
$state = $state->withInhibitAnyPolicy( |
403
|
6 |
|
$state->inhibitAnyPolicy() - 1); |
404
|
6 |
|
} |
405
|
6 |
|
return $state; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Apply policy constraints handling for the preparation step. |
410
|
|
|
* |
411
|
|
|
* @param ValidatorState $state |
412
|
|
|
* @param Certificate $cert |
413
|
|
|
* @return ValidatorState |
414
|
|
|
*/ |
415
|
21 |
|
private function _preparePolicyConstraints(ValidatorState $state, |
416
|
|
|
Certificate $cert) { |
417
|
21 |
|
$extensions = $cert->tbsCertificate()->extensions(); |
418
|
21 |
|
if (!$extensions->hasPolicyConstraints()) { |
419
|
20 |
|
return $state; |
420
|
|
|
} |
421
|
2 |
|
$ext = $extensions->policyConstraints(); |
422
|
|
|
// (i.1) |
423
|
2 |
|
if ($ext->hasRequireExplicitPolicy() && |
424
|
2 |
|
$ext->requireExplicitPolicy() < $state->explicitPolicy()) { |
425
|
2 |
|
$state = $state->withExplicitPolicy($ext->requireExplicitPolicy()); |
426
|
2 |
|
} |
427
|
|
|
// (i.2) |
428
|
2 |
|
if ($ext->hasInhibitPolicyMapping() && |
429
|
2 |
|
$ext->inhibitPolicyMapping() < $state->policyMapping()) { |
430
|
1 |
|
$state = $state->withPolicyMapping($ext->inhibitPolicyMapping()); |
431
|
1 |
|
} |
432
|
2 |
|
return $state; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Apply inhibit any-policy handling for the preparation step. |
437
|
|
|
* |
438
|
|
|
* @param ValidatorState $state |
439
|
|
|
* @param Certificate $cert |
440
|
|
|
* @return ValidatorState |
441
|
|
|
*/ |
442
|
21 |
|
private function _prepareInhibitAnyPolicy(ValidatorState $state, |
443
|
|
|
Certificate $cert) { |
444
|
21 |
|
$extensions = $cert->tbsCertificate()->extensions(); |
445
|
21 |
|
if ($extensions->hasInhibitAnyPolicy()) { |
446
|
1 |
|
$ext = $extensions->inhibitAnyPolicy(); |
447
|
1 |
|
if ($ext->skipCerts() < $state->inhibitAnyPolicy()) { |
448
|
1 |
|
$state = $state->withInhibitAnyPolicy($ext->skipCerts()); |
449
|
1 |
|
} |
450
|
1 |
|
} |
451
|
21 |
|
return $state; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Verify maximum certification path length for the preparation step. |
456
|
|
|
* |
457
|
|
|
* @param ValidatorState $state |
458
|
|
|
* @param Certificate $cert |
459
|
|
|
* @throws PathValidationException |
460
|
|
|
* @return ValidatorState |
461
|
|
|
*/ |
462
|
19 |
|
private function _verifyMaxPathLength(ValidatorState $state, |
463
|
|
|
Certificate $cert) { |
464
|
19 |
|
if (!$cert->isSelfIssued()) { |
465
|
6 |
|
if ($state->maxPathLength() <= 0) { |
466
|
2 |
|
throw new PathValidationException( |
467
|
2 |
|
"Certification path length exceeded."); |
468
|
|
|
} |
469
|
4 |
|
$state = $state->withMaxPathLength($state->maxPathLength() - 1); |
470
|
4 |
|
} |
471
|
19 |
|
return $state; |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* Check key usage extension for the preparation step. |
476
|
|
|
* |
477
|
|
|
* @param Certificate $cert |
478
|
|
|
* @throws PathValidationException |
479
|
|
|
*/ |
480
|
19 |
|
private function _checkKeyUsage(Certificate $cert) { |
481
|
19 |
|
$extensions = $cert->tbsCertificate()->extensions(); |
482
|
19 |
|
if ($extensions->hasKeyUsage()) { |
483
|
7 |
|
$ext = $extensions->keyUsage(); |
484
|
7 |
|
if (!$ext->isKeyCertSign()) { |
485
|
1 |
|
throw new PathValidationException("keyCertSign usage not set."); |
486
|
|
|
} |
487
|
6 |
|
} |
488
|
18 |
|
} |
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* |
492
|
|
|
* @param ValidatorState $state |
493
|
|
|
* @param Certificate $cert |
494
|
|
|
* @return ValidatorState |
495
|
|
|
*/ |
496
|
1 |
|
private function _processNameConstraints(ValidatorState $state, |
497
|
|
|
Certificate $cert) { |
|
|
|
|
498
|
|
|
// @todo Implement |
499
|
1 |
|
return $state; |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* Process basic constraints extension. |
504
|
|
|
* |
505
|
|
|
* @param Certificate $cert |
506
|
|
|
* @throws PathValidationException |
507
|
|
|
*/ |
508
|
21 |
|
private function _processBasicContraints(Certificate $cert) { |
509
|
21 |
|
if ($cert->tbsCertificate()->version() == TBSCertificate::VERSION_3) { |
510
|
18 |
|
$extensions = $cert->tbsCertificate()->extensions(); |
511
|
18 |
|
if (!$extensions->hasBasicConstraints()) { |
512
|
1 |
|
throw new PathValidationException( |
513
|
1 |
|
"v3 certificate must have basicConstraints extension."); |
514
|
|
|
} |
515
|
|
|
// verify that cA is set to TRUE |
516
|
17 |
|
if (!$extensions->basicConstraints()->isCA()) { |
517
|
1 |
|
throw new PathValidationException( |
518
|
1 |
|
"Certificate is not a CA certificate."); |
519
|
|
|
} |
520
|
16 |
|
} |
521
|
19 |
|
} |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* Process pathLenConstraint. |
525
|
|
|
* |
526
|
|
|
* @param ValidatorState $state |
527
|
|
|
* @param Certificate $cert |
528
|
|
|
* @return ValidatorState |
529
|
|
|
*/ |
530
|
19 |
|
private function _processPathLengthContraint(ValidatorState $state, |
531
|
|
|
Certificate $cert) { |
532
|
19 |
|
$extensions = $cert->tbsCertificate()->extensions(); |
533
|
19 |
|
if ($extensions->hasBasicConstraints()) { |
534
|
16 |
|
$ext = $extensions->basicConstraints(); |
535
|
16 |
|
if ($ext->hasPathLen()) { |
536
|
12 |
|
if ($ext->pathLen() < $state->maxPathLength()) { |
537
|
8 |
|
$state = $state->withMaxPathLength($ext->pathLen()); |
538
|
8 |
|
} |
539
|
12 |
|
} |
540
|
16 |
|
} |
541
|
19 |
|
return $state; |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* Process policy mappings extension. |
546
|
|
|
* |
547
|
|
|
* @param ValidatorState $state |
548
|
|
|
* @param Certificate $cert |
549
|
|
|
* @return ValidatorState |
550
|
|
|
*/ |
551
|
2 |
|
private function _processPolicyMappings(ValidatorState $state, |
552
|
|
|
Certificate $cert) { |
|
|
|
|
553
|
|
|
// @todo Implement |
554
|
2 |
|
return $state; |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
/** |
558
|
|
|
* |
559
|
|
|
* @param ValidatorState $state |
560
|
|
|
* @param Certificate $cert |
561
|
|
|
* @return ValidatorState |
562
|
|
|
*/ |
563
|
18 |
|
private function _processExtensions(ValidatorState $state, Certificate $cert) { |
|
|
|
|
564
|
|
|
// @todo Implement |
565
|
18 |
|
return $state; |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* |
570
|
|
|
* @param ValidatorState $state |
571
|
|
|
* @return ValidatorState |
572
|
|
|
*/ |
573
|
12 |
|
private function _calculatePolicyIntersection(ValidatorState $state) { |
574
|
|
|
// @todo Implement |
575
|
12 |
|
return $state; |
576
|
|
|
} |
577
|
|
|
} |
578
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: