From c53914f1aedf737ea9bd225bfd9ce62ebd9903c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henryk=20Pl=C3=B6tz?= Date: Fri, 15 Oct 2010 16:50:01 +0200 Subject: Refactor, move apdu SW handling from generic card class to iso card class --- cards/generic_card.py | 29 +---------------------------- cards/iso_card.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/cards/generic_card.py b/cards/generic_card.py index 94d8cc7..93c5374 100644 --- a/cards/generic_card.py +++ b/cards/generic_card.py @@ -88,9 +88,7 @@ class Card: self._i = 0 self.last_apdu = None - self.last_sw = None self.last_result = None - self.sw_changed = False self._last_start = None self.last_delta = None @@ -138,11 +136,9 @@ class Card: print ">> " + utils.hexdump(apdu_binary, indent = 3) result_binary = self.reader.transceive(apdu_binary) - result = R_APDU(result_binary) + result = apdu.RESPONSE_CLASS(result_binary) self.last_apdu = apdu - self.last_sw = result.sw - self.sw_changed = True if DEBUG: print "<< " + utils.hexdump(result_binary, indent = 3) @@ -238,29 +234,6 @@ class Card: return None match_statusword = staticmethod(match_statusword) - def decode_statusword(self): - if self.last_sw is None: - return "No command executed so far" - else: - retval = None - - matched_sw = self.match_statusword(self.STATUS_WORDS.keys(), self.last_sw) - if matched_sw is not None: - retval = self.STATUS_WORDS.get(matched_sw) - if isinstance(retval, str): - retval = retval % { "SW1": ord(self.last_sw[0]), - "SW2": ord(self.last_sw[1]) } - - elif callable(retval): - retval = retval( ord(self.last_sw[0]), - ord(self.last_sw[1]) ) - - if retval is None: - return "Unknown SW (SW %s)" % binascii.b2a_hex(self.last_sw) - else: - return "%s (SW %s)" % (retval, binascii.b2a_hex(self.last_sw)) - - def get_driver_name(self): if len(self.DRIVER_NAME) > 1: names = [e for e in self.DRIVER_NAME if e != _GENERIC_NAME] diff --git a/cards/iso_card.py b/cards/iso_card.py index fb3b016..e4cca21 100644 --- a/cards/iso_card.py +++ b/cards/iso_card.py @@ -85,11 +85,48 @@ class ISO_Card(Card): } TLV_OBJECTS[TLV_utils.context_FCI] = TLV_OBJECTS[TLV_utils.context_FCP] + def __init__(self, reader): + Card.__init__(self, reader) + self.last_sw = None + self.sw_changed = False + def post_merge(self): ## Called after cards.__init__.Cardmultiplexer._merge_attributes self.TLV_OBJECTS[TLV_utils.context_FCP][0x84] = (self._decode_df_name, "DF name") self.TLV_OBJECTS[TLV_utils.context_FCI][0x84] = (self._decode_df_name, "DF name") + def decode_statusword(self): + if self.last_sw is None: + return "No command executed so far" + else: + retval = None + + matched_sw = self.match_statusword(self.STATUS_WORDS.keys(), self.last_sw) + if matched_sw is not None: + retval = self.STATUS_WORDS.get(matched_sw) + if isinstance(retval, str): + retval = retval % { "SW1": ord(self.last_sw[0]), + "SW2": ord(self.last_sw[1]) } + + elif callable(retval): + retval = retval( ord(self.last_sw[0]), + ord(self.last_sw[1]) ) + + if retval is None: + return "Unknown SW (SW %s)" % binascii.b2a_hex(self.last_sw) + else: + return "%s (SW %s)" % (retval, binascii.b2a_hex(self.last_sw)) + + def _real_send(self, apdu): + result = Card._real_send(self, apdu) + + self.last_sw = result.sw + self.sw_changed = True + + return result + + + def verify_pin(self, pin_number, pin_value): apdu = C_APDU(self.APDU_VERIFY_PIN, P2 = pin_number, data = pin_value) -- cgit v1.2.3