@@ -12,219 +12,219 @@ |
||
12 | 12 | */ |
13 | 13 | class Length implements Encodable |
14 | 14 | { |
15 | - /** |
|
16 | - * Length. |
|
17 | - * |
|
18 | - * @var string |
|
19 | - */ |
|
20 | - private $_length; |
|
15 | + /** |
|
16 | + * Length. |
|
17 | + * |
|
18 | + * @var string |
|
19 | + */ |
|
20 | + private $_length; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Whether length is indefinite. |
|
24 | - * |
|
25 | - * @var boolean |
|
26 | - */ |
|
27 | - private $_indefinite; |
|
22 | + /** |
|
23 | + * Whether length is indefinite. |
|
24 | + * |
|
25 | + * @var boolean |
|
26 | + */ |
|
27 | + private $_indefinite; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Length as an integer type. |
|
31 | - * |
|
32 | - * @internal Lazily initialized |
|
33 | - * @var int |
|
34 | - */ |
|
35 | - private $_intLength; |
|
29 | + /** |
|
30 | + * Length as an integer type. |
|
31 | + * |
|
32 | + * @internal Lazily initialized |
|
33 | + * @var int |
|
34 | + */ |
|
35 | + private $_intLength; |
|
36 | 36 | |
37 | - /** |
|
38 | - * Constructor. |
|
39 | - * |
|
40 | - * @param int|string $length Length |
|
41 | - * @param boolean $indefinite Whether length is indefinite |
|
42 | - */ |
|
43 | - public function __construct($length, bool $indefinite = false) |
|
44 | - { |
|
45 | - $this->_length = strval($length); |
|
46 | - $this->_indefinite = $indefinite; |
|
47 | - } |
|
37 | + /** |
|
38 | + * Constructor. |
|
39 | + * |
|
40 | + * @param int|string $length Length |
|
41 | + * @param boolean $indefinite Whether length is indefinite |
|
42 | + */ |
|
43 | + public function __construct($length, bool $indefinite = false) |
|
44 | + { |
|
45 | + $this->_length = strval($length); |
|
46 | + $this->_indefinite = $indefinite; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Decode length component from DER data. |
|
51 | - * |
|
52 | - * @param string $data DER encoded data |
|
53 | - * @param int|null $offset Reference to the variable that contains offset |
|
54 | - * into the data where to start parsing. Variable is updated to |
|
55 | - * the offset next to the parsed length component. If null, start |
|
56 | - * from offset 0. |
|
57 | - * @throws DecodeException If decoding fails |
|
58 | - * @return self |
|
59 | - */ |
|
60 | - public static function fromDER(string $data, int &$offset = null): self |
|
61 | - { |
|
62 | - $idx = $offset ? $offset : 0; |
|
63 | - $datalen = strlen($data); |
|
64 | - if ($idx >= $datalen) { |
|
65 | - throw new DecodeException("Invalid offset."); |
|
66 | - } |
|
67 | - $indefinite = false; |
|
68 | - $byte = ord($data[$idx++]); |
|
69 | - // bits 7 to 1 |
|
70 | - $length = (0x7f & $byte); |
|
71 | - // long form |
|
72 | - if (0x80 & $byte) { |
|
73 | - if (!$length) { |
|
74 | - $indefinite = true; |
|
75 | - } else { |
|
76 | - if ($idx + $length > $datalen) { |
|
77 | - throw new DecodeException("Too many length octets."); |
|
78 | - } |
|
79 | - $length = self::_decodeLongFormLength($length, $data, $idx); |
|
80 | - } |
|
81 | - } |
|
82 | - if (isset($offset)) { |
|
83 | - $offset = $idx; |
|
84 | - } |
|
85 | - return new self($length, $indefinite); |
|
86 | - } |
|
49 | + /** |
|
50 | + * Decode length component from DER data. |
|
51 | + * |
|
52 | + * @param string $data DER encoded data |
|
53 | + * @param int|null $offset Reference to the variable that contains offset |
|
54 | + * into the data where to start parsing. Variable is updated to |
|
55 | + * the offset next to the parsed length component. If null, start |
|
56 | + * from offset 0. |
|
57 | + * @throws DecodeException If decoding fails |
|
58 | + * @return self |
|
59 | + */ |
|
60 | + public static function fromDER(string $data, int &$offset = null): self |
|
61 | + { |
|
62 | + $idx = $offset ? $offset : 0; |
|
63 | + $datalen = strlen($data); |
|
64 | + if ($idx >= $datalen) { |
|
65 | + throw new DecodeException("Invalid offset."); |
|
66 | + } |
|
67 | + $indefinite = false; |
|
68 | + $byte = ord($data[$idx++]); |
|
69 | + // bits 7 to 1 |
|
70 | + $length = (0x7f & $byte); |
|
71 | + // long form |
|
72 | + if (0x80 & $byte) { |
|
73 | + if (!$length) { |
|
74 | + $indefinite = true; |
|
75 | + } else { |
|
76 | + if ($idx + $length > $datalen) { |
|
77 | + throw new DecodeException("Too many length octets."); |
|
78 | + } |
|
79 | + $length = self::_decodeLongFormLength($length, $data, $idx); |
|
80 | + } |
|
81 | + } |
|
82 | + if (isset($offset)) { |
|
83 | + $offset = $idx; |
|
84 | + } |
|
85 | + return new self($length, $indefinite); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Decode long form length. |
|
90 | - * |
|
91 | - * @param int $length Number of octets |
|
92 | - * @param string $data Data |
|
93 | - * @param int $offset Reference to the variable containing offset to the |
|
94 | - * data. |
|
95 | - * @throws DecodeException If decoding fails |
|
96 | - * @return string Integer as a string |
|
97 | - */ |
|
98 | - private static function _decodeLongFormLength(int $length, string $data, |
|
99 | - int &$offset): string |
|
100 | - { |
|
101 | - // first octet must not be 0xff (spec 8.1.3.5c) |
|
102 | - if ($length == 127) { |
|
103 | - throw new DecodeException("Invalid number of length octets."); |
|
104 | - } |
|
105 | - $num = gmp_init(0, 10); |
|
106 | - while (--$length >= 0) { |
|
107 | - $byte = ord($data[$offset++]); |
|
108 | - $num <<= 8; |
|
109 | - $num |= $byte; |
|
110 | - } |
|
88 | + /** |
|
89 | + * Decode long form length. |
|
90 | + * |
|
91 | + * @param int $length Number of octets |
|
92 | + * @param string $data Data |
|
93 | + * @param int $offset Reference to the variable containing offset to the |
|
94 | + * data. |
|
95 | + * @throws DecodeException If decoding fails |
|
96 | + * @return string Integer as a string |
|
97 | + */ |
|
98 | + private static function _decodeLongFormLength(int $length, string $data, |
|
99 | + int &$offset): string |
|
100 | + { |
|
101 | + // first octet must not be 0xff (spec 8.1.3.5c) |
|
102 | + if ($length == 127) { |
|
103 | + throw new DecodeException("Invalid number of length octets."); |
|
104 | + } |
|
105 | + $num = gmp_init(0, 10); |
|
106 | + while (--$length >= 0) { |
|
107 | + $byte = ord($data[$offset++]); |
|
108 | + $num <<= 8; |
|
109 | + $num |= $byte; |
|
110 | + } |
|
111 | 111 | |
112 | - return gmp_strval($num); |
|
113 | - } |
|
112 | + return gmp_strval($num); |
|
113 | + } |
|
114 | 114 | |
115 | - /** |
|
116 | - * Decode length from DER. |
|
117 | - * |
|
118 | - * Throws an exception if length doesn't match with expected or if data |
|
119 | - * doesn't contain enough bytes. |
|
120 | - * |
|
121 | - * @see self::fromDER |
|
122 | - * @param string $data DER data |
|
123 | - * @param int $offset Reference to the offset variable |
|
124 | - * @param int|null $expected Expected length, null to bypass checking |
|
125 | - * @throws DecodeException If decoding or expectation fails |
|
126 | - * @return self |
|
127 | - */ |
|
128 | - public static function expectFromDER(string $data, int &$offset, |
|
129 | - int $expected = null): self |
|
130 | - { |
|
131 | - $idx = $offset; |
|
132 | - $length = self::fromDER($data, $idx); |
|
133 | - // DER encoding must have definite length (spec 10.1) |
|
134 | - if ($length->isIndefinite()) { |
|
135 | - throw new DecodeException("DER encoding must have definite length."); |
|
136 | - } |
|
137 | - // if certain length was expected |
|
138 | - if (isset($expected) && $expected != $length->intLength()) { |
|
139 | - throw new DecodeException( |
|
140 | - sprintf("Expected length %d, got %d.", $expected, |
|
141 | - $length->intLength())); |
|
142 | - } |
|
143 | - // check that enough data is available |
|
144 | - if (strlen($data) < $idx + $length->intLength()) { |
|
145 | - throw new DecodeException( |
|
146 | - sprintf("Length %d overflows data, %d bytes left.", |
|
147 | - $length->intLength(), strlen($data) - $idx)); |
|
148 | - } |
|
149 | - $offset = $idx; |
|
150 | - return $length; |
|
151 | - } |
|
115 | + /** |
|
116 | + * Decode length from DER. |
|
117 | + * |
|
118 | + * Throws an exception if length doesn't match with expected or if data |
|
119 | + * doesn't contain enough bytes. |
|
120 | + * |
|
121 | + * @see self::fromDER |
|
122 | + * @param string $data DER data |
|
123 | + * @param int $offset Reference to the offset variable |
|
124 | + * @param int|null $expected Expected length, null to bypass checking |
|
125 | + * @throws DecodeException If decoding or expectation fails |
|
126 | + * @return self |
|
127 | + */ |
|
128 | + public static function expectFromDER(string $data, int &$offset, |
|
129 | + int $expected = null): self |
|
130 | + { |
|
131 | + $idx = $offset; |
|
132 | + $length = self::fromDER($data, $idx); |
|
133 | + // DER encoding must have definite length (spec 10.1) |
|
134 | + if ($length->isIndefinite()) { |
|
135 | + throw new DecodeException("DER encoding must have definite length."); |
|
136 | + } |
|
137 | + // if certain length was expected |
|
138 | + if (isset($expected) && $expected != $length->intLength()) { |
|
139 | + throw new DecodeException( |
|
140 | + sprintf("Expected length %d, got %d.", $expected, |
|
141 | + $length->intLength())); |
|
142 | + } |
|
143 | + // check that enough data is available |
|
144 | + if (strlen($data) < $idx + $length->intLength()) { |
|
145 | + throw new DecodeException( |
|
146 | + sprintf("Length %d overflows data, %d bytes left.", |
|
147 | + $length->intLength(), strlen($data) - $idx)); |
|
148 | + } |
|
149 | + $offset = $idx; |
|
150 | + return $length; |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * |
|
155 | - * @see Encodable::toDER() |
|
156 | - * @throws \DomainException If length is too large to encode |
|
157 | - * @return string |
|
158 | - */ |
|
159 | - public function toDER(): string |
|
160 | - { |
|
161 | - $bytes = []; |
|
162 | - if ($this->_indefinite) { |
|
163 | - $bytes[] = 0x80; |
|
164 | - } else { |
|
165 | - $num = gmp_init($this->_length, 10); |
|
166 | - // long form |
|
167 | - if ($num > 127) { |
|
168 | - $octets = []; |
|
169 | - for (; $num > 0; $num >>= 8) { |
|
170 | - $octets[] = gmp_intval(0xff & $num); |
|
171 | - } |
|
172 | - $count = count($octets); |
|
173 | - // first octet must not be 0xff |
|
174 | - if ($count >= 127) { |
|
175 | - throw new \DomainException("Too many length octets."); |
|
176 | - } |
|
177 | - $bytes[] = 0x80 | $count; |
|
178 | - foreach (array_reverse($octets) as $octet) { |
|
179 | - $bytes[] = $octet; |
|
180 | - } |
|
181 | - } else { // short form |
|
182 | - $bytes[] = gmp_intval($num); |
|
183 | - } |
|
184 | - } |
|
185 | - return pack("C*", ...$bytes); |
|
186 | - } |
|
153 | + /** |
|
154 | + * |
|
155 | + * @see Encodable::toDER() |
|
156 | + * @throws \DomainException If length is too large to encode |
|
157 | + * @return string |
|
158 | + */ |
|
159 | + public function toDER(): string |
|
160 | + { |
|
161 | + $bytes = []; |
|
162 | + if ($this->_indefinite) { |
|
163 | + $bytes[] = 0x80; |
|
164 | + } else { |
|
165 | + $num = gmp_init($this->_length, 10); |
|
166 | + // long form |
|
167 | + if ($num > 127) { |
|
168 | + $octets = []; |
|
169 | + for (; $num > 0; $num >>= 8) { |
|
170 | + $octets[] = gmp_intval(0xff & $num); |
|
171 | + } |
|
172 | + $count = count($octets); |
|
173 | + // first octet must not be 0xff |
|
174 | + if ($count >= 127) { |
|
175 | + throw new \DomainException("Too many length octets."); |
|
176 | + } |
|
177 | + $bytes[] = 0x80 | $count; |
|
178 | + foreach (array_reverse($octets) as $octet) { |
|
179 | + $bytes[] = $octet; |
|
180 | + } |
|
181 | + } else { // short form |
|
182 | + $bytes[] = gmp_intval($num); |
|
183 | + } |
|
184 | + } |
|
185 | + return pack("C*", ...$bytes); |
|
186 | + } |
|
187 | 187 | |
188 | - /** |
|
189 | - * Get the length. |
|
190 | - * |
|
191 | - * @throws \LogicException If length is indefinite |
|
192 | - * @return string Length as an integer string |
|
193 | - */ |
|
194 | - public function length(): string |
|
195 | - { |
|
196 | - if ($this->_indefinite) { |
|
197 | - throw new \LogicException("Length is indefinite."); |
|
198 | - } |
|
199 | - return $this->_length; |
|
200 | - } |
|
188 | + /** |
|
189 | + * Get the length. |
|
190 | + * |
|
191 | + * @throws \LogicException If length is indefinite |
|
192 | + * @return string Length as an integer string |
|
193 | + */ |
|
194 | + public function length(): string |
|
195 | + { |
|
196 | + if ($this->_indefinite) { |
|
197 | + throw new \LogicException("Length is indefinite."); |
|
198 | + } |
|
199 | + return $this->_length; |
|
200 | + } |
|
201 | 201 | |
202 | - /** |
|
203 | - * Get the length as an integer. |
|
204 | - * |
|
205 | - * @throws \LogicException If length is indefinite |
|
206 | - * @throws \RuntimeException If length overflows integer size |
|
207 | - * @return int |
|
208 | - */ |
|
209 | - public function intLength(): int |
|
210 | - { |
|
211 | - if (!isset($this->_intLength)) { |
|
212 | - $num = gmp_init($this->length(), 10); |
|
213 | - if (gmp_cmp($num, gmp_init(PHP_INT_MAX, 10)) >= 0) { |
|
214 | - throw new \RuntimeException("Integer overflow."); |
|
215 | - } |
|
216 | - $this->_intLength = gmp_intval($num); |
|
217 | - } |
|
218 | - return $this->_intLength; |
|
219 | - } |
|
202 | + /** |
|
203 | + * Get the length as an integer. |
|
204 | + * |
|
205 | + * @throws \LogicException If length is indefinite |
|
206 | + * @throws \RuntimeException If length overflows integer size |
|
207 | + * @return int |
|
208 | + */ |
|
209 | + public function intLength(): int |
|
210 | + { |
|
211 | + if (!isset($this->_intLength)) { |
|
212 | + $num = gmp_init($this->length(), 10); |
|
213 | + if (gmp_cmp($num, gmp_init(PHP_INT_MAX, 10)) >= 0) { |
|
214 | + throw new \RuntimeException("Integer overflow."); |
|
215 | + } |
|
216 | + $this->_intLength = gmp_intval($num); |
|
217 | + } |
|
218 | + return $this->_intLength; |
|
219 | + } |
|
220 | 220 | |
221 | - /** |
|
222 | - * Whether length is indefinite. |
|
223 | - * |
|
224 | - * @return boolean |
|
225 | - */ |
|
226 | - public function isIndefinite(): bool |
|
227 | - { |
|
228 | - return $this->_indefinite; |
|
229 | - } |
|
221 | + /** |
|
222 | + * Whether length is indefinite. |
|
223 | + * |
|
224 | + * @return boolean |
|
225 | + */ |
|
226 | + public function isIndefinite(): bool |
|
227 | + { |
|
228 | + return $this->_indefinite; |
|
229 | + } |
|
230 | 230 | } |
@@ -17,85 +17,85 @@ |
||
17 | 17 | */ |
18 | 18 | abstract class TaggedType extends Element |
19 | 19 | { |
20 | - /** |
|
21 | - * |
|
22 | - * {@inheritdoc} |
|
23 | - */ |
|
24 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
25 | - int &$offset): ElementBase |
|
26 | - { |
|
27 | - $idx = $offset; |
|
28 | - $type = new DERTaggedType($identifier, $data, $idx); |
|
29 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
30 | - $offset = $idx + $length; |
|
31 | - return $type; |
|
32 | - } |
|
20 | + /** |
|
21 | + * |
|
22 | + * {@inheritdoc} |
|
23 | + */ |
|
24 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
25 | + int &$offset): ElementBase |
|
26 | + { |
|
27 | + $idx = $offset; |
|
28 | + $type = new DERTaggedType($identifier, $data, $idx); |
|
29 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
30 | + $offset = $idx + $length; |
|
31 | + return $type; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Check whether element supports explicit tagging. |
|
36 | - * |
|
37 | - * @param int|null $expectedTag Optional outer tag expectation |
|
38 | - * @throws \UnexpectedValueException If expectation fails |
|
39 | - * @return ExplicitTagging |
|
40 | - */ |
|
41 | - public function expectExplicit(int $expectedTag = null): ExplicitTagging |
|
42 | - { |
|
43 | - $el = $this; |
|
44 | - if (!$el instanceof ExplicitTagging) { |
|
45 | - throw new \UnexpectedValueException( |
|
46 | - "Element doesn't implement explicit tagging."); |
|
47 | - } |
|
48 | - if (isset($expectedTag)) { |
|
49 | - $el->expectTagged($expectedTag); |
|
50 | - } |
|
51 | - return $el; |
|
52 | - } |
|
34 | + /** |
|
35 | + * Check whether element supports explicit tagging. |
|
36 | + * |
|
37 | + * @param int|null $expectedTag Optional outer tag expectation |
|
38 | + * @throws \UnexpectedValueException If expectation fails |
|
39 | + * @return ExplicitTagging |
|
40 | + */ |
|
41 | + public function expectExplicit(int $expectedTag = null): ExplicitTagging |
|
42 | + { |
|
43 | + $el = $this; |
|
44 | + if (!$el instanceof ExplicitTagging) { |
|
45 | + throw new \UnexpectedValueException( |
|
46 | + "Element doesn't implement explicit tagging."); |
|
47 | + } |
|
48 | + if (isset($expectedTag)) { |
|
49 | + $el->expectTagged($expectedTag); |
|
50 | + } |
|
51 | + return $el; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Get the wrapped inner element employing explicit tagging. |
|
56 | - * |
|
57 | - * @param int|null $expectedTag Optional outer tag expectation |
|
58 | - * @throws \UnexpectedValueException If expectation fails |
|
59 | - * @return UnspecifiedType |
|
60 | - */ |
|
61 | - public function asExplicit($expectedTag = null): UnspecifiedType |
|
62 | - { |
|
63 | - return $this->expectExplicit($expectedTag)->explicit(); |
|
64 | - } |
|
54 | + /** |
|
55 | + * Get the wrapped inner element employing explicit tagging. |
|
56 | + * |
|
57 | + * @param int|null $expectedTag Optional outer tag expectation |
|
58 | + * @throws \UnexpectedValueException If expectation fails |
|
59 | + * @return UnspecifiedType |
|
60 | + */ |
|
61 | + public function asExplicit($expectedTag = null): UnspecifiedType |
|
62 | + { |
|
63 | + return $this->expectExplicit($expectedTag)->explicit(); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Check whether element supports implicit tagging. |
|
68 | - * |
|
69 | - * @param int|null $expectedTag Optional outer tag expectation |
|
70 | - * @throws \UnexpectedValueException If expectation fails |
|
71 | - * @return ImplicitTagging |
|
72 | - */ |
|
73 | - public function expectImplicit($expectedTag = null): ImplicitTagging |
|
74 | - { |
|
75 | - $el = $this; |
|
76 | - if (!$el instanceof ImplicitTagging) { |
|
77 | - throw new \UnexpectedValueException( |
|
78 | - "Element doesn't implement implicit tagging."); |
|
79 | - } |
|
80 | - if (isset($expectedTag)) { |
|
81 | - $el->expectTagged($expectedTag); |
|
82 | - } |
|
83 | - return $el; |
|
84 | - } |
|
66 | + /** |
|
67 | + * Check whether element supports implicit tagging. |
|
68 | + * |
|
69 | + * @param int|null $expectedTag Optional outer tag expectation |
|
70 | + * @throws \UnexpectedValueException If expectation fails |
|
71 | + * @return ImplicitTagging |
|
72 | + */ |
|
73 | + public function expectImplicit($expectedTag = null): ImplicitTagging |
|
74 | + { |
|
75 | + $el = $this; |
|
76 | + if (!$el instanceof ImplicitTagging) { |
|
77 | + throw new \UnexpectedValueException( |
|
78 | + "Element doesn't implement implicit tagging."); |
|
79 | + } |
|
80 | + if (isset($expectedTag)) { |
|
81 | + $el->expectTagged($expectedTag); |
|
82 | + } |
|
83 | + return $el; |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Get the wrapped inner element employing implicit tagging. |
|
88 | - * |
|
89 | - * @param int $tag Type tag of the inner element |
|
90 | - * @param int|null $expectedTag Optional outer tag expectation |
|
91 | - * @param int $expectedClass Optional inner type class expectation |
|
92 | - * @throws \UnexpectedValueException If expectation fails |
|
93 | - * @return UnspecifiedType |
|
94 | - */ |
|
95 | - public function asImplicit($tag, $expectedTag = null, |
|
96 | - int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
97 | - { |
|
98 | - return $this->expectImplicit($expectedTag)->implicit($tag, |
|
99 | - $expectedClass); |
|
100 | - } |
|
86 | + /** |
|
87 | + * Get the wrapped inner element employing implicit tagging. |
|
88 | + * |
|
89 | + * @param int $tag Type tag of the inner element |
|
90 | + * @param int|null $expectedTag Optional outer tag expectation |
|
91 | + * @param int $expectedClass Optional inner type class expectation |
|
92 | + * @throws \UnexpectedValueException If expectation fails |
|
93 | + * @return UnspecifiedType |
|
94 | + */ |
|
95 | + public function asImplicit($tag, $expectedTag = null, |
|
96 | + int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
97 | + { |
|
98 | + return $this->expectImplicit($expectedTag)->implicit($tag, |
|
99 | + $expectedClass); |
|
100 | + } |
|
101 | 101 | } |
@@ -14,336 +14,336 @@ |
||
14 | 14 | * Base class for the constructed types. |
15 | 15 | */ |
16 | 16 | abstract class Structure extends Element implements |
17 | - \Countable, |
|
18 | - \IteratorAggregate |
|
17 | + \Countable, |
|
18 | + \IteratorAggregate |
|
19 | 19 | { |
20 | - use UniversalClass; |
|
20 | + use UniversalClass; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Array of elements in the structure. |
|
24 | - * |
|
25 | - * @var Element[] $_elements |
|
26 | - */ |
|
27 | - protected $_elements; |
|
22 | + /** |
|
23 | + * Array of elements in the structure. |
|
24 | + * |
|
25 | + * @var Element[] $_elements |
|
26 | + */ |
|
27 | + protected $_elements; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Lookup table for the tagged elements. |
|
31 | - * |
|
32 | - * @var TaggedType[]|null $_taggedMap |
|
33 | - */ |
|
34 | - private $_taggedMap; |
|
29 | + /** |
|
30 | + * Lookup table for the tagged elements. |
|
31 | + * |
|
32 | + * @var TaggedType[]|null $_taggedMap |
|
33 | + */ |
|
34 | + private $_taggedMap; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Cache variable of elements wrapped into UnspecifiedType objects. |
|
38 | - * |
|
39 | - * @var UnspecifiedType[]|null $_unspecifiedTypes |
|
40 | - */ |
|
41 | - private $_unspecifiedTypes; |
|
36 | + /** |
|
37 | + * Cache variable of elements wrapped into UnspecifiedType objects. |
|
38 | + * |
|
39 | + * @var UnspecifiedType[]|null $_unspecifiedTypes |
|
40 | + */ |
|
41 | + private $_unspecifiedTypes; |
|
42 | 42 | |
43 | - /** |
|
44 | - * Constructor. |
|
45 | - * |
|
46 | - * @param Element ...$elements Any number of elements |
|
47 | - */ |
|
48 | - public function __construct(Element ...$elements) |
|
49 | - { |
|
50 | - $this->_elements = $elements; |
|
51 | - } |
|
43 | + /** |
|
44 | + * Constructor. |
|
45 | + * |
|
46 | + * @param Element ...$elements Any number of elements |
|
47 | + */ |
|
48 | + public function __construct(Element ...$elements) |
|
49 | + { |
|
50 | + $this->_elements = $elements; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Clone magic method. |
|
55 | - */ |
|
56 | - public function __clone() |
|
57 | - { |
|
58 | - // clear cache-variables |
|
59 | - $this->_taggedMap = null; |
|
60 | - $this->_unspecifiedTypes = null; |
|
61 | - } |
|
53 | + /** |
|
54 | + * Clone magic method. |
|
55 | + */ |
|
56 | + public function __clone() |
|
57 | + { |
|
58 | + // clear cache-variables |
|
59 | + $this->_taggedMap = null; |
|
60 | + $this->_unspecifiedTypes = null; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * |
|
65 | - * @see \ASN1\Element::isConstructed() |
|
66 | - * @return bool |
|
67 | - */ |
|
68 | - public function isConstructed(): bool |
|
69 | - { |
|
70 | - return true; |
|
71 | - } |
|
63 | + /** |
|
64 | + * |
|
65 | + * @see \ASN1\Element::isConstructed() |
|
66 | + * @return bool |
|
67 | + */ |
|
68 | + public function isConstructed(): bool |
|
69 | + { |
|
70 | + return true; |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * |
|
75 | - * @see \ASN1\Element::_encodedContentDER() |
|
76 | - * @return string |
|
77 | - */ |
|
78 | - protected function _encodedContentDER(): string |
|
79 | - { |
|
80 | - $data = ""; |
|
81 | - foreach ($this->_elements as $element) { |
|
82 | - $data .= $element->toDER(); |
|
83 | - } |
|
84 | - return $data; |
|
85 | - } |
|
73 | + /** |
|
74 | + * |
|
75 | + * @see \ASN1\Element::_encodedContentDER() |
|
76 | + * @return string |
|
77 | + */ |
|
78 | + protected function _encodedContentDER(): string |
|
79 | + { |
|
80 | + $data = ""; |
|
81 | + foreach ($this->_elements as $element) { |
|
82 | + $data .= $element->toDER(); |
|
83 | + } |
|
84 | + return $data; |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * |
|
89 | - * {@inheritdoc} |
|
90 | - * @see \ASN1\Element::_decodeFromDER() |
|
91 | - * @return self |
|
92 | - */ |
|
93 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
94 | - int &$offset): ElementBase |
|
95 | - { |
|
96 | - $idx = $offset; |
|
97 | - if (!$identifier->isConstructed()) { |
|
98 | - throw new DecodeException( |
|
99 | - "Structured element must have constructed bit set."); |
|
100 | - } |
|
101 | - $length = Length::expectFromDER($data, $idx); |
|
102 | - $end = $idx + $length->intLength(); |
|
103 | - $elements = []; |
|
104 | - while ($idx < $end) { |
|
105 | - $elements[] = Element::fromDER($data, $idx); |
|
106 | - // check that element didn't overflow length |
|
107 | - if ($idx > $end) { |
|
108 | - throw new DecodeException( |
|
109 | - "Structure's content overflows length."); |
|
110 | - } |
|
111 | - } |
|
112 | - $offset = $idx; |
|
113 | - // return instance by static late binding |
|
114 | - return new static(...$elements); |
|
115 | - } |
|
87 | + /** |
|
88 | + * |
|
89 | + * {@inheritdoc} |
|
90 | + * @see \ASN1\Element::_decodeFromDER() |
|
91 | + * @return self |
|
92 | + */ |
|
93 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
94 | + int &$offset): ElementBase |
|
95 | + { |
|
96 | + $idx = $offset; |
|
97 | + if (!$identifier->isConstructed()) { |
|
98 | + throw new DecodeException( |
|
99 | + "Structured element must have constructed bit set."); |
|
100 | + } |
|
101 | + $length = Length::expectFromDER($data, $idx); |
|
102 | + $end = $idx + $length->intLength(); |
|
103 | + $elements = []; |
|
104 | + while ($idx < $end) { |
|
105 | + $elements[] = Element::fromDER($data, $idx); |
|
106 | + // check that element didn't overflow length |
|
107 | + if ($idx > $end) { |
|
108 | + throw new DecodeException( |
|
109 | + "Structure's content overflows length."); |
|
110 | + } |
|
111 | + } |
|
112 | + $offset = $idx; |
|
113 | + // return instance by static late binding |
|
114 | + return new static(...$elements); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Explode DER structure to DER encoded components that it contains. |
|
119 | - * |
|
120 | - * @param string $data |
|
121 | - * @throws DecodeException |
|
122 | - * @return string[] |
|
123 | - */ |
|
124 | - public static function explodeDER(string $data): array |
|
125 | - { |
|
126 | - $offset = 0; |
|
127 | - $identifier = Identifier::fromDER($data, $offset); |
|
128 | - if (!$identifier->isConstructed()) { |
|
129 | - throw new DecodeException("Element is not constructed."); |
|
130 | - } |
|
131 | - $length = Length::expectFromDER($data, $offset)->intLength(); |
|
132 | - $end = $offset + $length; |
|
133 | - $parts = []; |
|
134 | - while ($offset < $end) { |
|
135 | - // start of the element |
|
136 | - $idx = $offset; |
|
137 | - // skip identifier |
|
138 | - Identifier::fromDER($data, $offset); |
|
139 | - // decode element length |
|
140 | - $length = Length::expectFromDER($data, $offset)->intLength(); |
|
141 | - // extract der encoding of the element |
|
142 | - $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
143 | - // update offset over content |
|
144 | - $offset += $length; |
|
145 | - } |
|
146 | - return $parts; |
|
147 | - } |
|
117 | + /** |
|
118 | + * Explode DER structure to DER encoded components that it contains. |
|
119 | + * |
|
120 | + * @param string $data |
|
121 | + * @throws DecodeException |
|
122 | + * @return string[] |
|
123 | + */ |
|
124 | + public static function explodeDER(string $data): array |
|
125 | + { |
|
126 | + $offset = 0; |
|
127 | + $identifier = Identifier::fromDER($data, $offset); |
|
128 | + if (!$identifier->isConstructed()) { |
|
129 | + throw new DecodeException("Element is not constructed."); |
|
130 | + } |
|
131 | + $length = Length::expectFromDER($data, $offset)->intLength(); |
|
132 | + $end = $offset + $length; |
|
133 | + $parts = []; |
|
134 | + while ($offset < $end) { |
|
135 | + // start of the element |
|
136 | + $idx = $offset; |
|
137 | + // skip identifier |
|
138 | + Identifier::fromDER($data, $offset); |
|
139 | + // decode element length |
|
140 | + $length = Length::expectFromDER($data, $offset)->intLength(); |
|
141 | + // extract der encoding of the element |
|
142 | + $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
143 | + // update offset over content |
|
144 | + $offset += $length; |
|
145 | + } |
|
146 | + return $parts; |
|
147 | + } |
|
148 | 148 | |
149 | - /** |
|
150 | - * Get self with an element at the given index replaced by another. |
|
151 | - * |
|
152 | - * @param int $idx Element index |
|
153 | - * @param Element $el New element to insert into the structure |
|
154 | - * @throws \OutOfBoundsException |
|
155 | - * @return self |
|
156 | - */ |
|
157 | - public function withReplaced(int $idx, Element $el): self |
|
158 | - { |
|
159 | - if (!isset($this->_elements[$idx])) { |
|
160 | - throw new \OutOfBoundsException( |
|
161 | - "Structure doesn't have element at index $idx."); |
|
162 | - } |
|
163 | - $obj = clone $this; |
|
164 | - $obj->_elements[$idx] = $el; |
|
165 | - return $obj; |
|
166 | - } |
|
149 | + /** |
|
150 | + * Get self with an element at the given index replaced by another. |
|
151 | + * |
|
152 | + * @param int $idx Element index |
|
153 | + * @param Element $el New element to insert into the structure |
|
154 | + * @throws \OutOfBoundsException |
|
155 | + * @return self |
|
156 | + */ |
|
157 | + public function withReplaced(int $idx, Element $el): self |
|
158 | + { |
|
159 | + if (!isset($this->_elements[$idx])) { |
|
160 | + throw new \OutOfBoundsException( |
|
161 | + "Structure doesn't have element at index $idx."); |
|
162 | + } |
|
163 | + $obj = clone $this; |
|
164 | + $obj->_elements[$idx] = $el; |
|
165 | + return $obj; |
|
166 | + } |
|
167 | 167 | |
168 | - /** |
|
169 | - * Get self with an element inserted before the given index. |
|
170 | - * |
|
171 | - * @param int $idx Element index |
|
172 | - * @param Element $el New element to insert into the structure |
|
173 | - * @throws \OutOfBoundsException |
|
174 | - * @return self |
|
175 | - */ |
|
176 | - public function withInserted(int $idx, Element $el): self |
|
177 | - { |
|
178 | - if (count($this->_elements) < $idx || $idx < 0) { |
|
179 | - throw new \OutOfBoundsException("Index $idx is out of bounds."); |
|
180 | - } |
|
181 | - $obj = clone $this; |
|
182 | - array_splice($obj->_elements, $idx, 0, [$el]); |
|
183 | - return $obj; |
|
184 | - } |
|
168 | + /** |
|
169 | + * Get self with an element inserted before the given index. |
|
170 | + * |
|
171 | + * @param int $idx Element index |
|
172 | + * @param Element $el New element to insert into the structure |
|
173 | + * @throws \OutOfBoundsException |
|
174 | + * @return self |
|
175 | + */ |
|
176 | + public function withInserted(int $idx, Element $el): self |
|
177 | + { |
|
178 | + if (count($this->_elements) < $idx || $idx < 0) { |
|
179 | + throw new \OutOfBoundsException("Index $idx is out of bounds."); |
|
180 | + } |
|
181 | + $obj = clone $this; |
|
182 | + array_splice($obj->_elements, $idx, 0, [$el]); |
|
183 | + return $obj; |
|
184 | + } |
|
185 | 185 | |
186 | - /** |
|
187 | - * Get self with an element appended to the end. |
|
188 | - * |
|
189 | - * @param Element $el Element to insert into the structure |
|
190 | - * @return self |
|
191 | - */ |
|
192 | - public function withAppended(Element $el): self |
|
193 | - { |
|
194 | - $obj = clone $this; |
|
195 | - array_push($obj->_elements, $el); |
|
196 | - return $obj; |
|
197 | - } |
|
186 | + /** |
|
187 | + * Get self with an element appended to the end. |
|
188 | + * |
|
189 | + * @param Element $el Element to insert into the structure |
|
190 | + * @return self |
|
191 | + */ |
|
192 | + public function withAppended(Element $el): self |
|
193 | + { |
|
194 | + $obj = clone $this; |
|
195 | + array_push($obj->_elements, $el); |
|
196 | + return $obj; |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * Get self with an element prepended in the beginning. |
|
201 | - * |
|
202 | - * @param Element $el Element to insert into the structure |
|
203 | - * @return self |
|
204 | - */ |
|
205 | - public function withPrepended(Element $el): self |
|
206 | - { |
|
207 | - $obj = clone $this; |
|
208 | - array_unshift($obj->_elements, $el); |
|
209 | - return $obj; |
|
210 | - } |
|
199 | + /** |
|
200 | + * Get self with an element prepended in the beginning. |
|
201 | + * |
|
202 | + * @param Element $el Element to insert into the structure |
|
203 | + * @return self |
|
204 | + */ |
|
205 | + public function withPrepended(Element $el): self |
|
206 | + { |
|
207 | + $obj = clone $this; |
|
208 | + array_unshift($obj->_elements, $el); |
|
209 | + return $obj; |
|
210 | + } |
|
211 | 211 | |
212 | - /** |
|
213 | - * Get self with an element at the given index removed. |
|
214 | - * |
|
215 | - * @param int $idx Element index |
|
216 | - * @throws \OutOfBoundsException |
|
217 | - * @return self |
|
218 | - */ |
|
219 | - public function withoutElement($idx): self |
|
220 | - { |
|
221 | - if (!isset($this->_elements[$idx])) { |
|
222 | - throw new \OutOfBoundsException( |
|
223 | - "Structure doesn't have element at index $idx."); |
|
224 | - } |
|
225 | - $obj = clone $this; |
|
226 | - array_splice($obj->_elements, $idx, 1); |
|
227 | - return $obj; |
|
228 | - } |
|
212 | + /** |
|
213 | + * Get self with an element at the given index removed. |
|
214 | + * |
|
215 | + * @param int $idx Element index |
|
216 | + * @throws \OutOfBoundsException |
|
217 | + * @return self |
|
218 | + */ |
|
219 | + public function withoutElement($idx): self |
|
220 | + { |
|
221 | + if (!isset($this->_elements[$idx])) { |
|
222 | + throw new \OutOfBoundsException( |
|
223 | + "Structure doesn't have element at index $idx."); |
|
224 | + } |
|
225 | + $obj = clone $this; |
|
226 | + array_splice($obj->_elements, $idx, 1); |
|
227 | + return $obj; |
|
228 | + } |
|
229 | 229 | |
230 | - /** |
|
231 | - * Get elements in the structure. |
|
232 | - * |
|
233 | - * @return UnspecifiedType[] |
|
234 | - */ |
|
235 | - public function elements(): array |
|
236 | - { |
|
237 | - if (!isset($this->_unspecifiedTypes)) { |
|
238 | - $this->_unspecifiedTypes = array_map( |
|
239 | - function (Element $el) { |
|
240 | - return new UnspecifiedType($el); |
|
241 | - }, $this->_elements); |
|
242 | - } |
|
243 | - return $this->_unspecifiedTypes; |
|
244 | - } |
|
230 | + /** |
|
231 | + * Get elements in the structure. |
|
232 | + * |
|
233 | + * @return UnspecifiedType[] |
|
234 | + */ |
|
235 | + public function elements(): array |
|
236 | + { |
|
237 | + if (!isset($this->_unspecifiedTypes)) { |
|
238 | + $this->_unspecifiedTypes = array_map( |
|
239 | + function (Element $el) { |
|
240 | + return new UnspecifiedType($el); |
|
241 | + }, $this->_elements); |
|
242 | + } |
|
243 | + return $this->_unspecifiedTypes; |
|
244 | + } |
|
245 | 245 | |
246 | - /** |
|
247 | - * Check whether the structure has an element at the given index, optionally |
|
248 | - * satisfying given tag expectation. |
|
249 | - * |
|
250 | - * @param int $idx Index 0..n |
|
251 | - * @param int|null $expectedTag Optional type tag expectation |
|
252 | - * @return bool |
|
253 | - */ |
|
254 | - public function has(int $idx, $expectedTag = null): bool |
|
255 | - { |
|
256 | - if (!isset($this->_elements[$idx])) { |
|
257 | - return false; |
|
258 | - } |
|
259 | - if (isset($expectedTag)) { |
|
260 | - if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
261 | - return false; |
|
262 | - } |
|
263 | - } |
|
264 | - return true; |
|
265 | - } |
|
246 | + /** |
|
247 | + * Check whether the structure has an element at the given index, optionally |
|
248 | + * satisfying given tag expectation. |
|
249 | + * |
|
250 | + * @param int $idx Index 0..n |
|
251 | + * @param int|null $expectedTag Optional type tag expectation |
|
252 | + * @return bool |
|
253 | + */ |
|
254 | + public function has(int $idx, $expectedTag = null): bool |
|
255 | + { |
|
256 | + if (!isset($this->_elements[$idx])) { |
|
257 | + return false; |
|
258 | + } |
|
259 | + if (isset($expectedTag)) { |
|
260 | + if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
261 | + return false; |
|
262 | + } |
|
263 | + } |
|
264 | + return true; |
|
265 | + } |
|
266 | 266 | |
267 | - /** |
|
268 | - * Get the element at the given index, optionally checking that the element |
|
269 | - * has a given tag. |
|
270 | - * |
|
271 | - * NOTE! Expectation checking is deprecated and should be done |
|
272 | - * with UnspecifiedType. |
|
273 | - * |
|
274 | - * @param int $idx Index 0..n |
|
275 | - * @param int|null $expectedTag Optional type tag expectation |
|
276 | - * @throws \OutOfBoundsException If element doesn't exists |
|
277 | - * @throws \UnexpectedValueException If expectation fails |
|
278 | - * @return UnspecifiedType |
|
279 | - */ |
|
280 | - public function at(int $idx, $expectedTag = null): UnspecifiedType |
|
281 | - { |
|
282 | - if (!isset($this->_elements[$idx])) { |
|
283 | - throw new \OutOfBoundsException( |
|
284 | - "Structure doesn't have an element at index $idx."); |
|
285 | - } |
|
286 | - $element = $this->_elements[$idx]; |
|
287 | - if (isset($expectedTag)) { |
|
288 | - $element->expectType($expectedTag); |
|
289 | - } |
|
290 | - return new UnspecifiedType($element); |
|
291 | - } |
|
267 | + /** |
|
268 | + * Get the element at the given index, optionally checking that the element |
|
269 | + * has a given tag. |
|
270 | + * |
|
271 | + * NOTE! Expectation checking is deprecated and should be done |
|
272 | + * with UnspecifiedType. |
|
273 | + * |
|
274 | + * @param int $idx Index 0..n |
|
275 | + * @param int|null $expectedTag Optional type tag expectation |
|
276 | + * @throws \OutOfBoundsException If element doesn't exists |
|
277 | + * @throws \UnexpectedValueException If expectation fails |
|
278 | + * @return UnspecifiedType |
|
279 | + */ |
|
280 | + public function at(int $idx, $expectedTag = null): UnspecifiedType |
|
281 | + { |
|
282 | + if (!isset($this->_elements[$idx])) { |
|
283 | + throw new \OutOfBoundsException( |
|
284 | + "Structure doesn't have an element at index $idx."); |
|
285 | + } |
|
286 | + $element = $this->_elements[$idx]; |
|
287 | + if (isset($expectedTag)) { |
|
288 | + $element->expectType($expectedTag); |
|
289 | + } |
|
290 | + return new UnspecifiedType($element); |
|
291 | + } |
|
292 | 292 | |
293 | - /** |
|
294 | - * Check whether the structure contains a context specific element with a |
|
295 | - * given tag. |
|
296 | - * |
|
297 | - * @param int $tag Tag number |
|
298 | - * @return boolean |
|
299 | - */ |
|
300 | - public function hasTagged($tag): bool |
|
301 | - { |
|
302 | - // lazily build lookup map |
|
303 | - if (!isset($this->_taggedMap)) { |
|
304 | - $this->_taggedMap = []; |
|
305 | - foreach ($this->_elements as $element) { |
|
306 | - if ($element->isTagged()) { |
|
307 | - $this->_taggedMap[$element->tag()] = $element; |
|
308 | - } |
|
309 | - } |
|
310 | - } |
|
311 | - return isset($this->_taggedMap[$tag]); |
|
312 | - } |
|
293 | + /** |
|
294 | + * Check whether the structure contains a context specific element with a |
|
295 | + * given tag. |
|
296 | + * |
|
297 | + * @param int $tag Tag number |
|
298 | + * @return boolean |
|
299 | + */ |
|
300 | + public function hasTagged($tag): bool |
|
301 | + { |
|
302 | + // lazily build lookup map |
|
303 | + if (!isset($this->_taggedMap)) { |
|
304 | + $this->_taggedMap = []; |
|
305 | + foreach ($this->_elements as $element) { |
|
306 | + if ($element->isTagged()) { |
|
307 | + $this->_taggedMap[$element->tag()] = $element; |
|
308 | + } |
|
309 | + } |
|
310 | + } |
|
311 | + return isset($this->_taggedMap[$tag]); |
|
312 | + } |
|
313 | 313 | |
314 | - /** |
|
315 | - * Get a context specific element tagged with a given tag. |
|
316 | - * |
|
317 | - * @param int $tag |
|
318 | - * @throws \LogicException If tag doesn't exists |
|
319 | - * @return TaggedType |
|
320 | - */ |
|
321 | - public function getTagged($tag): TaggedType |
|
322 | - { |
|
323 | - if (!$this->hasTagged($tag)) { |
|
324 | - throw new \LogicException("No tagged element for tag $tag."); |
|
325 | - } |
|
326 | - return $this->_taggedMap[$tag]; |
|
327 | - } |
|
314 | + /** |
|
315 | + * Get a context specific element tagged with a given tag. |
|
316 | + * |
|
317 | + * @param int $tag |
|
318 | + * @throws \LogicException If tag doesn't exists |
|
319 | + * @return TaggedType |
|
320 | + */ |
|
321 | + public function getTagged($tag): TaggedType |
|
322 | + { |
|
323 | + if (!$this->hasTagged($tag)) { |
|
324 | + throw new \LogicException("No tagged element for tag $tag."); |
|
325 | + } |
|
326 | + return $this->_taggedMap[$tag]; |
|
327 | + } |
|
328 | 328 | |
329 | - /** |
|
330 | - * |
|
331 | - * @see \Countable::count() |
|
332 | - * @return int |
|
333 | - */ |
|
334 | - public function count(): int |
|
335 | - { |
|
336 | - return count($this->_elements); |
|
337 | - } |
|
329 | + /** |
|
330 | + * |
|
331 | + * @see \Countable::count() |
|
332 | + * @return int |
|
333 | + */ |
|
334 | + public function count(): int |
|
335 | + { |
|
336 | + return count($this->_elements); |
|
337 | + } |
|
338 | 338 | |
339 | - /** |
|
340 | - * Get an iterator for the UnspecifiedElement objects. |
|
341 | - * |
|
342 | - * @see \IteratorAggregate::getIterator() |
|
343 | - * @return \ArrayIterator |
|
344 | - */ |
|
345 | - public function getIterator(): \ArrayIterator |
|
346 | - { |
|
347 | - return new \ArrayIterator($this->elements()); |
|
348 | - } |
|
339 | + /** |
|
340 | + * Get an iterator for the UnspecifiedElement objects. |
|
341 | + * |
|
342 | + * @see \IteratorAggregate::getIterator() |
|
343 | + * @return \ArrayIterator |
|
344 | + */ |
|
345 | + public function getIterator(): \ArrayIterator |
|
346 | + { |
|
347 | + return new \ArrayIterator($this->elements()); |
|
348 | + } |
|
349 | 349 | } |
@@ -14,41 +14,41 @@ |
||
14 | 14 | */ |
15 | 15 | abstract class PrimitiveString extends StringType |
16 | 16 | { |
17 | - use PrimitiveType; |
|
17 | + use PrimitiveType; |
|
18 | 18 | |
19 | - /** |
|
20 | - * |
|
21 | - * @see \ASN1\Element::_encodedContentDER() |
|
22 | - * @return string |
|
23 | - */ |
|
24 | - protected function _encodedContentDER(): string |
|
25 | - { |
|
26 | - return $this->_string; |
|
27 | - } |
|
19 | + /** |
|
20 | + * |
|
21 | + * @see \ASN1\Element::_encodedContentDER() |
|
22 | + * @return string |
|
23 | + */ |
|
24 | + protected function _encodedContentDER(): string |
|
25 | + { |
|
26 | + return $this->_string; |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * |
|
31 | - * {@inheritdoc} |
|
32 | - * @see \ASN1\Element::_decodeFromDER() |
|
33 | - * @return self |
|
34 | - */ |
|
35 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
36 | - int &$offset): ElementBase |
|
37 | - { |
|
38 | - $idx = $offset; |
|
39 | - if (!$identifier->isPrimitive()) { |
|
40 | - throw new DecodeException("DER encoded string must be primitive."); |
|
41 | - } |
|
42 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
43 | - $str = $length ? substr($data, $idx, $length) : ""; |
|
44 | - // substr should never return false, since length is |
|
45 | - // checked by Length::expectFromDER. |
|
46 | - assert(is_string($str), "substr"); |
|
47 | - $offset = $idx + $length; |
|
48 | - try { |
|
49 | - return new static($str); |
|
50 | - } catch (\InvalidArgumentException $e) { |
|
51 | - throw new DecodeException($e->getMessage(), 0, $e); |
|
52 | - } |
|
53 | - } |
|
29 | + /** |
|
30 | + * |
|
31 | + * {@inheritdoc} |
|
32 | + * @see \ASN1\Element::_decodeFromDER() |
|
33 | + * @return self |
|
34 | + */ |
|
35 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
36 | + int &$offset): ElementBase |
|
37 | + { |
|
38 | + $idx = $offset; |
|
39 | + if (!$identifier->isPrimitive()) { |
|
40 | + throw new DecodeException("DER encoded string must be primitive."); |
|
41 | + } |
|
42 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
43 | + $str = $length ? substr($data, $idx, $length) : ""; |
|
44 | + // substr should never return false, since length is |
|
45 | + // checked by Length::expectFromDER. |
|
46 | + assert(is_string($str), "substr"); |
|
47 | + $offset = $idx + $length; |
|
48 | + try { |
|
49 | + return new static($str); |
|
50 | + } catch (\InvalidArgumentException $e) { |
|
51 | + throw new DecodeException($e->getMessage(), 0, $e); |
|
52 | + } |
|
53 | + } |
|
54 | 54 | } |
@@ -17,194 +17,194 @@ |
||
17 | 17 | */ |
18 | 18 | class BitString extends StringType |
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 $_unusedBits |
|
27 | - */ |
|
28 | - protected $_unusedBits; |
|
23 | + /** |
|
24 | + * Number of unused bits in the last octet. |
|
25 | + * |
|
26 | + * @var int $_unusedBits |
|
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. |
|
67 | - * Most significant bit of the first octet is index 0. |
|
68 | - * @return boolean |
|
69 | - */ |
|
70 | - public function testBit($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("Index refers to an unused bit."); |
|
84 | - } |
|
85 | - } |
|
86 | - $byte = $this->_string[$oi]; |
|
87 | - // index 0 is the most significant bit in byte |
|
88 | - $mask = 0x01 << (7 - $bi); |
|
89 | - return (ord($byte) & $mask) > 0; |
|
90 | - } |
|
63 | + /** |
|
64 | + * Test whether bit is set. |
|
65 | + * |
|
66 | + * @param int $idx Bit index. |
|
67 | + * Most significant bit of the first octet is index 0. |
|
68 | + * @return boolean |
|
69 | + */ |
|
70 | + public function testBit($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("Index refers to an unused bit."); |
|
84 | + } |
|
85 | + } |
|
86 | + $byte = $this->_string[$oi]; |
|
87 | + // index 0 is the most significant bit in byte |
|
88 | + $mask = 0x01 << (7 - $bi); |
|
89 | + return (ord($byte) & $mask) > 0; |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * Get range of bits. |
|
94 | - * |
|
95 | - * @param int $start Index of first bit |
|
96 | - * @param int $length Number of bits in range |
|
97 | - * @throws \OutOfBoundsException |
|
98 | - * @return string Integer of $length bits |
|
99 | - */ |
|
100 | - public function range(int $start, int $length): string |
|
101 | - { |
|
102 | - if (!$length) { |
|
103 | - return "0"; |
|
104 | - } |
|
105 | - if ($start + $length > $this->numBits()) { |
|
106 | - throw new \OutOfBoundsException("Not enough bits."); |
|
107 | - } |
|
108 | - $bits = gmp_init(0); |
|
109 | - $idx = $start; |
|
110 | - $end = $start + $length; |
|
111 | - while (true) { |
|
112 | - $bit = $this->testBit($idx) ? 1 : 0; |
|
113 | - $bits |= $bit; |
|
114 | - if (++$idx >= $end) { |
|
115 | - break; |
|
116 | - } |
|
117 | - $bits <<= 1; |
|
118 | - } |
|
119 | - return gmp_strval($bits, 10); |
|
120 | - } |
|
92 | + /** |
|
93 | + * Get range of bits. |
|
94 | + * |
|
95 | + * @param int $start Index of first bit |
|
96 | + * @param int $length Number of bits in range |
|
97 | + * @throws \OutOfBoundsException |
|
98 | + * @return string Integer of $length bits |
|
99 | + */ |
|
100 | + public function range(int $start, int $length): string |
|
101 | + { |
|
102 | + if (!$length) { |
|
103 | + return "0"; |
|
104 | + } |
|
105 | + if ($start + $length > $this->numBits()) { |
|
106 | + throw new \OutOfBoundsException("Not enough bits."); |
|
107 | + } |
|
108 | + $bits = gmp_init(0); |
|
109 | + $idx = $start; |
|
110 | + $end = $start + $length; |
|
111 | + while (true) { |
|
112 | + $bit = $this->testBit($idx) ? 1 : 0; |
|
113 | + $bits |= $bit; |
|
114 | + if (++$idx >= $end) { |
|
115 | + break; |
|
116 | + } |
|
117 | + $bits <<= 1; |
|
118 | + } |
|
119 | + return gmp_strval($bits, 10); |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * Get a copy of the bit string with trailing zeroes removed. |
|
124 | - * |
|
125 | - * @return self |
|
126 | - */ |
|
127 | - public function withoutTrailingZeroes(): self |
|
128 | - { |
|
129 | - // if bit string was empty |
|
130 | - if (!strlen($this->_string)) { |
|
131 | - return new self(""); |
|
132 | - } |
|
133 | - $bits = $this->_string; |
|
134 | - // count number of empty trailing octets |
|
135 | - $unused_octets = 0; |
|
136 | - for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
137 | - if ($bits[$idx] != "\x0") { |
|
138 | - break; |
|
139 | - } |
|
140 | - } |
|
141 | - // strip trailing octets |
|
142 | - if ($unused_octets) { |
|
143 | - $bits = substr($bits, 0, -$unused_octets); |
|
144 | - } |
|
145 | - // if bit string was full of zeroes |
|
146 | - if (!strlen($bits)) { |
|
147 | - return new self(""); |
|
148 | - } |
|
149 | - // count number of trailing zeroes in the last octet |
|
150 | - $unused_bits = 0; |
|
151 | - $byte = ord($bits[strlen($bits) - 1]); |
|
152 | - while (!($byte & 0x01)) { |
|
153 | - $unused_bits++; |
|
154 | - $byte >>= 1; |
|
155 | - } |
|
156 | - return new self($bits, $unused_bits); |
|
157 | - } |
|
122 | + /** |
|
123 | + * Get a copy of the bit string with trailing zeroes removed. |
|
124 | + * |
|
125 | + * @return self |
|
126 | + */ |
|
127 | + public function withoutTrailingZeroes(): self |
|
128 | + { |
|
129 | + // if bit string was empty |
|
130 | + if (!strlen($this->_string)) { |
|
131 | + return new self(""); |
|
132 | + } |
|
133 | + $bits = $this->_string; |
|
134 | + // count number of empty trailing octets |
|
135 | + $unused_octets = 0; |
|
136 | + for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
137 | + if ($bits[$idx] != "\x0") { |
|
138 | + break; |
|
139 | + } |
|
140 | + } |
|
141 | + // strip trailing octets |
|
142 | + if ($unused_octets) { |
|
143 | + $bits = substr($bits, 0, -$unused_octets); |
|
144 | + } |
|
145 | + // if bit string was full of zeroes |
|
146 | + if (!strlen($bits)) { |
|
147 | + return new self(""); |
|
148 | + } |
|
149 | + // count number of trailing zeroes in the last octet |
|
150 | + $unused_bits = 0; |
|
151 | + $byte = ord($bits[strlen($bits) - 1]); |
|
152 | + while (!($byte & 0x01)) { |
|
153 | + $unused_bits++; |
|
154 | + $byte >>= 1; |
|
155 | + } |
|
156 | + return new self($bits, $unused_bits); |
|
157 | + } |
|
158 | 158 | |
159 | - /** |
|
160 | - * |
|
161 | - * {@inheritdoc} |
|
162 | - */ |
|
163 | - protected function _encodedContentDER(): string |
|
164 | - { |
|
165 | - $der = chr($this->_unusedBits); |
|
166 | - $der .= $this->_string; |
|
167 | - if ($this->_unusedBits) { |
|
168 | - $octet = $der[strlen($der) - 1]; |
|
169 | - // set unused bits to zero |
|
170 | - $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
171 | - $der[strlen($der) - 1] = $octet; |
|
172 | - } |
|
173 | - return $der; |
|
174 | - } |
|
159 | + /** |
|
160 | + * |
|
161 | + * {@inheritdoc} |
|
162 | + */ |
|
163 | + protected function _encodedContentDER(): string |
|
164 | + { |
|
165 | + $der = chr($this->_unusedBits); |
|
166 | + $der .= $this->_string; |
|
167 | + if ($this->_unusedBits) { |
|
168 | + $octet = $der[strlen($der) - 1]; |
|
169 | + // set unused bits to zero |
|
170 | + $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
171 | + $der[strlen($der) - 1] = $octet; |
|
172 | + } |
|
173 | + return $der; |
|
174 | + } |
|
175 | 175 | |
176 | - /** |
|
177 | - * |
|
178 | - * {@inheritdoc} |
|
179 | - * @return self |
|
180 | - */ |
|
181 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
182 | - 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 | - } |
|
176 | + /** |
|
177 | + * |
|
178 | + * {@inheritdoc} |
|
179 | + * @return self |
|
180 | + */ |
|
181 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
182 | + 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,271 +17,271 @@ |
||
17 | 17 | */ |
18 | 18 | class Real extends Element |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Regex pattern to parse NR3 form number conforming to DER. |
|
25 | - * |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
23 | + /** |
|
24 | + * Regex pattern to parse NR3 form number conforming to DER. |
|
25 | + * |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Regex pattern to parse PHP exponent number format. |
|
32 | - * |
|
33 | - * @link http://php.net/manual/en/language.types.float.php |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */ |
|
37 | - '([+\-]?'. // sign |
|
38 | - '(?:'. |
|
39 | - '\d+'. // LNUM |
|
40 | - '|'. |
|
41 | - '(?:\d*\.\d+|\d+\.\d*)'. // DNUM |
|
42 | - '))[eE]'. |
|
43 | - '([+\-]?\d+)'. // exponent |
|
44 | - '$/'; /* @formatter:on */ |
|
30 | + /** |
|
31 | + * Regex pattern to parse PHP exponent number format. |
|
32 | + * |
|
33 | + * @link http://php.net/manual/en/language.types.float.php |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */ |
|
37 | + '([+\-]?'. // sign |
|
38 | + '(?:'. |
|
39 | + '\d+'. // LNUM |
|
40 | + '|'. |
|
41 | + '(?:\d*\.\d+|\d+\.\d*)'. // DNUM |
|
42 | + '))[eE]'. |
|
43 | + '([+\-]?\d+)'. // exponent |
|
44 | + '$/'; /* @formatter:on */ |
|
45 | 45 | |
46 | - /** |
|
47 | - * Number zero represented in NR3 form. |
|
48 | - * |
|
49 | - * @var string |
|
50 | - */ |
|
51 | - const NR3_ZERO = ".E+0"; |
|
46 | + /** |
|
47 | + * Number zero represented in NR3 form. |
|
48 | + * |
|
49 | + * @var string |
|
50 | + */ |
|
51 | + const NR3_ZERO = ".E+0"; |
|
52 | 52 | |
53 | - /** |
|
54 | - * Number in NR3 form. |
|
55 | - * |
|
56 | - * @var string |
|
57 | - */ |
|
58 | - private $_number; |
|
53 | + /** |
|
54 | + * Number in NR3 form. |
|
55 | + * |
|
56 | + * @var string |
|
57 | + */ |
|
58 | + private $_number; |
|
59 | 59 | |
60 | - /** |
|
61 | - * Constructor. |
|
62 | - * |
|
63 | - * @param string $number Number in NR3 form. |
|
64 | - */ |
|
65 | - public function __construct(string $number) |
|
66 | - { |
|
67 | - $this->_typeTag = self::TYPE_REAL; |
|
68 | - if (!self::_validateNumber($number)) { |
|
69 | - throw new \InvalidArgumentException( |
|
70 | - "'$number' is not a valid NR3 form real."); |
|
71 | - } |
|
72 | - $this->_number = $number; |
|
73 | - } |
|
60 | + /** |
|
61 | + * Constructor. |
|
62 | + * |
|
63 | + * @param string $number Number in NR3 form. |
|
64 | + */ |
|
65 | + public function __construct(string $number) |
|
66 | + { |
|
67 | + $this->_typeTag = self::TYPE_REAL; |
|
68 | + if (!self::_validateNumber($number)) { |
|
69 | + throw new \InvalidArgumentException( |
|
70 | + "'$number' is not a valid NR3 form real."); |
|
71 | + } |
|
72 | + $this->_number = $number; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Initialize from float. |
|
77 | - * |
|
78 | - * @param float $number |
|
79 | - * @return self |
|
80 | - */ |
|
81 | - public static function fromFloat(float $number): self |
|
82 | - { |
|
83 | - return new self(self::_decimalToNR3(strval($number))); |
|
84 | - } |
|
75 | + /** |
|
76 | + * Initialize from float. |
|
77 | + * |
|
78 | + * @param float $number |
|
79 | + * @return self |
|
80 | + */ |
|
81 | + public static function fromFloat(float $number): self |
|
82 | + { |
|
83 | + return new self(self::_decimalToNR3(strval($number))); |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Get number as a float. |
|
88 | - * |
|
89 | - * @return float |
|
90 | - */ |
|
91 | - public function float(): float |
|
92 | - { |
|
93 | - return self::_nr3ToDecimal($this->_number); |
|
94 | - } |
|
86 | + /** |
|
87 | + * Get number as a float. |
|
88 | + * |
|
89 | + * @return float |
|
90 | + */ |
|
91 | + public function float(): float |
|
92 | + { |
|
93 | + return self::_nr3ToDecimal($this->_number); |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * |
|
98 | - * {@inheritdoc} |
|
99 | - */ |
|
100 | - protected function _encodedContentDER(): string |
|
101 | - { |
|
102 | - /* if the real value is the value zero, there shall be no contents |
|
96 | + /** |
|
97 | + * |
|
98 | + * {@inheritdoc} |
|
99 | + */ |
|
100 | + protected function _encodedContentDER(): string |
|
101 | + { |
|
102 | + /* if the real value is the value zero, there shall be no contents |
|
103 | 103 | octets in the encoding. (X.690 07-2002, section 8.5.2) */ |
104 | - if (self::NR3_ZERO == $this->_number) { |
|
105 | - return ""; |
|
106 | - } |
|
107 | - // encode in NR3 decimal encoding |
|
108 | - $data = chr(0x03) . $this->_number; |
|
109 | - return $data; |
|
110 | - } |
|
104 | + if (self::NR3_ZERO == $this->_number) { |
|
105 | + return ""; |
|
106 | + } |
|
107 | + // encode in NR3 decimal encoding |
|
108 | + $data = chr(0x03) . $this->_number; |
|
109 | + return $data; |
|
110 | + } |
|
111 | 111 | |
112 | - /** |
|
113 | - * |
|
114 | - * {@inheritdoc} |
|
115 | - * @return self |
|
116 | - */ |
|
117 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
118 | - int &$offset): ElementBase |
|
119 | - { |
|
120 | - $idx = $offset; |
|
121 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
122 | - // if length is zero, value is zero (spec 8.5.2) |
|
123 | - if (!$length) { |
|
124 | - $obj = new self(self::NR3_ZERO); |
|
125 | - } else { |
|
126 | - $bytes = substr($data, $idx, $length); |
|
127 | - $byte = ord($bytes[0]); |
|
128 | - if (0x80 & $byte) { // bit 8 = 1 |
|
129 | - $obj = self::_decodeBinaryEncoding($bytes); |
|
130 | - } else if ($byte >> 6 == 0x00) { // bit 8 = 0, bit 7 = 0 |
|
131 | - $obj = self::_decodeDecimalEncoding($bytes); |
|
132 | - } else { // bit 8 = 0, bit 7 = 1 |
|
133 | - $obj = self::_decodeSpecialRealValue($bytes); |
|
134 | - } |
|
135 | - } |
|
136 | - $offset = $idx + $length; |
|
137 | - return $obj; |
|
138 | - } |
|
112 | + /** |
|
113 | + * |
|
114 | + * {@inheritdoc} |
|
115 | + * @return self |
|
116 | + */ |
|
117 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
118 | + int &$offset): ElementBase |
|
119 | + { |
|
120 | + $idx = $offset; |
|
121 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
122 | + // if length is zero, value is zero (spec 8.5.2) |
|
123 | + if (!$length) { |
|
124 | + $obj = new self(self::NR3_ZERO); |
|
125 | + } else { |
|
126 | + $bytes = substr($data, $idx, $length); |
|
127 | + $byte = ord($bytes[0]); |
|
128 | + if (0x80 & $byte) { // bit 8 = 1 |
|
129 | + $obj = self::_decodeBinaryEncoding($bytes); |
|
130 | + } else if ($byte >> 6 == 0x00) { // bit 8 = 0, bit 7 = 0 |
|
131 | + $obj = self::_decodeDecimalEncoding($bytes); |
|
132 | + } else { // bit 8 = 0, bit 7 = 1 |
|
133 | + $obj = self::_decodeSpecialRealValue($bytes); |
|
134 | + } |
|
135 | + } |
|
136 | + $offset = $idx + $length; |
|
137 | + return $obj; |
|
138 | + } |
|
139 | 139 | |
140 | - /** |
|
141 | - * |
|
142 | - * @todo Implement |
|
143 | - * @param string $data |
|
144 | - */ |
|
145 | - protected static function _decodeBinaryEncoding(string $data) |
|
146 | - { |
|
147 | - throw new \RuntimeException( |
|
148 | - "Binary encoding of REAL is not implemented."); |
|
149 | - } |
|
140 | + /** |
|
141 | + * |
|
142 | + * @todo Implement |
|
143 | + * @param string $data |
|
144 | + */ |
|
145 | + protected static function _decodeBinaryEncoding(string $data) |
|
146 | + { |
|
147 | + throw new \RuntimeException( |
|
148 | + "Binary encoding of REAL is not implemented."); |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * |
|
153 | - * @param string $data |
|
154 | - * @throws \RuntimeException |
|
155 | - * @return \ASN1\Type\Primitive\Real |
|
156 | - */ |
|
157 | - protected static function _decodeDecimalEncoding(string $data): Real |
|
158 | - { |
|
159 | - $nr = ord($data[0]) & 0x03; |
|
160 | - if ($nr != 0x03) { |
|
161 | - throw new \RuntimeException("Only NR3 form supported."); |
|
162 | - } |
|
163 | - $str = substr($data, 1); |
|
164 | - return new self($str); |
|
165 | - } |
|
151 | + /** |
|
152 | + * |
|
153 | + * @param string $data |
|
154 | + * @throws \RuntimeException |
|
155 | + * @return \ASN1\Type\Primitive\Real |
|
156 | + */ |
|
157 | + protected static function _decodeDecimalEncoding(string $data): Real |
|
158 | + { |
|
159 | + $nr = ord($data[0]) & 0x03; |
|
160 | + if ($nr != 0x03) { |
|
161 | + throw new \RuntimeException("Only NR3 form supported."); |
|
162 | + } |
|
163 | + $str = substr($data, 1); |
|
164 | + return new self($str); |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * |
|
169 | - * @todo Implement |
|
170 | - * @param string $data |
|
171 | - */ |
|
172 | - protected static function _decodeSpecialRealValue(string $data) |
|
173 | - { |
|
174 | - if (strlen($data) != 1) { |
|
175 | - throw new DecodeException( |
|
176 | - "SpecialRealValue must have one content octet."); |
|
177 | - } |
|
178 | - $byte = ord($data[0]); |
|
179 | - if ($byte == 0x40) { // positive infinity |
|
180 | - throw new \RuntimeException("PLUS-INFINITY not supported."); |
|
181 | - } else if ($byte == 0x41) { // negative infinity |
|
182 | - throw new \RuntimeException("MINUS-INFINITY not supported."); |
|
183 | - } else { |
|
184 | - throw new DecodeException("Invalid SpecialRealValue encoding."); |
|
185 | - } |
|
186 | - } |
|
167 | + /** |
|
168 | + * |
|
169 | + * @todo Implement |
|
170 | + * @param string $data |
|
171 | + */ |
|
172 | + protected static function _decodeSpecialRealValue(string $data) |
|
173 | + { |
|
174 | + if (strlen($data) != 1) { |
|
175 | + throw new DecodeException( |
|
176 | + "SpecialRealValue must have one content octet."); |
|
177 | + } |
|
178 | + $byte = ord($data[0]); |
|
179 | + if ($byte == 0x40) { // positive infinity |
|
180 | + throw new \RuntimeException("PLUS-INFINITY not supported."); |
|
181 | + } else if ($byte == 0x41) { // negative infinity |
|
182 | + throw new \RuntimeException("MINUS-INFINITY not supported."); |
|
183 | + } else { |
|
184 | + throw new DecodeException("Invalid SpecialRealValue encoding."); |
|
185 | + } |
|
186 | + } |
|
187 | 187 | |
188 | - /** |
|
189 | - * Convert decimal number string to NR3 form. |
|
190 | - * |
|
191 | - * @param string $str |
|
192 | - * @return string |
|
193 | - */ |
|
194 | - private static function _decimalToNR3(string $str): string |
|
195 | - { |
|
196 | - // if number is in exponent form |
|
197 | - if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
198 | - $parts = explode(".", $match[1]); |
|
199 | - $m = ltrim($parts[0], "0"); |
|
200 | - $e = intval($match[2]); |
|
201 | - // if mantissa had decimals |
|
202 | - if (count($parts) == 2) { |
|
203 | - $d = rtrim($parts[1], "0"); |
|
204 | - $e -= strlen($d); |
|
205 | - $m .= $d; |
|
206 | - } |
|
207 | - } else { |
|
208 | - // explode from decimal |
|
209 | - $parts = explode(".", $str); |
|
210 | - $m = ltrim($parts[0], "0"); |
|
211 | - // if number had decimals |
|
212 | - if (count($parts) == 2) { |
|
213 | - // exponent is negative number of the decimals |
|
214 | - $e = -strlen($parts[1]); |
|
215 | - // append decimals to the mantissa |
|
216 | - $m .= $parts[1]; |
|
217 | - } else { |
|
218 | - $e = 0; |
|
219 | - } |
|
220 | - // shift trailing zeroes from the mantissa to the exponent |
|
221 | - while (substr($m, -1) === "0") { |
|
222 | - $e++; |
|
223 | - $m = substr($m, 0, -1); |
|
224 | - } |
|
225 | - } |
|
226 | - /* if exponent is zero, it must be prefixed with a "+" sign |
|
188 | + /** |
|
189 | + * Convert decimal number string to NR3 form. |
|
190 | + * |
|
191 | + * @param string $str |
|
192 | + * @return string |
|
193 | + */ |
|
194 | + private static function _decimalToNR3(string $str): string |
|
195 | + { |
|
196 | + // if number is in exponent form |
|
197 | + if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
198 | + $parts = explode(".", $match[1]); |
|
199 | + $m = ltrim($parts[0], "0"); |
|
200 | + $e = intval($match[2]); |
|
201 | + // if mantissa had decimals |
|
202 | + if (count($parts) == 2) { |
|
203 | + $d = rtrim($parts[1], "0"); |
|
204 | + $e -= strlen($d); |
|
205 | + $m .= $d; |
|
206 | + } |
|
207 | + } else { |
|
208 | + // explode from decimal |
|
209 | + $parts = explode(".", $str); |
|
210 | + $m = ltrim($parts[0], "0"); |
|
211 | + // if number had decimals |
|
212 | + if (count($parts) == 2) { |
|
213 | + // exponent is negative number of the decimals |
|
214 | + $e = -strlen($parts[1]); |
|
215 | + // append decimals to the mantissa |
|
216 | + $m .= $parts[1]; |
|
217 | + } else { |
|
218 | + $e = 0; |
|
219 | + } |
|
220 | + // shift trailing zeroes from the mantissa to the exponent |
|
221 | + while (substr($m, -1) === "0") { |
|
222 | + $e++; |
|
223 | + $m = substr($m, 0, -1); |
|
224 | + } |
|
225 | + } |
|
226 | + /* if exponent is zero, it must be prefixed with a "+" sign |
|
227 | 227 | (X.690 07-2002, section 11.3.2.6) */ |
228 | - if (0 == $e) { |
|
229 | - $es = "+"; |
|
230 | - } else { |
|
231 | - $es = $e < 0 ? "-" : ""; |
|
232 | - } |
|
233 | - return sprintf("%s.E%s%d", $m, $es, abs($e)); |
|
234 | - } |
|
228 | + if (0 == $e) { |
|
229 | + $es = "+"; |
|
230 | + } else { |
|
231 | + $es = $e < 0 ? "-" : ""; |
|
232 | + } |
|
233 | + return sprintf("%s.E%s%d", $m, $es, abs($e)); |
|
234 | + } |
|
235 | 235 | |
236 | - /** |
|
237 | - * Convert NR3 form number to decimal. |
|
238 | - * |
|
239 | - * @param string $str |
|
240 | - * @throws \UnexpectedValueException |
|
241 | - * @return float |
|
242 | - */ |
|
243 | - private static function _nr3ToDecimal(string $str): float |
|
244 | - { |
|
245 | - if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
246 | - throw new \UnexpectedValueException( |
|
247 | - "'$str' is not a valid NR3 form real."); |
|
248 | - } |
|
249 | - $m = $match[2]; |
|
250 | - // if number started with minus sign |
|
251 | - $inv = $match[1] == "-"; |
|
252 | - $e = intval($match[3]); |
|
253 | - // positive exponent |
|
254 | - if ($e > 0) { |
|
255 | - // pad with trailing zeroes |
|
256 | - $num = $m . str_repeat("0", $e); |
|
257 | - } else if ($e < 0) { |
|
258 | - // pad with leading zeroes |
|
259 | - if (strlen($m) < abs($e)) { |
|
260 | - $m = str_repeat("0", abs($e) - strlen($m)) . $m; |
|
261 | - } |
|
262 | - // insert decimal point |
|
263 | - $num = substr($m, 0, $e) . "." . substr($m, $e); |
|
264 | - } else { |
|
265 | - $num = empty($m) ? "0" : $m; |
|
266 | - } |
|
267 | - // if number is negative |
|
268 | - if ($inv) { |
|
269 | - $num = "-$num"; |
|
270 | - } |
|
271 | - return floatval($num); |
|
272 | - } |
|
236 | + /** |
|
237 | + * Convert NR3 form number to decimal. |
|
238 | + * |
|
239 | + * @param string $str |
|
240 | + * @throws \UnexpectedValueException |
|
241 | + * @return float |
|
242 | + */ |
|
243 | + private static function _nr3ToDecimal(string $str): float |
|
244 | + { |
|
245 | + if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
246 | + throw new \UnexpectedValueException( |
|
247 | + "'$str' is not a valid NR3 form real."); |
|
248 | + } |
|
249 | + $m = $match[2]; |
|
250 | + // if number started with minus sign |
|
251 | + $inv = $match[1] == "-"; |
|
252 | + $e = intval($match[3]); |
|
253 | + // positive exponent |
|
254 | + if ($e > 0) { |
|
255 | + // pad with trailing zeroes |
|
256 | + $num = $m . str_repeat("0", $e); |
|
257 | + } else if ($e < 0) { |
|
258 | + // pad with leading zeroes |
|
259 | + if (strlen($m) < abs($e)) { |
|
260 | + $m = str_repeat("0", abs($e) - strlen($m)) . $m; |
|
261 | + } |
|
262 | + // insert decimal point |
|
263 | + $num = substr($m, 0, $e) . "." . substr($m, $e); |
|
264 | + } else { |
|
265 | + $num = empty($m) ? "0" : $m; |
|
266 | + } |
|
267 | + // if number is negative |
|
268 | + if ($inv) { |
|
269 | + $num = "-$num"; |
|
270 | + } |
|
271 | + return floatval($num); |
|
272 | + } |
|
273 | 273 | |
274 | - /** |
|
275 | - * Test that number is valid for this context. |
|
276 | - * |
|
277 | - * @param mixed $num |
|
278 | - * @return boolean |
|
279 | - */ |
|
280 | - private static function _validateNumber($num): bool |
|
281 | - { |
|
282 | - if (!preg_match(self::NR3_REGEX, $num)) { |
|
283 | - return false; |
|
284 | - } |
|
285 | - return true; |
|
286 | - } |
|
274 | + /** |
|
275 | + * Test that number is valid for this context. |
|
276 | + * |
|
277 | + * @param mixed $num |
|
278 | + * @return boolean |
|
279 | + */ |
|
280 | + private static function _validateNumber($num): bool |
|
281 | + { |
|
282 | + if (!preg_match(self::NR3_REGEX, $num)) { |
|
283 | + return false; |
|
284 | + } |
|
285 | + return true; |
|
286 | + } |
|
287 | 287 | } |
@@ -33,14 +33,14 @@ discard block |
||
33 | 33 | * @link http://php.net/manual/en/language.types.float.php |
34 | 34 | * @var string |
35 | 35 | */ |
36 | - const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */ |
|
37 | - '([+\-]?'. // sign |
|
38 | - '(?:'. |
|
39 | - '\d+'. // LNUM |
|
40 | - '|'. |
|
41 | - '(?:\d*\.\d+|\d+\.\d*)'. // DNUM |
|
42 | - '))[eE]'. |
|
43 | - '([+\-]?\d+)'. // exponent |
|
36 | + const PHP_EXPONENT_DNUM = '/^' . /* @formatter:off */ |
|
37 | + '([+\-]?' . // sign |
|
38 | + '(?:' . |
|
39 | + '\d+' . // LNUM |
|
40 | + '|' . |
|
41 | + '(?:\d*\.\d+|\d+\.\d*)' . // DNUM |
|
42 | + '))[eE]' . |
|
43 | + '([+\-]?\d+)' . // exponent |
|
44 | 44 | '$/'; /* @formatter:on */ |
45 | 45 | |
46 | 46 | /** |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | * @return self |
116 | 116 | */ |
117 | 117 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
118 | - int &$offset): ElementBase |
|
118 | + int & $offset): ElementBase |
|
119 | 119 | { |
120 | 120 | $idx = $offset; |
121 | 121 | $length = Length::expectFromDER($data, $idx)->intLength(); |
@@ -13,38 +13,38 @@ |
||
13 | 13 | */ |
14 | 14 | class RelativeOID extends ObjectIdentifier |
15 | 15 | { |
16 | - /** |
|
17 | - * Constructor. |
|
18 | - * |
|
19 | - * @param string $oid OID in dotted format |
|
20 | - */ |
|
21 | - public function __construct(string $oid) |
|
22 | - { |
|
23 | - $this->_oid = $oid; |
|
24 | - $this->_typeTag = self::TYPE_RELATIVE_OID; |
|
25 | - } |
|
16 | + /** |
|
17 | + * Constructor. |
|
18 | + * |
|
19 | + * @param string $oid OID in dotted format |
|
20 | + */ |
|
21 | + public function __construct(string $oid) |
|
22 | + { |
|
23 | + $this->_oid = $oid; |
|
24 | + $this->_typeTag = self::TYPE_RELATIVE_OID; |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * |
|
29 | - * {@inheritdoc} |
|
30 | - */ |
|
31 | - protected function _encodedContentDER(): string |
|
32 | - { |
|
33 | - return self::_encodeSubIDs(...self::_explodeDottedOID($this->_oid)); |
|
34 | - } |
|
27 | + /** |
|
28 | + * |
|
29 | + * {@inheritdoc} |
|
30 | + */ |
|
31 | + protected function _encodedContentDER(): string |
|
32 | + { |
|
33 | + return self::_encodeSubIDs(...self::_explodeDottedOID($this->_oid)); |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * |
|
38 | - * {@inheritdoc} |
|
39 | - * @return self |
|
40 | - */ |
|
41 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
42 | - int &$offset): ElementBase |
|
43 | - { |
|
44 | - $idx = $offset; |
|
45 | - $len = Length::expectFromDER($data, $idx)->intLength(); |
|
46 | - $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
47 | - $offset = $idx + $len; |
|
48 | - return new self(self::_implodeSubIDs(...$subids)); |
|
49 | - } |
|
36 | + /** |
|
37 | + * |
|
38 | + * {@inheritdoc} |
|
39 | + * @return self |
|
40 | + */ |
|
41 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
42 | + int &$offset): ElementBase |
|
43 | + { |
|
44 | + $idx = $offset; |
|
45 | + $len = Length::expectFromDER($data, $idx)->intLength(); |
|
46 | + $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
47 | + $offset = $idx + $len; |
|
48 | + return new self(self::_implodeSubIDs(...$subids)); |
|
49 | + } |
|
50 | 50 | } |
@@ -39,7 +39,7 @@ |
||
39 | 39 | * @return self |
40 | 40 | */ |
41 | 41 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
42 | - int &$offset): ElementBase |
|
42 | + int & $offset): ElementBase |
|
43 | 43 | { |
44 | 44 | $idx = $offset; |
45 | 45 | $len = Length::expectFromDER($data, $idx)->intLength(); |
@@ -17,160 +17,160 @@ |
||
17 | 17 | */ |
18 | 18 | class ObjectIdentifier extends Element |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Object identifier in dotted format. |
|
25 | - * |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - protected $_oid; |
|
23 | + /** |
|
24 | + * Object identifier in dotted format. |
|
25 | + * |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + protected $_oid; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Constructor. |
|
32 | - * |
|
33 | - * @param string $oid OID in dotted format |
|
34 | - */ |
|
35 | - public function __construct(string $oid) |
|
36 | - { |
|
37 | - $this->_oid = $oid; |
|
38 | - $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER; |
|
39 | - } |
|
30 | + /** |
|
31 | + * Constructor. |
|
32 | + * |
|
33 | + * @param string $oid OID in dotted format |
|
34 | + */ |
|
35 | + public function __construct(string $oid) |
|
36 | + { |
|
37 | + $this->_oid = $oid; |
|
38 | + $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER; |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Get OID in dotted format. |
|
43 | - * |
|
44 | - * @return string |
|
45 | - */ |
|
46 | - public function oid(): string |
|
47 | - { |
|
48 | - return $this->_oid; |
|
49 | - } |
|
41 | + /** |
|
42 | + * Get OID in dotted format. |
|
43 | + * |
|
44 | + * @return string |
|
45 | + */ |
|
46 | + public function oid(): string |
|
47 | + { |
|
48 | + return $this->_oid; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * |
|
53 | - * {@inheritdoc} |
|
54 | - */ |
|
55 | - protected function _encodedContentDER(): string |
|
56 | - { |
|
57 | - $subids = self::_explodeDottedOID($this->_oid); |
|
58 | - // encode first two subids to one according to spec section 8.19.4 |
|
59 | - if (count($subids) >= 2) { |
|
60 | - $num = ($subids[0] * 40) + $subids[1]; |
|
61 | - array_splice($subids, 0, 2, array($num)); |
|
62 | - } |
|
63 | - return self::_encodeSubIDs(...$subids); |
|
64 | - } |
|
51 | + /** |
|
52 | + * |
|
53 | + * {@inheritdoc} |
|
54 | + */ |
|
55 | + protected function _encodedContentDER(): string |
|
56 | + { |
|
57 | + $subids = self::_explodeDottedOID($this->_oid); |
|
58 | + // encode first two subids to one according to spec section 8.19.4 |
|
59 | + if (count($subids) >= 2) { |
|
60 | + $num = ($subids[0] * 40) + $subids[1]; |
|
61 | + array_splice($subids, 0, 2, array($num)); |
|
62 | + } |
|
63 | + return self::_encodeSubIDs(...$subids); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * |
|
68 | - * {@inheritdoc} |
|
69 | - * @return self |
|
70 | - */ |
|
71 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
72 | - int &$offset): ElementBase |
|
73 | - { |
|
74 | - $idx = $offset; |
|
75 | - $len = Length::expectFromDER($data, $idx)->intLength(); |
|
76 | - $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
77 | - $idx += $len; |
|
78 | - // decode first subidentifier according to spec section 8.19.4 |
|
79 | - if (isset($subids[0])) { |
|
80 | - list($x, $y) = gmp_div_qr($subids[0], "40"); |
|
81 | - array_splice($subids, 0, 1, array($x, $y)); |
|
82 | - } |
|
83 | - $offset = $idx; |
|
84 | - return new self(self::_implodeSubIDs(...$subids)); |
|
85 | - } |
|
66 | + /** |
|
67 | + * |
|
68 | + * {@inheritdoc} |
|
69 | + * @return self |
|
70 | + */ |
|
71 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
72 | + int &$offset): ElementBase |
|
73 | + { |
|
74 | + $idx = $offset; |
|
75 | + $len = Length::expectFromDER($data, $idx)->intLength(); |
|
76 | + $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
77 | + $idx += $len; |
|
78 | + // decode first subidentifier according to spec section 8.19.4 |
|
79 | + if (isset($subids[0])) { |
|
80 | + list($x, $y) = gmp_div_qr($subids[0], "40"); |
|
81 | + array_splice($subids, 0, 1, array($x, $y)); |
|
82 | + } |
|
83 | + $offset = $idx; |
|
84 | + return new self(self::_implodeSubIDs(...$subids)); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * Explode dotted OID to an array of sub ID's. |
|
89 | - * |
|
90 | - * @param string $oid OID in dotted format |
|
91 | - * @return \GMP[] Array of GMP numbers |
|
92 | - */ |
|
93 | - protected static function _explodeDottedOID($oid): array |
|
94 | - { |
|
95 | - $subids = []; |
|
96 | - foreach (explode(".", $oid) as $subid) { |
|
97 | - $subids[] = gmp_init($subid, 10); |
|
98 | - } |
|
99 | - return $subids; |
|
100 | - } |
|
87 | + /** |
|
88 | + * Explode dotted OID to an array of sub ID's. |
|
89 | + * |
|
90 | + * @param string $oid OID in dotted format |
|
91 | + * @return \GMP[] Array of GMP numbers |
|
92 | + */ |
|
93 | + protected static function _explodeDottedOID($oid): array |
|
94 | + { |
|
95 | + $subids = []; |
|
96 | + foreach (explode(".", $oid) as $subid) { |
|
97 | + $subids[] = gmp_init($subid, 10); |
|
98 | + } |
|
99 | + return $subids; |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Implode an array of sub IDs to dotted OID format. |
|
104 | - * |
|
105 | - * @param \GMP[] $subids |
|
106 | - * @return string |
|
107 | - */ |
|
108 | - protected static function _implodeSubIDs(\GMP ...$subids): string |
|
109 | - { |
|
110 | - return implode(".", |
|
111 | - array_map( |
|
112 | - function ($num) { |
|
113 | - return gmp_strval($num, 10); |
|
114 | - }, $subids)); |
|
115 | - } |
|
102 | + /** |
|
103 | + * Implode an array of sub IDs to dotted OID format. |
|
104 | + * |
|
105 | + * @param \GMP[] $subids |
|
106 | + * @return string |
|
107 | + */ |
|
108 | + protected static function _implodeSubIDs(\GMP ...$subids): string |
|
109 | + { |
|
110 | + return implode(".", |
|
111 | + array_map( |
|
112 | + function ($num) { |
|
113 | + return gmp_strval($num, 10); |
|
114 | + }, $subids)); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Encode sub ID's to DER. |
|
119 | - * |
|
120 | - * @param \GMP[] $subids |
|
121 | - * @return string |
|
122 | - */ |
|
123 | - protected static function _encodeSubIDs(\GMP ...$subids): string |
|
124 | - { |
|
125 | - $data = ""; |
|
126 | - foreach ($subids as $subid) { |
|
127 | - // if number fits to one base 128 byte |
|
128 | - if ($subid < 128) { |
|
129 | - $data .= chr(intval($subid)); |
|
130 | - } else { // encode to multiple bytes |
|
131 | - $bytes = []; |
|
132 | - do { |
|
133 | - array_unshift($bytes, 0x7f & gmp_intval($subid)); |
|
134 | - $subid >>= 7; |
|
135 | - } while ($subid > 0); |
|
136 | - // all bytes except last must have bit 8 set to one |
|
137 | - foreach (array_splice($bytes, 0, -1) as $byte) { |
|
138 | - $data .= chr(0x80 | $byte); |
|
139 | - } |
|
140 | - $data .= chr(reset($bytes)); |
|
141 | - } |
|
142 | - } |
|
143 | - return $data; |
|
144 | - } |
|
117 | + /** |
|
118 | + * Encode sub ID's to DER. |
|
119 | + * |
|
120 | + * @param \GMP[] $subids |
|
121 | + * @return string |
|
122 | + */ |
|
123 | + protected static function _encodeSubIDs(\GMP ...$subids): string |
|
124 | + { |
|
125 | + $data = ""; |
|
126 | + foreach ($subids as $subid) { |
|
127 | + // if number fits to one base 128 byte |
|
128 | + if ($subid < 128) { |
|
129 | + $data .= chr(intval($subid)); |
|
130 | + } else { // encode to multiple bytes |
|
131 | + $bytes = []; |
|
132 | + do { |
|
133 | + array_unshift($bytes, 0x7f & gmp_intval($subid)); |
|
134 | + $subid >>= 7; |
|
135 | + } while ($subid > 0); |
|
136 | + // all bytes except last must have bit 8 set to one |
|
137 | + foreach (array_splice($bytes, 0, -1) as $byte) { |
|
138 | + $data .= chr(0x80 | $byte); |
|
139 | + } |
|
140 | + $data .= chr(reset($bytes)); |
|
141 | + } |
|
142 | + } |
|
143 | + return $data; |
|
144 | + } |
|
145 | 145 | |
146 | - /** |
|
147 | - * Decode sub ID's from DER data. |
|
148 | - * |
|
149 | - * @param string $data |
|
150 | - * @throws DecodeException |
|
151 | - * @return \GMP[] Array of GMP numbers |
|
152 | - */ |
|
153 | - protected static function _decodeSubIDs($data): array |
|
154 | - { |
|
155 | - $subids = []; |
|
156 | - $idx = 0; |
|
157 | - $end = strlen($data); |
|
158 | - while ($idx < $end) { |
|
159 | - $num = gmp_init("0", 10); |
|
160 | - while (true) { |
|
161 | - if ($idx >= $end) { |
|
162 | - throw new DecodeException("Unexpected end of data."); |
|
163 | - } |
|
164 | - $byte = ord($data[$idx++]); |
|
165 | - $num |= $byte & 0x7f; |
|
166 | - // bit 8 of the last octet is zero |
|
167 | - if (!($byte & 0x80)) { |
|
168 | - break; |
|
169 | - } |
|
170 | - $num <<= 7; |
|
171 | - } |
|
172 | - $subids[] = $num; |
|
173 | - } |
|
174 | - return $subids; |
|
175 | - } |
|
146 | + /** |
|
147 | + * Decode sub ID's from DER data. |
|
148 | + * |
|
149 | + * @param string $data |
|
150 | + * @throws DecodeException |
|
151 | + * @return \GMP[] Array of GMP numbers |
|
152 | + */ |
|
153 | + protected static function _decodeSubIDs($data): array |
|
154 | + { |
|
155 | + $subids = []; |
|
156 | + $idx = 0; |
|
157 | + $end = strlen($data); |
|
158 | + while ($idx < $end) { |
|
159 | + $num = gmp_init("0", 10); |
|
160 | + while (true) { |
|
161 | + if ($idx >= $end) { |
|
162 | + throw new DecodeException("Unexpected end of data."); |
|
163 | + } |
|
164 | + $byte = ord($data[$idx++]); |
|
165 | + $num |= $byte & 0x7f; |
|
166 | + // bit 8 of the last octet is zero |
|
167 | + if (!($byte & 0x80)) { |
|
168 | + break; |
|
169 | + } |
|
170 | + $num <<= 7; |
|
171 | + } |
|
172 | + $subids[] = $num; |
|
173 | + } |
|
174 | + return $subids; |
|
175 | + } |
|
176 | 176 | } |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | * @return self |
70 | 70 | */ |
71 | 71 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
72 | - int &$offset): ElementBase |
|
72 | + int & $offset): ElementBase |
|
73 | 73 | { |
74 | 74 | $idx = $offset; |
75 | 75 | $len = Length::expectFromDER($data, $idx)->intLength(); |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | { |
110 | 110 | return implode(".", |
111 | 111 | array_map( |
112 | - function ($num) { |
|
112 | + function($num) { |
|
113 | 113 | return gmp_strval($num, 10); |
114 | 114 | }, $subids)); |
115 | 115 | } |
@@ -17,112 +17,112 @@ |
||
17 | 17 | */ |
18 | 18 | class GeneralizedTime extends TimeType |
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 = /* @formatter:off */ '#^' . |
|
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 | - '$#' /* @formatter:on */; |
|
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 = /* @formatter:off */ '#^' . |
|
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 | + '$#' /* @formatter:on */; |
|
40 | 40 | |
41 | - /** |
|
42 | - * Cached formatted date. |
|
43 | - * |
|
44 | - * @var string|null |
|
45 | - */ |
|
46 | - private $_formatted; |
|
41 | + /** |
|
42 | + * Cached formatted date. |
|
43 | + * |
|
44 | + * @var string|null |
|
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 | - * |
|
69 | - * {@inheritdoc} |
|
70 | - */ |
|
71 | - protected function _encodedContentDER(): string |
|
72 | - { |
|
73 | - if (!isset($this->_formatted)) { |
|
74 | - $dt = $this->_dateTime->setTimezone( |
|
75 | - self::_createTimeZone(self::TZ_UTC)); |
|
76 | - $this->_formatted = $dt->format("YmdHis"); |
|
77 | - // if fractions were used |
|
78 | - $frac = $dt->format("u"); |
|
79 | - if ($frac != 0) { |
|
80 | - $frac = rtrim($frac, "0"); |
|
81 | - $this->_formatted .= ".$frac"; |
|
82 | - } |
|
83 | - // timezone |
|
84 | - $this->_formatted .= "Z"; |
|
85 | - } |
|
86 | - return $this->_formatted; |
|
87 | - } |
|
67 | + /** |
|
68 | + * |
|
69 | + * {@inheritdoc} |
|
70 | + */ |
|
71 | + protected function _encodedContentDER(): string |
|
72 | + { |
|
73 | + if (!isset($this->_formatted)) { |
|
74 | + $dt = $this->_dateTime->setTimezone( |
|
75 | + self::_createTimeZone(self::TZ_UTC)); |
|
76 | + $this->_formatted = $dt->format("YmdHis"); |
|
77 | + // if fractions were used |
|
78 | + $frac = $dt->format("u"); |
|
79 | + if ($frac != 0) { |
|
80 | + $frac = rtrim($frac, "0"); |
|
81 | + $this->_formatted .= ".$frac"; |
|
82 | + } |
|
83 | + // timezone |
|
84 | + $this->_formatted .= "Z"; |
|
85 | + } |
|
86 | + return $this->_formatted; |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * |
|
91 | - * {@inheritdoc} |
|
92 | - * @return self |
|
93 | - */ |
|
94 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
95 | - int &$offset): ElementBase |
|
96 | - { |
|
97 | - $idx = $offset; |
|
98 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
99 | - $str = substr($data, $idx, $length); |
|
100 | - $idx += $length; |
|
101 | - if (!preg_match(self::REGEX, $str, $match)) { |
|
102 | - throw new DecodeException("Invalid GeneralizedTime format."); |
|
103 | - } |
|
104 | - list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
105 | - if (isset($match[7])) { |
|
106 | - $frac = $match[7]; |
|
107 | - // DER restricts trailing zeroes in fractional seconds component |
|
108 | - if ('0' === $frac[strlen($frac) - 1]) { |
|
109 | - throw new DecodeException( |
|
110 | - "Fractional seconds must omit trailing zeroes."); |
|
111 | - } |
|
112 | - $frac = (int) $frac; |
|
113 | - } else { |
|
114 | - $frac = 0; |
|
115 | - } |
|
116 | - $time = $year . $month . $day . $hour . $minute . $second . "." . $frac . |
|
117 | - self::TZ_UTC; |
|
118 | - $dt = \DateTimeImmutable::createFromFormat("!YmdHis.uT", $time, |
|
119 | - self::_createTimeZone(self::TZ_UTC)); |
|
120 | - if (!$dt) { |
|
121 | - throw new DecodeException( |
|
122 | - "Failed to decode GeneralizedTime: " . |
|
123 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
124 | - } |
|
125 | - $offset = $idx; |
|
126 | - return new self($dt); |
|
127 | - } |
|
89 | + /** |
|
90 | + * |
|
91 | + * {@inheritdoc} |
|
92 | + * @return self |
|
93 | + */ |
|
94 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
95 | + int &$offset): ElementBase |
|
96 | + { |
|
97 | + $idx = $offset; |
|
98 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
99 | + $str = substr($data, $idx, $length); |
|
100 | + $idx += $length; |
|
101 | + if (!preg_match(self::REGEX, $str, $match)) { |
|
102 | + throw new DecodeException("Invalid GeneralizedTime format."); |
|
103 | + } |
|
104 | + list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
105 | + if (isset($match[7])) { |
|
106 | + $frac = $match[7]; |
|
107 | + // DER restricts trailing zeroes in fractional seconds component |
|
108 | + if ('0' === $frac[strlen($frac) - 1]) { |
|
109 | + throw new DecodeException( |
|
110 | + "Fractional seconds must omit trailing zeroes."); |
|
111 | + } |
|
112 | + $frac = (int) $frac; |
|
113 | + } else { |
|
114 | + $frac = 0; |
|
115 | + } |
|
116 | + $time = $year . $month . $day . $hour . $minute . $second . "." . $frac . |
|
117 | + self::TZ_UTC; |
|
118 | + $dt = \DateTimeImmutable::createFromFormat("!YmdHis.uT", $time, |
|
119 | + self::_createTimeZone(self::TZ_UTC)); |
|
120 | + if (!$dt) { |
|
121 | + throw new DecodeException( |
|
122 | + "Failed to decode GeneralizedTime: " . |
|
123 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
124 | + } |
|
125 | + $offset = $idx; |
|
126 | + return new self($dt); |
|
127 | + } |
|
128 | 128 | } |
@@ -92,7 +92,7 @@ |
||
92 | 92 | * @return self |
93 | 93 | */ |
94 | 94 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
95 | - int &$offset): ElementBase |
|
95 | + int & $offset): ElementBase |
|
96 | 96 | { |
97 | 97 | $idx = $offset; |
98 | 98 | $length = Length::expectFromDER($data, $idx)->intLength(); |