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 | |
Harald Welte | 6e0458d | 2021-04-03 11:52:37 +0200 | [diff] [blame] | 6 | from typing import Optional |
| 7 | |
Harald Welte | e79cc80 | 2021-01-21 14:10:43 +0100 | [diff] [blame] | 8 | from pySim.exceptions import * |
Harald Welte | e0f9ef1 | 2021-04-10 17:22:35 +0200 | [diff] [blame^] | 9 | from pySim.construct import filter_dict |
| 10 | from pySim.utils import sw_match, b2h, h2b, i2h |
Harald Welte | e79cc80 | 2021-01-21 14:10:43 +0100 | [diff] [blame] | 11 | |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 12 | # |
| 13 | # Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com> |
Harald Welte | e0f9ef1 | 2021-04-10 17:22:35 +0200 | [diff] [blame^] | 14 | # Copyright (C) 2021 Harald Welte <laforge@osmocom.org> |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 15 | # |
| 16 | # This program is free software: you can redistribute it and/or modify |
| 17 | # it under the terms of the GNU General Public License as published by |
| 18 | # the Free Software Foundation, either version 2 of the License, or |
| 19 | # (at your option) any later version. |
| 20 | # |
| 21 | # This program is distributed in the hope that it will be useful, |
| 22 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | # GNU General Public License for more details. |
| 25 | # |
| 26 | # You should have received a copy of the GNU General Public License |
| 27 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | # |
| 29 | |
Harald Welte | 7829d8a | 2021-04-10 11:28:53 +0200 | [diff] [blame] | 30 | class ApduTracer: |
| 31 | def trace_command(self, cmd): |
| 32 | pass |
| 33 | |
| 34 | def trace_response(self, cmd, sw, resp): |
| 35 | pass |
| 36 | |
| 37 | |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 38 | class LinkBase(object): |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 39 | """Base class for link/transport to card.""" |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 40 | |
Harald Welte | 7829d8a | 2021-04-10 11:28:53 +0200 | [diff] [blame] | 41 | def __init__(self, sw_interpreter=None, apdu_tracer=None): |
Harald Welte | eb05b2f | 2021-04-10 11:01:56 +0200 | [diff] [blame] | 42 | self.sw_interpreter = sw_interpreter |
Harald Welte | 7829d8a | 2021-04-10 11:28:53 +0200 | [diff] [blame] | 43 | self.apdu_tracer = apdu_tracer |
Harald Welte | 4f2c546 | 2021-04-03 11:48:22 +0200 | [diff] [blame] | 44 | |
| 45 | def set_sw_interpreter(self, interp): |
| 46 | """Set an (optional) status word interpreter.""" |
| 47 | self.sw_interpreter = interp |
| 48 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 49 | def wait_for_card(self, timeout:int=None, newcardonly:bool=False): |
| 50 | """Wait for a card and connect to it |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 51 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 52 | Args: |
| 53 | timeout : Maximum wait time in seconds (None=no timeout) |
| 54 | newcardonly : Should we wait for a new card, or an already inserted one ? |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 55 | """ |
| 56 | pass |
| 57 | |
| 58 | def connect(self): |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 59 | """Connect to a card immediately |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 60 | """ |
| 61 | pass |
| 62 | |
| 63 | def disconnect(self): |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 64 | """Disconnect from card |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 65 | """ |
| 66 | pass |
| 67 | |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 68 | def reset_card(self): |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 69 | """Resets the card (power down/up) |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 70 | """ |
| 71 | pass |
| 72 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 73 | def send_apdu_raw(self, pdu:str): |
| 74 | """Sends an APDU with minimal processing |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 75 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 76 | Args: |
| 77 | pdu : string of hexadecimal characters (ex. "A0A40000023F00") |
| 78 | Returns: |
| 79 | tuple(data, sw), where |
| 80 | data : string (in hex) of returned data (ex. "074F4EFFFF") |
| 81 | sw : string (in hex) of status word (ex. "9000") |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 82 | """ |
Harald Welte | 7829d8a | 2021-04-10 11:28:53 +0200 | [diff] [blame] | 83 | if self.apdu_tracer: |
| 84 | self.apdu_tracer.trace_command(pdu) |
| 85 | (data, sw) = self._send_apdu_raw(pdu) |
| 86 | if self.apdu_tracer: |
| 87 | self.apdu_tracer.trace_response(pdu, sw, data) |
| 88 | return (data, sw) |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 89 | |
| 90 | def send_apdu(self, pdu): |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 91 | """Sends an APDU and auto fetch response data |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 92 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 93 | Args: |
| 94 | pdu : string of hexadecimal characters (ex. "A0A40000023F00") |
| 95 | Returns: |
| 96 | tuple(data, sw), where |
| 97 | data : string (in hex) of returned data (ex. "074F4EFFFF") |
| 98 | sw : string (in hex) of status word (ex. "9000") |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 99 | """ |
| 100 | data, sw = self.send_apdu_raw(pdu) |
| 101 | |
Philipp Maier | 859e0fd | 2018-06-12 18:40:24 +0200 | [diff] [blame] | 102 | # When whe have sent the first APDU, the SW may indicate that there are response bytes |
| 103 | # available. There are two SWs commonly used for this 9fxx (sim) and 61xx (usim), where |
| 104 | # xx is the number of response bytes available. |
| 105 | # See also: |
| 106 | # SW1=9F: 3GPP TS 51.011 9.4.1, Responses to commands which are correctly executed |
| 107 | # SW1=61: ISO/IEC 7816-4, Table 5 — General meaning of the interindustry values of SW1-SW2 |
| 108 | if (sw is not None) and ((sw[0:2] == '9f') or (sw[0:2] == '61')): |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 109 | pdu_gr = pdu[0:2] + 'c00000' + sw[2:4] |
| 110 | data, sw = self.send_apdu_raw(pdu_gr) |
| 111 | |
| 112 | return data, sw |
| 113 | |
| 114 | def send_apdu_checksw(self, pdu, sw="9000"): |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 115 | """Sends an APDU and check returned SW |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 116 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 117 | Args: |
| 118 | pdu : string of hexadecimal characters (ex. "A0A40000023F00") |
| 119 | sw : string of 4 hexadecimal characters (ex. "9000"). The user may mask out certain |
| 120 | digits using a '?' to add some ambiguity if needed. |
| 121 | Returns: |
| 122 | tuple(data, sw), where |
| 123 | data : string (in hex) of returned data (ex. "074F4EFFFF") |
| 124 | sw : string (in hex) of status word (ex. "9000") |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 125 | """ |
| 126 | rv = self.send_apdu(pdu) |
Philipp Maier | d4ebb6f | 2018-06-12 17:56:07 +0200 | [diff] [blame] | 127 | |
Harald Welte | 67d551a | 2021-01-21 14:50:01 +0100 | [diff] [blame] | 128 | if not sw_match(rv[1], sw): |
Harald Welte | 4f2c546 | 2021-04-03 11:48:22 +0200 | [diff] [blame] | 129 | raise SwMatchError(rv[1], sw.lower(), self.sw_interpreter) |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 130 | return rv |
Harald Welte | 6e0458d | 2021-04-03 11:52:37 +0200 | [diff] [blame] | 131 | |
Harald Welte | e0f9ef1 | 2021-04-10 17:22:35 +0200 | [diff] [blame^] | 132 | def send_apdu_constr(self, cla, ins, p1, p2, cmd_constr, cmd_data, resp_constr): |
| 133 | """Build and sends an APDU using a 'construct' definition; parses response. |
| 134 | |
| 135 | Args: |
| 136 | cla : string (in hex) ISO 7816 class byte |
| 137 | ins : string (in hex) ISO 7816 instruction byte |
| 138 | p1 : string (in hex) ISO 7116 Parameter 1 byte |
| 139 | p2 : string (in hex) ISO 7116 Parameter 2 byte |
| 140 | cmd_cosntr : defining how to generate binary APDU command data |
| 141 | cmd_data : command data passed to cmd_constr |
| 142 | resp_cosntr : defining how to decode binary APDU response data |
| 143 | Returns: |
| 144 | Tuple of (decoded_data, sw) |
| 145 | """ |
| 146 | cmd = cmd_constr.build(cmd_data) if cmd_data else '' |
| 147 | p3 = i2h([len(cmd)]) |
| 148 | pdu = ''.join([cla, ins, p1, p2, p3, b2h(cmd)]) |
| 149 | (data, sw) = self.send_apdu(pdu) |
| 150 | if data: |
| 151 | # filter the resulting dict to avoid '_io' members inside |
| 152 | rsp = filter_dict(resp_constr.parse(h2b(data))) |
| 153 | else: |
| 154 | rsp = None |
| 155 | return (rsp, sw) |
| 156 | |
| 157 | def send_apdu_constr_checksw(self, cla, ins, p1, p2, cmd_constr, cmd_data, resp_constr, |
| 158 | sw_exp="9000"): |
| 159 | """Build and sends an APDU using a 'construct' definition; parses response. |
| 160 | |
| 161 | Args: |
| 162 | cla : string (in hex) ISO 7816 class byte |
| 163 | ins : string (in hex) ISO 7816 instruction byte |
| 164 | p1 : string (in hex) ISO 7116 Parameter 1 byte |
| 165 | p2 : string (in hex) ISO 7116 Parameter 2 byte |
| 166 | cmd_cosntr : defining how to generate binary APDU command data |
| 167 | cmd_data : command data passed to cmd_constr |
| 168 | resp_cosntr : defining how to decode binary APDU response data |
| 169 | exp_sw : string (in hex) of status word (ex. "9000") |
| 170 | Returns: |
| 171 | Tuple of (decoded_data, sw) |
| 172 | """ |
| 173 | (rsp, sw) = self.send_apdu_constr(cla, ins, p1, p2, cmd_constr, cmd_data, resp_constr) |
| 174 | if not sw_match(sw, sw_exp): |
| 175 | raise SwMatchError(sw, sw_exp.lower(), self.sw_interpreter) |
| 176 | return (rsp, sw) |
| 177 | |
Harald Welte | eb05b2f | 2021-04-10 11:01:56 +0200 | [diff] [blame] | 178 | def init_reader(opts, **kwargs) -> Optional[LinkBase]: |
Harald Welte | 6e0458d | 2021-04-03 11:52:37 +0200 | [diff] [blame] | 179 | """ |
| 180 | Init card reader driver |
| 181 | """ |
| 182 | sl = None # type : :Optional[LinkBase] |
| 183 | try: |
| 184 | if opts.pcsc_dev is not None: |
| 185 | print("Using PC/SC reader interface") |
| 186 | from pySim.transport.pcsc import PcscSimLink |
Harald Welte | eb05b2f | 2021-04-10 11:01:56 +0200 | [diff] [blame] | 187 | sl = PcscSimLink(opts.pcsc_dev, **kwargs) |
Harald Welte | 6e0458d | 2021-04-03 11:52:37 +0200 | [diff] [blame] | 188 | elif opts.osmocon_sock is not None: |
| 189 | print("Using Calypso-based (OsmocomBB) reader interface") |
| 190 | from pySim.transport.calypso import CalypsoSimLink |
Harald Welte | eb05b2f | 2021-04-10 11:01:56 +0200 | [diff] [blame] | 191 | sl = CalypsoSimLink(sock_path=opts.osmocon_sock, **kwargs) |
Harald Welte | 6e0458d | 2021-04-03 11:52:37 +0200 | [diff] [blame] | 192 | elif opts.modem_dev is not None: |
| 193 | print("Using modem for Generic SIM Access (3GPP TS 27.007)") |
| 194 | from pySim.transport.modem_atcmd import ModemATCommandLink |
Harald Welte | eb05b2f | 2021-04-10 11:01:56 +0200 | [diff] [blame] | 195 | sl = ModemATCommandLink(device=opts.modem_dev, baudrate=opts.modem_baud, **kwargs) |
Harald Welte | 6e0458d | 2021-04-03 11:52:37 +0200 | [diff] [blame] | 196 | else: # Serial reader is default |
| 197 | print("Using serial reader interface") |
| 198 | from pySim.transport.serial import SerialSimLink |
Harald Welte | eb05b2f | 2021-04-10 11:01:56 +0200 | [diff] [blame] | 199 | sl = SerialSimLink(device=opts.device, baudrate=opts.baudrate, **kwargs) |
Harald Welte | 6e0458d | 2021-04-03 11:52:37 +0200 | [diff] [blame] | 200 | return sl |
| 201 | except Exception as e: |
| 202 | print("Card reader initialization failed with exception:\n" + str(e)) |
| 203 | return None |