Index: trunk/extensions/TorBlock/ASN1Parser.php |
— | — | @@ -18,7 +18,7 @@ |
19 | 19 | 22 => 'IA5String', |
20 | 20 | 23 => 'UTCTime' |
21 | 21 | ); |
22 | | - |
| 22 | + |
23 | 23 | const INTEGER = 2; |
24 | 24 | const BIT_STRING = 3; |
25 | 25 | const OCTET_STRING = 4; |
— | — | @@ -30,43 +30,43 @@ |
31 | 31 | const T61String = 20; |
32 | 32 | const IA5String = 22; |
33 | 33 | const UTCTime = 23; |
34 | | - |
| 34 | + |
35 | 35 | static function encodeLength($number) { |
36 | 36 | if ($number < 128) |
37 | 37 | return chr($number); |
38 | | - |
| 38 | + |
39 | 39 | $out = pack("N*", $number); |
40 | 40 | $out = ltrim( $out, "\0" ); |
41 | | - |
| 41 | + |
42 | 42 | return chr( 0x80 | strlen($out) ) . $out; |
43 | 43 | } |
44 | 44 | |
45 | | - static function decode($buffer) { |
| 45 | + static function decode($buffer) { |
46 | 46 | if ( strlen( $buffer ) < 2 ) |
47 | 47 | throw new ASN1Exception( 'ASN1 string is too short' ); |
48 | | - |
| 48 | + |
49 | 49 | $i = 0; |
50 | 50 | $result = array(); |
51 | 51 | while ( $i < strlen( $buffer ) ) { |
52 | 52 | $item = array(); |
53 | | - |
| 53 | + |
54 | 54 | $tag = ord( $buffer[$i] ); |
55 | 55 | $item['tag-class'] = self::$tagClasses[$tag >> 6]; |
56 | | - |
| 56 | + |
57 | 57 | $i++; |
58 | 58 | $constructed = $tag & 0x20; // Primitive/Constructed bit |
59 | 59 | $tag &= 0x1f; |
60 | | - |
| 60 | + |
61 | 61 | if ( $tag != 0x1f ) { |
62 | 62 | // Great! it's in one octet |
63 | | - |
| 63 | + |
64 | 64 | $item['tag'] = $tag; |
65 | 65 | } else { |
66 | 66 | $tag = 0; |
67 | 67 | for (; $i < strlen($buffer); $i++) { |
68 | 68 | $t = ord( $buffer[$i] ); |
69 | 69 | $tag = ( $tag << 7 ) | ( $t & 0x7f ); |
70 | | - |
| 70 | + |
71 | 71 | // The last octet of the tag identifier will have the high-bit set to 0 |
72 | 72 | if ( ( $t & 0x80 ) == 0 ) { |
73 | 73 | break; |
— | — | @@ -78,11 +78,11 @@ |
79 | 79 | $item['tag'] = $tag; |
80 | 80 | $i++; |
81 | 81 | } |
82 | | - |
| 82 | + |
83 | 83 | /* Parse length */ |
84 | 84 | $length = ord( $buffer[$i] ); |
85 | 85 | $i++; |
86 | | - |
| 86 | + |
87 | 87 | if ( ( $length & 0x80 ) == 0 ) { |
88 | 88 | $item['length'] = $length; |
89 | 89 | } else { |
— | — | @@ -91,7 +91,7 @@ |
92 | 92 | if ( strlen( $lengthBytes ) != $l ) { |
93 | 93 | throw new ASN1Exception( 'Not enough bytes for long-form length' ); |
94 | 94 | } |
95 | | - |
| 95 | + |
96 | 96 | $length = 0; |
97 | 97 | for ($j = 0; $j < $l; $j++, $i++) { |
98 | 98 | $length = ( $length << 8 ) | ord( $lengthBytes[$j] ); |
— | — | @@ -100,41 +100,41 @@ |
101 | 101 | throw new ASN1Exception( 'Overflow calculating length' ); |
102 | 102 | } |
103 | 103 | $item['length'] = $length; |
104 | | - |
| 104 | + |
105 | 105 | } |
106 | 106 | $item['contents'] = substr( $buffer, $i, $length ); |
107 | 107 | $i += $length; |
108 | | - |
| 108 | + |
109 | 109 | if ( $constructed ) { |
110 | 110 | $item['contents'] = self::decode( $item['contents'] ); |
111 | 111 | } elseif ( $tag == 6 ) { |
112 | 112 | /* We could show the pretty name here instead of the hex dump |
113 | | - * |
| 113 | + * |
114 | 114 | * a source could be crypto/objects/objects.txt from openssl |
115 | | - * project or crypto/objects/obj_dat.txt (where hexadecimal |
| 115 | + * project or crypto/objects/obj_dat.txt (where hexadecimal |
116 | 116 | * data is available ) |
117 | 117 | */ |
118 | 118 | $item['contents'] = $item['contents']; |
119 | 119 | } else { |
120 | 120 | $item['contents'] = $item['contents']; |
121 | 121 | } |
122 | | - |
| 122 | + |
123 | 123 | $result[] = $item; |
124 | 124 | } |
125 | | - |
| 125 | + |
126 | 126 | return count ( $result ) > 1 ? $result : $result[0]; |
127 | 127 | } |
128 | | - |
| 128 | + |
129 | 129 | /** |
130 | 130 | * Prettifies an array output by decode() |
131 | 131 | */ |
132 | 132 | static function prettyDecode($decodedArray) { |
133 | 133 | $decoded = $decodedArray; |
134 | 134 | array_walk_recursive($decoded, array( __CLASS__, 'prettyItem' ) ); |
135 | | - |
| 135 | + |
136 | 136 | return $decoded; |
137 | 137 | } |
138 | | - |
| 138 | + |
139 | 139 | static protected function prettyItem(&$value, $key) { |
140 | 140 | switch ($key) { |
141 | 141 | case 'contents': // Not called when contents is an array |
— | — | @@ -147,7 +147,7 @@ |
148 | 148 | break; |
149 | 149 | } |
150 | 150 | } |
151 | | - |
| 151 | + |
152 | 152 | static function buildTag($tagId, $contents) { |
153 | 153 | if ( is_int( $tagId ) && $tagId < 31 ) { |
154 | 154 | if ( $tagId == self::SEQUENCE || $tagId == self::SET ) { |
— | — | @@ -159,7 +159,7 @@ |
160 | 160 | } |
161 | 161 | $out .= self::encodeLength( strlen( $contents ) ); |
162 | 162 | $out .= $contents; |
163 | | - |
| 163 | + |
164 | 164 | return $out; |
165 | 165 | } |
166 | 166 | } |