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 |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 26 | |
| 27 | class SimCardCommands(object): |
| 28 | def __init__(self, transport): |
| 29 | self._tp = transport; |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 30 | self._cla_byte = "a0" |
Philipp Maier | 4146086 | 2017-03-21 12:05:30 +0100 | [diff] [blame] | 31 | self.sel_ctrl = "0000" |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 32 | |
Philipp Maier | cdfdd41 | 2019-12-20 13:39:24 +0100 | [diff] [blame] | 33 | # Extract a single FCP item from TLV |
| 34 | def __parse_fcp(self, fcp): |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 35 | # see also: ETSI TS 102 221, chapter 11.1.1.3.1 Response for MF, |
| 36 | # DF or ADF |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 37 | from pytlv.TLV import TLV |
Philipp Maier | 91f26d7 | 2019-03-20 12:12:51 +0100 | [diff] [blame] | 38 | 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] | 39 | |
| 40 | # pytlv is case sensitive! |
| 41 | fcp = fcp.lower() |
| 42 | |
| 43 | if fcp[0:2] != '62': |
| 44 | raise ValueError('Tag of the FCP template does not match, expected 62 but got %s'%fcp[0:2]) |
| 45 | |
| 46 | # Unfortunately the spec is not very clear if the FCP length is |
| 47 | # coded as one or two byte vale, so we have to try it out by |
| 48 | # checking if the length of the remaining TLV string matches |
| 49 | # what we get in the length field. |
| 50 | # See also ETSI TS 102 221, chapter 11.1.1.3.0 Base coding. |
| 51 | exp_tlv_len = int(fcp[2:4], 16) |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 52 | if len(fcp[4:]) // 2 == exp_tlv_len: |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 53 | skip = 4 |
| 54 | else: |
| 55 | exp_tlv_len = int(fcp[2:6], 16) |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 56 | if len(fcp[4:]) // 2 == exp_tlv_len: |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 57 | skip = 6 |
| 58 | |
| 59 | # Skip FCP tag and length |
| 60 | tlv = fcp[skip:] |
Philipp Maier | cdfdd41 | 2019-12-20 13:39:24 +0100 | [diff] [blame] | 61 | return tlvparser.parse(tlv) |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 62 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 63 | # Tell the length of a record by the card response |
| 64 | # USIMs respond with an FCP template, which is different |
| 65 | # from what SIMs responds. See also: |
| 66 | # USIM: ETSI TS 102 221, chapter 11.1.1.3 Response Data |
| 67 | # SIM: GSM 11.11, chapter 9.2.1 SELECT |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 68 | def __record_len(self, r): |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 69 | if self.sel_ctrl == "0004": |
Philipp Maier | cdfdd41 | 2019-12-20 13:39:24 +0100 | [diff] [blame] | 70 | tlv_parsed = self.__parse_fcp(r[-1]) |
| 71 | file_descriptor = tlv_parsed['82'] |
| 72 | # See also ETSI TS 102 221, chapter 11.1.1.4.3 File Descriptor |
| 73 | return int(file_descriptor[4:8], 16) |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 74 | else: |
| 75 | return int(r[-1][28:30], 16) |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 76 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 77 | # Tell the length of a binary file. See also comment |
| 78 | # above. |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 79 | def __len(self, r): |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 80 | if self.sel_ctrl == "0004": |
Philipp Maier | cdfdd41 | 2019-12-20 13:39:24 +0100 | [diff] [blame] | 81 | tlv_parsed = self.__parse_fcp(r[-1]) |
| 82 | return int(tlv_parsed['80'], 16) |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 83 | else: |
| 84 | return int(r[-1][4:8], 16) |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 85 | |
Alexander Chemeris | d2d660a | 2017-07-18 16:52:25 +0300 | [diff] [blame] | 86 | def get_atr(self): |
| 87 | return self._tp.get_atr() |
| 88 | |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 89 | @property |
| 90 | def cla_byte(self): |
| 91 | return self._cla_byte |
| 92 | @cla_byte.setter |
| 93 | def cla_byte(self, value): |
| 94 | self._cla_byte = value |
| 95 | |
Philipp Maier | 4146086 | 2017-03-21 12:05:30 +0100 | [diff] [blame] | 96 | @property |
| 97 | def sel_ctrl(self): |
| 98 | return self._sel_ctrl |
| 99 | @sel_ctrl.setter |
| 100 | def sel_ctrl(self, value): |
| 101 | self._sel_ctrl = value |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 102 | |
| 103 | def select_file(self, dir_list): |
| 104 | rv = [] |
Vadim Yanitskiy | edf873d | 2020-02-27 01:40:14 +0700 | [diff] [blame] | 105 | if type(dir_list) is not list: |
| 106 | dir_list = [dir_list] |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 107 | for i in dir_list: |
Philipp Maier | 4146086 | 2017-03-21 12:05:30 +0100 | [diff] [blame] | 108 | 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] | 109 | rv.append(data) |
| 110 | return rv |
| 111 | |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 112 | def select_adf(self, aid): |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 113 | aidlen = ("0" + format(len(aid) // 2, 'x'))[-2:] |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 114 | return self._tp.send_apdu_checksw(self.cla_byte + "a4" + "0404" + aidlen + aid) |
| 115 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 116 | def read_binary(self, ef, length=None, offset=0): |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 117 | r = self.select_file(ef) |
Max | 5491c48 | 2019-01-03 11:29:25 +0100 | [diff] [blame] | 118 | if len(r[-1]) == 0: |
| 119 | return (None, None) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 120 | if length is None: |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 121 | length = self.__len(r) - offset |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 122 | pdu = self.cla_byte + 'b0%04x%02x' % (offset, (min(256, length) & 0xff)) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 123 | return self._tp.send_apdu(pdu) |
| 124 | |
| 125 | def update_binary(self, ef, data, offset=0): |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 126 | self.select_file(ef) |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 127 | pdu = self.cla_byte + 'd6%04x%02x' % (offset, len(data) // 2) + data |
Harald Welte | 982a307 | 2011-03-22 21:47:20 +0100 | [diff] [blame] | 128 | return self._tp.send_apdu_checksw(pdu) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 129 | |
| 130 | def read_record(self, ef, rec_no): |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 131 | r = self.select_file(ef) |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 132 | rec_length = self.__record_len(r) |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 133 | pdu = self.cla_byte + 'b2%02x04%02x' % (rec_no, rec_length) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 134 | return self._tp.send_apdu(pdu) |
| 135 | |
| 136 | def update_record(self, ef, rec_no, data, force_len=False): |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 137 | r = self.select_file(ef) |
| 138 | if not force_len: |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 139 | rec_length = self.__record_len(r) |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 140 | if (len(data) // 2 != rec_length): |
| 141 | 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] | 142 | else: |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 143 | rec_length = len(data) // 2 |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 144 | pdu = (self.cla_byte + 'dc%02x04%02x' % (rec_no, rec_length)) + data |
Harald Welte | 982a307 | 2011-03-22 21:47:20 +0100 | [diff] [blame] | 145 | return self._tp.send_apdu_checksw(pdu) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 146 | |
| 147 | def record_size(self, ef): |
| 148 | r = self.select_file(ef) |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 149 | return self.__record_len(r) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 150 | |
| 151 | def record_count(self, ef): |
| 152 | r = self.select_file(ef) |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 153 | return self.__len(r) // self.__record_len(r) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 154 | |
| 155 | def run_gsm(self, rand): |
| 156 | if len(rand) != 32: |
| 157 | raise ValueError('Invalid rand') |
| 158 | self.select_file(['3f00', '7f20']) |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 159 | return self._tp.send_apdu(self.cla_byte + '88000010' + rand) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 160 | |
| 161 | def reset_card(self): |
| 162 | return self._tp.reset_card() |
| 163 | |
| 164 | def verify_chv(self, chv_no, code): |
| 165 | fc = rpad(b2h(code), 16) |
Neels Hofmeyr | 3ce84d9 | 2017-03-21 13:07:46 +0100 | [diff] [blame] | 166 | return self._tp.send_apdu_checksw(self.cla_byte + '2000' + ('%02X' % chv_no) + '08' + fc) |