laforge has uploaded this change for review. (
https://gerrit.osmocom.org/c/pysim/+/35556?usp=email )
Change subject: TLV: Add DGI encoding of "GP Scripting Language Annex B"
......................................................................
TLV: Add DGI encoding of "GP Scripting Language Annex B"
The DGI encoding is specified in Annex B of the
"GlobalPlatform Systems Scripting Language Specification v1.1.0"
which is an "archived" specification that is no longer published
by GlobalPlatform, despite it being referenced from the GlobalPlatform
Card Specification v2.3, which is the basis of the GSMA eSIM
specifications.
For some reason it was the belief of the specification authors that
yet another format of TLV encoding is needed, in addition to the BER-TLV
and COMPREHENSION-TLV used by the very same specifications.
The encoding of the tag is not really specified anywhere, but I've only
seen 16-bit examples. The encoding of the length is specified and
implemented here accordingly.
Change-Id: Ie29ab7eb39f3165f3d695fcc1f02051338095697
---
M pySim/tlv.py
M pySim/utils.py
2 files changed, 81 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/56/35556/1
diff --git a/pySim/tlv.py b/pySim/tlv.py
index 826f02f..b3e386a 100644
--- a/pySim/tlv.py
+++ b/pySim/tlv.py
@@ -302,6 +302,27 @@
return bertlv_encode_len(len(val))
+class DGI_TLV_IE(TLV_IE):
+ """TLV_IE formated as GlobalPlatform Systems Scripting Language
Specification v1.1.0 Annex B."""
+
+ def __init__(self, **kwargs):
+ super().__init__(**kwargs)
+
+ @classmethod
+ def _parse_tag_raw(cls, do: bytes) -> Tuple[int, bytes]:
+ return dgi_parse_tag_raw(do)
+
+ @classmethod
+ def _parse_len(cls, do: bytes) -> Tuple[int, bytes]:
+ return dgi_parse_len(do)
+
+ def _encode_tag(self) -> bytes:
+ return dgi_encode_tag(self._compute_tag())
+
+ def _encode_len(self, val: bytes) -> bytes:
+ return dgi_encode_len(len(val))
+
+
class TLV_IE_Collection(metaclass=TlvCollectionMeta):
# we specify the metaclass so any downstream subclasses will automatically use it
"""A TLV_IE_Collection consists of multiple TLV_IE classes identified
by their tags.
diff --git a/pySim/utils.py b/pySim/utils.py
index 6523d98..9df2698 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -358,6 +358,41 @@
return (tagdict, length, value, remainder)
+def dgi_parse_tag_raw(binary: bytes) -> Tuple[int, bytes]:
+ # In absence of any clear spec guidance we assume it's always 16 bit
+ return int.from_bytes(binary[:2], 'big'), binary[2:]
+
+def dgi_encode_tag(t) -> bytes:
+ return t.to_bytes(2, 'big')
+
+def dgi_encode_len(length: int) -> bytes:
+ """Encode a single Length value according to GlobalPlatform Systems
Scripting Language
+ Specification v1.1.0 Annex B.
+ Args:
+ length : length value to be encoded
+ Returns:
+ binary output data of encoded length field
+ """
+ if length < 255:
+ return length.to_bytes(1, 'big')
+ elif length <= 0xffff:
+ return b'\xff' + length.to_bytes(2, 'big')
+ else:
+ raise ValueError("Length > 32bits not supported")
+
+def dgi_parse_len(binary: bytes) -> Tuple[int, bytes]:
+ """Parse a single Length value according to GlobalPlatform Systems
Scripting Language
+ Specification v1.1.0 Annex B.
+ Args:
+ binary : binary input data of BER-TLV length field
+ Returns:
+ Tuple of (length, remainder)
+ """
+ if binary[0] == 255:
+ return ((binary[0] << 8) | binary[1:]), binary[2:]
+ else:
+ return binary[0], binary[1:]
+
# IMSI encoded format:
# For IMSI 0123456789ABCDE:
#
--
To view, visit
https://gerrit.osmocom.org/c/pysim/+/35556?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.osmocom.org/settings
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ie29ab7eb39f3165f3d695fcc1f02051338095697
Gerrit-Change-Number: 35556
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge(a)osmocom.org>
Gerrit-MessageType: newchange