@@ -12,295 +12,295 @@ |
||
12 | 12 | */ |
13 | 13 | class Identifier implements Encodable |
14 | 14 | { |
15 | - // Type class enumerations |
|
16 | - const CLASS_UNIVERSAL = 0b00; |
|
17 | - const CLASS_APPLICATION = 0b01; |
|
18 | - const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
19 | - const CLASS_PRIVATE = 0b11; |
|
15 | + // Type class enumerations |
|
16 | + const CLASS_UNIVERSAL = 0b00; |
|
17 | + const CLASS_APPLICATION = 0b01; |
|
18 | + const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
19 | + const CLASS_PRIVATE = 0b11; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Mapping from type class to human readable name. |
|
23 | - * |
|
24 | - * @internal |
|
25 | - * |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - const MAP_CLASS_TO_NAME = [ /* @formatter:off */ |
|
29 | - self::CLASS_UNIVERSAL => "UNIVERSAL", |
|
30 | - self::CLASS_APPLICATION => "APPLICATION", |
|
31 | - self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
|
32 | - self::CLASS_PRIVATE => "PRIVATE", |
|
33 | - /* @formatter:on */ |
|
34 | - ]; |
|
21 | + /** |
|
22 | + * Mapping from type class to human readable name. |
|
23 | + * |
|
24 | + * @internal |
|
25 | + * |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + const MAP_CLASS_TO_NAME = [ /* @formatter:off */ |
|
29 | + self::CLASS_UNIVERSAL => "UNIVERSAL", |
|
30 | + self::CLASS_APPLICATION => "APPLICATION", |
|
31 | + self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
|
32 | + self::CLASS_PRIVATE => "PRIVATE", |
|
33 | + /* @formatter:on */ |
|
34 | + ]; |
|
35 | 35 | |
36 | - // P/C enumerations |
|
37 | - const PRIMITIVE = 0b0; |
|
38 | - const CONSTRUCTED = 0b1; |
|
36 | + // P/C enumerations |
|
37 | + const PRIMITIVE = 0b0; |
|
38 | + const CONSTRUCTED = 0b1; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Type class. |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - private $_class; |
|
40 | + /** |
|
41 | + * Type class. |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + private $_class; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Primitive or Constructed. |
|
49 | - * |
|
50 | - * @var int |
|
51 | - */ |
|
52 | - private $_pc; |
|
47 | + /** |
|
48 | + * Primitive or Constructed. |
|
49 | + * |
|
50 | + * @var int |
|
51 | + */ |
|
52 | + private $_pc; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Content type tag. |
|
56 | - * |
|
57 | - * @var BigInt |
|
58 | - */ |
|
59 | - private $_tag; |
|
54 | + /** |
|
55 | + * Content type tag. |
|
56 | + * |
|
57 | + * @var BigInt |
|
58 | + */ |
|
59 | + private $_tag; |
|
60 | 60 | |
61 | - /** |
|
62 | - * Constructor. |
|
63 | - * |
|
64 | - * @param int $class Type class |
|
65 | - * @param int $pc Primitive / Constructed |
|
66 | - * @param int|string $tag Type tag number |
|
67 | - */ |
|
68 | - public function __construct(int $class, int $pc, $tag) |
|
69 | - { |
|
70 | - $this->_class = 0b11 & $class; |
|
71 | - $this->_pc = 0b1 & $pc; |
|
72 | - $this->_tag = new BigInt($tag); |
|
73 | - } |
|
61 | + /** |
|
62 | + * Constructor. |
|
63 | + * |
|
64 | + * @param int $class Type class |
|
65 | + * @param int $pc Primitive / Constructed |
|
66 | + * @param int|string $tag Type tag number |
|
67 | + */ |
|
68 | + public function __construct(int $class, int $pc, $tag) |
|
69 | + { |
|
70 | + $this->_class = 0b11 & $class; |
|
71 | + $this->_pc = 0b1 & $pc; |
|
72 | + $this->_tag = new BigInt($tag); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Decode identifier component from DER data. |
|
77 | - * |
|
78 | - * @param string $data DER encoded data |
|
79 | - * @param int|null $offset Reference to the variable that contains offset |
|
80 | - * into the data where to start parsing. Variable is updated to |
|
81 | - * the offset next to the parsed identifier. If null, start from |
|
82 | - * offset 0. |
|
83 | - * @throws DecodeException If decoding fails |
|
84 | - * @return self |
|
85 | - */ |
|
86 | - public static function fromDER(string $data, int &$offset = null): Identifier |
|
87 | - { |
|
88 | - $idx = $offset ?? 0; |
|
89 | - $datalen = strlen($data); |
|
90 | - if ($idx >= $datalen) { |
|
91 | - throw new DecodeException("Invalid offset."); |
|
92 | - } |
|
93 | - $byte = ord($data[$idx++]); |
|
94 | - // bits 8 and 7 (class) |
|
95 | - // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
96 | - $class = (0b11000000 & $byte) >> 6; |
|
97 | - // bit 6 (0 = primitive / 1 = constructed) |
|
98 | - $pc = (0b00100000 & $byte) >> 5; |
|
99 | - // bits 5 to 1 (tag number) |
|
100 | - $tag = (0b00011111 & $byte); |
|
101 | - // long-form identifier |
|
102 | - if (0x1f == $tag) { |
|
103 | - $tag = self::_decodeLongFormTag($data, $idx); |
|
104 | - } |
|
105 | - if (isset($offset)) { |
|
106 | - $offset = $idx; |
|
107 | - } |
|
108 | - return new self($class, $pc, $tag); |
|
109 | - } |
|
75 | + /** |
|
76 | + * Decode identifier component from DER data. |
|
77 | + * |
|
78 | + * @param string $data DER encoded data |
|
79 | + * @param int|null $offset Reference to the variable that contains offset |
|
80 | + * into the data where to start parsing. Variable is updated to |
|
81 | + * the offset next to the parsed identifier. If null, start from |
|
82 | + * offset 0. |
|
83 | + * @throws DecodeException If decoding fails |
|
84 | + * @return self |
|
85 | + */ |
|
86 | + public static function fromDER(string $data, int &$offset = null): Identifier |
|
87 | + { |
|
88 | + $idx = $offset ?? 0; |
|
89 | + $datalen = strlen($data); |
|
90 | + if ($idx >= $datalen) { |
|
91 | + throw new DecodeException("Invalid offset."); |
|
92 | + } |
|
93 | + $byte = ord($data[$idx++]); |
|
94 | + // bits 8 and 7 (class) |
|
95 | + // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
96 | + $class = (0b11000000 & $byte) >> 6; |
|
97 | + // bit 6 (0 = primitive / 1 = constructed) |
|
98 | + $pc = (0b00100000 & $byte) >> 5; |
|
99 | + // bits 5 to 1 (tag number) |
|
100 | + $tag = (0b00011111 & $byte); |
|
101 | + // long-form identifier |
|
102 | + if (0x1f == $tag) { |
|
103 | + $tag = self::_decodeLongFormTag($data, $idx); |
|
104 | + } |
|
105 | + if (isset($offset)) { |
|
106 | + $offset = $idx; |
|
107 | + } |
|
108 | + return new self($class, $pc, $tag); |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * Parse long form tag. |
|
113 | - * |
|
114 | - * @param string $data DER data |
|
115 | - * @param int $offset Reference to the variable containing offset to data |
|
116 | - * @throws DecodeException If decoding fails |
|
117 | - * @return string Tag number |
|
118 | - */ |
|
119 | - private static function _decodeLongFormTag(string $data, int &$offset): string |
|
120 | - { |
|
121 | - $datalen = strlen($data); |
|
122 | - $tag = gmp_init(0, 10); |
|
123 | - while (true) { |
|
124 | - if ($offset >= $datalen) { |
|
125 | - throw new DecodeException( |
|
126 | - "Unexpected end of data while decoding" . |
|
127 | - " long form identifier."); |
|
128 | - } |
|
129 | - $byte = ord($data[$offset++]); |
|
130 | - $tag <<= 7; |
|
131 | - $tag |= 0x7f & $byte; |
|
132 | - // last byte has bit 8 set to zero |
|
133 | - if (!(0x80 & $byte)) { |
|
134 | - break; |
|
135 | - } |
|
136 | - } |
|
137 | - return gmp_strval($tag, 10); |
|
138 | - } |
|
111 | + /** |
|
112 | + * Parse long form tag. |
|
113 | + * |
|
114 | + * @param string $data DER data |
|
115 | + * @param int $offset Reference to the variable containing offset to data |
|
116 | + * @throws DecodeException If decoding fails |
|
117 | + * @return string Tag number |
|
118 | + */ |
|
119 | + private static function _decodeLongFormTag(string $data, int &$offset): string |
|
120 | + { |
|
121 | + $datalen = strlen($data); |
|
122 | + $tag = gmp_init(0, 10); |
|
123 | + while (true) { |
|
124 | + if ($offset >= $datalen) { |
|
125 | + throw new DecodeException( |
|
126 | + "Unexpected end of data while decoding" . |
|
127 | + " long form identifier."); |
|
128 | + } |
|
129 | + $byte = ord($data[$offset++]); |
|
130 | + $tag <<= 7; |
|
131 | + $tag |= 0x7f & $byte; |
|
132 | + // last byte has bit 8 set to zero |
|
133 | + if (!(0x80 & $byte)) { |
|
134 | + break; |
|
135 | + } |
|
136 | + } |
|
137 | + return gmp_strval($tag, 10); |
|
138 | + } |
|
139 | 139 | |
140 | - /** |
|
141 | - * |
|
142 | - * @see Encodable::toDER() |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - public function toDER(): string |
|
146 | - { |
|
147 | - $bytes = []; |
|
148 | - $byte = $this->_class << 6 | $this->_pc << 5; |
|
149 | - $tag = $this->_tag->gmpObj(); |
|
150 | - if ($tag < 0x1f) { |
|
151 | - $bytes[] = $byte | $tag; |
|
152 | - } else { // long-form identifier |
|
153 | - $bytes[] = $byte | 0x1f; |
|
154 | - $octets = []; |
|
155 | - for (; $tag > 0; $tag >>= 7) { |
|
156 | - array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
157 | - } |
|
158 | - // last octet has bit 8 set to zero |
|
159 | - $octets[0] &= 0x7f; |
|
160 | - foreach (array_reverse($octets) as $octet) { |
|
161 | - $bytes[] = $octet; |
|
162 | - } |
|
163 | - } |
|
164 | - return pack("C*", ...$bytes); |
|
165 | - } |
|
140 | + /** |
|
141 | + * |
|
142 | + * @see Encodable::toDER() |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + public function toDER(): string |
|
146 | + { |
|
147 | + $bytes = []; |
|
148 | + $byte = $this->_class << 6 | $this->_pc << 5; |
|
149 | + $tag = $this->_tag->gmpObj(); |
|
150 | + if ($tag < 0x1f) { |
|
151 | + $bytes[] = $byte | $tag; |
|
152 | + } else { // long-form identifier |
|
153 | + $bytes[] = $byte | 0x1f; |
|
154 | + $octets = []; |
|
155 | + for (; $tag > 0; $tag >>= 7) { |
|
156 | + array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
157 | + } |
|
158 | + // last octet has bit 8 set to zero |
|
159 | + $octets[0] &= 0x7f; |
|
160 | + foreach (array_reverse($octets) as $octet) { |
|
161 | + $bytes[] = $octet; |
|
162 | + } |
|
163 | + } |
|
164 | + return pack("C*", ...$bytes); |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * Get class of the type. |
|
169 | - * |
|
170 | - * @return int |
|
171 | - */ |
|
172 | - public function typeClass(): int |
|
173 | - { |
|
174 | - return $this->_class; |
|
175 | - } |
|
167 | + /** |
|
168 | + * Get class of the type. |
|
169 | + * |
|
170 | + * @return int |
|
171 | + */ |
|
172 | + public function typeClass(): int |
|
173 | + { |
|
174 | + return $this->_class; |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * Get P/C. |
|
179 | - * |
|
180 | - * @return int |
|
181 | - */ |
|
182 | - public function pc(): int |
|
183 | - { |
|
184 | - return $this->_pc; |
|
185 | - } |
|
177 | + /** |
|
178 | + * Get P/C. |
|
179 | + * |
|
180 | + * @return int |
|
181 | + */ |
|
182 | + public function pc(): int |
|
183 | + { |
|
184 | + return $this->_pc; |
|
185 | + } |
|
186 | 186 | |
187 | - /** |
|
188 | - * Get the tag number. |
|
189 | - * |
|
190 | - * @return string Base 10 integer string |
|
191 | - */ |
|
192 | - public function tag(): string |
|
193 | - { |
|
194 | - return $this->_tag->base10(); |
|
195 | - } |
|
187 | + /** |
|
188 | + * Get the tag number. |
|
189 | + * |
|
190 | + * @return string Base 10 integer string |
|
191 | + */ |
|
192 | + public function tag(): string |
|
193 | + { |
|
194 | + return $this->_tag->base10(); |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * Get the tag as an integer. |
|
199 | - * |
|
200 | - * @return int |
|
201 | - */ |
|
202 | - public function intTag(): int |
|
203 | - { |
|
204 | - return $this->_tag->intVal(); |
|
205 | - } |
|
197 | + /** |
|
198 | + * Get the tag as an integer. |
|
199 | + * |
|
200 | + * @return int |
|
201 | + */ |
|
202 | + public function intTag(): int |
|
203 | + { |
|
204 | + return $this->_tag->intVal(); |
|
205 | + } |
|
206 | 206 | |
207 | - /** |
|
208 | - * Check whether type is of an universal class. |
|
209 | - * |
|
210 | - * @return bool |
|
211 | - */ |
|
212 | - public function isUniversal(): bool |
|
213 | - { |
|
214 | - return self::CLASS_UNIVERSAL == $this->_class; |
|
215 | - } |
|
207 | + /** |
|
208 | + * Check whether type is of an universal class. |
|
209 | + * |
|
210 | + * @return bool |
|
211 | + */ |
|
212 | + public function isUniversal(): bool |
|
213 | + { |
|
214 | + return self::CLASS_UNIVERSAL == $this->_class; |
|
215 | + } |
|
216 | 216 | |
217 | - /** |
|
218 | - * Check whether type is of an application class. |
|
219 | - * |
|
220 | - * @return bool |
|
221 | - */ |
|
222 | - public function isApplication(): bool |
|
223 | - { |
|
224 | - return self::CLASS_APPLICATION == $this->_class; |
|
225 | - } |
|
217 | + /** |
|
218 | + * Check whether type is of an application class. |
|
219 | + * |
|
220 | + * @return bool |
|
221 | + */ |
|
222 | + public function isApplication(): bool |
|
223 | + { |
|
224 | + return self::CLASS_APPLICATION == $this->_class; |
|
225 | + } |
|
226 | 226 | |
227 | - /** |
|
228 | - * Check whether type is of a context specific class. |
|
229 | - * |
|
230 | - * @return bool |
|
231 | - */ |
|
232 | - public function isContextSpecific(): bool |
|
233 | - { |
|
234 | - return self::CLASS_CONTEXT_SPECIFIC == $this->_class; |
|
235 | - } |
|
227 | + /** |
|
228 | + * Check whether type is of a context specific class. |
|
229 | + * |
|
230 | + * @return bool |
|
231 | + */ |
|
232 | + public function isContextSpecific(): bool |
|
233 | + { |
|
234 | + return self::CLASS_CONTEXT_SPECIFIC == $this->_class; |
|
235 | + } |
|
236 | 236 | |
237 | - /** |
|
238 | - * Check whether type is of a private class. |
|
239 | - * |
|
240 | - * @return bool |
|
241 | - */ |
|
242 | - public function isPrivate(): bool |
|
243 | - { |
|
244 | - return self::CLASS_PRIVATE == $this->_class; |
|
245 | - } |
|
237 | + /** |
|
238 | + * Check whether type is of a private class. |
|
239 | + * |
|
240 | + * @return bool |
|
241 | + */ |
|
242 | + public function isPrivate(): bool |
|
243 | + { |
|
244 | + return self::CLASS_PRIVATE == $this->_class; |
|
245 | + } |
|
246 | 246 | |
247 | - /** |
|
248 | - * Check whether content is primitive type. |
|
249 | - * |
|
250 | - * @return bool |
|
251 | - */ |
|
252 | - public function isPrimitive(): bool |
|
253 | - { |
|
254 | - return self::PRIMITIVE == $this->_pc; |
|
255 | - } |
|
247 | + /** |
|
248 | + * Check whether content is primitive type. |
|
249 | + * |
|
250 | + * @return bool |
|
251 | + */ |
|
252 | + public function isPrimitive(): bool |
|
253 | + { |
|
254 | + return self::PRIMITIVE == $this->_pc; |
|
255 | + } |
|
256 | 256 | |
257 | - /** |
|
258 | - * Check hether content is constructed type. |
|
259 | - * |
|
260 | - * @return bool |
|
261 | - */ |
|
262 | - public function isConstructed(): bool |
|
263 | - { |
|
264 | - return self::CONSTRUCTED == $this->_pc; |
|
265 | - } |
|
257 | + /** |
|
258 | + * Check hether content is constructed type. |
|
259 | + * |
|
260 | + * @return bool |
|
261 | + */ |
|
262 | + public function isConstructed(): bool |
|
263 | + { |
|
264 | + return self::CONSTRUCTED == $this->_pc; |
|
265 | + } |
|
266 | 266 | |
267 | - /** |
|
268 | - * Get self with given type class. |
|
269 | - * |
|
270 | - * @param int $class One of <code>CLASS_*</code> enumerations |
|
271 | - * @return self |
|
272 | - */ |
|
273 | - public function withClass(int $class): Identifier |
|
274 | - { |
|
275 | - $obj = clone $this; |
|
276 | - $obj->_class = 0b11 & $class; |
|
277 | - return $obj; |
|
278 | - } |
|
267 | + /** |
|
268 | + * Get self with given type class. |
|
269 | + * |
|
270 | + * @param int $class One of <code>CLASS_*</code> enumerations |
|
271 | + * @return self |
|
272 | + */ |
|
273 | + public function withClass(int $class): Identifier |
|
274 | + { |
|
275 | + $obj = clone $this; |
|
276 | + $obj->_class = 0b11 & $class; |
|
277 | + return $obj; |
|
278 | + } |
|
279 | 279 | |
280 | - /** |
|
281 | - * Get self with given type tag. |
|
282 | - * |
|
283 | - * @param int|string $tag Tag number |
|
284 | - * @return self |
|
285 | - */ |
|
286 | - public function withTag($tag): Identifier |
|
287 | - { |
|
288 | - $obj = clone $this; |
|
289 | - $obj->_tag = new BigInt($tag); |
|
290 | - return $obj; |
|
291 | - } |
|
280 | + /** |
|
281 | + * Get self with given type tag. |
|
282 | + * |
|
283 | + * @param int|string $tag Tag number |
|
284 | + * @return self |
|
285 | + */ |
|
286 | + public function withTag($tag): Identifier |
|
287 | + { |
|
288 | + $obj = clone $this; |
|
289 | + $obj->_tag = new BigInt($tag); |
|
290 | + return $obj; |
|
291 | + } |
|
292 | 292 | |
293 | - /** |
|
294 | - * Get human readable name of the type class. |
|
295 | - * |
|
296 | - * @param int $class |
|
297 | - * @return string |
|
298 | - */ |
|
299 | - public static function classToName(int $class): string |
|
300 | - { |
|
301 | - if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
302 | - return "CLASS $class"; |
|
303 | - } |
|
304 | - return self::MAP_CLASS_TO_NAME[$class]; |
|
305 | - } |
|
293 | + /** |
|
294 | + * Get human readable name of the type class. |
|
295 | + * |
|
296 | + * @param int $class |
|
297 | + * @return string |
|
298 | + */ |
|
299 | + public static function classToName(int $class): string |
|
300 | + { |
|
301 | + if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
302 | + return "CLASS $class"; |
|
303 | + } |
|
304 | + return self::MAP_CLASS_TO_NAME[$class]; |
|
305 | + } |
|
306 | 306 | } |
@@ -12,214 +12,214 @@ |
||
12 | 12 | */ |
13 | 13 | class Length implements Encodable |
14 | 14 | { |
15 | - /** |
|
16 | - * Length. |
|
17 | - * |
|
18 | - * @var BigInt |
|
19 | - */ |
|
20 | - private $_length; |
|
15 | + /** |
|
16 | + * Length. |
|
17 | + * |
|
18 | + * @var BigInt |
|
19 | + */ |
|
20 | + private $_length; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Whether length is indefinite. |
|
24 | - * |
|
25 | - * @var bool |
|
26 | - */ |
|
27 | - private $_indefinite; |
|
22 | + /** |
|
23 | + * Whether length is indefinite. |
|
24 | + * |
|
25 | + * @var bool |
|
26 | + */ |
|
27 | + private $_indefinite; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Constructor. |
|
31 | - * |
|
32 | - * @param int|string $length Length |
|
33 | - * @param bool $indefinite Whether length is indefinite |
|
34 | - */ |
|
35 | - public function __construct($length, bool $indefinite = false) |
|
36 | - { |
|
37 | - $this->_length = new BigInt($length); |
|
38 | - $this->_indefinite = $indefinite; |
|
39 | - } |
|
29 | + /** |
|
30 | + * Constructor. |
|
31 | + * |
|
32 | + * @param int|string $length Length |
|
33 | + * @param bool $indefinite Whether length is indefinite |
|
34 | + */ |
|
35 | + public function __construct($length, bool $indefinite = false) |
|
36 | + { |
|
37 | + $this->_length = new BigInt($length); |
|
38 | + $this->_indefinite = $indefinite; |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Decode length component from DER data. |
|
43 | - * |
|
44 | - * @param string $data DER encoded data |
|
45 | - * @param int|null $offset Reference to the variable that contains offset |
|
46 | - * into the data where to start parsing. Variable is updated to |
|
47 | - * the offset next to the parsed length component. If null, start |
|
48 | - * from offset 0. |
|
49 | - * @throws DecodeException If decoding fails |
|
50 | - * @return self |
|
51 | - */ |
|
52 | - public static function fromDER(string $data, int &$offset = null): self |
|
53 | - { |
|
54 | - $idx = $offset ?? 0; |
|
55 | - $datalen = strlen($data); |
|
56 | - if ($idx >= $datalen) { |
|
57 | - throw new DecodeException( |
|
58 | - "Unexpected end of data while decoding length."); |
|
59 | - } |
|
60 | - $indefinite = false; |
|
61 | - $byte = ord($data[$idx++]); |
|
62 | - // bits 7 to 1 |
|
63 | - $length = (0x7f & $byte); |
|
64 | - // long form |
|
65 | - if (0x80 & $byte) { |
|
66 | - if (!$length) { |
|
67 | - $indefinite = true; |
|
68 | - } else { |
|
69 | - if ($idx + $length > $datalen) { |
|
70 | - throw new DecodeException( |
|
71 | - "Unexpected end of data while decoding long form length."); |
|
72 | - } |
|
73 | - $length = self::_decodeLongFormLength($length, $data, $idx); |
|
74 | - } |
|
75 | - } |
|
76 | - if (isset($offset)) { |
|
77 | - $offset = $idx; |
|
78 | - } |
|
79 | - return new self($length, $indefinite); |
|
80 | - } |
|
41 | + /** |
|
42 | + * Decode length component from DER data. |
|
43 | + * |
|
44 | + * @param string $data DER encoded data |
|
45 | + * @param int|null $offset Reference to the variable that contains offset |
|
46 | + * into the data where to start parsing. Variable is updated to |
|
47 | + * the offset next to the parsed length component. If null, start |
|
48 | + * from offset 0. |
|
49 | + * @throws DecodeException If decoding fails |
|
50 | + * @return self |
|
51 | + */ |
|
52 | + public static function fromDER(string $data, int &$offset = null): self |
|
53 | + { |
|
54 | + $idx = $offset ?? 0; |
|
55 | + $datalen = strlen($data); |
|
56 | + if ($idx >= $datalen) { |
|
57 | + throw new DecodeException( |
|
58 | + "Unexpected end of data while decoding length."); |
|
59 | + } |
|
60 | + $indefinite = false; |
|
61 | + $byte = ord($data[$idx++]); |
|
62 | + // bits 7 to 1 |
|
63 | + $length = (0x7f & $byte); |
|
64 | + // long form |
|
65 | + if (0x80 & $byte) { |
|
66 | + if (!$length) { |
|
67 | + $indefinite = true; |
|
68 | + } else { |
|
69 | + if ($idx + $length > $datalen) { |
|
70 | + throw new DecodeException( |
|
71 | + "Unexpected end of data while decoding long form length."); |
|
72 | + } |
|
73 | + $length = self::_decodeLongFormLength($length, $data, $idx); |
|
74 | + } |
|
75 | + } |
|
76 | + if (isset($offset)) { |
|
77 | + $offset = $idx; |
|
78 | + } |
|
79 | + return new self($length, $indefinite); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Decode long form length. |
|
84 | - * |
|
85 | - * @param int $length Number of octets |
|
86 | - * @param string $data Data |
|
87 | - * @param int $offset Reference to the variable containing offset to the |
|
88 | - * data. |
|
89 | - * @throws DecodeException If decoding fails |
|
90 | - * @return string Integer as a string |
|
91 | - */ |
|
92 | - private static function _decodeLongFormLength(int $length, string $data, |
|
93 | - int &$offset): string |
|
94 | - { |
|
95 | - // first octet must not be 0xff (spec 8.1.3.5c) |
|
96 | - if ($length == 127) { |
|
97 | - throw new DecodeException("Invalid number of length octets."); |
|
98 | - } |
|
99 | - $num = gmp_init(0, 10); |
|
100 | - while (--$length >= 0) { |
|
101 | - $byte = ord($data[$offset++]); |
|
102 | - $num <<= 8; |
|
103 | - $num |= $byte; |
|
104 | - } |
|
105 | - return gmp_strval($num); |
|
106 | - } |
|
82 | + /** |
|
83 | + * Decode long form length. |
|
84 | + * |
|
85 | + * @param int $length Number of octets |
|
86 | + * @param string $data Data |
|
87 | + * @param int $offset Reference to the variable containing offset to the |
|
88 | + * data. |
|
89 | + * @throws DecodeException If decoding fails |
|
90 | + * @return string Integer as a string |
|
91 | + */ |
|
92 | + private static function _decodeLongFormLength(int $length, string $data, |
|
93 | + int &$offset): string |
|
94 | + { |
|
95 | + // first octet must not be 0xff (spec 8.1.3.5c) |
|
96 | + if ($length == 127) { |
|
97 | + throw new DecodeException("Invalid number of length octets."); |
|
98 | + } |
|
99 | + $num = gmp_init(0, 10); |
|
100 | + while (--$length >= 0) { |
|
101 | + $byte = ord($data[$offset++]); |
|
102 | + $num <<= 8; |
|
103 | + $num |= $byte; |
|
104 | + } |
|
105 | + return gmp_strval($num); |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * Decode length from DER. |
|
110 | - * |
|
111 | - * Throws an exception if length doesn't match with expected or if data |
|
112 | - * doesn't contain enough bytes. |
|
113 | - * |
|
114 | - * Requirement of definite length is relaxed contrary to the specification |
|
115 | - * (sect. 10.1). |
|
116 | - * |
|
117 | - * @see self::fromDER |
|
118 | - * @param string $data DER data |
|
119 | - * @param int $offset Reference to the offset variable |
|
120 | - * @param int|null $expected Expected length, null to bypass checking |
|
121 | - * @throws DecodeException If decoding or expectation fails |
|
122 | - * @return self |
|
123 | - */ |
|
124 | - public static function expectFromDER(string $data, int &$offset, |
|
125 | - int $expected = null): self |
|
126 | - { |
|
127 | - $idx = $offset; |
|
128 | - $length = self::fromDER($data, $idx); |
|
129 | - // if certain length was expected |
|
130 | - if (isset($expected)) { |
|
131 | - if ($length->isIndefinite()) { |
|
132 | - throw new DecodeException('Expected length %d, got indefinite.', |
|
133 | - $expected); |
|
134 | - } |
|
135 | - if ($expected != $length->intLength()) { |
|
136 | - throw new DecodeException( |
|
137 | - sprintf("Expected length %d, got %d.", $expected, |
|
138 | - $length->intLength())); |
|
139 | - } |
|
140 | - } |
|
141 | - // check that enough data is available |
|
142 | - if (!$length->isIndefinite() && |
|
143 | - strlen($data) < $idx + $length->intLength()) { |
|
144 | - throw new DecodeException( |
|
145 | - sprintf("Length %d overflows data, %d bytes left.", |
|
146 | - $length->intLength(), strlen($data) - $idx)); |
|
147 | - } |
|
148 | - $offset = $idx; |
|
149 | - return $length; |
|
150 | - } |
|
108 | + /** |
|
109 | + * Decode length from DER. |
|
110 | + * |
|
111 | + * Throws an exception if length doesn't match with expected or if data |
|
112 | + * doesn't contain enough bytes. |
|
113 | + * |
|
114 | + * Requirement of definite length is relaxed contrary to the specification |
|
115 | + * (sect. 10.1). |
|
116 | + * |
|
117 | + * @see self::fromDER |
|
118 | + * @param string $data DER data |
|
119 | + * @param int $offset Reference to the offset variable |
|
120 | + * @param int|null $expected Expected length, null to bypass checking |
|
121 | + * @throws DecodeException If decoding or expectation fails |
|
122 | + * @return self |
|
123 | + */ |
|
124 | + public static function expectFromDER(string $data, int &$offset, |
|
125 | + int $expected = null): self |
|
126 | + { |
|
127 | + $idx = $offset; |
|
128 | + $length = self::fromDER($data, $idx); |
|
129 | + // if certain length was expected |
|
130 | + if (isset($expected)) { |
|
131 | + if ($length->isIndefinite()) { |
|
132 | + throw new DecodeException('Expected length %d, got indefinite.', |
|
133 | + $expected); |
|
134 | + } |
|
135 | + if ($expected != $length->intLength()) { |
|
136 | + throw new DecodeException( |
|
137 | + sprintf("Expected length %d, got %d.", $expected, |
|
138 | + $length->intLength())); |
|
139 | + } |
|
140 | + } |
|
141 | + // check that enough data is available |
|
142 | + if (!$length->isIndefinite() && |
|
143 | + strlen($data) < $idx + $length->intLength()) { |
|
144 | + throw new DecodeException( |
|
145 | + sprintf("Length %d overflows data, %d bytes left.", |
|
146 | + $length->intLength(), strlen($data) - $idx)); |
|
147 | + } |
|
148 | + $offset = $idx; |
|
149 | + return $length; |
|
150 | + } |
|
151 | 151 | |
152 | - /** |
|
153 | - * |
|
154 | - * @see Encodable::toDER() |
|
155 | - * @throws \DomainException If length is too large to encode |
|
156 | - * @return string |
|
157 | - */ |
|
158 | - public function toDER(): string |
|
159 | - { |
|
160 | - $bytes = []; |
|
161 | - if ($this->_indefinite) { |
|
162 | - $bytes[] = 0x80; |
|
163 | - } else { |
|
164 | - $num = $this->_length->gmpObj(); |
|
165 | - // long form |
|
166 | - if ($num > 127) { |
|
167 | - $octets = []; |
|
168 | - for (; $num > 0; $num >>= 8) { |
|
169 | - $octets[] = gmp_intval(0xff & $num); |
|
170 | - } |
|
171 | - $count = count($octets); |
|
172 | - // first octet must not be 0xff |
|
173 | - if ($count >= 127) { |
|
174 | - throw new \DomainException("Too many length octets."); |
|
175 | - } |
|
176 | - $bytes[] = 0x80 | $count; |
|
177 | - foreach (array_reverse($octets) as $octet) { |
|
178 | - $bytes[] = $octet; |
|
179 | - } |
|
180 | - } else { // short form |
|
181 | - $bytes[] = gmp_intval($num); |
|
182 | - } |
|
183 | - } |
|
184 | - return pack("C*", ...$bytes); |
|
185 | - } |
|
152 | + /** |
|
153 | + * |
|
154 | + * @see Encodable::toDER() |
|
155 | + * @throws \DomainException If length is too large to encode |
|
156 | + * @return string |
|
157 | + */ |
|
158 | + public function toDER(): string |
|
159 | + { |
|
160 | + $bytes = []; |
|
161 | + if ($this->_indefinite) { |
|
162 | + $bytes[] = 0x80; |
|
163 | + } else { |
|
164 | + $num = $this->_length->gmpObj(); |
|
165 | + // long form |
|
166 | + if ($num > 127) { |
|
167 | + $octets = []; |
|
168 | + for (; $num > 0; $num >>= 8) { |
|
169 | + $octets[] = gmp_intval(0xff & $num); |
|
170 | + } |
|
171 | + $count = count($octets); |
|
172 | + // first octet must not be 0xff |
|
173 | + if ($count >= 127) { |
|
174 | + throw new \DomainException("Too many length octets."); |
|
175 | + } |
|
176 | + $bytes[] = 0x80 | $count; |
|
177 | + foreach (array_reverse($octets) as $octet) { |
|
178 | + $bytes[] = $octet; |
|
179 | + } |
|
180 | + } else { // short form |
|
181 | + $bytes[] = gmp_intval($num); |
|
182 | + } |
|
183 | + } |
|
184 | + return pack("C*", ...$bytes); |
|
185 | + } |
|
186 | 186 | |
187 | - /** |
|
188 | - * Get the length. |
|
189 | - * |
|
190 | - * @throws \LogicException If length is indefinite |
|
191 | - * @return string Length as an integer string |
|
192 | - */ |
|
193 | - public function length(): string |
|
194 | - { |
|
195 | - if ($this->_indefinite) { |
|
196 | - throw new \LogicException("Length is indefinite."); |
|
197 | - } |
|
198 | - return $this->_length->base10(); |
|
199 | - } |
|
187 | + /** |
|
188 | + * Get the length. |
|
189 | + * |
|
190 | + * @throws \LogicException If length is indefinite |
|
191 | + * @return string Length as an integer string |
|
192 | + */ |
|
193 | + public function length(): string |
|
194 | + { |
|
195 | + if ($this->_indefinite) { |
|
196 | + throw new \LogicException("Length is indefinite."); |
|
197 | + } |
|
198 | + return $this->_length->base10(); |
|
199 | + } |
|
200 | 200 | |
201 | - /** |
|
202 | - * Get the length as an integer. |
|
203 | - * |
|
204 | - * @throws \LogicException If length is indefinite |
|
205 | - * @throws \RuntimeException If length overflows integer size |
|
206 | - * @return int |
|
207 | - */ |
|
208 | - public function intLength(): int |
|
209 | - { |
|
210 | - if ($this->_indefinite) { |
|
211 | - throw new \LogicException("Length is indefinite."); |
|
212 | - } |
|
213 | - return $this->_length->intVal(); |
|
214 | - } |
|
201 | + /** |
|
202 | + * Get the length as an integer. |
|
203 | + * |
|
204 | + * @throws \LogicException If length is indefinite |
|
205 | + * @throws \RuntimeException If length overflows integer size |
|
206 | + * @return int |
|
207 | + */ |
|
208 | + public function intLength(): int |
|
209 | + { |
|
210 | + if ($this->_indefinite) { |
|
211 | + throw new \LogicException("Length is indefinite."); |
|
212 | + } |
|
213 | + return $this->_length->intVal(); |
|
214 | + } |
|
215 | 215 | |
216 | - /** |
|
217 | - * Whether length is indefinite. |
|
218 | - * |
|
219 | - * @return bool |
|
220 | - */ |
|
221 | - public function isIndefinite(): bool |
|
222 | - { |
|
223 | - return $this->_indefinite; |
|
224 | - } |
|
216 | + /** |
|
217 | + * Whether length is indefinite. |
|
218 | + * |
|
219 | + * @return bool |
|
220 | + */ |
|
221 | + public function isIndefinite(): bool |
|
222 | + { |
|
223 | + return $this->_indefinite; |
|
224 | + } |
|
225 | 225 | } |
@@ -13,396 +13,396 @@ |
||
13 | 13 | * Base class for the constructed types. |
14 | 14 | */ |
15 | 15 | abstract class Structure extends Element implements |
16 | - \Countable, |
|
17 | - \IteratorAggregate |
|
16 | + \Countable, |
|
17 | + \IteratorAggregate |
|
18 | 18 | { |
19 | - use UniversalClass; |
|
19 | + use UniversalClass; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Array of elements in the structure. |
|
23 | - * |
|
24 | - * @var Element[] $_elements |
|
25 | - */ |
|
26 | - protected $_elements; |
|
21 | + /** |
|
22 | + * Array of elements in the structure. |
|
23 | + * |
|
24 | + * @var Element[] $_elements |
|
25 | + */ |
|
26 | + protected $_elements; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Lookup table for the tagged elements. |
|
30 | - * |
|
31 | - * @var TaggedType[]|null $_taggedMap |
|
32 | - */ |
|
33 | - private $_taggedMap; |
|
28 | + /** |
|
29 | + * Lookup table for the tagged elements. |
|
30 | + * |
|
31 | + * @var TaggedType[]|null $_taggedMap |
|
32 | + */ |
|
33 | + private $_taggedMap; |
|
34 | 34 | |
35 | - /** |
|
36 | - * Cache variable of elements wrapped into UnspecifiedType objects. |
|
37 | - * |
|
38 | - * @var UnspecifiedType[]|null $_unspecifiedTypes |
|
39 | - */ |
|
40 | - private $_unspecifiedTypes; |
|
35 | + /** |
|
36 | + * Cache variable of elements wrapped into UnspecifiedType objects. |
|
37 | + * |
|
38 | + * @var UnspecifiedType[]|null $_unspecifiedTypes |
|
39 | + */ |
|
40 | + private $_unspecifiedTypes; |
|
41 | 41 | |
42 | - /** |
|
43 | - * Constructor. |
|
44 | - * |
|
45 | - * @param ElementBase ...$elements Any number of elements |
|
46 | - */ |
|
47 | - public function __construct(ElementBase ...$elements) |
|
48 | - { |
|
49 | - $this->_elements = array_map( |
|
50 | - function (ElementBase $el) { |
|
51 | - return $el->asElement(); |
|
52 | - }, $elements); |
|
53 | - } |
|
42 | + /** |
|
43 | + * Constructor. |
|
44 | + * |
|
45 | + * @param ElementBase ...$elements Any number of elements |
|
46 | + */ |
|
47 | + public function __construct(ElementBase ...$elements) |
|
48 | + { |
|
49 | + $this->_elements = array_map( |
|
50 | + function (ElementBase $el) { |
|
51 | + return $el->asElement(); |
|
52 | + }, $elements); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Clone magic method. |
|
57 | - */ |
|
58 | - public function __clone() |
|
59 | - { |
|
60 | - // clear cache-variables |
|
61 | - $this->_taggedMap = null; |
|
62 | - $this->_unspecifiedTypes = null; |
|
63 | - } |
|
55 | + /** |
|
56 | + * Clone magic method. |
|
57 | + */ |
|
58 | + public function __clone() |
|
59 | + { |
|
60 | + // clear cache-variables |
|
61 | + $this->_taggedMap = null; |
|
62 | + $this->_unspecifiedTypes = null; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * |
|
67 | - * @see \ASN1\Element::isConstructed() |
|
68 | - * @return bool |
|
69 | - */ |
|
70 | - public function isConstructed(): bool |
|
71 | - { |
|
72 | - return true; |
|
73 | - } |
|
65 | + /** |
|
66 | + * |
|
67 | + * @see \ASN1\Element::isConstructed() |
|
68 | + * @return bool |
|
69 | + */ |
|
70 | + public function isConstructed(): bool |
|
71 | + { |
|
72 | + return true; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * |
|
77 | - * @see \ASN1\Element::_encodedContentDER() |
|
78 | - * @return string |
|
79 | - */ |
|
80 | - protected function _encodedContentDER(): string |
|
81 | - { |
|
82 | - $data = ""; |
|
83 | - foreach ($this->_elements as $element) { |
|
84 | - $data .= $element->toDER(); |
|
85 | - } |
|
86 | - return $data; |
|
87 | - } |
|
75 | + /** |
|
76 | + * |
|
77 | + * @see \ASN1\Element::_encodedContentDER() |
|
78 | + * @return string |
|
79 | + */ |
|
80 | + protected function _encodedContentDER(): string |
|
81 | + { |
|
82 | + $data = ""; |
|
83 | + foreach ($this->_elements as $element) { |
|
84 | + $data .= $element->toDER(); |
|
85 | + } |
|
86 | + return $data; |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * |
|
91 | - * {@inheritdoc} |
|
92 | - * @see \ASN1\Element::_decodeFromDER() |
|
93 | - * @return self |
|
94 | - */ |
|
95 | - protected static function _decodeFromDER(Identifier $identifier, |
|
96 | - string $data, int &$offset): ElementBase |
|
97 | - { |
|
98 | - if (!$identifier->isConstructed()) { |
|
99 | - throw new DecodeException( |
|
100 | - "Structured element must have constructed bit set."); |
|
101 | - } |
|
102 | - $idx = $offset; |
|
103 | - $length = Length::expectFromDER($data, $idx); |
|
104 | - if ($length->isIndefinite()) { |
|
105 | - $type = self::_decodeIndefiniteLength($data, $idx); |
|
106 | - } else { |
|
107 | - $type = self::_decodeDefiniteLength($data, $idx, |
|
108 | - $length->intLength()); |
|
109 | - } |
|
110 | - $offset = $idx; |
|
111 | - return $type; |
|
112 | - } |
|
89 | + /** |
|
90 | + * |
|
91 | + * {@inheritdoc} |
|
92 | + * @see \ASN1\Element::_decodeFromDER() |
|
93 | + * @return self |
|
94 | + */ |
|
95 | + protected static function _decodeFromDER(Identifier $identifier, |
|
96 | + string $data, int &$offset): ElementBase |
|
97 | + { |
|
98 | + if (!$identifier->isConstructed()) { |
|
99 | + throw new DecodeException( |
|
100 | + "Structured element must have constructed bit set."); |
|
101 | + } |
|
102 | + $idx = $offset; |
|
103 | + $length = Length::expectFromDER($data, $idx); |
|
104 | + if ($length->isIndefinite()) { |
|
105 | + $type = self::_decodeIndefiniteLength($data, $idx); |
|
106 | + } else { |
|
107 | + $type = self::_decodeDefiniteLength($data, $idx, |
|
108 | + $length->intLength()); |
|
109 | + } |
|
110 | + $offset = $idx; |
|
111 | + return $type; |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Decode elements for a definite length. |
|
116 | - * |
|
117 | - * @param string $data DER data |
|
118 | - * @param int $offset Offset to data |
|
119 | - * @param int $length Number of bytes to decode |
|
120 | - * @throws DecodeException |
|
121 | - * @return ElementBase |
|
122 | - */ |
|
123 | - private static function _decodeDefiniteLength(string $data, int &$offset, |
|
124 | - int $length): ElementBase |
|
125 | - { |
|
126 | - $idx = $offset; |
|
127 | - $end = $idx + $length; |
|
128 | - $elements = []; |
|
129 | - while ($idx < $end) { |
|
130 | - $elements[] = Element::fromDER($data, $idx); |
|
131 | - // check that element didn't overflow length |
|
132 | - if ($idx > $end) { |
|
133 | - throw new DecodeException( |
|
134 | - "Structure's content overflows length."); |
|
135 | - } |
|
136 | - } |
|
137 | - $offset = $idx; |
|
138 | - // return instance by static late binding |
|
139 | - return new static(...$elements); |
|
140 | - } |
|
114 | + /** |
|
115 | + * Decode elements for a definite length. |
|
116 | + * |
|
117 | + * @param string $data DER data |
|
118 | + * @param int $offset Offset to data |
|
119 | + * @param int $length Number of bytes to decode |
|
120 | + * @throws DecodeException |
|
121 | + * @return ElementBase |
|
122 | + */ |
|
123 | + private static function _decodeDefiniteLength(string $data, int &$offset, |
|
124 | + int $length): ElementBase |
|
125 | + { |
|
126 | + $idx = $offset; |
|
127 | + $end = $idx + $length; |
|
128 | + $elements = []; |
|
129 | + while ($idx < $end) { |
|
130 | + $elements[] = Element::fromDER($data, $idx); |
|
131 | + // check that element didn't overflow length |
|
132 | + if ($idx > $end) { |
|
133 | + throw new DecodeException( |
|
134 | + "Structure's content overflows length."); |
|
135 | + } |
|
136 | + } |
|
137 | + $offset = $idx; |
|
138 | + // return instance by static late binding |
|
139 | + return new static(...$elements); |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * Decode elements for an indefinite length. |
|
144 | - * |
|
145 | - * @param string $data DER data |
|
146 | - * @param int $offset Offset to data |
|
147 | - * @throws DecodeException |
|
148 | - * @return ElementBase |
|
149 | - */ |
|
150 | - private static function _decodeIndefiniteLength(string $data, int &$offset): ElementBase |
|
151 | - { |
|
152 | - $idx = $offset; |
|
153 | - $elements = []; |
|
154 | - $end = strlen($data); |
|
155 | - while (true) { |
|
156 | - if ($idx >= $end) { |
|
157 | - throw new DecodeException( |
|
158 | - 'Unexpected end of data while decoding indefinite length structure.'); |
|
159 | - } |
|
160 | - $el = Element::fromDER($data, $idx); |
|
161 | - if ($el->isType(self::TYPE_EOC)) { |
|
162 | - break; |
|
163 | - } |
|
164 | - $elements[] = $el; |
|
165 | - } |
|
166 | - $offset = $idx; |
|
167 | - $type = new static(...$elements); |
|
168 | - $type->_indefiniteLength = true; |
|
169 | - return $type; |
|
170 | - } |
|
142 | + /** |
|
143 | + * Decode elements for an indefinite length. |
|
144 | + * |
|
145 | + * @param string $data DER data |
|
146 | + * @param int $offset Offset to data |
|
147 | + * @throws DecodeException |
|
148 | + * @return ElementBase |
|
149 | + */ |
|
150 | + private static function _decodeIndefiniteLength(string $data, int &$offset): ElementBase |
|
151 | + { |
|
152 | + $idx = $offset; |
|
153 | + $elements = []; |
|
154 | + $end = strlen($data); |
|
155 | + while (true) { |
|
156 | + if ($idx >= $end) { |
|
157 | + throw new DecodeException( |
|
158 | + 'Unexpected end of data while decoding indefinite length structure.'); |
|
159 | + } |
|
160 | + $el = Element::fromDER($data, $idx); |
|
161 | + if ($el->isType(self::TYPE_EOC)) { |
|
162 | + break; |
|
163 | + } |
|
164 | + $elements[] = $el; |
|
165 | + } |
|
166 | + $offset = $idx; |
|
167 | + $type = new static(...$elements); |
|
168 | + $type->_indefiniteLength = true; |
|
169 | + return $type; |
|
170 | + } |
|
171 | 171 | |
172 | - /** |
|
173 | - * Explode DER structure to DER encoded components that it contains. |
|
174 | - * |
|
175 | - * @param string $data |
|
176 | - * @throws DecodeException |
|
177 | - * @return string[] |
|
178 | - */ |
|
179 | - public static function explodeDER(string $data): array |
|
180 | - { |
|
181 | - $offset = 0; |
|
182 | - $identifier = Identifier::fromDER($data, $offset); |
|
183 | - if (!$identifier->isConstructed()) { |
|
184 | - throw new DecodeException("Element is not constructed."); |
|
185 | - } |
|
186 | - $length = Length::expectFromDER($data, $offset); |
|
187 | - if ($length->isIndefinite()) { |
|
188 | - throw new DecodeException( |
|
189 | - 'Explode not implemented for indefinite length encoding.'); |
|
190 | - } |
|
191 | - $end = $offset + $length->intLength(); |
|
192 | - $parts = []; |
|
193 | - while ($offset < $end) { |
|
194 | - // start of the element |
|
195 | - $idx = $offset; |
|
196 | - // skip identifier |
|
197 | - Identifier::fromDER($data, $offset); |
|
198 | - // decode element length |
|
199 | - $length = Length::expectFromDER($data, $offset)->intLength(); |
|
200 | - // extract der encoding of the element |
|
201 | - $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
202 | - // update offset over content |
|
203 | - $offset += $length; |
|
204 | - } |
|
205 | - return $parts; |
|
206 | - } |
|
172 | + /** |
|
173 | + * Explode DER structure to DER encoded components that it contains. |
|
174 | + * |
|
175 | + * @param string $data |
|
176 | + * @throws DecodeException |
|
177 | + * @return string[] |
|
178 | + */ |
|
179 | + public static function explodeDER(string $data): array |
|
180 | + { |
|
181 | + $offset = 0; |
|
182 | + $identifier = Identifier::fromDER($data, $offset); |
|
183 | + if (!$identifier->isConstructed()) { |
|
184 | + throw new DecodeException("Element is not constructed."); |
|
185 | + } |
|
186 | + $length = Length::expectFromDER($data, $offset); |
|
187 | + if ($length->isIndefinite()) { |
|
188 | + throw new DecodeException( |
|
189 | + 'Explode not implemented for indefinite length encoding.'); |
|
190 | + } |
|
191 | + $end = $offset + $length->intLength(); |
|
192 | + $parts = []; |
|
193 | + while ($offset < $end) { |
|
194 | + // start of the element |
|
195 | + $idx = $offset; |
|
196 | + // skip identifier |
|
197 | + Identifier::fromDER($data, $offset); |
|
198 | + // decode element length |
|
199 | + $length = Length::expectFromDER($data, $offset)->intLength(); |
|
200 | + // extract der encoding of the element |
|
201 | + $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
202 | + // update offset over content |
|
203 | + $offset += $length; |
|
204 | + } |
|
205 | + return $parts; |
|
206 | + } |
|
207 | 207 | |
208 | - /** |
|
209 | - * Get self with an element at the given index replaced by another. |
|
210 | - * |
|
211 | - * @param int $idx Element index |
|
212 | - * @param Element $el New element to insert into the structure |
|
213 | - * @throws \OutOfBoundsException |
|
214 | - * @return self |
|
215 | - */ |
|
216 | - public function withReplaced(int $idx, Element $el): self |
|
217 | - { |
|
218 | - if (!isset($this->_elements[$idx])) { |
|
219 | - throw new \OutOfBoundsException( |
|
220 | - "Structure doesn't have element at index $idx."); |
|
221 | - } |
|
222 | - $obj = clone $this; |
|
223 | - $obj->_elements[$idx] = $el; |
|
224 | - return $obj; |
|
225 | - } |
|
208 | + /** |
|
209 | + * Get self with an element at the given index replaced by another. |
|
210 | + * |
|
211 | + * @param int $idx Element index |
|
212 | + * @param Element $el New element to insert into the structure |
|
213 | + * @throws \OutOfBoundsException |
|
214 | + * @return self |
|
215 | + */ |
|
216 | + public function withReplaced(int $idx, Element $el): self |
|
217 | + { |
|
218 | + if (!isset($this->_elements[$idx])) { |
|
219 | + throw new \OutOfBoundsException( |
|
220 | + "Structure doesn't have element at index $idx."); |
|
221 | + } |
|
222 | + $obj = clone $this; |
|
223 | + $obj->_elements[$idx] = $el; |
|
224 | + return $obj; |
|
225 | + } |
|
226 | 226 | |
227 | - /** |
|
228 | - * Get self with an element inserted before the given index. |
|
229 | - * |
|
230 | - * @param int $idx Element index |
|
231 | - * @param Element $el New element to insert into the structure |
|
232 | - * @throws \OutOfBoundsException |
|
233 | - * @return self |
|
234 | - */ |
|
235 | - public function withInserted(int $idx, Element $el): self |
|
236 | - { |
|
237 | - if (count($this->_elements) < $idx || $idx < 0) { |
|
238 | - throw new \OutOfBoundsException("Index $idx is out of bounds."); |
|
239 | - } |
|
240 | - $obj = clone $this; |
|
241 | - array_splice($obj->_elements, $idx, 0, [$el]); |
|
242 | - return $obj; |
|
243 | - } |
|
227 | + /** |
|
228 | + * Get self with an element inserted before the given index. |
|
229 | + * |
|
230 | + * @param int $idx Element index |
|
231 | + * @param Element $el New element to insert into the structure |
|
232 | + * @throws \OutOfBoundsException |
|
233 | + * @return self |
|
234 | + */ |
|
235 | + public function withInserted(int $idx, Element $el): self |
|
236 | + { |
|
237 | + if (count($this->_elements) < $idx || $idx < 0) { |
|
238 | + throw new \OutOfBoundsException("Index $idx is out of bounds."); |
|
239 | + } |
|
240 | + $obj = clone $this; |
|
241 | + array_splice($obj->_elements, $idx, 0, [$el]); |
|
242 | + return $obj; |
|
243 | + } |
|
244 | 244 | |
245 | - /** |
|
246 | - * Get self with an element appended to the end. |
|
247 | - * |
|
248 | - * @param Element $el Element to insert into the structure |
|
249 | - * @return self |
|
250 | - */ |
|
251 | - public function withAppended(Element $el): self |
|
252 | - { |
|
253 | - $obj = clone $this; |
|
254 | - array_push($obj->_elements, $el); |
|
255 | - return $obj; |
|
256 | - } |
|
245 | + /** |
|
246 | + * Get self with an element appended to the end. |
|
247 | + * |
|
248 | + * @param Element $el Element to insert into the structure |
|
249 | + * @return self |
|
250 | + */ |
|
251 | + public function withAppended(Element $el): self |
|
252 | + { |
|
253 | + $obj = clone $this; |
|
254 | + array_push($obj->_elements, $el); |
|
255 | + return $obj; |
|
256 | + } |
|
257 | 257 | |
258 | - /** |
|
259 | - * Get self with an element prepended in the beginning. |
|
260 | - * |
|
261 | - * @param Element $el Element to insert into the structure |
|
262 | - * @return self |
|
263 | - */ |
|
264 | - public function withPrepended(Element $el): self |
|
265 | - { |
|
266 | - $obj = clone $this; |
|
267 | - array_unshift($obj->_elements, $el); |
|
268 | - return $obj; |
|
269 | - } |
|
258 | + /** |
|
259 | + * Get self with an element prepended in the beginning. |
|
260 | + * |
|
261 | + * @param Element $el Element to insert into the structure |
|
262 | + * @return self |
|
263 | + */ |
|
264 | + public function withPrepended(Element $el): self |
|
265 | + { |
|
266 | + $obj = clone $this; |
|
267 | + array_unshift($obj->_elements, $el); |
|
268 | + return $obj; |
|
269 | + } |
|
270 | 270 | |
271 | - /** |
|
272 | - * Get self with an element at the given index removed. |
|
273 | - * |
|
274 | - * @param int $idx Element index |
|
275 | - * @throws \OutOfBoundsException |
|
276 | - * @return self |
|
277 | - */ |
|
278 | - public function withoutElement(int $idx): self |
|
279 | - { |
|
280 | - if (!isset($this->_elements[$idx])) { |
|
281 | - throw new \OutOfBoundsException( |
|
282 | - "Structure doesn't have element at index $idx."); |
|
283 | - } |
|
284 | - $obj = clone $this; |
|
285 | - array_splice($obj->_elements, $idx, 1); |
|
286 | - return $obj; |
|
287 | - } |
|
271 | + /** |
|
272 | + * Get self with an element at the given index removed. |
|
273 | + * |
|
274 | + * @param int $idx Element index |
|
275 | + * @throws \OutOfBoundsException |
|
276 | + * @return self |
|
277 | + */ |
|
278 | + public function withoutElement(int $idx): self |
|
279 | + { |
|
280 | + if (!isset($this->_elements[$idx])) { |
|
281 | + throw new \OutOfBoundsException( |
|
282 | + "Structure doesn't have element at index $idx."); |
|
283 | + } |
|
284 | + $obj = clone $this; |
|
285 | + array_splice($obj->_elements, $idx, 1); |
|
286 | + return $obj; |
|
287 | + } |
|
288 | 288 | |
289 | - /** |
|
290 | - * Get elements in the structure. |
|
291 | - * |
|
292 | - * @return UnspecifiedType[] |
|
293 | - */ |
|
294 | - public function elements(): array |
|
295 | - { |
|
296 | - if (!isset($this->_unspecifiedTypes)) { |
|
297 | - $this->_unspecifiedTypes = array_map( |
|
298 | - function (Element $el) { |
|
299 | - return new UnspecifiedType($el); |
|
300 | - }, $this->_elements); |
|
301 | - } |
|
302 | - return $this->_unspecifiedTypes; |
|
303 | - } |
|
289 | + /** |
|
290 | + * Get elements in the structure. |
|
291 | + * |
|
292 | + * @return UnspecifiedType[] |
|
293 | + */ |
|
294 | + public function elements(): array |
|
295 | + { |
|
296 | + if (!isset($this->_unspecifiedTypes)) { |
|
297 | + $this->_unspecifiedTypes = array_map( |
|
298 | + function (Element $el) { |
|
299 | + return new UnspecifiedType($el); |
|
300 | + }, $this->_elements); |
|
301 | + } |
|
302 | + return $this->_unspecifiedTypes; |
|
303 | + } |
|
304 | 304 | |
305 | - /** |
|
306 | - * Check whether the structure has an element at the given index, optionally |
|
307 | - * satisfying given tag expectation. |
|
308 | - * |
|
309 | - * @param int $idx Index 0..n |
|
310 | - * @param int|null $expectedTag Optional type tag expectation |
|
311 | - * @return bool |
|
312 | - */ |
|
313 | - public function has(int $idx, $expectedTag = null): bool |
|
314 | - { |
|
315 | - if (!isset($this->_elements[$idx])) { |
|
316 | - return false; |
|
317 | - } |
|
318 | - if (isset($expectedTag)) { |
|
319 | - if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
320 | - return false; |
|
321 | - } |
|
322 | - } |
|
323 | - return true; |
|
324 | - } |
|
305 | + /** |
|
306 | + * Check whether the structure has an element at the given index, optionally |
|
307 | + * satisfying given tag expectation. |
|
308 | + * |
|
309 | + * @param int $idx Index 0..n |
|
310 | + * @param int|null $expectedTag Optional type tag expectation |
|
311 | + * @return bool |
|
312 | + */ |
|
313 | + public function has(int $idx, $expectedTag = null): bool |
|
314 | + { |
|
315 | + if (!isset($this->_elements[$idx])) { |
|
316 | + return false; |
|
317 | + } |
|
318 | + if (isset($expectedTag)) { |
|
319 | + if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
320 | + return false; |
|
321 | + } |
|
322 | + } |
|
323 | + return true; |
|
324 | + } |
|
325 | 325 | |
326 | - /** |
|
327 | - * Get the element at the given index, optionally checking that the element |
|
328 | - * has a given tag. |
|
329 | - * |
|
330 | - * <strong>NOTE!</strong> Expectation checking is deprecated and should be |
|
331 | - * done with <code>UnspecifiedType</code>. |
|
332 | - * |
|
333 | - * @param int $idx Index 0..n |
|
334 | - * @param int|null $expectedTag Optional type tag expectation |
|
335 | - * @throws \OutOfBoundsException If element doesn't exists |
|
336 | - * @throws \UnexpectedValueException If expectation fails |
|
337 | - * @return UnspecifiedType |
|
338 | - */ |
|
339 | - public function at(int $idx, $expectedTag = null): UnspecifiedType |
|
340 | - { |
|
341 | - if (!isset($this->_elements[$idx])) { |
|
342 | - throw new \OutOfBoundsException( |
|
343 | - "Structure doesn't have an element at index $idx."); |
|
344 | - } |
|
345 | - $element = $this->_elements[$idx]; |
|
346 | - if (isset($expectedTag)) { |
|
347 | - $element->expectType($expectedTag); |
|
348 | - } |
|
349 | - return new UnspecifiedType($element); |
|
350 | - } |
|
326 | + /** |
|
327 | + * Get the element at the given index, optionally checking that the element |
|
328 | + * has a given tag. |
|
329 | + * |
|
330 | + * <strong>NOTE!</strong> Expectation checking is deprecated and should be |
|
331 | + * done with <code>UnspecifiedType</code>. |
|
332 | + * |
|
333 | + * @param int $idx Index 0..n |
|
334 | + * @param int|null $expectedTag Optional type tag expectation |
|
335 | + * @throws \OutOfBoundsException If element doesn't exists |
|
336 | + * @throws \UnexpectedValueException If expectation fails |
|
337 | + * @return UnspecifiedType |
|
338 | + */ |
|
339 | + public function at(int $idx, $expectedTag = null): UnspecifiedType |
|
340 | + { |
|
341 | + if (!isset($this->_elements[$idx])) { |
|
342 | + throw new \OutOfBoundsException( |
|
343 | + "Structure doesn't have an element at index $idx."); |
|
344 | + } |
|
345 | + $element = $this->_elements[$idx]; |
|
346 | + if (isset($expectedTag)) { |
|
347 | + $element->expectType($expectedTag); |
|
348 | + } |
|
349 | + return new UnspecifiedType($element); |
|
350 | + } |
|
351 | 351 | |
352 | - /** |
|
353 | - * Check whether the structure contains a context specific element with a |
|
354 | - * given tag. |
|
355 | - * |
|
356 | - * @param int $tag Tag number |
|
357 | - * @return bool |
|
358 | - */ |
|
359 | - public function hasTagged(int $tag): bool |
|
360 | - { |
|
361 | - // lazily build lookup map |
|
362 | - if (!isset($this->_taggedMap)) { |
|
363 | - $this->_taggedMap = []; |
|
364 | - foreach ($this->_elements as $element) { |
|
365 | - if ($element->isTagged()) { |
|
366 | - $this->_taggedMap[$element->tag()] = $element; |
|
367 | - } |
|
368 | - } |
|
369 | - } |
|
370 | - return isset($this->_taggedMap[$tag]); |
|
371 | - } |
|
352 | + /** |
|
353 | + * Check whether the structure contains a context specific element with a |
|
354 | + * given tag. |
|
355 | + * |
|
356 | + * @param int $tag Tag number |
|
357 | + * @return bool |
|
358 | + */ |
|
359 | + public function hasTagged(int $tag): bool |
|
360 | + { |
|
361 | + // lazily build lookup map |
|
362 | + if (!isset($this->_taggedMap)) { |
|
363 | + $this->_taggedMap = []; |
|
364 | + foreach ($this->_elements as $element) { |
|
365 | + if ($element->isTagged()) { |
|
366 | + $this->_taggedMap[$element->tag()] = $element; |
|
367 | + } |
|
368 | + } |
|
369 | + } |
|
370 | + return isset($this->_taggedMap[$tag]); |
|
371 | + } |
|
372 | 372 | |
373 | - /** |
|
374 | - * Get a context specific element tagged with a given tag. |
|
375 | - * |
|
376 | - * @param int $tag |
|
377 | - * @throws \LogicException If tag doesn't exists |
|
378 | - * @return TaggedType |
|
379 | - */ |
|
380 | - public function getTagged(int $tag): TaggedType |
|
381 | - { |
|
382 | - if (!$this->hasTagged($tag)) { |
|
383 | - throw new \LogicException("No tagged element for tag $tag."); |
|
384 | - } |
|
385 | - return $this->_taggedMap[$tag]; |
|
386 | - } |
|
373 | + /** |
|
374 | + * Get a context specific element tagged with a given tag. |
|
375 | + * |
|
376 | + * @param int $tag |
|
377 | + * @throws \LogicException If tag doesn't exists |
|
378 | + * @return TaggedType |
|
379 | + */ |
|
380 | + public function getTagged(int $tag): TaggedType |
|
381 | + { |
|
382 | + if (!$this->hasTagged($tag)) { |
|
383 | + throw new \LogicException("No tagged element for tag $tag."); |
|
384 | + } |
|
385 | + return $this->_taggedMap[$tag]; |
|
386 | + } |
|
387 | 387 | |
388 | - /** |
|
389 | - * |
|
390 | - * @see \Countable::count() |
|
391 | - * @return int |
|
392 | - */ |
|
393 | - public function count(): int |
|
394 | - { |
|
395 | - return count($this->_elements); |
|
396 | - } |
|
388 | + /** |
|
389 | + * |
|
390 | + * @see \Countable::count() |
|
391 | + * @return int |
|
392 | + */ |
|
393 | + public function count(): int |
|
394 | + { |
|
395 | + return count($this->_elements); |
|
396 | + } |
|
397 | 397 | |
398 | - /** |
|
399 | - * Get an iterator for the UnspecifiedElement objects. |
|
400 | - * |
|
401 | - * @see \IteratorAggregate::getIterator() |
|
402 | - * @return \ArrayIterator |
|
403 | - */ |
|
404 | - public function getIterator(): \ArrayIterator |
|
405 | - { |
|
406 | - return new \ArrayIterator($this->elements()); |
|
407 | - } |
|
398 | + /** |
|
399 | + * Get an iterator for the UnspecifiedElement objects. |
|
400 | + * |
|
401 | + * @see \IteratorAggregate::getIterator() |
|
402 | + * @return \ArrayIterator |
|
403 | + */ |
|
404 | + public function getIterator(): \ArrayIterator |
|
405 | + { |
|
406 | + return new \ArrayIterator($this->elements()); |
|
407 | + } |
|
408 | 408 | } |
@@ -16,41 +16,41 @@ |
||
16 | 16 | */ |
17 | 17 | class EOC extends Element |
18 | 18 | { |
19 | - use UniversalClass; |
|
20 | - use PrimitiveType; |
|
19 | + use UniversalClass; |
|
20 | + use PrimitiveType; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Constructor. |
|
24 | - */ |
|
25 | - public function __construct() |
|
26 | - { |
|
27 | - $this->_typeTag = self::TYPE_EOC; |
|
28 | - } |
|
22 | + /** |
|
23 | + * Constructor. |
|
24 | + */ |
|
25 | + public function __construct() |
|
26 | + { |
|
27 | + $this->_typeTag = self::TYPE_EOC; |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * |
|
32 | - * {@inheritdoc} |
|
33 | - */ |
|
34 | - protected function _encodedContentDER(): string |
|
35 | - { |
|
36 | - return ''; |
|
37 | - } |
|
30 | + /** |
|
31 | + * |
|
32 | + * {@inheritdoc} |
|
33 | + */ |
|
34 | + protected function _encodedContentDER(): string |
|
35 | + { |
|
36 | + return ''; |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * |
|
41 | - * {@inheritdoc} |
|
42 | - * @return self |
|
43 | - */ |
|
44 | - protected static function _decodeFromDER(Identifier $identifier, |
|
45 | - string $data, int &$offset): ElementBase |
|
46 | - { |
|
47 | - $idx = $offset; |
|
48 | - if (!$identifier->isPrimitive()) { |
|
49 | - throw new DecodeException("EOC value must be primitive."); |
|
50 | - } |
|
51 | - // EOC type has always zero length |
|
52 | - Length::expectFromDER($data, $idx, 0); |
|
53 | - $offset = $idx; |
|
54 | - return new self(); |
|
55 | - } |
|
39 | + /** |
|
40 | + * |
|
41 | + * {@inheritdoc} |
|
42 | + * @return self |
|
43 | + */ |
|
44 | + protected static function _decodeFromDER(Identifier $identifier, |
|
45 | + string $data, int &$offset): ElementBase |
|
46 | + { |
|
47 | + $idx = $offset; |
|
48 | + if (!$identifier->isPrimitive()) { |
|
49 | + throw new DecodeException("EOC value must be primitive."); |
|
50 | + } |
|
51 | + // EOC type has always zero length |
|
52 | + Length::expectFromDER($data, $idx, 0); |
|
53 | + $offset = $idx; |
|
54 | + return new self(); |
|
55 | + } |
|
56 | 56 | } |
@@ -21,154 +21,154 @@ |
||
21 | 21 | * May be encoded back to complete DER encoding. |
22 | 22 | */ |
23 | 23 | class DERTaggedType extends TaggedType implements |
24 | - ExplicitTagging, |
|
25 | - ImplicitTagging |
|
24 | + ExplicitTagging, |
|
25 | + ImplicitTagging |
|
26 | 26 | { |
27 | - /** |
|
28 | - * Identifier. |
|
29 | - * |
|
30 | - * @var Identifier |
|
31 | - */ |
|
32 | - private $_identifier; |
|
27 | + /** |
|
28 | + * Identifier. |
|
29 | + * |
|
30 | + * @var Identifier |
|
31 | + */ |
|
32 | + private $_identifier; |
|
33 | 33 | |
34 | - /** |
|
35 | - * DER data. |
|
36 | - * |
|
37 | - * @var string |
|
38 | - */ |
|
39 | - private $_data; |
|
34 | + /** |
|
35 | + * DER data. |
|
36 | + * |
|
37 | + * @var string |
|
38 | + */ |
|
39 | + private $_data; |
|
40 | 40 | |
41 | - /** |
|
42 | - * Offset to next byte after identifier. |
|
43 | - * |
|
44 | - * @var int |
|
45 | - */ |
|
46 | - private $_offset; |
|
41 | + /** |
|
42 | + * Offset to next byte after identifier. |
|
43 | + * |
|
44 | + * @var int |
|
45 | + */ |
|
46 | + private $_offset; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Offset to content. |
|
50 | - * |
|
51 | - * @var int |
|
52 | - */ |
|
53 | - private $_valueOffset; |
|
48 | + /** |
|
49 | + * Offset to content. |
|
50 | + * |
|
51 | + * @var int |
|
52 | + */ |
|
53 | + private $_valueOffset; |
|
54 | 54 | |
55 | - /** |
|
56 | - * Length of the content. |
|
57 | - * |
|
58 | - * @var int |
|
59 | - */ |
|
60 | - private $_valueLength; |
|
55 | + /** |
|
56 | + * Length of the content. |
|
57 | + * |
|
58 | + * @var int |
|
59 | + */ |
|
60 | + private $_valueLength; |
|
61 | 61 | |
62 | - /** |
|
63 | - * Constructor. |
|
64 | - * |
|
65 | - * @param Identifier $identifier Pre-parsed identifier |
|
66 | - * @param string $data DER data |
|
67 | - * @param int $offset Offset to next byte after identifier |
|
68 | - * @param int $value_offset Offset to content |
|
69 | - * @param int $value_length Content length |
|
70 | - */ |
|
71 | - public function __construct(Identifier $identifier, string $data, |
|
72 | - int $offset, int $value_offset, int $value_length, |
|
73 | - bool $indefinite_length) |
|
74 | - { |
|
75 | - $this->_identifier = $identifier; |
|
76 | - $this->_data = $data; |
|
77 | - $this->_offset = $offset; |
|
78 | - $this->_valueOffset = $value_offset; |
|
79 | - $this->_valueLength = $value_length; |
|
80 | - $this->_indefiniteLength = $indefinite_length; |
|
81 | - $this->_typeTag = $identifier->intTag(); |
|
82 | - } |
|
62 | + /** |
|
63 | + * Constructor. |
|
64 | + * |
|
65 | + * @param Identifier $identifier Pre-parsed identifier |
|
66 | + * @param string $data DER data |
|
67 | + * @param int $offset Offset to next byte after identifier |
|
68 | + * @param int $value_offset Offset to content |
|
69 | + * @param int $value_length Content length |
|
70 | + */ |
|
71 | + public function __construct(Identifier $identifier, string $data, |
|
72 | + int $offset, int $value_offset, int $value_length, |
|
73 | + bool $indefinite_length) |
|
74 | + { |
|
75 | + $this->_identifier = $identifier; |
|
76 | + $this->_data = $data; |
|
77 | + $this->_offset = $offset; |
|
78 | + $this->_valueOffset = $value_offset; |
|
79 | + $this->_valueLength = $value_length; |
|
80 | + $this->_indefiniteLength = $indefinite_length; |
|
81 | + $this->_typeTag = $identifier->intTag(); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * |
|
86 | - * {@inheritdoc} |
|
87 | - */ |
|
88 | - protected static function _decodeFromDER(Identifier $identifier, |
|
89 | - string $data, int &$offset): ElementBase |
|
90 | - { |
|
91 | - $idx = $offset; |
|
92 | - $length = Length::expectFromDER($data, $idx); |
|
93 | - // offset to inner value |
|
94 | - $value_offset = $idx; |
|
95 | - if ($length->isIndefinite()) { |
|
96 | - if ($identifier->isPrimitive()) { |
|
97 | - throw new DecodeException( |
|
98 | - 'Primitive type with indefinite length is not supported.'); |
|
99 | - } |
|
100 | - while (!Element::fromDER($data, $idx)->isType(self::TYPE_EOC)); |
|
101 | - // EOC consists of two octets. |
|
102 | - $value_length = $idx - $value_offset - 2; |
|
103 | - } else { |
|
104 | - $value_length = $length->intLength(); |
|
105 | - $idx += $value_length; |
|
106 | - } |
|
107 | - // late static binding since ApplicationType and PrivateType extend this class |
|
108 | - $type = new static($identifier, $data, $offset, $value_offset, |
|
109 | - $value_length, $length->isIndefinite()); |
|
110 | - $offset = $idx; |
|
111 | - return $type; |
|
112 | - } |
|
84 | + /** |
|
85 | + * |
|
86 | + * {@inheritdoc} |
|
87 | + */ |
|
88 | + protected static function _decodeFromDER(Identifier $identifier, |
|
89 | + string $data, int &$offset): ElementBase |
|
90 | + { |
|
91 | + $idx = $offset; |
|
92 | + $length = Length::expectFromDER($data, $idx); |
|
93 | + // offset to inner value |
|
94 | + $value_offset = $idx; |
|
95 | + if ($length->isIndefinite()) { |
|
96 | + if ($identifier->isPrimitive()) { |
|
97 | + throw new DecodeException( |
|
98 | + 'Primitive type with indefinite length is not supported.'); |
|
99 | + } |
|
100 | + while (!Element::fromDER($data, $idx)->isType(self::TYPE_EOC)); |
|
101 | + // EOC consists of two octets. |
|
102 | + $value_length = $idx - $value_offset - 2; |
|
103 | + } else { |
|
104 | + $value_length = $length->intLength(); |
|
105 | + $idx += $value_length; |
|
106 | + } |
|
107 | + // late static binding since ApplicationType and PrivateType extend this class |
|
108 | + $type = new static($identifier, $data, $offset, $value_offset, |
|
109 | + $value_length, $length->isIndefinite()); |
|
110 | + $offset = $idx; |
|
111 | + return $type; |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * |
|
116 | - * @see \ASN1\Element::typeClass() |
|
117 | - * @return int |
|
118 | - */ |
|
119 | - public function typeClass(): int |
|
120 | - { |
|
121 | - return $this->_identifier->typeClass(); |
|
122 | - } |
|
114 | + /** |
|
115 | + * |
|
116 | + * @see \ASN1\Element::typeClass() |
|
117 | + * @return int |
|
118 | + */ |
|
119 | + public function typeClass(): int |
|
120 | + { |
|
121 | + return $this->_identifier->typeClass(); |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * |
|
126 | - * @see \ASN1\Element::isConstructed() |
|
127 | - * @return bool |
|
128 | - */ |
|
129 | - public function isConstructed(): bool |
|
130 | - { |
|
131 | - return $this->_identifier->isConstructed(); |
|
132 | - } |
|
124 | + /** |
|
125 | + * |
|
126 | + * @see \ASN1\Element::isConstructed() |
|
127 | + * @return bool |
|
128 | + */ |
|
129 | + public function isConstructed(): bool |
|
130 | + { |
|
131 | + return $this->_identifier->isConstructed(); |
|
132 | + } |
|
133 | 133 | |
134 | - /** |
|
135 | - * |
|
136 | - * @see \ASN1\Element::_encodedContentDER() |
|
137 | - * @return string |
|
138 | - */ |
|
139 | - protected function _encodedContentDER(): string |
|
140 | - { |
|
141 | - return substr($this->_data, $this->_valueOffset, $this->_valueLength); |
|
142 | - } |
|
134 | + /** |
|
135 | + * |
|
136 | + * @see \ASN1\Element::_encodedContentDER() |
|
137 | + * @return string |
|
138 | + */ |
|
139 | + protected function _encodedContentDER(): string |
|
140 | + { |
|
141 | + return substr($this->_data, $this->_valueOffset, $this->_valueLength); |
|
142 | + } |
|
143 | 143 | |
144 | - /** |
|
145 | - * |
|
146 | - * {@inheritdoc} |
|
147 | - * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
148 | - * @return UnspecifiedType |
|
149 | - */ |
|
150 | - public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
151 | - { |
|
152 | - $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
153 | - $cls = self::_determineImplClass($identifier); |
|
154 | - $idx = $this->_offset; |
|
155 | - /** @var \ASN1\Feature\ElementBase $element */ |
|
156 | - $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
157 | - return $element->asUnspecified(); |
|
158 | - } |
|
144 | + /** |
|
145 | + * |
|
146 | + * {@inheritdoc} |
|
147 | + * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
148 | + * @return UnspecifiedType |
|
149 | + */ |
|
150 | + public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
151 | + { |
|
152 | + $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
153 | + $cls = self::_determineImplClass($identifier); |
|
154 | + $idx = $this->_offset; |
|
155 | + /** @var \ASN1\Feature\ElementBase $element */ |
|
156 | + $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
157 | + return $element->asUnspecified(); |
|
158 | + } |
|
159 | 159 | |
160 | - /** |
|
161 | - * |
|
162 | - * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
163 | - * @return UnspecifiedType |
|
164 | - */ |
|
165 | - public function explicit($expectedTag = null): UnspecifiedType |
|
166 | - { |
|
167 | - $idx = $this->_valueOffset; |
|
168 | - $element = Element::fromDER($this->_data, $idx); |
|
169 | - if (isset($expectedTag)) { |
|
170 | - $element->expectType($expectedTag); |
|
171 | - } |
|
172 | - return $element->asUnspecified(); |
|
173 | - } |
|
160 | + /** |
|
161 | + * |
|
162 | + * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
163 | + * @return UnspecifiedType |
|
164 | + */ |
|
165 | + public function explicit($expectedTag = null): UnspecifiedType |
|
166 | + { |
|
167 | + $idx = $this->_valueOffset; |
|
168 | + $element = Element::fromDER($this->_data, $idx); |
|
169 | + if (isset($expectedTag)) { |
|
170 | + $element->expectType($expectedTag); |
|
171 | + } |
|
172 | + return $element->asUnspecified(); |
|
173 | + } |
|
174 | 174 | } |
@@ -13,71 +13,71 @@ |
||
13 | 13 | */ |
14 | 14 | abstract class TaggedType extends Element |
15 | 15 | { |
16 | - /** |
|
17 | - * Check whether element supports explicit tagging. |
|
18 | - * |
|
19 | - * @param int|null $expectedTag Optional outer tag expectation |
|
20 | - * @throws \UnexpectedValueException If expectation fails |
|
21 | - * @return ExplicitTagging |
|
22 | - */ |
|
23 | - public function expectExplicit($expectedTag = null): ExplicitTagging |
|
24 | - { |
|
25 | - $el = $this; |
|
26 | - if (!$el instanceof ExplicitTagging) { |
|
27 | - throw new \UnexpectedValueException( |
|
28 | - "Element doesn't implement explicit tagging."); |
|
29 | - } |
|
30 | - if (isset($expectedTag)) { |
|
31 | - $el->expectTagged($expectedTag); |
|
32 | - } |
|
33 | - return $el; |
|
34 | - } |
|
16 | + /** |
|
17 | + * Check whether element supports explicit tagging. |
|
18 | + * |
|
19 | + * @param int|null $expectedTag Optional outer tag expectation |
|
20 | + * @throws \UnexpectedValueException If expectation fails |
|
21 | + * @return ExplicitTagging |
|
22 | + */ |
|
23 | + public function expectExplicit($expectedTag = null): ExplicitTagging |
|
24 | + { |
|
25 | + $el = $this; |
|
26 | + if (!$el instanceof ExplicitTagging) { |
|
27 | + throw new \UnexpectedValueException( |
|
28 | + "Element doesn't implement explicit tagging."); |
|
29 | + } |
|
30 | + if (isset($expectedTag)) { |
|
31 | + $el->expectTagged($expectedTag); |
|
32 | + } |
|
33 | + return $el; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * Get the wrapped inner element employing explicit tagging. |
|
38 | - * |
|
39 | - * @param int|null $expectedTag Optional outer tag expectation |
|
40 | - * @throws \UnexpectedValueException If expectation fails |
|
41 | - * @return UnspecifiedType |
|
42 | - */ |
|
43 | - public function asExplicit($expectedTag = null): UnspecifiedType |
|
44 | - { |
|
45 | - return $this->expectExplicit($expectedTag)->explicit(); |
|
46 | - } |
|
36 | + /** |
|
37 | + * Get the wrapped inner element employing explicit tagging. |
|
38 | + * |
|
39 | + * @param int|null $expectedTag Optional outer tag expectation |
|
40 | + * @throws \UnexpectedValueException If expectation fails |
|
41 | + * @return UnspecifiedType |
|
42 | + */ |
|
43 | + public function asExplicit($expectedTag = null): UnspecifiedType |
|
44 | + { |
|
45 | + return $this->expectExplicit($expectedTag)->explicit(); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Check whether element supports implicit tagging. |
|
50 | - * |
|
51 | - * @param int|null $expectedTag Optional outer tag expectation |
|
52 | - * @throws \UnexpectedValueException If expectation fails |
|
53 | - * @return ImplicitTagging |
|
54 | - */ |
|
55 | - public function expectImplicit($expectedTag = null): ImplicitTagging |
|
56 | - { |
|
57 | - $el = $this; |
|
58 | - if (!$el instanceof ImplicitTagging) { |
|
59 | - throw new \UnexpectedValueException( |
|
60 | - "Element doesn't implement implicit tagging."); |
|
61 | - } |
|
62 | - if (isset($expectedTag)) { |
|
63 | - $el->expectTagged($expectedTag); |
|
64 | - } |
|
65 | - return $el; |
|
66 | - } |
|
48 | + /** |
|
49 | + * Check whether element supports implicit tagging. |
|
50 | + * |
|
51 | + * @param int|null $expectedTag Optional outer tag expectation |
|
52 | + * @throws \UnexpectedValueException If expectation fails |
|
53 | + * @return ImplicitTagging |
|
54 | + */ |
|
55 | + public function expectImplicit($expectedTag = null): ImplicitTagging |
|
56 | + { |
|
57 | + $el = $this; |
|
58 | + if (!$el instanceof ImplicitTagging) { |
|
59 | + throw new \UnexpectedValueException( |
|
60 | + "Element doesn't implement implicit tagging."); |
|
61 | + } |
|
62 | + if (isset($expectedTag)) { |
|
63 | + $el->expectTagged($expectedTag); |
|
64 | + } |
|
65 | + return $el; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * Get the wrapped inner element employing implicit tagging. |
|
70 | - * |
|
71 | - * @param int $tag Type tag of the inner element |
|
72 | - * @param int|null $expectedTag Optional outer tag expectation |
|
73 | - * @param int $expectedClass Optional inner type class expectation |
|
74 | - * @throws \UnexpectedValueException If expectation fails |
|
75 | - * @return UnspecifiedType |
|
76 | - */ |
|
77 | - public function asImplicit(int $tag, $expectedTag = null, |
|
78 | - int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
79 | - { |
|
80 | - return $this->expectImplicit($expectedTag)->implicit($tag, |
|
81 | - $expectedClass); |
|
82 | - } |
|
68 | + /** |
|
69 | + * Get the wrapped inner element employing implicit tagging. |
|
70 | + * |
|
71 | + * @param int $tag Type tag of the inner element |
|
72 | + * @param int|null $expectedTag Optional outer tag expectation |
|
73 | + * @param int $expectedClass Optional inner type class expectation |
|
74 | + * @throws \UnexpectedValueException If expectation fails |
|
75 | + * @return UnspecifiedType |
|
76 | + */ |
|
77 | + public function asImplicit(int $tag, $expectedTag = null, |
|
78 | + int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
79 | + { |
|
80 | + return $this->expectImplicit($expectedTag)->implicit($tag, |
|
81 | + $expectedClass); |
|
82 | + } |
|
83 | 83 | } |
@@ -16,682 +16,682 @@ |
||
16 | 16 | */ |
17 | 17 | class UnspecifiedType implements ElementBase |
18 | 18 | { |
19 | - /** |
|
20 | - * The wrapped element. |
|
21 | - * |
|
22 | - * @var Element |
|
23 | - */ |
|
24 | - private $_element; |
|
25 | - |
|
26 | - /** |
|
27 | - * Constructor. |
|
28 | - * |
|
29 | - * @param Element $el |
|
30 | - */ |
|
31 | - public function __construct(Element $el) |
|
32 | - { |
|
33 | - $this->_element = $el; |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Initialize from DER data. |
|
38 | - * |
|
39 | - * @param string $data DER encoded data |
|
40 | - * @return self |
|
41 | - */ |
|
42 | - public static function fromDER(string $data): self |
|
43 | - { |
|
44 | - return Element::fromDER($data)->asUnspecified(); |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Initialize from ElementBase interface. |
|
49 | - * |
|
50 | - * @param ElementBase $el |
|
51 | - * @return self |
|
52 | - */ |
|
53 | - public static function fromElementBase(ElementBase $el): self |
|
54 | - { |
|
55 | - // if element is already wrapped |
|
56 | - if ($el instanceof self) { |
|
57 | - return $el; |
|
58 | - } |
|
59 | - return new self($el->asElement()); |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * Compatibility method to dispatch calls to the wrapped element. |
|
64 | - * |
|
65 | - * @deprecated Use <code>as*</code> accessor methods to ensure strict type |
|
66 | - * @param string $mtd Method name |
|
67 | - * @param array $args Arguments |
|
68 | - * @return mixed |
|
69 | - */ |
|
70 | - public function __call($mtd, array $args) |
|
71 | - { |
|
72 | - return call_user_func_array([$this->_element, $mtd], $args); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Get the wrapped element as a context specific tagged type. |
|
77 | - * |
|
78 | - * @throws \UnexpectedValueException If the element is not tagged |
|
79 | - * @return TaggedType |
|
80 | - */ |
|
81 | - public function asTagged(): TaggedType |
|
82 | - { |
|
83 | - if (!$this->_element instanceof TaggedType) { |
|
84 | - throw new \UnexpectedValueException( |
|
85 | - "Tagged element expected, got " . $this->_typeDescriptorString()); |
|
86 | - } |
|
87 | - return $this->_element; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Get the wrapped element as an application specific type. |
|
92 | - * |
|
93 | - * @throws \UnexpectedValueException If element is not application specific |
|
94 | - * @return \ASN1\Type\Tagged\ApplicationType |
|
95 | - */ |
|
96 | - public function asApplication(): Tagged\ApplicationType |
|
97 | - { |
|
98 | - if (!$this->_element instanceof Tagged\ApplicationType) { |
|
99 | - throw new \UnexpectedValueException( |
|
100 | - "Application type expected, got " . |
|
101 | - $this->_typeDescriptorString()); |
|
102 | - } |
|
103 | - return $this->_element; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Get the wrapped element as a private tagged type. |
|
108 | - * |
|
109 | - * @throws \UnexpectedValueException If element is not using private tagging |
|
110 | - * @return \ASN1\Type\Tagged\PrivateType |
|
111 | - */ |
|
112 | - public function asPrivate(): Tagged\PrivateType |
|
113 | - { |
|
114 | - if (!$this->_element instanceof Tagged\PrivateType) { |
|
115 | - throw new \UnexpectedValueException( |
|
116 | - "Private type expected, got " . $this->_typeDescriptorString()); |
|
117 | - } |
|
118 | - return $this->_element; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Get the wrapped element as a boolean type. |
|
123 | - * |
|
124 | - * @throws \UnexpectedValueException If the element is not a boolean |
|
125 | - * @return \ASN1\Type\Primitive\Boolean |
|
126 | - */ |
|
127 | - public function asBoolean(): Primitive\Boolean |
|
128 | - { |
|
129 | - if (!$this->_element instanceof Primitive\Boolean) { |
|
130 | - throw new \UnexpectedValueException( |
|
131 | - $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
132 | - } |
|
133 | - return $this->_element; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Get the wrapped element as an integer type. |
|
138 | - * |
|
139 | - * @throws \UnexpectedValueException If the element is not an integer |
|
140 | - * @return \ASN1\Type\Primitive\Integer |
|
141 | - */ |
|
142 | - public function asInteger(): Primitive\Integer |
|
143 | - { |
|
144 | - if (!$this->_element instanceof Primitive\Integer) { |
|
145 | - throw new \UnexpectedValueException( |
|
146 | - $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
147 | - } |
|
148 | - return $this->_element; |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Get the wrapped element as a bit string type. |
|
153 | - * |
|
154 | - * @throws \UnexpectedValueException If the element is not a bit string |
|
155 | - * @return \ASN1\Type\Primitive\BitString |
|
156 | - */ |
|
157 | - public function asBitString(): Primitive\BitString |
|
158 | - { |
|
159 | - if (!$this->_element instanceof Primitive\BitString) { |
|
160 | - throw new \UnexpectedValueException( |
|
161 | - $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
162 | - } |
|
163 | - return $this->_element; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Get the wrapped element as an octet string type. |
|
168 | - * |
|
169 | - * @throws \UnexpectedValueException If the element is not an octet string |
|
170 | - * @return \ASN1\Type\Primitive\OctetString |
|
171 | - */ |
|
172 | - public function asOctetString(): Primitive\OctetString |
|
173 | - { |
|
174 | - if (!$this->_element instanceof Primitive\OctetString) { |
|
175 | - throw new \UnexpectedValueException( |
|
176 | - $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
177 | - } |
|
178 | - return $this->_element; |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Get the wrapped element as a null type. |
|
183 | - * |
|
184 | - * @throws \UnexpectedValueException If the element is not a null |
|
185 | - * @return \ASN1\Type\Primitive\NullType |
|
186 | - */ |
|
187 | - public function asNull(): Primitive\NullType |
|
188 | - { |
|
189 | - if (!$this->_element instanceof Primitive\NullType) { |
|
190 | - throw new \UnexpectedValueException( |
|
191 | - $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
192 | - } |
|
193 | - return $this->_element; |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Get the wrapped element as an object identifier type. |
|
198 | - * |
|
199 | - * @throws \UnexpectedValueException If the element is not an object |
|
200 | - * identifier |
|
201 | - * @return \ASN1\Type\Primitive\ObjectIdentifier |
|
202 | - */ |
|
203 | - public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
204 | - { |
|
205 | - if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
206 | - throw new \UnexpectedValueException( |
|
207 | - $this->_generateExceptionMessage( |
|
208 | - Element::TYPE_OBJECT_IDENTIFIER)); |
|
209 | - } |
|
210 | - return $this->_element; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Get the wrapped element as an object descriptor type. |
|
215 | - * |
|
216 | - * @throws \UnexpectedValueException If the element is not an object |
|
217 | - * descriptor |
|
218 | - * @return \ASN1\Type\Primitive\ObjectDescriptor |
|
219 | - */ |
|
220 | - public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
221 | - { |
|
222 | - if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
223 | - throw new \UnexpectedValueException( |
|
224 | - $this->_generateExceptionMessage( |
|
225 | - Element::TYPE_OBJECT_DESCRIPTOR)); |
|
226 | - } |
|
227 | - return $this->_element; |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Get the wrapped element as a real type. |
|
232 | - * |
|
233 | - * @throws \UnexpectedValueException If the element is not a real |
|
234 | - * @return \ASN1\Type\Primitive\Real |
|
235 | - */ |
|
236 | - public function asReal(): Primitive\Real |
|
237 | - { |
|
238 | - if (!$this->_element instanceof Primitive\Real) { |
|
239 | - throw new \UnexpectedValueException( |
|
240 | - $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
241 | - } |
|
242 | - return $this->_element; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Get the wrapped element as an enumerated type. |
|
247 | - * |
|
248 | - * @throws \UnexpectedValueException If the element is not an enumerated |
|
249 | - * @return \ASN1\Type\Primitive\Enumerated |
|
250 | - */ |
|
251 | - public function asEnumerated(): Primitive\Enumerated |
|
252 | - { |
|
253 | - if (!$this->_element instanceof Primitive\Enumerated) { |
|
254 | - throw new \UnexpectedValueException( |
|
255 | - $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
256 | - } |
|
257 | - return $this->_element; |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * Get the wrapped element as a UTF8 string type. |
|
262 | - * |
|
263 | - * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
264 | - * @return \ASN1\Type\Primitive\UTF8String |
|
265 | - */ |
|
266 | - public function asUTF8String(): Primitive\UTF8String |
|
267 | - { |
|
268 | - if (!$this->_element instanceof Primitive\UTF8String) { |
|
269 | - throw new \UnexpectedValueException( |
|
270 | - $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
271 | - } |
|
272 | - return $this->_element; |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * Get the wrapped element as a relative OID type. |
|
277 | - * |
|
278 | - * @throws \UnexpectedValueException If the element is not a relative OID |
|
279 | - * @return \ASN1\Type\Primitive\RelativeOID |
|
280 | - */ |
|
281 | - public function asRelativeOID(): Primitive\RelativeOID |
|
282 | - { |
|
283 | - if (!$this->_element instanceof Primitive\RelativeOID) { |
|
284 | - throw new \UnexpectedValueException( |
|
285 | - $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
286 | - } |
|
287 | - return $this->_element; |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Get the wrapped element as a sequence type. |
|
292 | - * |
|
293 | - * @throws \UnexpectedValueException If the element is not a sequence |
|
294 | - * @return \ASN1\Type\Constructed\Sequence |
|
295 | - */ |
|
296 | - public function asSequence(): Constructed\Sequence |
|
297 | - { |
|
298 | - if (!$this->_element instanceof Constructed\Sequence) { |
|
299 | - throw new \UnexpectedValueException( |
|
300 | - $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
301 | - } |
|
302 | - return $this->_element; |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * Get the wrapped element as a set type. |
|
307 | - * |
|
308 | - * @throws \UnexpectedValueException If the element is not a set |
|
309 | - * @return \ASN1\Type\Constructed\Set |
|
310 | - */ |
|
311 | - public function asSet(): Constructed\Set |
|
312 | - { |
|
313 | - if (!$this->_element instanceof Constructed\Set) { |
|
314 | - throw new \UnexpectedValueException( |
|
315 | - $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
316 | - } |
|
317 | - return $this->_element; |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * Get the wrapped element as a numeric string type. |
|
322 | - * |
|
323 | - * @throws \UnexpectedValueException If the element is not a numeric string |
|
324 | - * @return \ASN1\Type\Primitive\NumericString |
|
325 | - */ |
|
326 | - public function asNumericString(): Primitive\NumericString |
|
327 | - { |
|
328 | - if (!$this->_element instanceof Primitive\NumericString) { |
|
329 | - throw new \UnexpectedValueException( |
|
330 | - $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
331 | - } |
|
332 | - return $this->_element; |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * Get the wrapped element as a printable string type. |
|
337 | - * |
|
338 | - * @throws \UnexpectedValueException If the element is not a printable |
|
339 | - * string |
|
340 | - * @return \ASN1\Type\Primitive\PrintableString |
|
341 | - */ |
|
342 | - public function asPrintableString(): Primitive\PrintableString |
|
343 | - { |
|
344 | - if (!$this->_element instanceof Primitive\PrintableString) { |
|
345 | - throw new \UnexpectedValueException( |
|
346 | - $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
347 | - } |
|
348 | - return $this->_element; |
|
349 | - } |
|
350 | - |
|
351 | - /** |
|
352 | - * Get the wrapped element as a T61 string type. |
|
353 | - * |
|
354 | - * @throws \UnexpectedValueException If the element is not a T61 string |
|
355 | - * @return \ASN1\Type\Primitive\T61String |
|
356 | - */ |
|
357 | - public function asT61String(): Primitive\T61String |
|
358 | - { |
|
359 | - if (!$this->_element instanceof Primitive\T61String) { |
|
360 | - throw new \UnexpectedValueException( |
|
361 | - $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
362 | - } |
|
363 | - return $this->_element; |
|
364 | - } |
|
365 | - |
|
366 | - /** |
|
367 | - * Get the wrapped element as a videotex string type. |
|
368 | - * |
|
369 | - * @throws \UnexpectedValueException If the element is not a videotex string |
|
370 | - * @return \ASN1\Type\Primitive\VideotexString |
|
371 | - */ |
|
372 | - public function asVideotexString(): Primitive\VideotexString |
|
373 | - { |
|
374 | - if (!$this->_element instanceof Primitive\VideotexString) { |
|
375 | - throw new \UnexpectedValueException( |
|
376 | - $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
377 | - } |
|
378 | - return $this->_element; |
|
379 | - } |
|
380 | - |
|
381 | - /** |
|
382 | - * Get the wrapped element as a IA5 string type. |
|
383 | - * |
|
384 | - * @throws \UnexpectedValueException If the element is not a IA5 string |
|
385 | - * @return \ASN1\Type\Primitive\IA5String |
|
386 | - */ |
|
387 | - public function asIA5String(): Primitive\IA5String |
|
388 | - { |
|
389 | - if (!$this->_element instanceof Primitive\IA5String) { |
|
390 | - throw new \UnexpectedValueException( |
|
391 | - $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
392 | - } |
|
393 | - return $this->_element; |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * Get the wrapped element as an UTC time type. |
|
398 | - * |
|
399 | - * @throws \UnexpectedValueException If the element is not a UTC time |
|
400 | - * @return \ASN1\Type\Primitive\UTCTime |
|
401 | - */ |
|
402 | - public function asUTCTime(): Primitive\UTCTime |
|
403 | - { |
|
404 | - if (!$this->_element instanceof Primitive\UTCTime) { |
|
405 | - throw new \UnexpectedValueException( |
|
406 | - $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
407 | - } |
|
408 | - return $this->_element; |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * Get the wrapped element as a generalized time type. |
|
413 | - * |
|
414 | - * @throws \UnexpectedValueException If the element is not a generalized |
|
415 | - * time |
|
416 | - * @return \ASN1\Type\Primitive\GeneralizedTime |
|
417 | - */ |
|
418 | - public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
419 | - { |
|
420 | - if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
421 | - throw new \UnexpectedValueException( |
|
422 | - $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
423 | - } |
|
424 | - return $this->_element; |
|
425 | - } |
|
426 | - |
|
427 | - /** |
|
428 | - * Get the wrapped element as a graphic string type. |
|
429 | - * |
|
430 | - * @throws \UnexpectedValueException If the element is not a graphic string |
|
431 | - * @return \ASN1\Type\Primitive\GraphicString |
|
432 | - */ |
|
433 | - public function asGraphicString(): Primitive\GraphicString |
|
434 | - { |
|
435 | - if (!$this->_element instanceof Primitive\GraphicString) { |
|
436 | - throw new \UnexpectedValueException( |
|
437 | - $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
438 | - } |
|
439 | - return $this->_element; |
|
440 | - } |
|
441 | - |
|
442 | - /** |
|
443 | - * Get the wrapped element as a visible string type. |
|
444 | - * |
|
445 | - * @throws \UnexpectedValueException If the element is not a visible string |
|
446 | - * @return \ASN1\Type\Primitive\VisibleString |
|
447 | - */ |
|
448 | - public function asVisibleString(): Primitive\VisibleString |
|
449 | - { |
|
450 | - if (!$this->_element instanceof Primitive\VisibleString) { |
|
451 | - throw new \UnexpectedValueException( |
|
452 | - $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
453 | - } |
|
454 | - return $this->_element; |
|
455 | - } |
|
456 | - |
|
457 | - /** |
|
458 | - * Get the wrapped element as a general string type. |
|
459 | - * |
|
460 | - * @throws \UnexpectedValueException If the element is not general string |
|
461 | - * @return \ASN1\Type\Primitive\GeneralString |
|
462 | - */ |
|
463 | - public function asGeneralString(): Primitive\GeneralString |
|
464 | - { |
|
465 | - if (!$this->_element instanceof Primitive\GeneralString) { |
|
466 | - throw new \UnexpectedValueException( |
|
467 | - $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
468 | - } |
|
469 | - return $this->_element; |
|
470 | - } |
|
471 | - |
|
472 | - /** |
|
473 | - * Get the wrapped element as a universal string type. |
|
474 | - * |
|
475 | - * @throws \UnexpectedValueException If the element is not a universal |
|
476 | - * string |
|
477 | - * @return \ASN1\Type\Primitive\UniversalString |
|
478 | - */ |
|
479 | - public function asUniversalString(): Primitive\UniversalString |
|
480 | - { |
|
481 | - if (!$this->_element instanceof Primitive\UniversalString) { |
|
482 | - throw new \UnexpectedValueException( |
|
483 | - $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
484 | - } |
|
485 | - return $this->_element; |
|
486 | - } |
|
487 | - |
|
488 | - /** |
|
489 | - * Get the wrapped element as a character string type. |
|
490 | - * |
|
491 | - * @throws \UnexpectedValueException If the element is not a character |
|
492 | - * string |
|
493 | - * @return \ASN1\Type\Primitive\CharacterString |
|
494 | - */ |
|
495 | - public function asCharacterString(): Primitive\CharacterString |
|
496 | - { |
|
497 | - if (!$this->_element instanceof Primitive\CharacterString) { |
|
498 | - throw new \UnexpectedValueException( |
|
499 | - $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
500 | - } |
|
501 | - return $this->_element; |
|
502 | - } |
|
503 | - |
|
504 | - /** |
|
505 | - * Get the wrapped element as a BMP string type. |
|
506 | - * |
|
507 | - * @throws \UnexpectedValueException If the element is not a bmp string |
|
508 | - * @return \ASN1\Type\Primitive\BMPString |
|
509 | - */ |
|
510 | - public function asBMPString(): Primitive\BMPString |
|
511 | - { |
|
512 | - if (!$this->_element instanceof Primitive\BMPString) { |
|
513 | - throw new \UnexpectedValueException( |
|
514 | - $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
515 | - } |
|
516 | - return $this->_element; |
|
517 | - } |
|
518 | - |
|
519 | - /** |
|
520 | - * Get the wrapped element as a constructed string type. |
|
521 | - * |
|
522 | - * @throws \UnexpectedValueException If the element is not a constructed |
|
523 | - * string |
|
524 | - * @return Constructed\ConstructedString |
|
525 | - */ |
|
526 | - public function asConstructedString(): Constructed\ConstructedString |
|
527 | - { |
|
528 | - if (!$this->_element instanceof Constructed\ConstructedString) { |
|
529 | - throw new \UnexpectedValueException( |
|
530 | - $this->_generateExceptionMessage( |
|
531 | - Element::TYPE_CONSTRUCTED_STRING)); |
|
532 | - } |
|
533 | - return $this->_element; |
|
534 | - } |
|
535 | - |
|
536 | - /** |
|
537 | - * Get the wrapped element as any string type. |
|
538 | - * |
|
539 | - * @throws \UnexpectedValueException If the element is not a string |
|
540 | - * @return StringType |
|
541 | - */ |
|
542 | - public function asString(): StringType |
|
543 | - { |
|
544 | - if (!$this->_element instanceof StringType) { |
|
545 | - throw new \UnexpectedValueException( |
|
546 | - $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
547 | - } |
|
548 | - return $this->_element; |
|
549 | - } |
|
550 | - |
|
551 | - /** |
|
552 | - * Get the wrapped element as any time type. |
|
553 | - * |
|
554 | - * @throws \UnexpectedValueException If the element is not a time |
|
555 | - * @return TimeType |
|
556 | - */ |
|
557 | - public function asTime(): TimeType |
|
558 | - { |
|
559 | - if (!$this->_element instanceof TimeType) { |
|
560 | - throw new \UnexpectedValueException( |
|
561 | - $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
562 | - } |
|
563 | - return $this->_element; |
|
564 | - } |
|
565 | - |
|
566 | - /** |
|
567 | - * Generate message for exceptions thrown by <code>as*</code> methods. |
|
568 | - * |
|
569 | - * @param int $tag Type tag of the expected element |
|
570 | - * @return string |
|
571 | - */ |
|
572 | - private function _generateExceptionMessage(int $tag): string |
|
573 | - { |
|
574 | - return sprintf("%s expected, got %s.", Element::tagToName($tag), |
|
575 | - $this->_typeDescriptorString()); |
|
576 | - } |
|
577 | - |
|
578 | - /** |
|
579 | - * Get textual description of the wrapped element for debugging purposes. |
|
580 | - * |
|
581 | - * @return string |
|
582 | - */ |
|
583 | - private function _typeDescriptorString(): string |
|
584 | - { |
|
585 | - $type_cls = $this->_element->typeClass(); |
|
586 | - $tag = $this->_element->tag(); |
|
587 | - if ($type_cls == Identifier::CLASS_UNIVERSAL) { |
|
588 | - return Element::tagToName($tag); |
|
589 | - } |
|
590 | - return Identifier::classToName($type_cls) . " TAG $tag"; |
|
591 | - } |
|
592 | - |
|
593 | - /** |
|
594 | - * |
|
595 | - * @see \ASN1\Feature\Encodable::toDER() |
|
596 | - * @return string |
|
597 | - */ |
|
598 | - public function toDER(): string |
|
599 | - { |
|
600 | - return $this->_element->toDER(); |
|
601 | - } |
|
602 | - |
|
603 | - /** |
|
604 | - * |
|
605 | - * @see \ASN1\Feature\ElementBase::typeClass() |
|
606 | - * @return int |
|
607 | - */ |
|
608 | - public function typeClass(): int |
|
609 | - { |
|
610 | - return $this->_element->typeClass(); |
|
611 | - } |
|
612 | - |
|
613 | - /** |
|
614 | - * |
|
615 | - * @see \ASN1\Feature\ElementBase::isConstructed() |
|
616 | - * @return bool |
|
617 | - */ |
|
618 | - public function isConstructed(): bool |
|
619 | - { |
|
620 | - return $this->_element->isConstructed(); |
|
621 | - } |
|
622 | - |
|
623 | - /** |
|
624 | - * |
|
625 | - * @see \ASN1\Feature\ElementBase::tag() |
|
626 | - * @return int |
|
627 | - */ |
|
628 | - public function tag(): int |
|
629 | - { |
|
630 | - return $this->_element->tag(); |
|
631 | - } |
|
632 | - |
|
633 | - /** |
|
634 | - * |
|
635 | - * {@inheritdoc} |
|
636 | - * @see \ASN1\Feature\ElementBase::isType() |
|
637 | - * @return bool |
|
638 | - */ |
|
639 | - public function isType(int $tag): bool |
|
640 | - { |
|
641 | - return $this->_element->isType($tag); |
|
642 | - } |
|
643 | - |
|
644 | - /** |
|
645 | - * |
|
646 | - * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
647 | - * type strictness. |
|
648 | - * @see \ASN1\Feature\ElementBase::expectType() |
|
649 | - * @return ElementBase |
|
650 | - */ |
|
651 | - public function expectType(int $tag): ElementBase |
|
652 | - { |
|
653 | - return $this->_element->expectType($tag); |
|
654 | - } |
|
655 | - |
|
656 | - /** |
|
657 | - * |
|
658 | - * @see \ASN1\Feature\ElementBase::isTagged() |
|
659 | - * @return bool |
|
660 | - */ |
|
661 | - public function isTagged(): bool |
|
662 | - { |
|
663 | - return $this->_element->isTagged(); |
|
664 | - } |
|
665 | - |
|
666 | - /** |
|
667 | - * |
|
668 | - * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
669 | - * type strictness. |
|
670 | - * @see \ASN1\Feature\ElementBase::expectTagged() |
|
671 | - * @return TaggedType |
|
672 | - */ |
|
673 | - public function expectTagged($tag = null): TaggedType |
|
674 | - { |
|
675 | - return $this->_element->expectTagged($tag); |
|
676 | - } |
|
677 | - |
|
678 | - /** |
|
679 | - * |
|
680 | - * @see \ASN1\Feature\ElementBase::asElement() |
|
681 | - * @return Element |
|
682 | - */ |
|
683 | - public function asElement(): Element |
|
684 | - { |
|
685 | - return $this->_element; |
|
686 | - } |
|
687 | - |
|
688 | - /** |
|
689 | - * |
|
690 | - * {@inheritdoc} |
|
691 | - * @return UnspecifiedType |
|
692 | - */ |
|
693 | - public function asUnspecified(): UnspecifiedType |
|
694 | - { |
|
695 | - return $this; |
|
696 | - } |
|
19 | + /** |
|
20 | + * The wrapped element. |
|
21 | + * |
|
22 | + * @var Element |
|
23 | + */ |
|
24 | + private $_element; |
|
25 | + |
|
26 | + /** |
|
27 | + * Constructor. |
|
28 | + * |
|
29 | + * @param Element $el |
|
30 | + */ |
|
31 | + public function __construct(Element $el) |
|
32 | + { |
|
33 | + $this->_element = $el; |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Initialize from DER data. |
|
38 | + * |
|
39 | + * @param string $data DER encoded data |
|
40 | + * @return self |
|
41 | + */ |
|
42 | + public static function fromDER(string $data): self |
|
43 | + { |
|
44 | + return Element::fromDER($data)->asUnspecified(); |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Initialize from ElementBase interface. |
|
49 | + * |
|
50 | + * @param ElementBase $el |
|
51 | + * @return self |
|
52 | + */ |
|
53 | + public static function fromElementBase(ElementBase $el): self |
|
54 | + { |
|
55 | + // if element is already wrapped |
|
56 | + if ($el instanceof self) { |
|
57 | + return $el; |
|
58 | + } |
|
59 | + return new self($el->asElement()); |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * Compatibility method to dispatch calls to the wrapped element. |
|
64 | + * |
|
65 | + * @deprecated Use <code>as*</code> accessor methods to ensure strict type |
|
66 | + * @param string $mtd Method name |
|
67 | + * @param array $args Arguments |
|
68 | + * @return mixed |
|
69 | + */ |
|
70 | + public function __call($mtd, array $args) |
|
71 | + { |
|
72 | + return call_user_func_array([$this->_element, $mtd], $args); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Get the wrapped element as a context specific tagged type. |
|
77 | + * |
|
78 | + * @throws \UnexpectedValueException If the element is not tagged |
|
79 | + * @return TaggedType |
|
80 | + */ |
|
81 | + public function asTagged(): TaggedType |
|
82 | + { |
|
83 | + if (!$this->_element instanceof TaggedType) { |
|
84 | + throw new \UnexpectedValueException( |
|
85 | + "Tagged element expected, got " . $this->_typeDescriptorString()); |
|
86 | + } |
|
87 | + return $this->_element; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Get the wrapped element as an application specific type. |
|
92 | + * |
|
93 | + * @throws \UnexpectedValueException If element is not application specific |
|
94 | + * @return \ASN1\Type\Tagged\ApplicationType |
|
95 | + */ |
|
96 | + public function asApplication(): Tagged\ApplicationType |
|
97 | + { |
|
98 | + if (!$this->_element instanceof Tagged\ApplicationType) { |
|
99 | + throw new \UnexpectedValueException( |
|
100 | + "Application type expected, got " . |
|
101 | + $this->_typeDescriptorString()); |
|
102 | + } |
|
103 | + return $this->_element; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Get the wrapped element as a private tagged type. |
|
108 | + * |
|
109 | + * @throws \UnexpectedValueException If element is not using private tagging |
|
110 | + * @return \ASN1\Type\Tagged\PrivateType |
|
111 | + */ |
|
112 | + public function asPrivate(): Tagged\PrivateType |
|
113 | + { |
|
114 | + if (!$this->_element instanceof Tagged\PrivateType) { |
|
115 | + throw new \UnexpectedValueException( |
|
116 | + "Private type expected, got " . $this->_typeDescriptorString()); |
|
117 | + } |
|
118 | + return $this->_element; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Get the wrapped element as a boolean type. |
|
123 | + * |
|
124 | + * @throws \UnexpectedValueException If the element is not a boolean |
|
125 | + * @return \ASN1\Type\Primitive\Boolean |
|
126 | + */ |
|
127 | + public function asBoolean(): Primitive\Boolean |
|
128 | + { |
|
129 | + if (!$this->_element instanceof Primitive\Boolean) { |
|
130 | + throw new \UnexpectedValueException( |
|
131 | + $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
132 | + } |
|
133 | + return $this->_element; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Get the wrapped element as an integer type. |
|
138 | + * |
|
139 | + * @throws \UnexpectedValueException If the element is not an integer |
|
140 | + * @return \ASN1\Type\Primitive\Integer |
|
141 | + */ |
|
142 | + public function asInteger(): Primitive\Integer |
|
143 | + { |
|
144 | + if (!$this->_element instanceof Primitive\Integer) { |
|
145 | + throw new \UnexpectedValueException( |
|
146 | + $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
147 | + } |
|
148 | + return $this->_element; |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Get the wrapped element as a bit string type. |
|
153 | + * |
|
154 | + * @throws \UnexpectedValueException If the element is not a bit string |
|
155 | + * @return \ASN1\Type\Primitive\BitString |
|
156 | + */ |
|
157 | + public function asBitString(): Primitive\BitString |
|
158 | + { |
|
159 | + if (!$this->_element instanceof Primitive\BitString) { |
|
160 | + throw new \UnexpectedValueException( |
|
161 | + $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
162 | + } |
|
163 | + return $this->_element; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Get the wrapped element as an octet string type. |
|
168 | + * |
|
169 | + * @throws \UnexpectedValueException If the element is not an octet string |
|
170 | + * @return \ASN1\Type\Primitive\OctetString |
|
171 | + */ |
|
172 | + public function asOctetString(): Primitive\OctetString |
|
173 | + { |
|
174 | + if (!$this->_element instanceof Primitive\OctetString) { |
|
175 | + throw new \UnexpectedValueException( |
|
176 | + $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
177 | + } |
|
178 | + return $this->_element; |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Get the wrapped element as a null type. |
|
183 | + * |
|
184 | + * @throws \UnexpectedValueException If the element is not a null |
|
185 | + * @return \ASN1\Type\Primitive\NullType |
|
186 | + */ |
|
187 | + public function asNull(): Primitive\NullType |
|
188 | + { |
|
189 | + if (!$this->_element instanceof Primitive\NullType) { |
|
190 | + throw new \UnexpectedValueException( |
|
191 | + $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
192 | + } |
|
193 | + return $this->_element; |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Get the wrapped element as an object identifier type. |
|
198 | + * |
|
199 | + * @throws \UnexpectedValueException If the element is not an object |
|
200 | + * identifier |
|
201 | + * @return \ASN1\Type\Primitive\ObjectIdentifier |
|
202 | + */ |
|
203 | + public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
204 | + { |
|
205 | + if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
206 | + throw new \UnexpectedValueException( |
|
207 | + $this->_generateExceptionMessage( |
|
208 | + Element::TYPE_OBJECT_IDENTIFIER)); |
|
209 | + } |
|
210 | + return $this->_element; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Get the wrapped element as an object descriptor type. |
|
215 | + * |
|
216 | + * @throws \UnexpectedValueException If the element is not an object |
|
217 | + * descriptor |
|
218 | + * @return \ASN1\Type\Primitive\ObjectDescriptor |
|
219 | + */ |
|
220 | + public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
221 | + { |
|
222 | + if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
223 | + throw new \UnexpectedValueException( |
|
224 | + $this->_generateExceptionMessage( |
|
225 | + Element::TYPE_OBJECT_DESCRIPTOR)); |
|
226 | + } |
|
227 | + return $this->_element; |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Get the wrapped element as a real type. |
|
232 | + * |
|
233 | + * @throws \UnexpectedValueException If the element is not a real |
|
234 | + * @return \ASN1\Type\Primitive\Real |
|
235 | + */ |
|
236 | + public function asReal(): Primitive\Real |
|
237 | + { |
|
238 | + if (!$this->_element instanceof Primitive\Real) { |
|
239 | + throw new \UnexpectedValueException( |
|
240 | + $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
241 | + } |
|
242 | + return $this->_element; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Get the wrapped element as an enumerated type. |
|
247 | + * |
|
248 | + * @throws \UnexpectedValueException If the element is not an enumerated |
|
249 | + * @return \ASN1\Type\Primitive\Enumerated |
|
250 | + */ |
|
251 | + public function asEnumerated(): Primitive\Enumerated |
|
252 | + { |
|
253 | + if (!$this->_element instanceof Primitive\Enumerated) { |
|
254 | + throw new \UnexpectedValueException( |
|
255 | + $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
256 | + } |
|
257 | + return $this->_element; |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * Get the wrapped element as a UTF8 string type. |
|
262 | + * |
|
263 | + * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
264 | + * @return \ASN1\Type\Primitive\UTF8String |
|
265 | + */ |
|
266 | + public function asUTF8String(): Primitive\UTF8String |
|
267 | + { |
|
268 | + if (!$this->_element instanceof Primitive\UTF8String) { |
|
269 | + throw new \UnexpectedValueException( |
|
270 | + $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
271 | + } |
|
272 | + return $this->_element; |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * Get the wrapped element as a relative OID type. |
|
277 | + * |
|
278 | + * @throws \UnexpectedValueException If the element is not a relative OID |
|
279 | + * @return \ASN1\Type\Primitive\RelativeOID |
|
280 | + */ |
|
281 | + public function asRelativeOID(): Primitive\RelativeOID |
|
282 | + { |
|
283 | + if (!$this->_element instanceof Primitive\RelativeOID) { |
|
284 | + throw new \UnexpectedValueException( |
|
285 | + $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
286 | + } |
|
287 | + return $this->_element; |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Get the wrapped element as a sequence type. |
|
292 | + * |
|
293 | + * @throws \UnexpectedValueException If the element is not a sequence |
|
294 | + * @return \ASN1\Type\Constructed\Sequence |
|
295 | + */ |
|
296 | + public function asSequence(): Constructed\Sequence |
|
297 | + { |
|
298 | + if (!$this->_element instanceof Constructed\Sequence) { |
|
299 | + throw new \UnexpectedValueException( |
|
300 | + $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
301 | + } |
|
302 | + return $this->_element; |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * Get the wrapped element as a set type. |
|
307 | + * |
|
308 | + * @throws \UnexpectedValueException If the element is not a set |
|
309 | + * @return \ASN1\Type\Constructed\Set |
|
310 | + */ |
|
311 | + public function asSet(): Constructed\Set |
|
312 | + { |
|
313 | + if (!$this->_element instanceof Constructed\Set) { |
|
314 | + throw new \UnexpectedValueException( |
|
315 | + $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
316 | + } |
|
317 | + return $this->_element; |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * Get the wrapped element as a numeric string type. |
|
322 | + * |
|
323 | + * @throws \UnexpectedValueException If the element is not a numeric string |
|
324 | + * @return \ASN1\Type\Primitive\NumericString |
|
325 | + */ |
|
326 | + public function asNumericString(): Primitive\NumericString |
|
327 | + { |
|
328 | + if (!$this->_element instanceof Primitive\NumericString) { |
|
329 | + throw new \UnexpectedValueException( |
|
330 | + $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
331 | + } |
|
332 | + return $this->_element; |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * Get the wrapped element as a printable string type. |
|
337 | + * |
|
338 | + * @throws \UnexpectedValueException If the element is not a printable |
|
339 | + * string |
|
340 | + * @return \ASN1\Type\Primitive\PrintableString |
|
341 | + */ |
|
342 | + public function asPrintableString(): Primitive\PrintableString |
|
343 | + { |
|
344 | + if (!$this->_element instanceof Primitive\PrintableString) { |
|
345 | + throw new \UnexpectedValueException( |
|
346 | + $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
347 | + } |
|
348 | + return $this->_element; |
|
349 | + } |
|
350 | + |
|
351 | + /** |
|
352 | + * Get the wrapped element as a T61 string type. |
|
353 | + * |
|
354 | + * @throws \UnexpectedValueException If the element is not a T61 string |
|
355 | + * @return \ASN1\Type\Primitive\T61String |
|
356 | + */ |
|
357 | + public function asT61String(): Primitive\T61String |
|
358 | + { |
|
359 | + if (!$this->_element instanceof Primitive\T61String) { |
|
360 | + throw new \UnexpectedValueException( |
|
361 | + $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
362 | + } |
|
363 | + return $this->_element; |
|
364 | + } |
|
365 | + |
|
366 | + /** |
|
367 | + * Get the wrapped element as a videotex string type. |
|
368 | + * |
|
369 | + * @throws \UnexpectedValueException If the element is not a videotex string |
|
370 | + * @return \ASN1\Type\Primitive\VideotexString |
|
371 | + */ |
|
372 | + public function asVideotexString(): Primitive\VideotexString |
|
373 | + { |
|
374 | + if (!$this->_element instanceof Primitive\VideotexString) { |
|
375 | + throw new \UnexpectedValueException( |
|
376 | + $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
377 | + } |
|
378 | + return $this->_element; |
|
379 | + } |
|
380 | + |
|
381 | + /** |
|
382 | + * Get the wrapped element as a IA5 string type. |
|
383 | + * |
|
384 | + * @throws \UnexpectedValueException If the element is not a IA5 string |
|
385 | + * @return \ASN1\Type\Primitive\IA5String |
|
386 | + */ |
|
387 | + public function asIA5String(): Primitive\IA5String |
|
388 | + { |
|
389 | + if (!$this->_element instanceof Primitive\IA5String) { |
|
390 | + throw new \UnexpectedValueException( |
|
391 | + $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
392 | + } |
|
393 | + return $this->_element; |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * Get the wrapped element as an UTC time type. |
|
398 | + * |
|
399 | + * @throws \UnexpectedValueException If the element is not a UTC time |
|
400 | + * @return \ASN1\Type\Primitive\UTCTime |
|
401 | + */ |
|
402 | + public function asUTCTime(): Primitive\UTCTime |
|
403 | + { |
|
404 | + if (!$this->_element instanceof Primitive\UTCTime) { |
|
405 | + throw new \UnexpectedValueException( |
|
406 | + $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
407 | + } |
|
408 | + return $this->_element; |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * Get the wrapped element as a generalized time type. |
|
413 | + * |
|
414 | + * @throws \UnexpectedValueException If the element is not a generalized |
|
415 | + * time |
|
416 | + * @return \ASN1\Type\Primitive\GeneralizedTime |
|
417 | + */ |
|
418 | + public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
419 | + { |
|
420 | + if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
421 | + throw new \UnexpectedValueException( |
|
422 | + $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
423 | + } |
|
424 | + return $this->_element; |
|
425 | + } |
|
426 | + |
|
427 | + /** |
|
428 | + * Get the wrapped element as a graphic string type. |
|
429 | + * |
|
430 | + * @throws \UnexpectedValueException If the element is not a graphic string |
|
431 | + * @return \ASN1\Type\Primitive\GraphicString |
|
432 | + */ |
|
433 | + public function asGraphicString(): Primitive\GraphicString |
|
434 | + { |
|
435 | + if (!$this->_element instanceof Primitive\GraphicString) { |
|
436 | + throw new \UnexpectedValueException( |
|
437 | + $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
438 | + } |
|
439 | + return $this->_element; |
|
440 | + } |
|
441 | + |
|
442 | + /** |
|
443 | + * Get the wrapped element as a visible string type. |
|
444 | + * |
|
445 | + * @throws \UnexpectedValueException If the element is not a visible string |
|
446 | + * @return \ASN1\Type\Primitive\VisibleString |
|
447 | + */ |
|
448 | + public function asVisibleString(): Primitive\VisibleString |
|
449 | + { |
|
450 | + if (!$this->_element instanceof Primitive\VisibleString) { |
|
451 | + throw new \UnexpectedValueException( |
|
452 | + $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
453 | + } |
|
454 | + return $this->_element; |
|
455 | + } |
|
456 | + |
|
457 | + /** |
|
458 | + * Get the wrapped element as a general string type. |
|
459 | + * |
|
460 | + * @throws \UnexpectedValueException If the element is not general string |
|
461 | + * @return \ASN1\Type\Primitive\GeneralString |
|
462 | + */ |
|
463 | + public function asGeneralString(): Primitive\GeneralString |
|
464 | + { |
|
465 | + if (!$this->_element instanceof Primitive\GeneralString) { |
|
466 | + throw new \UnexpectedValueException( |
|
467 | + $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
468 | + } |
|
469 | + return $this->_element; |
|
470 | + } |
|
471 | + |
|
472 | + /** |
|
473 | + * Get the wrapped element as a universal string type. |
|
474 | + * |
|
475 | + * @throws \UnexpectedValueException If the element is not a universal |
|
476 | + * string |
|
477 | + * @return \ASN1\Type\Primitive\UniversalString |
|
478 | + */ |
|
479 | + public function asUniversalString(): Primitive\UniversalString |
|
480 | + { |
|
481 | + if (!$this->_element instanceof Primitive\UniversalString) { |
|
482 | + throw new \UnexpectedValueException( |
|
483 | + $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
484 | + } |
|
485 | + return $this->_element; |
|
486 | + } |
|
487 | + |
|
488 | + /** |
|
489 | + * Get the wrapped element as a character string type. |
|
490 | + * |
|
491 | + * @throws \UnexpectedValueException If the element is not a character |
|
492 | + * string |
|
493 | + * @return \ASN1\Type\Primitive\CharacterString |
|
494 | + */ |
|
495 | + public function asCharacterString(): Primitive\CharacterString |
|
496 | + { |
|
497 | + if (!$this->_element instanceof Primitive\CharacterString) { |
|
498 | + throw new \UnexpectedValueException( |
|
499 | + $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
500 | + } |
|
501 | + return $this->_element; |
|
502 | + } |
|
503 | + |
|
504 | + /** |
|
505 | + * Get the wrapped element as a BMP string type. |
|
506 | + * |
|
507 | + * @throws \UnexpectedValueException If the element is not a bmp string |
|
508 | + * @return \ASN1\Type\Primitive\BMPString |
|
509 | + */ |
|
510 | + public function asBMPString(): Primitive\BMPString |
|
511 | + { |
|
512 | + if (!$this->_element instanceof Primitive\BMPString) { |
|
513 | + throw new \UnexpectedValueException( |
|
514 | + $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
515 | + } |
|
516 | + return $this->_element; |
|
517 | + } |
|
518 | + |
|
519 | + /** |
|
520 | + * Get the wrapped element as a constructed string type. |
|
521 | + * |
|
522 | + * @throws \UnexpectedValueException If the element is not a constructed |
|
523 | + * string |
|
524 | + * @return Constructed\ConstructedString |
|
525 | + */ |
|
526 | + public function asConstructedString(): Constructed\ConstructedString |
|
527 | + { |
|
528 | + if (!$this->_element instanceof Constructed\ConstructedString) { |
|
529 | + throw new \UnexpectedValueException( |
|
530 | + $this->_generateExceptionMessage( |
|
531 | + Element::TYPE_CONSTRUCTED_STRING)); |
|
532 | + } |
|
533 | + return $this->_element; |
|
534 | + } |
|
535 | + |
|
536 | + /** |
|
537 | + * Get the wrapped element as any string type. |
|
538 | + * |
|
539 | + * @throws \UnexpectedValueException If the element is not a string |
|
540 | + * @return StringType |
|
541 | + */ |
|
542 | + public function asString(): StringType |
|
543 | + { |
|
544 | + if (!$this->_element instanceof StringType) { |
|
545 | + throw new \UnexpectedValueException( |
|
546 | + $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
547 | + } |
|
548 | + return $this->_element; |
|
549 | + } |
|
550 | + |
|
551 | + /** |
|
552 | + * Get the wrapped element as any time type. |
|
553 | + * |
|
554 | + * @throws \UnexpectedValueException If the element is not a time |
|
555 | + * @return TimeType |
|
556 | + */ |
|
557 | + public function asTime(): TimeType |
|
558 | + { |
|
559 | + if (!$this->_element instanceof TimeType) { |
|
560 | + throw new \UnexpectedValueException( |
|
561 | + $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
562 | + } |
|
563 | + return $this->_element; |
|
564 | + } |
|
565 | + |
|
566 | + /** |
|
567 | + * Generate message for exceptions thrown by <code>as*</code> methods. |
|
568 | + * |
|
569 | + * @param int $tag Type tag of the expected element |
|
570 | + * @return string |
|
571 | + */ |
|
572 | + private function _generateExceptionMessage(int $tag): string |
|
573 | + { |
|
574 | + return sprintf("%s expected, got %s.", Element::tagToName($tag), |
|
575 | + $this->_typeDescriptorString()); |
|
576 | + } |
|
577 | + |
|
578 | + /** |
|
579 | + * Get textual description of the wrapped element for debugging purposes. |
|
580 | + * |
|
581 | + * @return string |
|
582 | + */ |
|
583 | + private function _typeDescriptorString(): string |
|
584 | + { |
|
585 | + $type_cls = $this->_element->typeClass(); |
|
586 | + $tag = $this->_element->tag(); |
|
587 | + if ($type_cls == Identifier::CLASS_UNIVERSAL) { |
|
588 | + return Element::tagToName($tag); |
|
589 | + } |
|
590 | + return Identifier::classToName($type_cls) . " TAG $tag"; |
|
591 | + } |
|
592 | + |
|
593 | + /** |
|
594 | + * |
|
595 | + * @see \ASN1\Feature\Encodable::toDER() |
|
596 | + * @return string |
|
597 | + */ |
|
598 | + public function toDER(): string |
|
599 | + { |
|
600 | + return $this->_element->toDER(); |
|
601 | + } |
|
602 | + |
|
603 | + /** |
|
604 | + * |
|
605 | + * @see \ASN1\Feature\ElementBase::typeClass() |
|
606 | + * @return int |
|
607 | + */ |
|
608 | + public function typeClass(): int |
|
609 | + { |
|
610 | + return $this->_element->typeClass(); |
|
611 | + } |
|
612 | + |
|
613 | + /** |
|
614 | + * |
|
615 | + * @see \ASN1\Feature\ElementBase::isConstructed() |
|
616 | + * @return bool |
|
617 | + */ |
|
618 | + public function isConstructed(): bool |
|
619 | + { |
|
620 | + return $this->_element->isConstructed(); |
|
621 | + } |
|
622 | + |
|
623 | + /** |
|
624 | + * |
|
625 | + * @see \ASN1\Feature\ElementBase::tag() |
|
626 | + * @return int |
|
627 | + */ |
|
628 | + public function tag(): int |
|
629 | + { |
|
630 | + return $this->_element->tag(); |
|
631 | + } |
|
632 | + |
|
633 | + /** |
|
634 | + * |
|
635 | + * {@inheritdoc} |
|
636 | + * @see \ASN1\Feature\ElementBase::isType() |
|
637 | + * @return bool |
|
638 | + */ |
|
639 | + public function isType(int $tag): bool |
|
640 | + { |
|
641 | + return $this->_element->isType($tag); |
|
642 | + } |
|
643 | + |
|
644 | + /** |
|
645 | + * |
|
646 | + * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
647 | + * type strictness. |
|
648 | + * @see \ASN1\Feature\ElementBase::expectType() |
|
649 | + * @return ElementBase |
|
650 | + */ |
|
651 | + public function expectType(int $tag): ElementBase |
|
652 | + { |
|
653 | + return $this->_element->expectType($tag); |
|
654 | + } |
|
655 | + |
|
656 | + /** |
|
657 | + * |
|
658 | + * @see \ASN1\Feature\ElementBase::isTagged() |
|
659 | + * @return bool |
|
660 | + */ |
|
661 | + public function isTagged(): bool |
|
662 | + { |
|
663 | + return $this->_element->isTagged(); |
|
664 | + } |
|
665 | + |
|
666 | + /** |
|
667 | + * |
|
668 | + * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
669 | + * type strictness. |
|
670 | + * @see \ASN1\Feature\ElementBase::expectTagged() |
|
671 | + * @return TaggedType |
|
672 | + */ |
|
673 | + public function expectTagged($tag = null): TaggedType |
|
674 | + { |
|
675 | + return $this->_element->expectTagged($tag); |
|
676 | + } |
|
677 | + |
|
678 | + /** |
|
679 | + * |
|
680 | + * @see \ASN1\Feature\ElementBase::asElement() |
|
681 | + * @return Element |
|
682 | + */ |
|
683 | + public function asElement(): Element |
|
684 | + { |
|
685 | + return $this->_element; |
|
686 | + } |
|
687 | + |
|
688 | + /** |
|
689 | + * |
|
690 | + * {@inheritdoc} |
|
691 | + * @return UnspecifiedType |
|
692 | + */ |
|
693 | + public function asUnspecified(): UnspecifiedType |
|
694 | + { |
|
695 | + return $this; |
|
696 | + } |
|
697 | 697 | } |
@@ -15,112 +15,112 @@ |
||
15 | 15 | */ |
16 | 16 | class ConstructedString extends Structure implements Stringable |
17 | 17 | { |
18 | - /** |
|
19 | - * Constructor. |
|
20 | - * |
|
21 | - * @internal Use create() |
|
22 | - * |
|
23 | - * @param Element ...$elements Any number of elements |
|
24 | - */ |
|
25 | - public function __construct(Element ...$elements) |
|
26 | - { |
|
27 | - parent::__construct(...$elements); |
|
28 | - } |
|
18 | + /** |
|
19 | + * Constructor. |
|
20 | + * |
|
21 | + * @internal Use create() |
|
22 | + * |
|
23 | + * @param Element ...$elements Any number of elements |
|
24 | + */ |
|
25 | + public function __construct(Element ...$elements) |
|
26 | + { |
|
27 | + parent::__construct(...$elements); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * Create from a list of string type elements |
|
32 | - * |
|
33 | - * All strings must have the same type. |
|
34 | - * |
|
35 | - * @param StringType ...$elements |
|
36 | - * @throws \LogicException |
|
37 | - * @return self |
|
38 | - */ |
|
39 | - public static function create(StringType ...$elements): self |
|
40 | - { |
|
41 | - if (!count($elements)) { |
|
42 | - throw new \LogicException( |
|
43 | - 'No elements, unable to determine type tag.'); |
|
44 | - } |
|
45 | - $tag = $elements[0]->tag(); |
|
46 | - foreach ($elements as $el) { |
|
47 | - if ($el->tag() !== $tag) { |
|
48 | - throw new \LogicException( |
|
49 | - 'All elements in constructed string must have the same type.'); |
|
50 | - } |
|
51 | - } |
|
52 | - return self::createWithTag($tag, ...$elements); |
|
53 | - } |
|
30 | + /** |
|
31 | + * Create from a list of string type elements |
|
32 | + * |
|
33 | + * All strings must have the same type. |
|
34 | + * |
|
35 | + * @param StringType ...$elements |
|
36 | + * @throws \LogicException |
|
37 | + * @return self |
|
38 | + */ |
|
39 | + public static function create(StringType ...$elements): self |
|
40 | + { |
|
41 | + if (!count($elements)) { |
|
42 | + throw new \LogicException( |
|
43 | + 'No elements, unable to determine type tag.'); |
|
44 | + } |
|
45 | + $tag = $elements[0]->tag(); |
|
46 | + foreach ($elements as $el) { |
|
47 | + if ($el->tag() !== $tag) { |
|
48 | + throw new \LogicException( |
|
49 | + 'All elements in constructed string must have the same type.'); |
|
50 | + } |
|
51 | + } |
|
52 | + return self::createWithTag($tag, ...$elements); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Create from strings with a given type tag. |
|
57 | - * |
|
58 | - * @param int $tag Type tag for the constructed string element |
|
59 | - * @param StringType ...$elements Any number of elements |
|
60 | - * @return self |
|
61 | - */ |
|
62 | - public static function createWithTag(int $tag, StringType ...$elements) |
|
63 | - { |
|
64 | - $el = new self(...$elements); |
|
65 | - $el->_typeTag = $tag; |
|
66 | - return $el; |
|
67 | - } |
|
55 | + /** |
|
56 | + * Create from strings with a given type tag. |
|
57 | + * |
|
58 | + * @param int $tag Type tag for the constructed string element |
|
59 | + * @param StringType ...$elements Any number of elements |
|
60 | + * @return self |
|
61 | + */ |
|
62 | + public static function createWithTag(int $tag, StringType ...$elements) |
|
63 | + { |
|
64 | + $el = new self(...$elements); |
|
65 | + $el->_typeTag = $tag; |
|
66 | + return $el; |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * Get a list of strings in this structure. |
|
71 | - * |
|
72 | - * @return string[] |
|
73 | - */ |
|
74 | - public function strings(): array |
|
75 | - { |
|
76 | - return array_map(function (StringType $el) { |
|
77 | - return $el->string(); |
|
78 | - }, $this->_elements); |
|
79 | - } |
|
69 | + /** |
|
70 | + * Get a list of strings in this structure. |
|
71 | + * |
|
72 | + * @return string[] |
|
73 | + */ |
|
74 | + public function strings(): array |
|
75 | + { |
|
76 | + return array_map(function (StringType $el) { |
|
77 | + return $el->string(); |
|
78 | + }, $this->_elements); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * Get the contained strings concatenated together. |
|
83 | - * |
|
84 | - * @return string |
|
85 | - */ |
|
86 | - public function concatenated(): string |
|
87 | - { |
|
88 | - return implode('', $this->strings()); |
|
89 | - } |
|
81 | + /** |
|
82 | + * Get the contained strings concatenated together. |
|
83 | + * |
|
84 | + * @return string |
|
85 | + */ |
|
86 | + public function concatenated(): string |
|
87 | + { |
|
88 | + return implode('', $this->strings()); |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * Get the contained strings concatenated together. |
|
93 | - * |
|
94 | - * @return string |
|
95 | - */ |
|
96 | - public function string(): string |
|
97 | - { |
|
98 | - return $this->concatenated(); |
|
99 | - } |
|
91 | + /** |
|
92 | + * Get the contained strings concatenated together. |
|
93 | + * |
|
94 | + * @return string |
|
95 | + */ |
|
96 | + public function string(): string |
|
97 | + { |
|
98 | + return $this->concatenated(); |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * |
|
103 | - * @inheritdoc |
|
104 | - * @return string |
|
105 | - */ |
|
106 | - public function __toString(): string |
|
107 | - { |
|
108 | - return $this->concatenated(); |
|
109 | - } |
|
101 | + /** |
|
102 | + * |
|
103 | + * @inheritdoc |
|
104 | + * @return string |
|
105 | + */ |
|
106 | + public function __toString(): string |
|
107 | + { |
|
108 | + return $this->concatenated(); |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * |
|
113 | - * {@inheritdoc} |
|
114 | - * |
|
115 | - * @return self |
|
116 | - */ |
|
117 | - protected static function _decodeFromDER(Identifier $identifier, |
|
118 | - string $data, int &$offset): ElementBase |
|
119 | - { |
|
120 | - /** @var ConstructedString $type */ |
|
121 | - $type = forward_static_call_array([parent::class, __FUNCTION__], |
|
122 | - [$identifier, $data, &$offset]); |
|
123 | - $type->_typeTag = $identifier->intTag(); |
|
124 | - return $type; |
|
125 | - } |
|
111 | + /** |
|
112 | + * |
|
113 | + * {@inheritdoc} |
|
114 | + * |
|
115 | + * @return self |
|
116 | + */ |
|
117 | + protected static function _decodeFromDER(Identifier $identifier, |
|
118 | + string $data, int &$offset): ElementBase |
|
119 | + { |
|
120 | + /** @var ConstructedString $type */ |
|
121 | + $type = forward_static_call_array([parent::class, __FUNCTION__], |
|
122 | + [$identifier, $data, &$offset]); |
|
123 | + $type->_typeTag = $identifier->intTag(); |
|
124 | + return $type; |
|
125 | + } |
|
126 | 126 | } |
@@ -11,58 +11,58 @@ |
||
11 | 11 | */ |
12 | 12 | abstract class StringType extends Element implements Stringable |
13 | 13 | { |
14 | - /** |
|
15 | - * String value. |
|
16 | - * |
|
17 | - * @var string $_string |
|
18 | - */ |
|
19 | - protected $_string; |
|
14 | + /** |
|
15 | + * String value. |
|
16 | + * |
|
17 | + * @var string $_string |
|
18 | + */ |
|
19 | + protected $_string; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Constructor. |
|
23 | - * |
|
24 | - * @param string $string |
|
25 | - * @throws \InvalidArgumentException |
|
26 | - */ |
|
27 | - public function __construct(string $string) |
|
28 | - { |
|
29 | - if (!$this->_validateString($string)) { |
|
30 | - throw new \InvalidArgumentException( |
|
31 | - sprintf("Not a valid %s string.", |
|
32 | - self::tagToName($this->_typeTag))); |
|
33 | - } |
|
34 | - $this->_string = $string; |
|
35 | - } |
|
21 | + /** |
|
22 | + * Constructor. |
|
23 | + * |
|
24 | + * @param string $string |
|
25 | + * @throws \InvalidArgumentException |
|
26 | + */ |
|
27 | + public function __construct(string $string) |
|
28 | + { |
|
29 | + if (!$this->_validateString($string)) { |
|
30 | + throw new \InvalidArgumentException( |
|
31 | + sprintf("Not a valid %s string.", |
|
32 | + self::tagToName($this->_typeTag))); |
|
33 | + } |
|
34 | + $this->_string = $string; |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * Get the string value. |
|
39 | - * |
|
40 | - * @return string |
|
41 | - */ |
|
42 | - public function string(): string |
|
43 | - { |
|
44 | - return $this->_string; |
|
45 | - } |
|
37 | + /** |
|
38 | + * Get the string value. |
|
39 | + * |
|
40 | + * @return string |
|
41 | + */ |
|
42 | + public function string(): string |
|
43 | + { |
|
44 | + return $this->_string; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * |
|
49 | - * @inheritdoc |
|
50 | - * @return string |
|
51 | - */ |
|
52 | - public function __toString(): string |
|
53 | - { |
|
54 | - return $this->_string; |
|
55 | - } |
|
47 | + /** |
|
48 | + * |
|
49 | + * @inheritdoc |
|
50 | + * @return string |
|
51 | + */ |
|
52 | + public function __toString(): string |
|
53 | + { |
|
54 | + return $this->_string; |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * Check whether string is valid for the concrete type. |
|
59 | - * |
|
60 | - * @param string $string |
|
61 | - * @return bool |
|
62 | - */ |
|
63 | - protected function _validateString(string $string): bool |
|
64 | - { |
|
65 | - // Override in derived classes |
|
66 | - return true; |
|
67 | - } |
|
57 | + /** |
|
58 | + * Check whether string is valid for the concrete type. |
|
59 | + * |
|
60 | + * @param string $string |
|
61 | + * @return bool |
|
62 | + */ |
|
63 | + protected function _validateString(string $string): bool |
|
64 | + { |
|
65 | + // Override in derived classes |
|
66 | + return true; |
|
67 | + } |
|
68 | 68 | } |