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 | # Copyright (C) 2010 Harald Welte <laforge@gnumonks.org> |
| 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 | |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 20 | from typing import Optional |
Harald Welte | ab6897c | 2023-07-09 16:21:23 +0200 | [diff] [blame] | 21 | |
Vadim Yanitskiy | fa617ac | 2020-02-26 22:33:08 +0700 | [diff] [blame] | 22 | from smartcard.CardConnection import CardConnection |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 23 | from smartcard.CardRequest import CardRequest |
Harald Welte | 2db843e | 2021-05-21 22:51:28 +0200 | [diff] [blame] | 24 | from smartcard.Exceptions import NoCardException, CardRequestTimeoutException, CardConnectionException, CardConnectionException |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 25 | from smartcard.System import readers |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 26 | |
Harald Welte | 2db843e | 2021-05-21 22:51:28 +0200 | [diff] [blame] | 27 | from pySim.exceptions import NoCardError, ProtocolError, ReaderError |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 28 | from pySim.transport import LinkBase |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 29 | from pySim.utils import h2i, i2h, Hexstr, ResTuple |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 30 | |
| 31 | |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 32 | class PcscSimLink(LinkBase): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 33 | """ pySim: PCSC reader transport link.""" |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 34 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 35 | def __init__(self, reader_number: int = 0, **kwargs): |
| 36 | super().__init__(**kwargs) |
Philipp Maier | 3077343 | 2023-10-23 10:18:04 +0200 | [diff] [blame^] | 37 | print("Using PC/SC reader interface") |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 38 | r = readers() |
| 39 | if reader_number >= len(r): |
Vadim Yanitskiy | e6b8687 | 2022-04-21 16:50:25 +0300 | [diff] [blame] | 40 | raise ReaderError('No reader found for number %d' % reader_number) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 41 | self._reader = r[reader_number] |
| 42 | self._con = self._reader.createConnection() |
Philipp Maier | 6bfa8a8 | 2023-10-09 13:32:49 +0200 | [diff] [blame] | 43 | self._reader_number = reader_number |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 44 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 45 | def __del__(self): |
| 46 | try: |
| 47 | # FIXME: this causes multiple warnings in Python 3.5.3 |
| 48 | self._con.disconnect() |
| 49 | except: |
| 50 | pass |
| 51 | return |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 52 | |
Harald Welte | ab6897c | 2023-07-09 16:21:23 +0200 | [diff] [blame] | 53 | def wait_for_card(self, timeout: Optional[int] = None, newcardonly: bool = False): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 54 | cr = CardRequest(readers=[self._reader], |
| 55 | timeout=timeout, newcardonly=newcardonly) |
| 56 | try: |
| 57 | cr.waitforcard() |
| 58 | except CardRequestTimeoutException: |
| 59 | raise NoCardError() |
| 60 | self.connect() |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 61 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 62 | def connect(self): |
| 63 | try: |
| 64 | # To avoid leakage of resources, make sure the reader |
| 65 | # is disconnected |
| 66 | self.disconnect() |
Philipp Maier | 8bf2125 | 2021-09-22 15:30:21 +0200 | [diff] [blame] | 67 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 68 | # Explicitly select T=0 communication protocol |
| 69 | self._con.connect(CardConnection.T0_protocol) |
| 70 | except CardConnectionException: |
| 71 | raise ProtocolError() |
| 72 | except NoCardException: |
| 73 | raise NoCardError() |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 74 | |
Harald Welte | ab6897c | 2023-07-09 16:21:23 +0200 | [diff] [blame] | 75 | def get_atr(self) -> Hexstr: |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 76 | return self._con.getATR() |
Alexander Chemeris | d2d660a | 2017-07-18 16:52:25 +0300 | [diff] [blame] | 77 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 78 | def disconnect(self): |
| 79 | self._con.disconnect() |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 80 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 81 | def reset_card(self): |
| 82 | self.disconnect() |
| 83 | self.connect() |
| 84 | return 1 |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 85 | |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 86 | def _send_apdu_raw(self, pdu: Hexstr) -> ResTuple: |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 87 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 88 | apdu = h2i(pdu) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 89 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 90 | data, sw1, sw2 = self._con.transmit(apdu) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 91 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 92 | sw = [sw1, sw2] |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 93 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 94 | # Return value |
| 95 | return i2h(data), i2h(sw) |
Philipp Maier | 6bfa8a8 | 2023-10-09 13:32:49 +0200 | [diff] [blame] | 96 | |
Philipp Maier | 58e89eb | 2023-10-10 11:59:03 +0200 | [diff] [blame] | 97 | def __str__(self) -> str: |
Philipp Maier | 6bfa8a8 | 2023-10-09 13:32:49 +0200 | [diff] [blame] | 98 | return "PCSC:%u[%s]" % (self._reader_number, self._reader) |