1 | <?php |
||
43 | abstract class Element implements ElementBase |
||
44 | { |
||
45 | // Universal type tags |
||
46 | const TYPE_EOC = 0x00; |
||
47 | const TYPE_BOOLEAN = 0x01; |
||
48 | const TYPE_INTEGER = 0x02; |
||
49 | const TYPE_BIT_STRING = 0x03; |
||
50 | const TYPE_OCTET_STRING = 0x04; |
||
51 | const TYPE_NULL = 0x05; |
||
52 | const TYPE_OBJECT_IDENTIFIER = 0x06; |
||
53 | const TYPE_OBJECT_DESCRIPTOR = 0x07; |
||
54 | const TYPE_EXTERNAL = 0x08; |
||
55 | const TYPE_REAL = 0x09; |
||
56 | const TYPE_ENUMERATED = 0x0a; |
||
57 | const TYPE_EMBEDDED_PDV = 0x0b; |
||
58 | const TYPE_UTF8_STRING = 0x0c; |
||
59 | const TYPE_RELATIVE_OID = 0x0d; |
||
60 | const TYPE_SEQUENCE = 0x10; |
||
61 | const TYPE_SET = 0x11; |
||
62 | const TYPE_NUMERIC_STRING = 0x12; |
||
63 | const TYPE_PRINTABLE_STRING = 0x13; |
||
64 | const TYPE_T61_STRING = 0x14; |
||
65 | const TYPE_VIDEOTEX_STRING = 0x15; |
||
66 | const TYPE_IA5_STRING = 0x16; |
||
67 | const TYPE_UTC_TIME = 0x17; |
||
68 | const TYPE_GENERALIZED_TIME = 0x18; |
||
69 | const TYPE_GRAPHIC_STRING = 0x19; |
||
70 | const TYPE_VISIBLE_STRING = 0x1a; |
||
71 | const TYPE_GENERAL_STRING = 0x1b; |
||
72 | const TYPE_UNIVERSAL_STRING = 0x1c; |
||
73 | const TYPE_CHARACTER_STRING = 0x1d; |
||
74 | const TYPE_BMP_STRING = 0x1e; |
||
75 | |||
76 | /** |
||
77 | * Mapping from universal type tag to implementation class name. |
||
78 | * |
||
79 | * @internal |
||
80 | * |
||
81 | * @var array |
||
82 | */ |
||
83 | const MAP_TAG_TO_CLASS = array( |
||
84 | /* @formatter:off */ |
||
85 | self::TYPE_BOOLEAN => Boolean::class, |
||
86 | self::TYPE_INTEGER => Integer::class, |
||
87 | self::TYPE_BIT_STRING => BitString::class, |
||
88 | self::TYPE_OCTET_STRING => OctetString::class, |
||
89 | self::TYPE_NULL => NullType::class, |
||
90 | self::TYPE_OBJECT_IDENTIFIER => ObjectIdentifier::class, |
||
91 | self::TYPE_OBJECT_DESCRIPTOR => ObjectDescriptor::class, |
||
92 | self::TYPE_REAL => Real::class, |
||
93 | self::TYPE_ENUMERATED => Enumerated::class, |
||
94 | self::TYPE_UTF8_STRING => UTF8String::class, |
||
95 | self::TYPE_RELATIVE_OID => RelativeOID::class, |
||
96 | self::TYPE_SEQUENCE => Sequence::class, |
||
97 | self::TYPE_SET => Set::class, |
||
98 | self::TYPE_NUMERIC_STRING => NumericString::class, |
||
99 | self::TYPE_PRINTABLE_STRING => PrintableString::class, |
||
100 | self::TYPE_T61_STRING => T61String::class, |
||
101 | self::TYPE_VIDEOTEX_STRING => VideotexString::class, |
||
102 | self::TYPE_IA5_STRING => IA5String::class, |
||
103 | self::TYPE_UTC_TIME => UTCTime::class, |
||
104 | self::TYPE_GENERALIZED_TIME => GeneralizedTime::class, |
||
105 | self::TYPE_GRAPHIC_STRING => GraphicString::class, |
||
106 | self::TYPE_VISIBLE_STRING => VisibleString::class, |
||
107 | self::TYPE_GENERAL_STRING => GeneralString::class, |
||
108 | self::TYPE_UNIVERSAL_STRING => UniversalString::class, |
||
109 | self::TYPE_CHARACTER_STRING => CharacterString::class, |
||
110 | self::TYPE_BMP_STRING => BMPString::class |
||
111 | /* @formatter:on */ |
||
112 | ); |
||
113 | |||
114 | /** |
||
115 | * Pseudotype for all string types. |
||
116 | * |
||
117 | * May be used as an expectation parameter. |
||
118 | * |
||
119 | * @var int |
||
120 | */ |
||
121 | const TYPE_STRING = -1; |
||
122 | |||
123 | /** |
||
124 | * Pseudotype for all time types. |
||
125 | * |
||
126 | * May be used as an expectation parameter. |
||
127 | * |
||
128 | * @var int |
||
129 | */ |
||
130 | const TYPE_TIME = -2; |
||
131 | |||
132 | /** |
||
133 | * Mapping from universal type tag to human readable name. |
||
134 | * |
||
135 | * @internal |
||
136 | * |
||
137 | * @var array |
||
138 | */ |
||
139 | const MAP_TYPE_TO_NAME = array( |
||
140 | /* @formatter:off */ |
||
141 | self::TYPE_EOC => "EOC", |
||
142 | self::TYPE_BOOLEAN => "BOOLEAN", |
||
143 | self::TYPE_INTEGER => "INTEGER", |
||
144 | self::TYPE_BIT_STRING => "BIT STRING", |
||
145 | self::TYPE_OCTET_STRING => "OCTET STRING", |
||
146 | self::TYPE_NULL => "NULL", |
||
147 | self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER", |
||
148 | self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor", |
||
149 | self::TYPE_EXTERNAL => "EXTERNAL", |
||
150 | self::TYPE_REAL => "REAL", |
||
151 | self::TYPE_ENUMERATED => "ENUMERATED", |
||
152 | self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV", |
||
153 | self::TYPE_UTF8_STRING => "UTF8String", |
||
154 | self::TYPE_RELATIVE_OID => "RELATIVE-OID", |
||
155 | self::TYPE_SEQUENCE => "SEQUENCE", |
||
156 | self::TYPE_SET => "SET", |
||
157 | self::TYPE_NUMERIC_STRING => "NumericString", |
||
158 | self::TYPE_PRINTABLE_STRING => "PrintableString", |
||
159 | self::TYPE_T61_STRING => "T61String", |
||
160 | self::TYPE_VIDEOTEX_STRING => "VideotexString", |
||
161 | self::TYPE_IA5_STRING => "IA5String", |
||
162 | self::TYPE_UTC_TIME => "UTCTime", |
||
163 | self::TYPE_GENERALIZED_TIME => "GeneralizedTime", |
||
164 | self::TYPE_GRAPHIC_STRING => "GraphicString", |
||
165 | self::TYPE_VISIBLE_STRING => "VisibleString", |
||
166 | self::TYPE_GENERAL_STRING => "GeneralString", |
||
167 | self::TYPE_UNIVERSAL_STRING => "UniversalString", |
||
168 | self::TYPE_CHARACTER_STRING => "CHARACTER STRING", |
||
169 | self::TYPE_BMP_STRING => "BMPString", |
||
170 | self::TYPE_STRING => "Any String", |
||
171 | self::TYPE_TIME => "Any Time" |
||
172 | /* @formatter:on */ |
||
173 | ); |
||
174 | |||
175 | /** |
||
176 | * Element's type tag. |
||
177 | * |
||
178 | * @var int |
||
179 | */ |
||
180 | protected $_typeTag; |
||
181 | |||
182 | /** |
||
183 | * |
||
184 | * @see \ASN1\Feature\ElementBase::typeClass() |
||
185 | */ |
||
186 | abstract public function typeClass(); |
||
187 | |||
188 | /** |
||
189 | * |
||
190 | * @see \ASN1\Feature\ElementBase::isConstructed() |
||
191 | */ |
||
192 | abstract public function isConstructed(); |
||
193 | |||
194 | /** |
||
195 | * Get content encoded in DER. |
||
196 | * |
||
197 | * Returns the DER encoded content without identifier and length header |
||
198 | * octets. |
||
199 | * |
||
200 | * @return string |
||
201 | */ |
||
202 | abstract protected function _encodedContentDER(); |
||
203 | |||
204 | /** |
||
205 | * Decode type-specific element from DER. |
||
206 | * |
||
207 | * @param Identifier $identifier Pre-parsed identifier |
||
208 | * @param string $data DER data |
||
209 | * @param int $offset Offset in data to the next byte after identifier |
||
210 | * @throws DecodeException If decoding fails |
||
211 | * @return Element |
||
212 | */ |
||
213 | 1 | protected static function _decodeFromDER(Identifier $identifier, $data, |
|
218 | |||
219 | /** |
||
220 | * |
||
221 | * @see \ASN1\Feature\ElementBase::tag() |
||
222 | */ |
||
223 | 63 | public function tag() { |
|
226 | |||
227 | /** |
||
228 | * Decode element from DER data. |
||
229 | * |
||
230 | * @param string $data DER encoded data |
||
231 | * @param int|null $offset Reference to the variable that contains offset |
||
232 | * into the data where to start parsing. Variable is updated to |
||
233 | * the offset next to the parsed element. If null, start from offset |
||
234 | * 0. |
||
235 | * @throws DecodeException If decoding fails |
||
236 | * @throws \UnexpectedValueException If called in the context of an expected |
||
237 | * type, but decoding yields another type |
||
238 | * @return self |
||
239 | */ |
||
240 | 194 | public static function fromDER($data, &$offset = null) { |
|
271 | |||
272 | /** |
||
273 | * Determine the class that implements the type. |
||
274 | * |
||
275 | * @param Identifier $identifier |
||
276 | * @return string Class name |
||
277 | */ |
||
278 | 193 | protected static function _determineImplClass(Identifier $identifier) { |
|
292 | |||
293 | /** |
||
294 | * Determine the class that implements an universal type of the given tag. |
||
295 | * |
||
296 | * @param int $tag |
||
297 | * @throws \UnexpectedValueException |
||
298 | * @return string Class name |
||
299 | */ |
||
300 | 192 | protected static function _determineUniversalImplClass($tag) { |
|
307 | |||
308 | /** |
||
309 | * |
||
310 | * @see \ASN1\Feature\Encodable::toDER() |
||
311 | */ |
||
312 | 109 | public function toDER() { |
|
320 | |||
321 | /** |
||
322 | * Check whether the element is a concrete type of a given tag. |
||
323 | * |
||
324 | * @param int $tag |
||
325 | * @return bool |
||
326 | */ |
||
327 | 13 | private function _isConcreteType($tag) { |
|
341 | |||
342 | /** |
||
343 | * |
||
344 | * @see \ASN1\Feature\ElementBase::isType() |
||
345 | */ |
||
346 | 19 | public function isType($tag) { |
|
361 | |||
362 | /** |
||
363 | * |
||
364 | * @see \ASN1\Feature\ElementBase::isTagged() |
||
365 | */ |
||
366 | 13 | public function isTagged() { |
|
369 | |||
370 | /** |
||
371 | * |
||
372 | * @see \ASN1\Feature\ElementBase::expectType() |
||
373 | */ |
||
374 | 14 | public function expectType($tag) { |
|
382 | |||
383 | /** |
||
384 | * |
||
385 | * @see \ASN1\Feature\ElementBase::expectTagged() |
||
386 | */ |
||
387 | 8 | public function expectTagged($tag = null) { |
|
399 | |||
400 | /** |
||
401 | * Get textual description of the type for debugging purposes. |
||
402 | * |
||
403 | * @return string |
||
404 | */ |
||
405 | 6 | protected function _typeDescriptorString() { |
|
412 | |||
413 | /** |
||
414 | * Get human readable name for an universal tag. |
||
415 | * |
||
416 | * @param int $tag |
||
417 | * @return string |
||
418 | */ |
||
419 | 16 | public static function tagToName($tag) { |
|
425 | } |
||
426 |