Refactor, move apdu SW handling from generic card class to iso card class

This commit is contained in:
Henryk Plötz 2010-10-15 16:50:01 +02:00
parent 9e8b7b0a82
commit c53914f1ae
2 changed files with 38 additions and 28 deletions

View File

@ -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]

View File

@ -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)