Rev 48 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
35 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
48 | daniel-mar | 4 | * ISO/IEC 7816 Application Identifier decoder for PHP |
35 | daniel-mar | 5 | * Copyright 2022 Daniel Marschall, ViaThinkSoft |
49 | daniel-mar | 6 | * Version 2022-09-20 |
35 | daniel-mar | 7 | * |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
9 | * you may not use this file except in compliance with the License. |
||
10 | * You may obtain a copy of the License at |
||
11 | * |
||
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
13 | * |
||
14 | * Unless required by applicable law or agreed to in writing, software |
||
15 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
17 | * See the License for the specific language governing permissions and |
||
18 | * limitations under the License. |
||
19 | */ |
||
20 | |||
44 | daniel-mar | 21 | include_once __DIR__ . '/gmp_supplement.inc.php'; |
40 | daniel-mar | 22 | include_once __DIR__ . '/misc_functions.inc.php'; |
23 | |||
24 | # --- |
||
25 | |||
35 | daniel-mar | 26 | /* |
45 | daniel-mar | 27 | #test2('A000000051AABBCC'); // International Registration |
28 | #test2('B01234567890'); // Illegal AID (RFU) |
||
29 | #test2('D276000098AABBCCDDEEFFAABBCCDDEE'); // National Registration |
||
30 | #test2('F01234567890'); // Unregistered AID |
||
31 | #test('91234FFF999'); // IIN based AID |
||
32 | #test('51234FFF999'); // IIN based AID |
||
33 | test('E828BD080F014E585031'); // ISO E8-OID 1.0.aaaa |
||
34 | test('E80704007F00070304'); // BSI Illegal E8-OID-AID (with DER Length) |
||
35 | test('E80704007F0007030499'); // BSI Illegal E8-OID-AID + PIX (PIX is never used by BSI; it's just for us to test) |
||
46 | daniel-mar | 36 | test('E829112233'); // Possible other illegal E8-OID |
35 | daniel-mar | 37 | |
38 | function test2($aid) { |
||
39 | while ($aid != '') { |
||
40 | echo test($aid); |
||
41 | $aid = substr($aid,0,strlen($aid)-1); |
||
42 | } |
||
43 | } |
||
44 | |||
45 | function test($aid) { |
||
46 | $out = _decode_aid($aid); |
||
47 | $max_key_len = 32; // min width of first column |
||
48 | foreach ($out as $a) { |
||
49 | if (is_array($a)) { |
||
50 | $max_key_len = max($max_key_len,strlen($a[0])); |
||
51 | } |
||
52 | } |
||
53 | foreach ($out as $a) { |
||
54 | if (is_array($a)) { |
||
55 | echo str_pad($a[0],$max_key_len,' ',STR_PAD_RIGHT).' '.$a[1]."\n"; |
||
56 | } else { |
||
57 | echo $a."\n"; |
||
58 | } |
||
59 | } |
||
60 | echo "\n"; |
||
61 | } |
||
62 | */ |
||
63 | |||
64 | # --- |
||
65 | |||
45 | daniel-mar | 66 | function _aid_e8_oid_helper($output_oid,$by,$ii,&$ret,$minmax_measure,$min,$max) { |
67 | if ($minmax_measure == 'ARC') { |
||
68 | if (($min!=-1) && (count($output_oid)<$min)) return true; // continue |
||
69 | if (($max!=-1) && (count($output_oid)>$max)) return false; // stop |
||
70 | } else if ($minmax_measure == 'DER') { |
||
71 | if (($min!=-1) && ($ii+1<$min)) return true; // continue |
||
72 | if (($max!=-1) && ($ii+1>$max)) return false; // stop |
||
73 | } |
||
74 | |||
75 | $byy = $by; |
||
76 | for ($i=0;$i<=$ii;$i++) array_shift($byy); |
||
77 | |||
78 | $is_iso_standard = (count($output_oid) >= 3) && ($output_oid[0] == '1') && ($output_oid[1] == '0'); |
||
79 | |||
80 | $s_oid = implode('.',$output_oid); |
||
81 | |||
82 | if ($is_iso_standard) { |
||
83 | $std_hf = 'Standard ISO/IEC '.$output_oid[2]; |
||
84 | if (isset($output_oid[3])) $std_hf .= "-".$output_oid[3]; |
||
85 | } else { |
||
86 | $std_hf = "Unknown Standard"; // should not happen |
||
87 | } |
||
88 | |||
89 | $pix = implode(':',$byy); |
||
90 | |||
91 | if ($pix !== '') { |
||
92 | $ret[] = array($ii+1,"$std_hf (OID $s_oid)",$pix); |
||
93 | } else { |
||
94 | $ret[] = array($ii+1,"$std_hf (OID $s_oid)",""); |
||
95 | } |
||
96 | |||
97 | return true; |
||
98 | } |
||
99 | |||
100 | function _aid_e8_interpretations($pure_der, $minmax_measure='ARC', $min=-1, $max=-1) { |
||
40 | daniel-mar | 101 | $ret = array(); |
102 | |||
103 | $output_oid = array(); |
||
104 | |||
45 | daniel-mar | 105 | $pure_der = strtoupper(str_replace(' ','',$pure_der)); |
106 | $pure_der = strtoupper(str_replace(':','',$pure_der)); |
||
107 | $by = str_split($pure_der,2); |
||
40 | daniel-mar | 108 | |
45 | daniel-mar | 109 | // The following part is partially taken from the DER decoder/encoder by Daniel Marschall: |
110 | // https://github.com/danielmarschall/oidconverter/blob/master/php/OidDerConverter.class.phps |
||
111 | // (Only the DER "value" part, without "type" and "length") |
||
40 | daniel-mar | 112 | |
45 | daniel-mar | 113 | $part = 2; // DER part 0 (type) and part 1 (length) not present |
114 | $fSub = 0; // Subtract value from next number output. Used when encoding {2 48} and up |
||
115 | $ll = gmp_init(0); |
||
116 | $arcBeginning = true; |
||
40 | daniel-mar | 117 | |
45 | daniel-mar | 118 | foreach ($by as $ii => $pb) { |
40 | daniel-mar | 119 | |
45 | daniel-mar | 120 | $pb = hexdec($pb); |
40 | daniel-mar | 121 | |
45 | daniel-mar | 122 | if ($part == 2) { // First two arcs |
123 | $first = floor($pb / 40); |
||
124 | $second = $pb % 40; |
||
125 | if ($first > 2) { |
||
126 | $first = 2; |
||
127 | $output_oid[] = $first; |
||
128 | if (!_aid_e8_oid_helper($output_oid, $by, $ii, $ret, $minmax_measure, $min, $max)) break; |
||
129 | $arcBeginning = true; |
||
40 | daniel-mar | 130 | |
45 | daniel-mar | 131 | if (($pb & 0x80) != 0) { |
132 | // 2.48 and up => 2+ octets |
||
133 | // Output in "part 3" |
||
40 | daniel-mar | 134 | |
45 | daniel-mar | 135 | if ($pb == 0x80) { |
136 | throw new Exception("Encoding error. Illegal 0x80 paddings. (See Rec. ITU-T X.690, clause 8.19.2)\n"); |
||
40 | daniel-mar | 137 | } else { |
45 | daniel-mar | 138 | $arcBeginning = false; |
40 | daniel-mar | 139 | } |
45 | daniel-mar | 140 | |
141 | $ll = gmp_add($ll, ($pb & 0x7F)); |
||
142 | $fSub = 80; |
||
143 | $fOK = false; |
||
40 | daniel-mar | 144 | } else { |
45 | daniel-mar | 145 | // 2.0 till 2.47 => 1 octet |
146 | $second = $pb - 80; |
||
40 | daniel-mar | 147 | $output_oid[] = $second; |
45 | daniel-mar | 148 | if (!_aid_e8_oid_helper($output_oid, $by, $ii, $ret, $minmax_measure, $min, $max)) break; |
40 | daniel-mar | 149 | $arcBeginning = true; |
150 | $fOK = true; |
||
151 | $ll = gmp_init(0); |
||
152 | } |
||
45 | daniel-mar | 153 | } else { |
154 | // 0.0 till 0.37 => 1 octet |
||
155 | // 1.0 till 1.37 => 1 octet |
||
156 | $output_oid[] = $first; |
||
157 | $output_oid[] = $second; |
||
158 | if (!_aid_e8_oid_helper($output_oid, $by, $ii, $ret, $minmax_measure, $min, $max)) break; |
||
159 | $arcBeginning = true; |
||
160 | $fOK = true; |
||
161 | $ll = gmp_init(0); |
||
162 | } |
||
163 | $part++; |
||
164 | } else { //else if ($part == 3) { // Arc three and higher |
||
165 | if (($pb & 0x80) != 0) { |
||
166 | if ($arcBeginning && ($pb == 0x80)) { |
||
167 | throw new Exception("Encoding error. Illegal 0x80 paddings. (See Rec. ITU-T X.690, clause 8.19.2)"); |
||
40 | daniel-mar | 168 | } else { |
45 | daniel-mar | 169 | $arcBeginning = false; |
170 | } |
||
40 | daniel-mar | 171 | |
45 | daniel-mar | 172 | $ll = gmp_mul($ll, 0x80); |
173 | $ll = gmp_add($ll, ($pb & 0x7F)); |
||
174 | $fOK = false; |
||
175 | } else { |
||
176 | $fOK = true; |
||
177 | $ll = gmp_mul($ll, 0x80); |
||
178 | $ll = gmp_add($ll, $pb); |
||
179 | $ll = gmp_sub($ll, $fSub); |
||
180 | $output_oid[] = gmp_strval($ll, 10); |
||
40 | daniel-mar | 181 | |
45 | daniel-mar | 182 | if (!_aid_e8_oid_helper($output_oid, $by, $ii, $ret, $minmax_measure, $min, $max)) break; |
40 | daniel-mar | 183 | |
45 | daniel-mar | 184 | // Happens only if 0x80 paddings are allowed |
185 | // $fOK = gmp_cmp($ll, 0) >= 0; |
||
186 | $ll = gmp_init(0); |
||
187 | $fSub = 0; |
||
188 | $arcBeginning = true; |
||
40 | daniel-mar | 189 | } |
190 | } |
||
191 | } |
||
45 | daniel-mar | 192 | |
40 | daniel-mar | 193 | return $ret; |
194 | } |
||
195 | |||
46 | daniel-mar | 196 | function _aid_e8_length_usage($aid) { |
197 | // Return true if $aid is most likely E8+Length+OID (not intended by ISO) |
||
198 | // Return false if $aid is most likely E8+OID (defined by ISO for their OID 1.0) |
||
199 | // Return null if it is ambiguous |
||
200 | |||
201 | assert(substr($aid,0,2) === 'E8'); |
||
202 | $len = substr($aid,2,2); |
||
203 | $rest = substr($aid,4); |
||
204 | $rest_num_bytes = floor(strlen($rest)/2); |
||
205 | |||
206 | $is_e8_len_oid = false; |
||
207 | $is_e8_oid = false; |
||
208 | |||
209 | // There are not enough following bytes, so it cannot be E8+Length+OID. It must be E8+OID |
||
210 | if ($len > $rest_num_bytes) $is_e8_oid = true; |
||
211 | |||
212 | // E8 00 ... must be E8+OID, with OID 0.0.xx (recommendation), because Length=0 is not possible |
||
213 | if ($len == 0) $is_e8_oid = true; |
||
214 | |||
215 | // E8 01 ... must be E8+Length+OID, because OID 0.1 (question) was never used |
||
216 | if ($len == 1) $is_e8_len_oid = true; |
||
217 | |||
218 | // E8 02 refers to OID 0.2 (administration) but could also refer to a length |
||
219 | //if ($len == 2) return null; |
||
220 | |||
221 | // E8 03 refers to OID 0.3 (network-operator) but could also refer to a length |
||
222 | //if ($len == 3) return null; |
||
223 | |||
224 | // E8 04 refers to OID 0.4 (identified-organization) but could also refer to a length |
||
225 | //if ($len == 4) return null; |
||
226 | |||
227 | // E8 05 refers to OID 0.5 (r-recommendation) but could also refer to a length |
||
228 | //if ($len == 5) return null; |
||
229 | |||
230 | // E8 06-08 refers to OID 0.6-8, which are not defined, E8+Length+OID |
||
231 | if (($len >= 6) && ($len <= 8)) $is_e8_len_oid = true; |
||
232 | |||
233 | // E8 09 refers to OID 0.9, which can be an OID or a Length |
||
234 | if ($len == 9) { |
||
235 | // The only legal child of OID 0.9 is OID 0.9.2342 ($len=09, $rest=9226); then it is E8+OID |
||
236 | // An OID beginning with DER encoding 9226 would be 2.2262, which is very unlikely |
||
237 | if (substr($rest,0,4) === '9226') { |
||
238 | // 09 92 26 is OID 0.9.2342, which is a valid OID (the only valid OID) => valid OID |
||
239 | // 92 26 would be OID 2.2262 which is most likely not a valid OID => invalid Len |
||
240 | $is_e8_oid = true; |
||
241 | } else { |
||
242 | // Any other child inside 0.9 except for 2342 is illegal, so it must be length |
||
243 | $is_e8_len_oid = true; |
||
244 | } |
||
245 | } |
||
246 | |||
247 | // E8 10-14 refers to OID 0.10-14 which is not defined. Therefore it must be E8+Length+OID |
||
248 | if (($len >= 10) && ($len <= 14)) $is_e8_len_oid = true; |
||
249 | |||
250 | // If E8+Length+OID, then Len can max be 14, because E8 takes 1 byte, length takes 1 byte, and AID must be max 16 bytes |
||
251 | if ($len > 14) $is_e8_oid = true; |
||
252 | |||
253 | // There is at least one case where the usage of E8+Length+OID is known: |
||
254 | // Including the DER Encoding "Length" is not defined by ISO but used illegally |
||
255 | // by German BSI (beside the fact that ISO never allowed anyone else to use E8-AIDs outside |
||
256 | // of OID arc 1.0), |
||
257 | // e.g. AID E80704007F00070302 defined by "BSI TR-03110" was intended to represent 0.4.0.127.0.7.3.2 |
||
258 | // "more correct" would have been AID E804007F00070302 |
||
259 | // AID E80704007F00070304 defined by "BSI TR-03109-2" was intended to represent 0.4.0.127.0.7.3.4 |
||
260 | // "more correct" would have been AID E804007F00070304 |
||
47 | daniel-mar | 261 | if (substr($rest,0,10) == '04007F0007'/*0.4.0.127.0.7*/) $is_e8_len_oid = $len <= 14; |
46 | daniel-mar | 262 | |
263 | // Now conclude |
||
264 | if (!$is_e8_oid && $is_e8_len_oid) return true/*E8+Length+OID*/; |
||
265 | if ( $is_e8_oid && !$is_e8_len_oid) return false/*E8+OID*/; |
||
266 | return null/*ambiguous*/; |
||
267 | } |
||
268 | |||
35 | daniel-mar | 269 | function decode_aid($aid,$compact=true) { |
270 | $sout = ''; |
||
45 | daniel-mar | 271 | if (strtolower(substr($aid,0,2)) == '0x') $aid = substr($aid,2); |
35 | daniel-mar | 272 | $out = _decode_aid($aid); |
273 | if ($compact) { |
||
274 | $max_key_len = 0; |
||
275 | foreach ($out as $a) { |
||
276 | if (is_array($a)) { |
||
277 | $max_key_len = max($max_key_len,strlen($a[0])); |
||
278 | } |
||
279 | } |
||
280 | } else { |
||
281 | $max_key_len = 32; // 16 bytes |
||
282 | } |
||
283 | foreach ($out as $a) { |
||
284 | if (is_array($a)) { |
||
285 | $sout .= str_pad($a[0],$max_key_len,' ',STR_PAD_RIGHT).' '.$a[1]."\n"; |
||
286 | } else { |
||
287 | $sout .= $a."\n"; |
||
288 | } |
||
289 | } |
||
290 | return $sout; |
||
291 | } |
||
292 | |||
293 | function _is_bcd($num) { |
||
294 | return preg_match('@^[0-9]+$@', $num, $m); |
||
295 | } |
||
296 | |||
297 | function _decode_aid($aid) { |
||
298 | |||
299 | // based on https://github.com/thephpleague/iso3166/blob/main/src/ISO3166.php |
||
45 | daniel-mar | 300 | // commit 26 July 2022 |
35 | daniel-mar | 301 | // Generated using: |
302 | /* |
||
303 | $x = new ISO3166(); |
||
304 | $bla = $x->all(); |
||
305 | foreach ($bla as $data) { |
||
306 | $out[] = "\t\$iso3166['".$data['numeric']."'] = \"".$data['name']."\";\n"; |
||
307 | } |
||
308 | */ |
||
309 | $iso3166['004'] = "Afghanistan"; |
||
310 | $iso3166['248'] = "Ã…land Islands"; |
||
311 | $iso3166['008'] = "Albania"; |
||
312 | $iso3166['012'] = "Algeria"; |
||
313 | $iso3166['016'] = "American Samoa"; |
||
314 | $iso3166['020'] = "Andorra"; |
||
315 | $iso3166['024'] = "Angola"; |
||
316 | $iso3166['660'] = "Anguilla"; |
||
317 | $iso3166['010'] = "Antarctica"; |
||
318 | $iso3166['028'] = "Antigua and Barbuda"; |
||
319 | $iso3166['032'] = "Argentina"; |
||
320 | $iso3166['051'] = "Armenia"; |
||
321 | $iso3166['533'] = "Aruba"; |
||
322 | $iso3166['036'] = "Australia"; |
||
323 | $iso3166['040'] = "Austria"; |
||
324 | $iso3166['031'] = "Azerbaijan"; |
||
325 | $iso3166['044'] = "Bahamas"; |
||
326 | $iso3166['048'] = "Bahrain"; |
||
327 | $iso3166['050'] = "Bangladesh"; |
||
328 | $iso3166['052'] = "Barbados"; |
||
329 | $iso3166['112'] = "Belarus"; |
||
330 | $iso3166['056'] = "Belgium"; |
||
331 | $iso3166['084'] = "Belize"; |
||
332 | $iso3166['204'] = "Benin"; |
||
333 | $iso3166['060'] = "Bermuda"; |
||
334 | $iso3166['064'] = "Bhutan"; |
||
335 | $iso3166['068'] = "Bolivia (Plurinational State of)"; |
||
336 | $iso3166['535'] = "Bonaire, Sint Eustatius and Saba"; |
||
337 | $iso3166['070'] = "Bosnia and Herzegovina"; |
||
338 | $iso3166['072'] = "Botswana"; |
||
339 | $iso3166['074'] = "Bouvet Island"; |
||
340 | $iso3166['076'] = "Brazil"; |
||
341 | $iso3166['086'] = "British Indian Ocean Territory"; |
||
342 | $iso3166['096'] = "Brunei Darussalam"; |
||
343 | $iso3166['100'] = "Bulgaria"; |
||
344 | $iso3166['854'] = "Burkina Faso"; |
||
345 | $iso3166['108'] = "Burundi"; |
||
346 | $iso3166['132'] = "Cabo Verde"; |
||
347 | $iso3166['116'] = "Cambodia"; |
||
348 | $iso3166['120'] = "Cameroon"; |
||
349 | $iso3166['124'] = "Canada"; |
||
350 | $iso3166['136'] = "Cayman Islands"; |
||
351 | $iso3166['140'] = "Central African Republic"; |
||
352 | $iso3166['148'] = "Chad"; |
||
353 | $iso3166['152'] = "Chile"; |
||
354 | $iso3166['156'] = "China"; |
||
355 | $iso3166['162'] = "Christmas Island"; |
||
356 | $iso3166['166'] = "Cocos (Keeling) Islands"; |
||
357 | $iso3166['170'] = "Colombia"; |
||
358 | $iso3166['174'] = "Comoros"; |
||
359 | $iso3166['178'] = "Congo"; |
||
360 | $iso3166['180'] = "Congo (Democratic Republic of the)"; |
||
361 | $iso3166['184'] = "Cook Islands"; |
||
362 | $iso3166['188'] = "Costa Rica"; |
||
363 | $iso3166['384'] = "Côte d'Ivoire"; |
||
364 | $iso3166['191'] = "Croatia"; |
||
365 | $iso3166['192'] = "Cuba"; |
||
366 | $iso3166['531'] = "Curaçao"; |
||
367 | $iso3166['196'] = "Cyprus"; |
||
368 | $iso3166['203'] = "Czechia"; |
||
369 | $iso3166['208'] = "Denmark"; |
||
370 | $iso3166['262'] = "Djibouti"; |
||
371 | $iso3166['212'] = "Dominica"; |
||
372 | $iso3166['214'] = "Dominican Republic"; |
||
373 | $iso3166['218'] = "Ecuador"; |
||
374 | $iso3166['818'] = "Egypt"; |
||
375 | $iso3166['222'] = "El Salvador"; |
||
376 | $iso3166['226'] = "Equatorial Guinea"; |
||
377 | $iso3166['232'] = "Eritrea"; |
||
378 | $iso3166['233'] = "Estonia"; |
||
379 | $iso3166['231'] = "Ethiopia"; |
||
380 | $iso3166['748'] = "Eswatini"; |
||
381 | $iso3166['238'] = "Falkland Islands (Malvinas)"; |
||
382 | $iso3166['234'] = "Faroe Islands"; |
||
383 | $iso3166['242'] = "Fiji"; |
||
384 | $iso3166['246'] = "Finland"; |
||
385 | $iso3166['250'] = "France"; |
||
386 | $iso3166['254'] = "French Guiana"; |
||
387 | $iso3166['258'] = "French Polynesia"; |
||
388 | $iso3166['260'] = "French Southern Territories"; |
||
389 | $iso3166['266'] = "Gabon"; |
||
390 | $iso3166['270'] = "Gambia"; |
||
391 | $iso3166['268'] = "Georgia"; |
||
392 | $iso3166['276'] = "Germany"; |
||
393 | $iso3166['288'] = "Ghana"; |
||
394 | $iso3166['292'] = "Gibraltar"; |
||
395 | $iso3166['300'] = "Greece"; |
||
396 | $iso3166['304'] = "Greenland"; |
||
397 | $iso3166['308'] = "Grenada"; |
||
398 | $iso3166['312'] = "Guadeloupe"; |
||
399 | $iso3166['316'] = "Guam"; |
||
400 | $iso3166['320'] = "Guatemala"; |
||
401 | $iso3166['831'] = "Guernsey"; |
||
402 | $iso3166['324'] = "Guinea"; |
||
403 | $iso3166['624'] = "Guinea-Bissau"; |
||
404 | $iso3166['328'] = "Guyana"; |
||
405 | $iso3166['332'] = "Haiti"; |
||
406 | $iso3166['334'] = "Heard Island and McDonald Islands"; |
||
407 | $iso3166['336'] = "Holy See"; |
||
408 | $iso3166['340'] = "Honduras"; |
||
409 | $iso3166['344'] = "Hong Kong"; |
||
410 | $iso3166['348'] = "Hungary"; |
||
411 | $iso3166['352'] = "Iceland"; |
||
412 | $iso3166['356'] = "India"; |
||
413 | $iso3166['360'] = "Indonesia"; |
||
414 | $iso3166['364'] = "Iran (Islamic Republic of)"; |
||
415 | $iso3166['368'] = "Iraq"; |
||
416 | $iso3166['372'] = "Ireland"; |
||
417 | $iso3166['833'] = "Isle of Man"; |
||
418 | $iso3166['376'] = "Israel"; |
||
419 | $iso3166['380'] = "Italy"; |
||
420 | $iso3166['388'] = "Jamaica"; |
||
421 | $iso3166['392'] = "Japan"; |
||
422 | $iso3166['832'] = "Jersey"; |
||
423 | $iso3166['400'] = "Jordan"; |
||
424 | $iso3166['398'] = "Kazakhstan"; |
||
425 | $iso3166['404'] = "Kenya"; |
||
426 | $iso3166['296'] = "Kiribati"; |
||
427 | $iso3166['408'] = "Korea (Democratic People's Republic of)"; |
||
428 | $iso3166['410'] = "Korea (Republic of)"; |
||
429 | $iso3166['414'] = "Kuwait"; |
||
430 | $iso3166['417'] = "Kyrgyzstan"; |
||
431 | $iso3166['418'] = "Lao People's Democratic Republic"; |
||
432 | $iso3166['428'] = "Latvia"; |
||
433 | $iso3166['422'] = "Lebanon"; |
||
434 | $iso3166['426'] = "Lesotho"; |
||
435 | $iso3166['430'] = "Liberia"; |
||
436 | $iso3166['434'] = "Libya"; |
||
437 | $iso3166['438'] = "Liechtenstein"; |
||
438 | $iso3166['440'] = "Lithuania"; |
||
439 | $iso3166['442'] = "Luxembourg"; |
||
440 | $iso3166['446'] = "Macao"; |
||
441 | $iso3166['807'] = "North Macedonia"; |
||
442 | $iso3166['450'] = "Madagascar"; |
||
443 | $iso3166['454'] = "Malawi"; |
||
444 | $iso3166['458'] = "Malaysia"; |
||
445 | $iso3166['462'] = "Maldives"; |
||
446 | $iso3166['466'] = "Mali"; |
||
447 | $iso3166['470'] = "Malta"; |
||
448 | $iso3166['584'] = "Marshall Islands"; |
||
449 | $iso3166['474'] = "Martinique"; |
||
450 | $iso3166['478'] = "Mauritania"; |
||
451 | $iso3166['480'] = "Mauritius"; |
||
452 | $iso3166['175'] = "Mayotte"; |
||
453 | $iso3166['484'] = "Mexico"; |
||
454 | $iso3166['583'] = "Micronesia (Federated States of)"; |
||
455 | $iso3166['498'] = "Moldova (Republic of)"; |
||
456 | $iso3166['492'] = "Monaco"; |
||
457 | $iso3166['496'] = "Mongolia"; |
||
458 | $iso3166['499'] = "Montenegro"; |
||
459 | $iso3166['500'] = "Montserrat"; |
||
460 | $iso3166['504'] = "Morocco"; |
||
461 | $iso3166['508'] = "Mozambique"; |
||
462 | $iso3166['104'] = "Myanmar"; |
||
463 | $iso3166['516'] = "Namibia"; |
||
464 | $iso3166['520'] = "Nauru"; |
||
465 | $iso3166['524'] = "Nepal"; |
||
466 | $iso3166['528'] = "Netherlands"; |
||
467 | $iso3166['540'] = "New Caledonia"; |
||
468 | $iso3166['554'] = "New Zealand"; |
||
469 | $iso3166['558'] = "Nicaragua"; |
||
470 | $iso3166['562'] = "Niger"; |
||
471 | $iso3166['566'] = "Nigeria"; |
||
472 | $iso3166['570'] = "Niue"; |
||
473 | $iso3166['574'] = "Norfolk Island"; |
||
474 | $iso3166['580'] = "Northern Mariana Islands"; |
||
475 | $iso3166['578'] = "Norway"; |
||
476 | $iso3166['512'] = "Oman"; |
||
477 | $iso3166['586'] = "Pakistan"; |
||
478 | $iso3166['585'] = "Palau"; |
||
479 | $iso3166['275'] = "Palestine, State of"; |
||
480 | $iso3166['591'] = "Panama"; |
||
481 | $iso3166['598'] = "Papua New Guinea"; |
||
482 | $iso3166['600'] = "Paraguay"; |
||
483 | $iso3166['604'] = "Peru"; |
||
484 | $iso3166['608'] = "Philippines"; |
||
485 | $iso3166['612'] = "Pitcairn"; |
||
486 | $iso3166['616'] = "Poland"; |
||
487 | $iso3166['620'] = "Portugal"; |
||
488 | $iso3166['630'] = "Puerto Rico"; |
||
489 | $iso3166['634'] = "Qatar"; |
||
490 | $iso3166['638'] = "Réunion"; |
||
491 | $iso3166['642'] = "Romania"; |
||
492 | $iso3166['643'] = "Russian Federation"; |
||
493 | $iso3166['646'] = "Rwanda"; |
||
494 | $iso3166['652'] = "Saint Barthélemy"; |
||
495 | $iso3166['654'] = "Saint Helena, Ascension and Tristan da Cunha"; |
||
496 | $iso3166['659'] = "Saint Kitts and Nevis"; |
||
497 | $iso3166['662'] = "Saint Lucia"; |
||
498 | $iso3166['663'] = "Saint Martin (French part)"; |
||
499 | $iso3166['666'] = "Saint Pierre and Miquelon"; |
||
500 | $iso3166['670'] = "Saint Vincent and the Grenadines"; |
||
501 | $iso3166['882'] = "Samoa"; |
||
502 | $iso3166['674'] = "San Marino"; |
||
503 | $iso3166['678'] = "Sao Tome and Principe"; |
||
504 | $iso3166['682'] = "Saudi Arabia"; |
||
505 | $iso3166['686'] = "Senegal"; |
||
506 | $iso3166['688'] = "Serbia"; |
||
507 | $iso3166['690'] = "Seychelles"; |
||
508 | $iso3166['694'] = "Sierra Leone"; |
||
509 | $iso3166['702'] = "Singapore"; |
||
510 | $iso3166['534'] = "Sint Maarten (Dutch part)"; |
||
511 | $iso3166['703'] = "Slovakia"; |
||
512 | $iso3166['705'] = "Slovenia"; |
||
513 | $iso3166['090'] = "Solomon Islands"; |
||
514 | $iso3166['706'] = "Somalia"; |
||
515 | $iso3166['710'] = "South Africa"; |
||
516 | $iso3166['239'] = "South Georgia and the South Sandwich Islands"; |
||
517 | $iso3166['728'] = "South Sudan"; |
||
518 | $iso3166['724'] = "Spain"; |
||
519 | $iso3166['144'] = "Sri Lanka"; |
||
520 | $iso3166['729'] = "Sudan"; |
||
521 | $iso3166['740'] = "Suriname"; |
||
522 | $iso3166['744'] = "Svalbard and Jan Mayen"; |
||
523 | $iso3166['752'] = "Sweden"; |
||
524 | $iso3166['756'] = "Switzerland"; |
||
525 | $iso3166['760'] = "Syrian Arab Republic"; |
||
526 | $iso3166['158'] = "Taiwan (Province of China)"; |
||
527 | $iso3166['762'] = "Tajikistan"; |
||
528 | $iso3166['834'] = "Tanzania, United Republic of"; |
||
529 | $iso3166['764'] = "Thailand"; |
||
530 | $iso3166['626'] = "Timor-Leste"; |
||
531 | $iso3166['768'] = "Togo"; |
||
532 | $iso3166['772'] = "Tokelau"; |
||
533 | $iso3166['776'] = "Tonga"; |
||
534 | $iso3166['780'] = "Trinidad and Tobago"; |
||
535 | $iso3166['788'] = "Tunisia"; |
||
536 | $iso3166['792'] = "Turkey"; |
||
537 | $iso3166['795'] = "Turkmenistan"; |
||
538 | $iso3166['796'] = "Turks and Caicos Islands"; |
||
539 | $iso3166['798'] = "Tuvalu"; |
||
540 | $iso3166['800'] = "Uganda"; |
||
541 | $iso3166['804'] = "Ukraine"; |
||
542 | $iso3166['784'] = "United Arab Emirates"; |
||
543 | $iso3166['826'] = "United Kingdom of Great Britain and Northern Ireland"; |
||
544 | $iso3166['840'] = "United States of America"; |
||
545 | $iso3166['581'] = "United States Minor Outlying Islands"; |
||
546 | $iso3166['858'] = "Uruguay"; |
||
547 | $iso3166['860'] = "Uzbekistan"; |
||
548 | $iso3166['548'] = "Vanuatu"; |
||
549 | $iso3166['862'] = "Venezuela (Bolivarian Republic of)"; |
||
550 | $iso3166['704'] = "Viet Nam"; |
||
551 | $iso3166['092'] = "Virgin Islands (British)"; |
||
552 | $iso3166['850'] = "Virgin Islands (U.S.)"; |
||
553 | $iso3166['876'] = "Wallis and Futuna"; |
||
554 | $iso3166['732'] = "Western Sahara"; |
||
555 | $iso3166['887'] = "Yemen"; |
||
556 | $iso3166['894'] = "Zambia"; |
||
557 | $iso3166['716'] = "Zimbabwe"; |
||
558 | |||
559 | $out = array(); |
||
560 | |||
561 | $aid = strtoupper($aid); |
||
562 | $aid = trim($aid); |
||
563 | $aid = str_replace(' ','',$aid); |
||
40 | daniel-mar | 564 | $aid = str_replace(':','',$aid); |
35 | daniel-mar | 565 | |
566 | if ($aid == '') { |
||
36 | daniel-mar | 567 | $out[] = "INVALID: The AID is empty"; |
35 | daniel-mar | 568 | return $out; |
569 | } |
||
570 | |||
571 | if (!preg_match('@^[0-9A-F]+$@', $aid, $m)) { |
||
36 | daniel-mar | 572 | $out[] = "INVALID: AID has invalid characters. Only A..F and 0..9 are allowed"; |
35 | daniel-mar | 573 | return $out; |
574 | } |
||
575 | |||
40 | daniel-mar | 576 | $aid_hf = implode(':',str_split($aid,2)); |
35 | daniel-mar | 577 | if (strlen($aid)%2 == 1) $aid_hf .= '_'; |
40 | daniel-mar | 578 | |
48 | daniel-mar | 579 | $out[] = array("$aid", "ISO/IEC 7816 Application Identifier (AID)"); |
35 | daniel-mar | 580 | $out[] = array('', "> $aid_hf <"); |
38 | daniel-mar | 581 | $out[] = array('', c_literal_hexstr($aid)); |
35 | daniel-mar | 582 | |
36 | daniel-mar | 583 | if ((strlen($aid) == 32) && (substr($aid,-2) == 'FF')) { |
48 | daniel-mar | 584 | // Sometimes you read that a 16-byte AID must not end with FF, because it is reserved by ISO. |
585 | // I have only found one official source: |
||
49 | daniel-mar | 586 | // ISO/IEC 7816-5 : 1994 |
48 | daniel-mar | 587 | // Identification cards - Integrated circuit(s) cards with contacts - |
588 | // Part 5 : Numbering system and registration procedure for application identifiers |
||
589 | // https://cdn.standards.iteh.ai/samples/19980/8ff6c7ccc9254fe4b7a8a21c0bf59424/ISO-IEC-7816-5-1994.pdf |
||
590 | // Quote from clause 5.2: |
||
49 | daniel-mar | 591 | // "The PIX has a free coding. If the AID is 16 bytes long, |
592 | // then the value 'FF' for the least significant byte is reserved for future use." |
||
48 | daniel-mar | 593 | // In the revisions of ISO/IEC 7816, parts of ISO 7816-5 (e.g. the AID categories) |
594 | // have been moved to ISO 7816-4. |
||
595 | // The "FF" reservation cannot be found in modern versions of 7816-4 or 7816-5. |
||
49 | daniel-mar | 596 | /*$out[] = array('',"INVALID: A 16-byte AID must not end with FF. (Reserved by ISO/IEC)");*/ |
597 | $out[] = array('',"Note: A 16-byte AID ending with FF was reserved by ISO/IEC 7816-5:1994"); |
||
36 | daniel-mar | 598 | } |
599 | |||
600 | if (strlen($aid) > 32) { |
||
601 | $out[] = array('',"INVALID: An AID must not be longer than 16 bytes"); |
||
602 | } |
||
603 | |||
35 | daniel-mar | 604 | $category = substr($aid,0,1); |
605 | |||
606 | // Category 0..9 |
||
607 | // RID = ISO/IEC 7812 Issuer Identification Number (IIN 6 or 8 digits) |
||
608 | // AID = RID + 'FF' + PIX |
||
609 | $iso7812_category = array(); |
||
610 | $iso7812_category['0'] = 'ISO/TC 68 and other industry assignments'; |
||
611 | $iso7812_category['1'] = 'Airlines'; |
||
612 | $iso7812_category['2'] = 'Airlines, financial and other future industry assignments'; |
||
613 | $iso7812_category['3'] = 'Travel and entertainment'; |
||
614 | $iso7812_category['4'] = 'Banking and financial'; |
||
615 | $iso7812_category['5'] = 'Banking and financial'; |
||
616 | $iso7812_category['6'] = 'Merchandising and banking/financial'; |
||
617 | $iso7812_category['7'] = 'Petroleum and other future industry assignments'; |
||
618 | $iso7812_category['8'] = 'Healthcare, telecommunications and other future industry assignments'; |
||
619 | $iso7812_category['9'] = 'Assignment by national standards bodies'; |
||
620 | foreach ($iso7812_category as $check_cat => $check_cat_name) { |
||
621 | if ("$category" == "$check_cat") { // comparison as string is important so that "===" works. "==" does not work because 0=='A' for some reason! |
||
622 | #$out[] = array($category, "AID based on category $category of ISO/IEC 7812 Issuer Identification Number (IIN)"); |
||
623 | #$out[] = array('', "($check_cat = $check_cat_name)"); |
||
624 | $out[] = array('', "AID based on ISO/IEC 7812 Issuer Identification Number (IIN)"); |
||
625 | |||
626 | $iin = $aid; |
||
627 | // IIN and PIX must be delimited with FF, but only if a PIX is available. |
||
628 | // When the IIN has an odd number, then an extra 'F' must be added at the end |
||
629 | $pos = strpos($aid,'F'); |
||
630 | if ($pos !== false) $iin = substr($iin, 0, $pos); |
||
631 | |||
632 | if (!_is_bcd($iin)) { |
||
36 | daniel-mar | 633 | $out[] = array($iin, "INVALID: Expected BCD encoded IIN, optionally followed by FF and PIX"); |
35 | daniel-mar | 634 | return $out; |
635 | } |
||
636 | |||
637 | $pad = ''; |
||
638 | |||
639 | $out[] = 'RID-HERE'; // will (must) be replaced below |
||
640 | |||
641 | $out[] = array($iin, "ISO/IEC 7812 Issuer Identification Number (IIN)"); |
||
642 | if ((strlen($iin) != 6) && (strlen($iin) != 8)) { |
||
643 | $out[] = array('',"Warning: IIN has an unusual length. 6 or 8 digits are expected!"); |
||
644 | } |
||
645 | |||
49 | daniel-mar | 646 | $out[] = array($category, "Major industry identifier $category = $check_cat_name"); |
36 | daniel-mar | 647 | $pad .= str_repeat(' ', strlen("$category")); |
35 | daniel-mar | 648 | |
36 | daniel-mar | 649 | if ("$category" === "9") { |
35 | daniel-mar | 650 | $country = substr($iin,1,3); |
651 | if ($country == '') { |
||
36 | daniel-mar | 652 | $out[] = array($pad.'___', 'ISO/IEC 3166-1 Numeric Country code (missing)'); |
35 | daniel-mar | 653 | } else { |
654 | $country_name = isset($iso3166[$country]) ? $iso3166[$country] : 'Unknown country'; |
||
36 | daniel-mar | 655 | $out[] = array($pad.str_pad($country,3,'_',STR_PAD_RIGHT), "ISO/IEC 3166-1 Numeric Country code : $country ($country_name)"); |
35 | daniel-mar | 656 | } |
36 | daniel-mar | 657 | $pad .= ' '; |
658 | $asi = substr($iin,4); |
||
659 | $asn = $asi; |
||
35 | daniel-mar | 660 | } else { |
36 | daniel-mar | 661 | $asi = substr($iin,1); |
662 | $asn = $asi; |
||
35 | daniel-mar | 663 | } |
36 | daniel-mar | 664 | $out[] = array("$pad$asn", 'Assigned number'.($asi=='' ? ' (missing)' : '')); |
38 | daniel-mar | 665 | if ($asi!='') $out[] = array('', c_literal_hexstr($asi)); |
36 | daniel-mar | 666 | $pad .= str_repeat(' ',strlen($asn)); |
35 | daniel-mar | 667 | |
668 | $padded_iin = $iin; |
||
669 | if (strlen($iin)%2 != 0) { |
||
670 | $odd_padding = substr($aid,strlen($iin),1); |
||
671 | if ($odd_padding != 'F') { |
||
672 | foreach ($out as $n => &$tmp) { |
||
673 | if ($tmp == 'RID-HERE') { |
||
674 | unset($out[$n]); |
||
675 | break; |
||
676 | } |
||
677 | } |
||
36 | daniel-mar | 678 | $out[] = array("$pad!","INVALID: An IIN with odd length must be padded with F, e.g. 123 => 123F"); |
35 | daniel-mar | 679 | return $out; |
680 | } |
||
681 | $out[] = array($pad.$odd_padding, 'Padding of IIN with odd length'); |
||
682 | $padded_iin .= $odd_padding; |
||
683 | $pad .= ' '; |
||
684 | } |
||
685 | |||
686 | $rid = $padded_iin; |
||
687 | foreach ($out as &$tmp) { |
||
688 | if ($tmp == 'RID-HERE') { |
||
689 | $tmp = array("$rid", "Registered Application Provider Identifier (RID)"); |
||
690 | break; |
||
691 | } |
||
692 | } |
||
693 | |||
694 | if (strlen($aid) == strlen($padded_iin)) { |
||
695 | // There is no PIX |
||
696 | $out[] = "Proprietary application identifier extension (PIX) missing"; |
||
697 | } else { |
||
698 | $delimiter = substr($aid,strlen($padded_iin),2); |
||
699 | if ($delimiter != 'FF') { |
||
36 | daniel-mar | 700 | $out[] = array($pad.substr($aid,strlen($padded_iin)), "INVALID: RID/IIN and PIX must be delimited by FF"); |
35 | daniel-mar | 701 | return $out; |
702 | } |
||
703 | $out[] = array($pad.$delimiter, 'Delimiter which separates RID/IIN from PIX'); |
||
704 | $pad .= str_repeat(' ',strlen($delimiter)); |
||
705 | |||
706 | $pix = substr($aid,strlen($padded_iin)+strlen('FF')); |
||
707 | if ($pix == '') { |
||
708 | $out[] = "Proprietary application identifier extension (PIX) missing"; |
||
36 | daniel-mar | 709 | $out[] = "Warning: If PIX is available, FF delimites RID/IIN from PIX. Since PIX is empty, consider removing FF."; // not sure if this is an error or not |
35 | daniel-mar | 710 | } else { |
711 | $out[] = array($pad.$pix, "Proprietary application identifier extension (PIX)"); |
||
38 | daniel-mar | 712 | $out[] = array('', c_literal_hexstr($pix)); |
35 | daniel-mar | 713 | } |
714 | } |
||
715 | |||
716 | return $out; |
||
717 | } |
||
718 | } |
||
719 | |||
720 | // Category 'A' (International Registration) |
||
721 | // RID = 'A' + 9 digits |
||
722 | // AID = RID + PIX |
||
723 | if ("$category" === "A") { |
||
724 | $rid = substr($aid,0,10); |
||
725 | $rid = str_pad($rid,10,'_',STR_PAD_RIGHT); |
||
726 | |||
727 | $pix = substr($aid,10); |
||
728 | |||
36 | daniel-mar | 729 | $asi = substr($aid,1,9); |
730 | $asn = str_pad($asi,9,'_',STR_PAD_RIGHT); |
||
35 | daniel-mar | 731 | |
732 | $out[] = array("$rid", "Registered Application Provider Identifier (RID)"); |
||
733 | $out[] = array("$category", "Category $category: International registration"); |
||
36 | daniel-mar | 734 | $out[] = array(" $asn", 'Assigned number, BCD recommended'.($asi=='' ? ' (missing)' : '')); |
38 | daniel-mar | 735 | if ($asi!='') $out[] = array('', c_literal_hexstr($asi)); |
35 | daniel-mar | 736 | if ($pix == '') { |
737 | $out[] = "Proprietary application identifier extension (PIX) missing"; |
||
738 | } else { |
||
739 | $out[] = array(str_pad($pix,strlen($aid),' ',STR_PAD_LEFT), "Proprietary application identifier extension (PIX)"); |
||
38 | daniel-mar | 740 | $out[] = array('', c_literal_hexstr($pix)); |
35 | daniel-mar | 741 | } |
742 | |||
743 | return $out; |
||
744 | } |
||
745 | |||
746 | // Category 'D' (Local/National Registration) |
||
747 | // RID = 'D' + 3 digits country code (ISO/IEC 3166-1) + 6 digits |
||
748 | // AID = RID + PIX |
||
749 | if ("$category" === "D") { |
||
750 | $rid = substr($aid,0,10); |
||
751 | $rid = str_pad($rid,10,'_',STR_PAD_RIGHT); |
||
752 | |||
753 | $pix = substr($aid,10); |
||
754 | |||
755 | $country = substr($aid,1,3); |
||
756 | |||
36 | daniel-mar | 757 | $asi = substr($aid,4,6); |
758 | $asn = str_pad($asi,6,'_',STR_PAD_RIGHT); |
||
35 | daniel-mar | 759 | |
760 | $out[] = array("$rid", "Registered Application Provider Identifier (RID)"); |
||
761 | $out[] = array("$category", "Category $category: Local/National registration"); |
||
762 | if ($country == '') { |
||
763 | $out[] = array(" ___", "ISO/IEC 3166-1 Numeric Country code (missing)"); |
||
764 | } else { |
||
765 | $country_name = isset($iso3166[$country]) ? $iso3166[$country] : 'Unknown country'; |
||
766 | $out[] = array(" ".str_pad($country,3,'_',STR_PAD_RIGHT), "ISO/IEC 3166-1 Numeric Country code : $country ($country_name)"); |
||
767 | } |
||
36 | daniel-mar | 768 | $out[] = array(" $asn", 'Assigned number, BCD recommended'.($asi=='' ? ' (missing)' : '')); |
38 | daniel-mar | 769 | if ($asi!='') $out[] = array('', c_literal_hexstr($asi)); |
35 | daniel-mar | 770 | if ($pix == '') { |
771 | $out[] = "Proprietary application identifier extension (PIX) missing"; |
||
772 | } else { |
||
773 | $out[] = array(str_pad($pix,strlen($aid),' ',STR_PAD_LEFT), "Proprietary application identifier extension (PIX)"); |
||
38 | daniel-mar | 774 | $out[] = array('', c_literal_hexstr($pix)); |
35 | daniel-mar | 775 | } |
776 | |||
777 | return $out; |
||
778 | } |
||
779 | |||
40 | daniel-mar | 780 | // Category 'E' |
781 | // AID = 'E8' + OID + PIX (OID is DER encoding without type and length) |
||
782 | if ("$category" === "E") { |
||
783 | $out[] = array("$category", "Category $category: Standard"); |
||
784 | |||
41 | daniel-mar | 785 | $std_schema = substr($aid,1,1); |
786 | if ($std_schema == '8') { |
||
787 | $out[] = array(" $std_schema", "Standard identified by OID"); |
||
40 | daniel-mar | 788 | |
46 | daniel-mar | 789 | // Start: Try to find out if it is E8+Length+OID (inofficial/illegal) or E8+OID (ISO) |
790 | $len_usage = _aid_e8_length_usage($aid); |
||
791 | $include_der_length = true; // In case it is ambiguous , let's say it is E8+Length+OID |
||
792 | // Note that these ambiguous are rare and will only happen inside the root OID 0 |
||
793 | if ($len_usage === true) $include_der_length = true; |
||
794 | if ($len_usage === false) $include_der_length = false; |
||
45 | daniel-mar | 795 | if ($include_der_length) { |
46 | daniel-mar | 796 | // Case E8+Length+OID (inofficial/illegal) |
797 | $der_length_hex = substr($aid,2,2); |
||
798 | $der_length_dec = hexdec($der_length_hex); |
||
45 | daniel-mar | 799 | $out[] = array(" $der_length_hex", "DER encoding length (illegal usage not defined by ISO)"); |
800 | $pure_der = substr($aid,4); |
||
801 | $indent = 4; |
||
802 | $e8_minmax_measure = 'DER'; |
||
803 | $e8_min = $der_length_dec; |
||
804 | $e8_max = $der_length_dec; |
||
805 | } else { |
||
46 | daniel-mar | 806 | // Case E8+OID (defined by ISO, but only for their 1.0 OID) |
45 | daniel-mar | 807 | $pure_der = substr($aid,2); |
808 | $indent = 2; |
||
46 | daniel-mar | 809 | if (substr($aid,2,2) == '28') { // '28' = OID 1.0 (ISO Standard) |
810 | // ISO Standards (OID 1.0) will only have 1 or 2 numbers. (Number 1 is the standard, and number 2 |
||
811 | // is the part in case of a multi-part standard). |
||
812 | $e8_minmax_measure = 'ARC'; |
||
813 | $e8_min = 3; // 1.0.aaaa (ISO AAAA) |
||
814 | $e8_max = 4; // 1.0.aaaa.b (ISO AAAA-B) |
||
815 | } else { |
||
816 | // This is the inofficial usage of E8+OID |
||
817 | $e8_minmax_measure = 'ARC'; |
||
818 | $e8_min = 2; // At least 2 arcs (OID x.y) |
||
819 | $e8_max = -1; // no limit |
||
820 | } |
||
45 | daniel-mar | 821 | } |
46 | daniel-mar | 822 | // End: Try to find out if it is E8+Length+OID (inofficial/illegal) or E8+OID (ISO) |
45 | daniel-mar | 823 | |
40 | daniel-mar | 824 | try { |
45 | daniel-mar | 825 | $interpretations = _aid_e8_interpretations($pure_der,$e8_minmax_measure,$e8_min,$e8_max); |
40 | daniel-mar | 826 | foreach ($interpretations as $ii => $interpretation) { |
827 | $pos = $interpretation[0]; |
||
828 | $txt1 = $interpretation[1]; // Standard |
||
829 | $txt2 = $interpretation[2]; // PIX (optional) |
||
830 | |||
45 | daniel-mar | 831 | $aid1 = str_repeat(' ',$indent).substr($pure_der,0,$pos*2); |
832 | $aid2 = substr($pure_der,$pos*2); |
||
40 | daniel-mar | 833 | |
834 | $out[] = array("$aid1", "$txt1"); |
||
835 | if ($txt2 !== '') { |
||
42 | daniel-mar | 836 | $pix = "$txt2 (".c_literal_hexstr(str_replace(':','',$txt2)).")"; |
40 | daniel-mar | 837 | $out[] = array(str_repeat(' ',strlen($aid1))."$aid2", "with PIX $pix"); |
838 | } |
||
839 | if ($ii < count($interpretations)-1) { |
||
840 | $out[] = array('', 'or:'); |
||
841 | } |
||
842 | } |
||
843 | } catch (Exception $e) { |
||
45 | daniel-mar | 844 | $out[] = array(str_repeat(' ',$indent).$pure_der, "ERROR: ".$e->getMessage()); |
40 | daniel-mar | 845 | } |
41 | daniel-mar | 846 | } else if ($std_schema != '') { |
40 | daniel-mar | 847 | // E0..E7, E9..EF are RFU |
848 | $unknown = substr($aid,1); |
||
849 | $out[] = array(" $unknown", "ILLEGAL USAGE / RESERVED"); |
||
850 | } |
||
851 | |||
852 | return $out; |
||
853 | } |
||
854 | |||
35 | daniel-mar | 855 | // Category 'F' |
856 | // AID = 'F' + PIX |
||
857 | if ("$category" === "F") { |
||
858 | $out[] = array("$category", "Category $category: Non-registered / Proprietary"); |
||
859 | $rid = substr($aid,0,1); |
||
860 | $pix = substr($aid,1); |
||
861 | if ($pix == '') { |
||
862 | $out[] = "Proprietary application identifier extension (PIX) missing"; |
||
863 | } else { |
||
864 | $out[] = array(' '.$pix, "Proprietary application identifier extension (PIX)"); |
||
38 | daniel-mar | 865 | $out[] = array('', c_literal_hexstr($pix)); |
35 | daniel-mar | 866 | } |
867 | return $out; |
||
868 | } |
||
869 | |||
40 | daniel-mar | 870 | // Category 'B' and 'C' are reserved |
35 | daniel-mar | 871 | $out[] = array("$category", "Category $category: ILLEGAL USAGE / RESERVED"); |
872 | if (strlen($aid) > 1) { |
||
38 | daniel-mar | 873 | $aid_ = substr($aid,1); |
874 | $out[] = array(" ".$aid_, "Unknown composition"); |
||
875 | $out[] = array('', c_literal_hexstr($aid_)); |
||
35 | daniel-mar | 876 | } |
877 | return $out; |
||
878 | } |
||
37 | daniel-mar | 879 | |
880 | /* --- Small extra function: not part of the decoder --- */ |
||
881 | |||
882 | function aid_split_rid_pix($a, &$rid=null, &$pix=null) { |
||
883 | // "Quick'n'Dirty" function which does not do any consistency checks! |
||
884 | // It expects that the string is a valid AID! |
||
885 | |||
886 | $cat = substr($a,0,1); |
||
887 | if (is_numeric($cat)) { |
||
888 | $p = strpos($a,'F'); |
||
889 | if ($p%2 != 0) $p++; |
||
890 | } else if (($cat == 'A') || ($cat == 'D')) { |
||
891 | $p = 10; |
||
892 | } else if ($cat == 'F') { |
||
893 | $p = 1; |
||
894 | } else { |
||
895 | $p = 0; |
||
896 | } |
||
897 | |||
898 | if ($rid !== null) $rid = substr($a, 0, $p); |
||
899 | if ($pix !== null) $pix = substr($a, $p); |
||
900 | |||
901 | return $p; |
||
902 | } |
||
43 | daniel-mar | 903 | |
904 | function aid_canonize(&$aid_candidate) { |
||
905 | $aid_candidate = str_replace(' ', '', $aid_candidate); |
||
906 | $aid_candidate = str_replace(':', '', $aid_candidate); |
||
907 | $aid_candidate = strtoupper($aid_candidate); |
||
908 | if (strlen($aid_candidate) > 16*2) { |
||
909 | $aid_is_ok = false; // OID DER encoding is too long to fit into the AID |
||
910 | } else if ((strlen($aid_candidate) == 16*2) && (substr($aid_candidate,-2) == 'FF')) { |
||
911 | $aid_is_ok = false; // 16 byte AID must not end with 0xFF (reserved by ISO) |
||
912 | } else { |
||
913 | $aid_is_ok = true; |
||
914 | } |
||
915 | return $aid_is_ok; |
||
916 | } |