@@ -100,36 +100,35 @@ discard block |
||
100 | 100 | } |
101 | 101 | protected function decode($header) |
102 | 102 | { |
103 | - $contents = $header['content_length'] > 0 ? |
|
104 | - $this->reader->chunk($header['content_start'], $header['content_length']) : |
|
105 | - ''; |
|
106 | - if ($header['class'] !== ASN1::CLASS_UNIVERSAL) { |
|
103 | + $contents = $header[ 'content_length' ] > 0 ? |
|
104 | + $this->reader->chunk($header[ 'content_start' ], $header[ 'content_length' ]) : ''; |
|
105 | + if ($header[ 'class' ] !== ASN1::CLASS_UNIVERSAL) { |
|
107 | 106 | return $contents; |
108 | 107 | } |
109 | - switch ($header['tag']) { |
|
108 | + switch ($header[ 'tag' ]) { |
|
110 | 109 | case ASN1::TYPE_BOOLEAN: |
111 | - return (bool)ord($contents[0]); |
|
110 | + return (bool) ord($contents[ 0 ]); |
|
112 | 111 | case ASN1::TYPE_INTEGER: |
113 | 112 | return ASN1::fromBase256($contents); |
114 | 113 | case ASN1::TYPE_ENUMERATED: |
115 | - return (int)base_convert(ASN1::fromBase256($contents), 2, 10); |
|
114 | + return (int) base_convert(ASN1::fromBase256($contents), 2, 10); |
|
116 | 115 | case ASN1::TYPE_REAL: |
117 | 116 | // TODO: read the specs |
118 | 117 | return false; |
119 | 118 | case ASN1::TYPE_BIT_STRING: |
120 | - if ($header['constructed']) { |
|
119 | + if ($header[ 'constructed' ]) { |
|
121 | 120 | $temp = static::fromString($contents)->values(); |
122 | 121 | $real = ''; |
123 | 122 | for ($i = 0; $i < count($temp) - 1; $i++) { |
124 | - $real .= $temp['value']; |
|
123 | + $real .= $temp[ 'value' ]; |
|
125 | 124 | } |
126 | - return $temp[count($temp) - 1]['value'][0] . $real . substr($temp[$i]['value'], 1); |
|
125 | + return $temp[ count($temp) - 1 ][ 'value' ][ 0 ].$real.substr($temp[ $i ][ 'value' ], 1); |
|
127 | 126 | } |
128 | 127 | return $contents; |
129 | 128 | case ASN1::TYPE_OCTET_STRING: |
130 | - if ($header['constructed']) { |
|
131 | - return implode('', array_map(function ($v) { |
|
132 | - return $v['value']; |
|
129 | + if ($header[ 'constructed' ]) { |
|
130 | + return implode('', array_map(function($v) { |
|
131 | + return $v[ 'value' ]; |
|
133 | 132 | }, static::fromString($contents)->values())); |
134 | 133 | } |
135 | 134 | return $contents; |
@@ -137,14 +136,14 @@ discard block |
||
137 | 136 | return null; |
138 | 137 | case ASN1::TYPE_UTC_TIME: |
139 | 138 | $format = 'YmdHis'; |
140 | - $matches = []; |
|
139 | + $matches = [ ]; |
|
141 | 140 | if (preg_match('#^(\d{10})(Z|[+-]\d{4})$#', $contents, $matches)) { |
142 | - $contents = $matches[1] . '00' . $matches[2]; |
|
141 | + $contents = $matches[ 1 ].'00'.$matches[ 2 ]; |
|
143 | 142 | } |
144 | 143 | $prefix = substr($contents, 0, 2) >= 50 ? '19' : '20'; |
145 | - $contents = $prefix . $contents; |
|
146 | - if ($contents[strlen($contents) - 1] == 'Z') { |
|
147 | - $contents = substr($contents, 0, -1) . '+0000'; |
|
144 | + $contents = $prefix.$contents; |
|
145 | + if ($contents[ strlen($contents) - 1 ] == 'Z') { |
|
146 | + $contents = substr($contents, 0, -1).'+0000'; |
|
148 | 147 | } |
149 | 148 | if (strpos($contents, '-') !== false || strpos($contents, '+') !== false) { |
150 | 149 | $format .= 'O'; |
@@ -156,8 +155,8 @@ discard block |
||
156 | 155 | if (strpos($contents, '.') !== false) { |
157 | 156 | $format .= '.u'; |
158 | 157 | } |
159 | - if ($contents[strlen($contents) - 1] == 'Z') { |
|
160 | - $contents = substr($contents, 0, -1) . '+0000'; |
|
158 | + if ($contents[ strlen($contents) - 1 ] == 'Z') { |
|
159 | + $contents = substr($contents, 0, -1).'+0000'; |
|
161 | 160 | } |
162 | 161 | if (strpos($contents, '-') !== false || strpos($contents, '+') !== false) { |
163 | 162 | $format .= 'O'; |
@@ -165,16 +164,16 @@ discard block |
||
165 | 164 | $result = @DateTime::createFromFormat($format, $contents); |
166 | 165 | return $result ? $result->getTimestamp() : false; |
167 | 166 | case ASN1::TYPE_OBJECT_IDENTIFIER: |
168 | - $temp = ord($contents[0]); |
|
167 | + $temp = ord($contents[ 0 ]); |
|
169 | 168 | $real = sprintf('%d.%d', floor($temp / 40), $temp % 40); |
170 | 169 | $obid = 0; |
171 | 170 | // process septets |
172 | 171 | for ($i = 1; $i < strlen($contents); $i++) { |
173 | - $temp = ord($contents[$i]); |
|
172 | + $temp = ord($contents[ $i ]); |
|
174 | 173 | $obid <<= 7; |
175 | 174 | $obid |= $temp & 0x7F; |
176 | 175 | if (~$temp & 0x80) { |
177 | - $real .= '.' . $obid; |
|
176 | + $real .= '.'.$obid; |
|
178 | 177 | $obid = 0; |
179 | 178 | } |
180 | 179 | } |
@@ -191,55 +190,55 @@ discard block |
||
191 | 190 | */ |
192 | 191 | public function structure($max = null) |
193 | 192 | { |
194 | - $skeleton = []; |
|
193 | + $skeleton = [ ]; |
|
195 | 194 | while (!$this->reader->eof() && ($max === null || $this->reader->pos() < $max)) { |
196 | 195 | $header = $this->header(); |
197 | - if ($header['class'] === 0 && $header['tag'] === 0) { |
|
196 | + if ($header[ 'class' ] === 0 && $header[ 'tag' ] === 0) { |
|
198 | 197 | if ($max === null) { |
199 | 198 | break; |
200 | 199 | } else { |
201 | 200 | continue; |
202 | 201 | } |
203 | 202 | } |
204 | - if ($header['class'] !== ASN1::CLASS_UNIVERSAL && $header['constructed']) { |
|
205 | - $header['children'] = $this->structure( |
|
206 | - $header['length'] ? $header['start'] + $header['length'] - 1 : null |
|
203 | + if ($header[ 'class' ] !== ASN1::CLASS_UNIVERSAL && $header[ 'constructed' ]) { |
|
204 | + $header[ 'children' ] = $this->structure( |
|
205 | + $header[ 'length' ] ? $header[ 'start' ] + $header[ 'length' ] - 1 : null |
|
207 | 206 | ); |
208 | - if ($header['length'] === null) { |
|
207 | + if ($header[ 'length' ] === null) { |
|
209 | 208 | $this->reader->byte(); |
210 | - $header['length'] = $this->reader->pos() - $header['start']; |
|
211 | - $header['content_length'] = $this->reader->pos() - $header['content_start']; |
|
209 | + $header[ 'length' ] = $this->reader->pos() - $header[ 'start' ]; |
|
210 | + $header[ 'content_length' ] = $this->reader->pos() - $header[ 'content_start' ]; |
|
212 | 211 | } |
213 | - $skeleton[] = $header; |
|
212 | + $skeleton[ ] = $header; |
|
214 | 213 | } else { |
215 | - if ($header['class'] === ASN1::CLASS_UNIVERSAL && |
|
216 | - in_array($header['tag'], [ASN1::TYPE_SET, ASN1::TYPE_SEQUENCE]) |
|
214 | + if ($header[ 'class' ] === ASN1::CLASS_UNIVERSAL && |
|
215 | + in_array($header[ 'tag' ], [ ASN1::TYPE_SET, ASN1::TYPE_SEQUENCE ]) |
|
217 | 216 | ) { |
218 | - $header['children'] = $this->structure( |
|
219 | - $header['length'] ? $header['start'] + $header['length'] - 1 : null |
|
217 | + $header[ 'children' ] = $this->structure( |
|
218 | + $header[ 'length' ] ? $header[ 'start' ] + $header[ 'length' ] - 1 : null |
|
220 | 219 | ); |
221 | - if ($header['length'] === null) { |
|
220 | + if ($header[ 'length' ] === null) { |
|
222 | 221 | $this->reader->byte(); |
223 | - $header['length'] = $this->reader->pos() - $header['start']; |
|
224 | - $header['content_length'] = $this->reader->pos() - $header['content_start']; |
|
222 | + $header[ 'length' ] = $this->reader->pos() - $header[ 'start' ]; |
|
223 | + $header[ 'content_length' ] = $this->reader->pos() - $header[ 'content_start' ]; |
|
225 | 224 | } |
226 | 225 | } else { |
227 | - if ($header['length'] === null) { |
|
226 | + if ($header[ 'length' ] === null) { |
|
228 | 227 | $this->reader->readUntil(chr(0).chr(0)); |
229 | - $header['length'] = $this->reader->pos() - $header['start']; |
|
230 | - $header['content_length'] = $this->reader->pos() - $header['content_start']; |
|
228 | + $header[ 'length' ] = $this->reader->pos() - $header[ 'start' ]; |
|
229 | + $header[ 'content_length' ] = $this->reader->pos() - $header[ 'content_start' ]; |
|
231 | 230 | } else { |
232 | - if ($header['content_length'] > 0) { |
|
233 | - $this->reader->bytes($header['content_length']); |
|
231 | + if ($header[ 'content_length' ] > 0) { |
|
232 | + $this->reader->bytes($header[ 'content_length' ]); |
|
234 | 233 | } |
235 | 234 | } |
236 | 235 | } |
237 | - if (!isset($header['children'])) { |
|
236 | + if (!isset($header[ 'children' ])) { |
|
238 | 237 | $pos = $this->reader->pos(); |
239 | - $header['value'] = $this->decode($header); |
|
238 | + $header[ 'value' ] = $this->decode($header); |
|
240 | 239 | $this->reader->seek($pos); |
241 | 240 | } |
242 | - $skeleton[] = $header; |
|
241 | + $skeleton[ ] = $header; |
|
243 | 242 | } |
244 | 243 | } |
245 | 244 | return $skeleton; |
@@ -254,10 +253,10 @@ discard block |
||
254 | 253 | { |
255 | 254 | $skeleton = $skeleton ?? $this->structure(); |
256 | 255 | foreach ($skeleton as $k => $v) { |
257 | - if (isset($v['children'])) { |
|
258 | - $skeleton[$k] = $this->values($v['children']); |
|
256 | + if (isset($v[ 'children' ])) { |
|
257 | + $skeleton[ $k ] = $this->values($v[ 'children' ]); |
|
259 | 258 | } else { |
260 | - $skeleton[$k] = $v['value'] ?? null; |
|
259 | + $skeleton[ $k ] = $v[ 'value' ] ?? null; |
|
261 | 260 | } |
262 | 261 | } |
263 | 262 | return $skeleton; |
@@ -274,109 +273,109 @@ discard block |
||
274 | 273 | if ($skeleton === null && $this->reader->pos() !== 0) { |
275 | 274 | $this->reader->rewind(); |
276 | 275 | } |
277 | - $skeleton = $skeleton ?? $this->structure()[0]; |
|
278 | - if ($skeleton['class'] !== ASN1::CLASS_UNIVERSAL) { |
|
279 | - if ($map['tag'] === ASN1::TYPE_CHOICE) { |
|
280 | - foreach ($map['children'] as $child) { |
|
281 | - if (isset($child['name']) && (int)$skeleton['tag'] === (int)$child['name']) { |
|
276 | + $skeleton = $skeleton ?? $this->structure()[ 0 ]; |
|
277 | + if ($skeleton[ 'class' ] !== ASN1::CLASS_UNIVERSAL) { |
|
278 | + if ($map[ 'tag' ] === ASN1::TYPE_CHOICE) { |
|
279 | + foreach ($map[ 'children' ] as $child) { |
|
280 | + if (isset($child[ 'name' ]) && (int) $skeleton[ 'tag' ] === (int) $child[ 'name' ]) { |
|
282 | 281 | $map = $child; |
283 | - if (isset($child['value']) && $child['value']) { |
|
284 | - return $child['value']; |
|
282 | + if (isset($child[ 'value' ]) && $child[ 'value' ]) { |
|
283 | + return $child[ 'value' ]; |
|
285 | 284 | } |
286 | 285 | break; |
287 | 286 | } |
288 | 287 | } |
289 | 288 | } |
290 | 289 | } |
291 | - if ($skeleton['class'] !== ASN1::CLASS_UNIVERSAL) { |
|
292 | - if (isset($map['implicit']) && $map['implicit']) { |
|
293 | - $skeleton['class'] = ASN1::CLASS_UNIVERSAL; |
|
294 | - $skeleton['tag'] = $map['tag']; |
|
290 | + if ($skeleton[ 'class' ] !== ASN1::CLASS_UNIVERSAL) { |
|
291 | + if (isset($map[ 'implicit' ]) && $map[ 'implicit' ]) { |
|
292 | + $skeleton[ 'class' ] = ASN1::CLASS_UNIVERSAL; |
|
293 | + $skeleton[ 'tag' ] = $map[ 'tag' ]; |
|
295 | 294 | } else { |
296 | - $skeleton = $skeleton['children'][0] ?? null; |
|
295 | + $skeleton = $skeleton[ 'children' ][ 0 ] ?? null; |
|
297 | 296 | } |
298 | 297 | } |
299 | - if ($map['tag'] === ASN1::TYPE_CHOICE) { |
|
300 | - foreach ($map['children'] as $child) { |
|
301 | - if ($skeleton['tag'] === $child['tag']) { |
|
298 | + if ($map[ 'tag' ] === ASN1::TYPE_CHOICE) { |
|
299 | + foreach ($map[ 'children' ] as $child) { |
|
300 | + if ($skeleton[ 'tag' ] === $child[ 'tag' ]) { |
|
302 | 301 | $map = $child; |
303 | - if (isset($child['value']) && $child['value']) { |
|
304 | - return $child['value']; |
|
302 | + if (isset($child[ 'value' ]) && $child[ 'value' ]) { |
|
303 | + return $child[ 'value' ]; |
|
305 | 304 | } |
306 | 305 | break; |
307 | 306 | } |
308 | 307 | } |
309 | 308 | } |
310 | - if (in_array($map['tag'], [ASN1::TYPE_SEQUENCE, ASN1::TYPE_SET]) && |
|
311 | - in_array($skeleton['tag'], [ASN1::TYPE_SEQUENCE, ASN1::TYPE_SET])) { |
|
312 | - $map['tag'] = $skeleton['tag']; |
|
309 | + if (in_array($map[ 'tag' ], [ ASN1::TYPE_SEQUENCE, ASN1::TYPE_SET ]) && |
|
310 | + in_array($skeleton[ 'tag' ], [ ASN1::TYPE_SEQUENCE, ASN1::TYPE_SET ])) { |
|
311 | + $map[ 'tag' ] = $skeleton[ 'tag' ]; |
|
313 | 312 | } |
314 | - if ($map['tag'] === ASN1::TYPE_ANY && isset($skeleton['tag'])) { |
|
315 | - $map['tag'] = $skeleton['tag']; |
|
313 | + if ($map[ 'tag' ] === ASN1::TYPE_ANY && isset($skeleton[ 'tag' ])) { |
|
314 | + $map[ 'tag' ] = $skeleton[ 'tag' ]; |
|
316 | 315 | } |
317 | - if (!in_array($map['tag'], [ASN1::TYPE_ANY, ASN1::TYPE_ANY_RAW, ASN1::TYPE_ANY_SKIP, ASN1::TYPE_ANY_DER]) && |
|
318 | - $map['tag'] !== $skeleton['tag'] |
|
316 | + if (!in_array($map[ 'tag' ], [ ASN1::TYPE_ANY, ASN1::TYPE_ANY_RAW, ASN1::TYPE_ANY_SKIP, ASN1::TYPE_ANY_DER ]) && |
|
317 | + $map[ 'tag' ] !== $skeleton[ 'tag' ] |
|
319 | 318 | ) { |
320 | - if (!isset($map['optional']) || !$map['optional']) { |
|
321 | - throw new ASN1Exception('Decoded data does not match mapping - ' . $skeleton['tag']); |
|
319 | + if (!isset($map[ 'optional' ]) || !$map[ 'optional' ]) { |
|
320 | + throw new ASN1Exception('Decoded data does not match mapping - '.$skeleton[ 'tag' ]); |
|
322 | 321 | } |
323 | 322 | return null; |
324 | 323 | } else { |
325 | - switch ($map['tag']) { |
|
324 | + switch ($map[ 'tag' ]) { |
|
326 | 325 | case ASN1::TYPE_ANY_DER: |
327 | - return $this->reader->chunk($skeleton['start'], $skeleton['length']); |
|
326 | + return $this->reader->chunk($skeleton[ 'start' ], $skeleton[ 'length' ]); |
|
328 | 327 | case ASN1::TYPE_ANY_SKIP: |
329 | 328 | return null; |
330 | 329 | case ASN1::TYPE_ANY_RAW: |
331 | - return $skeleton['value'] ?? null; |
|
330 | + return $skeleton[ 'value' ] ?? null; |
|
332 | 331 | case ASN1::TYPE_SET: |
333 | - if (isset($map['repeat'])) { |
|
334 | - $result = []; |
|
335 | - foreach ($skeleton['children'] as $v) { |
|
336 | - $result[] = $this->map($map['repeat'], $v); |
|
332 | + if (isset($map[ 'repeat' ])) { |
|
333 | + $result = [ ]; |
|
334 | + foreach ($skeleton[ 'children' ] as $v) { |
|
335 | + $result[ ] = $this->map($map[ 'repeat' ], $v); |
|
337 | 336 | } |
338 | 337 | return $result; |
339 | 338 | } else { |
340 | - if (!isset($map['children'])) { |
|
339 | + if (!isset($map[ 'children' ])) { |
|
341 | 340 | return null; |
342 | 341 | } |
343 | - $temp = $skeleton['children']; |
|
344 | - $result = []; |
|
342 | + $temp = $skeleton[ 'children' ]; |
|
343 | + $result = [ ]; |
|
345 | 344 | // named first |
346 | - foreach ($map['children'] as $k => $v) { |
|
347 | - if (isset($v['name'])) { |
|
348 | - $result[$k] = null; |
|
345 | + foreach ($map[ 'children' ] as $k => $v) { |
|
346 | + if (isset($v[ 'name' ])) { |
|
347 | + $result[ $k ] = null; |
|
349 | 348 | foreach ($temp as $kk => $vv) { |
350 | - if ($vv['class'] !== ASN1::CLASS_UNIVERSAL && (int)$v['name'] === $vv['tag']) { |
|
349 | + if ($vv[ 'class' ] !== ASN1::CLASS_UNIVERSAL && (int) $v[ 'name' ] === $vv[ 'tag' ]) { |
|
351 | 350 | try { |
352 | - if (isset($v['implicit']) && $v['implicit']) { |
|
353 | - $vv['class'] = ASN1::CLASS_UNIVERSAL; |
|
354 | - $vv['tag'] = $map['tag']; |
|
351 | + if (isset($v[ 'implicit' ]) && $v[ 'implicit' ]) { |
|
352 | + $vv[ 'class' ] = ASN1::CLASS_UNIVERSAL; |
|
353 | + $vv[ 'tag' ] = $map[ 'tag' ]; |
|
355 | 354 | } else { |
356 | - $vv = $vv['children'][0] ?? null; |
|
355 | + $vv = $vv[ 'children' ][ 0 ] ?? null; |
|
357 | 356 | } |
358 | - $result[$k] = $this->map($v, $vv); |
|
359 | - unset($temp[$kk]); |
|
357 | + $result[ $k ] = $this->map($v, $vv); |
|
358 | + unset($temp[ $kk ]); |
|
360 | 359 | break; |
361 | 360 | } catch (ASN1Exception $e) { |
362 | 361 | // continue trying other children in case of failure |
363 | 362 | } |
364 | 363 | } |
365 | 364 | } |
366 | - if ($result[$k] === null && (!isset($v['optional']) || !$v['optional'])) { |
|
367 | - throw new ASN1Exception('Missing tagged type - ' . $k); |
|
365 | + if ($result[ $k ] === null && (!isset($v[ 'optional' ]) || !$v[ 'optional' ])) { |
|
366 | + throw new ASN1Exception('Missing tagged type - '.$k); |
|
368 | 367 | } |
369 | 368 | } |
370 | 369 | } |
371 | - foreach ($map['children'] as $k => $v) { |
|
372 | - if (isset($v['name'])) { |
|
370 | + foreach ($map[ 'children' ] as $k => $v) { |
|
371 | + if (isset($v[ 'name' ])) { |
|
373 | 372 | continue; |
374 | 373 | } |
375 | - $result[$k] = null; |
|
374 | + $result[ $k ] = null; |
|
376 | 375 | foreach ($temp as $kk => $vv) { |
377 | - if ($v['tag'] === $vv['tag'] || |
|
376 | + if ($v[ 'tag' ] === $vv[ 'tag' ] || |
|
378 | 377 | in_array( |
379 | - $v['tag'], |
|
378 | + $v[ 'tag' ], |
|
380 | 379 | [ |
381 | 380 | ASN1::TYPE_ANY, |
382 | 381 | ASN1::TYPE_ANY_DER, |
@@ -387,53 +386,53 @@ discard block |
||
387 | 386 | ) |
388 | 387 | ) { |
389 | 388 | try { |
390 | - $result[$k] = $this->map($v, $vv); |
|
391 | - unset($temp[$kk]); |
|
389 | + $result[ $k ] = $this->map($v, $vv); |
|
390 | + unset($temp[ $kk ]); |
|
392 | 391 | break; |
393 | 392 | } catch (ASN1Exception $e) { |
394 | - $result[$k] = null; |
|
393 | + $result[ $k ] = null; |
|
395 | 394 | } |
396 | 395 | } |
397 | 396 | } |
398 | - if ($result[$k] === null && (!isset($v['optional']) || !$v['optional'])) { |
|
399 | - throw new ASN1Exception('Decoded data does not match mapping - ' . $k); |
|
397 | + if ($result[ $k ] === null && (!isset($v[ 'optional' ]) || !$v[ 'optional' ])) { |
|
398 | + throw new ASN1Exception('Decoded data does not match mapping - '.$k); |
|
400 | 399 | } |
401 | 400 | } |
402 | 401 | return $result; |
403 | 402 | } |
404 | 403 | break; |
405 | 404 | case ASN1::TYPE_SEQUENCE: |
406 | - if (isset($map['repeat'])) { |
|
407 | - $result = []; |
|
408 | - foreach ($skeleton['children'] as $v) { |
|
409 | - $result[] = $this->map($map['repeat'], $v); |
|
405 | + if (isset($map[ 'repeat' ])) { |
|
406 | + $result = [ ]; |
|
407 | + foreach ($skeleton[ 'children' ] as $v) { |
|
408 | + $result[ ] = $this->map($map[ 'repeat' ], $v); |
|
410 | 409 | } |
411 | 410 | return $result; |
412 | 411 | } else { |
413 | - if (!isset($map['children'])) { |
|
412 | + if (!isset($map[ 'children' ])) { |
|
414 | 413 | return null; |
415 | 414 | } |
416 | - $result = []; |
|
417 | - foreach ($skeleton['children'] as $vv) { |
|
418 | - foreach ($map['children'] as $k => $v) { |
|
419 | - if (isset($v['name']) && $vv['class'] !== ASN1::CLASS_UNIVERSAL && |
|
420 | - (int)$v['name'] === $vv['tag'] |
|
415 | + $result = [ ]; |
|
416 | + foreach ($skeleton[ 'children' ] as $vv) { |
|
417 | + foreach ($map[ 'children' ] as $k => $v) { |
|
418 | + if (isset($v[ 'name' ]) && $vv[ 'class' ] !== ASN1::CLASS_UNIVERSAL && |
|
419 | + (int) $v[ 'name' ] === $vv[ 'tag' ] |
|
421 | 420 | ) { |
422 | - if (isset($v['implicit']) && $v['implicit']) { |
|
423 | - $vv['class'] = ASN1::CLASS_UNIVERSAL; |
|
424 | - $vv['tag'] = $map['tag']; |
|
421 | + if (isset($v[ 'implicit' ]) && $v[ 'implicit' ]) { |
|
422 | + $vv[ 'class' ] = ASN1::CLASS_UNIVERSAL; |
|
423 | + $vv[ 'tag' ] = $map[ 'tag' ]; |
|
425 | 424 | } else { |
426 | - $vv = $vv['children'][0] ?? null; |
|
425 | + $vv = $vv[ 'children' ][ 0 ] ?? null; |
|
427 | 426 | } |
428 | - $result[$k] = $this->map($v, $vv); |
|
429 | - unset($map['children'][$k]); |
|
427 | + $result[ $k ] = $this->map($v, $vv); |
|
428 | + unset($map[ 'children' ][ $k ]); |
|
430 | 429 | break; |
431 | 430 | } |
432 | - if (!isset($v['name']) && |
|
431 | + if (!isset($v[ 'name' ]) && |
|
433 | 432 | ( |
434 | - $v['tag'] === $vv['tag'] || |
|
433 | + $v[ 'tag' ] === $vv[ 'tag' ] || |
|
435 | 434 | in_array( |
436 | - $v['tag'], |
|
435 | + $v[ 'tag' ], |
|
437 | 436 | [ |
438 | 437 | ASN1::TYPE_ANY, |
439 | 438 | ASN1::TYPE_ANY_DER, |
@@ -446,18 +445,18 @@ discard block |
||
446 | 445 | ) { |
447 | 446 | try { |
448 | 447 | $temp = $this->map($v, $vv); |
449 | - $result[$k] = $temp; |
|
450 | - unset($map['children'][$k]); |
|
448 | + $result[ $k ] = $temp; |
|
449 | + unset($map[ 'children' ][ $k ]); |
|
451 | 450 | break; |
452 | 451 | } catch (ASN1Exception $e) { |
453 | 452 | // continue trying other children in case of failure |
454 | 453 | } |
455 | 454 | } |
456 | - if (!isset($v['optional']) || !$v['optional']) { |
|
457 | - throw new ASN1Exception('Missing type - ' . $k); |
|
455 | + if (!isset($v[ 'optional' ]) || !$v[ 'optional' ]) { |
|
456 | + throw new ASN1Exception('Missing type - '.$k); |
|
458 | 457 | } else { |
459 | - $result[$k] = null; |
|
460 | - unset($map['children'][$k]); |
|
458 | + $result[ $k ] = null; |
|
459 | + unset($map[ 'children' ][ $k ]); |
|
461 | 460 | } |
462 | 461 | } |
463 | 462 | } |
@@ -465,49 +464,47 @@ discard block |
||
465 | 464 | } |
466 | 465 | break; |
467 | 466 | case ASN1::TYPE_OBJECT_IDENTIFIER: |
468 | - return isset($map['resolve']) && $map['resolve'] ? |
|
469 | - ASN1::OIDtoText($skeleton['value']) : |
|
470 | - $skeleton['value']; |
|
467 | + return isset($map[ 'resolve' ]) && $map[ 'resolve' ] ? |
|
468 | + ASN1::OIDtoText($skeleton[ 'value' ]) : $skeleton[ 'value' ]; |
|
471 | 469 | case ASN1::TYPE_OCTET_STRING: |
472 | - if (isset($map['der']) && $map['der']) { |
|
473 | - $temp = static::fromString($skeleton['value']); |
|
474 | - return isset($map['map']) ? $temp->map($map['map']) : $temp->values(); |
|
470 | + if (isset($map[ 'der' ]) && $map[ 'der' ]) { |
|
471 | + $temp = static::fromString($skeleton[ 'value' ]); |
|
472 | + return isset($map[ 'map' ]) ? $temp->map($map[ 'map' ]) : $temp->values(); |
|
475 | 473 | } else { |
476 | - return isset($map['raw']) && $map['raw'] ? |
|
477 | - $skeleton['value'] : |
|
478 | - base64_encode($skeleton['value']); |
|
474 | + return isset($map[ 'raw' ]) && $map[ 'raw' ] ? |
|
475 | + $skeleton[ 'value' ] : base64_encode($skeleton[ 'value' ]); |
|
479 | 476 | } |
480 | 477 | break; |
481 | 478 | case ASN1::TYPE_INTEGER: |
482 | - $base = isset($map['base']) && (int)$map['base'] ? (int)$map['base'] : 10; |
|
479 | + $base = isset($map[ 'base' ]) && (int) $map[ 'base' ] ? (int) $map[ 'base' ] : 10; |
|
483 | 480 | if ($base < 3) { |
484 | - $result = $skeleton['value']; |
|
481 | + $result = $skeleton[ 'value' ]; |
|
485 | 482 | } else { |
486 | - if (strlen($skeleton['value']) > 53 && $base === 16) { |
|
483 | + if (strlen($skeleton[ 'value' ]) > 53 && $base === 16) { |
|
487 | 484 | $hex = ''; |
488 | - for ($i = strlen($skeleton['value']) - 4; $i >= 0; $i-=4) { |
|
489 | - $hex .= dechex((int)bindec(substr($skeleton['value'], $i, 4))); |
|
485 | + for ($i = strlen($skeleton[ 'value' ]) - 4; $i >= 0; $i -= 4) { |
|
486 | + $hex .= dechex((int) bindec(substr($skeleton[ 'value' ], $i, 4))); |
|
490 | 487 | } |
491 | 488 | $result = strrev($hex); |
492 | 489 | } else { |
493 | - $temp = base_convert($skeleton['value'], 2, $base); |
|
490 | + $temp = base_convert($skeleton[ 'value' ], 2, $base); |
|
494 | 491 | if ($base === 10) { |
495 | - $temp = (int)$temp; |
|
492 | + $temp = (int) $temp; |
|
496 | 493 | } |
497 | 494 | $result = $temp; |
498 | 495 | } |
499 | 496 | } |
500 | - if (isset($map['map']) && isset($map['map'][$result])) { |
|
501 | - $result = $map['map'][$result]; |
|
497 | + if (isset($map[ 'map' ]) && isset($map[ 'map' ][ $result ])) { |
|
498 | + $result = $map[ 'map' ][ $result ]; |
|
502 | 499 | } |
503 | 500 | return $result; |
504 | 501 | case ASN1::TYPE_UTC_TIME: |
505 | 502 | case ASN1::TYPE_GENERALIZED_TIME: |
506 | - return $skeleton['value']; |
|
503 | + return $skeleton[ 'value' ]; |
|
507 | 504 | default: |
508 | - $result = $skeleton['value']; |
|
509 | - if (isset($map['map']) && isset($map['map'][$result])) { |
|
510 | - $result = $map['map'][$result]; |
|
505 | + $result = $skeleton[ 'value' ]; |
|
506 | + if (isset($map[ 'map' ]) && isset($map[ 'map' ][ $result ])) { |
|
507 | + $result = $map[ 'map' ][ $result ]; |
|
511 | 508 | } |
512 | 509 | return $result; |
513 | 510 | } |
@@ -18,18 +18,18 @@ discard block |
||
18 | 18 | */ |
19 | 19 | public static function encode($source, $mapping) |
20 | 20 | { |
21 | - if (isset($mapping['default']) && $source === $mapping['default']) { |
|
21 | + if (isset($mapping[ 'default' ]) && $source === $mapping[ 'default' ]) { |
|
22 | 22 | return ''; |
23 | 23 | } |
24 | 24 | |
25 | - $tag = $mapping['tag']; |
|
25 | + $tag = $mapping[ 'tag' ]; |
|
26 | 26 | switch ($tag) { |
27 | 27 | case ASN1::TYPE_SET: |
28 | 28 | case ASN1::TYPE_SEQUENCE: |
29 | 29 | $tag |= 0x20; // set the constructed bit |
30 | 30 | $value = ''; |
31 | - if (isset($mapping['min']) && isset($mapping['max'])) { |
|
32 | - $child = $mapping['children']; |
|
31 | + if (isset($mapping[ 'min' ]) && isset($mapping[ 'max' ])) { |
|
32 | + $child = $mapping[ 'children' ]; |
|
33 | 33 | foreach ($source as $content) { |
34 | 34 | $temp = static::encode($content, $child); |
35 | 35 | if ($temp === false) { |
@@ -39,23 +39,23 @@ discard block |
||
39 | 39 | } |
40 | 40 | break; |
41 | 41 | } |
42 | - if (isset($mapping['repeat'])) { |
|
42 | + if (isset($mapping[ 'repeat' ])) { |
|
43 | 43 | foreach ($source as $content) { |
44 | - $temp = static::encode($content, $mapping['repeat']); |
|
44 | + $temp = static::encode($content, $mapping[ 'repeat' ]); |
|
45 | 45 | if ($temp === false) { |
46 | 46 | return false; |
47 | 47 | } |
48 | 48 | $value .= $temp; |
49 | 49 | } |
50 | 50 | } else { |
51 | - foreach ($mapping['children'] as $key => $child) { |
|
51 | + foreach ($mapping[ 'children' ] as $key => $child) { |
|
52 | 52 | if (!array_key_exists($key, $source)) { |
53 | - if (!isset($child['optional'])) { |
|
53 | + if (!isset($child[ 'optional' ])) { |
|
54 | 54 | return false; |
55 | 55 | } |
56 | 56 | continue; |
57 | 57 | } |
58 | - $temp = static::encode($source[$key], $child); |
|
58 | + $temp = static::encode($source[ $key ], $child); |
|
59 | 59 | if ($temp === false) { |
60 | 60 | return false; |
61 | 61 | } |
@@ -68,11 +68,11 @@ discard block |
||
68 | 68 | break; |
69 | 69 | case ASN1::TYPE_CHOICE: |
70 | 70 | $temp = false; |
71 | - foreach ($mapping['children'] as $key => $child) { |
|
72 | - if (!isset($source[$key])) { |
|
71 | + foreach ($mapping[ 'children' ] as $key => $child) { |
|
72 | + if (!isset($source[ $key ])) { |
|
73 | 73 | continue; |
74 | 74 | } |
75 | - $temp = static::encode($source[$key], $child); |
|
75 | + $temp = static::encode($source[ $key ], $child); |
|
76 | 76 | if ($temp === false) { |
77 | 77 | return false; |
78 | 78 | } |
@@ -83,17 +83,17 @@ discard block |
||
83 | 83 | return $temp; |
84 | 84 | case ASN1::TYPE_INTEGER: |
85 | 85 | case ASN1::TYPE_ENUMERATED: |
86 | - if (!isset($mapping['mapping']) && isset($mapping['base']) && $mapping['base'] === 16) { |
|
86 | + if (!isset($mapping[ 'mapping' ]) && isset($mapping[ 'base' ]) && $mapping[ 'base' ] === 16) { |
|
87 | 87 | $value = hex2bin($source); |
88 | 88 | } else { |
89 | - if (!isset($mapping['mapping'])) { |
|
90 | - $value = ASN1::toBase256($source, isset($mapping['base']) ? $mapping['base'] : 10); |
|
89 | + if (!isset($mapping[ 'mapping' ])) { |
|
90 | + $value = ASN1::toBase256($source, isset($mapping[ 'base' ]) ? $mapping[ 'base' ] : 10); |
|
91 | 91 | } else { |
92 | - $value = array_search($source, $mapping['mapping']); |
|
92 | + $value = array_search($source, $mapping[ 'mapping' ]); |
|
93 | 93 | if ($value === false) { |
94 | 94 | return false; |
95 | 95 | } |
96 | - $value = ASN1::toBase256($value, isset($mapping['base']) ? (int)$mapping['base'] : 10); |
|
96 | + $value = ASN1::toBase256($value, isset($mapping[ 'base' ]) ? (int) $mapping[ 'base' ] : 10); |
|
97 | 97 | } |
98 | 98 | } |
99 | 99 | if (!strlen($value)) { |
@@ -102,24 +102,24 @@ discard block |
||
102 | 102 | break; |
103 | 103 | case ASN1::TYPE_UTC_TIME: |
104 | 104 | case ASN1::TYPE_GENERALIZED_TIME: |
105 | - $format = $mapping['tag'] == ASN1::TYPE_UTC_TIME ? 'y' : 'Y'; |
|
106 | - $format.= 'mdHis'; |
|
107 | - $value = @gmdate($format, strtotime($source)) . 'Z'; |
|
105 | + $format = $mapping[ 'tag' ] == ASN1::TYPE_UTC_TIME ? 'y' : 'Y'; |
|
106 | + $format .= 'mdHis'; |
|
107 | + $value = @gmdate($format, strtotime($source)).'Z'; |
|
108 | 108 | break; |
109 | 109 | case ASN1::TYPE_BIT_STRING: |
110 | - if (isset($mapping['mapping'])) { |
|
111 | - $mcnt = count($mapping['mapping']); |
|
110 | + if (isset($mapping[ 'mapping' ])) { |
|
111 | + $mcnt = count($mapping[ 'mapping' ]); |
|
112 | 112 | $bits = array_fill(0, $mcnt, 0); |
113 | 113 | $size = 0; |
114 | 114 | for ($i = 0; $i < $mcnt; $i++) { |
115 | - if (in_array($mapping['mapping'][$i], $source)) { |
|
116 | - $bits[$i] = 1; |
|
115 | + if (in_array($mapping[ 'mapping' ][ $i ], $source)) { |
|
116 | + $bits[ $i ] = 1; |
|
117 | 117 | $size = $i; |
118 | 118 | } |
119 | 119 | } |
120 | 120 | |
121 | - if (isset($mapping['min']) && $mapping['min'] >= 1 && $size < $mapping['min']) { |
|
122 | - $size = $mapping['min'] - 1; |
|
121 | + if (isset($mapping[ 'min' ]) && $mapping[ 'min' ] >= 1 && $size < $mapping[ 'min' ]) { |
|
122 | + $size = $mapping[ 'min' ] - 1; |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | $offset = 8 - (($size + 1) & 7); |
@@ -128,23 +128,23 @@ discard block |
||
128 | 128 | $value = chr($offset); |
129 | 129 | |
130 | 130 | for ($i = $size + 1; $i < $mcnt; $i++) { |
131 | - unset($bits[$i]); |
|
131 | + unset($bits[ $i ]); |
|
132 | 132 | } |
133 | 133 | |
134 | 134 | $bits = implode('', array_pad($bits, $size + $offset + 1, 0)); |
135 | 135 | $bytes = explode(' ', rtrim(chunk_split($bits, 8, ' '))); |
136 | 136 | foreach ($bytes as $byte) { |
137 | - $value.= chr((int)bindec($byte)); |
|
137 | + $value .= chr((int) bindec($byte)); |
|
138 | 138 | } |
139 | 139 | |
140 | 140 | break; |
141 | 141 | } |
142 | 142 | // default to octet string if no mapping is present |
143 | 143 | case ASN1::TYPE_OCTET_STRING: |
144 | - $value = isset($mapping['raw']) && $mapping['raw'] ? $source : base64_decode($source); |
|
144 | + $value = isset($mapping[ 'raw' ]) && $mapping[ 'raw' ] ? $source : base64_decode($source); |
|
145 | 145 | break; |
146 | 146 | case ASN1::TYPE_OBJECT_IDENTIFIER: |
147 | - if (!isset($source) && $mapping['optional']) { |
|
147 | + if (!isset($source) && $mapping[ 'optional' ]) { |
|
148 | 148 | return; |
149 | 149 | } |
150 | 150 | $oid = preg_match('(^(\d+\.?)+$)', $source) ? $source : ASN1::TextToOID($source); |
@@ -152,20 +152,20 @@ discard block |
||
152 | 152 | throw new ASN1Exception('Invalid OID'); |
153 | 153 | } |
154 | 154 | $parts = explode('.', $oid); |
155 | - $value = chr(40 * $parts[0] + $parts[1]); |
|
155 | + $value = chr(40 * $parts[ 0 ] + $parts[ 1 ]); |
|
156 | 156 | $pcnt = count($parts); |
157 | 157 | for ($i = 2; $i < $pcnt; $i++) { |
158 | 158 | $temp = ''; |
159 | - if (!$parts[$i]) { |
|
159 | + if (!$parts[ $i ]) { |
|
160 | 160 | $temp = "\0"; |
161 | 161 | } else { |
162 | - while ($parts[$i]) { |
|
163 | - $temp = chr(0x80 | ($parts[$i] & 0x7F)) . $temp; |
|
164 | - $parts[$i] >>= 7; |
|
162 | + while ($parts[ $i ]) { |
|
163 | + $temp = chr(0x80 | ($parts[ $i ] & 0x7F)).$temp; |
|
164 | + $parts[ $i ] >>= 7; |
|
165 | 165 | } |
166 | - $temp[strlen($temp) - 1] = $temp[strlen($temp) - 1] & chr(0x7F); |
|
166 | + $temp[ strlen($temp) - 1 ] = $temp[ strlen($temp) - 1 ] & chr(0x7F); |
|
167 | 167 | } |
168 | - $value.= $temp; |
|
168 | + $value .= $temp; |
|
169 | 169 | } |
170 | 170 | break; |
171 | 171 | case ASN1::TYPE_ANY: |
@@ -206,18 +206,18 @@ discard block |
||
206 | 206 | } |
207 | 207 | |
208 | 208 | $length = static::length(strlen($value)); |
209 | - if (isset($mapping['name'])) { |
|
210 | - if (isset($mapping['implicit']) && $mapping['implicit']) { |
|
211 | - $tag = ((ASN1::CLASS_CONTEXT_SPECIFIC ?? 2) << 6) | (ord($value[0]) & 0x20) | $mapping['name']; |
|
212 | - return chr($tag) . $length . $value; |
|
209 | + if (isset($mapping[ 'name' ])) { |
|
210 | + if (isset($mapping[ 'implicit' ]) && $mapping[ 'implicit' ]) { |
|
211 | + $tag = ((ASN1::CLASS_CONTEXT_SPECIFIC ?? 2) << 6) | (ord($value[ 0 ]) & 0x20) | $mapping[ 'name' ]; |
|
212 | + return chr($tag).$length.$value; |
|
213 | 213 | } else { |
214 | - $value = chr($tag) . $length . $value; |
|
215 | - return chr(((ASN1::CLASS_CONTEXT_SPECIFIC ?? 2) << 6) | 0x20 | $mapping['name']) . |
|
216 | - static::length(strlen($value)) . |
|
214 | + $value = chr($tag).$length.$value; |
|
215 | + return chr(((ASN1::CLASS_CONTEXT_SPECIFIC ?? 2) << 6) | 0x20 | $mapping[ 'name' ]). |
|
216 | + static::length(strlen($value)). |
|
217 | 217 | $value; |
218 | 218 | } |
219 | 219 | } |
220 | - return chr($tag) . $length . $value; |
|
220 | + return chr($tag).$length.$value; |
|
221 | 221 | } |
222 | 222 | |
223 | 223 | protected static function length($length) |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | [ |
20 | 20 | 'reqCert' => [ |
21 | 21 | 'hashAlgorithm' => [ |
22 | - 'algorithm' => ASN1::$oids[strtolower($algorithm)] ?? ASN1::$oids['md5'] |
|
22 | + 'algorithm' => ASN1::$oids[ strtolower($algorithm) ] ?? ASN1::$oids[ 'md5' ] |
|
23 | 23 | ], |
24 | 24 | 'issuerNameHash' => $issuerNameHash, |
25 | 25 | 'issuerKeyHash' => $issuerKeyHash, |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | 'tag' => ASN1::TYPE_INTEGER, |
45 | 45 | 'name' => 0, |
46 | 46 | 'implicit' => false, |
47 | - 'map' => [1=>'v1'], |
|
47 | + 'map' => [ 1=>'v1' ], |
|
48 | 48 | 'optional' => true |
49 | 49 | ], |
50 | 50 | 'requestorName' => [ |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | ] |
83 | 83 | ] |
84 | 84 | ], |
85 | - 'extensions' => Common::extensions() + ['name' => 2, 'implicit' => false, 'optional' => true ] |
|
85 | + 'extensions' => Common::extensions() + [ 'name' => 2, 'implicit' => false, 'optional' => true ] |
|
86 | 86 | ] |
87 | 87 | ], |
88 | 88 | 'signature' => [ |
@@ -12,10 +12,10 @@ discard block |
||
12 | 12 | public function subject() |
13 | 13 | { |
14 | 14 | $map = static::map(); |
15 | - $map['children']['responseBytes']['children']['response']['map']['children']['tbsResponseData']['tag'] = |
|
15 | + $map[ 'children' ][ 'responseBytes' ][ 'children' ][ 'response' ][ 'map' ][ 'children' ][ 'tbsResponseData' ][ 'tag' ] = |
|
16 | 16 | ASN1::TYPE_ANY_DER; |
17 | 17 | $temp = $this->data->map($map); |
18 | - return $temp['responseBytes']['response']['tbsResponseData']; |
|
18 | + return $temp[ 'responseBytes' ][ 'response' ][ 'tbsResponseData' ]; |
|
19 | 19 | } |
20 | 20 | |
21 | 21 | public static function map() |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | 'tag' => ASN1::TYPE_INTEGER, |
58 | 58 | 'name' => 0, |
59 | 59 | 'implicit' => false, |
60 | - 'map' => [1=>'v1'], |
|
60 | + 'map' => [ 1=>'v1' ], |
|
61 | 61 | 'optional' => true |
62 | 62 | ], |
63 | 63 | 'responderID' => [ |