blob: fbe7c0fc47bdaaa3926b79ae8ad417fc5d11e4a3 [file] [log] [blame]
Sylvain Munaut76504e02010-12-07 00:24:32 +01001# -*- coding: utf-8 -*-
2
Sylvain Munaut76504e02010-12-07 00:24:32 +01003# 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
Philipp Maier8c823782023-10-23 10:44:44 +020020import argparse
Harald Weltef9f8d7a2023-07-09 17:06:16 +020021from typing import Optional
Harald Welteab6897c2023-07-09 16:21:23 +020022
Vadim Yanitskiyfa617ac2020-02-26 22:33:08 +070023from smartcard.CardConnection import CardConnection
Sylvain Munautbdca2522010-12-09 13:31:58 +010024from smartcard.CardRequest import CardRequest
Harald Welte2db843e2021-05-21 22:51:28 +020025from smartcard.Exceptions import NoCardException, CardRequestTimeoutException, CardConnectionException, CardConnectionException
Sylvain Munaut76504e02010-12-07 00:24:32 +010026from smartcard.System import readers
Sylvain Munaut76504e02010-12-07 00:24:32 +010027
Harald Welte2db843e2021-05-21 22:51:28 +020028from pySim.exceptions import NoCardError, ProtocolError, ReaderError
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010029from pySim.transport import LinkBase
Harald Weltef9f8d7a2023-07-09 17:06:16 +020030from pySim.utils import h2i, i2h, Hexstr, ResTuple
Sylvain Munaut76504e02010-12-07 00:24:32 +010031
32
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010033class PcscSimLink(LinkBase):
Harald Weltec91085e2022-02-10 18:05:45 +010034 """ pySim: PCSC reader transport link."""
Sylvain Munaut76504e02010-12-07 00:24:32 +010035
Harald Weltec91085e2022-02-10 18:05:45 +010036 def __init__(self, reader_number: int = 0, **kwargs):
37 super().__init__(**kwargs)
Philipp Maier30773432023-10-23 10:18:04 +020038 print("Using PC/SC reader interface")
Harald Weltec91085e2022-02-10 18:05:45 +010039 r = readers()
40 if reader_number >= len(r):
Vadim Yanitskiye6b86872022-04-21 16:50:25 +030041 raise ReaderError('No reader found for number %d' % reader_number)
Harald Weltec91085e2022-02-10 18:05:45 +010042 self._reader = r[reader_number]
43 self._con = self._reader.createConnection()
Philipp Maier6bfa8a82023-10-09 13:32:49 +020044 self._reader_number = reader_number
Sylvain Munaut76504e02010-12-07 00:24:32 +010045
Harald Weltec91085e2022-02-10 18:05:45 +010046 def __del__(self):
47 try:
48 # FIXME: this causes multiple warnings in Python 3.5.3
49 self._con.disconnect()
50 except:
51 pass
52 return
Sylvain Munaut76504e02010-12-07 00:24:32 +010053
Harald Welteab6897c2023-07-09 16:21:23 +020054 def wait_for_card(self, timeout: Optional[int] = None, newcardonly: bool = False):
Harald Weltec91085e2022-02-10 18:05:45 +010055 cr = CardRequest(readers=[self._reader],
56 timeout=timeout, newcardonly=newcardonly)
57 try:
58 cr.waitforcard()
59 except CardRequestTimeoutException:
60 raise NoCardError()
61 self.connect()
Sylvain Munautbdca2522010-12-09 13:31:58 +010062
Harald Weltec91085e2022-02-10 18:05:45 +010063 def connect(self):
64 try:
65 # To avoid leakage of resources, make sure the reader
66 # is disconnected
67 self.disconnect()
Philipp Maier8bf21252021-09-22 15:30:21 +020068
Harald Weltec91085e2022-02-10 18:05:45 +010069 # Explicitly select T=0 communication protocol
70 self._con.connect(CardConnection.T0_protocol)
71 except CardConnectionException:
72 raise ProtocolError()
73 except NoCardException:
74 raise NoCardError()
Sylvain Munautbdca2522010-12-09 13:31:58 +010075
Harald Welteab6897c2023-07-09 16:21:23 +020076 def get_atr(self) -> Hexstr:
Harald Weltec91085e2022-02-10 18:05:45 +010077 return self._con.getATR()
Alexander Chemerisd2d660a2017-07-18 16:52:25 +030078
Harald Weltec91085e2022-02-10 18:05:45 +010079 def disconnect(self):
80 self._con.disconnect()
Sylvain Munautbdca2522010-12-09 13:31:58 +010081
Harald Weltec91085e2022-02-10 18:05:45 +010082 def reset_card(self):
83 self.disconnect()
84 self.connect()
85 return 1
Sylvain Munaut76504e02010-12-07 00:24:32 +010086
Harald Weltef9f8d7a2023-07-09 17:06:16 +020087 def _send_apdu_raw(self, pdu: Hexstr) -> ResTuple:
Sylvain Munaut76504e02010-12-07 00:24:32 +010088
Harald Weltec91085e2022-02-10 18:05:45 +010089 apdu = h2i(pdu)
Sylvain Munaut76504e02010-12-07 00:24:32 +010090
Harald Weltec91085e2022-02-10 18:05:45 +010091 data, sw1, sw2 = self._con.transmit(apdu)
Sylvain Munaut76504e02010-12-07 00:24:32 +010092
Harald Weltec91085e2022-02-10 18:05:45 +010093 sw = [sw1, sw2]
Sylvain Munaut76504e02010-12-07 00:24:32 +010094
Harald Weltec91085e2022-02-10 18:05:45 +010095 # Return value
96 return i2h(data), i2h(sw)
Philipp Maier6bfa8a82023-10-09 13:32:49 +020097
Philipp Maier58e89eb2023-10-10 11:59:03 +020098 def __str__(self) -> str:
Philipp Maier6bfa8a82023-10-09 13:32:49 +020099 return "PCSC:%u[%s]" % (self._reader_number, self._reader)
Philipp Maier8c823782023-10-23 10:44:44 +0200100
101 @staticmethod
102 def argparse_add_reader_args(arg_parser: argparse.ArgumentParser):
103 pcsc_group = arg_parser.add_argument_group('PC/SC Reader')
104 pcsc_group.add_argument('-p', '--pcsc-device', type=int, dest='pcsc_dev', metavar='PCSC', default=None,
105 help='PC/SC reader number to use for SIM access')