Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | """ pySim: SIM Card commands according to ISO 7816-4 and TS 11.11 |
| 5 | """ |
| 6 | |
| 7 | # |
| 8 | # Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com> |
| 9 | # Copyright (C) 2010 Harald Welte <laforge@gnumonks.org> |
| 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 | |
Sylvain Munaut | 530076e | 2010-12-23 20:28:58 +0100 | [diff] [blame^] | 25 | from pySim.utils import rpad, b2h |
| 26 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 27 | |
| 28 | class SimCardCommands(object): |
| 29 | def __init__(self, transport): |
| 30 | self._tp = transport; |
| 31 | |
| 32 | def select_file(self, dir_list): |
| 33 | rv = [] |
| 34 | for i in dir_list: |
| 35 | data, sw = self._tp.send_apdu_checksw("a0a4000002" + i) |
| 36 | rv.append(data) |
| 37 | return rv |
| 38 | |
| 39 | def read_binary(self, ef, length=None, offset=0): |
| 40 | if not hasattr(type(ef), '__iter__'): |
| 41 | ef = [ef] |
| 42 | r = self.select_file(ef) |
| 43 | if length is None: |
| 44 | length = int(r[-1][4:8], 16) - offset |
| 45 | pdu = 'a0b0%04x%02x' % (offset, (min(256, length) & 0xff)) |
| 46 | return self._tp.send_apdu(pdu) |
| 47 | |
| 48 | def update_binary(self, ef, data, offset=0): |
| 49 | if not hasattr(type(ef), '__iter__'): |
| 50 | ef = [ef] |
| 51 | self.select_file(ef) |
| 52 | pdu = 'a0d6%04x%02x' % (offset, len(data)/2) + data |
| 53 | return self._tp.send_apdu(pdu) |
| 54 | |
| 55 | def read_record(self, ef, rec_no): |
| 56 | if not hasattr(type(ef), '__iter__'): |
| 57 | ef = [ef] |
| 58 | r = self.select_file(ef) |
| 59 | rec_length = int(r[-1][28:30], 16) |
| 60 | pdu = 'a0b2%02x04%02x' % (rec_no, rec_length) |
| 61 | return self._tp.send_apdu(pdu) |
| 62 | |
| 63 | def update_record(self, ef, rec_no, data, force_len=False): |
| 64 | if not hasattr(type(ef), '__iter__'): |
| 65 | ef = [ef] |
| 66 | r = self.select_file(ef) |
| 67 | if not force_len: |
| 68 | rec_length = int(r[-1][28:30], 16) |
| 69 | if (len(data)/2 != rec_length): |
| 70 | raise ValueError('Invalid data length (expected %d, got %d)' % (rec_length, len(data)/2)) |
| 71 | else: |
| 72 | rec_length = len(data)/2 |
| 73 | pdu = ('a0dc%02x04%02x' % (rec_no, rec_length)) + data |
| 74 | return self._tp.send_apdu(pdu) |
| 75 | |
| 76 | def record_size(self, ef): |
| 77 | r = self.select_file(ef) |
| 78 | return int(r[-1][28:30], 16) |
| 79 | |
| 80 | def record_count(self, ef): |
| 81 | r = self.select_file(ef) |
| 82 | return int(r[-1][4:8], 16) // int(r[-1][28:30], 16) |
| 83 | |
| 84 | def run_gsm(self, rand): |
| 85 | if len(rand) != 32: |
| 86 | raise ValueError('Invalid rand') |
| 87 | self.select_file(['3f00', '7f20']) |
| 88 | return self._tp.send_apdu('a088000010' + rand) |
| 89 | |
| 90 | def reset_card(self): |
| 91 | return self._tp.reset_card() |
| 92 | |
| 93 | def verify_chv(self, chv_no, code): |
| 94 | fc = rpad(b2h(code), 16) |
Sylvain Munaut | 530076e | 2010-12-23 20:28:58 +0100 | [diff] [blame^] | 95 | return self._tp.send_apdu('a02000' + ('%02x' % chv_no) + '08' + fc) |