From ed0c9ab4dce875ecdddd78133edbcfe8b1f8188f Mon Sep 17 00:00:00 2001 From: Philipp Maier Date: Wed, 4 Jul 2018 15:10:23 +0200 Subject: [PATCH] ICC: recognize card by its ATR When working in an environment with multiple card terminals, then it might be helpful to make sure that only the intended target simcard (sysmo-usim-sjs1) is accessed. - Add atr parameter to the ICC, SIM and USIM classes. When no atr is given, then ICC will pick any card as it was before - Make sure to pass the ATR when the sim object is created Change-Id: I874aae92d29a7e1051e4cebefd85592c5fb78a2e Related: OS#3376 --- card/ICC.py | 10 ++++++++-- card/SIM.py | 4 ++-- card/USIM.py | 4 ++-- simcard.py | 6 +++--- sysmo-usim-tool.sjs1.py | 2 +- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/card/ICC.py b/card/ICC.py index 9f94022..28b7fb2 100644 --- a/card/ICC.py +++ b/card/ICC.py @@ -35,6 +35,7 @@ import re # smartcard python modules from pyscard from smartcard.CardType import AnyCardType +from smartcard.CardType import ATRCardType from smartcard.CardRequest import CardRequest from smartcard.CardConnection import CardConnection from smartcard.CardConnectionObserver import ConsoleCardConnectionObserver @@ -142,7 +143,7 @@ class ISO7816(object): 0xAB : 'Security Attribute expanded', } - def __init__(self, CLA=0x00): + def __init__(self, atr=None, CLA=0x00): ''' connect smartcard and defines class CLA code for communication uses "pyscard" library services @@ -150,7 +151,12 @@ class ISO7816(object): creates self.CLA attribute with CLA code and self.coms attribute with associated "apdu_stack" instance ''' - cardtype = AnyCardType() + + if (atr): + cardtype = ATRCardType(atr) + else: + cardtype = AnyCardType() + cardrequest = CardRequest(timeout=1, cardType=cardtype) self.cardservice = cardrequest.waitforcard() self.cardservice.connection.connect() diff --git a/card/SIM.py b/card/SIM.py index 06eb458..4065167 100644 --- a/card/SIM.py +++ b/card/SIM.py @@ -99,12 +99,12 @@ class SIM(ISO7816): use self.dbg = 1 or more to print live debugging information ''' - def __init__(self): + def __init__(self, atr = None): ''' initialize like an ISO7816-4 card with CLA=0xA0 can also be used for USIM working in SIM mode, ''' - ISO7816.__init__(self, CLA=0xA0) + ISO7816.__init__(self, atr, CLA=0xA0) if self.dbg >= 2: log(3, '(SIM.__init__) type definition: %s' % type(self)) diff --git a/card/USIM.py b/card/USIM.py index 71b87ce..cafe510 100644 --- a/card/USIM.py +++ b/card/USIM.py @@ -143,7 +143,7 @@ class USIM(UICC): use self.dbg = 1 or more to print live debugging information ''' - def __init__(self): + def __init__(self, atr = None): ''' initializes like an ISO7816-4 card with CLA=0x00 and checks available AID (Application ID) read from EF_DIR @@ -151,7 +151,7 @@ class USIM(UICC): initializes on the MF ''' # initialize like a UICC - ISO7816.__init__(self, CLA=0x00) + ISO7816.__init__(self, atr, CLA=0x00) self.AID = [] if self.dbg >= 2: diff --git a/simcard.py b/simcard.py index 915aa94..a1adb73 100644 --- a/simcard.py +++ b/simcard.py @@ -75,12 +75,12 @@ class Simcard(): card = None # Constructor: Create a new simcard object - def __init__(self, cardtype = GSM_USIM): + def __init__(self, cardtype = GSM_USIM, atr = None): if cardtype == GSM_USIM: - self.card = USIM() + self.card = USIM(atr) self.usim = True else: - self.card = SIM() + self.card = SIM(atr) self.usim = False # Find the right class byte, depending on the simcard type diff --git a/sysmo-usim-tool.sjs1.py b/sysmo-usim-tool.sjs1.py index 867b565..cadb204 100755 --- a/sysmo-usim-tool.sjs1.py +++ b/sysmo-usim-tool.sjs1.py @@ -142,7 +142,7 @@ def main(argv): # Claim terminal print "Initializing smartcard terminal..." - sim = Simcard() + sim = Simcard(GSM_USIM, toBytes("3B 9F 96 80 1F C7 80 31 A0 73 BE 21 13 67 43 20 07 18 00 00 01 A5")) print("") print "Detected Card ICCID: ", sim.card.get_ICCID()