diff options
author | Harald Welte <laforge@osmocom.org> | 2021-04-10 17:22:35 +0200 |
---|---|---|
committer | Harald Welte <laforge@osmocom.org> | 2021-04-11 12:20:29 +0200 |
commit | e0f9ef16060f91d1ff36dbbac77ae44c9bac4b7a (patch) | |
tree | 5a4c0f683f849d23e7936e47dd2507dd3e13b309 /pySim/transport/__init__.py | |
parent | d0505bdb5574257ce52404384864a596227f79bb (diff) |
integrate 'construct' python library
'construct' is a declarative symmetric encoder/decoder for user
specified binary formats. It should come in extremely handy in
tools like pySim.
We start the integration by adding transport methods for transceiving
APDUs with built-in encoding of the command data and decoding of the
response data.
Change-Id: Ibf457aa8b9480a8db5979defcfafd67674303f6c
Diffstat (limited to 'pySim/transport/__init__.py')
-rw-r--r-- | pySim/transport/__init__.py | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/pySim/transport/__init__.py b/pySim/transport/__init__.py index 96ad974..290bc7c 100644 --- a/pySim/transport/__init__.py +++ b/pySim/transport/__init__.py @@ -6,10 +6,12 @@ from typing import Optional from pySim.exceptions import * -from pySim.utils import sw_match +from pySim.construct import filter_dict +from pySim.utils import sw_match, b2h, h2b, i2h # # Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com> +# Copyright (C) 2021 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 @@ -127,6 +129,52 @@ class LinkBase(object): raise SwMatchError(rv[1], sw.lower(), self.sw_interpreter) return rv + def send_apdu_constr(self, cla, ins, p1, p2, cmd_constr, cmd_data, resp_constr): + """Build and sends an APDU using a 'construct' definition; parses response. + + Args: + cla : string (in hex) ISO 7816 class byte + ins : string (in hex) ISO 7816 instruction byte + p1 : string (in hex) ISO 7116 Parameter 1 byte + p2 : string (in hex) ISO 7116 Parameter 2 byte + cmd_cosntr : defining how to generate binary APDU command data + cmd_data : command data passed to cmd_constr + resp_cosntr : defining how to decode binary APDU response data + Returns: + Tuple of (decoded_data, sw) + """ + cmd = cmd_constr.build(cmd_data) if cmd_data else '' + p3 = i2h([len(cmd)]) + pdu = ''.join([cla, ins, p1, p2, p3, b2h(cmd)]) + (data, sw) = self.send_apdu(pdu) + if data: + # filter the resulting dict to avoid '_io' members inside + rsp = filter_dict(resp_constr.parse(h2b(data))) + else: + rsp = None + return (rsp, sw) + + def send_apdu_constr_checksw(self, cla, ins, p1, p2, cmd_constr, cmd_data, resp_constr, + sw_exp="9000"): + """Build and sends an APDU using a 'construct' definition; parses response. + + Args: + cla : string (in hex) ISO 7816 class byte + ins : string (in hex) ISO 7816 instruction byte + p1 : string (in hex) ISO 7116 Parameter 1 byte + p2 : string (in hex) ISO 7116 Parameter 2 byte + cmd_cosntr : defining how to generate binary APDU command data + cmd_data : command data passed to cmd_constr + resp_cosntr : defining how to decode binary APDU response data + exp_sw : string (in hex) of status word (ex. "9000") + Returns: + Tuple of (decoded_data, sw) + """ + (rsp, sw) = self.send_apdu_constr(cla, ins, p1, p2, cmd_constr, cmd_data, resp_constr) + if not sw_match(sw, sw_exp): + raise SwMatchError(sw, sw_exp.lower(), self.sw_interpreter) + return (rsp, sw) + def init_reader(opts, **kwargs) -> Optional[LinkBase]: """ Init card reader driver |