Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | """ pySim: PCSC reader transport link base |
| 4 | """ |
| 5 | |
Vadim Yanitskiy | e8c34ca | 2021-05-02 02:29:52 +0200 | [diff] [blame] | 6 | import abc |
Harald Welte | 28c2431 | 2021-04-11 12:19:36 +0200 | [diff] [blame] | 7 | import argparse |
Vadim Yanitskiy | e8c34ca | 2021-05-02 02:29:52 +0200 | [diff] [blame] | 8 | from typing import Optional, Tuple |
Harald Welte | 6e0458d | 2021-04-03 11:52:37 +0200 | [diff] [blame] | 9 | |
Harald Welte | e79cc80 | 2021-01-21 14:10:43 +0100 | [diff] [blame] | 10 | from pySim.exceptions import * |
Harald Welte | e0f9ef1 | 2021-04-10 17:22:35 +0200 | [diff] [blame] | 11 | from pySim.construct import filter_dict |
Harald Welte | fd476b4 | 2022-08-06 14:01:26 +0200 | [diff] [blame] | 12 | from pySim.utils import sw_match, b2h, h2b, i2h, Hexstr |
| 13 | from pySim.cat import ProactiveCommand |
Harald Welte | e79cc80 | 2021-01-21 14:10:43 +0100 | [diff] [blame] | 14 | |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 15 | # |
| 16 | # Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com> |
Harald Welte | fd476b4 | 2022-08-06 14:01:26 +0200 | [diff] [blame] | 17 | # Copyright (C) 2021-2022 Harald Welte <laforge@osmocom.org> |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 18 | # |
| 19 | # This program is free software: you can redistribute it and/or modify |
| 20 | # it under the terms of the GNU General Public License as published by |
| 21 | # the Free Software Foundation, either version 2 of the License, or |
| 22 | # (at your option) any later version. |
| 23 | # |
| 24 | # This program is distributed in the hope that it will be useful, |
| 25 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 | # GNU General Public License for more details. |
| 28 | # |
| 29 | # You should have received a copy of the GNU General Public License |
| 30 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 31 | # |
| 32 | |
Harald Welte | 7829d8a | 2021-04-10 11:28:53 +0200 | [diff] [blame] | 33 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 34 | class ApduTracer: |
| 35 | def trace_command(self, cmd): |
| 36 | pass |
| 37 | |
| 38 | def trace_response(self, cmd, sw, resp): |
| 39 | pass |
Harald Welte | 7829d8a | 2021-04-10 11:28:53 +0200 | [diff] [blame] | 40 | |
Harald Welte | fd476b4 | 2022-08-06 14:01:26 +0200 | [diff] [blame] | 41 | class ProactiveHandler(abc.ABC): |
| 42 | """Abstract base class representing the interface of some code that handles |
| 43 | the proactive commands, as returned by the card in responses to the FETCH |
| 44 | command.""" |
| 45 | def receive_fetch_raw(self, payload: Hexstr): |
| 46 | # parse the proactive command |
| 47 | pcmd = ProactiveCommand() |
| 48 | parsed = pcmd.from_tlv(h2b(payload)) |
| 49 | # try to find a generic handler like handle_SendShortMessage |
| 50 | handle_name = 'handle_%s' % type(parsed).__name__ |
| 51 | if hasattr(self, handle_name): |
| 52 | handler = getattr(self, handle_name) |
| 53 | return handler(pcmd.decoded) |
| 54 | # fall back to common handler |
| 55 | return self.receive_fetch(pcmd) |
| 56 | |
| 57 | def receive_fetch(self, pcmd: ProactiveCommand): |
| 58 | """Default handler for not otherwise handled proactive commands.""" |
| 59 | raise NotImplementedError('No handler method for %s' % pcmd.decoded) |
| 60 | |
| 61 | |
Harald Welte | 7829d8a | 2021-04-10 11:28:53 +0200 | [diff] [blame] | 62 | |
Vadim Yanitskiy | e8c34ca | 2021-05-02 02:29:52 +0200 | [diff] [blame] | 63 | class LinkBase(abc.ABC): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 64 | """Base class for link/transport to card.""" |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 65 | |
Harald Welte | fd476b4 | 2022-08-06 14:01:26 +0200 | [diff] [blame] | 66 | def __init__(self, sw_interpreter=None, apdu_tracer=None, |
| 67 | proactive_handler: Optional[ProactiveHandler]=None): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 68 | self.sw_interpreter = sw_interpreter |
| 69 | self.apdu_tracer = apdu_tracer |
Harald Welte | fd476b4 | 2022-08-06 14:01:26 +0200 | [diff] [blame] | 70 | self.proactive_handler = proactive_handler |
Harald Welte | 4f2c546 | 2021-04-03 11:48:22 +0200 | [diff] [blame] | 71 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 72 | @abc.abstractmethod |
| 73 | def _send_apdu_raw(self, pdu: str) -> Tuple[str, str]: |
| 74 | """Implementation specific method for sending the PDU.""" |
Vadim Yanitskiy | e8c34ca | 2021-05-02 02:29:52 +0200 | [diff] [blame] | 75 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 76 | def set_sw_interpreter(self, interp): |
| 77 | """Set an (optional) status word interpreter.""" |
| 78 | self.sw_interpreter = interp |
Harald Welte | 4f2c546 | 2021-04-03 11:48:22 +0200 | [diff] [blame] | 79 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 80 | @abc.abstractmethod |
| 81 | def wait_for_card(self, timeout: int = None, newcardonly: bool = False): |
| 82 | """Wait for a card and connect to it |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 83 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 84 | Args: |
| 85 | timeout : Maximum wait time in seconds (None=no timeout) |
| 86 | newcardonly : Should we wait for a new card, or an already inserted one ? |
| 87 | """ |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 88 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 89 | @abc.abstractmethod |
| 90 | def connect(self): |
| 91 | """Connect to a card immediately |
| 92 | """ |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 93 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 94 | @abc.abstractmethod |
| 95 | def disconnect(self): |
| 96 | """Disconnect from card |
| 97 | """ |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 98 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 99 | @abc.abstractmethod |
| 100 | def reset_card(self): |
| 101 | """Resets the card (power down/up) |
| 102 | """ |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 103 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 104 | def send_apdu_raw(self, pdu: str): |
| 105 | """Sends an APDU with minimal processing |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 106 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 107 | Args: |
| 108 | pdu : string of hexadecimal characters (ex. "A0A40000023F00") |
| 109 | Returns: |
| 110 | tuple(data, sw), where |
| 111 | data : string (in hex) of returned data (ex. "074F4EFFFF") |
| 112 | sw : string (in hex) of status word (ex. "9000") |
| 113 | """ |
| 114 | if self.apdu_tracer: |
| 115 | self.apdu_tracer.trace_command(pdu) |
| 116 | (data, sw) = self._send_apdu_raw(pdu) |
| 117 | if self.apdu_tracer: |
| 118 | self.apdu_tracer.trace_response(pdu, sw, data) |
| 119 | return (data, sw) |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 120 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 121 | def send_apdu(self, pdu): |
| 122 | """Sends an APDU and auto fetch response data |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 123 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 124 | Args: |
| 125 | pdu : string of hexadecimal characters (ex. "A0A40000023F00") |
| 126 | Returns: |
| 127 | tuple(data, sw), where |
| 128 | data : string (in hex) of returned data (ex. "074F4EFFFF") |
| 129 | sw : string (in hex) of status word (ex. "9000") |
| 130 | """ |
| 131 | data, sw = self.send_apdu_raw(pdu) |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 132 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 133 | # When we have sent the first APDU, the SW may indicate that there are response bytes |
| 134 | # available. There are two SWs commonly used for this 9fxx (sim) and 61xx (usim), where |
| 135 | # xx is the number of response bytes available. |
| 136 | # See also: |
| 137 | if (sw is not None): |
| 138 | if ((sw[0:2] == '9f') or (sw[0:2] == '61')): |
| 139 | # SW1=9F: 3GPP TS 51.011 9.4.1, Responses to commands which are correctly executed |
| 140 | # SW1=61: ISO/IEC 7816-4, Table 5 — General meaning of the interindustry values of SW1-SW2 |
| 141 | pdu_gr = pdu[0:2] + 'c00000' + sw[2:4] |
| 142 | data, sw = self.send_apdu_raw(pdu_gr) |
| 143 | if sw[0:2] == '6c': |
| 144 | # SW1=6C: ETSI TS 102 221 Table 7.1: Procedure byte coding |
| 145 | pdu_gr = pdu[0:8] + sw[2:4] |
| 146 | data, sw = self.send_apdu_raw(pdu_gr) |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 147 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 148 | return data, sw |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 149 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 150 | def send_apdu_checksw(self, pdu, sw="9000"): |
| 151 | """Sends an APDU and check returned SW |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 152 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 153 | Args: |
| 154 | pdu : string of hexadecimal characters (ex. "A0A40000023F00") |
| 155 | sw : string of 4 hexadecimal characters (ex. "9000"). The user may mask out certain |
| 156 | digits using a '?' to add some ambiguity if needed. |
| 157 | Returns: |
| 158 | tuple(data, sw), where |
| 159 | data : string (in hex) of returned data (ex. "074F4EFFFF") |
| 160 | sw : string (in hex) of status word (ex. "9000") |
| 161 | """ |
| 162 | rv = self.send_apdu(pdu) |
Philipp Maier | d4ebb6f | 2018-06-12 17:56:07 +0200 | [diff] [blame] | 163 | |
Harald Welte | fd476b4 | 2022-08-06 14:01:26 +0200 | [diff] [blame] | 164 | while sw == '9000' and sw_match(rv[1], '91xx'): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 165 | # proactive sim as per TS 102 221 Setion 7.4.2 |
| 166 | rv = self.send_apdu_checksw('80120000' + rv[1][2:], sw) |
Harald Welte | fd476b4 | 2022-08-06 14:01:26 +0200 | [diff] [blame] | 167 | print("FETCH: %s" % rv[0]) |
| 168 | if self.proactive_handler: |
| 169 | self.proactive_handler.receive_fetch_raw(rv[0]) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 170 | if not sw_match(rv[1], sw): |
| 171 | raise SwMatchError(rv[1], sw.lower(), self.sw_interpreter) |
| 172 | return rv |
Harald Welte | 6e0458d | 2021-04-03 11:52:37 +0200 | [diff] [blame] | 173 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 174 | def send_apdu_constr(self, cla, ins, p1, p2, cmd_constr, cmd_data, resp_constr): |
| 175 | """Build and sends an APDU using a 'construct' definition; parses response. |
Harald Welte | e0f9ef1 | 2021-04-10 17:22:35 +0200 | [diff] [blame] | 176 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 177 | Args: |
| 178 | cla : string (in hex) ISO 7816 class byte |
| 179 | ins : string (in hex) ISO 7816 instruction byte |
| 180 | p1 : string (in hex) ISO 7116 Parameter 1 byte |
| 181 | p2 : string (in hex) ISO 7116 Parameter 2 byte |
| 182 | cmd_cosntr : defining how to generate binary APDU command data |
| 183 | cmd_data : command data passed to cmd_constr |
| 184 | resp_cosntr : defining how to decode binary APDU response data |
| 185 | Returns: |
| 186 | Tuple of (decoded_data, sw) |
| 187 | """ |
| 188 | cmd = cmd_constr.build(cmd_data) if cmd_data else '' |
| 189 | p3 = i2h([len(cmd)]) |
| 190 | pdu = ''.join([cla, ins, p1, p2, p3, b2h(cmd)]) |
| 191 | (data, sw) = self.send_apdu(pdu) |
| 192 | if data: |
| 193 | # filter the resulting dict to avoid '_io' members inside |
| 194 | rsp = filter_dict(resp_constr.parse(h2b(data))) |
| 195 | else: |
| 196 | rsp = None |
| 197 | return (rsp, sw) |
Harald Welte | e0f9ef1 | 2021-04-10 17:22:35 +0200 | [diff] [blame] | 198 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 199 | def send_apdu_constr_checksw(self, cla, ins, p1, p2, cmd_constr, cmd_data, resp_constr, |
| 200 | sw_exp="9000"): |
| 201 | """Build and sends an APDU using a 'construct' definition; parses response. |
Harald Welte | e0f9ef1 | 2021-04-10 17:22:35 +0200 | [diff] [blame] | 202 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 203 | Args: |
| 204 | cla : string (in hex) ISO 7816 class byte |
| 205 | ins : string (in hex) ISO 7816 instruction byte |
| 206 | p1 : string (in hex) ISO 7116 Parameter 1 byte |
| 207 | p2 : string (in hex) ISO 7116 Parameter 2 byte |
| 208 | cmd_cosntr : defining how to generate binary APDU command data |
| 209 | cmd_data : command data passed to cmd_constr |
| 210 | resp_cosntr : defining how to decode binary APDU response data |
| 211 | exp_sw : string (in hex) of status word (ex. "9000") |
| 212 | Returns: |
| 213 | Tuple of (decoded_data, sw) |
| 214 | """ |
| 215 | (rsp, sw) = self.send_apdu_constr(cla, ins, |
| 216 | p1, p2, cmd_constr, cmd_data, resp_constr) |
| 217 | if not sw_match(sw, sw_exp): |
| 218 | raise SwMatchError(sw, sw_exp.lower(), self.sw_interpreter) |
| 219 | return (rsp, sw) |
| 220 | |
Harald Welte | e0f9ef1 | 2021-04-10 17:22:35 +0200 | [diff] [blame] | 221 | |
Harald Welte | 28c2431 | 2021-04-11 12:19:36 +0200 | [diff] [blame] | 222 | def argparse_add_reader_args(arg_parser): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 223 | """Add all reader related arguments to the given argparse.Argumentparser instance.""" |
| 224 | serial_group = arg_parser.add_argument_group('Serial Reader') |
| 225 | serial_group.add_argument('-d', '--device', metavar='DEV', default='/dev/ttyUSB0', |
| 226 | help='Serial Device for SIM access') |
| 227 | serial_group.add_argument('-b', '--baud', dest='baudrate', type=int, metavar='BAUD', default=9600, |
| 228 | help='Baud rate used for SIM access') |
Harald Welte | 28c2431 | 2021-04-11 12:19:36 +0200 | [diff] [blame] | 229 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 230 | pcsc_group = arg_parser.add_argument_group('PC/SC Reader') |
| 231 | pcsc_group.add_argument('-p', '--pcsc-device', type=int, dest='pcsc_dev', metavar='PCSC', default=None, |
| 232 | help='PC/SC reader number to use for SIM access') |
Harald Welte | 28c2431 | 2021-04-11 12:19:36 +0200 | [diff] [blame] | 233 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 234 | modem_group = arg_parser.add_argument_group('AT Command Modem Reader') |
| 235 | modem_group.add_argument('--modem-device', dest='modem_dev', metavar='DEV', default=None, |
| 236 | help='Serial port of modem for Generic SIM Access (3GPP TS 27.007)') |
| 237 | modem_group.add_argument('--modem-baud', type=int, metavar='BAUD', default=115200, |
| 238 | help='Baud rate used for modem port') |
Harald Welte | 28c2431 | 2021-04-11 12:19:36 +0200 | [diff] [blame] | 239 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 240 | osmobb_group = arg_parser.add_argument_group('OsmocomBB Reader') |
| 241 | osmobb_group.add_argument('--osmocon', dest='osmocon_sock', metavar='PATH', default=None, |
| 242 | help='Socket path for Calypso (e.g. Motorola C1XX) based reader (via OsmocomBB)') |
Harald Welte | 28c2431 | 2021-04-11 12:19:36 +0200 | [diff] [blame] | 243 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 244 | return arg_parser |
| 245 | |
Harald Welte | 28c2431 | 2021-04-11 12:19:36 +0200 | [diff] [blame] | 246 | |
Harald Welte | eb05b2f | 2021-04-10 11:01:56 +0200 | [diff] [blame] | 247 | def init_reader(opts, **kwargs) -> Optional[LinkBase]: |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 248 | """ |
| 249 | Init card reader driver |
| 250 | """ |
| 251 | sl = None # type : :Optional[LinkBase] |
| 252 | try: |
| 253 | if opts.pcsc_dev is not None: |
| 254 | print("Using PC/SC reader interface") |
| 255 | from pySim.transport.pcsc import PcscSimLink |
| 256 | sl = PcscSimLink(opts.pcsc_dev, **kwargs) |
| 257 | elif opts.osmocon_sock is not None: |
| 258 | print("Using Calypso-based (OsmocomBB) reader interface") |
| 259 | from pySim.transport.calypso import CalypsoSimLink |
| 260 | sl = CalypsoSimLink(sock_path=opts.osmocon_sock, **kwargs) |
| 261 | elif opts.modem_dev is not None: |
| 262 | print("Using modem for Generic SIM Access (3GPP TS 27.007)") |
| 263 | from pySim.transport.modem_atcmd import ModemATCommandLink |
| 264 | sl = ModemATCommandLink( |
| 265 | device=opts.modem_dev, baudrate=opts.modem_baud, **kwargs) |
| 266 | else: # Serial reader is default |
| 267 | print("Using serial reader interface") |
| 268 | from pySim.transport.serial import SerialSimLink |
| 269 | sl = SerialSimLink(device=opts.device, |
| 270 | baudrate=opts.baudrate, **kwargs) |
| 271 | return sl |
| 272 | except Exception as e: |
| 273 | if str(e): |
| 274 | print("Card reader initialization failed with exception:\n" + str(e)) |
| 275 | else: |
| 276 | print( |
| 277 | "Card reader initialization failed with an exception of type:\n" + str(type(e))) |
| 278 | return None |