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 |
Philipp Maier | 91b379a | 2023-08-16 11:43:19 +0200 | [diff] [blame] | 29 | from pySim.cards import UiccCardBase |
Philipp Maier | 7d13845 | 2023-08-16 11:47:36 +0200 | [diff] [blame] | 30 | from pySim.utils import dec_iccid, dec_imsi |
| 31 | from pySim.ts_51_011 import EF_IMSI |
| 32 | from pySim.ts_102_221 import EF_ICCID |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 33 | from pySim.exceptions import * |
| 34 | |
| 35 | class ApduPrintTracer(ApduTracer): |
| 36 | def trace_response(self, cmd, sw, resp): |
| 37 | #print("CMD: %s -> RSP: %s %s" % (cmd, sw, resp)) |
| 38 | pass |
| 39 | |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 40 | def connect_to_card(slot_nr:int): |
| 41 | tp = PcscSimLink(slot_nr, apdu_tracer=ApduPrintTracer()) |
| 42 | tp.connect() |
| 43 | |
| 44 | scc = SimCardCommands(tp) |
Philipp Maier | 91b379a | 2023-08-16 11:43:19 +0200 | [diff] [blame] | 45 | card = UiccCardBase(scc) |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 46 | |
| 47 | # this should be part of UsimCard, but FairewavesSIM breaks with that :/ |
| 48 | scc.cla_byte = "00" |
| 49 | scc.sel_ctrl = "0004" |
| 50 | |
| 51 | card.read_aids() |
Philipp Maier | 71a3fb8 | 2023-08-16 11:31:31 +0200 | [diff] [blame] | 52 | |
| 53 | # ensure that MF is selected when we are done. |
| 54 | card._scc.select_file('3f00') |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 55 | |
| 56 | return tp, scc, card |
| 57 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 58 | class ApiError: |
Harald Welte | 04897d5 | 2022-07-23 14:07:00 +0200 | [diff] [blame] | 59 | def __init__(self, msg:str, sw=None): |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 60 | self.msg = msg |
Harald Welte | 04897d5 | 2022-07-23 14:07:00 +0200 | [diff] [blame] | 61 | self.sw = sw |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 62 | |
| 63 | def __str__(self): |
Harald Welte | 04897d5 | 2022-07-23 14:07:00 +0200 | [diff] [blame] | 64 | d = {'error': {'message':self.msg}} |
| 65 | if self.sw: |
| 66 | d['error']['status_word'] = self.sw |
| 67 | return json.dumps(d) |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 68 | |
| 69 | |
Harald Welte | 6f8cf9b | 2022-07-06 16:28:56 +0200 | [diff] [blame] | 70 | def set_headers(request): |
| 71 | request.setHeader('Content-Type', 'application/json') |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 72 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 73 | class SimRestServer: |
| 74 | app = Klein() |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 75 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 76 | @app.handle_errors(NoCardError) |
| 77 | def no_card_error(self, request, failure): |
| 78 | set_headers(request) |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 79 | request.setResponseCode(410) |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 80 | return str(ApiError("No SIM card inserted in slot")) |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 81 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 82 | @app.handle_errors(ReaderError) |
| 83 | def reader_error(self, request, failure): |
| 84 | set_headers(request) |
| 85 | request.setResponseCode(404) |
| 86 | return str(ApiError("Reader Error: Specified SIM Slot doesn't exist")) |
| 87 | |
| 88 | @app.handle_errors(ProtocolError) |
| 89 | def protocol_error(self, request, failure): |
| 90 | set_headers(request) |
| 91 | request.setResponseCode(500) |
Harald Welte | 04897d5 | 2022-07-23 14:07:00 +0200 | [diff] [blame] | 92 | return str(ApiError("Protocol Error: %s" % failure.value)) |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 93 | |
| 94 | @app.handle_errors(SwMatchError) |
| 95 | def sw_match_error(self, request, failure): |
| 96 | set_headers(request) |
| 97 | request.setResponseCode(500) |
Harald Welte | 04897d5 | 2022-07-23 14:07:00 +0200 | [diff] [blame] | 98 | sw = failure.value.sw_actual |
| 99 | if sw == '9862': |
| 100 | return str(ApiError("Card Authentication Error - Incorrect MAC", sw)) |
| 101 | elif sw == '6982': |
| 102 | return str(ApiError("Security Status not satisfied - Card PIN enabled?", sw)) |
| 103 | else: |
Philipp Maier | 3a37ad0 | 2023-08-16 12:08:56 +0200 | [diff] [blame^] | 104 | return str(ApiError("Card Communication Error %s" % failure.value, sw)) |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 105 | |
| 106 | |
| 107 | @app.route('/sim-auth-api/v1/slot/<int:slot>') |
| 108 | def auth(self, request, slot): |
| 109 | """REST API endpoint for performing authentication against a USIM. |
| 110 | Expects a JSON body containing RAND and AUTN. |
| 111 | Returns a JSON body containing RES, CK, IK and Kc.""" |
| 112 | try: |
| 113 | # there are two hex-string JSON parameters in the body: rand and autn |
| 114 | content = json.loads(request.content.read()) |
| 115 | rand = content['rand'] |
| 116 | autn = content['autn'] |
| 117 | except: |
| 118 | set_headers(request) |
| 119 | request.setResponseCode(400) |
| 120 | return str(ApiError("Malformed Request")) |
| 121 | |
| 122 | tp, scc, card = connect_to_card(slot) |
| 123 | |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 124 | card.select_adf_by_aid(adf='usim') |
| 125 | res, sw = scc.authenticate(rand, autn) |
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 | tp.disconnect() |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 128 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 129 | set_headers(request) |
| 130 | return json.dumps(res, indent=4) |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 131 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 132 | @app.route('/sim-info-api/v1/slot/<int:slot>') |
| 133 | def info(self, request, slot): |
| 134 | """REST API endpoint for obtaining information about an USIM. |
| 135 | Expects empty body in request. |
| 136 | Returns a JSON body containing ICCID, IMSI.""" |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 137 | |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 138 | tp, scc, card = connect_to_card(slot) |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 139 | |
Philipp Maier | 7d13845 | 2023-08-16 11:47:36 +0200 | [diff] [blame] | 140 | ef_iccid = EF_ICCID() |
| 141 | (iccid, sw) = card._scc.read_binary(ef_iccid.fid) |
| 142 | |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 143 | card.select_adf_by_aid(adf='usim') |
Philipp Maier | 7d13845 | 2023-08-16 11:47:36 +0200 | [diff] [blame] | 144 | ef_imsi = EF_IMSI() |
| 145 | (imsi, sw) = card._scc.read_binary(ef_imsi.fid) |
| 146 | |
| 147 | res = {"imsi": dec_imsi(imsi), "iccid": dec_iccid(iccid) } |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 148 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 149 | tp.disconnect() |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 150 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 151 | set_headers(request) |
| 152 | return json.dumps(res, indent=4) |
Harald Welte | 33f8da8 | 2021-11-03 12:14:50 +0100 | [diff] [blame] | 153 | |
| 154 | |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 155 | def main(argv): |
| 156 | parser = argparse.ArgumentParser() |
| 157 | parser.add_argument("-H", "--host", help="Host/IP to bind HTTP to", default="localhost") |
| 158 | parser.add_argument("-p", "--port", help="TCP port to bind HTTP to", default=8000) |
| 159 | #parser.add_argument("-v", "--verbose", help="increase output verbosity", action='count', default=0) |
| 160 | |
| 161 | args = parser.parse_args() |
| 162 | |
Harald Welte | 3f3b45a | 2022-07-23 13:44:20 +0200 | [diff] [blame] | 163 | srr = SimRestServer() |
| 164 | srr.app.run(args.host, args.port) |
Harald Welte | c781ab8 | 2021-05-23 11:50:19 +0200 | [diff] [blame] | 165 | |
| 166 | if __name__ == "__main__": |
| 167 | main(sys.argv) |