Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | """ pySim: PCSC reader transport link |
| 4 | """ |
| 5 | |
| 6 | # |
| 7 | # Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com> |
| 8 | # Copyright (C) 2010 Harald Welte <laforge@gnumonks.org> |
| 9 | # |
| 10 | # This program is free software: you can redistribute it and/or modify |
| 11 | # it under the terms of the GNU General Public License as published by |
| 12 | # the Free Software Foundation, either version 2 of the License, or |
| 13 | # (at your option) any later version. |
| 14 | # |
| 15 | # This program is distributed in the hope that it will be useful, |
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | # GNU General Public License for more details. |
| 19 | # |
| 20 | # You should have received a copy of the GNU General Public License |
| 21 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | # |
| 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 |
Daniel Willmann | dd014ea | 2020-10-19 10:35:11 +0200 | [diff] [blame] | 26 | from smartcard.Exceptions import NoCardException, CardRequestTimeoutException, 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 | |
Daniel Willmann | dd014ea | 2020-10-19 10:35:11 +0200 | [diff] [blame] | 29 | from pySim.exceptions import NoCardError, ProtocolError |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 30 | from pySim.transport import LinkBase |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 31 | from pySim.utils import h2i, i2h |
| 32 | |
| 33 | |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 34 | class PcscSimLink(LinkBase): |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 35 | |
Sylvain Munaut | 245d534 | 2010-12-08 22:42:07 +0100 | [diff] [blame] | 36 | def __init__(self, reader_number=0): |
Daniel Willmann | 677d41b | 2020-10-19 10:34:31 +0200 | [diff] [blame] | 37 | r = readers() |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 38 | self._reader = r[reader_number] |
| 39 | self._con = self._reader.createConnection() |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 40 | |
| 41 | def __del__(self): |
Vadim Yanitskiy | 1f8acd9 | 2020-02-27 02:42:56 +0700 | [diff] [blame] | 42 | try: |
| 43 | # FIXME: this causes multiple warnings in Python 3.5.3 |
| 44 | self._con.disconnect() |
| 45 | except: |
| 46 | pass |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 47 | return |
| 48 | |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 49 | def wait_for_card(self, timeout=None, newcardonly=False): |
| 50 | cr = CardRequest(readers=[self._reader], timeout=timeout, newcardonly=newcardonly) |
| 51 | try: |
| 52 | cr.waitforcard() |
| 53 | except CardRequestTimeoutException: |
| 54 | raise NoCardError() |
| 55 | self.connect() |
| 56 | |
| 57 | def connect(self): |
| 58 | try: |
Vadim Yanitskiy | fa617ac | 2020-02-26 22:33:08 +0700 | [diff] [blame] | 59 | # Explicitly select T=0 communication protocol |
| 60 | self._con.connect(CardConnection.T0_protocol) |
| 61 | except CardConnectionException: |
| 62 | raise ProtocolError() |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 63 | except NoCardException: |
| 64 | raise NoCardError() |
| 65 | |
Alexander Chemeris | d2d660a | 2017-07-18 16:52:25 +0300 | [diff] [blame] | 66 | def get_atr(self): |
| 67 | return self._con.getATR() |
| 68 | |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 69 | def disconnect(self): |
| 70 | self._con.disconnect() |
| 71 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 72 | def reset_card(self): |
Vadim Yanitskiy | 41c2217 | 2020-02-26 22:18:32 +0700 | [diff] [blame] | 73 | self.disconnect() |
| 74 | self.connect() |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 75 | return 1 |
| 76 | |
| 77 | def send_apdu_raw(self, pdu): |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 78 | """see LinkBase.send_apdu_raw""" |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 79 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 80 | apdu = h2i(pdu) |
| 81 | |
| 82 | data, sw1, sw2 = self._con.transmit(apdu) |
| 83 | |
| 84 | sw = [sw1, sw2] |
| 85 | |
| 86 | # Return value |
| 87 | return i2h(data), i2h(sw) |