<p>dexter <strong>submitted</strong> this change.</p><p><a href="https://gerrit.osmocom.org/c/pysim/+/26055">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  daniel: Looks good to me, approved
  osmith: Looks good to me, but someone else must approve
  Jenkins Builder: Verified

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">commands: do not check SW manually, use send_apdu_checksw()<br><br>The transport layer provides a method send_apdu_checksw to send APDUs<br>and to be sure the SW is the expected one. Given that, there is no need<br>to verify the SW manually. The exception of send_apdu_checksw will catch<br>the problem and also display the SW in a human readable form.<br><br>Change-Id: I9ce556ac0b7bb21c5c5a27170c32af0152255b79<br>Related: OS#5275<br>---<br>M pySim/commands.py<br>M pySim/utils.py<br>2 files changed, 30 insertions(+), 16 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/pySim/commands.py b/pySim/commands.py</span><br><span>index 805fe61..1faa00f 100644</span><br><span>--- a/pySim/commands.py</span><br><span>+++ b/pySim/commands.py</span><br><span>@@ -23,7 +23,7 @@</span><br><span> </span><br><span> from construct import *</span><br><span> from pySim.construct import LV</span><br><span style="color: hsl(0, 100%, 40%);">-from pySim.utils import rpad, b2h, h2b, sw_match, bertlv_encode_len, Hexstr, h2i</span><br><span style="color: hsl(120, 100%, 40%);">+from pySim.utils import rpad, b2h, h2b, sw_match, bertlv_encode_len, Hexstr, h2i, str_sanitize</span><br><span> from pySim.exceptions import SwMatchError</span><br><span> </span><br><span> class SimCardCommands(object):</span><br><span>@@ -145,12 +145,12 @@</span><br><span>           while chunk_offset < length:</span><br><span>                      chunk_len = min(255, length-chunk_offset)</span><br><span>                    pdu = self.cla_byte + 'b0%04x%02x' % (offset + chunk_offset, chunk_len)</span><br><span style="color: hsl(0, 100%, 40%);">-                 data,sw = self._tp.send_apdu(pdu)</span><br><span style="color: hsl(0, 100%, 40%);">-                       if sw == '9000':</span><br><span style="color: hsl(0, 100%, 40%);">-                                total_data += data</span><br><span style="color: hsl(0, 100%, 40%);">-                              chunk_offset += chunk_len</span><br><span style="color: hsl(0, 100%, 40%);">-                       else:</span><br><span style="color: hsl(0, 100%, 40%);">-                           raise ValueError('Failed to read (offset %d)' % (offset))</span><br><span style="color: hsl(120, 100%, 40%);">+                     try:</span><br><span style="color: hsl(120, 100%, 40%);">+                          data, sw = self._tp.send_apdu_checksw(pdu)</span><br><span style="color: hsl(120, 100%, 40%);">+                    except Exception as e:</span><br><span style="color: hsl(120, 100%, 40%);">+                                raise ValueError('%s, failed to read (offset %d)' % (str_sanitize(str(e)), offset))</span><br><span style="color: hsl(120, 100%, 40%);">+                   total_data += data</span><br><span style="color: hsl(120, 100%, 40%);">+                    chunk_offset += chunk_len</span><br><span>            return total_data, sw</span><br><span> </span><br><span>    def update_binary(self, ef, data:str, offset:int=0, verify:bool=False, conserve:bool=False):</span><br><span>@@ -172,22 +172,21 @@</span><br><span> </span><br><span>             self.select_path(ef)</span><br><span>                 total_data = ''</span><br><span style="color: hsl(0, 100%, 40%);">-         total_sw = "9000"</span><br><span>          chunk_offset = 0</span><br><span>             while chunk_offset < data_length:</span><br><span>                         chunk_len = min(255, data_length - chunk_offset)</span><br><span>                     # chunk_offset is bytes, but data slicing is hex chars, so we need to multiply by 2</span><br><span>                  pdu = self.cla_byte + 'd6%04x%02x' % (offset + chunk_offset, chunk_len) + data[chunk_offset*2 : (chunk_offset+chunk_len)*2]</span><br><span style="color: hsl(0, 100%, 40%);">-                     chunk_data, chunk_sw = self._tp.send_apdu(pdu)</span><br><span style="color: hsl(0, 100%, 40%);">-                  if chunk_sw == total_sw:</span><br><span style="color: hsl(0, 100%, 40%);">-                                total_data += chunk_data</span><br><span style="color: hsl(0, 100%, 40%);">-                                chunk_offset += chunk_len</span><br><span style="color: hsl(0, 100%, 40%);">-                       else:</span><br><span style="color: hsl(0, 100%, 40%);">-                           total_sw = chunk_sw</span><br><span style="color: hsl(0, 100%, 40%);">-                             raise ValueError('Failed to write chunk (chunk_offset %d, chunk_len %d)' % (chunk_offset, chunk_len))</span><br><span style="color: hsl(120, 100%, 40%);">+                 try:</span><br><span style="color: hsl(120, 100%, 40%);">+                          chunk_data, chunk_sw = self._tp.send_apdu_checksw(pdu)</span><br><span style="color: hsl(120, 100%, 40%);">+                        except Exception as e:</span><br><span style="color: hsl(120, 100%, 40%);">+                                raise ValueError('%s, failed to write chunk (chunk_offset %d, chunk_len %d)' % \</span><br><span style="color: hsl(120, 100%, 40%);">+                                               (str_sanitize(str(e)), chunk_offset, chunk_len))</span><br><span style="color: hsl(120, 100%, 40%);">+                     total_data += data</span><br><span style="color: hsl(120, 100%, 40%);">+                    chunk_offset += chunk_len</span><br><span>            if verify:</span><br><span>                   self.verify_binary(ef, data, offset)</span><br><span style="color: hsl(0, 100%, 40%);">-            return total_data, total_sw</span><br><span style="color: hsl(120, 100%, 40%);">+           return total_data, chunk_sw</span><br><span> </span><br><span>      def verify_binary(self, ef, data:str, offset:int=0):</span><br><span>                 """Verify contents of transparent EF.</span><br><span>diff --git a/pySim/utils.py b/pySim/utils.py</span><br><span>index 68de14a..8f2b2e9 100644</span><br><span>--- a/pySim/utils.py</span><br><span>+++ b/pySim/utils.py</span><br><span>@@ -5,6 +5,7 @@</span><br><span> </span><br><span> import json</span><br><span> import abc</span><br><span style="color: hsl(120, 100%, 40%);">+import string</span><br><span> from io import BytesIO</span><br><span> from typing import Optional, List, Dict, Any, Tuple</span><br><span> </span><br><span>@@ -89,6 +90,20 @@</span><br><span> def half_round_up(n:int) -> int:</span><br><span>         return (n + 1)//2</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+def str_sanitize(s:str) -> str:</span><br><span style="color: hsl(120, 100%, 40%);">+     """replace all non printable chars, line breaks and whitespaces, with ' ', make sure that</span><br><span style="color: hsl(120, 100%, 40%);">+      there are no whitespaces at the end and at the beginning of the string.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     Args:</span><br><span style="color: hsl(120, 100%, 40%);">+         s : string to sanitize</span><br><span style="color: hsl(120, 100%, 40%);">+        Returns:</span><br><span style="color: hsl(120, 100%, 40%);">+              filtered result of string 's'</span><br><span style="color: hsl(120, 100%, 40%);">+ """</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  chars_to_keep = string.digits + string.ascii_letters + string.punctuation</span><br><span style="color: hsl(120, 100%, 40%);">+     res = ''.join([c if c in chars_to_keep else ' ' for c in s])</span><br><span style="color: hsl(120, 100%, 40%);">+  return res.strip()</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> #########################################################################</span><br><span> # poor man's COMPREHENSION-TLV decoder.</span><br><span> #########################################################################</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/pysim/+/26055">change 26055</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/c/pysim/+/26055"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: pysim </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I9ce556ac0b7bb21c5c5a27170c32af0152255b79 </div>
<div style="display:none"> Gerrit-Change-Number: 26055 </div>
<div style="display:none"> Gerrit-PatchSet: 6 </div>
<div style="display:none"> Gerrit-Owner: dexter <pmaier@sysmocom.de> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins Builder </div>
<div style="display:none"> Gerrit-Reviewer: daniel <dwillmann@sysmocom.de> </div>
<div style="display:none"> Gerrit-Reviewer: dexter <pmaier@sysmocom.de> </div>
<div style="display:none"> Gerrit-Reviewer: fixeria <vyanitskiy@sysmocom.de> </div>
<div style="display:none"> Gerrit-Reviewer: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-Reviewer: osmith <osmith@sysmocom.de> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>