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