1 | <?php |
||
24 | */ |
||
25 | class CertificationPath implements \Countable, \IteratorAggregate |
||
26 | { |
||
27 | /** |
||
28 | * Certification path. |
||
29 | * |
||
30 | * @var Certificate[] |
||
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 | 38 | public function __construct(Certificate ...$certificates) |
|
41 | { |
||
42 | 38 | $this->_certificates = $certificates; |
|
43 | 38 | } |
|
44 | |||
45 | /** |
||
46 | * Initialize from a certificate chain. |
||
47 | * |
||
48 | * @param CertificateChain $chain |
||
49 | * |
||
50 | * @return self |
||
51 | */ |
||
52 | 2 | public static function fromCertificateChain(CertificateChain $chain): self |
|
53 | { |
||
54 | 2 | return new self(...array_reverse($chain->certificates(), false)); |
|
55 | } |
||
56 | |||
57 | /** |
||
58 | * Build certification path to given target. |
||
59 | * |
||
60 | * @param Certificate $target Target end-entity certificate |
||
61 | * @param CertificateBundle $trust_anchors List of trust anchors |
||
62 | * @param null|CertificateBundle $intermediate Optional intermediate certificates |
||
63 | * |
||
64 | * @return self |
||
65 | */ |
||
66 | 2 | public static function toTarget(Certificate $target, |
|
67 | CertificateBundle $trust_anchors, ?CertificateBundle $intermediate = null): self |
||
68 | { |
||
69 | 2 | $builder = new CertificationPathBuilder($trust_anchors); |
|
70 | 2 | return $builder->shortestPathToTarget($target, $intermediate); |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Build certification path from given trust anchor to target certificate, |
||
75 | * using intermediate certificates from given bundle. |
||
76 | * |
||
77 | * @param Certificate $trust_anchor Trust anchor certificate |
||
78 | * @param Certificate $target Target end-entity certificate |
||
79 | * @param null|CertificateBundle $intermediate Optional intermediate certificates |
||
80 | * |
||
81 | * @return self |
||
82 | */ |
||
83 | 2 | public static function fromTrustAnchorToTarget(Certificate $trust_anchor, |
|
84 | Certificate $target, ?CertificateBundle $intermediate = null): self |
||
85 | { |
||
86 | 2 | return self::toTarget($target, new CertificateBundle($trust_anchor), |
|
87 | 2 | $intermediate); |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Get certificates. |
||
92 | * |
||
93 | * @return Certificate[] |
||
94 | */ |
||
95 | 5 | public function certificates(): array |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * Get the trust anchor certificate from the path. |
||
102 | * |
||
103 | * @throws \LogicException If path is empty |
||
104 | * |
||
105 | * @return Certificate |
||
106 | */ |
||
107 | 2 | public function trustAnchorCertificate(): Certificate |
|
108 | { |
||
109 | 2 | if (!count($this->_certificates)) { |
|
110 | 1 | throw new \LogicException('No certificates.'); |
|
111 | } |
||
112 | 1 | return $this->_certificates[0]; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * Get the end-entity certificate from the path. |
||
117 | * |
||
118 | * @throws \LogicException If path is empty |
||
119 | * |
||
120 | * @return Certificate |
||
121 | */ |
||
122 | 2 | public function endEntityCertificate(): Certificate |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * Get certification path as a certificate chain. |
||
132 | * |
||
133 | * @return CertificateChain |
||
134 | */ |
||
135 | 1 | public function certificateChain(): CertificateChain |
|
136 | { |
||
137 | 1 | return new CertificateChain(...array_reverse($this->_certificates, false)); |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * Check whether certification path starts with one ore more given |
||
142 | * certificates in parameter order. |
||
143 | * |
||
144 | * @param Certificate ...$certs Certificates |
||
145 | * |
||
146 | * @return true |
||
147 | */ |
||
148 | 5 | public function startsWith(Certificate ...$certs): bool |
|
149 | { |
||
150 | 5 | $n = count($certs); |
|
151 | 5 | if ($n > count($this->_certificates)) { |
|
152 | 1 | return false; |
|
|
|||
153 | } |
||
154 | 4 | for ($i = 0; $i < $n; ++$i) { |
|
155 | 4 | if (!$certs[$i]->equals($this->_certificates[$i])) { |
|
156 | 1 | return false; |
|
157 | } |
||
158 | } |
||
159 | 3 | return true; |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * Validate certification path. |
||
164 | * |
||
165 | * @param PathValidationConfig $config |
||
166 | * @param null|Crypto $crypto Crypto engine, use default if not set |
||
167 | * |
||
168 | * @throws Exception\PathValidationException |
||
169 | * |
||
170 | * @return PathValidationResult |
||
171 | */ |
||
172 | 43 | public function validate(PathValidationConfig $config, |
|
173 | ?Crypto $crypto = null): PathValidationResult |
||
174 | { |
||
175 | 43 | $crypto = $crypto ?? Crypto::getDefault(); |
|
176 | 43 | $validator = new PathValidator($crypto, $config, ...$this->_certificates); |
|
177 | 43 | return $validator->validate(); |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * @see \Countable::count() |
||
182 | * |
||
183 | * @return int |
||
184 | */ |
||
185 | 18 | public function count(): int |
|
188 | } |
||
189 | |||
190 | /** |
||
191 | * Get iterator for certificates. |
||
192 | * |
||
193 | * @see \IteratorAggregate::getIterator() |
||
194 | * |
||
195 | * @return \ArrayIterator |
||
202 |