Change in pysim[master]: sim-rest-server: Add capability to obtain IMSI + ICCID of card

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Wed Nov 3 11:38:35 UTC 2021


laforge has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/26085 )

Change subject: sim-rest-server: Add capability to obtain IMSI + ICCID of card
......................................................................

sim-rest-server: Add capability to obtain IMSI + ICCID of card

$ curl http://localhost:8000/sim-info-api/v1/slot/0
{
    "imsi": "262011500776110",
    "iccid": "89490240001879910128"
}

Change-Id: I9df8854f6a962e7f86f62b2d44ec7696271c58c8
---
M contrib/sim-rest-server.py
1 file changed, 50 insertions(+), 8 deletions(-)

Approvals:
  laforge: Looks good to me, approved; Verified



diff --git a/contrib/sim-rest-server.py b/contrib/sim-rest-server.py
index 0f77dfe..fb73ef8 100755
--- a/contrib/sim-rest-server.py
+++ b/contrib/sim-rest-server.py
@@ -34,6 +34,23 @@
         #print("CMD: %s -> RSP: %s %s" % (cmd, sw, resp))
         pass
 
+def connect_to_card(slot_nr:int):
+    tp = PcscSimLink(slot_nr, apdu_tracer=ApduPrintTracer())
+    tp.connect()
+
+    scc = SimCardCommands(tp)
+    card = UsimCard(scc)
+
+    # this should be part of UsimCard, but FairewavesSIM breaks with that :/
+    scc.cla_byte = "00"
+    scc.sel_ctrl = "0004"
+
+    card.read_aids()
+    card.select_adf_by_aid(adf='usim')
+
+    return tp, scc, card
+
+
 @route('/sim-auth-api/v1/slot/<int:slot>')
 def auth(request, slot):
     """REST API endpoint for performing authentication against a USIM.
@@ -49,8 +66,7 @@
         return "Malformed Request"
 
     try:
-        tp = PcscSimLink(slot, apdu_tracer=ApduPrintTracer())
-        tp.connect()
+        tp, scc, card = connect_to_card(slot)
     except ReaderError:
         request.setResponseCode(404)
         return "Specified SIM Slot doesn't exist"
@@ -61,13 +77,7 @@
         request.setResponseCode(410)
         return "No SIM card inserted in slot"
 
-    scc = SimCardCommands(tp)
-    card = UsimCard(scc)
-    # this should be part of UsimCard, but FairewavesSIM breaks with that :/
-    scc.cla_byte = "00"
-    scc.sel_ctrl = "0004"
     try:
-        card.read_aids()
         card.select_adf_by_aid(adf='usim')
         res, sw = scc.authenticate(rand, autn)
     except SwMatchError as e:
@@ -78,6 +88,38 @@
 
     return json.dumps(res, indent=4)
 
+ at route('/sim-info-api/v1/slot/<int:slot>')
+def info(request, slot):
+    """REST API endpoint for obtaining information about an USIM.
+    Expects empty body in request.
+    Returns a JSON body containing ICCID, IMSI."""
+
+    try:
+        tp, scc, card = connect_to_card(slot)
+    except ReaderError:
+        request.setResponseCode(404)
+        return "Specified SIM Slot doesn't exist"
+    except ProtocolError:
+        request.setResponseCode(500)
+        return "Error"
+    except NoCardError:
+        request.setResponseCode(410)
+        return "No SIM card inserted in slot"
+
+    try:
+        card.select_adf_by_aid(adf='usim')
+        iccid, sw = card.read_iccid()
+        imsi, sw = card.read_imsi()
+        res = {"imsi": imsi, "iccid": iccid }
+    except SwMatchError as e:
+        request.setResponseCode(500)
+        return "Communication Error %s" % e
+
+    tp.disconnect()
+
+    return json.dumps(res, indent=4)
+
+
 def main(argv):
     parser = argparse.ArgumentParser()
     parser.add_argument("-H", "--host", help="Host/IP to bind HTTP to", default="localhost")

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

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I9df8854f6a962e7f86f62b2d44ec7696271c58c8
Gerrit-Change-Number: 26085
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-CC: Jenkins Builder
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20211103/f2b265b6/attachment.htm>


More information about the gerrit-log mailing list