laforge submitted this change.

View Change

Approvals: Jenkins Builder: Verified laforge: Looks good to me, approved
ts_102_221: Implement proper parsing of EF.DIR

EF.DIR can not only contain the AID + Label of TS 102 221, but can
also contain any of the DOs specified in ISO7816-4. Let's imoplement
this based on the modern pySim.tlv parser

Change-Id: I875eb49e1f0370428c2eae69af84f5483bd5b1fc
Closes: OS#5410
---
A pySim/iso7816_4.py
M pySim/ts_102_221.py
2 files changed, 78 insertions(+), 10 deletions(-)

diff --git a/pySim/iso7816_4.py b/pySim/iso7816_4.py
new file mode 100644
index 0000000..ea838cf
--- /dev/null
+++ b/pySim/iso7816_4.py
@@ -0,0 +1,62 @@
+# coding=utf-8
+"""Utilities / Functions related to ISO 7816-4
+
+(C) 2022 by Harald Welte <laforge@osmocom.org>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+"""
+
+from construct import *
+from pySim.construct import *
+from pySim.utils import *
+from pySim.filesystem import *
+from pySim.tlv import *
+
+# Table 91 + Section 8.2.1.2
+class ApplicationId(BER_TLV_IE, tag=0x4f):
+ _construct = GreedyBytes
+
+# Table 91
+class ApplicationLabel(BER_TLV_IE, tag=0x50):
+ _construct = GreedyBytes
+
+# Table 91 + Section 5.3.1.2
+class FileReference(BER_TLV_IE, tag=0x51):
+ _construct = GreedyBytes
+
+# Table 91
+class CommandApdu(BER_TLV_IE, tag=0x52):
+ _construct = GreedyBytes
+
+# Table 91
+class DiscretionaryData(BER_TLV_IE, tag=0x53):
+ _construct = GreedyBytes
+
+# Table 91
+class DiscretionaryTemplate(BER_TLV_IE, tag=0x73):
+ _construct = GreedyBytes
+
+# Table 91 + RFC1738 / RFC2396
+class URL(BER_TLV_IE, tag=0x5f50):
+ _construct = GreedyString('ascii')
+
+# Table 91
+class ApplicationRelatedDOSet(BER_TLV_IE, tag=0x61):
+ _construct = GreedyBytes
+
+# Section 8.2.1.3 Application Template
+class ApplicationTemplate(BER_TLV_IE, tag=0x61, nested=[ApplicationId, ApplicationLabel, FileReference,
+ CommandApdu, DiscretionaryData, DiscretionaryTemplate,URL,
+ ApplicationRelatedDOSet]):
+ pass
diff --git a/pySim/ts_102_221.py b/pySim/ts_102_221.py
index 71d5b82..1955484 100644
--- a/pySim/ts_102_221.py
+++ b/pySim/ts_102_221.py
@@ -22,10 +22,12 @@
from pySim.construct import *
from pySim.utils import *
from pySim.filesystem import *
+from pySim.tlv import *
from bidict import bidict
from pySim.profile import CardProfile
from pySim.profile import match_uicc
from pySim.profile import match_sim
+import pySim.iso7816_4 as iso7816_4

# A UICC will usually also support 2G functionality. If this is the case, we
# need to add DF_GSM and DF_TELECOM along with the UICC related files
@@ -502,18 +504,22 @@

# TS 102 221 Section 13.1
class EF_DIR(LinFixedEF):
+ class ApplicationLabel(BER_TLV_IE, tag=0x50):
+ # TODO: UCS-2 coding option as per Annex A of TS 102 221
+ _construct = GreedyString('ascii')
+
+ # see https://github.com/PyCQA/pylint/issues/5794
+ #pylint: disable=undefined-variable
+ class ApplicationTemplate(BER_TLV_IE, tag=0x61,
+ nested=[iso7816_4.ApplicationId, ApplicationLabel, iso7816_4.FileReference,
+ iso7816_4.CommandApdu, iso7816_4.DiscretionaryData,
+ iso7816_4.DiscretionaryTemplate, iso7816_4.URL,
+ iso7816_4.ApplicationRelatedDOSet]):
+ pass
+
def __init__(self, fid='2f00', sfid=0x1e, name='EF.DIR', desc='Application Directory'):
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'])
- # FIXME: "All other Dos are according to ISO/IEC 7816-4"
- return tlv_key_replace(atempl_TLV_MAP, atempl)
+ self._tlv = EF_DIR.ApplicationTemplate

# TS 102 221 Section 13.2
class EF_ICCID(TransparentEF):

To view, visit change 27130. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I875eb49e1f0370428c2eae69af84f5483bd5b1fc
Gerrit-Change-Number: 27130
Gerrit-PatchSet: 7
Gerrit-Owner: laforge <laforge@osmocom.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: fixeria <vyanitskiy@sysmocom.de>
Gerrit-Reviewer: laforge <laforge@osmocom.org>
Gerrit-MessageType: merged