@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | */ |
34 | 34 | public static function generateFromData($data, $nonce = true, $requireCert = false, $alg = 'sha1', $policy = null) |
35 | 35 | { |
36 | - if (!in_array($alg, ['sha1', 'sha256', 'sha384', 'sha512', 'md5'])) { |
|
36 | + if (!in_array($alg, [ 'sha1', 'sha256', 'sha384', 'sha512', 'md5' ])) { |
|
37 | 37 | throw new TimestampException('Unsupported hash algorithm'); |
38 | 38 | } |
39 | 39 | $hash = hash($alg, $data, true); |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | */ |
69 | 69 | public static function generateFromHash($data, $nonce = true, $requireCert = false, $alg = 'sha1', $policy = null) |
70 | 70 | { |
71 | - if (!in_array($alg, ['sha1', 'sha256', 'sha384', 'sha512', 'md5'])) { |
|
71 | + if (!in_array($alg, [ 'sha1', 'sha256', 'sha384', 'sha512', 'md5' ])) { |
|
72 | 72 | throw new ASN1Exception('Unsupported hash algorithm'); |
73 | 73 | } |
74 | 74 | if ($nonce === true) { |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | 'children' => [ |
100 | 100 | 'version' => [ |
101 | 101 | 'tag' => ASN1::TYPE_INTEGER, |
102 | - 'map' => [1=>'v1','v2','v3'] |
|
102 | + 'map' => [ 1=>'v1', 'v2', 'v3' ] |
|
103 | 103 | ], |
104 | 104 | 'reqPolicy' => [ |
105 | 105 | 'tag' => ASN1::TYPE_OBJECT_IDENTIFIER, |
@@ -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,51 +190,51 @@ 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($header['length'] ? $header['start'] + $header['length'] - 1 : null); |
|
206 | - if ($header['length'] === null) { |
|
203 | + if ($header[ 'class' ] !== ASN1::CLASS_UNIVERSAL && $header[ 'constructed' ]) { |
|
204 | + $header[ 'children' ] = $this->structure($header[ 'length' ] ? $header[ 'start' ] + $header[ 'length' ] - 1 : null); |
|
205 | + if ($header[ 'length' ] === null) { |
|
207 | 206 | $this->reader->byte(); |
208 | - $header['length'] = $this->reader->pos() - $header['start']; |
|
209 | - $header['content_length'] = $this->reader->pos() - $header['content_start']; |
|
207 | + $header[ 'length' ] = $this->reader->pos() - $header[ 'start' ]; |
|
208 | + $header[ 'content_length' ] = $this->reader->pos() - $header[ 'content_start' ]; |
|
210 | 209 | } |
211 | - $skeleton[] = $header; |
|
210 | + $skeleton[ ] = $header; |
|
212 | 211 | } else { |
213 | - if ($header['class'] === ASN1::CLASS_UNIVERSAL && |
|
214 | - in_array($header['tag'], [ASN1::TYPE_SET, ASN1::TYPE_SEQUENCE]) |
|
212 | + if ($header[ 'class' ] === ASN1::CLASS_UNIVERSAL && |
|
213 | + in_array($header[ 'tag' ], [ ASN1::TYPE_SET, ASN1::TYPE_SEQUENCE ]) |
|
215 | 214 | ) { |
216 | - $header['children'] = $this->structure($header['length'] ? $header['start'] + $header['length'] - 1 : null); |
|
217 | - if ($header['length'] === null) { |
|
215 | + $header[ 'children' ] = $this->structure($header[ 'length' ] ? $header[ 'start' ] + $header[ 'length' ] - 1 : null); |
|
216 | + if ($header[ 'length' ] === null) { |
|
218 | 217 | $this->reader->byte(); |
219 | - $header['length'] = $this->reader->pos() - $header['start']; |
|
220 | - $header['content_length'] = $this->reader->pos() - $header['content_start']; |
|
218 | + $header[ 'length' ] = $this->reader->pos() - $header[ 'start' ]; |
|
219 | + $header[ 'content_length' ] = $this->reader->pos() - $header[ 'content_start' ]; |
|
221 | 220 | } |
222 | 221 | } else { |
223 | - if ($header['length'] === null) { |
|
222 | + if ($header[ 'length' ] === null) { |
|
224 | 223 | $this->reader->readUntil(chr(0).chr(0)); |
225 | - $header['length'] = $this->reader->pos() - $header['start']; |
|
226 | - $header['content_length'] = $this->reader->pos() - $header['content_start']; |
|
224 | + $header[ 'length' ] = $this->reader->pos() - $header[ 'start' ]; |
|
225 | + $header[ 'content_length' ] = $this->reader->pos() - $header[ 'content_start' ]; |
|
227 | 226 | } else { |
228 | - if ($header['content_length'] > 0) { |
|
229 | - $this->reader->bytes($header['content_length']); |
|
227 | + if ($header[ 'content_length' ] > 0) { |
|
228 | + $this->reader->bytes($header[ 'content_length' ]); |
|
230 | 229 | } |
231 | 230 | } |
232 | 231 | } |
233 | - if (!isset($header['children'])) { |
|
232 | + if (!isset($header[ 'children' ])) { |
|
234 | 233 | $pos = $this->reader->pos(); |
235 | - $header['value'] = $this->decode($header); |
|
234 | + $header[ 'value' ] = $this->decode($header); |
|
236 | 235 | $this->reader->seek($pos); |
237 | 236 | } |
238 | - $skeleton[] = $header; |
|
237 | + $skeleton[ ] = $header; |
|
239 | 238 | } |
240 | 239 | } |
241 | 240 | return $skeleton; |
@@ -250,10 +249,10 @@ discard block |
||
250 | 249 | { |
251 | 250 | $skeleton = $skeleton ?? $this->structure(); |
252 | 251 | foreach ($skeleton as $k => $v) { |
253 | - if (isset($v['children'])) { |
|
254 | - $skeleton[$k] = $this->values($v['children']); |
|
252 | + if (isset($v[ 'children' ])) { |
|
253 | + $skeleton[ $k ] = $this->values($v[ 'children' ]); |
|
255 | 254 | } else { |
256 | - $skeleton[$k] = $v['value'] ?? null; |
|
255 | + $skeleton[ $k ] = $v[ 'value' ] ?? null; |
|
257 | 256 | } |
258 | 257 | } |
259 | 258 | return $skeleton; |
@@ -270,174 +269,174 @@ discard block |
||
270 | 269 | if ($skeleton === null && $this->reader->pos() !== 0) { |
271 | 270 | $this->reader->rewind(); |
272 | 271 | } |
273 | - $skeleton = $skeleton ?? $this->structure()[0]; |
|
274 | - if ($skeleton['class'] !== ASN1::CLASS_UNIVERSAL) { |
|
275 | - if ($map['tag'] === ASN1::TYPE_CHOICE) { |
|
276 | - foreach ($map['children'] as $child) { |
|
277 | - if (isset($child['name']) && (int)$skeleton['tag'] === (int)$child['name']) { |
|
272 | + $skeleton = $skeleton ?? $this->structure()[ 0 ]; |
|
273 | + if ($skeleton[ 'class' ] !== ASN1::CLASS_UNIVERSAL) { |
|
274 | + if ($map[ 'tag' ] === ASN1::TYPE_CHOICE) { |
|
275 | + foreach ($map[ 'children' ] as $child) { |
|
276 | + if (isset($child[ 'name' ]) && (int) $skeleton[ 'tag' ] === (int) $child[ 'name' ]) { |
|
278 | 277 | $map = $child; |
279 | - if (isset($child['value']) && $child['value']) { |
|
280 | - return $child['value']; |
|
278 | + if (isset($child[ 'value' ]) && $child[ 'value' ]) { |
|
279 | + return $child[ 'value' ]; |
|
281 | 280 | } |
282 | 281 | break; |
283 | 282 | } |
284 | 283 | } |
285 | 284 | } |
286 | 285 | } |
287 | - if ($skeleton['class'] !== ASN1::CLASS_UNIVERSAL) { |
|
288 | - if (isset($map['implicit']) && $map['implicit']) { |
|
289 | - $skeleton['class'] = ASN1::CLASS_UNIVERSAL; |
|
290 | - $skeleton['tag'] = $map['tag']; |
|
286 | + if ($skeleton[ 'class' ] !== ASN1::CLASS_UNIVERSAL) { |
|
287 | + if (isset($map[ 'implicit' ]) && $map[ 'implicit' ]) { |
|
288 | + $skeleton[ 'class' ] = ASN1::CLASS_UNIVERSAL; |
|
289 | + $skeleton[ 'tag' ] = $map[ 'tag' ]; |
|
291 | 290 | } else { |
292 | - $skeleton = $skeleton['children'][0] ?? null; |
|
291 | + $skeleton = $skeleton[ 'children' ][ 0 ] ?? null; |
|
293 | 292 | } |
294 | 293 | } |
295 | - if ($map['tag'] === ASN1::TYPE_CHOICE) { |
|
296 | - foreach ($map['children'] as $child) { |
|
297 | - if ($skeleton['tag'] === $child['tag']) { |
|
294 | + if ($map[ 'tag' ] === ASN1::TYPE_CHOICE) { |
|
295 | + foreach ($map[ 'children' ] as $child) { |
|
296 | + if ($skeleton[ 'tag' ] === $child[ 'tag' ]) { |
|
298 | 297 | $map = $child; |
299 | - if (isset($child['value']) && $child['value']) { |
|
300 | - return $child['value']; |
|
298 | + if (isset($child[ 'value' ]) && $child[ 'value' ]) { |
|
299 | + return $child[ 'value' ]; |
|
301 | 300 | } |
302 | 301 | break; |
303 | 302 | } |
304 | 303 | } |
305 | 304 | } |
306 | - if (in_array($map['tag'], [ASN1::TYPE_SEQUENCE, ASN1::TYPE_SET]) && |
|
307 | - in_array($skeleton['tag'], [ASN1::TYPE_SEQUENCE, ASN1::TYPE_SET])) { |
|
308 | - $map['tag'] = $skeleton['tag']; |
|
305 | + if (in_array($map[ 'tag' ], [ ASN1::TYPE_SEQUENCE, ASN1::TYPE_SET ]) && |
|
306 | + in_array($skeleton[ 'tag' ], [ ASN1::TYPE_SEQUENCE, ASN1::TYPE_SET ])) { |
|
307 | + $map[ 'tag' ] = $skeleton[ 'tag' ]; |
|
309 | 308 | } |
310 | - if ($map['tag'] === ASN1::TYPE_ANY && isset($skeleton['tag'])) { |
|
311 | - $map['tag'] = $skeleton['tag']; |
|
309 | + if ($map[ 'tag' ] === ASN1::TYPE_ANY && isset($skeleton[ 'tag' ])) { |
|
310 | + $map[ 'tag' ] = $skeleton[ 'tag' ]; |
|
312 | 311 | } |
313 | - if (!in_array($map['tag'], [ASN1::TYPE_ANY, ASN1::TYPE_ANY_RAW, ASN1::TYPE_ANY_SKIP, ASN1::TYPE_ANY_DER]) && |
|
314 | - $map['tag'] !== $skeleton['tag'] |
|
312 | + if (!in_array($map[ 'tag' ], [ ASN1::TYPE_ANY, ASN1::TYPE_ANY_RAW, ASN1::TYPE_ANY_SKIP, ASN1::TYPE_ANY_DER ]) && |
|
313 | + $map[ 'tag' ] !== $skeleton[ 'tag' ] |
|
315 | 314 | ) { |
316 | - if (!isset($map['optional']) || !$map['optional']) { |
|
317 | - throw new ASN1Exception('Decoded data does not match mapping - ' . $skeleton['tag']); |
|
315 | + if (!isset($map[ 'optional' ]) || !$map[ 'optional' ]) { |
|
316 | + throw new ASN1Exception('Decoded data does not match mapping - '.$skeleton[ 'tag' ]); |
|
318 | 317 | } |
319 | 318 | return null; |
320 | 319 | } else { |
321 | - switch ($map['tag']) { |
|
320 | + switch ($map[ 'tag' ]) { |
|
322 | 321 | case ASN1::TYPE_ANY_DER: |
323 | - return $this->reader->chunk($skeleton['start'], $skeleton['length']); |
|
322 | + return $this->reader->chunk($skeleton[ 'start' ], $skeleton[ 'length' ]); |
|
324 | 323 | case ASN1::TYPE_ANY_SKIP: |
325 | 324 | return null; |
326 | 325 | case ASN1::TYPE_ANY_RAW: |
327 | - return $skeleton['value'] ?? null; |
|
326 | + return $skeleton[ 'value' ] ?? null; |
|
328 | 327 | case ASN1::TYPE_SET: |
329 | - if (isset($map['repeat'])) { |
|
330 | - $result = []; |
|
331 | - foreach ($skeleton['children'] as $v) { |
|
332 | - $result[] = $this->map($map['repeat'], $v); |
|
328 | + if (isset($map[ 'repeat' ])) { |
|
329 | + $result = [ ]; |
|
330 | + foreach ($skeleton[ 'children' ] as $v) { |
|
331 | + $result[ ] = $this->map($map[ 'repeat' ], $v); |
|
333 | 332 | } |
334 | 333 | return $result; |
335 | 334 | } else { |
336 | - if (!isset($map['children'])) { |
|
335 | + if (!isset($map[ 'children' ])) { |
|
337 | 336 | return null; |
338 | 337 | } |
339 | - $temp = $skeleton['children']; |
|
340 | - $result = []; |
|
338 | + $temp = $skeleton[ 'children' ]; |
|
339 | + $result = [ ]; |
|
341 | 340 | // named first |
342 | - foreach ($map['children'] as $k => $v) { |
|
343 | - if (isset($v['name'])) { |
|
344 | - $result[$k] = null; |
|
341 | + foreach ($map[ 'children' ] as $k => $v) { |
|
342 | + if (isset($v[ 'name' ])) { |
|
343 | + $result[ $k ] = null; |
|
345 | 344 | foreach ($temp as $kk => $vv) { |
346 | - if ($vv['class'] !== ASN1::CLASS_UNIVERSAL && (int)$v['name'] === $vv['tag']) { |
|
345 | + if ($vv[ 'class' ] !== ASN1::CLASS_UNIVERSAL && (int) $v[ 'name' ] === $vv[ 'tag' ]) { |
|
347 | 346 | try { |
348 | - if (isset($v['implicit']) && $v['implicit']) { |
|
349 | - $vv['class'] = ASN1::CLASS_UNIVERSAL; |
|
350 | - $vv['tag'] = $map['tag']; |
|
347 | + if (isset($v[ 'implicit' ]) && $v[ 'implicit' ]) { |
|
348 | + $vv[ 'class' ] = ASN1::CLASS_UNIVERSAL; |
|
349 | + $vv[ 'tag' ] = $map[ 'tag' ]; |
|
351 | 350 | } else { |
352 | - $vv = $vv['children'][0] ?? null; |
|
351 | + $vv = $vv[ 'children' ][ 0 ] ?? null; |
|
353 | 352 | } |
354 | - $result[$k] = $this->map($v, $vv); |
|
355 | - unset($temp[$kk]); |
|
353 | + $result[ $k ] = $this->map($v, $vv); |
|
354 | + unset($temp[ $kk ]); |
|
356 | 355 | break; |
357 | 356 | } catch (ASN1Exception $e) { |
358 | 357 | } |
359 | 358 | } |
360 | 359 | } |
361 | - if ($result[$k] === null && (!isset($v['optional']) || !$v['optional'])) { |
|
362 | - throw new ASN1Exception('Missing tagged type - ' . $k); |
|
360 | + if ($result[ $k ] === null && (!isset($v[ 'optional' ]) || !$v[ 'optional' ])) { |
|
361 | + throw new ASN1Exception('Missing tagged type - '.$k); |
|
363 | 362 | } |
364 | 363 | } |
365 | 364 | } |
366 | - foreach ($map['children'] as $k => $v) { |
|
367 | - if (isset($v['name'])) { |
|
365 | + foreach ($map[ 'children' ] as $k => $v) { |
|
366 | + if (isset($v[ 'name' ])) { |
|
368 | 367 | continue; |
369 | 368 | } |
370 | - $result[$k] = null; |
|
369 | + $result[ $k ] = null; |
|
371 | 370 | foreach ($temp as $kk => $vv) { |
372 | - if ($v['tag'] === $vv['tag'] || |
|
371 | + if ($v[ 'tag' ] === $vv[ 'tag' ] || |
|
373 | 372 | in_array( |
374 | - $v['tag'], |
|
373 | + $v[ 'tag' ], |
|
375 | 374 | [ ASN1::TYPE_ANY, ASN1::TYPE_ANY_DER, ASN1::TYPE_ANY_RAW, ASN1::TYPE_ANY_SKIP, ASN1::TYPE_CHOICE ] |
376 | 375 | ) |
377 | 376 | ) { |
378 | 377 | try { |
379 | - $result[$k] = $this->map($v, $vv); |
|
380 | - unset($temp[$kk]); |
|
378 | + $result[ $k ] = $this->map($v, $vv); |
|
379 | + unset($temp[ $kk ]); |
|
381 | 380 | break; |
382 | 381 | } catch (ASN1Exception $e) { |
383 | - $result[$k] = null; |
|
382 | + $result[ $k ] = null; |
|
384 | 383 | } |
385 | 384 | } |
386 | 385 | } |
387 | - if ($result[$k] === null && (!isset($v['optional']) || !$v['optional'])) { |
|
388 | - throw new ASN1Exception('Decoded data does not match mapping - ' . $k); |
|
386 | + if ($result[ $k ] === null && (!isset($v[ 'optional' ]) || !$v[ 'optional' ])) { |
|
387 | + throw new ASN1Exception('Decoded data does not match mapping - '.$k); |
|
389 | 388 | } |
390 | 389 | } |
391 | 390 | return $result; |
392 | 391 | } |
393 | 392 | break; |
394 | 393 | case ASN1::TYPE_SEQUENCE: |
395 | - if (isset($map['repeat'])) { |
|
396 | - $result = []; |
|
397 | - foreach ($skeleton['children'] as $v) { |
|
398 | - $result[] = $this->map($map['repeat'], $v); |
|
394 | + if (isset($map[ 'repeat' ])) { |
|
395 | + $result = [ ]; |
|
396 | + foreach ($skeleton[ 'children' ] as $v) { |
|
397 | + $result[ ] = $this->map($map[ 'repeat' ], $v); |
|
399 | 398 | } |
400 | 399 | return $result; |
401 | 400 | } else { |
402 | - if (!isset($map['children'])) { |
|
401 | + if (!isset($map[ 'children' ])) { |
|
403 | 402 | return null; |
404 | 403 | } |
405 | - $result = []; |
|
406 | - foreach ($skeleton['children'] as $vv) { |
|
407 | - foreach ($map['children'] as $k => $v) { |
|
408 | - if (isset($v['name']) && $vv['class'] !== ASN1::CLASS_UNIVERSAL && (int)$v['name'] === $vv['tag']) { |
|
409 | - if (isset($v['implicit']) && $v['implicit']) { |
|
410 | - $vv['class'] = ASN1::CLASS_UNIVERSAL; |
|
411 | - $vv['tag'] = $map['tag']; |
|
404 | + $result = [ ]; |
|
405 | + foreach ($skeleton[ 'children' ] as $vv) { |
|
406 | + foreach ($map[ 'children' ] as $k => $v) { |
|
407 | + if (isset($v[ 'name' ]) && $vv[ 'class' ] !== ASN1::CLASS_UNIVERSAL && (int) $v[ 'name' ] === $vv[ 'tag' ]) { |
|
408 | + if (isset($v[ 'implicit' ]) && $v[ 'implicit' ]) { |
|
409 | + $vv[ 'class' ] = ASN1::CLASS_UNIVERSAL; |
|
410 | + $vv[ 'tag' ] = $map[ 'tag' ]; |
|
412 | 411 | } else { |
413 | - $vv = $vv['children'][0] ?? null; |
|
412 | + $vv = $vv[ 'children' ][ 0 ] ?? null; |
|
414 | 413 | } |
415 | - $result[$k] = $this->map($v, $vv); |
|
416 | - unset($map['children'][$k]); |
|
414 | + $result[ $k ] = $this->map($v, $vv); |
|
415 | + unset($map[ 'children' ][ $k ]); |
|
417 | 416 | break; |
418 | 417 | } |
419 | - if (!isset($v['name']) && |
|
418 | + if (!isset($v[ 'name' ]) && |
|
420 | 419 | ( |
421 | - $v['tag'] === $vv['tag'] || |
|
420 | + $v[ 'tag' ] === $vv[ 'tag' ] || |
|
422 | 421 | in_array( |
423 | - $v['tag'], |
|
422 | + $v[ 'tag' ], |
|
424 | 423 | [ ASN1::TYPE_ANY, ASN1::TYPE_ANY_DER, ASN1::TYPE_ANY_RAW, ASN1::TYPE_ANY_SKIP, ASN1::TYPE_CHOICE ] |
425 | 424 | ) |
426 | 425 | ) |
427 | 426 | ) { |
428 | 427 | try { |
429 | 428 | $temp = $this->map($v, $vv); |
430 | - $result[$k] = $temp; |
|
431 | - unset($map['children'][$k]); |
|
429 | + $result[ $k ] = $temp; |
|
430 | + unset($map[ 'children' ][ $k ]); |
|
432 | 431 | break; |
433 | 432 | } catch (ASN1Exception $e) { |
434 | 433 | } |
435 | 434 | } |
436 | - if (!isset($v['optional']) || !$v['optional']) { |
|
437 | - throw new ASN1Exception('Missing type - ' . $k); |
|
435 | + if (!isset($v[ 'optional' ]) || !$v[ 'optional' ]) { |
|
436 | + throw new ASN1Exception('Missing type - '.$k); |
|
438 | 437 | } else { |
439 | - $result[$k] = null; |
|
440 | - unset($map['children'][$k]); |
|
438 | + $result[ $k ] = null; |
|
439 | + unset($map[ 'children' ][ $k ]); |
|
441 | 440 | } |
442 | 441 | } |
443 | 442 | } |
@@ -445,45 +444,45 @@ discard block |
||
445 | 444 | } |
446 | 445 | break; |
447 | 446 | case ASN1::TYPE_OBJECT_IDENTIFIER: |
448 | - return isset($map['resolve']) && $map['resolve'] ? ASN1::OIDtoText($skeleton['value']) : $skeleton['value']; |
|
447 | + return isset($map[ 'resolve' ]) && $map[ 'resolve' ] ? ASN1::OIDtoText($skeleton[ 'value' ]) : $skeleton[ 'value' ]; |
|
449 | 448 | case ASN1::TYPE_OCTET_STRING: |
450 | - if (isset($map['der']) && $map['der']) { |
|
451 | - $temp = static::fromString($skeleton['value']); |
|
452 | - return isset($map['map']) ? $temp->map($map['map']) : $temp->values(); |
|
449 | + if (isset($map[ 'der' ]) && $map[ 'der' ]) { |
|
450 | + $temp = static::fromString($skeleton[ 'value' ]); |
|
451 | + return isset($map[ 'map' ]) ? $temp->map($map[ 'map' ]) : $temp->values(); |
|
453 | 452 | } else { |
454 | - return isset($map['raw']) && $map['raw'] ? $skeleton['value'] : base64_encode($skeleton['value']); |
|
453 | + return isset($map[ 'raw' ]) && $map[ 'raw' ] ? $skeleton[ 'value' ] : base64_encode($skeleton[ 'value' ]); |
|
455 | 454 | } |
456 | 455 | break; |
457 | 456 | case ASN1::TYPE_INTEGER: |
458 | - $base = isset($map['base']) && (int)$map['base'] ? (int)$map['base'] : 10; |
|
457 | + $base = isset($map[ 'base' ]) && (int) $map[ 'base' ] ? (int) $map[ 'base' ] : 10; |
|
459 | 458 | if ($base < 3) { |
460 | - $result = $skeleton['value']; |
|
459 | + $result = $skeleton[ 'value' ]; |
|
461 | 460 | } else { |
462 | - if (strlen($skeleton['value']) > 53 && $base === 16) { |
|
461 | + if (strlen($skeleton[ 'value' ]) > 53 && $base === 16) { |
|
463 | 462 | $hex = ''; |
464 | - for ($i = strlen($skeleton['value']) - 4; $i >= 0; $i-=4) { |
|
465 | - $hex .= dechex((int)bindec(substr($skeleton['value'], $i, 4))); |
|
463 | + for ($i = strlen($skeleton[ 'value' ]) - 4; $i >= 0; $i -= 4) { |
|
464 | + $hex .= dechex((int) bindec(substr($skeleton[ 'value' ], $i, 4))); |
|
466 | 465 | } |
467 | 466 | $result = strrev($hex); |
468 | 467 | } else { |
469 | - $temp = base_convert($skeleton['value'], 2, $base); |
|
468 | + $temp = base_convert($skeleton[ 'value' ], 2, $base); |
|
470 | 469 | if ($base === 10) { |
471 | - $temp = (int)$temp; |
|
470 | + $temp = (int) $temp; |
|
472 | 471 | } |
473 | 472 | $result = $temp; |
474 | 473 | } |
475 | 474 | } |
476 | - if (isset($map['map']) && isset($map['map'][$result])) { |
|
477 | - $result = $map['map'][$result]; |
|
475 | + if (isset($map[ 'map' ]) && isset($map[ 'map' ][ $result ])) { |
|
476 | + $result = $map[ 'map' ][ $result ]; |
|
478 | 477 | } |
479 | 478 | return $result; |
480 | 479 | case ASN1::TYPE_UTC_TIME: |
481 | 480 | case ASN1::TYPE_GENERALIZED_TIME: |
482 | - return $skeleton['value']; |
|
481 | + return $skeleton[ 'value' ]; |
|
483 | 482 | default: |
484 | - $result = $skeleton['value']; |
|
485 | - if (isset($map['map']) && isset($map['map'][$result])) { |
|
486 | - $result = $map['map'][$result]; |
|
483 | + $result = $skeleton[ 'value' ]; |
|
484 | + if (isset($map[ 'map' ]) && isset($map[ 'map' ][ $result ])) { |
|
485 | + $result = $map[ 'map' ][ $result ]; |
|
487 | 486 | } |
488 | 487 | return $result; |
489 | 488 | } |
@@ -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,16 +206,16 @@ 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']) . static::length(strlen($value)) . $value; |
|
214 | + $value = chr($tag).$length.$value; |
|
215 | + return chr(((ASN1::CLASS_CONTEXT_SPECIFIC ?? 2) << 6) | 0x20 | $mapping[ 'name' ]).static::length(strlen($value)).$value; |
|
216 | 216 | } |
217 | 217 | } |
218 | - return chr($tag) . $length . $value; |
|
218 | + return chr($tag).$length.$value; |
|
219 | 219 | } |
220 | 220 | |
221 | 221 | protected static function length($length) |
@@ -52,10 +52,10 @@ discard block |
||
52 | 52 | { |
53 | 53 | $bin = base_convert($number, $base, 2); |
54 | 54 | $res = ""; |
55 | - $len = (int)ceil(strlen($bin) / 8) * 8; |
|
55 | + $len = (int) ceil(strlen($bin) / 8) * 8; |
|
56 | 56 | $bin = str_pad($bin, $len, "0", STR_PAD_LEFT); |
57 | - for ($i = ($len-8); $i >= 0; $i -= 8) { |
|
58 | - $res = chr(base_convert(substr($bin, $i, 8), 2, 10)) . $res; |
|
57 | + for ($i = ($len - 8); $i >= 0; $i -= 8) { |
|
58 | + $res = chr(base_convert(substr($bin, $i, 8), 2, 10)).$res; |
|
59 | 59 | } |
60 | 60 | return $res; |
61 | 61 | } |
@@ -311,6 +311,6 @@ discard block |
||
311 | 311 | } |
312 | 312 | public static function TextToOID($text) |
313 | 313 | { |
314 | - return static::$oids[$text] ?? $text; |
|
314 | + return static::$oids[ $text ] ?? $text; |
|
315 | 315 | } |
316 | 316 | } |