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.

Extensions   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 402
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 37
eloc 52
dl 0
loc 402
ccs 83
cts 83
cp 1
rs 9.44
c 0
b 0
f 0

34 Methods

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