blob: f9ab1151a9bcab1e588172a9e1595c2e15b08bf8 [file] [log] [blame]
Harald Welteb2edd142021-01-08 23:29:35 +01001#!/usr/bin/env python3
2
3# Interactive shell for working with SIM / UICC / USIM / ISIM cards
4#
5# (C) 2021 by Harald Welte <laforge@osmocom.org>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20from typing import List
21
22import json
Philipp Maier5d698e52021-09-16 13:18:01 +020023import traceback
Harald Welteb2edd142021-01-08 23:29:35 +010024
25import cmd2
Vadim Yanitskiy0d9f0882022-07-14 19:08:24 +070026from cmd2 import style, fg
Harald Welteb2edd142021-01-08 23:29:35 +010027from cmd2 import CommandSet, with_default_category, with_argparser
28import argparse
29
30import os
31import sys
Philipp Maier2b11c322021-03-17 12:37:39 +010032from pathlib import Path
Philipp Maier76667642021-09-22 16:53:22 +020033from io import StringIO
Harald Welteb2edd142021-01-08 23:29:35 +010034
Harald Welteb2edd142021-01-08 23:29:35 +010035from pySim.exceptions import *
36from pySim.commands import SimCardCommands
Harald Welte28c24312021-04-11 12:19:36 +020037from pySim.transport import init_reader, ApduTracer, argparse_add_reader_args
Philipp Maierbb73e512021-05-05 16:14:00 +020038from pySim.cards import card_detect, SimCard
Harald Welte1e52b0d2022-07-16 11:53:21 +020039from pySim.utils import h2b, swap_nibbles, rpad, b2h, JsonEncoder, bertlv_parse_one, sw_match
40from pySim.utils import sanitize_pin_adm, tabulate_str_list, boxed_heading_str
Philipp Maier76667642021-09-22 16:53:22 +020041from pySim.card_handler import CardHandler, CardHandlerAuto
Harald Welteb2edd142021-01-08 23:29:35 +010042
Harald Welte1e52b0d2022-07-16 11:53:21 +020043from pySim.filesystem import RuntimeState, CardDF, CardADF, CardModel
Philipp Maiera028c7d2021-11-08 16:12:03 +010044from pySim.profile import CardProfile
Harald Welteb2edd142021-01-08 23:29:35 +010045from pySim.ts_102_221 import CardProfileUICC
Harald Welte3c9b7842021-10-19 21:44:24 +020046from pySim.ts_102_222 import Ts102222Commands
Harald Welte5ce35242021-04-02 20:27:05 +020047from pySim.ts_31_102 import CardApplicationUSIM
48from pySim.ts_31_103 import CardApplicationISIM
Harald Welte95ce6b12021-10-20 18:40:54 +020049from pySim.ara_m import CardApplicationARAM
Harald Welte34eb5042022-02-21 17:19:28 +010050from pySim.global_platform import CardApplicationISD
Harald Welte2a33ad22021-10-20 10:14:18 +020051from pySim.gsm_r import DF_EIRENE
Harald Welteb2edd142021-01-08 23:29:35 +010052
Harald Welte4c1dca02021-10-14 17:48:25 +020053# we need to import this module so that the SysmocomSJA2 sub-class of
54# CardModel is created, which will add the ATR-based matching and
55# calling of SysmocomSJA2.add_files. See CardModel.apply_matching_models
Harald Weltef44256c2021-10-14 15:53:39 +020056import pySim.sysmocom_sja2
57
Harald Welte4442b3d2021-04-03 09:00:16 +020058from pySim.card_key_provider import CardKeyProviderCsv, card_key_provider_register, card_key_provider_get_field
Philipp Maier2b11c322021-03-17 12:37:39 +010059
Harald Weltec91085e2022-02-10 18:05:45 +010060
Philipp Maierea95c392021-09-16 13:10:19 +020061def init_card(sl):
Harald Weltec91085e2022-02-10 18:05:45 +010062 """
63 Detect card in reader and setup card profile and runtime state. This
64 function must be called at least once on startup. The card and runtime
65 state object (rs) is required for all pySim-shell commands.
66 """
Philipp Maierea95c392021-09-16 13:10:19 +020067
Harald Weltec91085e2022-02-10 18:05:45 +010068 # Wait up to three seconds for a card in reader and try to detect
69 # the card type.
70 print("Waiting for card...")
71 try:
72 sl.wait_for_card(3)
73 except NoCardError:
74 print("No card detected!")
75 return None, None
76 except:
77 print("Card not readable!")
78 return None, None
Philipp Maierea95c392021-09-16 13:10:19 +020079
Philipp Maier24031252022-06-14 16:16:42 +020080 generic_card = False
Harald Weltec91085e2022-02-10 18:05:45 +010081 card = card_detect("auto", scc)
82 if card is None:
83 print("Warning: Could not detect card type - assuming a generic card type...")
84 card = SimCard(scc)
Philipp Maier24031252022-06-14 16:16:42 +020085 generic_card = True
Philipp Maiera028c7d2021-11-08 16:12:03 +010086
Harald Weltec91085e2022-02-10 18:05:45 +010087 profile = CardProfile.pick(scc)
88 if profile is None:
89 print("Unsupported card type!")
Philipp Maier7226c092022-06-01 17:58:38 +020090 return None, card
Philipp Maierea95c392021-09-16 13:10:19 +020091
Philipp Maier24031252022-06-14 16:16:42 +020092 # ETSI TS 102 221, Table 9.3 specifies a default for the PIN key
93 # references, however card manufactures may still decide to pick an
94 # arbitrary key reference. In case we run on a generic card class that is
95 # detected as an UICC, we will pick the key reference that is officially
96 # specified.
97 if generic_card and isinstance(profile, CardProfileUICC):
98 card._adm_chv_num = 0x0A
99
Harald Weltec91085e2022-02-10 18:05:45 +0100100 print("Info: Card is of type: %s" % str(profile))
Philipp Maiera028c7d2021-11-08 16:12:03 +0100101
Harald Weltec91085e2022-02-10 18:05:45 +0100102 # FIXME: This shouln't be here, the profile should add the applications,
103 # however, we cannot simply put his into ts_102_221.py since we would
104 # have to e.g. import CardApplicationUSIM from ts_31_102.py, which already
105 # imports from ts_102_221.py. This means we will end up with a circular
106 # import, which needs to be resolved first.
107 if isinstance(profile, CardProfileUICC):
108 profile.add_application(CardApplicationUSIM())
109 profile.add_application(CardApplicationISIM())
110 profile.add_application(CardApplicationARAM())
Harald Welte34eb5042022-02-21 17:19:28 +0100111 profile.add_application(CardApplicationISD())
Philipp Maiera028c7d2021-11-08 16:12:03 +0100112
Harald Weltec91085e2022-02-10 18:05:45 +0100113 # Create runtime state with card profile
114 rs = RuntimeState(card, profile)
Philipp Maierea95c392021-09-16 13:10:19 +0200115
Harald Weltec91085e2022-02-10 18:05:45 +0100116 # FIXME: This is an GSM-R related file, it needs to be added throughout,
117 # the profile. At the moment we add it for all cards, this won't hurt,
118 # but regular SIM and UICC will not have it and fail to select it.
119 rs.mf.add_file(DF_EIRENE())
Philipp Maierea95c392021-09-16 13:10:19 +0200120
Harald Weltec91085e2022-02-10 18:05:45 +0100121 CardModel.apply_matching_models(scc, rs)
Harald Weltef44256c2021-10-14 15:53:39 +0200122
Harald Weltec91085e2022-02-10 18:05:45 +0100123 # inform the transport that we can do context-specific SW interpretation
124 sl.set_sw_interpreter(rs)
Philipp Maierea95c392021-09-16 13:10:19 +0200125
Harald Weltec91085e2022-02-10 18:05:45 +0100126 return rs, card
127
Philipp Maier2b11c322021-03-17 12:37:39 +0100128
Harald Welteb2edd142021-01-08 23:29:35 +0100129class PysimApp(cmd2.Cmd):
Harald Weltec91085e2022-02-10 18:05:45 +0100130 CUSTOM_CATEGORY = 'pySim Commands'
Philipp Maier76667642021-09-22 16:53:22 +0200131
Harald Weltec91085e2022-02-10 18:05:45 +0100132 def __init__(self, card, rs, sl, ch, script=None):
133 super().__init__(persistent_history_file='~/.pysim_shell_history', allow_cli_args=False,
134 use_ipython=True, auto_load_commands=False, startup_script=script)
135 self.intro = style('Welcome to pySim-shell!', fg=fg.red)
136 self.default_category = 'pySim-shell built-in commands'
137 self.card = None
138 self.rs = None
Harald Weltea6c0f882022-07-17 14:23:17 +0200139 self.lchan = None
140 self.py_locals = {'card': self.card, 'rs': self.rs, 'lchan': self.lchan}
Harald Weltec91085e2022-02-10 18:05:45 +0100141 self.sl = sl
142 self.ch = ch
Harald Welte1748b932021-04-06 21:12:25 +0200143
Harald Weltec91085e2022-02-10 18:05:45 +0100144 self.numeric_path = False
145 self.add_settable(cmd2.Settable('numeric_path', bool, 'Print File IDs instead of names',
146 onchange_cb=self._onchange_numeric_path))
147 self.conserve_write = True
148 self.add_settable(cmd2.Settable('conserve_write', bool, 'Read and compare before write',
149 onchange_cb=self._onchange_conserve_write))
150 self.json_pretty_print = True
151 self.add_settable(cmd2.Settable('json_pretty_print',
152 bool, 'Pretty-Print JSON output'))
153 self.apdu_trace = False
154 self.add_settable(cmd2.Settable('apdu_trace', bool, 'Trace and display APDUs exchanged with card',
155 onchange_cb=self._onchange_apdu_trace))
Philipp Maier5d698e52021-09-16 13:18:01 +0200156
Harald Weltec91085e2022-02-10 18:05:45 +0100157 self.equip(card, rs)
Philipp Maier5d698e52021-09-16 13:18:01 +0200158
Harald Weltec91085e2022-02-10 18:05:45 +0100159 def equip(self, card, rs):
160 """
161 Equip pySim-shell with the supplied card and runtime state, add (or remove) all required settables and
162 and commands to enable card operations.
163 """
Philipp Maier76667642021-09-22 16:53:22 +0200164
Harald Weltec91085e2022-02-10 18:05:45 +0100165 rc = False
Philipp Maier5d698e52021-09-16 13:18:01 +0200166
Harald Weltec91085e2022-02-10 18:05:45 +0100167 # Unequip everything from pySim-shell that would not work in unequipped state
168 if self.rs:
Harald Weltea6c0f882022-07-17 14:23:17 +0200169 lchan = self.rs.lchan[0]
170 lchan.unregister_cmds(self)
Harald Weltec91085e2022-02-10 18:05:45 +0100171 for cmds in [Iso7816Commands, PySimCommands]:
172 cmd_set = self.find_commandsets(cmds)
173 if cmd_set:
174 self.unregister_command_set(cmd_set[0])
Philipp Maier5d698e52021-09-16 13:18:01 +0200175
Harald Weltec91085e2022-02-10 18:05:45 +0100176 self.card = card
177 self.rs = rs
Philipp Maier5d698e52021-09-16 13:18:01 +0200178
Harald Weltec91085e2022-02-10 18:05:45 +0100179 # When a card object and a runtime state is present, (re)equip pySim-shell with everything that is
180 # needed to operate on cards.
181 if self.card and self.rs:
Harald Weltea6c0f882022-07-17 14:23:17 +0200182 self.lchan = self.rs.lchan[0]
Harald Weltec91085e2022-02-10 18:05:45 +0100183 self._onchange_conserve_write(
184 'conserve_write', False, self.conserve_write)
185 self._onchange_apdu_trace('apdu_trace', False, self.apdu_trace)
186 self.register_command_set(Iso7816Commands())
Harald Welte3c9b7842021-10-19 21:44:24 +0200187 self.register_command_set(Ts102222Commands())
Harald Weltec91085e2022-02-10 18:05:45 +0100188 self.register_command_set(PySimCommands())
189 self.iccid, sw = self.card.read_iccid()
Harald Weltea6c0f882022-07-17 14:23:17 +0200190 self.lchan.select('MF', self)
Harald Weltec91085e2022-02-10 18:05:45 +0100191 rc = True
192 else:
193 self.poutput("pySim-shell not equipped!")
Philipp Maier5d698e52021-09-16 13:18:01 +0200194
Harald Weltec91085e2022-02-10 18:05:45 +0100195 self.update_prompt()
196 return rc
Harald Welteb2edd142021-01-08 23:29:35 +0100197
Harald Weltec91085e2022-02-10 18:05:45 +0100198 def poutput_json(self, data, force_no_pretty=False):
199 """like cmd2.poutput() but for a JSON serializable dict."""
200 if force_no_pretty or self.json_pretty_print == False:
201 output = json.dumps(data, cls=JsonEncoder)
202 else:
203 output = json.dumps(data, cls=JsonEncoder, indent=4)
204 self.poutput(output)
Harald Welteb2edd142021-01-08 23:29:35 +0100205
Harald Weltec91085e2022-02-10 18:05:45 +0100206 def _onchange_numeric_path(self, param_name, old, new):
207 self.update_prompt()
Philipp Maier38c74f62021-03-17 17:19:52 +0100208
Harald Weltec91085e2022-02-10 18:05:45 +0100209 def _onchange_conserve_write(self, param_name, old, new):
210 if self.rs:
211 self.rs.conserve_write = new
Harald Welte7829d8a2021-04-10 11:28:53 +0200212
Harald Weltec91085e2022-02-10 18:05:45 +0100213 def _onchange_apdu_trace(self, param_name, old, new):
214 if self.card:
215 if new == True:
216 self.card._scc._tp.apdu_tracer = self.Cmd2ApduTracer(self)
217 else:
218 self.card._scc._tp.apdu_tracer = None
Harald Welte7829d8a2021-04-10 11:28:53 +0200219
Harald Weltec91085e2022-02-10 18:05:45 +0100220 class Cmd2ApduTracer(ApduTracer):
221 def __init__(self, cmd2_app):
222 self.cmd2 = app
Harald Welte7829d8a2021-04-10 11:28:53 +0200223
Harald Weltec91085e2022-02-10 18:05:45 +0100224 def trace_response(self, cmd, sw, resp):
225 self.cmd2.poutput("-> %s %s" % (cmd[:10], cmd[10:]))
226 self.cmd2.poutput("<- %s: %s" % (sw, resp))
Harald Welteb2edd142021-01-08 23:29:35 +0100227
Harald Weltec91085e2022-02-10 18:05:45 +0100228 def update_prompt(self):
Harald Weltea6c0f882022-07-17 14:23:17 +0200229 if self.lchan:
Harald Welteb2e4b4a2022-07-19 23:48:45 +0200230 path_str = self.lchan.selected_file.fully_qualified_path_str(not self.numeric_path)
231 self.prompt = 'pySIM-shell (%s)> ' % (path_str)
Harald Weltec91085e2022-02-10 18:05:45 +0100232 else:
Philipp Maier7226c092022-06-01 17:58:38 +0200233 if self.card:
234 self.prompt = 'pySIM-shell (no card profile)> '
235 else:
236 self.prompt = 'pySIM-shell (no card)> '
Harald Welteb2edd142021-01-08 23:29:35 +0100237
Harald Weltec91085e2022-02-10 18:05:45 +0100238 @cmd2.with_category(CUSTOM_CATEGORY)
239 def do_intro(self, _):
240 """Display the intro banner"""
241 self.poutput(self.intro)
Philipp Maier9764de22021-11-03 10:44:39 +0100242
Harald Weltec91085e2022-02-10 18:05:45 +0100243 def do_eof(self, _: argparse.Namespace) -> bool:
244 self.poutput("")
245 return self.do_quit('')
Philipp Maier5d698e52021-09-16 13:18:01 +0200246
Harald Weltec91085e2022-02-10 18:05:45 +0100247 @cmd2.with_category(CUSTOM_CATEGORY)
248 def do_equip(self, opts):
249 """Equip pySim-shell with card"""
250 rs, card = init_card(sl)
251 self.equip(card, rs)
Philipp Maier76667642021-09-22 16:53:22 +0200252
Philipp Maier7226c092022-06-01 17:58:38 +0200253 apdu_cmd_parser = argparse.ArgumentParser()
254 apdu_cmd_parser.add_argument('APDU', type=str, help='APDU as hex string')
Philipp Maiere7d1b672022-06-01 18:05:34 +0200255 apdu_cmd_parser.add_argument('--expect-sw', help='expect a specified status word', type=str, default=None)
Philipp Maier7226c092022-06-01 17:58:38 +0200256
257 @cmd2.with_argparser(apdu_cmd_parser)
258 def do_apdu(self, opts):
259 """Send a raw APDU to the card, and print SW + Response.
260 DANGEROUS: pySim-shell will not know any card state changes, and
261 not continue to work as expected if you e.g. select a different
262 file."""
263 data, sw = self.card._scc._tp.send_apdu(opts.APDU)
264 if data:
265 self.poutput("SW: %s, RESP: %s" % (sw, data))
266 else:
267 self.poutput("SW: %s" % sw)
Philipp Maiere7d1b672022-06-01 18:05:34 +0200268 if opts.expect_sw:
269 if not sw_match(sw, opts.expect_sw):
270 raise SwMatchError(sw, opts.expect_sw)
Philipp Maier7226c092022-06-01 17:58:38 +0200271
Harald Weltec91085e2022-02-10 18:05:45 +0100272 class InterceptStderr(list):
273 def __init__(self):
274 self._stderr_backup = sys.stderr
Philipp Maier76667642021-09-22 16:53:22 +0200275
Harald Weltec91085e2022-02-10 18:05:45 +0100276 def __enter__(self):
277 self._stringio_stderr = StringIO()
278 sys.stderr = self._stringio_stderr
279 return self
Philipp Maier76667642021-09-22 16:53:22 +0200280
Harald Weltec91085e2022-02-10 18:05:45 +0100281 def __exit__(self, *args):
282 self.stderr = self._stringio_stderr.getvalue().strip()
283 del self._stringio_stderr
284 sys.stderr = self._stderr_backup
Philipp Maier76667642021-09-22 16:53:22 +0200285
Harald Weltec91085e2022-02-10 18:05:45 +0100286 def _show_failure_sign(self):
287 self.poutput(style(" +-------------+", fg=fg.bright_red))
288 self.poutput(style(" + ## ## +", fg=fg.bright_red))
289 self.poutput(style(" + ## ## +", fg=fg.bright_red))
290 self.poutput(style(" + ### +", fg=fg.bright_red))
291 self.poutput(style(" + ## ## +", fg=fg.bright_red))
292 self.poutput(style(" + ## ## +", fg=fg.bright_red))
293 self.poutput(style(" +-------------+", fg=fg.bright_red))
294 self.poutput("")
Philipp Maier76667642021-09-22 16:53:22 +0200295
Harald Weltec91085e2022-02-10 18:05:45 +0100296 def _show_success_sign(self):
297 self.poutput(style(" +-------------+", fg=fg.bright_green))
298 self.poutput(style(" + ## +", fg=fg.bright_green))
299 self.poutput(style(" + ## +", fg=fg.bright_green))
300 self.poutput(style(" + # ## +", fg=fg.bright_green))
301 self.poutput(style(" + ## # +", fg=fg.bright_green))
302 self.poutput(style(" + ## +", fg=fg.bright_green))
303 self.poutput(style(" +-------------+", fg=fg.bright_green))
304 self.poutput("")
Philipp Maier76667642021-09-22 16:53:22 +0200305
Harald Weltec91085e2022-02-10 18:05:45 +0100306 def _process_card(self, first, script_path):
Philipp Maier76667642021-09-22 16:53:22 +0200307
Harald Weltec91085e2022-02-10 18:05:45 +0100308 # Early phase of card initialzation (this part may fail with an exception)
309 try:
310 rs, card = init_card(self.sl)
311 rc = self.equip(card, rs)
312 except:
313 self.poutput("")
314 self.poutput("Card initialization failed with an exception:")
315 self.poutput("---------------------8<---------------------")
316 traceback.print_exc()
317 self.poutput("---------------------8<---------------------")
318 self.poutput("")
319 return -1
Philipp Maier76667642021-09-22 16:53:22 +0200320
Harald Weltec91085e2022-02-10 18:05:45 +0100321 # Actual card processing step. This part should never fail with an exception since the cmd2
322 # do_run_script method will catch any exception that might occur during script execution.
323 if rc:
324 self.poutput("")
325 self.poutput("Transcript stdout:")
326 self.poutput("---------------------8<---------------------")
327 with self.InterceptStderr() as logged:
328 self.do_run_script(script_path)
329 self.poutput("---------------------8<---------------------")
Philipp Maier76667642021-09-22 16:53:22 +0200330
Harald Weltec91085e2022-02-10 18:05:45 +0100331 self.poutput("")
332 self.poutput("Transcript stderr:")
333 if logged.stderr:
334 self.poutput("---------------------8<---------------------")
335 self.poutput(logged.stderr)
336 self.poutput("---------------------8<---------------------")
337 else:
338 self.poutput("(none)")
Philipp Maier76667642021-09-22 16:53:22 +0200339
Harald Weltec91085e2022-02-10 18:05:45 +0100340 # Check for exceptions
341 self.poutput("")
342 if "EXCEPTION of type" not in logged.stderr:
343 return 0
Philipp Maier76667642021-09-22 16:53:22 +0200344
Harald Weltec91085e2022-02-10 18:05:45 +0100345 return -1
Philipp Maier76667642021-09-22 16:53:22 +0200346
Harald Weltec91085e2022-02-10 18:05:45 +0100347 bulk_script_parser = argparse.ArgumentParser()
348 bulk_script_parser.add_argument(
349 'script_path', help="path to the script file")
350 bulk_script_parser.add_argument('--halt_on_error', help='stop card handling if an exeption occurs',
351 action='store_true')
352 bulk_script_parser.add_argument('--tries', type=int, default=2,
353 help='how many tries before trying the next card')
354 bulk_script_parser.add_argument('--on_stop_action', type=str, default=None,
355 help='commandline to execute when card handling has stopped')
356 bulk_script_parser.add_argument('--pre_card_action', type=str, default=None,
357 help='commandline to execute before actually talking to the card')
Philipp Maier76667642021-09-22 16:53:22 +0200358
Harald Weltec91085e2022-02-10 18:05:45 +0100359 @cmd2.with_argparser(bulk_script_parser)
360 @cmd2.with_category(CUSTOM_CATEGORY)
361 def do_bulk_script(self, opts):
362 """Run script on multiple cards (bulk provisioning)"""
Philipp Maier76667642021-09-22 16:53:22 +0200363
Harald Weltec91085e2022-02-10 18:05:45 +0100364 # Make sure that the script file exists and that it is readable.
365 if not os.access(opts.script_path, os.R_OK):
366 self.poutput("Invalid script file!")
367 return
Philipp Maier76667642021-09-22 16:53:22 +0200368
Harald Weltec91085e2022-02-10 18:05:45 +0100369 success_count = 0
370 fail_count = 0
Philipp Maier76667642021-09-22 16:53:22 +0200371
Harald Weltec91085e2022-02-10 18:05:45 +0100372 first = True
373 while 1:
374 # TODO: Count consecutive failures, if more than N consecutive failures occur, then stop.
375 # The ratinale is: There may be a problem with the device, we do want to prevent that
376 # all remaining cards are fired to the error bin. This is only relevant for situations
377 # with large stacks, probably we do not need this feature right now.
Philipp Maier76667642021-09-22 16:53:22 +0200378
Harald Weltec91085e2022-02-10 18:05:45 +0100379 try:
380 # In case of failure, try multiple times.
381 for i in range(opts.tries):
382 # fetch card into reader bay
383 ch.get(first)
Philipp Maier76667642021-09-22 16:53:22 +0200384
Harald Weltec91085e2022-02-10 18:05:45 +0100385 # if necessary execute an action before we start processing the card
386 if(opts.pre_card_action):
387 os.system(opts.pre_card_action)
Philipp Maier76667642021-09-22 16:53:22 +0200388
Harald Weltec91085e2022-02-10 18:05:45 +0100389 # process the card
390 rc = self._process_card(first, opts.script_path)
391 if rc == 0:
392 success_count = success_count + 1
393 self._show_success_sign()
394 self.poutput("Statistics: success :%i, failure: %i" % (
395 success_count, fail_count))
396 break
397 else:
398 fail_count = fail_count + 1
399 self._show_failure_sign()
400 self.poutput("Statistics: success :%i, failure: %i" % (
401 success_count, fail_count))
Philipp Maier76667642021-09-22 16:53:22 +0200402
Harald Weltec91085e2022-02-10 18:05:45 +0100403 # Depending on success or failure, the card goes either in the "error" bin or in the
404 # "done" bin.
405 if rc < 0:
406 ch.error()
407 else:
408 ch.done()
Philipp Maier76667642021-09-22 16:53:22 +0200409
Harald Weltec91085e2022-02-10 18:05:45 +0100410 # In most cases it is possible to proceed with the next card, but the
411 # user may decide to halt immediately when an error occurs
412 if opts.halt_on_error and rc < 0:
413 return
Philipp Maier76667642021-09-22 16:53:22 +0200414
Harald Weltec91085e2022-02-10 18:05:45 +0100415 except (KeyboardInterrupt):
416 self.poutput("")
417 self.poutput("Terminated by user!")
418 return
419 except (SystemExit):
420 # When all cards are processed the card handler device will throw a SystemExit
421 # exception. Also Errors that are not recoverable (cards stuck etc.) will end up here.
422 # The user has the option to execute some action to make aware that the card handler
423 # needs service.
424 if(opts.on_stop_action):
425 os.system(opts.on_stop_action)
426 return
427 except:
428 self.poutput("")
429 self.poutput("Card handling failed with an exception:")
430 self.poutput("---------------------8<---------------------")
431 traceback.print_exc()
432 self.poutput("---------------------8<---------------------")
433 self.poutput("")
434 fail_count = fail_count + 1
435 self._show_failure_sign()
436 self.poutput("Statistics: success :%i, failure: %i" %
437 (success_count, fail_count))
Philipp Maierb52feed2021-09-22 16:43:13 +0200438
Harald Weltec91085e2022-02-10 18:05:45 +0100439 first = False
440
441 echo_parser = argparse.ArgumentParser()
442 echo_parser.add_argument('string', help="string to echo on the shell")
443
444 @cmd2.with_argparser(echo_parser)
445 @cmd2.with_category(CUSTOM_CATEGORY)
446 def do_echo(self, opts):
447 """Echo (print) a string on the console"""
448 self.poutput(opts.string)
449
Harald Welteb2edd142021-01-08 23:29:35 +0100450
Harald Welte31d2cf02021-04-03 10:47:29 +0200451@with_default_category('pySim Commands')
452class PySimCommands(CommandSet):
Harald Weltec91085e2022-02-10 18:05:45 +0100453 def __init__(self):
454 super().__init__()
Harald Welteb2edd142021-01-08 23:29:35 +0100455
Harald Weltec91085e2022-02-10 18:05:45 +0100456 dir_parser = argparse.ArgumentParser()
457 dir_parser.add_argument(
458 '--fids', help='Show file identifiers', action='store_true')
459 dir_parser.add_argument(
460 '--names', help='Show file names', action='store_true')
461 dir_parser.add_argument(
462 '--apps', help='Show applications', action='store_true')
463 dir_parser.add_argument(
464 '--all', help='Show all selectable identifiers and names', action='store_true')
Philipp Maier5d3e2592021-02-22 17:22:16 +0100465
Harald Weltec91085e2022-02-10 18:05:45 +0100466 @cmd2.with_argparser(dir_parser)
467 def do_dir(self, opts):
468 """Show a listing of files available in currently selected DF or MF"""
469 if opts.all:
470 flags = []
471 elif opts.fids or opts.names or opts.apps:
472 flags = ['PARENT', 'SELF']
473 if opts.fids:
474 flags += ['FIDS', 'AIDS']
475 if opts.names:
476 flags += ['FNAMES', 'ANAMES']
477 if opts.apps:
478 flags += ['ANAMES', 'AIDS']
479 else:
480 flags = ['PARENT', 'SELF', 'FNAMES', 'ANAMES']
481 selectables = list(
Harald Weltea6c0f882022-07-17 14:23:17 +0200482 self._cmd.lchan.selected_file.get_selectable_names(flags=flags))
Harald Weltec91085e2022-02-10 18:05:45 +0100483 directory_str = tabulate_str_list(
484 selectables, width=79, hspace=2, lspace=1, align_left=True)
Harald Welteb2e4b4a2022-07-19 23:48:45 +0200485 path = self._cmd.lchan.selected_file.fully_qualified_path_str(True)
486 self._cmd.poutput(path)
487 path = self._cmd.lchan.selected_file.fully_qualified_path_str(False)
488 self._cmd.poutput(path)
Harald Weltec91085e2022-02-10 18:05:45 +0100489 self._cmd.poutput(directory_str)
490 self._cmd.poutput("%d files" % len(selectables))
Harald Welteb2edd142021-01-08 23:29:35 +0100491
Philipp Maier7b138b02022-05-31 13:42:56 +0200492 def walk(self, indent=0, action_ef=None, action_df=None, context=None, **kwargs):
Harald Weltec91085e2022-02-10 18:05:45 +0100493 """Recursively walk through the file system, starting at the currently selected DF"""
Philipp Maier7b138b02022-05-31 13:42:56 +0200494
Harald Weltea6c0f882022-07-17 14:23:17 +0200495 if isinstance(self._cmd.lchan.selected_file, CardDF):
Philipp Maier7b138b02022-05-31 13:42:56 +0200496 if action_df:
497 action_df(context, opts)
498
Harald Weltea6c0f882022-07-17 14:23:17 +0200499 files = self._cmd.lchan.selected_file.get_selectables(
Harald Weltec91085e2022-02-10 18:05:45 +0100500 flags=['FNAMES', 'ANAMES'])
501 for f in files:
Philipp Maier7b138b02022-05-31 13:42:56 +0200502 # special case: When no action is performed, just output a directory
503 if not action_ef and not action_df:
Harald Weltec91085e2022-02-10 18:05:45 +0100504 output_str = " " * indent + str(f) + (" " * 250)
505 output_str = output_str[0:25]
506 if isinstance(files[f], CardADF):
507 output_str += " " + str(files[f].aid)
508 else:
509 output_str += " " + str(files[f].fid)
510 output_str += " " + str(files[f].desc)
511 self._cmd.poutput(output_str)
Philipp Maierf408a402021-04-09 21:16:12 +0200512
Harald Weltec91085e2022-02-10 18:05:45 +0100513 if isinstance(files[f], CardDF):
514 skip_df = False
515 try:
Harald Weltea6c0f882022-07-17 14:23:17 +0200516 fcp_dec = self._cmd.lchan.select(f, self._cmd)
Harald Weltec91085e2022-02-10 18:05:45 +0100517 except Exception as e:
518 skip_df = True
Harald Weltea6c0f882022-07-17 14:23:17 +0200519 df = self._cmd.lchan.selected_file
Harald Welteb2e4b4a2022-07-19 23:48:45 +0200520 df_path = df.fully_qualified_path_str(True)
521 df_skip_reason_str = df_path + \
Harald Weltec91085e2022-02-10 18:05:45 +0100522 "/" + str(f) + ", " + str(e)
523 if context:
524 context['DF_SKIP'] += 1
525 context['DF_SKIP_REASON'].append(df_skip_reason_str)
Philipp Maierf408a402021-04-09 21:16:12 +0200526
Harald Weltec91085e2022-02-10 18:05:45 +0100527 # If the DF was skipped, we never have entered the directory
528 # below, so we must not move up.
529 if skip_df == False:
Philipp Maier7b138b02022-05-31 13:42:56 +0200530 self.walk(indent + 1, action_ef, action_df, context, **kwargs)
Harald Weltea6c0f882022-07-17 14:23:17 +0200531 fcp_dec = self._cmd.lchan.select("..", self._cmd)
Philipp Maierf408a402021-04-09 21:16:12 +0200532
Philipp Maier7b138b02022-05-31 13:42:56 +0200533 elif action_ef:
Harald Weltea6c0f882022-07-17 14:23:17 +0200534 df_before_action = self._cmd.lchan.selected_file
Philipp Maier7b138b02022-05-31 13:42:56 +0200535 action_ef(f, context, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100536 # When walking through the file system tree the action must not
537 # always restore the currently selected file to the file that
538 # was selected before executing the action() callback.
Harald Weltea6c0f882022-07-17 14:23:17 +0200539 if df_before_action != self._cmd.lchan.selected_file:
Harald Weltec91085e2022-02-10 18:05:45 +0100540 raise RuntimeError("inconsistent walk, %s is currently selected but expecting %s to be selected"
Harald Weltea6c0f882022-07-17 14:23:17 +0200541 % (str(self._cmd.lchan.selected_file), str(df_before_action)))
Philipp Maierff9dae22021-02-25 17:03:21 +0100542
Harald Weltec91085e2022-02-10 18:05:45 +0100543 def do_tree(self, opts):
544 """Display a filesystem-tree with all selectable files"""
545 self.walk()
Philipp Maierff9dae22021-02-25 17:03:21 +0100546
Philipp Maier7b138b02022-05-31 13:42:56 +0200547 def export_ef(self, filename, context, as_json):
548 """ Select and export a single elementary file (EF) """
Harald Weltec91085e2022-02-10 18:05:45 +0100549 context['COUNT'] += 1
Harald Weltea6c0f882022-07-17 14:23:17 +0200550 df = self._cmd.lchan.selected_file
Philipp Maierac34dcc2021-04-01 17:19:05 +0200551
Philipp Maierea81f752022-05-19 10:13:30 +0200552 # The currently selected file (not the file we are going to export)
553 # must always be an ADF or DF. From this starting point we select
554 # the EF we want to export. To maintain consistency we will then
555 # select the current DF again (see comment below).
Harald Weltec91085e2022-02-10 18:05:45 +0100556 if not isinstance(df, CardDF):
557 raise RuntimeError(
558 "currently selected file %s is not a DF or ADF" % str(df))
Philipp Maierac34dcc2021-04-01 17:19:05 +0200559
Harald Weltec91085e2022-02-10 18:05:45 +0100560 df_path_list = df.fully_qualified_path(True)
Harald Welteb2e4b4a2022-07-19 23:48:45 +0200561 df_path = df.fully_qualified_path_str(True)
562 df_path_fid = df.fully_qualified_path_str(False)
Philipp Maier24f7bd32021-02-25 17:06:18 +0100563
Harald Welteb2e4b4a2022-07-19 23:48:45 +0200564 file_str = df_path + "/" + str(filename)
Harald Weltec91085e2022-02-10 18:05:45 +0100565 self._cmd.poutput(boxed_heading_str(file_str))
Philipp Maier24f7bd32021-02-25 17:06:18 +0100566
Harald Welteb2e4b4a2022-07-19 23:48:45 +0200567 self._cmd.poutput("# directory: %s (%s)" % (df_path, df_path_fid))
Harald Weltec91085e2022-02-10 18:05:45 +0100568 try:
Harald Weltea6c0f882022-07-17 14:23:17 +0200569 fcp_dec = self._cmd.lchan.select(filename, self._cmd)
Harald Weltec91085e2022-02-10 18:05:45 +0100570 self._cmd.poutput("# file: %s (%s)" % (
Harald Weltea6c0f882022-07-17 14:23:17 +0200571 self._cmd.lchan.selected_file.name, self._cmd.lchan.selected_file.fid))
Philipp Maier24f7bd32021-02-25 17:06:18 +0100572
Harald Weltea6c0f882022-07-17 14:23:17 +0200573 structure = self._cmd.lchan.selected_file_structure()
Harald Weltec91085e2022-02-10 18:05:45 +0100574 self._cmd.poutput("# structure: %s" % str(structure))
Harald Weltea6c0f882022-07-17 14:23:17 +0200575 self._cmd.poutput("# RAW FCP Template: %s" % str(self._cmd.lchan.selected_file_fcp_hex))
576 self._cmd.poutput("# Decoded FCP Template: %s" % str(self._cmd.lchan.selected_file_fcp))
Philipp Maier24f7bd32021-02-25 17:06:18 +0100577
Harald Weltec91085e2022-02-10 18:05:45 +0100578 for f in df_path_list:
579 self._cmd.poutput("select " + str(f))
Harald Weltea6c0f882022-07-17 14:23:17 +0200580 self._cmd.poutput("select " + self._cmd.lchan.selected_file.name)
Philipp Maier24f7bd32021-02-25 17:06:18 +0100581
Harald Weltec91085e2022-02-10 18:05:45 +0100582 if structure == 'transparent':
Harald Welte08b11ab2022-02-10 18:56:41 +0100583 if as_json:
Harald Weltea6c0f882022-07-17 14:23:17 +0200584 result = self._cmd.lchan.read_binary_dec()
Harald Welte08b11ab2022-02-10 18:56:41 +0100585 self._cmd.poutput("update_binary_decoded '%s'" % json.dumps(result[0], cls=JsonEncoder))
586 else:
Harald Weltea6c0f882022-07-17 14:23:17 +0200587 result = self._cmd.lchan.read_binary()
Harald Welte08b11ab2022-02-10 18:56:41 +0100588 self._cmd.poutput("update_binary " + str(result[0]))
Harald Weltec91085e2022-02-10 18:05:45 +0100589 elif structure == 'cyclic' or structure == 'linear_fixed':
590 # Use number of records specified in select response
Harald Weltea6c0f882022-07-17 14:23:17 +0200591 num_of_rec = self._cmd.lchan.selected_file_num_of_rec()
Harald Welte747a9782022-02-13 17:52:28 +0100592 if num_of_rec:
Harald Weltec91085e2022-02-10 18:05:45 +0100593 for r in range(1, num_of_rec + 1):
Harald Welte08b11ab2022-02-10 18:56:41 +0100594 if as_json:
Harald Weltea6c0f882022-07-17 14:23:17 +0200595 result = self._cmd.lchan.read_record_dec(r)
Harald Welte08b11ab2022-02-10 18:56:41 +0100596 self._cmd.poutput("update_record_decoded %d '%s'" % (r, json.dumps(result[0], cls=JsonEncoder)))
597 else:
Harald Weltea6c0f882022-07-17 14:23:17 +0200598 result = self._cmd.lchan.read_record(r)
Harald Welte08b11ab2022-02-10 18:56:41 +0100599 self._cmd.poutput("update_record %d %s" % (r, str(result[0])))
600
Harald Weltec91085e2022-02-10 18:05:45 +0100601 # When the select response does not return the number of records, read until we hit the
602 # first record that cannot be read.
603 else:
604 r = 1
605 while True:
606 try:
Harald Welte08b11ab2022-02-10 18:56:41 +0100607 if as_json:
Harald Weltea6c0f882022-07-17 14:23:17 +0200608 result = self._cmd.lchan.read_record_dec(r)
Harald Welte08b11ab2022-02-10 18:56:41 +0100609 self._cmd.poutput("update_record_decoded %d '%s'" % (r, json.dumps(result[0], cls=JsonEncoder)))
610 else:
Harald Weltea6c0f882022-07-17 14:23:17 +0200611 result = self._cmd.lchan.read_record(r)
Harald Welte08b11ab2022-02-10 18:56:41 +0100612 self._cmd.poutput("update_record %d %s" % (r, str(result[0])))
Harald Weltec91085e2022-02-10 18:05:45 +0100613 except SwMatchError as e:
614 # We are past the last valid record - stop
615 if e.sw_actual == "9402":
616 break
617 # Some other problem occurred
618 else:
619 raise e
Harald Weltec91085e2022-02-10 18:05:45 +0100620 r = r + 1
621 elif structure == 'ber_tlv':
Harald Weltea6c0f882022-07-17 14:23:17 +0200622 tags = self._cmd.lchan.retrieve_tags()
Harald Weltec91085e2022-02-10 18:05:45 +0100623 for t in tags:
Harald Weltea6c0f882022-07-17 14:23:17 +0200624 result = self._cmd.lchan.retrieve_data(t)
Harald Weltec91085e2022-02-10 18:05:45 +0100625 (tag, l, val, remainer) = bertlv_parse_one(h2b(result[0]))
626 self._cmd.poutput("set_data 0x%02x %s" % (t, b2h(val)))
627 else:
628 raise RuntimeError(
629 'Unsupported structure "%s" of file "%s"' % (structure, filename))
630 except Exception as e:
Harald Welteb2e4b4a2022-07-19 23:48:45 +0200631 bad_file_str = df_path + "/" + str(filename) + ", " + str(e)
Harald Weltec91085e2022-02-10 18:05:45 +0100632 self._cmd.poutput("# bad file: %s" % bad_file_str)
633 context['ERR'] += 1
634 context['BAD'].append(bad_file_str)
Philipp Maier24f7bd32021-02-25 17:06:18 +0100635
Harald Weltec91085e2022-02-10 18:05:45 +0100636 # When reading the file is done, make sure the parent file is
637 # selected again. This will be the usual case, however we need
638 # to check before since we must not select the same DF twice
Harald Weltea6c0f882022-07-17 14:23:17 +0200639 if df != self._cmd.lchan.selected_file:
640 self._cmd.lchan.select(df.fid or df.aid, self._cmd)
Philipp Maierac34dcc2021-04-01 17:19:05 +0200641
Harald Weltec91085e2022-02-10 18:05:45 +0100642 self._cmd.poutput("#")
Philipp Maier24f7bd32021-02-25 17:06:18 +0100643
Harald Weltec91085e2022-02-10 18:05:45 +0100644 export_parser = argparse.ArgumentParser()
645 export_parser.add_argument(
646 '--filename', type=str, default=None, help='only export specific file')
Harald Welte08b11ab2022-02-10 18:56:41 +0100647 export_parser.add_argument(
648 '--json', action='store_true', help='export as JSON (less reliable)')
Philipp Maier24f7bd32021-02-25 17:06:18 +0100649
Harald Weltec91085e2022-02-10 18:05:45 +0100650 @cmd2.with_argparser(export_parser)
651 def do_export(self, opts):
652 """Export files to script that can be imported back later"""
653 context = {'ERR': 0, 'COUNT': 0, 'BAD': [],
654 'DF_SKIP': 0, 'DF_SKIP_REASON': []}
Philipp Maier9a4091d2022-05-19 10:20:30 +0200655 kwargs_export = {'as_json': opts.json}
Philipp Maierf16ac6a2022-05-31 14:08:47 +0200656 exception_str_add = ""
657
Harald Weltec91085e2022-02-10 18:05:45 +0100658 if opts.filename:
Philipp Maier7b138b02022-05-31 13:42:56 +0200659 self.export_ef(opts.filename, context, **kwargs_export)
Harald Weltec91085e2022-02-10 18:05:45 +0100660 else:
Philipp Maierf16ac6a2022-05-31 14:08:47 +0200661 try:
662 self.walk(0, self.export_ef, None, context, **kwargs_export)
663 except Exception as e:
664 print("# Stopping early here due to exception: " + str(e))
665 print("#")
666 exception_str_add = ", also had to stop early due to exception:" + str(e)
Philipp Maier80ce71f2021-04-19 21:24:23 +0200667
Harald Weltec91085e2022-02-10 18:05:45 +0100668 self._cmd.poutput(boxed_heading_str("Export summary"))
Philipp Maier80ce71f2021-04-19 21:24:23 +0200669
Harald Weltec91085e2022-02-10 18:05:45 +0100670 self._cmd.poutput("# total files visited: %u" % context['COUNT'])
671 self._cmd.poutput("# bad files: %u" % context['ERR'])
672 for b in context['BAD']:
673 self._cmd.poutput("# " + b)
Philipp Maierf408a402021-04-09 21:16:12 +0200674
Harald Weltec91085e2022-02-10 18:05:45 +0100675 self._cmd.poutput("# skipped dedicated files(s): %u" %
676 context['DF_SKIP'])
677 for b in context['DF_SKIP_REASON']:
678 self._cmd.poutput("# " + b)
Philipp Maierf408a402021-04-09 21:16:12 +0200679
Harald Weltec91085e2022-02-10 18:05:45 +0100680 if context['ERR'] and context['DF_SKIP']:
Philipp Maierf16ac6a2022-05-31 14:08:47 +0200681 raise RuntimeError("unable to export %i elementary file(s) and %i dedicated file(s)%s" % (
682 context['ERR'], context['DF_SKIP'], exception_str_add))
Harald Weltec91085e2022-02-10 18:05:45 +0100683 elif context['ERR']:
684 raise RuntimeError(
Philipp Maierf16ac6a2022-05-31 14:08:47 +0200685 "unable to export %i elementary file(s)%s" % (context['ERR'], exception_str_add))
Harald Weltec91085e2022-02-10 18:05:45 +0100686 elif context['DF_SKIP']:
687 raise RuntimeError(
Philipp Maierf16ac6a2022-05-31 14:08:47 +0200688 "unable to export %i dedicated files(s)%s" % (context['ERR'], exception_str_add))
Harald Welteb2edd142021-01-08 23:29:35 +0100689
Harald Weltec91085e2022-02-10 18:05:45 +0100690 def do_reset(self, opts):
691 """Reset the Card."""
Harald Weltea6c0f882022-07-17 14:23:17 +0200692 atr = self._cmd.lchan.reset(self._cmd)
Harald Weltec91085e2022-02-10 18:05:45 +0100693 self._cmd.poutput('Card ATR: %s' % atr)
694 self._cmd.update_prompt()
Harald Weltedaf2b392021-05-03 23:17:29 +0200695
Harald Weltec91085e2022-02-10 18:05:45 +0100696 def do_desc(self, opts):
697 """Display human readable file description for the currently selected file"""
Harald Weltea6c0f882022-07-17 14:23:17 +0200698 desc = self._cmd.lchan.selected_file.desc
Harald Weltec91085e2022-02-10 18:05:45 +0100699 if desc:
700 self._cmd.poutput(desc)
701 else:
702 self._cmd.poutput("no description available")
Philipp Maiera8c9ea92021-09-16 12:51:46 +0200703
Harald Weltec91085e2022-02-10 18:05:45 +0100704 def do_verify_adm(self, arg):
705 """VERIFY the ADM1 PIN"""
706 if arg:
707 # use specified ADM-PIN
708 pin_adm = sanitize_pin_adm(arg)
709 else:
710 # try to find an ADM-PIN if none is specified
711 result = card_key_provider_get_field(
712 'ADM1', key='ICCID', value=self._cmd.iccid)
713 pin_adm = sanitize_pin_adm(result)
714 if pin_adm:
715 self._cmd.poutput(
716 "found ADM-PIN '%s' for ICCID '%s'" % (result, self._cmd.iccid))
717 else:
718 raise ValueError(
719 "cannot find ADM-PIN for ICCID '%s'" % (self._cmd.iccid))
Philipp Maiera8c9ea92021-09-16 12:51:46 +0200720
Harald Weltec91085e2022-02-10 18:05:45 +0100721 if pin_adm:
722 self._cmd.card.verify_adm(h2b(pin_adm))
723 else:
724 raise ValueError("error: cannot authenticate, no adm-pin!")
725
Harald Welteb2edd142021-01-08 23:29:35 +0100726
Harald Welte31d2cf02021-04-03 10:47:29 +0200727@with_default_category('ISO7816 Commands')
728class Iso7816Commands(CommandSet):
Harald Weltec91085e2022-02-10 18:05:45 +0100729 def __init__(self):
730 super().__init__()
Harald Welte31d2cf02021-04-03 10:47:29 +0200731
Harald Weltec91085e2022-02-10 18:05:45 +0100732 def do_select(self, opts):
733 """SELECT a File (ADF/DF/EF)"""
734 if len(opts.arg_list) == 0:
Harald Welteb2e4b4a2022-07-19 23:48:45 +0200735 path = self._cmd.lchan.selected_file.fully_qualified_path_str(True)
736 path_fid = self._cmd.lchan.selected_file.fully_qualified_path_str(False)
737 self._cmd.poutput("currently selected file: %s (%s)" % (path, path_fid))
Harald Weltec91085e2022-02-10 18:05:45 +0100738 return
Harald Welte31d2cf02021-04-03 10:47:29 +0200739
Harald Weltec91085e2022-02-10 18:05:45 +0100740 path = opts.arg_list[0]
Harald Weltea6c0f882022-07-17 14:23:17 +0200741 fcp_dec = self._cmd.lchan.select(path, self._cmd)
Harald Weltec91085e2022-02-10 18:05:45 +0100742 self._cmd.update_prompt()
743 self._cmd.poutput_json(fcp_dec)
Harald Welte31d2cf02021-04-03 10:47:29 +0200744
Harald Weltec91085e2022-02-10 18:05:45 +0100745 def complete_select(self, text, line, begidx, endidx) -> List[str]:
746 """Command Line tab completion for SELECT"""
Harald Weltea6c0f882022-07-17 14:23:17 +0200747 index_dict = {1: self._cmd.lchan.selected_file.get_selectable_names()}
Harald Weltec91085e2022-02-10 18:05:45 +0100748 return self._cmd.index_based_complete(text, line, begidx, endidx, index_dict=index_dict)
Harald Welte31d2cf02021-04-03 10:47:29 +0200749
Harald Weltec91085e2022-02-10 18:05:45 +0100750 def get_code(self, code):
751 """Use code either directly or try to get it from external data source"""
752 auto = ('PIN1', 'PIN2', 'PUK1', 'PUK2')
Harald Welte31d2cf02021-04-03 10:47:29 +0200753
Harald Weltec91085e2022-02-10 18:05:45 +0100754 if str(code).upper() not in auto:
755 return sanitize_pin_adm(code)
Harald Welte31d2cf02021-04-03 10:47:29 +0200756
Harald Weltec91085e2022-02-10 18:05:45 +0100757 result = card_key_provider_get_field(
758 str(code), key='ICCID', value=self._cmd.iccid)
759 result = sanitize_pin_adm(result)
760 if result:
761 self._cmd.poutput("found %s '%s' for ICCID '%s'" %
762 (code.upper(), result, self._cmd.iccid))
763 else:
764 self._cmd.poutput("cannot find %s for ICCID '%s'" %
765 (code.upper(), self._cmd.iccid))
766 return result
Harald Welte31d2cf02021-04-03 10:47:29 +0200767
Harald Weltec91085e2022-02-10 18:05:45 +0100768 verify_chv_parser = argparse.ArgumentParser()
769 verify_chv_parser.add_argument(
770 '--pin-nr', type=int, default=1, help='PIN Number, 1=PIN1, 2=PIN2 or custom value (decimal)')
771 verify_chv_parser.add_argument(
772 'pin_code', type=str, help='PIN code digits, \"PIN1\" or \"PIN2\" to get PIN code from external data source')
Harald Welte31d2cf02021-04-03 10:47:29 +0200773
Harald Weltec91085e2022-02-10 18:05:45 +0100774 @cmd2.with_argparser(verify_chv_parser)
775 def do_verify_chv(self, opts):
Harald Welte12af7932022-02-15 16:39:08 +0100776 """Verify (authenticate) using specified CHV (PIN) code, which is how the specifications
777 call it if you authenticate yourself using the specified PIN. There usually is at least PIN1 and
778 PIN2."""
Harald Weltec91085e2022-02-10 18:05:45 +0100779 pin = self.get_code(opts.pin_code)
780 (data, sw) = self._cmd.card._scc.verify_chv(opts.pin_nr, h2b(pin))
781 self._cmd.poutput("CHV verification successful")
Harald Welte31d2cf02021-04-03 10:47:29 +0200782
Harald Weltec91085e2022-02-10 18:05:45 +0100783 unblock_chv_parser = argparse.ArgumentParser()
784 unblock_chv_parser.add_argument(
785 '--pin-nr', type=int, default=1, help='PUK Number, 1=PIN1, 2=PIN2 or custom value (decimal)')
786 unblock_chv_parser.add_argument(
787 'puk_code', type=str, help='PUK code digits \"PUK1\" or \"PUK2\" to get PUK code from external data source')
788 unblock_chv_parser.add_argument(
789 'new_pin_code', type=str, help='PIN code digits \"PIN1\" or \"PIN2\" to get PIN code from external data source')
Harald Welte31d2cf02021-04-03 10:47:29 +0200790
Harald Weltec91085e2022-02-10 18:05:45 +0100791 @cmd2.with_argparser(unblock_chv_parser)
792 def do_unblock_chv(self, opts):
793 """Unblock PIN code using specified PUK code"""
794 new_pin = self.get_code(opts.new_pin_code)
795 puk = self.get_code(opts.puk_code)
796 (data, sw) = self._cmd.card._scc.unblock_chv(
797 opts.pin_nr, h2b(puk), h2b(new_pin))
798 self._cmd.poutput("CHV unblock successful")
Harald Welte31d2cf02021-04-03 10:47:29 +0200799
Harald Weltec91085e2022-02-10 18:05:45 +0100800 change_chv_parser = argparse.ArgumentParser()
801 change_chv_parser.add_argument(
802 '--pin-nr', type=int, default=1, help='PUK Number, 1=PIN1, 2=PIN2 or custom value (decimal)')
803 change_chv_parser.add_argument(
804 'pin_code', type=str, help='PIN code digits \"PIN1\" or \"PIN2\" to get PIN code from external data source')
805 change_chv_parser.add_argument(
806 'new_pin_code', type=str, help='PIN code digits \"PIN1\" or \"PIN2\" to get PIN code from external data source')
Harald Welte31d2cf02021-04-03 10:47:29 +0200807
Harald Weltec91085e2022-02-10 18:05:45 +0100808 @cmd2.with_argparser(change_chv_parser)
809 def do_change_chv(self, opts):
810 """Change PIN code to a new PIN code"""
811 new_pin = self.get_code(opts.new_pin_code)
812 pin = self.get_code(opts.pin_code)
813 (data, sw) = self._cmd.card._scc.change_chv(
814 opts.pin_nr, h2b(pin), h2b(new_pin))
815 self._cmd.poutput("CHV change successful")
Harald Welte31d2cf02021-04-03 10:47:29 +0200816
Harald Weltec91085e2022-02-10 18:05:45 +0100817 disable_chv_parser = argparse.ArgumentParser()
818 disable_chv_parser.add_argument(
819 '--pin-nr', type=int, default=1, help='PIN Number, 1=PIN1, 2=PIN2 or custom value (decimal)')
820 disable_chv_parser.add_argument(
821 'pin_code', type=str, help='PIN code digits, \"PIN1\" or \"PIN2\" to get PIN code from external data source')
Harald Welte31d2cf02021-04-03 10:47:29 +0200822
Harald Weltec91085e2022-02-10 18:05:45 +0100823 @cmd2.with_argparser(disable_chv_parser)
824 def do_disable_chv(self, opts):
825 """Disable PIN code using specified PIN code"""
826 pin = self.get_code(opts.pin_code)
827 (data, sw) = self._cmd.card._scc.disable_chv(opts.pin_nr, h2b(pin))
828 self._cmd.poutput("CHV disable successful")
Harald Welte31d2cf02021-04-03 10:47:29 +0200829
Harald Weltec91085e2022-02-10 18:05:45 +0100830 enable_chv_parser = argparse.ArgumentParser()
831 enable_chv_parser.add_argument(
832 '--pin-nr', type=int, default=1, help='PIN Number, 1=PIN1, 2=PIN2 or custom value (decimal)')
833 enable_chv_parser.add_argument(
834 'pin_code', type=str, help='PIN code digits, \"PIN1\" or \"PIN2\" to get PIN code from external data source')
Harald Welte31d2cf02021-04-03 10:47:29 +0200835
Harald Weltec91085e2022-02-10 18:05:45 +0100836 @cmd2.with_argparser(enable_chv_parser)
837 def do_enable_chv(self, opts):
838 """Enable PIN code using specified PIN code"""
839 pin = self.get_code(opts.pin_code)
840 (data, sw) = self._cmd.card._scc.enable_chv(opts.pin_nr, h2b(pin))
841 self._cmd.poutput("CHV enable successful")
Harald Welte31d2cf02021-04-03 10:47:29 +0200842
Harald Weltec91085e2022-02-10 18:05:45 +0100843 def do_deactivate_file(self, opts):
Harald Welte799c3542022-02-15 15:56:28 +0100844 """Deactivate the currently selected EF"""
Harald Weltec91085e2022-02-10 18:05:45 +0100845 (data, sw) = self._cmd.card._scc.deactivate_file()
Harald Weltea4631612021-04-10 18:17:55 +0200846
Harald Welte799c3542022-02-15 15:56:28 +0100847 activate_file_parser = argparse.ArgumentParser()
848 activate_file_parser.add_argument('NAME', type=str, help='File name or FID of file to activate')
849 @cmd2.with_argparser(activate_file_parser)
Harald Weltec91085e2022-02-10 18:05:45 +0100850 def do_activate_file(self, opts):
Harald Welte12af7932022-02-15 16:39:08 +0100851 """Activate the specified EF. This used to be called REHABILITATE in TS 11.11 for classic
852 SIM. You need to specify the name or FID of the file to activate."""
Harald Weltea6c0f882022-07-17 14:23:17 +0200853 (data, sw) = self._cmd.lchan.activate_file(opts.NAME)
Harald Welte485692b2021-05-25 22:21:44 +0200854
Harald Weltec91085e2022-02-10 18:05:45 +0100855 def complete_activate_file(self, text, line, begidx, endidx) -> List[str]:
856 """Command Line tab completion for ACTIVATE FILE"""
Harald Weltea6c0f882022-07-17 14:23:17 +0200857 index_dict = {1: self._cmd.lchan.selected_file.get_selectable_names()}
Harald Weltec91085e2022-02-10 18:05:45 +0100858 return self._cmd.index_based_complete(text, line, begidx, endidx, index_dict=index_dict)
Harald Welte31d2cf02021-04-03 10:47:29 +0200859
Harald Weltec91085e2022-02-10 18:05:45 +0100860 open_chan_parser = argparse.ArgumentParser()
861 open_chan_parser.add_argument(
862 'chan_nr', type=int, default=0, help='Channel Number')
Harald Welte703f9332021-04-10 18:39:32 +0200863
Harald Weltec91085e2022-02-10 18:05:45 +0100864 @cmd2.with_argparser(open_chan_parser)
865 def do_open_channel(self, opts):
866 """Open a logical channel."""
867 (data, sw) = self._cmd.card._scc.manage_channel(
868 mode='open', lchan_nr=opts.chan_nr)
Harald Welte703f9332021-04-10 18:39:32 +0200869
Harald Weltec91085e2022-02-10 18:05:45 +0100870 close_chan_parser = argparse.ArgumentParser()
871 close_chan_parser.add_argument(
872 'chan_nr', type=int, default=0, help='Channel Number')
Harald Welte703f9332021-04-10 18:39:32 +0200873
Harald Weltec91085e2022-02-10 18:05:45 +0100874 @cmd2.with_argparser(close_chan_parser)
875 def do_close_channel(self, opts):
876 """Close a logical channel."""
877 (data, sw) = self._cmd.card._scc.manage_channel(
878 mode='close', lchan_nr=opts.chan_nr)
Harald Welte703f9332021-04-10 18:39:32 +0200879
Harald Weltec91085e2022-02-10 18:05:45 +0100880 def do_status(self, opts):
881 """Perform the STATUS command."""
Harald Weltea6c0f882022-07-17 14:23:17 +0200882 fcp_dec = self._cmd.lchan.status()
Harald Weltec91085e2022-02-10 18:05:45 +0100883 self._cmd.poutput_json(fcp_dec)
Harald Welte34b05d32021-05-25 22:03:13 +0200884
Harald Weltec91085e2022-02-10 18:05:45 +0100885 suspend_uicc_parser = argparse.ArgumentParser()
886 suspend_uicc_parser.add_argument('--min-duration-secs', type=int, default=60,
887 help='Proposed minimum duration of suspension')
888 suspend_uicc_parser.add_argument('--max-duration-secs', type=int, default=24*60*60,
889 help='Proposed maximum duration of suspension')
Harald Welteec950532021-10-20 13:09:00 +0200890
Harald Weltec91085e2022-02-10 18:05:45 +0100891 # not ISO7816-4 but TS 102 221
892 @cmd2.with_argparser(suspend_uicc_parser)
893 def do_suspend_uicc(self, opts):
894 """Perform the SUSPEND UICC command. Only supported on some UICC."""
895 (duration, token, sw) = self._cmd.card._scc.suspend_uicc(min_len_secs=opts.min_duration_secs,
896 max_len_secs=opts.max_duration_secs)
897 self._cmd.poutput(
898 'Negotiated Duration: %u secs, Token: %s, SW: %s' % (duration, token, sw))
Harald Welteec950532021-10-20 13:09:00 +0200899
Harald Welte703f9332021-04-10 18:39:32 +0200900
Harald Weltef2e761c2021-04-11 11:56:44 +0200901option_parser = argparse.ArgumentParser(prog='pySim-shell', description='interactive SIM card shell',
902 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
Harald Welte28c24312021-04-11 12:19:36 +0200903argparse_add_reader_args(option_parser)
Harald Weltec8ff0262021-04-11 12:06:13 +0200904
905global_group = option_parser.add_argument_group('General Options')
906global_group.add_argument('--script', metavar='PATH', default=None,
Harald Welte28c24312021-04-11 12:19:36 +0200907 help='script with pySim-shell commands to be executed automatically at start-up')
Harald Weltec91085e2022-02-10 18:05:45 +0100908global_group.add_argument('--csv', metavar='FILE',
909 default=None, help='Read card data from CSV file')
Philipp Maier76667642021-09-22 16:53:22 +0200910global_group.add_argument("--card_handler", dest="card_handler_config", metavar="FILE",
Harald Weltec91085e2022-02-10 18:05:45 +0100911 help="Use automatic card handling machine")
Harald Weltec8ff0262021-04-11 12:06:13 +0200912
913adm_group = global_group.add_mutually_exclusive_group()
914adm_group.add_argument('-a', '--pin-adm', metavar='PIN_ADM1', dest='pin_adm', default=None,
915 help='ADM PIN used for provisioning (overwrites default)')
916adm_group.add_argument('-A', '--pin-adm-hex', metavar='PIN_ADM1_HEX', dest='pin_adm_hex', default=None,
917 help='ADM PIN used for provisioning, as hex string (16 characters long)')
Harald Welteb2edd142021-01-08 23:29:35 +0100918
919
920if __name__ == '__main__':
921
Harald Weltec91085e2022-02-10 18:05:45 +0100922 # Parse options
923 opts = option_parser.parse_args()
Harald Welteb2edd142021-01-08 23:29:35 +0100924
Harald Weltec91085e2022-02-10 18:05:45 +0100925 # If a script file is specified, be sure that it actually exists
926 if opts.script:
927 if not os.access(opts.script, os.R_OK):
928 print("Invalid script file!")
929 sys.exit(2)
Philipp Maier13e258d2021-04-08 17:48:49 +0200930
Harald Weltec91085e2022-02-10 18:05:45 +0100931 # Register csv-file as card data provider, either from specified CSV
932 # or from CSV file in home directory
933 csv_default = str(Path.home()) + "/.osmocom/pysim/card_data.csv"
934 if opts.csv:
935 card_key_provider_register(CardKeyProviderCsv(opts.csv))
936 if os.path.isfile(csv_default):
937 card_key_provider_register(CardKeyProviderCsv(csv_default))
Philipp Maier2b11c322021-03-17 12:37:39 +0100938
Harald Weltec91085e2022-02-10 18:05:45 +0100939 # Init card reader driver
940 sl = init_reader(opts)
941 if sl is None:
942 exit(1)
Philipp Maierea95c392021-09-16 13:10:19 +0200943
Harald Weltec91085e2022-02-10 18:05:45 +0100944 # Create command layer
945 scc = SimCardCommands(transport=sl)
Philipp Maierea95c392021-09-16 13:10:19 +0200946
Harald Weltec91085e2022-02-10 18:05:45 +0100947 # Create a card handler (for bulk provisioning)
948 if opts.card_handler_config:
949 ch = CardHandlerAuto(None, opts.card_handler_config)
950 else:
951 ch = CardHandler(sl)
Philipp Maier76667642021-09-22 16:53:22 +0200952
Harald Weltec91085e2022-02-10 18:05:45 +0100953 # Detect and initialize the card in the reader. This may fail when there
954 # is no card in the reader or the card is unresponsive. PysimApp is
955 # able to tolerate and recover from that.
956 try:
957 rs, card = init_card(sl)
958 app = PysimApp(card, rs, sl, ch, opts.script)
959 except:
960 print("Card initialization failed with an exception:")
961 print("---------------------8<---------------------")
962 traceback.print_exc()
963 print("---------------------8<---------------------")
964 print("(you may still try to recover from this manually by using the 'equip' command.)")
965 print(
966 " it should also be noted that some readers may behave strangely when no card")
967 print(" is inserted.)")
968 print("")
Philipp Maier7226c092022-06-01 17:58:38 +0200969 app = PysimApp(card, None, sl, ch, opts.script)
Philipp Maierea95c392021-09-16 13:10:19 +0200970
Harald Weltec91085e2022-02-10 18:05:45 +0100971 # If the user supplies an ADM PIN at via commandline args authenticate
972 # immediately so that the user does not have to use the shell commands
973 pin_adm = sanitize_pin_adm(opts.pin_adm, opts.pin_adm_hex)
974 if pin_adm:
975 if not card:
976 print("Card error, cannot do ADM verification with supplied ADM pin now.")
977 try:
978 card.verify_adm(h2b(pin_adm))
979 except Exception as e:
980 print(e)
Philipp Maier228c98e2021-03-10 20:14:06 +0100981
Harald Weltec91085e2022-02-10 18:05:45 +0100982 app.cmdloop()