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; |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 31 | self._cla_byte = "a0" |
Philipp Maier | 4146086 | 2017-03-21 12:05:30 +0100 | [diff] [blame] | 32 | self.sel_ctrl = "0000" |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 33 | |
Alexander Chemeris | d2d660a | 2017-07-18 16:52:25 +0300 | [diff] [blame] | 34 | def get_atr(self): |
| 35 | return self._tp.get_atr() |
| 36 | |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 37 | @property |
| 38 | def cla_byte(self): |
| 39 | return self._cla_byte |
| 40 | @cla_byte.setter |
| 41 | def cla_byte(self, value): |
| 42 | self._cla_byte = value |
| 43 | |
Philipp Maier | 4146086 | 2017-03-21 12:05:30 +0100 | [diff] [blame] | 44 | @property |
| 45 | def sel_ctrl(self): |
| 46 | return self._sel_ctrl |
| 47 | @sel_ctrl.setter |
| 48 | def sel_ctrl(self, value): |
| 49 | self._sel_ctrl = value |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 50 | |
| 51 | def select_file(self, dir_list): |
| 52 | rv = [] |
| 53 | for i in dir_list: |
Philipp Maier | 4146086 | 2017-03-21 12:05:30 +0100 | [diff] [blame] | 54 | data, sw = self._tp.send_apdu_checksw(self.cla_byte + "a4" + self.sel_ctrl + "02" + i) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 55 | rv.append(data) |
| 56 | return rv |
| 57 | |
| 58 | def read_binary(self, ef, length=None, offset=0): |
| 59 | if not hasattr(type(ef), '__iter__'): |
| 60 | ef = [ef] |
| 61 | r = self.select_file(ef) |
| 62 | if length is None: |
| 63 | length = int(r[-1][4:8], 16) - offset |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 64 | pdu = self.cla_byte + 'b0%04x%02x' % (offset, (min(256, length) & 0xff)) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 65 | return self._tp.send_apdu(pdu) |
| 66 | |
| 67 | def update_binary(self, ef, data, offset=0): |
| 68 | if not hasattr(type(ef), '__iter__'): |
| 69 | ef = [ef] |
| 70 | self.select_file(ef) |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 71 | pdu = self.cla_byte + 'd6%04x%02x' % (offset, len(data)/2) + data |
Harald Welte | 982a307 | 2011-03-22 21:47:20 +0100 | [diff] [blame] | 72 | return self._tp.send_apdu_checksw(pdu) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 73 | |
| 74 | def read_record(self, ef, rec_no): |
| 75 | if not hasattr(type(ef), '__iter__'): |
| 76 | ef = [ef] |
| 77 | r = self.select_file(ef) |
| 78 | rec_length = int(r[-1][28:30], 16) |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 79 | pdu = self.cla_byte + 'b2%02x04%02x' % (rec_no, rec_length) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 80 | return self._tp.send_apdu(pdu) |
| 81 | |
| 82 | def update_record(self, ef, rec_no, data, force_len=False): |
| 83 | if not hasattr(type(ef), '__iter__'): |
| 84 | ef = [ef] |
| 85 | r = self.select_file(ef) |
| 86 | if not force_len: |
| 87 | rec_length = int(r[-1][28:30], 16) |
| 88 | if (len(data)/2 != rec_length): |
| 89 | raise ValueError('Invalid data length (expected %d, got %d)' % (rec_length, len(data)/2)) |
| 90 | else: |
| 91 | rec_length = len(data)/2 |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 92 | pdu = (self.cla_byte + 'dc%02x04%02x' % (rec_no, rec_length)) + data |
Harald Welte | 982a307 | 2011-03-22 21:47:20 +0100 | [diff] [blame] | 93 | return self._tp.send_apdu_checksw(pdu) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 94 | |
| 95 | def record_size(self, ef): |
| 96 | r = self.select_file(ef) |
| 97 | return int(r[-1][28:30], 16) |
| 98 | |
| 99 | def record_count(self, ef): |
| 100 | r = self.select_file(ef) |
| 101 | return int(r[-1][4:8], 16) // int(r[-1][28:30], 16) |
| 102 | |
| 103 | def run_gsm(self, rand): |
| 104 | if len(rand) != 32: |
| 105 | raise ValueError('Invalid rand') |
| 106 | self.select_file(['3f00', '7f20']) |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 107 | return self._tp.send_apdu(self.cla_byte + '88000010' + rand) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 108 | |
| 109 | def reset_card(self): |
| 110 | return self._tp.reset_card() |
| 111 | |
| 112 | def verify_chv(self, chv_no, code): |
| 113 | fc = rpad(b2h(code), 16) |
Neels Hofmeyr | 3ce84d9 | 2017-03-21 13:07:46 +0100 | [diff] [blame] | 114 | return self._tp.send_apdu_checksw(self.cla_byte + '2000' + ('%02X' % chv_no) + '08' + fc) |