@@ -23,495 +23,495 @@ |
||
23 | 23 | */ |
24 | 24 | abstract class Element implements ElementBase |
25 | 25 | { |
26 | - // Universal type tags |
|
27 | - const TYPE_EOC = 0x00; |
|
28 | - const TYPE_BOOLEAN = 0x01; |
|
29 | - const TYPE_INTEGER = 0x02; |
|
30 | - const TYPE_BIT_STRING = 0x03; |
|
31 | - const TYPE_OCTET_STRING = 0x04; |
|
32 | - const TYPE_NULL = 0x05; |
|
33 | - const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
34 | - const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
35 | - const TYPE_EXTERNAL = 0x08; |
|
36 | - const TYPE_REAL = 0x09; |
|
37 | - const TYPE_ENUMERATED = 0x0a; |
|
38 | - const TYPE_EMBEDDED_PDV = 0x0b; |
|
39 | - const TYPE_UTF8_STRING = 0x0c; |
|
40 | - const TYPE_RELATIVE_OID = 0x0d; |
|
41 | - const TYPE_SEQUENCE = 0x10; |
|
42 | - const TYPE_SET = 0x11; |
|
43 | - const TYPE_NUMERIC_STRING = 0x12; |
|
44 | - const TYPE_PRINTABLE_STRING = 0x13; |
|
45 | - const TYPE_T61_STRING = 0x14; |
|
46 | - const TYPE_VIDEOTEX_STRING = 0x15; |
|
47 | - const TYPE_IA5_STRING = 0x16; |
|
48 | - const TYPE_UTC_TIME = 0x17; |
|
49 | - const TYPE_GENERALIZED_TIME = 0x18; |
|
50 | - const TYPE_GRAPHIC_STRING = 0x19; |
|
51 | - const TYPE_VISIBLE_STRING = 0x1a; |
|
52 | - const TYPE_GENERAL_STRING = 0x1b; |
|
53 | - const TYPE_UNIVERSAL_STRING = 0x1c; |
|
54 | - const TYPE_CHARACTER_STRING = 0x1d; |
|
55 | - const TYPE_BMP_STRING = 0x1e; |
|
26 | + // Universal type tags |
|
27 | + const TYPE_EOC = 0x00; |
|
28 | + const TYPE_BOOLEAN = 0x01; |
|
29 | + const TYPE_INTEGER = 0x02; |
|
30 | + const TYPE_BIT_STRING = 0x03; |
|
31 | + const TYPE_OCTET_STRING = 0x04; |
|
32 | + const TYPE_NULL = 0x05; |
|
33 | + const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
34 | + const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
35 | + const TYPE_EXTERNAL = 0x08; |
|
36 | + const TYPE_REAL = 0x09; |
|
37 | + const TYPE_ENUMERATED = 0x0a; |
|
38 | + const TYPE_EMBEDDED_PDV = 0x0b; |
|
39 | + const TYPE_UTF8_STRING = 0x0c; |
|
40 | + const TYPE_RELATIVE_OID = 0x0d; |
|
41 | + const TYPE_SEQUENCE = 0x10; |
|
42 | + const TYPE_SET = 0x11; |
|
43 | + const TYPE_NUMERIC_STRING = 0x12; |
|
44 | + const TYPE_PRINTABLE_STRING = 0x13; |
|
45 | + const TYPE_T61_STRING = 0x14; |
|
46 | + const TYPE_VIDEOTEX_STRING = 0x15; |
|
47 | + const TYPE_IA5_STRING = 0x16; |
|
48 | + const TYPE_UTC_TIME = 0x17; |
|
49 | + const TYPE_GENERALIZED_TIME = 0x18; |
|
50 | + const TYPE_GRAPHIC_STRING = 0x19; |
|
51 | + const TYPE_VISIBLE_STRING = 0x1a; |
|
52 | + const TYPE_GENERAL_STRING = 0x1b; |
|
53 | + const TYPE_UNIVERSAL_STRING = 0x1c; |
|
54 | + const TYPE_CHARACTER_STRING = 0x1d; |
|
55 | + const TYPE_BMP_STRING = 0x1e; |
|
56 | 56 | |
57 | - /** |
|
58 | - * Mapping from universal type tag to implementation class name. |
|
59 | - * |
|
60 | - * @internal |
|
61 | - * |
|
62 | - * @var array |
|
63 | - */ |
|
64 | - const MAP_TAG_TO_CLASS = [ /* @formatter:off */ |
|
65 | - self::TYPE_EOC => Primitive\EOC::class, |
|
66 | - self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
67 | - self::TYPE_INTEGER => Primitive\Integer::class, |
|
68 | - self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
69 | - self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
70 | - self::TYPE_NULL => Primitive\NullType::class, |
|
71 | - self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
72 | - self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
73 | - self::TYPE_REAL => Primitive\Real::class, |
|
74 | - self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
75 | - self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
76 | - self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
77 | - self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
78 | - self::TYPE_SET => Constructed\Set::class, |
|
79 | - self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
80 | - self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
81 | - self::TYPE_T61_STRING => Primitive\T61String::class, |
|
82 | - self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
83 | - self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
84 | - self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
85 | - self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
86 | - self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
87 | - self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
88 | - self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
89 | - self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
90 | - self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
91 | - self::TYPE_BMP_STRING => Primitive\BMPString::class |
|
92 | - /* @formatter:on */ |
|
93 | - ]; |
|
57 | + /** |
|
58 | + * Mapping from universal type tag to implementation class name. |
|
59 | + * |
|
60 | + * @internal |
|
61 | + * |
|
62 | + * @var array |
|
63 | + */ |
|
64 | + const MAP_TAG_TO_CLASS = [ /* @formatter:off */ |
|
65 | + self::TYPE_EOC => Primitive\EOC::class, |
|
66 | + self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
67 | + self::TYPE_INTEGER => Primitive\Integer::class, |
|
68 | + self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
69 | + self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
70 | + self::TYPE_NULL => Primitive\NullType::class, |
|
71 | + self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
72 | + self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
73 | + self::TYPE_REAL => Primitive\Real::class, |
|
74 | + self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
75 | + self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
76 | + self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
77 | + self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
78 | + self::TYPE_SET => Constructed\Set::class, |
|
79 | + self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
80 | + self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
81 | + self::TYPE_T61_STRING => Primitive\T61String::class, |
|
82 | + self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
83 | + self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
84 | + self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
85 | + self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
86 | + self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
87 | + self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
88 | + self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
89 | + self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
90 | + self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
91 | + self::TYPE_BMP_STRING => Primitive\BMPString::class |
|
92 | + /* @formatter:on */ |
|
93 | + ]; |
|
94 | 94 | |
95 | - /** |
|
96 | - * Pseudotype for all string types. |
|
97 | - * |
|
98 | - * May be used as an expectation parameter. |
|
99 | - * |
|
100 | - * @var int |
|
101 | - */ |
|
102 | - const TYPE_STRING = -1; |
|
95 | + /** |
|
96 | + * Pseudotype for all string types. |
|
97 | + * |
|
98 | + * May be used as an expectation parameter. |
|
99 | + * |
|
100 | + * @var int |
|
101 | + */ |
|
102 | + const TYPE_STRING = -1; |
|
103 | 103 | |
104 | - /** |
|
105 | - * Pseudotype for all time types. |
|
106 | - * |
|
107 | - * May be used as an expectation parameter. |
|
108 | - * |
|
109 | - * @var int |
|
110 | - */ |
|
111 | - const TYPE_TIME = -2; |
|
104 | + /** |
|
105 | + * Pseudotype for all time types. |
|
106 | + * |
|
107 | + * May be used as an expectation parameter. |
|
108 | + * |
|
109 | + * @var int |
|
110 | + */ |
|
111 | + const TYPE_TIME = -2; |
|
112 | 112 | |
113 | - /** |
|
114 | - * Pseudotype for constructed strings. |
|
115 | - * |
|
116 | - * May be used as an expectation parameter. |
|
117 | - * |
|
118 | - * @var int |
|
119 | - */ |
|
120 | - const TYPE_CONSTRUCTED_STRING = -3; |
|
113 | + /** |
|
114 | + * Pseudotype for constructed strings. |
|
115 | + * |
|
116 | + * May be used as an expectation parameter. |
|
117 | + * |
|
118 | + * @var int |
|
119 | + */ |
|
120 | + const TYPE_CONSTRUCTED_STRING = -3; |
|
121 | 121 | |
122 | - /** |
|
123 | - * Mapping from universal type tag to human readable name. |
|
124 | - * |
|
125 | - * @internal |
|
126 | - * |
|
127 | - * @var array |
|
128 | - */ |
|
129 | - const MAP_TYPE_TO_NAME = [ /* @formatter:off */ |
|
130 | - self::TYPE_EOC => "EOC", |
|
131 | - self::TYPE_BOOLEAN => "BOOLEAN", |
|
132 | - self::TYPE_INTEGER => "INTEGER", |
|
133 | - self::TYPE_BIT_STRING => "BIT STRING", |
|
134 | - self::TYPE_OCTET_STRING => "OCTET STRING", |
|
135 | - self::TYPE_NULL => "NULL", |
|
136 | - self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER", |
|
137 | - self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor", |
|
138 | - self::TYPE_EXTERNAL => "EXTERNAL", |
|
139 | - self::TYPE_REAL => "REAL", |
|
140 | - self::TYPE_ENUMERATED => "ENUMERATED", |
|
141 | - self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV", |
|
142 | - self::TYPE_UTF8_STRING => "UTF8String", |
|
143 | - self::TYPE_RELATIVE_OID => "RELATIVE-OID", |
|
144 | - self::TYPE_SEQUENCE => "SEQUENCE", |
|
145 | - self::TYPE_SET => "SET", |
|
146 | - self::TYPE_NUMERIC_STRING => "NumericString", |
|
147 | - self::TYPE_PRINTABLE_STRING => "PrintableString", |
|
148 | - self::TYPE_T61_STRING => "T61String", |
|
149 | - self::TYPE_VIDEOTEX_STRING => "VideotexString", |
|
150 | - self::TYPE_IA5_STRING => "IA5String", |
|
151 | - self::TYPE_UTC_TIME => "UTCTime", |
|
152 | - self::TYPE_GENERALIZED_TIME => "GeneralizedTime", |
|
153 | - self::TYPE_GRAPHIC_STRING => "GraphicString", |
|
154 | - self::TYPE_VISIBLE_STRING => "VisibleString", |
|
155 | - self::TYPE_GENERAL_STRING => "GeneralString", |
|
156 | - self::TYPE_UNIVERSAL_STRING => "UniversalString", |
|
157 | - self::TYPE_CHARACTER_STRING => "CHARACTER STRING", |
|
158 | - self::TYPE_BMP_STRING => "BMPString", |
|
159 | - self::TYPE_STRING => "Any String", |
|
160 | - self::TYPE_TIME => "Any Time", |
|
161 | - self::TYPE_CONSTRUCTED_STRING => "Constructed String" |
|
162 | - /* @formatter:on */ |
|
163 | - ]; |
|
122 | + /** |
|
123 | + * Mapping from universal type tag to human readable name. |
|
124 | + * |
|
125 | + * @internal |
|
126 | + * |
|
127 | + * @var array |
|
128 | + */ |
|
129 | + const MAP_TYPE_TO_NAME = [ /* @formatter:off */ |
|
130 | + self::TYPE_EOC => "EOC", |
|
131 | + self::TYPE_BOOLEAN => "BOOLEAN", |
|
132 | + self::TYPE_INTEGER => "INTEGER", |
|
133 | + self::TYPE_BIT_STRING => "BIT STRING", |
|
134 | + self::TYPE_OCTET_STRING => "OCTET STRING", |
|
135 | + self::TYPE_NULL => "NULL", |
|
136 | + self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER", |
|
137 | + self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor", |
|
138 | + self::TYPE_EXTERNAL => "EXTERNAL", |
|
139 | + self::TYPE_REAL => "REAL", |
|
140 | + self::TYPE_ENUMERATED => "ENUMERATED", |
|
141 | + self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV", |
|
142 | + self::TYPE_UTF8_STRING => "UTF8String", |
|
143 | + self::TYPE_RELATIVE_OID => "RELATIVE-OID", |
|
144 | + self::TYPE_SEQUENCE => "SEQUENCE", |
|
145 | + self::TYPE_SET => "SET", |
|
146 | + self::TYPE_NUMERIC_STRING => "NumericString", |
|
147 | + self::TYPE_PRINTABLE_STRING => "PrintableString", |
|
148 | + self::TYPE_T61_STRING => "T61String", |
|
149 | + self::TYPE_VIDEOTEX_STRING => "VideotexString", |
|
150 | + self::TYPE_IA5_STRING => "IA5String", |
|
151 | + self::TYPE_UTC_TIME => "UTCTime", |
|
152 | + self::TYPE_GENERALIZED_TIME => "GeneralizedTime", |
|
153 | + self::TYPE_GRAPHIC_STRING => "GraphicString", |
|
154 | + self::TYPE_VISIBLE_STRING => "VisibleString", |
|
155 | + self::TYPE_GENERAL_STRING => "GeneralString", |
|
156 | + self::TYPE_UNIVERSAL_STRING => "UniversalString", |
|
157 | + self::TYPE_CHARACTER_STRING => "CHARACTER STRING", |
|
158 | + self::TYPE_BMP_STRING => "BMPString", |
|
159 | + self::TYPE_STRING => "Any String", |
|
160 | + self::TYPE_TIME => "Any Time", |
|
161 | + self::TYPE_CONSTRUCTED_STRING => "Constructed String" |
|
162 | + /* @formatter:on */ |
|
163 | + ]; |
|
164 | 164 | |
165 | - /** |
|
166 | - * Element's type tag. |
|
167 | - * |
|
168 | - * @var int |
|
169 | - */ |
|
170 | - protected $_typeTag; |
|
165 | + /** |
|
166 | + * Element's type tag. |
|
167 | + * |
|
168 | + * @var int |
|
169 | + */ |
|
170 | + protected $_typeTag; |
|
171 | 171 | |
172 | - /** |
|
173 | - * Whether type shall be encoded with indefinite length. |
|
174 | - * |
|
175 | - * @var bool |
|
176 | - */ |
|
177 | - protected $_indefiniteLength = false; |
|
172 | + /** |
|
173 | + * Whether type shall be encoded with indefinite length. |
|
174 | + * |
|
175 | + * @var bool |
|
176 | + */ |
|
177 | + protected $_indefiniteLength = false; |
|
178 | 178 | |
179 | - /** |
|
180 | - * |
|
181 | - * @see \ASN1\Feature\ElementBase::typeClass() |
|
182 | - * @return int |
|
183 | - */ |
|
184 | - abstract public function typeClass(): int; |
|
179 | + /** |
|
180 | + * |
|
181 | + * @see \ASN1\Feature\ElementBase::typeClass() |
|
182 | + * @return int |
|
183 | + */ |
|
184 | + abstract public function typeClass(): int; |
|
185 | 185 | |
186 | - /** |
|
187 | - * |
|
188 | - * @see \ASN1\Feature\ElementBase::isConstructed() |
|
189 | - * @return bool |
|
190 | - */ |
|
191 | - abstract public function isConstructed(): bool; |
|
186 | + /** |
|
187 | + * |
|
188 | + * @see \ASN1\Feature\ElementBase::isConstructed() |
|
189 | + * @return bool |
|
190 | + */ |
|
191 | + abstract public function isConstructed(): bool; |
|
192 | 192 | |
193 | - /** |
|
194 | - * Get the content encoded in DER. |
|
195 | - * |
|
196 | - * Returns the DER encoded content without identifier and length header |
|
197 | - * octets. |
|
198 | - * |
|
199 | - * @return string |
|
200 | - */ |
|
201 | - abstract protected function _encodedContentDER(): string; |
|
193 | + /** |
|
194 | + * Get the content encoded in DER. |
|
195 | + * |
|
196 | + * Returns the DER encoded content without identifier and length header |
|
197 | + * octets. |
|
198 | + * |
|
199 | + * @return string |
|
200 | + */ |
|
201 | + abstract protected function _encodedContentDER(): string; |
|
202 | 202 | |
203 | - /** |
|
204 | - * Decode type-specific element from DER. |
|
205 | - * |
|
206 | - * @param Identifier $identifier Pre-parsed identifier |
|
207 | - * @param string $data DER data |
|
208 | - * @param int $offset Offset in data to the next byte after identifier |
|
209 | - * @throws DecodeException If decoding fails |
|
210 | - * @return ElementBase |
|
211 | - */ |
|
212 | - protected static function _decodeFromDER(Identifier $identifier, |
|
213 | - string $data, int &$offset): ElementBase |
|
214 | - { |
|
215 | - throw new \BadMethodCallException( |
|
216 | - __METHOD__ . " must be implemented in derived class."); |
|
217 | - } |
|
203 | + /** |
|
204 | + * Decode type-specific element from DER. |
|
205 | + * |
|
206 | + * @param Identifier $identifier Pre-parsed identifier |
|
207 | + * @param string $data DER data |
|
208 | + * @param int $offset Offset in data to the next byte after identifier |
|
209 | + * @throws DecodeException If decoding fails |
|
210 | + * @return ElementBase |
|
211 | + */ |
|
212 | + protected static function _decodeFromDER(Identifier $identifier, |
|
213 | + string $data, int &$offset): ElementBase |
|
214 | + { |
|
215 | + throw new \BadMethodCallException( |
|
216 | + __METHOD__ . " must be implemented in derived class."); |
|
217 | + } |
|
218 | 218 | |
219 | - /** |
|
220 | - * Decode element from DER data. |
|
221 | - * |
|
222 | - * @param string $data DER encoded data |
|
223 | - * @param int|null $offset Reference to the variable that contains offset |
|
224 | - * into the data where to start parsing. Variable is updated to |
|
225 | - * the offset next to the parsed element. If null, start from offset |
|
226 | - * 0. |
|
227 | - * @throws DecodeException If decoding fails |
|
228 | - * @throws \UnexpectedValueException If called in the context of an expected |
|
229 | - * type, but decoding yields another type |
|
230 | - * @return ElementBase |
|
231 | - */ |
|
232 | - public static function fromDER(string $data, int &$offset = null): ElementBase |
|
233 | - { |
|
234 | - $idx = $offset ?? 0; |
|
235 | - // decode identifier |
|
236 | - $identifier = Identifier::fromDER($data, $idx); |
|
237 | - // determine class that implements type specific decoding |
|
238 | - $cls = self::_determineImplClass($identifier); |
|
239 | - try { |
|
240 | - // decode remaining element |
|
241 | - $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
242 | - } catch (\LogicException $e) { |
|
243 | - // rethrow as a RuntimeException for unified exception handling |
|
244 | - throw new DecodeException( |
|
245 | - sprintf("Error while decoding %s.", |
|
246 | - self::tagToName($identifier->intTag())), 0, $e); |
|
247 | - } |
|
248 | - // if called in the context of a concrete class, check |
|
249 | - // that decoded type matches the type of a calling class |
|
250 | - $called_class = get_called_class(); |
|
251 | - if (self::class != $called_class) { |
|
252 | - if (!$element instanceof $called_class) { |
|
253 | - throw new \UnexpectedValueException( |
|
254 | - sprintf("%s expected, got %s.", $called_class, |
|
255 | - get_class($element))); |
|
256 | - } |
|
257 | - } |
|
258 | - // update offset for the caller |
|
259 | - if (isset($offset)) { |
|
260 | - $offset = $idx; |
|
261 | - } |
|
262 | - return $element; |
|
263 | - } |
|
219 | + /** |
|
220 | + * Decode element from DER data. |
|
221 | + * |
|
222 | + * @param string $data DER encoded data |
|
223 | + * @param int|null $offset Reference to the variable that contains offset |
|
224 | + * into the data where to start parsing. Variable is updated to |
|
225 | + * the offset next to the parsed element. If null, start from offset |
|
226 | + * 0. |
|
227 | + * @throws DecodeException If decoding fails |
|
228 | + * @throws \UnexpectedValueException If called in the context of an expected |
|
229 | + * type, but decoding yields another type |
|
230 | + * @return ElementBase |
|
231 | + */ |
|
232 | + public static function fromDER(string $data, int &$offset = null): ElementBase |
|
233 | + { |
|
234 | + $idx = $offset ?? 0; |
|
235 | + // decode identifier |
|
236 | + $identifier = Identifier::fromDER($data, $idx); |
|
237 | + // determine class that implements type specific decoding |
|
238 | + $cls = self::_determineImplClass($identifier); |
|
239 | + try { |
|
240 | + // decode remaining element |
|
241 | + $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
242 | + } catch (\LogicException $e) { |
|
243 | + // rethrow as a RuntimeException for unified exception handling |
|
244 | + throw new DecodeException( |
|
245 | + sprintf("Error while decoding %s.", |
|
246 | + self::tagToName($identifier->intTag())), 0, $e); |
|
247 | + } |
|
248 | + // if called in the context of a concrete class, check |
|
249 | + // that decoded type matches the type of a calling class |
|
250 | + $called_class = get_called_class(); |
|
251 | + if (self::class != $called_class) { |
|
252 | + if (!$element instanceof $called_class) { |
|
253 | + throw new \UnexpectedValueException( |
|
254 | + sprintf("%s expected, got %s.", $called_class, |
|
255 | + get_class($element))); |
|
256 | + } |
|
257 | + } |
|
258 | + // update offset for the caller |
|
259 | + if (isset($offset)) { |
|
260 | + $offset = $idx; |
|
261 | + } |
|
262 | + return $element; |
|
263 | + } |
|
264 | 264 | |
265 | - /** |
|
266 | - * |
|
267 | - * @see \ASN1\Feature\Encodable::toDER() |
|
268 | - * @return string |
|
269 | - */ |
|
270 | - public function toDER(): string |
|
271 | - { |
|
272 | - $identifier = new Identifier($this->typeClass(), |
|
273 | - $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
274 | - $this->_typeTag); |
|
275 | - $content = $this->_encodedContentDER(); |
|
276 | - if ($this->_indefiniteLength) { |
|
277 | - $length = new Length(0, true); |
|
278 | - $eoc = new Primitive\EOC(); |
|
279 | - return $identifier->toDER() . $length->toDER() . $content . |
|
280 | - $eoc->toDER(); |
|
281 | - } else { |
|
282 | - $length = new Length(strlen($content)); |
|
283 | - return $identifier->toDER() . $length->toDER() . $content; |
|
284 | - } |
|
285 | - } |
|
265 | + /** |
|
266 | + * |
|
267 | + * @see \ASN1\Feature\Encodable::toDER() |
|
268 | + * @return string |
|
269 | + */ |
|
270 | + public function toDER(): string |
|
271 | + { |
|
272 | + $identifier = new Identifier($this->typeClass(), |
|
273 | + $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
274 | + $this->_typeTag); |
|
275 | + $content = $this->_encodedContentDER(); |
|
276 | + if ($this->_indefiniteLength) { |
|
277 | + $length = new Length(0, true); |
|
278 | + $eoc = new Primitive\EOC(); |
|
279 | + return $identifier->toDER() . $length->toDER() . $content . |
|
280 | + $eoc->toDER(); |
|
281 | + } else { |
|
282 | + $length = new Length(strlen($content)); |
|
283 | + return $identifier->toDER() . $length->toDER() . $content; |
|
284 | + } |
|
285 | + } |
|
286 | 286 | |
287 | - /** |
|
288 | - * |
|
289 | - * @see \ASN1\Feature\ElementBase::tag() |
|
290 | - * @return int |
|
291 | - */ |
|
292 | - public function tag(): int |
|
293 | - { |
|
294 | - return $this->_typeTag; |
|
295 | - } |
|
287 | + /** |
|
288 | + * |
|
289 | + * @see \ASN1\Feature\ElementBase::tag() |
|
290 | + * @return int |
|
291 | + */ |
|
292 | + public function tag(): int |
|
293 | + { |
|
294 | + return $this->_typeTag; |
|
295 | + } |
|
296 | 296 | |
297 | - /** |
|
298 | - * |
|
299 | - * @see \ASN1\Feature\ElementBase::isType() |
|
300 | - * @return bool |
|
301 | - */ |
|
302 | - public function isType(int $tag): bool |
|
303 | - { |
|
304 | - // if element is context specific |
|
305 | - if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) { |
|
306 | - return false; |
|
307 | - } |
|
308 | - // negative tags identify an abstract pseudotype |
|
309 | - if ($tag < 0) { |
|
310 | - return $this->_isPseudoType($tag); |
|
311 | - } |
|
312 | - return $this->_isConcreteType($tag); |
|
313 | - } |
|
297 | + /** |
|
298 | + * |
|
299 | + * @see \ASN1\Feature\ElementBase::isType() |
|
300 | + * @return bool |
|
301 | + */ |
|
302 | + public function isType(int $tag): bool |
|
303 | + { |
|
304 | + // if element is context specific |
|
305 | + if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) { |
|
306 | + return false; |
|
307 | + } |
|
308 | + // negative tags identify an abstract pseudotype |
|
309 | + if ($tag < 0) { |
|
310 | + return $this->_isPseudoType($tag); |
|
311 | + } |
|
312 | + return $this->_isConcreteType($tag); |
|
313 | + } |
|
314 | 314 | |
315 | - /** |
|
316 | - * |
|
317 | - * @see \ASN1\Feature\ElementBase::expectType() |
|
318 | - * @return ElementBase |
|
319 | - */ |
|
320 | - public function expectType(int $tag): ElementBase |
|
321 | - { |
|
322 | - if (!$this->isType($tag)) { |
|
323 | - throw new \UnexpectedValueException( |
|
324 | - sprintf("%s expected, got %s.", self::tagToName($tag), |
|
325 | - $this->_typeDescriptorString())); |
|
326 | - } |
|
327 | - return $this; |
|
328 | - } |
|
315 | + /** |
|
316 | + * |
|
317 | + * @see \ASN1\Feature\ElementBase::expectType() |
|
318 | + * @return ElementBase |
|
319 | + */ |
|
320 | + public function expectType(int $tag): ElementBase |
|
321 | + { |
|
322 | + if (!$this->isType($tag)) { |
|
323 | + throw new \UnexpectedValueException( |
|
324 | + sprintf("%s expected, got %s.", self::tagToName($tag), |
|
325 | + $this->_typeDescriptorString())); |
|
326 | + } |
|
327 | + return $this; |
|
328 | + } |
|
329 | 329 | |
330 | - /** |
|
331 | - * Check whether the element is a concrete type of a given tag. |
|
332 | - * |
|
333 | - * @param int $tag |
|
334 | - * @return bool |
|
335 | - */ |
|
336 | - private function _isConcreteType(int $tag): bool |
|
337 | - { |
|
338 | - // if tag doesn't match |
|
339 | - if ($this->tag() != $tag) { |
|
340 | - return false; |
|
341 | - } |
|
342 | - // if type is universal check that instance is of a correct class |
|
343 | - if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
344 | - $cls = self::_determineUniversalImplClass($tag); |
|
345 | - if (!$this instanceof $cls) { |
|
346 | - return false; |
|
347 | - } |
|
348 | - } |
|
349 | - return true; |
|
350 | - } |
|
330 | + /** |
|
331 | + * Check whether the element is a concrete type of a given tag. |
|
332 | + * |
|
333 | + * @param int $tag |
|
334 | + * @return bool |
|
335 | + */ |
|
336 | + private function _isConcreteType(int $tag): bool |
|
337 | + { |
|
338 | + // if tag doesn't match |
|
339 | + if ($this->tag() != $tag) { |
|
340 | + return false; |
|
341 | + } |
|
342 | + // if type is universal check that instance is of a correct class |
|
343 | + if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
344 | + $cls = self::_determineUniversalImplClass($tag); |
|
345 | + if (!$this instanceof $cls) { |
|
346 | + return false; |
|
347 | + } |
|
348 | + } |
|
349 | + return true; |
|
350 | + } |
|
351 | 351 | |
352 | - /** |
|
353 | - * Check whether the element is a pseudotype. |
|
354 | - * |
|
355 | - * @param int $tag |
|
356 | - * @return bool |
|
357 | - */ |
|
358 | - private function _isPseudoType(int $tag): bool |
|
359 | - { |
|
360 | - switch ($tag) { |
|
361 | - case self::TYPE_STRING: |
|
362 | - return $this instanceof StringType; |
|
363 | - case self::TYPE_TIME: |
|
364 | - return $this instanceof TimeType; |
|
365 | - case self::TYPE_CONSTRUCTED_STRING: |
|
366 | - return $this instanceof ConstructedString; |
|
367 | - } |
|
368 | - return false; |
|
369 | - } |
|
352 | + /** |
|
353 | + * Check whether the element is a pseudotype. |
|
354 | + * |
|
355 | + * @param int $tag |
|
356 | + * @return bool |
|
357 | + */ |
|
358 | + private function _isPseudoType(int $tag): bool |
|
359 | + { |
|
360 | + switch ($tag) { |
|
361 | + case self::TYPE_STRING: |
|
362 | + return $this instanceof StringType; |
|
363 | + case self::TYPE_TIME: |
|
364 | + return $this instanceof TimeType; |
|
365 | + case self::TYPE_CONSTRUCTED_STRING: |
|
366 | + return $this instanceof ConstructedString; |
|
367 | + } |
|
368 | + return false; |
|
369 | + } |
|
370 | 370 | |
371 | - /** |
|
372 | - * |
|
373 | - * @see \ASN1\Feature\ElementBase::isTagged() |
|
374 | - * @return bool |
|
375 | - */ |
|
376 | - public function isTagged(): bool |
|
377 | - { |
|
378 | - return $this instanceof TaggedType; |
|
379 | - } |
|
371 | + /** |
|
372 | + * |
|
373 | + * @see \ASN1\Feature\ElementBase::isTagged() |
|
374 | + * @return bool |
|
375 | + */ |
|
376 | + public function isTagged(): bool |
|
377 | + { |
|
378 | + return $this instanceof TaggedType; |
|
379 | + } |
|
380 | 380 | |
381 | - /** |
|
382 | - * |
|
383 | - * @see \ASN1\Feature\ElementBase::expectTagged() |
|
384 | - * @return TaggedType |
|
385 | - */ |
|
386 | - public function expectTagged($tag = null): TaggedType |
|
387 | - { |
|
388 | - if (!$this->isTagged()) { |
|
389 | - throw new \UnexpectedValueException( |
|
390 | - sprintf("Context specific element expected, got %s.", |
|
391 | - Identifier::classToName($this->typeClass()))); |
|
392 | - } |
|
393 | - if (isset($tag) && $this->tag() != $tag) { |
|
394 | - throw new \UnexpectedValueException( |
|
395 | - sprintf("Tag %d expected, got %d.", $tag, $this->tag())); |
|
396 | - } |
|
397 | - return $this; |
|
398 | - } |
|
381 | + /** |
|
382 | + * |
|
383 | + * @see \ASN1\Feature\ElementBase::expectTagged() |
|
384 | + * @return TaggedType |
|
385 | + */ |
|
386 | + public function expectTagged($tag = null): TaggedType |
|
387 | + { |
|
388 | + if (!$this->isTagged()) { |
|
389 | + throw new \UnexpectedValueException( |
|
390 | + sprintf("Context specific element expected, got %s.", |
|
391 | + Identifier::classToName($this->typeClass()))); |
|
392 | + } |
|
393 | + if (isset($tag) && $this->tag() != $tag) { |
|
394 | + throw new \UnexpectedValueException( |
|
395 | + sprintf("Tag %d expected, got %d.", $tag, $this->tag())); |
|
396 | + } |
|
397 | + return $this; |
|
398 | + } |
|
399 | 399 | |
400 | - /** |
|
401 | - * Whether element has indefinite length. |
|
402 | - * |
|
403 | - * @return bool |
|
404 | - */ |
|
405 | - public function hasIndefiniteLength(): bool |
|
406 | - { |
|
407 | - return $this->_indefiniteLength; |
|
408 | - } |
|
400 | + /** |
|
401 | + * Whether element has indefinite length. |
|
402 | + * |
|
403 | + * @return bool |
|
404 | + */ |
|
405 | + public function hasIndefiniteLength(): bool |
|
406 | + { |
|
407 | + return $this->_indefiniteLength; |
|
408 | + } |
|
409 | 409 | |
410 | - /** |
|
411 | - * Get self with indefinite length encoding set. |
|
412 | - * |
|
413 | - * @param bool $indefinite True for indefinite length, false for definite |
|
414 | - * length |
|
415 | - * @return self |
|
416 | - */ |
|
417 | - public function withIndefiniteLength(bool $indefinite = true): self |
|
418 | - { |
|
419 | - $obj = clone $this; |
|
420 | - $obj->_indefiniteLength = $indefinite; |
|
421 | - return $obj; |
|
422 | - } |
|
410 | + /** |
|
411 | + * Get self with indefinite length encoding set. |
|
412 | + * |
|
413 | + * @param bool $indefinite True for indefinite length, false for definite |
|
414 | + * length |
|
415 | + * @return self |
|
416 | + */ |
|
417 | + public function withIndefiniteLength(bool $indefinite = true): self |
|
418 | + { |
|
419 | + $obj = clone $this; |
|
420 | + $obj->_indefiniteLength = $indefinite; |
|
421 | + return $obj; |
|
422 | + } |
|
423 | 423 | |
424 | - /** |
|
425 | - * |
|
426 | - * @see \ASN1\Feature\ElementBase::asElement() |
|
427 | - * @return Element |
|
428 | - */ |
|
429 | - final public function asElement(): Element |
|
430 | - { |
|
431 | - return $this; |
|
432 | - } |
|
424 | + /** |
|
425 | + * |
|
426 | + * @see \ASN1\Feature\ElementBase::asElement() |
|
427 | + * @return Element |
|
428 | + */ |
|
429 | + final public function asElement(): Element |
|
430 | + { |
|
431 | + return $this; |
|
432 | + } |
|
433 | 433 | |
434 | - /** |
|
435 | - * Get element decorated with UnspecifiedType object. |
|
436 | - * |
|
437 | - * @return UnspecifiedType |
|
438 | - */ |
|
439 | - public function asUnspecified(): UnspecifiedType |
|
440 | - { |
|
441 | - return new UnspecifiedType($this); |
|
442 | - } |
|
434 | + /** |
|
435 | + * Get element decorated with UnspecifiedType object. |
|
436 | + * |
|
437 | + * @return UnspecifiedType |
|
438 | + */ |
|
439 | + public function asUnspecified(): UnspecifiedType |
|
440 | + { |
|
441 | + return new UnspecifiedType($this); |
|
442 | + } |
|
443 | 443 | |
444 | - /** |
|
445 | - * Determine the class that implements the type. |
|
446 | - * |
|
447 | - * @param Identifier $identifier |
|
448 | - * @return string Class name |
|
449 | - */ |
|
450 | - protected static function _determineImplClass(Identifier $identifier): string |
|
451 | - { |
|
452 | - switch ($identifier->typeClass()) { |
|
453 | - case Identifier::CLASS_UNIVERSAL: |
|
454 | - $cls = self::_determineUniversalImplClass($identifier->intTag()); |
|
455 | - // constructed strings may be present in BER |
|
456 | - if ($identifier->isConstructed() && |
|
457 | - is_subclass_of($cls, StringType::class)) { |
|
458 | - $cls = ConstructedString::class; |
|
459 | - } |
|
460 | - return $cls; |
|
461 | - case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
462 | - return ContextSpecificType::class; |
|
463 | - case Identifier::CLASS_APPLICATION: |
|
464 | - return ApplicationType::class; |
|
465 | - case Identifier::CLASS_PRIVATE: |
|
466 | - return PrivateType::class; |
|
467 | - } |
|
468 | - throw new \UnexpectedValueException( |
|
469 | - sprintf("%s %d not implemented.", |
|
470 | - Identifier::classToName($identifier->typeClass()), |
|
471 | - $identifier->tag())); |
|
472 | - } |
|
444 | + /** |
|
445 | + * Determine the class that implements the type. |
|
446 | + * |
|
447 | + * @param Identifier $identifier |
|
448 | + * @return string Class name |
|
449 | + */ |
|
450 | + protected static function _determineImplClass(Identifier $identifier): string |
|
451 | + { |
|
452 | + switch ($identifier->typeClass()) { |
|
453 | + case Identifier::CLASS_UNIVERSAL: |
|
454 | + $cls = self::_determineUniversalImplClass($identifier->intTag()); |
|
455 | + // constructed strings may be present in BER |
|
456 | + if ($identifier->isConstructed() && |
|
457 | + is_subclass_of($cls, StringType::class)) { |
|
458 | + $cls = ConstructedString::class; |
|
459 | + } |
|
460 | + return $cls; |
|
461 | + case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
462 | + return ContextSpecificType::class; |
|
463 | + case Identifier::CLASS_APPLICATION: |
|
464 | + return ApplicationType::class; |
|
465 | + case Identifier::CLASS_PRIVATE: |
|
466 | + return PrivateType::class; |
|
467 | + } |
|
468 | + throw new \UnexpectedValueException( |
|
469 | + sprintf("%s %d not implemented.", |
|
470 | + Identifier::classToName($identifier->typeClass()), |
|
471 | + $identifier->tag())); |
|
472 | + } |
|
473 | 473 | |
474 | - /** |
|
475 | - * Determine the class that implements an universal type of the given tag. |
|
476 | - * |
|
477 | - * @param int $tag |
|
478 | - * @throws \UnexpectedValueException |
|
479 | - * @return string Class name |
|
480 | - */ |
|
481 | - protected static function _determineUniversalImplClass(int $tag): string |
|
482 | - { |
|
483 | - if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
484 | - throw new \UnexpectedValueException( |
|
485 | - "Universal tag $tag not implemented."); |
|
486 | - } |
|
487 | - return self::MAP_TAG_TO_CLASS[$tag]; |
|
488 | - } |
|
474 | + /** |
|
475 | + * Determine the class that implements an universal type of the given tag. |
|
476 | + * |
|
477 | + * @param int $tag |
|
478 | + * @throws \UnexpectedValueException |
|
479 | + * @return string Class name |
|
480 | + */ |
|
481 | + protected static function _determineUniversalImplClass(int $tag): string |
|
482 | + { |
|
483 | + if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
484 | + throw new \UnexpectedValueException( |
|
485 | + "Universal tag $tag not implemented."); |
|
486 | + } |
|
487 | + return self::MAP_TAG_TO_CLASS[$tag]; |
|
488 | + } |
|
489 | 489 | |
490 | - /** |
|
491 | - * Get textual description of the type for debugging purposes. |
|
492 | - * |
|
493 | - * @return string |
|
494 | - */ |
|
495 | - protected function _typeDescriptorString(): string |
|
496 | - { |
|
497 | - if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
498 | - return self::tagToName($this->_typeTag); |
|
499 | - } |
|
500 | - return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()), |
|
501 | - $this->_typeTag); |
|
502 | - } |
|
490 | + /** |
|
491 | + * Get textual description of the type for debugging purposes. |
|
492 | + * |
|
493 | + * @return string |
|
494 | + */ |
|
495 | + protected function _typeDescriptorString(): string |
|
496 | + { |
|
497 | + if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
498 | + return self::tagToName($this->_typeTag); |
|
499 | + } |
|
500 | + return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()), |
|
501 | + $this->_typeTag); |
|
502 | + } |
|
503 | 503 | |
504 | - /** |
|
505 | - * Get human readable name for an universal tag. |
|
506 | - * |
|
507 | - * @param int $tag |
|
508 | - * @return string |
|
509 | - */ |
|
510 | - public static function tagToName(int $tag): string |
|
511 | - { |
|
512 | - if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
513 | - return "TAG $tag"; |
|
514 | - } |
|
515 | - return self::MAP_TYPE_TO_NAME[$tag]; |
|
516 | - } |
|
504 | + /** |
|
505 | + * Get human readable name for an universal tag. |
|
506 | + * |
|
507 | + * @param int $tag |
|
508 | + * @return string |
|
509 | + */ |
|
510 | + public static function tagToName(int $tag): string |
|
511 | + { |
|
512 | + if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
513 | + return "TAG $tag"; |
|
514 | + } |
|
515 | + return self::MAP_TYPE_TO_NAME[$tag]; |
|
516 | + } |
|
517 | 517 | } |
@@ -358,12 +358,12 @@ discard block |
||
358 | 358 | private function _isPseudoType(int $tag): bool |
359 | 359 | { |
360 | 360 | switch ($tag) { |
361 | - case self::TYPE_STRING: |
|
362 | - return $this instanceof StringType; |
|
363 | - case self::TYPE_TIME: |
|
364 | - return $this instanceof TimeType; |
|
365 | - case self::TYPE_CONSTRUCTED_STRING: |
|
366 | - return $this instanceof ConstructedString; |
|
361 | + case self::TYPE_STRING: |
|
362 | + return $this instanceof StringType; |
|
363 | + case self::TYPE_TIME: |
|
364 | + return $this instanceof TimeType; |
|
365 | + case self::TYPE_CONSTRUCTED_STRING: |
|
366 | + return $this instanceof ConstructedString; |
|
367 | 367 | } |
368 | 368 | return false; |
369 | 369 | } |
@@ -450,20 +450,20 @@ discard block |
||
450 | 450 | protected static function _determineImplClass(Identifier $identifier): string |
451 | 451 | { |
452 | 452 | switch ($identifier->typeClass()) { |
453 | - case Identifier::CLASS_UNIVERSAL: |
|
454 | - $cls = self::_determineUniversalImplClass($identifier->intTag()); |
|
455 | - // constructed strings may be present in BER |
|
456 | - if ($identifier->isConstructed() && |
|
457 | - is_subclass_of($cls, StringType::class)) { |
|
458 | - $cls = ConstructedString::class; |
|
459 | - } |
|
460 | - return $cls; |
|
461 | - case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
462 | - return ContextSpecificType::class; |
|
463 | - case Identifier::CLASS_APPLICATION: |
|
464 | - return ApplicationType::class; |
|
465 | - case Identifier::CLASS_PRIVATE: |
|
466 | - return PrivateType::class; |
|
453 | + case Identifier::CLASS_UNIVERSAL: |
|
454 | + $cls = self::_determineUniversalImplClass($identifier->intTag()); |
|
455 | + // constructed strings may be present in BER |
|
456 | + if ($identifier->isConstructed() && |
|
457 | + is_subclass_of($cls, StringType::class)) { |
|
458 | + $cls = ConstructedString::class; |
|
459 | + } |
|
460 | + return $cls; |
|
461 | + case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
462 | + return ContextSpecificType::class; |
|
463 | + case Identifier::CLASS_APPLICATION: |
|
464 | + return ApplicationType::class; |
|
465 | + case Identifier::CLASS_PRIVATE: |
|
466 | + return PrivateType::class; |
|
467 | 467 | } |
468 | 468 | throw new \UnexpectedValueException( |
469 | 469 | sprintf("%s %d not implemented.", |