blob: 00c7bd9f7993858f034290cd74880aa1047c390b [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 *
7
Sylvain Munaute7c15cd2010-12-07 10:01:55 +01008#
9# Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com>
10#
11# This program is free software: you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation, either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program. If not, see <http://www.gnu.org/licenses/>.
23#
24
25class LinkBase(object):
26
Sylvain Munautbdca2522010-12-09 13:31:58 +010027 def wait_for_card(self, timeout=None, newcardonly=False):
28 """wait_for_card(): Wait for a card and connect to it
29
30 timeout : Maximum wait time (None=no timeout)
31 newcardonly : Should we wait for a new card, or an already
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +020032 inserted one ?
Sylvain Munautbdca2522010-12-09 13:31:58 +010033 """
34 pass
35
36 def connect(self):
37 """connect(): Connect to a card immediately
38 """
39 pass
40
41 def disconnect(self):
42 """disconnect(): Disconnect from card
43 """
44 pass
45
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010046 def reset_card(self):
47 """reset_card(): Resets the card (power down/up)
48 """
49 pass
50
51 def send_apdu_raw(self, pdu):
52 """send_apdu_raw(pdu): Sends an APDU with minimal processing
53
54 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
55 return : tuple(data, sw), where
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +020056 data : string (in hex) of returned data (ex. "074F4EFFFF")
57 sw : string (in hex) of status word (ex. "9000")
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010058 """
59 pass
60
61 def send_apdu(self, pdu):
62 """send_apdu(pdu): Sends an APDU and auto fetch response data
63
64 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
65 return : tuple(data, sw), where
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +020066 data : string (in hex) of returned data (ex. "074F4EFFFF")
67 sw : string (in hex) of status word (ex. "9000")
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010068 """
69 data, sw = self.send_apdu_raw(pdu)
70
Philipp Maier859e0fd2018-06-12 18:40:24 +020071 # When whe have sent the first APDU, the SW may indicate that there are response bytes
72 # available. There are two SWs commonly used for this 9fxx (sim) and 61xx (usim), where
73 # xx is the number of response bytes available.
74 # See also:
75 # SW1=9F: 3GPP TS 51.011 9.4.1, Responses to commands which are correctly executed
76 # SW1=61: ISO/IEC 7816-4, Table 5 — General meaning of the interindustry values of SW1-SW2
77 if (sw is not None) and ((sw[0:2] == '9f') or (sw[0:2] == '61')):
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010078 pdu_gr = pdu[0:2] + 'c00000' + sw[2:4]
79 data, sw = self.send_apdu_raw(pdu_gr)
80
81 return data, sw
82
83 def send_apdu_checksw(self, pdu, sw="9000"):
84 """send_apdu_checksw(pdu,sw): Sends an APDU and check returned SW
85
86 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
Philipp Maierd4ebb6f2018-06-12 17:56:07 +020087 sw : string of 4 hexadecimal characters (ex. "9000"). The
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +020088 user may mask out certain digits using a '?' to add some
89 ambiguity if needed.
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010090 return : tuple(data, sw), where
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +020091 data : string (in hex) of returned data (ex. "074F4EFFFF")
92 sw : string (in hex) of status word (ex. "9000")
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010093 """
94 rv = self.send_apdu(pdu)
Philipp Maierd4ebb6f2018-06-12 17:56:07 +020095
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +020096 # Create a masked version of the returned status word
Philipp Maierd4ebb6f2018-06-12 17:56:07 +020097 sw_masked = ""
98 for i in range(0, 4):
99 if sw.lower()[i] == '?':
100 sw_masked = sw_masked + '?'
101 else:
102 sw_masked = sw_masked + rv[1][i].lower()
103
104 if sw.lower() != sw_masked:
Harald Weltee79cc802021-01-21 14:10:43 +0100105 raise SwMatchError(rv[1], sw.lower())
Sylvain Munaute7c15cd2010-12-07 10:01:55 +0100106 return rv