blob: 8e009ee503a4988fa1e9217df01cb1f76dd387af [file] [log] [blame]
Sylvain Munaute7c15cd2010-12-07 10:01:55 +01001# -*- coding: utf-8 -*-
2
3""" pySim: PCSC reader transport link base
4"""
5
6#
7# Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com>
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
21#
22
23class LinkBase(object):
24
Sylvain Munautbdca2522010-12-09 13:31:58 +010025 def wait_for_card(self, timeout=None, newcardonly=False):
26 """wait_for_card(): Wait for a card and connect to it
27
28 timeout : Maximum wait time (None=no timeout)
29 newcardonly : Should we wait for a new card, or an already
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +020030 inserted one ?
Sylvain Munautbdca2522010-12-09 13:31:58 +010031 """
32 pass
33
34 def connect(self):
35 """connect(): Connect to a card immediately
36 """
37 pass
38
39 def disconnect(self):
40 """disconnect(): Disconnect from card
41 """
42 pass
43
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010044 def reset_card(self):
45 """reset_card(): Resets the card (power down/up)
46 """
47 pass
48
49 def send_apdu_raw(self, pdu):
50 """send_apdu_raw(pdu): Sends an APDU with minimal processing
51
52 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
53 return : tuple(data, sw), where
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +020054 data : string (in hex) of returned data (ex. "074F4EFFFF")
55 sw : string (in hex) of status word (ex. "9000")
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010056 """
57 pass
58
59 def send_apdu(self, pdu):
60 """send_apdu(pdu): Sends an APDU and auto fetch response data
61
62 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
63 return : tuple(data, sw), where
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +020064 data : string (in hex) of returned data (ex. "074F4EFFFF")
65 sw : string (in hex) of status word (ex. "9000")
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010066 """
67 data, sw = self.send_apdu_raw(pdu)
68
Philipp Maier859e0fd2018-06-12 18:40:24 +020069 # When whe have sent the first APDU, the SW may indicate that there are response bytes
70 # available. There are two SWs commonly used for this 9fxx (sim) and 61xx (usim), where
71 # xx is the number of response bytes available.
72 # See also:
73 # SW1=9F: 3GPP TS 51.011 9.4.1, Responses to commands which are correctly executed
74 # SW1=61: ISO/IEC 7816-4, Table 5 — General meaning of the interindustry values of SW1-SW2
75 if (sw is not None) and ((sw[0:2] == '9f') or (sw[0:2] == '61')):
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010076 pdu_gr = pdu[0:2] + 'c00000' + sw[2:4]
77 data, sw = self.send_apdu_raw(pdu_gr)
78
79 return data, sw
80
81 def send_apdu_checksw(self, pdu, sw="9000"):
82 """send_apdu_checksw(pdu,sw): Sends an APDU and check returned SW
83
84 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
Philipp Maierd4ebb6f2018-06-12 17:56:07 +020085 sw : string of 4 hexadecimal characters (ex. "9000"). The
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +020086 user may mask out certain digits using a '?' to add some
87 ambiguity if needed.
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010088 return : tuple(data, sw), where
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +020089 data : string (in hex) of returned data (ex. "074F4EFFFF")
90 sw : string (in hex) of status word (ex. "9000")
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010091 """
92 rv = self.send_apdu(pdu)
Philipp Maierd4ebb6f2018-06-12 17:56:07 +020093
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +020094 # Create a masked version of the returned status word
Philipp Maierd4ebb6f2018-06-12 17:56:07 +020095 sw_masked = ""
96 for i in range(0, 4):
97 if sw.lower()[i] == '?':
98 sw_masked = sw_masked + '?'
99 else:
100 sw_masked = sw_masked + rv[1][i].lower()
101
102 if sw.lower() != sw_masked:
Philipp Maier589c1a42018-07-11 22:59:53 +0200103 raise RuntimeError("SW match failed! Expected %s and got %s." % (sw.lower(), rv[1]))
Sylvain Munaute7c15cd2010-12-07 10:01:55 +0100104 return rv