1 """ISO7816-4 error checking strategy.
2
3 __author__ = "http://www.gemalto.com"
4
5 Copyright 2001-2011 gemalto
6 Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
7
8 This file is part of pyscard.
9
10 pyscard is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14
15 pyscard is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with pyscard; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 """
24
25 from smartcard.sw.ErrorChecker import ErrorChecker
26 import smartcard.sw.SWExceptions
27
28 iso7816_4SW = {
29 0x62: (smartcard.sw.SWExceptions.WarningProcessingException,
30 {0x00: "Response padded/ More APDU commands expected",
31 0x81: "Part of returned data may be corrupted",
32 0x82: "End of file/record reached before reading Le bytes",
33 0x83: "File invalidated",
34 0x84: "FCI not correctly formatted",
35 0xFF: "Correct execution, response padded"}),
36
37 0x63: (smartcard.sw.SWExceptions.WarningProcessingException,
38 {0x00: "Authentication failed",
39 0x81: "File filled up by the last write",
40 0xC0: "PIN verification failed. 0 tries remaining before blocking PIN",
41 0xC1: "PIN verification failed. 1 tries remaining before blocking PIN",
42 0xC2: "PIN verification failed. 2 tries remaining before blocking PIN",
43 0xC3: "PIN verification failed. 3 tries remaining before blocking PIN",
44 0xC4: "PIN verification failed. 4 tries remaining before blocking PIN",
45 0xC5: "PIN verification failed. 5 tries remaining before blocking PIN",
46 0xC6: "PIN verification failed. 6 tries remaining before blocking PIN",
47 0xC7: "PIN verification failed. 7 tries remaining before blocking PIN",
48 0xC8: "PIN verification failed. 8 tries remaining before blocking PIN",
49 0xC9: "PIN verification failed. 9 tries remaining before blocking PIN",
50 0xCA: "PIN verification failed. 10 tries remaining before blocking PIN",
51 0xCB: "PIN verification failed. 11 tries remaining before blocking PIN",
52 0xCC: "PIN verification failed. 12 tries remaining before blocking PIN",
53 0xCD: "PIN verification failed. 13 tries remaining before blocking PIN",
54 0xCE: "PIN verification failed. 14 tries remaining before blocking PIN",
55 0xCF: "PIN verification failed. 15 tries remaining before blocking PIN"}),
56
57 0x64: (smartcard.sw.SWExceptions.ExecutionErrorException,
58 {0x00: "Integrity error detected in EEPROM"}),
59
60 0x67: (smartcard.sw.SWExceptions.CheckingErrorException,
61 {0x00: "Wrong length in Lc"}),
62
63 0x68: (smartcard.sw.SWExceptions.CheckingErrorException,
64 {0x81: "Logical channel not supported",
65 0x82: "Secure messaging not supported"}),
66
67 0x69: (smartcard.sw.SWExceptions.CheckingErrorException,
68 {0x81: "Command incompatible with file structure.",
69 0x82: "Security status not satisfied",
70 0x83: "Authentification method blocked",
71 0x84: "Referenced data invalid",
72 0x85: "Conditions of use not satisfied",
73 0x86: "Command not allowed (no current EF)",
74 0x87: "Secure messaging data object missing.",
75 0x88: "Secure messaging data object incorrect"}),
76
77 0x6A: (smartcard.sw.SWExceptions.CheckingErrorException,
78 {0x80: "Incorrect parameters in the data field",
79 0x81: "Function not supported",
80 0x82: "File not found",
81 0x83: "Record not found",
82 0x84: "Not enough memory space in the file",
83 0x85: "Lc inconsistent with TLV structure",
84 0x86: "Incorrect parameters P1-P2",
85 0x87: "Lc is inconsistent with P1-P2",
86 0x88: "Referenced data not found"}),
87
88 0x6B: (smartcard.sw.SWExceptions.CheckingErrorException,
89 {0x00: "Incorrect parameters P1-P2"}),
90
91 0x6D: (smartcard.sw.SWExceptions.CheckingErrorException,
92 {0x00: "Instruction (INS) not supported"}),
93
94 0x6E: (smartcard.sw.SWExceptions.CheckingErrorException,
95 {0x00: "Class (CLA) not supported"}),
96
97 0x6F: (smartcard.sw.SWExceptions.CheckingErrorException,
98 {0x00: "Fatal error"}),
99 }
100
101
103 """ISO7816-4 error checking strategy.
104
105 This strategy raises the following exceptions:
106 - sw1 sw2
107 - 62 00 81 82 83 84 FF WarningProcessingException
108 - 63 00 81 C0->CF WarningProcessingException
109 - 64 00 ExecutionErrorException
110 - 67 00 CheckingErrorException
111 - 68 81 82 CheckingErrorException
112 - 69 81->88 99? c1? CheckingErrorException
113 - 6a 80->88 CheckingErrorException
114 - 6b 00 CheckingErrorException
115 - 6d 00 CheckingErrorException
116 - 6e 00 CheckingErrorException
117 - 6f 00 CheckingErrorException
118
119 This checker does not raise exceptions on undefined sw1 values, e.g.:
120 - sw1 sw2
121 - 65 any
122 - 66 any
123 - 6c any
124
125 and on undefined sw2 values, e.g.:
126 - sw1 sw2
127 - 62 80 85
128 - 6b any except 00
129
130
131 Use another checker in the error checking chain, e.g., the
132 ISO7816_4SW1ErrorChecker, to raise exceptions on these undefined
133 values.
134 """
135
137 """Called to test data, sw1 and sw2 for error.
138
139 @param data: apdu response data
140 @param sw1, sw2: apdu data status words
141
142 Derived classes must raise a L{smartcard.sw.SWException} upon error."""
143 if iso7816_4SW.has_key(sw1):
144 exception, sw2dir = iso7816_4SW[sw1]
145 if type(sw2dir) == type({}):
146 try:
147 message = sw2dir[sw2]
148 raise exception(data, sw1, sw2, message)
149 except KeyError:
150 pass
151
152 if __name__ == '__main__':
153 """Small sample illustrating the use of ISO7816_4ErrorChecker."""
154 ecs = ISO7816_4ErrorChecker()
155 ecs([], 0x90, 0x00)
156 try:
157 ecs([], 0x6b, 0x00)
158 except smartcard.sw.SWExceptions.CheckingErrorException, e:
159 print e, "%x %x" % (e.sw1, e.sw2)
160