Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X509/Certificate/Extensions.php

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