Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | """ pySim: SIM Card commands according to ISO 7816-4 and TS 11.11 |
| 4 | """ |
| 5 | |
| 6 | # |
| 7 | # Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com> |
| 8 | # Copyright (C) 2010 Harald Welte <laforge@gnumonks.org> |
| 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 | |
Sylvain Munaut | 530076e | 2010-12-23 20:28:58 +0100 | [diff] [blame] | 24 | from pySim.utils import rpad, b2h |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 25 | |
| 26 | class SimCardCommands(object): |
| 27 | def __init__(self, transport): |
Daniel Willmann | 677d41b | 2020-10-19 10:34:31 +0200 | [diff] [blame] | 28 | self._tp = transport |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 29 | self._cla_byte = "a0" |
Philipp Maier | 4146086 | 2017-03-21 12:05:30 +0100 | [diff] [blame] | 30 | self.sel_ctrl = "0000" |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 31 | |
Philipp Maier | cdfdd41 | 2019-12-20 13:39:24 +0100 | [diff] [blame] | 32 | # Extract a single FCP item from TLV |
| 33 | def __parse_fcp(self, fcp): |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 34 | # see also: ETSI TS 102 221, chapter 11.1.1.3.1 Response for MF, |
| 35 | # DF or ADF |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 36 | from pytlv.TLV import TLV |
Philipp Maier | 91f26d7 | 2019-03-20 12:12:51 +0100 | [diff] [blame] | 37 | tlvparser = TLV(['82', '83', '84', 'a5', '8a', '8b', '8c', '80', 'ab', 'c6', '81', '88']) |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 38 | |
| 39 | # pytlv is case sensitive! |
| 40 | fcp = fcp.lower() |
| 41 | |
| 42 | if fcp[0:2] != '62': |
| 43 | raise ValueError('Tag of the FCP template does not match, expected 62 but got %s'%fcp[0:2]) |
| 44 | |
| 45 | # Unfortunately the spec is not very clear if the FCP length is |
| 46 | # coded as one or two byte vale, so we have to try it out by |
| 47 | # checking if the length of the remaining TLV string matches |
| 48 | # what we get in the length field. |
| 49 | # See also ETSI TS 102 221, chapter 11.1.1.3.0 Base coding. |
| 50 | exp_tlv_len = int(fcp[2:4], 16) |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 51 | if len(fcp[4:]) // 2 == exp_tlv_len: |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 52 | skip = 4 |
| 53 | else: |
| 54 | exp_tlv_len = int(fcp[2:6], 16) |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 55 | if len(fcp[4:]) // 2 == exp_tlv_len: |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 56 | skip = 6 |
| 57 | |
| 58 | # Skip FCP tag and length |
| 59 | tlv = fcp[skip:] |
Philipp Maier | cdfdd41 | 2019-12-20 13:39:24 +0100 | [diff] [blame] | 60 | return tlvparser.parse(tlv) |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 61 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 62 | # Tell the length of a record by the card response |
| 63 | # USIMs respond with an FCP template, which is different |
| 64 | # from what SIMs responds. See also: |
| 65 | # USIM: ETSI TS 102 221, chapter 11.1.1.3 Response Data |
| 66 | # SIM: GSM 11.11, chapter 9.2.1 SELECT |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 67 | def __record_len(self, r): |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 68 | if self.sel_ctrl == "0004": |
Philipp Maier | cdfdd41 | 2019-12-20 13:39:24 +0100 | [diff] [blame] | 69 | tlv_parsed = self.__parse_fcp(r[-1]) |
| 70 | file_descriptor = tlv_parsed['82'] |
| 71 | # See also ETSI TS 102 221, chapter 11.1.1.4.3 File Descriptor |
| 72 | return int(file_descriptor[4:8], 16) |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 73 | else: |
| 74 | return int(r[-1][28:30], 16) |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 75 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 76 | # Tell the length of a binary file. See also comment |
| 77 | # above. |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 78 | def __len(self, r): |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 79 | if self.sel_ctrl == "0004": |
Philipp Maier | cdfdd41 | 2019-12-20 13:39:24 +0100 | [diff] [blame] | 80 | tlv_parsed = self.__parse_fcp(r[-1]) |
| 81 | return int(tlv_parsed['80'], 16) |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 82 | else: |
| 83 | return int(r[-1][4:8], 16) |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 84 | |
Alexander Chemeris | d2d660a | 2017-07-18 16:52:25 +0300 | [diff] [blame] | 85 | def get_atr(self): |
| 86 | return self._tp.get_atr() |
| 87 | |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 88 | @property |
| 89 | def cla_byte(self): |
| 90 | return self._cla_byte |
| 91 | @cla_byte.setter |
| 92 | def cla_byte(self, value): |
| 93 | self._cla_byte = value |
| 94 | |
Philipp Maier | 4146086 | 2017-03-21 12:05:30 +0100 | [diff] [blame] | 95 | @property |
| 96 | def sel_ctrl(self): |
| 97 | return self._sel_ctrl |
| 98 | @sel_ctrl.setter |
| 99 | def sel_ctrl(self, value): |
| 100 | self._sel_ctrl = value |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 101 | |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 102 | def try_select_path(self, dir_list): |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 103 | rv = [] |
| 104 | if type(dir_list) is not list: |
| 105 | dir_list = [dir_list] |
| 106 | for i in dir_list: |
| 107 | data, sw = self._tp.send_apdu(self.cla_byte + "a4" + self.sel_ctrl + "02" + i) |
| 108 | rv.append((data, sw)) |
| 109 | if sw != '9000': |
| 110 | return rv |
| 111 | return rv |
| 112 | |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 113 | def select_path(self, dir_list): |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 114 | rv = [] |
Vadim Yanitskiy | edf873d | 2020-02-27 01:40:14 +0700 | [diff] [blame] | 115 | if type(dir_list) is not list: |
| 116 | dir_list = [dir_list] |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 117 | for i in dir_list: |
Harald Welte | 85484a9 | 2021-01-21 16:08:56 +0100 | [diff] [blame] | 118 | data, sw = self.select_file(i) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 119 | rv.append(data) |
| 120 | return rv |
| 121 | |
Harald Welte | 85484a9 | 2021-01-21 16:08:56 +0100 | [diff] [blame] | 122 | def select_file(self, fid): |
| 123 | return self._tp.send_apdu_checksw(self.cla_byte + "a4" + self.sel_ctrl + "02" + fid) |
| 124 | |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 125 | def select_adf(self, aid): |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 126 | aidlen = ("0" + format(len(aid) // 2, 'x'))[-2:] |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 127 | return self._tp.send_apdu_checksw(self.cla_byte + "a4" + "0404" + aidlen + aid) |
| 128 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 129 | def read_binary(self, ef, length=None, offset=0): |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 130 | r = self.select_path(ef) |
Max | 5491c48 | 2019-01-03 11:29:25 +0100 | [diff] [blame] | 131 | if len(r[-1]) == 0: |
| 132 | return (None, None) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 133 | if length is None: |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 134 | length = self.__len(r) - offset |
Sebastian Viviani | 0e9f93f | 2020-04-17 16:42:09 +0100 | [diff] [blame] | 135 | total_data = '' |
| 136 | while offset < length: |
| 137 | chunk_len = min(255, length-offset) |
| 138 | pdu = self.cla_byte + 'b0%04x%02x' % (offset, chunk_len) |
| 139 | data,sw = self._tp.send_apdu(pdu) |
| 140 | if sw == '9000': |
| 141 | total_data += data |
| 142 | offset += chunk_len |
| 143 | else: |
| 144 | raise ValueError('Failed to read (offset %d)' % (offset)) |
| 145 | return total_data, sw |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 146 | |
Philipp Maier | 30eb8ca | 2020-05-11 22:51:37 +0200 | [diff] [blame] | 147 | def update_binary(self, ef, data, offset=0, verify=False): |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 148 | self.select_path(ef) |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 149 | pdu = self.cla_byte + 'd6%04x%02x' % (offset, len(data) // 2) + data |
Philipp Maier | 30eb8ca | 2020-05-11 22:51:37 +0200 | [diff] [blame] | 150 | res = self._tp.send_apdu_checksw(pdu) |
| 151 | if verify: |
| 152 | self.verify_binary(ef, data, offset) |
| 153 | return res |
| 154 | |
| 155 | def verify_binary(self, ef, data, offset=0): |
| 156 | res = self.read_binary(ef, len(data) // 2, offset) |
| 157 | if res[0].lower() != data.lower(): |
| 158 | raise ValueError('Binary verification failed (expected %s, got %s)' % (data.lower(), res[0].lower())) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 159 | |
| 160 | def read_record(self, ef, rec_no): |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 161 | r = self.select_path(ef) |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 162 | rec_length = self.__record_len(r) |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 163 | pdu = self.cla_byte + 'b2%02x04%02x' % (rec_no, rec_length) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 164 | return self._tp.send_apdu(pdu) |
| 165 | |
Philipp Maier | 30eb8ca | 2020-05-11 22:51:37 +0200 | [diff] [blame] | 166 | def update_record(self, ef, rec_no, data, force_len=False, verify=False): |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 167 | r = self.select_path(ef) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 168 | if not force_len: |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 169 | rec_length = self.__record_len(r) |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 170 | if (len(data) // 2 != rec_length): |
| 171 | raise ValueError('Invalid data length (expected %d, got %d)' % (rec_length, len(data) // 2)) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 172 | else: |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 173 | rec_length = len(data) // 2 |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 174 | pdu = (self.cla_byte + 'dc%02x04%02x' % (rec_no, rec_length)) + data |
Philipp Maier | 30eb8ca | 2020-05-11 22:51:37 +0200 | [diff] [blame] | 175 | res = self._tp.send_apdu_checksw(pdu) |
| 176 | if verify: |
| 177 | self.verify_record(ef, rec_no, data) |
| 178 | return res |
| 179 | |
| 180 | def verify_record(self, ef, rec_no, data): |
| 181 | res = self.read_record(ef, rec_no) |
| 182 | if res[0].lower() != data.lower(): |
| 183 | raise ValueError('Record verification failed (expected %s, got %s)' % (data.lower(), res[0].lower())) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 184 | |
| 185 | def record_size(self, ef): |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 186 | r = self.select_path(ef) |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 187 | return self.__record_len(r) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 188 | |
| 189 | def record_count(self, ef): |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 190 | r = self.select_path(ef) |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 191 | return self.__len(r) // self.__record_len(r) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 192 | |
Philipp Maier | 32daaf5 | 2020-05-11 21:48:33 +0200 | [diff] [blame] | 193 | def binary_size(self, ef): |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 194 | r = self.select_path(ef) |
Philipp Maier | 32daaf5 | 2020-05-11 21:48:33 +0200 | [diff] [blame] | 195 | return self.__len(r) |
| 196 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 197 | def run_gsm(self, rand): |
| 198 | if len(rand) != 32: |
| 199 | raise ValueError('Invalid rand') |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 200 | self.select_path(['3f00', '7f20']) |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 201 | return self._tp.send_apdu(self.cla_byte + '88000010' + rand) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 202 | |
| 203 | def reset_card(self): |
| 204 | return self._tp.reset_card() |
| 205 | |
| 206 | def verify_chv(self, chv_no, code): |
| 207 | fc = rpad(b2h(code), 16) |
Neels Hofmeyr | 3ce84d9 | 2017-03-21 13:07:46 +0100 | [diff] [blame] | 208 | return self._tp.send_apdu_checksw(self.cla_byte + '2000' + ('%02X' % chv_no) + '08' + fc) |