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
This commit is contained in:
Philipp Maier 2018-07-04 15:10:23 +02:00
parent 5c2733a7e9
commit ed0c9ab4dc
5 changed files with 16 additions and 10 deletions

View File

@ -35,6 +35,7 @@ import re
# smartcard python modules from pyscard # smartcard python modules from pyscard
from smartcard.CardType import AnyCardType from smartcard.CardType import AnyCardType
from smartcard.CardType import ATRCardType
from smartcard.CardRequest import CardRequest from smartcard.CardRequest import CardRequest
from smartcard.CardConnection import CardConnection from smartcard.CardConnection import CardConnection
from smartcard.CardConnectionObserver import ConsoleCardConnectionObserver from smartcard.CardConnectionObserver import ConsoleCardConnectionObserver
@ -142,7 +143,7 @@ class ISO7816(object):
0xAB : 'Security Attribute expanded', 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 connect smartcard and defines class CLA code for communication
uses "pyscard" library services uses "pyscard" library services
@ -150,7 +151,12 @@ class ISO7816(object):
creates self.CLA attribute with CLA code creates self.CLA attribute with CLA code
and self.coms attribute with associated "apdu_stack" instance 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) cardrequest = CardRequest(timeout=1, cardType=cardtype)
self.cardservice = cardrequest.waitforcard() self.cardservice = cardrequest.waitforcard()
self.cardservice.connection.connect() self.cardservice.connection.connect()

View File

@ -99,12 +99,12 @@ class SIM(ISO7816):
use self.dbg = 1 or more to print live debugging information 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 initialize like an ISO7816-4 card with CLA=0xA0
can also be used for USIM working in SIM mode, 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: if self.dbg >= 2:
log(3, '(SIM.__init__) type definition: %s' % type(self)) log(3, '(SIM.__init__) type definition: %s' % type(self))

View File

@ -143,7 +143,7 @@ class USIM(UICC):
use self.dbg = 1 or more to print live debugging information 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 initializes like an ISO7816-4 card with CLA=0x00
and checks available AID (Application ID) read from EF_DIR and checks available AID (Application ID) read from EF_DIR
@ -151,7 +151,7 @@ class USIM(UICC):
initializes on the MF initializes on the MF
''' '''
# initialize like a UICC # initialize like a UICC
ISO7816.__init__(self, CLA=0x00) ISO7816.__init__(self, atr, CLA=0x00)
self.AID = [] self.AID = []
if self.dbg >= 2: if self.dbg >= 2:

View File

@ -75,12 +75,12 @@ class Simcard():
card = None card = None
# Constructor: Create a new simcard object # Constructor: Create a new simcard object
def __init__(self, cardtype = GSM_USIM): def __init__(self, cardtype = GSM_USIM, atr = None):
if cardtype == GSM_USIM: if cardtype == GSM_USIM:
self.card = USIM() self.card = USIM(atr)
self.usim = True self.usim = True
else: else:
self.card = SIM() self.card = SIM(atr)
self.usim = False self.usim = False
# Find the right class byte, depending on the simcard type # Find the right class byte, depending on the simcard type

View File

@ -142,7 +142,7 @@ def main(argv):
# Claim terminal # Claim terminal
print "Initializing smartcard 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("")
print "Detected Card ICCID: ", sim.card.get_ICCID() print "Detected Card ICCID: ", sim.card.get_ICCID()