GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 96d802...e5ec4d )
by Joni
04:39
created

PathValidator::_verifySignature()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
nc 2
cc 2
eloc 4
nop 2
crap 2
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 26
	public function __construct(Crypto $crypto, PathValidationConfig $config, 
55
			Certificate ...$certificates) {
56 26
		if (!count($certificates)) {
57 1
			throw new \LogicException("No certificates.");
58
		}
59 25
		$this->_crypto = $crypto;
60 25
		$this->_config = $config;
61 25
		$this->_certificates = $certificates;
62
		// if trust anchor is explicitly given in configuration
63 25
		if ($config->hasTrustAnchor()) {
64 1
			$this->_trustAnchor = $config->trustAnchor();
65 1
		} else {
66 24
			$this->_trustAnchor = $certificates[0];
67
		}
68 25
	}
69
	
70
	/**
71
	 * Validate certification path.
72
	 *
73
	 * @throws PathValidationException
74
	 * @return PathValidationResult
75
	 */
76 25
	public function validate() {
77 25
		$n = count($this->_certificates);
78 25
		$state = ValidatorState::initialize($this->_config, $this->_trustAnchor, 
79 25
			$n);
80 25
		for ($i = 0; $i < $n; ++$i) {
81
			// whether processing final certificate
82 24
			$state = $state->withIsFinal($i === $n - 1);
83 24
			$cert = $this->_certificates[$i];
84
			// process certificate (section 6.1.3.)
85 24
			$state = $this->_processCertificate($state, $cert);
86 23
			if (!$state->isFinal()) {
87
				// prepare next certificate (section 6.1.4.)
88 23
				$state = $this->_prepareNext($state, $cert);
89 19
			}
90 19
		}
91 14
		if (!isset($cert)) {
92 1
			throw new \LogicException("No certificates.");
93
		}
94
		// wrap-up (section 6.1.5.)
95 13
		$this->_wrapUp($state, $cert);
96
		// return outputs
97 12
		return new PathValidationResult($cert, $state->validPolicyTree(), 
98 12
			$state->workingPublicKey(), $state->workingPublicKeyAlgorithm(), 
99 12
			$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 24
	private function _processCertificate(ValidatorState $state, 
112
			Certificate $cert) {
113
		// (a.1) verify signature
114 24
		$this->_verifySignature($state, $cert);
115
		// (a.2) check validity period
116 24
		$this->_checkValidity($cert);
117
		// (a.3) check that certificate is not revoked
118 23
		$this->_checkRevocation($cert);
0 ignored issues
show
Unused Code introduced by
The call to the method X509\CertificationPath\P...tor::_checkRevocation() seems un-needed as the method has no side-effects.

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:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

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:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

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:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
119
		// (a.4) check issuer
120 23
		$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 23
		if (!($cert->isSelfIssued() && !$state->isFinal())) {
124
			// (b) check permitted subtrees
125 16
			$this->_checkPermittedSubtrees($state, $cert);
0 ignored issues
show
Unused Code introduced by
The call to the method X509\CertificationPath\P...heckPermittedSubtrees() seems un-needed as the method has no side-effects.

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:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

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:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

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:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
126
			// (c) check excluded subtrees
127 16
			$this->_checkExcludedSubtrees($state, $cert);
0 ignored issues
show
Unused Code introduced by
The call to the method X509\CertificationPath\P...checkExcludedSubtrees() seems un-needed as the method has no side-effects.

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:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

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:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

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:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
128 16
		}
129 23
		$extensions = $cert->tbsCertificate()->extensions();
130 23
		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 19
			$state = $state->withValidPolicyTree(null);
139
		}
140
		// (f) check that explicit_policy > 0 or valid_policy_tree is set
141 23
		if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) {
142 1
			throw new PathValidationException("Policy error.");
143
		}
144 23
		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 23
	private function _prepareNext(ValidatorState $state, Certificate $cert) {
157
		// (a)(b) if policy mappings extension is present
158 23
		$state = $this->_preparePolicyMappings($state, $cert);
159
		// (c) assign working_issuer_name
160 22
		$state = $state->withWorkingIssuerName(
161 22
			$cert->tbsCertificate()
162 22
				->subject());
163
		// (d)(e)(f)
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
164 22
		$state = $this->_setPublicKeyState($state, $cert);
165
		// (g) if name constraints extension is present
166 22
		$state = $this->_prepareNameConstraints($state, $cert);
167
		// (h) if certificate is not self-issued
168 22
		if (!$cert->isSelfIssued()) {
169 7
			$state = $this->_prepareNonSelfIssued($state);
170 7
		}
171
		// (i) if policy constraints extension is present
172 22
		$state = $this->_preparePolicyConstraints($state, $cert);
173
		// (j) if inhibit any policy extension is present
174 22
		$state = $this->_prepareInhibitAnyPolicy($state, $cert);
175
		// (k) check basic constraints
176 22
		$this->_processBasicContraints($cert);
177
		// (l) verify max_path_length
178 20
		$state = $this->_verifyMaxPathLength($state, $cert);
179
		// (m) check pathLenContraint
180 20
		$state = $this->_processPathLengthContraint($state, $cert);
181
		// (n) check key usage
182 20
		$this->_checkKeyUsage($cert);
183
		// (o) process relevant extensions
184 19
		$state = $this->_processExtensions($state, $cert);
185 19
		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 13
	private function _wrapUp(ValidatorState $state, Certificate $cert) {
197 13
		$tbs_cert = $cert->tbsCertificate();
198 13
		$extensions = $tbs_cert->extensions();
199
		// (a)
200 13
		if ($state->explicitPolicy() > 0) {
201 10
			$state = $state->withExplicitPolicy($state->explicitPolicy() - 1);
202 10
		}
203
		// (b)
204 13
		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)
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
212 13
		$state = $this->_setPublicKeyState($state, $cert);
213
		// (f) process relevant extensions
214 13
		$state = $this->_processExtensions($state, $cert);
215
		// (g) intersection of valid_policy_tree and the initial-policy-set
216 13
		$state = $this->_calculatePolicyIntersection($state);
217
		// check that explicit_policy > 0 or valid_policy_tree is set
218 13
		if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) {
219 1
			throw new PathValidationException("Policy error.");
220
		}
221
		// path validation succeeded
222 12
	}
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 22
	private function _setPublicKeyState(ValidatorState $state, Certificate $cert) {
233 22
		$pk_info = $cert->tbsCertificate()->subjectPublicKeyInfo();
234
		// assign working_public_key
235 22
		$state = $state->withWorkingPublicKey($pk_info);
236
		// assign working_public_key_parameters
237 22
		$params = ValidatorState::getAlgorithmParameters(
238 22
			$pk_info->algorithmIdentifier());
239 22
		if (null !== $params) {
240 22
			$state = $state->withWorkingPublicKeyParameters($params);
241 22
		} 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 22
		$state = $state->withWorkingPublicKeyAlgorithm(
250 22
			$pk_info->algorithmIdentifier());
251 22
		return $state;
252
	}
253
	
254
	/**
255
	 * Verify certificate signature.
256
	 *
257
	 * @param ValidatorState $state
258
	 * @param Certificate $cert
259
	 * @throws PathValidationException
260
	 */
261 24
	private function _verifySignature(ValidatorState $state, Certificate $cert) {
262 24
		if (!$cert->verify($this->_crypto, $state->workingPublicKey())) {
263 1
			throw new PathValidationException(
264 1
				"Certificate signature doesn't match.");
265
		}
266 24
	}
267
	
268
	/**
269
	 * Check certificate validity.
270
	 *
271
	 * @param Certificate $cert
272
	 * @throws PathValidationException
273
	 */
274 24
	private function _checkValidity(Certificate $cert) {
275 24
		$refdt = $this->_config->dateTime();
276 24
		$validity = $cert->tbsCertificate()->validity();
277 24
		if ($validity->notBefore()
278 24
			->dateTime()
279 24
			->diff($refdt)->invert) {
280 1
			throw new PathValidationException(
281 1
				"Certificate validity period has not started.");
282
		}
283 23
		if ($refdt->diff($validity->notAfter()
284 23
			->dateTime())->invert) {
285 1
			throw new PathValidationException("Certificate has expired.");
286
		}
287 23
	}
288
	
289
	/**
290
	 * Check certificate revocation.
291
	 *
292
	 * @param Certificate $cert
293
	 */
294 23
	private function _checkRevocation(Certificate $cert) {
0 ignored issues
show
Unused Code introduced by
The parameter $cert is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
295
		// @todo Implement CRL handling
296 23
	}
297
	
298
	/**
299
	 * Check certificate issuer.
300
	 *
301
	 * @param ValidatorState $state
302
	 * @param Certificate $cert
303
	 * @throws PathValidationException
304
	 */
305 23
	private function _checkIssuer(ValidatorState $state, Certificate $cert) {
306 23
		if (!$cert->tbsCertificate()
307 23
			->issuer()
308 23
			->equals($state->workingIssuerName())) {
0 ignored issues
show
Documentation introduced by
$state->workingIssuerName() is of type object<X501\ASN1\Name>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
309 1
			throw new PathValidationException("Certification issuer mismatch.");
310
		}
311 23
	}
312
	
313
	/**
314
	 *
315
	 * @param ValidatorState $state
316
	 * @param Certificate $cert
317
	 */
318 16
	private function _checkPermittedSubtrees(ValidatorState $state, 
319
			Certificate $cert) {
0 ignored issues
show
Unused Code introduced by
The parameter $cert is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
320
		// @todo Implement
321 16
		$state->permittedSubtrees();
0 ignored issues
show
Unused Code introduced by
The call to the method X509\CertificationPath\P...te::permittedSubtrees() seems un-needed as the method has no side-effects.

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:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

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:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

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:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
322 16
	}
323
	
324
	/**
325
	 *
326
	 * @param ValidatorState $state
327
	 * @param Certificate $cert
328
	 */
329 16
	private function _checkExcludedSubtrees(ValidatorState $state, 
330
			Certificate $cert) {
0 ignored issues
show
Unused Code introduced by
The parameter $cert is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
331
		// @todo Implement
332 16
		$state->excludedSubtrees();
0 ignored issues
show
Unused Code introduced by
The call to the method X509\CertificationPath\P...ate::excludedSubtrees() seems un-needed as the method has no side-effects.

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:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

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:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

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:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
333 16
	}
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) {
0 ignored issues
show
Unused Code introduced by
The parameter $cert is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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 23
	private function _preparePolicyMappings(ValidatorState $state, 
356
			Certificate $cert) {
357 23
		$extensions = $cert->tbsCertificate()->extensions();
358 23
		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 22
		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 22
	private function _prepareNameConstraints(ValidatorState $state, 
377
			Certificate $cert) {
378 22
		$extensions = $cert->tbsCertificate()->extensions();
379 22
		if ($extensions->hasNameConstraints()) {
380 1
			$state = $this->_processNameConstraints($state, $cert);
381 1
		}
382 22
		return $state;
383
	}
384
	
385
	/**
386
	 * Apply preparation for a non-self-signed certificate.
387
	 *
388
	 * @param ValidatorState $state
389
	 * @return ValidatorState
390
	 */
391 7
	private function _prepareNonSelfIssued(ValidatorState $state) {
392
		// (h.1)
393 7
		if ($state->explicitPolicy() > 0) {
394 6
			$state = $state->withExplicitPolicy($state->explicitPolicy() - 1);
395 6
		}
396
		// (h.2)
397 7
		if ($state->policyMapping() > 0) {
398 6
			$state = $state->withPolicyMapping($state->policyMapping() - 1);
399 6
		}
400
		// (h.3)
401 7
		if ($state->inhibitAnyPolicy() > 0) {
402 7
			$state = $state->withInhibitAnyPolicy(
403 7
				$state->inhibitAnyPolicy() - 1);
404 7
		}
405 7
		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 22
	private function _preparePolicyConstraints(ValidatorState $state, 
416
			Certificate $cert) {
417 22
		$extensions = $cert->tbsCertificate()->extensions();
418 22
		if (!$extensions->hasPolicyConstraints()) {
419 21
			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 22
	private function _prepareInhibitAnyPolicy(ValidatorState $state, 
443
			Certificate $cert) {
444 22
		$extensions = $cert->tbsCertificate()->extensions();
445 22
		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 22
		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 20
	private function _verifyMaxPathLength(ValidatorState $state, 
463
			Certificate $cert) {
464 20
		if (!$cert->isSelfIssued()) {
465 7
			if ($state->maxPathLength() <= 0) {
466 2
				throw new PathValidationException(
467 2
					"Certification path length exceeded.");
468
			}
469 5
			$state = $state->withMaxPathLength($state->maxPathLength() - 1);
470 5
		}
471 20
		return $state;
472
	}
473
	
474
	/**
475
	 * Check key usage extension for the preparation step.
476
	 *
477
	 * @param Certificate $cert
478
	 * @throws PathValidationException
479
	 */
480 20
	private function _checkKeyUsage(Certificate $cert) {
481 20
		$extensions = $cert->tbsCertificate()->extensions();
482 20
		if ($extensions->hasKeyUsage()) {
483 8
			$ext = $extensions->keyUsage();
484 8
			if (!$ext->isKeyCertSign()) {
485 1
				throw new PathValidationException("keyCertSign usage not set.");
486
			}
487 7
		}
488 19
	}
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) {
0 ignored issues
show
Unused Code introduced by
The parameter $cert is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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 22
	private function _processBasicContraints(Certificate $cert) {
509 22
		if ($cert->tbsCertificate()->version() == TBSCertificate::VERSION_3) {
510 19
			$extensions = $cert->tbsCertificate()->extensions();
511 19
			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 18
			if (!$extensions->basicConstraints()->isCA()) {
517 1
				throw new PathValidationException(
518 1
					"Certificate is not a CA certificate.");
519
			}
520 17
		}
521 20
	}
522
	
523
	/**
524
	 * Process pathLenConstraint.
525
	 *
526
	 * @param ValidatorState $state
527
	 * @param Certificate $cert
528
	 * @return ValidatorState
529
	 */
530 20
	private function _processPathLengthContraint(ValidatorState $state, 
531
			Certificate $cert) {
532 20
		$extensions = $cert->tbsCertificate()->extensions();
533 20
		if ($extensions->hasBasicConstraints()) {
534 17
			$ext = $extensions->basicConstraints();
535 17
			if ($ext->hasPathLen()) {
536 13
				if ($ext->pathLen() < $state->maxPathLength()) {
537 8
					$state = $state->withMaxPathLength($ext->pathLen());
538 8
				}
539 13
			}
540 17
		}
541 20
		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) {
0 ignored issues
show
Unused Code introduced by
The parameter $cert is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
553
		// @todo Implement
554 2
		return $state;
555
	}
556
	
557
	/**
558
	 *
559
	 * @param ValidatorState $state
560
	 * @param Certificate $cert
561
	 * @return ValidatorState
562
	 */
563 19
	private function _processExtensions(ValidatorState $state, Certificate $cert) {
0 ignored issues
show
Unused Code introduced by
The parameter $cert is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
564
		// @todo Implement
565 19
		return $state;
566
	}
567
	
568
	/**
569
	 *
570
	 * @param ValidatorState $state
571
	 * @return ValidatorState
572
	 */
573 13
	private function _calculatePolicyIntersection(ValidatorState $state) {
574
		// @todo Implement
575 13
		return $state;
576
	}
577
}
578