@@ -19,7 +19,7 @@ |
||
| 19 | 19 | 'children' => [ |
| 20 | 20 | 'version' => [ |
| 21 | 21 | 'tag' => ASN1::TYPE_INTEGER, |
| 22 | - 'map' => [1=>'v1','v2','v3'] |
|
| 22 | + 'map' => [ 1=>'v1', 'v2', 'v3' ] |
|
| 23 | 23 | ], |
| 24 | 24 | 'signature' => Common::AlgorithmIdentifier(), |
| 25 | 25 | 'issuer' => Common::RDNSequence(), |
@@ -21,7 +21,7 @@ |
||
| 21 | 21 | 'name' => 0, |
| 22 | 22 | 'implicit' => false, |
| 23 | 23 | 'tag' => ASN1::TYPE_INTEGER, |
| 24 | - 'map' => [1=>'v1','v2','v3'] |
|
| 24 | + 'map' => [ 1=>'v1', 'v2', 'v3' ] |
|
| 25 | 25 | ], |
| 26 | 26 | 'serialNumber' => [ |
| 27 | 27 | 'tag' => ASN1::TYPE_INTEGER, |
@@ -43,12 +43,12 @@ discard block |
||
| 43 | 43 | 'tag' => ASN1::TYPE_SEQUENCE, |
| 44 | 44 | 'optional' => true, |
| 45 | 45 | 'children' => [ |
| 46 | - 'contentType' => ['tag' => ASN1::TYPE_OBJECT_IDENTIFIER ], |
|
| 46 | + 'contentType' => [ 'tag' => ASN1::TYPE_OBJECT_IDENTIFIER ], |
|
| 47 | 47 | 'signedData' => [ |
| 48 | 48 | 'name' => 0, |
| 49 | 49 | 'tag' => ASN1::TYPE_SEQUENCE, |
| 50 | 50 | 'children' => [ |
| 51 | - 'version' => ['tag' => ASN1::TYPE_INTEGER ], |
|
| 51 | + 'version' => [ 'tag' => ASN1::TYPE_INTEGER ], |
|
| 52 | 52 | 'algorithms' => [ |
| 53 | 53 | 'tag' => ASN1::TYPE_SET, |
| 54 | 54 | 'children' => [ |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | 'tag' => ASN1::TYPE_SEQUENCE, |
| 60 | 60 | 'optional' => true, |
| 61 | 61 | 'children' => [ |
| 62 | - 'contentType' => ['tag' => ASN1::TYPE_OBJECT_IDENTIFIER ], |
|
| 62 | + 'contentType' => [ 'tag' => ASN1::TYPE_OBJECT_IDENTIFIER ], |
|
| 63 | 63 | 'data' => [ |
| 64 | 64 | 'name' => 0, |
| 65 | 65 | 'tag' => ASN1::TYPE_OCTET_STRING, |
@@ -80,8 +80,8 @@ discard block |
||
| 80 | 80 | return [ |
| 81 | 81 | 'tag' => ASN1::TYPE_SEQUENCE, |
| 82 | 82 | 'children' => [ |
| 83 | - 'version' => ['tag' => ASN1::TYPE_INTEGER, 'map' => [1 => 'v1','v2','v3'] ], |
|
| 84 | - 'policy' => ['tag' => ASN1::TYPE_OBJECT_IDENTIFIER, 'optional' => true ], |
|
| 83 | + 'version' => [ 'tag' => ASN1::TYPE_INTEGER, 'map' => [ 1 => 'v1', 'v2', 'v3' ] ], |
|
| 84 | + 'policy' => [ 'tag' => ASN1::TYPE_OBJECT_IDENTIFIER, 'optional' => true ], |
|
| 85 | 85 | 'messageImprint' => [ |
| 86 | 86 | 'tag' => ASN1::TYPE_SEQUENCE, |
| 87 | 87 | 'children' => [ |
@@ -92,15 +92,15 @@ discard block |
||
| 92 | 92 | ] |
| 93 | 93 | ] |
| 94 | 94 | ], |
| 95 | - 'serialNumber' => ['tag' => ASN1::TYPE_INTEGER, 'base' => 16, 'optional' => true ], // non-optional |
|
| 96 | - 'genTime' => ['tag' => ASN1::TYPE_GENERALIZED_TIME], // GeneralizedTime (non-optional] |
|
| 95 | + 'serialNumber' => [ 'tag' => ASN1::TYPE_INTEGER, 'base' => 16, 'optional' => true ], // non-optional |
|
| 96 | + 'genTime' => [ 'tag' => ASN1::TYPE_GENERALIZED_TIME ], // GeneralizedTime (non-optional] |
|
| 97 | 97 | 'accuracy' => [ |
| 98 | 98 | 'tag' => ASN1::TYPE_SEQUENCE, |
| 99 | 99 | 'optional' => true, |
| 100 | 100 | 'children' => [ |
| 101 | - 'seconds' => ['tag' => ASN1::TYPE_ANY, 'optional' => true ], |
|
| 102 | - 'millis' => ['tag' => ASN1::TYPE_ANY, 'optional' => true ], |
|
| 103 | - 'micros' => ['tag' => ASN1::TYPE_ANY, 'optional' => true ], |
|
| 101 | + 'seconds' => [ 'tag' => ASN1::TYPE_ANY, 'optional' => true ], |
|
| 102 | + 'millis' => [ 'tag' => ASN1::TYPE_ANY, 'optional' => true ], |
|
| 103 | + 'micros' => [ 'tag' => ASN1::TYPE_ANY, 'optional' => true ], |
|
| 104 | 104 | ] |
| 105 | 105 | ], |
| 106 | 106 | 'ordering' => [ |
@@ -111,8 +111,8 @@ discard block |
||
| 111 | 111 | 'tag' => ASN1::TYPE_INTEGER, |
| 112 | 112 | 'optional' => true |
| 113 | 113 | ], |
| 114 | - 'tsa' => ['tag' => ASN1::TYPE_ANY_DER, 'optional' => true, 'name' => 0], // [0] GeneralName |
|
| 115 | - 'extensions' => ['tag' => ASN1::TYPE_ANY, 'optional' => true, 'name' => 1] // [0] GeneralName |
|
| 114 | + 'tsa' => [ 'tag' => ASN1::TYPE_ANY_DER, 'optional' => true, 'name' => 0 ], // [0] GeneralName |
|
| 115 | + 'extensions' => [ 'tag' => ASN1::TYPE_ANY, 'optional' => true, 'name' => 1 ] // [0] GeneralName |
|
| 116 | 116 | ] |
| 117 | 117 | ]; |
| 118 | 118 | } |
@@ -65,10 +65,10 @@ |
||
| 65 | 65 | new \RecursiveArrayIterator($this->toArray()), |
| 66 | 66 | \RecursiveIteratorIterator::LEAVES_ONLY |
| 67 | 67 | ); |
| 68 | - $result = []; |
|
| 68 | + $result = [ ]; |
|
| 69 | 69 | foreach ($iterator as $k => $v) { |
| 70 | 70 | if (strlen($v)) { |
| 71 | - $result[] = str_repeat(' ', $iterator->getDepth()) . $v; |
|
| 71 | + $result[ ] = str_repeat(' ', $iterator->getDepth()).$v; |
|
| 72 | 72 | } |
| 73 | 73 | } |
| 74 | 74 | return implode("\r\n", $result); |
@@ -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, |
@@ -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) |
@@ -7,22 +7,22 @@ |
||
| 7 | 7 | protected $data; |
| 8 | 8 | protected $processor; |
| 9 | 9 | |
| 10 | - public function __construct(array &$data = [], callable $processor = null) |
|
| 10 | + public function __construct(array &$data = [ ], callable $processor = null) |
|
| 11 | 11 | { |
| 12 | 12 | $this->data = $data; |
| 13 | - $this->processor = $processor ?? function ($v) { return $v; }; |
|
| 13 | + $this->processor = $processor ?? function($v) { return $v; }; |
|
| 14 | 14 | } |
| 15 | 15 | public function __get($k) |
| 16 | 16 | { |
| 17 | - return $this[$k] ?? null; |
|
| 17 | + return $this[ $k ] ?? null; |
|
| 18 | 18 | } |
| 19 | 19 | public function offsetExists($offset) |
| 20 | 20 | { |
| 21 | - return isset($this->data[$offset]); |
|
| 21 | + return isset($this->data[ $offset ]); |
|
| 22 | 22 | } |
| 23 | 23 | public function offsetGet($offset) |
| 24 | 24 | { |
| 25 | - return call_user_func($this->processor, $this->data[$offset]); |
|
| 25 | + return call_user_func($this->processor, $this->data[ $offset ]); |
|
| 26 | 26 | } |
| 27 | 27 | public function offsetSet($offset, $value) |
| 28 | 28 | { |
@@ -10,42 +10,42 @@ discard block |
||
| 10 | 10 | { |
| 11 | 11 | public function lazyDecodeHeader(array $header) |
| 12 | 12 | { |
| 13 | - $this->reader->seek($header['content_start']); |
|
| 14 | - if ($header['class'] !== ASN1::CLASS_UNIVERSAL && $header['constructed']) { |
|
| 15 | - $header['children'] = $this->lazyParse( |
|
| 13 | + $this->reader->seek($header[ 'content_start' ]); |
|
| 14 | + if ($header[ 'class' ] !== ASN1::CLASS_UNIVERSAL && $header[ 'constructed' ]) { |
|
| 15 | + $header[ 'children' ] = $this->lazyParse( |
|
| 16 | 16 | null, |
| 17 | - $header['length'] ? $header['start'] + $header['length'] - 1 : null, |
|
| 17 | + $header[ 'length' ] ? $header[ 'start' ] + $header[ 'length' ] - 1 : null, |
|
| 18 | 18 | 'header' |
| 19 | 19 | ); |
| 20 | - } elseif ($header['class'] === ASN1::CLASS_UNIVERSAL && |
|
| 21 | - in_array($header['tag'], [ASN1::TYPE_SET, ASN1::TYPE_SEQUENCE]) |
|
| 20 | + } elseif ($header[ 'class' ] === ASN1::CLASS_UNIVERSAL && |
|
| 21 | + in_array($header[ 'tag' ], [ ASN1::TYPE_SET, ASN1::TYPE_SEQUENCE ]) |
|
| 22 | 22 | ) { |
| 23 | - $header['children'] = $this->lazyParse( |
|
| 23 | + $header[ 'children' ] = $this->lazyParse( |
|
| 24 | 24 | null, |
| 25 | - $header['length'] ? $header['start'] + $header['length'] - 1 : null, |
|
| 25 | + $header[ 'length' ] ? $header[ 'start' ] + $header[ 'length' ] - 1 : null, |
|
| 26 | 26 | 'header' |
| 27 | 27 | ); |
| 28 | 28 | } else { |
| 29 | - $header['value'] = $this->decode($header); |
|
| 29 | + $header[ 'value' ] = $this->decode($header); |
|
| 30 | 30 | } |
| 31 | 31 | return $header; |
| 32 | 32 | } |
| 33 | 33 | public function lazyDecodeValue(array $header) |
| 34 | 34 | { |
| 35 | - $this->reader->seek($header['content_start']); |
|
| 36 | - if ($header['class'] !== ASN1::CLASS_UNIVERSAL && $header['constructed']) { |
|
| 35 | + $this->reader->seek($header[ 'content_start' ]); |
|
| 36 | + if ($header[ 'class' ] !== ASN1::CLASS_UNIVERSAL && $header[ 'constructed' ]) { |
|
| 37 | 37 | return $this->lazyParse( |
| 38 | 38 | null, |
| 39 | - $header['length'] ? $header['start'] + $header['length'] - 1 : null, |
|
| 39 | + $header[ 'length' ] ? $header[ 'start' ] + $header[ 'length' ] - 1 : null, |
|
| 40 | 40 | 'value' |
| 41 | 41 | ); |
| 42 | 42 | } |
| 43 | - if ($header['class'] === ASN1::CLASS_UNIVERSAL && |
|
| 44 | - in_array($header['tag'], [ASN1::TYPE_SET, ASN1::TYPE_SEQUENCE]) |
|
| 43 | + if ($header[ 'class' ] === ASN1::CLASS_UNIVERSAL && |
|
| 44 | + in_array($header[ 'tag' ], [ ASN1::TYPE_SET, ASN1::TYPE_SEQUENCE ]) |
|
| 45 | 45 | ) { |
| 46 | 46 | return $this->lazyParse( |
| 47 | 47 | null, |
| 48 | - $header['length'] ? $header['start'] + $header['length'] - 1 : null, |
|
| 48 | + $header[ 'length' ] ? $header[ 'start' ] + $header[ 'length' ] - 1 : null, |
|
| 49 | 49 | 'value' |
| 50 | 50 | ); |
| 51 | 51 | } |
@@ -56,33 +56,33 @@ discard block |
||
| 56 | 56 | if ($start !== null) { |
| 57 | 57 | $this->reader->seek($start); |
| 58 | 58 | } |
| 59 | - $skeleton = []; |
|
| 59 | + $skeleton = [ ]; |
|
| 60 | 60 | while (!$this->reader->eof() && ($max === null || $this->reader->pos() < $max)) { |
| 61 | 61 | $header = $this->header(); |
| 62 | - if ($header['class'] === 0 && $header['tag'] === 0) { |
|
| 62 | + if ($header[ 'class' ] === 0 && $header[ 'tag' ] === 0) { |
|
| 63 | 63 | if ($max === null) { |
| 64 | 64 | break; |
| 65 | 65 | } else { |
| 66 | 66 | continue; |
| 67 | 67 | } |
| 68 | 68 | } |
| 69 | - if ($header['length'] === null) { |
|
| 69 | + if ($header[ 'length' ] === null) { |
|
| 70 | 70 | $this->reader->readUntil(chr(0).chr(0)); |
| 71 | - $header['length'] = $this->reader->pos() - $header['start']; |
|
| 72 | - $header['content_length'] = $this->reader->pos() - $header['content_start']; |
|
| 71 | + $header[ 'length' ] = $this->reader->pos() - $header[ 'start' ]; |
|
| 72 | + $header[ 'content_length' ] = $this->reader->pos() - $header[ 'content_start' ]; |
|
| 73 | 73 | } else { |
| 74 | - if ($header['content_length'] > 0) { |
|
| 75 | - $this->reader->bytes($header['content_length']); |
|
| 74 | + if ($header[ 'content_length' ] > 0) { |
|
| 75 | + $this->reader->bytes($header[ 'content_length' ]); |
|
| 76 | 76 | } |
| 77 | 77 | } |
| 78 | - $skeleton[] = $header; |
|
| 78 | + $skeleton[ ] = $header; |
|
| 79 | 79 | } |
| 80 | 80 | switch ($mode) { |
| 81 | 81 | case 'value': |
| 82 | - return new LazyArray($skeleton, function ($v) { return $this->lazyDecodeValue($v); }); |
|
| 82 | + return new LazyArray($skeleton, function($v) { return $this->lazyDecodeValue($v); }); |
|
| 83 | 83 | case 'header': |
| 84 | 84 | default: |
| 85 | - return new LazyArray($skeleton, function ($v) { return $this->lazyDecodeHeader($v); }); |
|
| 85 | + return new LazyArray($skeleton, function($v) { return $this->lazyDecodeHeader($v); }); |
|
| 86 | 86 | } |
| 87 | 87 | } |
| 88 | 88 | public function structure($max = null) |
@@ -99,114 +99,114 @@ discard block |
||
| 99 | 99 | if ($skeleton === null && $this->reader->pos() !== 0) { |
| 100 | 100 | $this->reader->rewind(); |
| 101 | 101 | } |
| 102 | - $skeleton = $skeleton ?? $this->structure()[0] ?? null; |
|
| 102 | + $skeleton = $skeleton ?? $this->structure()[ 0 ] ?? null; |
|
| 103 | 103 | if (!isset($skeleton)) { |
| 104 | 104 | throw new ASN1Exception('No decoded data for map'); |
| 105 | 105 | } |
| 106 | - if ($skeleton['class'] !== ASN1::CLASS_UNIVERSAL) { |
|
| 107 | - if ($map['tag'] === ASN1::TYPE_CHOICE) { |
|
| 108 | - foreach ($map['children'] as $child) { |
|
| 109 | - if (isset($child['name']) && (int)$skeleton['tag'] === (int)$child['name']) { |
|
| 106 | + if ($skeleton[ 'class' ] !== ASN1::CLASS_UNIVERSAL) { |
|
| 107 | + if ($map[ 'tag' ] === ASN1::TYPE_CHOICE) { |
|
| 108 | + foreach ($map[ 'children' ] as $child) { |
|
| 109 | + if (isset($child[ 'name' ]) && (int) $skeleton[ 'tag' ] === (int) $child[ 'name' ]) { |
|
| 110 | 110 | $map = $child; |
| 111 | - if (isset($child['value']) && $child['value']) { |
|
| 112 | - return $child['value']; |
|
| 111 | + if (isset($child[ 'value' ]) && $child[ 'value' ]) { |
|
| 112 | + return $child[ 'value' ]; |
|
| 113 | 113 | } |
| 114 | 114 | break; |
| 115 | 115 | } |
| 116 | 116 | } |
| 117 | 117 | } |
| 118 | 118 | } |
| 119 | - if ($skeleton['class'] !== ASN1::CLASS_UNIVERSAL) { |
|
| 120 | - if (isset($map['implicit']) && $map['implicit']) { |
|
| 121 | - $skeleton['class'] = ASN1::CLASS_UNIVERSAL; |
|
| 122 | - $skeleton['tag'] = $map['tag']; |
|
| 119 | + if ($skeleton[ 'class' ] !== ASN1::CLASS_UNIVERSAL) { |
|
| 120 | + if (isset($map[ 'implicit' ]) && $map[ 'implicit' ]) { |
|
| 121 | + $skeleton[ 'class' ] = ASN1::CLASS_UNIVERSAL; |
|
| 122 | + $skeleton[ 'tag' ] = $map[ 'tag' ]; |
|
| 123 | 123 | } else { |
| 124 | - $skeleton = $skeleton['children'][0] ?? null; |
|
| 124 | + $skeleton = $skeleton[ 'children' ][ 0 ] ?? null; |
|
| 125 | 125 | } |
| 126 | 126 | } |
| 127 | - if ($map['tag'] === ASN1::TYPE_CHOICE) { |
|
| 128 | - foreach ($map['children'] as $child) { |
|
| 129 | - if ($skeleton['tag'] === $child['tag']) { |
|
| 127 | + if ($map[ 'tag' ] === ASN1::TYPE_CHOICE) { |
|
| 128 | + foreach ($map[ 'children' ] as $child) { |
|
| 129 | + if ($skeleton[ 'tag' ] === $child[ 'tag' ]) { |
|
| 130 | 130 | $map = $child; |
| 131 | - if (isset($child['value']) && $child['value']) { |
|
| 132 | - return $child['value']; |
|
| 131 | + if (isset($child[ 'value' ]) && $child[ 'value' ]) { |
|
| 132 | + return $child[ 'value' ]; |
|
| 133 | 133 | } |
| 134 | 134 | break; |
| 135 | 135 | } |
| 136 | 136 | } |
| 137 | 137 | } |
| 138 | - if (in_array($map['tag'], [ASN1::TYPE_SEQUENCE, ASN1::TYPE_SET]) && |
|
| 139 | - in_array($skeleton['tag'], [ASN1::TYPE_SEQUENCE, ASN1::TYPE_SET])) { |
|
| 140 | - $map['tag'] = $skeleton['tag']; |
|
| 138 | + if (in_array($map[ 'tag' ], [ ASN1::TYPE_SEQUENCE, ASN1::TYPE_SET ]) && |
|
| 139 | + in_array($skeleton[ 'tag' ], [ ASN1::TYPE_SEQUENCE, ASN1::TYPE_SET ])) { |
|
| 140 | + $map[ 'tag' ] = $skeleton[ 'tag' ]; |
|
| 141 | 141 | } |
| 142 | - if ($map['tag'] === ASN1::TYPE_ANY && isset($skeleton['tag'])) { |
|
| 143 | - $map['tag'] = $skeleton['tag']; |
|
| 142 | + if ($map[ 'tag' ] === ASN1::TYPE_ANY && isset($skeleton[ 'tag' ])) { |
|
| 143 | + $map[ 'tag' ] = $skeleton[ 'tag' ]; |
|
| 144 | 144 | } |
| 145 | - if (!in_array($map['tag'], [ASN1::TYPE_ANY, ASN1::TYPE_ANY_RAW, ASN1::TYPE_ANY_SKIP, ASN1::TYPE_ANY_DER]) && |
|
| 146 | - $map['tag'] !== $skeleton['tag'] |
|
| 145 | + if (!in_array($map[ 'tag' ], [ ASN1::TYPE_ANY, ASN1::TYPE_ANY_RAW, ASN1::TYPE_ANY_SKIP, ASN1::TYPE_ANY_DER ]) && |
|
| 146 | + $map[ 'tag' ] !== $skeleton[ 'tag' ] |
|
| 147 | 147 | ) { |
| 148 | - if (!isset($map['optional']) || !$map['optional']) { |
|
| 149 | - throw new ASN1Exception('Decoded data does not match mapping - ' . $skeleton['tag']); |
|
| 148 | + if (!isset($map[ 'optional' ]) || !$map[ 'optional' ]) { |
|
| 149 | + throw new ASN1Exception('Decoded data does not match mapping - '.$skeleton[ 'tag' ]); |
|
| 150 | 150 | } |
| 151 | 151 | return $null; |
| 152 | 152 | } else { |
| 153 | - switch ($map['tag']) { |
|
| 153 | + switch ($map[ 'tag' ]) { |
|
| 154 | 154 | case ASN1::TYPE_ANY_DER: |
| 155 | - $temp = $this->reader->chunk($skeleton['start'], $skeleton['length']); |
|
| 155 | + $temp = $this->reader->chunk($skeleton[ 'start' ], $skeleton[ 'length' ]); |
|
| 156 | 156 | return $temp; |
| 157 | 157 | case ASN1::TYPE_ANY_SKIP: |
| 158 | 158 | return $null; |
| 159 | 159 | case ASN1::TYPE_ANY_RAW: |
| 160 | - return $skeleton['value'] ?? null; |
|
| 160 | + return $skeleton[ 'value' ] ?? null; |
|
| 161 | 161 | case ASN1::TYPE_SET: |
| 162 | - if (isset($map['repeat'])) { |
|
| 163 | - $mapRepeat = $map['repeat']; |
|
| 164 | - return new LazyArray($skeleton['children']->rawData(), function ($v) use ($mapRepeat) { |
|
| 162 | + if (isset($map[ 'repeat' ])) { |
|
| 163 | + $mapRepeat = $map[ 'repeat' ]; |
|
| 164 | + return new LazyArray($skeleton[ 'children' ]->rawData(), function($v) use ($mapRepeat) { |
|
| 165 | 165 | return $this->map($mapRepeat, $this->lazyDecodeHeader($v)); |
| 166 | 166 | }); |
| 167 | 167 | } else { |
| 168 | - if (!isset($map['children'])) { |
|
| 168 | + if (!isset($map[ 'children' ])) { |
|
| 169 | 169 | return $null; |
| 170 | 170 | } |
| 171 | - $temp = $skeleton['children']; |
|
| 172 | - $result = []; |
|
| 171 | + $temp = $skeleton[ 'children' ]; |
|
| 172 | + $result = [ ]; |
|
| 173 | 173 | // named first |
| 174 | - foreach ($map['children'] as $k => $v) { |
|
| 175 | - if (isset($v['name'])) { |
|
| 176 | - $result[$k] = null; |
|
| 174 | + foreach ($map[ 'children' ] as $k => $v) { |
|
| 175 | + if (isset($v[ 'name' ])) { |
|
| 176 | + $result[ $k ] = null; |
|
| 177 | 177 | foreach ($temp as $kk => $vv) { |
| 178 | - if ($vv['class'] !== ASN1::CLASS_UNIVERSAL && (int)$v['name'] === $vv['tag']) { |
|
| 178 | + if ($vv[ 'class' ] !== ASN1::CLASS_UNIVERSAL && (int) $v[ 'name' ] === $vv[ 'tag' ]) { |
|
| 179 | 179 | try { |
| 180 | - if (isset($v['implicit']) && $v['implicit']) { |
|
| 181 | - $vv['class'] = ASN1::CLASS_UNIVERSAL; |
|
| 182 | - $vv['tag'] = $map['tag']; |
|
| 180 | + if (isset($v[ 'implicit' ]) && $v[ 'implicit' ]) { |
|
| 181 | + $vv[ 'class' ] = ASN1::CLASS_UNIVERSAL; |
|
| 182 | + $vv[ 'tag' ] = $map[ 'tag' ]; |
|
| 183 | 183 | } else { |
| 184 | - $vv = $vv['children'][0] ?? null; |
|
| 184 | + $vv = $vv[ 'children' ][ 0 ] ?? null; |
|
| 185 | 185 | } |
| 186 | - $result[$k] = $this->map($v, $vv); |
|
| 187 | - $vv['map'] = $v; |
|
| 188 | - $result[$k] = $vv; |
|
| 189 | - unset($temp[$kk]); |
|
| 186 | + $result[ $k ] = $this->map($v, $vv); |
|
| 187 | + $vv[ 'map' ] = $v; |
|
| 188 | + $result[ $k ] = $vv; |
|
| 189 | + unset($temp[ $kk ]); |
|
| 190 | 190 | break; |
| 191 | 191 | } catch (ASN1Exception $e) { |
| 192 | 192 | // continue trying other children in case of failure |
| 193 | 193 | } |
| 194 | 194 | } |
| 195 | 195 | } |
| 196 | - if ($result[$k] === null && (!isset($v['optional']) || !$v['optional'])) { |
|
| 197 | - throw new ASN1Exception('Missing tagged type - ' . $k); |
|
| 196 | + if ($result[ $k ] === null && (!isset($v[ 'optional' ]) || !$v[ 'optional' ])) { |
|
| 197 | + throw new ASN1Exception('Missing tagged type - '.$k); |
|
| 198 | 198 | } |
| 199 | 199 | } |
| 200 | 200 | } |
| 201 | - foreach ($map['children'] as $k => $v) { |
|
| 202 | - if (isset($v['name'])) { |
|
| 201 | + foreach ($map[ 'children' ] as $k => $v) { |
|
| 202 | + if (isset($v[ 'name' ])) { |
|
| 203 | 203 | continue; |
| 204 | 204 | } |
| 205 | - $result[$k] = null; |
|
| 205 | + $result[ $k ] = null; |
|
| 206 | 206 | foreach ($temp as $kk => $vv) { |
| 207 | - if ($v['tag'] === $vv['tag'] || |
|
| 207 | + if ($v[ 'tag' ] === $vv[ 'tag' ] || |
|
| 208 | 208 | in_array( |
| 209 | - $v['tag'], |
|
| 209 | + $v[ 'tag' ], |
|
| 210 | 210 | [ |
| 211 | 211 | ASN1::TYPE_ANY, |
| 212 | 212 | ASN1::TYPE_ANY_DER, |
@@ -217,57 +217,57 @@ discard block |
||
| 217 | 217 | ) |
| 218 | 218 | ) { |
| 219 | 219 | try { |
| 220 | - $result[$k] = $this->map($v, $vv); |
|
| 221 | - $vv['map'] = $v; |
|
| 222 | - $result[$k] = $vv; |
|
| 223 | - unset($temp[$kk]); |
|
| 220 | + $result[ $k ] = $this->map($v, $vv); |
|
| 221 | + $vv[ 'map' ] = $v; |
|
| 222 | + $result[ $k ] = $vv; |
|
| 223 | + unset($temp[ $kk ]); |
|
| 224 | 224 | break; |
| 225 | 225 | } catch (ASN1Exception $e) { |
| 226 | - $result[$k] = null; |
|
| 226 | + $result[ $k ] = null; |
|
| 227 | 227 | } |
| 228 | 228 | } |
| 229 | 229 | } |
| 230 | - if ($result[$k] === null && (!isset($v['optional']) || !$v['optional'])) { |
|
| 231 | - throw new ASN1Exception('Decoded data does not match mapping - ' . $k); |
|
| 230 | + if ($result[ $k ] === null && (!isset($v[ 'optional' ]) || !$v[ 'optional' ])) { |
|
| 231 | + throw new ASN1Exception('Decoded data does not match mapping - '.$k); |
|
| 232 | 232 | } |
| 233 | 233 | } |
| 234 | - return new LazyArray($result, function ($v) { |
|
| 235 | - return $v === null ? null : $this->map($v['map'], $this->lazyDecodeHeader($v)); |
|
| 234 | + return new LazyArray($result, function($v) { |
|
| 235 | + return $v === null ? null : $this->map($v[ 'map' ], $this->lazyDecodeHeader($v)); |
|
| 236 | 236 | }); |
| 237 | 237 | } |
| 238 | 238 | break; |
| 239 | 239 | case ASN1::TYPE_SEQUENCE: |
| 240 | - if (isset($map['repeat'])) { |
|
| 241 | - $mapRepeat = $map['repeat']; |
|
| 242 | - return new LazyArray($skeleton['children']->rawData(), function ($v) use ($mapRepeat) { |
|
| 240 | + if (isset($map[ 'repeat' ])) { |
|
| 241 | + $mapRepeat = $map[ 'repeat' ]; |
|
| 242 | + return new LazyArray($skeleton[ 'children' ]->rawData(), function($v) use ($mapRepeat) { |
|
| 243 | 243 | return $this->map($mapRepeat, $this->lazyDecodeHeader($v)); |
| 244 | 244 | }); |
| 245 | 245 | } else { |
| 246 | - if (!isset($map['children'])) { |
|
| 246 | + if (!isset($map[ 'children' ])) { |
|
| 247 | 247 | return $null; |
| 248 | 248 | } |
| 249 | - $result = []; |
|
| 250 | - foreach ($skeleton['children'] as $vv) { |
|
| 251 | - foreach ($map['children'] as $k => $v) { |
|
| 252 | - if (isset($v['name']) && $vv['class'] !== ASN1::CLASS_UNIVERSAL && |
|
| 253 | - (int)$v['name'] === $vv['tag'] |
|
| 249 | + $result = [ ]; |
|
| 250 | + foreach ($skeleton[ 'children' ] as $vv) { |
|
| 251 | + foreach ($map[ 'children' ] as $k => $v) { |
|
| 252 | + if (isset($v[ 'name' ]) && $vv[ 'class' ] !== ASN1::CLASS_UNIVERSAL && |
|
| 253 | + (int) $v[ 'name' ] === $vv[ 'tag' ] |
|
| 254 | 254 | ) { |
| 255 | - if (isset($v['implicit']) && $v['implicit']) { |
|
| 256 | - $vv['class'] = ASN1::CLASS_UNIVERSAL; |
|
| 257 | - $vv['tag'] = $map['tag']; |
|
| 255 | + if (isset($v[ 'implicit' ]) && $v[ 'implicit' ]) { |
|
| 256 | + $vv[ 'class' ] = ASN1::CLASS_UNIVERSAL; |
|
| 257 | + $vv[ 'tag' ] = $map[ 'tag' ]; |
|
| 258 | 258 | } else { |
| 259 | - $vv = $vv['children'][0] ?? null; |
|
| 259 | + $vv = $vv[ 'children' ][ 0 ] ?? null; |
|
| 260 | 260 | } |
| 261 | - $vv['map'] = $v; |
|
| 262 | - $result[$k] = $vv; |
|
| 263 | - unset($map['children'][$k]); |
|
| 261 | + $vv[ 'map' ] = $v; |
|
| 262 | + $result[ $k ] = $vv; |
|
| 263 | + unset($map[ 'children' ][ $k ]); |
|
| 264 | 264 | break; |
| 265 | 265 | } |
| 266 | - if (!isset($v['name']) && |
|
| 266 | + if (!isset($v[ 'name' ]) && |
|
| 267 | 267 | ( |
| 268 | - $v['tag'] === $vv['tag'] || |
|
| 268 | + $v[ 'tag' ] === $vv[ 'tag' ] || |
|
| 269 | 269 | in_array( |
| 270 | - $v['tag'], |
|
| 270 | + $v[ 'tag' ], |
|
| 271 | 271 | [ |
| 272 | 272 | ASN1::TYPE_ANY, |
| 273 | 273 | ASN1::TYPE_ANY_DER, |
@@ -280,74 +280,72 @@ discard block |
||
| 280 | 280 | ) { |
| 281 | 281 | try { |
| 282 | 282 | $temp = $this->map($v, $vv); |
| 283 | - $vv['map'] = $v; |
|
| 284 | - $result[$k] = $vv; |
|
| 285 | - unset($map['children'][$k]); |
|
| 283 | + $vv[ 'map' ] = $v; |
|
| 284 | + $result[ $k ] = $vv; |
|
| 285 | + unset($map[ 'children' ][ $k ]); |
|
| 286 | 286 | break; |
| 287 | 287 | } catch (ASN1Exception $e) { |
| 288 | 288 | // continue trying other children in case of failure |
| 289 | 289 | } |
| 290 | 290 | } |
| 291 | - if (!isset($v['optional']) || !$v['optional']) { |
|
| 292 | - throw new ASN1Exception('Missing type - ' . $k); |
|
| 291 | + if (!isset($v[ 'optional' ]) || !$v[ 'optional' ]) { |
|
| 292 | + throw new ASN1Exception('Missing type - '.$k); |
|
| 293 | 293 | } else { |
| 294 | - $result[$k] = null; |
|
| 295 | - unset($map['children'][$k]); |
|
| 294 | + $result[ $k ] = null; |
|
| 295 | + unset($map[ 'children' ][ $k ]); |
|
| 296 | 296 | } |
| 297 | 297 | } |
| 298 | 298 | } |
| 299 | - return new LazyArray($result, function ($v) { |
|
| 300 | - return $v === null ? null : $this->map($v['map'], $this->lazyDecodeHeader($v)); |
|
| 299 | + return new LazyArray($result, function($v) { |
|
| 300 | + return $v === null ? null : $this->map($v[ 'map' ], $this->lazyDecodeHeader($v)); |
|
| 301 | 301 | }); |
| 302 | 302 | } |
| 303 | 303 | break; |
| 304 | 304 | case ASN1::TYPE_OBJECT_IDENTIFIER: |
| 305 | - $temp = isset($map['resolve']) && $map['resolve'] ? |
|
| 306 | - ASN1::OIDtoText($skeleton['value']) : |
|
| 307 | - $skeleton['value']; |
|
| 305 | + $temp = isset($map[ 'resolve' ]) && $map[ 'resolve' ] ? |
|
| 306 | + ASN1::OIDtoText($skeleton[ 'value' ]) : $skeleton[ 'value' ]; |
|
| 308 | 307 | return $temp; |
| 309 | 308 | case ASN1::TYPE_OCTET_STRING: |
| 310 | - if (isset($map['der']) && $map['der']) { |
|
| 311 | - $temp = static::fromString($skeleton['value']); |
|
| 312 | - $temp = isset($map['map']) ? $temp->map($map['map']) : $temp->values(); |
|
| 309 | + if (isset($map[ 'der' ]) && $map[ 'der' ]) { |
|
| 310 | + $temp = static::fromString($skeleton[ 'value' ]); |
|
| 311 | + $temp = isset($map[ 'map' ]) ? $temp->map($map[ 'map' ]) : $temp->values(); |
|
| 313 | 312 | return $temp; |
| 314 | 313 | } else { |
| 315 | - $temp = isset($map['raw']) && $map['raw'] ? |
|
| 316 | - $skeleton['value'] : |
|
| 317 | - base64_encode($skeleton['value']); |
|
| 314 | + $temp = isset($map[ 'raw' ]) && $map[ 'raw' ] ? |
|
| 315 | + $skeleton[ 'value' ] : base64_encode($skeleton[ 'value' ]); |
|
| 318 | 316 | return $temp; |
| 319 | 317 | } |
| 320 | 318 | break; |
| 321 | 319 | case ASN1::TYPE_INTEGER: |
| 322 | - $base = isset($map['base']) && (int)$map['base'] ? (int)$map['base'] : 10; |
|
| 320 | + $base = isset($map[ 'base' ]) && (int) $map[ 'base' ] ? (int) $map[ 'base' ] : 10; |
|
| 323 | 321 | if ($base < 3) { |
| 324 | - $result = $skeleton['value']; |
|
| 322 | + $result = $skeleton[ 'value' ]; |
|
| 325 | 323 | } else { |
| 326 | - if (strlen($skeleton['value']) > 53 && $base === 16) { |
|
| 324 | + if (strlen($skeleton[ 'value' ]) > 53 && $base === 16) { |
|
| 327 | 325 | $hex = ''; |
| 328 | - for ($i = strlen($skeleton['value']) - 4; $i >= 0; $i-=4) { |
|
| 329 | - $hex .= dechex((int)bindec(substr($skeleton['value'], $i, 4))); |
|
| 326 | + for ($i = strlen($skeleton[ 'value' ]) - 4; $i >= 0; $i -= 4) { |
|
| 327 | + $hex .= dechex((int) bindec(substr($skeleton[ 'value' ], $i, 4))); |
|
| 330 | 328 | } |
| 331 | 329 | $result = strrev($hex); |
| 332 | 330 | } else { |
| 333 | - $temp = base_convert($skeleton['value'], 2, $base); |
|
| 331 | + $temp = base_convert($skeleton[ 'value' ], 2, $base); |
|
| 334 | 332 | if ($base === 10) { |
| 335 | - $temp = (int)$temp; |
|
| 333 | + $temp = (int) $temp; |
|
| 336 | 334 | } |
| 337 | 335 | $result = $temp; |
| 338 | 336 | } |
| 339 | 337 | } |
| 340 | - if (isset($map['map']) && isset($map['map'][$result])) { |
|
| 341 | - $result = $map['map'][$result]; |
|
| 338 | + if (isset($map[ 'map' ]) && isset($map[ 'map' ][ $result ])) { |
|
| 339 | + $result = $map[ 'map' ][ $result ]; |
|
| 342 | 340 | } |
| 343 | 341 | return $result; |
| 344 | 342 | case ASN1::TYPE_UTC_TIME: |
| 345 | 343 | case ASN1::TYPE_GENERALIZED_TIME: |
| 346 | - return $skeleton['value']; |
|
| 344 | + return $skeleton[ 'value' ]; |
|
| 347 | 345 | default: |
| 348 | - $result = $skeleton['value']; |
|
| 349 | - if (isset($map['map']) && isset($map['map'][$result])) { |
|
| 350 | - $result = $map['map'][$result]; |
|
| 346 | + $result = $skeleton[ 'value' ]; |
|
| 347 | + if (isset($map[ 'map' ]) && isset($map[ 'map' ][ $result ])) { |
|
| 348 | + $result = $map[ 'map' ][ $result ]; |
|
| 351 | 349 | } |
| 352 | 350 | return $result; |
| 353 | 351 | } |
@@ -105,36 +105,35 @@ discard block |
||
| 105 | 105 | } |
| 106 | 106 | protected function decode($header) |
| 107 | 107 | { |
| 108 | - $contents = $header['content_length'] > 0 ? |
|
| 109 | - $this->reader->chunk($header['content_start'], $header['content_length']) : |
|
| 110 | - ''; |
|
| 111 | - if ($header['class'] !== ASN1::CLASS_UNIVERSAL) { |
|
| 108 | + $contents = $header[ 'content_length' ] > 0 ? |
|
| 109 | + $this->reader->chunk($header[ 'content_start' ], $header[ 'content_length' ]) : ''; |
|
| 110 | + if ($header[ 'class' ] !== ASN1::CLASS_UNIVERSAL) { |
|
| 112 | 111 | return $contents; |
| 113 | 112 | } |
| 114 | - switch ($header['tag']) { |
|
| 113 | + switch ($header[ 'tag' ]) { |
|
| 115 | 114 | case ASN1::TYPE_BOOLEAN: |
| 116 | - return (bool)ord($contents[0]); |
|
| 115 | + return (bool) ord($contents[ 0 ]); |
|
| 117 | 116 | case ASN1::TYPE_INTEGER: |
| 118 | 117 | return ASN1::fromBase256($contents); |
| 119 | 118 | case ASN1::TYPE_ENUMERATED: |
| 120 | - return (int)base_convert(ASN1::fromBase256($contents), 2, 10); |
|
| 119 | + return (int) base_convert(ASN1::fromBase256($contents), 2, 10); |
|
| 121 | 120 | case ASN1::TYPE_REAL: |
| 122 | 121 | // TODO: read the specs |
| 123 | 122 | return false; |
| 124 | 123 | case ASN1::TYPE_BIT_STRING: |
| 125 | - if ($header['constructed']) { |
|
| 124 | + if ($header[ 'constructed' ]) { |
|
| 126 | 125 | $temp = static::fromString($contents)->values(); |
| 127 | 126 | $real = ''; |
| 128 | 127 | for ($i = 0; $i < count($temp) - 1; $i++) { |
| 129 | - $real .= $temp['value']; |
|
| 128 | + $real .= $temp[ 'value' ]; |
|
| 130 | 129 | } |
| 131 | - return $temp[count($temp) - 1]['value'][0] . $real . substr($temp[$i]['value'], 1); |
|
| 130 | + return $temp[ count($temp) - 1 ][ 'value' ][ 0 ].$real.substr($temp[ $i ][ 'value' ], 1); |
|
| 132 | 131 | } |
| 133 | 132 | return $contents; |
| 134 | 133 | case ASN1::TYPE_OCTET_STRING: |
| 135 | - if ($header['constructed']) { |
|
| 136 | - return implode('', array_map(function ($v) { |
|
| 137 | - return $v['value']; |
|
| 134 | + if ($header[ 'constructed' ]) { |
|
| 135 | + return implode('', array_map(function($v) { |
|
| 136 | + return $v[ 'value' ]; |
|
| 138 | 137 | }, static::fromString($contents)->values())); |
| 139 | 138 | } |
| 140 | 139 | return $contents; |
@@ -142,14 +141,14 @@ discard block |
||
| 142 | 141 | return null; |
| 143 | 142 | case ASN1::TYPE_UTC_TIME: |
| 144 | 143 | $format = 'YmdHis'; |
| 145 | - $matches = []; |
|
| 144 | + $matches = [ ]; |
|
| 146 | 145 | if (preg_match('#^(\d{10})(Z|[+-]\d{4})$#', $contents, $matches)) { |
| 147 | - $contents = $matches[1] . '00' . $matches[2]; |
|
| 146 | + $contents = $matches[ 1 ].'00'.$matches[ 2 ]; |
|
| 148 | 147 | } |
| 149 | 148 | $prefix = substr($contents, 0, 2) >= 50 ? '19' : '20'; |
| 150 | - $contents = $prefix . $contents; |
|
| 151 | - if ($contents[strlen($contents) - 1] == 'Z') { |
|
| 152 | - $contents = substr($contents, 0, -1) . '+0000'; |
|
| 149 | + $contents = $prefix.$contents; |
|
| 150 | + if ($contents[ strlen($contents) - 1 ] == 'Z') { |
|
| 151 | + $contents = substr($contents, 0, -1).'+0000'; |
|
| 153 | 152 | } |
| 154 | 153 | if (strpos($contents, '-') !== false || strpos($contents, '+') !== false) { |
| 155 | 154 | $format .= 'O'; |
@@ -161,8 +160,8 @@ discard block |
||
| 161 | 160 | if (strpos($contents, '.') !== false) { |
| 162 | 161 | $format .= '.u'; |
| 163 | 162 | } |
| 164 | - if ($contents[strlen($contents) - 1] == 'Z') { |
|
| 165 | - $contents = substr($contents, 0, -1) . '+0000'; |
|
| 163 | + if ($contents[ strlen($contents) - 1 ] == 'Z') { |
|
| 164 | + $contents = substr($contents, 0, -1).'+0000'; |
|
| 166 | 165 | } |
| 167 | 166 | if (strpos($contents, '-') !== false || strpos($contents, '+') !== false) { |
| 168 | 167 | $format .= 'O'; |
@@ -170,16 +169,16 @@ discard block |
||
| 170 | 169 | $result = @DateTime::createFromFormat($format, $contents); |
| 171 | 170 | return $result ? $result->getTimestamp() : false; |
| 172 | 171 | case ASN1::TYPE_OBJECT_IDENTIFIER: |
| 173 | - $temp = ord($contents[0]); |
|
| 172 | + $temp = ord($contents[ 0 ]); |
|
| 174 | 173 | $real = sprintf('%d.%d', floor($temp / 40), $temp % 40); |
| 175 | 174 | $obid = 0; |
| 176 | 175 | // process septets |
| 177 | 176 | for ($i = 1; $i < strlen($contents); $i++) { |
| 178 | - $temp = ord($contents[$i]); |
|
| 177 | + $temp = ord($contents[ $i ]); |
|
| 179 | 178 | $obid <<= 7; |
| 180 | 179 | $obid |= $temp & 0x7F; |
| 181 | 180 | if (~$temp & 0x80) { |
| 182 | - $real .= '.' . $obid; |
|
| 181 | + $real .= '.'.$obid; |
|
| 183 | 182 | $obid = 0; |
| 184 | 183 | } |
| 185 | 184 | } |
@@ -196,55 +195,55 @@ discard block |
||
| 196 | 195 | */ |
| 197 | 196 | public function structure($max = null) |
| 198 | 197 | { |
| 199 | - $skeleton = []; |
|
| 198 | + $skeleton = [ ]; |
|
| 200 | 199 | while (!$this->reader->eof() && ($max === null || $this->reader->pos() < $max)) { |
| 201 | 200 | $header = $this->header(); |
| 202 | - if ($header['class'] === 0 && $header['tag'] === 0) { |
|
| 201 | + if ($header[ 'class' ] === 0 && $header[ 'tag' ] === 0) { |
|
| 203 | 202 | if ($max === null) { |
| 204 | 203 | break; |
| 205 | 204 | } else { |
| 206 | 205 | continue; |
| 207 | 206 | } |
| 208 | 207 | } |
| 209 | - if ($header['class'] !== ASN1::CLASS_UNIVERSAL && $header['constructed']) { |
|
| 210 | - $header['children'] = $this->structure( |
|
| 211 | - $header['length'] ? $header['start'] + $header['length'] - 1 : null |
|
| 208 | + if ($header[ 'class' ] !== ASN1::CLASS_UNIVERSAL && $header[ 'constructed' ]) { |
|
| 209 | + $header[ 'children' ] = $this->structure( |
|
| 210 | + $header[ 'length' ] ? $header[ 'start' ] + $header[ 'length' ] - 1 : null |
|
| 212 | 211 | ); |
| 213 | - if ($header['length'] === null) { |
|
| 212 | + if ($header[ 'length' ] === null) { |
|
| 214 | 213 | $this->reader->byte(); |
| 215 | - $header['length'] = $this->reader->pos() - $header['start']; |
|
| 216 | - $header['content_length'] = $this->reader->pos() - $header['content_start']; |
|
| 214 | + $header[ 'length' ] = $this->reader->pos() - $header[ 'start' ]; |
|
| 215 | + $header[ 'content_length' ] = $this->reader->pos() - $header[ 'content_start' ]; |
|
| 217 | 216 | } |
| 218 | - $skeleton[] = $header; |
|
| 217 | + $skeleton[ ] = $header; |
|
| 219 | 218 | } else { |
| 220 | - if ($header['class'] === ASN1::CLASS_UNIVERSAL && |
|
| 221 | - in_array($header['tag'], [ASN1::TYPE_SET, ASN1::TYPE_SEQUENCE]) |
|
| 219 | + if ($header[ 'class' ] === ASN1::CLASS_UNIVERSAL && |
|
| 220 | + in_array($header[ 'tag' ], [ ASN1::TYPE_SET, ASN1::TYPE_SEQUENCE ]) |
|
| 222 | 221 | ) { |
| 223 | - $header['children'] = $this->structure( |
|
| 224 | - $header['length'] ? $header['start'] + $header['length'] - 1 : null |
|
| 222 | + $header[ 'children' ] = $this->structure( |
|
| 223 | + $header[ 'length' ] ? $header[ 'start' ] + $header[ 'length' ] - 1 : null |
|
| 225 | 224 | ); |
| 226 | - if ($header['length'] === null) { |
|
| 225 | + if ($header[ 'length' ] === null) { |
|
| 227 | 226 | $this->reader->byte(); |
| 228 | - $header['length'] = $this->reader->pos() - $header['start']; |
|
| 229 | - $header['content_length'] = $this->reader->pos() - $header['content_start']; |
|
| 227 | + $header[ 'length' ] = $this->reader->pos() - $header[ 'start' ]; |
|
| 228 | + $header[ 'content_length' ] = $this->reader->pos() - $header[ 'content_start' ]; |
|
| 230 | 229 | } |
| 231 | 230 | } else { |
| 232 | - if ($header['length'] === null) { |
|
| 231 | + if ($header[ 'length' ] === null) { |
|
| 233 | 232 | $this->reader->readUntil(chr(0).chr(0)); |
| 234 | - $header['length'] = $this->reader->pos() - $header['start']; |
|
| 235 | - $header['content_length'] = $this->reader->pos() - $header['content_start']; |
|
| 233 | + $header[ 'length' ] = $this->reader->pos() - $header[ 'start' ]; |
|
| 234 | + $header[ 'content_length' ] = $this->reader->pos() - $header[ 'content_start' ]; |
|
| 236 | 235 | } else { |
| 237 | - if ($header['content_length'] > 0) { |
|
| 238 | - $this->reader->bytes($header['content_length']); |
|
| 236 | + if ($header[ 'content_length' ] > 0) { |
|
| 237 | + $this->reader->bytes($header[ 'content_length' ]); |
|
| 239 | 238 | } |
| 240 | 239 | } |
| 241 | 240 | } |
| 242 | - if (!isset($header['children'])) { |
|
| 241 | + if (!isset($header[ 'children' ])) { |
|
| 243 | 242 | $pos = $this->reader->pos(); |
| 244 | - $header['value'] = $this->decode($header); |
|
| 243 | + $header[ 'value' ] = $this->decode($header); |
|
| 245 | 244 | $this->reader->seek($pos); |
| 246 | 245 | } |
| 247 | - $skeleton[] = $header; |
|
| 246 | + $skeleton[ ] = $header; |
|
| 248 | 247 | } |
| 249 | 248 | } |
| 250 | 249 | return $skeleton; |
@@ -259,10 +258,10 @@ discard block |
||
| 259 | 258 | { |
| 260 | 259 | $skeleton = $skeleton ?? $this->structure(); |
| 261 | 260 | foreach ($skeleton as $k => $v) { |
| 262 | - if (isset($v['children'])) { |
|
| 263 | - $skeleton[$k] = $this->values($v['children']); |
|
| 261 | + if (isset($v[ 'children' ])) { |
|
| 262 | + $skeleton[ $k ] = $this->values($v[ 'children' ]); |
|
| 264 | 263 | } else { |
| 265 | - $skeleton[$k] = $v['value'] ?? null; |
|
| 264 | + $skeleton[ $k ] = $v[ 'value' ] ?? null; |
|
| 266 | 265 | } |
| 267 | 266 | } |
| 268 | 267 | return $skeleton; |
@@ -280,113 +279,113 @@ discard block |
||
| 280 | 279 | if ($skeleton === null && $this->reader->pos() !== 0) { |
| 281 | 280 | $this->reader->rewind(); |
| 282 | 281 | } |
| 283 | - $skeleton = $skeleton ?? $this->structure()[0] ?? null; |
|
| 282 | + $skeleton = $skeleton ?? $this->structure()[ 0 ] ?? null; |
|
| 284 | 283 | if (!isset($skeleton)) { |
| 285 | 284 | throw new ASN1Exception('No decoded data for map'); |
| 286 | 285 | } |
| 287 | - if ($skeleton['class'] !== ASN1::CLASS_UNIVERSAL) { |
|
| 288 | - if ($map['tag'] === ASN1::TYPE_CHOICE) { |
|
| 289 | - foreach ($map['children'] as $child) { |
|
| 290 | - if (isset($child['name']) && (int)$skeleton['tag'] === (int)$child['name']) { |
|
| 286 | + if ($skeleton[ 'class' ] !== ASN1::CLASS_UNIVERSAL) { |
|
| 287 | + if ($map[ 'tag' ] === ASN1::TYPE_CHOICE) { |
|
| 288 | + foreach ($map[ 'children' ] as $child) { |
|
| 289 | + if (isset($child[ 'name' ]) && (int) $skeleton[ 'tag' ] === (int) $child[ 'name' ]) { |
|
| 291 | 290 | $map = $child; |
| 292 | - if (isset($child['value']) && $child['value']) { |
|
| 293 | - return $child['value']; |
|
| 291 | + if (isset($child[ 'value' ]) && $child[ 'value' ]) { |
|
| 292 | + return $child[ 'value' ]; |
|
| 294 | 293 | } |
| 295 | 294 | break; |
| 296 | 295 | } |
| 297 | 296 | } |
| 298 | 297 | } |
| 299 | 298 | } |
| 300 | - if ($skeleton['class'] !== ASN1::CLASS_UNIVERSAL) { |
|
| 301 | - if (isset($map['implicit']) && $map['implicit']) { |
|
| 302 | - $skeleton['class'] = ASN1::CLASS_UNIVERSAL; |
|
| 303 | - $skeleton['tag'] = $map['tag']; |
|
| 299 | + if ($skeleton[ 'class' ] !== ASN1::CLASS_UNIVERSAL) { |
|
| 300 | + if (isset($map[ 'implicit' ]) && $map[ 'implicit' ]) { |
|
| 301 | + $skeleton[ 'class' ] = ASN1::CLASS_UNIVERSAL; |
|
| 302 | + $skeleton[ 'tag' ] = $map[ 'tag' ]; |
|
| 304 | 303 | } else { |
| 305 | - $skeleton = $skeleton['children'][0] ?? null; |
|
| 304 | + $skeleton = $skeleton[ 'children' ][ 0 ] ?? null; |
|
| 306 | 305 | } |
| 307 | 306 | } |
| 308 | - if ($map['tag'] === ASN1::TYPE_CHOICE) { |
|
| 309 | - foreach ($map['children'] as $child) { |
|
| 310 | - if ($skeleton['tag'] === $child['tag']) { |
|
| 307 | + if ($map[ 'tag' ] === ASN1::TYPE_CHOICE) { |
|
| 308 | + foreach ($map[ 'children' ] as $child) { |
|
| 309 | + if ($skeleton[ 'tag' ] === $child[ 'tag' ]) { |
|
| 311 | 310 | $map = $child; |
| 312 | - if (isset($child['value']) && $child['value']) { |
|
| 313 | - return $child['value']; |
|
| 311 | + if (isset($child[ 'value' ]) && $child[ 'value' ]) { |
|
| 312 | + return $child[ 'value' ]; |
|
| 314 | 313 | } |
| 315 | 314 | break; |
| 316 | 315 | } |
| 317 | 316 | } |
| 318 | 317 | } |
| 319 | - if (in_array($map['tag'], [ASN1::TYPE_SEQUENCE, ASN1::TYPE_SET]) && |
|
| 320 | - in_array($skeleton['tag'], [ASN1::TYPE_SEQUENCE, ASN1::TYPE_SET])) { |
|
| 321 | - $map['tag'] = $skeleton['tag']; |
|
| 318 | + if (in_array($map[ 'tag' ], [ ASN1::TYPE_SEQUENCE, ASN1::TYPE_SET ]) && |
|
| 319 | + in_array($skeleton[ 'tag' ], [ ASN1::TYPE_SEQUENCE, ASN1::TYPE_SET ])) { |
|
| 320 | + $map[ 'tag' ] = $skeleton[ 'tag' ]; |
|
| 322 | 321 | } |
| 323 | - if ($map['tag'] === ASN1::TYPE_ANY && isset($skeleton['tag'])) { |
|
| 324 | - $map['tag'] = $skeleton['tag']; |
|
| 322 | + if ($map[ 'tag' ] === ASN1::TYPE_ANY && isset($skeleton[ 'tag' ])) { |
|
| 323 | + $map[ 'tag' ] = $skeleton[ 'tag' ]; |
|
| 325 | 324 | } |
| 326 | - if (!in_array($map['tag'], [ASN1::TYPE_ANY, ASN1::TYPE_ANY_RAW, ASN1::TYPE_ANY_SKIP, ASN1::TYPE_ANY_DER]) && |
|
| 327 | - $map['tag'] !== $skeleton['tag'] |
|
| 325 | + if (!in_array($map[ 'tag' ], [ ASN1::TYPE_ANY, ASN1::TYPE_ANY_RAW, ASN1::TYPE_ANY_SKIP, ASN1::TYPE_ANY_DER ]) && |
|
| 326 | + $map[ 'tag' ] !== $skeleton[ 'tag' ] |
|
| 328 | 327 | ) { |
| 329 | - if (!isset($map['optional']) || !$map['optional']) { |
|
| 330 | - throw new ASN1Exception('Decoded data does not match mapping - ' . $skeleton['tag']); |
|
| 328 | + if (!isset($map[ 'optional' ]) || !$map[ 'optional' ]) { |
|
| 329 | + throw new ASN1Exception('Decoded data does not match mapping - '.$skeleton[ 'tag' ]); |
|
| 331 | 330 | } |
| 332 | 331 | return $null; |
| 333 | 332 | } else { |
| 334 | - switch ($map['tag']) { |
|
| 333 | + switch ($map[ 'tag' ]) { |
|
| 335 | 334 | case ASN1::TYPE_ANY_DER: |
| 336 | - $temp = $this->reader->chunk($skeleton['start'], $skeleton['length']); |
|
| 335 | + $temp = $this->reader->chunk($skeleton[ 'start' ], $skeleton[ 'length' ]); |
|
| 337 | 336 | return $temp; |
| 338 | 337 | case ASN1::TYPE_ANY_SKIP: |
| 339 | 338 | return $null; |
| 340 | 339 | case ASN1::TYPE_ANY_RAW: |
| 341 | - return $skeleton['value'] ?? null; |
|
| 340 | + return $skeleton[ 'value' ] ?? null; |
|
| 342 | 341 | case ASN1::TYPE_SET: |
| 343 | - if (isset($map['repeat'])) { |
|
| 344 | - $result = []; |
|
| 345 | - foreach ($skeleton['children'] as $v) { |
|
| 346 | - $result[] = $this->map($map['repeat'], $v); |
|
| 342 | + if (isset($map[ 'repeat' ])) { |
|
| 343 | + $result = [ ]; |
|
| 344 | + foreach ($skeleton[ 'children' ] as $v) { |
|
| 345 | + $result[ ] = $this->map($map[ 'repeat' ], $v); |
|
| 347 | 346 | } |
| 348 | 347 | return $result; |
| 349 | 348 | } else { |
| 350 | - if (!isset($map['children'])) { |
|
| 349 | + if (!isset($map[ 'children' ])) { |
|
| 351 | 350 | return $null; |
| 352 | 351 | } |
| 353 | - $temp = $skeleton['children']; |
|
| 354 | - $result = []; |
|
| 352 | + $temp = $skeleton[ 'children' ]; |
|
| 353 | + $result = [ ]; |
|
| 355 | 354 | // named first |
| 356 | - foreach ($map['children'] as $k => $v) { |
|
| 357 | - if (isset($v['name'])) { |
|
| 358 | - $result[$k] = null; |
|
| 355 | + foreach ($map[ 'children' ] as $k => $v) { |
|
| 356 | + if (isset($v[ 'name' ])) { |
|
| 357 | + $result[ $k ] = null; |
|
| 359 | 358 | foreach ($temp as $kk => $vv) { |
| 360 | - if ($vv['class'] !== ASN1::CLASS_UNIVERSAL && (int)$v['name'] === $vv['tag']) { |
|
| 359 | + if ($vv[ 'class' ] !== ASN1::CLASS_UNIVERSAL && (int) $v[ 'name' ] === $vv[ 'tag' ]) { |
|
| 361 | 360 | try { |
| 362 | - if (isset($v['implicit']) && $v['implicit']) { |
|
| 363 | - $vv['class'] = ASN1::CLASS_UNIVERSAL; |
|
| 364 | - $vv['tag'] = $map['tag']; |
|
| 361 | + if (isset($v[ 'implicit' ]) && $v[ 'implicit' ]) { |
|
| 362 | + $vv[ 'class' ] = ASN1::CLASS_UNIVERSAL; |
|
| 363 | + $vv[ 'tag' ] = $map[ 'tag' ]; |
|
| 365 | 364 | } else { |
| 366 | - $vv = $vv['children'][0] ?? null; |
|
| 365 | + $vv = $vv[ 'children' ][ 0 ] ?? null; |
|
| 367 | 366 | } |
| 368 | - $result[$k] = $this->map($v, $vv); |
|
| 369 | - unset($temp[$kk]); |
|
| 367 | + $result[ $k ] = $this->map($v, $vv); |
|
| 368 | + unset($temp[ $kk ]); |
|
| 370 | 369 | break; |
| 371 | 370 | } catch (ASN1Exception $e) { |
| 372 | 371 | // continue trying other children in case of failure |
| 373 | 372 | } |
| 374 | 373 | } |
| 375 | 374 | } |
| 376 | - if ($result[$k] === null && (!isset($v['optional']) || !$v['optional'])) { |
|
| 377 | - throw new ASN1Exception('Missing tagged type - ' . $k); |
|
| 375 | + if ($result[ $k ] === null && (!isset($v[ 'optional' ]) || !$v[ 'optional' ])) { |
|
| 376 | + throw new ASN1Exception('Missing tagged type - '.$k); |
|
| 378 | 377 | } |
| 379 | 378 | } |
| 380 | 379 | } |
| 381 | - foreach ($map['children'] as $k => $v) { |
|
| 382 | - if (isset($v['name'])) { |
|
| 380 | + foreach ($map[ 'children' ] as $k => $v) { |
|
| 381 | + if (isset($v[ 'name' ])) { |
|
| 383 | 382 | continue; |
| 384 | 383 | } |
| 385 | - $result[$k] = null; |
|
| 384 | + $result[ $k ] = null; |
|
| 386 | 385 | foreach ($temp as $kk => $vv) { |
| 387 | - if ($v['tag'] === $vv['tag'] || |
|
| 386 | + if ($v[ 'tag' ] === $vv[ 'tag' ] || |
|
| 388 | 387 | in_array( |
| 389 | - $v['tag'], |
|
| 388 | + $v[ 'tag' ], |
|
| 390 | 389 | [ |
| 391 | 390 | ASN1::TYPE_ANY, |
| 392 | 391 | ASN1::TYPE_ANY_DER, |
@@ -397,53 +396,53 @@ discard block |
||
| 397 | 396 | ) |
| 398 | 397 | ) { |
| 399 | 398 | try { |
| 400 | - $result[$k] = $this->map($v, $vv); |
|
| 401 | - unset($temp[$kk]); |
|
| 399 | + $result[ $k ] = $this->map($v, $vv); |
|
| 400 | + unset($temp[ $kk ]); |
|
| 402 | 401 | break; |
| 403 | 402 | } catch (ASN1Exception $e) { |
| 404 | - $result[$k] = null; |
|
| 403 | + $result[ $k ] = null; |
|
| 405 | 404 | } |
| 406 | 405 | } |
| 407 | 406 | } |
| 408 | - if ($result[$k] === null && (!isset($v['optional']) || !$v['optional'])) { |
|
| 409 | - throw new ASN1Exception('Decoded data does not match mapping - ' . $k); |
|
| 407 | + if ($result[ $k ] === null && (!isset($v[ 'optional' ]) || !$v[ 'optional' ])) { |
|
| 408 | + throw new ASN1Exception('Decoded data does not match mapping - '.$k); |
|
| 410 | 409 | } |
| 411 | 410 | } |
| 412 | 411 | return $result; |
| 413 | 412 | } |
| 414 | 413 | break; |
| 415 | 414 | case ASN1::TYPE_SEQUENCE: |
| 416 | - if (isset($map['repeat'])) { |
|
| 417 | - $result = []; |
|
| 418 | - foreach ($skeleton['children'] as $v) { |
|
| 419 | - $result[] = $this->map($map['repeat'], $v); |
|
| 415 | + if (isset($map[ 'repeat' ])) { |
|
| 416 | + $result = [ ]; |
|
| 417 | + foreach ($skeleton[ 'children' ] as $v) { |
|
| 418 | + $result[ ] = $this->map($map[ 'repeat' ], $v); |
|
| 420 | 419 | } |
| 421 | 420 | return $result; |
| 422 | 421 | } else { |
| 423 | - if (!isset($map['children'])) { |
|
| 422 | + if (!isset($map[ 'children' ])) { |
|
| 424 | 423 | return $null; |
| 425 | 424 | } |
| 426 | - $result = []; |
|
| 427 | - foreach ($skeleton['children'] as $vv) { |
|
| 428 | - foreach ($map['children'] as $k => $v) { |
|
| 429 | - if (isset($v['name']) && $vv['class'] !== ASN1::CLASS_UNIVERSAL && |
|
| 430 | - (int)$v['name'] === $vv['tag'] |
|
| 425 | + $result = [ ]; |
|
| 426 | + foreach ($skeleton[ 'children' ] as $vv) { |
|
| 427 | + foreach ($map[ 'children' ] as $k => $v) { |
|
| 428 | + if (isset($v[ 'name' ]) && $vv[ 'class' ] !== ASN1::CLASS_UNIVERSAL && |
|
| 429 | + (int) $v[ 'name' ] === $vv[ 'tag' ] |
|
| 431 | 430 | ) { |
| 432 | - if (isset($v['implicit']) && $v['implicit']) { |
|
| 433 | - $vv['class'] = ASN1::CLASS_UNIVERSAL; |
|
| 434 | - $vv['tag'] = $map['tag']; |
|
| 431 | + if (isset($v[ 'implicit' ]) && $v[ 'implicit' ]) { |
|
| 432 | + $vv[ 'class' ] = ASN1::CLASS_UNIVERSAL; |
|
| 433 | + $vv[ 'tag' ] = $map[ 'tag' ]; |
|
| 435 | 434 | } else { |
| 436 | - $vv = $vv['children'][0] ?? null; |
|
| 435 | + $vv = $vv[ 'children' ][ 0 ] ?? null; |
|
| 437 | 436 | } |
| 438 | - $result[$k] = $this->map($v, $vv); |
|
| 439 | - unset($map['children'][$k]); |
|
| 437 | + $result[ $k ] = $this->map($v, $vv); |
|
| 438 | + unset($map[ 'children' ][ $k ]); |
|
| 440 | 439 | break; |
| 441 | 440 | } |
| 442 | - if (!isset($v['name']) && |
|
| 441 | + if (!isset($v[ 'name' ]) && |
|
| 443 | 442 | ( |
| 444 | - $v['tag'] === $vv['tag'] || |
|
| 443 | + $v[ 'tag' ] === $vv[ 'tag' ] || |
|
| 445 | 444 | in_array( |
| 446 | - $v['tag'], |
|
| 445 | + $v[ 'tag' ], |
|
| 447 | 446 | [ |
| 448 | 447 | ASN1::TYPE_ANY, |
| 449 | 448 | ASN1::TYPE_ANY_DER, |
@@ -456,18 +455,18 @@ discard block |
||
| 456 | 455 | ) { |
| 457 | 456 | try { |
| 458 | 457 | $temp = $this->map($v, $vv); |
| 459 | - $result[$k] = $temp; |
|
| 460 | - unset($map['children'][$k]); |
|
| 458 | + $result[ $k ] = $temp; |
|
| 459 | + unset($map[ 'children' ][ $k ]); |
|
| 461 | 460 | break; |
| 462 | 461 | } catch (ASN1Exception $e) { |
| 463 | 462 | // continue trying other children in case of failure |
| 464 | 463 | } |
| 465 | 464 | } |
| 466 | - if (!isset($v['optional']) || !$v['optional']) { |
|
| 467 | - throw new ASN1Exception('Missing type - ' . $k); |
|
| 465 | + if (!isset($v[ 'optional' ]) || !$v[ 'optional' ]) { |
|
| 466 | + throw new ASN1Exception('Missing type - '.$k); |
|
| 468 | 467 | } else { |
| 469 | - $result[$k] = null; |
|
| 470 | - unset($map['children'][$k]); |
|
| 468 | + $result[ $k ] = null; |
|
| 469 | + unset($map[ 'children' ][ $k ]); |
|
| 471 | 470 | } |
| 472 | 471 | } |
| 473 | 472 | } |
@@ -475,52 +474,50 @@ discard block |
||
| 475 | 474 | } |
| 476 | 475 | break; |
| 477 | 476 | case ASN1::TYPE_OBJECT_IDENTIFIER: |
| 478 | - $temp = isset($map['resolve']) && $map['resolve'] ? |
|
| 479 | - ASN1::OIDtoText($skeleton['value']) : |
|
| 480 | - $skeleton['value']; |
|
| 477 | + $temp = isset($map[ 'resolve' ]) && $map[ 'resolve' ] ? |
|
| 478 | + ASN1::OIDtoText($skeleton[ 'value' ]) : $skeleton[ 'value' ]; |
|
| 481 | 479 | return $temp; |
| 482 | 480 | case ASN1::TYPE_OCTET_STRING: |
| 483 | - if (isset($map['der']) && $map['der']) { |
|
| 484 | - $temp = static::fromString($skeleton['value']); |
|
| 485 | - $temp = isset($map['map']) ? $temp->map($map['map']) : $temp->values(); |
|
| 481 | + if (isset($map[ 'der' ]) && $map[ 'der' ]) { |
|
| 482 | + $temp = static::fromString($skeleton[ 'value' ]); |
|
| 483 | + $temp = isset($map[ 'map' ]) ? $temp->map($map[ 'map' ]) : $temp->values(); |
|
| 486 | 484 | return $temp; |
| 487 | 485 | } else { |
| 488 | - $temp = isset($map['raw']) && $map['raw'] ? |
|
| 489 | - $skeleton['value'] : |
|
| 490 | - base64_encode($skeleton['value']); |
|
| 486 | + $temp = isset($map[ 'raw' ]) && $map[ 'raw' ] ? |
|
| 487 | + $skeleton[ 'value' ] : base64_encode($skeleton[ 'value' ]); |
|
| 491 | 488 | return $temp; |
| 492 | 489 | } |
| 493 | 490 | break; |
| 494 | 491 | case ASN1::TYPE_INTEGER: |
| 495 | - $base = isset($map['base']) && (int)$map['base'] ? (int)$map['base'] : 10; |
|
| 492 | + $base = isset($map[ 'base' ]) && (int) $map[ 'base' ] ? (int) $map[ 'base' ] : 10; |
|
| 496 | 493 | if ($base < 3) { |
| 497 | - $result = $skeleton['value']; |
|
| 494 | + $result = $skeleton[ 'value' ]; |
|
| 498 | 495 | } else { |
| 499 | - if (strlen($skeleton['value']) > 53 && $base === 16) { |
|
| 496 | + if (strlen($skeleton[ 'value' ]) > 53 && $base === 16) { |
|
| 500 | 497 | $hex = ''; |
| 501 | - for ($i = strlen($skeleton['value']) - 4; $i >= 0; $i-=4) { |
|
| 502 | - $hex .= dechex((int)bindec(substr($skeleton['value'], $i, 4))); |
|
| 498 | + for ($i = strlen($skeleton[ 'value' ]) - 4; $i >= 0; $i -= 4) { |
|
| 499 | + $hex .= dechex((int) bindec(substr($skeleton[ 'value' ], $i, 4))); |
|
| 503 | 500 | } |
| 504 | 501 | $result = strrev($hex); |
| 505 | 502 | } else { |
| 506 | - $temp = base_convert($skeleton['value'], 2, $base); |
|
| 503 | + $temp = base_convert($skeleton[ 'value' ], 2, $base); |
|
| 507 | 504 | if ($base === 10) { |
| 508 | - $temp = (int)$temp; |
|
| 505 | + $temp = (int) $temp; |
|
| 509 | 506 | } |
| 510 | 507 | $result = $temp; |
| 511 | 508 | } |
| 512 | 509 | } |
| 513 | - if (isset($map['map']) && isset($map['map'][$result])) { |
|
| 514 | - $result = $map['map'][$result]; |
|
| 510 | + if (isset($map[ 'map' ]) && isset($map[ 'map' ][ $result ])) { |
|
| 511 | + $result = $map[ 'map' ][ $result ]; |
|
| 515 | 512 | } |
| 516 | 513 | return $result; |
| 517 | 514 | case ASN1::TYPE_UTC_TIME: |
| 518 | 515 | case ASN1::TYPE_GENERALIZED_TIME: |
| 519 | - return $skeleton['value']; |
|
| 516 | + return $skeleton[ 'value' ]; |
|
| 520 | 517 | default: |
| 521 | - $result = $skeleton['value']; |
|
| 522 | - if (isset($map['map']) && isset($map['map'][$result])) { |
|
| 523 | - $result = $map['map'][$result]; |
|
| 518 | + $result = $skeleton[ 'value' ]; |
|
| 519 | + if (isset($map[ 'map' ]) && isset($map[ 'map' ][ $result ])) { |
|
| 520 | + $result = $map[ 'map' ][ $result ]; |
|
| 524 | 521 | } |
| 525 | 522 | return $result; |
| 526 | 523 | } |