dexter has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/37937?usp=email )
Change subject: commands: avoid double lchan patching ......................................................................
commands: avoid double lchan patching
The SimCardCommands has a cla_byte @property method, which automatically returns the lchan patched CLA byte. Since we use cla_byte property to build the UICC command APDUs inside SimCardCommands, we do not need to patch the CLA byte again in the send_apdu* methods. This patch is more or less cosmetic since double patching the CLA byte does not cause any problems.
Related: OS#6531 Change-Id: I420f8a5f7ff8d9e5ef94d6519fb3716d6c7caf64 --- M pySim/commands.py 1 file changed, 22 insertions(+), 22 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/37/37937/1
diff --git a/pySim/commands.py b/pySim/commands.py index 9e231f1..d8165af 100644 --- a/pySim/commands.py +++ b/pySim/commands.py @@ -257,7 +257,7 @@ if not isinstance(dir_list, list): dir_list = [dir_list] for i in dir_list: - data, sw = self.send_apdu(self.cla_byte + "a4" + self.sel_ctrl + "02" + i) + data, sw = self.send_apdu(self.cla_byte + "a4" + self.sel_ctrl + "02" + i, apply_lchan = False) rv.append((data, sw)) if sw != '9000': return rv @@ -287,11 +287,11 @@ fid : file identifier as hex string """
- return self.send_apdu_checksw(self.cla_byte + "a4" + self.sel_ctrl + "02" + fid) + return self.send_apdu_checksw(self.cla_byte + "a4" + self.sel_ctrl + "02" + fid, apply_lchan = False)
def select_parent_df(self) -> ResTuple: """Execute SELECT to switch to the parent DF """ - return self.send_apdu_checksw(self.cla_byte + "a4030400") + return self.send_apdu_checksw(self.cla_byte + "a4030400", apply_lchan = False)
def select_adf(self, aid: Hexstr) -> ResTuple: """Execute SELECT a given Applicaiton ADF. @@ -301,7 +301,7 @@ """
aidlen = ("0" + format(len(aid) // 2, 'x'))[-2:] - return self.send_apdu_checksw(self.cla_byte + "a4" + "0404" + aidlen + aid) + return self.send_apdu_checksw(self.cla_byte + "a4" + "0404" + aidlen + aid, apply_lchan = False)
def read_binary(self, ef: Path, length: int = None, offset: int = 0) -> ResTuple: """Execute READD BINARY. @@ -326,7 +326,7 @@ pdu = self.cla_byte + \ 'b0%04x%02x' % (offset + chunk_offset, chunk_len) try: - data, sw = self.send_apdu_checksw(pdu) + data, sw = self.send_apdu_checksw(pdu, apply_lchan = False) except Exception as e: e.add_note('failed to read (offset %d)' % offset) raise e @@ -386,7 +386,7 @@ 'd6%04x%02x' % (offset + chunk_offset, chunk_len) + \ data[chunk_offset*2: (chunk_offset+chunk_len)*2] try: - chunk_data, chunk_sw = self.send_apdu_checksw(pdu) + chunk_data, chunk_sw = self.send_apdu_checksw(pdu, apply_lchan = False) except Exception as e: e.add_note('failed to write chunk (chunk_offset %d, chunk_len %d)' % (chunk_offset, chunk_len)) raise e @@ -406,7 +406,7 @@ r = self.select_path(ef) rec_length = self.__record_len(r) pdu = self.cla_byte + 'b2%02x04%02x' % (rec_no, rec_length) - return self.send_apdu_checksw(pdu) + return self.send_apdu_checksw(pdu, apply_lchan = False)
def __verify_record(self, ef: Path, rec_no: int, data: str): """Verify record against given data @@ -469,7 +469,7 @@ pass
pdu = (self.cla_byte + 'dc%02x04%02x' % (rec_no, rec_length)) + data - res = self.send_apdu_checksw(pdu) + res = self.send_apdu_checksw(pdu, apply_lchan = False) if verify: self.__verify_record(ef, rec_no, data) return res @@ -579,7 +579,7 @@ if len(rand) != 32: raise ValueError('Invalid rand') self.select_path(['3f00', '7f20']) - return self.send_apdu_checksw('a088000010' + rand, sw='9000') + return self.send_apdu_checksw('a088000010' + rand, sw='9000', apply_lchan = False)
def authenticate(self, rand: Hexstr, autn: Hexstr, context: str = '3g') -> ResTuple: """Execute AUTHENTICATE (USIM/ISIM). @@ -603,7 +603,7 @@ else: raise ValueError("Unsupported context '%s'" % context) (data, sw) = self.send_apdu_constr_checksw( - self.cla_byte, '88', '00', p2, AuthCmd3G, cmd_data, AuthResp3G) + self.cla_byte, '88', '00', p2, AuthCmd3G, cmd_data, AuthResp3G, apply_lchan = False) if 'auts' in data: ret = {'synchronisation_failure': data} else: @@ -624,11 +624,11 @@ Args: fid : file identifier as hex string """ - return self.send_apdu_checksw(self.cla_byte + '44000002' + fid) + return self.send_apdu_checksw(self.cla_byte + '44000002' + fid, apply_lchan = False)
def create_file(self, payload: Hexstr) -> ResTuple: """Execute CREEATE FILE command as per TS 102 222 Section 6.3""" - return self.send_apdu_checksw(self.cla_byte + 'e00000%02x%s' % (len(payload)//2, payload)) + return self.send_apdu_checksw(self.cla_byte + 'e00000%02x%s' % (len(payload)//2, payload), apply_lchan = False)
def resize_file(self, payload: Hexstr) -> ResTuple: """Execute RESIZE FILE command as per TS 102 222 Section 6.10""" @@ -636,19 +636,19 @@
def delete_file(self, fid: Hexstr) -> ResTuple: """Execute DELETE FILE command as per TS 102 222 Section 6.4""" - return self.send_apdu_checksw(self.cla_byte + 'e4000002' + fid) + return self.send_apdu_checksw(self.cla_byte + 'e4000002' + fid, apply_lchan = False)
def terminate_df(self, fid: Hexstr) -> ResTuple: """Execute TERMINATE DF command as per TS 102 222 Section 6.7""" - return self.send_apdu_checksw(self.cla_byte + 'e6000002' + fid) + return self.send_apdu_checksw(self.cla_byte + 'e6000002' + fid, apply_lchan = False)
def terminate_ef(self, fid: Hexstr) -> ResTuple: """Execute TERMINATE EF command as per TS 102 222 Section 6.8""" - return self.send_apdu_checksw(self.cla_byte + 'e8000002' + fid) + return self.send_apdu_checksw(self.cla_byte + 'e8000002' + fid, apply_lchan = False)
def terminate_card_usage(self) -> ResTuple: """Execute TERMINATE CARD USAGE command as per TS 102 222 Section 6.9""" - return self.send_apdu_checksw(self.cla_byte + 'fe000000') + return self.send_apdu_checksw(self.cla_byte + 'fe000000', apply_lchan = False)
def manage_channel(self, mode: str = 'open', lchan_nr: int =0) -> ResTuple: """Execute MANAGE CHANNEL command as per TS 102 221 Section 11.1.17. @@ -662,7 +662,7 @@ else: p1 = 0x00 pdu = self.cla_byte + '70%02x%02x00' % (p1, lchan_nr) - return self.send_apdu_checksw(pdu) + return self.send_apdu_checksw(pdu, apply_lchan = False)
def reset_card(self) -> Hexstr: """Physically reset the card""" @@ -683,7 +683,7 @@ code : chv code as hex string """ fc = rpad(b2h(code), 16) - data, sw = self.send_apdu(self.cla_byte + '2000' + ('%02X' % chv_no) + '08' + fc) + data, sw = self.send_apdu(self.cla_byte + '2000' + ('%02X' % chv_no) + '08' + fc, apply_lchan = False) self._chv_process_sw('verify', chv_no, code, sw) return (data, sw)
@@ -696,7 +696,7 @@ pin_code : new chv code as hex string """ fc = rpad(b2h(puk_code), 16) + rpad(b2h(pin_code), 16) - data, sw = self.send_apdu(self.cla_byte + '2C00' + ('%02X' % chv_no) + '10' + fc) + data, sw = self.send_apdu(self.cla_byte + '2C00' + ('%02X' % chv_no) + '10' + fc, apply_lchan = False) self._chv_process_sw('unblock', chv_no, pin_code, sw) return (data, sw)
@@ -709,7 +709,7 @@ new_pin_code : new chv code as hex string """ fc = rpad(b2h(pin_code), 16) + rpad(b2h(new_pin_code), 16) - data, sw = self.send_apdu(self.cla_byte + '2400' + ('%02X' % chv_no) + '10' + fc) + data, sw = self.send_apdu(self.cla_byte + '2400' + ('%02X' % chv_no) + '10' + fc, apply_lchan = False) self._chv_process_sw('change', chv_no, pin_code, sw) return (data, sw)
@@ -722,7 +722,7 @@ new_pin_code : new chv code as hex string """ fc = rpad(b2h(pin_code), 16) - data, sw = self.send_apdu(self.cla_byte + '2600' + ('%02X' % chv_no) + '08' + fc) + data, sw = self.send_apdu(self.cla_byte + '2600' + ('%02X' % chv_no) + '08' + fc, apply_lchan = False) self._chv_process_sw('disable', chv_no, pin_code, sw) return (data, sw)
@@ -734,7 +734,7 @@ pin_code : chv code as hex string """ fc = rpad(b2h(pin_code), 16) - data, sw = self.send_apdu(self.cla_byte + '2800' + ('%02X' % chv_no) + '08' + fc) + data, sw = self.send_apdu(self.cla_byte + '2800' + ('%02X' % chv_no) + '08' + fc, apply_lchan = False) self._chv_process_sw('enable', chv_no, pin_code, sw) return (data, sw)