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