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