This is merely a historical archive of years 2008-2021, before the migration to mailman3.
A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.
dexter gerrit-no-reply at lists.osmocom.orgdexter has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/26055 ) Change subject: commands: do not check SW manually, use send_apdu_checksw() ...................................................................... commands: do not check SW manually, use send_apdu_checksw() The transport layer provides a method send_apdu_checksw to send APDUs and to be sure the SW is the expected one. Given that, there is no need to verify the SW manually. The exception of send_apdu_checksw will catch the problem and also display the SW in a human readable form. Change-Id: I9ce556ac0b7bb21c5c5a27170c32af0152255b79 Related: OS#5275 --- M pySim/commands.py M pySim/utils.py 2 files changed, 30 insertions(+), 16 deletions(-) Approvals: daniel: Looks good to me, approved osmith: Looks good to me, but someone else must approve Jenkins Builder: Verified diff --git a/pySim/commands.py b/pySim/commands.py index 805fe61..1faa00f 100644 --- a/pySim/commands.py +++ b/pySim/commands.py @@ -23,7 +23,7 @@ from construct import * from pySim.construct import LV -from pySim.utils import rpad, b2h, h2b, sw_match, bertlv_encode_len, Hexstr, h2i +from pySim.utils import rpad, b2h, h2b, sw_match, bertlv_encode_len, Hexstr, h2i, str_sanitize from pySim.exceptions import SwMatchError class SimCardCommands(object): @@ -145,12 +145,12 @@ while chunk_offset < length: chunk_len = min(255, length-chunk_offset) pdu = self.cla_byte + 'b0%04x%02x' % (offset + chunk_offset, chunk_len) - data,sw = self._tp.send_apdu(pdu) - if sw == '9000': - total_data += data - chunk_offset += chunk_len - else: - raise ValueError('Failed to read (offset %d)' % (offset)) + try: + data, sw = self._tp.send_apdu_checksw(pdu) + except Exception as e: + raise ValueError('%s, failed to read (offset %d)' % (str_sanitize(str(e)), offset)) + total_data += data + chunk_offset += chunk_len return total_data, sw def update_binary(self, ef, data:str, offset:int=0, verify:bool=False, conserve:bool=False): @@ -172,22 +172,21 @@ self.select_path(ef) total_data = '' - total_sw = "9000" chunk_offset = 0 while chunk_offset < data_length: chunk_len = min(255, data_length - chunk_offset) # chunk_offset is bytes, but data slicing is hex chars, so we need to multiply by 2 pdu = self.cla_byte + 'd6%04x%02x' % (offset + chunk_offset, chunk_len) + data[chunk_offset*2 : (chunk_offset+chunk_len)*2] - chunk_data, chunk_sw = self._tp.send_apdu(pdu) - if chunk_sw == total_sw: - total_data += chunk_data - chunk_offset += chunk_len - else: - total_sw = chunk_sw - raise ValueError('Failed to write chunk (chunk_offset %d, chunk_len %d)' % (chunk_offset, chunk_len)) + try: + chunk_data, chunk_sw = self._tp.send_apdu_checksw(pdu) + except Exception as e: + raise ValueError('%s, failed to write chunk (chunk_offset %d, chunk_len %d)' % \ + (str_sanitize(str(e)), chunk_offset, chunk_len)) + total_data += data + chunk_offset += chunk_len if verify: self.verify_binary(ef, data, offset) - return total_data, total_sw + return total_data, chunk_sw def verify_binary(self, ef, data:str, offset:int=0): """Verify contents of transparent EF. diff --git a/pySim/utils.py b/pySim/utils.py index 68de14a..8f2b2e9 100644 --- a/pySim/utils.py +++ b/pySim/utils.py @@ -5,6 +5,7 @@ import json import abc +import string from io import BytesIO from typing import Optional, List, Dict, Any, Tuple @@ -89,6 +90,20 @@ def half_round_up(n:int) -> int: return (n + 1)//2 +def str_sanitize(s:str) -> str: + """replace all non printable chars, line breaks and whitespaces, with ' ', make sure that + there are no whitespaces at the end and at the beginning of the string. + + Args: + s : string to sanitize + Returns: + filtered result of string 's' + """ + + chars_to_keep = string.digits + string.ascii_letters + string.punctuation + res = ''.join([c if c in chars_to_keep else ' ' for c in s]) + return res.strip() + ######################################################################### # poor man's COMPREHENSION-TLV decoder. ######################################################################### -- To view, visit https://gerrit.osmocom.org/c/pysim/+/26055 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: pysim Gerrit-Branch: master Gerrit-Change-Id: I9ce556ac0b7bb21c5c5a27170c32af0152255b79 Gerrit-Change-Number: 26055 Gerrit-PatchSet: 6 Gerrit-Owner: dexter <pmaier at sysmocom.de> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: daniel <dwillmann at sysmocom.de> Gerrit-Reviewer: dexter <pmaier at sysmocom.de> Gerrit-Reviewer: fixeria <vyanitskiy at sysmocom.de> Gerrit-Reviewer: laforge <laforge at osmocom.org> Gerrit-Reviewer: osmith <osmith at sysmocom.de> Gerrit-MessageType: merged -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20211105/3c4626b6/attachment.htm>