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 | |
Vadim Yanitskiy | fa617ac | 2020-02-26 22:33:08 +0700 | [diff] [blame] | 20 | from smartcard.CardConnection import CardConnection |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 21 | from smartcard.CardRequest import CardRequest |
Daniel Willmann | dd014ea | 2020-10-19 10:35:11 +0200 | [diff] [blame] | 22 | from smartcard.Exceptions import NoCardException, CardRequestTimeoutException, CardConnectionException |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 23 | from smartcard.System import readers |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 24 | |
Daniel Willmann | dd014ea | 2020-10-19 10:35:11 +0200 | [diff] [blame] | 25 | from pySim.exceptions import NoCardError, ProtocolError |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 26 | from pySim.transport import LinkBase |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 27 | from pySim.utils import h2i, i2h |
| 28 | |
| 29 | |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 30 | class PcscSimLink(LinkBase): |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 31 | """ pySim: PCSC reader transport link.""" |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 32 | |
Harald Welte | eb05b2f | 2021-04-10 11:01:56 +0200 | [diff] [blame^] | 33 | def __init__(self, reader_number:int=0, **kwargs): |
| 34 | super().__init__(**kwargs) |
Daniel Willmann | 677d41b | 2020-10-19 10:34:31 +0200 | [diff] [blame] | 35 | r = readers() |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 36 | self._reader = r[reader_number] |
| 37 | self._con = self._reader.createConnection() |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 38 | |
| 39 | def __del__(self): |
Vadim Yanitskiy | 1f8acd9 | 2020-02-27 02:42:56 +0700 | [diff] [blame] | 40 | try: |
| 41 | # FIXME: this causes multiple warnings in Python 3.5.3 |
| 42 | self._con.disconnect() |
| 43 | except: |
| 44 | pass |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 45 | return |
| 46 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 47 | def wait_for_card(self, timeout:int=None, newcardonly:bool=False): |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 48 | cr = CardRequest(readers=[self._reader], timeout=timeout, newcardonly=newcardonly) |
| 49 | try: |
| 50 | cr.waitforcard() |
| 51 | except CardRequestTimeoutException: |
| 52 | raise NoCardError() |
| 53 | self.connect() |
| 54 | |
| 55 | def connect(self): |
| 56 | try: |
Vadim Yanitskiy | fa617ac | 2020-02-26 22:33:08 +0700 | [diff] [blame] | 57 | # Explicitly select T=0 communication protocol |
| 58 | self._con.connect(CardConnection.T0_protocol) |
| 59 | except CardConnectionException: |
| 60 | raise ProtocolError() |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 61 | except NoCardException: |
| 62 | raise NoCardError() |
| 63 | |
Alexander Chemeris | d2d660a | 2017-07-18 16:52:25 +0300 | [diff] [blame] | 64 | def get_atr(self): |
| 65 | return self._con.getATR() |
| 66 | |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 67 | def disconnect(self): |
| 68 | self._con.disconnect() |
| 69 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 70 | def reset_card(self): |
Vadim Yanitskiy | 41c2217 | 2020-02-26 22:18:32 +0700 | [diff] [blame] | 71 | self.disconnect() |
| 72 | self.connect() |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 73 | return 1 |
| 74 | |
Harald Welte | c34f940 | 2021-04-10 10:55:24 +0200 | [diff] [blame] | 75 | def _send_apdu_raw(self, pdu): |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 76 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 77 | apdu = h2i(pdu) |
| 78 | |
| 79 | data, sw1, sw2 = self._con.transmit(apdu) |
| 80 | |
| 81 | sw = [sw1, sw2] |
| 82 | |
| 83 | # Return value |
| 84 | return i2h(data), i2h(sw) |