From 5887fb70fb34d085ecd76562efd13ed74f1cc822 Mon Sep 17 00:00:00 2001 From: Philipp Maier Date: Fri, 1 Nov 2024 12:33:59 +0100 Subject: [PATCH] pySim-shell: allow checking of APDU responses The "apdu" command allows us to send custom APDUs to a card. This command is often used in low level initialization scripts or tests. To stop the script execution in case of an error, the command allows us to specify a status word that must match the status word of the response. But we have no such mechanism for the response itself. Let's add another parameter where we can pass a regex that the response must match. Related: OS#6367 Change-Id: I97bbcdf37bdcf00ad50a875b96940c211de7073d --- pySim-shell.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pySim-shell.py b/pySim-shell.py index 37d58ae6..f53efd3c 100755 --- a/pySim-shell.py +++ b/pySim-shell.py @@ -21,6 +21,7 @@ from typing import List, Optional import json import traceback +import re import cmd2 from packaging import version @@ -239,6 +240,7 @@ Online manual available at https://downloads.osmocom.org/docs/pysim/master/html/ apdu_cmd_parser = argparse.ArgumentParser() apdu_cmd_parser.add_argument('--expect-sw', help='expect a specified status word', type=str, default=None) + apdu_cmd_parser.add_argument('--expect-response-regex', help='match response against regex', type=str, default=None) apdu_cmd_parser.add_argument('--raw', help='Bypass the logical channel (and secure channel)', action='store_true') apdu_cmd_parser.add_argument('APDU', type=is_hexstr, help='APDU as hex string') @@ -264,6 +266,10 @@ Online manual available at https://downloads.osmocom.org/docs/pysim/master/html/ if opts.expect_sw: if not sw_match(sw, opts.expect_sw): raise SwMatchError(sw, opts.expect_sw) + if opts.expect_response_regex: + response_regex_compiled = re.compile(opts.expect_response_regex) + if re.match(response_regex_compiled, data) is None: + raise ValueError("RESP does not match regex \'%s\'" % opts.expect_response_regex) @cmd2.with_category(CUSTOM_CATEGORY) def do_reset(self, opts):