@@ -14,336 +14,336 @@ |
||
14 | 14 | * Base class for the constructed types. |
15 | 15 | */ |
16 | 16 | abstract class Structure extends Element implements |
17 | - \Countable, |
|
18 | - \IteratorAggregate |
|
17 | + \Countable, |
|
18 | + \IteratorAggregate |
|
19 | 19 | { |
20 | - use UniversalClass; |
|
20 | + use UniversalClass; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Array of elements in the structure. |
|
24 | - * |
|
25 | - * @var Element[] $_elements |
|
26 | - */ |
|
27 | - protected $_elements; |
|
22 | + /** |
|
23 | + * Array of elements in the structure. |
|
24 | + * |
|
25 | + * @var Element[] $_elements |
|
26 | + */ |
|
27 | + protected $_elements; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Lookup table for the tagged elements. |
|
31 | - * |
|
32 | - * @var TaggedType[]|null $_taggedMap |
|
33 | - */ |
|
34 | - private $_taggedMap; |
|
29 | + /** |
|
30 | + * Lookup table for the tagged elements. |
|
31 | + * |
|
32 | + * @var TaggedType[]|null $_taggedMap |
|
33 | + */ |
|
34 | + private $_taggedMap; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Cache variable of elements wrapped into UnspecifiedType objects. |
|
38 | - * |
|
39 | - * @var UnspecifiedType[]|null $_unspecifiedTypes |
|
40 | - */ |
|
41 | - private $_unspecifiedTypes; |
|
36 | + /** |
|
37 | + * Cache variable of elements wrapped into UnspecifiedType objects. |
|
38 | + * |
|
39 | + * @var UnspecifiedType[]|null $_unspecifiedTypes |
|
40 | + */ |
|
41 | + private $_unspecifiedTypes; |
|
42 | 42 | |
43 | - /** |
|
44 | - * Constructor. |
|
45 | - * |
|
46 | - * @param Element ...$elements Any number of elements |
|
47 | - */ |
|
48 | - public function __construct(Element ...$elements) |
|
49 | - { |
|
50 | - $this->_elements = $elements; |
|
51 | - } |
|
43 | + /** |
|
44 | + * Constructor. |
|
45 | + * |
|
46 | + * @param Element ...$elements Any number of elements |
|
47 | + */ |
|
48 | + public function __construct(Element ...$elements) |
|
49 | + { |
|
50 | + $this->_elements = $elements; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Clone magic method. |
|
55 | - */ |
|
56 | - public function __clone() |
|
57 | - { |
|
58 | - // clear cache-variables |
|
59 | - $this->_taggedMap = null; |
|
60 | - $this->_unspecifiedTypes = null; |
|
61 | - } |
|
53 | + /** |
|
54 | + * Clone magic method. |
|
55 | + */ |
|
56 | + public function __clone() |
|
57 | + { |
|
58 | + // clear cache-variables |
|
59 | + $this->_taggedMap = null; |
|
60 | + $this->_unspecifiedTypes = null; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * |
|
65 | - * @see \ASN1\Element::isConstructed() |
|
66 | - * @return bool |
|
67 | - */ |
|
68 | - public function isConstructed(): bool |
|
69 | - { |
|
70 | - return true; |
|
71 | - } |
|
63 | + /** |
|
64 | + * |
|
65 | + * @see \ASN1\Element::isConstructed() |
|
66 | + * @return bool |
|
67 | + */ |
|
68 | + public function isConstructed(): bool |
|
69 | + { |
|
70 | + return true; |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * |
|
75 | - * @see \ASN1\Element::_encodedContentDER() |
|
76 | - * @return string |
|
77 | - */ |
|
78 | - protected function _encodedContentDER(): string |
|
79 | - { |
|
80 | - $data = ""; |
|
81 | - foreach ($this->_elements as $element) { |
|
82 | - $data .= $element->toDER(); |
|
83 | - } |
|
84 | - return $data; |
|
85 | - } |
|
73 | + /** |
|
74 | + * |
|
75 | + * @see \ASN1\Element::_encodedContentDER() |
|
76 | + * @return string |
|
77 | + */ |
|
78 | + protected function _encodedContentDER(): string |
|
79 | + { |
|
80 | + $data = ""; |
|
81 | + foreach ($this->_elements as $element) { |
|
82 | + $data .= $element->toDER(); |
|
83 | + } |
|
84 | + return $data; |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * |
|
89 | - * {@inheritdoc} |
|
90 | - * @see \ASN1\Element::_decodeFromDER() |
|
91 | - * @return self |
|
92 | - */ |
|
93 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
94 | - int &$offset): ElementBase |
|
95 | - { |
|
96 | - $idx = $offset; |
|
97 | - if (!$identifier->isConstructed()) { |
|
98 | - throw new DecodeException( |
|
99 | - "Structured element must have constructed bit set."); |
|
100 | - } |
|
101 | - $length = Length::expectFromDER($data, $idx); |
|
102 | - $end = $idx + $length->intLength(); |
|
103 | - $elements = []; |
|
104 | - while ($idx < $end) { |
|
105 | - $elements[] = Element::fromDER($data, $idx); |
|
106 | - // check that element didn't overflow length |
|
107 | - if ($idx > $end) { |
|
108 | - throw new DecodeException( |
|
109 | - "Structure's content overflows length."); |
|
110 | - } |
|
111 | - } |
|
112 | - $offset = $idx; |
|
113 | - // return instance by static late binding |
|
114 | - return new static(...$elements); |
|
115 | - } |
|
87 | + /** |
|
88 | + * |
|
89 | + * {@inheritdoc} |
|
90 | + * @see \ASN1\Element::_decodeFromDER() |
|
91 | + * @return self |
|
92 | + */ |
|
93 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
94 | + int &$offset): ElementBase |
|
95 | + { |
|
96 | + $idx = $offset; |
|
97 | + if (!$identifier->isConstructed()) { |
|
98 | + throw new DecodeException( |
|
99 | + "Structured element must have constructed bit set."); |
|
100 | + } |
|
101 | + $length = Length::expectFromDER($data, $idx); |
|
102 | + $end = $idx + $length->intLength(); |
|
103 | + $elements = []; |
|
104 | + while ($idx < $end) { |
|
105 | + $elements[] = Element::fromDER($data, $idx); |
|
106 | + // check that element didn't overflow length |
|
107 | + if ($idx > $end) { |
|
108 | + throw new DecodeException( |
|
109 | + "Structure's content overflows length."); |
|
110 | + } |
|
111 | + } |
|
112 | + $offset = $idx; |
|
113 | + // return instance by static late binding |
|
114 | + return new static(...$elements); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Explode DER structure to DER encoded components that it contains. |
|
119 | - * |
|
120 | - * @param string $data |
|
121 | - * @throws DecodeException |
|
122 | - * @return string[] |
|
123 | - */ |
|
124 | - public static function explodeDER(string $data): array |
|
125 | - { |
|
126 | - $offset = 0; |
|
127 | - $identifier = Identifier::fromDER($data, $offset); |
|
128 | - if (!$identifier->isConstructed()) { |
|
129 | - throw new DecodeException("Element is not constructed."); |
|
130 | - } |
|
131 | - $length = Length::expectFromDER($data, $offset)->intLength(); |
|
132 | - $end = $offset + $length; |
|
133 | - $parts = []; |
|
134 | - while ($offset < $end) { |
|
135 | - // start of the element |
|
136 | - $idx = $offset; |
|
137 | - // skip identifier |
|
138 | - Identifier::fromDER($data, $offset); |
|
139 | - // decode element length |
|
140 | - $length = Length::expectFromDER($data, $offset)->intLength(); |
|
141 | - // extract der encoding of the element |
|
142 | - $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
143 | - // update offset over content |
|
144 | - $offset += $length; |
|
145 | - } |
|
146 | - return $parts; |
|
147 | - } |
|
117 | + /** |
|
118 | + * Explode DER structure to DER encoded components that it contains. |
|
119 | + * |
|
120 | + * @param string $data |
|
121 | + * @throws DecodeException |
|
122 | + * @return string[] |
|
123 | + */ |
|
124 | + public static function explodeDER(string $data): array |
|
125 | + { |
|
126 | + $offset = 0; |
|
127 | + $identifier = Identifier::fromDER($data, $offset); |
|
128 | + if (!$identifier->isConstructed()) { |
|
129 | + throw new DecodeException("Element is not constructed."); |
|
130 | + } |
|
131 | + $length = Length::expectFromDER($data, $offset)->intLength(); |
|
132 | + $end = $offset + $length; |
|
133 | + $parts = []; |
|
134 | + while ($offset < $end) { |
|
135 | + // start of the element |
|
136 | + $idx = $offset; |
|
137 | + // skip identifier |
|
138 | + Identifier::fromDER($data, $offset); |
|
139 | + // decode element length |
|
140 | + $length = Length::expectFromDER($data, $offset)->intLength(); |
|
141 | + // extract der encoding of the element |
|
142 | + $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
143 | + // update offset over content |
|
144 | + $offset += $length; |
|
145 | + } |
|
146 | + return $parts; |
|
147 | + } |
|
148 | 148 | |
149 | - /** |
|
150 | - * Get self with an element at the given index replaced by another. |
|
151 | - * |
|
152 | - * @param int $idx Element index |
|
153 | - * @param Element $el New element to insert into the structure |
|
154 | - * @throws \OutOfBoundsException |
|
155 | - * @return self |
|
156 | - */ |
|
157 | - public function withReplaced(int $idx, Element $el): self |
|
158 | - { |
|
159 | - if (!isset($this->_elements[$idx])) { |
|
160 | - throw new \OutOfBoundsException( |
|
161 | - "Structure doesn't have element at index $idx."); |
|
162 | - } |
|
163 | - $obj = clone $this; |
|
164 | - $obj->_elements[$idx] = $el; |
|
165 | - return $obj; |
|
166 | - } |
|
149 | + /** |
|
150 | + * Get self with an element at the given index replaced by another. |
|
151 | + * |
|
152 | + * @param int $idx Element index |
|
153 | + * @param Element $el New element to insert into the structure |
|
154 | + * @throws \OutOfBoundsException |
|
155 | + * @return self |
|
156 | + */ |
|
157 | + public function withReplaced(int $idx, Element $el): self |
|
158 | + { |
|
159 | + if (!isset($this->_elements[$idx])) { |
|
160 | + throw new \OutOfBoundsException( |
|
161 | + "Structure doesn't have element at index $idx."); |
|
162 | + } |
|
163 | + $obj = clone $this; |
|
164 | + $obj->_elements[$idx] = $el; |
|
165 | + return $obj; |
|
166 | + } |
|
167 | 167 | |
168 | - /** |
|
169 | - * Get self with an element inserted before the given index. |
|
170 | - * |
|
171 | - * @param int $idx Element index |
|
172 | - * @param Element $el New element to insert into the structure |
|
173 | - * @throws \OutOfBoundsException |
|
174 | - * @return self |
|
175 | - */ |
|
176 | - public function withInserted(int $idx, Element $el): self |
|
177 | - { |
|
178 | - if (count($this->_elements) < $idx || $idx < 0) { |
|
179 | - throw new \OutOfBoundsException("Index $idx is out of bounds."); |
|
180 | - } |
|
181 | - $obj = clone $this; |
|
182 | - array_splice($obj->_elements, $idx, 0, [$el]); |
|
183 | - return $obj; |
|
184 | - } |
|
168 | + /** |
|
169 | + * Get self with an element inserted before the given index. |
|
170 | + * |
|
171 | + * @param int $idx Element index |
|
172 | + * @param Element $el New element to insert into the structure |
|
173 | + * @throws \OutOfBoundsException |
|
174 | + * @return self |
|
175 | + */ |
|
176 | + public function withInserted(int $idx, Element $el): self |
|
177 | + { |
|
178 | + if (count($this->_elements) < $idx || $idx < 0) { |
|
179 | + throw new \OutOfBoundsException("Index $idx is out of bounds."); |
|
180 | + } |
|
181 | + $obj = clone $this; |
|
182 | + array_splice($obj->_elements, $idx, 0, [$el]); |
|
183 | + return $obj; |
|
184 | + } |
|
185 | 185 | |
186 | - /** |
|
187 | - * Get self with an element appended to the end. |
|
188 | - * |
|
189 | - * @param Element $el Element to insert into the structure |
|
190 | - * @return self |
|
191 | - */ |
|
192 | - public function withAppended(Element $el): self |
|
193 | - { |
|
194 | - $obj = clone $this; |
|
195 | - array_push($obj->_elements, $el); |
|
196 | - return $obj; |
|
197 | - } |
|
186 | + /** |
|
187 | + * Get self with an element appended to the end. |
|
188 | + * |
|
189 | + * @param Element $el Element to insert into the structure |
|
190 | + * @return self |
|
191 | + */ |
|
192 | + public function withAppended(Element $el): self |
|
193 | + { |
|
194 | + $obj = clone $this; |
|
195 | + array_push($obj->_elements, $el); |
|
196 | + return $obj; |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * Get self with an element prepended in the beginning. |
|
201 | - * |
|
202 | - * @param Element $el Element to insert into the structure |
|
203 | - * @return self |
|
204 | - */ |
|
205 | - public function withPrepended(Element $el): self |
|
206 | - { |
|
207 | - $obj = clone $this; |
|
208 | - array_unshift($obj->_elements, $el); |
|
209 | - return $obj; |
|
210 | - } |
|
199 | + /** |
|
200 | + * Get self with an element prepended in the beginning. |
|
201 | + * |
|
202 | + * @param Element $el Element to insert into the structure |
|
203 | + * @return self |
|
204 | + */ |
|
205 | + public function withPrepended(Element $el): self |
|
206 | + { |
|
207 | + $obj = clone $this; |
|
208 | + array_unshift($obj->_elements, $el); |
|
209 | + return $obj; |
|
210 | + } |
|
211 | 211 | |
212 | - /** |
|
213 | - * Get self with an element at the given index removed. |
|
214 | - * |
|
215 | - * @param int $idx Element index |
|
216 | - * @throws \OutOfBoundsException |
|
217 | - * @return self |
|
218 | - */ |
|
219 | - public function withoutElement(int $idx): self |
|
220 | - { |
|
221 | - if (!isset($this->_elements[$idx])) { |
|
222 | - throw new \OutOfBoundsException( |
|
223 | - "Structure doesn't have element at index $idx."); |
|
224 | - } |
|
225 | - $obj = clone $this; |
|
226 | - array_splice($obj->_elements, $idx, 1); |
|
227 | - return $obj; |
|
228 | - } |
|
212 | + /** |
|
213 | + * Get self with an element at the given index removed. |
|
214 | + * |
|
215 | + * @param int $idx Element index |
|
216 | + * @throws \OutOfBoundsException |
|
217 | + * @return self |
|
218 | + */ |
|
219 | + public function withoutElement(int $idx): self |
|
220 | + { |
|
221 | + if (!isset($this->_elements[$idx])) { |
|
222 | + throw new \OutOfBoundsException( |
|
223 | + "Structure doesn't have element at index $idx."); |
|
224 | + } |
|
225 | + $obj = clone $this; |
|
226 | + array_splice($obj->_elements, $idx, 1); |
|
227 | + return $obj; |
|
228 | + } |
|
229 | 229 | |
230 | - /** |
|
231 | - * Get elements in the structure. |
|
232 | - * |
|
233 | - * @return UnspecifiedType[] |
|
234 | - */ |
|
235 | - public function elements(): array |
|
236 | - { |
|
237 | - if (!isset($this->_unspecifiedTypes)) { |
|
238 | - $this->_unspecifiedTypes = array_map( |
|
239 | - function (Element $el) { |
|
240 | - return new UnspecifiedType($el); |
|
241 | - }, $this->_elements); |
|
242 | - } |
|
243 | - return $this->_unspecifiedTypes; |
|
244 | - } |
|
230 | + /** |
|
231 | + * Get elements in the structure. |
|
232 | + * |
|
233 | + * @return UnspecifiedType[] |
|
234 | + */ |
|
235 | + public function elements(): array |
|
236 | + { |
|
237 | + if (!isset($this->_unspecifiedTypes)) { |
|
238 | + $this->_unspecifiedTypes = array_map( |
|
239 | + function (Element $el) { |
|
240 | + return new UnspecifiedType($el); |
|
241 | + }, $this->_elements); |
|
242 | + } |
|
243 | + return $this->_unspecifiedTypes; |
|
244 | + } |
|
245 | 245 | |
246 | - /** |
|
247 | - * Check whether the structure has an element at the given index, optionally |
|
248 | - * satisfying given tag expectation. |
|
249 | - * |
|
250 | - * @param int $idx Index 0..n |
|
251 | - * @param int|null $expectedTag Optional type tag expectation |
|
252 | - * @return bool |
|
253 | - */ |
|
254 | - public function has(int $idx, $expectedTag = null): bool |
|
255 | - { |
|
256 | - if (!isset($this->_elements[$idx])) { |
|
257 | - return false; |
|
258 | - } |
|
259 | - if (isset($expectedTag)) { |
|
260 | - if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
261 | - return false; |
|
262 | - } |
|
263 | - } |
|
264 | - return true; |
|
265 | - } |
|
246 | + /** |
|
247 | + * Check whether the structure has an element at the given index, optionally |
|
248 | + * satisfying given tag expectation. |
|
249 | + * |
|
250 | + * @param int $idx Index 0..n |
|
251 | + * @param int|null $expectedTag Optional type tag expectation |
|
252 | + * @return bool |
|
253 | + */ |
|
254 | + public function has(int $idx, $expectedTag = null): bool |
|
255 | + { |
|
256 | + if (!isset($this->_elements[$idx])) { |
|
257 | + return false; |
|
258 | + } |
|
259 | + if (isset($expectedTag)) { |
|
260 | + if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
261 | + return false; |
|
262 | + } |
|
263 | + } |
|
264 | + return true; |
|
265 | + } |
|
266 | 266 | |
267 | - /** |
|
268 | - * Get the element at the given index, optionally checking that the element |
|
269 | - * has a given tag. |
|
270 | - * |
|
271 | - * NOTE! Expectation checking is deprecated and should be done |
|
272 | - * with UnspecifiedType. |
|
273 | - * |
|
274 | - * @param int $idx Index 0..n |
|
275 | - * @param int|null $expectedTag Optional type tag expectation |
|
276 | - * @throws \OutOfBoundsException If element doesn't exists |
|
277 | - * @throws \UnexpectedValueException If expectation fails |
|
278 | - * @return UnspecifiedType |
|
279 | - */ |
|
280 | - public function at(int $idx, $expectedTag = null): UnspecifiedType |
|
281 | - { |
|
282 | - if (!isset($this->_elements[$idx])) { |
|
283 | - throw new \OutOfBoundsException( |
|
284 | - "Structure doesn't have an element at index $idx."); |
|
285 | - } |
|
286 | - $element = $this->_elements[$idx]; |
|
287 | - if (isset($expectedTag)) { |
|
288 | - $element->expectType($expectedTag); |
|
289 | - } |
|
290 | - return new UnspecifiedType($element); |
|
291 | - } |
|
267 | + /** |
|
268 | + * Get the element at the given index, optionally checking that the element |
|
269 | + * has a given tag. |
|
270 | + * |
|
271 | + * NOTE! Expectation checking is deprecated and should be done |
|
272 | + * with UnspecifiedType. |
|
273 | + * |
|
274 | + * @param int $idx Index 0..n |
|
275 | + * @param int|null $expectedTag Optional type tag expectation |
|
276 | + * @throws \OutOfBoundsException If element doesn't exists |
|
277 | + * @throws \UnexpectedValueException If expectation fails |
|
278 | + * @return UnspecifiedType |
|
279 | + */ |
|
280 | + public function at(int $idx, $expectedTag = null): UnspecifiedType |
|
281 | + { |
|
282 | + if (!isset($this->_elements[$idx])) { |
|
283 | + throw new \OutOfBoundsException( |
|
284 | + "Structure doesn't have an element at index $idx."); |
|
285 | + } |
|
286 | + $element = $this->_elements[$idx]; |
|
287 | + if (isset($expectedTag)) { |
|
288 | + $element->expectType($expectedTag); |
|
289 | + } |
|
290 | + return new UnspecifiedType($element); |
|
291 | + } |
|
292 | 292 | |
293 | - /** |
|
294 | - * Check whether the structure contains a context specific element with a |
|
295 | - * given tag. |
|
296 | - * |
|
297 | - * @param int $tag Tag number |
|
298 | - * @return boolean |
|
299 | - */ |
|
300 | - public function hasTagged(int $tag): bool |
|
301 | - { |
|
302 | - // lazily build lookup map |
|
303 | - if (!isset($this->_taggedMap)) { |
|
304 | - $this->_taggedMap = []; |
|
305 | - foreach ($this->_elements as $element) { |
|
306 | - if ($element->isTagged()) { |
|
307 | - $this->_taggedMap[$element->tag()] = $element; |
|
308 | - } |
|
309 | - } |
|
310 | - } |
|
311 | - return isset($this->_taggedMap[$tag]); |
|
312 | - } |
|
293 | + /** |
|
294 | + * Check whether the structure contains a context specific element with a |
|
295 | + * given tag. |
|
296 | + * |
|
297 | + * @param int $tag Tag number |
|
298 | + * @return boolean |
|
299 | + */ |
|
300 | + public function hasTagged(int $tag): bool |
|
301 | + { |
|
302 | + // lazily build lookup map |
|
303 | + if (!isset($this->_taggedMap)) { |
|
304 | + $this->_taggedMap = []; |
|
305 | + foreach ($this->_elements as $element) { |
|
306 | + if ($element->isTagged()) { |
|
307 | + $this->_taggedMap[$element->tag()] = $element; |
|
308 | + } |
|
309 | + } |
|
310 | + } |
|
311 | + return isset($this->_taggedMap[$tag]); |
|
312 | + } |
|
313 | 313 | |
314 | - /** |
|
315 | - * Get a context specific element tagged with a given tag. |
|
316 | - * |
|
317 | - * @param int $tag |
|
318 | - * @throws \LogicException If tag doesn't exists |
|
319 | - * @return TaggedType |
|
320 | - */ |
|
321 | - public function getTagged(int $tag): TaggedType |
|
322 | - { |
|
323 | - if (!$this->hasTagged($tag)) { |
|
324 | - throw new \LogicException("No tagged element for tag $tag."); |
|
325 | - } |
|
326 | - return $this->_taggedMap[$tag]; |
|
327 | - } |
|
314 | + /** |
|
315 | + * Get a context specific element tagged with a given tag. |
|
316 | + * |
|
317 | + * @param int $tag |
|
318 | + * @throws \LogicException If tag doesn't exists |
|
319 | + * @return TaggedType |
|
320 | + */ |
|
321 | + public function getTagged(int $tag): TaggedType |
|
322 | + { |
|
323 | + if (!$this->hasTagged($tag)) { |
|
324 | + throw new \LogicException("No tagged element for tag $tag."); |
|
325 | + } |
|
326 | + return $this->_taggedMap[$tag]; |
|
327 | + } |
|
328 | 328 | |
329 | - /** |
|
330 | - * |
|
331 | - * @see \Countable::count() |
|
332 | - * @return int |
|
333 | - */ |
|
334 | - public function count(): int |
|
335 | - { |
|
336 | - return count($this->_elements); |
|
337 | - } |
|
329 | + /** |
|
330 | + * |
|
331 | + * @see \Countable::count() |
|
332 | + * @return int |
|
333 | + */ |
|
334 | + public function count(): int |
|
335 | + { |
|
336 | + return count($this->_elements); |
|
337 | + } |
|
338 | 338 | |
339 | - /** |
|
340 | - * Get an iterator for the UnspecifiedElement objects. |
|
341 | - * |
|
342 | - * @see \IteratorAggregate::getIterator() |
|
343 | - * @return \ArrayIterator |
|
344 | - */ |
|
345 | - public function getIterator(): \ArrayIterator |
|
346 | - { |
|
347 | - return new \ArrayIterator($this->elements()); |
|
348 | - } |
|
339 | + /** |
|
340 | + * Get an iterator for the UnspecifiedElement objects. |
|
341 | + * |
|
342 | + * @see \IteratorAggregate::getIterator() |
|
343 | + * @return \ArrayIterator |
|
344 | + */ |
|
345 | + public function getIterator(): \ArrayIterator |
|
346 | + { |
|
347 | + return new \ArrayIterator($this->elements()); |
|
348 | + } |
|
349 | 349 | } |
@@ -20,438 +20,438 @@ |
||
20 | 20 | */ |
21 | 21 | abstract class Element implements ElementBase |
22 | 22 | { |
23 | - // Universal type tags |
|
24 | - const TYPE_EOC = 0x00; |
|
25 | - const TYPE_BOOLEAN = 0x01; |
|
26 | - const TYPE_INTEGER = 0x02; |
|
27 | - const TYPE_BIT_STRING = 0x03; |
|
28 | - const TYPE_OCTET_STRING = 0x04; |
|
29 | - const TYPE_NULL = 0x05; |
|
30 | - const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
31 | - const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
32 | - const TYPE_EXTERNAL = 0x08; |
|
33 | - const TYPE_REAL = 0x09; |
|
34 | - const TYPE_ENUMERATED = 0x0a; |
|
35 | - const TYPE_EMBEDDED_PDV = 0x0b; |
|
36 | - const TYPE_UTF8_STRING = 0x0c; |
|
37 | - const TYPE_RELATIVE_OID = 0x0d; |
|
38 | - const TYPE_SEQUENCE = 0x10; |
|
39 | - const TYPE_SET = 0x11; |
|
40 | - const TYPE_NUMERIC_STRING = 0x12; |
|
41 | - const TYPE_PRINTABLE_STRING = 0x13; |
|
42 | - const TYPE_T61_STRING = 0x14; |
|
43 | - const TYPE_VIDEOTEX_STRING = 0x15; |
|
44 | - const TYPE_IA5_STRING = 0x16; |
|
45 | - const TYPE_UTC_TIME = 0x17; |
|
46 | - const TYPE_GENERALIZED_TIME = 0x18; |
|
47 | - const TYPE_GRAPHIC_STRING = 0x19; |
|
48 | - const TYPE_VISIBLE_STRING = 0x1a; |
|
49 | - const TYPE_GENERAL_STRING = 0x1b; |
|
50 | - const TYPE_UNIVERSAL_STRING = 0x1c; |
|
51 | - const TYPE_CHARACTER_STRING = 0x1d; |
|
52 | - const TYPE_BMP_STRING = 0x1e; |
|
23 | + // Universal type tags |
|
24 | + const TYPE_EOC = 0x00; |
|
25 | + const TYPE_BOOLEAN = 0x01; |
|
26 | + const TYPE_INTEGER = 0x02; |
|
27 | + const TYPE_BIT_STRING = 0x03; |
|
28 | + const TYPE_OCTET_STRING = 0x04; |
|
29 | + const TYPE_NULL = 0x05; |
|
30 | + const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
31 | + const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
32 | + const TYPE_EXTERNAL = 0x08; |
|
33 | + const TYPE_REAL = 0x09; |
|
34 | + const TYPE_ENUMERATED = 0x0a; |
|
35 | + const TYPE_EMBEDDED_PDV = 0x0b; |
|
36 | + const TYPE_UTF8_STRING = 0x0c; |
|
37 | + const TYPE_RELATIVE_OID = 0x0d; |
|
38 | + const TYPE_SEQUENCE = 0x10; |
|
39 | + const TYPE_SET = 0x11; |
|
40 | + const TYPE_NUMERIC_STRING = 0x12; |
|
41 | + const TYPE_PRINTABLE_STRING = 0x13; |
|
42 | + const TYPE_T61_STRING = 0x14; |
|
43 | + const TYPE_VIDEOTEX_STRING = 0x15; |
|
44 | + const TYPE_IA5_STRING = 0x16; |
|
45 | + const TYPE_UTC_TIME = 0x17; |
|
46 | + const TYPE_GENERALIZED_TIME = 0x18; |
|
47 | + const TYPE_GRAPHIC_STRING = 0x19; |
|
48 | + const TYPE_VISIBLE_STRING = 0x1a; |
|
49 | + const TYPE_GENERAL_STRING = 0x1b; |
|
50 | + const TYPE_UNIVERSAL_STRING = 0x1c; |
|
51 | + const TYPE_CHARACTER_STRING = 0x1d; |
|
52 | + const TYPE_BMP_STRING = 0x1e; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Mapping from universal type tag to implementation class name. |
|
56 | - * |
|
57 | - * @internal |
|
58 | - * |
|
59 | - * @var array |
|
60 | - */ |
|
61 | - const MAP_TAG_TO_CLASS = [ |
|
62 | - /* @formatter:off */ |
|
63 | - self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
64 | - self::TYPE_INTEGER => Primitive\Integer::class, |
|
65 | - self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
66 | - self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
67 | - self::TYPE_NULL => Primitive\NullType::class, |
|
68 | - self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
69 | - self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
70 | - self::TYPE_REAL => Primitive\Real::class, |
|
71 | - self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
72 | - self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
73 | - self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
74 | - self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
75 | - self::TYPE_SET => Constructed\Set::class, |
|
76 | - self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
77 | - self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
78 | - self::TYPE_T61_STRING => Primitive\T61String::class, |
|
79 | - self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
80 | - self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
81 | - self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
82 | - self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
83 | - self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
84 | - self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
85 | - self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
86 | - self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
87 | - self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
88 | - self::TYPE_BMP_STRING => Primitive\BMPString::class, |
|
89 | - /* @formatter:on */ |
|
90 | - ]; |
|
54 | + /** |
|
55 | + * Mapping from universal type tag to implementation class name. |
|
56 | + * |
|
57 | + * @internal |
|
58 | + * |
|
59 | + * @var array |
|
60 | + */ |
|
61 | + const MAP_TAG_TO_CLASS = [ |
|
62 | + /* @formatter:off */ |
|
63 | + self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
64 | + self::TYPE_INTEGER => Primitive\Integer::class, |
|
65 | + self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
66 | + self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
67 | + self::TYPE_NULL => Primitive\NullType::class, |
|
68 | + self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
69 | + self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
70 | + self::TYPE_REAL => Primitive\Real::class, |
|
71 | + self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
72 | + self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
73 | + self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
74 | + self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
75 | + self::TYPE_SET => Constructed\Set::class, |
|
76 | + self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
77 | + self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
78 | + self::TYPE_T61_STRING => Primitive\T61String::class, |
|
79 | + self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
80 | + self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
81 | + self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
82 | + self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
83 | + self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
84 | + self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
85 | + self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
86 | + self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
87 | + self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
88 | + self::TYPE_BMP_STRING => Primitive\BMPString::class, |
|
89 | + /* @formatter:on */ |
|
90 | + ]; |
|
91 | 91 | |
92 | - /** |
|
93 | - * Pseudotype for all string types. |
|
94 | - * |
|
95 | - * May be used as an expectation parameter. |
|
96 | - * |
|
97 | - * @var int |
|
98 | - */ |
|
99 | - const TYPE_STRING = -1; |
|
92 | + /** |
|
93 | + * Pseudotype for all string types. |
|
94 | + * |
|
95 | + * May be used as an expectation parameter. |
|
96 | + * |
|
97 | + * @var int |
|
98 | + */ |
|
99 | + const TYPE_STRING = -1; |
|
100 | 100 | |
101 | - /** |
|
102 | - * Pseudotype for all time types. |
|
103 | - * |
|
104 | - * May be used as an expectation parameter. |
|
105 | - * |
|
106 | - * @var int |
|
107 | - */ |
|
108 | - const TYPE_TIME = -2; |
|
101 | + /** |
|
102 | + * Pseudotype for all time types. |
|
103 | + * |
|
104 | + * May be used as an expectation parameter. |
|
105 | + * |
|
106 | + * @var int |
|
107 | + */ |
|
108 | + const TYPE_TIME = -2; |
|
109 | 109 | |
110 | - /** |
|
111 | - * Mapping from universal type tag to human readable name. |
|
112 | - * |
|
113 | - * @internal |
|
114 | - * |
|
115 | - * @var array |
|
116 | - */ |
|
117 | - const MAP_TYPE_TO_NAME = array( |
|
118 | - /* @formatter:off */ |
|
119 | - self::TYPE_EOC => "EOC", |
|
120 | - self::TYPE_BOOLEAN => "BOOLEAN", |
|
121 | - self::TYPE_INTEGER => "INTEGER", |
|
122 | - self::TYPE_BIT_STRING => "BIT STRING", |
|
123 | - self::TYPE_OCTET_STRING => "OCTET STRING", |
|
124 | - self::TYPE_NULL => "NULL", |
|
125 | - self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER", |
|
126 | - self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor", |
|
127 | - self::TYPE_EXTERNAL => "EXTERNAL", |
|
128 | - self::TYPE_REAL => "REAL", |
|
129 | - self::TYPE_ENUMERATED => "ENUMERATED", |
|
130 | - self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV", |
|
131 | - self::TYPE_UTF8_STRING => "UTF8String", |
|
132 | - self::TYPE_RELATIVE_OID => "RELATIVE-OID", |
|
133 | - self::TYPE_SEQUENCE => "SEQUENCE", |
|
134 | - self::TYPE_SET => "SET", |
|
135 | - self::TYPE_NUMERIC_STRING => "NumericString", |
|
136 | - self::TYPE_PRINTABLE_STRING => "PrintableString", |
|
137 | - self::TYPE_T61_STRING => "T61String", |
|
138 | - self::TYPE_VIDEOTEX_STRING => "VideotexString", |
|
139 | - self::TYPE_IA5_STRING => "IA5String", |
|
140 | - self::TYPE_UTC_TIME => "UTCTime", |
|
141 | - self::TYPE_GENERALIZED_TIME => "GeneralizedTime", |
|
142 | - self::TYPE_GRAPHIC_STRING => "GraphicString", |
|
143 | - self::TYPE_VISIBLE_STRING => "VisibleString", |
|
144 | - self::TYPE_GENERAL_STRING => "GeneralString", |
|
145 | - self::TYPE_UNIVERSAL_STRING => "UniversalString", |
|
146 | - self::TYPE_CHARACTER_STRING => "CHARACTER STRING", |
|
147 | - self::TYPE_BMP_STRING => "BMPString", |
|
148 | - self::TYPE_STRING => "Any String", |
|
149 | - self::TYPE_TIME => "Any Time" |
|
150 | - /* @formatter:on */ |
|
151 | - ); |
|
110 | + /** |
|
111 | + * Mapping from universal type tag to human readable name. |
|
112 | + * |
|
113 | + * @internal |
|
114 | + * |
|
115 | + * @var array |
|
116 | + */ |
|
117 | + const MAP_TYPE_TO_NAME = array( |
|
118 | + /* @formatter:off */ |
|
119 | + self::TYPE_EOC => "EOC", |
|
120 | + self::TYPE_BOOLEAN => "BOOLEAN", |
|
121 | + self::TYPE_INTEGER => "INTEGER", |
|
122 | + self::TYPE_BIT_STRING => "BIT STRING", |
|
123 | + self::TYPE_OCTET_STRING => "OCTET STRING", |
|
124 | + self::TYPE_NULL => "NULL", |
|
125 | + self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER", |
|
126 | + self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor", |
|
127 | + self::TYPE_EXTERNAL => "EXTERNAL", |
|
128 | + self::TYPE_REAL => "REAL", |
|
129 | + self::TYPE_ENUMERATED => "ENUMERATED", |
|
130 | + self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV", |
|
131 | + self::TYPE_UTF8_STRING => "UTF8String", |
|
132 | + self::TYPE_RELATIVE_OID => "RELATIVE-OID", |
|
133 | + self::TYPE_SEQUENCE => "SEQUENCE", |
|
134 | + self::TYPE_SET => "SET", |
|
135 | + self::TYPE_NUMERIC_STRING => "NumericString", |
|
136 | + self::TYPE_PRINTABLE_STRING => "PrintableString", |
|
137 | + self::TYPE_T61_STRING => "T61String", |
|
138 | + self::TYPE_VIDEOTEX_STRING => "VideotexString", |
|
139 | + self::TYPE_IA5_STRING => "IA5String", |
|
140 | + self::TYPE_UTC_TIME => "UTCTime", |
|
141 | + self::TYPE_GENERALIZED_TIME => "GeneralizedTime", |
|
142 | + self::TYPE_GRAPHIC_STRING => "GraphicString", |
|
143 | + self::TYPE_VISIBLE_STRING => "VisibleString", |
|
144 | + self::TYPE_GENERAL_STRING => "GeneralString", |
|
145 | + self::TYPE_UNIVERSAL_STRING => "UniversalString", |
|
146 | + self::TYPE_CHARACTER_STRING => "CHARACTER STRING", |
|
147 | + self::TYPE_BMP_STRING => "BMPString", |
|
148 | + self::TYPE_STRING => "Any String", |
|
149 | + self::TYPE_TIME => "Any Time" |
|
150 | + /* @formatter:on */ |
|
151 | + ); |
|
152 | 152 | |
153 | - /** |
|
154 | - * Element's type tag. |
|
155 | - * |
|
156 | - * @var int |
|
157 | - */ |
|
158 | - protected $_typeTag; |
|
153 | + /** |
|
154 | + * Element's type tag. |
|
155 | + * |
|
156 | + * @var int |
|
157 | + */ |
|
158 | + protected $_typeTag; |
|
159 | 159 | |
160 | - /** |
|
161 | - * |
|
162 | - * @see \ASN1\Feature\ElementBase::typeClass() |
|
163 | - * @return int |
|
164 | - */ |
|
165 | - abstract public function typeClass(): int; |
|
160 | + /** |
|
161 | + * |
|
162 | + * @see \ASN1\Feature\ElementBase::typeClass() |
|
163 | + * @return int |
|
164 | + */ |
|
165 | + abstract public function typeClass(): int; |
|
166 | 166 | |
167 | - /** |
|
168 | - * |
|
169 | - * @see \ASN1\Feature\ElementBase::isConstructed() |
|
170 | - * @return bool |
|
171 | - */ |
|
172 | - abstract public function isConstructed(): bool; |
|
167 | + /** |
|
168 | + * |
|
169 | + * @see \ASN1\Feature\ElementBase::isConstructed() |
|
170 | + * @return bool |
|
171 | + */ |
|
172 | + abstract public function isConstructed(): bool; |
|
173 | 173 | |
174 | - /** |
|
175 | - * Get the content encoded in DER. |
|
176 | - * |
|
177 | - * Returns the DER encoded content without identifier and length header |
|
178 | - * octets. |
|
179 | - * |
|
180 | - * @return string |
|
181 | - */ |
|
182 | - abstract protected function _encodedContentDER(): string; |
|
174 | + /** |
|
175 | + * Get the content encoded in DER. |
|
176 | + * |
|
177 | + * Returns the DER encoded content without identifier and length header |
|
178 | + * octets. |
|
179 | + * |
|
180 | + * @return string |
|
181 | + */ |
|
182 | + abstract protected function _encodedContentDER(): string; |
|
183 | 183 | |
184 | - /** |
|
185 | - * Decode type-specific element from DER. |
|
186 | - * |
|
187 | - * @param Identifier $identifier Pre-parsed identifier |
|
188 | - * @param string $data DER data |
|
189 | - * @param int $offset Offset in data to the next byte after identifier |
|
190 | - * @throws DecodeException If decoding fails |
|
191 | - * @return self |
|
192 | - */ |
|
193 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
194 | - int &$offset): ElementBase |
|
195 | - { |
|
196 | - throw new \BadMethodCallException( |
|
197 | - __METHOD__ . " must be implemented in derived class."); |
|
198 | - } |
|
184 | + /** |
|
185 | + * Decode type-specific element from DER. |
|
186 | + * |
|
187 | + * @param Identifier $identifier Pre-parsed identifier |
|
188 | + * @param string $data DER data |
|
189 | + * @param int $offset Offset in data to the next byte after identifier |
|
190 | + * @throws DecodeException If decoding fails |
|
191 | + * @return self |
|
192 | + */ |
|
193 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
194 | + int &$offset): ElementBase |
|
195 | + { |
|
196 | + throw new \BadMethodCallException( |
|
197 | + __METHOD__ . " must be implemented in derived class."); |
|
198 | + } |
|
199 | 199 | |
200 | - /** |
|
201 | - * Decode element from DER data. |
|
202 | - * |
|
203 | - * @param string $data DER encoded data |
|
204 | - * @param int|null $offset Reference to the variable that contains offset |
|
205 | - * into the data where to start parsing. Variable is updated to |
|
206 | - * the offset next to the parsed element. If null, start from offset |
|
207 | - * 0. |
|
208 | - * @throws DecodeException If decoding fails |
|
209 | - * @throws \UnexpectedValueException If called in the context of an expected |
|
210 | - * type, but decoding yields another type |
|
211 | - * @return self |
|
212 | - */ |
|
213 | - public static function fromDER(string $data, int &$offset = null): ElementBase |
|
214 | - { |
|
215 | - // decode identifier |
|
216 | - $idx = $offset ? $offset : 0; |
|
217 | - $identifier = Identifier::fromDER($data, $idx); |
|
218 | - // determine class that implements type specific decoding |
|
219 | - $cls = self::_determineImplClass($identifier); |
|
220 | - try { |
|
221 | - // decode remaining element |
|
222 | - $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
223 | - } catch (\LogicException $e) { |
|
224 | - // rethrow as a RuntimeException for unified exception handling |
|
225 | - throw new DecodeException( |
|
226 | - sprintf("Error while decoding %s.", |
|
227 | - self::tagToName($identifier->intTag())), 0, $e); |
|
228 | - } |
|
229 | - // if called in the context of a concrete class, check |
|
230 | - // that decoded type matches the type of a calling class |
|
231 | - $called_class = get_called_class(); |
|
232 | - if (__CLASS__ != $called_class) { |
|
233 | - if (!$element instanceof $called_class) { |
|
234 | - throw new \UnexpectedValueException( |
|
235 | - sprintf("%s expected, got %s.", $called_class, |
|
236 | - get_class($element))); |
|
237 | - } |
|
238 | - } |
|
239 | - // update offset for the caller |
|
240 | - if (isset($offset)) { |
|
241 | - $offset = $idx; |
|
242 | - } |
|
243 | - return $element; |
|
244 | - } |
|
200 | + /** |
|
201 | + * Decode element from DER data. |
|
202 | + * |
|
203 | + * @param string $data DER encoded data |
|
204 | + * @param int|null $offset Reference to the variable that contains offset |
|
205 | + * into the data where to start parsing. Variable is updated to |
|
206 | + * the offset next to the parsed element. If null, start from offset |
|
207 | + * 0. |
|
208 | + * @throws DecodeException If decoding fails |
|
209 | + * @throws \UnexpectedValueException If called in the context of an expected |
|
210 | + * type, but decoding yields another type |
|
211 | + * @return self |
|
212 | + */ |
|
213 | + public static function fromDER(string $data, int &$offset = null): ElementBase |
|
214 | + { |
|
215 | + // decode identifier |
|
216 | + $idx = $offset ? $offset : 0; |
|
217 | + $identifier = Identifier::fromDER($data, $idx); |
|
218 | + // determine class that implements type specific decoding |
|
219 | + $cls = self::_determineImplClass($identifier); |
|
220 | + try { |
|
221 | + // decode remaining element |
|
222 | + $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
223 | + } catch (\LogicException $e) { |
|
224 | + // rethrow as a RuntimeException for unified exception handling |
|
225 | + throw new DecodeException( |
|
226 | + sprintf("Error while decoding %s.", |
|
227 | + self::tagToName($identifier->intTag())), 0, $e); |
|
228 | + } |
|
229 | + // if called in the context of a concrete class, check |
|
230 | + // that decoded type matches the type of a calling class |
|
231 | + $called_class = get_called_class(); |
|
232 | + if (__CLASS__ != $called_class) { |
|
233 | + if (!$element instanceof $called_class) { |
|
234 | + throw new \UnexpectedValueException( |
|
235 | + sprintf("%s expected, got %s.", $called_class, |
|
236 | + get_class($element))); |
|
237 | + } |
|
238 | + } |
|
239 | + // update offset for the caller |
|
240 | + if (isset($offset)) { |
|
241 | + $offset = $idx; |
|
242 | + } |
|
243 | + return $element; |
|
244 | + } |
|
245 | 245 | |
246 | - /** |
|
247 | - * |
|
248 | - * @see \ASN1\Feature\Encodable::toDER() |
|
249 | - * @return string |
|
250 | - */ |
|
251 | - public function toDER(): string |
|
252 | - { |
|
253 | - $identifier = new Identifier($this->typeClass(), |
|
254 | - $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
255 | - $this->_typeTag); |
|
256 | - $content = $this->_encodedContentDER(); |
|
257 | - $length = new Length(strlen($content)); |
|
258 | - return $identifier->toDER() . $length->toDER() . $content; |
|
259 | - } |
|
246 | + /** |
|
247 | + * |
|
248 | + * @see \ASN1\Feature\Encodable::toDER() |
|
249 | + * @return string |
|
250 | + */ |
|
251 | + public function toDER(): string |
|
252 | + { |
|
253 | + $identifier = new Identifier($this->typeClass(), |
|
254 | + $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
255 | + $this->_typeTag); |
|
256 | + $content = $this->_encodedContentDER(); |
|
257 | + $length = new Length(strlen($content)); |
|
258 | + return $identifier->toDER() . $length->toDER() . $content; |
|
259 | + } |
|
260 | 260 | |
261 | - /** |
|
262 | - * |
|
263 | - * @see \ASN1\Feature\ElementBase::tag() |
|
264 | - * @return int |
|
265 | - */ |
|
266 | - public function tag(): int |
|
267 | - { |
|
268 | - return $this->_typeTag; |
|
269 | - } |
|
261 | + /** |
|
262 | + * |
|
263 | + * @see \ASN1\Feature\ElementBase::tag() |
|
264 | + * @return int |
|
265 | + */ |
|
266 | + public function tag(): int |
|
267 | + { |
|
268 | + return $this->_typeTag; |
|
269 | + } |
|
270 | 270 | |
271 | - /** |
|
272 | - * |
|
273 | - * @see \ASN1\Feature\ElementBase::isType() |
|
274 | - * @return bool |
|
275 | - */ |
|
276 | - public function isType(int $tag): bool |
|
277 | - { |
|
278 | - // if element is context specific |
|
279 | - if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) { |
|
280 | - return false; |
|
281 | - } |
|
282 | - // negative tags identify an abstract pseudotype |
|
283 | - if ($tag < 0) { |
|
284 | - return $this->_isPseudoType($tag); |
|
285 | - } |
|
286 | - return $this->_isConcreteType($tag); |
|
287 | - } |
|
271 | + /** |
|
272 | + * |
|
273 | + * @see \ASN1\Feature\ElementBase::isType() |
|
274 | + * @return bool |
|
275 | + */ |
|
276 | + public function isType(int $tag): bool |
|
277 | + { |
|
278 | + // if element is context specific |
|
279 | + if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) { |
|
280 | + return false; |
|
281 | + } |
|
282 | + // negative tags identify an abstract pseudotype |
|
283 | + if ($tag < 0) { |
|
284 | + return $this->_isPseudoType($tag); |
|
285 | + } |
|
286 | + return $this->_isConcreteType($tag); |
|
287 | + } |
|
288 | 288 | |
289 | - /** |
|
290 | - * |
|
291 | - * @see \ASN1\Feature\ElementBase::expectType() |
|
292 | - * @return ElementBase |
|
293 | - */ |
|
294 | - public function expectType(int $tag): ElementBase |
|
295 | - { |
|
296 | - if (!$this->isType($tag)) { |
|
297 | - throw new \UnexpectedValueException( |
|
298 | - sprintf("%s expected, got %s.", self::tagToName($tag), |
|
299 | - $this->_typeDescriptorString())); |
|
300 | - } |
|
301 | - return $this; |
|
302 | - } |
|
289 | + /** |
|
290 | + * |
|
291 | + * @see \ASN1\Feature\ElementBase::expectType() |
|
292 | + * @return ElementBase |
|
293 | + */ |
|
294 | + public function expectType(int $tag): ElementBase |
|
295 | + { |
|
296 | + if (!$this->isType($tag)) { |
|
297 | + throw new \UnexpectedValueException( |
|
298 | + sprintf("%s expected, got %s.", self::tagToName($tag), |
|
299 | + $this->_typeDescriptorString())); |
|
300 | + } |
|
301 | + return $this; |
|
302 | + } |
|
303 | 303 | |
304 | - /** |
|
305 | - * Check whether the element is a concrete type of a given tag. |
|
306 | - * |
|
307 | - * @param int $tag |
|
308 | - * @return bool |
|
309 | - */ |
|
310 | - private function _isConcreteType(int $tag): bool |
|
311 | - { |
|
312 | - // if tag doesn't match |
|
313 | - if ($this->tag() != $tag) { |
|
314 | - return false; |
|
315 | - } |
|
316 | - // if type is universal check that instance is of a correct class |
|
317 | - if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
318 | - $cls = self::_determineUniversalImplClass($tag); |
|
319 | - if (!$this instanceof $cls) { |
|
320 | - return false; |
|
321 | - } |
|
322 | - } |
|
323 | - return true; |
|
324 | - } |
|
304 | + /** |
|
305 | + * Check whether the element is a concrete type of a given tag. |
|
306 | + * |
|
307 | + * @param int $tag |
|
308 | + * @return bool |
|
309 | + */ |
|
310 | + private function _isConcreteType(int $tag): bool |
|
311 | + { |
|
312 | + // if tag doesn't match |
|
313 | + if ($this->tag() != $tag) { |
|
314 | + return false; |
|
315 | + } |
|
316 | + // if type is universal check that instance is of a correct class |
|
317 | + if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
318 | + $cls = self::_determineUniversalImplClass($tag); |
|
319 | + if (!$this instanceof $cls) { |
|
320 | + return false; |
|
321 | + } |
|
322 | + } |
|
323 | + return true; |
|
324 | + } |
|
325 | 325 | |
326 | - /** |
|
327 | - * Check whether the element is a pseudotype. |
|
328 | - * |
|
329 | - * @param int $tag |
|
330 | - * @return bool |
|
331 | - */ |
|
332 | - private function _isPseudoType(int $tag): bool |
|
333 | - { |
|
334 | - switch ($tag) { |
|
335 | - case self::TYPE_STRING: |
|
336 | - return $this instanceof StringType; |
|
337 | - case self::TYPE_TIME: |
|
338 | - return $this instanceof TimeType; |
|
339 | - } |
|
340 | - return false; |
|
341 | - } |
|
326 | + /** |
|
327 | + * Check whether the element is a pseudotype. |
|
328 | + * |
|
329 | + * @param int $tag |
|
330 | + * @return bool |
|
331 | + */ |
|
332 | + private function _isPseudoType(int $tag): bool |
|
333 | + { |
|
334 | + switch ($tag) { |
|
335 | + case self::TYPE_STRING: |
|
336 | + return $this instanceof StringType; |
|
337 | + case self::TYPE_TIME: |
|
338 | + return $this instanceof TimeType; |
|
339 | + } |
|
340 | + return false; |
|
341 | + } |
|
342 | 342 | |
343 | - /** |
|
344 | - * |
|
345 | - * @see \ASN1\Feature\ElementBase::isTagged() |
|
346 | - * @return bool |
|
347 | - */ |
|
348 | - public function isTagged(): bool |
|
349 | - { |
|
350 | - return $this instanceof TaggedType; |
|
351 | - } |
|
343 | + /** |
|
344 | + * |
|
345 | + * @see \ASN1\Feature\ElementBase::isTagged() |
|
346 | + * @return bool |
|
347 | + */ |
|
348 | + public function isTagged(): bool |
|
349 | + { |
|
350 | + return $this instanceof TaggedType; |
|
351 | + } |
|
352 | 352 | |
353 | - /** |
|
354 | - * |
|
355 | - * @see \ASN1\Feature\ElementBase::expectTagged() |
|
356 | - * @return TaggedType |
|
357 | - */ |
|
358 | - public function expectTagged($tag = null): TaggedType |
|
359 | - { |
|
360 | - if (!$this->isTagged()) { |
|
361 | - throw new \UnexpectedValueException( |
|
362 | - sprintf("Context specific element expected, got %s.", |
|
363 | - Identifier::classToName($this->typeClass()))); |
|
364 | - } |
|
365 | - if (isset($tag) && $this->tag() != $tag) { |
|
366 | - throw new \UnexpectedValueException( |
|
367 | - sprintf("Tag %d expected, got %d.", $tag, $this->tag())); |
|
368 | - } |
|
369 | - return $this; |
|
370 | - } |
|
353 | + /** |
|
354 | + * |
|
355 | + * @see \ASN1\Feature\ElementBase::expectTagged() |
|
356 | + * @return TaggedType |
|
357 | + */ |
|
358 | + public function expectTagged($tag = null): TaggedType |
|
359 | + { |
|
360 | + if (!$this->isTagged()) { |
|
361 | + throw new \UnexpectedValueException( |
|
362 | + sprintf("Context specific element expected, got %s.", |
|
363 | + Identifier::classToName($this->typeClass()))); |
|
364 | + } |
|
365 | + if (isset($tag) && $this->tag() != $tag) { |
|
366 | + throw new \UnexpectedValueException( |
|
367 | + sprintf("Tag %d expected, got %d.", $tag, $this->tag())); |
|
368 | + } |
|
369 | + return $this; |
|
370 | + } |
|
371 | 371 | |
372 | - /** |
|
373 | - * |
|
374 | - * @see \ASN1\Feature\ElementBase::asElement() |
|
375 | - * @return Element |
|
376 | - */ |
|
377 | - final public function asElement(): Element |
|
378 | - { |
|
379 | - return $this; |
|
380 | - } |
|
372 | + /** |
|
373 | + * |
|
374 | + * @see \ASN1\Feature\ElementBase::asElement() |
|
375 | + * @return Element |
|
376 | + */ |
|
377 | + final public function asElement(): Element |
|
378 | + { |
|
379 | + return $this; |
|
380 | + } |
|
381 | 381 | |
382 | - /** |
|
383 | - * Get element decorated with UnspecifiedType object. |
|
384 | - * |
|
385 | - * @return UnspecifiedType |
|
386 | - */ |
|
387 | - public function asUnspecified(): UnspecifiedType |
|
388 | - { |
|
389 | - return new UnspecifiedType($this); |
|
390 | - } |
|
382 | + /** |
|
383 | + * Get element decorated with UnspecifiedType object. |
|
384 | + * |
|
385 | + * @return UnspecifiedType |
|
386 | + */ |
|
387 | + public function asUnspecified(): UnspecifiedType |
|
388 | + { |
|
389 | + return new UnspecifiedType($this); |
|
390 | + } |
|
391 | 391 | |
392 | - /** |
|
393 | - * Determine the class that implements the type. |
|
394 | - * |
|
395 | - * @param Identifier $identifier |
|
396 | - * @return string Class name |
|
397 | - */ |
|
398 | - protected static function _determineImplClass(Identifier $identifier): string |
|
399 | - { |
|
400 | - // tagged type |
|
401 | - if ($identifier->isContextSpecific()) { |
|
402 | - return TaggedType::class; |
|
403 | - } |
|
404 | - // universal class |
|
405 | - if ($identifier->isUniversal()) { |
|
406 | - return self::_determineUniversalImplClass($identifier->intTag()); |
|
407 | - } |
|
408 | - throw new \UnexpectedValueException( |
|
409 | - sprintf("%s %d not implemented.", |
|
410 | - Identifier::classToName($identifier->typeClass()), |
|
411 | - $identifier->tag())); |
|
412 | - } |
|
392 | + /** |
|
393 | + * Determine the class that implements the type. |
|
394 | + * |
|
395 | + * @param Identifier $identifier |
|
396 | + * @return string Class name |
|
397 | + */ |
|
398 | + protected static function _determineImplClass(Identifier $identifier): string |
|
399 | + { |
|
400 | + // tagged type |
|
401 | + if ($identifier->isContextSpecific()) { |
|
402 | + return TaggedType::class; |
|
403 | + } |
|
404 | + // universal class |
|
405 | + if ($identifier->isUniversal()) { |
|
406 | + return self::_determineUniversalImplClass($identifier->intTag()); |
|
407 | + } |
|
408 | + throw new \UnexpectedValueException( |
|
409 | + sprintf("%s %d not implemented.", |
|
410 | + Identifier::classToName($identifier->typeClass()), |
|
411 | + $identifier->tag())); |
|
412 | + } |
|
413 | 413 | |
414 | - /** |
|
415 | - * Determine the class that implements an universal type of the given tag. |
|
416 | - * |
|
417 | - * @param int $tag |
|
418 | - * @throws \UnexpectedValueException |
|
419 | - * @return string Class name |
|
420 | - */ |
|
421 | - protected static function _determineUniversalImplClass(int $tag): string |
|
422 | - { |
|
423 | - if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
424 | - throw new \UnexpectedValueException( |
|
425 | - "Universal tag $tag not implemented."); |
|
426 | - } |
|
427 | - return self::MAP_TAG_TO_CLASS[$tag]; |
|
428 | - } |
|
414 | + /** |
|
415 | + * Determine the class that implements an universal type of the given tag. |
|
416 | + * |
|
417 | + * @param int $tag |
|
418 | + * @throws \UnexpectedValueException |
|
419 | + * @return string Class name |
|
420 | + */ |
|
421 | + protected static function _determineUniversalImplClass(int $tag): string |
|
422 | + { |
|
423 | + if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
424 | + throw new \UnexpectedValueException( |
|
425 | + "Universal tag $tag not implemented."); |
|
426 | + } |
|
427 | + return self::MAP_TAG_TO_CLASS[$tag]; |
|
428 | + } |
|
429 | 429 | |
430 | - /** |
|
431 | - * Get textual description of the type for debugging purposes. |
|
432 | - * |
|
433 | - * @return string |
|
434 | - */ |
|
435 | - protected function _typeDescriptorString(): string |
|
436 | - { |
|
437 | - if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
438 | - return self::tagToName($this->_typeTag); |
|
439 | - } |
|
440 | - return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()), |
|
441 | - $this->_typeTag); |
|
442 | - } |
|
430 | + /** |
|
431 | + * Get textual description of the type for debugging purposes. |
|
432 | + * |
|
433 | + * @return string |
|
434 | + */ |
|
435 | + protected function _typeDescriptorString(): string |
|
436 | + { |
|
437 | + if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
438 | + return self::tagToName($this->_typeTag); |
|
439 | + } |
|
440 | + return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()), |
|
441 | + $this->_typeTag); |
|
442 | + } |
|
443 | 443 | |
444 | - /** |
|
445 | - * Get human readable name for an universal tag. |
|
446 | - * |
|
447 | - * @param int $tag |
|
448 | - * @return string |
|
449 | - */ |
|
450 | - public static function tagToName(int $tag): string |
|
451 | - { |
|
452 | - if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
453 | - return "TAG $tag"; |
|
454 | - } |
|
455 | - return self::MAP_TYPE_TO_NAME[$tag]; |
|
456 | - } |
|
444 | + /** |
|
445 | + * Get human readable name for an universal tag. |
|
446 | + * |
|
447 | + * @param int $tag |
|
448 | + * @return string |
|
449 | + */ |
|
450 | + public static function tagToName(int $tag): string |
|
451 | + { |
|
452 | + if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
453 | + return "TAG $tag"; |
|
454 | + } |
|
455 | + return self::MAP_TYPE_TO_NAME[$tag]; |
|
456 | + } |
|
457 | 457 | } |
@@ -332,10 +332,10 @@ |
||
332 | 332 | private function _isPseudoType(int $tag): bool |
333 | 333 | { |
334 | 334 | switch ($tag) { |
335 | - case self::TYPE_STRING: |
|
336 | - return $this instanceof StringType; |
|
337 | - case self::TYPE_TIME: |
|
338 | - return $this instanceof TimeType; |
|
335 | + case self::TYPE_STRING: |
|
336 | + return $this instanceof StringType; |
|
337 | + case self::TYPE_TIME: |
|
338 | + return $this instanceof TimeType; |
|
339 | 339 | } |
340 | 340 | return false; |
341 | 341 | } |
@@ -12,79 +12,79 @@ |
||
12 | 12 | */ |
13 | 13 | interface ElementBase extends Encodable |
14 | 14 | { |
15 | - /** |
|
16 | - * Get the class of the ASN.1 type. |
|
17 | - * |
|
18 | - * One of <code>Identifier::CLASS_*</code> constants. |
|
19 | - * |
|
20 | - * @return int |
|
21 | - */ |
|
22 | - public function typeClass(): int; |
|
15 | + /** |
|
16 | + * Get the class of the ASN.1 type. |
|
17 | + * |
|
18 | + * One of <code>Identifier::CLASS_*</code> constants. |
|
19 | + * |
|
20 | + * @return int |
|
21 | + */ |
|
22 | + public function typeClass(): int; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Check whether the element is constructed. |
|
26 | - * |
|
27 | - * Otherwise it's primitive. |
|
28 | - * |
|
29 | - * @return bool |
|
30 | - */ |
|
31 | - public function isConstructed(): bool; |
|
24 | + /** |
|
25 | + * Check whether the element is constructed. |
|
26 | + * |
|
27 | + * Otherwise it's primitive. |
|
28 | + * |
|
29 | + * @return bool |
|
30 | + */ |
|
31 | + public function isConstructed(): bool; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Get the tag of the element. |
|
35 | - * |
|
36 | - * Interpretation of the tag depends on the context. For example it may |
|
37 | - * represent a universal type tag or a tag of an implicitly or explicitly |
|
38 | - * tagged type. |
|
39 | - * |
|
40 | - * @return int |
|
41 | - */ |
|
42 | - public function tag(): int; |
|
33 | + /** |
|
34 | + * Get the tag of the element. |
|
35 | + * |
|
36 | + * Interpretation of the tag depends on the context. For example it may |
|
37 | + * represent a universal type tag or a tag of an implicitly or explicitly |
|
38 | + * tagged type. |
|
39 | + * |
|
40 | + * @return int |
|
41 | + */ |
|
42 | + public function tag(): int; |
|
43 | 43 | |
44 | - /** |
|
45 | - * Check whether the element is a type of a given tag. |
|
46 | - * |
|
47 | - * @param int $tag Type tag |
|
48 | - * @return boolean |
|
49 | - */ |
|
50 | - public function isType(int $tag): bool; |
|
44 | + /** |
|
45 | + * Check whether the element is a type of a given tag. |
|
46 | + * |
|
47 | + * @param int $tag Type tag |
|
48 | + * @return boolean |
|
49 | + */ |
|
50 | + public function isType(int $tag): bool; |
|
51 | 51 | |
52 | - /** |
|
53 | - * Check whether the element is a type of a given tag. |
|
54 | - * |
|
55 | - * Throws an exception if expectation fails. |
|
56 | - * |
|
57 | - * @param int $tag Type tag |
|
58 | - * @throws \UnexpectedValueException If the element type differs from the |
|
59 | - * expected |
|
60 | - * @return ElementBase |
|
61 | - */ |
|
62 | - public function expectType(int $tag): ElementBase; |
|
52 | + /** |
|
53 | + * Check whether the element is a type of a given tag. |
|
54 | + * |
|
55 | + * Throws an exception if expectation fails. |
|
56 | + * |
|
57 | + * @param int $tag Type tag |
|
58 | + * @throws \UnexpectedValueException If the element type differs from the |
|
59 | + * expected |
|
60 | + * @return ElementBase |
|
61 | + */ |
|
62 | + public function expectType(int $tag): ElementBase; |
|
63 | 63 | |
64 | - /** |
|
65 | - * Check whether the element is tagged (context specific). |
|
66 | - * |
|
67 | - * @return bool |
|
68 | - */ |
|
69 | - public function isTagged(): bool; |
|
64 | + /** |
|
65 | + * Check whether the element is tagged (context specific). |
|
66 | + * |
|
67 | + * @return bool |
|
68 | + */ |
|
69 | + public function isTagged(): bool; |
|
70 | 70 | |
71 | - /** |
|
72 | - * Check whether the element is tagged (context specific) and optionally has |
|
73 | - * a given tag. |
|
74 | - * |
|
75 | - * Throws an exception if the element is not tagged or tag differs from |
|
76 | - * the expected. |
|
77 | - * |
|
78 | - * @param int|null $tag Optional type tag |
|
79 | - * @throws \UnexpectedValueException If expectation fails |
|
80 | - * @return \ASN1\Type\TaggedType |
|
81 | - */ |
|
82 | - public function expectTagged($tag = null): TaggedType; |
|
71 | + /** |
|
72 | + * Check whether the element is tagged (context specific) and optionally has |
|
73 | + * a given tag. |
|
74 | + * |
|
75 | + * Throws an exception if the element is not tagged or tag differs from |
|
76 | + * the expected. |
|
77 | + * |
|
78 | + * @param int|null $tag Optional type tag |
|
79 | + * @throws \UnexpectedValueException If expectation fails |
|
80 | + * @return \ASN1\Type\TaggedType |
|
81 | + */ |
|
82 | + public function expectTagged($tag = null): TaggedType; |
|
83 | 83 | |
84 | - /** |
|
85 | - * Get the object as an abstract Element instance. |
|
86 | - * |
|
87 | - * @return \ASN1\Element |
|
88 | - */ |
|
89 | - public function asElement(): Element; |
|
84 | + /** |
|
85 | + * Get the object as an abstract Element instance. |
|
86 | + * |
|
87 | + * @return \ASN1\Element |
|
88 | + */ |
|
89 | + public function asElement(): Element; |
|
90 | 90 | } |