Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame^] | 1 | #!/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 | |
| 24 | class LinkBase(object): |
| 25 | |
| 26 | def reset_card(self): |
| 27 | """reset_card(): Resets the card (power down/up) |
| 28 | """ |
| 29 | pass |
| 30 | |
| 31 | def send_apdu_raw(self, pdu): |
| 32 | """send_apdu_raw(pdu): Sends an APDU with minimal processing |
| 33 | |
| 34 | pdu : string of hexadecimal characters (ex. "A0A40000023F00") |
| 35 | return : tuple(data, sw), where |
| 36 | data : string (in hex) of returned data (ex. "074F4EFFFF") |
| 37 | sw : string (in hex) of status word (ex. "9000") |
| 38 | """ |
| 39 | pass |
| 40 | |
| 41 | def send_apdu(self, pdu): |
| 42 | """send_apdu(pdu): Sends an APDU and auto fetch response data |
| 43 | |
| 44 | pdu : string of hexadecimal characters (ex. "A0A40000023F00") |
| 45 | return : tuple(data, sw), where |
| 46 | data : string (in hex) of returned data (ex. "074F4EFFFF") |
| 47 | sw : string (in hex) of status word (ex. "9000") |
| 48 | """ |
| 49 | data, sw = self.send_apdu_raw(pdu) |
| 50 | |
| 51 | if (sw is not None) and (sw[0:2] == '9f'): |
| 52 | pdu_gr = pdu[0:2] + 'c00000' + sw[2:4] |
| 53 | data, sw = self.send_apdu_raw(pdu_gr) |
| 54 | |
| 55 | return data, sw |
| 56 | |
| 57 | def send_apdu_checksw(self, pdu, sw="9000"): |
| 58 | """send_apdu_checksw(pdu,sw): Sends an APDU and check returned SW |
| 59 | |
| 60 | pdu : string of hexadecimal characters (ex. "A0A40000023F00") |
| 61 | sw : string of 4 hexadecimal characters (ex. "9000") |
| 62 | return : tuple(data, sw), where |
| 63 | data : string (in hex) of returned data (ex. "074F4EFFFF") |
| 64 | sw : string (in hex) of status word (ex. "9000") |
| 65 | """ |
| 66 | rv = self.send_apdu(pdu) |
| 67 | if sw.lower() != rv[1]: |
| 68 | raise RuntimeError("SW match failed ! Expected %s and got %s." % (sw.lower(), rv[1])) |
| 69 | return rv |