Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\Certificate\Extension;
6
7 use ArrayIterator;
8 use function count;
9 use Countable;
10 use IteratorAggregate;
11 use LogicException;
12 use SpomkyLabs\Pki\ASN1\Element;
13 use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence;
14 use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType;
15 use SpomkyLabs\Pki\X509\Certificate\Extension\CertificatePolicy\PolicyInformation;
16 use UnexpectedValueException;
17
18 /**
19 * Implements 'Certificate Policies' certificate extension.
20 *
21 * @see https://tools.ietf.org/html/rfc5280#section-4.2.1.4
22 */
23 final class CertificatePoliciesExtension extends Extension implements Countable, IteratorAggregate
24 {
25 /**
26 * Policy information terms.
27 *
28 * @var PolicyInformation[]
29 */
30 protected array $_policies;
31
32 private function __construct(bool $critical, PolicyInformation ...$policies)
33 {
34 parent::__construct(Extension::OID_CERTIFICATE_POLICIES, $critical);
35 $this->_policies = [];
36 foreach ($policies as $policy) {
37 $this->_policies[$policy->oid()] = $policy;
38 }
39 }
40
41 public static function create(bool $critical, PolicyInformation ...$policies): self
42 {
43 return new self($critical, ...$policies);
44 }
45
46 /**
47 * Check whether policy information by OID is present.
48 */
49 public function has(string $oid): bool
50 {
51 return isset($this->_policies[$oid]);
52 }
53
54 /**
55 * Get policy information by OID.
56 */
57 public function get(string $oid): PolicyInformation
58 {
59 if (! $this->has($oid)) {
60 throw new LogicException("Not certificate policy by OID {$oid}.");
61 }
62 return $this->_policies[$oid];
63 }
64
65 /**
66 * Check whether anyPolicy is present.
67 */
68 public function hasAnyPolicy(): bool
69 {
70 return $this->has(PolicyInformation::OID_ANY_POLICY);
71 }
72
73 /**
74 * Get anyPolicy information.
75 */
76 public function anyPolicy(): PolicyInformation
77 {
78 if (! $this->hasAnyPolicy()) {
79 throw new LogicException('No anyPolicy.');
80 }
81 return $this->get(PolicyInformation::OID_ANY_POLICY);
82 }
83
84 /**
85 * Get the number of policies.
86 *
87 * @see \Countable::count()
88 */
89 public function count(): int
90 {
91 return count($this->_policies);
92 }
93
94 /**
95 * Get iterator for policy information terms.
96 *
97 * @see \IteratorAggregate::getIterator()
98 */
99 public function getIterator(): ArrayIterator
100 {
101 return new ArrayIterator($this->_policies);
102 }
103
104 protected static function fromDER(string $data, bool $critical): static
105 {
106 $policies = array_map(
107 static fn (UnspecifiedType $el) => PolicyInformation::fromASN1($el->asSequence()),
108 UnspecifiedType::fromDER($data)->asSequence()->elements()
109 );
110 if (count($policies) === 0) {
111 throw new UnexpectedValueException('certificatePolicies must contain at least one PolicyInformation.');
112 }
113 return self::create($critical, ...$policies);
114 }
115
116 protected function valueASN1(): Element
117 {
118 if (count($this->_policies) === 0) {
119 throw new LogicException('No policies.');
120 }
121 $elements = array_map(static fn (PolicyInformation $pi) => $pi->toASN1(), array_values($this->_policies));
122 return Sequence::create(...$elements);
123 }
124 }