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