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