@@ -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 ASN1Exception('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,176 +269,176 @@ 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 | // continue trying other children in case of failure |
| 359 | 358 | } |
| 360 | 359 | } |
| 361 | 360 | } |
| 362 | - if ($result[$k] === null && (!isset($v['optional']) || !$v['optional'])) { |
|
| 363 | - throw new ASN1Exception('Missing tagged type - ' . $k); |
|
| 361 | + if ($result[ $k ] === null && (!isset($v[ 'optional' ]) || !$v[ 'optional' ])) { |
|
| 362 | + throw new ASN1Exception('Missing tagged type - '.$k); |
|
| 364 | 363 | } |
| 365 | 364 | } |
| 366 | 365 | } |
| 367 | - foreach ($map['children'] as $k => $v) { |
|
| 368 | - if (isset($v['name'])) { |
|
| 366 | + foreach ($map[ 'children' ] as $k => $v) { |
|
| 367 | + if (isset($v[ 'name' ])) { |
|
| 369 | 368 | continue; |
| 370 | 369 | } |
| 371 | - $result[$k] = null; |
|
| 370 | + $result[ $k ] = null; |
|
| 372 | 371 | foreach ($temp as $kk => $vv) { |
| 373 | - if ($v['tag'] === $vv['tag'] || |
|
| 372 | + if ($v[ 'tag' ] === $vv[ 'tag' ] || |
|
| 374 | 373 | in_array( |
| 375 | - $v['tag'], |
|
| 374 | + $v[ 'tag' ], |
|
| 376 | 375 | [ ASN1::TYPE_ANY, ASN1::TYPE_ANY_DER, ASN1::TYPE_ANY_RAW, ASN1::TYPE_ANY_SKIP, ASN1::TYPE_CHOICE ] |
| 377 | 376 | ) |
| 378 | 377 | ) { |
| 379 | 378 | try { |
| 380 | - $result[$k] = $this->map($v, $vv); |
|
| 381 | - unset($temp[$kk]); |
|
| 379 | + $result[ $k ] = $this->map($v, $vv); |
|
| 380 | + unset($temp[ $kk ]); |
|
| 382 | 381 | break; |
| 383 | 382 | } catch (ASN1Exception $e) { |
| 384 | - $result[$k] = null; |
|
| 383 | + $result[ $k ] = null; |
|
| 385 | 384 | } |
| 386 | 385 | } |
| 387 | 386 | } |
| 388 | - if ($result[$k] === null && (!isset($v['optional']) || !$v['optional'])) { |
|
| 389 | - throw new ASN1Exception('Decoded data does not match mapping - ' . $k); |
|
| 387 | + if ($result[ $k ] === null && (!isset($v[ 'optional' ]) || !$v[ 'optional' ])) { |
|
| 388 | + throw new ASN1Exception('Decoded data does not match mapping - '.$k); |
|
| 390 | 389 | } |
| 391 | 390 | } |
| 392 | 391 | return $result; |
| 393 | 392 | } |
| 394 | 393 | break; |
| 395 | 394 | case ASN1::TYPE_SEQUENCE: |
| 396 | - if (isset($map['repeat'])) { |
|
| 397 | - $result = []; |
|
| 398 | - foreach ($skeleton['children'] as $v) { |
|
| 399 | - $result[] = $this->map($map['repeat'], $v); |
|
| 395 | + if (isset($map[ 'repeat' ])) { |
|
| 396 | + $result = [ ]; |
|
| 397 | + foreach ($skeleton[ 'children' ] as $v) { |
|
| 398 | + $result[ ] = $this->map($map[ 'repeat' ], $v); |
|
| 400 | 399 | } |
| 401 | 400 | return $result; |
| 402 | 401 | } else { |
| 403 | - if (!isset($map['children'])) { |
|
| 402 | + if (!isset($map[ 'children' ])) { |
|
| 404 | 403 | return null; |
| 405 | 404 | } |
| 406 | - $result = []; |
|
| 407 | - foreach ($skeleton['children'] as $vv) { |
|
| 408 | - foreach ($map['children'] as $k => $v) { |
|
| 409 | - if (isset($v['name']) && $vv['class'] !== ASN1::CLASS_UNIVERSAL && (int)$v['name'] === $vv['tag']) { |
|
| 410 | - if (isset($v['implicit']) && $v['implicit']) { |
|
| 411 | - $vv['class'] = ASN1::CLASS_UNIVERSAL; |
|
| 412 | - $vv['tag'] = $map['tag']; |
|
| 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' ]; |
|
| 413 | 412 | } else { |
| 414 | - $vv = $vv['children'][0] ?? null; |
|
| 413 | + $vv = $vv[ 'children' ][ 0 ] ?? null; |
|
| 415 | 414 | } |
| 416 | - $result[$k] = $this->map($v, $vv); |
|
| 417 | - unset($map['children'][$k]); |
|
| 415 | + $result[ $k ] = $this->map($v, $vv); |
|
| 416 | + unset($map[ 'children' ][ $k ]); |
|
| 418 | 417 | break; |
| 419 | 418 | } |
| 420 | - if (!isset($v['name']) && |
|
| 419 | + if (!isset($v[ 'name' ]) && |
|
| 421 | 420 | ( |
| 422 | - $v['tag'] === $vv['tag'] || |
|
| 421 | + $v[ 'tag' ] === $vv[ 'tag' ] || |
|
| 423 | 422 | in_array( |
| 424 | - $v['tag'], |
|
| 423 | + $v[ 'tag' ], |
|
| 425 | 424 | [ ASN1::TYPE_ANY, ASN1::TYPE_ANY_DER, ASN1::TYPE_ANY_RAW, ASN1::TYPE_ANY_SKIP, ASN1::TYPE_CHOICE ] |
| 426 | 425 | ) |
| 427 | 426 | ) |
| 428 | 427 | ) { |
| 429 | 428 | try { |
| 430 | 429 | $temp = $this->map($v, $vv); |
| 431 | - $result[$k] = $temp; |
|
| 432 | - unset($map['children'][$k]); |
|
| 430 | + $result[ $k ] = $temp; |
|
| 431 | + unset($map[ 'children' ][ $k ]); |
|
| 433 | 432 | break; |
| 434 | 433 | } catch (ASN1Exception $e) { |
| 435 | 434 | // continue trying other children in case of failure |
| 436 | 435 | } |
| 437 | 436 | } |
| 438 | - if (!isset($v['optional']) || !$v['optional']) { |
|
| 439 | - throw new ASN1Exception('Missing type - ' . $k); |
|
| 437 | + if (!isset($v[ 'optional' ]) || !$v[ 'optional' ]) { |
|
| 438 | + throw new ASN1Exception('Missing type - '.$k); |
|
| 440 | 439 | } else { |
| 441 | - $result[$k] = null; |
|
| 442 | - unset($map['children'][$k]); |
|
| 440 | + $result[ $k ] = null; |
|
| 441 | + unset($map[ 'children' ][ $k ]); |
|
| 443 | 442 | } |
| 444 | 443 | } |
| 445 | 444 | } |
@@ -447,45 +446,45 @@ discard block |
||
| 447 | 446 | } |
| 448 | 447 | break; |
| 449 | 448 | case ASN1::TYPE_OBJECT_IDENTIFIER: |
| 450 | - return isset($map['resolve']) && $map['resolve'] ? ASN1::OIDtoText($skeleton['value']) : $skeleton['value']; |
|
| 449 | + return isset($map[ 'resolve' ]) && $map[ 'resolve' ] ? ASN1::OIDtoText($skeleton[ 'value' ]) : $skeleton[ 'value' ]; |
|
| 451 | 450 | case ASN1::TYPE_OCTET_STRING: |
| 452 | - if (isset($map['der']) && $map['der']) { |
|
| 453 | - $temp = static::fromString($skeleton['value']); |
|
| 454 | - return isset($map['map']) ? $temp->map($map['map']) : $temp->values(); |
|
| 451 | + if (isset($map[ 'der' ]) && $map[ 'der' ]) { |
|
| 452 | + $temp = static::fromString($skeleton[ 'value' ]); |
|
| 453 | + return isset($map[ 'map' ]) ? $temp->map($map[ 'map' ]) : $temp->values(); |
|
| 455 | 454 | } else { |
| 456 | - return isset($map['raw']) && $map['raw'] ? $skeleton['value'] : base64_encode($skeleton['value']); |
|
| 455 | + return isset($map[ 'raw' ]) && $map[ 'raw' ] ? $skeleton[ 'value' ] : base64_encode($skeleton[ 'value' ]); |
|
| 457 | 456 | } |
| 458 | 457 | break; |
| 459 | 458 | case ASN1::TYPE_INTEGER: |
| 460 | - $base = isset($map['base']) && (int)$map['base'] ? (int)$map['base'] : 10; |
|
| 459 | + $base = isset($map[ 'base' ]) && (int) $map[ 'base' ] ? (int) $map[ 'base' ] : 10; |
|
| 461 | 460 | if ($base < 3) { |
| 462 | - $result = $skeleton['value']; |
|
| 461 | + $result = $skeleton[ 'value' ]; |
|
| 463 | 462 | } else { |
| 464 | - if (strlen($skeleton['value']) > 53 && $base === 16) { |
|
| 463 | + if (strlen($skeleton[ 'value' ]) > 53 && $base === 16) { |
|
| 465 | 464 | $hex = ''; |
| 466 | - for ($i = strlen($skeleton['value']) - 4; $i >= 0; $i-=4) { |
|
| 467 | - $hex .= dechex((int)bindec(substr($skeleton['value'], $i, 4))); |
|
| 465 | + for ($i = strlen($skeleton[ 'value' ]) - 4; $i >= 0; $i -= 4) { |
|
| 466 | + $hex .= dechex((int) bindec(substr($skeleton[ 'value' ], $i, 4))); |
|
| 468 | 467 | } |
| 469 | 468 | $result = strrev($hex); |
| 470 | 469 | } else { |
| 471 | - $temp = base_convert($skeleton['value'], 2, $base); |
|
| 470 | + $temp = base_convert($skeleton[ 'value' ], 2, $base); |
|
| 472 | 471 | if ($base === 10) { |
| 473 | - $temp = (int)$temp; |
|
| 472 | + $temp = (int) $temp; |
|
| 474 | 473 | } |
| 475 | 474 | $result = $temp; |
| 476 | 475 | } |
| 477 | 476 | } |
| 478 | - if (isset($map['map']) && isset($map['map'][$result])) { |
|
| 479 | - $result = $map['map'][$result]; |
|
| 477 | + if (isset($map[ 'map' ]) && isset($map[ 'map' ][ $result ])) { |
|
| 478 | + $result = $map[ 'map' ][ $result ]; |
|
| 480 | 479 | } |
| 481 | 480 | return $result; |
| 482 | 481 | case ASN1::TYPE_UTC_TIME: |
| 483 | 482 | case ASN1::TYPE_GENERALIZED_TIME: |
| 484 | - return $skeleton['value']; |
|
| 483 | + return $skeleton[ 'value' ]; |
|
| 485 | 484 | default: |
| 486 | - $result = $skeleton['value']; |
|
| 487 | - if (isset($map['map']) && isset($map['map'][$result])) { |
|
| 488 | - $result = $map['map'][$result]; |
|
| 485 | + $result = $skeleton[ 'value' ]; |
|
| 486 | + if (isset($map[ 'map' ]) && isset($map[ 'map' ][ $result ])) { |
|
| 487 | + $result = $map[ 'map' ][ $result ]; |
|
| 489 | 488 | } |
| 490 | 489 | return $result; |
| 491 | 490 | } |
@@ -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((int)base_convert(substr($bin, $i, 8), 2, 10)) . $res; |
|
| 57 | + for ($i = ($len - 8); $i >= 0; $i -= 8) { |
|
| 58 | + $res = chr((int) 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 | } |