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 ( 07b53b...5f38b4 )
by Joni
04:47
created

CertificationPath::certificateChain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace X509\CertificationPath;
4
5
use CryptoUtil\Crypto\Crypto;
6
use X509\Certificate\Certificate;
7
use X509\Certificate\CertificateBundle;
8
use X509\Certificate\CertificateChain;
9
use X509\CertificationPath\Exception\PathValidationException;
10
use X509\CertificationPath\PathBuilding\CertificationPathBuilder;
11
use X509\CertificationPath\PathValidation\PathValidationConfig;
12
use X509\CertificationPath\PathValidation\PathValidationResult;
13
use X509\CertificationPath\PathValidation\PathValidator;
14
15
16
/**
17
 * Implements certification path structure.
18
 *
19
 * Certification path is a list of certificates from the trust anchor to
20
 * the end entity certificate, possibly spanning over multiple intermediate
21
 * certificates.
22
 *
23
 * @link https://tools.ietf.org/html/rfc5280#section-3.2
24
 */
25
class CertificationPath implements \Countable, \IteratorAggregate
26
{
27
	/**
28
	 * Certification path.
29
	 *
30
	 * @var Certificate[] $_certificates
31
	 */
32
	protected $_certificates;
33
	
34
	/**
35
	 * Constructor.
36
	 *
37
	 * @param Certificate ...$certificates Certificates from the trust anchor
38
	 *        to the target end-entity certificate
39
	 */
40 30
	public function __construct(Certificate ...$certificates) {
41 30
		$this->_certificates = $certificates;
42 30
	}
43
	
44
	/**
45
	 * Initialize from a certificate chain.
46
	 *
47
	 * @param CertificateChain $chain
48
	 * @return self
49
	 */
50 2
	public static function fromCertificateChain(CertificateChain $chain) {
51 2
		return new self(...array_reverse($chain->certificates(), false));
52
	}
53
	
54
	/**
55
	 * Build certification path to given target.
56
	 *
57
	 * @param Certificate $target Target end-entity certificate
58
	 * @param CertificateBundle $trust_anchors List of trust anchors
59
	 * @param CertificateBundle|null $intermediate Optional intermediate
60
	 *        certificates
61
	 * @return self
62
	 */
63 2
	public static function toTarget(Certificate $target, 
64
			CertificateBundle $trust_anchors, 
65
			CertificateBundle $intermediate = null) {
66 2
		$builder = new CertificationPathBuilder($trust_anchors);
67 2
		return $builder->shortestPathToTarget($target, $intermediate);
68
	}
69
	
70
	/**
71
	 * Build certification path from given trust anchor to target certificate,
72
	 * using intermediate certificates from given bundle.
73
	 *
74
	 * @param Certificate $trust_anchor Trust anchor certificate
75
	 * @param Certificate $target Target end-entity certificate
76
	 * @param CertificateBundle|null $intermediate Optional intermediate
77
	 *        certificates
78
	 * @return self
79
	 */
80 2
	public static function fromTrustAnchorToTarget(Certificate $trust_anchor, 
81
			Certificate $target, CertificateBundle $intermediate = null) {
82 2
		return self::toTarget($target, new CertificateBundle($trust_anchor), 
83 2
			$intermediate);
84
	}
85
	
86
	/**
87
	 * Get certificates.
88
	 *
89
	 * @return Certificate[]
90
	 */
91 5
	public function certificates() {
92 5
		return $this->_certificates;
93
	}
94
	
95
	/**
96
	 * Get the trust anchor certificate from the path.
97
	 *
98
	 * @throws \LogicException If path is empty
99
	 * @return Certificate
100
	 */
101 2
	public function trustAnchorCertificate() {
102 2
		if (!count($this->_certificates)) {
103 1
			throw new \LogicException("No certificates.");
104
		}
105 1
		return $this->_certificates[0];
106
	}
107
	
108
	/**
109
	 * Get the end-entity certificate from the path.
110
	 *
111
	 * @throws \LogicException If path is empty
112
	 * @return Certificate
113
	 */
114 2
	public function endEntityCertificate() {
115 2
		if (!count($this->_certificates)) {
116 1
			throw new \LogicException("No certificates.");
117
		}
118 1
		return $this->_certificates[count($this->_certificates) - 1];
119
	}
120
	
121
	/**
122
	 * Get certification path as a certificate chain.
123
	 *
124
	 * @return CertificateChain
125
	 */
126 1
	public function certificateChain() {
127 1
		return new CertificateChain(
128 1
			...array_reverse($this->_certificates, false));
129
	}
130
	
131
	/**
132
	 * Check whether certification path starts with one ore more given
133
	 * certificates in parameter order.
134
	 *
135
	 * @param Certificate ...$certs Certificates
136
	 * @return true
137
	 */
138 5
	public function startsWith(Certificate ...$certs) {
139 5
		$n = count($certs);
140 5
		if ($n > count($this->_certificates)) {
141 1
			return false;
142
		}
143 4
		for ($i = 0; $i < $n; ++$i) {
144 4
			if (!$certs[$i]->equals($this->_certificates[$i])) {
145 1
				return false;
146
			}
147 3
		}
148 3
		return true;
149
	}
150
	
151
	/**
152
	 * Validate certification path.
153
	 *
154
	 * @param Crypto $crypto
155
	 * @param PathValidationConfig $config
156
	 * @throws PathValidationException
157
	 * @return PathValidationResult
158
	 */
159 35
	public function validate(Crypto $crypto, PathValidationConfig $config) {
160 35
		$validator = new PathValidator($crypto, $config, ...$this->_certificates);
161 35
		return $validator->validate();
162
	}
163
	
164
	/**
165
	 *
166
	 * @see Countable::count()
167
	 * @return int
168
	 */
169 18
	public function count() {
170 18
		return count($this->_certificates);
171
	}
172
	
173
	/**
174
	 * Get iterator for certificates.
175
	 *
176
	 * @see IteratorAggregate::getIterator()
177
	 * @return \ArrayIterator
178
	 */
179 1
	public function getIterator() {
180 1
		return new \ArrayIterator($this->_certificates);
181
	}
182
}
183