Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/ASN1/Component/Identifier.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\ASN1\Component;
6
7 use function array_key_exists;
8 use Brick\Math\BigInteger;
9 use function mb_strlen;
10 use function ord;
11 use SpomkyLabs\Pki\ASN1\Exception\DecodeException;
12 use SpomkyLabs\Pki\ASN1\Feature\Encodable;
13 use SpomkyLabs\Pki\ASN1\Util\BigInt;
14
15 /**
16 * Class to represent BER/DER identifier octets.
17 * @see \SpomkyLabs\Pki\Test\ASN1\Component\IdentifierTest
18 */
19 final class Identifier implements Encodable
20 {
21 // Type class enumerations
22 final public const CLASS_UNIVERSAL = 0b00;
23
24 final public const CLASS_APPLICATION = 0b01;
25
26 final public const CLASS_CONTEXT_SPECIFIC = 0b10;
27
28 final public const CLASS_PRIVATE = 0b11;
29
30 // P/C enumerations
31 final public const PRIMITIVE = 0b0;
32
33 final public const CONSTRUCTED = 0b1;
34
35 /**
36 * Mapping from type class to human readable name.
37 *
38 * @internal
39 *
40 * @var array<int, string>
41 */
42 private const MAP_CLASS_TO_NAME = [
43 self::CLASS_UNIVERSAL => 'UNIVERSAL',
44 self::CLASS_APPLICATION => 'APPLICATION',
45 self::CLASS_CONTEXT_SPECIFIC => 'CONTEXT SPECIFIC',
46 self::CLASS_PRIVATE => 'PRIVATE',
47 ];
48
49 /**
50 * Type class.
51 */
52 private int $_class;
53
54 /**
55 * Primitive or Constructed.
56 */
57 private readonly int $_pc;
58
59 /**
60 * Content type tag.
61 */
62 private BigInt $_tag;
63
64 /**
65 * @param int $class Type class
66 * @param int $pc Primitive / Constructed
67 * @param BigInteger|int $tag Type tag number
68 */
69 private function __construct(int $class, int $pc, BigInteger|int $tag)
70 {
71 $this->_class = 0b11 & $class;
72 $this->_pc = 0b1 & $pc;
73 $this->_tag = BigInt::create($tag);
74 }
75
76 public static function create(int $class, int $pc, BigInteger|int $tag): self
77 {
78 return new self($class, $pc, $tag);
79 }
80
81 /**
82 * Decode identifier component from DER data.
83 *
84 * @param string $data DER encoded data
85 * @param null|int $offset Reference to the variable that contains offset
86 * into the data where to start parsing.
87 * Variable is updated to the offset next to the
88 * parsed identifier. If null, start from offset 0.
89 */
90 public static function fromDER(string $data, ?int &$offset = null): self
91 {
92 $idx = $offset ?? 0;
93 $datalen = mb_strlen($data, '8bit');
94 if ($idx >= $datalen) {
95 throw new DecodeException('Invalid offset.');
96 }
97 $byte = ord($data[$idx++]);
98 // bits 8 and 7 (class)
99 // 0 = universal, 1 = application, 2 = context-specific, 3 = private
100 $class = (0b11000000 & $byte) >> 6;
101 // bit 6 (0 = primitive / 1 = constructed)
102 $pc = (0b00100000 & $byte) >> 5;
103 // bits 5 to 1 (tag number)
104 $tag = (0b00011111 & $byte);
105 // long-form identifier
106 if ($tag === 0x1f) {
107 $tag = self::decodeLongFormTag($data, $idx);
108 }
109 if (isset($offset)) {
110 $offset = $idx;
111 }
112 return self::create($class, $pc, $tag);
113 }
114
115 public function toDER(): string
116 {
117 $bytes = [];
118 $byte = $this->_class << 6 | $this->_pc << 5;
119 $tag = $this->_tag->getValue();
120 if ($tag->isLessThan(0x1f)) {
121 $bytes[] = $byte | $tag->toInt();
122 } // long-form identifier
123 else {
124 $bytes[] = $byte | 0x1f;
125 $octets = [];
126 for (; $tag->isGreaterThan(0); $tag = $tag->shiftedRight(7)) {
127 $octets[] = 0x80 | $tag->and(0x7f)->toInt();
128 }
129 // last octet has bit 8 set to zero
130 $octets[0] &= 0x7f;
131 foreach (array_reverse($octets) as $octet) {
132 $bytes[] = $octet;
133 }
134 }
135 return pack('C*', ...$bytes);
136 }
137
138 /**
139 * Get class of the type.
140 */
141 public function typeClass(): int
142 {
143 return $this->_class;
144 }
145
146 public function pc(): int
147 {
148 return $this->_pc;
149 }
150
151 /**
152 * Get the tag number.
153 *
154 * @return string Base 10 integer string
155 */
156 public function tag(): string
157 {
158 return $this->_tag->base10();
159 }
160
161 /**
162 * Get the tag as an integer.
163 */
164 public function intTag(): int
165 {
166 return $this->_tag->toInt();
167 }
168
169 /**
170 * Check whether type is of an universal class.
171 */
172 public function isUniversal(): bool
173 {
174 return $this->_class === self::CLASS_UNIVERSAL;
175 }
176
177 /**
178 * Check whether type is of an application class.
179 */
180 public function isApplication(): bool
181 {
182 return $this->_class === self::CLASS_APPLICATION;
183 }
184
185 /**
186 * Check whether type is of a context specific class.
187 */
188 public function isContextSpecific(): bool
189 {
190 return $this->_class === self::CLASS_CONTEXT_SPECIFIC;
191 }
192
193 /**
194 * Check whether type is of a private class.
195 */
196 public function isPrivate(): bool
197 {
198 return $this->_class === self::CLASS_PRIVATE;
199 }
200
201 /**
202 * Check whether content is primitive type.
203 */
204 public function isPrimitive(): bool
205 {
206 return $this->_pc === self::PRIMITIVE;
207 }
208
209 /**
210 * Check hether content is constructed type.
211 */
212 public function isConstructed(): bool
213 {
214 return $this->_pc === self::CONSTRUCTED;
215 }
216
217 /**
218 * Get self with given type class.
219 *
220 * @param int $class One of `CLASS_*` enumerations
221 */
222 public function withClass(int $class): self
223 {
224 $obj = clone $this;
225 $obj->_class = 0b11 & $class;
226 return $obj;
227 }
228
229 /**
230 * Get self with given type tag.
231 *
232 * @param int $tag Tag number
233 */
234 public function withTag(int $tag): self
235 {
236 $obj = clone $this;
237 $obj->_tag = BigInt::create($tag);
238 return $obj;
239 }
240
241 /**
242 * Get human readable name of the type class.
243 */
244 public static function classToName(int $class): string
245 {
246 if (! array_key_exists($class, self::MAP_CLASS_TO_NAME)) {
247 return "CLASS {$class}";
248 }
249 return self::MAP_CLASS_TO_NAME[$class];
250 }
251
252 /**
253 * Parse long form tag.
254 *
255 * @param string $data DER data
256 * @param int $offset Reference to the variable containing offset to data
257 *
258 * @return BigInteger Tag number
259 */
260 private static function decodeLongFormTag(string $data, int &$offset): BigInteger
261 {
262 $datalen = mb_strlen($data, '8bit');
263 $tag = BigInteger::of(0);
264 while (true) {
265 if ($offset >= $datalen) {
266 throw new DecodeException('Unexpected end of data while decoding long form identifier.');
267 }
268 $byte = ord($data[$offset++]);
269 $tag = $tag->shiftedLeft(7);
270 $tag = $tag->or(0x7f & $byte);
271 // last byte has bit 8 set to zero
272 if ((0x80 & $byte) === 0) {
273 break;
274 }
275 }
276 return $tag;
277 }
278 }