1 """ISO7816-8 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_8SW = {
29 0x63: (smartcard.sw.SWExceptions.WarningProcessingException,
30 {0x00: "Authentication failed",
31 0xC0: "PIN verification failed. 0 retries remaining before blocking PIN",
32 0xC1: "PIN verification failed. 1 retries remaining before blocking PIN",
33 0xC2: "PIN verification failed. 2 retries remaining before blocking PIN",
34 0xC3: "PIN verification failed. 3 retries remaining before blocking PIN",
35 0xC4: "PIN verification failed. 4 retries remaining before blocking PIN",
36 0xC5: "PIN verification failed. 5 retries remaining before blocking PIN",
37 0xC6: "PIN verification failed. 6 retries remaining before blocking PIN",
38 0xC7: "PIN verification failed. 7 retries remaining before blocking PIN",
39 0xC8: "PIN verification failed. 8 retries remaining before blocking PIN",
40 0xC9: "PIN verification failed. 9 retries remaining before blocking PIN",
41 0xCA: "PIN verification failed. 10 retries remaining before blocking PIN",
42 0xCB: "PIN verification failed. 11 retries remaining before blocking PIN",
43 0xCC: "PIN verification failed. 12 retries remaining before blocking PIN",
44 0xCD: "PIN verification failed. 13 retries remaining before blocking PIN",
45 0xCE: "PIN verification failed. 14 retries remaining before blocking PIN",
46 0xCF: "PIN verification failed. 15 retries remaining before blocking PIN"}),
47
48 0x65: (smartcard.sw.SWExceptions.ExecutionErrorException,
49 {0x81: "Memory failure (unsuccessful changing)"}),
50
51 0x66: (smartcard.sw.SWExceptions.SecurityRelatedException,
52 {0x00: "The environment cannot be set or modified",
53 0x87: "Expected SM data objects missing",
54 0x88: "SM data objects incorrect"}),
55
56 0x67: (smartcard.sw.SWExceptions.CheckingErrorException,
57 {0x00: "Wrong length (emtpy Lc field)"}),
58
59 0x68: (smartcard.sw.SWExceptions.CheckingErrorException,
60 {0x83: "Final command expected",
61 0x84: "Command chaining not supported"}),
62
63 0x69: (smartcard.sw.SWExceptions.CheckingErrorException,
64 {0x82: "Security status not satisfied",
65 0x83: "Authentification method blocked",
66 0x84: "Referenced data invalidated",
67 0x85: "Conditions of use not satisfied"}),
68
69 0x6A: (smartcard.sw.SWExceptions.CheckingErrorException,
70 {0x81: "Function not supported",
71 0x82: "File not found",
72 0x86: "Incorrect parameters P1-P2",
73 0x88: "Referenced data not found"}),
74 }
75
76
78 """ISO7816-8 error checker.
79
80 This error checker raises the following exceptions:
81 - sw1 sw2
82 - 63 00,c0-cf WarningProcessingException
83 - 65 81 ExecutionErrorException
84 - 66 00,87,88 SecurityRelatedException
85 - 67 00 CheckingErrorException
86 - 68 82,84 CheckingErrorException
87 - 69 82,83,84,85 CheckingErrorException
88 - 6A 81,82,86,88 CheckingErrorException
89
90 This checker does not raise exceptions on undefined sw1 values, e.g.:
91 - sw1 sw2
92 - 62 any
93 - 6f any
94
95 and on undefined sw2 values, e.g.:
96 - sw1 sw2
97 - 66 81 82
98 - 67 any except 00
99
100
101 Use another checker in the error checking chain, e.g., the
102 ISO7816_4SW1ErrorChecker or ISO7816_4ErrorChecker, to raise
103 exceptions on these undefined values.
104 """
105
107 """Called to test data, sw1 and sw2 for error.
108
109 @param data: apdu response data
110 @param sw1, sw2: apdu data status words
111
112 Derived classes must raise a L{smartcard.sw.SWException} upon error."""
113 if iso7816_8SW.has_key(sw1):
114 exception, sw2dir = iso7816_8SW[sw1]
115 if type(sw2dir) == type({}):
116 try:
117 message = sw2dir[sw2]
118 raise exception(data, sw1, sw2, message)
119 except KeyError:
120 pass
121
122
123 if __name__ == '__main__':
124 """Small sample illustrating the use of ISO7816_8ErrorChecker."""
125 ecs = ISO7816_8ErrorChecker()
126 ecs([], 0x90, 0x00)
127 ecs([], 0x6a, 0x83)
128 try:
129 ecs([], 0x66, 0x87)
130 except smartcard.sw.SWExceptions.SecurityRelatedException, e:
131 print e, "%x %x" % (e.sw1, e.sw2)
132