def _chv_process_sw(self, op_name, chv_no, pin_code, sw): if sw_match(sw, '63cx'): raise RuntimeError( 'Failed to %s chv_no 0x%02X with code 0x%s, %i tries left.' % (op_name, chv_no, b2h(pin_code).upper(), int(sw[3]))) elif (sw != '9000'): raise SwMatchError(sw, '9000')
def send_apdu_constr_checksw(self, cla, ins, p1, p2, cmd_constr, cmd_data, resp_constr, sw_exp="9000"): """Build and sends an APDU using a 'construct' definition; parses response. Args: cla : string (in hex) ISO 7816 class byte ins : string (in hex) ISO 7816 instruction byte p1 : string (in hex) ISO 7116 Parameter 1 byte p2 : string (in hex) ISO 7116 Parameter 2 byte cmd_cosntr : defining how to generate binary APDU command data cmd_data : command data passed to cmd_constr resp_cosntr : defining how to decode binary APDU response data exp_sw : string (in hex) of status word (ex. "9000") Returns: Tuple of (decoded_data, sw) """ (rsp, sw) = self.send_apdu_constr(cla, ins, p1, p2, cmd_constr, cmd_data, resp_constr) if not sw_match(sw, sw_exp): raise SwMatchError(sw, sw_exp.lower(), self.sw_interpreter) return (rsp, sw)
def interpret_sw(sw_data, sw): """Interpret a given status word within the profile. Returns tuple of two strings""" for class_str, swdict in sw_data.items(): # first try direct match if sw in swdict: return (class_str, swdict[sw]) # next try wildcard matches for pattern, descr in swdict.items(): if sw_match(sw, pattern): return (class_str, descr) return None
def send_apdu_checksw(self, pdu, sw="9000"): """Sends an APDU and check returned SW Args: pdu : string of hexadecimal characters (ex. "A0A40000023F00") sw : string of 4 hexadecimal characters (ex. "9000"). The user may mask out certain digits using a '?' to add some ambiguity if needed. Returns: tuple(data, sw), where data : string (in hex) of returned data (ex. "074F4EFFFF") sw : string (in hex) of status word (ex. "9000") """ rv = self.send_apdu(pdu) if sw == '9000' and sw_match(rv[1], '91xx'): # proactive sim as per TS 102 221 Setion 7.4.2 rv = self.send_apdu_checksw('80120000' + rv[1][2:], sw) print("FETCH: %s", rv[0]) if not sw_match(rv[1], sw): raise SwMatchError(rv[1], sw.lower(), self.sw_interpreter) return rv
def send_apdu_checksw(self, pdu, sw="9000"): """send_apdu_checksw(pdu,sw): Sends an APDU and check returned SW pdu : string of hexadecimal characters (ex. "A0A40000023F00") sw : string of 4 hexadecimal characters (ex. "9000"). The user may mask out certain digits using a '?' to add some ambiguity if needed. return : tuple(data, sw), where data : string (in hex) of returned data (ex. "074F4EFFFF") sw : string (in hex) of status word (ex. "9000") """ rv = self.send_apdu(pdu) if not sw_match(rv[1], sw): raise SwMatchError(rv[1], sw.lower()) return rv