Rev 40 | Rev 42 | 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 | /* |
||
4 | * ISO/IEC 7816-5 Application Identifier decoder for PHP |
||
5 | * Copyright 2022 Daniel Marschall, ViaThinkSoft |
||
40 | daniel-mar | 6 | * Version 2022-09-07 |
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 | |||
40 | daniel-mar | 21 | include_once __DIR__ . '/misc_functions.inc.php'; |
22 | |||
23 | # --- |
||
24 | |||
35 | daniel-mar | 25 | /* |
26 | #test2('A000000051AABBCC'); |
||
27 | #test2('B01234567890'); |
||
28 | #test2('D276000098AABBCCDDEEFFAABBCCDDEE'); |
||
29 | #test2('F01234567890'); |
||
40 | daniel-mar | 30 | #test('91234FFF999'); |
31 | #test('51234FFF999'); |
||
32 | #test2('E828BD080F014E585031'); |
||
35 | daniel-mar | 33 | |
34 | function test2($aid) { |
||
35 | while ($aid != '') { |
||
36 | echo test($aid); |
||
37 | $aid = substr($aid,0,strlen($aid)-1); |
||
38 | } |
||
39 | } |
||
40 | |||
41 | function test($aid) { |
||
42 | $out = _decode_aid($aid); |
||
43 | $max_key_len = 32; // min width of first column |
||
44 | foreach ($out as $a) { |
||
45 | if (is_array($a)) { |
||
46 | $max_key_len = max($max_key_len,strlen($a[0])); |
||
47 | } |
||
48 | } |
||
49 | foreach ($out as $a) { |
||
50 | if (is_array($a)) { |
||
51 | echo str_pad($a[0],$max_key_len,' ',STR_PAD_RIGHT).' '.$a[1]."\n"; |
||
52 | } else { |
||
53 | echo $a."\n"; |
||
54 | } |
||
55 | } |
||
56 | echo "\n"; |
||
57 | } |
||
58 | */ |
||
59 | |||
60 | # --- |
||
61 | |||
40 | daniel-mar | 62 | function _aid_e8_interpretations($aid) { |
63 | $ret = array(); |
||
64 | |||
65 | $output_oid = array(); |
||
66 | |||
67 | $aid = strtoupper(str_replace(' ','',$aid)); |
||
68 | $aid = strtoupper(str_replace(':','',$aid)); |
||
69 | $by = str_split($aid,2); |
||
70 | if ((array_shift($by) == 'E8') ) { |
||
71 | // The following part is partially taken from the DER decoder/encoder by Daniel Marschall: |
||
72 | // https://github.com/danielmarschall/oidconverter/blob/master/php/OidDerConverter.class.phps |
||
73 | // (Only the DER "value" part, without "type" and "length") |
||
74 | |||
75 | $part = 2; // DER part 0 (type) and part 1 (length) not present |
||
76 | $fSub = 0; // Subtract value from next number output. Used when encoding {2 48} and up |
||
77 | $ll = gmp_init(0); |
||
78 | $arcBeginning = true; |
||
79 | |||
80 | foreach ($by as $ii => $pb) { |
||
81 | |||
82 | $pb = hexdec($pb); |
||
83 | |||
84 | if ($part == 2) { // First two arcs |
||
85 | $first = $pb / 40; |
||
86 | $second = $pb % 40; |
||
87 | if ($first > 2) { |
||
88 | $first = 2; |
||
89 | $output_oid[] = $first; |
||
90 | $arcBeginning = true; |
||
91 | |||
92 | if (($pb & 0x80) != 0) { |
||
93 | // 2.48 and up => 2+ octets |
||
94 | // Output in "part 3" |
||
95 | |||
96 | if ($pb == 0x80) { |
||
97 | throw new Exception("Encoding error. Illegal 0x80 paddings. (See Rec. ITU-T X.690, clause 8.19.2)\n"); |
||
98 | } else { |
||
99 | $arcBeginning = false; |
||
100 | } |
||
101 | |||
102 | $ll = gmp_add($ll, ($pb & 0x7F)); |
||
103 | $fSub = 80; |
||
104 | $fOK = false; |
||
105 | } else { |
||
106 | // 2.0 till 2.47 => 1 octet |
||
107 | $second = $pb - 80; |
||
108 | $output_oid[] = $second; |
||
109 | $arcBeginning = true; |
||
110 | $fOK = true; |
||
111 | $ll = gmp_init(0); |
||
112 | } |
||
113 | } else { |
||
114 | // 0.0 till 0.37 => 1 octet |
||
115 | // 1.0 till 1.37 => 1 octet |
||
116 | $output_oid[] = $first; |
||
117 | $output_oid[] = $second; |
||
118 | $arcBeginning = true; |
||
119 | $fOK = true; |
||
120 | $ll = gmp_init(0); |
||
121 | } |
||
122 | $part++; |
||
123 | } else { //else if ($part == 3) { // Arc three and higher |
||
124 | if (($pb & 0x80) != 0) { |
||
125 | if ($arcBeginning && ($pb == 0x80)) { |
||
126 | throw new Exception("Encoding error. Illegal 0x80 paddings. (See Rec. ITU-T X.690, clause 8.19.2)"); |
||
127 | } else { |
||
128 | $arcBeginning = false; |
||
129 | } |
||
130 | |||
131 | $ll = gmp_mul($ll, 0x80); |
||
132 | $ll = gmp_add($ll, ($pb & 0x7F)); |
||
133 | $fOK = false; |
||
134 | } else { |
||
135 | $fOK = true; |
||
136 | $ll = gmp_mul($ll, 0x80); |
||
137 | $ll = gmp_add($ll, $pb); |
||
138 | $ll = gmp_sub($ll, $fSub); |
||
139 | $output_oid[] = gmp_strval($ll, 10); |
||
140 | |||
141 | $is_iso_standard = ($output_oid[0] == '1') && ($output_oid[1] == '0'); |
||
142 | |||
143 | $byy = $by; |
||
144 | for ($i=0;$i<=$ii;$i++) array_shift($byy); |
||
145 | |||
146 | $s_oid = implode('.',$output_oid); |
||
147 | |||
148 | if ($is_iso_standard) { |
||
149 | $std_hf = 'Standard ISO/IEC '.$output_oid[2]; |
||
150 | if (isset($output_oid[3])) $std_hf .= "-".$output_oid[3]; |
||
151 | } else { |
||
152 | $std_hf = "Unknown Standard"; // should not happen |
||
153 | } |
||
154 | |||
155 | $pix = implode(':',$byy); |
||
156 | |||
157 | if ($pix !== '') { |
||
158 | $ret[] = array($ii+1,"$std_hf (OID $s_oid)",$pix); |
||
159 | } else { |
||
160 | $ret[] = array($ii+1,"$std_hf (OID $s_oid)",""); |
||
161 | } |
||
162 | |||
163 | // ISO Standards (OID 1.0) will only have 1 or 2 numbers. (Number 1 is the standard, and number 2 |
||
164 | // is the part in case of a multi-part standard). |
||
165 | if ($is_iso_standard && (count($output_oid) == 4)) break; |
||
166 | |||
167 | // Happens only if 0x80 paddings are allowed |
||
168 | // $fOK = gmp_cmp($ll, 0) >= 0; |
||
169 | $ll = gmp_init(0); |
||
170 | $fSub = 0; |
||
171 | $arcBeginning = true; |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | return $ret; |
||
177 | } |
||
178 | |||
35 | daniel-mar | 179 | function decode_aid($aid,$compact=true) { |
180 | $sout = ''; |
||
181 | $out = _decode_aid($aid); |
||
182 | if ($compact) { |
||
183 | $max_key_len = 0; |
||
184 | foreach ($out as $a) { |
||
185 | if (is_array($a)) { |
||
186 | $max_key_len = max($max_key_len,strlen($a[0])); |
||
187 | } |
||
188 | } |
||
189 | } else { |
||
190 | $max_key_len = 32; // 16 bytes |
||
191 | } |
||
192 | foreach ($out as $a) { |
||
193 | if (is_array($a)) { |
||
194 | $sout .= str_pad($a[0],$max_key_len,' ',STR_PAD_RIGHT).' '.$a[1]."\n"; |
||
195 | } else { |
||
196 | $sout .= $a."\n"; |
||
197 | } |
||
198 | } |
||
199 | return $sout; |
||
200 | } |
||
201 | |||
202 | function _is_bcd($num) { |
||
203 | return preg_match('@^[0-9]+$@', $num, $m); |
||
204 | } |
||
205 | |||
206 | function _decode_aid($aid) { |
||
207 | |||
208 | // based on https://github.com/thephpleague/iso3166/blob/main/src/ISO3166.php |
||
209 | // commit 26.07.2022 |
||
210 | // Generated using: |
||
211 | /* |
||
212 | $x = new ISO3166(); |
||
213 | $bla = $x->all(); |
||
214 | foreach ($bla as $data) { |
||
215 | $out[] = "\t\$iso3166['".$data['numeric']."'] = \"".$data['name']."\";\n"; |
||
216 | } |
||
217 | */ |
||
218 | $iso3166['004'] = "Afghanistan"; |
||
219 | $iso3166['248'] = "Ã…land Islands"; |
||
220 | $iso3166['008'] = "Albania"; |
||
221 | $iso3166['012'] = "Algeria"; |
||
222 | $iso3166['016'] = "American Samoa"; |
||
223 | $iso3166['020'] = "Andorra"; |
||
224 | $iso3166['024'] = "Angola"; |
||
225 | $iso3166['660'] = "Anguilla"; |
||
226 | $iso3166['010'] = "Antarctica"; |
||
227 | $iso3166['028'] = "Antigua and Barbuda"; |
||
228 | $iso3166['032'] = "Argentina"; |
||
229 | $iso3166['051'] = "Armenia"; |
||
230 | $iso3166['533'] = "Aruba"; |
||
231 | $iso3166['036'] = "Australia"; |
||
232 | $iso3166['040'] = "Austria"; |
||
233 | $iso3166['031'] = "Azerbaijan"; |
||
234 | $iso3166['044'] = "Bahamas"; |
||
235 | $iso3166['048'] = "Bahrain"; |
||
236 | $iso3166['050'] = "Bangladesh"; |
||
237 | $iso3166['052'] = "Barbados"; |
||
238 | $iso3166['112'] = "Belarus"; |
||
239 | $iso3166['056'] = "Belgium"; |
||
240 | $iso3166['084'] = "Belize"; |
||
241 | $iso3166['204'] = "Benin"; |
||
242 | $iso3166['060'] = "Bermuda"; |
||
243 | $iso3166['064'] = "Bhutan"; |
||
244 | $iso3166['068'] = "Bolivia (Plurinational State of)"; |
||
245 | $iso3166['535'] = "Bonaire, Sint Eustatius and Saba"; |
||
246 | $iso3166['070'] = "Bosnia and Herzegovina"; |
||
247 | $iso3166['072'] = "Botswana"; |
||
248 | $iso3166['074'] = "Bouvet Island"; |
||
249 | $iso3166['076'] = "Brazil"; |
||
250 | $iso3166['086'] = "British Indian Ocean Territory"; |
||
251 | $iso3166['096'] = "Brunei Darussalam"; |
||
252 | $iso3166['100'] = "Bulgaria"; |
||
253 | $iso3166['854'] = "Burkina Faso"; |
||
254 | $iso3166['108'] = "Burundi"; |
||
255 | $iso3166['132'] = "Cabo Verde"; |
||
256 | $iso3166['116'] = "Cambodia"; |
||
257 | $iso3166['120'] = "Cameroon"; |
||
258 | $iso3166['124'] = "Canada"; |
||
259 | $iso3166['136'] = "Cayman Islands"; |
||
260 | $iso3166['140'] = "Central African Republic"; |
||
261 | $iso3166['148'] = "Chad"; |
||
262 | $iso3166['152'] = "Chile"; |
||
263 | $iso3166['156'] = "China"; |
||
264 | $iso3166['162'] = "Christmas Island"; |
||
265 | $iso3166['166'] = "Cocos (Keeling) Islands"; |
||
266 | $iso3166['170'] = "Colombia"; |
||
267 | $iso3166['174'] = "Comoros"; |
||
268 | $iso3166['178'] = "Congo"; |
||
269 | $iso3166['180'] = "Congo (Democratic Republic of the)"; |
||
270 | $iso3166['184'] = "Cook Islands"; |
||
271 | $iso3166['188'] = "Costa Rica"; |
||
272 | $iso3166['384'] = "Côte d'Ivoire"; |
||
273 | $iso3166['191'] = "Croatia"; |
||
274 | $iso3166['192'] = "Cuba"; |
||
275 | $iso3166['531'] = "Curaçao"; |
||
276 | $iso3166['196'] = "Cyprus"; |
||
277 | $iso3166['203'] = "Czechia"; |
||
278 | $iso3166['208'] = "Denmark"; |
||
279 | $iso3166['262'] = "Djibouti"; |
||
280 | $iso3166['212'] = "Dominica"; |
||
281 | $iso3166['214'] = "Dominican Republic"; |
||
282 | $iso3166['218'] = "Ecuador"; |
||
283 | $iso3166['818'] = "Egypt"; |
||
284 | $iso3166['222'] = "El Salvador"; |
||
285 | $iso3166['226'] = "Equatorial Guinea"; |
||
286 | $iso3166['232'] = "Eritrea"; |
||
287 | $iso3166['233'] = "Estonia"; |
||
288 | $iso3166['231'] = "Ethiopia"; |
||
289 | $iso3166['748'] = "Eswatini"; |
||
290 | $iso3166['238'] = "Falkland Islands (Malvinas)"; |
||
291 | $iso3166['234'] = "Faroe Islands"; |
||
292 | $iso3166['242'] = "Fiji"; |
||
293 | $iso3166['246'] = "Finland"; |
||
294 | $iso3166['250'] = "France"; |
||
295 | $iso3166['254'] = "French Guiana"; |
||
296 | $iso3166['258'] = "French Polynesia"; |
||
297 | $iso3166['260'] = "French Southern Territories"; |
||
298 | $iso3166['266'] = "Gabon"; |
||
299 | $iso3166['270'] = "Gambia"; |
||
300 | $iso3166['268'] = "Georgia"; |
||
301 | $iso3166['276'] = "Germany"; |
||
302 | $iso3166['288'] = "Ghana"; |
||
303 | $iso3166['292'] = "Gibraltar"; |
||
304 | $iso3166['300'] = "Greece"; |
||
305 | $iso3166['304'] = "Greenland"; |
||
306 | $iso3166['308'] = "Grenada"; |
||
307 | $iso3166['312'] = "Guadeloupe"; |
||
308 | $iso3166['316'] = "Guam"; |
||
309 | $iso3166['320'] = "Guatemala"; |
||
310 | $iso3166['831'] = "Guernsey"; |
||
311 | $iso3166['324'] = "Guinea"; |
||
312 | $iso3166['624'] = "Guinea-Bissau"; |
||
313 | $iso3166['328'] = "Guyana"; |
||
314 | $iso3166['332'] = "Haiti"; |
||
315 | $iso3166['334'] = "Heard Island and McDonald Islands"; |
||
316 | $iso3166['336'] = "Holy See"; |
||
317 | $iso3166['340'] = "Honduras"; |
||
318 | $iso3166['344'] = "Hong Kong"; |
||
319 | $iso3166['348'] = "Hungary"; |
||
320 | $iso3166['352'] = "Iceland"; |
||
321 | $iso3166['356'] = "India"; |
||
322 | $iso3166['360'] = "Indonesia"; |
||
323 | $iso3166['364'] = "Iran (Islamic Republic of)"; |
||
324 | $iso3166['368'] = "Iraq"; |
||
325 | $iso3166['372'] = "Ireland"; |
||
326 | $iso3166['833'] = "Isle of Man"; |
||
327 | $iso3166['376'] = "Israel"; |
||
328 | $iso3166['380'] = "Italy"; |
||
329 | $iso3166['388'] = "Jamaica"; |
||
330 | $iso3166['392'] = "Japan"; |
||
331 | $iso3166['832'] = "Jersey"; |
||
332 | $iso3166['400'] = "Jordan"; |
||
333 | $iso3166['398'] = "Kazakhstan"; |
||
334 | $iso3166['404'] = "Kenya"; |
||
335 | $iso3166['296'] = "Kiribati"; |
||
336 | $iso3166['408'] = "Korea (Democratic People's Republic of)"; |
||
337 | $iso3166['410'] = "Korea (Republic of)"; |
||
338 | $iso3166['414'] = "Kuwait"; |
||
339 | $iso3166['417'] = "Kyrgyzstan"; |
||
340 | $iso3166['418'] = "Lao People's Democratic Republic"; |
||
341 | $iso3166['428'] = "Latvia"; |
||
342 | $iso3166['422'] = "Lebanon"; |
||
343 | $iso3166['426'] = "Lesotho"; |
||
344 | $iso3166['430'] = "Liberia"; |
||
345 | $iso3166['434'] = "Libya"; |
||
346 | $iso3166['438'] = "Liechtenstein"; |
||
347 | $iso3166['440'] = "Lithuania"; |
||
348 | $iso3166['442'] = "Luxembourg"; |
||
349 | $iso3166['446'] = "Macao"; |
||
350 | $iso3166['807'] = "North Macedonia"; |
||
351 | $iso3166['450'] = "Madagascar"; |
||
352 | $iso3166['454'] = "Malawi"; |
||
353 | $iso3166['458'] = "Malaysia"; |
||
354 | $iso3166['462'] = "Maldives"; |
||
355 | $iso3166['466'] = "Mali"; |
||
356 | $iso3166['470'] = "Malta"; |
||
357 | $iso3166['584'] = "Marshall Islands"; |
||
358 | $iso3166['474'] = "Martinique"; |
||
359 | $iso3166['478'] = "Mauritania"; |
||
360 | $iso3166['480'] = "Mauritius"; |
||
361 | $iso3166['175'] = "Mayotte"; |
||
362 | $iso3166['484'] = "Mexico"; |
||
363 | $iso3166['583'] = "Micronesia (Federated States of)"; |
||
364 | $iso3166['498'] = "Moldova (Republic of)"; |
||
365 | $iso3166['492'] = "Monaco"; |
||
366 | $iso3166['496'] = "Mongolia"; |
||
367 | $iso3166['499'] = "Montenegro"; |
||
368 | $iso3166['500'] = "Montserrat"; |
||
369 | $iso3166['504'] = "Morocco"; |
||
370 | $iso3166['508'] = "Mozambique"; |
||
371 | $iso3166['104'] = "Myanmar"; |
||
372 | $iso3166['516'] = "Namibia"; |
||
373 | $iso3166['520'] = "Nauru"; |
||
374 | $iso3166['524'] = "Nepal"; |
||
375 | $iso3166['528'] = "Netherlands"; |
||
376 | $iso3166['540'] = "New Caledonia"; |
||
377 | $iso3166['554'] = "New Zealand"; |
||
378 | $iso3166['558'] = "Nicaragua"; |
||
379 | $iso3166['562'] = "Niger"; |
||
380 | $iso3166['566'] = "Nigeria"; |
||
381 | $iso3166['570'] = "Niue"; |
||
382 | $iso3166['574'] = "Norfolk Island"; |
||
383 | $iso3166['580'] = "Northern Mariana Islands"; |
||
384 | $iso3166['578'] = "Norway"; |
||
385 | $iso3166['512'] = "Oman"; |
||
386 | $iso3166['586'] = "Pakistan"; |
||
387 | $iso3166['585'] = "Palau"; |
||
388 | $iso3166['275'] = "Palestine, State of"; |
||
389 | $iso3166['591'] = "Panama"; |
||
390 | $iso3166['598'] = "Papua New Guinea"; |
||
391 | $iso3166['600'] = "Paraguay"; |
||
392 | $iso3166['604'] = "Peru"; |
||
393 | $iso3166['608'] = "Philippines"; |
||
394 | $iso3166['612'] = "Pitcairn"; |
||
395 | $iso3166['616'] = "Poland"; |
||
396 | $iso3166['620'] = "Portugal"; |
||
397 | $iso3166['630'] = "Puerto Rico"; |
||
398 | $iso3166['634'] = "Qatar"; |
||
399 | $iso3166['638'] = "Réunion"; |
||
400 | $iso3166['642'] = "Romania"; |
||
401 | $iso3166['643'] = "Russian Federation"; |
||
402 | $iso3166['646'] = "Rwanda"; |
||
403 | $iso3166['652'] = "Saint Barthélemy"; |
||
404 | $iso3166['654'] = "Saint Helena, Ascension and Tristan da Cunha"; |
||
405 | $iso3166['659'] = "Saint Kitts and Nevis"; |
||
406 | $iso3166['662'] = "Saint Lucia"; |
||
407 | $iso3166['663'] = "Saint Martin (French part)"; |
||
408 | $iso3166['666'] = "Saint Pierre and Miquelon"; |
||
409 | $iso3166['670'] = "Saint Vincent and the Grenadines"; |
||
410 | $iso3166['882'] = "Samoa"; |
||
411 | $iso3166['674'] = "San Marino"; |
||
412 | $iso3166['678'] = "Sao Tome and Principe"; |
||
413 | $iso3166['682'] = "Saudi Arabia"; |
||
414 | $iso3166['686'] = "Senegal"; |
||
415 | $iso3166['688'] = "Serbia"; |
||
416 | $iso3166['690'] = "Seychelles"; |
||
417 | $iso3166['694'] = "Sierra Leone"; |
||
418 | $iso3166['702'] = "Singapore"; |
||
419 | $iso3166['534'] = "Sint Maarten (Dutch part)"; |
||
420 | $iso3166['703'] = "Slovakia"; |
||
421 | $iso3166['705'] = "Slovenia"; |
||
422 | $iso3166['090'] = "Solomon Islands"; |
||
423 | $iso3166['706'] = "Somalia"; |
||
424 | $iso3166['710'] = "South Africa"; |
||
425 | $iso3166['239'] = "South Georgia and the South Sandwich Islands"; |
||
426 | $iso3166['728'] = "South Sudan"; |
||
427 | $iso3166['724'] = "Spain"; |
||
428 | $iso3166['144'] = "Sri Lanka"; |
||
429 | $iso3166['729'] = "Sudan"; |
||
430 | $iso3166['740'] = "Suriname"; |
||
431 | $iso3166['744'] = "Svalbard and Jan Mayen"; |
||
432 | $iso3166['752'] = "Sweden"; |
||
433 | $iso3166['756'] = "Switzerland"; |
||
434 | $iso3166['760'] = "Syrian Arab Republic"; |
||
435 | $iso3166['158'] = "Taiwan (Province of China)"; |
||
436 | $iso3166['762'] = "Tajikistan"; |
||
437 | $iso3166['834'] = "Tanzania, United Republic of"; |
||
438 | $iso3166['764'] = "Thailand"; |
||
439 | $iso3166['626'] = "Timor-Leste"; |
||
440 | $iso3166['768'] = "Togo"; |
||
441 | $iso3166['772'] = "Tokelau"; |
||
442 | $iso3166['776'] = "Tonga"; |
||
443 | $iso3166['780'] = "Trinidad and Tobago"; |
||
444 | $iso3166['788'] = "Tunisia"; |
||
445 | $iso3166['792'] = "Turkey"; |
||
446 | $iso3166['795'] = "Turkmenistan"; |
||
447 | $iso3166['796'] = "Turks and Caicos Islands"; |
||
448 | $iso3166['798'] = "Tuvalu"; |
||
449 | $iso3166['800'] = "Uganda"; |
||
450 | $iso3166['804'] = "Ukraine"; |
||
451 | $iso3166['784'] = "United Arab Emirates"; |
||
452 | $iso3166['826'] = "United Kingdom of Great Britain and Northern Ireland"; |
||
453 | $iso3166['840'] = "United States of America"; |
||
454 | $iso3166['581'] = "United States Minor Outlying Islands"; |
||
455 | $iso3166['858'] = "Uruguay"; |
||
456 | $iso3166['860'] = "Uzbekistan"; |
||
457 | $iso3166['548'] = "Vanuatu"; |
||
458 | $iso3166['862'] = "Venezuela (Bolivarian Republic of)"; |
||
459 | $iso3166['704'] = "Viet Nam"; |
||
460 | $iso3166['092'] = "Virgin Islands (British)"; |
||
461 | $iso3166['850'] = "Virgin Islands (U.S.)"; |
||
462 | $iso3166['876'] = "Wallis and Futuna"; |
||
463 | $iso3166['732'] = "Western Sahara"; |
||
464 | $iso3166['887'] = "Yemen"; |
||
465 | $iso3166['894'] = "Zambia"; |
||
466 | $iso3166['716'] = "Zimbabwe"; |
||
467 | |||
468 | // ISO/IEC 7816-5 AID decoder |
||
469 | |||
470 | $out = array(); |
||
471 | |||
472 | // A very good source about the coding |
||
473 | // https://blog.actorsfit.com/a?ID=00250-166ef507-edff-4400-8d0e-9e85d6ae2310 |
||
474 | |||
475 | $aid = strtoupper($aid); |
||
476 | $aid = trim($aid); |
||
477 | $aid = str_replace(' ','',$aid); |
||
40 | daniel-mar | 478 | $aid = str_replace(':','',$aid); |
35 | daniel-mar | 479 | |
480 | if ($aid == '') { |
||
36 | daniel-mar | 481 | $out[] = "INVALID: The AID is empty"; |
35 | daniel-mar | 482 | return $out; |
483 | } |
||
484 | |||
485 | if (!preg_match('@^[0-9A-F]+$@', $aid, $m)) { |
||
36 | daniel-mar | 486 | $out[] = "INVALID: AID has invalid characters. Only A..F and 0..9 are allowed"; |
35 | daniel-mar | 487 | return $out; |
488 | } |
||
489 | |||
40 | daniel-mar | 490 | $aid_hf = implode(':',str_split($aid,2)); |
35 | daniel-mar | 491 | if (strlen($aid)%2 == 1) $aid_hf .= '_'; |
40 | daniel-mar | 492 | |
35 | daniel-mar | 493 | $out[] = array("$aid", "ISO/IEC 7816-5 Application Identifier (AID)"); |
494 | $out[] = array('', "> $aid_hf <"); |
||
38 | daniel-mar | 495 | $out[] = array('', c_literal_hexstr($aid)); |
35 | daniel-mar | 496 | |
36 | daniel-mar | 497 | if ((strlen($aid) == 32) && (substr($aid,-2) == 'FF')) { |
498 | // https://www.kartenbezogene-identifier.de/content/dam/kartenbezogene_identifier/de/PDFs/RID_Antrag_2006.pdf |
||
499 | // writes: "Wenn die PIX aus 11 Bytes besteht, muss das letzte Byte einen Hexadezimal-Wert ungleich ´FF´ aufweisen (´FF´ ist von ISO reserviert)." |
||
500 | // https://www.etsi.org/deliver/etsi_ts/101200_101299/101220/07.03.00_60/ts_101220v070300p.pdf |
||
501 | // writes: According to ISO/IEC 7816-4, if the AID is 16 bytes long, then the value 'FF' for the least significant byte [...] is reserved for future use. |
||
502 | $out[] = array('',"INVALID: A 16-byte AID must not end with FF. (Reserved by ISO/IEC 7816-4)"); |
||
503 | } |
||
504 | |||
505 | if (strlen($aid) > 32) { |
||
506 | $out[] = array('',"INVALID: An AID must not be longer than 16 bytes"); |
||
507 | } |
||
508 | |||
35 | daniel-mar | 509 | $category = substr($aid,0,1); |
510 | |||
511 | // Category 0..9 |
||
512 | // RID = ISO/IEC 7812 Issuer Identification Number (IIN 6 or 8 digits) |
||
513 | // AID = RID + 'FF' + PIX |
||
514 | $iso7812_category = array(); |
||
515 | $iso7812_category['0'] = 'ISO/TC 68 and other industry assignments'; |
||
516 | $iso7812_category['1'] = 'Airlines'; |
||
517 | $iso7812_category['2'] = 'Airlines, financial and other future industry assignments'; |
||
518 | $iso7812_category['3'] = 'Travel and entertainment'; |
||
519 | $iso7812_category['4'] = 'Banking and financial'; |
||
520 | $iso7812_category['5'] = 'Banking and financial'; |
||
521 | $iso7812_category['6'] = 'Merchandising and banking/financial'; |
||
522 | $iso7812_category['7'] = 'Petroleum and other future industry assignments'; |
||
523 | $iso7812_category['8'] = 'Healthcare, telecommunications and other future industry assignments'; |
||
524 | $iso7812_category['9'] = 'Assignment by national standards bodies'; |
||
525 | foreach ($iso7812_category as $check_cat => $check_cat_name) { |
||
526 | if ("$category" == "$check_cat") { // comparison as string is important so that "===" works. "==" does not work because 0=='A' for some reason! |
||
527 | #$out[] = array($category, "AID based on category $category of ISO/IEC 7812 Issuer Identification Number (IIN)"); |
||
528 | #$out[] = array('', "($check_cat = $check_cat_name)"); |
||
529 | $out[] = array('', "AID based on ISO/IEC 7812 Issuer Identification Number (IIN)"); |
||
530 | |||
531 | $iin = $aid; |
||
532 | // IIN and PIX must be delimited with FF, but only if a PIX is available. |
||
533 | // When the IIN has an odd number, then an extra 'F' must be added at the end |
||
534 | $pos = strpos($aid,'F'); |
||
535 | if ($pos !== false) $iin = substr($iin, 0, $pos); |
||
536 | |||
537 | if (!_is_bcd($iin)) { |
||
36 | daniel-mar | 538 | $out[] = array($iin, "INVALID: Expected BCD encoded IIN, optionally followed by FF and PIX"); |
35 | daniel-mar | 539 | return $out; |
540 | } |
||
541 | |||
542 | $pad = ''; |
||
543 | |||
544 | $out[] = 'RID-HERE'; // will (must) be replaced below |
||
545 | |||
546 | $out[] = array($iin, "ISO/IEC 7812 Issuer Identification Number (IIN)"); |
||
547 | if ((strlen($iin) != 6) && (strlen($iin) != 8)) { |
||
548 | $out[] = array('',"Warning: IIN has an unusual length. 6 or 8 digits are expected!"); |
||
549 | } |
||
550 | |||
36 | daniel-mar | 551 | $out[] = array($category, "IIN Category $category = $check_cat_name"); |
552 | $pad .= str_repeat(' ', strlen("$category")); |
||
35 | daniel-mar | 553 | |
36 | daniel-mar | 554 | if ("$category" === "9") { |
35 | daniel-mar | 555 | $country = substr($iin,1,3); |
556 | if ($country == '') { |
||
36 | daniel-mar | 557 | $out[] = array($pad.'___', 'ISO/IEC 3166-1 Numeric Country code (missing)'); |
35 | daniel-mar | 558 | } else { |
559 | $country_name = isset($iso3166[$country]) ? $iso3166[$country] : 'Unknown country'; |
||
36 | daniel-mar | 560 | $out[] = array($pad.str_pad($country,3,'_',STR_PAD_RIGHT), "ISO/IEC 3166-1 Numeric Country code : $country ($country_name)"); |
35 | daniel-mar | 561 | } |
36 | daniel-mar | 562 | $pad .= ' '; |
563 | $asi = substr($iin,4); |
||
564 | $asn = $asi; |
||
35 | daniel-mar | 565 | } else { |
36 | daniel-mar | 566 | $asi = substr($iin,1); |
567 | $asn = $asi; |
||
35 | daniel-mar | 568 | } |
36 | daniel-mar | 569 | $out[] = array("$pad$asn", 'Assigned number'.($asi=='' ? ' (missing)' : '')); |
38 | daniel-mar | 570 | if ($asi!='') $out[] = array('', c_literal_hexstr($asi)); |
36 | daniel-mar | 571 | $pad .= str_repeat(' ',strlen($asn)); |
35 | daniel-mar | 572 | |
573 | $padded_iin = $iin; |
||
574 | if (strlen($iin)%2 != 0) { |
||
575 | $odd_padding = substr($aid,strlen($iin),1); |
||
576 | if ($odd_padding != 'F') { |
||
577 | foreach ($out as $n => &$tmp) { |
||
578 | if ($tmp == 'RID-HERE') { |
||
579 | unset($out[$n]); |
||
580 | break; |
||
581 | } |
||
582 | } |
||
36 | daniel-mar | 583 | $out[] = array("$pad!","INVALID: An IIN with odd length must be padded with F, e.g. 123 => 123F"); |
35 | daniel-mar | 584 | return $out; |
585 | } |
||
586 | $out[] = array($pad.$odd_padding, 'Padding of IIN with odd length'); |
||
587 | $padded_iin .= $odd_padding; |
||
588 | $pad .= ' '; |
||
589 | } |
||
590 | |||
591 | $rid = $padded_iin; |
||
592 | foreach ($out as &$tmp) { |
||
593 | if ($tmp == 'RID-HERE') { |
||
594 | $tmp = array("$rid", "Registered Application Provider Identifier (RID)"); |
||
595 | break; |
||
596 | } |
||
597 | } |
||
598 | |||
599 | if (strlen($aid) == strlen($padded_iin)) { |
||
600 | // There is no PIX |
||
601 | $out[] = "Proprietary application identifier extension (PIX) missing"; |
||
602 | } else { |
||
603 | $delimiter = substr($aid,strlen($padded_iin),2); |
||
604 | if ($delimiter != 'FF') { |
||
36 | daniel-mar | 605 | $out[] = array($pad.substr($aid,strlen($padded_iin)), "INVALID: RID/IIN and PIX must be delimited by FF"); |
35 | daniel-mar | 606 | return $out; |
607 | } |
||
608 | $out[] = array($pad.$delimiter, 'Delimiter which separates RID/IIN from PIX'); |
||
609 | $pad .= str_repeat(' ',strlen($delimiter)); |
||
610 | |||
611 | $pix = substr($aid,strlen($padded_iin)+strlen('FF')); |
||
612 | if ($pix == '') { |
||
613 | $out[] = "Proprietary application identifier extension (PIX) missing"; |
||
36 | daniel-mar | 614 | $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 | 615 | } else { |
616 | $out[] = array($pad.$pix, "Proprietary application identifier extension (PIX)"); |
||
38 | daniel-mar | 617 | $out[] = array('', c_literal_hexstr($pix)); |
35 | daniel-mar | 618 | } |
619 | } |
||
620 | |||
621 | return $out; |
||
622 | } |
||
623 | } |
||
624 | |||
625 | // Category 'A' (International Registration) |
||
626 | // RID = 'A' + 9 digits |
||
627 | // AID = RID + PIX |
||
628 | if ("$category" === "A") { |
||
629 | $rid = substr($aid,0,10); |
||
630 | $rid = str_pad($rid,10,'_',STR_PAD_RIGHT); |
||
631 | |||
632 | $pix = substr($aid,10); |
||
633 | |||
36 | daniel-mar | 634 | $asi = substr($aid,1,9); |
635 | $asn = str_pad($asi,9,'_',STR_PAD_RIGHT); |
||
35 | daniel-mar | 636 | |
637 | $out[] = array("$rid", "Registered Application Provider Identifier (RID)"); |
||
638 | $out[] = array("$category", "Category $category: International registration"); |
||
36 | daniel-mar | 639 | $out[] = array(" $asn", 'Assigned number, BCD recommended'.($asi=='' ? ' (missing)' : '')); |
38 | daniel-mar | 640 | if ($asi!='') $out[] = array('', c_literal_hexstr($asi)); |
35 | daniel-mar | 641 | if ($pix == '') { |
642 | $out[] = "Proprietary application identifier extension (PIX) missing"; |
||
643 | } else { |
||
644 | $out[] = array(str_pad($pix,strlen($aid),' ',STR_PAD_LEFT), "Proprietary application identifier extension (PIX)"); |
||
38 | daniel-mar | 645 | $out[] = array('', c_literal_hexstr($pix)); |
35 | daniel-mar | 646 | } |
647 | |||
648 | return $out; |
||
649 | } |
||
650 | |||
651 | // Category 'D' (Local/National Registration) |
||
652 | // RID = 'D' + 3 digits country code (ISO/IEC 3166-1) + 6 digits |
||
653 | // AID = RID + PIX |
||
654 | if ("$category" === "D") { |
||
655 | $rid = substr($aid,0,10); |
||
656 | $rid = str_pad($rid,10,'_',STR_PAD_RIGHT); |
||
657 | |||
658 | $pix = substr($aid,10); |
||
659 | |||
660 | $country = substr($aid,1,3); |
||
661 | |||
36 | daniel-mar | 662 | $asi = substr($aid,4,6); |
663 | $asn = str_pad($asi,6,'_',STR_PAD_RIGHT); |
||
35 | daniel-mar | 664 | |
665 | $out[] = array("$rid", "Registered Application Provider Identifier (RID)"); |
||
666 | $out[] = array("$category", "Category $category: Local/National registration"); |
||
667 | if ($country == '') { |
||
668 | $out[] = array(" ___", "ISO/IEC 3166-1 Numeric Country code (missing)"); |
||
669 | } else { |
||
670 | $country_name = isset($iso3166[$country]) ? $iso3166[$country] : 'Unknown country'; |
||
671 | $out[] = array(" ".str_pad($country,3,'_',STR_PAD_RIGHT), "ISO/IEC 3166-1 Numeric Country code : $country ($country_name)"); |
||
672 | } |
||
36 | daniel-mar | 673 | $out[] = array(" $asn", 'Assigned number, BCD recommended'.($asi=='' ? ' (missing)' : '')); |
38 | daniel-mar | 674 | if ($asi!='') $out[] = array('', c_literal_hexstr($asi)); |
35 | daniel-mar | 675 | if ($pix == '') { |
676 | $out[] = "Proprietary application identifier extension (PIX) missing"; |
||
677 | } else { |
||
678 | $out[] = array(str_pad($pix,strlen($aid),' ',STR_PAD_LEFT), "Proprietary application identifier extension (PIX)"); |
||
38 | daniel-mar | 679 | $out[] = array('', c_literal_hexstr($pix)); |
35 | daniel-mar | 680 | } |
681 | |||
682 | return $out; |
||
683 | } |
||
684 | |||
40 | daniel-mar | 685 | // Category 'E' |
686 | // AID = 'E8' + OID + PIX (OID is DER encoding without type and length) |
||
687 | if ("$category" === "E") { |
||
688 | $out[] = array("$category", "Category $category: Standard"); |
||
689 | |||
41 | daniel-mar | 690 | $std_schema = substr($aid,1,1); |
691 | if ($std_schema == '8') { |
||
692 | $out[] = array(" $std_schema", "Standard identified by OID"); |
||
40 | daniel-mar | 693 | |
694 | $data = substr($aid,2); |
||
695 | try { |
||
696 | $interpretations = _aid_e8_interpretations($aid); |
||
697 | foreach ($interpretations as $ii => $interpretation) { |
||
698 | $pos = $interpretation[0]; |
||
699 | $txt1 = $interpretation[1]; // Standard |
||
700 | $txt2 = $interpretation[2]; // PIX (optional) |
||
701 | |||
702 | $aid1 = ' '.substr($aid,2,$pos*2); |
||
703 | $aid2 = substr($aid,2+$pos*2); |
||
704 | |||
705 | $out[] = array("$aid1", "$txt1"); |
||
706 | if ($txt2 !== '') { |
||
707 | $pix = "'$txt2' (".c_literal_hexstr(str_replace(':','',$txt2)).")"; |
||
708 | $out[] = array(str_repeat(' ',strlen($aid1))."$aid2", "with PIX $pix"); |
||
709 | } |
||
710 | if ($ii < count($interpretations)-1) { |
||
711 | $out[] = array('', 'or:'); |
||
712 | } |
||
713 | } |
||
714 | } catch (Exception $e) { |
||
715 | $out[] = array(" $data", "ERROR: ".$e->getMessage()); |
||
716 | |||
717 | } |
||
41 | daniel-mar | 718 | } else if ($std_schema != '') { |
40 | daniel-mar | 719 | // E0..E7, E9..EF are RFU |
720 | $unknown = substr($aid,1); |
||
721 | $out[] = array(" $unknown", "ILLEGAL USAGE / RESERVED"); |
||
722 | } |
||
723 | |||
724 | return $out; |
||
725 | } |
||
726 | |||
35 | daniel-mar | 727 | // Category 'F' |
728 | // AID = 'F' + PIX |
||
729 | if ("$category" === "F") { |
||
730 | $out[] = array("$category", "Category $category: Non-registered / Proprietary"); |
||
731 | $rid = substr($aid,0,1); |
||
732 | $pix = substr($aid,1); |
||
733 | if ($pix == '') { |
||
734 | $out[] = "Proprietary application identifier extension (PIX) missing"; |
||
735 | } else { |
||
736 | $out[] = array(' '.$pix, "Proprietary application identifier extension (PIX)"); |
||
38 | daniel-mar | 737 | $out[] = array('', c_literal_hexstr($pix)); |
35 | daniel-mar | 738 | } |
739 | return $out; |
||
740 | } |
||
741 | |||
40 | daniel-mar | 742 | // Category 'B' and 'C' are reserved |
35 | daniel-mar | 743 | $out[] = array("$category", "Category $category: ILLEGAL USAGE / RESERVED"); |
744 | if (strlen($aid) > 1) { |
||
38 | daniel-mar | 745 | $aid_ = substr($aid,1); |
746 | $out[] = array(" ".$aid_, "Unknown composition"); |
||
747 | $out[] = array('', c_literal_hexstr($aid_)); |
||
35 | daniel-mar | 748 | } |
749 | return $out; |
||
750 | } |
||
37 | daniel-mar | 751 | |
752 | /* --- Small extra function: not part of the decoder --- */ |
||
753 | |||
754 | function aid_split_rid_pix($a, &$rid=null, &$pix=null) { |
||
755 | // "Quick'n'Dirty" function which does not do any consistency checks! |
||
756 | // It expects that the string is a valid AID! |
||
757 | |||
758 | $cat = substr($a,0,1); |
||
759 | if (is_numeric($cat)) { |
||
760 | $p = strpos($a,'F'); |
||
761 | if ($p%2 != 0) $p++; |
||
762 | } else if (($cat == 'A') || ($cat == 'D')) { |
||
763 | $p = 10; |
||
764 | } else if ($cat == 'F') { |
||
765 | $p = 1; |
||
766 | } else { |
||
767 | $p = 0; |
||
768 | } |
||
769 | |||
770 | if ($rid !== null) $rid = substr($a, 0, $p); |
||
771 | if ($pix !== null) $pix = substr($a, $p); |
||
772 | |||
773 | return $p; |
||
774 | } |