gui...@gmail.com has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/pysim/+/18710 )


Change subject: cards.py: added generic USIM
......................................................................

cards.py: added generic USIM

Work in progress

a generic USIM class card will try to CLA set to 00
and assume it's an USIM card type if it gets a reply

Change-Id: I8c0a914ed00a713fe473fc64f35d17ba34176e52
---
M pySim-read.py
M pySim/cards.py
M pySim/transport/serial.py
3 files changed, 57 insertions(+), 14 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/10/18710/1

diff --git a/pySim-read.py b/pySim-read.py
index b23e6ee..345e6d5 100755
--- a/pySim-read.py
+++ b/pySim-read.py
@@ -88,17 +88,6 @@
        # Wait for SIM card
        sl.wait_for_card()

-       # Assuming UICC SIM
-       scc.cla_byte = "00"
-       scc.sel_ctrl = "0004"
-
-       # Testing for Classic SIM or UICC
-       (res, sw) = sl.send_apdu(scc.cla_byte + "a4" + scc.sel_ctrl + "02" + 
"3f00")
-       if sw == '6e00':
-               # Just a Classic SIM
-               scc.cla_byte = "a0"
-               scc.sel_ctrl = "0000"
-
        # Program the card
        print("Reading ...")

@@ -240,7 +229,8 @@

        # Check whether we have th AID of USIM, if so select it by its AID
        # EF.UST - File Id in ADF USIM : 6f38
-       if '9000' == card.select_adf_by_aid():
+
+       if card.name == 'GenericUSIM' and '9000' == card.select_adf_by_aid():
                # EF.UST
                (res, sw) = card.read_binary(EF_USIM_ADF_map['UST'])
                if sw == '9000':
@@ -252,7 +242,7 @@

        # Check whether we have th AID of ISIM, if so select it by its AID
        # EF.IST - File Id in ADF ISIM : 6f07
-       if '9000' == card.select_adf_by_aid(adf="isim"):
+       if card.name != 'GenericUSIM' and '9000' == 
card.select_adf_by_aid(adf="isim"):
                # EF.IST
                (res, sw) = card.read_binary('6f07')
                if sw == '9000':
diff --git a/pySim/cards.py b/pySim/cards.py
index 808fde1..b0bc537 100644
--- a/pySim/cards.py
+++ b/pySim/cards.py
@@ -1156,11 +1156,63 @@

                return

+class GenericUSIM(Card):
+       """
+       Generic USIM card
+       """
+
+       name = 'GenericUSIM'
+
+       def __init__(self, ssc):
+               super(GenericUSIM, self).__init__(ssc)
+               self._scc.cla_byte = "00"
+               self._scc.sel_ctrl = "0004" #request an FCP
+
+
+       @classmethod
+       def autodetect(kls, scc):
+               # reset the card, and send a PDU with CLA, P1 & P2 for USIM.
+               scc.reset_card()
+               scc._tp.wait_for_card()
+               #
+               (res, sw) = scc._tp.send_apdu("00a40004023f00")
+               if sw == '9000':
+                       return kls(scc)
+               #if it is not successfull, the it's not a USIM card
+               return None
+
+
+       # Select ADF.USIM in the Card using its full AID
+       def select_adf_by_aid(self):
+               for aid in self._aids:
+                       if "a0000000871002" in aid:
+                               (res, sw) = self._scc.select_adf(aid)
+                               return sw
+
+               return None
+
+       # Read the (full) AID
+       def read_aid(self):
+
+               # First (known) halves of the AID
+               aid_usim = "a0000000871002"
+
+               # Find out how many records the EF.DIR has, then go through
+               # all records and try to find the AID we are looking for
+               aid_record_count = self._scc.record_count(['2F00'])
+               for i in range(0, aid_record_count):
+                       record = self._scc.read_record(['2F00'], i + 1)
+                       if aid_usim in record[0]:
+                               aid_len = int(record[0][6:8], 16)
+                               return record[0][8:8 + aid_len * 2]
+
+               return None
+

 # In order for autodetection ...
 _cards_classes = [ FakeMagicSim, SuperSim, MagicSim, GrcardSim,
                   SysmoSIMgr1, SysmoSIMgr2, SysmoUSIMgr1, SysmoUSIMSJS1,
-                  FairwavesSIM, OpenCellsSim, WavemobileSim, SysmoISIMSJA2 ]
+                  FairwavesSIM, OpenCellsSim, WavemobileSim, SysmoISIMSJA2, 
GenericUSIM ]

 def card_autodetect(scc):
        for kls in _cards_classes:
diff --git a/pySim/transport/serial.py b/pySim/transport/serial.py
index 11fcd6a..12a7874 100644
--- a/pySim/transport/serial.py
+++ b/pySim/transport/serial.py
@@ -184,6 +184,7 @@
        def send_apdu_raw(self, pdu):
                """see LinkBase.send_apdu_raw"""

+               self._dbg_print("TX PDU: %s" % pdu)
                pdu = h2b(pdu)
                data_len = ord(pdu[4])  # P3


--
To view, visit https://gerrit.osmocom.org/c/pysim/+/18710
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I8c0a914ed00a713fe473fc64f35d17ba34176e52
Gerrit-Change-Number: 18710
Gerrit-PatchSet: 1
Gerrit-Owner: gui...@gmail.com <gui...@gmail.com>
Gerrit-MessageType: newchange

Reply via email to