blob: fb8f31d192a95b02c8d49eac06a0bd7e5451464e [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 Welte6e0458d2021-04-03 11:52:37 +02006from typing import Optional
7
Harald Weltee79cc802021-01-21 14:10:43 +01008from pySim.exceptions import *
Harald Welte67d551a2021-01-21 14:50:01 +01009from pySim.utils import sw_match
Harald Weltee79cc802021-01-21 14:10:43 +010010
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010011#
12# Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com>
13#
14# This program is free software: you can redistribute it and/or modify
15# it under the terms of the GNU General Public License as published by
16# the Free Software Foundation, either version 2 of the License, or
17# (at your option) any later version.
18#
19# This program is distributed in the hope that it will be useful,
20# but WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22# GNU General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with this program. If not, see <http://www.gnu.org/licenses/>.
26#
27
28class LinkBase(object):
Harald Welteee3501f2021-04-02 13:00:18 +020029 """Base class for link/transport to card."""
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010030
Harald Welte4f2c5462021-04-03 11:48:22 +020031 sw_interpreter = None
32
33 def set_sw_interpreter(self, interp):
34 """Set an (optional) status word interpreter."""
35 self.sw_interpreter = interp
36
Harald Welteee3501f2021-04-02 13:00:18 +020037 def wait_for_card(self, timeout:int=None, newcardonly:bool=False):
38 """Wait for a card and connect to it
Sylvain Munautbdca2522010-12-09 13:31:58 +010039
Harald Welteee3501f2021-04-02 13:00:18 +020040 Args:
41 timeout : Maximum wait time in seconds (None=no timeout)
42 newcardonly : Should we wait for a new card, or an already inserted one ?
Sylvain Munautbdca2522010-12-09 13:31:58 +010043 """
44 pass
45
46 def connect(self):
Harald Welteee3501f2021-04-02 13:00:18 +020047 """Connect to a card immediately
Sylvain Munautbdca2522010-12-09 13:31:58 +010048 """
49 pass
50
51 def disconnect(self):
Harald Welteee3501f2021-04-02 13:00:18 +020052 """Disconnect from card
Sylvain Munautbdca2522010-12-09 13:31:58 +010053 """
54 pass
55
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010056 def reset_card(self):
Harald Welteee3501f2021-04-02 13:00:18 +020057 """Resets the card (power down/up)
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010058 """
59 pass
60
Harald Welteee3501f2021-04-02 13:00:18 +020061 def send_apdu_raw(self, pdu:str):
62 """Sends an APDU with minimal processing
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010063
Harald Welteee3501f2021-04-02 13:00:18 +020064 Args:
65 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
66 Returns:
67 tuple(data, sw), where
68 data : string (in hex) of returned data (ex. "074F4EFFFF")
69 sw : string (in hex) of status word (ex. "9000")
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010070 """
71 pass
72
73 def send_apdu(self, pdu):
Harald Welteee3501f2021-04-02 13:00:18 +020074 """Sends an APDU and auto fetch response data
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010075
Harald Welteee3501f2021-04-02 13:00:18 +020076 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 Munaute7c15cd2010-12-07 10:01:55 +010082 """
83 data, sw = self.send_apdu_raw(pdu)
84
Philipp Maier859e0fd2018-06-12 18:40:24 +020085 # When whe have sent the first APDU, the SW may indicate that there are response bytes
86 # available. There are two SWs commonly used for this 9fxx (sim) and 61xx (usim), where
87 # xx is the number of response bytes available.
88 # See also:
89 # SW1=9F: 3GPP TS 51.011 9.4.1, Responses to commands which are correctly executed
90 # SW1=61: ISO/IEC 7816-4, Table 5 — General meaning of the interindustry values of SW1-SW2
91 if (sw is not None) and ((sw[0:2] == '9f') or (sw[0:2] == '61')):
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010092 pdu_gr = pdu[0:2] + 'c00000' + sw[2:4]
93 data, sw = self.send_apdu_raw(pdu_gr)
94
95 return data, sw
96
97 def send_apdu_checksw(self, pdu, sw="9000"):
Harald Welteee3501f2021-04-02 13:00:18 +020098 """Sends an APDU and check returned SW
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010099
Harald Welteee3501f2021-04-02 13:00:18 +0200100 Args:
101 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
102 sw : string of 4 hexadecimal characters (ex. "9000"). The user may mask out certain
103 digits using a '?' to add some ambiguity if needed.
104 Returns:
105 tuple(data, sw), where
106 data : string (in hex) of returned data (ex. "074F4EFFFF")
107 sw : string (in hex) of status word (ex. "9000")
Sylvain Munaute7c15cd2010-12-07 10:01:55 +0100108 """
109 rv = self.send_apdu(pdu)
Philipp Maierd4ebb6f2018-06-12 17:56:07 +0200110
Harald Welte67d551a2021-01-21 14:50:01 +0100111 if not sw_match(rv[1], sw):
Harald Welte4f2c5462021-04-03 11:48:22 +0200112 raise SwMatchError(rv[1], sw.lower(), self.sw_interpreter)
Sylvain Munaute7c15cd2010-12-07 10:01:55 +0100113 return rv
Harald Welte6e0458d2021-04-03 11:52:37 +0200114
115def init_reader(opts) -> Optional[LinkBase]:
116 """
117 Init card reader driver
118 """
119 sl = None # type : :Optional[LinkBase]
120 try:
121 if opts.pcsc_dev is not None:
122 print("Using PC/SC reader interface")
123 from pySim.transport.pcsc import PcscSimLink
124 sl = PcscSimLink(opts.pcsc_dev)
125 elif opts.osmocon_sock is not None:
126 print("Using Calypso-based (OsmocomBB) reader interface")
127 from pySim.transport.calypso import CalypsoSimLink
128 sl = CalypsoSimLink(sock_path=opts.osmocon_sock)
129 elif opts.modem_dev is not None:
130 print("Using modem for Generic SIM Access (3GPP TS 27.007)")
131 from pySim.transport.modem_atcmd import ModemATCommandLink
132 sl = ModemATCommandLink(device=opts.modem_dev, baudrate=opts.modem_baud)
133 else: # Serial reader is default
134 print("Using serial reader interface")
135 from pySim.transport.serial import SerialSimLink
136 sl = SerialSimLink(device=opts.device, baudrate=opts.baudrate)
137 return sl
138 except Exception as e:
139 print("Card reader initialization failed with exception:\n" + str(e))
140 return None