This repository was archived by the owner on May 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseATR.py
1385 lines (1072 loc) · 33 KB
/
parseATR.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python
"""
parseATR: convert an ATR in a human readable format
Copyright (C) 2009-2016 Ludovic Rousseau
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
from __future__ import print_function
import re
ATR_PROTOCOL_TYPE_T0 = 0
ATR_MAX_PROTOCOLS = 7
T = -1
class ParseAtrException(Exception):
""" Base class for exceptions in this module """
def __init__(self, text):
self.text = text
def __str__(self):
return self.text
def toHexString(bytes):
"""Returns a hex list
Args:
bytes: list of bytes (integers)
Returns:
string representing the bytes in hexadecimal
>>> toHexString([1,2,3, 10, 255])
'01 02 03 0A FF'
"""
return " ".join(["%02X" % b for b in bytes])
def toASCIIString(bytes):
"""Returns a string.
Args:
bytes: list of bytes (integers)
Returns:
string representing the list of bytes in ASCII. Non ASCII
characters are replaced with .
>>> toASCIIString([72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33])
'Hello world!'
"""
ascii = ""
for b in bytes:
if b > 31 and b < 127:
ascii += chr(b)
else:
ascii += '.'
return ascii
def normalize(atr):
"""Transform an ATR in list of integers.
valid input formats are
"3B A7 00 40 18 80 65 A2 08 01 01 52"
"3B:A7:00:40:18:80:65:A2:08:01:01:52"
Args:
atr: string
Returns:
list of bytes
>>> normalize("3B:A7:00:40:18:80:65:A2:08:01:01:52")
[59, 167, 0, 64, 24, 128, 101, 162, 8, 1, 1, 82]
"""
atr = atr.replace(":", "")
atr = atr.replace(" ", "")
res = list()
while len(atr) >= 2:
byte, atr = atr[:2], atr[2:]
res.append(byte)
if len(atr) > 0:
raise ParseAtrException('warning: odd string, remainder: %r' % atr)
atr = [int(x, 16) for x in res]
return atr
def int2bin(i, padding=8):
"""Converts an integer into its binary representation
Args:
i: integer value
padding: minimum number of digits (default value 8)
Returns:
string representation of i in binary
>>> int2bin(2015)
'11111011111'
>>> int2bin(42)
'00101010'
"""
b = ""
while i > 0:
b = str(i % 2) + b
i >>= 1
b = "0" * (padding - len(b)) + b
return b
def parseATR(atr_txt):
"""Parses an ATR
Args:
atr_txt: ATR as a hex bytes string
Returns:
dictionary of field and values
>>> parseATR("3B A7 00 40 18 80 65 A2 08 01 01 52")
{'hbn': 7, 'TB1': 0, 'TC2': 24, 'TS': 59, 'T0': 167,
'atr': [59, 167, 0, 64, 24, 128, 101, 162, 8, 1, 1, 82],
'hb': [128, 101, 162, 8, 1, 1, 82], 'TD1': 64, 'pn': 2}
"""
atr_txt = normalize(atr_txt)
atr = {}
# the ATR itself as a list of integers
atr["atr"] = atr_txt
# store TS and T0
atr["TS"] = atr_txt[0]
atr["T0"] = TDi = atr_txt[1]
hb_length = atr["T0"] & 15
pointer = 1
# protocol number
pn = 1
# store number of historical bytes
atr["hbn"] = TDi & 0xF
while (pointer < len(atr_txt)):
# Check TAi is present
if ((TDi | 0xEF) == 0xFF):
pointer += 1
atr["TA%d" % pn] = atr_txt[pointer]
# Check TBi is present
if ((TDi | 0xDF) == 0xFF):
pointer += 1
atr["TB%d" % pn] = atr_txt[pointer]
# Check TCi is present
if ((TDi | 0xBF) == 0xFF):
pointer += 1
atr["TC%d" % pn] = atr_txt[pointer]
# Check TDi is present
if ((TDi | 0x7F) == 0xFF):
pointer += 1
atr["TD%d" % pn] = TDi = atr_txt[pointer]
if ((TDi & 0x0F) != ATR_PROTOCOL_TYPE_T0):
atr["TCK"] = True
pn += 1
else:
break
# Store number of protocols
atr["pn"] = pn
# Store historical bytes
atr["hb"] = atr_txt[pointer + 1: pointer + 1 + hb_length]
# Store TCK
last = pointer + 1 + hb_length
if "TCK" in atr:
try:
atr["TCK"] = atr_txt[last]
except IndexError:
atr["TCK"] = -1
last += 1
if len(atr_txt) > last:
atr["extra"] = atr_txt[last:]
if len(atr["hb"]) < hb_length:
missing = hb_length - len(atr["hb"])
if missing > 1:
(t1, t2) = ("s", "are")
else:
(t1, t2) = ("", "is")
atr["warning"] = "ATR is truncated: %d byte%s %s missing" % (missing, t1, t2)
return atr
def TA1(v):
"""Parse TA1 value
Args:
v: TA1
Returns:
value according to ISO 7816-3
>>> TA1(0x11)
['Fi=%s, Di=%s, %g cycles/ETU (%d bits/s at 4.00 MHz, %d bits/s for fMax=%d MHz)', (372, 1, 372, 10752, 13440, 5)]
"""
Fi = (372, 372, 558, 744, 1116, 1488, 1860, "RFU", "RFU", 512, 768, 1024,
1536, 2048, "RFU", "RFU")
Di = ("RFU", 1, 2, 4, 8, 16, 32, 64, 12, 20, "RFU", "RFU", "RFU", "RFU",
"RFU", "RFU")
FMax = (4, 5, 6, 8, 12, 16, 20, "RFU", "RFU", 5, 7.5, 10, 15, 20, "RFU",
"RFU")
F = v >> 4
D = v & 0xF
text = "Fi=%s, Di=%s"
args = (Fi[F], Di[D])
if "RFU" in [Fi[F], Di[D]]:
text += ", INVALID VALUE"
else:
value = Fi[F] / Di[D]
text += ", %g cycles/ETU (%d bits/s at 4.00 MHz, %d bits/s for fMax=%d MHz)"
args += (value, 4000000 / value, FMax[F] * 1000000 / value, FMax[F])
return [text, args]
def TA2(v):
"""Parse TA2 value
Args:
v: TA2
Returns:
value according to ISO 7816-3
>>> TA2(1)
'Protocol to be used in spec mode: T=1 - Capable to change - defined by interface bytes'
"""
F = v >> 4
D = v & 0xF
text = ["Protocol to be used in spec mode: T=%s" % (D)]
if (F & 0x8):
text.append(" - Unable to change")
else:
text.append(" - Capable to change")
if (F & 0x1):
text.append(" - implicity defined")
else:
text.append(" - defined by interface bytes")
return ''.join(text)
def TA3(v):
"""Parse TA3 value
Args:
v: TA3
Returns:
value according to ISO 7816-3
"""
return TAn(3, v)
def TA4(v):
"""Parse TA4 value
Args:
v: TA4
Returns:
value according to ISO 7816-3
"""
return TAn(4, v)
def TA5(v):
"""Parse TA5 value
Args:
v: TA5
Returns:
value according to ISO 7816-3
"""
return TAn(5, v)
def TAn(i, v):
"""Parse TAi (3 <= i <= 5)
Args:
i: i
v: value of TAi
Returns:
value according to ISO 7816-3
"""
XI = ("not supported", "state L", "state H", "no preference")
if (T == 1):
text = "IFSC: %s"
args = (v)
else:
F = v >> 6
D = v % 64
Class = ["(3G) "]
if (D & 0x1):
Class.append("A 5V ")
if (D & 0x2):
Class.append("B 3V ")
if (D & 0x4):
Class.append("C 1.8V ")
if (D & 0x8):
Class.append("D RFU ")
if (D & 0x10):
Class.append("E RFU")
text = "Clock stop: %s - Class accepted by the card: %s"
args = (XI[F], ''.join(Class))
return [text, args]
def TB1(v):
"""Parse TB1 value
Args:
v: TB1
Returns:
value according to ISO 7816-3
"""
I_tab = { 0: "25 milliAmperes",
1: "50 milliAmperes",
2: "100 milliAmperes",
3: "RFU"
}
I = (v >> 5) & 3
PI = v & 0x1F
if (PI == 0):
text = "VPP is not electrically connected"
else:
text = "Programming Param P: %d Volts, I: %s" % (PI, I_tab[I])
return text
def TB2(v):
"""Parse TB2 value
Args:
v: TB2
Returns:
value according to ISO 7816-3
"""
text = ["Programming param PI2 (PI1 should be ignored): %d" % v, ]
if ((v > 49) or (v < 251)):
text.append(" (dV)")
else:
text.append(" is RFU")
return ''.join(text)
def TB3(v):
"""Parse TB3 value
Args:
v: TB3
Returns:
value according to ISO 7816-3
"""
return TBn(3, v)
def TB4(v):
"""Parse TB4 value
Args:
v: TB4
Returns:
value according to ISO 7816-3
"""
return TBn(4, v)
def TB5(v):
"""Parse TB5 value
Args:
v: TB5
Returns:
value according to ISO 7816-3
"""
return TBn(5, v)
def TBn(i, v):
"""Parse TBi (3 <= i <= 5)
Args:
i: i
v: value of TBi
Returns:
value according to ISO 7816-3
"""
text = "Undocumented"
args = list()
if (T == 1):
BWI = v >> 4
CWI = v % 16
text = "Block Waiting Integer: %d - Character Waiting Integer: %d"
args = (BWI, CWI)
else:
if (i > 2 and T == 15):
# see ETSI TS 102 221 V8.3.0 (2009-08)
# Smart Cards; UICC-Terminal interface;
# Physical and logical characteristics (Release 8)
texts = {0x00: "No additional global interface parameters supported",
0x88: "Secure Channel supported as defined in TS 102 484",
0x8C: "Secured APDU - Platform to Platform required as defined in TS 102 484",
0x82: "eUICC-related functions supported",
0x90: "Low Impedance drivers and protocol available on the I/O line available (see clause 7.2.1)",
0xA0: "UICC-CLF interface supported as defined in TS 102 613",
0xC0: "Inter-Chip USB UICC-Terminal interface supported as defined in TS 102 600"}
text = texts.get(v, "RFU")
return [text, args]
def TC1(v):
"""Parse TC1 value
Args:
v: TC1
Returns:
value according to ISO 7816-3
"""
text = "Extra guard time: %d"
args = v
if (v == 255):
text += " (special value)"
return [text, args]
def TC2(v):
"""Parse TC2 value
Args:
v: TC2
Returns:
value according to ISO 7816-3
"""
return "Work waiting time: 960 x %d x (Fi/F)" % v
def TC3(v):
"""Parse TC3 value
Args:
v: TC3
Returns:
value according to ISO 7816-3
"""
return TCn(3, v)
def TC4(v):
"""Parse TC4 value
Args:
v: TC4
Returns:
value according to ISO 7816-3
"""
return TCn(4, v)
def TC5(v):
"""Parse TC5 value
Args:
v: TC5
Returns:
value according to ISO 7816-3
"""
return TCn(5, v)
def TCn(i, v):
"""Parse TCi (3 <= i <= 5)
Args:
i: i
v: value of TCi
Returns:
value according to ISO 7816-3
"""
text = list()
args = list()
if (T == 1):
text.append("Error detection code: %s")
if (v == 1):
args = "CRC"
else:
if (v == 0):
args = "LRC"
else:
args = "RFU"
return [''.join(text), args]
def TD1(v):
"""Parse TD1 value
Args:
v: TD1
Returns:
value according to ISO 7816-3
"""
return TDn(1, v)
def TD2(v):
"""Parse TD2 value
Args:
v: TD2
Returns:
value according to ISO 7816-3
"""
return TDn(2, v)
def TD3(v):
"""Parse TD3 value
Args:
v: TD3
Returns:
value according to ISO 7816-3
"""
return TDn(3, v)
def TD4(v):
"""Parse TD4 value
Args:
v: TD4
Returns:
value according to ISO 7816-3
"""
return TDn(4, v)
def TD5(v):
"""Parse TD5 value
Args:
v: TD5
Returns:
value according to ISO 7816-3
"""
return TDn(5, v)
def TDn(i, v):
"""Parse TDi (1 <= i <= 5)
Args:
i: i
v: value of TDi
Returns:
value according to ISO 7816-3
"""
global T
Y = v >> 4
T = v & 0xF
text = "Y(i+1) = b%s, Protocol T=%d"
args = (int2bin(Y, 4), T)
if 1 == i and 15 == T:
text += " (INVALID VALUE for TD1)"
return [text, args]
def life_cycle_status(lcs):
"""Life Cycle Status
# Table 13 - Life cycle status byte
# ISO 7816-4:2004, page 21
Args:
lcs: Life Cycle Status
Returns:
Text value
>>> life_cycle_status(5)
'Operational state (activated)'
"""
text = "Unknown"
if lcs > 15:
text = "Proprietary"
if lcs == 0:
text = "No information given"
if lcs == 1:
text = "Creation state"
if lcs == 3:
text = "Initialisation state"
if lcs in [4, 6]:
text = "Operational state (deactivated)"
if lcs in [5, 7]:
text = "Operational state (activated)"
if lcs in [12, 13, 14, 15]:
text = "Termination state"
return text
def data_coding(dc):
"""Data Coding
# Table 87 - Second software function table (data coding byte)
# ISO 7816-4:2004, page 60
Args:
dc: data coding
Returns:
Text value
"""
text = list()
if dc & 128:
text.append(" - EF of TLV structure supported\n")
# get bits 6 and 7
text.append(" - Behaviour of write functions: ")
v = (dc & (64 + 32)) >> 5
t = ["one-time write\n", "proprietary\n", "write OR\n", "write AND\n"]
text.append(t[v])
text.append(" - Value 'FF' for the first byte of BER-TLV tag fields: ")
if dc & 16:
text.append("in")
text.append("valid\n")
text.append(" - Data unit in quartets: %d\n" % (dc & 15))
return ''.join(text)
def selection_methods(sm):
"""Selection Methods
# Table 86 - First software function table (selection methods)
# ISO 7816-4:2004, page 60
Args:
sm: Selection Methods
Returns:
Text value
"""
text = list()
if sm & 1:
text.append(" - Record identifier supported\n")
if sm & 2:
text.append(" - Record number supported\n")
if sm & 4:
text.append(" - Short EF identifier supported\n")
if sm & 8:
text.append(" - Implicit DF selection\n")
if sm & 16:
text.append(" - DF selection by file identifier\n")
if sm & 32:
text.append(" - DF selection by path\n")
if sm & 64:
text.append(" - DF selection by partial DF name\n")
if sm & 128:
text.append(" - DF selection by full DF name\n")
return ''.join(text)
def selection_mode(sm):
"""Selection Mode
# Table 87 - Second software function table (data coding byte)
# ISO 7816-4:2004, page 60
Args:
sm: Selection Mode
Returns:
Text value
"""
text = list()
if sm & 1:
text.append(" - Record identifier supported\n")
if sm & 2:
text.append(" - Record number supported\n")
if sm & 4:
text.append(" - Short EF identifier supported\n")
if sm & 8:
text.append(" - Implicit DF selection\n")
if sm & 16:
text.append(" - DF selection by file identifier\n")
if sm & 32:
text.append(" - DF selection by path\n")
if sm & 64:
text.append(" - DF selection by partial DF name\n")
if sm & 128:
text.append(" - DF selection by full DF name\n")
return ''.join(text)
def command_chaining(cc):
"""Command Chaining
# Table 88 - Third software function table (command chaining,
# length fields and logical channels)
# ISO 7816-4:2004, page 61
Args:
cc: Command Chaining
Returns:
Text value
"""
text = list()
if cc & 128:
text.append(" - Command chaining\n")
if cc & 64:
text.append(" - Extended Lc and Le fields\n")
if cc & 32:
text.append(" - RFU (should not happen)\n")
v = (cc >> 3) & 3
t = ["No logical channel\n", "by the interface device\n", "by the card\n",
"by the interface device and card\n"]
text.append(" - Logical channel number assignment: " + t[v])
text.append(" - Maximum number of logical channels: %d\n" % (1 + cc & 7))
return ''.join(text)
def card_service(cs):
"""Card Service
# Table 85 - Card service data byte
# ISO 7816-4:2004, page 59
Args:
ccs Card Service
Returns:
Text value
"""
text = list()
if cs & 128:
text.append(" - Application selection: by full DF name\n")
if cs & 64:
text.append(" - Application selection: by partial DF name\n")
if cs & 32:
text.append(" - BER-TLV data objects available in EF.DIR\n")
if cs & 16:
text.append(" - BER-TLV data objects available in EF.ATR\n")
text.append(" - EF.DIR and EF.ATR access services: ")
v = (cs >> 1) & 7
if v == 4:
text.append("by READ BINARY command\n")
elif v == 2:
text.append("by GET DATA command\n")
elif v == 0:
text.append("by GET RECORD(s) command\n")
else:
text.append("reverved for future use\n")
if cs & 1:
text.append(" - Card without MF\n")
else:
text.append(" - Card with MF\n")
return ''.join(text)
def safe_get(historical_bytes, number):
"""Get bytes from historical bytes without crashing if not enough
bytes are provided
Args:
historical_bytes
number
Returns:
list of values
>>> safe_get([1,2,3], 2)
[1, 2]
>>> safe_get([1,2,3], 4)
[1, 2, 3, 0]
"""
result = list()
for i in range(number):
try:
v = historical_bytes[i]
except IndexError:
v = 0
result.append(v)
return result
def compact_tlv(atr, historical_bytes):
"""Compact TLV
Args:
historical_bytes
Returns:
list of text values
>>> compact_tlv({}, [101, 162, 8, 1, 1, 82])
[' Tag: 6, Len: 5 (%s)\\n Data: %s "%s"\\n', ('pre-issuing data', 'A2 08 01 01 52', '....R')]
"""
text = ""
tlv = historical_bytes.pop(0)
# return if we have NO historical bytes
if tlv is None:
return text
tag = int(tlv / 16)
len = tlv % 16
text = list()
args = list()
text.append(" Tag: %d, Len: %d (%%s)\n" % (tag, len))
if tag == 1:
args.append("country code, ISO 3166-1")
text.append(" Country code: %s\n")
args.append(toHexString(historical_bytes[:len]))
elif tag == 2:
args.append("issuer identification number, ISO 7812-1")
text.append(" Issuer identification number: %s\n")
args.append(toHexString(historical_bytes[:len]))
elif tag == 3:
args.append("card service data byte")
try:
cs = historical_bytes[0]
except IndexError:
warning = "Error in the ATR: expecting 1 byte and got 0\n"
text.append(warning)
atr["warning"] = warning
else:
text.append(" Card service data byte: %d\n%s")
args += (cs, card_service(cs))
elif tag == 4:
args.append("initial access data")
text.append(" Initial access data: %s \"%s\"\n")
args.append(toHexString(historical_bytes[:len]))
args.append(toASCIIString(historical_bytes[:len]))
elif tag == 5:
args.append("card issuer's data")
text.append(" Card issuer data: %s \"%s\"\n")
args.append(toHexString(historical_bytes[:len]))
args.append(toASCIIString(historical_bytes[:len]))
elif tag == 6:
args.append("pre-issuing data")
text.append(" Data: %s \"%s\"\n")
args.append(toHexString(historical_bytes[:len]))
args.append(toASCIIString(historical_bytes[:len]))
elif tag == 7:
args.append("card capabilities")
if len == 1:
try:
sm = historical_bytes[0]
except IndexError:
warning = "Error in the ATR: expecting 1 byte and got 0\n"
text.append(warning)
atr["warning"] = warning
else:
text.append(" Selection methods: %d\n%s")
args.append(sm)
args.append(selection_mode(sm))
elif len == 2:
(sm, dc) = safe_get(historical_bytes, 2)
text.append(" Selection methods: %d\n%s")
args.append(sm)
args.append(selection_methods(sm))
text.append(" Data coding byte: %d\n%s")
args.append(dc)
args.append(data_coding(dc))
elif len == 3:
(sm, dc, cc) = safe_get(historical_bytes, 3)
text.append(" Selection methods: %d\n%s")
args.append(sm)
args.append(selection_mode(sm))
text.append(" Data coding byte: %d\n%s")
args.append(dc)
args.append(data_coding(dc))
text.append(" Command chaining, length fields and logical channels: %d\n%s")
args.append(cc)
args.append(command_chaining(cc))
else:
text.append(" wrong ATR")
elif tag == 8:
args.append("status indicator")
if len == 1:
lcs = historical_bytes[0]
text.append(" LCS (life card cycle): %d\n")
args.append(lcs)
elif len == 2:
(sw1, sw2) = safe_get(historical_bytes, 2)
text.append(" SW: %s")
args.append("%02X %02X" % (sw1, sw2))
elif len == 3:
(lcs, sw1, sw2) = safe_get(historical_bytes, 3)
text.append(" LCS (life card cycle): %d\n")
args.append(lcs)
text.append(" SW: %s")
args.append("%02X %02X" % (sw1, sw2))
elif tag == 15:
args.append("application identifier")
text.append(" Application identifier: %s \"%s\"\n")
args.append(toHexString(historical_bytes[:len]))
args.append(toASCIIString(historical_bytes[:len]))
else:
args.append("unknown")
text.append(" Value: %s \"%s\"\n")
args.append(toHexString(historical_bytes[:len]))
args.append(toASCIIString(historical_bytes[:len]))
# consume len bytes of historic
del historical_bytes[0:len]
return [''.join(text), tuple(args)]
def analyse_histrorical_bytes(atr, historical_bytes):
"""Analyse Historical Bytes
Args:
historical_bytes: list of bytes
Returns:
list
>>> analyse_histrorical_bytes({}, [128, 101, 162, 8, 1, 1, 82])