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