blob: 1dd8d18cd5fe27ff16a27dd93d1df49a71fa1ab7 [file] [log] [blame]
Sylvain Munaute7c15cd2010-12-07 10:01:55 +01001# -*- coding: utf-8 -*-
2
3""" pySim: PCSC reader transport link base
4"""
5
Vadim Yanitskiye8c34ca2021-05-02 02:29:52 +02006import abc
Harald Welte28c24312021-04-11 12:19:36 +02007import argparse
Vadim Yanitskiye8c34ca2021-05-02 02:29:52 +02008from typing import Optional, Tuple
Harald Welteab6897c2023-07-09 16:21:23 +02009from construct import Construct
Harald Welte6e0458d2021-04-03 11:52:37 +020010
Harald Weltee79cc802021-01-21 14:10:43 +010011from pySim.exceptions import *
Harald Weltee0f9ef12021-04-10 17:22:35 +020012from pySim.construct import filter_dict
Harald Weltef9f8d7a2023-07-09 17:06:16 +020013from pySim.utils import sw_match, b2h, h2b, i2h, Hexstr, SwHexstr, SwMatchstr, ResTuple
Christian Amsüss59f3b112022-08-12 15:46:52 +020014from pySim.cat import ProactiveCommand, CommandDetails, DeviceIdentities, Result
Harald Weltee79cc802021-01-21 14:10:43 +010015
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010016#
17# Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com>
Harald Weltef9f8d7a2023-07-09 17:06:16 +020018# Copyright (C) 2021-2023 Harald Welte <laforge@osmocom.org>
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010019#
20# This program is free software: you can redistribute it and/or modify
21# it under the terms of the GNU General Public License as published by
22# the Free Software Foundation, either version 2 of the License, or
23# (at your option) any later version.
24#
25# This program is distributed in the hope that it will be useful,
26# but WITHOUT ANY WARRANTY; without even the implied warranty of
27# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28# GNU General Public License for more details.
29#
30# You should have received a copy of the GNU General Public License
31# along with this program. If not, see <http://www.gnu.org/licenses/>.
32#
33
Harald Welte7829d8a2021-04-10 11:28:53 +020034
Harald Weltec91085e2022-02-10 18:05:45 +010035class ApduTracer:
36 def trace_command(self, cmd):
37 pass
38
39 def trace_response(self, cmd, sw, resp):
40 pass
Harald Welte7829d8a2021-04-10 11:28:53 +020041
Harald Weltefd476b42022-08-06 14:01:26 +020042class ProactiveHandler(abc.ABC):
43 """Abstract base class representing the interface of some code that handles
44 the proactive commands, as returned by the card in responses to the FETCH
45 command."""
Christian Amsüss59f3b112022-08-12 15:46:52 +020046 def receive_fetch_raw(self, pcmd: ProactiveCommand, parsed: Hexstr):
Harald Weltefd476b42022-08-06 14:01:26 +020047 # try to find a generic handler like handle_SendShortMessage
48 handle_name = 'handle_%s' % type(parsed).__name__
49 if hasattr(self, handle_name):
50 handler = getattr(self, handle_name)
51 return handler(pcmd.decoded)
52 # fall back to common handler
53 return self.receive_fetch(pcmd)
54
55 def receive_fetch(self, pcmd: ProactiveCommand):
56 """Default handler for not otherwise handled proactive commands."""
57 raise NotImplementedError('No handler method for %s' % pcmd.decoded)
58
59
Harald Welte7829d8a2021-04-10 11:28:53 +020060
Vadim Yanitskiye8c34ca2021-05-02 02:29:52 +020061class LinkBase(abc.ABC):
Harald Weltec91085e2022-02-10 18:05:45 +010062 """Base class for link/transport to card."""
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010063
Harald Welteab6897c2023-07-09 16:21:23 +020064 def __init__(self, sw_interpreter=None, apdu_tracer: Optional[ApduTracer]=None,
Harald Weltefd476b42022-08-06 14:01:26 +020065 proactive_handler: Optional[ProactiveHandler]=None):
Harald Weltec91085e2022-02-10 18:05:45 +010066 self.sw_interpreter = sw_interpreter
67 self.apdu_tracer = apdu_tracer
Harald Weltefd476b42022-08-06 14:01:26 +020068 self.proactive_handler = proactive_handler
Harald Welte4f2c5462021-04-03 11:48:22 +020069
Harald Weltec91085e2022-02-10 18:05:45 +010070 @abc.abstractmethod
Philipp Maier6bfa8a82023-10-09 13:32:49 +020071 def __str__(self):
72 """Implementation specific method for printing an information to identify the device."""
73
74 @abc.abstractmethod
Harald Weltef9f8d7a2023-07-09 17:06:16 +020075 def _send_apdu_raw(self, pdu: Hexstr) -> ResTuple:
Harald Weltec91085e2022-02-10 18:05:45 +010076 """Implementation specific method for sending the PDU."""
Vadim Yanitskiye8c34ca2021-05-02 02:29:52 +020077
Harald Weltec91085e2022-02-10 18:05:45 +010078 def set_sw_interpreter(self, interp):
79 """Set an (optional) status word interpreter."""
80 self.sw_interpreter = interp
Harald Welte4f2c5462021-04-03 11:48:22 +020081
Harald Weltec91085e2022-02-10 18:05:45 +010082 @abc.abstractmethod
Harald Welteab6897c2023-07-09 16:21:23 +020083 def wait_for_card(self, timeout: Optional[int] = None, newcardonly: bool = False):
Harald Weltec91085e2022-02-10 18:05:45 +010084 """Wait for a card and connect to it
Sylvain Munautbdca2522010-12-09 13:31:58 +010085
Harald Weltec91085e2022-02-10 18:05:45 +010086 Args:
87 timeout : Maximum wait time in seconds (None=no timeout)
88 newcardonly : Should we wait for a new card, or an already inserted one ?
89 """
Sylvain Munautbdca2522010-12-09 13:31:58 +010090
Harald Weltec91085e2022-02-10 18:05:45 +010091 @abc.abstractmethod
92 def connect(self):
93 """Connect to a card immediately
94 """
Sylvain Munautbdca2522010-12-09 13:31:58 +010095
Harald Weltec91085e2022-02-10 18:05:45 +010096 @abc.abstractmethod
97 def disconnect(self):
98 """Disconnect from card
99 """
Sylvain Munautbdca2522010-12-09 13:31:58 +0100100
Harald Weltec91085e2022-02-10 18:05:45 +0100101 @abc.abstractmethod
102 def reset_card(self):
103 """Resets the card (power down/up)
104 """
Sylvain Munaute7c15cd2010-12-07 10:01:55 +0100105
Harald Weltef9f8d7a2023-07-09 17:06:16 +0200106 def send_apdu_raw(self, pdu: Hexstr) -> ResTuple:
Harald Weltec91085e2022-02-10 18:05:45 +0100107 """Sends an APDU with minimal processing
Sylvain Munaute7c15cd2010-12-07 10:01:55 +0100108
Harald Weltec91085e2022-02-10 18:05:45 +0100109 Args:
110 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
111 Returns:
112 tuple(data, sw), where
113 data : string (in hex) of returned data (ex. "074F4EFFFF")
114 sw : string (in hex) of status word (ex. "9000")
115 """
116 if self.apdu_tracer:
117 self.apdu_tracer.trace_command(pdu)
118 (data, sw) = self._send_apdu_raw(pdu)
119 if self.apdu_tracer:
120 self.apdu_tracer.trace_response(pdu, sw, data)
121 return (data, sw)
Sylvain Munaute7c15cd2010-12-07 10:01:55 +0100122
Harald Weltef9f8d7a2023-07-09 17:06:16 +0200123 def send_apdu(self, pdu: Hexstr) -> ResTuple:
Harald Weltec91085e2022-02-10 18:05:45 +0100124 """Sends an APDU and auto fetch response data
Sylvain Munaute7c15cd2010-12-07 10:01:55 +0100125
Harald Weltec91085e2022-02-10 18:05:45 +0100126 Args:
127 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
128 Returns:
129 tuple(data, sw), where
130 data : string (in hex) of returned data (ex. "074F4EFFFF")
131 sw : string (in hex) of status word (ex. "9000")
132 """
133 data, sw = self.send_apdu_raw(pdu)
Sylvain Munaute7c15cd2010-12-07 10:01:55 +0100134
Harald Weltec91085e2022-02-10 18:05:45 +0100135 # When we have sent the first APDU, the SW may indicate that there are response bytes
136 # available. There are two SWs commonly used for this 9fxx (sim) and 61xx (usim), where
137 # xx is the number of response bytes available.
138 # See also:
139 if (sw is not None):
140 if ((sw[0:2] == '9f') or (sw[0:2] == '61')):
141 # SW1=9F: 3GPP TS 51.011 9.4.1, Responses to commands which are correctly executed
142 # SW1=61: ISO/IEC 7816-4, Table 5 — General meaning of the interindustry values of SW1-SW2
143 pdu_gr = pdu[0:2] + 'c00000' + sw[2:4]
144 data, sw = self.send_apdu_raw(pdu_gr)
145 if sw[0:2] == '6c':
146 # SW1=6C: ETSI TS 102 221 Table 7.1: Procedure byte coding
147 pdu_gr = pdu[0:8] + sw[2:4]
148 data, sw = self.send_apdu_raw(pdu_gr)
Sylvain Munaute7c15cd2010-12-07 10:01:55 +0100149
Harald Weltec91085e2022-02-10 18:05:45 +0100150 return data, sw
Sylvain Munaute7c15cd2010-12-07 10:01:55 +0100151
Harald Weltef9f8d7a2023-07-09 17:06:16 +0200152 def send_apdu_checksw(self, pdu: Hexstr, sw: SwMatchstr = "9000") -> ResTuple:
Harald Weltec91085e2022-02-10 18:05:45 +0100153 """Sends an APDU and check returned SW
Sylvain Munaute7c15cd2010-12-07 10:01:55 +0100154
Harald Weltec91085e2022-02-10 18:05:45 +0100155 Args:
156 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
157 sw : string of 4 hexadecimal characters (ex. "9000"). The user may mask out certain
158 digits using a '?' to add some ambiguity if needed.
159 Returns:
160 tuple(data, sw), where
161 data : string (in hex) of returned data (ex. "074F4EFFFF")
162 sw : string (in hex) of status word (ex. "9000")
163 """
164 rv = self.send_apdu(pdu)
Christian Amsüss98552ef2022-08-11 19:29:37 +0200165 last_sw = rv[1]
Philipp Maierd4ebb6f2018-06-12 17:56:07 +0200166
Christian Amsüss98552ef2022-08-11 19:29:37 +0200167 while sw == '9000' and sw_match(last_sw, '91xx'):
168 # It *was* successful after all -- the extra pieces FETCH handled
169 # need not concern the caller.
170 rv = (rv[0], '9000')
Harald Weltec91085e2022-02-10 18:05:45 +0100171 # proactive sim as per TS 102 221 Setion 7.4.2
Christian Amsüss59f3b112022-08-12 15:46:52 +0200172 # TODO: Check SW manually to avoid recursing on the stack (provided this piece of code stays in this place)
Christian Amsüss98552ef2022-08-11 19:29:37 +0200173 fetch_rv = self.send_apdu_checksw('80120000' + last_sw[2:], sw)
Christian Amsüss59f3b112022-08-12 15:46:52 +0200174 # Setting this in case we later decide not to send a terminal
175 # response immediately unconditionally -- the card may still have
176 # something pending even though the last command was not processed
177 # yet.
Christian Amsüss98552ef2022-08-11 19:29:37 +0200178 last_sw = fetch_rv[1]
Christian Amsüss59f3b112022-08-12 15:46:52 +0200179 # parse the proactive command
180 pcmd = ProactiveCommand()
181 parsed = pcmd.from_tlv(h2b(fetch_rv[0]))
182 print("FETCH: %s (%s)" % (fetch_rv[0], type(parsed).__name__))
183 result = Result()
Harald Weltefd476b42022-08-06 14:01:26 +0200184 if self.proactive_handler:
Christian Amsüss59f3b112022-08-12 15:46:52 +0200185 # Extension point: If this does return a list of TLV objects,
186 # they could be appended after the Result; if the first is a
187 # Result, that cuold replace the one built here.
188 self.proactive_handler.receive_fetch_raw(pcmd, parsed)
189 result.from_dict({'general_result': 'performed_successfully', 'additional_information': ''})
190 else:
191 result.from_dict({'general_result': 'command_beyond_terminal_capability', 'additional_information': ''})
192
193 # Send response immediately, thus also flushing out any further
194 # proactive commands that the card already wants to send
195 #
196 # Structure as per TS 102 223 V4.4.0 Section 6.8
197
198 # The Command Details are echoed from the command that has been processed.
199 (command_details,) = [c for c in pcmd.decoded.children if isinstance(c, CommandDetails)]
200 # The Device Identities are fixed. (TS 102 223 V4.0.0 Section 6.8.2)
201 device_identities = DeviceIdentities()
202 device_identities.from_dict({'source_dev_id': 'terminal', 'dest_dev_id': 'uicc'})
203
204 # Testing hint: The value of tail does not influence the behavior
205 # of an SJA2 that sent ans SMS, so this is implemented only
206 # following TS 102 223, and not fully tested.
207 tail = command_details.to_tlv() + device_identities.to_tlv() + result.to_tlv()
208 # Testing hint: In contrast to the above, this part is positively
209 # essential to get the SJA2 to provide the later parts of a
210 # multipart SMS in response to an OTA RFM command.
211 terminal_response = '80140000' + b2h(len(tail).to_bytes(1, 'big') + tail)
212
213 terminal_response_rv = self.send_apdu(terminal_response)
214 last_sw = terminal_response_rv[1]
215
Harald Weltec91085e2022-02-10 18:05:45 +0100216 if not sw_match(rv[1], sw):
217 raise SwMatchError(rv[1], sw.lower(), self.sw_interpreter)
218 return rv
Harald Welte6e0458d2021-04-03 11:52:37 +0200219
Harald Welteab6897c2023-07-09 16:21:23 +0200220 def send_apdu_constr(self, cla: Hexstr, ins: Hexstr, p1: Hexstr, p2: Hexstr, cmd_constr: Construct,
221 cmd_data: Hexstr, resp_constr: Construct) -> Tuple[dict, SwHexstr]:
Harald Weltec91085e2022-02-10 18:05:45 +0100222 """Build and sends an APDU using a 'construct' definition; parses response.
Harald Weltee0f9ef12021-04-10 17:22:35 +0200223
Harald Weltec91085e2022-02-10 18:05:45 +0100224 Args:
225 cla : string (in hex) ISO 7816 class byte
226 ins : string (in hex) ISO 7816 instruction byte
227 p1 : string (in hex) ISO 7116 Parameter 1 byte
228 p2 : string (in hex) ISO 7116 Parameter 2 byte
229 cmd_cosntr : defining how to generate binary APDU command data
230 cmd_data : command data passed to cmd_constr
231 resp_cosntr : defining how to decode binary APDU response data
232 Returns:
233 Tuple of (decoded_data, sw)
234 """
235 cmd = cmd_constr.build(cmd_data) if cmd_data else ''
236 p3 = i2h([len(cmd)])
237 pdu = ''.join([cla, ins, p1, p2, p3, b2h(cmd)])
238 (data, sw) = self.send_apdu(pdu)
239 if data:
240 # filter the resulting dict to avoid '_io' members inside
241 rsp = filter_dict(resp_constr.parse(h2b(data)))
242 else:
243 rsp = None
244 return (rsp, sw)
Harald Weltee0f9ef12021-04-10 17:22:35 +0200245
Harald Welteab6897c2023-07-09 16:21:23 +0200246 def send_apdu_constr_checksw(self, cla: Hexstr, ins: Hexstr, p1: Hexstr, p2: Hexstr,
247 cmd_constr: Construct, cmd_data: Hexstr, resp_constr: Construct,
248 sw_exp: SwMatchstr="9000") -> Tuple[dict, SwHexstr]:
Harald Weltec91085e2022-02-10 18:05:45 +0100249 """Build and sends an APDU using a 'construct' definition; parses response.
Harald Weltee0f9ef12021-04-10 17:22:35 +0200250
Harald Weltec91085e2022-02-10 18:05:45 +0100251 Args:
252 cla : string (in hex) ISO 7816 class byte
253 ins : string (in hex) ISO 7816 instruction byte
254 p1 : string (in hex) ISO 7116 Parameter 1 byte
255 p2 : string (in hex) ISO 7116 Parameter 2 byte
256 cmd_cosntr : defining how to generate binary APDU command data
257 cmd_data : command data passed to cmd_constr
258 resp_cosntr : defining how to decode binary APDU response data
259 exp_sw : string (in hex) of status word (ex. "9000")
260 Returns:
261 Tuple of (decoded_data, sw)
262 """
263 (rsp, sw) = self.send_apdu_constr(cla, ins,
264 p1, p2, cmd_constr, cmd_data, resp_constr)
265 if not sw_match(sw, sw_exp):
266 raise SwMatchError(sw, sw_exp.lower(), self.sw_interpreter)
267 return (rsp, sw)
268
Harald Weltee0f9ef12021-04-10 17:22:35 +0200269
Harald Welte28c24312021-04-11 12:19:36 +0200270def argparse_add_reader_args(arg_parser):
Harald Weltec91085e2022-02-10 18:05:45 +0100271 """Add all reader related arguments to the given argparse.Argumentparser instance."""
272 serial_group = arg_parser.add_argument_group('Serial Reader')
273 serial_group.add_argument('-d', '--device', metavar='DEV', default='/dev/ttyUSB0',
274 help='Serial Device for SIM access')
275 serial_group.add_argument('-b', '--baud', dest='baudrate', type=int, metavar='BAUD', default=9600,
276 help='Baud rate used for SIM access')
Harald Welte28c24312021-04-11 12:19:36 +0200277
Harald Weltec91085e2022-02-10 18:05:45 +0100278 pcsc_group = arg_parser.add_argument_group('PC/SC Reader')
279 pcsc_group.add_argument('-p', '--pcsc-device', type=int, dest='pcsc_dev', metavar='PCSC', default=None,
280 help='PC/SC reader number to use for SIM access')
Harald Welte28c24312021-04-11 12:19:36 +0200281
Harald Weltec91085e2022-02-10 18:05:45 +0100282 modem_group = arg_parser.add_argument_group('AT Command Modem Reader')
283 modem_group.add_argument('--modem-device', dest='modem_dev', metavar='DEV', default=None,
284 help='Serial port of modem for Generic SIM Access (3GPP TS 27.007)')
285 modem_group.add_argument('--modem-baud', type=int, metavar='BAUD', default=115200,
286 help='Baud rate used for modem port')
Harald Welte28c24312021-04-11 12:19:36 +0200287
Harald Weltec91085e2022-02-10 18:05:45 +0100288 osmobb_group = arg_parser.add_argument_group('OsmocomBB Reader')
289 osmobb_group.add_argument('--osmocon', dest='osmocon_sock', metavar='PATH', default=None,
290 help='Socket path for Calypso (e.g. Motorola C1XX) based reader (via OsmocomBB)')
Harald Welte28c24312021-04-11 12:19:36 +0200291
Harald Weltec91085e2022-02-10 18:05:45 +0100292 return arg_parser
293
Harald Welte28c24312021-04-11 12:19:36 +0200294
Harald Welteeb05b2f2021-04-10 11:01:56 +0200295def init_reader(opts, **kwargs) -> Optional[LinkBase]:
Harald Weltec91085e2022-02-10 18:05:45 +0100296 """
297 Init card reader driver
298 """
299 sl = None # type : :Optional[LinkBase]
300 try:
301 if opts.pcsc_dev is not None:
302 print("Using PC/SC reader interface")
303 from pySim.transport.pcsc import PcscSimLink
304 sl = PcscSimLink(opts.pcsc_dev, **kwargs)
305 elif opts.osmocon_sock is not None:
306 print("Using Calypso-based (OsmocomBB) reader interface")
307 from pySim.transport.calypso import CalypsoSimLink
308 sl = CalypsoSimLink(sock_path=opts.osmocon_sock, **kwargs)
309 elif opts.modem_dev is not None:
310 print("Using modem for Generic SIM Access (3GPP TS 27.007)")
311 from pySim.transport.modem_atcmd import ModemATCommandLink
312 sl = ModemATCommandLink(
313 device=opts.modem_dev, baudrate=opts.modem_baud, **kwargs)
314 else: # Serial reader is default
315 print("Using serial reader interface")
316 from pySim.transport.serial import SerialSimLink
317 sl = SerialSimLink(device=opts.device,
318 baudrate=opts.baudrate, **kwargs)
319 return sl
320 except Exception as e:
321 if str(e):
322 print("Card reader initialization failed with exception:\n" + str(e))
323 else:
324 print(
325 "Card reader initialization failed with an exception of type:\n" + str(type(e)))
326 return None