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 | |
Philipp Maier | 46f09af | 2021-03-25 20:24:27 +0100 | [diff] [blame] | 24 | from pySim.utils import rpad, b2h, sw_match |
| 25 | from pySim.exceptions import SwMatchError |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 26 | |
| 27 | class SimCardCommands(object): |
| 28 | def __init__(self, transport): |
Daniel Willmann | 677d41b | 2020-10-19 10:34:31 +0200 | [diff] [blame] | 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 |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 68 | def __record_len(self, r) -> int: |
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. |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 79 | def __len(self, r) -> int: |
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 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 86 | def get_atr(self) -> str: |
| 87 | """Return the ATR of the currently inserted card.""" |
Alexander Chemeris | d2d660a | 2017-07-18 16:52:25 +0300 | [diff] [blame] | 88 | return self._tp.get_atr() |
| 89 | |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 90 | @property |
| 91 | def cla_byte(self): |
| 92 | return self._cla_byte |
| 93 | @cla_byte.setter |
| 94 | def cla_byte(self, value): |
| 95 | self._cla_byte = value |
| 96 | |
Philipp Maier | 4146086 | 2017-03-21 12:05:30 +0100 | [diff] [blame] | 97 | @property |
| 98 | def sel_ctrl(self): |
| 99 | return self._sel_ctrl |
| 100 | @sel_ctrl.setter |
| 101 | def sel_ctrl(self, value): |
| 102 | self._sel_ctrl = value |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 103 | |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 104 | def try_select_path(self, dir_list): |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 105 | """ Try to select a specified path given as list of hex-string FIDs""" |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 106 | rv = [] |
| 107 | if type(dir_list) is not list: |
| 108 | dir_list = [dir_list] |
| 109 | for i in dir_list: |
| 110 | data, sw = self._tp.send_apdu(self.cla_byte + "a4" + self.sel_ctrl + "02" + i) |
| 111 | rv.append((data, sw)) |
| 112 | if sw != '9000': |
| 113 | return rv |
| 114 | return rv |
| 115 | |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 116 | def select_path(self, dir_list): |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 117 | """Execute SELECT for an entire list/path of FIDs. |
| 118 | |
| 119 | Args: |
| 120 | dir_list: list of FIDs representing the path to select |
| 121 | |
| 122 | Returns: |
| 123 | list of return values (FCP in hex encoding) for each element of the path |
| 124 | """ |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 125 | rv = [] |
Vadim Yanitskiy | edf873d | 2020-02-27 01:40:14 +0700 | [diff] [blame] | 126 | if type(dir_list) is not list: |
| 127 | dir_list = [dir_list] |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 128 | for i in dir_list: |
Harald Welte | 85484a9 | 2021-01-21 16:08:56 +0100 | [diff] [blame] | 129 | data, sw = self.select_file(i) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 130 | rv.append(data) |
| 131 | return rv |
| 132 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 133 | def select_file(self, fid:str): |
| 134 | """Execute SELECT a given file by FID.""" |
Harald Welte | 85484a9 | 2021-01-21 16:08:56 +0100 | [diff] [blame] | 135 | return self._tp.send_apdu_checksw(self.cla_byte + "a4" + self.sel_ctrl + "02" + fid) |
| 136 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 137 | def select_adf(self, aid:str): |
| 138 | """Execute SELECT a given Applicaiton ADF.""" |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 139 | aidlen = ("0" + format(len(aid) // 2, 'x'))[-2:] |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 140 | return self._tp.send_apdu_checksw(self.cla_byte + "a4" + "0404" + aidlen + aid) |
| 141 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 142 | def read_binary(self, ef, length:int=None, offset:int=0): |
| 143 | """Execute READD BINARY. |
| 144 | |
| 145 | Args: |
| 146 | ef : string or list of strings indicating name or path of transparent EF |
| 147 | length : number of bytes to read |
| 148 | offset : byte offset in file from which to start reading |
| 149 | """ |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 150 | r = self.select_path(ef) |
Max | 5491c48 | 2019-01-03 11:29:25 +0100 | [diff] [blame] | 151 | if len(r[-1]) == 0: |
| 152 | return (None, None) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 153 | if length is None: |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 154 | length = self.__len(r) - offset |
Sebastian Viviani | 0e9f93f | 2020-04-17 16:42:09 +0100 | [diff] [blame] | 155 | total_data = '' |
| 156 | while offset < length: |
| 157 | chunk_len = min(255, length-offset) |
| 158 | pdu = self.cla_byte + 'b0%04x%02x' % (offset, chunk_len) |
| 159 | data,sw = self._tp.send_apdu(pdu) |
| 160 | if sw == '9000': |
| 161 | total_data += data |
| 162 | offset += chunk_len |
| 163 | else: |
| 164 | raise ValueError('Failed to read (offset %d)' % (offset)) |
| 165 | return total_data, sw |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 166 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 167 | def update_binary(self, ef, data:str, offset:int=0, verify:bool=False, conserve:bool=False): |
| 168 | """Execute UPDATE BINARY. |
| 169 | |
| 170 | Args: |
| 171 | ef : string or list of strings indicating name or path of transparent EF |
| 172 | data : hex string of data to be written |
| 173 | offset : byte offset in file from which to start writing |
| 174 | verify : Whether or not to verify data after write |
| 175 | """ |
Philipp Maier | 38c74f6 | 2021-03-17 17:19:52 +0100 | [diff] [blame] | 176 | data_length = len(data) // 2 |
| 177 | |
| 178 | # Save write cycles by reading+comparing before write |
| 179 | if conserve: |
| 180 | data_current, sw = self.read_binary(ef, data_length, offset) |
| 181 | if data_current == data: |
| 182 | return None, sw |
| 183 | |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 184 | self.select_path(ef) |
Philipp Maier | 38c74f6 | 2021-03-17 17:19:52 +0100 | [diff] [blame] | 185 | pdu = self.cla_byte + 'd6%04x%02x' % (offset, data_length) + data |
Philipp Maier | 30eb8ca | 2020-05-11 22:51:37 +0200 | [diff] [blame] | 186 | res = self._tp.send_apdu_checksw(pdu) |
| 187 | if verify: |
| 188 | self.verify_binary(ef, data, offset) |
| 189 | return res |
| 190 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 191 | def verify_binary(self, ef, data:str, offset:int=0): |
| 192 | """Verify contents of transparent EF. |
| 193 | |
| 194 | Args: |
| 195 | ef : string or list of strings indicating name or path of transparent EF |
| 196 | data : hex string of expected data |
| 197 | offset : byte offset in file from which to start verifying |
| 198 | """ |
Philipp Maier | 30eb8ca | 2020-05-11 22:51:37 +0200 | [diff] [blame] | 199 | res = self.read_binary(ef, len(data) // 2, offset) |
| 200 | if res[0].lower() != data.lower(): |
| 201 | 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] | 202 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 203 | def read_record(self, ef, rec_no:int): |
| 204 | """Execute READ RECORD. |
| 205 | |
| 206 | Args: |
| 207 | ef : string or list of strings indicating name or path of linear fixed EF |
| 208 | rec_no : record number to read |
| 209 | """ |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 210 | r = self.select_path(ef) |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 211 | rec_length = self.__record_len(r) |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 212 | pdu = self.cla_byte + 'b2%02x04%02x' % (rec_no, rec_length) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 213 | return self._tp.send_apdu(pdu) |
| 214 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 215 | def update_record(self, ef, rec_no:int, data:str, force_len:bool=False, verify:bool=False, |
| 216 | conserve:bool=False): |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 217 | r = self.select_path(ef) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 218 | if not force_len: |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 219 | rec_length = self.__record_len(r) |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 220 | if (len(data) // 2 != rec_length): |
| 221 | 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] | 222 | else: |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 223 | rec_length = len(data) // 2 |
Philipp Maier | 38c74f6 | 2021-03-17 17:19:52 +0100 | [diff] [blame] | 224 | |
| 225 | # Save write cycles by reading+comparing before write |
| 226 | if conserve: |
| 227 | data_current, sw = self.read_record(ef, rec_no) |
| 228 | data_current = data_current[0:rec_length*2] |
| 229 | if data_current == data: |
| 230 | return None, sw |
| 231 | |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 232 | pdu = (self.cla_byte + 'dc%02x04%02x' % (rec_no, rec_length)) + data |
Philipp Maier | 30eb8ca | 2020-05-11 22:51:37 +0200 | [diff] [blame] | 233 | res = self._tp.send_apdu_checksw(pdu) |
| 234 | if verify: |
| 235 | self.verify_record(ef, rec_no, data) |
| 236 | return res |
| 237 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 238 | def verify_record(self, ef, rec_no:int, data:str): |
Philipp Maier | 30eb8ca | 2020-05-11 22:51:37 +0200 | [diff] [blame] | 239 | res = self.read_record(ef, rec_no) |
| 240 | if res[0].lower() != data.lower(): |
| 241 | 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] | 242 | |
| 243 | def record_size(self, ef): |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 244 | """Determine the record size of given file. |
| 245 | |
| 246 | Args: |
| 247 | ef : string or list of strings indicating name or path of linear fixed EF |
| 248 | """ |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 249 | r = self.select_path(ef) |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 250 | return self.__record_len(r) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 251 | |
| 252 | def record_count(self, ef): |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 253 | """Determine the number of records in given file. |
| 254 | |
| 255 | Args: |
| 256 | ef : string or list of strings indicating name or path of linear fixed EF |
| 257 | """ |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 258 | r = self.select_path(ef) |
Philipp Maier | 0e3fcaa | 2018-06-13 12:34:03 +0200 | [diff] [blame] | 259 | return self.__len(r) // self.__record_len(r) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 260 | |
Philipp Maier | 32daaf5 | 2020-05-11 21:48:33 +0200 | [diff] [blame] | 261 | def binary_size(self, ef): |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 262 | """Determine the size of given transparent file. |
| 263 | |
| 264 | Args: |
| 265 | ef : string or list of strings indicating name or path of transparent EF |
| 266 | """ |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 267 | r = self.select_path(ef) |
Philipp Maier | 32daaf5 | 2020-05-11 21:48:33 +0200 | [diff] [blame] | 268 | return self.__len(r) |
| 269 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 270 | def run_gsm(self, rand:str): |
| 271 | """Execute RUN GSM ALGORITHM.""" |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 272 | if len(rand) != 32: |
| 273 | raise ValueError('Invalid rand') |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 274 | self.select_path(['3f00', '7f20']) |
Jan Balke | 14b350f | 2015-01-26 11:15:25 +0100 | [diff] [blame] | 275 | return self._tp.send_apdu(self.cla_byte + '88000010' + rand) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 276 | |
| 277 | def reset_card(self): |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 278 | """Physically reset the card""" |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 279 | return self._tp.reset_card() |
| 280 | |
Philipp Maier | 46f09af | 2021-03-25 20:24:27 +0100 | [diff] [blame] | 281 | def _chv_process_sw(self, op_name, chv_no, pin_code, sw): |
| 282 | if sw_match(sw, '63cx'): |
| 283 | raise RuntimeError('Failed to %s chv_no 0x%02X with code 0x%s, %i tries left.' % |
| 284 | (op_name, chv_no, b2h(pin_code).upper(), int(sw[3]))) |
| 285 | elif (sw != '9000'): |
| 286 | raise SwMatchError(sw, '9000') |
| 287 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 288 | def verify_chv(self, chv_no:int, code:str): |
| 289 | """Verify a given CHV (Card Holder Verification == PIN)""" |
| 290 | fc = rpad(b2h(code), 16) |
Philipp Maier | a31e9a9 | 2021-03-11 13:46:32 +0100 | [diff] [blame] | 291 | data, sw = self._tp.send_apdu(self.cla_byte + '2000' + ('%02X' % chv_no) + '08' + fc) |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 292 | self._chv_process_sw('verify', chv_no, code, sw) |
Philipp Maier | 46f09af | 2021-03-25 20:24:27 +0100 | [diff] [blame] | 293 | return (data, sw) |
| 294 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 295 | def unblock_chv(self, chv_no:int, puk_code:str, pin_code:str): |
| 296 | """Unblock a given CHV (Card Holder Verification == PIN)""" |
Philipp Maier | 46f09af | 2021-03-25 20:24:27 +0100 | [diff] [blame] | 297 | fc = rpad(b2h(puk_code), 16) + rpad(b2h(pin_code), 16) |
| 298 | data, sw = self._tp.send_apdu(self.cla_byte + '2C00' + ('%02X' % chv_no) + '10' + fc) |
| 299 | self._chv_process_sw('unblock', chv_no, pin_code, sw) |
| 300 | return (data, sw) |
| 301 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 302 | def change_chv(self, chv_no:int, pin_code:str, new_pin_code:str): |
| 303 | """Change a given CHV (Card Holder Verification == PIN)""" |
Philipp Maier | 46f09af | 2021-03-25 20:24:27 +0100 | [diff] [blame] | 304 | fc = rpad(b2h(pin_code), 16) + rpad(b2h(new_pin_code), 16) |
| 305 | data, sw = self._tp.send_apdu(self.cla_byte + '2400' + ('%02X' % chv_no) + '10' + fc) |
| 306 | self._chv_process_sw('change', chv_no, pin_code, sw) |
| 307 | return (data, sw) |
| 308 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 309 | def disable_chv(self, chv_no:int, pin_code:str): |
| 310 | """Disable a given CHV (Card Holder Verification == PIN)""" |
Philipp Maier | 46f09af | 2021-03-25 20:24:27 +0100 | [diff] [blame] | 311 | fc = rpad(b2h(pin_code), 16) |
| 312 | data, sw = self._tp.send_apdu(self.cla_byte + '2600' + ('%02X' % chv_no) + '08' + fc) |
| 313 | self._chv_process_sw('disable', chv_no, pin_code, sw) |
| 314 | return (data, sw) |
| 315 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 316 | def enable_chv(self, chv_no:int, pin_code:str): |
| 317 | """Enable a given CHV (Card Holder Verification == PIN)""" |
Philipp Maier | 46f09af | 2021-03-25 20:24:27 +0100 | [diff] [blame] | 318 | fc = rpad(b2h(pin_code), 16) |
| 319 | data, sw = self._tp.send_apdu(self.cla_byte + '2800' + ('%02X' % chv_no) + '08' + fc) |
| 320 | self._chv_process_sw('enable', chv_no, pin_code, sw) |
| 321 | return (data, sw) |