aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilipp Maier <pmaier@sysmocom.de>2021-11-15 17:24:44 +0100
committerPhilipp Maier <pmaier@sysmocom.de>2021-11-23 18:35:34 +0100
commitabc23365715d36d8e4c3afcaa4fbf2df2764d7be (patch)
treecd134a244154c036839882a95e1f89ff230e30f9
parent47833bc176d1807ca215425b3cc42289c3557212 (diff)
pySim-read: put try/catch block around select_adf_by_aid()
Selecting an application may fail, especially when the application does not exist on the card. Change-Id: Ia904a74d672cf9551fb4ee062dd606b350b64cef
-rwxr-xr-xpySim-read.py23
1 files changed, 21 insertions, 2 deletions
diff --git a/pySim-read.py b/pySim-read.py
index 959a7c5..5e48116 100755
--- a/pySim-read.py
+++ b/pySim-read.py
@@ -34,6 +34,7 @@ from pySim.ts_31_103 import EF_IST_map, EF_ISIM_ADF_map
from pySim.commands import SimCardCommands
from pySim.transport import init_reader, argparse_add_reader_args
+from pySim.exceptions import SwMatchError
from pySim.cards import card_detect, SimCard, UsimCard, IsimCard
from pySim.utils import h2b, swap_nibbles, rpad, dec_imsi, dec_iccid, dec_msisdn
from pySim.utils import format_xplmn_w_act, dec_st
@@ -44,6 +45,24 @@ option_parser = argparse.ArgumentParser(prog='pySim-read',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
argparse_add_reader_args(option_parser)
+def select_app(adf:str, card:SimCard):
+ """Select application by its AID"""
+ sw = 0
+ try:
+ if card._scc.cla_byte == "00":
+ data, sw = card.select_adf_by_aid(adf)
+ except SwMatchError as e:
+ if e.sw_actual == "6a82":
+ # If we can't select the file because it does not exist, we just remain silent since it means
+ # that this card just does not have an USIM application installed, which is not an error.
+ pass
+ else:
+ print("ADF." + adf + ": Can't select application -- " + str(e))
+ except Exception as e:
+ print("ADF." + adf + ": Can't select application -- " + str(e))
+
+ return sw
+
if __name__ == '__main__':
# Parse options
@@ -219,7 +238,7 @@ if __name__ == '__main__':
# Check whether we have th AID of USIM, if so select it by its AID
# EF.UST - File Id in ADF USIM : 6f38
- data, sw = card.select_adf_by_aid(adf="usim")
+ sw = select_app("USIM", card)
if sw == '9000':
# Select USIM profile
usim_card = UsimCard(scc)
@@ -269,7 +288,7 @@ if __name__ == '__main__':
print("ePDGSelection: Can't read file -- " + str(e))
# Select ISIM application by its AID
- data, sw = card.select_adf_by_aid(adf="isim")
+ sw = select_app("ISIM", card)
if sw == '9000':
# Select USIM profile
isim_card = IsimCard(scc)