Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X501/ASN1/Name.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X501\ASN1;
6
7 use ArrayIterator;
8 use function count;
9 use Countable;
10 use IteratorAggregate;
11 use RangeException;
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\X501\ASN1\AttributeValue\AttributeValue;
16 use SpomkyLabs\Pki\X501\DN\DNParser;
17 use Stringable;
18
19 /**
20 * Implements *Name* ASN.1 type.
21 *
22 * Since *Name* is a CHOICE only supporting *RDNSequence* type, this class implements *RDNSequence* semantics as well.
23 *
24 * @see https://www.itu.int/ITU-T/formal-language/itu-t/x/x501/2012/InformationFramework.html#InformationFramework.Name
25 */
26 final class Name implements Countable, IteratorAggregate, Stringable
27 {
28 /**
29 * Relative distinguished name components.
30 *
31 * @var RDN[]
32 */
33 private readonly array $rdns;
34
35 /**
36 * @param RDN ...$rdns RDN components
37 */
38 private function __construct(RDN ...$rdns)
39 {
40 $this->rdns = $rdns;
41 }
42
43 public function __toString(): string
44 {
45 return $this->toString();
46 }
47
48 public static function create(RDN ...$rdns): self
49 {
50 return new self(...$rdns);
51 }
52
53 /**
54 * Initialize from ASN.1.
55 */
56 public static function fromASN1(Sequence $seq): self
57 {
58 $rdns = array_map(static fn (UnspecifiedType $el) => RDN::fromASN1($el->asSet()), $seq->elements());
59 return self::create(...$rdns);
60 }
61
62 /**
63 * Initialize from distinguished name string.
64 *
65 * @see https://tools.ietf.org/html/rfc1779
66 */
67 public static function fromString(string $str): self
68 {
69 $rdns = [];
70 foreach (DNParser::parseString($str) as $nameComponent) {
71 $attribs = [];
72 foreach ($nameComponent as [$name, $val]) {
73 $type = AttributeType::fromName($name);
74 // hexstrings are parsed to ASN.1 elements
75 if ($val instanceof Element) {
76 $el = $val;
77 } else {
78 $el = AttributeType::asn1StringForType($type->oid(), $val);
79 }
80 $value = AttributeValue::fromASN1ByOID($type->oid(), $el->asUnspecified());
81 $attribs[] = AttributeTypeAndValue::create($type, $value);
82 }
83 $rdns[] = RDN::create(...$attribs);
84 }
85 return self::create(...$rdns);
86 }
87
88 /**
89 * Generate ASN.1 structure.
90 */
91 public function toASN1(): Sequence
92 {
93 $elements = array_map(static fn (RDN $rdn) => $rdn->toASN1(), $this->rdns);
94 return Sequence::create(...$elements);
95 }
96
97 /**
98 * Get distinguised name string conforming to RFC 2253.
99 *
100 * @see https://tools.ietf.org/html/rfc2253#section-2.1
101 */
102 public function toString(): string
103 {
104 $parts = array_map(static fn (RDN $rdn) => $rdn->toString(), array_reverse($this->rdns));
105 return implode(',', $parts);
106 }
107
108 /**
109 * Whether name is semantically equal to other.
110 *
111 * Comparison conforms to RFC 4518 string preparation algorithm.
112 *
113 * @see https://tools.ietf.org/html/rfc4518
114 *
115 * @param Name $other Object to compare to
116 */
117 public function equals(self $other): bool
118 {
119 // if RDN count doesn't match
120 if (count($this) !== count($other)) {
121 return false;
122 }
123 for ($i = count($this) - 1; $i >= 0; --$i) {
124 $rdn1 = $this->rdns[$i];
125 $rdn2 = $other->rdns[$i];
126 if (! $rdn1->equals($rdn2)) {
127 return false;
128 }
129 }
130 return true;
131 }
132
133 /**
134 * Get all RDN objects.
135 *
136 * @return RDN[]
137 */
138 public function all(): array
139 {
140 return $this->rdns;
141 }
142
143 /**
144 * Get the first AttributeValue of given type.
145 *
146 * Relative name components shall be traversed in encoding order, which is reversed in regards to the string
147 * representation. Multi-valued RDN with multiple attributes of the requested type is ambiguous and shall throw an
148 * exception.
149 *
150 * @param string $name Attribute OID or name
151 */
152 public function firstValueOf(string $name): AttributeValue
153 {
154 $oid = AttributeType::attrNameToOID($name);
155 foreach ($this->rdns as $rdn) {
156 $tvs = $rdn->allOf($oid);
157 if (count($tvs) > 1) {
158 throw new RangeException("RDN with multiple {$name} attributes.");
159 }
160 if (count($tvs) === 1) {
161 return $tvs[0]->value();
162 }
163 }
164 throw new RangeException("Attribute {$name} not found.");
165 }
166
167 /**
168 * @see \Countable::count()
169 */
170 public function count(): int
171 {
172 return count($this->rdns);
173 }
174
175 /**
176 * Get the number of attributes of given type.
177 *
178 * @param string $name Attribute OID or name
179 */
180 public function countOfType(string $name): int
181 {
182 $oid = AttributeType::attrNameToOID($name);
183 return array_sum(array_map(static fn (RDN $rdn): int => count($rdn->allOf($oid)), $this->rdns));
184 }
185
186 /**
187 * @see \IteratorAggregate::getIterator()
188 */
189 public function getIterator(): ArrayIterator
190 {
191 return new ArrayIterator($this->rdns);
192 }
193 }