Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | """ pySim: Card programmation logic |
| 4 | """ |
| 5 | |
| 6 | # |
| 7 | # Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com> |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 8 | # Copyright (C) 2011 Harald Welte <laforge@gnumonks.org> |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 9 | # Copyright (C) 2017 Alexander.Chemeris <Alexander.Chemeris@gmail.com> |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 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 | |
Vadim Yanitskiy | 03c67f7 | 2021-05-02 02:10:39 +0200 | [diff] [blame] | 25 | from typing import Optional, Dict, Tuple |
Vadim Yanitskiy | 85302d6 | 2021-05-02 02:18:42 +0200 | [diff] [blame] | 26 | import abc |
Vadim Yanitskiy | 03c67f7 | 2021-05-02 02:10:39 +0200 | [diff] [blame] | 27 | |
Robert Falkenberg | d0505bd | 2021-02-24 14:06:18 +0100 | [diff] [blame] | 28 | from pySim.ts_51_011 import EF, DF, EF_AD |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 29 | from pySim.ts_31_102 import EF_USIM_ADF_map |
Supreeth Herle | 5ad9aec | 2020-03-24 17:26:40 +0100 | [diff] [blame] | 30 | from pySim.ts_31_103 import EF_ISIM_ADF_map |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 31 | from pySim.utils import * |
Alexander Chemeris | 8ad124a | 2018-01-10 14:17:55 +0900 | [diff] [blame] | 32 | from smartcard.util import toBytes |
Supreeth Herle | 79f43dd | 2020-03-25 11:43:19 +0100 | [diff] [blame] | 33 | from pytlv.TLV import * |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 34 | |
| 35 | class Card(object): |
| 36 | |
| 37 | def __init__(self, scc): |
| 38 | self._scc = scc |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 39 | self._adm_chv_num = 4 |
Supreeth Herle | e4e9831 | 2020-03-18 11:33:14 +0100 | [diff] [blame] | 40 | self._aids = [] |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 41 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 42 | def reset(self): |
| 43 | self._scc.reset_card() |
| 44 | |
Philipp Maier | d58c632 | 2020-05-12 16:47:45 +0200 | [diff] [blame] | 45 | def erase(self): |
| 46 | print("warning: erasing is not supported for specified card type!") |
| 47 | return |
| 48 | |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 49 | def file_exists(self, fid): |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 50 | res_arr = self._scc.try_select_path(fid) |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 51 | for res in res_arr: |
Harald Welte | 1e42420 | 2020-08-31 15:04:19 +0200 | [diff] [blame] | 52 | if res[1] != '9000': |
| 53 | return False |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 54 | return True |
| 55 | |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 56 | def verify_adm(self, key): |
| 57 | ''' |
| 58 | Authenticate with ADM key |
| 59 | ''' |
| 60 | (res, sw) = self._scc.verify_chv(self._adm_chv_num, key) |
| 61 | return sw |
| 62 | |
| 63 | def read_iccid(self): |
| 64 | (res, sw) = self._scc.read_binary(EF['ICCID']) |
| 65 | if sw == '9000': |
| 66 | return (dec_iccid(res), sw) |
| 67 | else: |
| 68 | return (None, sw) |
| 69 | |
| 70 | def read_imsi(self): |
| 71 | (res, sw) = self._scc.read_binary(EF['IMSI']) |
| 72 | if sw == '9000': |
| 73 | return (dec_imsi(res), sw) |
| 74 | else: |
| 75 | return (None, sw) |
| 76 | |
| 77 | def update_imsi(self, imsi): |
| 78 | data, sw = self._scc.update_binary(EF['IMSI'], enc_imsi(imsi)) |
| 79 | return sw |
| 80 | |
| 81 | def update_acc(self, acc): |
Robert Falkenberg | 75487ae | 2021-04-01 16:14:27 +0200 | [diff] [blame] | 82 | data, sw = self._scc.update_binary(EF['ACC'], lpad(acc, 4, c='0')) |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 83 | return sw |
| 84 | |
Supreeth Herle | a850a47 | 2020-03-19 12:44:11 +0100 | [diff] [blame] | 85 | def read_hplmn_act(self): |
| 86 | (res, sw) = self._scc.read_binary(EF['HPLMNAcT']) |
| 87 | if sw == '9000': |
| 88 | return (format_xplmn_w_act(res), sw) |
| 89 | else: |
| 90 | return (None, sw) |
| 91 | |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 92 | def update_hplmn_act(self, mcc, mnc, access_tech='FFFF'): |
| 93 | """ |
| 94 | Update Home PLMN with access technology bit-field |
| 95 | |
| 96 | See Section "10.3.37 EFHPLMNwAcT (HPLMN Selector with Access Technology)" |
| 97 | in ETSI TS 151 011 for the details of the access_tech field coding. |
| 98 | Some common values: |
| 99 | access_tech = '0080' # Only GSM is selected |
Harald Welte | c9cdce3 | 2021-04-11 10:28:28 +0200 | [diff] [blame] | 100 | access_tech = 'FFFF' # All technologies selected, even Reserved for Future Use ones |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 101 | """ |
| 102 | # get size and write EF.HPLMNwAcT |
Supreeth Herle | 2d78597 | 2019-11-30 11:00:10 +0100 | [diff] [blame] | 103 | data = self._scc.read_binary(EF['HPLMNwAcT'], length=None, offset=0) |
Vadim Yanitskiy | 9664b2e | 2020-02-27 01:49:51 +0700 | [diff] [blame] | 104 | size = len(data[0]) // 2 |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 105 | hplmn = enc_plmn(mcc, mnc) |
| 106 | content = hplmn + access_tech |
Vadim Yanitskiy | 9664b2e | 2020-02-27 01:49:51 +0700 | [diff] [blame] | 107 | data, sw = self._scc.update_binary(EF['HPLMNwAcT'], content + 'ffffff0000' * (size // 5 - 1)) |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 108 | return sw |
| 109 | |
Supreeth Herle | 1757b26 | 2020-03-19 12:43:11 +0100 | [diff] [blame] | 110 | def read_oplmn_act(self): |
| 111 | (res, sw) = self._scc.read_binary(EF['OPLMNwAcT']) |
| 112 | if sw == '9000': |
| 113 | return (format_xplmn_w_act(res), sw) |
| 114 | else: |
| 115 | return (None, sw) |
| 116 | |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 117 | def update_oplmn_act(self, mcc, mnc, access_tech='FFFF'): |
| 118 | """ |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 119 | See note in update_hplmn_act() |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 120 | """ |
| 121 | # get size and write EF.OPLMNwAcT |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 122 | data = self._scc.read_binary(EF['OPLMNwAcT'], length=None, offset=0) |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 123 | size = len(data[0]) // 2 |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 124 | hplmn = enc_plmn(mcc, mnc) |
| 125 | content = hplmn + access_tech |
Vadim Yanitskiy | 9664b2e | 2020-02-27 01:49:51 +0700 | [diff] [blame] | 126 | data, sw = self._scc.update_binary(EF['OPLMNwAcT'], content + 'ffffff0000' * (size // 5 - 1)) |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 127 | return sw |
| 128 | |
Supreeth Herle | 1408440 | 2020-03-19 12:42:10 +0100 | [diff] [blame] | 129 | def read_plmn_act(self): |
| 130 | (res, sw) = self._scc.read_binary(EF['PLMNwAcT']) |
| 131 | if sw == '9000': |
| 132 | return (format_xplmn_w_act(res), sw) |
| 133 | else: |
| 134 | return (None, sw) |
| 135 | |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 136 | def update_plmn_act(self, mcc, mnc, access_tech='FFFF'): |
| 137 | """ |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 138 | See note in update_hplmn_act() |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 139 | """ |
| 140 | # get size and write EF.PLMNwAcT |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 141 | data = self._scc.read_binary(EF['PLMNwAcT'], length=None, offset=0) |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 142 | size = len(data[0]) // 2 |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 143 | hplmn = enc_plmn(mcc, mnc) |
| 144 | content = hplmn + access_tech |
Vadim Yanitskiy | 9664b2e | 2020-02-27 01:49:51 +0700 | [diff] [blame] | 145 | data, sw = self._scc.update_binary(EF['PLMNwAcT'], content + 'ffffff0000' * (size // 5 - 1)) |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 146 | return sw |
| 147 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 148 | def update_plmnsel(self, mcc, mnc): |
| 149 | data = self._scc.read_binary(EF['PLMNsel'], length=None, offset=0) |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 150 | size = len(data[0]) // 2 |
Philipp Maier | 5bf4260 | 2018-07-11 23:23:40 +0200 | [diff] [blame] | 151 | hplmn = enc_plmn(mcc, mnc) |
Philipp Maier | af9ae8b | 2018-07-13 11:15:49 +0200 | [diff] [blame] | 152 | data, sw = self._scc.update_binary(EF['PLMNsel'], hplmn + 'ff' * (size-3)) |
| 153 | return sw |
Philipp Maier | 5bf4260 | 2018-07-11 23:23:40 +0200 | [diff] [blame] | 154 | |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 155 | def update_smsp(self, smsp): |
| 156 | data, sw = self._scc.update_record(EF['SMSP'], 1, rpad(smsp, 84)) |
| 157 | return sw |
| 158 | |
Robert Falkenberg | d0505bd | 2021-02-24 14:06:18 +0100 | [diff] [blame] | 159 | def update_ad(self, mnc=None, opmode=None, ofm=None): |
| 160 | """ |
| 161 | Update Administrative Data (AD) |
Philipp Maier | ee908ae | 2019-03-21 16:21:12 +0100 | [diff] [blame] | 162 | |
Robert Falkenberg | d0505bd | 2021-02-24 14:06:18 +0100 | [diff] [blame] | 163 | See Sec. "4.2.18 EF_AD (Administrative Data)" |
| 164 | in 3GPP TS 31.102 for the details of the EF_AD contents. |
Philipp Maier | 7f9f64a | 2020-05-11 21:28:52 +0200 | [diff] [blame] | 165 | |
Robert Falkenberg | d0505bd | 2021-02-24 14:06:18 +0100 | [diff] [blame] | 166 | Set any parameter to None to keep old value(s) on card. |
Philipp Maier | 7f9f64a | 2020-05-11 21:28:52 +0200 | [diff] [blame] | 167 | |
Robert Falkenberg | d0505bd | 2021-02-24 14:06:18 +0100 | [diff] [blame] | 168 | Parameters: |
| 169 | mnc (str): MNC of IMSI |
| 170 | opmode (Hex-str, 1 Byte): MS Operation Mode |
| 171 | ofm (Hex-str, 1 Byte): Operational Feature Monitor (OFM) aka Ciphering Indicator |
| 172 | |
| 173 | Returns: |
| 174 | str: Return code of write operation |
| 175 | """ |
| 176 | |
| 177 | ad = EF_AD() |
| 178 | |
| 179 | # read from card |
| 180 | raw_hex_data, sw = self._scc.read_binary(EF['AD'], length=None, offset=0) |
Robert Falkenberg | 9d16fbc | 2021-04-12 11:43:22 +0200 | [diff] [blame] | 181 | abstract_data = ad.decode_hex(raw_hex_data) |
Robert Falkenberg | d0505bd | 2021-02-24 14:06:18 +0100 | [diff] [blame] | 182 | |
| 183 | # perform updates |
Robert Falkenberg | 9d16fbc | 2021-04-12 11:43:22 +0200 | [diff] [blame] | 184 | if mnc and abstract_data['extensions']: |
Robert Falkenberg | d0505bd | 2021-02-24 14:06:18 +0100 | [diff] [blame] | 185 | mnclen = len(str(mnc)) |
| 186 | if mnclen == 1: |
| 187 | mnclen = 2 |
| 188 | if mnclen > 3: |
| 189 | raise RuntimeError('invalid length of mnc "{}"'.format(mnc)) |
Robert Falkenberg | 9d16fbc | 2021-04-12 11:43:22 +0200 | [diff] [blame] | 190 | abstract_data['extensions']['mnc_len'] = mnclen |
Robert Falkenberg | d0505bd | 2021-02-24 14:06:18 +0100 | [diff] [blame] | 191 | if opmode: |
Robert Falkenberg | 9d16fbc | 2021-04-12 11:43:22 +0200 | [diff] [blame] | 192 | opmode_num = int(opmode, 16) |
| 193 | if opmode_num in [int(v) for v in EF_AD.OP_MODE]: |
| 194 | abstract_data['ms_operation_mode'] = opmode_num |
Robert Falkenberg | d0505bd | 2021-02-24 14:06:18 +0100 | [diff] [blame] | 195 | else: |
| 196 | raise RuntimeError('invalid opmode "{}"'.format(opmode)) |
| 197 | if ofm: |
Robert Falkenberg | 9d16fbc | 2021-04-12 11:43:22 +0200 | [diff] [blame] | 198 | abstract_data['ofm'] = bool(int(ofm, 16)) |
Robert Falkenberg | d0505bd | 2021-02-24 14:06:18 +0100 | [diff] [blame] | 199 | |
| 200 | # write to card |
Robert Falkenberg | 9d16fbc | 2021-04-12 11:43:22 +0200 | [diff] [blame] | 201 | raw_hex_data = ad.encode_hex(abstract_data) |
Robert Falkenberg | d0505bd | 2021-02-24 14:06:18 +0100 | [diff] [blame] | 202 | data, sw = self._scc.update_binary(EF['AD'], raw_hex_data) |
Philipp Maier | ee908ae | 2019-03-21 16:21:12 +0100 | [diff] [blame] | 203 | return sw |
| 204 | |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 205 | def read_spn(self): |
| 206 | (spn, sw) = self._scc.read_binary(EF['SPN']) |
| 207 | if sw == '9000': |
| 208 | return (dec_spn(spn), sw) |
| 209 | else: |
| 210 | return (None, sw) |
| 211 | |
| 212 | def update_spn(self, name, hplmn_disp=False, oplmn_disp=False): |
| 213 | content = enc_spn(name, hplmn_disp, oplmn_disp) |
| 214 | data, sw = self._scc.update_binary(EF['SPN'], rpad(content, 32)) |
| 215 | return sw |
| 216 | |
Supreeth Herle | d21349a | 2020-04-01 08:37:47 +0200 | [diff] [blame] | 217 | def read_binary(self, ef, length=None, offset=0): |
| 218 | ef_path = ef in EF and EF[ef] or ef |
| 219 | return self._scc.read_binary(ef_path, length, offset) |
| 220 | |
Supreeth Herle | ad10d66 | 2020-04-01 08:43:08 +0200 | [diff] [blame] | 221 | def read_record(self, ef, rec_no): |
| 222 | ef_path = ef in EF and EF[ef] or ef |
| 223 | return self._scc.read_record(ef_path, rec_no) |
| 224 | |
Supreeth Herle | 98a6927 | 2020-03-18 12:14:48 +0100 | [diff] [blame] | 225 | def read_gid1(self): |
| 226 | (res, sw) = self._scc.read_binary(EF['GID1']) |
| 227 | if sw == '9000': |
| 228 | return (res, sw) |
| 229 | else: |
| 230 | return (None, sw) |
| 231 | |
Supreeth Herle | 6d66af6 | 2020-03-19 12:49:16 +0100 | [diff] [blame] | 232 | def read_msisdn(self): |
| 233 | (res, sw) = self._scc.read_record(EF['MSISDN'], 1) |
| 234 | if sw == '9000': |
| 235 | return (dec_msisdn(res), sw) |
| 236 | else: |
| 237 | return (None, sw) |
| 238 | |
Supreeth Herle | e4e9831 | 2020-03-18 11:33:14 +0100 | [diff] [blame] | 239 | # Fetch all the AIDs present on UICC |
| 240 | def read_aids(self): |
Philipp Maier | 1e896f3 | 2021-03-10 17:02:53 +0100 | [diff] [blame] | 241 | self._aids = [] |
Supreeth Herle | e4e9831 | 2020-03-18 11:33:14 +0100 | [diff] [blame] | 242 | try: |
| 243 | # Find out how many records the EF.DIR has |
| 244 | # and store all the AIDs in the UICC |
Sebastian Viviani | 0dc8f69 | 2020-05-29 00:14:55 +0100 | [diff] [blame] | 245 | rec_cnt = self._scc.record_count(EF['DIR']) |
Supreeth Herle | e4e9831 | 2020-03-18 11:33:14 +0100 | [diff] [blame] | 246 | for i in range(0, rec_cnt): |
Sebastian Viviani | 0dc8f69 | 2020-05-29 00:14:55 +0100 | [diff] [blame] | 247 | rec = self._scc.read_record(EF['DIR'], i + 1) |
Supreeth Herle | e4e9831 | 2020-03-18 11:33:14 +0100 | [diff] [blame] | 248 | if (rec[0][0:2], rec[0][4:6]) == ('61', '4f') and len(rec[0]) > 12 \ |
| 249 | and rec[0][8:8 + int(rec[0][6:8], 16) * 2] not in self._aids: |
| 250 | self._aids.append(rec[0][8:8 + int(rec[0][6:8], 16) * 2]) |
| 251 | except Exception as e: |
| 252 | print("Can't read AIDs from SIM -- %s" % (str(e),)) |
Philipp Maier | 1e896f3 | 2021-03-10 17:02:53 +0100 | [diff] [blame] | 253 | self._aids = [] |
| 254 | return self._aids |
Supreeth Herle | e4e9831 | 2020-03-18 11:33:14 +0100 | [diff] [blame] | 255 | |
Supreeth Herle | f9f3e5e | 2020-03-22 08:04:59 +0100 | [diff] [blame] | 256 | # Select ADF.U/ISIM in the Card using its full AID |
| 257 | def select_adf_by_aid(self, adf="usim"): |
Philipp Maier | cba6dbc | 2021-03-11 13:03:18 +0100 | [diff] [blame] | 258 | # Find full AID by partial AID: |
| 259 | if is_hex(adf): |
| 260 | for aid in self._aids: |
| 261 | if len(aid) >= len(adf) and adf == aid[0:len(adf)]: |
| 262 | return self._scc.select_adf(aid) |
| 263 | # Find full AID by application name: |
| 264 | elif adf in ["usim", "isim"]: |
| 265 | # First (known) halves of the U/ISIM AID |
| 266 | aid_map = {} |
| 267 | aid_map["usim"] = "a0000000871002" |
| 268 | aid_map["isim"] = "a0000000871004" |
| 269 | for aid in self._aids: |
| 270 | if aid_map[adf] in aid: |
| 271 | return self._scc.select_adf(aid) |
| 272 | return (None, None) |
Supreeth Herle | f9f3e5e | 2020-03-22 08:04:59 +0100 | [diff] [blame] | 273 | |
Philipp Maier | 5c2cc66 | 2020-05-12 16:27:12 +0200 | [diff] [blame] | 274 | # Erase the contents of a file |
| 275 | def erase_binary(self, ef): |
| 276 | len = self._scc.binary_size(ef) |
| 277 | self._scc.update_binary(ef, "ff" * len, offset=0, verify=True) |
| 278 | |
| 279 | # Erase the contents of a single record |
| 280 | def erase_record(self, ef, rec_no): |
| 281 | len = self._scc.record_size(ef) |
| 282 | self._scc.update_record(ef, rec_no, "ff" * len, force_len=False, verify=True) |
| 283 | |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 284 | class UsimCard(Card): |
| 285 | def __init__(self, ssc): |
| 286 | super(UsimCard, self).__init__(ssc) |
| 287 | |
| 288 | def read_ehplmn(self): |
| 289 | (res, sw) = self._scc.read_binary(EF_USIM_ADF_map['EHPLMN']) |
| 290 | if sw == '9000': |
| 291 | return (format_xplmn(res), sw) |
| 292 | else: |
| 293 | return (None, sw) |
| 294 | |
| 295 | def update_ehplmn(self, mcc, mnc): |
| 296 | data = self._scc.read_binary(EF_USIM_ADF_map['EHPLMN'], length=None, offset=0) |
| 297 | size = len(data[0]) // 2 |
| 298 | ehplmn = enc_plmn(mcc, mnc) |
| 299 | data, sw = self._scc.update_binary(EF_USIM_ADF_map['EHPLMN'], ehplmn) |
| 300 | return sw |
| 301 | |
herlesupreeth | f8232db | 2020-09-29 10:03:06 +0200 | [diff] [blame] | 302 | def read_epdgid(self): |
| 303 | (res, sw) = self._scc.read_binary(EF_USIM_ADF_map['ePDGId']) |
| 304 | if sw == '9000': |
Supreeth Herle | 3b342c2 | 2020-03-24 16:15:02 +0100 | [diff] [blame] | 305 | return (dec_addr_tlv(res), sw) |
herlesupreeth | f8232db | 2020-09-29 10:03:06 +0200 | [diff] [blame] | 306 | else: |
| 307 | return (None, sw) |
| 308 | |
herlesupreeth | 5d0a30c | 2020-09-29 09:44:24 +0200 | [diff] [blame] | 309 | def update_epdgid(self, epdgid): |
Supreeth Herle | 4779034 | 2020-03-25 12:51:38 +0100 | [diff] [blame] | 310 | size = self._scc.binary_size(EF_USIM_ADF_map['ePDGId']) * 2 |
| 311 | if len(epdgid) > 0: |
Supreeth Herle | c491dc0 | 2020-03-25 14:56:13 +0100 | [diff] [blame] | 312 | addr_type = get_addr_type(epdgid) |
| 313 | if addr_type == None: |
| 314 | raise ValueError("Unknown ePDG Id address type or invalid address provided") |
| 315 | epdgid_tlv = rpad(enc_addr_tlv(epdgid, ('%02x' % addr_type)), size) |
Supreeth Herle | 4779034 | 2020-03-25 12:51:38 +0100 | [diff] [blame] | 316 | else: |
| 317 | epdgid_tlv = rpad('ff', size) |
herlesupreeth | 5d0a30c | 2020-09-29 09:44:24 +0200 | [diff] [blame] | 318 | data, sw = self._scc.update_binary( |
| 319 | EF_USIM_ADF_map['ePDGId'], epdgid_tlv) |
| 320 | return sw |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 321 | |
Supreeth Herle | 99d5555 | 2020-03-24 13:03:43 +0100 | [diff] [blame] | 322 | def read_ePDGSelection(self): |
| 323 | (res, sw) = self._scc.read_binary(EF_USIM_ADF_map['ePDGSelection']) |
| 324 | if sw == '9000': |
| 325 | return (format_ePDGSelection(res), sw) |
| 326 | else: |
| 327 | return (None, sw) |
| 328 | |
Supreeth Herle | f964df4 | 2020-03-24 13:15:37 +0100 | [diff] [blame] | 329 | def update_ePDGSelection(self, mcc, mnc): |
| 330 | (res, sw) = self._scc.read_binary(EF_USIM_ADF_map['ePDGSelection'], length=None, offset=0) |
| 331 | if sw == '9000' and (len(mcc) == 0 or len(mnc) == 0): |
| 332 | # Reset contents |
| 333 | # 80 - Tag value |
| 334 | (res, sw) = self._scc.update_binary(EF_USIM_ADF_map['ePDGSelection'], rpad('', len(res))) |
| 335 | elif sw == '9000': |
| 336 | (res, sw) = self._scc.update_binary(EF_USIM_ADF_map['ePDGSelection'], enc_ePDGSelection(res, mcc, mnc)) |
| 337 | return sw |
| 338 | |
herlesupreeth | 4a3580b | 2020-09-29 10:11:36 +0200 | [diff] [blame] | 339 | def read_ust(self): |
| 340 | (res, sw) = self._scc.read_binary(EF_USIM_ADF_map['UST']) |
| 341 | if sw == '9000': |
| 342 | # Print those which are available |
| 343 | return ([res, dec_st(res, table="usim")], sw) |
| 344 | else: |
| 345 | return ([None, None], sw) |
| 346 | |
Supreeth Herle | acc222f | 2020-03-24 13:26:53 +0100 | [diff] [blame] | 347 | def update_ust(self, service, bit=1): |
| 348 | (res, sw) = self._scc.read_binary(EF_USIM_ADF_map['UST']) |
| 349 | if sw == '9000': |
| 350 | content = enc_st(res, service, bit) |
| 351 | (res, sw) = self._scc.update_binary(EF_USIM_ADF_map['UST'], content) |
| 352 | return sw |
| 353 | |
herlesupreeth | ecbada9 | 2020-12-23 09:24:29 +0100 | [diff] [blame] | 354 | class IsimCard(Card): |
| 355 | def __init__(self, ssc): |
| 356 | super(IsimCard, self).__init__(ssc) |
| 357 | |
Supreeth Herle | 5ad9aec | 2020-03-24 17:26:40 +0100 | [diff] [blame] | 358 | def read_pcscf(self): |
| 359 | rec_cnt = self._scc.record_count(EF_ISIM_ADF_map['PCSCF']) |
| 360 | pcscf_recs = "" |
| 361 | for i in range(0, rec_cnt): |
| 362 | (res, sw) = self._scc.read_record(EF_ISIM_ADF_map['PCSCF'], i + 1) |
| 363 | if sw == '9000': |
| 364 | content = dec_addr_tlv(res) |
| 365 | pcscf_recs += "%s" % (len(content) and content or '\tNot available\n') |
| 366 | else: |
| 367 | pcscf_recs += "\tP-CSCF: Can't read, response code = %s\n" % (sw) |
| 368 | return pcscf_recs |
| 369 | |
Supreeth Herle | cf727f2 | 2020-03-24 17:32:21 +0100 | [diff] [blame] | 370 | def update_pcscf(self, pcscf): |
| 371 | if len(pcscf) > 0: |
herlesupreeth | 1279085 | 2020-12-24 09:38:42 +0100 | [diff] [blame] | 372 | addr_type = get_addr_type(pcscf) |
| 373 | if addr_type == None: |
| 374 | raise ValueError("Unknown PCSCF address type or invalid address provided") |
| 375 | content = enc_addr_tlv(pcscf, ('%02x' % addr_type)) |
Supreeth Herle | cf727f2 | 2020-03-24 17:32:21 +0100 | [diff] [blame] | 376 | else: |
| 377 | # Just the tag value |
| 378 | content = '80' |
| 379 | rec_size_bytes = self._scc.record_size(EF_ISIM_ADF_map['PCSCF']) |
herlesupreeth | 1279085 | 2020-12-24 09:38:42 +0100 | [diff] [blame] | 380 | pcscf_tlv = rpad(content, rec_size_bytes*2) |
| 381 | data, sw = self._scc.update_record(EF_ISIM_ADF_map['PCSCF'], 1, pcscf_tlv) |
Supreeth Herle | cf727f2 | 2020-03-24 17:32:21 +0100 | [diff] [blame] | 382 | return sw |
| 383 | |
Supreeth Herle | 05b2807 | 2020-03-25 10:23:48 +0100 | [diff] [blame] | 384 | def read_domain(self): |
| 385 | (res, sw) = self._scc.read_binary(EF_ISIM_ADF_map['DOMAIN']) |
| 386 | if sw == '9000': |
| 387 | # Skip the inital tag value ('80') byte and get length of contents |
| 388 | length = int(res[2:4], 16) |
| 389 | content = h2s(res[4:4+(length*2)]) |
| 390 | return (content, sw) |
| 391 | else: |
| 392 | return (None, sw) |
| 393 | |
Supreeth Herle | 79f43dd | 2020-03-25 11:43:19 +0100 | [diff] [blame] | 394 | def update_domain(self, domain=None, mcc=None, mnc=None): |
| 395 | hex_str = "" |
| 396 | if domain: |
| 397 | hex_str = s2h(domain) |
| 398 | elif mcc and mnc: |
| 399 | # MCC and MNC always has 3 digits in domain form |
| 400 | plmn_str = 'mnc' + lpad(mnc, 3, "0") + '.mcc' + lpad(mcc, 3, "0") |
| 401 | hex_str = s2h('ims.' + plmn_str + '.3gppnetwork.org') |
| 402 | |
| 403 | # Build TLV |
| 404 | tlv = TLV(['80']) |
| 405 | content = tlv.build({'80': hex_str}) |
| 406 | |
| 407 | bin_size_bytes = self._scc.binary_size(EF_ISIM_ADF_map['DOMAIN']) |
| 408 | data, sw = self._scc.update_binary(EF_ISIM_ADF_map['DOMAIN'], rpad(content, bin_size_bytes*2)) |
| 409 | return sw |
| 410 | |
Supreeth Herle | 3f67f9c | 2020-03-25 15:38:02 +0100 | [diff] [blame] | 411 | def read_impi(self): |
| 412 | (res, sw) = self._scc.read_binary(EF_ISIM_ADF_map['IMPI']) |
| 413 | if sw == '9000': |
| 414 | # Skip the inital tag value ('80') byte and get length of contents |
| 415 | length = int(res[2:4], 16) |
| 416 | content = h2s(res[4:4+(length*2)]) |
| 417 | return (content, sw) |
| 418 | else: |
| 419 | return (None, sw) |
| 420 | |
Supreeth Herle | a5bd968 | 2020-03-26 09:16:14 +0100 | [diff] [blame] | 421 | def update_impi(self, impi=None): |
| 422 | hex_str = "" |
| 423 | if impi: |
| 424 | hex_str = s2h(impi) |
| 425 | # Build TLV |
| 426 | tlv = TLV(['80']) |
| 427 | content = tlv.build({'80': hex_str}) |
| 428 | |
| 429 | bin_size_bytes = self._scc.binary_size(EF_ISIM_ADF_map['IMPI']) |
| 430 | data, sw = self._scc.update_binary(EF_ISIM_ADF_map['IMPI'], rpad(content, bin_size_bytes*2)) |
| 431 | return sw |
| 432 | |
Supreeth Herle | 0c02d8a | 2020-03-26 09:00:06 +0100 | [diff] [blame] | 433 | def read_impu(self): |
| 434 | rec_cnt = self._scc.record_count(EF_ISIM_ADF_map['IMPU']) |
| 435 | impu_recs = "" |
| 436 | for i in range(0, rec_cnt): |
| 437 | (res, sw) = self._scc.read_record(EF_ISIM_ADF_map['IMPU'], i + 1) |
| 438 | if sw == '9000': |
| 439 | # Skip the inital tag value ('80') byte and get length of contents |
| 440 | length = int(res[2:4], 16) |
| 441 | content = h2s(res[4:4+(length*2)]) |
| 442 | impu_recs += "\t%s\n" % (len(content) and content or 'Not available') |
| 443 | else: |
| 444 | impu_recs += "IMS public user identity: Can't read, response code = %s\n" % (sw) |
| 445 | return impu_recs |
| 446 | |
Supreeth Herle | be7007e | 2020-03-26 09:27:45 +0100 | [diff] [blame] | 447 | def update_impu(self, impu=None): |
| 448 | hex_str = "" |
| 449 | if impu: |
| 450 | hex_str = s2h(impu) |
| 451 | # Build TLV |
| 452 | tlv = TLV(['80']) |
| 453 | content = tlv.build({'80': hex_str}) |
| 454 | |
| 455 | rec_size_bytes = self._scc.record_size(EF_ISIM_ADF_map['IMPU']) |
| 456 | impu_tlv = rpad(content, rec_size_bytes*2) |
| 457 | data, sw = self._scc.update_record(EF_ISIM_ADF_map['IMPU'], 1, impu_tlv) |
| 458 | return sw |
| 459 | |
Supreeth Herle | be3b641 | 2020-06-01 12:53:57 +0200 | [diff] [blame] | 460 | def read_iari(self): |
| 461 | rec_cnt = self._scc.record_count(EF_ISIM_ADF_map['UICCIARI']) |
| 462 | uiari_recs = "" |
| 463 | for i in range(0, rec_cnt): |
| 464 | (res, sw) = self._scc.read_record(EF_ISIM_ADF_map['UICCIARI'], i + 1) |
| 465 | if sw == '9000': |
| 466 | # Skip the inital tag value ('80') byte and get length of contents |
| 467 | length = int(res[2:4], 16) |
| 468 | content = h2s(res[4:4+(length*2)]) |
| 469 | uiari_recs += "\t%s\n" % (len(content) and content or 'Not available') |
| 470 | else: |
| 471 | uiari_recs += "UICC IARI: Can't read, response code = %s\n" % (sw) |
| 472 | return uiari_recs |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 473 | |
Vadim Yanitskiy | 85302d6 | 2021-05-02 02:18:42 +0200 | [diff] [blame] | 474 | class MagicSimBase(abc.ABC, Card): |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 475 | """ |
| 476 | Theses cards uses several record based EFs to store the provider infos, |
| 477 | each possible provider uses a specific record number in each EF. The |
| 478 | indexes used are ( where N is the number of providers supported ) : |
| 479 | - [2 .. N+1] for the operator name |
Harald Welte | c9cdce3 | 2021-04-11 10:28:28 +0200 | [diff] [blame] | 480 | - [1 .. N] for the programmable EFs |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 481 | |
| 482 | * 3f00/7f4d/8f0c : Operator Name |
| 483 | |
| 484 | bytes 0-15 : provider name, padded with 0xff |
| 485 | byte 16 : length of the provider name |
| 486 | byte 17 : 01 for valid records, 00 otherwise |
| 487 | |
| 488 | * 3f00/7f4d/8f0d : Programmable Binary EFs |
| 489 | |
| 490 | * 3f00/7f4d/8f0e : Programmable Record EFs |
| 491 | |
| 492 | """ |
| 493 | |
Vadim Yanitskiy | 03c67f7 | 2021-05-02 02:10:39 +0200 | [diff] [blame] | 494 | _files = { } # type: Dict[str, Tuple[str, int, bool]] |
| 495 | _ki_file = None # type: Optional[str] |
| 496 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 497 | @classmethod |
| 498 | def autodetect(kls, scc): |
| 499 | try: |
| 500 | for p, l, t in kls._files.values(): |
| 501 | if not t: |
| 502 | continue |
| 503 | if scc.record_size(['3f00', '7f4d', p]) != l: |
| 504 | return None |
| 505 | except: |
| 506 | return None |
| 507 | |
| 508 | return kls(scc) |
| 509 | |
| 510 | def _get_count(self): |
| 511 | """ |
| 512 | Selects the file and returns the total number of entries |
| 513 | and entry size |
| 514 | """ |
| 515 | f = self._files['name'] |
| 516 | |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 517 | r = self._scc.select_path(['3f00', '7f4d', f[0]]) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 518 | rec_len = int(r[-1][28:30], 16) |
| 519 | tlen = int(r[-1][4:8],16) |
Vadim Yanitskiy | eb39586 | 2021-05-02 02:23:48 +0200 | [diff] [blame] | 520 | rec_cnt = (tlen // rec_len) - 1 |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 521 | |
| 522 | if (rec_cnt < 1) or (rec_len != f[1]): |
| 523 | raise RuntimeError('Bad card type') |
| 524 | |
| 525 | return rec_cnt |
| 526 | |
| 527 | def program(self, p): |
| 528 | # Go to dir |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 529 | self._scc.select_path(['3f00', '7f4d']) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 530 | |
| 531 | # Home PLMN in PLMN_Sel format |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 532 | hplmn = enc_plmn(p['mcc'], p['mnc']) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 533 | |
| 534 | # Operator name ( 3f00/7f4d/8f0c ) |
| 535 | self._scc.update_record(self._files['name'][0], 2, |
| 536 | rpad(b2h(p['name']), 32) + ('%02x' % len(p['name'])) + '01' |
| 537 | ) |
| 538 | |
| 539 | # ICCID/IMSI/Ki/HPLMN ( 3f00/7f4d/8f0d ) |
| 540 | v = '' |
| 541 | |
| 542 | # inline Ki |
| 543 | if self._ki_file is None: |
| 544 | v += p['ki'] |
| 545 | |
| 546 | # ICCID |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 547 | v += '3f00' + '2fe2' + '0a' + enc_iccid(p['iccid']) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 548 | |
| 549 | # IMSI |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 550 | v += '7f20' + '6f07' + '09' + enc_imsi(p['imsi']) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 551 | |
| 552 | # Ki |
| 553 | if self._ki_file: |
| 554 | v += self._ki_file + '10' + p['ki'] |
| 555 | |
| 556 | # PLMN_Sel |
| 557 | v+= '6f30' + '18' + rpad(hplmn, 36) |
| 558 | |
Alexander Chemeris | 2188524 | 2013-07-02 16:56:55 +0400 | [diff] [blame] | 559 | # ACC |
| 560 | # This doesn't work with "fake" SuperSIM cards, |
| 561 | # but will hopefully work with real SuperSIMs. |
| 562 | if p.get('acc') is not None: |
| 563 | v+= '6f78' + '02' + lpad(p['acc'], 4) |
| 564 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 565 | self._scc.update_record(self._files['b_ef'][0], 1, |
| 566 | rpad(v, self._files['b_ef'][1]*2) |
| 567 | ) |
| 568 | |
| 569 | # SMSP ( 3f00/7f4d/8f0e ) |
| 570 | # FIXME |
| 571 | |
| 572 | # Write PLMN_Sel forcefully as well |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 573 | r = self._scc.select_path(['3f00', '7f20', '6f30']) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 574 | tl = int(r[-1][4:8], 16) |
| 575 | |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 576 | hplmn = enc_plmn(p['mcc'], p['mnc']) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 577 | self._scc.update_binary('6f30', hplmn + 'ff' * (tl-3)) |
| 578 | |
| 579 | def erase(self): |
| 580 | # Dummy |
| 581 | df = {} |
Vadim Yanitskiy | d9a8d2f | 2021-05-02 02:12:47 +0200 | [diff] [blame] | 582 | for k, v in self._files.items(): |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 583 | ofs = 1 |
| 584 | fv = v[1] * 'ff' |
| 585 | if k == 'name': |
| 586 | ofs = 2 |
| 587 | fv = fv[0:-4] + '0000' |
| 588 | df[v[0]] = (fv, ofs) |
| 589 | |
| 590 | # Write |
| 591 | for n in range(0,self._get_count()): |
Vadim Yanitskiy | d9a8d2f | 2021-05-02 02:12:47 +0200 | [diff] [blame] | 592 | for k, (msg, ofs) in df.items(): |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 593 | self._scc.update_record(['3f00', '7f4d', k], n + ofs, msg) |
| 594 | |
| 595 | |
Vadim Yanitskiy | 85302d6 | 2021-05-02 02:18:42 +0200 | [diff] [blame] | 596 | class SuperSim(MagicSimBase): |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 597 | |
| 598 | name = 'supersim' |
| 599 | |
| 600 | _files = { |
| 601 | 'name' : ('8f0c', 18, True), |
| 602 | 'b_ef' : ('8f0d', 74, True), |
| 603 | 'r_ef' : ('8f0e', 50, True), |
| 604 | } |
| 605 | |
| 606 | _ki_file = None |
| 607 | |
| 608 | |
Vadim Yanitskiy | 85302d6 | 2021-05-02 02:18:42 +0200 | [diff] [blame] | 609 | class MagicSim(MagicSimBase): |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 610 | |
| 611 | name = 'magicsim' |
| 612 | |
| 613 | _files = { |
| 614 | 'name' : ('8f0c', 18, True), |
| 615 | 'b_ef' : ('8f0d', 130, True), |
| 616 | 'r_ef' : ('8f0e', 102, False), |
| 617 | } |
| 618 | |
| 619 | _ki_file = '6f1b' |
| 620 | |
| 621 | |
| 622 | class FakeMagicSim(Card): |
| 623 | """ |
| 624 | Theses cards have a record based EF 3f00/000c that contains the provider |
Harald Welte | c9cdce3 | 2021-04-11 10:28:28 +0200 | [diff] [blame] | 625 | information. See the program method for its format. The records go from |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 626 | 1 to N. |
| 627 | """ |
| 628 | |
| 629 | name = 'fakemagicsim' |
| 630 | |
| 631 | @classmethod |
| 632 | def autodetect(kls, scc): |
| 633 | try: |
| 634 | if scc.record_size(['3f00', '000c']) != 0x5a: |
| 635 | return None |
| 636 | except: |
| 637 | return None |
| 638 | |
| 639 | return kls(scc) |
| 640 | |
| 641 | def _get_infos(self): |
| 642 | """ |
| 643 | Selects the file and returns the total number of entries |
| 644 | and entry size |
| 645 | """ |
| 646 | |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 647 | r = self._scc.select_path(['3f00', '000c']) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 648 | rec_len = int(r[-1][28:30], 16) |
| 649 | tlen = int(r[-1][4:8],16) |
Vadim Yanitskiy | eb39586 | 2021-05-02 02:23:48 +0200 | [diff] [blame] | 650 | rec_cnt = (tlen // rec_len) - 1 |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 651 | |
| 652 | if (rec_cnt < 1) or (rec_len != 0x5a): |
| 653 | raise RuntimeError('Bad card type') |
| 654 | |
| 655 | return rec_cnt, rec_len |
| 656 | |
| 657 | def program(self, p): |
| 658 | # Home PLMN |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 659 | r = self._scc.select_path(['3f00', '7f20', '6f30']) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 660 | tl = int(r[-1][4:8], 16) |
| 661 | |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 662 | hplmn = enc_plmn(p['mcc'], p['mnc']) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 663 | self._scc.update_binary('6f30', hplmn + 'ff' * (tl-3)) |
| 664 | |
| 665 | # Get total number of entries and entry size |
| 666 | rec_cnt, rec_len = self._get_infos() |
| 667 | |
| 668 | # Set first entry |
| 669 | entry = ( |
Philipp Maier | 45daa92 | 2019-04-01 15:49:45 +0200 | [diff] [blame] | 670 | '81' + # 1b Status: Valid & Active |
Harald Welte | 4f6ca43 | 2021-02-01 17:51:56 +0100 | [diff] [blame] | 671 | rpad(s2h(p['name'][0:14]), 28) + # 14b Entry Name |
Philipp Maier | 45daa92 | 2019-04-01 15:49:45 +0200 | [diff] [blame] | 672 | enc_iccid(p['iccid']) + # 10b ICCID |
| 673 | enc_imsi(p['imsi']) + # 9b IMSI_len + id_type(9) + IMSI |
| 674 | p['ki'] + # 16b Ki |
| 675 | lpad(p['smsp'], 80) # 40b SMSP (padded with ff if needed) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 676 | ) |
| 677 | self._scc.update_record('000c', 1, entry) |
| 678 | |
| 679 | def erase(self): |
| 680 | # Get total number of entries and entry size |
| 681 | rec_cnt, rec_len = self._get_infos() |
| 682 | |
| 683 | # Erase all entries |
| 684 | entry = 'ff' * rec_len |
| 685 | for i in range(0, rec_cnt): |
| 686 | self._scc.update_record('000c', 1+i, entry) |
| 687 | |
Sylvain Munaut | 5da8d4e | 2013-07-02 15:13:24 +0200 | [diff] [blame] | 688 | |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 689 | class GrcardSim(Card): |
| 690 | """ |
| 691 | Greencard (grcard.cn) HZCOS GSM SIM |
| 692 | These cards have a much more regular ISO 7816-4 / TS 11.11 structure, |
| 693 | and use standard UPDATE RECORD / UPDATE BINARY commands except for Ki. |
| 694 | """ |
| 695 | |
| 696 | name = 'grcardsim' |
| 697 | |
| 698 | @classmethod |
| 699 | def autodetect(kls, scc): |
| 700 | return None |
| 701 | |
| 702 | def program(self, p): |
| 703 | # We don't really know yet what ADM PIN 4 is about |
| 704 | #self._scc.verify_chv(4, h2b("4444444444444444")) |
| 705 | |
| 706 | # Authenticate using ADM PIN 5 |
Jan Balke | c3ebd33 | 2015-01-26 12:22:55 +0100 | [diff] [blame] | 707 | if p['pin_adm']: |
Philipp Maier | a3de5a3 | 2018-08-23 10:27:04 +0200 | [diff] [blame] | 708 | pin = h2b(p['pin_adm']) |
Jan Balke | c3ebd33 | 2015-01-26 12:22:55 +0100 | [diff] [blame] | 709 | else: |
| 710 | pin = h2b("4444444444444444") |
| 711 | self._scc.verify_chv(5, pin) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 712 | |
| 713 | # EF.ICCID |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 714 | r = self._scc.select_path(['3f00', '2fe2']) |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 715 | data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid'])) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 716 | |
| 717 | # EF.IMSI |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 718 | r = self._scc.select_path(['3f00', '7f20', '6f07']) |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 719 | data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi'])) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 720 | |
| 721 | # EF.ACC |
Alexander Chemeris | 2188524 | 2013-07-02 16:56:55 +0400 | [diff] [blame] | 722 | if p.get('acc') is not None: |
| 723 | data, sw = self._scc.update_binary('6f78', lpad(p['acc'], 4)) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 724 | |
| 725 | # EF.SMSP |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 726 | if p.get('smsp'): |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 727 | r = self._scc.select_path(['3f00', '7f10', '6f42']) |
Harald Welte | 23888da | 2019-08-28 23:19:11 +0200 | [diff] [blame] | 728 | data, sw = self._scc.update_record('6f42', 1, lpad(p['smsp'], 80)) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 729 | |
| 730 | # Set the Ki using proprietary command |
| 731 | pdu = '80d4020010' + p['ki'] |
| 732 | data, sw = self._scc._tp.send_apdu(pdu) |
| 733 | |
| 734 | # EF.HPLMN |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 735 | r = self._scc.select_path(['3f00', '7f20', '6f30']) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 736 | size = int(r[-1][4:8], 16) |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 737 | hplmn = enc_plmn(p['mcc'], p['mnc']) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 738 | self._scc.update_binary('6f30', hplmn + 'ff' * (size-3)) |
| 739 | |
| 740 | # EF.SPN (Service Provider Name) |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 741 | r = self._scc.select_path(['3f00', '7f20', '6f30']) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 742 | size = int(r[-1][4:8], 16) |
| 743 | # FIXME |
| 744 | |
| 745 | # FIXME: EF.MSISDN |
| 746 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 747 | |
Harald Welte | e10394b | 2011-12-07 12:34:14 +0100 | [diff] [blame] | 748 | class SysmoSIMgr1(GrcardSim): |
| 749 | """ |
| 750 | sysmocom sysmoSIM-GR1 |
| 751 | These cards have a much more regular ISO 7816-4 / TS 11.11 structure, |
| 752 | and use standard UPDATE RECORD / UPDATE BINARY commands except for Ki. |
| 753 | """ |
| 754 | name = 'sysmosim-gr1' |
| 755 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 756 | @classmethod |
Philipp Maier | 087feff | 2018-08-23 09:41:36 +0200 | [diff] [blame] | 757 | def autodetect(kls, scc): |
| 758 | try: |
| 759 | # Look for ATR |
| 760 | if scc.get_atr() == toBytes("3B 99 18 00 11 88 22 33 44 55 66 77 60"): |
| 761 | return kls(scc) |
| 762 | except: |
| 763 | return None |
| 764 | return None |
Sylvain Munaut | 5da8d4e | 2013-07-02 15:13:24 +0200 | [diff] [blame] | 765 | |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 766 | class SysmoUSIMgr1(UsimCard): |
Holger Hans Peter Freyther | 4d91bf4 | 2012-03-22 14:28:38 +0100 | [diff] [blame] | 767 | """ |
| 768 | sysmocom sysmoUSIM-GR1 |
| 769 | """ |
| 770 | name = 'sysmoUSIM-GR1' |
| 771 | |
| 772 | @classmethod |
| 773 | def autodetect(kls, scc): |
| 774 | # TODO: Access the ATR |
| 775 | return None |
| 776 | |
| 777 | def program(self, p): |
| 778 | # TODO: check if verify_chv could be used or what it needs |
| 779 | # self._scc.verify_chv(0x0A, [0x33,0x32,0x32,0x31,0x33,0x32,0x33,0x32]) |
| 780 | # Unlock the card.. |
| 781 | data, sw = self._scc._tp.send_apdu_checksw("0020000A083332323133323332") |
| 782 | |
| 783 | # TODO: move into SimCardCommands |
Holger Hans Peter Freyther | 4d91bf4 | 2012-03-22 14:28:38 +0100 | [diff] [blame] | 784 | par = ( p['ki'] + # 16b K |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 785 | p['opc'] + # 32b OPC |
| 786 | enc_iccid(p['iccid']) + # 10b ICCID |
| 787 | enc_imsi(p['imsi']) # 9b IMSI_len + id_type(9) + IMSI |
Holger Hans Peter Freyther | 4d91bf4 | 2012-03-22 14:28:38 +0100 | [diff] [blame] | 788 | ) |
| 789 | data, sw = self._scc._tp.send_apdu_checksw("0099000033" + par) |
| 790 | |
Sylvain Munaut | 053c895 | 2013-07-02 15:12:32 +0200 | [diff] [blame] | 791 | |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 792 | class SysmoSIMgr2(Card): |
| 793 | """ |
| 794 | sysmocom sysmoSIM-GR2 |
| 795 | """ |
| 796 | |
| 797 | name = 'sysmoSIM-GR2' |
| 798 | |
| 799 | @classmethod |
| 800 | def autodetect(kls, scc): |
Alexander Chemeris | 8ad124a | 2018-01-10 14:17:55 +0900 | [diff] [blame] | 801 | try: |
| 802 | # Look for ATR |
| 803 | if scc.get_atr() == toBytes("3B 7D 94 00 00 55 55 53 0A 74 86 93 0B 24 7C 4D 54 68"): |
| 804 | return kls(scc) |
| 805 | except: |
| 806 | return None |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 807 | return None |
| 808 | |
| 809 | def program(self, p): |
| 810 | |
Daniel Willmann | 5d8cd9b | 2020-10-19 11:01:49 +0200 | [diff] [blame] | 811 | # select MF |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 812 | r = self._scc.select_path(['3f00']) |
Daniel Willmann | 5d8cd9b | 2020-10-19 11:01:49 +0200 | [diff] [blame] | 813 | |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 814 | # authenticate as SUPER ADM using default key |
| 815 | self._scc.verify_chv(0x0b, h2b("3838383838383838")) |
| 816 | |
| 817 | # set ADM pin using proprietary command |
| 818 | # INS: D4 |
| 819 | # P1: 3A for PIN, 3B for PUK |
| 820 | # P2: CHV number, as in VERIFY CHV for PIN, and as in UNBLOCK CHV for PUK |
| 821 | # P3: 08, CHV length (curiously the PUK is also 08 length, instead of 10) |
Jan Balke | c3ebd33 | 2015-01-26 12:22:55 +0100 | [diff] [blame] | 822 | if p['pin_adm']: |
Daniel Willmann | 7d38d74 | 2018-06-15 07:31:50 +0200 | [diff] [blame] | 823 | pin = h2b(p['pin_adm']) |
Jan Balke | c3ebd33 | 2015-01-26 12:22:55 +0100 | [diff] [blame] | 824 | else: |
| 825 | pin = h2b("4444444444444444") |
| 826 | |
| 827 | pdu = 'A0D43A0508' + b2h(pin) |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 828 | data, sw = self._scc._tp.send_apdu(pdu) |
Daniel Willmann | 5d8cd9b | 2020-10-19 11:01:49 +0200 | [diff] [blame] | 829 | |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 830 | # authenticate as ADM (enough to write file, and can set PINs) |
Jan Balke | c3ebd33 | 2015-01-26 12:22:55 +0100 | [diff] [blame] | 831 | |
| 832 | self._scc.verify_chv(0x05, pin) |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 833 | |
| 834 | # write EF.ICCID |
| 835 | data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid'])) |
| 836 | |
| 837 | # select DF_GSM |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 838 | r = self._scc.select_path(['7f20']) |
Daniel Willmann | 5d8cd9b | 2020-10-19 11:01:49 +0200 | [diff] [blame] | 839 | |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 840 | # write EF.IMSI |
| 841 | data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi'])) |
| 842 | |
| 843 | # write EF.ACC |
| 844 | if p.get('acc') is not None: |
| 845 | data, sw = self._scc.update_binary('6f78', lpad(p['acc'], 4)) |
| 846 | |
| 847 | # get size and write EF.HPLMN |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 848 | r = self._scc.select_path(['6f30']) |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 849 | size = int(r[-1][4:8], 16) |
| 850 | hplmn = enc_plmn(p['mcc'], p['mnc']) |
| 851 | self._scc.update_binary('6f30', hplmn + 'ff' * (size-3)) |
| 852 | |
| 853 | # set COMP128 version 0 in proprietary file |
| 854 | data, sw = self._scc.update_binary('0001', '001000') |
| 855 | |
| 856 | # set Ki in proprietary file |
| 857 | data, sw = self._scc.update_binary('0001', p['ki'], 3) |
| 858 | |
| 859 | # select DF_TELECOM |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 860 | r = self._scc.select_path(['3f00', '7f10']) |
Daniel Willmann | 5d8cd9b | 2020-10-19 11:01:49 +0200 | [diff] [blame] | 861 | |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 862 | # write EF.SMSP |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 863 | if p.get('smsp'): |
Harald Welte | 23888da | 2019-08-28 23:19:11 +0200 | [diff] [blame] | 864 | data, sw = self._scc.update_record('6f42', 1, lpad(p['smsp'], 80)) |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 865 | |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 866 | |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 867 | class SysmoUSIMSJS1(UsimCard): |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 868 | """ |
| 869 | sysmocom sysmoUSIM-SJS1 |
| 870 | """ |
| 871 | |
| 872 | name = 'sysmoUSIM-SJS1' |
| 873 | |
| 874 | def __init__(self, ssc): |
| 875 | super(SysmoUSIMSJS1, self).__init__(ssc) |
| 876 | self._scc.cla_byte = "00" |
Philipp Maier | 2d15ea0 | 2019-03-20 12:40:36 +0100 | [diff] [blame] | 877 | self._scc.sel_ctrl = "0004" #request an FCP |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 878 | |
| 879 | @classmethod |
| 880 | def autodetect(kls, scc): |
Alexander Chemeris | 8ad124a | 2018-01-10 14:17:55 +0900 | [diff] [blame] | 881 | try: |
| 882 | # Look for ATR |
| 883 | if scc.get_atr() == toBytes("3B 9F 96 80 1F C7 80 31 A0 73 BE 21 13 67 43 20 07 18 00 00 01 A5"): |
| 884 | return kls(scc) |
| 885 | except: |
| 886 | return None |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 887 | return None |
| 888 | |
Harald Welte | a670425 | 2021-01-08 20:19:11 +0100 | [diff] [blame] | 889 | def verify_adm(self, key): |
Philipp Maier | e960488 | 2017-03-21 17:24:31 +0100 | [diff] [blame] | 890 | # authenticate as ADM using default key (written on the card..) |
Harald Welte | a670425 | 2021-01-08 20:19:11 +0100 | [diff] [blame] | 891 | if not key: |
Philipp Maier | e960488 | 2017-03-21 17:24:31 +0100 | [diff] [blame] | 892 | raise ValueError("Please provide a PIN-ADM as there is no default one") |
Harald Welte | a670425 | 2021-01-08 20:19:11 +0100 | [diff] [blame] | 893 | (res, sw) = self._scc.verify_chv(0x0A, key) |
Harald Welte | a670425 | 2021-01-08 20:19:11 +0100 | [diff] [blame] | 894 | return sw |
| 895 | |
| 896 | def program(self, p): |
| 897 | self.verify_adm(h2b(p['pin_adm'])) |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 898 | |
| 899 | # select MF |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 900 | r = self._scc.select_path(['3f00']) |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 901 | |
Philipp Maier | e960488 | 2017-03-21 17:24:31 +0100 | [diff] [blame] | 902 | # write EF.ICCID |
| 903 | data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid'])) |
| 904 | |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 905 | # select DF_GSM |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 906 | r = self._scc.select_path(['7f20']) |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 907 | |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 908 | # set Ki in proprietary file |
| 909 | data, sw = self._scc.update_binary('00FF', p['ki']) |
| 910 | |
Philipp Maier | 1be35bf | 2018-07-13 11:29:03 +0200 | [diff] [blame] | 911 | # set OPc in proprietary file |
Daniel Willmann | 67acdbc | 2018-06-15 07:42:48 +0200 | [diff] [blame] | 912 | if 'opc' in p: |
| 913 | content = "01" + p['opc'] |
| 914 | data, sw = self._scc.update_binary('00F7', content) |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 915 | |
Supreeth Herle | 7947d92 | 2019-06-08 07:50:53 +0200 | [diff] [blame] | 916 | # set Service Provider Name |
Supreeth Herle | 840a9e2 | 2020-01-21 13:32:46 +0100 | [diff] [blame] | 917 | if p.get('name') is not None: |
| 918 | content = enc_spn(p['name'], True, True) |
| 919 | data, sw = self._scc.update_binary('6F46', rpad(content, 32)) |
Supreeth Herle | 7947d92 | 2019-06-08 07:50:53 +0200 | [diff] [blame] | 920 | |
Supreeth Herle | c8796a3 | 2019-12-23 12:23:42 +0100 | [diff] [blame] | 921 | if p.get('acc') is not None: |
| 922 | self.update_acc(p['acc']) |
| 923 | |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 924 | # write EF.IMSI |
| 925 | data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi'])) |
| 926 | |
Philipp Maier | 2d15ea0 | 2019-03-20 12:40:36 +0100 | [diff] [blame] | 927 | # EF.PLMNsel |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 928 | if p.get('mcc') and p.get('mnc'): |
| 929 | sw = self.update_plmnsel(p['mcc'], p['mnc']) |
| 930 | if sw != '9000': |
Philipp Maier | 2d15ea0 | 2019-03-20 12:40:36 +0100 | [diff] [blame] | 931 | print("Programming PLMNsel failed with code %s"%sw) |
| 932 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 933 | # EF.PLMNwAcT |
| 934 | if p.get('mcc') and p.get('mnc'): |
Philipp Maier | 2d15ea0 | 2019-03-20 12:40:36 +0100 | [diff] [blame] | 935 | sw = self.update_plmn_act(p['mcc'], p['mnc']) |
| 936 | if sw != '9000': |
| 937 | print("Programming PLMNwAcT failed with code %s"%sw) |
| 938 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 939 | # EF.OPLMNwAcT |
| 940 | if p.get('mcc') and p.get('mnc'): |
Philipp Maier | 2d15ea0 | 2019-03-20 12:40:36 +0100 | [diff] [blame] | 941 | sw = self.update_oplmn_act(p['mcc'], p['mnc']) |
| 942 | if sw != '9000': |
| 943 | print("Programming OPLMNwAcT failed with code %s"%sw) |
| 944 | |
Supreeth Herle | f442fb4 | 2020-01-21 12:47:32 +0100 | [diff] [blame] | 945 | # EF.HPLMNwAcT |
| 946 | if p.get('mcc') and p.get('mnc'): |
| 947 | sw = self.update_hplmn_act(p['mcc'], p['mnc']) |
| 948 | if sw != '9000': |
| 949 | print("Programming HPLMNwAcT failed with code %s"%sw) |
| 950 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 951 | # EF.AD |
Robert Falkenberg | d0505bd | 2021-02-24 14:06:18 +0100 | [diff] [blame] | 952 | if (p.get('mcc') and p.get('mnc')) or p.get('opmode'): |
| 953 | if p.get('mcc') and p.get('mnc'): |
| 954 | mnc = p['mnc'] |
| 955 | else: |
| 956 | mnc = None |
| 957 | sw = self.update_ad(mnc=mnc, opmode=p.get('opmode')) |
Philipp Maier | ee908ae | 2019-03-21 16:21:12 +0100 | [diff] [blame] | 958 | if sw != '9000': |
| 959 | print("Programming AD failed with code %s"%sw) |
Philipp Maier | 2d15ea0 | 2019-03-20 12:40:36 +0100 | [diff] [blame] | 960 | |
Daniel Willmann | 1d087ef | 2017-08-31 10:08:45 +0200 | [diff] [blame] | 961 | # EF.SMSP |
Harald Welte | 23888da | 2019-08-28 23:19:11 +0200 | [diff] [blame] | 962 | if p.get('smsp'): |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 963 | r = self._scc.select_path(['3f00', '7f10']) |
Harald Welte | 23888da | 2019-08-28 23:19:11 +0200 | [diff] [blame] | 964 | data, sw = self._scc.update_record('6f42', 1, lpad(p['smsp'], 104), force_len=True) |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 965 | |
Supreeth Herle | 5a54101 | 2019-12-22 08:59:16 +0100 | [diff] [blame] | 966 | # EF.MSISDN |
| 967 | # TODO: Alpha Identifier (currently 'ff'O * 20) |
| 968 | # TODO: Capability/Configuration1 Record Identifier |
| 969 | # TODO: Extension1 Record Identifier |
| 970 | if p.get('msisdn') is not None: |
| 971 | msisdn = enc_msisdn(p['msisdn']) |
Philipp Maier | b46cb3f | 2021-04-20 22:38:21 +0200 | [diff] [blame] | 972 | data = 'ff' * 20 + msisdn |
Supreeth Herle | 5a54101 | 2019-12-22 08:59:16 +0100 | [diff] [blame] | 973 | |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 974 | r = self._scc.select_path(['3f00', '7f10']) |
Supreeth Herle | 5a54101 | 2019-12-22 08:59:16 +0100 | [diff] [blame] | 975 | data, sw = self._scc.update_record('6F40', 1, data, force_len=True) |
| 976 | |
Alexander Chemeris | e0d9d88 | 2018-01-10 14:18:32 +0900 | [diff] [blame] | 977 | |
herlesupreeth | 4a3580b | 2020-09-29 10:11:36 +0200 | [diff] [blame] | 978 | class FairwavesSIM(UsimCard): |
Alexander Chemeris | e0d9d88 | 2018-01-10 14:18:32 +0900 | [diff] [blame] | 979 | """ |
| 980 | FairwavesSIM |
| 981 | |
| 982 | The SIM card is operating according to the standard. |
| 983 | For Ki/OP/OPC programming the following files are additionally open for writing: |
| 984 | 3F00/7F20/FF01 – OP/OPC: |
| 985 | byte 1 = 0x01, bytes 2-17: OPC; |
| 986 | byte 1 = 0x00, bytes 2-17: OP; |
| 987 | 3F00/7F20/FF02: Ki |
| 988 | """ |
| 989 | |
Philipp Maier | 5a87631 | 2019-11-11 11:01:46 +0100 | [diff] [blame] | 990 | name = 'Fairwaves-SIM' |
Alexander Chemeris | e0d9d88 | 2018-01-10 14:18:32 +0900 | [diff] [blame] | 991 | # Propriatary files |
| 992 | _EF_num = { |
| 993 | 'Ki': 'FF02', |
| 994 | 'OP/OPC': 'FF01', |
| 995 | } |
| 996 | _EF = { |
| 997 | 'Ki': DF['GSM']+[_EF_num['Ki']], |
| 998 | 'OP/OPC': DF['GSM']+[_EF_num['OP/OPC']], |
| 999 | } |
| 1000 | |
| 1001 | def __init__(self, ssc): |
| 1002 | super(FairwavesSIM, self).__init__(ssc) |
| 1003 | self._adm_chv_num = 0x11 |
| 1004 | self._adm2_chv_num = 0x12 |
| 1005 | |
| 1006 | |
| 1007 | @classmethod |
| 1008 | def autodetect(kls, scc): |
| 1009 | try: |
| 1010 | # Look for ATR |
| 1011 | if scc.get_atr() == toBytes("3B 9F 96 80 1F C7 80 31 A0 73 BE 21 13 67 44 22 06 10 00 00 01 A9"): |
| 1012 | return kls(scc) |
| 1013 | except: |
| 1014 | return None |
| 1015 | return None |
| 1016 | |
| 1017 | |
| 1018 | def verify_adm2(self, key): |
| 1019 | ''' |
| 1020 | Authenticate with ADM2 key. |
| 1021 | |
| 1022 | Fairwaves SIM cards support hierarchical key structure and ADM2 key |
| 1023 | is a key which has access to proprietary files (Ki and OP/OPC). |
| 1024 | That said, ADM key inherits permissions of ADM2 key and thus we rarely |
| 1025 | need ADM2 key per se. |
| 1026 | ''' |
| 1027 | (res, sw) = self._scc.verify_chv(self._adm2_chv_num, key) |
| 1028 | return sw |
| 1029 | |
| 1030 | |
| 1031 | def read_ki(self): |
| 1032 | """ |
| 1033 | Read Ki in proprietary file. |
| 1034 | |
| 1035 | Requires ADM1 access level |
| 1036 | """ |
| 1037 | return self._scc.read_binary(self._EF['Ki']) |
| 1038 | |
| 1039 | |
| 1040 | def update_ki(self, ki): |
| 1041 | """ |
| 1042 | Set Ki in proprietary file. |
| 1043 | |
| 1044 | Requires ADM1 access level |
| 1045 | """ |
| 1046 | data, sw = self._scc.update_binary(self._EF['Ki'], ki) |
| 1047 | return sw |
| 1048 | |
| 1049 | |
| 1050 | def read_op_opc(self): |
| 1051 | """ |
| 1052 | Read Ki in proprietary file. |
| 1053 | |
| 1054 | Requires ADM1 access level |
| 1055 | """ |
| 1056 | (ef, sw) = self._scc.read_binary(self._EF['OP/OPC']) |
| 1057 | type = 'OP' if ef[0:2] == '00' else 'OPC' |
| 1058 | return ((type, ef[2:]), sw) |
| 1059 | |
| 1060 | |
| 1061 | def update_op(self, op): |
| 1062 | """ |
| 1063 | Set OP in proprietary file. |
| 1064 | |
| 1065 | Requires ADM1 access level |
| 1066 | """ |
| 1067 | content = '00' + op |
| 1068 | data, sw = self._scc.update_binary(self._EF['OP/OPC'], content) |
| 1069 | return sw |
| 1070 | |
| 1071 | |
| 1072 | def update_opc(self, opc): |
| 1073 | """ |
| 1074 | Set OPC in proprietary file. |
| 1075 | |
| 1076 | Requires ADM1 access level |
| 1077 | """ |
| 1078 | content = '01' + opc |
| 1079 | data, sw = self._scc.update_binary(self._EF['OP/OPC'], content) |
| 1080 | return sw |
| 1081 | |
| 1082 | |
| 1083 | def program(self, p): |
| 1084 | # authenticate as ADM1 |
| 1085 | if not p['pin_adm']: |
| 1086 | raise ValueError("Please provide a PIN-ADM as there is no default one") |
Philipp Maier | 05f42ee | 2021-03-11 13:59:44 +0100 | [diff] [blame] | 1087 | self.verify_adm(h2b(p['pin_adm'])) |
Alexander Chemeris | e0d9d88 | 2018-01-10 14:18:32 +0900 | [diff] [blame] | 1088 | |
| 1089 | # TODO: Set operator name |
| 1090 | if p.get('smsp') is not None: |
| 1091 | sw = self.update_smsp(p['smsp']) |
| 1092 | if sw != '9000': |
| 1093 | print("Programming SMSP failed with code %s"%sw) |
| 1094 | # This SIM doesn't support changing ICCID |
| 1095 | if p.get('mcc') is not None and p.get('mnc') is not None: |
| 1096 | sw = self.update_hplmn_act(p['mcc'], p['mnc']) |
| 1097 | if sw != '9000': |
| 1098 | print("Programming MCC/MNC failed with code %s"%sw) |
| 1099 | if p.get('imsi') is not None: |
| 1100 | sw = self.update_imsi(p['imsi']) |
| 1101 | if sw != '9000': |
| 1102 | print("Programming IMSI failed with code %s"%sw) |
| 1103 | if p.get('ki') is not None: |
| 1104 | sw = self.update_ki(p['ki']) |
| 1105 | if sw != '9000': |
| 1106 | print("Programming Ki failed with code %s"%sw) |
| 1107 | if p.get('opc') is not None: |
| 1108 | sw = self.update_opc(p['opc']) |
| 1109 | if sw != '9000': |
| 1110 | print("Programming OPC failed with code %s"%sw) |
| 1111 | if p.get('acc') is not None: |
| 1112 | sw = self.update_acc(p['acc']) |
| 1113 | if sw != '9000': |
| 1114 | print("Programming ACC failed with code %s"%sw) |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 1115 | |
Todd Neal | 9eeadfc | 2018-04-25 15:36:29 -0500 | [diff] [blame] | 1116 | class OpenCellsSim(Card): |
| 1117 | """ |
| 1118 | OpenCellsSim |
| 1119 | |
| 1120 | """ |
| 1121 | |
Philipp Maier | 5a87631 | 2019-11-11 11:01:46 +0100 | [diff] [blame] | 1122 | name = 'OpenCells-SIM' |
Todd Neal | 9eeadfc | 2018-04-25 15:36:29 -0500 | [diff] [blame] | 1123 | |
| 1124 | def __init__(self, ssc): |
| 1125 | super(OpenCellsSim, self).__init__(ssc) |
| 1126 | self._adm_chv_num = 0x0A |
| 1127 | |
| 1128 | |
| 1129 | @classmethod |
| 1130 | def autodetect(kls, scc): |
| 1131 | try: |
| 1132 | # Look for ATR |
| 1133 | if scc.get_atr() == toBytes("3B 9F 95 80 1F C3 80 31 E0 73 FE 21 13 57 86 81 02 86 98 44 18 A8"): |
| 1134 | return kls(scc) |
| 1135 | except: |
| 1136 | return None |
| 1137 | return None |
| 1138 | |
| 1139 | |
| 1140 | def program(self, p): |
| 1141 | if not p['pin_adm']: |
| 1142 | raise ValueError("Please provide a PIN-ADM as there is no default one") |
| 1143 | self._scc.verify_chv(0x0A, h2b(p['pin_adm'])) |
| 1144 | |
| 1145 | # select MF |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 1146 | r = self._scc.select_path(['3f00']) |
Todd Neal | 9eeadfc | 2018-04-25 15:36:29 -0500 | [diff] [blame] | 1147 | |
| 1148 | # write EF.ICCID |
| 1149 | data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid'])) |
| 1150 | |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 1151 | r = self._scc.select_path(['7ff0']) |
Todd Neal | 9eeadfc | 2018-04-25 15:36:29 -0500 | [diff] [blame] | 1152 | |
| 1153 | # set Ki in proprietary file |
| 1154 | data, sw = self._scc.update_binary('FF02', p['ki']) |
| 1155 | |
| 1156 | # set OPC in proprietary file |
| 1157 | data, sw = self._scc.update_binary('FF01', p['opc']) |
| 1158 | |
| 1159 | # select DF_GSM |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 1160 | r = self._scc.select_path(['7f20']) |
Todd Neal | 9eeadfc | 2018-04-25 15:36:29 -0500 | [diff] [blame] | 1161 | |
| 1162 | # write EF.IMSI |
| 1163 | data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi'])) |
| 1164 | |
herlesupreeth | 4a3580b | 2020-09-29 10:11:36 +0200 | [diff] [blame] | 1165 | class WavemobileSim(UsimCard): |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1166 | """ |
| 1167 | WavemobileSim |
| 1168 | |
| 1169 | """ |
| 1170 | |
| 1171 | name = 'Wavemobile-SIM' |
| 1172 | |
| 1173 | def __init__(self, ssc): |
| 1174 | super(WavemobileSim, self).__init__(ssc) |
| 1175 | self._adm_chv_num = 0x0A |
| 1176 | self._scc.cla_byte = "00" |
| 1177 | self._scc.sel_ctrl = "0004" #request an FCP |
| 1178 | |
| 1179 | @classmethod |
| 1180 | def autodetect(kls, scc): |
| 1181 | try: |
| 1182 | # Look for ATR |
| 1183 | if scc.get_atr() == toBytes("3B 9F 95 80 1F C7 80 31 E0 73 F6 21 13 67 4D 45 16 00 43 01 00 8F"): |
| 1184 | return kls(scc) |
| 1185 | except: |
| 1186 | return None |
| 1187 | return None |
| 1188 | |
| 1189 | def program(self, p): |
| 1190 | if not p['pin_adm']: |
| 1191 | raise ValueError("Please provide a PIN-ADM as there is no default one") |
Philipp Maier | 05f42ee | 2021-03-11 13:59:44 +0100 | [diff] [blame] | 1192 | self.verify_adm(h2b(p['pin_adm'])) |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1193 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1194 | # EF.ICCID |
| 1195 | # TODO: Add programming of the ICCID |
| 1196 | if p.get('iccid'): |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1197 | print("Warning: Programming of the ICCID is not implemented for this type of card.") |
| 1198 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1199 | # KI (Presumably a propritary file) |
| 1200 | # TODO: Add programming of KI |
| 1201 | if p.get('ki'): |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1202 | print("Warning: Programming of the KI is not implemented for this type of card.") |
| 1203 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1204 | # OPc (Presumably a propritary file) |
| 1205 | # TODO: Add programming of OPc |
| 1206 | if p.get('opc'): |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1207 | print("Warning: Programming of the OPc is not implemented for this type of card.") |
| 1208 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1209 | # EF.SMSP |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1210 | if p.get('smsp'): |
| 1211 | sw = self.update_smsp(p['smsp']) |
| 1212 | if sw != '9000': |
| 1213 | print("Programming SMSP failed with code %s"%sw) |
| 1214 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1215 | # EF.IMSI |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1216 | if p.get('imsi'): |
| 1217 | sw = self.update_imsi(p['imsi']) |
| 1218 | if sw != '9000': |
| 1219 | print("Programming IMSI failed with code %s"%sw) |
| 1220 | |
| 1221 | # EF.ACC |
| 1222 | if p.get('acc'): |
| 1223 | sw = self.update_acc(p['acc']) |
| 1224 | if sw != '9000': |
| 1225 | print("Programming ACC failed with code %s"%sw) |
| 1226 | |
| 1227 | # EF.PLMNsel |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1228 | if p.get('mcc') and p.get('mnc'): |
| 1229 | sw = self.update_plmnsel(p['mcc'], p['mnc']) |
| 1230 | if sw != '9000': |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1231 | print("Programming PLMNsel failed with code %s"%sw) |
| 1232 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1233 | # EF.PLMNwAcT |
| 1234 | if p.get('mcc') and p.get('mnc'): |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1235 | sw = self.update_plmn_act(p['mcc'], p['mnc']) |
| 1236 | if sw != '9000': |
| 1237 | print("Programming PLMNwAcT failed with code %s"%sw) |
| 1238 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1239 | # EF.OPLMNwAcT |
| 1240 | if p.get('mcc') and p.get('mnc'): |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1241 | sw = self.update_oplmn_act(p['mcc'], p['mnc']) |
| 1242 | if sw != '9000': |
| 1243 | print("Programming OPLMNwAcT failed with code %s"%sw) |
| 1244 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1245 | # EF.AD |
Robert Falkenberg | d0505bd | 2021-02-24 14:06:18 +0100 | [diff] [blame] | 1246 | if (p.get('mcc') and p.get('mnc')) or p.get('opmode'): |
| 1247 | if p.get('mcc') and p.get('mnc'): |
| 1248 | mnc = p['mnc'] |
| 1249 | else: |
| 1250 | mnc = None |
| 1251 | sw = self.update_ad(mnc=mnc, opmode=p.get('opmode')) |
Philipp Maier | 6e507a7 | 2019-04-01 16:33:48 +0200 | [diff] [blame] | 1252 | if sw != '9000': |
| 1253 | print("Programming AD failed with code %s"%sw) |
| 1254 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1255 | return None |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1256 | |
Todd Neal | 9eeadfc | 2018-04-25 15:36:29 -0500 | [diff] [blame] | 1257 | |
herlesupreeth | b0c7d12 | 2020-12-23 09:25:46 +0100 | [diff] [blame] | 1258 | class SysmoISIMSJA2(UsimCard, IsimCard): |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1259 | """ |
| 1260 | sysmocom sysmoISIM-SJA2 |
| 1261 | """ |
| 1262 | |
| 1263 | name = 'sysmoISIM-SJA2' |
| 1264 | |
| 1265 | def __init__(self, ssc): |
| 1266 | super(SysmoISIMSJA2, self).__init__(ssc) |
| 1267 | self._scc.cla_byte = "00" |
| 1268 | self._scc.sel_ctrl = "0004" #request an FCP |
| 1269 | |
| 1270 | @classmethod |
| 1271 | def autodetect(kls, scc): |
| 1272 | try: |
| 1273 | # Try card model #1 |
| 1274 | atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 75 30 34 05 4B A9" |
| 1275 | if scc.get_atr() == toBytes(atr): |
| 1276 | return kls(scc) |
| 1277 | |
| 1278 | # Try card model #2 |
| 1279 | atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 75 31 33 02 51 B2" |
| 1280 | if scc.get_atr() == toBytes(atr): |
| 1281 | return kls(scc) |
Philipp Maier | b3e11ea | 2020-03-11 12:32:44 +0100 | [diff] [blame] | 1282 | |
| 1283 | # Try card model #3 |
| 1284 | atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 52 75 31 04 51 D5" |
| 1285 | if scc.get_atr() == toBytes(atr): |
| 1286 | return kls(scc) |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1287 | except: |
| 1288 | return None |
| 1289 | return None |
| 1290 | |
Harald Welte | a670425 | 2021-01-08 20:19:11 +0100 | [diff] [blame] | 1291 | def verify_adm(self, key): |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1292 | # authenticate as ADM using default key (written on the card..) |
Harald Welte | a670425 | 2021-01-08 20:19:11 +0100 | [diff] [blame] | 1293 | if not key: |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1294 | raise ValueError("Please provide a PIN-ADM as there is no default one") |
Harald Welte | a670425 | 2021-01-08 20:19:11 +0100 | [diff] [blame] | 1295 | (res, sw) = self._scc.verify_chv(0x0A, key) |
Harald Welte | a670425 | 2021-01-08 20:19:11 +0100 | [diff] [blame] | 1296 | return sw |
| 1297 | |
| 1298 | def program(self, p): |
| 1299 | self.verify_adm(h2b(p['pin_adm'])) |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1300 | |
| 1301 | # This type of card does not allow to reprogram the ICCID. |
| 1302 | # Reprogramming the ICCID would mess up the card os software |
| 1303 | # license management, so the ICCID must be kept at its factory |
| 1304 | # setting! |
| 1305 | if p.get('iccid'): |
| 1306 | print("Warning: Programming of the ICCID is not implemented for this type of card.") |
| 1307 | |
| 1308 | # select DF_GSM |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 1309 | self._scc.select_path(['7f20']) |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1310 | |
Robert Falkenberg | 5459536 | 2021-04-06 12:04:34 +0200 | [diff] [blame] | 1311 | # set Service Provider Name |
| 1312 | if p.get('name') is not None: |
| 1313 | content = enc_spn(p['name'], True, True) |
| 1314 | data, sw = self._scc.update_binary('6F46', rpad(content, 32)) |
| 1315 | |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1316 | # write EF.IMSI |
| 1317 | if p.get('imsi'): |
| 1318 | self._scc.update_binary('6f07', enc_imsi(p['imsi'])) |
| 1319 | |
| 1320 | # EF.PLMNsel |
| 1321 | if p.get('mcc') and p.get('mnc'): |
| 1322 | sw = self.update_plmnsel(p['mcc'], p['mnc']) |
| 1323 | if sw != '9000': |
| 1324 | print("Programming PLMNsel failed with code %s"%sw) |
| 1325 | |
| 1326 | # EF.PLMNwAcT |
| 1327 | if p.get('mcc') and p.get('mnc'): |
| 1328 | sw = self.update_plmn_act(p['mcc'], p['mnc']) |
| 1329 | if sw != '9000': |
| 1330 | print("Programming PLMNwAcT failed with code %s"%sw) |
| 1331 | |
| 1332 | # EF.OPLMNwAcT |
| 1333 | if p.get('mcc') and p.get('mnc'): |
| 1334 | sw = self.update_oplmn_act(p['mcc'], p['mnc']) |
| 1335 | if sw != '9000': |
| 1336 | print("Programming OPLMNwAcT failed with code %s"%sw) |
| 1337 | |
Harald Welte | 32f0d41 | 2020-05-05 17:35:57 +0200 | [diff] [blame] | 1338 | # EF.HPLMNwAcT |
| 1339 | if p.get('mcc') and p.get('mnc'): |
| 1340 | sw = self.update_hplmn_act(p['mcc'], p['mnc']) |
| 1341 | if sw != '9000': |
| 1342 | print("Programming HPLMNwAcT failed with code %s"%sw) |
| 1343 | |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1344 | # EF.AD |
Robert Falkenberg | d0505bd | 2021-02-24 14:06:18 +0100 | [diff] [blame] | 1345 | if (p.get('mcc') and p.get('mnc')) or p.get('opmode'): |
| 1346 | if p.get('mcc') and p.get('mnc'): |
| 1347 | mnc = p['mnc'] |
| 1348 | else: |
| 1349 | mnc = None |
| 1350 | sw = self.update_ad(mnc=mnc, opmode=p.get('opmode')) |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1351 | if sw != '9000': |
| 1352 | print("Programming AD failed with code %s"%sw) |
| 1353 | |
| 1354 | # EF.SMSP |
| 1355 | if p.get('smsp'): |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 1356 | r = self._scc.select_path(['3f00', '7f10']) |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1357 | data, sw = self._scc.update_record('6f42', 1, lpad(p['smsp'], 104), force_len=True) |
| 1358 | |
Supreeth Herle | c601923 | 2020-03-26 10:00:45 +0100 | [diff] [blame] | 1359 | # EF.MSISDN |
| 1360 | # TODO: Alpha Identifier (currently 'ff'O * 20) |
| 1361 | # TODO: Capability/Configuration1 Record Identifier |
| 1362 | # TODO: Extension1 Record Identifier |
| 1363 | if p.get('msisdn') is not None: |
| 1364 | msisdn = enc_msisdn(p['msisdn']) |
Philipp Maier | b46cb3f | 2021-04-20 22:38:21 +0200 | [diff] [blame] | 1365 | content = 'ff' * 20 + msisdn |
Supreeth Herle | c601923 | 2020-03-26 10:00:45 +0100 | [diff] [blame] | 1366 | |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 1367 | r = self._scc.select_path(['3f00', '7f10']) |
Supreeth Herle | c601923 | 2020-03-26 10:00:45 +0100 | [diff] [blame] | 1368 | data, sw = self._scc.update_record('6F40', 1, content, force_len=True) |
| 1369 | |
Supreeth Herle | a97944b | 2020-03-26 10:03:25 +0100 | [diff] [blame] | 1370 | # EF.ACC |
| 1371 | if p.get('acc'): |
| 1372 | sw = self.update_acc(p['acc']) |
| 1373 | if sw != '9000': |
| 1374 | print("Programming ACC failed with code %s"%sw) |
| 1375 | |
Supreeth Herle | 8016405 | 2020-03-23 12:06:29 +0100 | [diff] [blame] | 1376 | # Populate AIDs |
| 1377 | self.read_aids() |
| 1378 | |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1379 | # update EF-SIM_AUTH_KEY (and EF-USIM_AUTH_KEY_2G, which is |
| 1380 | # hard linked to EF-USIM_AUTH_KEY) |
Harald Welte | c0499c8 | 2021-01-21 16:06:50 +0100 | [diff] [blame] | 1381 | self._scc.select_path(['3f00']) |
| 1382 | self._scc.select_path(['a515']) |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1383 | if p.get('ki'): |
| 1384 | self._scc.update_binary('6f20', p['ki'], 1) |
| 1385 | if p.get('opc'): |
| 1386 | self._scc.update_binary('6f20', p['opc'], 17) |
| 1387 | |
| 1388 | # update EF-USIM_AUTH_KEY in ADF.ISIM |
Philipp Maier | cba6dbc | 2021-03-11 13:03:18 +0100 | [diff] [blame] | 1389 | data, sw = self.select_adf_by_aid(adf="isim") |
| 1390 | if sw == '9000': |
Philipp Maier | d950786 | 2020-03-11 12:18:29 +0100 | [diff] [blame] | 1391 | if p.get('ki'): |
| 1392 | self._scc.update_binary('af20', p['ki'], 1) |
| 1393 | if p.get('opc'): |
| 1394 | self._scc.update_binary('af20', p['opc'], 17) |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1395 | |
Supreeth Herle | cf727f2 | 2020-03-24 17:32:21 +0100 | [diff] [blame] | 1396 | # update EF.P-CSCF in ADF.ISIM |
| 1397 | if self.file_exists(EF_ISIM_ADF_map['PCSCF']): |
| 1398 | if p.get('pcscf'): |
| 1399 | sw = self.update_pcscf(p['pcscf']) |
| 1400 | else: |
| 1401 | sw = self.update_pcscf("") |
| 1402 | if sw != '9000': |
| 1403 | print("Programming P-CSCF failed with code %s"%sw) |
| 1404 | |
| 1405 | |
Supreeth Herle | 79f43dd | 2020-03-25 11:43:19 +0100 | [diff] [blame] | 1406 | # update EF.DOMAIN in ADF.ISIM |
| 1407 | if self.file_exists(EF_ISIM_ADF_map['DOMAIN']): |
| 1408 | if p.get('ims_hdomain'): |
| 1409 | sw = self.update_domain(domain=p['ims_hdomain']) |
| 1410 | else: |
| 1411 | sw = self.update_domain() |
| 1412 | |
| 1413 | if sw != '9000': |
| 1414 | print("Programming Home Network Domain Name failed with code %s"%sw) |
| 1415 | |
Supreeth Herle | a5bd968 | 2020-03-26 09:16:14 +0100 | [diff] [blame] | 1416 | # update EF.IMPI in ADF.ISIM |
| 1417 | # TODO: Validate IMPI input |
| 1418 | if self.file_exists(EF_ISIM_ADF_map['IMPI']): |
| 1419 | if p.get('impi'): |
| 1420 | sw = self.update_impi(p['impi']) |
| 1421 | else: |
| 1422 | sw = self.update_impi() |
| 1423 | if sw != '9000': |
| 1424 | print("Programming IMPI failed with code %s"%sw) |
| 1425 | |
Supreeth Herle | be7007e | 2020-03-26 09:27:45 +0100 | [diff] [blame] | 1426 | # update EF.IMPU in ADF.ISIM |
| 1427 | # TODO: Validate IMPU input |
| 1428 | # Support multiple IMPU if there is enough space |
| 1429 | if self.file_exists(EF_ISIM_ADF_map['IMPU']): |
| 1430 | if p.get('impu'): |
| 1431 | sw = self.update_impu(p['impu']) |
| 1432 | else: |
| 1433 | sw = self.update_impu() |
| 1434 | if sw != '9000': |
| 1435 | print("Programming IMPU failed with code %s"%sw) |
| 1436 | |
Philipp Maier | cba6dbc | 2021-03-11 13:03:18 +0100 | [diff] [blame] | 1437 | data, sw = self.select_adf_by_aid(adf="usim") |
| 1438 | if sw == '9000': |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 1439 | # update EF-USIM_AUTH_KEY in ADF.USIM |
Philipp Maier | d950786 | 2020-03-11 12:18:29 +0100 | [diff] [blame] | 1440 | if p.get('ki'): |
| 1441 | self._scc.update_binary('af20', p['ki'], 1) |
| 1442 | if p.get('opc'): |
| 1443 | self._scc.update_binary('af20', p['opc'], 17) |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1444 | |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 1445 | # update EF.EHPLMN in ADF.USIM |
Harald Welte | 1e42420 | 2020-08-31 15:04:19 +0200 | [diff] [blame] | 1446 | if self.file_exists(EF_USIM_ADF_map['EHPLMN']): |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 1447 | if p.get('mcc') and p.get('mnc'): |
| 1448 | sw = self.update_ehplmn(p['mcc'], p['mnc']) |
| 1449 | if sw != '9000': |
| 1450 | print("Programming EHPLMN failed with code %s"%sw) |
Supreeth Herle | 8e0fccd | 2020-03-23 12:10:56 +0100 | [diff] [blame] | 1451 | |
| 1452 | # update EF.ePDGId in ADF.USIM |
| 1453 | if self.file_exists(EF_USIM_ADF_map['ePDGId']): |
| 1454 | if p.get('epdgid'): |
herlesupreeth | 5d0a30c | 2020-09-29 09:44:24 +0200 | [diff] [blame] | 1455 | sw = self.update_epdgid(p['epdgid']) |
Supreeth Herle | 4779034 | 2020-03-25 12:51:38 +0100 | [diff] [blame] | 1456 | else: |
| 1457 | sw = self.update_epdgid("") |
| 1458 | if sw != '9000': |
| 1459 | print("Programming ePDGId failed with code %s"%sw) |
Supreeth Herle | 8e0fccd | 2020-03-23 12:10:56 +0100 | [diff] [blame] | 1460 | |
Supreeth Herle | f964df4 | 2020-03-24 13:15:37 +0100 | [diff] [blame] | 1461 | # update EF.ePDGSelection in ADF.USIM |
| 1462 | if self.file_exists(EF_USIM_ADF_map['ePDGSelection']): |
| 1463 | if p.get('epdgSelection'): |
| 1464 | epdg_plmn = p['epdgSelection'] |
| 1465 | sw = self.update_ePDGSelection(epdg_plmn[:3], epdg_plmn[3:]) |
| 1466 | else: |
| 1467 | sw = self.update_ePDGSelection("", "") |
| 1468 | if sw != '9000': |
| 1469 | print("Programming ePDGSelection failed with code %s"%sw) |
| 1470 | |
| 1471 | |
Supreeth Herle | acc222f | 2020-03-24 13:26:53 +0100 | [diff] [blame] | 1472 | # After successfully programming EF.ePDGId and EF.ePDGSelection, |
| 1473 | # Set service 106 and 107 as available in EF.UST |
Supreeth Herle | 44e0462 | 2020-03-25 10:34:28 +0100 | [diff] [blame] | 1474 | # Disable service 95, 99, 115 if ISIM application is present |
Supreeth Herle | acc222f | 2020-03-24 13:26:53 +0100 | [diff] [blame] | 1475 | if self.file_exists(EF_USIM_ADF_map['UST']): |
| 1476 | if p.get('epdgSelection') and p.get('epdgid'): |
| 1477 | sw = self.update_ust(106, 1) |
| 1478 | if sw != '9000': |
| 1479 | print("Programming UST failed with code %s"%sw) |
| 1480 | sw = self.update_ust(107, 1) |
| 1481 | if sw != '9000': |
| 1482 | print("Programming UST failed with code %s"%sw) |
| 1483 | |
Supreeth Herle | 44e0462 | 2020-03-25 10:34:28 +0100 | [diff] [blame] | 1484 | sw = self.update_ust(95, 0) |
| 1485 | if sw != '9000': |
| 1486 | print("Programming UST failed with code %s"%sw) |
| 1487 | sw = self.update_ust(99, 0) |
| 1488 | if sw != '9000': |
| 1489 | print("Programming UST failed with code %s"%sw) |
| 1490 | sw = self.update_ust(115, 0) |
| 1491 | if sw != '9000': |
| 1492 | print("Programming UST failed with code %s"%sw) |
| 1493 | |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1494 | return |
| 1495 | |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1496 | |
Todd Neal | 9eeadfc | 2018-04-25 15:36:29 -0500 | [diff] [blame] | 1497 | # In order for autodetection ... |
Harald Welte | e10394b | 2011-12-07 12:34:14 +0100 | [diff] [blame] | 1498 | _cards_classes = [ FakeMagicSim, SuperSim, MagicSim, GrcardSim, |
Alexander Chemeris | e0d9d88 | 2018-01-10 14:18:32 +0900 | [diff] [blame] | 1499 | SysmoSIMgr1, SysmoSIMgr2, SysmoUSIMgr1, SysmoUSIMSJS1, |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1500 | FairwavesSIM, OpenCellsSim, WavemobileSim, SysmoISIMSJA2 ] |
Alexander Chemeris | 8ad124a | 2018-01-10 14:17:55 +0900 | [diff] [blame] | 1501 | |
| 1502 | def card_autodetect(scc): |
| 1503 | for kls in _cards_classes: |
| 1504 | card = kls.autodetect(scc) |
| 1505 | if card is not None: |
| 1506 | card.reset() |
| 1507 | return card |
| 1508 | return None |
Supreeth Herle | 4c306ab | 2020-03-18 11:38:00 +0100 | [diff] [blame] | 1509 | |
| 1510 | def card_detect(ctype, scc): |
| 1511 | # Detect type if needed |
| 1512 | card = None |
| 1513 | ctypes = dict([(kls.name, kls) for kls in _cards_classes]) |
| 1514 | |
| 1515 | if ctype in ("auto", "auto_once"): |
| 1516 | for kls in _cards_classes: |
| 1517 | card = kls.autodetect(scc) |
| 1518 | if card: |
| 1519 | print("Autodetected card type: %s" % card.name) |
| 1520 | card.reset() |
| 1521 | break |
| 1522 | |
| 1523 | if card is None: |
| 1524 | print("Autodetection failed") |
| 1525 | return None |
| 1526 | |
| 1527 | if ctype == "auto_once": |
| 1528 | ctype = card.name |
| 1529 | |
| 1530 | elif ctype in ctypes: |
| 1531 | card = ctypes[ctype](scc) |
| 1532 | |
| 1533 | else: |
| 1534 | raise ValueError("Unknown card type: %s" % ctype) |
| 1535 | |
| 1536 | return card |