blob: f946af8cf273cdcf2c057ff7c9a49f8453d00e26 [file] [log] [blame]
Sylvain Munaute7c15cd2010-12-07 10:01:55 +01001# -*- coding: utf-8 -*-
2
3""" pySim: PCSC reader transport link base
4"""
5
Harald Weltee79cc802021-01-21 14:10:43 +01006from pySim.exceptions import *
Harald Welte67d551a2021-01-21 14:50:01 +01007from pySim.utils import sw_match
Harald Weltee79cc802021-01-21 14:10:43 +01008
Sylvain Munaute7c15cd2010-12-07 10:01:55 +01009#
10# Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com>
11#
12# This program is free software: you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation, either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program. If not, see <http://www.gnu.org/licenses/>.
24#
25
26class LinkBase(object):
Harald Welteee3501f2021-04-02 13:00:18 +020027 """Base class for link/transport to card."""
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010028
Harald Welteee3501f2021-04-02 13:00:18 +020029 def wait_for_card(self, timeout:int=None, newcardonly:bool=False):
30 """Wait for a card and connect to it
Sylvain Munautbdca2522010-12-09 13:31:58 +010031
Harald Welteee3501f2021-04-02 13:00:18 +020032 Args:
33 timeout : Maximum wait time in seconds (None=no timeout)
34 newcardonly : Should we wait for a new card, or an already inserted one ?
Sylvain Munautbdca2522010-12-09 13:31:58 +010035 """
36 pass
37
38 def connect(self):
Harald Welteee3501f2021-04-02 13:00:18 +020039 """Connect to a card immediately
Sylvain Munautbdca2522010-12-09 13:31:58 +010040 """
41 pass
42
43 def disconnect(self):
Harald Welteee3501f2021-04-02 13:00:18 +020044 """Disconnect from card
Sylvain Munautbdca2522010-12-09 13:31:58 +010045 """
46 pass
47
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010048 def reset_card(self):
Harald Welteee3501f2021-04-02 13:00:18 +020049 """Resets the card (power down/up)
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010050 """
51 pass
52
Harald Welteee3501f2021-04-02 13:00:18 +020053 def send_apdu_raw(self, pdu:str):
54 """Sends an APDU with minimal processing
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010055
Harald Welteee3501f2021-04-02 13:00:18 +020056 Args:
57 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
58 Returns:
59 tuple(data, sw), where
60 data : string (in hex) of returned data (ex. "074F4EFFFF")
61 sw : string (in hex) of status word (ex. "9000")
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010062 """
63 pass
64
65 def send_apdu(self, pdu):
Harald Welteee3501f2021-04-02 13:00:18 +020066 """Sends an APDU and auto fetch response data
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010067
Harald Welteee3501f2021-04-02 13:00:18 +020068 Args:
69 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
70 Returns:
71 tuple(data, sw), where
72 data : string (in hex) of returned data (ex. "074F4EFFFF")
73 sw : string (in hex) of status word (ex. "9000")
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010074 """
75 data, sw = self.send_apdu_raw(pdu)
76
Philipp Maier859e0fd2018-06-12 18:40:24 +020077 # When whe have sent the first APDU, the SW may indicate that there are response bytes
78 # available. There are two SWs commonly used for this 9fxx (sim) and 61xx (usim), where
79 # xx is the number of response bytes available.
80 # See also:
81 # SW1=9F: 3GPP TS 51.011 9.4.1, Responses to commands which are correctly executed
82 # SW1=61: ISO/IEC 7816-4, Table 5 — General meaning of the interindustry values of SW1-SW2
83 if (sw is not None) and ((sw[0:2] == '9f') or (sw[0:2] == '61')):
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010084 pdu_gr = pdu[0:2] + 'c00000' + sw[2:4]
85 data, sw = self.send_apdu_raw(pdu_gr)
86
87 return data, sw
88
89 def send_apdu_checksw(self, pdu, sw="9000"):
Harald Welteee3501f2021-04-02 13:00:18 +020090 """Sends an APDU and check returned SW
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010091
Harald Welteee3501f2021-04-02 13:00:18 +020092 Args:
93 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
94 sw : string of 4 hexadecimal characters (ex. "9000"). The user may mask out certain
95 digits using a '?' to add some ambiguity if needed.
96 Returns:
97 tuple(data, sw), where
98 data : string (in hex) of returned data (ex. "074F4EFFFF")
99 sw : string (in hex) of status word (ex. "9000")
Sylvain Munaute7c15cd2010-12-07 10:01:55 +0100100 """
101 rv = self.send_apdu(pdu)
Philipp Maierd4ebb6f2018-06-12 17:56:07 +0200102
Harald Welte67d551a2021-01-21 14:50:01 +0100103 if not sw_match(rv[1], sw):
Harald Weltee79cc802021-01-21 14:10:43 +0100104 raise SwMatchError(rv[1], sw.lower())
Sylvain Munaute7c15cd2010-12-07 10:01:55 +0100105 return rv