1 | <?php |
||
14 | class Length implements Encodable |
||
15 | { |
||
16 | /** |
||
17 | * Length. |
||
18 | * |
||
19 | * @var BigInt |
||
20 | */ |
||
21 | private $_length; |
||
22 | |||
23 | /** |
||
24 | * Whether length is indefinite. |
||
25 | * |
||
26 | * @var bool |
||
27 | */ |
||
28 | private $_indefinite; |
||
29 | |||
30 | /** |
||
31 | * Constructor. |
||
32 | * |
||
33 | * @param int|string $length Length |
||
34 | * @param bool $indefinite Whether length is indefinite |
||
35 | */ |
||
36 | 328 | public function __construct($length, bool $indefinite = false) |
|
37 | { |
||
38 | 328 | $this->_length = new BigInt($length); |
|
39 | 328 | $this->_indefinite = $indefinite; |
|
40 | 328 | } |
|
41 | |||
42 | /** |
||
43 | * Decode length component from DER data. |
||
44 | * |
||
45 | * @param string $data DER encoded data |
||
46 | * @param int|null $offset Reference to the variable that contains offset |
||
47 | * into the data where to start parsing. Variable is updated to |
||
48 | * the offset next to the parsed length component. If null, start |
||
49 | * from offset 0. |
||
50 | * @throws DecodeException If decoding fails |
||
51 | * @return self |
||
52 | */ |
||
53 | 227 | public static function fromDER(string $data, int &$offset = null): self |
|
54 | { |
||
55 | 227 | $idx = $offset ?? 0; |
|
56 | 227 | $datalen = strlen($data); |
|
57 | 227 | if ($idx >= $datalen) { |
|
58 | 1 | throw new DecodeException("Invalid offset."); |
|
59 | } |
||
60 | 226 | $indefinite = false; |
|
61 | 226 | $byte = ord($data[$idx++]); |
|
62 | // bits 7 to 1 |
||
63 | 226 | $length = (0x7f & $byte); |
|
64 | // long form |
||
65 | 226 | if (0x80 & $byte) { |
|
66 | 12 | if (!$length) { |
|
67 | 4 | $indefinite = true; |
|
68 | } else { |
||
69 | 8 | if ($idx + $length > $datalen) { |
|
70 | 1 | throw new DecodeException("Too many length octets."); |
|
71 | } |
||
72 | 7 | $length = self::_decodeLongFormLength($length, $data, $idx); |
|
73 | } |
||
74 | } |
||
75 | 224 | if (isset($offset)) { |
|
76 | 214 | $offset = $idx; |
|
77 | } |
||
78 | 224 | return new self($length, $indefinite); |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * Decode long form length. |
||
83 | * |
||
84 | * @param int $length Number of octets |
||
85 | * @param string $data Data |
||
86 | * @param int $offset Reference to the variable containing offset to the |
||
87 | * data. |
||
88 | * @throws DecodeException If decoding fails |
||
89 | * @return string Integer as a string |
||
90 | */ |
||
91 | 7 | private static function _decodeLongFormLength(int $length, string $data, |
|
92 | int &$offset): string |
||
93 | { |
||
94 | // first octet must not be 0xff (spec 8.1.3.5c) |
||
95 | 7 | if ($length == 127) { |
|
96 | 1 | throw new DecodeException("Invalid number of length octets."); |
|
97 | } |
||
98 | 6 | $num = gmp_init(0, 10); |
|
99 | 6 | while (--$length >= 0) { |
|
100 | 6 | $byte = ord($data[$offset++]); |
|
101 | 6 | $num <<= 8; |
|
102 | 6 | $num |= $byte; |
|
103 | } |
||
104 | 6 | return gmp_strval($num); |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Decode length from DER. |
||
109 | * |
||
110 | * Throws an exception if length doesn't match with expected or if data |
||
111 | * doesn't contain enough bytes. |
||
112 | * |
||
113 | * @see self::fromDER |
||
114 | * @param string $data DER data |
||
115 | * @param int $offset Reference to the offset variable |
||
116 | * @param int|null $expected Expected length, null to bypass checking |
||
117 | * @throws DecodeException If decoding or expectation fails |
||
118 | * @return self |
||
119 | */ |
||
120 | 214 | public static function expectFromDER(string $data, int &$offset, |
|
144 | |||
145 | /** |
||
146 | * |
||
147 | * @see Encodable::toDER() |
||
148 | * @throws \DomainException If length is too large to encode |
||
149 | * @return string |
||
150 | */ |
||
151 | 124 | public function toDER(): string |
|
179 | |||
180 | /** |
||
181 | * Get the length. |
||
182 | * |
||
183 | * @throws \LogicException If length is indefinite |
||
184 | * @return string Length as an integer string |
||
185 | */ |
||
186 | 5 | public function length(): string |
|
193 | |||
194 | /** |
||
195 | * Get the length as an integer. |
||
196 | * |
||
197 | * @throws \LogicException If length is indefinite |
||
198 | * @throws \RuntimeException If length overflows integer size |
||
199 | * @return int |
||
200 | */ |
||
201 | 217 | public function intLength(): int |
|
208 | |||
209 | /** |
||
210 | * Whether length is indefinite. |
||
211 | * |
||
212 | * @return bool |
||
213 | */ |
||
214 | 216 | public function isIndefinite(): bool |
|
218 | } |
||
219 |