@@ -12,54 +12,54 @@ |
||
12 | 12 | */ |
13 | 13 | class Set extends Structure |
14 | 14 | { |
15 | - /** |
|
16 | - * Constructor. |
|
17 | - * |
|
18 | - * @param Element ...$elements Any number of elements |
|
19 | - */ |
|
20 | - public function __construct(Element ...$elements) |
|
21 | - { |
|
22 | - $this->_typeTag = self::TYPE_SET; |
|
23 | - parent::__construct(...$elements); |
|
24 | - } |
|
15 | + /** |
|
16 | + * Constructor. |
|
17 | + * |
|
18 | + * @param Element ...$elements Any number of elements |
|
19 | + */ |
|
20 | + public function __construct(Element ...$elements) |
|
21 | + { |
|
22 | + $this->_typeTag = self::TYPE_SET; |
|
23 | + parent::__construct(...$elements); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Sort by canonical ascending order. |
|
28 | - * Used for DER encoding of SET type. |
|
29 | - * |
|
30 | - * @return self |
|
31 | - */ |
|
32 | - public function sortedSet(): self |
|
33 | - { |
|
34 | - $obj = clone $this; |
|
35 | - usort($obj->_elements, |
|
36 | - function (Element $a, Element $b) { |
|
37 | - if ($a->typeClass() != $b->typeClass()) { |
|
38 | - return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
39 | - } |
|
40 | - if ($a->tag() == $b->tag()) { |
|
41 | - return 0; |
|
42 | - } |
|
43 | - return $a->tag() < $b->tag() ? -1 : 1; |
|
44 | - }); |
|
45 | - return $obj; |
|
46 | - } |
|
26 | + /** |
|
27 | + * Sort by canonical ascending order. |
|
28 | + * Used for DER encoding of SET type. |
|
29 | + * |
|
30 | + * @return self |
|
31 | + */ |
|
32 | + public function sortedSet(): self |
|
33 | + { |
|
34 | + $obj = clone $this; |
|
35 | + usort($obj->_elements, |
|
36 | + function (Element $a, Element $b) { |
|
37 | + if ($a->typeClass() != $b->typeClass()) { |
|
38 | + return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
39 | + } |
|
40 | + if ($a->tag() == $b->tag()) { |
|
41 | + return 0; |
|
42 | + } |
|
43 | + return $a->tag() < $b->tag() ? -1 : 1; |
|
44 | + }); |
|
45 | + return $obj; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Sort by encoding ascending order. |
|
50 | - * Used for DER encoding of SET OF type. |
|
51 | - * |
|
52 | - * @return self |
|
53 | - */ |
|
54 | - public function sortedSetOf(): self |
|
55 | - { |
|
56 | - $obj = clone $this; |
|
57 | - usort($obj->_elements, |
|
58 | - function (Element $a, Element $b) { |
|
59 | - $a_der = $a->toDER(); |
|
60 | - $b_der = $b->toDER(); |
|
61 | - return strcmp($a_der, $b_der); |
|
62 | - }); |
|
63 | - return $obj; |
|
64 | - } |
|
48 | + /** |
|
49 | + * Sort by encoding ascending order. |
|
50 | + * Used for DER encoding of SET OF type. |
|
51 | + * |
|
52 | + * @return self |
|
53 | + */ |
|
54 | + public function sortedSetOf(): self |
|
55 | + { |
|
56 | + $obj = clone $this; |
|
57 | + usort($obj->_elements, |
|
58 | + function (Element $a, Element $b) { |
|
59 | + $a_der = $a->toDER(); |
|
60 | + $b_der = $b->toDER(); |
|
61 | + return strcmp($a_der, $b_der); |
|
62 | + }); |
|
63 | + return $obj; |
|
64 | + } |
|
65 | 65 | } |
@@ -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\Type\Constructed; |
6 | 6 | |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | { |
34 | 34 | $obj = clone $this; |
35 | 35 | usort($obj->_elements, |
36 | - function (Element $a, Element $b) { |
|
36 | + function(Element $a, Element $b) { |
|
37 | 37 | if ($a->typeClass() != $b->typeClass()) { |
38 | 38 | return $a->typeClass() < $b->typeClass() ? -1 : 1; |
39 | 39 | } |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | { |
56 | 56 | $obj = clone $this; |
57 | 57 | usort($obj->_elements, |
58 | - function (Element $a, Element $b) { |
|
58 | + function(Element $a, Element $b) { |
|
59 | 59 | $a_der = $a->toDER(); |
60 | 60 | $b_der = $b->toDER(); |
61 | 61 | return strcmp($a_der, $b_der); |
@@ -332,10 +332,10 @@ discard block |
||
332 | 332 | private function _isPseudoType(int $tag): bool |
333 | 333 | { |
334 | 334 | switch ($tag) { |
335 | - case self::TYPE_STRING: |
|
336 | - return $this instanceof StringType; |
|
337 | - case self::TYPE_TIME: |
|
338 | - return $this instanceof TimeType; |
|
335 | + case self::TYPE_STRING: |
|
336 | + return $this instanceof StringType; |
|
337 | + case self::TYPE_TIME: |
|
338 | + return $this instanceof TimeType; |
|
339 | 339 | } |
340 | 340 | return false; |
341 | 341 | } |
@@ -398,14 +398,14 @@ discard block |
||
398 | 398 | protected static function _determineImplClass(Identifier $identifier): string |
399 | 399 | { |
400 | 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; |
|
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 | 409 | } |
410 | 410 | throw new \UnexpectedValueException( |
411 | 411 | sprintf("%s %d not implemented.", |
@@ -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", |
@@ -21,438 +21,438 @@ |
||
21 | 21 | */ |
22 | 22 | abstract class Element implements ElementBase |
23 | 23 | { |
24 | - // Universal type tags |
|
25 | - const TYPE_EOC = 0x00; |
|
26 | - const TYPE_BOOLEAN = 0x01; |
|
27 | - const TYPE_INTEGER = 0x02; |
|
28 | - const TYPE_BIT_STRING = 0x03; |
|
29 | - const TYPE_OCTET_STRING = 0x04; |
|
30 | - const TYPE_NULL = 0x05; |
|
31 | - const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
32 | - const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
33 | - const TYPE_EXTERNAL = 0x08; |
|
34 | - const TYPE_REAL = 0x09; |
|
35 | - const TYPE_ENUMERATED = 0x0a; |
|
36 | - const TYPE_EMBEDDED_PDV = 0x0b; |
|
37 | - const TYPE_UTF8_STRING = 0x0c; |
|
38 | - const TYPE_RELATIVE_OID = 0x0d; |
|
39 | - const TYPE_SEQUENCE = 0x10; |
|
40 | - const TYPE_SET = 0x11; |
|
41 | - const TYPE_NUMERIC_STRING = 0x12; |
|
42 | - const TYPE_PRINTABLE_STRING = 0x13; |
|
43 | - const TYPE_T61_STRING = 0x14; |
|
44 | - const TYPE_VIDEOTEX_STRING = 0x15; |
|
45 | - const TYPE_IA5_STRING = 0x16; |
|
46 | - const TYPE_UTC_TIME = 0x17; |
|
47 | - const TYPE_GENERALIZED_TIME = 0x18; |
|
48 | - const TYPE_GRAPHIC_STRING = 0x19; |
|
49 | - const TYPE_VISIBLE_STRING = 0x1a; |
|
50 | - const TYPE_GENERAL_STRING = 0x1b; |
|
51 | - const TYPE_UNIVERSAL_STRING = 0x1c; |
|
52 | - const TYPE_CHARACTER_STRING = 0x1d; |
|
53 | - const TYPE_BMP_STRING = 0x1e; |
|
24 | + // Universal type tags |
|
25 | + const TYPE_EOC = 0x00; |
|
26 | + const TYPE_BOOLEAN = 0x01; |
|
27 | + const TYPE_INTEGER = 0x02; |
|
28 | + const TYPE_BIT_STRING = 0x03; |
|
29 | + const TYPE_OCTET_STRING = 0x04; |
|
30 | + const TYPE_NULL = 0x05; |
|
31 | + const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
32 | + const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
33 | + const TYPE_EXTERNAL = 0x08; |
|
34 | + const TYPE_REAL = 0x09; |
|
35 | + const TYPE_ENUMERATED = 0x0a; |
|
36 | + const TYPE_EMBEDDED_PDV = 0x0b; |
|
37 | + const TYPE_UTF8_STRING = 0x0c; |
|
38 | + const TYPE_RELATIVE_OID = 0x0d; |
|
39 | + const TYPE_SEQUENCE = 0x10; |
|
40 | + const TYPE_SET = 0x11; |
|
41 | + const TYPE_NUMERIC_STRING = 0x12; |
|
42 | + const TYPE_PRINTABLE_STRING = 0x13; |
|
43 | + const TYPE_T61_STRING = 0x14; |
|
44 | + const TYPE_VIDEOTEX_STRING = 0x15; |
|
45 | + const TYPE_IA5_STRING = 0x16; |
|
46 | + const TYPE_UTC_TIME = 0x17; |
|
47 | + const TYPE_GENERALIZED_TIME = 0x18; |
|
48 | + const TYPE_GRAPHIC_STRING = 0x19; |
|
49 | + const TYPE_VISIBLE_STRING = 0x1a; |
|
50 | + const TYPE_GENERAL_STRING = 0x1b; |
|
51 | + const TYPE_UNIVERSAL_STRING = 0x1c; |
|
52 | + const TYPE_CHARACTER_STRING = 0x1d; |
|
53 | + const TYPE_BMP_STRING = 0x1e; |
|
54 | 54 | |
55 | - /** |
|
56 | - * Mapping from universal type tag to implementation class name. |
|
57 | - * |
|
58 | - * @internal |
|
59 | - * |
|
60 | - * @var array |
|
61 | - */ |
|
62 | - const MAP_TAG_TO_CLASS = [ /* @formatter:off */ |
|
63 | - self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
64 | - self::TYPE_INTEGER => Primitive\Integer::class, |
|
65 | - self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
66 | - self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
67 | - self::TYPE_NULL => Primitive\NullType::class, |
|
68 | - self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
69 | - self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
70 | - self::TYPE_REAL => Primitive\Real::class, |
|
71 | - self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
72 | - self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
73 | - self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
74 | - self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
75 | - self::TYPE_SET => Constructed\Set::class, |
|
76 | - self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
77 | - self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
78 | - self::TYPE_T61_STRING => Primitive\T61String::class, |
|
79 | - self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
80 | - self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
81 | - self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
82 | - self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
83 | - self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
84 | - self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
85 | - self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
86 | - self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
87 | - self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
88 | - self::TYPE_BMP_STRING => Primitive\BMPString::class |
|
89 | - /* @formatter:on */ |
|
90 | - ]; |
|
55 | + /** |
|
56 | + * Mapping from universal type tag to implementation class name. |
|
57 | + * |
|
58 | + * @internal |
|
59 | + * |
|
60 | + * @var array |
|
61 | + */ |
|
62 | + const MAP_TAG_TO_CLASS = [ /* @formatter:off */ |
|
63 | + self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
64 | + self::TYPE_INTEGER => Primitive\Integer::class, |
|
65 | + self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
66 | + self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
67 | + self::TYPE_NULL => Primitive\NullType::class, |
|
68 | + self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
69 | + self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
70 | + self::TYPE_REAL => Primitive\Real::class, |
|
71 | + self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
72 | + self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
73 | + self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
74 | + self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
75 | + self::TYPE_SET => Constructed\Set::class, |
|
76 | + self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
77 | + self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
78 | + self::TYPE_T61_STRING => Primitive\T61String::class, |
|
79 | + self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
80 | + self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
81 | + self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
82 | + self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
83 | + self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
84 | + self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
85 | + self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
86 | + self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
87 | + self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
88 | + self::TYPE_BMP_STRING => Primitive\BMPString::class |
|
89 | + /* @formatter:on */ |
|
90 | + ]; |
|
91 | 91 | |
92 | - /** |
|
93 | - * Pseudotype for all string types. |
|
94 | - * |
|
95 | - * May be used as an expectation parameter. |
|
96 | - * |
|
97 | - * @var int |
|
98 | - */ |
|
99 | - const TYPE_STRING = -1; |
|
92 | + /** |
|
93 | + * Pseudotype for all string types. |
|
94 | + * |
|
95 | + * May be used as an expectation parameter. |
|
96 | + * |
|
97 | + * @var int |
|
98 | + */ |
|
99 | + const TYPE_STRING = -1; |
|
100 | 100 | |
101 | - /** |
|
102 | - * Pseudotype for all time types. |
|
103 | - * |
|
104 | - * May be used as an expectation parameter. |
|
105 | - * |
|
106 | - * @var int |
|
107 | - */ |
|
108 | - const TYPE_TIME = -2; |
|
101 | + /** |
|
102 | + * Pseudotype for all time types. |
|
103 | + * |
|
104 | + * May be used as an expectation parameter. |
|
105 | + * |
|
106 | + * @var int |
|
107 | + */ |
|
108 | + const TYPE_TIME = -2; |
|
109 | 109 | |
110 | - /** |
|
111 | - * Mapping from universal type tag to human readable name. |
|
112 | - * |
|
113 | - * @internal |
|
114 | - * |
|
115 | - * @var array |
|
116 | - */ |
|
117 | - const MAP_TYPE_TO_NAME = [ /* @formatter:off */ |
|
118 | - self::TYPE_EOC => "EOC", |
|
119 | - self::TYPE_BOOLEAN => "BOOLEAN", |
|
120 | - self::TYPE_INTEGER => "INTEGER", |
|
121 | - self::TYPE_BIT_STRING => "BIT STRING", |
|
122 | - self::TYPE_OCTET_STRING => "OCTET STRING", |
|
123 | - self::TYPE_NULL => "NULL", |
|
124 | - self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER", |
|
125 | - self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor", |
|
126 | - self::TYPE_EXTERNAL => "EXTERNAL", |
|
127 | - self::TYPE_REAL => "REAL", |
|
128 | - self::TYPE_ENUMERATED => "ENUMERATED", |
|
129 | - self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV", |
|
130 | - self::TYPE_UTF8_STRING => "UTF8String", |
|
131 | - self::TYPE_RELATIVE_OID => "RELATIVE-OID", |
|
132 | - self::TYPE_SEQUENCE => "SEQUENCE", |
|
133 | - self::TYPE_SET => "SET", |
|
134 | - self::TYPE_NUMERIC_STRING => "NumericString", |
|
135 | - self::TYPE_PRINTABLE_STRING => "PrintableString", |
|
136 | - self::TYPE_T61_STRING => "T61String", |
|
137 | - self::TYPE_VIDEOTEX_STRING => "VideotexString", |
|
138 | - self::TYPE_IA5_STRING => "IA5String", |
|
139 | - self::TYPE_UTC_TIME => "UTCTime", |
|
140 | - self::TYPE_GENERALIZED_TIME => "GeneralizedTime", |
|
141 | - self::TYPE_GRAPHIC_STRING => "GraphicString", |
|
142 | - self::TYPE_VISIBLE_STRING => "VisibleString", |
|
143 | - self::TYPE_GENERAL_STRING => "GeneralString", |
|
144 | - self::TYPE_UNIVERSAL_STRING => "UniversalString", |
|
145 | - self::TYPE_CHARACTER_STRING => "CHARACTER STRING", |
|
146 | - self::TYPE_BMP_STRING => "BMPString", |
|
147 | - self::TYPE_STRING => "Any String", |
|
148 | - self::TYPE_TIME => "Any Time" |
|
149 | - /* @formatter:on */ |
|
150 | - ]; |
|
110 | + /** |
|
111 | + * Mapping from universal type tag to human readable name. |
|
112 | + * |
|
113 | + * @internal |
|
114 | + * |
|
115 | + * @var array |
|
116 | + */ |
|
117 | + const MAP_TYPE_TO_NAME = [ /* @formatter:off */ |
|
118 | + self::TYPE_EOC => "EOC", |
|
119 | + self::TYPE_BOOLEAN => "BOOLEAN", |
|
120 | + self::TYPE_INTEGER => "INTEGER", |
|
121 | + self::TYPE_BIT_STRING => "BIT STRING", |
|
122 | + self::TYPE_OCTET_STRING => "OCTET STRING", |
|
123 | + self::TYPE_NULL => "NULL", |
|
124 | + self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER", |
|
125 | + self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor", |
|
126 | + self::TYPE_EXTERNAL => "EXTERNAL", |
|
127 | + self::TYPE_REAL => "REAL", |
|
128 | + self::TYPE_ENUMERATED => "ENUMERATED", |
|
129 | + self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV", |
|
130 | + self::TYPE_UTF8_STRING => "UTF8String", |
|
131 | + self::TYPE_RELATIVE_OID => "RELATIVE-OID", |
|
132 | + self::TYPE_SEQUENCE => "SEQUENCE", |
|
133 | + self::TYPE_SET => "SET", |
|
134 | + self::TYPE_NUMERIC_STRING => "NumericString", |
|
135 | + self::TYPE_PRINTABLE_STRING => "PrintableString", |
|
136 | + self::TYPE_T61_STRING => "T61String", |
|
137 | + self::TYPE_VIDEOTEX_STRING => "VideotexString", |
|
138 | + self::TYPE_IA5_STRING => "IA5String", |
|
139 | + self::TYPE_UTC_TIME => "UTCTime", |
|
140 | + self::TYPE_GENERALIZED_TIME => "GeneralizedTime", |
|
141 | + self::TYPE_GRAPHIC_STRING => "GraphicString", |
|
142 | + self::TYPE_VISIBLE_STRING => "VisibleString", |
|
143 | + self::TYPE_GENERAL_STRING => "GeneralString", |
|
144 | + self::TYPE_UNIVERSAL_STRING => "UniversalString", |
|
145 | + self::TYPE_CHARACTER_STRING => "CHARACTER STRING", |
|
146 | + self::TYPE_BMP_STRING => "BMPString", |
|
147 | + self::TYPE_STRING => "Any String", |
|
148 | + self::TYPE_TIME => "Any Time" |
|
149 | + /* @formatter:on */ |
|
150 | + ]; |
|
151 | 151 | |
152 | - /** |
|
153 | - * Element's type tag. |
|
154 | - * |
|
155 | - * @var int |
|
156 | - */ |
|
157 | - protected $_typeTag; |
|
152 | + /** |
|
153 | + * Element's type tag. |
|
154 | + * |
|
155 | + * @var int |
|
156 | + */ |
|
157 | + protected $_typeTag; |
|
158 | 158 | |
159 | - /** |
|
160 | - * |
|
161 | - * @see \ASN1\Feature\ElementBase::typeClass() |
|
162 | - * @return int |
|
163 | - */ |
|
164 | - abstract public function typeClass(): int; |
|
159 | + /** |
|
160 | + * |
|
161 | + * @see \ASN1\Feature\ElementBase::typeClass() |
|
162 | + * @return int |
|
163 | + */ |
|
164 | + abstract public function typeClass(): int; |
|
165 | 165 | |
166 | - /** |
|
167 | - * |
|
168 | - * @see \ASN1\Feature\ElementBase::isConstructed() |
|
169 | - * @return bool |
|
170 | - */ |
|
171 | - abstract public function isConstructed(): bool; |
|
166 | + /** |
|
167 | + * |
|
168 | + * @see \ASN1\Feature\ElementBase::isConstructed() |
|
169 | + * @return bool |
|
170 | + */ |
|
171 | + abstract public function isConstructed(): bool; |
|
172 | 172 | |
173 | - /** |
|
174 | - * Get the content encoded in DER. |
|
175 | - * |
|
176 | - * Returns the DER encoded content without identifier and length header |
|
177 | - * octets. |
|
178 | - * |
|
179 | - * @return string |
|
180 | - */ |
|
181 | - abstract protected function _encodedContentDER(): string; |
|
173 | + /** |
|
174 | + * Get the content encoded in DER. |
|
175 | + * |
|
176 | + * Returns the DER encoded content without identifier and length header |
|
177 | + * octets. |
|
178 | + * |
|
179 | + * @return string |
|
180 | + */ |
|
181 | + abstract protected function _encodedContentDER(): string; |
|
182 | 182 | |
183 | - /** |
|
184 | - * Decode type-specific element from DER. |
|
185 | - * |
|
186 | - * @param Identifier $identifier Pre-parsed identifier |
|
187 | - * @param string $data DER data |
|
188 | - * @param int $offset Offset in data to the next byte after identifier |
|
189 | - * @throws DecodeException If decoding fails |
|
190 | - * @return ElementBase |
|
191 | - */ |
|
192 | - protected static function _decodeFromDER(Identifier $identifier, |
|
193 | - string $data, int &$offset): ElementBase |
|
194 | - { |
|
195 | - throw new \BadMethodCallException( |
|
196 | - __METHOD__ . " must be implemented in derived class."); |
|
197 | - } |
|
183 | + /** |
|
184 | + * Decode type-specific element from DER. |
|
185 | + * |
|
186 | + * @param Identifier $identifier Pre-parsed identifier |
|
187 | + * @param string $data DER data |
|
188 | + * @param int $offset Offset in data to the next byte after identifier |
|
189 | + * @throws DecodeException If decoding fails |
|
190 | + * @return ElementBase |
|
191 | + */ |
|
192 | + protected static function _decodeFromDER(Identifier $identifier, |
|
193 | + string $data, int &$offset): ElementBase |
|
194 | + { |
|
195 | + throw new \BadMethodCallException( |
|
196 | + __METHOD__ . " must be implemented in derived class."); |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * Decode element from DER data. |
|
201 | - * |
|
202 | - * @param string $data DER encoded data |
|
203 | - * @param int|null $offset Reference to the variable that contains offset |
|
204 | - * into the data where to start parsing. Variable is updated to |
|
205 | - * the offset next to the parsed element. If null, start from offset |
|
206 | - * 0. |
|
207 | - * @throws DecodeException If decoding fails |
|
208 | - * @throws \UnexpectedValueException If called in the context of an expected |
|
209 | - * type, but decoding yields another type |
|
210 | - * @return ElementBase |
|
211 | - */ |
|
212 | - public static function fromDER(string $data, int &$offset = null): ElementBase |
|
213 | - { |
|
214 | - // decode identifier |
|
215 | - $idx = $offset ?? 0; |
|
216 | - $identifier = Identifier::fromDER($data, $idx); |
|
217 | - // determine class that implements type specific decoding |
|
218 | - $cls = self::_determineImplClass($identifier); |
|
219 | - try { |
|
220 | - // decode remaining element |
|
221 | - $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
222 | - } catch (\LogicException $e) { |
|
223 | - // rethrow as a RuntimeException for unified exception handling |
|
224 | - throw new DecodeException( |
|
225 | - sprintf("Error while decoding %s.", |
|
226 | - self::tagToName($identifier->intTag())), 0, $e); |
|
227 | - } |
|
228 | - // if called in the context of a concrete class, check |
|
229 | - // that decoded type matches the type of a calling class |
|
230 | - $called_class = get_called_class(); |
|
231 | - if (self::class != $called_class) { |
|
232 | - if (!$element instanceof $called_class) { |
|
233 | - throw new \UnexpectedValueException( |
|
234 | - sprintf("%s expected, got %s.", $called_class, |
|
235 | - get_class($element))); |
|
236 | - } |
|
237 | - } |
|
238 | - // update offset for the caller |
|
239 | - if (isset($offset)) { |
|
240 | - $offset = $idx; |
|
241 | - } |
|
242 | - return $element; |
|
243 | - } |
|
199 | + /** |
|
200 | + * Decode element from DER data. |
|
201 | + * |
|
202 | + * @param string $data DER encoded data |
|
203 | + * @param int|null $offset Reference to the variable that contains offset |
|
204 | + * into the data where to start parsing. Variable is updated to |
|
205 | + * the offset next to the parsed element. If null, start from offset |
|
206 | + * 0. |
|
207 | + * @throws DecodeException If decoding fails |
|
208 | + * @throws \UnexpectedValueException If called in the context of an expected |
|
209 | + * type, but decoding yields another type |
|
210 | + * @return ElementBase |
|
211 | + */ |
|
212 | + public static function fromDER(string $data, int &$offset = null): ElementBase |
|
213 | + { |
|
214 | + // decode identifier |
|
215 | + $idx = $offset ?? 0; |
|
216 | + $identifier = Identifier::fromDER($data, $idx); |
|
217 | + // determine class that implements type specific decoding |
|
218 | + $cls = self::_determineImplClass($identifier); |
|
219 | + try { |
|
220 | + // decode remaining element |
|
221 | + $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
222 | + } catch (\LogicException $e) { |
|
223 | + // rethrow as a RuntimeException for unified exception handling |
|
224 | + throw new DecodeException( |
|
225 | + sprintf("Error while decoding %s.", |
|
226 | + self::tagToName($identifier->intTag())), 0, $e); |
|
227 | + } |
|
228 | + // if called in the context of a concrete class, check |
|
229 | + // that decoded type matches the type of a calling class |
|
230 | + $called_class = get_called_class(); |
|
231 | + if (self::class != $called_class) { |
|
232 | + if (!$element instanceof $called_class) { |
|
233 | + throw new \UnexpectedValueException( |
|
234 | + sprintf("%s expected, got %s.", $called_class, |
|
235 | + get_class($element))); |
|
236 | + } |
|
237 | + } |
|
238 | + // update offset for the caller |
|
239 | + if (isset($offset)) { |
|
240 | + $offset = $idx; |
|
241 | + } |
|
242 | + return $element; |
|
243 | + } |
|
244 | 244 | |
245 | - /** |
|
246 | - * |
|
247 | - * @see \ASN1\Feature\Encodable::toDER() |
|
248 | - * @return string |
|
249 | - */ |
|
250 | - public function toDER(): string |
|
251 | - { |
|
252 | - $identifier = new Identifier($this->typeClass(), |
|
253 | - $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
254 | - $this->_typeTag); |
|
255 | - $content = $this->_encodedContentDER(); |
|
256 | - $length = new Length(strlen($content)); |
|
257 | - return $identifier->toDER() . $length->toDER() . $content; |
|
258 | - } |
|
245 | + /** |
|
246 | + * |
|
247 | + * @see \ASN1\Feature\Encodable::toDER() |
|
248 | + * @return string |
|
249 | + */ |
|
250 | + public function toDER(): string |
|
251 | + { |
|
252 | + $identifier = new Identifier($this->typeClass(), |
|
253 | + $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
254 | + $this->_typeTag); |
|
255 | + $content = $this->_encodedContentDER(); |
|
256 | + $length = new Length(strlen($content)); |
|
257 | + return $identifier->toDER() . $length->toDER() . $content; |
|
258 | + } |
|
259 | 259 | |
260 | - /** |
|
261 | - * |
|
262 | - * @see \ASN1\Feature\ElementBase::tag() |
|
263 | - * @return int |
|
264 | - */ |
|
265 | - public function tag(): int |
|
266 | - { |
|
267 | - return $this->_typeTag; |
|
268 | - } |
|
260 | + /** |
|
261 | + * |
|
262 | + * @see \ASN1\Feature\ElementBase::tag() |
|
263 | + * @return int |
|
264 | + */ |
|
265 | + public function tag(): int |
|
266 | + { |
|
267 | + return $this->_typeTag; |
|
268 | + } |
|
269 | 269 | |
270 | - /** |
|
271 | - * |
|
272 | - * @see \ASN1\Feature\ElementBase::isType() |
|
273 | - * @return bool |
|
274 | - */ |
|
275 | - public function isType(int $tag): bool |
|
276 | - { |
|
277 | - // if element is context specific |
|
278 | - if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) { |
|
279 | - return false; |
|
280 | - } |
|
281 | - // negative tags identify an abstract pseudotype |
|
282 | - if ($tag < 0) { |
|
283 | - return $this->_isPseudoType($tag); |
|
284 | - } |
|
285 | - return $this->_isConcreteType($tag); |
|
286 | - } |
|
270 | + /** |
|
271 | + * |
|
272 | + * @see \ASN1\Feature\ElementBase::isType() |
|
273 | + * @return bool |
|
274 | + */ |
|
275 | + public function isType(int $tag): bool |
|
276 | + { |
|
277 | + // if element is context specific |
|
278 | + if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) { |
|
279 | + return false; |
|
280 | + } |
|
281 | + // negative tags identify an abstract pseudotype |
|
282 | + if ($tag < 0) { |
|
283 | + return $this->_isPseudoType($tag); |
|
284 | + } |
|
285 | + return $this->_isConcreteType($tag); |
|
286 | + } |
|
287 | 287 | |
288 | - /** |
|
289 | - * |
|
290 | - * @see \ASN1\Feature\ElementBase::expectType() |
|
291 | - * @return ElementBase |
|
292 | - */ |
|
293 | - public function expectType(int $tag): ElementBase |
|
294 | - { |
|
295 | - if (!$this->isType($tag)) { |
|
296 | - throw new \UnexpectedValueException( |
|
297 | - sprintf("%s expected, got %s.", self::tagToName($tag), |
|
298 | - $this->_typeDescriptorString())); |
|
299 | - } |
|
300 | - return $this; |
|
301 | - } |
|
288 | + /** |
|
289 | + * |
|
290 | + * @see \ASN1\Feature\ElementBase::expectType() |
|
291 | + * @return ElementBase |
|
292 | + */ |
|
293 | + public function expectType(int $tag): ElementBase |
|
294 | + { |
|
295 | + if (!$this->isType($tag)) { |
|
296 | + throw new \UnexpectedValueException( |
|
297 | + sprintf("%s expected, got %s.", self::tagToName($tag), |
|
298 | + $this->_typeDescriptorString())); |
|
299 | + } |
|
300 | + return $this; |
|
301 | + } |
|
302 | 302 | |
303 | - /** |
|
304 | - * Check whether the element is a concrete type of a given tag. |
|
305 | - * |
|
306 | - * @param int $tag |
|
307 | - * @return bool |
|
308 | - */ |
|
309 | - private function _isConcreteType(int $tag): bool |
|
310 | - { |
|
311 | - // if tag doesn't match |
|
312 | - if ($this->tag() != $tag) { |
|
313 | - return false; |
|
314 | - } |
|
315 | - // if type is universal check that instance is of a correct class |
|
316 | - if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
317 | - $cls = self::_determineUniversalImplClass($tag); |
|
318 | - if (!$this instanceof $cls) { |
|
319 | - return false; |
|
320 | - } |
|
321 | - } |
|
322 | - return true; |
|
323 | - } |
|
303 | + /** |
|
304 | + * Check whether the element is a concrete type of a given tag. |
|
305 | + * |
|
306 | + * @param int $tag |
|
307 | + * @return bool |
|
308 | + */ |
|
309 | + private function _isConcreteType(int $tag): bool |
|
310 | + { |
|
311 | + // if tag doesn't match |
|
312 | + if ($this->tag() != $tag) { |
|
313 | + return false; |
|
314 | + } |
|
315 | + // if type is universal check that instance is of a correct class |
|
316 | + if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
317 | + $cls = self::_determineUniversalImplClass($tag); |
|
318 | + if (!$this instanceof $cls) { |
|
319 | + return false; |
|
320 | + } |
|
321 | + } |
|
322 | + return true; |
|
323 | + } |
|
324 | 324 | |
325 | - /** |
|
326 | - * Check whether the element is a pseudotype. |
|
327 | - * |
|
328 | - * @param int $tag |
|
329 | - * @return bool |
|
330 | - */ |
|
331 | - private function _isPseudoType(int $tag): bool |
|
332 | - { |
|
333 | - switch ($tag) { |
|
334 | - case self::TYPE_STRING: |
|
335 | - return $this instanceof StringType; |
|
336 | - case self::TYPE_TIME: |
|
337 | - return $this instanceof TimeType; |
|
338 | - } |
|
339 | - return false; |
|
340 | - } |
|
325 | + /** |
|
326 | + * Check whether the element is a pseudotype. |
|
327 | + * |
|
328 | + * @param int $tag |
|
329 | + * @return bool |
|
330 | + */ |
|
331 | + private function _isPseudoType(int $tag): bool |
|
332 | + { |
|
333 | + switch ($tag) { |
|
334 | + case self::TYPE_STRING: |
|
335 | + return $this instanceof StringType; |
|
336 | + case self::TYPE_TIME: |
|
337 | + return $this instanceof TimeType; |
|
338 | + } |
|
339 | + return false; |
|
340 | + } |
|
341 | 341 | |
342 | - /** |
|
343 | - * |
|
344 | - * @see \ASN1\Feature\ElementBase::isTagged() |
|
345 | - * @return bool |
|
346 | - */ |
|
347 | - public function isTagged(): bool |
|
348 | - { |
|
349 | - return $this instanceof TaggedType; |
|
350 | - } |
|
342 | + /** |
|
343 | + * |
|
344 | + * @see \ASN1\Feature\ElementBase::isTagged() |
|
345 | + * @return bool |
|
346 | + */ |
|
347 | + public function isTagged(): bool |
|
348 | + { |
|
349 | + return $this instanceof TaggedType; |
|
350 | + } |
|
351 | 351 | |
352 | - /** |
|
353 | - * |
|
354 | - * @see \ASN1\Feature\ElementBase::expectTagged() |
|
355 | - * @return TaggedType |
|
356 | - */ |
|
357 | - public function expectTagged($tag = null): TaggedType |
|
358 | - { |
|
359 | - if (!$this->isTagged()) { |
|
360 | - throw new \UnexpectedValueException( |
|
361 | - sprintf("Context specific element expected, got %s.", |
|
362 | - Identifier::classToName($this->typeClass()))); |
|
363 | - } |
|
364 | - if (isset($tag) && $this->tag() != $tag) { |
|
365 | - throw new \UnexpectedValueException( |
|
366 | - sprintf("Tag %d expected, got %d.", $tag, $this->tag())); |
|
367 | - } |
|
368 | - return $this; |
|
369 | - } |
|
352 | + /** |
|
353 | + * |
|
354 | + * @see \ASN1\Feature\ElementBase::expectTagged() |
|
355 | + * @return TaggedType |
|
356 | + */ |
|
357 | + public function expectTagged($tag = null): TaggedType |
|
358 | + { |
|
359 | + if (!$this->isTagged()) { |
|
360 | + throw new \UnexpectedValueException( |
|
361 | + sprintf("Context specific element expected, got %s.", |
|
362 | + Identifier::classToName($this->typeClass()))); |
|
363 | + } |
|
364 | + if (isset($tag) && $this->tag() != $tag) { |
|
365 | + throw new \UnexpectedValueException( |
|
366 | + sprintf("Tag %d expected, got %d.", $tag, $this->tag())); |
|
367 | + } |
|
368 | + return $this; |
|
369 | + } |
|
370 | 370 | |
371 | - /** |
|
372 | - * |
|
373 | - * @see \ASN1\Feature\ElementBase::asElement() |
|
374 | - * @return Element |
|
375 | - */ |
|
376 | - final public function asElement(): Element |
|
377 | - { |
|
378 | - return $this; |
|
379 | - } |
|
371 | + /** |
|
372 | + * |
|
373 | + * @see \ASN1\Feature\ElementBase::asElement() |
|
374 | + * @return Element |
|
375 | + */ |
|
376 | + final public function asElement(): Element |
|
377 | + { |
|
378 | + return $this; |
|
379 | + } |
|
380 | 380 | |
381 | - /** |
|
382 | - * Get element decorated with UnspecifiedType object. |
|
383 | - * |
|
384 | - * @return UnspecifiedType |
|
385 | - */ |
|
386 | - public function asUnspecified(): UnspecifiedType |
|
387 | - { |
|
388 | - return new UnspecifiedType($this); |
|
389 | - } |
|
381 | + /** |
|
382 | + * Get element decorated with UnspecifiedType object. |
|
383 | + * |
|
384 | + * @return UnspecifiedType |
|
385 | + */ |
|
386 | + public function asUnspecified(): UnspecifiedType |
|
387 | + { |
|
388 | + return new UnspecifiedType($this); |
|
389 | + } |
|
390 | 390 | |
391 | - /** |
|
392 | - * Determine the class that implements the type. |
|
393 | - * |
|
394 | - * @param Identifier $identifier |
|
395 | - * @return string Class name |
|
396 | - */ |
|
397 | - protected static function _determineImplClass(Identifier $identifier): string |
|
398 | - { |
|
399 | - switch ($identifier->typeClass()) { |
|
400 | - case Identifier::CLASS_UNIVERSAL: |
|
401 | - return self::_determineUniversalImplClass($identifier->intTag()); |
|
402 | - case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
403 | - return TaggedType::class; |
|
404 | - case Identifier::CLASS_APPLICATION: |
|
405 | - return ApplicationType::class; |
|
406 | - case Identifier::CLASS_PRIVATE: |
|
407 | - return PrivateType::class; |
|
408 | - } |
|
409 | - throw new \UnexpectedValueException( |
|
410 | - sprintf("%s %d not implemented.", |
|
411 | - Identifier::classToName($identifier->typeClass()), |
|
412 | - $identifier->tag())); |
|
413 | - } |
|
391 | + /** |
|
392 | + * Determine the class that implements the type. |
|
393 | + * |
|
394 | + * @param Identifier $identifier |
|
395 | + * @return string Class name |
|
396 | + */ |
|
397 | + protected static function _determineImplClass(Identifier $identifier): string |
|
398 | + { |
|
399 | + switch ($identifier->typeClass()) { |
|
400 | + case Identifier::CLASS_UNIVERSAL: |
|
401 | + return self::_determineUniversalImplClass($identifier->intTag()); |
|
402 | + case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
403 | + return TaggedType::class; |
|
404 | + case Identifier::CLASS_APPLICATION: |
|
405 | + return ApplicationType::class; |
|
406 | + case Identifier::CLASS_PRIVATE: |
|
407 | + return PrivateType::class; |
|
408 | + } |
|
409 | + throw new \UnexpectedValueException( |
|
410 | + sprintf("%s %d not implemented.", |
|
411 | + Identifier::classToName($identifier->typeClass()), |
|
412 | + $identifier->tag())); |
|
413 | + } |
|
414 | 414 | |
415 | - /** |
|
416 | - * Determine the class that implements an universal type of the given tag. |
|
417 | - * |
|
418 | - * @param int $tag |
|
419 | - * @throws \UnexpectedValueException |
|
420 | - * @return string Class name |
|
421 | - */ |
|
422 | - protected static function _determineUniversalImplClass(int $tag): string |
|
423 | - { |
|
424 | - if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
425 | - throw new \UnexpectedValueException( |
|
426 | - "Universal tag $tag not implemented."); |
|
427 | - } |
|
428 | - return self::MAP_TAG_TO_CLASS[$tag]; |
|
429 | - } |
|
415 | + /** |
|
416 | + * Determine the class that implements an universal type of the given tag. |
|
417 | + * |
|
418 | + * @param int $tag |
|
419 | + * @throws \UnexpectedValueException |
|
420 | + * @return string Class name |
|
421 | + */ |
|
422 | + protected static function _determineUniversalImplClass(int $tag): string |
|
423 | + { |
|
424 | + if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
425 | + throw new \UnexpectedValueException( |
|
426 | + "Universal tag $tag not implemented."); |
|
427 | + } |
|
428 | + return self::MAP_TAG_TO_CLASS[$tag]; |
|
429 | + } |
|
430 | 430 | |
431 | - /** |
|
432 | - * Get textual description of the type for debugging purposes. |
|
433 | - * |
|
434 | - * @return string |
|
435 | - */ |
|
436 | - protected function _typeDescriptorString(): string |
|
437 | - { |
|
438 | - if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
439 | - return self::tagToName($this->_typeTag); |
|
440 | - } |
|
441 | - return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()), |
|
442 | - $this->_typeTag); |
|
443 | - } |
|
431 | + /** |
|
432 | + * Get textual description of the type for debugging purposes. |
|
433 | + * |
|
434 | + * @return string |
|
435 | + */ |
|
436 | + protected function _typeDescriptorString(): string |
|
437 | + { |
|
438 | + if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
439 | + return self::tagToName($this->_typeTag); |
|
440 | + } |
|
441 | + return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()), |
|
442 | + $this->_typeTag); |
|
443 | + } |
|
444 | 444 | |
445 | - /** |
|
446 | - * Get human readable name for an universal tag. |
|
447 | - * |
|
448 | - * @param int $tag |
|
449 | - * @return string |
|
450 | - */ |
|
451 | - public static function tagToName(int $tag): string |
|
452 | - { |
|
453 | - if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
454 | - return "TAG $tag"; |
|
455 | - } |
|
456 | - return self::MAP_TYPE_TO_NAME[$tag]; |
|
457 | - } |
|
445 | + /** |
|
446 | + * Get human readable name for an universal tag. |
|
447 | + * |
|
448 | + * @param int $tag |
|
449 | + * @return string |
|
450 | + */ |
|
451 | + public static function tagToName(int $tag): string |
|
452 | + { |
|
453 | + if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
454 | + return "TAG $tag"; |
|
455 | + } |
|
456 | + return self::MAP_TYPE_TO_NAME[$tag]; |
|
457 | + } |
|
458 | 458 | } |
@@ -13,206 +13,206 @@ |
||
13 | 13 | */ |
14 | 14 | class Length implements Encodable |
15 | 15 | { |
16 | - /** |
|
17 | - * Length. |
|
18 | - * |
|
19 | - * @var BigInt |
|
20 | - */ |
|
21 | - private $_length; |
|
16 | + /** |
|
17 | + * Length. |
|
18 | + * |
|
19 | + * @var BigInt |
|
20 | + */ |
|
21 | + private $_length; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Whether length is indefinite. |
|
25 | - * |
|
26 | - * @var bool |
|
27 | - */ |
|
28 | - private $_indefinite; |
|
23 | + /** |
|
24 | + * Whether length is indefinite. |
|
25 | + * |
|
26 | + * @var bool |
|
27 | + */ |
|
28 | + private $_indefinite; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Constructor. |
|
32 | - * |
|
33 | - * @param int|string $length Length |
|
34 | - * @param bool $indefinite Whether length is indefinite |
|
35 | - */ |
|
36 | - public function __construct($length, bool $indefinite = false) |
|
37 | - { |
|
38 | - $this->_length = new BigInt($length); |
|
39 | - $this->_indefinite = $indefinite; |
|
40 | - } |
|
30 | + /** |
|
31 | + * Constructor. |
|
32 | + * |
|
33 | + * @param int|string $length Length |
|
34 | + * @param bool $indefinite Whether length is indefinite |
|
35 | + */ |
|
36 | + public function __construct($length, bool $indefinite = false) |
|
37 | + { |
|
38 | + $this->_length = new BigInt($length); |
|
39 | + $this->_indefinite = $indefinite; |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Decode length component from DER data. |
|
44 | - * |
|
45 | - * @param string $data DER encoded data |
|
46 | - * @param int|null $offset Reference to the variable that contains offset |
|
47 | - * into the data where to start parsing. Variable is updated to |
|
48 | - * the offset next to the parsed length component. If null, start |
|
49 | - * from offset 0. |
|
50 | - * @throws DecodeException If decoding fails |
|
51 | - * @return self |
|
52 | - */ |
|
53 | - public static function fromDER(string $data, int &$offset = null): self |
|
54 | - { |
|
55 | - $idx = $offset ?? 0; |
|
56 | - $datalen = strlen($data); |
|
57 | - if ($idx >= $datalen) { |
|
58 | - throw new DecodeException("Invalid offset."); |
|
59 | - } |
|
60 | - $indefinite = false; |
|
61 | - $byte = ord($data[$idx++]); |
|
62 | - // bits 7 to 1 |
|
63 | - $length = (0x7f & $byte); |
|
64 | - // long form |
|
65 | - if (0x80 & $byte) { |
|
66 | - if (!$length) { |
|
67 | - $indefinite = true; |
|
68 | - } else { |
|
69 | - if ($idx + $length > $datalen) { |
|
70 | - throw new DecodeException("Too many length octets."); |
|
71 | - } |
|
72 | - $length = self::_decodeLongFormLength($length, $data, $idx); |
|
73 | - } |
|
74 | - } |
|
75 | - if (isset($offset)) { |
|
76 | - $offset = $idx; |
|
77 | - } |
|
78 | - return new self($length, $indefinite); |
|
79 | - } |
|
42 | + /** |
|
43 | + * Decode length component from DER data. |
|
44 | + * |
|
45 | + * @param string $data DER encoded data |
|
46 | + * @param int|null $offset Reference to the variable that contains offset |
|
47 | + * into the data where to start parsing. Variable is updated to |
|
48 | + * the offset next to the parsed length component. If null, start |
|
49 | + * from offset 0. |
|
50 | + * @throws DecodeException If decoding fails |
|
51 | + * @return self |
|
52 | + */ |
|
53 | + public static function fromDER(string $data, int &$offset = null): self |
|
54 | + { |
|
55 | + $idx = $offset ?? 0; |
|
56 | + $datalen = strlen($data); |
|
57 | + if ($idx >= $datalen) { |
|
58 | + throw new DecodeException("Invalid offset."); |
|
59 | + } |
|
60 | + $indefinite = false; |
|
61 | + $byte = ord($data[$idx++]); |
|
62 | + // bits 7 to 1 |
|
63 | + $length = (0x7f & $byte); |
|
64 | + // long form |
|
65 | + if (0x80 & $byte) { |
|
66 | + if (!$length) { |
|
67 | + $indefinite = true; |
|
68 | + } else { |
|
69 | + if ($idx + $length > $datalen) { |
|
70 | + throw new DecodeException("Too many length octets."); |
|
71 | + } |
|
72 | + $length = self::_decodeLongFormLength($length, $data, $idx); |
|
73 | + } |
|
74 | + } |
|
75 | + if (isset($offset)) { |
|
76 | + $offset = $idx; |
|
77 | + } |
|
78 | + return new self($length, $indefinite); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * Decode long form length. |
|
83 | - * |
|
84 | - * @param int $length Number of octets |
|
85 | - * @param string $data Data |
|
86 | - * @param int $offset Reference to the variable containing offset to the |
|
87 | - * data. |
|
88 | - * @throws DecodeException If decoding fails |
|
89 | - * @return string Integer as a string |
|
90 | - */ |
|
91 | - private static function _decodeLongFormLength(int $length, string $data, |
|
92 | - int &$offset): string |
|
93 | - { |
|
94 | - // first octet must not be 0xff (spec 8.1.3.5c) |
|
95 | - if ($length == 127) { |
|
96 | - throw new DecodeException("Invalid number of length octets."); |
|
97 | - } |
|
98 | - $num = gmp_init(0, 10); |
|
99 | - while (--$length >= 0) { |
|
100 | - $byte = ord($data[$offset++]); |
|
101 | - $num <<= 8; |
|
102 | - $num |= $byte; |
|
103 | - } |
|
104 | - return gmp_strval($num); |
|
105 | - } |
|
81 | + /** |
|
82 | + * Decode long form length. |
|
83 | + * |
|
84 | + * @param int $length Number of octets |
|
85 | + * @param string $data Data |
|
86 | + * @param int $offset Reference to the variable containing offset to the |
|
87 | + * data. |
|
88 | + * @throws DecodeException If decoding fails |
|
89 | + * @return string Integer as a string |
|
90 | + */ |
|
91 | + private static function _decodeLongFormLength(int $length, string $data, |
|
92 | + int &$offset): string |
|
93 | + { |
|
94 | + // first octet must not be 0xff (spec 8.1.3.5c) |
|
95 | + if ($length == 127) { |
|
96 | + throw new DecodeException("Invalid number of length octets."); |
|
97 | + } |
|
98 | + $num = gmp_init(0, 10); |
|
99 | + while (--$length >= 0) { |
|
100 | + $byte = ord($data[$offset++]); |
|
101 | + $num <<= 8; |
|
102 | + $num |= $byte; |
|
103 | + } |
|
104 | + return gmp_strval($num); |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * Decode length from DER. |
|
109 | - * |
|
110 | - * Throws an exception if length doesn't match with expected or if data |
|
111 | - * doesn't contain enough bytes. |
|
112 | - * |
|
113 | - * @see self::fromDER |
|
114 | - * @param string $data DER data |
|
115 | - * @param int $offset Reference to the offset variable |
|
116 | - * @param int|null $expected Expected length, null to bypass checking |
|
117 | - * @throws DecodeException If decoding or expectation fails |
|
118 | - * @return self |
|
119 | - */ |
|
120 | - public static function expectFromDER(string $data, int &$offset, |
|
121 | - int $expected = null): self |
|
122 | - { |
|
123 | - $idx = $offset; |
|
124 | - $length = self::fromDER($data, $idx); |
|
125 | - // DER encoding must have definite length (spec 10.1) |
|
126 | - if ($length->isIndefinite()) { |
|
127 | - throw new DecodeException("DER encoding must have definite length."); |
|
128 | - } |
|
129 | - // if certain length was expected |
|
130 | - if (isset($expected) && $expected != $length->intLength()) { |
|
131 | - throw new DecodeException( |
|
132 | - sprintf("Expected length %d, got %d.", $expected, |
|
133 | - $length->intLength())); |
|
134 | - } |
|
135 | - // check that enough data is available |
|
136 | - if (strlen($data) < $idx + $length->intLength()) { |
|
137 | - throw new DecodeException( |
|
138 | - sprintf("Length %d overflows data, %d bytes left.", |
|
139 | - $length->intLength(), strlen($data) - $idx)); |
|
140 | - } |
|
141 | - $offset = $idx; |
|
142 | - return $length; |
|
143 | - } |
|
107 | + /** |
|
108 | + * Decode length from DER. |
|
109 | + * |
|
110 | + * Throws an exception if length doesn't match with expected or if data |
|
111 | + * doesn't contain enough bytes. |
|
112 | + * |
|
113 | + * @see self::fromDER |
|
114 | + * @param string $data DER data |
|
115 | + * @param int $offset Reference to the offset variable |
|
116 | + * @param int|null $expected Expected length, null to bypass checking |
|
117 | + * @throws DecodeException If decoding or expectation fails |
|
118 | + * @return self |
|
119 | + */ |
|
120 | + public static function expectFromDER(string $data, int &$offset, |
|
121 | + int $expected = null): self |
|
122 | + { |
|
123 | + $idx = $offset; |
|
124 | + $length = self::fromDER($data, $idx); |
|
125 | + // DER encoding must have definite length (spec 10.1) |
|
126 | + if ($length->isIndefinite()) { |
|
127 | + throw new DecodeException("DER encoding must have definite length."); |
|
128 | + } |
|
129 | + // if certain length was expected |
|
130 | + if (isset($expected) && $expected != $length->intLength()) { |
|
131 | + throw new DecodeException( |
|
132 | + sprintf("Expected length %d, got %d.", $expected, |
|
133 | + $length->intLength())); |
|
134 | + } |
|
135 | + // check that enough data is available |
|
136 | + if (strlen($data) < $idx + $length->intLength()) { |
|
137 | + throw new DecodeException( |
|
138 | + sprintf("Length %d overflows data, %d bytes left.", |
|
139 | + $length->intLength(), strlen($data) - $idx)); |
|
140 | + } |
|
141 | + $offset = $idx; |
|
142 | + return $length; |
|
143 | + } |
|
144 | 144 | |
145 | - /** |
|
146 | - * |
|
147 | - * @see Encodable::toDER() |
|
148 | - * @throws \DomainException If length is too large to encode |
|
149 | - * @return string |
|
150 | - */ |
|
151 | - public function toDER(): string |
|
152 | - { |
|
153 | - $bytes = []; |
|
154 | - if ($this->_indefinite) { |
|
155 | - $bytes[] = 0x80; |
|
156 | - } else { |
|
157 | - $num = $this->_length->gmpObj(); |
|
158 | - // long form |
|
159 | - if ($num > 127) { |
|
160 | - $octets = []; |
|
161 | - for (; $num > 0; $num >>= 8) { |
|
162 | - $octets[] = gmp_intval(0xff & $num); |
|
163 | - } |
|
164 | - $count = count($octets); |
|
165 | - // first octet must not be 0xff |
|
166 | - if ($count >= 127) { |
|
167 | - throw new \DomainException("Too many length octets."); |
|
168 | - } |
|
169 | - $bytes[] = 0x80 | $count; |
|
170 | - foreach (array_reverse($octets) as $octet) { |
|
171 | - $bytes[] = $octet; |
|
172 | - } |
|
173 | - } else { // short form |
|
174 | - $bytes[] = gmp_intval($num); |
|
175 | - } |
|
176 | - } |
|
177 | - return pack("C*", ...$bytes); |
|
178 | - } |
|
145 | + /** |
|
146 | + * |
|
147 | + * @see Encodable::toDER() |
|
148 | + * @throws \DomainException If length is too large to encode |
|
149 | + * @return string |
|
150 | + */ |
|
151 | + public function toDER(): string |
|
152 | + { |
|
153 | + $bytes = []; |
|
154 | + if ($this->_indefinite) { |
|
155 | + $bytes[] = 0x80; |
|
156 | + } else { |
|
157 | + $num = $this->_length->gmpObj(); |
|
158 | + // long form |
|
159 | + if ($num > 127) { |
|
160 | + $octets = []; |
|
161 | + for (; $num > 0; $num >>= 8) { |
|
162 | + $octets[] = gmp_intval(0xff & $num); |
|
163 | + } |
|
164 | + $count = count($octets); |
|
165 | + // first octet must not be 0xff |
|
166 | + if ($count >= 127) { |
|
167 | + throw new \DomainException("Too many length octets."); |
|
168 | + } |
|
169 | + $bytes[] = 0x80 | $count; |
|
170 | + foreach (array_reverse($octets) as $octet) { |
|
171 | + $bytes[] = $octet; |
|
172 | + } |
|
173 | + } else { // short form |
|
174 | + $bytes[] = gmp_intval($num); |
|
175 | + } |
|
176 | + } |
|
177 | + return pack("C*", ...$bytes); |
|
178 | + } |
|
179 | 179 | |
180 | - /** |
|
181 | - * Get the length. |
|
182 | - * |
|
183 | - * @throws \LogicException If length is indefinite |
|
184 | - * @return string Length as an integer string |
|
185 | - */ |
|
186 | - public function length(): string |
|
187 | - { |
|
188 | - if ($this->_indefinite) { |
|
189 | - throw new \LogicException("Length is indefinite."); |
|
190 | - } |
|
191 | - return $this->_length->base10(); |
|
192 | - } |
|
180 | + /** |
|
181 | + * Get the length. |
|
182 | + * |
|
183 | + * @throws \LogicException If length is indefinite |
|
184 | + * @return string Length as an integer string |
|
185 | + */ |
|
186 | + public function length(): string |
|
187 | + { |
|
188 | + if ($this->_indefinite) { |
|
189 | + throw new \LogicException("Length is indefinite."); |
|
190 | + } |
|
191 | + return $this->_length->base10(); |
|
192 | + } |
|
193 | 193 | |
194 | - /** |
|
195 | - * Get the length as an integer. |
|
196 | - * |
|
197 | - * @throws \LogicException If length is indefinite |
|
198 | - * @throws \RuntimeException If length overflows integer size |
|
199 | - * @return int |
|
200 | - */ |
|
201 | - public function intLength(): int |
|
202 | - { |
|
203 | - if ($this->_indefinite) { |
|
204 | - throw new \LogicException("Length is indefinite."); |
|
205 | - } |
|
206 | - return $this->_length->intVal(); |
|
207 | - } |
|
194 | + /** |
|
195 | + * Get the length as an integer. |
|
196 | + * |
|
197 | + * @throws \LogicException If length is indefinite |
|
198 | + * @throws \RuntimeException If length overflows integer size |
|
199 | + * @return int |
|
200 | + */ |
|
201 | + public function intLength(): int |
|
202 | + { |
|
203 | + if ($this->_indefinite) { |
|
204 | + throw new \LogicException("Length is indefinite."); |
|
205 | + } |
|
206 | + return $this->_length->intVal(); |
|
207 | + } |
|
208 | 208 | |
209 | - /** |
|
210 | - * Whether length is indefinite. |
|
211 | - * |
|
212 | - * @return bool |
|
213 | - */ |
|
214 | - public function isIndefinite(): bool |
|
215 | - { |
|
216 | - return $this->_indefinite; |
|
217 | - } |
|
209 | + /** |
|
210 | + * Whether length is indefinite. |
|
211 | + * |
|
212 | + * @return bool |
|
213 | + */ |
|
214 | + public function isIndefinite(): bool |
|
215 | + { |
|
216 | + return $this->_indefinite; |
|
217 | + } |
|
218 | 218 | } |
@@ -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\Component; |
6 | 6 |
@@ -13,86 +13,86 @@ |
||
13 | 13 | */ |
14 | 14 | interface ElementBase extends Encodable |
15 | 15 | { |
16 | - /** |
|
17 | - * Get the class of the ASN.1 type. |
|
18 | - * |
|
19 | - * One of <code>Identifier::CLASS_*</code> constants. |
|
20 | - * |
|
21 | - * @return int |
|
22 | - */ |
|
23 | - public function typeClass(): int; |
|
16 | + /** |
|
17 | + * Get the class of the ASN.1 type. |
|
18 | + * |
|
19 | + * One of <code>Identifier::CLASS_*</code> constants. |
|
20 | + * |
|
21 | + * @return int |
|
22 | + */ |
|
23 | + public function typeClass(): int; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Check whether the element is constructed. |
|
27 | - * |
|
28 | - * Otherwise it's primitive. |
|
29 | - * |
|
30 | - * @return bool |
|
31 | - */ |
|
32 | - public function isConstructed(): bool; |
|
25 | + /** |
|
26 | + * Check whether the element is constructed. |
|
27 | + * |
|
28 | + * Otherwise it's primitive. |
|
29 | + * |
|
30 | + * @return bool |
|
31 | + */ |
|
32 | + public function isConstructed(): bool; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Get the tag of the element. |
|
36 | - * |
|
37 | - * Interpretation of the tag depends on the context. For example it may |
|
38 | - * represent a universal type tag or a tag of an implicitly or explicitly |
|
39 | - * tagged type. |
|
40 | - * |
|
41 | - * @return int |
|
42 | - */ |
|
43 | - public function tag(): int; |
|
34 | + /** |
|
35 | + * Get the tag of the element. |
|
36 | + * |
|
37 | + * Interpretation of the tag depends on the context. For example it may |
|
38 | + * represent a universal type tag or a tag of an implicitly or explicitly |
|
39 | + * tagged type. |
|
40 | + * |
|
41 | + * @return int |
|
42 | + */ |
|
43 | + public function tag(): int; |
|
44 | 44 | |
45 | - /** |
|
46 | - * Check whether the element is a type of a given tag. |
|
47 | - * |
|
48 | - * @param int $tag Type tag |
|
49 | - * @return bool |
|
50 | - */ |
|
51 | - public function isType(int $tag): bool; |
|
45 | + /** |
|
46 | + * Check whether the element is a type of a given tag. |
|
47 | + * |
|
48 | + * @param int $tag Type tag |
|
49 | + * @return bool |
|
50 | + */ |
|
51 | + public function isType(int $tag): bool; |
|
52 | 52 | |
53 | - /** |
|
54 | - * Check whether the element is a type of a given tag. |
|
55 | - * |
|
56 | - * Throws an exception if expectation fails. |
|
57 | - * |
|
58 | - * @param int $tag Type tag |
|
59 | - * @throws \UnexpectedValueException If the element type differs from the |
|
60 | - * expected |
|
61 | - * @return ElementBase |
|
62 | - */ |
|
63 | - public function expectType(int $tag): ElementBase; |
|
53 | + /** |
|
54 | + * Check whether the element is a type of a given tag. |
|
55 | + * |
|
56 | + * Throws an exception if expectation fails. |
|
57 | + * |
|
58 | + * @param int $tag Type tag |
|
59 | + * @throws \UnexpectedValueException If the element type differs from the |
|
60 | + * expected |
|
61 | + * @return ElementBase |
|
62 | + */ |
|
63 | + public function expectType(int $tag): ElementBase; |
|
64 | 64 | |
65 | - /** |
|
66 | - * Check whether the element is tagged (context specific). |
|
67 | - * |
|
68 | - * @return bool |
|
69 | - */ |
|
70 | - public function isTagged(): bool; |
|
65 | + /** |
|
66 | + * Check whether the element is tagged (context specific). |
|
67 | + * |
|
68 | + * @return bool |
|
69 | + */ |
|
70 | + public function isTagged(): bool; |
|
71 | 71 | |
72 | - /** |
|
73 | - * Check whether the element is tagged (context specific) and optionally has |
|
74 | - * a given tag. |
|
75 | - * |
|
76 | - * Throws an exception if the element is not tagged or tag differs from |
|
77 | - * the expected. |
|
78 | - * |
|
79 | - * @param int|null $tag Optional type tag |
|
80 | - * @throws \UnexpectedValueException If expectation fails |
|
81 | - * @return \ASN1\Type\TaggedType |
|
82 | - */ |
|
83 | - public function expectTagged($tag = null): TaggedType; |
|
72 | + /** |
|
73 | + * Check whether the element is tagged (context specific) and optionally has |
|
74 | + * a given tag. |
|
75 | + * |
|
76 | + * Throws an exception if the element is not tagged or tag differs from |
|
77 | + * the expected. |
|
78 | + * |
|
79 | + * @param int|null $tag Optional type tag |
|
80 | + * @throws \UnexpectedValueException If expectation fails |
|
81 | + * @return \ASN1\Type\TaggedType |
|
82 | + */ |
|
83 | + public function expectTagged($tag = null): TaggedType; |
|
84 | 84 | |
85 | - /** |
|
86 | - * Get the object as an abstract Element instance. |
|
87 | - * |
|
88 | - * @return \ASN1\Element |
|
89 | - */ |
|
90 | - public function asElement(): Element; |
|
85 | + /** |
|
86 | + * Get the object as an abstract Element instance. |
|
87 | + * |
|
88 | + * @return \ASN1\Element |
|
89 | + */ |
|
90 | + public function asElement(): Element; |
|
91 | 91 | |
92 | - /** |
|
93 | - * Get the object as an UnspecifiedType instance. |
|
94 | - * |
|
95 | - * @return UnspecifiedType |
|
96 | - */ |
|
97 | - public function asUnspecified(): UnspecifiedType; |
|
92 | + /** |
|
93 | + * Get the object as an UnspecifiedType instance. |
|
94 | + * |
|
95 | + * @return UnspecifiedType |
|
96 | + */ |
|
97 | + public function asUnspecified(): UnspecifiedType; |
|
98 | 98 | } |
@@ -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 |
@@ -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\Exception; |
6 | 6 |
@@ -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\Component; |
6 | 6 | |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | * |
27 | 27 | * @var array |
28 | 28 | */ |
29 | - const MAP_CLASS_TO_NAME = [ /* @formatter:off */ |
|
29 | + const MAP_CLASS_TO_NAME = [/* @formatter:off */ |
|
30 | 30 | self::CLASS_UNIVERSAL => "UNIVERSAL", |
31 | 31 | self::CLASS_APPLICATION => "APPLICATION", |
32 | 32 | self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
@@ -12,295 +12,295 @@ |
||
12 | 12 | */ |
13 | 13 | class Identifier implements Encodable |
14 | 14 | { |
15 | - // Type class enumerations |
|
16 | - const CLASS_UNIVERSAL = 0b00; |
|
17 | - const CLASS_APPLICATION = 0b01; |
|
18 | - const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
19 | - const CLASS_PRIVATE = 0b11; |
|
15 | + // Type class enumerations |
|
16 | + const CLASS_UNIVERSAL = 0b00; |
|
17 | + const CLASS_APPLICATION = 0b01; |
|
18 | + const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
19 | + const CLASS_PRIVATE = 0b11; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Mapping from type class to human readable name. |
|
23 | - * |
|
24 | - * @internal |
|
25 | - * |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - const MAP_CLASS_TO_NAME = [ /* @formatter:off */ |
|
29 | - self::CLASS_UNIVERSAL => "UNIVERSAL", |
|
30 | - self::CLASS_APPLICATION => "APPLICATION", |
|
31 | - self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
|
32 | - self::CLASS_PRIVATE => "PRIVATE", |
|
33 | - /* @formatter:on */ |
|
34 | - ]; |
|
21 | + /** |
|
22 | + * Mapping from type class to human readable name. |
|
23 | + * |
|
24 | + * @internal |
|
25 | + * |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + const MAP_CLASS_TO_NAME = [ /* @formatter:off */ |
|
29 | + self::CLASS_UNIVERSAL => "UNIVERSAL", |
|
30 | + self::CLASS_APPLICATION => "APPLICATION", |
|
31 | + self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
|
32 | + self::CLASS_PRIVATE => "PRIVATE", |
|
33 | + /* @formatter:on */ |
|
34 | + ]; |
|
35 | 35 | |
36 | - // P/C enumerations |
|
37 | - const PRIMITIVE = 0b0; |
|
38 | - const CONSTRUCTED = 0b1; |
|
36 | + // P/C enumerations |
|
37 | + const PRIMITIVE = 0b0; |
|
38 | + const CONSTRUCTED = 0b1; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Type class. |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - private $_class; |
|
40 | + /** |
|
41 | + * Type class. |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + private $_class; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Primitive or Constructed. |
|
49 | - * |
|
50 | - * @var int |
|
51 | - */ |
|
52 | - private $_pc; |
|
47 | + /** |
|
48 | + * Primitive or Constructed. |
|
49 | + * |
|
50 | + * @var int |
|
51 | + */ |
|
52 | + private $_pc; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Content type tag. |
|
56 | - * |
|
57 | - * @var BigInt |
|
58 | - */ |
|
59 | - private $_tag; |
|
54 | + /** |
|
55 | + * Content type tag. |
|
56 | + * |
|
57 | + * @var BigInt |
|
58 | + */ |
|
59 | + private $_tag; |
|
60 | 60 | |
61 | - /** |
|
62 | - * Constructor. |
|
63 | - * |
|
64 | - * @param int $class Type class |
|
65 | - * @param int $pc Primitive / Constructed |
|
66 | - * @param int|string $tag Type tag number |
|
67 | - */ |
|
68 | - public function __construct(int $class, int $pc, $tag) |
|
69 | - { |
|
70 | - $this->_class = 0b11 & $class; |
|
71 | - $this->_pc = 0b1 & $pc; |
|
72 | - $this->_tag = new BigInt($tag); |
|
73 | - } |
|
61 | + /** |
|
62 | + * Constructor. |
|
63 | + * |
|
64 | + * @param int $class Type class |
|
65 | + * @param int $pc Primitive / Constructed |
|
66 | + * @param int|string $tag Type tag number |
|
67 | + */ |
|
68 | + public function __construct(int $class, int $pc, $tag) |
|
69 | + { |
|
70 | + $this->_class = 0b11 & $class; |
|
71 | + $this->_pc = 0b1 & $pc; |
|
72 | + $this->_tag = new BigInt($tag); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Decode identifier component from DER data. |
|
77 | - * |
|
78 | - * @param string $data DER encoded data |
|
79 | - * @param int|null $offset Reference to the variable that contains offset |
|
80 | - * into the data where to start parsing. Variable is updated to |
|
81 | - * the offset next to the parsed identifier. If null, start from |
|
82 | - * offset 0. |
|
83 | - * @throws DecodeException If decoding fails |
|
84 | - * @return self |
|
85 | - */ |
|
86 | - public static function fromDER(string $data, int &$offset = null): Identifier |
|
87 | - { |
|
88 | - $idx = $offset ?? 0; |
|
89 | - $datalen = strlen($data); |
|
90 | - if ($idx >= $datalen) { |
|
91 | - throw new DecodeException("Invalid offset."); |
|
92 | - } |
|
93 | - $byte = ord($data[$idx++]); |
|
94 | - // bits 8 and 7 (class) |
|
95 | - // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
96 | - $class = (0b11000000 & $byte) >> 6; |
|
97 | - // bit 6 (0 = primitive / 1 = constructed) |
|
98 | - $pc = (0b00100000 & $byte) >> 5; |
|
99 | - // bits 5 to 1 (tag number) |
|
100 | - $tag = (0b00011111 & $byte); |
|
101 | - // long-form identifier |
|
102 | - if (0x1f == $tag) { |
|
103 | - $tag = self::_decodeLongFormTag($data, $idx); |
|
104 | - } |
|
105 | - if (isset($offset)) { |
|
106 | - $offset = $idx; |
|
107 | - } |
|
108 | - return new self($class, $pc, $tag); |
|
109 | - } |
|
75 | + /** |
|
76 | + * Decode identifier component from DER data. |
|
77 | + * |
|
78 | + * @param string $data DER encoded data |
|
79 | + * @param int|null $offset Reference to the variable that contains offset |
|
80 | + * into the data where to start parsing. Variable is updated to |
|
81 | + * the offset next to the parsed identifier. If null, start from |
|
82 | + * offset 0. |
|
83 | + * @throws DecodeException If decoding fails |
|
84 | + * @return self |
|
85 | + */ |
|
86 | + public static function fromDER(string $data, int &$offset = null): Identifier |
|
87 | + { |
|
88 | + $idx = $offset ?? 0; |
|
89 | + $datalen = strlen($data); |
|
90 | + if ($idx >= $datalen) { |
|
91 | + throw new DecodeException("Invalid offset."); |
|
92 | + } |
|
93 | + $byte = ord($data[$idx++]); |
|
94 | + // bits 8 and 7 (class) |
|
95 | + // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
96 | + $class = (0b11000000 & $byte) >> 6; |
|
97 | + // bit 6 (0 = primitive / 1 = constructed) |
|
98 | + $pc = (0b00100000 & $byte) >> 5; |
|
99 | + // bits 5 to 1 (tag number) |
|
100 | + $tag = (0b00011111 & $byte); |
|
101 | + // long-form identifier |
|
102 | + if (0x1f == $tag) { |
|
103 | + $tag = self::_decodeLongFormTag($data, $idx); |
|
104 | + } |
|
105 | + if (isset($offset)) { |
|
106 | + $offset = $idx; |
|
107 | + } |
|
108 | + return new self($class, $pc, $tag); |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * Parse long form tag. |
|
113 | - * |
|
114 | - * @param string $data DER data |
|
115 | - * @param int $offset Reference to the variable containing offset to data |
|
116 | - * @throws DecodeException If decoding fails |
|
117 | - * @return string Tag number |
|
118 | - */ |
|
119 | - private static function _decodeLongFormTag(string $data, int &$offset): string |
|
120 | - { |
|
121 | - $datalen = strlen($data); |
|
122 | - $tag = gmp_init(0, 10); |
|
123 | - while (true) { |
|
124 | - if ($offset >= $datalen) { |
|
125 | - throw new DecodeException( |
|
126 | - "Unexpected end of data while decoding" . |
|
127 | - " long form identifier."); |
|
128 | - } |
|
129 | - $byte = ord($data[$offset++]); |
|
130 | - $tag <<= 7; |
|
131 | - $tag |= 0x7f & $byte; |
|
132 | - // last byte has bit 8 set to zero |
|
133 | - if (!(0x80 & $byte)) { |
|
134 | - break; |
|
135 | - } |
|
136 | - } |
|
137 | - return gmp_strval($tag, 10); |
|
138 | - } |
|
111 | + /** |
|
112 | + * Parse long form tag. |
|
113 | + * |
|
114 | + * @param string $data DER data |
|
115 | + * @param int $offset Reference to the variable containing offset to data |
|
116 | + * @throws DecodeException If decoding fails |
|
117 | + * @return string Tag number |
|
118 | + */ |
|
119 | + private static function _decodeLongFormTag(string $data, int &$offset): string |
|
120 | + { |
|
121 | + $datalen = strlen($data); |
|
122 | + $tag = gmp_init(0, 10); |
|
123 | + while (true) { |
|
124 | + if ($offset >= $datalen) { |
|
125 | + throw new DecodeException( |
|
126 | + "Unexpected end of data while decoding" . |
|
127 | + " long form identifier."); |
|
128 | + } |
|
129 | + $byte = ord($data[$offset++]); |
|
130 | + $tag <<= 7; |
|
131 | + $tag |= 0x7f & $byte; |
|
132 | + // last byte has bit 8 set to zero |
|
133 | + if (!(0x80 & $byte)) { |
|
134 | + break; |
|
135 | + } |
|
136 | + } |
|
137 | + return gmp_strval($tag, 10); |
|
138 | + } |
|
139 | 139 | |
140 | - /** |
|
141 | - * |
|
142 | - * @see Encodable::toDER() |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - public function toDER(): string |
|
146 | - { |
|
147 | - $bytes = []; |
|
148 | - $byte = $this->_class << 6 | $this->_pc << 5; |
|
149 | - $tag = $this->_tag->gmpObj(); |
|
150 | - if ($tag < 0x1f) { |
|
151 | - $bytes[] = $byte | $tag; |
|
152 | - } else { // long-form identifier |
|
153 | - $bytes[] = $byte | 0x1f; |
|
154 | - $octets = []; |
|
155 | - for (; $tag > 0; $tag >>= 7) { |
|
156 | - array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
157 | - } |
|
158 | - // last octet has bit 8 set to zero |
|
159 | - $octets[0] &= 0x7f; |
|
160 | - foreach (array_reverse($octets) as $octet) { |
|
161 | - $bytes[] = $octet; |
|
162 | - } |
|
163 | - } |
|
164 | - return pack("C*", ...$bytes); |
|
165 | - } |
|
140 | + /** |
|
141 | + * |
|
142 | + * @see Encodable::toDER() |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + public function toDER(): string |
|
146 | + { |
|
147 | + $bytes = []; |
|
148 | + $byte = $this->_class << 6 | $this->_pc << 5; |
|
149 | + $tag = $this->_tag->gmpObj(); |
|
150 | + if ($tag < 0x1f) { |
|
151 | + $bytes[] = $byte | $tag; |
|
152 | + } else { // long-form identifier |
|
153 | + $bytes[] = $byte | 0x1f; |
|
154 | + $octets = []; |
|
155 | + for (; $tag > 0; $tag >>= 7) { |
|
156 | + array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
157 | + } |
|
158 | + // last octet has bit 8 set to zero |
|
159 | + $octets[0] &= 0x7f; |
|
160 | + foreach (array_reverse($octets) as $octet) { |
|
161 | + $bytes[] = $octet; |
|
162 | + } |
|
163 | + } |
|
164 | + return pack("C*", ...$bytes); |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * Get class of the type. |
|
169 | - * |
|
170 | - * @return int |
|
171 | - */ |
|
172 | - public function typeClass(): int |
|
173 | - { |
|
174 | - return $this->_class; |
|
175 | - } |
|
167 | + /** |
|
168 | + * Get class of the type. |
|
169 | + * |
|
170 | + * @return int |
|
171 | + */ |
|
172 | + public function typeClass(): int |
|
173 | + { |
|
174 | + return $this->_class; |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * Get P/C. |
|
179 | - * |
|
180 | - * @return int |
|
181 | - */ |
|
182 | - public function pc(): int |
|
183 | - { |
|
184 | - return $this->_pc; |
|
185 | - } |
|
177 | + /** |
|
178 | + * Get P/C. |
|
179 | + * |
|
180 | + * @return int |
|
181 | + */ |
|
182 | + public function pc(): int |
|
183 | + { |
|
184 | + return $this->_pc; |
|
185 | + } |
|
186 | 186 | |
187 | - /** |
|
188 | - * Get the tag number. |
|
189 | - * |
|
190 | - * @return string Base 10 integer string |
|
191 | - */ |
|
192 | - public function tag(): string |
|
193 | - { |
|
194 | - return $this->_tag->base10(); |
|
195 | - } |
|
187 | + /** |
|
188 | + * Get the tag number. |
|
189 | + * |
|
190 | + * @return string Base 10 integer string |
|
191 | + */ |
|
192 | + public function tag(): string |
|
193 | + { |
|
194 | + return $this->_tag->base10(); |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * Get the tag as an integer. |
|
199 | - * |
|
200 | - * @return int |
|
201 | - */ |
|
202 | - public function intTag(): int |
|
203 | - { |
|
204 | - return $this->_tag->intVal(); |
|
205 | - } |
|
197 | + /** |
|
198 | + * Get the tag as an integer. |
|
199 | + * |
|
200 | + * @return int |
|
201 | + */ |
|
202 | + public function intTag(): int |
|
203 | + { |
|
204 | + return $this->_tag->intVal(); |
|
205 | + } |
|
206 | 206 | |
207 | - /** |
|
208 | - * Check whether type is of an universal class. |
|
209 | - * |
|
210 | - * @return bool |
|
211 | - */ |
|
212 | - public function isUniversal(): bool |
|
213 | - { |
|
214 | - return self::CLASS_UNIVERSAL == $this->_class; |
|
215 | - } |
|
207 | + /** |
|
208 | + * Check whether type is of an universal class. |
|
209 | + * |
|
210 | + * @return bool |
|
211 | + */ |
|
212 | + public function isUniversal(): bool |
|
213 | + { |
|
214 | + return self::CLASS_UNIVERSAL == $this->_class; |
|
215 | + } |
|
216 | 216 | |
217 | - /** |
|
218 | - * Check whether type is of an application class. |
|
219 | - * |
|
220 | - * @return bool |
|
221 | - */ |
|
222 | - public function isApplication(): bool |
|
223 | - { |
|
224 | - return self::CLASS_APPLICATION == $this->_class; |
|
225 | - } |
|
217 | + /** |
|
218 | + * Check whether type is of an application class. |
|
219 | + * |
|
220 | + * @return bool |
|
221 | + */ |
|
222 | + public function isApplication(): bool |
|
223 | + { |
|
224 | + return self::CLASS_APPLICATION == $this->_class; |
|
225 | + } |
|
226 | 226 | |
227 | - /** |
|
228 | - * Check whether type is of a context specific class. |
|
229 | - * |
|
230 | - * @return bool |
|
231 | - */ |
|
232 | - public function isContextSpecific(): bool |
|
233 | - { |
|
234 | - return self::CLASS_CONTEXT_SPECIFIC == $this->_class; |
|
235 | - } |
|
227 | + /** |
|
228 | + * Check whether type is of a context specific class. |
|
229 | + * |
|
230 | + * @return bool |
|
231 | + */ |
|
232 | + public function isContextSpecific(): bool |
|
233 | + { |
|
234 | + return self::CLASS_CONTEXT_SPECIFIC == $this->_class; |
|
235 | + } |
|
236 | 236 | |
237 | - /** |
|
238 | - * Check whether type is of a private class. |
|
239 | - * |
|
240 | - * @return bool |
|
241 | - */ |
|
242 | - public function isPrivate(): bool |
|
243 | - { |
|
244 | - return self::CLASS_PRIVATE == $this->_class; |
|
245 | - } |
|
237 | + /** |
|
238 | + * Check whether type is of a private class. |
|
239 | + * |
|
240 | + * @return bool |
|
241 | + */ |
|
242 | + public function isPrivate(): bool |
|
243 | + { |
|
244 | + return self::CLASS_PRIVATE == $this->_class; |
|
245 | + } |
|
246 | 246 | |
247 | - /** |
|
248 | - * Check whether content is primitive type. |
|
249 | - * |
|
250 | - * @return bool |
|
251 | - */ |
|
252 | - public function isPrimitive(): bool |
|
253 | - { |
|
254 | - return self::PRIMITIVE == $this->_pc; |
|
255 | - } |
|
247 | + /** |
|
248 | + * Check whether content is primitive type. |
|
249 | + * |
|
250 | + * @return bool |
|
251 | + */ |
|
252 | + public function isPrimitive(): bool |
|
253 | + { |
|
254 | + return self::PRIMITIVE == $this->_pc; |
|
255 | + } |
|
256 | 256 | |
257 | - /** |
|
258 | - * Check hether content is constructed type. |
|
259 | - * |
|
260 | - * @return bool |
|
261 | - */ |
|
262 | - public function isConstructed(): bool |
|
263 | - { |
|
264 | - return self::CONSTRUCTED == $this->_pc; |
|
265 | - } |
|
257 | + /** |
|
258 | + * Check hether content is constructed type. |
|
259 | + * |
|
260 | + * @return bool |
|
261 | + */ |
|
262 | + public function isConstructed(): bool |
|
263 | + { |
|
264 | + return self::CONSTRUCTED == $this->_pc; |
|
265 | + } |
|
266 | 266 | |
267 | - /** |
|
268 | - * Get self with given type class. |
|
269 | - * |
|
270 | - * @param int $class One of <code>CLASS_*</code> enumerations |
|
271 | - * @return self |
|
272 | - */ |
|
273 | - public function withClass(int $class): Identifier |
|
274 | - { |
|
275 | - $obj = clone $this; |
|
276 | - $obj->_class = 0b11 & $class; |
|
277 | - return $obj; |
|
278 | - } |
|
267 | + /** |
|
268 | + * Get self with given type class. |
|
269 | + * |
|
270 | + * @param int $class One of <code>CLASS_*</code> enumerations |
|
271 | + * @return self |
|
272 | + */ |
|
273 | + public function withClass(int $class): Identifier |
|
274 | + { |
|
275 | + $obj = clone $this; |
|
276 | + $obj->_class = 0b11 & $class; |
|
277 | + return $obj; |
|
278 | + } |
|
279 | 279 | |
280 | - /** |
|
281 | - * Get self with given type tag. |
|
282 | - * |
|
283 | - * @param int|string $tag Tag number |
|
284 | - * @return self |
|
285 | - */ |
|
286 | - public function withTag($tag): Identifier |
|
287 | - { |
|
288 | - $obj = clone $this; |
|
289 | - $obj->_tag = new BigInt($tag); |
|
290 | - return $obj; |
|
291 | - } |
|
280 | + /** |
|
281 | + * Get self with given type tag. |
|
282 | + * |
|
283 | + * @param int|string $tag Tag number |
|
284 | + * @return self |
|
285 | + */ |
|
286 | + public function withTag($tag): Identifier |
|
287 | + { |
|
288 | + $obj = clone $this; |
|
289 | + $obj->_tag = new BigInt($tag); |
|
290 | + return $obj; |
|
291 | + } |
|
292 | 292 | |
293 | - /** |
|
294 | - * Get human readable name of the type class. |
|
295 | - * |
|
296 | - * @param int $class |
|
297 | - * @return string |
|
298 | - */ |
|
299 | - public static function classToName(int $class): string |
|
300 | - { |
|
301 | - if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
302 | - return "CLASS $class"; |
|
303 | - } |
|
304 | - return self::MAP_CLASS_TO_NAME[$class]; |
|
305 | - } |
|
293 | + /** |
|
294 | + * Get human readable name of the type class. |
|
295 | + * |
|
296 | + * @param int $class |
|
297 | + * @return string |
|
298 | + */ |
|
299 | + public static function classToName(int $class): string |
|
300 | + { |
|
301 | + if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
302 | + return "CLASS $class"; |
|
303 | + } |
|
304 | + return self::MAP_CLASS_TO_NAME[$class]; |
|
305 | + } |
|
306 | 306 | } |
@@ -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 | } |
@@ -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, |
|
96 | - string $data, 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, |
|
96 | + string $data, 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,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; |
6 | 6 |
@@ -16,85 +16,85 @@ |
||
16 | 16 | */ |
17 | 17 | abstract class TaggedType extends Element |
18 | 18 | { |
19 | - /** |
|
20 | - * |
|
21 | - * {@inheritdoc} |
|
22 | - */ |
|
23 | - protected static function _decodeFromDER(Identifier $identifier, |
|
24 | - string $data, int &$offset): ElementBase |
|
25 | - { |
|
26 | - $idx = $offset; |
|
27 | - $type = new DERTaggedType($identifier, $data, $idx); |
|
28 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
29 | - $offset = $idx + $length; |
|
30 | - return $type; |
|
31 | - } |
|
19 | + /** |
|
20 | + * |
|
21 | + * {@inheritdoc} |
|
22 | + */ |
|
23 | + protected static function _decodeFromDER(Identifier $identifier, |
|
24 | + string $data, int &$offset): ElementBase |
|
25 | + { |
|
26 | + $idx = $offset; |
|
27 | + $type = new DERTaggedType($identifier, $data, $idx); |
|
28 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
29 | + $offset = $idx + $length; |
|
30 | + return $type; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Check whether element supports explicit tagging. |
|
35 | - * |
|
36 | - * @param int|null $expectedTag Optional outer tag expectation |
|
37 | - * @throws \UnexpectedValueException If expectation fails |
|
38 | - * @return ExplicitTagging |
|
39 | - */ |
|
40 | - public function expectExplicit($expectedTag = null): ExplicitTagging |
|
41 | - { |
|
42 | - $el = $this; |
|
43 | - if (!$el instanceof ExplicitTagging) { |
|
44 | - throw new \UnexpectedValueException( |
|
45 | - "Element doesn't implement explicit tagging."); |
|
46 | - } |
|
47 | - if (isset($expectedTag)) { |
|
48 | - $el->expectTagged($expectedTag); |
|
49 | - } |
|
50 | - return $el; |
|
51 | - } |
|
33 | + /** |
|
34 | + * Check whether element supports explicit tagging. |
|
35 | + * |
|
36 | + * @param int|null $expectedTag Optional outer tag expectation |
|
37 | + * @throws \UnexpectedValueException If expectation fails |
|
38 | + * @return ExplicitTagging |
|
39 | + */ |
|
40 | + public function expectExplicit($expectedTag = null): ExplicitTagging |
|
41 | + { |
|
42 | + $el = $this; |
|
43 | + if (!$el instanceof ExplicitTagging) { |
|
44 | + throw new \UnexpectedValueException( |
|
45 | + "Element doesn't implement explicit tagging."); |
|
46 | + } |
|
47 | + if (isset($expectedTag)) { |
|
48 | + $el->expectTagged($expectedTag); |
|
49 | + } |
|
50 | + return $el; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Get the wrapped inner element employing explicit tagging. |
|
55 | - * |
|
56 | - * @param int|null $expectedTag Optional outer tag expectation |
|
57 | - * @throws \UnexpectedValueException If expectation fails |
|
58 | - * @return UnspecifiedType |
|
59 | - */ |
|
60 | - public function asExplicit($expectedTag = null): UnspecifiedType |
|
61 | - { |
|
62 | - return $this->expectExplicit($expectedTag)->explicit(); |
|
63 | - } |
|
53 | + /** |
|
54 | + * Get the wrapped inner element employing explicit tagging. |
|
55 | + * |
|
56 | + * @param int|null $expectedTag Optional outer tag expectation |
|
57 | + * @throws \UnexpectedValueException If expectation fails |
|
58 | + * @return UnspecifiedType |
|
59 | + */ |
|
60 | + public function asExplicit($expectedTag = null): UnspecifiedType |
|
61 | + { |
|
62 | + return $this->expectExplicit($expectedTag)->explicit(); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Check whether element supports implicit tagging. |
|
67 | - * |
|
68 | - * @param int|null $expectedTag Optional outer tag expectation |
|
69 | - * @throws \UnexpectedValueException If expectation fails |
|
70 | - * @return ImplicitTagging |
|
71 | - */ |
|
72 | - public function expectImplicit($expectedTag = null): ImplicitTagging |
|
73 | - { |
|
74 | - $el = $this; |
|
75 | - if (!$el instanceof ImplicitTagging) { |
|
76 | - throw new \UnexpectedValueException( |
|
77 | - "Element doesn't implement implicit tagging."); |
|
78 | - } |
|
79 | - if (isset($expectedTag)) { |
|
80 | - $el->expectTagged($expectedTag); |
|
81 | - } |
|
82 | - return $el; |
|
83 | - } |
|
65 | + /** |
|
66 | + * Check whether element supports implicit tagging. |
|
67 | + * |
|
68 | + * @param int|null $expectedTag Optional outer tag expectation |
|
69 | + * @throws \UnexpectedValueException If expectation fails |
|
70 | + * @return ImplicitTagging |
|
71 | + */ |
|
72 | + public function expectImplicit($expectedTag = null): ImplicitTagging |
|
73 | + { |
|
74 | + $el = $this; |
|
75 | + if (!$el instanceof ImplicitTagging) { |
|
76 | + throw new \UnexpectedValueException( |
|
77 | + "Element doesn't implement implicit tagging."); |
|
78 | + } |
|
79 | + if (isset($expectedTag)) { |
|
80 | + $el->expectTagged($expectedTag); |
|
81 | + } |
|
82 | + return $el; |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Get the wrapped inner element employing implicit tagging. |
|
87 | - * |
|
88 | - * @param int $tag Type tag of the inner element |
|
89 | - * @param int|null $expectedTag Optional outer tag expectation |
|
90 | - * @param int $expectedClass Optional inner type class expectation |
|
91 | - * @throws \UnexpectedValueException If expectation fails |
|
92 | - * @return UnspecifiedType |
|
93 | - */ |
|
94 | - public function asImplicit(int $tag, $expectedTag = null, |
|
95 | - int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
96 | - { |
|
97 | - return $this->expectImplicit($expectedTag)->implicit($tag, |
|
98 | - $expectedClass); |
|
99 | - } |
|
85 | + /** |
|
86 | + * Get the wrapped inner element employing implicit tagging. |
|
87 | + * |
|
88 | + * @param int $tag Type tag of the inner element |
|
89 | + * @param int|null $expectedTag Optional outer tag expectation |
|
90 | + * @param int $expectedClass Optional inner type class expectation |
|
91 | + * @throws \UnexpectedValueException If expectation fails |
|
92 | + * @return UnspecifiedType |
|
93 | + */ |
|
94 | + public function asImplicit(int $tag, $expectedTag = null, |
|
95 | + int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
96 | + { |
|
97 | + return $this->expectImplicit($expectedTag)->implicit($tag, |
|
98 | + $expectedClass); |
|
99 | + } |
|
100 | 100 | } |
@@ -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\Primitive; |
5 | 5 | |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | protected static function _implodeSubIDs(\GMP ...$subids): string |
142 | 142 | { |
143 | 143 | return implode(".", |
144 | - array_map(function ($num) { |
|
144 | + array_map(function($num) { |
|
145 | 145 | return gmp_strval($num, 10); |
146 | 146 | }, $subids)); |
147 | 147 | } |
@@ -16,197 +16,197 @@ |
||
16 | 16 | */ |
17 | 17 | class ObjectIdentifier extends Element |
18 | 18 | { |
19 | - use UniversalClass; |
|
20 | - use PrimitiveType; |
|
19 | + use UniversalClass; |
|
20 | + use PrimitiveType; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Object identifier in dotted format. |
|
24 | - * |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - protected $_oid; |
|
22 | + /** |
|
23 | + * Object identifier in dotted format. |
|
24 | + * |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + protected $_oid; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Object identifier split to sub ID's. |
|
31 | - * |
|
32 | - * @var \GMP[] |
|
33 | - */ |
|
34 | - protected $_subids; |
|
29 | + /** |
|
30 | + * Object identifier split to sub ID's. |
|
31 | + * |
|
32 | + * @var \GMP[] |
|
33 | + */ |
|
34 | + protected $_subids; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Constructor. |
|
38 | - * |
|
39 | - * @param string $oid OID in dotted format |
|
40 | - */ |
|
41 | - public function __construct(string $oid) |
|
42 | - { |
|
43 | - $this->_oid = $oid; |
|
44 | - $this->_subids = self::_explodeDottedOID($oid); |
|
45 | - // if OID is non-empty |
|
46 | - if (count($this->_subids) > 0) { |
|
47 | - // check that at least two nodes are set |
|
48 | - if (count($this->_subids) < 2) { |
|
49 | - throw new \UnexpectedValueException( |
|
50 | - "OID must have at least two nodes."); |
|
51 | - } |
|
52 | - // check that root arc is in 0..2 range |
|
53 | - if ($this->_subids[0] > 2) { |
|
54 | - throw new \UnexpectedValueException( |
|
55 | - "Root arc must be in range of 0..2."); |
|
56 | - } |
|
57 | - // if root arc is 0 or 1, second node must be in 0..39 range |
|
58 | - if ($this->_subids[0] < 2 && $this->_subids[1] >= 40) { |
|
59 | - throw new \UnexpectedValueException( |
|
60 | - "Second node must be in 0..39 range for root arcs 0 and 1."); |
|
61 | - } |
|
62 | - } |
|
63 | - $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER; |
|
64 | - } |
|
36 | + /** |
|
37 | + * Constructor. |
|
38 | + * |
|
39 | + * @param string $oid OID in dotted format |
|
40 | + */ |
|
41 | + public function __construct(string $oid) |
|
42 | + { |
|
43 | + $this->_oid = $oid; |
|
44 | + $this->_subids = self::_explodeDottedOID($oid); |
|
45 | + // if OID is non-empty |
|
46 | + if (count($this->_subids) > 0) { |
|
47 | + // check that at least two nodes are set |
|
48 | + if (count($this->_subids) < 2) { |
|
49 | + throw new \UnexpectedValueException( |
|
50 | + "OID must have at least two nodes."); |
|
51 | + } |
|
52 | + // check that root arc is in 0..2 range |
|
53 | + if ($this->_subids[0] > 2) { |
|
54 | + throw new \UnexpectedValueException( |
|
55 | + "Root arc must be in range of 0..2."); |
|
56 | + } |
|
57 | + // if root arc is 0 or 1, second node must be in 0..39 range |
|
58 | + if ($this->_subids[0] < 2 && $this->_subids[1] >= 40) { |
|
59 | + throw new \UnexpectedValueException( |
|
60 | + "Second node must be in 0..39 range for root arcs 0 and 1."); |
|
61 | + } |
|
62 | + } |
|
63 | + $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Get OID in dotted format. |
|
68 | - * |
|
69 | - * @return string |
|
70 | - */ |
|
71 | - public function oid(): string |
|
72 | - { |
|
73 | - return $this->_oid; |
|
74 | - } |
|
66 | + /** |
|
67 | + * Get OID in dotted format. |
|
68 | + * |
|
69 | + * @return string |
|
70 | + */ |
|
71 | + public function oid(): string |
|
72 | + { |
|
73 | + return $this->_oid; |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * |
|
78 | - * {@inheritdoc} |
|
79 | - */ |
|
80 | - protected function _encodedContentDER(): string |
|
81 | - { |
|
82 | - $subids = $this->_subids; |
|
83 | - // encode first two subids to one according to spec section 8.19.4 |
|
84 | - if (count($subids) >= 2) { |
|
85 | - $num = ($subids[0] * 40) + $subids[1]; |
|
86 | - array_splice($subids, 0, 2, array($num)); |
|
87 | - } |
|
88 | - return self::_encodeSubIDs(...$subids); |
|
89 | - } |
|
76 | + /** |
|
77 | + * |
|
78 | + * {@inheritdoc} |
|
79 | + */ |
|
80 | + protected function _encodedContentDER(): string |
|
81 | + { |
|
82 | + $subids = $this->_subids; |
|
83 | + // encode first two subids to one according to spec section 8.19.4 |
|
84 | + if (count($subids) >= 2) { |
|
85 | + $num = ($subids[0] * 40) + $subids[1]; |
|
86 | + array_splice($subids, 0, 2, array($num)); |
|
87 | + } |
|
88 | + return self::_encodeSubIDs(...$subids); |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * |
|
93 | - * {@inheritdoc} |
|
94 | - * @return self |
|
95 | - */ |
|
96 | - protected static function _decodeFromDER(Identifier $identifier, |
|
97 | - string $data, int &$offset): ElementBase |
|
98 | - { |
|
99 | - $idx = $offset; |
|
100 | - $len = Length::expectFromDER($data, $idx)->intLength(); |
|
101 | - $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
102 | - $idx += $len; |
|
103 | - // decode first subidentifier according to spec section 8.19.4 |
|
104 | - if (isset($subids[0])) { |
|
105 | - if ($subids[0] < 80) { |
|
106 | - list($x, $y) = gmp_div_qr($subids[0], "40"); |
|
107 | - } else { |
|
108 | - $x = gmp_init(2, 10); |
|
109 | - $y = $subids[0] - 80; |
|
110 | - } |
|
111 | - array_splice($subids, 0, 1, array($x, $y)); |
|
112 | - } |
|
113 | - $offset = $idx; |
|
114 | - return new self(self::_implodeSubIDs(...$subids)); |
|
115 | - } |
|
91 | + /** |
|
92 | + * |
|
93 | + * {@inheritdoc} |
|
94 | + * @return self |
|
95 | + */ |
|
96 | + protected static function _decodeFromDER(Identifier $identifier, |
|
97 | + string $data, int &$offset): ElementBase |
|
98 | + { |
|
99 | + $idx = $offset; |
|
100 | + $len = Length::expectFromDER($data, $idx)->intLength(); |
|
101 | + $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
102 | + $idx += $len; |
|
103 | + // decode first subidentifier according to spec section 8.19.4 |
|
104 | + if (isset($subids[0])) { |
|
105 | + if ($subids[0] < 80) { |
|
106 | + list($x, $y) = gmp_div_qr($subids[0], "40"); |
|
107 | + } else { |
|
108 | + $x = gmp_init(2, 10); |
|
109 | + $y = $subids[0] - 80; |
|
110 | + } |
|
111 | + array_splice($subids, 0, 1, array($x, $y)); |
|
112 | + } |
|
113 | + $offset = $idx; |
|
114 | + return new self(self::_implodeSubIDs(...$subids)); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Explode dotted OID to an array of sub ID's. |
|
119 | - * |
|
120 | - * @param string $oid OID in dotted format |
|
121 | - * @return \GMP[] Array of GMP numbers |
|
122 | - */ |
|
123 | - protected static function _explodeDottedOID(string $oid): array |
|
124 | - { |
|
125 | - $subids = []; |
|
126 | - if (strlen($oid)) { |
|
127 | - foreach (explode(".", $oid) as $subid) { |
|
128 | - $n = @gmp_init($subid, 10); |
|
129 | - if (false === $n) { |
|
130 | - throw new \UnexpectedValueException( |
|
131 | - "'$subid' is not a number."); |
|
132 | - } |
|
133 | - $subids[] = $n; |
|
134 | - } |
|
135 | - } |
|
136 | - return $subids; |
|
137 | - } |
|
117 | + /** |
|
118 | + * Explode dotted OID to an array of sub ID's. |
|
119 | + * |
|
120 | + * @param string $oid OID in dotted format |
|
121 | + * @return \GMP[] Array of GMP numbers |
|
122 | + */ |
|
123 | + protected static function _explodeDottedOID(string $oid): array |
|
124 | + { |
|
125 | + $subids = []; |
|
126 | + if (strlen($oid)) { |
|
127 | + foreach (explode(".", $oid) as $subid) { |
|
128 | + $n = @gmp_init($subid, 10); |
|
129 | + if (false === $n) { |
|
130 | + throw new \UnexpectedValueException( |
|
131 | + "'$subid' is not a number."); |
|
132 | + } |
|
133 | + $subids[] = $n; |
|
134 | + } |
|
135 | + } |
|
136 | + return $subids; |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * Implode an array of sub IDs to dotted OID format. |
|
141 | - * |
|
142 | - * @param \GMP ...$subids |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - protected static function _implodeSubIDs(\GMP ...$subids): string |
|
146 | - { |
|
147 | - return implode(".", |
|
148 | - array_map(function ($num) { |
|
149 | - return gmp_strval($num, 10); |
|
150 | - }, $subids)); |
|
151 | - } |
|
139 | + /** |
|
140 | + * Implode an array of sub IDs to dotted OID format. |
|
141 | + * |
|
142 | + * @param \GMP ...$subids |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + protected static function _implodeSubIDs(\GMP ...$subids): string |
|
146 | + { |
|
147 | + return implode(".", |
|
148 | + array_map(function ($num) { |
|
149 | + return gmp_strval($num, 10); |
|
150 | + }, $subids)); |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * Encode sub ID's to DER. |
|
155 | - * |
|
156 | - * @param \GMP ...$subids |
|
157 | - * @return string |
|
158 | - */ |
|
159 | - protected static function _encodeSubIDs(\GMP ...$subids): string |
|
160 | - { |
|
161 | - $data = ""; |
|
162 | - foreach ($subids as $subid) { |
|
163 | - // if number fits to one base 128 byte |
|
164 | - if ($subid < 128) { |
|
165 | - $data .= chr(intval($subid)); |
|
166 | - } else { // encode to multiple bytes |
|
167 | - $bytes = []; |
|
168 | - do { |
|
169 | - array_unshift($bytes, 0x7f & gmp_intval($subid)); |
|
170 | - $subid >>= 7; |
|
171 | - } while ($subid > 0); |
|
172 | - // all bytes except last must have bit 8 set to one |
|
173 | - foreach (array_splice($bytes, 0, -1) as $byte) { |
|
174 | - $data .= chr(0x80 | $byte); |
|
175 | - } |
|
176 | - $data .= chr(reset($bytes)); |
|
177 | - } |
|
178 | - } |
|
179 | - return $data; |
|
180 | - } |
|
153 | + /** |
|
154 | + * Encode sub ID's to DER. |
|
155 | + * |
|
156 | + * @param \GMP ...$subids |
|
157 | + * @return string |
|
158 | + */ |
|
159 | + protected static function _encodeSubIDs(\GMP ...$subids): string |
|
160 | + { |
|
161 | + $data = ""; |
|
162 | + foreach ($subids as $subid) { |
|
163 | + // if number fits to one base 128 byte |
|
164 | + if ($subid < 128) { |
|
165 | + $data .= chr(intval($subid)); |
|
166 | + } else { // encode to multiple bytes |
|
167 | + $bytes = []; |
|
168 | + do { |
|
169 | + array_unshift($bytes, 0x7f & gmp_intval($subid)); |
|
170 | + $subid >>= 7; |
|
171 | + } while ($subid > 0); |
|
172 | + // all bytes except last must have bit 8 set to one |
|
173 | + foreach (array_splice($bytes, 0, -1) as $byte) { |
|
174 | + $data .= chr(0x80 | $byte); |
|
175 | + } |
|
176 | + $data .= chr(reset($bytes)); |
|
177 | + } |
|
178 | + } |
|
179 | + return $data; |
|
180 | + } |
|
181 | 181 | |
182 | - /** |
|
183 | - * Decode sub ID's from DER data. |
|
184 | - * |
|
185 | - * @param string $data |
|
186 | - * @throws DecodeException |
|
187 | - * @return \GMP[] Array of GMP numbers |
|
188 | - */ |
|
189 | - protected static function _decodeSubIDs(string $data): array |
|
190 | - { |
|
191 | - $subids = []; |
|
192 | - $idx = 0; |
|
193 | - $end = strlen($data); |
|
194 | - while ($idx < $end) { |
|
195 | - $num = gmp_init("0", 10); |
|
196 | - while (true) { |
|
197 | - if ($idx >= $end) { |
|
198 | - throw new DecodeException("Unexpected end of data."); |
|
199 | - } |
|
200 | - $byte = ord($data[$idx++]); |
|
201 | - $num |= $byte & 0x7f; |
|
202 | - // bit 8 of the last octet is zero |
|
203 | - if (!($byte & 0x80)) { |
|
204 | - break; |
|
205 | - } |
|
206 | - $num <<= 7; |
|
207 | - } |
|
208 | - $subids[] = $num; |
|
209 | - } |
|
210 | - return $subids; |
|
211 | - } |
|
182 | + /** |
|
183 | + * Decode sub ID's from DER data. |
|
184 | + * |
|
185 | + * @param string $data |
|
186 | + * @throws DecodeException |
|
187 | + * @return \GMP[] Array of GMP numbers |
|
188 | + */ |
|
189 | + protected static function _decodeSubIDs(string $data): array |
|
190 | + { |
|
191 | + $subids = []; |
|
192 | + $idx = 0; |
|
193 | + $end = strlen($data); |
|
194 | + while ($idx < $end) { |
|
195 | + $num = gmp_init("0", 10); |
|
196 | + while (true) { |
|
197 | + if ($idx >= $end) { |
|
198 | + throw new DecodeException("Unexpected end of data."); |
|
199 | + } |
|
200 | + $byte = ord($data[$idx++]); |
|
201 | + $num |= $byte & 0x7f; |
|
202 | + // bit 8 of the last octet is zero |
|
203 | + if (!($byte & 0x80)) { |
|
204 | + break; |
|
205 | + } |
|
206 | + $num <<= 7; |
|
207 | + } |
|
208 | + $subids[] = $num; |
|
209 | + } |
|
210 | + return $subids; |
|
211 | + } |
|
212 | 212 | } |