Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 3 | # Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com> |
| 4 | # |
| 5 | # This program is free software: you can redistribute it and/or modify |
| 6 | # it under the terms of the GNU General Public License as published by |
| 7 | # the Free Software Foundation, either version 2 of the License, or |
| 8 | # (at your option) any later version. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU General Public License |
| 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | # |
| 18 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 19 | import serial |
| 20 | import time |
Philipp Maier | 92bdd5e | 2021-02-22 16:14:47 +0100 | [diff] [blame] | 21 | import os.path |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 22 | |
| 23 | from pySim.exceptions import NoCardError, ProtocolError |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 24 | from pySim.transport import LinkBase |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 25 | from pySim.utils import h2b, b2h |
| 26 | |
| 27 | |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 28 | class SerialSimLink(LinkBase): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 29 | """ pySim: Transport Link for serial (RS232) based readers included with simcard""" |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 30 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 31 | def __init__(self, device: str = '/dev/ttyUSB0', baudrate: int = 9600, rst: str = '-rts', |
| 32 | debug: bool = False, **kwargs): |
| 33 | super().__init__(**kwargs) |
| 34 | if not os.path.exists(device): |
| 35 | raise ValueError("device file %s does not exist -- abort" % device) |
| 36 | self._sl = serial.Serial( |
| 37 | port=device, |
| 38 | parity=serial.PARITY_EVEN, |
| 39 | bytesize=serial.EIGHTBITS, |
| 40 | stopbits=serial.STOPBITS_TWO, |
| 41 | timeout=1, |
| 42 | xonxoff=0, |
| 43 | rtscts=0, |
| 44 | baudrate=baudrate, |
| 45 | ) |
| 46 | self._rst_pin = rst |
| 47 | self._debug = debug |
| 48 | self._atr = None |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 49 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 50 | def __del__(self): |
| 51 | if (hasattr(self, "_sl")): |
| 52 | self._sl.close() |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 53 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 54 | def wait_for_card(self, timeout=None, newcardonly=False): |
| 55 | # Direct try |
| 56 | existing = False |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 57 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 58 | try: |
| 59 | self.reset_card() |
| 60 | if not newcardonly: |
| 61 | return |
| 62 | else: |
| 63 | existing = True |
| 64 | except NoCardError: |
| 65 | pass |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 66 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 67 | # Poll ... |
| 68 | mt = time.time() + timeout if timeout is not None else None |
| 69 | pe = 0 |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 70 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 71 | while (mt is None) or (time.time() < mt): |
| 72 | try: |
| 73 | time.sleep(0.5) |
| 74 | self.reset_card() |
| 75 | if not existing: |
| 76 | return |
| 77 | except NoCardError: |
| 78 | existing = False |
| 79 | except ProtocolError: |
| 80 | if existing: |
| 81 | existing = False |
| 82 | else: |
| 83 | # Tolerate a couple of protocol error ... can happen if |
| 84 | # we try when the card is 'half' inserted |
| 85 | pe += 1 |
| 86 | if (pe > 2): |
| 87 | raise |
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 | # Timed out ... |
| 90 | raise NoCardError() |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 91 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 92 | def connect(self): |
| 93 | self.reset_card() |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 94 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 95 | def get_atr(self): |
| 96 | return self._atr |
Alexander Chemeris | d2d660a | 2017-07-18 16:52:25 +0300 | [diff] [blame] | 97 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 98 | def disconnect(self): |
| 99 | pass # Nothing to do really ... |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 100 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 101 | def reset_card(self): |
| 102 | rv = self._reset_card() |
| 103 | if rv == 0: |
| 104 | raise NoCardError() |
| 105 | elif rv < 0: |
| 106 | raise ProtocolError() |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 107 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 108 | def _reset_card(self): |
| 109 | self._atr = None |
| 110 | rst_meth_map = { |
| 111 | 'rts': self._sl.setRTS, |
| 112 | 'dtr': self._sl.setDTR, |
| 113 | } |
| 114 | rst_val_map = {'+': 0, '-': 1} |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 115 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 116 | try: |
| 117 | rst_meth = rst_meth_map[self._rst_pin[1:]] |
| 118 | rst_val = rst_val_map[self._rst_pin[0]] |
| 119 | except: |
| 120 | raise ValueError('Invalid reset pin %s' % self._rst_pin) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 121 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 122 | rst_meth(rst_val) |
| 123 | time.sleep(0.1) # 100 ms |
| 124 | self._sl.flushInput() |
| 125 | rst_meth(rst_val ^ 1) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 126 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 127 | b = self._rx_byte() |
| 128 | if not b: |
| 129 | return 0 |
| 130 | if ord(b) != 0x3b: |
| 131 | return -1 |
| 132 | self._dbg_print("TS: 0x%x Direct convention" % ord(b)) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 133 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 134 | while ord(b) == 0x3b: |
| 135 | b = self._rx_byte() |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 136 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 137 | if not b: |
| 138 | return -1 |
| 139 | t0 = ord(b) |
| 140 | self._dbg_print("T0: 0x%x" % t0) |
| 141 | self._atr = [0x3b, ord(b)] |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 142 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 143 | for i in range(4): |
| 144 | if t0 & (0x10 << i): |
| 145 | b = self._rx_byte() |
| 146 | self._atr.append(ord(b)) |
| 147 | self._dbg_print("T%si = %x" % (chr(ord('A')+i), ord(b))) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 148 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 149 | for i in range(0, t0 & 0xf): |
| 150 | b = self._rx_byte() |
| 151 | self._atr.append(ord(b)) |
| 152 | self._dbg_print("Historical = %x" % ord(b)) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 153 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 154 | while True: |
| 155 | x = self._rx_byte() |
| 156 | if not x: |
| 157 | break |
| 158 | self._atr.append(ord(x)) |
| 159 | self._dbg_print("Extra: %x" % ord(x)) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 160 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 161 | return 1 |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 162 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 163 | def _dbg_print(self, s): |
| 164 | if self._debug: |
| 165 | print(s) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 166 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 167 | def _tx_byte(self, b): |
| 168 | self._sl.write(b) |
| 169 | r = self._sl.read() |
| 170 | if r != b: # TX and RX are tied, so we must clear the echo |
| 171 | raise ProtocolError("Bad echo value. Expected %02x, got %s)" % ( |
| 172 | ord(b), '%02x' % ord(r) if r else '(nil)')) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 173 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 174 | def _tx_string(self, s): |
| 175 | """This is only safe if it's guaranteed the card won't send any data |
| 176 | during the time of tx of the string !!!""" |
| 177 | self._sl.write(s) |
| 178 | r = self._sl.read(len(s)) |
| 179 | if r != s: # TX and RX are tied, so we must clear the echo |
| 180 | raise ProtocolError( |
| 181 | "Bad echo value (Expected: %s, got %s)" % (b2h(s), b2h(r))) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 182 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 183 | def _rx_byte(self): |
| 184 | return self._sl.read() |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 185 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 186 | def _send_apdu_raw(self, pdu): |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 187 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 188 | pdu = h2b(pdu) |
| 189 | data_len = pdu[4] # P3 |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 190 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 191 | # Send first CLASS,INS,P1,P2,P3 |
| 192 | self._tx_string(pdu[0:5]) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 193 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 194 | # Wait ack which can be |
| 195 | # - INS: Command acked -> go ahead |
| 196 | # - 0x60: NULL, just wait some more |
| 197 | # - SW1: The card can apparently proceed ... |
| 198 | while True: |
| 199 | b = self._rx_byte() |
| 200 | if ord(b) == pdu[1]: |
| 201 | break |
| 202 | elif b != '\x60': |
| 203 | # Ok, it 'could' be SW1 |
| 204 | sw1 = b |
| 205 | sw2 = self._rx_byte() |
| 206 | nil = self._rx_byte() |
| 207 | if (sw2 and not nil): |
| 208 | return '', b2h(sw1+sw2) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 209 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 210 | raise ProtocolError() |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 211 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 212 | # Send data (if any) |
| 213 | if len(pdu) > 5: |
| 214 | self._tx_string(pdu[5:]) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 215 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 216 | # Receive data (including SW !) |
| 217 | # length = [P3 - tx_data (=len(pdu)-len(hdr)) + 2 (SW1//2) ] |
| 218 | to_recv = data_len - len(pdu) + 5 + 2 |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 219 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 220 | data = bytes(0) |
| 221 | while (len(data) < to_recv): |
| 222 | b = self._rx_byte() |
| 223 | if (to_recv == 2) and (b == '\x60'): # Ignore NIL if we have no RX data (hack ?) |
| 224 | continue |
| 225 | if not b: |
| 226 | break |
| 227 | data += b |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 228 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 229 | # Split datafield from SW |
| 230 | if len(data) < 2: |
| 231 | return None, None |
| 232 | sw = data[-2:] |
| 233 | data = data[0:-2] |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 234 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 235 | # Return value |
| 236 | return b2h(data), b2h(sw) |