Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # RESTful HTTP service for performing authentication against USIM cards |
| 4 | # |
Harald Welte | 04897d5 | 2022-07-23 14:07:00 +0200 | [diff] [blame] | 5 | # (C) 2021-2022 by Harald Welte <laforge@osmocom.org> |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation, either version 2 of the License, or |
| 10 | # (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | import json |
| 21 | import sys |
| 22 | import argparse |
| 23 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 24 | from klein import Klein |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 25 | |
| 26 | from pySim.transport import ApduTracer |
| 27 | from pySim.transport.pcsc import PcscSimLink |
| 28 | from pySim.commands import SimCardCommands |
| 29 | from pySim.cards import UsimCard |
| 30 | from pySim.exceptions import * |
| 31 | |
| 32 | class ApduPrintTracer(ApduTracer): |
| 33 | def trace_response(self, cmd, sw, resp): |
| 34 | #print("CMD: %s -> RSP: %s %s" % (cmd, sw, resp)) |
| 35 | pass |
| 36 | |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 37 | def connect_to_card(slot_nr:int): |
| 38 | tp = PcscSimLink(slot_nr, apdu_tracer=ApduPrintTracer()) |
| 39 | tp.connect() |
| 40 | |
| 41 | scc = SimCardCommands(tp) |
| 42 | card = UsimCard(scc) |
| 43 | |
| 44 | # this should be part of UsimCard, but FairewavesSIM breaks with that :/ |
| 45 | scc.cla_byte = "00" |
| 46 | scc.sel_ctrl = "0004" |
| 47 | |
| 48 | card.read_aids() |
| 49 | card.select_adf_by_aid(adf='usim') |
| 50 | |
| 51 | return tp, scc, card |
| 52 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 53 | class ApiError: |
Harald Welte | 04897d5 | 2022-07-23 14:07:00 +0200 | [diff] [blame] | 54 | def __init__(self, msg:str, sw=None): |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 55 | self.msg = msg |
Harald Welte | 04897d5 | 2022-07-23 14:07:00 +0200 | [diff] [blame] | 56 | self.sw = sw |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 57 | |
| 58 | def __str__(self): |
Harald Welte | 04897d5 | 2022-07-23 14:07:00 +0200 | [diff] [blame] | 59 | d = {'error': {'message':self.msg}} |
| 60 | if self.sw: |
| 61 | d['error']['status_word'] = self.sw |
| 62 | return json.dumps(d) |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 63 | |
| 64 | |
Harald Welte | 6f8cf9b | 2022-07-06 16:28:56 +0200 | [diff] [blame] | 65 | def set_headers(request): |
| 66 | request.setHeader('Content-Type', 'application/json') |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 67 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 68 | class SimRestServer: |
| 69 | app = Klein() |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 70 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 71 | @app.handle_errors(NoCardError) |
| 72 | def no_card_error(self, request, failure): |
| 73 | set_headers(request) |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 74 | request.setResponseCode(410) |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 75 | return str(ApiError("No SIM card inserted in slot")) |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 76 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 77 | @app.handle_errors(ReaderError) |
| 78 | def reader_error(self, request, failure): |
| 79 | set_headers(request) |
| 80 | request.setResponseCode(404) |
| 81 | return str(ApiError("Reader Error: Specified SIM Slot doesn't exist")) |
| 82 | |
| 83 | @app.handle_errors(ProtocolError) |
| 84 | def protocol_error(self, request, failure): |
| 85 | set_headers(request) |
| 86 | request.setResponseCode(500) |
Harald Welte | 04897d5 | 2022-07-23 14:07:00 +0200 | [diff] [blame] | 87 | return str(ApiError("Protocol Error: %s" % failure.value)) |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 88 | |
| 89 | @app.handle_errors(SwMatchError) |
| 90 | def sw_match_error(self, request, failure): |
| 91 | set_headers(request) |
| 92 | request.setResponseCode(500) |
Harald Welte | 04897d5 | 2022-07-23 14:07:00 +0200 | [diff] [blame] | 93 | sw = failure.value.sw_actual |
| 94 | if sw == '9862': |
| 95 | return str(ApiError("Card Authentication Error - Incorrect MAC", sw)) |
| 96 | elif sw == '6982': |
| 97 | return str(ApiError("Security Status not satisfied - Card PIN enabled?", sw)) |
| 98 | else: |
| 99 | return str(ApiError("Card Communication Error %s" % failure.value), sw) |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 100 | |
| 101 | |
| 102 | @app.route('/sim-auth-api/v1/slot/<int:slot>') |
| 103 | def auth(self, request, slot): |
| 104 | """REST API endpoint for performing authentication against a USIM. |
| 105 | Expects a JSON body containing RAND and AUTN. |
| 106 | Returns a JSON body containing RES, CK, IK and Kc.""" |
| 107 | try: |
| 108 | # there are two hex-string JSON parameters in the body: rand and autn |
| 109 | content = json.loads(request.content.read()) |
| 110 | rand = content['rand'] |
| 111 | autn = content['autn'] |
| 112 | except: |
| 113 | set_headers(request) |
| 114 | request.setResponseCode(400) |
| 115 | return str(ApiError("Malformed Request")) |
| 116 | |
| 117 | tp, scc, card = connect_to_card(slot) |
| 118 | |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 119 | card.select_adf_by_aid(adf='usim') |
| 120 | res, sw = scc.authenticate(rand, autn) |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 121 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 122 | tp.disconnect() |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 123 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 124 | set_headers(request) |
| 125 | return json.dumps(res, indent=4) |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 126 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 127 | @app.route('/sim-info-api/v1/slot/<int:slot>') |
| 128 | def info(self, request, slot): |
| 129 | """REST API endpoint for obtaining information about an USIM. |
| 130 | Expects empty body in request. |
| 131 | Returns a JSON body containing ICCID, IMSI.""" |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 132 | |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 133 | tp, scc, card = connect_to_card(slot) |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 134 | |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 135 | card.select_adf_by_aid(adf='usim') |
| 136 | iccid, sw = card.read_iccid() |
| 137 | imsi, sw = card.read_imsi() |
| 138 | res = {"imsi": imsi, "iccid": iccid } |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 139 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 140 | tp.disconnect() |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 141 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 142 | set_headers(request) |
| 143 | return json.dumps(res, indent=4) |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 144 | |
| 145 | |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 146 | def main(argv): |
| 147 | parser = argparse.ArgumentParser() |
| 148 | parser.add_argument("-H", "--host", help="Host/IP to bind HTTP to", default="localhost") |
| 149 | parser.add_argument("-p", "--port", help="TCP port to bind HTTP to", default=8000) |
| 150 | #parser.add_argument("-v", "--verbose", help="increase output verbosity", action='count', default=0) |
| 151 | |
| 152 | args = parser.parse_args() |
| 153 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 154 | srr = SimRestServer() |
| 155 | srr.app.run(args.host, args.port) |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 156 | |
| 157 | if __name__ == "__main__": |
| 158 | main(sys.argv) |