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> |
Harald Welte | ad00279 | 2023-11-03 11:42:18 +0100 | [diff] [blame] | 4 | # Copyright (C) 2010-2023 Harald Welte <laforge@gnumonks.org> |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 5 | # |
| 6 | # This program is free software: you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License as published by |
| 8 | # the Free Software Foundation, either version 2 of the License, or |
| 9 | # (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | # |
| 19 | |
Philipp Maier | 8c82378 | 2023-10-23 10:44:44 +0200 | [diff] [blame] | 20 | import argparse |
Harald Welte | ad00279 | 2023-11-03 11:42:18 +0100 | [diff] [blame] | 21 | import re |
| 22 | from typing import Optional, Union |
Harald Welte | ab6897c | 2023-07-09 16:21:23 +0200 | [diff] [blame] | 23 | |
Vadim Yanitskiy | fa617ac | 2020-02-26 22:33:08 +0700 | [diff] [blame] | 24 | from smartcard.CardConnection import CardConnection |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 25 | from smartcard.CardRequest import CardRequest |
Harald Welte | 2db843e | 2021-05-21 22:51:28 +0200 | [diff] [blame] | 26 | from smartcard.Exceptions import NoCardException, CardRequestTimeoutException, CardConnectionException, CardConnectionException |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 27 | from smartcard.System import readers |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 28 | |
Harald Welte | 2db843e | 2021-05-21 22:51:28 +0200 | [diff] [blame] | 29 | from pySim.exceptions import NoCardError, ProtocolError, ReaderError |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 30 | from pySim.transport import LinkBase |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 31 | from pySim.utils import h2i, i2h, Hexstr, ResTuple |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 32 | |
| 33 | |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 34 | class PcscSimLink(LinkBase): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 35 | """ pySim: PCSC reader transport link.""" |
Harald Welte | baec4e9 | 2023-11-03 11:49:54 +0100 | [diff] [blame^] | 36 | name = 'PC/SC' |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 37 | |
Harald Welte | 0f177c1 | 2023-12-17 12:38:29 +0100 | [diff] [blame] | 38 | def __init__(self, opts: argparse.Namespace = argparse.Namespace(pcsc_dev=0), **kwargs): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 39 | super().__init__(**kwargs) |
Harald Welte | ad00279 | 2023-11-03 11:42:18 +0100 | [diff] [blame] | 40 | self._reader = None |
| 41 | r = readers() |
| 42 | if opts.pcsc_dev is not None: |
| 43 | # actual reader index number (integer) |
| 44 | reader_number = opts.pcsc_dev |
| 45 | if reader_number >= len(r): |
| 46 | raise ReaderError('No reader found for number %d' % reader_number) |
| 47 | self._reader = r[reader_number] |
| 48 | else: |
| 49 | # reader regex string |
| 50 | cre = re.compile(opts.pcsc_regex) |
| 51 | for reader in r: |
| 52 | if cre.search(reader.name): |
| 53 | self._reader = reader |
| 54 | break |
| 55 | if not self._reader: |
| 56 | raise ReaderError('No matching reader found for regex %s' % opts.pcsc_regex) |
| 57 | |
| 58 | self._con = self._reader.createConnection() |
| 59 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 60 | def __del__(self): |
| 61 | try: |
| 62 | # FIXME: this causes multiple warnings in Python 3.5.3 |
| 63 | self._con.disconnect() |
| 64 | except: |
| 65 | pass |
| 66 | return |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 67 | |
Harald Welte | ab6897c | 2023-07-09 16:21:23 +0200 | [diff] [blame] | 68 | def wait_for_card(self, timeout: Optional[int] = None, newcardonly: bool = False): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 69 | cr = CardRequest(readers=[self._reader], |
| 70 | timeout=timeout, newcardonly=newcardonly) |
| 71 | try: |
| 72 | cr.waitforcard() |
| 73 | except CardRequestTimeoutException: |
| 74 | raise NoCardError() |
| 75 | self.connect() |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 76 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 77 | def connect(self): |
| 78 | try: |
| 79 | # To avoid leakage of resources, make sure the reader |
| 80 | # is disconnected |
| 81 | self.disconnect() |
Philipp Maier | 8bf2125 | 2021-09-22 15:30:21 +0200 | [diff] [blame] | 82 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 83 | # Explicitly select T=0 communication protocol |
| 84 | self._con.connect(CardConnection.T0_protocol) |
| 85 | except CardConnectionException: |
| 86 | raise ProtocolError() |
| 87 | except NoCardException: |
| 88 | raise NoCardError() |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 89 | |
Harald Welte | ab6897c | 2023-07-09 16:21:23 +0200 | [diff] [blame] | 90 | def get_atr(self) -> Hexstr: |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 91 | return self._con.getATR() |
Alexander Chemeris | d2d660a | 2017-07-18 16:52:25 +0300 | [diff] [blame] | 92 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 93 | def disconnect(self): |
| 94 | self._con.disconnect() |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 95 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 96 | def reset_card(self): |
| 97 | self.disconnect() |
| 98 | self.connect() |
| 99 | return 1 |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 100 | |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 101 | def _send_apdu_raw(self, pdu: Hexstr) -> ResTuple: |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 102 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 103 | apdu = h2i(pdu) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 104 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 105 | data, sw1, sw2 = self._con.transmit(apdu) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 106 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 107 | sw = [sw1, sw2] |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 108 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 109 | # Return value |
| 110 | return i2h(data), i2h(sw) |
Philipp Maier | 6bfa8a8 | 2023-10-09 13:32:49 +0200 | [diff] [blame] | 111 | |
Philipp Maier | 58e89eb | 2023-10-10 11:59:03 +0200 | [diff] [blame] | 112 | def __str__(self) -> str: |
Harald Welte | ad00279 | 2023-11-03 11:42:18 +0100 | [diff] [blame] | 113 | return "PCSC[%s]" % (self._reader) |
Philipp Maier | 8c82378 | 2023-10-23 10:44:44 +0200 | [diff] [blame] | 114 | |
| 115 | @staticmethod |
| 116 | def argparse_add_reader_args(arg_parser: argparse.ArgumentParser): |
| 117 | pcsc_group = arg_parser.add_argument_group('PC/SC Reader') |
Harald Welte | ad00279 | 2023-11-03 11:42:18 +0100 | [diff] [blame] | 118 | dev_group = pcsc_group.add_mutually_exclusive_group() |
| 119 | dev_group.add_argument('-p', '--pcsc-device', type=int, dest='pcsc_dev', metavar='PCSC', default=None, |
| 120 | help='Number of PC/SC reader to use for SIM access') |
| 121 | dev_group.add_argument('--pcsc-regex', type=str, dest='pcsc_regex', metavar='REGEX', default=None, |
| 122 | help='Regex matching PC/SC reader to use for SIM access') |