first init

This commit is contained in:
ISMAIL MASSERAN
2026-09-01 10:12:31 +08:00
commit ca6e55f853
718 changed files with 204746 additions and 0 deletions
@@ -0,0 +1,49 @@
"""Base class for status word error checkers.
__author__ = "https://www.gemalto.com/"
Copyright 2001-2012 gemalto
Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
This file is part of pyscard.
pyscard is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
pyscard 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with pyscard; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
# pylint: disable=too-few-public-methods
class ErrorChecker:
"""Base class for status word error checking strategies.
Error checking strategies are chained into an L{ErrorCheckingChain} to
implement a Chain of Responsibility. Each strategy in the chain is
called until an error is detected. The strategy raises a
L{smartcard.sw.SWExceptions} exception when an error is detected.
Implementation derived from Bruce Eckel, Thinking in Python. The
L{ErrorCheckingChain} implements the Chain Of Responsibility design
pattern.
"""
def __call__(self, data, sw1, sw2):
"""Called to test data, sw1 and sw2 for error.
Derived classes must raise a L{smartcard.sw.SWExceptions} upon error.
@param data: apdu response data
@param sw1: apdu data status word 1
@param sw2: apdu data status word 2
"""
@@ -0,0 +1,96 @@
"""The error checking chain is a list of status word
(sw1, sw2) error check strategies.
__author__ = "https://www.gemalto.com/"
Copyright 2001-2012 gemalto
Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
This file is part of pyscard.
pyscard is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
pyscard 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with pyscard; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
from sys import exc_info
class ErrorCheckingChain:
"""The error checking chain is a list of response apdu status word
(sw1, sw2) error check strategies. Each strategy in the chain is
called until an error is detected. A L{smartcard.sw.SWExceptions}
exception is raised when an error is detected. No exception is
raised if no error is detected.
Implementation derived from Bruce Eckel, Thinking in Python. The
L{ErrorCheckingChain} implements the Chain Of Responsibility design
pattern.
"""
def __init__(self, chain, strategy):
"""constructor. Appends a strategy to the L{ErrorCheckingChain}
chain."""
self.strategy = strategy
self.chain = chain
self.chain.append(self)
self.excludes = []
def next(self):
"""Returns next error checking strategy."""
# Where this link is in the chain:
location = self.chain.index(self)
if not self.end():
return self.chain[location + 1]
return None
def addFilterException(self, exClass):
"""Add an exception filter to the error checking chain.
@param exClass: the exception to exclude, e.g.
L{smartcard.sw.SWExceptions.WarningProcessingException} A filtered
exception will not be raised when the sw1,sw2 conditions that
would raise the exception are met.
"""
self.excludes.append(exClass)
if self.end():
return
self.next().addFilterException(exClass)
def end(self):
"""Returns True if this is the end of the error checking
strategy chain."""
return self.chain.index(self) + 1 >= len(self.chain)
def __call__(self, data, sw1, sw2):
"""Called to test data, sw1 and sw2 for error on the chain."""
try:
self.strategy(data, sw1, sw2)
except tuple(self.excludes):
# The following additional filter may look redundant, it isn't.
# It checks that type(exc) is *equal* to any of self.excludes,
# rather than equal-or-subclass to any of self.excludes.
# This maintains backward compatibility with the behaviour of
# pyscard <= 1.6.16.
# if exception is filtered, return
for exception in self.excludes:
if exception == exc_info()[0]:
return None
# otherwise reraise exception
raise
# if not done, call next strategy
if self.end():
return None
return self.next()(data, sw1, sw2)
@@ -0,0 +1,183 @@
"""ISO7816-4 error checking strategy.
__author__ = "https://www.gemalto.com/"
Copyright 2001-2012 gemalto
Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
This file is part of pyscard.
pyscard is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
pyscard 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with pyscard; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
import smartcard.sw.SWExceptions
from smartcard.sw.ErrorChecker import ErrorChecker
# pylint: disable=too-few-public-methods
iso7816_4SW = {
0x62: (
smartcard.sw.SWExceptions.WarningProcessingException,
{
0x00: "Response padded/ More APDU commands expected",
0x81: "Part of returned data may be corrupted",
0x82: "End of file/record reached before reading Le bytes",
0x83: "File invalidated",
0x84: "FCI not correctly formatted",
0xFF: "Correct execution, response padded",
},
),
0x63: (
smartcard.sw.SWExceptions.WarningProcessingException,
{
0x00: "Authentication failed",
0x81: "File filled up by the last write",
0xC0: "PIN verification failed. 0 tries before blocking PIN",
0xC1: "PIN verification failed. 1 tries before blocking PIN",
0xC2: "PIN verification failed. 2 tries before blocking PIN",
0xC3: "PIN verification failed. 3 tries before blocking PIN",
0xC4: "PIN verification failed. 4 tries before blocking PIN",
0xC5: "PIN verification failed. 5 tries before blocking PIN",
0xC6: "PIN verification failed. 6 tries before blocking PIN",
0xC7: "PIN verification failed. 7 tries before blocking PIN",
0xC8: "PIN verification failed. 8 tries before blocking PIN",
0xC9: "PIN verification failed. 9 tries before blocking PIN",
0xCA: "PIN verification failed. 10 tries before blocking PIN",
0xCB: "PIN verification failed. 11 tries before blocking PIN",
0xCC: "PIN verification failed. 12 tries before blocking PIN",
0xCD: "PIN verification failed. 13 tries before blocking PIN",
0xCE: "PIN verification failed. 14 tries before blocking PIN",
0xCF: "PIN verification failed. 15 tries before blocking PIN",
},
),
0x64: (
smartcard.sw.SWExceptions.ExecutionErrorException,
{0x00: "Integrity error detected in EEPROM"},
),
0x67: (
smartcard.sw.SWExceptions.CheckingErrorException,
{0x00: "Wrong length in Lc"},
),
0x68: (
smartcard.sw.SWExceptions.CheckingErrorException,
{0x81: "Logical channel not supported", 0x82: "Secure messaging not supported"},
),
0x69: (
smartcard.sw.SWExceptions.CheckingErrorException,
{
0x81: "Command incompatible with file structure.",
0x82: "Security status not satisfied",
0x83: "Authentication method blocked",
0x84: "Referenced data invalid",
0x85: "Conditions of use not satisfied",
0x86: "Command not allowed (no current EF)",
0x87: "Secure messaging data object missing.",
0x88: "Secure messaging data object incorrect",
},
),
0x6A: (
smartcard.sw.SWExceptions.CheckingErrorException,
{
0x80: "Incorrect parameters in the data field",
0x81: "Function not supported",
0x82: "File not found",
0x83: "Record not found",
0x84: "Not enough memory space in the file",
0x85: "Lc inconsistent with TLV structure",
0x86: "Incorrect parameters P1-P2",
0x87: "Lc is inconsistent with P1-P2",
0x88: "Referenced data not found",
},
),
0x6B: (
smartcard.sw.SWExceptions.CheckingErrorException,
{0x00: "Incorrect parameters P1-P2"},
),
0x6D: (
smartcard.sw.SWExceptions.CheckingErrorException,
{0x00: "Instruction (INS) not supported"},
),
0x6E: (
smartcard.sw.SWExceptions.CheckingErrorException,
{0x00: "Class (CLA) not supported"},
),
0x6F: (smartcard.sw.SWExceptions.CheckingErrorException, {0x00: "Fatal error"}),
}
class ISO7816_4ErrorChecker(ErrorChecker):
"""ISO7816-4 error checking strategy.
This strategy raises the following exceptions:
- sw1 sw2
- 62 00 81 82 83 84 FF WarningProcessingException
- 63 00 81 C0->CF WarningProcessingException
- 64 00 ExecutionErrorException
- 67 00 CheckingErrorException
- 68 81 82 CheckingErrorException
- 69 81->88 99? c1? CheckingErrorException
- 6a 80->88 CheckingErrorException
- 6b 00 CheckingErrorException
- 6d 00 CheckingErrorException
- 6e 00 CheckingErrorException
- 6f 00 CheckingErrorException
This checker does not raise exceptions on undefined sw1 values, e.g.:
- sw1 sw2
- 65 any
- 66 any
- 6c any
and on undefined sw2 values, e.g.:
- sw1 sw2
- 62 80 85
- 6b any except 00
Use another checker in the error checking chain, e.g., the
ISO7816_4SW1ErrorChecker, to raise exceptions on these undefined
values.
"""
def __call__(self, data, sw1, sw2):
"""Called to test data, sw1 and sw2 for error.
Derived classes must raise a L{smartcard.sw.SWExceptions} upon error.
@param data: apdu response data
@param sw1: apdu data status words
@param sw2: apdu data status words
"""
# pylint: disable=duplicate-code
if sw1 in iso7816_4SW:
exception, sw2dir = iso7816_4SW[sw1]
if isinstance(sw2dir, dict):
try:
message = sw2dir[sw2]
raise exception(data, sw1, sw2, message)
except KeyError:
pass
if __name__ == "__main__":
# Small sample illustrating the use of ISO7816_4ErrorChecker.
ecs = ISO7816_4ErrorChecker()
ecs([], 0x90, 0x00)
try:
ecs([], 0x6B, 0x00)
except smartcard.sw.SWExceptions.CheckingErrorException as e:
print(str(e) + f" {e.sw1:x} {e.sw2:x}")
@@ -0,0 +1,88 @@
"""ISO7816-4 sw1 only error checker.
__author__ = "https://www.gemalto.com/"
Copyright 2001-2012 gemalto
Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
This file is part of pyscard.
pyscard is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
pyscard 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with pyscard; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
import smartcard.sw.SWExceptions
from smartcard.sw.ErrorChecker import ErrorChecker
# pylint: disable=too-few-public-methods
iso7816_4SW1 = {
0x62: smartcard.sw.SWExceptions.WarningProcessingException,
0x63: smartcard.sw.SWExceptions.WarningProcessingException,
0x64: smartcard.sw.SWExceptions.ExecutionErrorException,
0x65: smartcard.sw.SWExceptions.ExecutionErrorException,
0x66: smartcard.sw.SWExceptions.SecurityRelatedException,
0x67: smartcard.sw.SWExceptions.CheckingErrorException,
0x68: smartcard.sw.SWExceptions.CheckingErrorException,
0x69: smartcard.sw.SWExceptions.CheckingErrorException,
0x6A: smartcard.sw.SWExceptions.CheckingErrorException,
0x6B: smartcard.sw.SWExceptions.CheckingErrorException,
0x6C: smartcard.sw.SWExceptions.CheckingErrorException,
0x6D: smartcard.sw.SWExceptions.CheckingErrorException,
0x6E: smartcard.sw.SWExceptions.CheckingErrorException,
0x6F: smartcard.sw.SWExceptions.CheckingErrorException,
}
class ISO7816_4_SW1ErrorChecker(ErrorChecker):
"""ISO7816-4 error checker based on status word sw1 only.
This error checker raises the following exceptions:
- sw1 sw2
- 62 any L{WarningProcessingException}
- 63 any L{WarningProcessingException}
- 64 any L{ExecutionErrorException}
- 65 any L{ExecutionErrorException}
- 66 any L{SecurityRelatedException}
- 67 any L{CheckingErrorException}
- 68 any L{CheckingErrorException}
- 69 any L{CheckingErrorException}
- 6a any L{CheckingErrorException}
- 6b any L{CheckingErrorException}
- 6c any L{CheckingErrorException}
- 6d any L{CheckingErrorException}
- 6e any L{CheckingErrorException}
- 6f any L{CheckingErrorException}
"""
def __call__(self, data, sw1, sw2):
"""Called to test data, sw1 and sw2 for error.
@param data: apdu response data
@param sw1: apdu data status word 1
@param sw2: apdu data status word 2
"""
if sw1 in iso7816_4SW1:
exception = iso7816_4SW1[sw1]
raise exception(data, sw1, sw2)
if __name__ == "__main__":
# Small sample illustrating the use of ISO7816_4_SW1ErrorChecker.
ecs = ISO7816_4_SW1ErrorChecker()
ecs([], 0x90, 0x00)
try:
ecs([], 0x66, 0x80)
except smartcard.sw.SWExceptions.SecurityRelatedException as e:
print(str(e) + f" {e.sw1:x} {e.sw2:x}")
@@ -0,0 +1,153 @@
"""ISO7816-8 error checker.
__author__ = "https://www.gemalto.com/"
Copyright 2001-2012 gemalto
Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
This file is part of pyscard.
pyscard is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
pyscard 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with pyscard; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
import smartcard.sw.SWExceptions
from smartcard.sw.ErrorChecker import ErrorChecker
# pylint: disable=too-few-public-methods
iso7816_8SW = {
0x63: (
smartcard.sw.SWExceptions.WarningProcessingException,
{
0x00: "Authentication failed",
0xC0: "PIN verification failed. 0 retries before blocking PIN",
0xC1: "PIN verification failed. 1 retries before blocking PIN",
0xC2: "PIN verification failed. 2 retries before blocking PIN",
0xC3: "PIN verification failed. 3 retries before blocking PIN",
0xC4: "PIN verification failed. 4 retries before blocking PIN",
0xC5: "PIN verification failed. 5 retries before blocking PIN",
0xC6: "PIN verification failed. 6 retries before blocking PIN",
0xC7: "PIN verification failed. 7 retries before blocking PIN",
0xC8: "PIN verification failed. 8 retries before blocking PIN",
0xC9: "PIN verification failed. 9 retries before blocking PIN",
0xCA: "PIN verification failed. 10 retries before blocking PIN",
0xCB: "PIN verification failed. 11 retries before blocking PIN",
0xCC: "PIN verification failed. 12 retries before blocking PIN",
0xCD: "PIN verification failed. 13 retries before blocking PIN",
0xCE: "PIN verification failed. 14 retries before blocking PIN",
0xCF: "PIN verification failed. 15 retries before blocking PIN",
},
),
0x65: (
smartcard.sw.SWExceptions.ExecutionErrorException,
{0x81: "Memory failure (unsuccessful changing)"},
),
0x66: (
smartcard.sw.SWExceptions.SecurityRelatedException,
{
0x00: "The environment cannot be set or modified",
0x87: "Expected SM data objects missing",
0x88: "SM data objects incorrect",
},
),
0x67: (
smartcard.sw.SWExceptions.CheckingErrorException,
{0x00: "Wrong length (empty Lc field)"},
),
0x68: (
smartcard.sw.SWExceptions.CheckingErrorException,
{0x83: "Final command expected", 0x84: "Command chaining not supported"},
),
0x69: (
smartcard.sw.SWExceptions.CheckingErrorException,
{
0x82: "Security status not satisfied",
0x83: "Authentication method blocked",
0x84: "Referenced data invalidated",
0x85: "Conditions of use not satisfied",
},
),
0x6A: (
smartcard.sw.SWExceptions.CheckingErrorException,
{
0x81: "Function not supported",
0x82: "File not found",
0x86: "Incorrect parameters P1-P2",
0x88: "Referenced data not found",
},
),
}
class ISO7816_8ErrorChecker(ErrorChecker):
"""ISO7816-8 error checker.
This error checker raises the following exceptions:
- sw1 sw2
- 63 00,c0-cf L{WarningProcessingException}
- 65 81 L{ExecutionErrorException}
- 66 00,87,88 L{SecurityRelatedException}
- 67 00 L{CheckingErrorException}
- 68 82,84 L{CheckingErrorException}
- 69 82,83,84,85 L{CheckingErrorException}
- 6A 81,82,86,88 L{CheckingErrorException}
This checker does not raise exceptions on undefined sw1 values, e.g.:
- sw1 sw2
- 62 any
- 6f any
and on undefined sw2 values, e.g.:
- sw1 sw2
- 66 81 82
- 67 any except 00
Use another checker in the error checking chain, e.g., the
L{ISO7816_4_SW1ErrorChecker} or L{ISO7816_4ErrorChecker}, to raise
exceptions on these undefined values.
"""
def __call__(self, data, sw1, sw2):
"""Called to test data, sw1 and sw2 for error.
Derived classes must raise a L{smartcard.sw.SWExceptions} upon error.
@param data: apdu response data
@param sw1: apdu data status word 1
@param sw2: apdu data status word 2
"""
# pylint: disable=duplicate-code
if sw1 in iso7816_8SW:
exception, sw2dir = iso7816_8SW[sw1]
if isinstance(sw2dir, dict):
try:
message = sw2dir[sw2]
raise exception(data, sw1, sw2, message)
except KeyError:
pass
if __name__ == "__main__":
# Small sample illustrating the use of ISO7816_8ErrorChecker.
ecs = ISO7816_8ErrorChecker()
ecs([], 0x90, 0x00)
ecs([], 0x6A, 0x83)
try:
ecs([], 0x66, 0x87)
except smartcard.sw.SWExceptions.SecurityRelatedException as e:
print(str(e) + f" {e.sw1:x} {e.sw2:x}")
@@ -0,0 +1,111 @@
"""ISO7816-9 error checker.
__author__ = "https://www.gemalto.com/"
Copyright 2001-2012 gemalto
Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
This file is part of pyscard.
pyscard is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
pyscard 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with pyscard; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
import smartcard.sw.SWExceptions
from smartcard.sw.ErrorChecker import ErrorChecker
# pylint: disable=too-few-public-methods
iso7816_9SW = {
0x62: (
smartcard.sw.SWExceptions.WarningProcessingException,
{0x82: "End of file/record reached"},
),
0x64: (
smartcard.sw.SWExceptions.ExecutionErrorException,
{0x00: "Execution error"},
),
0x69: (
smartcard.sw.SWExceptions.CheckingErrorException,
{0x82: "Security status not satisfied"},
),
0x6A: (
smartcard.sw.SWExceptions.CheckingErrorException,
{
0x80: "Incorrect parameters in data field",
0x84: "Not enough memory space",
0x89: "File already exists",
0x8A: "DF name already exists",
},
),
}
class ISO7816_9ErrorChecker(ErrorChecker):
"""ISO7816-8 error checker.
This error checker raises the following exceptions:
- sw1 sw2
- 62 82 WarningProcessingException
- 64 00 ExecutionErrorException
- 69 82 CheckingErrorException
- 6A 80,84,89,8A CheckingErrorException
This checker does not raise exceptions on undefined sw1 values, e.g.:
- sw1 sw2
- 63 any
- 6F any
and on undefined sw2 values, e.g.:
- sw1 sw2
- 62 81 83
- 64 any except 00
Use another checker in the error checking chain, e.g., the
L{ISO7816_4_SW1ErrorChecker} or L{ISO7816_4ErrorChecker}, to raise
exceptions on these undefined values.
"""
def __call__(self, data, sw1, sw2):
"""Called to test data, sw1 and sw2 for error.
Derived classes must raise a L{smartcard.sw.SWExceptions} upon error.
@param data: apdu response data
@param sw1: apdu data status word 1
@param sw2: apdu data status word 2
"""
# pylint: disable=duplicate-code
if sw1 in iso7816_9SW:
exception, sw2dir = iso7816_9SW[sw1]
if isinstance(sw2dir, dict):
try:
message = sw2dir[sw2]
raise exception(data, sw1, sw2, message)
except KeyError:
pass
if __name__ == "__main__":
# Small sample illustrating the use of ISO7816_9ErrorChecker.
ecs = ISO7816_9ErrorChecker()
ecs([], 0x90, 0x00)
ecs([], 0x6A, 0x81)
try:
ecs([], 0x6A, 0x8A)
except smartcard.sw.SWExceptions.CheckingErrorException as e:
print(str(e) + f" {e.sw1:x} {e.sw2:x}")
@@ -0,0 +1,78 @@
"""Status Word (SW) Exceptions
This module defines the exceptions raised by status word errors or warnings.
__author__ = "https://www.gemalto.com/"
Copyright 2001-2012 gemalto
Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
This file is part of pyscard.
pyscard is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
pyscard 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with pyscard; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
class SWException(Exception):
"""Base class for status word exceptions.
Status word exceptions are generated when errors and warnings are detected
in the sw1 and sw2 bytes of the response apdu.
"""
def __init__(self, data, sw1, sw2, message=""):
self.message = message
"""response apdu data"""
self.data = data
"""response apdu sw1"""
self.sw1 = sw1
"""response apdu sw2"""
self.sw2 = sw2
def __str__(self):
return repr("Status word exception: " + self.message + "!")
class WarningProcessingException(SWException):
"""Raised when a warning processing is detected from sw1, sw2.
Examples of warning processing exception: sw1=62 or sw=63 (ISO7816-4)."""
def __init__(self, data, sw1, sw2, message=""):
SWException.__init__(self, data, sw1, sw2, "warning processing - " + message)
class ExecutionErrorException(SWException):
"""Raised when an execution error is detected from sw1, sw2.
Examples of execution error: sw1=64 or sw=65 (ISO7816-4)."""
def __init__(self, data, sw1, sw2, message=""):
SWException.__init__(self, data, sw1, sw2, "execution error - " + message)
class SecurityRelatedException(SWException):
"""Raised when a security issue is detected from sw1, sw2.
Examples of security issue: sw1=66 (ISO7816-4)."""
def __init__(self, data, sw1, sw2, message=""):
SWException.__init__(self, data, sw1, sw2, "security issue - " + message)
class CheckingErrorException(SWException):
"""Raised when a checking error is detected from sw1, sw2.
Examples of checking error: sw1=67 to 6F (ISO781604)."""
def __init__(self, data, sw1, sw2, message=""):
SWException.__init__(self, data, sw1, sw2, "checking error - " + message)
@@ -0,0 +1,23 @@
"""smartcard.sw module for status word error checking.
__author__ = "gemalto https://www.gemalto.com/"
Copyright 2001-2012 gemalto
Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
This file is part of pyscard.
pyscard is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
pyscard 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with pyscard; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
@@ -0,0 +1,139 @@
"""Open Platform 2.1 error checker.
__author__ = "https://www.gemalto.com/"
Copyright 2001-2012 gemalto
Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
This file is part of pyscard.
pyscard is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
pyscard 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with pyscard; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
import smartcard.sw.SWExceptions
from smartcard.sw.ErrorChecker import ErrorChecker
# pylint: disable=too-few-public-methods
op21_SW = {
0x62: (
smartcard.sw.SWExceptions.WarningProcessingException,
{0x83: "Card life cycle is CARD_LOCKED"},
),
0x63: (
smartcard.sw.SWExceptions.WarningProcessingException,
{0x00: "Authentication failed"},
),
0x64: (
smartcard.sw.SWExceptions.ExecutionErrorException,
{0x00: "Execution error"},
),
0x65: (smartcard.sw.SWExceptions.ExecutionErrorException, {0x81: "Memory failure"}),
0x67: (
smartcard.sw.SWExceptions.CheckingErrorException,
{0x00: "Wrong length in Lc"},
),
0x69: (
smartcard.sw.SWExceptions.CheckingErrorException,
{
0x82: "Security status not satisfied",
0x85: "Conditions of use not satisfied",
},
),
0x6A: (
smartcard.sw.SWExceptions.CheckingErrorException,
{
0x80: "Incorrect values in command data",
0x81: "Function not supported",
0x82: "Application not found",
0x84: "Not enough memory space",
0x86: "Incorrect parameters P1-P2",
0x88: "Referenced data not found",
},
),
0x6D: (
smartcard.sw.SWExceptions.CheckingErrorException,
{0x00: "Instruction not supported"},
),
0x6E: (
smartcard.sw.SWExceptions.CheckingErrorException,
{0x00: "Class not supported"},
),
0x94: (
smartcard.sw.SWExceptions.CheckingErrorException,
{0x84: "Algorithm not supported", 0x85: "Invalid key check value"},
),
}
class op21_ErrorChecker(ErrorChecker):
"""Open platform 2.1 error checker.
This error checker raises the following exceptions:
- sw1 sw2
- 62 83 L{WarningProcessingException}
- 63 00 L{WarningProcessingException}
- 64 00 L{ExecutionErrorException}
- 65 81 L{ExecutionErrorException}
- 67 00 L{CheckingErrorException}
- 69 82 85 L{CheckingErrorException}
- 6A 80 81 82 84 86 88 L{CheckingErrorException}
- 6D 00 L{CheckingErrorException}
- 6E 00 L{CheckingErrorException}
- 94 84 85 L{CheckingErrorException}
This checker does not raise exceptions on undefined sw1 values, e.g.:
- sw1 sw2
- 63 any
- 6F any
and on undefined sw2 values, e.g.:
- sw1 sw2
- 62 81 83
- 64 any except 00
Use another checker in the error checking chain to raise exceptions
on these undefined values.
"""
def __call__(self, data, sw1, sw2):
"""Called to test data, sw1 and sw2 for error.
Derived classes must raise a L{smartcard.sw.SWExceptions} upon error.
@param data: apdu response data
@param sw1: apdu data status word 1
@param sw2: apdu data status word 2
"""
if sw1 in op21_SW:
exception, sw2dir = op21_SW[sw1]
if isinstance(sw2dir, dict):
try:
message = sw2dir[sw2]
raise exception(data, sw1, sw2, message)
except KeyError:
pass
if __name__ == "__main__":
# Small sample illustrating the use of op21_ErrorChecker.
ecs = op21_ErrorChecker()
ecs([], 0x90, 0x00)
ecs([], 0x94, 0x81)
try:
ecs([], 0x94, 0x84)
except smartcard.sw.SWExceptions.CheckingErrorException as e:
print(str(e) + f"{e.sw1:x} {e.sw2:x}")