Change in pysim[master]: commands, ts_102_221: replace pytlv with uttlv

dexter gerrit-no-reply at lists.osmocom.org
Wed Mar 24 15:42:49 UTC 2021


dexter has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/23483 )


Change subject: commands, ts_102_221: replace pytlv with uttlv
......................................................................

commands, ts_102_221: replace pytlv with uttlv

pytlv requires to tell it all expected tags in a TLV string that is
going to be parsed. This often causes problems since TLV is by design
extensible, so newer specifications may add additional tags which our
code then would not tolerate. uttlv does not have such a limitation, pre
defining the tags in advance is optional here. Lets use uttlv from now
on.

Change-Id: I971b818836aff2fbeb6769f819d496cee4bfa7cf
Related: OS#4963
---
M README.md
M contrib/jenkins.sh
M pySim/commands.py
M pySim/ts_102_221.py
M requirements.txt
5 files changed, 60 insertions(+), 45 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/83/23483/1

diff --git a/README.md b/README.md
index 028a84a..8cce9ac 100644
--- a/README.md
+++ b/README.md
@@ -36,13 +36,13 @@
 
 - pyscard
 - serial
-- pytlv (for specific card types)
+- uttlv (for specific card types)
 - cmd2 (for pySim-shell.py)
 
 Example for Debian:
 
 	apt-get install python3-pyscard python3-serial python3-cmd2 python3-pip python3-yaml
-	pip3 install pytlv
+	pip3 install uttlv
 
 Alternatively, everything can be installed using pip:
 
diff --git a/contrib/jenkins.sh b/contrib/jenkins.sh
index e286047..db5a75e 100755
--- a/contrib/jenkins.sh
+++ b/contrib/jenkins.sh
@@ -11,7 +11,7 @@
 
 virtualenv -p python3 venv --system-site-packages
 . venv/bin/activate
-pip install pytlv
+pip install uttlv
 pip install pyyaml
 pip install cmd2
 
diff --git a/pySim/commands.py b/pySim/commands.py
index 9aed588..ef355e7 100644
--- a/pySim/commands.py
+++ b/pySim/commands.py
@@ -21,7 +21,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
-from pySim.utils import rpad, b2h
+from pySim.utils import rpad, b2h, h2b
 
 class SimCardCommands(object):
 	def __init__(self, transport):
@@ -33,11 +33,10 @@
 	def __parse_fcp(self, fcp):
 		# see also: ETSI TS 102 221, chapter 11.1.1.3.1 Response for MF,
 		# DF or ADF
-		from pytlv.TLV import TLV
-		tlvparser = TLV(['82', '83', '84', 'a5', '8a', '8b', '8c', '80', 'ab', 'c6', '81', '88'])
-
-		# pytlv is case sensitive!
-		fcp = fcp.lower()
+		from uttlv import TLV
+		tlvparser = TLV()
+		tlvparser.tag_size = 1
+		tlvparser.len_size = 1
 
 		if fcp[0:2] != '62':
 			raise ValueError('Tag of the FCP template does not match, expected 62 but got %s'%fcp[0:2])
@@ -57,7 +56,8 @@
 
 		# Skip FCP tag and length
 		tlv = fcp[skip:]
-		return tlvparser.parse(tlv)
+		tlvparser.parse_array(bytes(h2b(tlv)))
+		return tlvparser
 
 	# Tell the length of a record by the card response
 	# USIMs respond with an FCP template, which is different
@@ -67,7 +67,7 @@
 	def __record_len(self, r):
 		if self.sel_ctrl == "0004":
 			tlv_parsed = self.__parse_fcp(r[-1])
-			file_descriptor = tlv_parsed['82']
+			file_descriptor = b2h(tlv_parsed[0x82])
 			# See also ETSI TS 102 221, chapter 11.1.1.4.3 File Descriptor
 			return int(file_descriptor[4:8], 16)
 		else:
@@ -78,7 +78,7 @@
 	def __len(self, r):
 		if self.sel_ctrl == "0004":
 			tlv_parsed = self.__parse_fcp(r[-1])
-			return int(tlv_parsed['80'], 16)
+			return int(b2h(tlv_parsed[0x80]), 16)
 		else:
 			return int(r[-1][4:8], 16)
 
diff --git a/pySim/ts_102_221.py b/pySim/ts_102_221.py
index 256a697..a8d67a1 100644
--- a/pySim/ts_102_221.py
+++ b/pySim/ts_102_221.py
@@ -17,7 +17,7 @@
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 """
 
-from pytlv.TLV import *
+from uttlv import TLV
 from struct import pack, unpack
 from pySim.utils import *
 from pySim.filesystem import *
@@ -122,18 +122,6 @@
     '83': lambda x: int(x, 16),
     }
 
-# pytlv unfortunately doesn't have a setting using which we can make it
-# accept unknown tags.  It also doesn't raise a specific exception type but
-# just the generic ValueError, so we cannot ignore those either.  Instead,
-# we insert a dict entry for every possible proprietary tag permitted
-def fixup_fcp_proprietary_tlv_map(tlv_map):
-    if 'D0' in tlv_map:
-        return
-    for i in range(0xd0, 0xff):
-        i_hex = i2h([i]).upper()
-        tlv_map[i_hex] = 'proprietary_' + i_hex
-
-
 def tlv_key_replace(inmap, indata):
     def newkey(inmap, key):
         if key in inmap:
@@ -153,22 +141,39 @@
 
 # ETSI TS 102 221 Section 11.1.1.3
 def decode_select_response(resp_hex):
-    fixup_fcp_proprietary_tlv_map(FCP_Proprietary_TLV_MAP)
-    resp_hex = resp_hex.upper()
     # outer layer
-    fcp_base_tlv = TLV(['62'])
-    fcp_base = fcp_base_tlv.parse(resp_hex)
+    fcp_base_tlv = TLV()
+    fcp_base_tlv.tag_size = 1
+    fcp_base_tlv.len_size = 1
+    fcp_base_tlv.parse_array(bytes(h2b(resp_hex)))
+
     # actual FCP
-    fcp_tlv = TLV(FCP_TLV_MAP)
-    fcp = fcp_tlv.parse(fcp_base['62'])
+    fcp_tlv = TLV()
+    fcp_tlv.tag_size = 1
+    fcp_tlv.len_size = 1
+    fcp_tlv.parse_array(fcp_base_tlv[0x62])
+
     # further decode the proprietary information
-    if fcp['A5']:
-        prop_tlv = TLV(FCP_Proprietary_TLV_MAP)
-        prop = prop_tlv.parse(fcp['A5'])
-        fcp['A5'] = tlv_val_interpret(FCP_prorietary_interpreter_map, prop)
-        fcp['A5'] = tlv_key_replace(FCP_Proprietary_TLV_MAP, fcp['A5'])
+    if fcp_tlv[0xA5]:
+        prop_tlv = TLV()
+        prop_tlv.tag_size = 1
+        prop_tlv.len_size = 1
+        prop_tlv.parse_array(fcp_tlv[0xA5])
+
+        fcp = fcp_tlv._items
+        prop = prop_tlv._items
+
+        fcp_str = {}
+        for key in fcp:
+                fcp_str.update({("%02X" % key):b2h(fcp[key])})
+        prop_str = {}
+        for key in prop:
+                prop_str.update({("%02X" % key):b2h(prop[key])})
+
+        fcp_str['A5'] = tlv_val_interpret(FCP_prorietary_interpreter_map, prop_str)
+        fcp_str['A5'] = tlv_key_replace(FCP_Proprietary_TLV_MAP, fcp_str['A5'])
     # finally make sure we get human-readable keys in the output dict
-    r = tlv_val_interpret(FCP_interpreter_map, fcp)
+    r = tlv_val_interpret(FCP_interpreter_map, fcp_str)
     return tlv_key_replace(FCP_TLV_MAP, r)
 
 
@@ -178,14 +183,24 @@
         super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len={5,54})
 
     def _decode_record_hex(self, raw_hex_data):
-        raw_hex_data = raw_hex_data.upper()
-        atempl_base_tlv = TLV(['61'])
-        atempl_base = atempl_base_tlv.parse(raw_hex_data)
-        atempl_TLV_MAP = {'4F': 'aid_value', 50:'label'}
-        atempl_tlv = TLV(atempl_TLV_MAP)
-        atempl = atempl_tlv.parse(atempl_base['61'])
+        atempl_base_tlv = TLV()
+        atempl_base_tlv.tag_size = 1
+        atempl_base_tlv.len_size = 1
+        atempl_base_tlv.parse_array(bytes(h2b(raw_hex_data)))
+
+        atempl_tlv = TLV()
+        atempl_tlv.tag_size = 1
+        atempl_tlv.len_size = 1
+        atempl_tlv.parse_array(atempl_base_tlv[0x61])
+
+        atempl = atempl_tlv._items
+        atempl_str = {}
+        for key in atempl:
+                atempl_str.update({("%02X" % key):b2h(atempl[key])})
+
         # FIXME: "All other Dos are according to ISO/IEC 7816-4"
-        return tlv_key_replace(atempl_TLV_MAP, atempl)
+        atempl_TLV_MAP = {'4F': 'aid_value', 50:'label'}
+        return tlv_key_replace(atempl_TLV_MAP, atempl_str)
 
 # TS 102 221 Section 13.2
 class EF_ICCID(TransparentEF):
diff --git a/requirements.txt b/requirements.txt
index 978a3db..fb900b0 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,4 +1,4 @@
 pyscard
 serial
-pytlv
+uttlv
 cmd2

-- 
To view, visit https://gerrit.osmocom.org/c/pysim/+/23483
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I971b818836aff2fbeb6769f819d496cee4bfa7cf
Gerrit-Change-Number: 23483
Gerrit-PatchSet: 1
Gerrit-Owner: dexter <pmaier at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210324/f6ec76a5/attachment-0001.htm>


More information about the gerrit-log mailing list