Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\Certificate\Extension;
6
7 use LogicException;
8 use SpomkyLabs\Pki\ASN1\Element;
9 use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence;
10 use SpomkyLabs\Pki\ASN1\Type\Primitive\Boolean;
11 use SpomkyLabs\Pki\ASN1\Type\Primitive\Integer;
12 use SpomkyLabs\Pki\ASN1\Type\Primitive\ObjectIdentifier;
13 use SpomkyLabs\Pki\ASN1\Type\Tagged\ImplicitlyTaggedType;
14 use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType;
15
16 /**
17 * Implements 'AA Controls' certificate extension.
18 *
19 * @see https://tools.ietf.org/html/rfc5755#section-7.4
20 */
21 final class AAControlsExtension extends Extension
22 {
23 /**
24 * @param null|string[] $permittedAttrs
25 * @param null|string[] $excludedAttrs
26 */
27 private function __construct(
28 bool $critical,
29 private readonly ?int $pathLenConstraint,
30 private readonly ?array $permittedAttrs,
31 private readonly ?array $excludedAttrs,
32 private readonly bool $permitUnSpecified
33 ) {
34 parent::__construct(self::OID_AA_CONTROLS, $critical);
35 }
36
37 /**
38 * @param bool $critical Path length contraint.
39 * @param null|string[] $permittedAttrs Permitted attributes.
40 * @param null|string[] $excludedAttrs Excluded attributes.
41 * @param bool $permitUnSpecified Whether to permit unspecified attributes.
42 */
43 public static function create(
44 bool $critical,
45 ?int $pathLenConstraint = null,
46 ?array $permittedAttrs = null,
47 ?array $excludedAttrs = null,
48 bool $permitUnSpecified = true
49 ): self {
50 return new self($critical, $pathLenConstraint, $permittedAttrs, $excludedAttrs, $permitUnSpecified);
51 }
52
53 /**
54 * Check whether path length constraint is present.
55 */
56 public function hasPathLen(): bool
57 {
58 return isset($this->pathLenConstraint);
59 }
60
61 /**
62 * Get path length constraint.
63 */
64 public function pathLen(): int
65 {
66 if (! $this->hasPathLen()) {
67 throw new LogicException('pathLen not set.');
68 }
69 return $this->pathLenConstraint;
70 }
71
72 /**
73 * Check whether permitted attributes are present.
74 */
75 public function hasPermittedAttrs(): bool
76 {
77 return isset($this->permittedAttrs);
78 }
79
80 /**
81 * Get OID's of permitted attributes.
82 *
83 * @return string[]
84 */
85 public function permittedAttrs(): array
86 {
87 if (! $this->hasPermittedAttrs()) {
88 throw new LogicException('permittedAttrs not set.');
89 }
90 return $this->permittedAttrs;
91 }
92
93 /**
94 * Check whether excluded attributes are present.
95 */
96 public function hasExcludedAttrs(): bool
97 {
98 return isset($this->excludedAttrs);
99 }
100
101 /**
102 * Get OID's of excluded attributes.
103 *
104 * @return string[]
105 */
106 public function excludedAttrs(): array
107 {
108 if (! $this->hasExcludedAttrs()) {
109 throw new LogicException('excludedAttrs not set.');
110 }
111 return $this->excludedAttrs;
112 }
113
114 /**
115 * Whether to permit attributes that are not explicitly specified in neither permitted nor excluded list.
116 */
117 public function permitUnspecified(): bool
118 {
119 return $this->permitUnSpecified;
120 }
121
122 protected static function fromDER(string $data, bool $critical): static
123 {
124 $seq = UnspecifiedType::fromDER($data)->asSequence();
125 $path_len = null;
126 $permitted = null;
127 $excluded = null;
128 $permit_unspecified = true;
129 $idx = 0;
130 if ($seq->has($idx, Element::TYPE_INTEGER)) {
131 $path_len = $seq->at($idx++)
132 ->asInteger()
133 ->intNumber();
134 }
135 if ($seq->hasTagged(0)) {
136 $attr_seq = $seq->getTagged(0)
137 ->asImplicit(Element::TYPE_SEQUENCE)
138 ->asSequence();
139 $permitted = array_map(
140 static fn (UnspecifiedType $el) => $el->asObjectIdentifier()
141 ->oid(),
142 $attr_seq->elements()
143 );
144 ++$idx;
145 }
146 if ($seq->hasTagged(1)) {
147 $attr_seq = $seq->getTagged(1)
148 ->asImplicit(Element::TYPE_SEQUENCE)
149 ->asSequence();
150 $excluded = array_map(
151 static fn (UnspecifiedType $el) => $el->asObjectIdentifier()
152 ->oid(),
153 $attr_seq->elements()
154 );
155 ++$idx;
156 }
157 if ($seq->has($idx, Element::TYPE_BOOLEAN)) {
158 $permit_unspecified = $seq->at($idx++)
159 ->asBoolean()
160 ->value();
161 }
162 return self::create($critical, $path_len, $permitted, $excluded, $permit_unspecified);
163 }
164
165 protected function valueASN1(): Element
166 {
167 $elements = [];
168 if (isset($this->pathLenConstraint)) {
169 $elements[] = Integer::create($this->pathLenConstraint);
170 }
171 if (isset($this->permittedAttrs)) {
172 $oids = array_map(ObjectIdentifier::create(...), $this->permittedAttrs);
173 $elements[] = ImplicitlyTaggedType::create(0, Sequence::create(...$oids));
174 }
175 if (isset($this->excludedAttrs)) {
176 $oids = array_map(ObjectIdentifier::create(...), $this->excludedAttrs);
177 $elements[] = ImplicitlyTaggedType::create(1, Sequence::create(...$oids));
178 }
179 if ($this->permitUnSpecified !== true) {
180 $elements[] = Boolean::create(false);
181 }
182 return Sequence::create(...$elements);
183 }
184 }