aboutsummaryrefslogtreecommitdiffstats
path: root/contrib
diff options
context:
space:
mode:
authorHarald Welte <laforge@osmocom.org>2021-11-03 12:14:50 +0100
committerHarald Welte <laforge@osmocom.org>2021-11-03 12:34:24 +0100
commit33f8da8a52825ef3dc341da7ed9491e6c8ab749b (patch)
treec2444e14aa16963d57d52f10b460d193ff5030fa /contrib
parent7a401a24bbf399d10c39dfb689164918dccc2b4f (diff)
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
Diffstat (limited to 'contrib')
-rwxr-xr-xcontrib/sim-rest-server.py58
1 files changed, 50 insertions, 8 deletions
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 @@ class ApduPrintTracer(ApduTracer):
#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 @@ def auth(request, slot):
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 @@ def auth(request, slot):
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 @@ def auth(request, slot):
return json.dumps(res, indent=4)
+@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")