Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | """ pySim: PCSC reader transport link base |
| 4 | """ |
| 5 | |
Harald Welte | e79cc80 | 2021-01-21 14:10:43 +0100 | [diff] [blame] | 6 | from pySim.exceptions import * |
Harald Welte | 67d551a | 2021-01-21 14:50:01 +0100 | [diff] [blame] | 7 | from pySim.utils import sw_match |
Harald Welte | e79cc80 | 2021-01-21 14:10:43 +0100 | [diff] [blame] | 8 | |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 9 | # |
| 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 | |
| 26 | class LinkBase(object): |
| 27 | |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 28 | 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' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 33 | inserted one ? |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 34 | """ |
| 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 Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 47 | 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' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 57 | data : string (in hex) of returned data (ex. "074F4EFFFF") |
| 58 | sw : string (in hex) of status word (ex. "9000") |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 59 | """ |
| 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' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 67 | data : string (in hex) of returned data (ex. "074F4EFFFF") |
| 68 | sw : string (in hex) of status word (ex. "9000") |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 69 | """ |
| 70 | data, sw = self.send_apdu_raw(pdu) |
| 71 | |
Philipp Maier | 859e0fd | 2018-06-12 18:40:24 +0200 | [diff] [blame] | 72 | # 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 Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 79 | 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 Maier | d4ebb6f | 2018-06-12 17:56:07 +0200 | [diff] [blame] | 88 | sw : string of 4 hexadecimal characters (ex. "9000"). The |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 89 | user may mask out certain digits using a '?' to add some |
| 90 | ambiguity if needed. |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 91 | return : tuple(data, sw), where |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 92 | data : string (in hex) of returned data (ex. "074F4EFFFF") |
| 93 | sw : string (in hex) of status word (ex. "9000") |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 94 | """ |
| 95 | rv = self.send_apdu(pdu) |
Philipp Maier | d4ebb6f | 2018-06-12 17:56:07 +0200 | [diff] [blame] | 96 | |
Harald Welte | 67d551a | 2021-01-21 14:50:01 +0100 | [diff] [blame] | 97 | if not sw_match(rv[1], sw): |
Harald Welte | e79cc80 | 2021-01-21 14:10:43 +0100 | [diff] [blame] | 98 | raise SwMatchError(rv[1], sw.lower()) |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 99 | return rv |