From 33a6daee6d70c681d468e95b2c4e42e3d17cd233 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sat, 8 Jun 2024 17:26:51 +0200 Subject: [PATCH] pySim.apdu: Allow TLV based decoders for APDU command and response body So far we only supported construct. Change-Id: Ibb80d328c9a1f464aa5338ca0ca1d6bfb00734e1 --- pySim/apdu/__init__.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/pySim/apdu/__init__.py b/pySim/apdu/__init__.py index f5b78520..02ccbadc 100644 --- a/pySim/apdu/__init__.py +++ b/pySim/apdu/__init__.py @@ -9,7 +9,7 @@ is far too simplistic, while this decoder can utilize all of the information we already know in pySim about the filesystem structure, file encoding, etc. """ -# (C) 2022 by Harald Welte +# (C) 2022-2024 by Harald Welte # # 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 @@ -152,6 +152,8 @@ class ApduCommand(Apdu, metaclass=ApduCommandMeta): _construct_p2 = Byte _construct = HexAdapter(GreedyBytes) _construct_rsp = HexAdapter(GreedyBytes) + _tlv = None + _tlv_rsp = None def __init__(self, cmd: BytesOrHex, rsp: Optional[BytesOrHex] = None): """Instantiate a new ApduCommand from give cmd + resp.""" @@ -299,7 +301,12 @@ class ApduCommand(Apdu, metaclass=ApduCommandMeta): r['p2'] = parse_construct(self._construct_p2, self.p2.to_bytes(1, 'big')) r['p3'] = self.p3 if self.cmd_data: - r['body'] = parse_construct(self._construct, self.cmd_data) + if self._tlv: + ie = self._tlv() + ie.from_tlv(self.cmd_data) + r['body'] = ie.to_dict() + else: + r['body'] = parse_construct(self._construct, self.cmd_data) return r def rsp_to_dict(self) -> Dict: @@ -310,7 +317,12 @@ class ApduCommand(Apdu, metaclass=ApduCommandMeta): else: r = {} if self.rsp_data: - r['body'] = parse_construct(self._construct_rsp, self.rsp_data) + if self._tlv_rsp: + ie = self._tlv_rsp() + ie.from_tlv(self.rsp_data) + r['body'] = ie.to_dict() + else: + r['body'] = parse_construct(self._construct_rsp, self.rsp_data) r['sw'] = b2h(self.sw) return r