1 | <?php |
||
19 | use Sop\X509\Certificate\Extension\PolicyConstraintsExtension; |
||
20 | use Sop\X509\Certificate\Extension\PolicyMappingsExtension; |
||
21 | use Sop\X509\Certificate\Extension\SubjectAlternativeNameExtension; |
||
22 | use Sop\X509\Certificate\Extension\SubjectKeyIdentifierExtension; |
||
23 | |||
24 | /** |
||
25 | * Implements *Extensions* ASN.1 type. |
||
26 | * |
||
27 | * Several convenience methods are provided to fetch commonly used standard extensions. |
||
28 | * Others can be accessed using `get($oid)`. |
||
29 | * |
||
30 | * @see https://tools.ietf.org/html/rfc5280#section-4.1.2.9 |
||
31 | */ |
||
32 | class Extensions implements \Countable, \IteratorAggregate |
||
33 | 65 | { |
|
34 | /** |
||
35 | 65 | * Extensions. |
|
36 | 65 | * |
|
37 | 40 | * @var Extension[] |
|
38 | */ |
||
39 | 65 | protected $_extensions; |
|
40 | |||
41 | /** |
||
42 | * Constructor. |
||
43 | * |
||
44 | * @param Extension ...$extensions Extension objects |
||
45 | */ |
||
46 | public function __construct(Extension ...$extensions) |
||
47 | 26 | { |
|
48 | $this->_extensions = []; |
||
49 | 26 | foreach ($extensions as $ext) { |
|
50 | 26 | $this->_extensions[$ext->oid()] = $ext; |
|
51 | 23 | } |
|
52 | 26 | } |
|
53 | 26 | ||
54 | /** |
||
55 | * Initialize from ASN.1. |
||
56 | * |
||
57 | * @param Sequence $seq |
||
58 | * |
||
59 | * @return self |
||
60 | */ |
||
61 | 66 | public static function fromASN1(Sequence $seq): Extensions |
|
62 | { |
||
63 | 66 | $extensions = array_map( |
|
64 | 66 | function (UnspecifiedType $el) { |
|
65 | 66 | return Extension::fromASN1($el->asSequence()); |
|
66 | 60 | }, $seq->elements()); |
|
67 | 66 | return new self(...$extensions); |
|
68 | 66 | } |
|
69 | |||
70 | /** |
||
71 | * Generate ASN.1 structure. |
||
72 | * |
||
73 | * @return Sequence |
||
74 | */ |
||
75 | public function toASN1(): Sequence |
||
76 | { |
||
77 | 5 | $elements = array_values( |
|
78 | array_map( |
||
79 | 5 | function ($ext) { |
|
80 | 5 | return $ext->toASN1(); |
|
81 | 5 | }, $this->_extensions)); |
|
82 | return new Sequence(...$elements); |
||
83 | 5 | } |
|
84 | |||
85 | /** |
||
86 | * Get self with extensions added. |
||
87 | * |
||
88 | * @param Extension ...$exts One or more extensions to add |
||
89 | * |
||
90 | * @return self |
||
91 | */ |
||
92 | 108 | public function withExtensions(Extension ...$exts): Extensions |
|
93 | { |
||
94 | 108 | $obj = clone $this; |
|
95 | foreach ($exts as $ext) { |
||
96 | $obj->_extensions[$ext->oid()] = $ext; |
||
97 | } |
||
98 | return $obj; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Check whether extension is present. |
||
103 | * |
||
104 | 86 | * @param string $oid Extensions OID |
|
105 | * |
||
106 | 86 | * @return bool |
|
107 | 1 | */ |
|
108 | public function has(string $oid): bool |
||
109 | 85 | { |
|
110 | return isset($this->_extensions[$oid]); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Get extension by OID. |
||
115 | * |
||
116 | * @param string $oid |
||
117 | 10 | * |
|
118 | * @throws \LogicException If extension is not present |
||
119 | 10 | * |
|
120 | * @return Extension |
||
121 | */ |
||
122 | public function get(string $oid): Extension |
||
123 | { |
||
124 | if (!$this->has($oid)) { |
||
125 | throw new \LogicException("No extension by OID {$oid}."); |
||
126 | } |
||
127 | return $this->_extensions[$oid]; |
||
128 | 11 | } |
|
129 | |||
130 | /** |
||
131 | 11 | * Check whether 'Authority Key Identifier' extension is present. |
|
132 | 11 | * |
|
133 | * @return bool |
||
134 | */ |
||
135 | public function hasAuthorityKeyIdentifier(): bool |
||
136 | { |
||
137 | return $this->has(Extension::OID_AUTHORITY_KEY_IDENTIFIER); |
||
138 | } |
||
139 | |||
140 | 14 | /** |
|
141 | * Get 'Authority Key Identifier' extension. |
||
142 | 14 | * |
|
143 | * @throws \LogicException If extension is not present |
||
144 | * |
||
145 | * @return AuthorityKeyIdentifierExtension |
||
146 | */ |
||
147 | public function authorityKeyIdentifier(): AuthorityKeyIdentifierExtension |
||
148 | { |
||
149 | return $this->get(Extension::OID_AUTHORITY_KEY_IDENTIFIER); |
||
150 | } |
||
151 | 13 | ||
152 | /** |
||
153 | * Check whether 'Subject Key Identifier' extension is present. |
||
154 | 13 | * |
|
155 | 13 | * @return bool |
|
156 | 13 | */ |
|
157 | public function hasSubjectKeyIdentifier(): bool |
||
158 | { |
||
159 | return $this->has(Extension::OID_SUBJECT_KEY_IDENTIFIER); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Get 'Subject Key Identifier' extension. |
||
164 | 41 | * |
|
165 | * @throws \LogicException If extension is not present |
||
166 | 41 | * |
|
167 | * @return SubjectKeyIdentifierExtension |
||
168 | */ |
||
169 | public function subjectKeyIdentifier(): SubjectKeyIdentifierExtension |
||
170 | { |
||
171 | return $this->get(Extension::OID_SUBJECT_KEY_IDENTIFIER); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | 21 | * Check whether 'Key Usage' extension is present. |
|
176 | * |
||
177 | * @return bool |
||
178 | 21 | */ |
|
179 | 21 | public function hasKeyUsage(): bool |
|
180 | { |
||
181 | return $this->has(Extension::OID_KEY_USAGE); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Get 'Key Usage' extension. |
||
186 | * |
||
187 | 44 | * @throws \LogicException If extension is not present |
|
188 | * |
||
189 | 44 | * @return KeyUsageExtension |
|
190 | */ |
||
191 | public function keyUsage(): KeyUsageExtension |
||
192 | { |
||
193 | return $this->get(Extension::OID_KEY_USAGE); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Check whether 'Certificate Policies' extension is present. |
||
198 | 16 | * |
|
199 | * @return bool |
||
200 | */ |
||
201 | 16 | public function hasCertificatePolicies(): bool |
|
204 | } |
||
205 | |||
206 | /** |
||
207 | * Get 'Certificate Policies' extension. |
||
208 | * |
||
209 | * @throws \LogicException If extension is not present |
||
210 | 44 | * |
|
211 | * @return CertificatePoliciesExtension |
||
212 | 44 | */ |
|
213 | public function certificatePolicies(): CertificatePoliciesExtension |
||
214 | { |
||
215 | return $this->get(Extension::OID_CERTIFICATE_POLICIES); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Check whether 'Policy Mappings' extension is present. |
||
220 | * |
||
221 | 5 | * @return bool |
|
222 | */ |
||
223 | public function hasPolicyMappings(): bool |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Get 'Policy Mappings' extension. |
||
230 | * |
||
231 | * @throws \LogicException If extension is not present |
||
232 | * |
||
233 | 3 | * @return PolicyMappingsExtension |
|
234 | */ |
||
235 | 3 | public function policyMappings(): PolicyMappingsExtension |
|
236 | { |
||
237 | return $this->get(Extension::OID_POLICY_MAPPINGS); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Check whether 'Subject Alternative Name' extension is present. |
||
242 | * |
||
243 | * @return bool |
||
244 | 4 | */ |
|
245 | public function hasSubjectAlternativeName(): bool |
||
246 | { |
||
247 | 4 | return $this->has(Extension::OID_SUBJECT_ALT_NAME); |
|
248 | 4 | } |
|
249 | |||
250 | /** |
||
251 | * Get 'Subject Alternative Name' extension. |
||
252 | * |
||
253 | * @throws \LogicException If extension is not present |
||
254 | * |
||
255 | * @return SubjectAlternativeNameExtension |
||
256 | 1 | */ |
|
257 | public function subjectAlternativeName(): SubjectAlternativeNameExtension |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Check whether 'Issuer Alternative Name' extension is present. |
||
264 | * |
||
265 | * @return bool |
||
266 | 1 | */ |
|
267 | public function hasIssuerAlternativeName(): bool |
||
270 | 1 | } |
|
271 | |||
272 | /** |
||
273 | * Get 'Issuer Alternative Name' extension. |
||
274 | * |
||
275 | * @return IssuerAlternativeNameExtension |
||
276 | */ |
||
277 | public function issuerAlternativeName(): IssuerAlternativeNameExtension |
||
278 | 43 | { |
|
279 | return $this->get(Extension::OID_ISSUER_ALT_NAME); |
||
280 | 43 | } |
|
281 | |||
282 | /** |
||
283 | * Check whether 'Basic Constraints' extension is present. |
||
284 | * |
||
285 | * @return bool |
||
286 | */ |
||
287 | public function hasBasicConstraints(): bool |
||
288 | { |
||
289 | 40 | return $this->has(Extension::OID_BASIC_CONSTRAINTS); |
|
290 | } |
||
291 | |||
292 | 40 | /** |
|
293 | 40 | * Get 'Basic Constraints' extension. |
|
294 | * |
||
295 | * @throws \LogicException If extension is not present |
||
296 | * |
||
297 | * @return BasicConstraintsExtension |
||
298 | */ |
||
299 | public function basicConstraints(): BasicConstraintsExtension |
||
300 | { |
||
301 | 43 | return $this->get(Extension::OID_BASIC_CONSTRAINTS); |
|
302 | } |
||
303 | 43 | ||
304 | /** |
||
305 | * Check whether 'Name Constraints' extension is present. |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | public function hasNameConstraints(): bool |
||
310 | { |
||
311 | return $this->has(Extension::OID_NAME_CONSTRAINTS); |
||
312 | 1 | } |
|
313 | |||
314 | /** |
||
315 | 1 | * Get 'Name Constraints' extension. |
|
316 | 1 | * |
|
317 | * @throws \LogicException If extension is not present |
||
318 | * |
||
319 | * @return NameConstraintsExtension |
||
320 | */ |
||
321 | public function nameConstraints(): NameConstraintsExtension |
||
322 | { |
||
323 | return $this->get(Extension::OID_NAME_CONSTRAINTS); |
||
324 | 43 | } |
|
325 | |||
326 | 43 | /** |
|
327 | * Check whether 'Policy Constraints' extension is present. |
||
328 | * |
||
329 | * @return bool |
||
330 | */ |
||
331 | public function hasPolicyConstraints(): bool |
||
334 | } |
||
335 | 16 | ||
336 | /** |
||
337 | * Get 'Policy Constraints' extension. |
||
338 | 16 | * |
|
339 | 16 | * @throws \LogicException If extension is not present |
|
340 | * |
||
341 | * @return PolicyConstraintsExtension |
||
342 | */ |
||
343 | public function policyConstraints(): PolicyConstraintsExtension |
||
344 | { |
||
345 | return $this->get(Extension::OID_POLICY_CONSTRAINTS); |
||
346 | } |
||
347 | 1 | ||
348 | /** |
||
349 | 1 | * Check whether 'Extended Key Usage' extension is present. |
|
350 | * |
||
351 | * @return bool |
||
352 | */ |
||
353 | public function hasExtendedKeyUsage(): bool |
||
354 | { |
||
355 | return $this->has(Extension::OID_EXT_KEY_USAGE); |
||
356 | } |
||
357 | |||
358 | 1 | /** |
|
359 | * Get 'Extended Key Usage' extension. |
||
360 | * |
||
361 | 1 | * @throws \LogicException If extension is not present |
|
362 | 1 | * |
|
363 | * @return ExtendedKeyUsageExtension |
||
364 | */ |
||
365 | public function extendedKeyUsage(): ExtendedKeyUsageExtension |
||
368 | } |
||
369 | |||
370 | 1 | /** |
|
371 | * Check whether 'CRL Distribution Points' extension is present. |
||
372 | 1 | * |
|
373 | * @return bool |
||
374 | */ |
||
375 | public function hasCRLDistributionPoints(): bool |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | 1 | * Get 'CRL Distribution Points' extension. |
|
382 | * |
||
383 | * @throws \LogicException If extension is not present |
||
384 | 1 | * |
|
385 | 1 | * @return CRLDistributionPointsExtension |
|
386 | */ |
||
387 | public function crlDistributionPoints(): CRLDistributionPointsExtension |
||
388 | { |
||
389 | return $this->get(Extension::OID_CRL_DISTRIBUTION_POINTS); |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | 43 | * Check whether 'Inhibit anyPolicy' extension is present. |
|
394 | * |
||
395 | 43 | * @return bool |
|
396 | */ |
||
397 | public function hasInhibitAnyPolicy(): bool |
||
398 | { |
||
399 | return $this->has(Extension::OID_INHIBIT_ANY_POLICY); |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * Get 'Inhibit anyPolicy' extension. |
||
404 | 3 | * |
|
405 | * @throws \LogicException If extension is not present |
||
406 | * |
||
407 | 3 | * @return InhibitAnyPolicyExtension |
|
408 | 3 | */ |
|
409 | public function inhibitAnyPolicy(): InhibitAnyPolicyExtension |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * @see \Countable::count() |
||
416 | 77 | * |
|
417 | * @return int |
||
418 | 77 | */ |
|
419 | public function count(): int |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * Get iterator for extensions. |
||
426 | * |
||
427 | 1 | * @see \IteratorAggregate::getIterator() |
|
428 | * |
||
429 | 1 | * @return \Traversable |
|
430 | */ |
||
431 | public function getIterator(): \Traversable |
||
432 | { |
||
436 |