@@ -17,194 +17,194 @@ |
||
17 | 17 | */ |
18 | 18 | class BitString extends BaseString |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Number of unused bits in the last octet. |
|
25 | - * |
|
26 | - * @var int |
|
27 | - */ |
|
28 | - protected $_unusedBits; |
|
23 | + /** |
|
24 | + * Number of unused bits in the last octet. |
|
25 | + * |
|
26 | + * @var int |
|
27 | + */ |
|
28 | + protected $_unusedBits; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Constructor. |
|
32 | - * |
|
33 | - * @param string $string Content octets |
|
34 | - * @param int $unused_bits Number of unused bits in the last octet |
|
35 | - */ |
|
36 | - public function __construct(string $string, int $unused_bits = 0) |
|
37 | - { |
|
38 | - $this->_typeTag = self::TYPE_BIT_STRING; |
|
39 | - parent::__construct($string); |
|
40 | - $this->_unusedBits = $unused_bits; |
|
41 | - } |
|
30 | + /** |
|
31 | + * Constructor. |
|
32 | + * |
|
33 | + * @param string $string Content octets |
|
34 | + * @param int $unused_bits Number of unused bits in the last octet |
|
35 | + */ |
|
36 | + public function __construct(string $string, int $unused_bits = 0) |
|
37 | + { |
|
38 | + $this->_typeTag = self::TYPE_BIT_STRING; |
|
39 | + parent::__construct($string); |
|
40 | + $this->_unusedBits = $unused_bits; |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Get the number of bits in the string. |
|
45 | - * |
|
46 | - * @return int |
|
47 | - */ |
|
48 | - public function numBits(): int |
|
49 | - { |
|
50 | - return strlen($this->_string) * 8 - $this->_unusedBits; |
|
51 | - } |
|
43 | + /** |
|
44 | + * Get the number of bits in the string. |
|
45 | + * |
|
46 | + * @return int |
|
47 | + */ |
|
48 | + public function numBits(): int |
|
49 | + { |
|
50 | + return strlen($this->_string) * 8 - $this->_unusedBits; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Get the number of unused bits in the last octet of the string. |
|
55 | - * |
|
56 | - * @return int |
|
57 | - */ |
|
58 | - public function unusedBits(): int |
|
59 | - { |
|
60 | - return $this->_unusedBits; |
|
61 | - } |
|
53 | + /** |
|
54 | + * Get the number of unused bits in the last octet of the string. |
|
55 | + * |
|
56 | + * @return int |
|
57 | + */ |
|
58 | + public function unusedBits(): int |
|
59 | + { |
|
60 | + return $this->_unusedBits; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Test whether bit is set. |
|
65 | - * |
|
66 | - * @param int $idx Bit index. Most significant bit of the first octet is index 0. |
|
67 | - * |
|
68 | - * @return bool |
|
69 | - */ |
|
70 | - public function testBit(int $idx): bool |
|
71 | - { |
|
72 | - // octet index |
|
73 | - $oi = (int) floor($idx / 8); |
|
74 | - // if octet is outside range |
|
75 | - if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
76 | - throw new \OutOfBoundsException('Index is out of bounds.'); |
|
77 | - } |
|
78 | - // bit index |
|
79 | - $bi = $idx % 8; |
|
80 | - // if tested bit is last octet's unused bit |
|
81 | - if ($oi === strlen($this->_string) - 1) { |
|
82 | - if ($bi >= 8 - $this->_unusedBits) { |
|
83 | - throw new \OutOfBoundsException( |
|
84 | - 'Index refers to an unused bit.'); |
|
85 | - } |
|
86 | - } |
|
87 | - $byte = $this->_string[$oi]; |
|
88 | - // index 0 is the most significant bit in byte |
|
89 | - $mask = 0x01 << (7 - $bi); |
|
90 | - return (ord($byte) & $mask) > 0; |
|
91 | - } |
|
63 | + /** |
|
64 | + * Test whether bit is set. |
|
65 | + * |
|
66 | + * @param int $idx Bit index. Most significant bit of the first octet is index 0. |
|
67 | + * |
|
68 | + * @return bool |
|
69 | + */ |
|
70 | + public function testBit(int $idx): bool |
|
71 | + { |
|
72 | + // octet index |
|
73 | + $oi = (int) floor($idx / 8); |
|
74 | + // if octet is outside range |
|
75 | + if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
76 | + throw new \OutOfBoundsException('Index is out of bounds.'); |
|
77 | + } |
|
78 | + // bit index |
|
79 | + $bi = $idx % 8; |
|
80 | + // if tested bit is last octet's unused bit |
|
81 | + if ($oi === strlen($this->_string) - 1) { |
|
82 | + if ($bi >= 8 - $this->_unusedBits) { |
|
83 | + throw new \OutOfBoundsException( |
|
84 | + 'Index refers to an unused bit.'); |
|
85 | + } |
|
86 | + } |
|
87 | + $byte = $this->_string[$oi]; |
|
88 | + // index 0 is the most significant bit in byte |
|
89 | + $mask = 0x01 << (7 - $bi); |
|
90 | + return (ord($byte) & $mask) > 0; |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Get range of bits. |
|
95 | - * |
|
96 | - * @param int $start Index of first bit |
|
97 | - * @param int $length Number of bits in range |
|
98 | - * |
|
99 | - * @throws \OutOfBoundsException |
|
100 | - * |
|
101 | - * @return string Integer of $length bits |
|
102 | - */ |
|
103 | - public function range(int $start, int $length): string |
|
104 | - { |
|
105 | - if (!$length) { |
|
106 | - return '0'; |
|
107 | - } |
|
108 | - if ($start + $length > $this->numBits()) { |
|
109 | - throw new \OutOfBoundsException('Not enough bits.'); |
|
110 | - } |
|
111 | - $bits = gmp_init(0); |
|
112 | - $idx = $start; |
|
113 | - $end = $start + $length; |
|
114 | - while (true) { |
|
115 | - $bit = $this->testBit($idx) ? 1 : 0; |
|
116 | - $bits |= $bit; |
|
117 | - if (++$idx >= $end) { |
|
118 | - break; |
|
119 | - } |
|
120 | - $bits <<= 1; |
|
121 | - } |
|
122 | - return gmp_strval($bits, 10); |
|
123 | - } |
|
93 | + /** |
|
94 | + * Get range of bits. |
|
95 | + * |
|
96 | + * @param int $start Index of first bit |
|
97 | + * @param int $length Number of bits in range |
|
98 | + * |
|
99 | + * @throws \OutOfBoundsException |
|
100 | + * |
|
101 | + * @return string Integer of $length bits |
|
102 | + */ |
|
103 | + public function range(int $start, int $length): string |
|
104 | + { |
|
105 | + if (!$length) { |
|
106 | + return '0'; |
|
107 | + } |
|
108 | + if ($start + $length > $this->numBits()) { |
|
109 | + throw new \OutOfBoundsException('Not enough bits.'); |
|
110 | + } |
|
111 | + $bits = gmp_init(0); |
|
112 | + $idx = $start; |
|
113 | + $end = $start + $length; |
|
114 | + while (true) { |
|
115 | + $bit = $this->testBit($idx) ? 1 : 0; |
|
116 | + $bits |= $bit; |
|
117 | + if (++$idx >= $end) { |
|
118 | + break; |
|
119 | + } |
|
120 | + $bits <<= 1; |
|
121 | + } |
|
122 | + return gmp_strval($bits, 10); |
|
123 | + } |
|
124 | 124 | |
125 | - /** |
|
126 | - * Get a copy of the bit string with trailing zeroes removed. |
|
127 | - * |
|
128 | - * @return self |
|
129 | - */ |
|
130 | - public function withoutTrailingZeroes(): self |
|
131 | - { |
|
132 | - // if bit string was empty |
|
133 | - if (!strlen($this->_string)) { |
|
134 | - return new self(''); |
|
135 | - } |
|
136 | - $bits = $this->_string; |
|
137 | - // count number of empty trailing octets |
|
138 | - $unused_octets = 0; |
|
139 | - for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
140 | - if ("\x0" !== $bits[$idx]) { |
|
141 | - break; |
|
142 | - } |
|
143 | - } |
|
144 | - // strip trailing octets |
|
145 | - if ($unused_octets) { |
|
146 | - $bits = substr($bits, 0, -$unused_octets); |
|
147 | - } |
|
148 | - // if bit string was full of zeroes |
|
149 | - if (!strlen($bits)) { |
|
150 | - return new self(''); |
|
151 | - } |
|
152 | - // count number of trailing zeroes in the last octet |
|
153 | - $unused_bits = 0; |
|
154 | - $byte = ord($bits[strlen($bits) - 1]); |
|
155 | - while (!($byte & 0x01)) { |
|
156 | - ++$unused_bits; |
|
157 | - $byte >>= 1; |
|
158 | - } |
|
159 | - return new self($bits, $unused_bits); |
|
160 | - } |
|
125 | + /** |
|
126 | + * Get a copy of the bit string with trailing zeroes removed. |
|
127 | + * |
|
128 | + * @return self |
|
129 | + */ |
|
130 | + public function withoutTrailingZeroes(): self |
|
131 | + { |
|
132 | + // if bit string was empty |
|
133 | + if (!strlen($this->_string)) { |
|
134 | + return new self(''); |
|
135 | + } |
|
136 | + $bits = $this->_string; |
|
137 | + // count number of empty trailing octets |
|
138 | + $unused_octets = 0; |
|
139 | + for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
140 | + if ("\x0" !== $bits[$idx]) { |
|
141 | + break; |
|
142 | + } |
|
143 | + } |
|
144 | + // strip trailing octets |
|
145 | + if ($unused_octets) { |
|
146 | + $bits = substr($bits, 0, -$unused_octets); |
|
147 | + } |
|
148 | + // if bit string was full of zeroes |
|
149 | + if (!strlen($bits)) { |
|
150 | + return new self(''); |
|
151 | + } |
|
152 | + // count number of trailing zeroes in the last octet |
|
153 | + $unused_bits = 0; |
|
154 | + $byte = ord($bits[strlen($bits) - 1]); |
|
155 | + while (!($byte & 0x01)) { |
|
156 | + ++$unused_bits; |
|
157 | + $byte >>= 1; |
|
158 | + } |
|
159 | + return new self($bits, $unused_bits); |
|
160 | + } |
|
161 | 161 | |
162 | - /** |
|
163 | - * {@inheritdoc} |
|
164 | - */ |
|
165 | - protected function _encodedContentDER(): string |
|
166 | - { |
|
167 | - $der = chr($this->_unusedBits); |
|
168 | - $der .= $this->_string; |
|
169 | - if ($this->_unusedBits) { |
|
170 | - $octet = $der[strlen($der) - 1]; |
|
171 | - // set unused bits to zero |
|
172 | - $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
173 | - $der[strlen($der) - 1] = $octet; |
|
174 | - } |
|
175 | - return $der; |
|
176 | - } |
|
162 | + /** |
|
163 | + * {@inheritdoc} |
|
164 | + */ |
|
165 | + protected function _encodedContentDER(): string |
|
166 | + { |
|
167 | + $der = chr($this->_unusedBits); |
|
168 | + $der .= $this->_string; |
|
169 | + if ($this->_unusedBits) { |
|
170 | + $octet = $der[strlen($der) - 1]; |
|
171 | + // set unused bits to zero |
|
172 | + $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
173 | + $der[strlen($der) - 1] = $octet; |
|
174 | + } |
|
175 | + return $der; |
|
176 | + } |
|
177 | 177 | |
178 | - /** |
|
179 | - * {@inheritdoc} |
|
180 | - */ |
|
181 | - protected static function _decodeFromDER(Identifier $identifier, |
|
182 | - string $data, int &$offset): ElementBase |
|
183 | - { |
|
184 | - $idx = $offset; |
|
185 | - $length = Length::expectFromDER($data, $idx); |
|
186 | - if ($length->intLength() < 1) { |
|
187 | - throw new DecodeException('Bit string length must be at least 1.'); |
|
188 | - } |
|
189 | - $unused_bits = ord($data[$idx++]); |
|
190 | - if ($unused_bits > 7) { |
|
191 | - throw new DecodeException( |
|
192 | - 'Unused bits in a bit string must be less than 8.'); |
|
193 | - } |
|
194 | - $str_len = $length->intLength() - 1; |
|
195 | - if ($str_len) { |
|
196 | - $str = substr($data, $idx, $str_len); |
|
197 | - if ($unused_bits) { |
|
198 | - $mask = (1 << $unused_bits) - 1; |
|
199 | - if (ord($str[strlen($str) - 1]) & $mask) { |
|
200 | - throw new DecodeException( |
|
201 | - 'DER encoded bit string must have zero padding.'); |
|
202 | - } |
|
203 | - } |
|
204 | - } else { |
|
205 | - $str = ''; |
|
206 | - } |
|
207 | - $offset = $idx + $str_len; |
|
208 | - return new self($str, $unused_bits); |
|
209 | - } |
|
178 | + /** |
|
179 | + * {@inheritdoc} |
|
180 | + */ |
|
181 | + protected static function _decodeFromDER(Identifier $identifier, |
|
182 | + string $data, int &$offset): ElementBase |
|
183 | + { |
|
184 | + $idx = $offset; |
|
185 | + $length = Length::expectFromDER($data, $idx); |
|
186 | + if ($length->intLength() < 1) { |
|
187 | + throw new DecodeException('Bit string length must be at least 1.'); |
|
188 | + } |
|
189 | + $unused_bits = ord($data[$idx++]); |
|
190 | + if ($unused_bits > 7) { |
|
191 | + throw new DecodeException( |
|
192 | + 'Unused bits in a bit string must be less than 8.'); |
|
193 | + } |
|
194 | + $str_len = $length->intLength() - 1; |
|
195 | + if ($str_len) { |
|
196 | + $str = substr($data, $idx, $str_len); |
|
197 | + if ($unused_bits) { |
|
198 | + $mask = (1 << $unused_bits) - 1; |
|
199 | + if (ord($str[strlen($str) - 1]) & $mask) { |
|
200 | + throw new DecodeException( |
|
201 | + 'DER encoded bit string must have zero padding.'); |
|
202 | + } |
|
203 | + } |
|
204 | + } else { |
|
205 | + $str = ''; |
|
206 | + } |
|
207 | + $offset = $idx + $str_len; |
|
208 | + return new self($str, $unused_bits); |
|
209 | + } |
|
210 | 210 | } |
@@ -17,111 +17,111 @@ |
||
17 | 17 | */ |
18 | 18 | class GeneralizedTime extends BaseTime |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Regular expression to parse date. |
|
25 | - * |
|
26 | - * DER restricts format to UTC timezone (Z suffix). |
|
27 | - * |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - const REGEX = '#^' . |
|
31 | - '(\d\d\d\d)' . // YYYY |
|
32 | - '(\d\d)' . // MM |
|
33 | - '(\d\d)' . // DD |
|
34 | - '(\d\d)' . // hh |
|
35 | - '(\d\d)' . // mm |
|
36 | - '(\d\d)' . // ss |
|
37 | - '(?:\.(\d+))?' . // frac |
|
38 | - 'Z' . // TZ |
|
39 | - '$#'; |
|
23 | + /** |
|
24 | + * Regular expression to parse date. |
|
25 | + * |
|
26 | + * DER restricts format to UTC timezone (Z suffix). |
|
27 | + * |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + const REGEX = '#^' . |
|
31 | + '(\d\d\d\d)' . // YYYY |
|
32 | + '(\d\d)' . // MM |
|
33 | + '(\d\d)' . // DD |
|
34 | + '(\d\d)' . // hh |
|
35 | + '(\d\d)' . // mm |
|
36 | + '(\d\d)' . // ss |
|
37 | + '(?:\.(\d+))?' . // frac |
|
38 | + 'Z' . // TZ |
|
39 | + '$#'; |
|
40 | 40 | |
41 | - /** |
|
42 | - * Cached formatted date. |
|
43 | - * |
|
44 | - * @var null|string |
|
45 | - */ |
|
46 | - private $_formatted; |
|
41 | + /** |
|
42 | + * Cached formatted date. |
|
43 | + * |
|
44 | + * @var null|string |
|
45 | + */ |
|
46 | + private $_formatted; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Constructor. |
|
50 | - * |
|
51 | - * @param \DateTimeImmutable $dt |
|
52 | - */ |
|
53 | - public function __construct(\DateTimeImmutable $dt) |
|
54 | - { |
|
55 | - $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
56 | - parent::__construct($dt); |
|
57 | - } |
|
48 | + /** |
|
49 | + * Constructor. |
|
50 | + * |
|
51 | + * @param \DateTimeImmutable $dt |
|
52 | + */ |
|
53 | + public function __construct(\DateTimeImmutable $dt) |
|
54 | + { |
|
55 | + $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
56 | + parent::__construct($dt); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Clear cached variables on clone. |
|
61 | - */ |
|
62 | - public function __clone() |
|
63 | - { |
|
64 | - $this->_formatted = null; |
|
65 | - } |
|
59 | + /** |
|
60 | + * Clear cached variables on clone. |
|
61 | + */ |
|
62 | + public function __clone() |
|
63 | + { |
|
64 | + $this->_formatted = null; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * {@inheritdoc} |
|
69 | - */ |
|
70 | - protected function _encodedContentDER(): string |
|
71 | - { |
|
72 | - if (!isset($this->_formatted)) { |
|
73 | - $dt = $this->_dateTime->setTimezone( |
|
74 | - self::_createTimeZone(self::TZ_UTC)); |
|
75 | - $this->_formatted = $dt->format('YmdHis'); |
|
76 | - // if fractions were used |
|
77 | - $frac = $dt->format('u'); |
|
78 | - if (0 !== intval($frac)) { |
|
79 | - $frac = rtrim($frac, '0'); |
|
80 | - $this->_formatted .= ".{$frac}"; |
|
81 | - } |
|
82 | - // timezone |
|
83 | - $this->_formatted .= 'Z'; |
|
84 | - } |
|
85 | - return $this->_formatted; |
|
86 | - } |
|
67 | + /** |
|
68 | + * {@inheritdoc} |
|
69 | + */ |
|
70 | + protected function _encodedContentDER(): string |
|
71 | + { |
|
72 | + if (!isset($this->_formatted)) { |
|
73 | + $dt = $this->_dateTime->setTimezone( |
|
74 | + self::_createTimeZone(self::TZ_UTC)); |
|
75 | + $this->_formatted = $dt->format('YmdHis'); |
|
76 | + // if fractions were used |
|
77 | + $frac = $dt->format('u'); |
|
78 | + if (0 !== intval($frac)) { |
|
79 | + $frac = rtrim($frac, '0'); |
|
80 | + $this->_formatted .= ".{$frac}"; |
|
81 | + } |
|
82 | + // timezone |
|
83 | + $this->_formatted .= 'Z'; |
|
84 | + } |
|
85 | + return $this->_formatted; |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * {@inheritdoc} |
|
90 | - */ |
|
91 | - protected static function _decodeFromDER(Identifier $identifier, |
|
92 | - string $data, int &$offset): ElementBase |
|
93 | - { |
|
94 | - $idx = $offset; |
|
95 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
96 | - $str = substr($data, $idx, $length); |
|
97 | - $idx += $length; |
|
98 | - /** @var string[] $match */ |
|
99 | - if (!preg_match(self::REGEX, $str, $match)) { |
|
100 | - throw new DecodeException('Invalid GeneralizedTime format.'); |
|
101 | - } |
|
102 | - [, $year, $month, $day, $hour, $minute, $second] = $match; |
|
103 | - // if fractions match, there's at least one digit |
|
104 | - if (isset($match[7])) { |
|
105 | - $frac = $match[7]; |
|
106 | - // DER restricts trailing zeroes in fractional seconds component |
|
107 | - if ('0' === $frac[strlen($frac) - 1]) { |
|
108 | - throw new DecodeException( |
|
109 | - 'Fractional seconds must omit trailing zeroes.'); |
|
110 | - } |
|
111 | - $frac = $frac; |
|
112 | - } else { |
|
113 | - $frac = '0'; |
|
114 | - } |
|
115 | - $time = $year . $month . $day . $hour . $minute . $second . '.' . $frac . |
|
116 | - self::TZ_UTC; |
|
117 | - $dt = \DateTimeImmutable::createFromFormat('!YmdHis.uT', $time, |
|
118 | - self::_createTimeZone(self::TZ_UTC)); |
|
119 | - if (!$dt) { |
|
120 | - throw new DecodeException( |
|
121 | - 'Failed to decode GeneralizedTime: ' . |
|
122 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
123 | - } |
|
124 | - $offset = $idx; |
|
125 | - return new self($dt); |
|
126 | - } |
|
88 | + /** |
|
89 | + * {@inheritdoc} |
|
90 | + */ |
|
91 | + protected static function _decodeFromDER(Identifier $identifier, |
|
92 | + string $data, int &$offset): ElementBase |
|
93 | + { |
|
94 | + $idx = $offset; |
|
95 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
96 | + $str = substr($data, $idx, $length); |
|
97 | + $idx += $length; |
|
98 | + /** @var string[] $match */ |
|
99 | + if (!preg_match(self::REGEX, $str, $match)) { |
|
100 | + throw new DecodeException('Invalid GeneralizedTime format.'); |
|
101 | + } |
|
102 | + [, $year, $month, $day, $hour, $minute, $second] = $match; |
|
103 | + // if fractions match, there's at least one digit |
|
104 | + if (isset($match[7])) { |
|
105 | + $frac = $match[7]; |
|
106 | + // DER restricts trailing zeroes in fractional seconds component |
|
107 | + if ('0' === $frac[strlen($frac) - 1]) { |
|
108 | + throw new DecodeException( |
|
109 | + 'Fractional seconds must omit trailing zeroes.'); |
|
110 | + } |
|
111 | + $frac = $frac; |
|
112 | + } else { |
|
113 | + $frac = '0'; |
|
114 | + } |
|
115 | + $time = $year . $month . $day . $hour . $minute . $second . '.' . $frac . |
|
116 | + self::TZ_UTC; |
|
117 | + $dt = \DateTimeImmutable::createFromFormat('!YmdHis.uT', $time, |
|
118 | + self::_createTimeZone(self::TZ_UTC)); |
|
119 | + if (!$dt) { |
|
120 | + throw new DecodeException( |
|
121 | + 'Failed to decode GeneralizedTime: ' . |
|
122 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
123 | + } |
|
124 | + $offset = $idx; |
|
125 | + return new self($dt); |
|
126 | + } |
|
127 | 127 | } |
@@ -15,400 +15,400 @@ |
||
15 | 15 | */ |
16 | 16 | abstract class Structure extends Element implements \Countable, \IteratorAggregate |
17 | 17 | { |
18 | - use UniversalClass; |
|
18 | + use UniversalClass; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Array of elements in the structure. |
|
22 | - * |
|
23 | - * @var Element[] |
|
24 | - */ |
|
25 | - protected $_elements; |
|
20 | + /** |
|
21 | + * Array of elements in the structure. |
|
22 | + * |
|
23 | + * @var Element[] |
|
24 | + */ |
|
25 | + protected $_elements; |
|
26 | 26 | |
27 | - /** |
|
28 | - * Lookup table for the tagged elements. |
|
29 | - * |
|
30 | - * @var null|TaggedType[] |
|
31 | - */ |
|
32 | - private $_taggedMap; |
|
27 | + /** |
|
28 | + * Lookup table for the tagged elements. |
|
29 | + * |
|
30 | + * @var null|TaggedType[] |
|
31 | + */ |
|
32 | + private $_taggedMap; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Cache variable of elements wrapped into `UnspecifiedType` objects. |
|
36 | - * |
|
37 | - * @var null|UnspecifiedType[] |
|
38 | - */ |
|
39 | - private $_unspecifiedTypes; |
|
34 | + /** |
|
35 | + * Cache variable of elements wrapped into `UnspecifiedType` objects. |
|
36 | + * |
|
37 | + * @var null|UnspecifiedType[] |
|
38 | + */ |
|
39 | + private $_unspecifiedTypes; |
|
40 | 40 | |
41 | - /** |
|
42 | - * Constructor. |
|
43 | - * |
|
44 | - * @param ElementBase ...$elements Any number of elements |
|
45 | - */ |
|
46 | - public function __construct(ElementBase ...$elements) |
|
47 | - { |
|
48 | - $this->_elements = array_map( |
|
49 | - function (ElementBase $el) { |
|
50 | - return $el->asElement(); |
|
51 | - }, $elements); |
|
52 | - } |
|
41 | + /** |
|
42 | + * Constructor. |
|
43 | + * |
|
44 | + * @param ElementBase ...$elements Any number of elements |
|
45 | + */ |
|
46 | + public function __construct(ElementBase ...$elements) |
|
47 | + { |
|
48 | + $this->_elements = array_map( |
|
49 | + function (ElementBase $el) { |
|
50 | + return $el->asElement(); |
|
51 | + }, $elements); |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Clone magic method. |
|
56 | - */ |
|
57 | - public function __clone() |
|
58 | - { |
|
59 | - // clear cache-variables |
|
60 | - $this->_taggedMap = null; |
|
61 | - $this->_unspecifiedTypes = null; |
|
62 | - } |
|
54 | + /** |
|
55 | + * Clone magic method. |
|
56 | + */ |
|
57 | + public function __clone() |
|
58 | + { |
|
59 | + // clear cache-variables |
|
60 | + $this->_taggedMap = null; |
|
61 | + $this->_unspecifiedTypes = null; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * {@inheritdoc} |
|
66 | - */ |
|
67 | - public function isConstructed(): bool |
|
68 | - { |
|
69 | - return true; |
|
70 | - } |
|
64 | + /** |
|
65 | + * {@inheritdoc} |
|
66 | + */ |
|
67 | + public function isConstructed(): bool |
|
68 | + { |
|
69 | + return true; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Explode DER structure to DER encoded components that it contains. |
|
74 | - * |
|
75 | - * @param string $data |
|
76 | - * |
|
77 | - * @throws DecodeException |
|
78 | - * |
|
79 | - * @return string[] |
|
80 | - */ |
|
81 | - public static function explodeDER(string $data): array |
|
82 | - { |
|
83 | - $offset = 0; |
|
84 | - $identifier = Identifier::fromDER($data, $offset); |
|
85 | - if (!$identifier->isConstructed()) { |
|
86 | - throw new DecodeException('Element is not constructed.'); |
|
87 | - } |
|
88 | - $length = Length::expectFromDER($data, $offset); |
|
89 | - if ($length->isIndefinite()) { |
|
90 | - throw new DecodeException( |
|
91 | - 'Explode not implemented for indefinite length encoding.'); |
|
92 | - } |
|
93 | - $end = $offset + $length->intLength(); |
|
94 | - $parts = []; |
|
95 | - while ($offset < $end) { |
|
96 | - // start of the element |
|
97 | - $idx = $offset; |
|
98 | - // skip identifier |
|
99 | - Identifier::fromDER($data, $offset); |
|
100 | - // decode element length |
|
101 | - $length = Length::expectFromDER($data, $offset)->intLength(); |
|
102 | - // extract der encoding of the element |
|
103 | - $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
104 | - // update offset over content |
|
105 | - $offset += $length; |
|
106 | - } |
|
107 | - return $parts; |
|
108 | - } |
|
72 | + /** |
|
73 | + * Explode DER structure to DER encoded components that it contains. |
|
74 | + * |
|
75 | + * @param string $data |
|
76 | + * |
|
77 | + * @throws DecodeException |
|
78 | + * |
|
79 | + * @return string[] |
|
80 | + */ |
|
81 | + public static function explodeDER(string $data): array |
|
82 | + { |
|
83 | + $offset = 0; |
|
84 | + $identifier = Identifier::fromDER($data, $offset); |
|
85 | + if (!$identifier->isConstructed()) { |
|
86 | + throw new DecodeException('Element is not constructed.'); |
|
87 | + } |
|
88 | + $length = Length::expectFromDER($data, $offset); |
|
89 | + if ($length->isIndefinite()) { |
|
90 | + throw new DecodeException( |
|
91 | + 'Explode not implemented for indefinite length encoding.'); |
|
92 | + } |
|
93 | + $end = $offset + $length->intLength(); |
|
94 | + $parts = []; |
|
95 | + while ($offset < $end) { |
|
96 | + // start of the element |
|
97 | + $idx = $offset; |
|
98 | + // skip identifier |
|
99 | + Identifier::fromDER($data, $offset); |
|
100 | + // decode element length |
|
101 | + $length = Length::expectFromDER($data, $offset)->intLength(); |
|
102 | + // extract der encoding of the element |
|
103 | + $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
104 | + // update offset over content |
|
105 | + $offset += $length; |
|
106 | + } |
|
107 | + return $parts; |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * Get self with an element at the given index replaced by another. |
|
112 | - * |
|
113 | - * @param int $idx Element index |
|
114 | - * @param Element $el New element to insert into the structure |
|
115 | - * |
|
116 | - * @throws \OutOfBoundsException |
|
117 | - * |
|
118 | - * @return self |
|
119 | - */ |
|
120 | - public function withReplaced(int $idx, Element $el): self |
|
121 | - { |
|
122 | - if (!isset($this->_elements[$idx])) { |
|
123 | - throw new \OutOfBoundsException( |
|
124 | - "Structure doesn't have element at index {$idx}."); |
|
125 | - } |
|
126 | - $obj = clone $this; |
|
127 | - $obj->_elements[$idx] = $el; |
|
128 | - return $obj; |
|
129 | - } |
|
110 | + /** |
|
111 | + * Get self with an element at the given index replaced by another. |
|
112 | + * |
|
113 | + * @param int $idx Element index |
|
114 | + * @param Element $el New element to insert into the structure |
|
115 | + * |
|
116 | + * @throws \OutOfBoundsException |
|
117 | + * |
|
118 | + * @return self |
|
119 | + */ |
|
120 | + public function withReplaced(int $idx, Element $el): self |
|
121 | + { |
|
122 | + if (!isset($this->_elements[$idx])) { |
|
123 | + throw new \OutOfBoundsException( |
|
124 | + "Structure doesn't have element at index {$idx}."); |
|
125 | + } |
|
126 | + $obj = clone $this; |
|
127 | + $obj->_elements[$idx] = $el; |
|
128 | + return $obj; |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * Get self with an element inserted before the given index. |
|
133 | - * |
|
134 | - * @param int $idx Element index |
|
135 | - * @param Element $el New element to insert into the structure |
|
136 | - * |
|
137 | - * @throws \OutOfBoundsException |
|
138 | - * |
|
139 | - * @return self |
|
140 | - */ |
|
141 | - public function withInserted(int $idx, Element $el): self |
|
142 | - { |
|
143 | - if (count($this->_elements) < $idx || $idx < 0) { |
|
144 | - throw new \OutOfBoundsException("Index {$idx} is out of bounds."); |
|
145 | - } |
|
146 | - $obj = clone $this; |
|
147 | - array_splice($obj->_elements, $idx, 0, [$el]); |
|
148 | - return $obj; |
|
149 | - } |
|
131 | + /** |
|
132 | + * Get self with an element inserted before the given index. |
|
133 | + * |
|
134 | + * @param int $idx Element index |
|
135 | + * @param Element $el New element to insert into the structure |
|
136 | + * |
|
137 | + * @throws \OutOfBoundsException |
|
138 | + * |
|
139 | + * @return self |
|
140 | + */ |
|
141 | + public function withInserted(int $idx, Element $el): self |
|
142 | + { |
|
143 | + if (count($this->_elements) < $idx || $idx < 0) { |
|
144 | + throw new \OutOfBoundsException("Index {$idx} is out of bounds."); |
|
145 | + } |
|
146 | + $obj = clone $this; |
|
147 | + array_splice($obj->_elements, $idx, 0, [$el]); |
|
148 | + return $obj; |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * Get self with an element appended to the end. |
|
153 | - * |
|
154 | - * @param Element $el Element to insert into the structure |
|
155 | - * |
|
156 | - * @return self |
|
157 | - */ |
|
158 | - public function withAppended(Element $el): self |
|
159 | - { |
|
160 | - $obj = clone $this; |
|
161 | - array_push($obj->_elements, $el); |
|
162 | - return $obj; |
|
163 | - } |
|
151 | + /** |
|
152 | + * Get self with an element appended to the end. |
|
153 | + * |
|
154 | + * @param Element $el Element to insert into the structure |
|
155 | + * |
|
156 | + * @return self |
|
157 | + */ |
|
158 | + public function withAppended(Element $el): self |
|
159 | + { |
|
160 | + $obj = clone $this; |
|
161 | + array_push($obj->_elements, $el); |
|
162 | + return $obj; |
|
163 | + } |
|
164 | 164 | |
165 | - /** |
|
166 | - * Get self with an element prepended in the beginning. |
|
167 | - * |
|
168 | - * @param Element $el Element to insert into the structure |
|
169 | - * |
|
170 | - * @return self |
|
171 | - */ |
|
172 | - public function withPrepended(Element $el): self |
|
173 | - { |
|
174 | - $obj = clone $this; |
|
175 | - array_unshift($obj->_elements, $el); |
|
176 | - return $obj; |
|
177 | - } |
|
165 | + /** |
|
166 | + * Get self with an element prepended in the beginning. |
|
167 | + * |
|
168 | + * @param Element $el Element to insert into the structure |
|
169 | + * |
|
170 | + * @return self |
|
171 | + */ |
|
172 | + public function withPrepended(Element $el): self |
|
173 | + { |
|
174 | + $obj = clone $this; |
|
175 | + array_unshift($obj->_elements, $el); |
|
176 | + return $obj; |
|
177 | + } |
|
178 | 178 | |
179 | - /** |
|
180 | - * Get self with an element at the given index removed. |
|
181 | - * |
|
182 | - * @param int $idx Element index |
|
183 | - * |
|
184 | - * @throws \OutOfBoundsException |
|
185 | - * |
|
186 | - * @return self |
|
187 | - */ |
|
188 | - public function withoutElement(int $idx): self |
|
189 | - { |
|
190 | - if (!isset($this->_elements[$idx])) { |
|
191 | - throw new \OutOfBoundsException( |
|
192 | - "Structure doesn't have element at index {$idx}."); |
|
193 | - } |
|
194 | - $obj = clone $this; |
|
195 | - array_splice($obj->_elements, $idx, 1); |
|
196 | - return $obj; |
|
197 | - } |
|
179 | + /** |
|
180 | + * Get self with an element at the given index removed. |
|
181 | + * |
|
182 | + * @param int $idx Element index |
|
183 | + * |
|
184 | + * @throws \OutOfBoundsException |
|
185 | + * |
|
186 | + * @return self |
|
187 | + */ |
|
188 | + public function withoutElement(int $idx): self |
|
189 | + { |
|
190 | + if (!isset($this->_elements[$idx])) { |
|
191 | + throw new \OutOfBoundsException( |
|
192 | + "Structure doesn't have element at index {$idx}."); |
|
193 | + } |
|
194 | + $obj = clone $this; |
|
195 | + array_splice($obj->_elements, $idx, 1); |
|
196 | + return $obj; |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * Get elements in the structure. |
|
201 | - * |
|
202 | - * @return UnspecifiedType[] |
|
203 | - */ |
|
204 | - public function elements(): array |
|
205 | - { |
|
206 | - if (!isset($this->_unspecifiedTypes)) { |
|
207 | - $this->_unspecifiedTypes = array_map( |
|
208 | - function (Element $el) { |
|
209 | - return new UnspecifiedType($el); |
|
210 | - }, $this->_elements); |
|
211 | - } |
|
212 | - return $this->_unspecifiedTypes; |
|
213 | - } |
|
199 | + /** |
|
200 | + * Get elements in the structure. |
|
201 | + * |
|
202 | + * @return UnspecifiedType[] |
|
203 | + */ |
|
204 | + public function elements(): array |
|
205 | + { |
|
206 | + if (!isset($this->_unspecifiedTypes)) { |
|
207 | + $this->_unspecifiedTypes = array_map( |
|
208 | + function (Element $el) { |
|
209 | + return new UnspecifiedType($el); |
|
210 | + }, $this->_elements); |
|
211 | + } |
|
212 | + return $this->_unspecifiedTypes; |
|
213 | + } |
|
214 | 214 | |
215 | - /** |
|
216 | - * Check whether the structure has an element at the given index, optionally |
|
217 | - * satisfying given tag expectation. |
|
218 | - * |
|
219 | - * @param int $idx Index 0..n |
|
220 | - * @param null|int $expectedTag Optional type tag expectation |
|
221 | - * |
|
222 | - * @return bool |
|
223 | - */ |
|
224 | - public function has(int $idx, ?int $expectedTag = null): bool |
|
225 | - { |
|
226 | - if (!isset($this->_elements[$idx])) { |
|
227 | - return false; |
|
228 | - } |
|
229 | - if (isset($expectedTag)) { |
|
230 | - if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
231 | - return false; |
|
232 | - } |
|
233 | - } |
|
234 | - return true; |
|
235 | - } |
|
215 | + /** |
|
216 | + * Check whether the structure has an element at the given index, optionally |
|
217 | + * satisfying given tag expectation. |
|
218 | + * |
|
219 | + * @param int $idx Index 0..n |
|
220 | + * @param null|int $expectedTag Optional type tag expectation |
|
221 | + * |
|
222 | + * @return bool |
|
223 | + */ |
|
224 | + public function has(int $idx, ?int $expectedTag = null): bool |
|
225 | + { |
|
226 | + if (!isset($this->_elements[$idx])) { |
|
227 | + return false; |
|
228 | + } |
|
229 | + if (isset($expectedTag)) { |
|
230 | + if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
231 | + return false; |
|
232 | + } |
|
233 | + } |
|
234 | + return true; |
|
235 | + } |
|
236 | 236 | |
237 | - /** |
|
238 | - * Get the element at the given index, optionally checking that the element |
|
239 | - * has a given tag. |
|
240 | - * |
|
241 | - * @param int $idx Index 0..n |
|
242 | - * |
|
243 | - * @throws \OutOfBoundsException If element doesn't exists |
|
244 | - * @throws \UnexpectedValueException If expectation fails |
|
245 | - * |
|
246 | - * @return UnspecifiedType |
|
247 | - */ |
|
248 | - public function at(int $idx): UnspecifiedType |
|
249 | - { |
|
250 | - if (!isset($this->_elements[$idx])) { |
|
251 | - throw new \OutOfBoundsException( |
|
252 | - "Structure doesn't have an element at index {$idx}."); |
|
253 | - } |
|
254 | - return new UnspecifiedType($this->_elements[$idx]); |
|
255 | - } |
|
237 | + /** |
|
238 | + * Get the element at the given index, optionally checking that the element |
|
239 | + * has a given tag. |
|
240 | + * |
|
241 | + * @param int $idx Index 0..n |
|
242 | + * |
|
243 | + * @throws \OutOfBoundsException If element doesn't exists |
|
244 | + * @throws \UnexpectedValueException If expectation fails |
|
245 | + * |
|
246 | + * @return UnspecifiedType |
|
247 | + */ |
|
248 | + public function at(int $idx): UnspecifiedType |
|
249 | + { |
|
250 | + if (!isset($this->_elements[$idx])) { |
|
251 | + throw new \OutOfBoundsException( |
|
252 | + "Structure doesn't have an element at index {$idx}."); |
|
253 | + } |
|
254 | + return new UnspecifiedType($this->_elements[$idx]); |
|
255 | + } |
|
256 | 256 | |
257 | - /** |
|
258 | - * Check whether the structure contains a context specific element with a |
|
259 | - * given tag. |
|
260 | - * |
|
261 | - * @param int $tag Tag number |
|
262 | - * |
|
263 | - * @return bool |
|
264 | - */ |
|
265 | - public function hasTagged(int $tag): bool |
|
266 | - { |
|
267 | - // lazily build lookup map |
|
268 | - if (!isset($this->_taggedMap)) { |
|
269 | - $this->_taggedMap = []; |
|
270 | - foreach ($this->_elements as $element) { |
|
271 | - if ($element->isTagged()) { |
|
272 | - $this->_taggedMap[$element->tag()] = $element; |
|
273 | - } |
|
274 | - } |
|
275 | - } |
|
276 | - return isset($this->_taggedMap[$tag]); |
|
277 | - } |
|
257 | + /** |
|
258 | + * Check whether the structure contains a context specific element with a |
|
259 | + * given tag. |
|
260 | + * |
|
261 | + * @param int $tag Tag number |
|
262 | + * |
|
263 | + * @return bool |
|
264 | + */ |
|
265 | + public function hasTagged(int $tag): bool |
|
266 | + { |
|
267 | + // lazily build lookup map |
|
268 | + if (!isset($this->_taggedMap)) { |
|
269 | + $this->_taggedMap = []; |
|
270 | + foreach ($this->_elements as $element) { |
|
271 | + if ($element->isTagged()) { |
|
272 | + $this->_taggedMap[$element->tag()] = $element; |
|
273 | + } |
|
274 | + } |
|
275 | + } |
|
276 | + return isset($this->_taggedMap[$tag]); |
|
277 | + } |
|
278 | 278 | |
279 | - /** |
|
280 | - * Get a context specific element tagged with a given tag. |
|
281 | - * |
|
282 | - * @param int $tag |
|
283 | - * |
|
284 | - * @throws \LogicException If tag doesn't exists |
|
285 | - * |
|
286 | - * @return TaggedType |
|
287 | - */ |
|
288 | - public function getTagged(int $tag): TaggedType |
|
289 | - { |
|
290 | - if (!$this->hasTagged($tag)) { |
|
291 | - throw new \LogicException("No tagged element for tag {$tag}."); |
|
292 | - } |
|
293 | - return $this->_taggedMap[$tag]; |
|
294 | - } |
|
279 | + /** |
|
280 | + * Get a context specific element tagged with a given tag. |
|
281 | + * |
|
282 | + * @param int $tag |
|
283 | + * |
|
284 | + * @throws \LogicException If tag doesn't exists |
|
285 | + * |
|
286 | + * @return TaggedType |
|
287 | + */ |
|
288 | + public function getTagged(int $tag): TaggedType |
|
289 | + { |
|
290 | + if (!$this->hasTagged($tag)) { |
|
291 | + throw new \LogicException("No tagged element for tag {$tag}."); |
|
292 | + } |
|
293 | + return $this->_taggedMap[$tag]; |
|
294 | + } |
|
295 | 295 | |
296 | - /** |
|
297 | - * @see \Countable::count() |
|
298 | - * |
|
299 | - * @return int |
|
300 | - */ |
|
301 | - public function count(): int |
|
302 | - { |
|
303 | - return count($this->_elements); |
|
304 | - } |
|
296 | + /** |
|
297 | + * @see \Countable::count() |
|
298 | + * |
|
299 | + * @return int |
|
300 | + */ |
|
301 | + public function count(): int |
|
302 | + { |
|
303 | + return count($this->_elements); |
|
304 | + } |
|
305 | 305 | |
306 | - /** |
|
307 | - * Get an iterator for the `UnspecifiedElement` objects. |
|
308 | - * |
|
309 | - * @see \IteratorAggregate::getIterator() |
|
310 | - * |
|
311 | - * @return \ArrayIterator |
|
312 | - */ |
|
313 | - public function getIterator(): \ArrayIterator |
|
314 | - { |
|
315 | - return new \ArrayIterator($this->elements()); |
|
316 | - } |
|
306 | + /** |
|
307 | + * Get an iterator for the `UnspecifiedElement` objects. |
|
308 | + * |
|
309 | + * @see \IteratorAggregate::getIterator() |
|
310 | + * |
|
311 | + * @return \ArrayIterator |
|
312 | + */ |
|
313 | + public function getIterator(): \ArrayIterator |
|
314 | + { |
|
315 | + return new \ArrayIterator($this->elements()); |
|
316 | + } |
|
317 | 317 | |
318 | - /** |
|
319 | - * {@inheritdoc} |
|
320 | - */ |
|
321 | - protected function _encodedContentDER(): string |
|
322 | - { |
|
323 | - $data = ''; |
|
324 | - foreach ($this->_elements as $element) { |
|
325 | - $data .= $element->toDER(); |
|
326 | - } |
|
327 | - return $data; |
|
328 | - } |
|
318 | + /** |
|
319 | + * {@inheritdoc} |
|
320 | + */ |
|
321 | + protected function _encodedContentDER(): string |
|
322 | + { |
|
323 | + $data = ''; |
|
324 | + foreach ($this->_elements as $element) { |
|
325 | + $data .= $element->toDER(); |
|
326 | + } |
|
327 | + return $data; |
|
328 | + } |
|
329 | 329 | |
330 | - /** |
|
331 | - * {@inheritdoc} |
|
332 | - * |
|
333 | - * @return self |
|
334 | - */ |
|
335 | - protected static function _decodeFromDER(Identifier $identifier, |
|
336 | - string $data, int &$offset): ElementBase |
|
337 | - { |
|
338 | - if (!$identifier->isConstructed()) { |
|
339 | - throw new DecodeException( |
|
340 | - 'Structured element must have constructed bit set.'); |
|
341 | - } |
|
342 | - $idx = $offset; |
|
343 | - $length = Length::expectFromDER($data, $idx); |
|
344 | - if ($length->isIndefinite()) { |
|
345 | - $type = self::_decodeIndefiniteLength($data, $idx); |
|
346 | - } else { |
|
347 | - $type = self::_decodeDefiniteLength($data, $idx, |
|
348 | - $length->intLength()); |
|
349 | - } |
|
350 | - $offset = $idx; |
|
351 | - return $type; |
|
352 | - } |
|
330 | + /** |
|
331 | + * {@inheritdoc} |
|
332 | + * |
|
333 | + * @return self |
|
334 | + */ |
|
335 | + protected static function _decodeFromDER(Identifier $identifier, |
|
336 | + string $data, int &$offset): ElementBase |
|
337 | + { |
|
338 | + if (!$identifier->isConstructed()) { |
|
339 | + throw new DecodeException( |
|
340 | + 'Structured element must have constructed bit set.'); |
|
341 | + } |
|
342 | + $idx = $offset; |
|
343 | + $length = Length::expectFromDER($data, $idx); |
|
344 | + if ($length->isIndefinite()) { |
|
345 | + $type = self::_decodeIndefiniteLength($data, $idx); |
|
346 | + } else { |
|
347 | + $type = self::_decodeDefiniteLength($data, $idx, |
|
348 | + $length->intLength()); |
|
349 | + } |
|
350 | + $offset = $idx; |
|
351 | + return $type; |
|
352 | + } |
|
353 | 353 | |
354 | - /** |
|
355 | - * Decode elements for a definite length. |
|
356 | - * |
|
357 | - * @param string $data DER data |
|
358 | - * @param int $offset Offset to data |
|
359 | - * @param int $length Number of bytes to decode |
|
360 | - * |
|
361 | - * @throws DecodeException |
|
362 | - * |
|
363 | - * @return ElementBase |
|
364 | - */ |
|
365 | - private static function _decodeDefiniteLength(string $data, int &$offset, |
|
366 | - int $length): ElementBase |
|
367 | - { |
|
368 | - $idx = $offset; |
|
369 | - $end = $idx + $length; |
|
370 | - $elements = []; |
|
371 | - while ($idx < $end) { |
|
372 | - $elements[] = Element::fromDER($data, $idx); |
|
373 | - // check that element didn't overflow length |
|
374 | - if ($idx > $end) { |
|
375 | - throw new DecodeException("Structure's content overflows length."); |
|
376 | - } |
|
377 | - } |
|
378 | - $offset = $idx; |
|
379 | - // return instance by static late binding |
|
380 | - return new static(...$elements); |
|
381 | - } |
|
354 | + /** |
|
355 | + * Decode elements for a definite length. |
|
356 | + * |
|
357 | + * @param string $data DER data |
|
358 | + * @param int $offset Offset to data |
|
359 | + * @param int $length Number of bytes to decode |
|
360 | + * |
|
361 | + * @throws DecodeException |
|
362 | + * |
|
363 | + * @return ElementBase |
|
364 | + */ |
|
365 | + private static function _decodeDefiniteLength(string $data, int &$offset, |
|
366 | + int $length): ElementBase |
|
367 | + { |
|
368 | + $idx = $offset; |
|
369 | + $end = $idx + $length; |
|
370 | + $elements = []; |
|
371 | + while ($idx < $end) { |
|
372 | + $elements[] = Element::fromDER($data, $idx); |
|
373 | + // check that element didn't overflow length |
|
374 | + if ($idx > $end) { |
|
375 | + throw new DecodeException("Structure's content overflows length."); |
|
376 | + } |
|
377 | + } |
|
378 | + $offset = $idx; |
|
379 | + // return instance by static late binding |
|
380 | + return new static(...$elements); |
|
381 | + } |
|
382 | 382 | |
383 | - /** |
|
384 | - * Decode elements for an indefinite length. |
|
385 | - * |
|
386 | - * @param string $data DER data |
|
387 | - * @param int $offset Offset to data |
|
388 | - * |
|
389 | - * @throws DecodeException |
|
390 | - * |
|
391 | - * @return ElementBase |
|
392 | - */ |
|
393 | - private static function _decodeIndefiniteLength(string $data, int &$offset): ElementBase |
|
394 | - { |
|
395 | - $idx = $offset; |
|
396 | - $elements = []; |
|
397 | - $end = strlen($data); |
|
398 | - while (true) { |
|
399 | - if ($idx >= $end) { |
|
400 | - throw new DecodeException( |
|
401 | - 'Unexpected end of data while decoding indefinite length structure.'); |
|
402 | - } |
|
403 | - $el = Element::fromDER($data, $idx); |
|
404 | - if ($el->isType(self::TYPE_EOC)) { |
|
405 | - break; |
|
406 | - } |
|
407 | - $elements[] = $el; |
|
408 | - } |
|
409 | - $offset = $idx; |
|
410 | - $type = new static(...$elements); |
|
411 | - $type->_indefiniteLength = true; |
|
412 | - return $type; |
|
413 | - } |
|
383 | + /** |
|
384 | + * Decode elements for an indefinite length. |
|
385 | + * |
|
386 | + * @param string $data DER data |
|
387 | + * @param int $offset Offset to data |
|
388 | + * |
|
389 | + * @throws DecodeException |
|
390 | + * |
|
391 | + * @return ElementBase |
|
392 | + */ |
|
393 | + private static function _decodeIndefiniteLength(string $data, int &$offset): ElementBase |
|
394 | + { |
|
395 | + $idx = $offset; |
|
396 | + $elements = []; |
|
397 | + $end = strlen($data); |
|
398 | + while (true) { |
|
399 | + if ($idx >= $end) { |
|
400 | + throw new DecodeException( |
|
401 | + 'Unexpected end of data while decoding indefinite length structure.'); |
|
402 | + } |
|
403 | + $el = Element::fromDER($data, $idx); |
|
404 | + if ($el->isType(self::TYPE_EOC)) { |
|
405 | + break; |
|
406 | + } |
|
407 | + $elements[] = $el; |
|
408 | + } |
|
409 | + $offset = $idx; |
|
410 | + $type = new static(...$elements); |
|
411 | + $type->_indefiniteLength = true; |
|
412 | + return $type; |
|
413 | + } |
|
414 | 414 | } |
@@ -13,90 +13,90 @@ |
||
13 | 13 | */ |
14 | 14 | interface ElementBase extends Encodable |
15 | 15 | { |
16 | - /** |
|
17 | - * Get the class of the ASN.1 type. |
|
18 | - * |
|
19 | - * One of `Identifier::CLASS_*` 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 `Identifier::CLASS_*` 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 | - * |
|
50 | - * @return bool |
|
51 | - */ |
|
52 | - 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 | + * |
|
50 | + * @return bool |
|
51 | + */ |
|
52 | + public function isType(int $tag): bool; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Check whether the element is a type of a given tag. |
|
56 | - * |
|
57 | - * Throws an exception if expectation fails. |
|
58 | - * |
|
59 | - * @param int $tag Type tag |
|
60 | - * |
|
61 | - * @throws \UnexpectedValueException If the element type differs from the expected |
|
62 | - * |
|
63 | - * @return ElementBase |
|
64 | - */ |
|
65 | - public function expectType(int $tag): ElementBase; |
|
54 | + /** |
|
55 | + * Check whether the element is a type of a given tag. |
|
56 | + * |
|
57 | + * Throws an exception if expectation fails. |
|
58 | + * |
|
59 | + * @param int $tag Type tag |
|
60 | + * |
|
61 | + * @throws \UnexpectedValueException If the element type differs from the expected |
|
62 | + * |
|
63 | + * @return ElementBase |
|
64 | + */ |
|
65 | + public function expectType(int $tag): ElementBase; |
|
66 | 66 | |
67 | - /** |
|
68 | - * Check whether the element is tagged (context specific). |
|
69 | - * |
|
70 | - * @return bool |
|
71 | - */ |
|
72 | - public function isTagged(): bool; |
|
67 | + /** |
|
68 | + * Check whether the element is tagged (context specific). |
|
69 | + * |
|
70 | + * @return bool |
|
71 | + */ |
|
72 | + public function isTagged(): bool; |
|
73 | 73 | |
74 | - /** |
|
75 | - * Check whether the element is tagged (context specific) and optionally has |
|
76 | - * a given tag. |
|
77 | - * |
|
78 | - * Throws an exception if the element is not tagged or tag differs from |
|
79 | - * the expected. |
|
80 | - * |
|
81 | - * @param null|int $tag Optional type tag |
|
82 | - * |
|
83 | - * @throws \UnexpectedValueException If expectation fails |
|
84 | - * |
|
85 | - * @return TaggedType |
|
86 | - */ |
|
87 | - public function expectTagged(?int $tag = null): TaggedType; |
|
74 | + /** |
|
75 | + * Check whether the element is tagged (context specific) and optionally has |
|
76 | + * a given tag. |
|
77 | + * |
|
78 | + * Throws an exception if the element is not tagged or tag differs from |
|
79 | + * the expected. |
|
80 | + * |
|
81 | + * @param null|int $tag Optional type tag |
|
82 | + * |
|
83 | + * @throws \UnexpectedValueException If expectation fails |
|
84 | + * |
|
85 | + * @return TaggedType |
|
86 | + */ |
|
87 | + public function expectTagged(?int $tag = null): TaggedType; |
|
88 | 88 | |
89 | - /** |
|
90 | - * Get the object as an abstract `Element` instance. |
|
91 | - * |
|
92 | - * @return Element |
|
93 | - */ |
|
94 | - public function asElement(): Element; |
|
89 | + /** |
|
90 | + * Get the object as an abstract `Element` instance. |
|
91 | + * |
|
92 | + * @return Element |
|
93 | + */ |
|
94 | + public function asElement(): Element; |
|
95 | 95 | |
96 | - /** |
|
97 | - * Get the object as an `UnspecifiedType` instance. |
|
98 | - * |
|
99 | - * @return UnspecifiedType |
|
100 | - */ |
|
101 | - public function asUnspecified(): UnspecifiedType; |
|
96 | + /** |
|
97 | + * Get the object as an `UnspecifiedType` instance. |
|
98 | + * |
|
99 | + * @return UnspecifiedType |
|
100 | + */ |
|
101 | + public function asUnspecified(): UnspecifiedType; |
|
102 | 102 | } |
@@ -13,223 +13,223 @@ |
||
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 null|int $offset Reference to the variable that contains offset |
|
47 | - * into the data where to start parsing. |
|
48 | - * Variable is updated to the offset next to the |
|
49 | - * parsed length component. If null, start from offset 0. |
|
50 | - * |
|
51 | - * @throws DecodeException If decoding fails |
|
52 | - * |
|
53 | - * @return self |
|
54 | - */ |
|
55 | - public static function fromDER(string $data, int &$offset = null): self |
|
56 | - { |
|
57 | - $idx = $offset ?? 0; |
|
58 | - $datalen = strlen($data); |
|
59 | - if ($idx >= $datalen) { |
|
60 | - throw new DecodeException( |
|
61 | - 'Unexpected end of data while decoding length.'); |
|
62 | - } |
|
63 | - $indefinite = false; |
|
64 | - $byte = ord($data[$idx++]); |
|
65 | - // bits 7 to 1 |
|
66 | - $length = (0x7f & $byte); |
|
67 | - // long form |
|
68 | - if (0x80 & $byte) { |
|
69 | - if (!$length) { |
|
70 | - $indefinite = true; |
|
71 | - } else { |
|
72 | - if ($idx + $length > $datalen) { |
|
73 | - throw new DecodeException( |
|
74 | - 'Unexpected end of data while decoding long form length.'); |
|
75 | - } |
|
76 | - $length = self::_decodeLongFormLength($length, $data, $idx); |
|
77 | - } |
|
78 | - } |
|
79 | - if (isset($offset)) { |
|
80 | - $offset = $idx; |
|
81 | - } |
|
82 | - return new self($length, $indefinite); |
|
83 | - } |
|
42 | + /** |
|
43 | + * Decode length component from DER data. |
|
44 | + * |
|
45 | + * @param string $data DER encoded data |
|
46 | + * @param null|int $offset Reference to the variable that contains offset |
|
47 | + * into the data where to start parsing. |
|
48 | + * Variable is updated to the offset next to the |
|
49 | + * parsed length component. If null, start from offset 0. |
|
50 | + * |
|
51 | + * @throws DecodeException If decoding fails |
|
52 | + * |
|
53 | + * @return self |
|
54 | + */ |
|
55 | + public static function fromDER(string $data, int &$offset = null): self |
|
56 | + { |
|
57 | + $idx = $offset ?? 0; |
|
58 | + $datalen = strlen($data); |
|
59 | + if ($idx >= $datalen) { |
|
60 | + throw new DecodeException( |
|
61 | + 'Unexpected end of data while decoding length.'); |
|
62 | + } |
|
63 | + $indefinite = false; |
|
64 | + $byte = ord($data[$idx++]); |
|
65 | + // bits 7 to 1 |
|
66 | + $length = (0x7f & $byte); |
|
67 | + // long form |
|
68 | + if (0x80 & $byte) { |
|
69 | + if (!$length) { |
|
70 | + $indefinite = true; |
|
71 | + } else { |
|
72 | + if ($idx + $length > $datalen) { |
|
73 | + throw new DecodeException( |
|
74 | + 'Unexpected end of data while decoding long form length.'); |
|
75 | + } |
|
76 | + $length = self::_decodeLongFormLength($length, $data, $idx); |
|
77 | + } |
|
78 | + } |
|
79 | + if (isset($offset)) { |
|
80 | + $offset = $idx; |
|
81 | + } |
|
82 | + return new self($length, $indefinite); |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Decode length from DER. |
|
87 | - * |
|
88 | - * Throws an exception if length doesn't match with expected or if data |
|
89 | - * doesn't contain enough bytes. |
|
90 | - * |
|
91 | - * Requirement of definite length is relaxed contrary to the specification |
|
92 | - * (sect. 10.1). |
|
93 | - * |
|
94 | - * @see self::fromDER |
|
95 | - * |
|
96 | - * @param string $data DER data |
|
97 | - * @param int $offset Reference to the offset variable |
|
98 | - * @param null|int $expected Expected length, null to bypass checking |
|
99 | - * |
|
100 | - * @throws DecodeException If decoding or expectation fails |
|
101 | - * |
|
102 | - * @return self |
|
103 | - */ |
|
104 | - public static function expectFromDER(string $data, int &$offset, |
|
105 | - int $expected = null): self |
|
106 | - { |
|
107 | - $idx = $offset; |
|
108 | - $length = self::fromDER($data, $idx); |
|
109 | - // if certain length was expected |
|
110 | - if (isset($expected)) { |
|
111 | - if ($length->isIndefinite()) { |
|
112 | - throw new DecodeException('Expected length %d, got indefinite.', |
|
113 | - $expected); |
|
114 | - } |
|
115 | - if ($expected !== $length->intLength()) { |
|
116 | - throw new DecodeException( |
|
117 | - sprintf('Expected length %d, got %d.', $expected, |
|
118 | - $length->intLength())); |
|
119 | - } |
|
120 | - } |
|
121 | - // check that enough data is available |
|
122 | - if (!$length->isIndefinite() && |
|
123 | - strlen($data) < $idx + $length->intLength()) { |
|
124 | - throw new DecodeException( |
|
125 | - sprintf('Length %d overflows data, %d bytes left.', |
|
126 | - $length->intLength(), strlen($data) - $idx)); |
|
127 | - } |
|
128 | - $offset = $idx; |
|
129 | - return $length; |
|
130 | - } |
|
85 | + /** |
|
86 | + * Decode length from DER. |
|
87 | + * |
|
88 | + * Throws an exception if length doesn't match with expected or if data |
|
89 | + * doesn't contain enough bytes. |
|
90 | + * |
|
91 | + * Requirement of definite length is relaxed contrary to the specification |
|
92 | + * (sect. 10.1). |
|
93 | + * |
|
94 | + * @see self::fromDER |
|
95 | + * |
|
96 | + * @param string $data DER data |
|
97 | + * @param int $offset Reference to the offset variable |
|
98 | + * @param null|int $expected Expected length, null to bypass checking |
|
99 | + * |
|
100 | + * @throws DecodeException If decoding or expectation fails |
|
101 | + * |
|
102 | + * @return self |
|
103 | + */ |
|
104 | + public static function expectFromDER(string $data, int &$offset, |
|
105 | + int $expected = null): self |
|
106 | + { |
|
107 | + $idx = $offset; |
|
108 | + $length = self::fromDER($data, $idx); |
|
109 | + // if certain length was expected |
|
110 | + if (isset($expected)) { |
|
111 | + if ($length->isIndefinite()) { |
|
112 | + throw new DecodeException('Expected length %d, got indefinite.', |
|
113 | + $expected); |
|
114 | + } |
|
115 | + if ($expected !== $length->intLength()) { |
|
116 | + throw new DecodeException( |
|
117 | + sprintf('Expected length %d, got %d.', $expected, |
|
118 | + $length->intLength())); |
|
119 | + } |
|
120 | + } |
|
121 | + // check that enough data is available |
|
122 | + if (!$length->isIndefinite() && |
|
123 | + strlen($data) < $idx + $length->intLength()) { |
|
124 | + throw new DecodeException( |
|
125 | + sprintf('Length %d overflows data, %d bytes left.', |
|
126 | + $length->intLength(), strlen($data) - $idx)); |
|
127 | + } |
|
128 | + $offset = $idx; |
|
129 | + return $length; |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * {@inheritdoc} |
|
134 | - * |
|
135 | - * @throws \DomainException If length is too large to encode |
|
136 | - */ |
|
137 | - public function toDER(): string |
|
138 | - { |
|
139 | - $bytes = []; |
|
140 | - if ($this->_indefinite) { |
|
141 | - $bytes[] = 0x80; |
|
142 | - } else { |
|
143 | - $num = $this->_length->gmpObj(); |
|
144 | - // long form |
|
145 | - if ($num > 127) { |
|
146 | - $octets = []; |
|
147 | - for (; $num > 0; $num >>= 8) { |
|
148 | - $octets[] = gmp_intval(0xff & $num); |
|
149 | - } |
|
150 | - $count = count($octets); |
|
151 | - // first octet must not be 0xff |
|
152 | - if ($count >= 127) { |
|
153 | - throw new \DomainException('Too many length octets.'); |
|
154 | - } |
|
155 | - $bytes[] = 0x80 | $count; |
|
156 | - foreach (array_reverse($octets) as $octet) { |
|
157 | - $bytes[] = $octet; |
|
158 | - } |
|
159 | - } |
|
160 | - // short form |
|
161 | - else { |
|
162 | - $bytes[] = gmp_intval($num); |
|
163 | - } |
|
164 | - } |
|
165 | - return pack('C*', ...$bytes); |
|
166 | - } |
|
132 | + /** |
|
133 | + * {@inheritdoc} |
|
134 | + * |
|
135 | + * @throws \DomainException If length is too large to encode |
|
136 | + */ |
|
137 | + public function toDER(): string |
|
138 | + { |
|
139 | + $bytes = []; |
|
140 | + if ($this->_indefinite) { |
|
141 | + $bytes[] = 0x80; |
|
142 | + } else { |
|
143 | + $num = $this->_length->gmpObj(); |
|
144 | + // long form |
|
145 | + if ($num > 127) { |
|
146 | + $octets = []; |
|
147 | + for (; $num > 0; $num >>= 8) { |
|
148 | + $octets[] = gmp_intval(0xff & $num); |
|
149 | + } |
|
150 | + $count = count($octets); |
|
151 | + // first octet must not be 0xff |
|
152 | + if ($count >= 127) { |
|
153 | + throw new \DomainException('Too many length octets.'); |
|
154 | + } |
|
155 | + $bytes[] = 0x80 | $count; |
|
156 | + foreach (array_reverse($octets) as $octet) { |
|
157 | + $bytes[] = $octet; |
|
158 | + } |
|
159 | + } |
|
160 | + // short form |
|
161 | + else { |
|
162 | + $bytes[] = gmp_intval($num); |
|
163 | + } |
|
164 | + } |
|
165 | + return pack('C*', ...$bytes); |
|
166 | + } |
|
167 | 167 | |
168 | - /** |
|
169 | - * Get the length. |
|
170 | - * |
|
171 | - * @throws \LogicException If length is indefinite |
|
172 | - * |
|
173 | - * @return string Length as an integer string |
|
174 | - */ |
|
175 | - public function length(): string |
|
176 | - { |
|
177 | - if ($this->_indefinite) { |
|
178 | - throw new \LogicException('Length is indefinite.'); |
|
179 | - } |
|
180 | - return $this->_length->base10(); |
|
181 | - } |
|
168 | + /** |
|
169 | + * Get the length. |
|
170 | + * |
|
171 | + * @throws \LogicException If length is indefinite |
|
172 | + * |
|
173 | + * @return string Length as an integer string |
|
174 | + */ |
|
175 | + public function length(): string |
|
176 | + { |
|
177 | + if ($this->_indefinite) { |
|
178 | + throw new \LogicException('Length is indefinite.'); |
|
179 | + } |
|
180 | + return $this->_length->base10(); |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * Get the length as an integer. |
|
185 | - * |
|
186 | - * @throws \LogicException If length is indefinite |
|
187 | - * @throws \RuntimeException If length overflows integer size |
|
188 | - * |
|
189 | - * @return int |
|
190 | - */ |
|
191 | - public function intLength(): int |
|
192 | - { |
|
193 | - if ($this->_indefinite) { |
|
194 | - throw new \LogicException('Length is indefinite.'); |
|
195 | - } |
|
196 | - return $this->_length->intVal(); |
|
197 | - } |
|
183 | + /** |
|
184 | + * Get the length as an integer. |
|
185 | + * |
|
186 | + * @throws \LogicException If length is indefinite |
|
187 | + * @throws \RuntimeException If length overflows integer size |
|
188 | + * |
|
189 | + * @return int |
|
190 | + */ |
|
191 | + public function intLength(): int |
|
192 | + { |
|
193 | + if ($this->_indefinite) { |
|
194 | + throw new \LogicException('Length is indefinite.'); |
|
195 | + } |
|
196 | + return $this->_length->intVal(); |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * Whether length is indefinite. |
|
201 | - * |
|
202 | - * @return bool |
|
203 | - */ |
|
204 | - public function isIndefinite(): bool |
|
205 | - { |
|
206 | - return $this->_indefinite; |
|
207 | - } |
|
199 | + /** |
|
200 | + * Whether length is indefinite. |
|
201 | + * |
|
202 | + * @return bool |
|
203 | + */ |
|
204 | + public function isIndefinite(): bool |
|
205 | + { |
|
206 | + return $this->_indefinite; |
|
207 | + } |
|
208 | 208 | |
209 | - /** |
|
210 | - * Decode long form length. |
|
211 | - * |
|
212 | - * @param int $length Number of octets |
|
213 | - * @param string $data Data |
|
214 | - * @param int $offset reference to the variable containing offset to the data |
|
215 | - * |
|
216 | - * @throws DecodeException If decoding fails |
|
217 | - * |
|
218 | - * @return string Integer as a string |
|
219 | - */ |
|
220 | - private static function _decodeLongFormLength(int $length, string $data, |
|
221 | - int &$offset): string |
|
222 | - { |
|
223 | - // first octet must not be 0xff (spec 8.1.3.5c) |
|
224 | - if (127 === $length) { |
|
225 | - throw new DecodeException('Invalid number of length octets.'); |
|
226 | - } |
|
227 | - $num = gmp_init(0, 10); |
|
228 | - while (--$length >= 0) { |
|
229 | - $byte = ord($data[$offset++]); |
|
230 | - $num <<= 8; |
|
231 | - $num |= $byte; |
|
232 | - } |
|
233 | - return gmp_strval($num); |
|
234 | - } |
|
209 | + /** |
|
210 | + * Decode long form length. |
|
211 | + * |
|
212 | + * @param int $length Number of octets |
|
213 | + * @param string $data Data |
|
214 | + * @param int $offset reference to the variable containing offset to the data |
|
215 | + * |
|
216 | + * @throws DecodeException If decoding fails |
|
217 | + * |
|
218 | + * @return string Integer as a string |
|
219 | + */ |
|
220 | + private static function _decodeLongFormLength(int $length, string $data, |
|
221 | + int &$offset): string |
|
222 | + { |
|
223 | + // first octet must not be 0xff (spec 8.1.3.5c) |
|
224 | + if (127 === $length) { |
|
225 | + throw new DecodeException('Invalid number of length octets.'); |
|
226 | + } |
|
227 | + $num = gmp_init(0, 10); |
|
228 | + while (--$length >= 0) { |
|
229 | + $byte = ord($data[$offset++]); |
|
230 | + $num <<= 8; |
|
231 | + $num |= $byte; |
|
232 | + } |
|
233 | + return gmp_strval($num); |
|
234 | + } |
|
235 | 235 | } |
@@ -11,149 +11,149 @@ |
||
11 | 11 | */ |
12 | 12 | class Flags |
13 | 13 | { |
14 | - /** |
|
15 | - * Flag octets. |
|
16 | - * |
|
17 | - * @var string |
|
18 | - */ |
|
19 | - protected $_flags; |
|
14 | + /** |
|
15 | + * Flag octets. |
|
16 | + * |
|
17 | + * @var string |
|
18 | + */ |
|
19 | + protected $_flags; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Number of flags. |
|
23 | - * |
|
24 | - * @var int |
|
25 | - */ |
|
26 | - protected $_width; |
|
21 | + /** |
|
22 | + * Number of flags. |
|
23 | + * |
|
24 | + * @var int |
|
25 | + */ |
|
26 | + protected $_width; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Constructor. |
|
30 | - * |
|
31 | - * @param int|string $flags Flags |
|
32 | - * @param int $width The number of flags. If width is larger than |
|
33 | - * number of bits in $flags, zeroes are prepended |
|
34 | - * to flag field. |
|
35 | - */ |
|
36 | - public function __construct($flags, int $width) |
|
37 | - { |
|
38 | - if (!$width) { |
|
39 | - $this->_flags = ''; |
|
40 | - } else { |
|
41 | - // calculate number of unused bits in last octet |
|
42 | - $last_octet_bits = $width % 8; |
|
43 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
44 | - $num = gmp_init($flags); |
|
45 | - // mask bits outside bitfield width |
|
46 | - $mask = gmp_sub(gmp_init(1) << $width, 1); |
|
47 | - $num &= $mask; |
|
48 | - // shift towards MSB if needed |
|
49 | - $data = gmp_export($num << $unused_bits, 1, |
|
50 | - GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
51 | - $octets = unpack('C*', $data); |
|
52 | - assert(is_array($octets), new \RuntimeException('unpack() failed')); |
|
53 | - $bits = count($octets) * 8; |
|
54 | - // pad with zeroes |
|
55 | - while ($bits < $width) { |
|
56 | - array_unshift($octets, 0); |
|
57 | - $bits += 8; |
|
58 | - } |
|
59 | - $this->_flags = pack('C*', ...$octets); |
|
60 | - } |
|
61 | - $this->_width = $width; |
|
62 | - } |
|
28 | + /** |
|
29 | + * Constructor. |
|
30 | + * |
|
31 | + * @param int|string $flags Flags |
|
32 | + * @param int $width The number of flags. If width is larger than |
|
33 | + * number of bits in $flags, zeroes are prepended |
|
34 | + * to flag field. |
|
35 | + */ |
|
36 | + public function __construct($flags, int $width) |
|
37 | + { |
|
38 | + if (!$width) { |
|
39 | + $this->_flags = ''; |
|
40 | + } else { |
|
41 | + // calculate number of unused bits in last octet |
|
42 | + $last_octet_bits = $width % 8; |
|
43 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
44 | + $num = gmp_init($flags); |
|
45 | + // mask bits outside bitfield width |
|
46 | + $mask = gmp_sub(gmp_init(1) << $width, 1); |
|
47 | + $num &= $mask; |
|
48 | + // shift towards MSB if needed |
|
49 | + $data = gmp_export($num << $unused_bits, 1, |
|
50 | + GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
51 | + $octets = unpack('C*', $data); |
|
52 | + assert(is_array($octets), new \RuntimeException('unpack() failed')); |
|
53 | + $bits = count($octets) * 8; |
|
54 | + // pad with zeroes |
|
55 | + while ($bits < $width) { |
|
56 | + array_unshift($octets, 0); |
|
57 | + $bits += 8; |
|
58 | + } |
|
59 | + $this->_flags = pack('C*', ...$octets); |
|
60 | + } |
|
61 | + $this->_width = $width; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * Initialize from `BitString`. |
|
66 | - * |
|
67 | - * @param BitString $bs |
|
68 | - * @param int $width |
|
69 | - * |
|
70 | - * @return self |
|
71 | - */ |
|
72 | - public static function fromBitString(BitString $bs, int $width): self |
|
73 | - { |
|
74 | - $num_bits = $bs->numBits(); |
|
75 | - $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
76 | - $num >>= $bs->unusedBits(); |
|
77 | - if ($num_bits < $width) { |
|
78 | - $num <<= ($width - $num_bits); |
|
79 | - } |
|
80 | - return new self(gmp_strval($num, 10), $width); |
|
81 | - } |
|
64 | + /** |
|
65 | + * Initialize from `BitString`. |
|
66 | + * |
|
67 | + * @param BitString $bs |
|
68 | + * @param int $width |
|
69 | + * |
|
70 | + * @return self |
|
71 | + */ |
|
72 | + public static function fromBitString(BitString $bs, int $width): self |
|
73 | + { |
|
74 | + $num_bits = $bs->numBits(); |
|
75 | + $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
76 | + $num >>= $bs->unusedBits(); |
|
77 | + if ($num_bits < $width) { |
|
78 | + $num <<= ($width - $num_bits); |
|
79 | + } |
|
80 | + return new self(gmp_strval($num, 10), $width); |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Check whether a bit at given index is set. |
|
85 | - * |
|
86 | - * Index 0 is the leftmost bit. |
|
87 | - * |
|
88 | - * @param int $idx |
|
89 | - * |
|
90 | - * @throws \OutOfBoundsException |
|
91 | - * |
|
92 | - * @return bool |
|
93 | - */ |
|
94 | - public function test(int $idx): bool |
|
95 | - { |
|
96 | - if ($idx >= $this->_width) { |
|
97 | - throw new \OutOfBoundsException('Index is out of bounds.'); |
|
98 | - } |
|
99 | - // octet index |
|
100 | - $oi = (int) floor($idx / 8); |
|
101 | - $byte = $this->_flags[$oi]; |
|
102 | - // bit index |
|
103 | - $bi = $idx % 8; |
|
104 | - // index 0 is the most significant bit in byte |
|
105 | - $mask = 0x01 << (7 - $bi); |
|
106 | - return (ord($byte) & $mask) > 0; |
|
107 | - } |
|
83 | + /** |
|
84 | + * Check whether a bit at given index is set. |
|
85 | + * |
|
86 | + * Index 0 is the leftmost bit. |
|
87 | + * |
|
88 | + * @param int $idx |
|
89 | + * |
|
90 | + * @throws \OutOfBoundsException |
|
91 | + * |
|
92 | + * @return bool |
|
93 | + */ |
|
94 | + public function test(int $idx): bool |
|
95 | + { |
|
96 | + if ($idx >= $this->_width) { |
|
97 | + throw new \OutOfBoundsException('Index is out of bounds.'); |
|
98 | + } |
|
99 | + // octet index |
|
100 | + $oi = (int) floor($idx / 8); |
|
101 | + $byte = $this->_flags[$oi]; |
|
102 | + // bit index |
|
103 | + $bi = $idx % 8; |
|
104 | + // index 0 is the most significant bit in byte |
|
105 | + $mask = 0x01 << (7 - $bi); |
|
106 | + return (ord($byte) & $mask) > 0; |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * Get flags as an octet string. |
|
111 | - * |
|
112 | - * Zeroes are appended to the last octet if width is not divisible by 8. |
|
113 | - * |
|
114 | - * @return string |
|
115 | - */ |
|
116 | - public function string(): string |
|
117 | - { |
|
118 | - return $this->_flags; |
|
119 | - } |
|
109 | + /** |
|
110 | + * Get flags as an octet string. |
|
111 | + * |
|
112 | + * Zeroes are appended to the last octet if width is not divisible by 8. |
|
113 | + * |
|
114 | + * @return string |
|
115 | + */ |
|
116 | + public function string(): string |
|
117 | + { |
|
118 | + return $this->_flags; |
|
119 | + } |
|
120 | 120 | |
121 | - /** |
|
122 | - * Get flags as a base 10 integer. |
|
123 | - * |
|
124 | - * @return string Integer as a string |
|
125 | - */ |
|
126 | - public function number(): string |
|
127 | - { |
|
128 | - $num = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
129 | - $last_octet_bits = $this->_width % 8; |
|
130 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
131 | - $num >>= $unused_bits; |
|
132 | - return gmp_strval($num, 10); |
|
133 | - } |
|
121 | + /** |
|
122 | + * Get flags as a base 10 integer. |
|
123 | + * |
|
124 | + * @return string Integer as a string |
|
125 | + */ |
|
126 | + public function number(): string |
|
127 | + { |
|
128 | + $num = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
129 | + $last_octet_bits = $this->_width % 8; |
|
130 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
131 | + $num >>= $unused_bits; |
|
132 | + return gmp_strval($num, 10); |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * Get flags as an integer. |
|
137 | - * |
|
138 | - * @return int |
|
139 | - */ |
|
140 | - public function intNumber(): int |
|
141 | - { |
|
142 | - $num = new BigInt($this->number()); |
|
143 | - return $num->intVal(); |
|
144 | - } |
|
135 | + /** |
|
136 | + * Get flags as an integer. |
|
137 | + * |
|
138 | + * @return int |
|
139 | + */ |
|
140 | + public function intNumber(): int |
|
141 | + { |
|
142 | + $num = new BigInt($this->number()); |
|
143 | + return $num->intVal(); |
|
144 | + } |
|
145 | 145 | |
146 | - /** |
|
147 | - * Get flags as a `BitString` object. |
|
148 | - * |
|
149 | - * Unused bits are set accordingly. Trailing zeroes are not stripped. |
|
150 | - * |
|
151 | - * @return BitString |
|
152 | - */ |
|
153 | - public function bitString(): BitString |
|
154 | - { |
|
155 | - $last_octet_bits = $this->_width % 8; |
|
156 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
157 | - return new BitString($this->_flags, $unused_bits); |
|
158 | - } |
|
146 | + /** |
|
147 | + * Get flags as a `BitString` object. |
|
148 | + * |
|
149 | + * Unused bits are set accordingly. Trailing zeroes are not stripped. |
|
150 | + * |
|
151 | + * @return BitString |
|
152 | + */ |
|
153 | + public function bitString(): BitString |
|
154 | + { |
|
155 | + $last_octet_bits = $this->_width % 8; |
|
156 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
157 | + return new BitString($this->_flags, $unused_bits); |
|
158 | + } |
|
159 | 159 | } |
@@ -9,107 +9,107 @@ |
||
9 | 9 | */ |
10 | 10 | class BigInt |
11 | 11 | { |
12 | - /** |
|
13 | - * Number as a base10 integer string. |
|
14 | - * |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - private $_num; |
|
12 | + /** |
|
13 | + * Number as a base10 integer string. |
|
14 | + * |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + private $_num; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Number as an integer type. |
|
21 | - * |
|
22 | - * @internal Lazily initialized |
|
23 | - * |
|
24 | - * @var null|int |
|
25 | - */ |
|
26 | - private $_intNum; |
|
19 | + /** |
|
20 | + * Number as an integer type. |
|
21 | + * |
|
22 | + * @internal Lazily initialized |
|
23 | + * |
|
24 | + * @var null|int |
|
25 | + */ |
|
26 | + private $_intNum; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Constructor. |
|
30 | - * |
|
31 | - * @param int|string $num |
|
32 | - */ |
|
33 | - public function __construct($num) |
|
34 | - { |
|
35 | - $this->_num = strval($num); |
|
36 | - } |
|
28 | + /** |
|
29 | + * Constructor. |
|
30 | + * |
|
31 | + * @param int|string $num |
|
32 | + */ |
|
33 | + public function __construct($num) |
|
34 | + { |
|
35 | + $this->_num = strval($num); |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - public function __toString() |
|
42 | - { |
|
43 | - return $this->base10(); |
|
44 | - } |
|
38 | + /** |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + public function __toString() |
|
42 | + { |
|
43 | + return $this->base10(); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Get the number as a base10 integer string. |
|
48 | - * |
|
49 | - * @return string |
|
50 | - */ |
|
51 | - public function base10(): string |
|
52 | - { |
|
53 | - return $this->_num; |
|
54 | - } |
|
46 | + /** |
|
47 | + * Get the number as a base10 integer string. |
|
48 | + * |
|
49 | + * @return string |
|
50 | + */ |
|
51 | + public function base10(): string |
|
52 | + { |
|
53 | + return $this->_num; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Get the number as an integer. |
|
58 | - * |
|
59 | - * @throws \RuntimeException If number overflows integer size |
|
60 | - * |
|
61 | - * @return int |
|
62 | - */ |
|
63 | - public function intVal(): int |
|
64 | - { |
|
65 | - if (!isset($this->_intNum)) { |
|
66 | - $num = gmp_init($this->_num, 10); |
|
67 | - if (gmp_cmp($num, $this->_intMaxGmp()) > 0) { |
|
68 | - throw new \RuntimeException('Integer overflow.'); |
|
69 | - } |
|
70 | - if (gmp_cmp($num, $this->_intMinGmp()) < 0) { |
|
71 | - throw new \RuntimeException('Integer underflow.'); |
|
72 | - } |
|
73 | - $this->_intNum = gmp_intval($num); |
|
74 | - } |
|
75 | - return $this->_intNum; |
|
76 | - } |
|
56 | + /** |
|
57 | + * Get the number as an integer. |
|
58 | + * |
|
59 | + * @throws \RuntimeException If number overflows integer size |
|
60 | + * |
|
61 | + * @return int |
|
62 | + */ |
|
63 | + public function intVal(): int |
|
64 | + { |
|
65 | + if (!isset($this->_intNum)) { |
|
66 | + $num = gmp_init($this->_num, 10); |
|
67 | + if (gmp_cmp($num, $this->_intMaxGmp()) > 0) { |
|
68 | + throw new \RuntimeException('Integer overflow.'); |
|
69 | + } |
|
70 | + if (gmp_cmp($num, $this->_intMinGmp()) < 0) { |
|
71 | + throw new \RuntimeException('Integer underflow.'); |
|
72 | + } |
|
73 | + $this->_intNum = gmp_intval($num); |
|
74 | + } |
|
75 | + return $this->_intNum; |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * Get the number as a `GMP` object. |
|
80 | - * |
|
81 | - * @return \GMP |
|
82 | - */ |
|
83 | - public function gmpObj(): \GMP |
|
84 | - { |
|
85 | - return gmp_init($this->_num, 10); |
|
86 | - } |
|
78 | + /** |
|
79 | + * Get the number as a `GMP` object. |
|
80 | + * |
|
81 | + * @return \GMP |
|
82 | + */ |
|
83 | + public function gmpObj(): \GMP |
|
84 | + { |
|
85 | + return gmp_init($this->_num, 10); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Get the maximum integer value. |
|
90 | - * |
|
91 | - * @return \GMP |
|
92 | - */ |
|
93 | - private function _intMaxGmp(): \GMP |
|
94 | - { |
|
95 | - static $gmp; |
|
96 | - if (!isset($gmp)) { |
|
97 | - $gmp = gmp_init(PHP_INT_MAX, 10); |
|
98 | - } |
|
99 | - return $gmp; |
|
100 | - } |
|
88 | + /** |
|
89 | + * Get the maximum integer value. |
|
90 | + * |
|
91 | + * @return \GMP |
|
92 | + */ |
|
93 | + private function _intMaxGmp(): \GMP |
|
94 | + { |
|
95 | + static $gmp; |
|
96 | + if (!isset($gmp)) { |
|
97 | + $gmp = gmp_init(PHP_INT_MAX, 10); |
|
98 | + } |
|
99 | + return $gmp; |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Get the minimum integer value. |
|
104 | - * |
|
105 | - * @return \GMP |
|
106 | - */ |
|
107 | - private function _intMinGmp(): \GMP |
|
108 | - { |
|
109 | - static $gmp; |
|
110 | - if (!isset($gmp)) { |
|
111 | - $gmp = gmp_init(PHP_INT_MIN, 10); |
|
112 | - } |
|
113 | - return $gmp; |
|
114 | - } |
|
102 | + /** |
|
103 | + * Get the minimum integer value. |
|
104 | + * |
|
105 | + * @return \GMP |
|
106 | + */ |
|
107 | + private function _intMinGmp(): \GMP |
|
108 | + { |
|
109 | + static $gmp; |
|
110 | + if (!isset($gmp)) { |
|
111 | + $gmp = gmp_init(PHP_INT_MIN, 10); |
|
112 | + } |
|
113 | + return $gmp; |
|
114 | + } |
|
115 | 115 | } |
@@ -24,482 +24,482 @@ |
||
24 | 24 | */ |
25 | 25 | abstract class Element implements ElementBase |
26 | 26 | { |
27 | - // Universal type tags |
|
28 | - const TYPE_EOC = 0x00; |
|
29 | - const TYPE_BOOLEAN = 0x01; |
|
30 | - const TYPE_INTEGER = 0x02; |
|
31 | - const TYPE_BIT_STRING = 0x03; |
|
32 | - const TYPE_OCTET_STRING = 0x04; |
|
33 | - const TYPE_NULL = 0x05; |
|
34 | - const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
35 | - const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
36 | - const TYPE_EXTERNAL = 0x08; |
|
37 | - const TYPE_REAL = 0x09; |
|
38 | - const TYPE_ENUMERATED = 0x0a; |
|
39 | - const TYPE_EMBEDDED_PDV = 0x0b; |
|
40 | - const TYPE_UTF8_STRING = 0x0c; |
|
41 | - const TYPE_RELATIVE_OID = 0x0d; |
|
42 | - const TYPE_SEQUENCE = 0x10; |
|
43 | - const TYPE_SET = 0x11; |
|
44 | - const TYPE_NUMERIC_STRING = 0x12; |
|
45 | - const TYPE_PRINTABLE_STRING = 0x13; |
|
46 | - const TYPE_T61_STRING = 0x14; |
|
47 | - const TYPE_VIDEOTEX_STRING = 0x15; |
|
48 | - const TYPE_IA5_STRING = 0x16; |
|
49 | - const TYPE_UTC_TIME = 0x17; |
|
50 | - const TYPE_GENERALIZED_TIME = 0x18; |
|
51 | - const TYPE_GRAPHIC_STRING = 0x19; |
|
52 | - const TYPE_VISIBLE_STRING = 0x1a; |
|
53 | - const TYPE_GENERAL_STRING = 0x1b; |
|
54 | - const TYPE_UNIVERSAL_STRING = 0x1c; |
|
55 | - const TYPE_CHARACTER_STRING = 0x1d; |
|
56 | - const TYPE_BMP_STRING = 0x1e; |
|
57 | - |
|
58 | - /** |
|
59 | - * Mapping from universal type tag to implementation class name. |
|
60 | - * |
|
61 | - * @internal |
|
62 | - * |
|
63 | - * @var array |
|
64 | - */ |
|
65 | - const MAP_TAG_TO_CLASS = [ |
|
66 | - self::TYPE_EOC => Primitive\EOC::class, |
|
67 | - self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
68 | - self::TYPE_INTEGER => Primitive\Integer::class, |
|
69 | - self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
70 | - self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
71 | - self::TYPE_NULL => Primitive\NullType::class, |
|
72 | - self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
73 | - self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
74 | - self::TYPE_REAL => Primitive\Real::class, |
|
75 | - self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
76 | - self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
77 | - self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
78 | - self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
79 | - self::TYPE_SET => Constructed\Set::class, |
|
80 | - self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
81 | - self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
82 | - self::TYPE_T61_STRING => Primitive\T61String::class, |
|
83 | - self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
84 | - self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
85 | - self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
86 | - self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
87 | - self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
88 | - self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
89 | - self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
90 | - self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
91 | - self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
92 | - self::TYPE_BMP_STRING => Primitive\BMPString::class, |
|
93 | - ]; |
|
94 | - |
|
95 | - /** |
|
96 | - * Pseudotype for all string types. |
|
97 | - * |
|
98 | - * May be used as an expectation parameter. |
|
99 | - * |
|
100 | - * @var int |
|
101 | - */ |
|
102 | - const TYPE_STRING = -1; |
|
103 | - |
|
104 | - /** |
|
105 | - * Pseudotype for all time types. |
|
106 | - * |
|
107 | - * May be used as an expectation parameter. |
|
108 | - * |
|
109 | - * @var int |
|
110 | - */ |
|
111 | - const TYPE_TIME = -2; |
|
112 | - |
|
113 | - /** |
|
114 | - * Pseudotype for constructed strings. |
|
115 | - * |
|
116 | - * May be used as an expectation parameter. |
|
117 | - * |
|
118 | - * @var int |
|
119 | - */ |
|
120 | - const TYPE_CONSTRUCTED_STRING = -3; |
|
121 | - |
|
122 | - /** |
|
123 | - * Mapping from universal type tag to human readable name. |
|
124 | - * |
|
125 | - * @internal |
|
126 | - * |
|
127 | - * @var array |
|
128 | - */ |
|
129 | - const MAP_TYPE_TO_NAME = [ |
|
130 | - self::TYPE_EOC => 'EOC', |
|
131 | - self::TYPE_BOOLEAN => 'BOOLEAN', |
|
132 | - self::TYPE_INTEGER => 'INTEGER', |
|
133 | - self::TYPE_BIT_STRING => 'BIT STRING', |
|
134 | - self::TYPE_OCTET_STRING => 'OCTET STRING', |
|
135 | - self::TYPE_NULL => 'NULL', |
|
136 | - self::TYPE_OBJECT_IDENTIFIER => 'OBJECT IDENTIFIER', |
|
137 | - self::TYPE_OBJECT_DESCRIPTOR => 'ObjectDescriptor', |
|
138 | - self::TYPE_EXTERNAL => 'EXTERNAL', |
|
139 | - self::TYPE_REAL => 'REAL', |
|
140 | - self::TYPE_ENUMERATED => 'ENUMERATED', |
|
141 | - self::TYPE_EMBEDDED_PDV => 'EMBEDDED PDV', |
|
142 | - self::TYPE_UTF8_STRING => 'UTF8String', |
|
143 | - self::TYPE_RELATIVE_OID => 'RELATIVE-OID', |
|
144 | - self::TYPE_SEQUENCE => 'SEQUENCE', |
|
145 | - self::TYPE_SET => 'SET', |
|
146 | - self::TYPE_NUMERIC_STRING => 'NumericString', |
|
147 | - self::TYPE_PRINTABLE_STRING => 'PrintableString', |
|
148 | - self::TYPE_T61_STRING => 'T61String', |
|
149 | - self::TYPE_VIDEOTEX_STRING => 'VideotexString', |
|
150 | - self::TYPE_IA5_STRING => 'IA5String', |
|
151 | - self::TYPE_UTC_TIME => 'UTCTime', |
|
152 | - self::TYPE_GENERALIZED_TIME => 'GeneralizedTime', |
|
153 | - self::TYPE_GRAPHIC_STRING => 'GraphicString', |
|
154 | - self::TYPE_VISIBLE_STRING => 'VisibleString', |
|
155 | - self::TYPE_GENERAL_STRING => 'GeneralString', |
|
156 | - self::TYPE_UNIVERSAL_STRING => 'UniversalString', |
|
157 | - self::TYPE_CHARACTER_STRING => 'CHARACTER STRING', |
|
158 | - self::TYPE_BMP_STRING => 'BMPString', |
|
159 | - self::TYPE_STRING => 'Any String', |
|
160 | - self::TYPE_TIME => 'Any Time', |
|
161 | - self::TYPE_CONSTRUCTED_STRING => 'Constructed String', |
|
162 | - ]; |
|
163 | - |
|
164 | - /** |
|
165 | - * Element's type tag. |
|
166 | - * |
|
167 | - * @var int |
|
168 | - */ |
|
169 | - protected $_typeTag; |
|
170 | - |
|
171 | - /** |
|
172 | - * Whether type shall be encoded with indefinite length. |
|
173 | - * |
|
174 | - * @var bool |
|
175 | - */ |
|
176 | - protected $_indefiniteLength = false; |
|
177 | - |
|
178 | - /** |
|
179 | - * {@inheritdoc} |
|
180 | - */ |
|
181 | - abstract public function typeClass(): int; |
|
182 | - |
|
183 | - /** |
|
184 | - * {@inheritdoc} |
|
185 | - */ |
|
186 | - abstract public function isConstructed(): bool; |
|
187 | - |
|
188 | - /** |
|
189 | - * Decode element from DER data. |
|
190 | - * |
|
191 | - * @param string $data DER encoded data |
|
192 | - * @param null|int $offset Reference to the variable that contains offset |
|
193 | - * into the data where to start parsing. |
|
194 | - * Variable is updated to the offset next to the |
|
195 | - * parsed element. If null, start from offset 0. |
|
196 | - * |
|
197 | - * @throws DecodeException If decoding fails |
|
198 | - * @throws \UnexpectedValueException If called in the context of an expected |
|
199 | - * type, but decoding yields another type |
|
200 | - * |
|
201 | - * @return ElementBase |
|
202 | - */ |
|
203 | - public static function fromDER(string $data, int &$offset = null): ElementBase |
|
204 | - { |
|
205 | - $idx = $offset ?? 0; |
|
206 | - // decode identifier |
|
207 | - $identifier = Identifier::fromDER($data, $idx); |
|
208 | - // determine class that implements type specific decoding |
|
209 | - $cls = self::_determineImplClass($identifier); |
|
210 | - try { |
|
211 | - // decode remaining element |
|
212 | - $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
213 | - } catch (\LogicException $e) { |
|
214 | - // rethrow as a RuntimeException for unified exception handling |
|
215 | - throw new DecodeException( |
|
216 | - sprintf('Error while decoding %s.', |
|
217 | - self::tagToName($identifier->intTag())), 0, $e); |
|
218 | - } |
|
219 | - // if called in the context of a concrete class, check |
|
220 | - // that decoded type matches the type of a calling class |
|
221 | - $called_class = get_called_class(); |
|
222 | - if (self::class !== $called_class) { |
|
223 | - if (!$element instanceof $called_class) { |
|
224 | - throw new \UnexpectedValueException( |
|
225 | - sprintf('%s expected, got %s.', $called_class, |
|
226 | - get_class($element))); |
|
227 | - } |
|
228 | - } |
|
229 | - // update offset for the caller |
|
230 | - if (isset($offset)) { |
|
231 | - $offset = $idx; |
|
232 | - } |
|
233 | - return $element; |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * {@inheritdoc} |
|
238 | - */ |
|
239 | - public function toDER(): string |
|
240 | - { |
|
241 | - $identifier = new Identifier($this->typeClass(), |
|
242 | - $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
243 | - $this->_typeTag); |
|
244 | - $content = $this->_encodedContentDER(); |
|
245 | - if ($this->_indefiniteLength) { |
|
246 | - $length = new Length(0, true); |
|
247 | - $eoc = new Primitive\EOC(); |
|
248 | - return $identifier->toDER() . $length->toDER() . $content . $eoc->toDER(); |
|
249 | - } |
|
250 | - $length = new Length(strlen($content)); |
|
251 | - return $identifier->toDER() . $length->toDER() . $content; |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * {@inheritdoc} |
|
256 | - */ |
|
257 | - public function tag(): int |
|
258 | - { |
|
259 | - return $this->_typeTag; |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * {@inheritdoc} |
|
264 | - */ |
|
265 | - public function isType(int $tag): bool |
|
266 | - { |
|
267 | - // if element is context specific |
|
268 | - if (Identifier::CLASS_CONTEXT_SPECIFIC === $this->typeClass()) { |
|
269 | - return false; |
|
270 | - } |
|
271 | - // negative tags identify an abstract pseudotype |
|
272 | - if ($tag < 0) { |
|
273 | - return $this->_isPseudoType($tag); |
|
274 | - } |
|
275 | - return $this->_isConcreteType($tag); |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * {@inheritdoc} |
|
280 | - */ |
|
281 | - public function expectType(int $tag): ElementBase |
|
282 | - { |
|
283 | - if (!$this->isType($tag)) { |
|
284 | - throw new \UnexpectedValueException( |
|
285 | - sprintf('%s expected, got %s.', self::tagToName($tag), |
|
286 | - $this->_typeDescriptorString())); |
|
287 | - } |
|
288 | - return $this; |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * {@inheritdoc} |
|
293 | - */ |
|
294 | - public function isTagged(): bool |
|
295 | - { |
|
296 | - return $this instanceof TaggedType; |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * {@inheritdoc} |
|
301 | - */ |
|
302 | - public function expectTagged(?int $tag = null): TaggedType |
|
303 | - { |
|
304 | - if (!$this->isTagged()) { |
|
305 | - throw new \UnexpectedValueException( |
|
306 | - sprintf('Context specific element expected, got %s.', |
|
307 | - Identifier::classToName($this->typeClass()))); |
|
308 | - } |
|
309 | - if (isset($tag) && $this->tag() !== $tag) { |
|
310 | - throw new \UnexpectedValueException( |
|
311 | - sprintf('Tag %d expected, got %d.', $tag, $this->tag())); |
|
312 | - } |
|
313 | - return $this; |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * Whether element has indefinite length. |
|
318 | - * |
|
319 | - * @return bool |
|
320 | - */ |
|
321 | - public function hasIndefiniteLength(): bool |
|
322 | - { |
|
323 | - return $this->_indefiniteLength; |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * Get self with indefinite length encoding set. |
|
328 | - * |
|
329 | - * @param bool $indefinite True for indefinite length, false for definite length |
|
330 | - * |
|
331 | - * @return self |
|
332 | - */ |
|
333 | - public function withIndefiniteLength(bool $indefinite = true): self |
|
334 | - { |
|
335 | - $obj = clone $this; |
|
336 | - $obj->_indefiniteLength = $indefinite; |
|
337 | - return $obj; |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * {@inheritdoc} |
|
342 | - */ |
|
343 | - final public function asElement(): Element |
|
344 | - { |
|
345 | - return $this; |
|
346 | - } |
|
347 | - |
|
348 | - /** |
|
349 | - * Get element decorated with `UnspecifiedType` object. |
|
350 | - * |
|
351 | - * @return UnspecifiedType |
|
352 | - */ |
|
353 | - public function asUnspecified(): UnspecifiedType |
|
354 | - { |
|
355 | - return new UnspecifiedType($this); |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * Get human readable name for an universal tag. |
|
360 | - * |
|
361 | - * @param int $tag |
|
362 | - * |
|
363 | - * @return string |
|
364 | - */ |
|
365 | - public static function tagToName(int $tag): string |
|
366 | - { |
|
367 | - if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
368 | - return "TAG {$tag}"; |
|
369 | - } |
|
370 | - return self::MAP_TYPE_TO_NAME[$tag]; |
|
371 | - } |
|
372 | - |
|
373 | - /** |
|
374 | - * Get the content encoded in DER. |
|
375 | - * |
|
376 | - * Returns the DER encoded content without identifier and length header octets. |
|
377 | - * |
|
378 | - * @return string |
|
379 | - */ |
|
380 | - abstract protected function _encodedContentDER(): string; |
|
381 | - |
|
382 | - /** |
|
383 | - * Decode type-specific element from DER. |
|
384 | - * |
|
385 | - * @param Identifier $identifier Pre-parsed identifier |
|
386 | - * @param string $data DER data |
|
387 | - * @param int $offset Offset in data to the next byte after identifier |
|
388 | - * |
|
389 | - * @throws DecodeException If decoding fails |
|
390 | - * |
|
391 | - * @return ElementBase |
|
392 | - */ |
|
393 | - protected static function _decodeFromDER(Identifier $identifier, |
|
394 | - string $data, int &$offset): ElementBase |
|
395 | - { |
|
396 | - throw new \BadMethodCallException( |
|
397 | - __METHOD__ . ' must be implemented in derived class.'); |
|
398 | - } |
|
399 | - |
|
400 | - /** |
|
401 | - * Determine the class that implements the type. |
|
402 | - * |
|
403 | - * @param Identifier $identifier |
|
404 | - * |
|
405 | - * @return string Class name |
|
406 | - */ |
|
407 | - protected static function _determineImplClass(Identifier $identifier): string |
|
408 | - { |
|
409 | - switch ($identifier->typeClass()) { |
|
410 | - case Identifier::CLASS_UNIVERSAL: |
|
411 | - $cls = self::_determineUniversalImplClass($identifier->intTag()); |
|
412 | - // constructed strings may be present in BER |
|
413 | - if ($identifier->isConstructed() && |
|
414 | - is_subclass_of($cls, StringType::class)) { |
|
415 | - $cls = ConstructedString::class; |
|
416 | - } |
|
417 | - return $cls; |
|
418 | - case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
419 | - return ContextSpecificType::class; |
|
420 | - case Identifier::CLASS_APPLICATION: |
|
421 | - return ApplicationType::class; |
|
422 | - case Identifier::CLASS_PRIVATE: |
|
423 | - return PrivateType::class; |
|
424 | - } |
|
425 | - throw new \UnexpectedValueException( |
|
426 | - sprintf('%s %d not implemented.', |
|
427 | - Identifier::classToName($identifier->typeClass()), |
|
428 | - $identifier->tag())); |
|
429 | - } |
|
430 | - |
|
431 | - /** |
|
432 | - * Determine the class that implements an universal type of the given tag. |
|
433 | - * |
|
434 | - * @param int $tag |
|
435 | - * |
|
436 | - * @throws \UnexpectedValueException |
|
437 | - * |
|
438 | - * @return string Class name |
|
439 | - */ |
|
440 | - protected static function _determineUniversalImplClass(int $tag): string |
|
441 | - { |
|
442 | - if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
443 | - throw new \UnexpectedValueException( |
|
444 | - "Universal tag {$tag} not implemented."); |
|
445 | - } |
|
446 | - return self::MAP_TAG_TO_CLASS[$tag]; |
|
447 | - } |
|
448 | - |
|
449 | - /** |
|
450 | - * Get textual description of the type for debugging purposes. |
|
451 | - * |
|
452 | - * @return string |
|
453 | - */ |
|
454 | - protected function _typeDescriptorString(): string |
|
455 | - { |
|
456 | - if (Identifier::CLASS_UNIVERSAL === $this->typeClass()) { |
|
457 | - return self::tagToName($this->_typeTag); |
|
458 | - } |
|
459 | - return sprintf('%s TAG %d', Identifier::classToName($this->typeClass()), |
|
460 | - $this->_typeTag); |
|
461 | - } |
|
462 | - |
|
463 | - /** |
|
464 | - * Check whether the element is a concrete type of a given tag. |
|
465 | - * |
|
466 | - * @param int $tag |
|
467 | - * |
|
468 | - * @return bool |
|
469 | - */ |
|
470 | - private function _isConcreteType(int $tag): bool |
|
471 | - { |
|
472 | - // if tag doesn't match |
|
473 | - if ($this->tag() !== $tag) { |
|
474 | - return false; |
|
475 | - } |
|
476 | - // if type is universal check that instance is of a correct class |
|
477 | - if (Identifier::CLASS_UNIVERSAL === $this->typeClass()) { |
|
478 | - $cls = self::_determineUniversalImplClass($tag); |
|
479 | - if (!$this instanceof $cls) { |
|
480 | - return false; |
|
481 | - } |
|
482 | - } |
|
483 | - return true; |
|
484 | - } |
|
485 | - |
|
486 | - /** |
|
487 | - * Check whether the element is a pseudotype. |
|
488 | - * |
|
489 | - * @param int $tag |
|
490 | - * |
|
491 | - * @return bool |
|
492 | - */ |
|
493 | - private function _isPseudoType(int $tag): bool |
|
494 | - { |
|
495 | - switch ($tag) { |
|
496 | - case self::TYPE_STRING: |
|
497 | - return $this instanceof StringType; |
|
498 | - case self::TYPE_TIME: |
|
499 | - return $this instanceof TimeType; |
|
500 | - case self::TYPE_CONSTRUCTED_STRING: |
|
501 | - return $this instanceof ConstructedString; |
|
502 | - } |
|
503 | - return false; |
|
504 | - } |
|
27 | + // Universal type tags |
|
28 | + const TYPE_EOC = 0x00; |
|
29 | + const TYPE_BOOLEAN = 0x01; |
|
30 | + const TYPE_INTEGER = 0x02; |
|
31 | + const TYPE_BIT_STRING = 0x03; |
|
32 | + const TYPE_OCTET_STRING = 0x04; |
|
33 | + const TYPE_NULL = 0x05; |
|
34 | + const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
35 | + const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
36 | + const TYPE_EXTERNAL = 0x08; |
|
37 | + const TYPE_REAL = 0x09; |
|
38 | + const TYPE_ENUMERATED = 0x0a; |
|
39 | + const TYPE_EMBEDDED_PDV = 0x0b; |
|
40 | + const TYPE_UTF8_STRING = 0x0c; |
|
41 | + const TYPE_RELATIVE_OID = 0x0d; |
|
42 | + const TYPE_SEQUENCE = 0x10; |
|
43 | + const TYPE_SET = 0x11; |
|
44 | + const TYPE_NUMERIC_STRING = 0x12; |
|
45 | + const TYPE_PRINTABLE_STRING = 0x13; |
|
46 | + const TYPE_T61_STRING = 0x14; |
|
47 | + const TYPE_VIDEOTEX_STRING = 0x15; |
|
48 | + const TYPE_IA5_STRING = 0x16; |
|
49 | + const TYPE_UTC_TIME = 0x17; |
|
50 | + const TYPE_GENERALIZED_TIME = 0x18; |
|
51 | + const TYPE_GRAPHIC_STRING = 0x19; |
|
52 | + const TYPE_VISIBLE_STRING = 0x1a; |
|
53 | + const TYPE_GENERAL_STRING = 0x1b; |
|
54 | + const TYPE_UNIVERSAL_STRING = 0x1c; |
|
55 | + const TYPE_CHARACTER_STRING = 0x1d; |
|
56 | + const TYPE_BMP_STRING = 0x1e; |
|
57 | + |
|
58 | + /** |
|
59 | + * Mapping from universal type tag to implementation class name. |
|
60 | + * |
|
61 | + * @internal |
|
62 | + * |
|
63 | + * @var array |
|
64 | + */ |
|
65 | + const MAP_TAG_TO_CLASS = [ |
|
66 | + self::TYPE_EOC => Primitive\EOC::class, |
|
67 | + self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
68 | + self::TYPE_INTEGER => Primitive\Integer::class, |
|
69 | + self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
70 | + self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
71 | + self::TYPE_NULL => Primitive\NullType::class, |
|
72 | + self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
73 | + self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
74 | + self::TYPE_REAL => Primitive\Real::class, |
|
75 | + self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
76 | + self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
77 | + self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
78 | + self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
79 | + self::TYPE_SET => Constructed\Set::class, |
|
80 | + self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
81 | + self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
82 | + self::TYPE_T61_STRING => Primitive\T61String::class, |
|
83 | + self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
84 | + self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
85 | + self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
86 | + self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
87 | + self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
88 | + self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
89 | + self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
90 | + self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
91 | + self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
92 | + self::TYPE_BMP_STRING => Primitive\BMPString::class, |
|
93 | + ]; |
|
94 | + |
|
95 | + /** |
|
96 | + * Pseudotype for all string types. |
|
97 | + * |
|
98 | + * May be used as an expectation parameter. |
|
99 | + * |
|
100 | + * @var int |
|
101 | + */ |
|
102 | + const TYPE_STRING = -1; |
|
103 | + |
|
104 | + /** |
|
105 | + * Pseudotype for all time types. |
|
106 | + * |
|
107 | + * May be used as an expectation parameter. |
|
108 | + * |
|
109 | + * @var int |
|
110 | + */ |
|
111 | + const TYPE_TIME = -2; |
|
112 | + |
|
113 | + /** |
|
114 | + * Pseudotype for constructed strings. |
|
115 | + * |
|
116 | + * May be used as an expectation parameter. |
|
117 | + * |
|
118 | + * @var int |
|
119 | + */ |
|
120 | + const TYPE_CONSTRUCTED_STRING = -3; |
|
121 | + |
|
122 | + /** |
|
123 | + * Mapping from universal type tag to human readable name. |
|
124 | + * |
|
125 | + * @internal |
|
126 | + * |
|
127 | + * @var array |
|
128 | + */ |
|
129 | + const MAP_TYPE_TO_NAME = [ |
|
130 | + self::TYPE_EOC => 'EOC', |
|
131 | + self::TYPE_BOOLEAN => 'BOOLEAN', |
|
132 | + self::TYPE_INTEGER => 'INTEGER', |
|
133 | + self::TYPE_BIT_STRING => 'BIT STRING', |
|
134 | + self::TYPE_OCTET_STRING => 'OCTET STRING', |
|
135 | + self::TYPE_NULL => 'NULL', |
|
136 | + self::TYPE_OBJECT_IDENTIFIER => 'OBJECT IDENTIFIER', |
|
137 | + self::TYPE_OBJECT_DESCRIPTOR => 'ObjectDescriptor', |
|
138 | + self::TYPE_EXTERNAL => 'EXTERNAL', |
|
139 | + self::TYPE_REAL => 'REAL', |
|
140 | + self::TYPE_ENUMERATED => 'ENUMERATED', |
|
141 | + self::TYPE_EMBEDDED_PDV => 'EMBEDDED PDV', |
|
142 | + self::TYPE_UTF8_STRING => 'UTF8String', |
|
143 | + self::TYPE_RELATIVE_OID => 'RELATIVE-OID', |
|
144 | + self::TYPE_SEQUENCE => 'SEQUENCE', |
|
145 | + self::TYPE_SET => 'SET', |
|
146 | + self::TYPE_NUMERIC_STRING => 'NumericString', |
|
147 | + self::TYPE_PRINTABLE_STRING => 'PrintableString', |
|
148 | + self::TYPE_T61_STRING => 'T61String', |
|
149 | + self::TYPE_VIDEOTEX_STRING => 'VideotexString', |
|
150 | + self::TYPE_IA5_STRING => 'IA5String', |
|
151 | + self::TYPE_UTC_TIME => 'UTCTime', |
|
152 | + self::TYPE_GENERALIZED_TIME => 'GeneralizedTime', |
|
153 | + self::TYPE_GRAPHIC_STRING => 'GraphicString', |
|
154 | + self::TYPE_VISIBLE_STRING => 'VisibleString', |
|
155 | + self::TYPE_GENERAL_STRING => 'GeneralString', |
|
156 | + self::TYPE_UNIVERSAL_STRING => 'UniversalString', |
|
157 | + self::TYPE_CHARACTER_STRING => 'CHARACTER STRING', |
|
158 | + self::TYPE_BMP_STRING => 'BMPString', |
|
159 | + self::TYPE_STRING => 'Any String', |
|
160 | + self::TYPE_TIME => 'Any Time', |
|
161 | + self::TYPE_CONSTRUCTED_STRING => 'Constructed String', |
|
162 | + ]; |
|
163 | + |
|
164 | + /** |
|
165 | + * Element's type tag. |
|
166 | + * |
|
167 | + * @var int |
|
168 | + */ |
|
169 | + protected $_typeTag; |
|
170 | + |
|
171 | + /** |
|
172 | + * Whether type shall be encoded with indefinite length. |
|
173 | + * |
|
174 | + * @var bool |
|
175 | + */ |
|
176 | + protected $_indefiniteLength = false; |
|
177 | + |
|
178 | + /** |
|
179 | + * {@inheritdoc} |
|
180 | + */ |
|
181 | + abstract public function typeClass(): int; |
|
182 | + |
|
183 | + /** |
|
184 | + * {@inheritdoc} |
|
185 | + */ |
|
186 | + abstract public function isConstructed(): bool; |
|
187 | + |
|
188 | + /** |
|
189 | + * Decode element from DER data. |
|
190 | + * |
|
191 | + * @param string $data DER encoded data |
|
192 | + * @param null|int $offset Reference to the variable that contains offset |
|
193 | + * into the data where to start parsing. |
|
194 | + * Variable is updated to the offset next to the |
|
195 | + * parsed element. If null, start from offset 0. |
|
196 | + * |
|
197 | + * @throws DecodeException If decoding fails |
|
198 | + * @throws \UnexpectedValueException If called in the context of an expected |
|
199 | + * type, but decoding yields another type |
|
200 | + * |
|
201 | + * @return ElementBase |
|
202 | + */ |
|
203 | + public static function fromDER(string $data, int &$offset = null): ElementBase |
|
204 | + { |
|
205 | + $idx = $offset ?? 0; |
|
206 | + // decode identifier |
|
207 | + $identifier = Identifier::fromDER($data, $idx); |
|
208 | + // determine class that implements type specific decoding |
|
209 | + $cls = self::_determineImplClass($identifier); |
|
210 | + try { |
|
211 | + // decode remaining element |
|
212 | + $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
213 | + } catch (\LogicException $e) { |
|
214 | + // rethrow as a RuntimeException for unified exception handling |
|
215 | + throw new DecodeException( |
|
216 | + sprintf('Error while decoding %s.', |
|
217 | + self::tagToName($identifier->intTag())), 0, $e); |
|
218 | + } |
|
219 | + // if called in the context of a concrete class, check |
|
220 | + // that decoded type matches the type of a calling class |
|
221 | + $called_class = get_called_class(); |
|
222 | + if (self::class !== $called_class) { |
|
223 | + if (!$element instanceof $called_class) { |
|
224 | + throw new \UnexpectedValueException( |
|
225 | + sprintf('%s expected, got %s.', $called_class, |
|
226 | + get_class($element))); |
|
227 | + } |
|
228 | + } |
|
229 | + // update offset for the caller |
|
230 | + if (isset($offset)) { |
|
231 | + $offset = $idx; |
|
232 | + } |
|
233 | + return $element; |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * {@inheritdoc} |
|
238 | + */ |
|
239 | + public function toDER(): string |
|
240 | + { |
|
241 | + $identifier = new Identifier($this->typeClass(), |
|
242 | + $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
243 | + $this->_typeTag); |
|
244 | + $content = $this->_encodedContentDER(); |
|
245 | + if ($this->_indefiniteLength) { |
|
246 | + $length = new Length(0, true); |
|
247 | + $eoc = new Primitive\EOC(); |
|
248 | + return $identifier->toDER() . $length->toDER() . $content . $eoc->toDER(); |
|
249 | + } |
|
250 | + $length = new Length(strlen($content)); |
|
251 | + return $identifier->toDER() . $length->toDER() . $content; |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * {@inheritdoc} |
|
256 | + */ |
|
257 | + public function tag(): int |
|
258 | + { |
|
259 | + return $this->_typeTag; |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * {@inheritdoc} |
|
264 | + */ |
|
265 | + public function isType(int $tag): bool |
|
266 | + { |
|
267 | + // if element is context specific |
|
268 | + if (Identifier::CLASS_CONTEXT_SPECIFIC === $this->typeClass()) { |
|
269 | + return false; |
|
270 | + } |
|
271 | + // negative tags identify an abstract pseudotype |
|
272 | + if ($tag < 0) { |
|
273 | + return $this->_isPseudoType($tag); |
|
274 | + } |
|
275 | + return $this->_isConcreteType($tag); |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * {@inheritdoc} |
|
280 | + */ |
|
281 | + public function expectType(int $tag): ElementBase |
|
282 | + { |
|
283 | + if (!$this->isType($tag)) { |
|
284 | + throw new \UnexpectedValueException( |
|
285 | + sprintf('%s expected, got %s.', self::tagToName($tag), |
|
286 | + $this->_typeDescriptorString())); |
|
287 | + } |
|
288 | + return $this; |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * {@inheritdoc} |
|
293 | + */ |
|
294 | + public function isTagged(): bool |
|
295 | + { |
|
296 | + return $this instanceof TaggedType; |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * {@inheritdoc} |
|
301 | + */ |
|
302 | + public function expectTagged(?int $tag = null): TaggedType |
|
303 | + { |
|
304 | + if (!$this->isTagged()) { |
|
305 | + throw new \UnexpectedValueException( |
|
306 | + sprintf('Context specific element expected, got %s.', |
|
307 | + Identifier::classToName($this->typeClass()))); |
|
308 | + } |
|
309 | + if (isset($tag) && $this->tag() !== $tag) { |
|
310 | + throw new \UnexpectedValueException( |
|
311 | + sprintf('Tag %d expected, got %d.', $tag, $this->tag())); |
|
312 | + } |
|
313 | + return $this; |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * Whether element has indefinite length. |
|
318 | + * |
|
319 | + * @return bool |
|
320 | + */ |
|
321 | + public function hasIndefiniteLength(): bool |
|
322 | + { |
|
323 | + return $this->_indefiniteLength; |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * Get self with indefinite length encoding set. |
|
328 | + * |
|
329 | + * @param bool $indefinite True for indefinite length, false for definite length |
|
330 | + * |
|
331 | + * @return self |
|
332 | + */ |
|
333 | + public function withIndefiniteLength(bool $indefinite = true): self |
|
334 | + { |
|
335 | + $obj = clone $this; |
|
336 | + $obj->_indefiniteLength = $indefinite; |
|
337 | + return $obj; |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * {@inheritdoc} |
|
342 | + */ |
|
343 | + final public function asElement(): Element |
|
344 | + { |
|
345 | + return $this; |
|
346 | + } |
|
347 | + |
|
348 | + /** |
|
349 | + * Get element decorated with `UnspecifiedType` object. |
|
350 | + * |
|
351 | + * @return UnspecifiedType |
|
352 | + */ |
|
353 | + public function asUnspecified(): UnspecifiedType |
|
354 | + { |
|
355 | + return new UnspecifiedType($this); |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * Get human readable name for an universal tag. |
|
360 | + * |
|
361 | + * @param int $tag |
|
362 | + * |
|
363 | + * @return string |
|
364 | + */ |
|
365 | + public static function tagToName(int $tag): string |
|
366 | + { |
|
367 | + if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
368 | + return "TAG {$tag}"; |
|
369 | + } |
|
370 | + return self::MAP_TYPE_TO_NAME[$tag]; |
|
371 | + } |
|
372 | + |
|
373 | + /** |
|
374 | + * Get the content encoded in DER. |
|
375 | + * |
|
376 | + * Returns the DER encoded content without identifier and length header octets. |
|
377 | + * |
|
378 | + * @return string |
|
379 | + */ |
|
380 | + abstract protected function _encodedContentDER(): string; |
|
381 | + |
|
382 | + /** |
|
383 | + * Decode type-specific element from DER. |
|
384 | + * |
|
385 | + * @param Identifier $identifier Pre-parsed identifier |
|
386 | + * @param string $data DER data |
|
387 | + * @param int $offset Offset in data to the next byte after identifier |
|
388 | + * |
|
389 | + * @throws DecodeException If decoding fails |
|
390 | + * |
|
391 | + * @return ElementBase |
|
392 | + */ |
|
393 | + protected static function _decodeFromDER(Identifier $identifier, |
|
394 | + string $data, int &$offset): ElementBase |
|
395 | + { |
|
396 | + throw new \BadMethodCallException( |
|
397 | + __METHOD__ . ' must be implemented in derived class.'); |
|
398 | + } |
|
399 | + |
|
400 | + /** |
|
401 | + * Determine the class that implements the type. |
|
402 | + * |
|
403 | + * @param Identifier $identifier |
|
404 | + * |
|
405 | + * @return string Class name |
|
406 | + */ |
|
407 | + protected static function _determineImplClass(Identifier $identifier): string |
|
408 | + { |
|
409 | + switch ($identifier->typeClass()) { |
|
410 | + case Identifier::CLASS_UNIVERSAL: |
|
411 | + $cls = self::_determineUniversalImplClass($identifier->intTag()); |
|
412 | + // constructed strings may be present in BER |
|
413 | + if ($identifier->isConstructed() && |
|
414 | + is_subclass_of($cls, StringType::class)) { |
|
415 | + $cls = ConstructedString::class; |
|
416 | + } |
|
417 | + return $cls; |
|
418 | + case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
419 | + return ContextSpecificType::class; |
|
420 | + case Identifier::CLASS_APPLICATION: |
|
421 | + return ApplicationType::class; |
|
422 | + case Identifier::CLASS_PRIVATE: |
|
423 | + return PrivateType::class; |
|
424 | + } |
|
425 | + throw new \UnexpectedValueException( |
|
426 | + sprintf('%s %d not implemented.', |
|
427 | + Identifier::classToName($identifier->typeClass()), |
|
428 | + $identifier->tag())); |
|
429 | + } |
|
430 | + |
|
431 | + /** |
|
432 | + * Determine the class that implements an universal type of the given tag. |
|
433 | + * |
|
434 | + * @param int $tag |
|
435 | + * |
|
436 | + * @throws \UnexpectedValueException |
|
437 | + * |
|
438 | + * @return string Class name |
|
439 | + */ |
|
440 | + protected static function _determineUniversalImplClass(int $tag): string |
|
441 | + { |
|
442 | + if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
443 | + throw new \UnexpectedValueException( |
|
444 | + "Universal tag {$tag} not implemented."); |
|
445 | + } |
|
446 | + return self::MAP_TAG_TO_CLASS[$tag]; |
|
447 | + } |
|
448 | + |
|
449 | + /** |
|
450 | + * Get textual description of the type for debugging purposes. |
|
451 | + * |
|
452 | + * @return string |
|
453 | + */ |
|
454 | + protected function _typeDescriptorString(): string |
|
455 | + { |
|
456 | + if (Identifier::CLASS_UNIVERSAL === $this->typeClass()) { |
|
457 | + return self::tagToName($this->_typeTag); |
|
458 | + } |
|
459 | + return sprintf('%s TAG %d', Identifier::classToName($this->typeClass()), |
|
460 | + $this->_typeTag); |
|
461 | + } |
|
462 | + |
|
463 | + /** |
|
464 | + * Check whether the element is a concrete type of a given tag. |
|
465 | + * |
|
466 | + * @param int $tag |
|
467 | + * |
|
468 | + * @return bool |
|
469 | + */ |
|
470 | + private function _isConcreteType(int $tag): bool |
|
471 | + { |
|
472 | + // if tag doesn't match |
|
473 | + if ($this->tag() !== $tag) { |
|
474 | + return false; |
|
475 | + } |
|
476 | + // if type is universal check that instance is of a correct class |
|
477 | + if (Identifier::CLASS_UNIVERSAL === $this->typeClass()) { |
|
478 | + $cls = self::_determineUniversalImplClass($tag); |
|
479 | + if (!$this instanceof $cls) { |
|
480 | + return false; |
|
481 | + } |
|
482 | + } |
|
483 | + return true; |
|
484 | + } |
|
485 | + |
|
486 | + /** |
|
487 | + * Check whether the element is a pseudotype. |
|
488 | + * |
|
489 | + * @param int $tag |
|
490 | + * |
|
491 | + * @return bool |
|
492 | + */ |
|
493 | + private function _isPseudoType(int $tag): bool |
|
494 | + { |
|
495 | + switch ($tag) { |
|
496 | + case self::TYPE_STRING: |
|
497 | + return $this instanceof StringType; |
|
498 | + case self::TYPE_TIME: |
|
499 | + return $this instanceof TimeType; |
|
500 | + case self::TYPE_CONSTRUCTED_STRING: |
|
501 | + return $this instanceof ConstructedString; |
|
502 | + } |
|
503 | + return false; |
|
504 | + } |
|
505 | 505 | } |