blob: 0a71117cf714d3a5997f807a1e2c36cf0e36a4ab [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
70 if (sw is not None) and (sw[0:2] == '9f'):
71 pdu_gr = pdu[0:2] + 'c00000' + sw[2:4]
72 data, sw = self.send_apdu_raw(pdu_gr)
73
74 return data, sw
75
76 def send_apdu_checksw(self, pdu, sw="9000"):
77 """send_apdu_checksw(pdu,sw): Sends an APDU and check returned SW
78
79 pdu : string of hexadecimal characters (ex. "A0A40000023F00")
Philipp Maierd4ebb6f2018-06-12 17:56:07 +020080 sw : string of 4 hexadecimal characters (ex. "9000"). The
81 user may mask out certain digits using a '?' to add some
82 ambiguity if needed.
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010083 return : tuple(data, sw), where
84 data : string (in hex) of returned data (ex. "074F4EFFFF")
85 sw : string (in hex) of status word (ex. "9000")
86 """
87 rv = self.send_apdu(pdu)
Philipp Maierd4ebb6f2018-06-12 17:56:07 +020088
89 # Create a masked version of the returned status word
90 sw_masked = ""
91 for i in range(0, 4):
92 if sw.lower()[i] == '?':
93 sw_masked = sw_masked + '?'
94 else:
95 sw_masked = sw_masked + rv[1][i].lower()
96
97 if sw.lower() != sw_masked:
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010098 raise RuntimeError("SW match failed ! Expected %s and got %s." % (sw.lower(), rv[1]))
99 return rv