1 """ISO7816-9 error checker.
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_9SW = {
29 0x62: (smartcard.sw.SWExceptions.WarningProcessingException,
30 {0x82: "End of file/record reached"}),
31
32 0x64: (smartcard.sw.SWExceptions.ExecutionErrorException,
33 {0x00: "Execution error"}),
34
35 0x69: (smartcard.sw.SWExceptions.CheckingErrorException,
36 {0x82: "Security status not satisfied"}),
37
38 0x6A: (smartcard.sw.SWExceptions.CheckingErrorException,
39 {0x80: "Incorrect parameters in data field",
40 0x84: "Not enough memory space",
41 0x89: "File already exists",
42 0x8A: "DF name already exists"}),
43 }
44
45
47 """ISO7816-8 error checker.
48
49 This error checker raises the following exceptions:
50 - sw1 sw2
51 - 62 82 WarningProcessingException
52 - 64 00 ExecutionErrorException
53 - 69 82 CheckingErrorException
54 - 6A 80,84,89,8A CheckingErrorException
55
56 This checker does not raise exceptions on undefined sw1 values, e.g.:
57 - sw1 sw2
58 - 63 any
59 - 6F any
60
61 and on undefined sw2 values, e.g.:
62 - sw1 sw2
63 - 62 81 83
64 - 64 any except 00
65
66
67 Use another checker in the error checking chain, e.g., the
68 ISO7816_4SW1ErrorChecker or ISO7816_4ErrorChecker, to raise
69 exceptions on these undefined values.
70 """
71
73 """Called to test data, sw1 and sw2 for error.
74
75 @param data: apdu response data
76 @param sw1, sw2: apdu data status words
77
78 Derived classes must raise a L{smartcard.sw.SWException} upon error."""
79 if iso7816_9SW.has_key(sw1):
80 exception, sw2dir = iso7816_9SW[sw1]
81 if type(sw2dir) == type({}):
82 try:
83 message = sw2dir[sw2]
84 raise exception(data, sw1, sw2, message)
85 except KeyError:
86 pass
87
88
89 if __name__ == '__main__':
90 """Small sample illustrating the use of ISO7816_9ErrorChecker."""
91 ecs = ISO7816_9ErrorChecker()
92 ecs([], 0x90, 0x00)
93 ecs([], 0x6a, 0x81)
94 try:
95 ecs([], 0x6A, 0x8A)
96 except smartcard.sw.SWExceptions.CheckingErrorException, e:
97 print e, "%x %x" % (e.sw1, e.sw2)
98