blob: d720259fd5d22cf949efb8f1ae0045409f6f5b4b [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):
27
Sylvain Munautbdca2522010-12-09 13:31:58 +010028 def wait_for_card(self, timeout=None, newcardonly=False):
29 """wait_for_card(): Wait for a card and connect to it
30
31 timeout : Maximum wait time (None=no timeout)
32 newcardonly : Should we wait for a new card, or an already
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +020033 inserted one ?
Sylvain Munautbdca2522010-12-09 13:31:58 +010034 """
35 pass
36
37 def connect(self):
38 """connect(): Connect to a card immediately
39 """
40 pass
41
42 def disconnect(self):
43 """disconnect(): Disconnect from card
44 """
45 pass
46
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010047 def reset_card(self):
48 """reset_card(): Resets the card (power down/up)
49 """
50 pass
51
52 def send_apdu_raw(self, pdu):
53 """send_apdu_raw(pdu): Sends an APDU with minimal processing
54
55 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
56 return : tuple(data, sw), where
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +020057 data : string (in hex) of returned data (ex. "074F4EFFFF")
58 sw : string (in hex) of status word (ex. "9000")
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010059 """
60 pass
61
62 def send_apdu(self, pdu):
63 """send_apdu(pdu): Sends an APDU and auto fetch response data
64
65 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
66 return : tuple(data, sw), where
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +020067 data : string (in hex) of returned data (ex. "074F4EFFFF")
68 sw : string (in hex) of status word (ex. "9000")
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010069 """
70 data, sw = self.send_apdu_raw(pdu)
71
Philipp Maier859e0fd2018-06-12 18:40:24 +020072 # When whe have sent the first APDU, the SW may indicate that there are response bytes
73 # available. There are two SWs commonly used for this 9fxx (sim) and 61xx (usim), where
74 # xx is the number of response bytes available.
75 # See also:
76 # SW1=9F: 3GPP TS 51.011 9.4.1, Responses to commands which are correctly executed
77 # SW1=61: ISO/IEC 7816-4, Table 5 — General meaning of the interindustry values of SW1-SW2
78 if (sw is not None) and ((sw[0:2] == '9f') or (sw[0:2] == '61')):
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010079 pdu_gr = pdu[0:2] + 'c00000' + sw[2:4]
80 data, sw = self.send_apdu_raw(pdu_gr)
81
82 return data, sw
83
84 def send_apdu_checksw(self, pdu, sw="9000"):
85 """send_apdu_checksw(pdu,sw): Sends an APDU and check returned SW
86
87 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
Philipp Maierd4ebb6f2018-06-12 17:56:07 +020088 sw : string of 4 hexadecimal characters (ex. "9000"). The
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +020089 user may mask out certain digits using a '?' to add some
90 ambiguity if needed.
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010091 return : tuple(data, sw), where
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +020092 data : string (in hex) of returned data (ex. "074F4EFFFF")
93 sw : string (in hex) of status word (ex. "9000")
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010094 """
95 rv = self.send_apdu(pdu)
Philipp Maierd4ebb6f2018-06-12 17:56:07 +020096
Harald Welte67d551a2021-01-21 14:50:01 +010097 if not sw_match(rv[1], sw):
Harald Weltee79cc802021-01-21 14:10:43 +010098 raise SwMatchError(rv[1], sw.lower())
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010099 return rv