blob: d4f7f3a26beddd8eade15ee7ecf85ceaadf378fa [file] [log] [blame]
Sylvain Munaute7c15cd2010-12-07 10:01:55 +01001# -*- coding: utf-8 -*-
2
3""" pySim: PCSC reader transport link base
4"""
5
Harald Welte28c24312021-04-11 12:19:36 +02006import argparse
Harald Welte6e0458d2021-04-03 11:52:37 +02007from typing import Optional
8
Harald Weltee79cc802021-01-21 14:10:43 +01009from pySim.exceptions import *
Harald Weltee0f9ef12021-04-10 17:22:35 +020010from pySim.construct import filter_dict
11from pySim.utils import sw_match, b2h, h2b, i2h
Harald Weltee79cc802021-01-21 14:10:43 +010012
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010013#
14# Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com>
Harald Weltee0f9ef12021-04-10 17:22:35 +020015# Copyright (C) 2021 Harald Welte <laforge@osmocom.org>
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010016#
17# This program is free software: you can redistribute it and/or modify
18# it under the terms of the GNU General Public License as published by
19# the Free Software Foundation, either version 2 of the License, or
20# (at your option) any later version.
21#
22# This program is distributed in the hope that it will be useful,
23# but WITHOUT ANY WARRANTY; without even the implied warranty of
24# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25# GNU General Public License for more details.
26#
27# You should have received a copy of the GNU General Public License
28# along with this program. If not, see <http://www.gnu.org/licenses/>.
29#
30
Harald Welte7829d8a2021-04-10 11:28:53 +020031class ApduTracer:
32 def trace_command(self, cmd):
33 pass
34
35 def trace_response(self, cmd, sw, resp):
36 pass
37
38
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010039class LinkBase(object):
Harald Welteee3501f2021-04-02 13:00:18 +020040 """Base class for link/transport to card."""
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010041
Harald Welte7829d8a2021-04-10 11:28:53 +020042 def __init__(self, sw_interpreter=None, apdu_tracer=None):
Harald Welteeb05b2f2021-04-10 11:01:56 +020043 self.sw_interpreter = sw_interpreter
Harald Welte7829d8a2021-04-10 11:28:53 +020044 self.apdu_tracer = apdu_tracer
Harald Welte4f2c5462021-04-03 11:48:22 +020045
46 def set_sw_interpreter(self, interp):
47 """Set an (optional) status word interpreter."""
48 self.sw_interpreter = interp
49
Harald Welteee3501f2021-04-02 13:00:18 +020050 def wait_for_card(self, timeout:int=None, newcardonly:bool=False):
51 """Wait for a card and connect to it
Sylvain Munautbdca2522010-12-09 13:31:58 +010052
Harald Welteee3501f2021-04-02 13:00:18 +020053 Args:
54 timeout : Maximum wait time in seconds (None=no timeout)
55 newcardonly : Should we wait for a new card, or an already inserted one ?
Sylvain Munautbdca2522010-12-09 13:31:58 +010056 """
57 pass
58
59 def connect(self):
Harald Welteee3501f2021-04-02 13:00:18 +020060 """Connect to a card immediately
Sylvain Munautbdca2522010-12-09 13:31:58 +010061 """
62 pass
63
64 def disconnect(self):
Harald Welteee3501f2021-04-02 13:00:18 +020065 """Disconnect from card
Sylvain Munautbdca2522010-12-09 13:31:58 +010066 """
67 pass
68
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010069 def reset_card(self):
Harald Welteee3501f2021-04-02 13:00:18 +020070 """Resets the card (power down/up)
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010071 """
72 pass
73
Harald Welteee3501f2021-04-02 13:00:18 +020074 def send_apdu_raw(self, pdu:str):
75 """Sends an APDU with minimal processing
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010076
Harald Welteee3501f2021-04-02 13:00:18 +020077 Args:
78 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
79 Returns:
80 tuple(data, sw), where
81 data : string (in hex) of returned data (ex. "074F4EFFFF")
82 sw : string (in hex) of status word (ex. "9000")
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010083 """
Harald Welte7829d8a2021-04-10 11:28:53 +020084 if self.apdu_tracer:
85 self.apdu_tracer.trace_command(pdu)
86 (data, sw) = self._send_apdu_raw(pdu)
87 if self.apdu_tracer:
88 self.apdu_tracer.trace_response(pdu, sw, data)
89 return (data, sw)
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010090
91 def send_apdu(self, pdu):
Harald Welteee3501f2021-04-02 13:00:18 +020092 """Sends an APDU and auto fetch response data
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010093
Harald Welteee3501f2021-04-02 13:00:18 +020094 Args:
95 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
96 Returns:
97 tuple(data, sw), where
98 data : string (in hex) of returned data (ex. "074F4EFFFF")
99 sw : string (in hex) of status word (ex. "9000")
Sylvain Munaute7c15cd2010-12-07 10:01:55 +0100100 """
101 data, sw = self.send_apdu_raw(pdu)
102
Philipp Maier859e0fd2018-06-12 18:40:24 +0200103 # When whe have sent the first APDU, the SW may indicate that there are response bytes
104 # available. There are two SWs commonly used for this 9fxx (sim) and 61xx (usim), where
105 # xx is the number of response bytes available.
106 # See also:
107 # SW1=9F: 3GPP TS 51.011 9.4.1, Responses to commands which are correctly executed
108 # SW1=61: ISO/IEC 7816-4, Table 5 — General meaning of the interindustry values of SW1-SW2
109 if (sw is not None) and ((sw[0:2] == '9f') or (sw[0:2] == '61')):
Sylvain Munaute7c15cd2010-12-07 10:01:55 +0100110 pdu_gr = pdu[0:2] + 'c00000' + sw[2:4]
111 data, sw = self.send_apdu_raw(pdu_gr)
112
113 return data, sw
114
115 def send_apdu_checksw(self, pdu, sw="9000"):
Harald Welteee3501f2021-04-02 13:00:18 +0200116 """Sends an APDU and check returned SW
Sylvain Munaute7c15cd2010-12-07 10:01:55 +0100117
Harald Welteee3501f2021-04-02 13:00:18 +0200118 Args:
119 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
120 sw : string of 4 hexadecimal characters (ex. "9000"). The user may mask out certain
121 digits using a '?' to add some ambiguity if needed.
122 Returns:
123 tuple(data, sw), where
124 data : string (in hex) of returned data (ex. "074F4EFFFF")
125 sw : string (in hex) of status word (ex. "9000")
Sylvain Munaute7c15cd2010-12-07 10:01:55 +0100126 """
127 rv = self.send_apdu(pdu)
Philipp Maierd4ebb6f2018-06-12 17:56:07 +0200128
Harald Welte67d551a2021-01-21 14:50:01 +0100129 if not sw_match(rv[1], sw):
Harald Welte4f2c5462021-04-03 11:48:22 +0200130 raise SwMatchError(rv[1], sw.lower(), self.sw_interpreter)
Sylvain Munaute7c15cd2010-12-07 10:01:55 +0100131 return rv
Harald Welte6e0458d2021-04-03 11:52:37 +0200132
Harald Weltee0f9ef12021-04-10 17:22:35 +0200133 def send_apdu_constr(self, cla, ins, p1, p2, cmd_constr, cmd_data, resp_constr):
134 """Build and sends an APDU using a 'construct' definition; parses response.
135
136 Args:
137 cla : string (in hex) ISO 7816 class byte
138 ins : string (in hex) ISO 7816 instruction byte
139 p1 : string (in hex) ISO 7116 Parameter 1 byte
140 p2 : string (in hex) ISO 7116 Parameter 2 byte
141 cmd_cosntr : defining how to generate binary APDU command data
142 cmd_data : command data passed to cmd_constr
143 resp_cosntr : defining how to decode binary APDU response data
144 Returns:
145 Tuple of (decoded_data, sw)
146 """
147 cmd = cmd_constr.build(cmd_data) if cmd_data else ''
148 p3 = i2h([len(cmd)])
149 pdu = ''.join([cla, ins, p1, p2, p3, b2h(cmd)])
150 (data, sw) = self.send_apdu(pdu)
151 if data:
152 # filter the resulting dict to avoid '_io' members inside
153 rsp = filter_dict(resp_constr.parse(h2b(data)))
154 else:
155 rsp = None
156 return (rsp, sw)
157
158 def send_apdu_constr_checksw(self, cla, ins, p1, p2, cmd_constr, cmd_data, resp_constr,
159 sw_exp="9000"):
160 """Build and sends an APDU using a 'construct' definition; parses response.
161
162 Args:
163 cla : string (in hex) ISO 7816 class byte
164 ins : string (in hex) ISO 7816 instruction byte
165 p1 : string (in hex) ISO 7116 Parameter 1 byte
166 p2 : string (in hex) ISO 7116 Parameter 2 byte
167 cmd_cosntr : defining how to generate binary APDU command data
168 cmd_data : command data passed to cmd_constr
169 resp_cosntr : defining how to decode binary APDU response data
170 exp_sw : string (in hex) of status word (ex. "9000")
171 Returns:
172 Tuple of (decoded_data, sw)
173 """
174 (rsp, sw) = self.send_apdu_constr(cla, ins, p1, p2, cmd_constr, cmd_data, resp_constr)
175 if not sw_match(sw, sw_exp):
176 raise SwMatchError(sw, sw_exp.lower(), self.sw_interpreter)
177 return (rsp, sw)
178
Harald Welte28c24312021-04-11 12:19:36 +0200179def argparse_add_reader_args(arg_parser):
180 """Add all reader related arguments to the given argparse.Argumentparser instance."""
181 serial_group = arg_parser.add_argument_group('Serial Reader')
182 serial_group.add_argument('-d', '--device', metavar='DEV', default='/dev/ttyUSB0',
183 help='Serial Device for SIM access')
184 serial_group.add_argument('-b', '--baud', dest='baudrate', type=int, metavar='BAUD', default=9600,
185 help='Baud rate used for SIM access')
186
187 pcsc_group = arg_parser.add_argument_group('PC/SC Reader')
188 pcsc_group.add_argument('-p', '--pcsc-device', type=int, dest='pcsc_dev', metavar='PCSC', default=None,
189 help='PC/SC reader number to use for SIM access')
190
191 modem_group = arg_parser.add_argument_group('AT Command Modem Reader')
192 modem_group.add_argument('--modem-device', dest='modem_dev', metavar='DEV', default=None,
193 help='Serial port of modem for Generic SIM Access (3GPP TS 27.007)')
194 modem_group.add_argument('--modem-baud', type=int, metavar='BAUD', default=115200,
195 help='Baud rate used for modem port')
196
197 osmobb_group = arg_parser.add_argument_group('OsmocomBB Reader')
198 osmobb_group.add_argument('--osmocon', dest='osmocon_sock', metavar='PATH', default=None,
199 help='Socket path for Calypso (e.g. Motorola C1XX) based reader (via OsmocomBB)')
200
201 return arg_parser
202
Harald Welteeb05b2f2021-04-10 11:01:56 +0200203def init_reader(opts, **kwargs) -> Optional[LinkBase]:
Harald Welte6e0458d2021-04-03 11:52:37 +0200204 """
205 Init card reader driver
206 """
207 sl = None # type : :Optional[LinkBase]
208 try:
209 if opts.pcsc_dev is not None:
210 print("Using PC/SC reader interface")
211 from pySim.transport.pcsc import PcscSimLink
Harald Welteeb05b2f2021-04-10 11:01:56 +0200212 sl = PcscSimLink(opts.pcsc_dev, **kwargs)
Harald Welte6e0458d2021-04-03 11:52:37 +0200213 elif opts.osmocon_sock is not None:
214 print("Using Calypso-based (OsmocomBB) reader interface")
215 from pySim.transport.calypso import CalypsoSimLink
Harald Welteeb05b2f2021-04-10 11:01:56 +0200216 sl = CalypsoSimLink(sock_path=opts.osmocon_sock, **kwargs)
Harald Welte6e0458d2021-04-03 11:52:37 +0200217 elif opts.modem_dev is not None:
218 print("Using modem for Generic SIM Access (3GPP TS 27.007)")
219 from pySim.transport.modem_atcmd import ModemATCommandLink
Harald Welteeb05b2f2021-04-10 11:01:56 +0200220 sl = ModemATCommandLink(device=opts.modem_dev, baudrate=opts.modem_baud, **kwargs)
Harald Welte6e0458d2021-04-03 11:52:37 +0200221 else: # Serial reader is default
222 print("Using serial reader interface")
223 from pySim.transport.serial import SerialSimLink
Harald Welteeb05b2f2021-04-10 11:01:56 +0200224 sl = SerialSimLink(device=opts.device, baudrate=opts.baudrate, **kwargs)
Harald Welte6e0458d2021-04-03 11:52:37 +0200225 return sl
226 except Exception as e:
227 print("Card reader initialization failed with exception:\n" + str(e))
228 return None