Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | """ pySim: Card programmation logic |
| 5 | """ |
| 6 | |
| 7 | # |
| 8 | # Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com> |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 9 | # Copyright (C) 2011 Harald Welte <laforge@gnumonks.org> |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 10 | # Copyright (C) 2017 Alexander.Chemeris <Alexander.Chemeris@gmail.com> |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 11 | # |
| 12 | # This program is free software: you can redistribute it and/or modify |
| 13 | # it under the terms of the GNU General Public License as published by |
| 14 | # the Free Software Foundation, either version 2 of the License, or |
| 15 | # (at your option) any later version. |
| 16 | # |
| 17 | # This program is distributed in the hope that it will be useful, |
| 18 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | # GNU General Public License for more details. |
| 21 | # |
| 22 | # You should have received a copy of the GNU General Public License |
| 23 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | # |
| 25 | |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 26 | from pySim.ts_51_011 import EF, DF |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 27 | from pySim.ts_31_102 import EF_USIM_ADF_map |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 28 | from pySim.utils import * |
Alexander Chemeris | 8ad124a | 2018-01-10 14:17:55 +0900 | [diff] [blame] | 29 | from smartcard.util import toBytes |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 30 | |
| 31 | class Card(object): |
| 32 | |
| 33 | def __init__(self, scc): |
| 34 | self._scc = scc |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 35 | self._adm_chv_num = 4 |
Supreeth Herle | e4e9831 | 2020-03-18 11:33:14 +0100 | [diff] [blame] | 36 | self._aids = [] |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 37 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 38 | def reset(self): |
| 39 | self._scc.reset_card() |
| 40 | |
Philipp Maier | d58c632 | 2020-05-12 16:47:45 +0200 | [diff] [blame] | 41 | def erase(self): |
| 42 | print("warning: erasing is not supported for specified card type!") |
| 43 | return |
| 44 | |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 45 | def file_exists(self, fid): |
| 46 | res_arr = self._scc.try_select_file(fid) |
| 47 | for res in res_arr: |
Harald Welte | 1e42420 | 2020-08-31 15:04:19 +0200 | [diff] [blame] | 48 | if res[1] != '9000': |
| 49 | return False |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 50 | return True |
| 51 | |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 52 | def verify_adm(self, key): |
| 53 | ''' |
| 54 | Authenticate with ADM key |
| 55 | ''' |
| 56 | (res, sw) = self._scc.verify_chv(self._adm_chv_num, key) |
| 57 | return sw |
| 58 | |
| 59 | def read_iccid(self): |
| 60 | (res, sw) = self._scc.read_binary(EF['ICCID']) |
| 61 | if sw == '9000': |
| 62 | return (dec_iccid(res), sw) |
| 63 | else: |
| 64 | return (None, sw) |
| 65 | |
| 66 | def read_imsi(self): |
| 67 | (res, sw) = self._scc.read_binary(EF['IMSI']) |
| 68 | if sw == '9000': |
| 69 | return (dec_imsi(res), sw) |
| 70 | else: |
| 71 | return (None, sw) |
| 72 | |
| 73 | def update_imsi(self, imsi): |
| 74 | data, sw = self._scc.update_binary(EF['IMSI'], enc_imsi(imsi)) |
| 75 | return sw |
| 76 | |
| 77 | def update_acc(self, acc): |
| 78 | data, sw = self._scc.update_binary(EF['ACC'], lpad(acc, 4)) |
| 79 | return sw |
| 80 | |
Supreeth Herle | a850a47 | 2020-03-19 12:44:11 +0100 | [diff] [blame] | 81 | def read_hplmn_act(self): |
| 82 | (res, sw) = self._scc.read_binary(EF['HPLMNAcT']) |
| 83 | if sw == '9000': |
| 84 | return (format_xplmn_w_act(res), sw) |
| 85 | else: |
| 86 | return (None, sw) |
| 87 | |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 88 | def update_hplmn_act(self, mcc, mnc, access_tech='FFFF'): |
| 89 | """ |
| 90 | Update Home PLMN with access technology bit-field |
| 91 | |
| 92 | See Section "10.3.37 EFHPLMNwAcT (HPLMN Selector with Access Technology)" |
| 93 | in ETSI TS 151 011 for the details of the access_tech field coding. |
| 94 | Some common values: |
| 95 | access_tech = '0080' # Only GSM is selected |
| 96 | access_tech = 'FFFF' # All technologues selected, even Reserved for Future Use ones |
| 97 | """ |
| 98 | # get size and write EF.HPLMNwAcT |
Supreeth Herle | 2d78597 | 2019-11-30 11:00:10 +0100 | [diff] [blame] | 99 | data = self._scc.read_binary(EF['HPLMNwAcT'], length=None, offset=0) |
Vadim Yanitskiy | 9664b2e | 2020-02-27 01:49:51 +0700 | [diff] [blame] | 100 | size = len(data[0]) // 2 |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 101 | hplmn = enc_plmn(mcc, mnc) |
| 102 | content = hplmn + access_tech |
Vadim Yanitskiy | 9664b2e | 2020-02-27 01:49:51 +0700 | [diff] [blame] | 103 | 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] | 104 | return sw |
| 105 | |
Supreeth Herle | 1757b26 | 2020-03-19 12:43:11 +0100 | [diff] [blame] | 106 | def read_oplmn_act(self): |
| 107 | (res, sw) = self._scc.read_binary(EF['OPLMNwAcT']) |
| 108 | if sw == '9000': |
| 109 | return (format_xplmn_w_act(res), sw) |
| 110 | else: |
| 111 | return (None, sw) |
| 112 | |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 113 | def update_oplmn_act(self, mcc, mnc, access_tech='FFFF'): |
| 114 | """ |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 115 | See note in update_hplmn_act() |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 116 | """ |
| 117 | # get size and write EF.OPLMNwAcT |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 118 | data = self._scc.read_binary(EF['OPLMNwAcT'], length=None, offset=0) |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 119 | size = len(data[0]) // 2 |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 120 | hplmn = enc_plmn(mcc, mnc) |
| 121 | content = hplmn + access_tech |
Vadim Yanitskiy | 9664b2e | 2020-02-27 01:49:51 +0700 | [diff] [blame] | 122 | 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] | 123 | return sw |
| 124 | |
Supreeth Herle | 1408440 | 2020-03-19 12:42:10 +0100 | [diff] [blame] | 125 | def read_plmn_act(self): |
| 126 | (res, sw) = self._scc.read_binary(EF['PLMNwAcT']) |
| 127 | if sw == '9000': |
| 128 | return (format_xplmn_w_act(res), sw) |
| 129 | else: |
| 130 | return (None, sw) |
| 131 | |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 132 | def update_plmn_act(self, mcc, mnc, access_tech='FFFF'): |
| 133 | """ |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 134 | See note in update_hplmn_act() |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 135 | """ |
| 136 | # get size and write EF.PLMNwAcT |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 137 | data = self._scc.read_binary(EF['PLMNwAcT'], length=None, offset=0) |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 138 | size = len(data[0]) // 2 |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 139 | hplmn = enc_plmn(mcc, mnc) |
| 140 | content = hplmn + access_tech |
Vadim Yanitskiy | 9664b2e | 2020-02-27 01:49:51 +0700 | [diff] [blame] | 141 | 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] | 142 | return sw |
| 143 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 144 | def update_plmnsel(self, mcc, mnc): |
| 145 | data = self._scc.read_binary(EF['PLMNsel'], length=None, offset=0) |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 146 | size = len(data[0]) // 2 |
Philipp Maier | 5bf4260 | 2018-07-11 23:23:40 +0200 | [diff] [blame] | 147 | hplmn = enc_plmn(mcc, mnc) |
Philipp Maier | af9ae8b | 2018-07-13 11:15:49 +0200 | [diff] [blame] | 148 | data, sw = self._scc.update_binary(EF['PLMNsel'], hplmn + 'ff' * (size-3)) |
| 149 | return sw |
Philipp Maier | 5bf4260 | 2018-07-11 23:23:40 +0200 | [diff] [blame] | 150 | |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 151 | def update_smsp(self, smsp): |
| 152 | data, sw = self._scc.update_record(EF['SMSP'], 1, rpad(smsp, 84)) |
| 153 | return sw |
| 154 | |
Philipp Maier | ee908ae | 2019-03-21 16:21:12 +0100 | [diff] [blame] | 155 | def update_ad(self, mnc): |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 156 | #See also: 3GPP TS 31.102, chapter 4.2.18 |
| 157 | mnclen = len(str(mnc)) |
| 158 | if mnclen == 1: |
| 159 | mnclen = 2 |
| 160 | if mnclen > 3: |
Philipp Maier | ee908ae | 2019-03-21 16:21:12 +0100 | [diff] [blame] | 161 | raise RuntimeError('unable to calculate proper mnclen') |
| 162 | |
Philipp Maier | 7f9f64a | 2020-05-11 21:28:52 +0200 | [diff] [blame] | 163 | data, sw = self._scc.read_binary(EF['AD'], length=None, offset=0) |
| 164 | |
| 165 | # Reset contents to EF.AD in case the file is uninintalized |
| 166 | if data.lower() == "ffffffff": |
| 167 | data = "00000000" |
| 168 | |
| 169 | content = data[0:6] + "%02X" % mnclen |
Philipp Maier | ee908ae | 2019-03-21 16:21:12 +0100 | [diff] [blame] | 170 | data, sw = self._scc.update_binary(EF['AD'], content) |
| 171 | return sw |
| 172 | |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 173 | def read_spn(self): |
| 174 | (spn, sw) = self._scc.read_binary(EF['SPN']) |
| 175 | if sw == '9000': |
| 176 | return (dec_spn(spn), sw) |
| 177 | else: |
| 178 | return (None, sw) |
| 179 | |
| 180 | def update_spn(self, name, hplmn_disp=False, oplmn_disp=False): |
| 181 | content = enc_spn(name, hplmn_disp, oplmn_disp) |
| 182 | data, sw = self._scc.update_binary(EF['SPN'], rpad(content, 32)) |
| 183 | return sw |
| 184 | |
Supreeth Herle | d21349a | 2020-04-01 08:37:47 +0200 | [diff] [blame] | 185 | def read_binary(self, ef, length=None, offset=0): |
| 186 | ef_path = ef in EF and EF[ef] or ef |
| 187 | return self._scc.read_binary(ef_path, length, offset) |
| 188 | |
Supreeth Herle | ad10d66 | 2020-04-01 08:43:08 +0200 | [diff] [blame] | 189 | def read_record(self, ef, rec_no): |
| 190 | ef_path = ef in EF and EF[ef] or ef |
| 191 | return self._scc.read_record(ef_path, rec_no) |
| 192 | |
Supreeth Herle | 98a6927 | 2020-03-18 12:14:48 +0100 | [diff] [blame] | 193 | def read_gid1(self): |
| 194 | (res, sw) = self._scc.read_binary(EF['GID1']) |
| 195 | if sw == '9000': |
| 196 | return (res, sw) |
| 197 | else: |
| 198 | return (None, sw) |
| 199 | |
Supreeth Herle | 6d66af6 | 2020-03-19 12:49:16 +0100 | [diff] [blame] | 200 | def read_msisdn(self): |
| 201 | (res, sw) = self._scc.read_record(EF['MSISDN'], 1) |
| 202 | if sw == '9000': |
| 203 | return (dec_msisdn(res), sw) |
| 204 | else: |
| 205 | return (None, sw) |
| 206 | |
Supreeth Herle | e26331e | 2020-03-20 18:50:39 +0100 | [diff] [blame] | 207 | # Read the (full) AID for either ISIM or USIM or ISIM application |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 208 | def read_aid(self, isim = False): |
| 209 | |
| 210 | # First (known) halves of the AID |
| 211 | aid_usim = "a0000000871002" |
| 212 | aid_isim = "a0000000871004" |
| 213 | |
| 214 | # Select which one to look for |
| 215 | if isim: |
| 216 | aid = aid_isim |
| 217 | else: |
| 218 | aid = aid_usim |
| 219 | |
| 220 | # Find out how many records the EF.DIR has, then go through |
| 221 | # all records and try to find the AID we are looking for |
| 222 | aid_record_count = self._scc.record_count(['2F00']) |
| 223 | for i in range(0, aid_record_count): |
| 224 | record = self._scc.read_record(['2F00'], i + 1) |
| 225 | if aid in record[0]: |
| 226 | aid_len = int(record[0][6:8], 16) |
| 227 | return record[0][8:8 + aid_len * 2] |
| 228 | |
| 229 | return None |
| 230 | |
Supreeth Herle | e4e9831 | 2020-03-18 11:33:14 +0100 | [diff] [blame] | 231 | # Fetch all the AIDs present on UICC |
| 232 | def read_aids(self): |
| 233 | try: |
| 234 | # Find out how many records the EF.DIR has |
| 235 | # and store all the AIDs in the UICC |
Sebastian Viviani | 0dc8f69 | 2020-05-29 00:14:55 +0100 | [diff] [blame] | 236 | rec_cnt = self._scc.record_count(EF['DIR']) |
Supreeth Herle | e4e9831 | 2020-03-18 11:33:14 +0100 | [diff] [blame] | 237 | for i in range(0, rec_cnt): |
Sebastian Viviani | 0dc8f69 | 2020-05-29 00:14:55 +0100 | [diff] [blame] | 238 | rec = self._scc.read_record(EF['DIR'], i + 1) |
Supreeth Herle | e4e9831 | 2020-03-18 11:33:14 +0100 | [diff] [blame] | 239 | if (rec[0][0:2], rec[0][4:6]) == ('61', '4f') and len(rec[0]) > 12 \ |
| 240 | and rec[0][8:8 + int(rec[0][6:8], 16) * 2] not in self._aids: |
| 241 | self._aids.append(rec[0][8:8 + int(rec[0][6:8], 16) * 2]) |
| 242 | except Exception as e: |
| 243 | print("Can't read AIDs from SIM -- %s" % (str(e),)) |
| 244 | |
Supreeth Herle | f9f3e5e | 2020-03-22 08:04:59 +0100 | [diff] [blame] | 245 | # Select ADF.U/ISIM in the Card using its full AID |
| 246 | def select_adf_by_aid(self, adf="usim"): |
| 247 | # Check for valid ADF name |
| 248 | if adf not in ["usim", "isim"]: |
| 249 | return None |
| 250 | |
| 251 | # First (known) halves of the U/ISIM AID |
| 252 | aid_map = {} |
| 253 | aid_map["usim"] = "a0000000871002" |
| 254 | aid_map["isim"] = "a0000000871004" |
| 255 | |
| 256 | for aid in self._aids: |
| 257 | if aid_map[adf] in aid: |
| 258 | (res, sw) = self._scc.select_adf(aid) |
| 259 | return sw |
| 260 | |
| 261 | return None |
| 262 | |
Philipp Maier | 5c2cc66 | 2020-05-12 16:27:12 +0200 | [diff] [blame] | 263 | # Erase the contents of a file |
| 264 | def erase_binary(self, ef): |
| 265 | len = self._scc.binary_size(ef) |
| 266 | self._scc.update_binary(ef, "ff" * len, offset=0, verify=True) |
| 267 | |
| 268 | # Erase the contents of a single record |
| 269 | def erase_record(self, ef, rec_no): |
| 270 | len = self._scc.record_size(ef) |
| 271 | self._scc.update_record(ef, rec_no, "ff" * len, force_len=False, verify=True) |
| 272 | |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 273 | class UsimCard(Card): |
| 274 | def __init__(self, ssc): |
| 275 | super(UsimCard, self).__init__(ssc) |
| 276 | |
| 277 | def read_ehplmn(self): |
| 278 | (res, sw) = self._scc.read_binary(EF_USIM_ADF_map['EHPLMN']) |
| 279 | if sw == '9000': |
| 280 | return (format_xplmn(res), sw) |
| 281 | else: |
| 282 | return (None, sw) |
| 283 | |
| 284 | def update_ehplmn(self, mcc, mnc): |
| 285 | data = self._scc.read_binary(EF_USIM_ADF_map['EHPLMN'], length=None, offset=0) |
| 286 | size = len(data[0]) // 2 |
| 287 | ehplmn = enc_plmn(mcc, mnc) |
| 288 | data, sw = self._scc.update_binary(EF_USIM_ADF_map['EHPLMN'], ehplmn) |
| 289 | return sw |
| 290 | |
| 291 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 292 | |
| 293 | class _MagicSimBase(Card): |
| 294 | """ |
| 295 | Theses cards uses several record based EFs to store the provider infos, |
| 296 | each possible provider uses a specific record number in each EF. The |
| 297 | indexes used are ( where N is the number of providers supported ) : |
| 298 | - [2 .. N+1] for the operator name |
Supreeth Herle | 9ca41c1 | 2020-01-21 12:50:30 +0100 | [diff] [blame] | 299 | - [1 .. N] for the programable EFs |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 300 | |
| 301 | * 3f00/7f4d/8f0c : Operator Name |
| 302 | |
| 303 | bytes 0-15 : provider name, padded with 0xff |
| 304 | byte 16 : length of the provider name |
| 305 | byte 17 : 01 for valid records, 00 otherwise |
| 306 | |
| 307 | * 3f00/7f4d/8f0d : Programmable Binary EFs |
| 308 | |
| 309 | * 3f00/7f4d/8f0e : Programmable Record EFs |
| 310 | |
| 311 | """ |
| 312 | |
| 313 | @classmethod |
| 314 | def autodetect(kls, scc): |
| 315 | try: |
| 316 | for p, l, t in kls._files.values(): |
| 317 | if not t: |
| 318 | continue |
| 319 | if scc.record_size(['3f00', '7f4d', p]) != l: |
| 320 | return None |
| 321 | except: |
| 322 | return None |
| 323 | |
| 324 | return kls(scc) |
| 325 | |
| 326 | def _get_count(self): |
| 327 | """ |
| 328 | Selects the file and returns the total number of entries |
| 329 | and entry size |
| 330 | """ |
| 331 | f = self._files['name'] |
| 332 | |
| 333 | r = self._scc.select_file(['3f00', '7f4d', f[0]]) |
| 334 | rec_len = int(r[-1][28:30], 16) |
| 335 | tlen = int(r[-1][4:8],16) |
| 336 | rec_cnt = (tlen / rec_len) - 1; |
| 337 | |
| 338 | if (rec_cnt < 1) or (rec_len != f[1]): |
| 339 | raise RuntimeError('Bad card type') |
| 340 | |
| 341 | return rec_cnt |
| 342 | |
| 343 | def program(self, p): |
| 344 | # Go to dir |
| 345 | self._scc.select_file(['3f00', '7f4d']) |
| 346 | |
| 347 | # Home PLMN in PLMN_Sel format |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 348 | hplmn = enc_plmn(p['mcc'], p['mnc']) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 349 | |
| 350 | # Operator name ( 3f00/7f4d/8f0c ) |
| 351 | self._scc.update_record(self._files['name'][0], 2, |
| 352 | rpad(b2h(p['name']), 32) + ('%02x' % len(p['name'])) + '01' |
| 353 | ) |
| 354 | |
| 355 | # ICCID/IMSI/Ki/HPLMN ( 3f00/7f4d/8f0d ) |
| 356 | v = '' |
| 357 | |
| 358 | # inline Ki |
| 359 | if self._ki_file is None: |
| 360 | v += p['ki'] |
| 361 | |
| 362 | # ICCID |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 363 | v += '3f00' + '2fe2' + '0a' + enc_iccid(p['iccid']) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 364 | |
| 365 | # IMSI |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 366 | v += '7f20' + '6f07' + '09' + enc_imsi(p['imsi']) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 367 | |
| 368 | # Ki |
| 369 | if self._ki_file: |
| 370 | v += self._ki_file + '10' + p['ki'] |
| 371 | |
| 372 | # PLMN_Sel |
| 373 | v+= '6f30' + '18' + rpad(hplmn, 36) |
| 374 | |
Alexander Chemeris | 2188524 | 2013-07-02 16:56:55 +0400 | [diff] [blame] | 375 | # ACC |
| 376 | # This doesn't work with "fake" SuperSIM cards, |
| 377 | # but will hopefully work with real SuperSIMs. |
| 378 | if p.get('acc') is not None: |
| 379 | v+= '6f78' + '02' + lpad(p['acc'], 4) |
| 380 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 381 | self._scc.update_record(self._files['b_ef'][0], 1, |
| 382 | rpad(v, self._files['b_ef'][1]*2) |
| 383 | ) |
| 384 | |
| 385 | # SMSP ( 3f00/7f4d/8f0e ) |
| 386 | # FIXME |
| 387 | |
| 388 | # Write PLMN_Sel forcefully as well |
| 389 | r = self._scc.select_file(['3f00', '7f20', '6f30']) |
| 390 | tl = int(r[-1][4:8], 16) |
| 391 | |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 392 | hplmn = enc_plmn(p['mcc'], p['mnc']) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 393 | self._scc.update_binary('6f30', hplmn + 'ff' * (tl-3)) |
| 394 | |
| 395 | def erase(self): |
| 396 | # Dummy |
| 397 | df = {} |
| 398 | for k, v in self._files.iteritems(): |
| 399 | ofs = 1 |
| 400 | fv = v[1] * 'ff' |
| 401 | if k == 'name': |
| 402 | ofs = 2 |
| 403 | fv = fv[0:-4] + '0000' |
| 404 | df[v[0]] = (fv, ofs) |
| 405 | |
| 406 | # Write |
| 407 | for n in range(0,self._get_count()): |
| 408 | for k, (msg, ofs) in df.iteritems(): |
| 409 | self._scc.update_record(['3f00', '7f4d', k], n + ofs, msg) |
| 410 | |
| 411 | |
| 412 | class SuperSim(_MagicSimBase): |
| 413 | |
| 414 | name = 'supersim' |
| 415 | |
| 416 | _files = { |
| 417 | 'name' : ('8f0c', 18, True), |
| 418 | 'b_ef' : ('8f0d', 74, True), |
| 419 | 'r_ef' : ('8f0e', 50, True), |
| 420 | } |
| 421 | |
| 422 | _ki_file = None |
| 423 | |
| 424 | |
| 425 | class MagicSim(_MagicSimBase): |
| 426 | |
| 427 | name = 'magicsim' |
| 428 | |
| 429 | _files = { |
| 430 | 'name' : ('8f0c', 18, True), |
| 431 | 'b_ef' : ('8f0d', 130, True), |
| 432 | 'r_ef' : ('8f0e', 102, False), |
| 433 | } |
| 434 | |
| 435 | _ki_file = '6f1b' |
| 436 | |
| 437 | |
| 438 | class FakeMagicSim(Card): |
| 439 | """ |
| 440 | Theses cards have a record based EF 3f00/000c that contains the provider |
| 441 | informations. See the program method for its format. The records go from |
| 442 | 1 to N. |
| 443 | """ |
| 444 | |
| 445 | name = 'fakemagicsim' |
| 446 | |
| 447 | @classmethod |
| 448 | def autodetect(kls, scc): |
| 449 | try: |
| 450 | if scc.record_size(['3f00', '000c']) != 0x5a: |
| 451 | return None |
| 452 | except: |
| 453 | return None |
| 454 | |
| 455 | return kls(scc) |
| 456 | |
| 457 | def _get_infos(self): |
| 458 | """ |
| 459 | Selects the file and returns the total number of entries |
| 460 | and entry size |
| 461 | """ |
| 462 | |
| 463 | r = self._scc.select_file(['3f00', '000c']) |
| 464 | rec_len = int(r[-1][28:30], 16) |
| 465 | tlen = int(r[-1][4:8],16) |
| 466 | rec_cnt = (tlen / rec_len) - 1; |
| 467 | |
| 468 | if (rec_cnt < 1) or (rec_len != 0x5a): |
| 469 | raise RuntimeError('Bad card type') |
| 470 | |
| 471 | return rec_cnt, rec_len |
| 472 | |
| 473 | def program(self, p): |
| 474 | # Home PLMN |
| 475 | r = self._scc.select_file(['3f00', '7f20', '6f30']) |
| 476 | tl = int(r[-1][4:8], 16) |
| 477 | |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 478 | hplmn = enc_plmn(p['mcc'], p['mnc']) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 479 | self._scc.update_binary('6f30', hplmn + 'ff' * (tl-3)) |
| 480 | |
| 481 | # Get total number of entries and entry size |
| 482 | rec_cnt, rec_len = self._get_infos() |
| 483 | |
| 484 | # Set first entry |
| 485 | entry = ( |
Philipp Maier | 45daa92 | 2019-04-01 15:49:45 +0200 | [diff] [blame] | 486 | '81' + # 1b Status: Valid & Active |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 487 | rpad(b2h(p['name'][0:14]), 28) + # 14b Entry Name |
Philipp Maier | 45daa92 | 2019-04-01 15:49:45 +0200 | [diff] [blame] | 488 | enc_iccid(p['iccid']) + # 10b ICCID |
| 489 | enc_imsi(p['imsi']) + # 9b IMSI_len + id_type(9) + IMSI |
| 490 | p['ki'] + # 16b Ki |
| 491 | lpad(p['smsp'], 80) # 40b SMSP (padded with ff if needed) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 492 | ) |
| 493 | self._scc.update_record('000c', 1, entry) |
| 494 | |
| 495 | def erase(self): |
| 496 | # Get total number of entries and entry size |
| 497 | rec_cnt, rec_len = self._get_infos() |
| 498 | |
| 499 | # Erase all entries |
| 500 | entry = 'ff' * rec_len |
| 501 | for i in range(0, rec_cnt): |
| 502 | self._scc.update_record('000c', 1+i, entry) |
| 503 | |
Sylvain Munaut | 5da8d4e | 2013-07-02 15:13:24 +0200 | [diff] [blame] | 504 | |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 505 | class GrcardSim(Card): |
| 506 | """ |
| 507 | Greencard (grcard.cn) HZCOS GSM SIM |
| 508 | These cards have a much more regular ISO 7816-4 / TS 11.11 structure, |
| 509 | and use standard UPDATE RECORD / UPDATE BINARY commands except for Ki. |
| 510 | """ |
| 511 | |
| 512 | name = 'grcardsim' |
| 513 | |
| 514 | @classmethod |
| 515 | def autodetect(kls, scc): |
| 516 | return None |
| 517 | |
| 518 | def program(self, p): |
| 519 | # We don't really know yet what ADM PIN 4 is about |
| 520 | #self._scc.verify_chv(4, h2b("4444444444444444")) |
| 521 | |
| 522 | # Authenticate using ADM PIN 5 |
Jan Balke | c3ebd33 | 2015-01-26 12:22:55 +0100 | [diff] [blame] | 523 | if p['pin_adm']: |
Philipp Maier | a3de5a3 | 2018-08-23 10:27:04 +0200 | [diff] [blame] | 524 | pin = h2b(p['pin_adm']) |
Jan Balke | c3ebd33 | 2015-01-26 12:22:55 +0100 | [diff] [blame] | 525 | else: |
| 526 | pin = h2b("4444444444444444") |
| 527 | self._scc.verify_chv(5, pin) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 528 | |
| 529 | # EF.ICCID |
| 530 | r = self._scc.select_file(['3f00', '2fe2']) |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 531 | data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid'])) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 532 | |
| 533 | # EF.IMSI |
| 534 | r = self._scc.select_file(['3f00', '7f20', '6f07']) |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 535 | data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi'])) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 536 | |
| 537 | # EF.ACC |
Alexander Chemeris | 2188524 | 2013-07-02 16:56:55 +0400 | [diff] [blame] | 538 | if p.get('acc') is not None: |
| 539 | data, sw = self._scc.update_binary('6f78', lpad(p['acc'], 4)) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 540 | |
| 541 | # EF.SMSP |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 542 | if p.get('smsp'): |
Harald Welte | 23888da | 2019-08-28 23:19:11 +0200 | [diff] [blame] | 543 | r = self._scc.select_file(['3f00', '7f10', '6f42']) |
| 544 | data, sw = self._scc.update_record('6f42', 1, lpad(p['smsp'], 80)) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 545 | |
| 546 | # Set the Ki using proprietary command |
| 547 | pdu = '80d4020010' + p['ki'] |
| 548 | data, sw = self._scc._tp.send_apdu(pdu) |
| 549 | |
| 550 | # EF.HPLMN |
| 551 | r = self._scc.select_file(['3f00', '7f20', '6f30']) |
| 552 | size = int(r[-1][4:8], 16) |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 553 | hplmn = enc_plmn(p['mcc'], p['mnc']) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 554 | self._scc.update_binary('6f30', hplmn + 'ff' * (size-3)) |
| 555 | |
| 556 | # EF.SPN (Service Provider Name) |
| 557 | r = self._scc.select_file(['3f00', '7f20', '6f30']) |
| 558 | size = int(r[-1][4:8], 16) |
| 559 | # FIXME |
| 560 | |
| 561 | # FIXME: EF.MSISDN |
| 562 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 563 | |
Harald Welte | e10394b | 2011-12-07 12:34:14 +0100 | [diff] [blame] | 564 | class SysmoSIMgr1(GrcardSim): |
| 565 | """ |
| 566 | sysmocom sysmoSIM-GR1 |
| 567 | These cards have a much more regular ISO 7816-4 / TS 11.11 structure, |
| 568 | and use standard UPDATE RECORD / UPDATE BINARY commands except for Ki. |
| 569 | """ |
| 570 | name = 'sysmosim-gr1' |
| 571 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 572 | @classmethod |
Philipp Maier | 087feff | 2018-08-23 09:41:36 +0200 | [diff] [blame] | 573 | def autodetect(kls, scc): |
| 574 | try: |
| 575 | # Look for ATR |
| 576 | if scc.get_atr() == toBytes("3B 99 18 00 11 88 22 33 44 55 66 77 60"): |
| 577 | return kls(scc) |
| 578 | except: |
| 579 | return None |
| 580 | return None |
Sylvain Munaut | 5da8d4e | 2013-07-02 15:13:24 +0200 | [diff] [blame] | 581 | |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 582 | class SysmoUSIMgr1(UsimCard): |
Holger Hans Peter Freyther | 4d91bf4 | 2012-03-22 14:28:38 +0100 | [diff] [blame] | 583 | """ |
| 584 | sysmocom sysmoUSIM-GR1 |
| 585 | """ |
| 586 | name = 'sysmoUSIM-GR1' |
| 587 | |
| 588 | @classmethod |
| 589 | def autodetect(kls, scc): |
| 590 | # TODO: Access the ATR |
| 591 | return None |
| 592 | |
| 593 | def program(self, p): |
| 594 | # TODO: check if verify_chv could be used or what it needs |
| 595 | # self._scc.verify_chv(0x0A, [0x33,0x32,0x32,0x31,0x33,0x32,0x33,0x32]) |
| 596 | # Unlock the card.. |
| 597 | data, sw = self._scc._tp.send_apdu_checksw("0020000A083332323133323332") |
| 598 | |
| 599 | # TODO: move into SimCardCommands |
Holger Hans Peter Freyther | 4d91bf4 | 2012-03-22 14:28:38 +0100 | [diff] [blame] | 600 | par = ( p['ki'] + # 16b K |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 601 | p['opc'] + # 32b OPC |
| 602 | enc_iccid(p['iccid']) + # 10b ICCID |
| 603 | 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] | 604 | ) |
| 605 | data, sw = self._scc._tp.send_apdu_checksw("0099000033" + par) |
| 606 | |
Sylvain Munaut | 053c895 | 2013-07-02 15:12:32 +0200 | [diff] [blame] | 607 | |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 608 | class SysmoSIMgr2(Card): |
| 609 | """ |
| 610 | sysmocom sysmoSIM-GR2 |
| 611 | """ |
| 612 | |
| 613 | name = 'sysmoSIM-GR2' |
| 614 | |
| 615 | @classmethod |
| 616 | def autodetect(kls, scc): |
Alexander Chemeris | 8ad124a | 2018-01-10 14:17:55 +0900 | [diff] [blame] | 617 | try: |
| 618 | # Look for ATR |
| 619 | if scc.get_atr() == toBytes("3B 7D 94 00 00 55 55 53 0A 74 86 93 0B 24 7C 4D 54 68"): |
| 620 | return kls(scc) |
| 621 | except: |
| 622 | return None |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 623 | return None |
| 624 | |
| 625 | def program(self, p): |
| 626 | |
| 627 | # select MF |
| 628 | r = self._scc.select_file(['3f00']) |
| 629 | |
| 630 | # authenticate as SUPER ADM using default key |
| 631 | self._scc.verify_chv(0x0b, h2b("3838383838383838")) |
| 632 | |
| 633 | # set ADM pin using proprietary command |
| 634 | # INS: D4 |
| 635 | # P1: 3A for PIN, 3B for PUK |
| 636 | # P2: CHV number, as in VERIFY CHV for PIN, and as in UNBLOCK CHV for PUK |
| 637 | # 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] | 638 | if p['pin_adm']: |
Daniel Willmann | 7d38d74 | 2018-06-15 07:31:50 +0200 | [diff] [blame] | 639 | pin = h2b(p['pin_adm']) |
Jan Balke | c3ebd33 | 2015-01-26 12:22:55 +0100 | [diff] [blame] | 640 | else: |
| 641 | pin = h2b("4444444444444444") |
| 642 | |
| 643 | pdu = 'A0D43A0508' + b2h(pin) |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 644 | data, sw = self._scc._tp.send_apdu(pdu) |
| 645 | |
| 646 | # authenticate as ADM (enough to write file, and can set PINs) |
Jan Balke | c3ebd33 | 2015-01-26 12:22:55 +0100 | [diff] [blame] | 647 | |
| 648 | self._scc.verify_chv(0x05, pin) |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 649 | |
| 650 | # write EF.ICCID |
| 651 | data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid'])) |
| 652 | |
| 653 | # select DF_GSM |
| 654 | r = self._scc.select_file(['7f20']) |
| 655 | |
| 656 | # write EF.IMSI |
| 657 | data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi'])) |
| 658 | |
| 659 | # write EF.ACC |
| 660 | if p.get('acc') is not None: |
| 661 | data, sw = self._scc.update_binary('6f78', lpad(p['acc'], 4)) |
| 662 | |
| 663 | # get size and write EF.HPLMN |
| 664 | r = self._scc.select_file(['6f30']) |
| 665 | size = int(r[-1][4:8], 16) |
| 666 | hplmn = enc_plmn(p['mcc'], p['mnc']) |
| 667 | self._scc.update_binary('6f30', hplmn + 'ff' * (size-3)) |
| 668 | |
| 669 | # set COMP128 version 0 in proprietary file |
| 670 | data, sw = self._scc.update_binary('0001', '001000') |
| 671 | |
| 672 | # set Ki in proprietary file |
| 673 | data, sw = self._scc.update_binary('0001', p['ki'], 3) |
| 674 | |
| 675 | # select DF_TELECOM |
| 676 | r = self._scc.select_file(['3f00', '7f10']) |
| 677 | |
| 678 | # write EF.SMSP |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 679 | if p.get('smsp'): |
Harald Welte | 23888da | 2019-08-28 23:19:11 +0200 | [diff] [blame] | 680 | data, sw = self._scc.update_record('6f42', 1, lpad(p['smsp'], 80)) |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 681 | |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 682 | |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 683 | class SysmoUSIMSJS1(UsimCard): |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 684 | """ |
| 685 | sysmocom sysmoUSIM-SJS1 |
| 686 | """ |
| 687 | |
| 688 | name = 'sysmoUSIM-SJS1' |
| 689 | |
| 690 | def __init__(self, ssc): |
| 691 | super(SysmoUSIMSJS1, self).__init__(ssc) |
| 692 | self._scc.cla_byte = "00" |
Philipp Maier | 2d15ea0 | 2019-03-20 12:40:36 +0100 | [diff] [blame] | 693 | self._scc.sel_ctrl = "0004" #request an FCP |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 694 | |
| 695 | @classmethod |
| 696 | def autodetect(kls, scc): |
Alexander Chemeris | 8ad124a | 2018-01-10 14:17:55 +0900 | [diff] [blame] | 697 | try: |
| 698 | # Look for ATR |
| 699 | 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"): |
| 700 | return kls(scc) |
| 701 | except: |
| 702 | return None |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 703 | return None |
| 704 | |
| 705 | def program(self, p): |
| 706 | |
Philipp Maier | e960488 | 2017-03-21 17:24:31 +0100 | [diff] [blame] | 707 | # authenticate as ADM using default key (written on the card..) |
| 708 | if not p['pin_adm']: |
| 709 | raise ValueError("Please provide a PIN-ADM as there is no default one") |
| 710 | self._scc.verify_chv(0x0A, h2b(p['pin_adm'])) |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 711 | |
| 712 | # select MF |
| 713 | r = self._scc.select_file(['3f00']) |
| 714 | |
Philipp Maier | e960488 | 2017-03-21 17:24:31 +0100 | [diff] [blame] | 715 | # write EF.ICCID |
| 716 | data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid'])) |
| 717 | |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 718 | # select DF_GSM |
| 719 | r = self._scc.select_file(['7f20']) |
| 720 | |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 721 | # set Ki in proprietary file |
| 722 | data, sw = self._scc.update_binary('00FF', p['ki']) |
| 723 | |
Philipp Maier | 1be35bf | 2018-07-13 11:29:03 +0200 | [diff] [blame] | 724 | # set OPc in proprietary file |
Daniel Willmann | 67acdbc | 2018-06-15 07:42:48 +0200 | [diff] [blame] | 725 | if 'opc' in p: |
| 726 | content = "01" + p['opc'] |
| 727 | data, sw = self._scc.update_binary('00F7', content) |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 728 | |
Supreeth Herle | 7947d92 | 2019-06-08 07:50:53 +0200 | [diff] [blame] | 729 | # set Service Provider Name |
Supreeth Herle | 840a9e2 | 2020-01-21 13:32:46 +0100 | [diff] [blame] | 730 | if p.get('name') is not None: |
| 731 | content = enc_spn(p['name'], True, True) |
| 732 | data, sw = self._scc.update_binary('6F46', rpad(content, 32)) |
Supreeth Herle | 7947d92 | 2019-06-08 07:50:53 +0200 | [diff] [blame] | 733 | |
Supreeth Herle | c8796a3 | 2019-12-23 12:23:42 +0100 | [diff] [blame] | 734 | if p.get('acc') is not None: |
| 735 | self.update_acc(p['acc']) |
| 736 | |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 737 | # write EF.IMSI |
| 738 | data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi'])) |
| 739 | |
Philipp Maier | 2d15ea0 | 2019-03-20 12:40:36 +0100 | [diff] [blame] | 740 | # EF.PLMNsel |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 741 | if p.get('mcc') and p.get('mnc'): |
| 742 | sw = self.update_plmnsel(p['mcc'], p['mnc']) |
| 743 | if sw != '9000': |
Philipp Maier | 2d15ea0 | 2019-03-20 12:40:36 +0100 | [diff] [blame] | 744 | print("Programming PLMNsel failed with code %s"%sw) |
| 745 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 746 | # EF.PLMNwAcT |
| 747 | if p.get('mcc') and p.get('mnc'): |
Philipp Maier | 2d15ea0 | 2019-03-20 12:40:36 +0100 | [diff] [blame] | 748 | sw = self.update_plmn_act(p['mcc'], p['mnc']) |
| 749 | if sw != '9000': |
| 750 | print("Programming PLMNwAcT failed with code %s"%sw) |
| 751 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 752 | # EF.OPLMNwAcT |
| 753 | if p.get('mcc') and p.get('mnc'): |
Philipp Maier | 2d15ea0 | 2019-03-20 12:40:36 +0100 | [diff] [blame] | 754 | sw = self.update_oplmn_act(p['mcc'], p['mnc']) |
| 755 | if sw != '9000': |
| 756 | print("Programming OPLMNwAcT failed with code %s"%sw) |
| 757 | |
Supreeth Herle | f442fb4 | 2020-01-21 12:47:32 +0100 | [diff] [blame] | 758 | # EF.HPLMNwAcT |
| 759 | if p.get('mcc') and p.get('mnc'): |
| 760 | sw = self.update_hplmn_act(p['mcc'], p['mnc']) |
| 761 | if sw != '9000': |
| 762 | print("Programming HPLMNwAcT failed with code %s"%sw) |
| 763 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 764 | # EF.AD |
| 765 | if p.get('mcc') and p.get('mnc'): |
Philipp Maier | ee908ae | 2019-03-21 16:21:12 +0100 | [diff] [blame] | 766 | sw = self.update_ad(p['mnc']) |
| 767 | if sw != '9000': |
| 768 | print("Programming AD failed with code %s"%sw) |
Philipp Maier | 2d15ea0 | 2019-03-20 12:40:36 +0100 | [diff] [blame] | 769 | |
Daniel Willmann | 1d087ef | 2017-08-31 10:08:45 +0200 | [diff] [blame] | 770 | # EF.SMSP |
Harald Welte | 23888da | 2019-08-28 23:19:11 +0200 | [diff] [blame] | 771 | if p.get('smsp'): |
| 772 | r = self._scc.select_file(['3f00', '7f10']) |
| 773 | 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] | 774 | |
Supreeth Herle | 5a54101 | 2019-12-22 08:59:16 +0100 | [diff] [blame] | 775 | # EF.MSISDN |
| 776 | # TODO: Alpha Identifier (currently 'ff'O * 20) |
| 777 | # TODO: Capability/Configuration1 Record Identifier |
| 778 | # TODO: Extension1 Record Identifier |
| 779 | if p.get('msisdn') is not None: |
| 780 | msisdn = enc_msisdn(p['msisdn']) |
| 781 | data = 'ff' * 20 + msisdn + 'ff' * 2 |
| 782 | |
| 783 | r = self._scc.select_file(['3f00', '7f10']) |
| 784 | data, sw = self._scc.update_record('6F40', 1, data, force_len=True) |
| 785 | |
Alexander Chemeris | e0d9d88 | 2018-01-10 14:18:32 +0900 | [diff] [blame] | 786 | |
| 787 | class FairwavesSIM(Card): |
| 788 | """ |
| 789 | FairwavesSIM |
| 790 | |
| 791 | The SIM card is operating according to the standard. |
| 792 | For Ki/OP/OPC programming the following files are additionally open for writing: |
| 793 | 3F00/7F20/FF01 – OP/OPC: |
| 794 | byte 1 = 0x01, bytes 2-17: OPC; |
| 795 | byte 1 = 0x00, bytes 2-17: OP; |
| 796 | 3F00/7F20/FF02: Ki |
| 797 | """ |
| 798 | |
Philipp Maier | 5a87631 | 2019-11-11 11:01:46 +0100 | [diff] [blame] | 799 | name = 'Fairwaves-SIM' |
Alexander Chemeris | e0d9d88 | 2018-01-10 14:18:32 +0900 | [diff] [blame] | 800 | # Propriatary files |
| 801 | _EF_num = { |
| 802 | 'Ki': 'FF02', |
| 803 | 'OP/OPC': 'FF01', |
| 804 | } |
| 805 | _EF = { |
| 806 | 'Ki': DF['GSM']+[_EF_num['Ki']], |
| 807 | 'OP/OPC': DF['GSM']+[_EF_num['OP/OPC']], |
| 808 | } |
| 809 | |
| 810 | def __init__(self, ssc): |
| 811 | super(FairwavesSIM, self).__init__(ssc) |
| 812 | self._adm_chv_num = 0x11 |
| 813 | self._adm2_chv_num = 0x12 |
| 814 | |
| 815 | |
| 816 | @classmethod |
| 817 | def autodetect(kls, scc): |
| 818 | try: |
| 819 | # Look for ATR |
| 820 | 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"): |
| 821 | return kls(scc) |
| 822 | except: |
| 823 | return None |
| 824 | return None |
| 825 | |
| 826 | |
| 827 | def verify_adm2(self, key): |
| 828 | ''' |
| 829 | Authenticate with ADM2 key. |
| 830 | |
| 831 | Fairwaves SIM cards support hierarchical key structure and ADM2 key |
| 832 | is a key which has access to proprietary files (Ki and OP/OPC). |
| 833 | That said, ADM key inherits permissions of ADM2 key and thus we rarely |
| 834 | need ADM2 key per se. |
| 835 | ''' |
| 836 | (res, sw) = self._scc.verify_chv(self._adm2_chv_num, key) |
| 837 | return sw |
| 838 | |
| 839 | |
| 840 | def read_ki(self): |
| 841 | """ |
| 842 | Read Ki in proprietary file. |
| 843 | |
| 844 | Requires ADM1 access level |
| 845 | """ |
| 846 | return self._scc.read_binary(self._EF['Ki']) |
| 847 | |
| 848 | |
| 849 | def update_ki(self, ki): |
| 850 | """ |
| 851 | Set Ki in proprietary file. |
| 852 | |
| 853 | Requires ADM1 access level |
| 854 | """ |
| 855 | data, sw = self._scc.update_binary(self._EF['Ki'], ki) |
| 856 | return sw |
| 857 | |
| 858 | |
| 859 | def read_op_opc(self): |
| 860 | """ |
| 861 | Read Ki in proprietary file. |
| 862 | |
| 863 | Requires ADM1 access level |
| 864 | """ |
| 865 | (ef, sw) = self._scc.read_binary(self._EF['OP/OPC']) |
| 866 | type = 'OP' if ef[0:2] == '00' else 'OPC' |
| 867 | return ((type, ef[2:]), sw) |
| 868 | |
| 869 | |
| 870 | def update_op(self, op): |
| 871 | """ |
| 872 | Set OP in proprietary file. |
| 873 | |
| 874 | Requires ADM1 access level |
| 875 | """ |
| 876 | content = '00' + op |
| 877 | data, sw = self._scc.update_binary(self._EF['OP/OPC'], content) |
| 878 | return sw |
| 879 | |
| 880 | |
| 881 | def update_opc(self, opc): |
| 882 | """ |
| 883 | Set OPC in proprietary file. |
| 884 | |
| 885 | Requires ADM1 access level |
| 886 | """ |
| 887 | content = '01' + opc |
| 888 | data, sw = self._scc.update_binary(self._EF['OP/OPC'], content) |
| 889 | return sw |
| 890 | |
| 891 | |
| 892 | def program(self, p): |
| 893 | # authenticate as ADM1 |
| 894 | if not p['pin_adm']: |
| 895 | raise ValueError("Please provide a PIN-ADM as there is no default one") |
| 896 | sw = self.verify_adm(h2b(p['pin_adm'])) |
| 897 | if sw != '9000': |
| 898 | raise RuntimeError('Failed to authenticate with ADM key %s'%(p['pin_adm'],)) |
| 899 | |
| 900 | # TODO: Set operator name |
| 901 | if p.get('smsp') is not None: |
| 902 | sw = self.update_smsp(p['smsp']) |
| 903 | if sw != '9000': |
| 904 | print("Programming SMSP failed with code %s"%sw) |
| 905 | # This SIM doesn't support changing ICCID |
| 906 | if p.get('mcc') is not None and p.get('mnc') is not None: |
| 907 | sw = self.update_hplmn_act(p['mcc'], p['mnc']) |
| 908 | if sw != '9000': |
| 909 | print("Programming MCC/MNC failed with code %s"%sw) |
| 910 | if p.get('imsi') is not None: |
| 911 | sw = self.update_imsi(p['imsi']) |
| 912 | if sw != '9000': |
| 913 | print("Programming IMSI failed with code %s"%sw) |
| 914 | if p.get('ki') is not None: |
| 915 | sw = self.update_ki(p['ki']) |
| 916 | if sw != '9000': |
| 917 | print("Programming Ki failed with code %s"%sw) |
| 918 | if p.get('opc') is not None: |
| 919 | sw = self.update_opc(p['opc']) |
| 920 | if sw != '9000': |
| 921 | print("Programming OPC failed with code %s"%sw) |
| 922 | if p.get('acc') is not None: |
| 923 | sw = self.update_acc(p['acc']) |
| 924 | if sw != '9000': |
| 925 | print("Programming ACC failed with code %s"%sw) |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 926 | |
Todd Neal | 9eeadfc | 2018-04-25 15:36:29 -0500 | [diff] [blame] | 927 | class OpenCellsSim(Card): |
| 928 | """ |
| 929 | OpenCellsSim |
| 930 | |
| 931 | """ |
| 932 | |
Philipp Maier | 5a87631 | 2019-11-11 11:01:46 +0100 | [diff] [blame] | 933 | name = 'OpenCells-SIM' |
Todd Neal | 9eeadfc | 2018-04-25 15:36:29 -0500 | [diff] [blame] | 934 | |
| 935 | def __init__(self, ssc): |
| 936 | super(OpenCellsSim, self).__init__(ssc) |
| 937 | self._adm_chv_num = 0x0A |
| 938 | |
| 939 | |
| 940 | @classmethod |
| 941 | def autodetect(kls, scc): |
| 942 | try: |
| 943 | # Look for ATR |
| 944 | 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"): |
| 945 | return kls(scc) |
| 946 | except: |
| 947 | return None |
| 948 | return None |
| 949 | |
| 950 | |
| 951 | def program(self, p): |
| 952 | if not p['pin_adm']: |
| 953 | raise ValueError("Please provide a PIN-ADM as there is no default one") |
| 954 | self._scc.verify_chv(0x0A, h2b(p['pin_adm'])) |
| 955 | |
| 956 | # select MF |
| 957 | r = self._scc.select_file(['3f00']) |
| 958 | |
| 959 | # write EF.ICCID |
| 960 | data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid'])) |
| 961 | |
| 962 | r = self._scc.select_file(['7ff0']) |
| 963 | |
| 964 | # set Ki in proprietary file |
| 965 | data, sw = self._scc.update_binary('FF02', p['ki']) |
| 966 | |
| 967 | # set OPC in proprietary file |
| 968 | data, sw = self._scc.update_binary('FF01', p['opc']) |
| 969 | |
| 970 | # select DF_GSM |
| 971 | r = self._scc.select_file(['7f20']) |
| 972 | |
| 973 | # write EF.IMSI |
| 974 | data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi'])) |
| 975 | |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 976 | class WavemobileSim(Card): |
| 977 | """ |
| 978 | WavemobileSim |
| 979 | |
| 980 | """ |
| 981 | |
| 982 | name = 'Wavemobile-SIM' |
| 983 | |
| 984 | def __init__(self, ssc): |
| 985 | super(WavemobileSim, self).__init__(ssc) |
| 986 | self._adm_chv_num = 0x0A |
| 987 | self._scc.cla_byte = "00" |
| 988 | self._scc.sel_ctrl = "0004" #request an FCP |
| 989 | |
| 990 | @classmethod |
| 991 | def autodetect(kls, scc): |
| 992 | try: |
| 993 | # Look for ATR |
| 994 | 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"): |
| 995 | return kls(scc) |
| 996 | except: |
| 997 | return None |
| 998 | return None |
| 999 | |
| 1000 | def program(self, p): |
| 1001 | if not p['pin_adm']: |
| 1002 | raise ValueError("Please provide a PIN-ADM as there is no default one") |
| 1003 | sw = self.verify_adm(h2b(p['pin_adm'])) |
| 1004 | if sw != '9000': |
| 1005 | raise RuntimeError('Failed to authenticate with ADM key %s'%(p['pin_adm'],)) |
| 1006 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1007 | # EF.ICCID |
| 1008 | # TODO: Add programming of the ICCID |
| 1009 | if p.get('iccid'): |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1010 | print("Warning: Programming of the ICCID is not implemented for this type of card.") |
| 1011 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1012 | # KI (Presumably a propritary file) |
| 1013 | # TODO: Add programming of KI |
| 1014 | if p.get('ki'): |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1015 | print("Warning: Programming of the KI is not implemented for this type of card.") |
| 1016 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1017 | # OPc (Presumably a propritary file) |
| 1018 | # TODO: Add programming of OPc |
| 1019 | if p.get('opc'): |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1020 | print("Warning: Programming of the OPc is not implemented for this type of card.") |
| 1021 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1022 | # EF.SMSP |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1023 | if p.get('smsp'): |
| 1024 | sw = self.update_smsp(p['smsp']) |
| 1025 | if sw != '9000': |
| 1026 | print("Programming SMSP failed with code %s"%sw) |
| 1027 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1028 | # EF.IMSI |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1029 | if p.get('imsi'): |
| 1030 | sw = self.update_imsi(p['imsi']) |
| 1031 | if sw != '9000': |
| 1032 | print("Programming IMSI failed with code %s"%sw) |
| 1033 | |
| 1034 | # EF.ACC |
| 1035 | if p.get('acc'): |
| 1036 | sw = self.update_acc(p['acc']) |
| 1037 | if sw != '9000': |
| 1038 | print("Programming ACC failed with code %s"%sw) |
| 1039 | |
| 1040 | # EF.PLMNsel |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1041 | if p.get('mcc') and p.get('mnc'): |
| 1042 | sw = self.update_plmnsel(p['mcc'], p['mnc']) |
| 1043 | if sw != '9000': |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1044 | print("Programming PLMNsel failed with code %s"%sw) |
| 1045 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1046 | # EF.PLMNwAcT |
| 1047 | if p.get('mcc') and p.get('mnc'): |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1048 | sw = self.update_plmn_act(p['mcc'], p['mnc']) |
| 1049 | if sw != '9000': |
| 1050 | print("Programming PLMNwAcT failed with code %s"%sw) |
| 1051 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1052 | # EF.OPLMNwAcT |
| 1053 | if p.get('mcc') and p.get('mnc'): |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1054 | sw = self.update_oplmn_act(p['mcc'], p['mnc']) |
| 1055 | if sw != '9000': |
| 1056 | print("Programming OPLMNwAcT failed with code %s"%sw) |
| 1057 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1058 | # EF.AD |
| 1059 | if p.get('mcc') and p.get('mnc'): |
Philipp Maier | 6e507a7 | 2019-04-01 16:33:48 +0200 | [diff] [blame] | 1060 | sw = self.update_ad(p['mnc']) |
| 1061 | if sw != '9000': |
| 1062 | print("Programming AD failed with code %s"%sw) |
| 1063 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1064 | return None |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1065 | |
Todd Neal | 9eeadfc | 2018-04-25 15:36:29 -0500 | [diff] [blame] | 1066 | |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 1067 | class SysmoISIMSJA2(UsimCard): |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1068 | """ |
| 1069 | sysmocom sysmoISIM-SJA2 |
| 1070 | """ |
| 1071 | |
| 1072 | name = 'sysmoISIM-SJA2' |
| 1073 | |
| 1074 | def __init__(self, ssc): |
| 1075 | super(SysmoISIMSJA2, self).__init__(ssc) |
| 1076 | self._scc.cla_byte = "00" |
| 1077 | self._scc.sel_ctrl = "0004" #request an FCP |
| 1078 | |
| 1079 | @classmethod |
| 1080 | def autodetect(kls, scc): |
| 1081 | try: |
| 1082 | # Try card model #1 |
| 1083 | atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 75 30 34 05 4B A9" |
| 1084 | if scc.get_atr() == toBytes(atr): |
| 1085 | return kls(scc) |
| 1086 | |
| 1087 | # Try card model #2 |
| 1088 | atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 75 31 33 02 51 B2" |
| 1089 | if scc.get_atr() == toBytes(atr): |
| 1090 | return kls(scc) |
Philipp Maier | b3e11ea | 2020-03-11 12:32:44 +0100 | [diff] [blame] | 1091 | |
| 1092 | # Try card model #3 |
| 1093 | atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 52 75 31 04 51 D5" |
| 1094 | if scc.get_atr() == toBytes(atr): |
| 1095 | return kls(scc) |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1096 | except: |
| 1097 | return None |
| 1098 | return None |
| 1099 | |
| 1100 | def program(self, p): |
| 1101 | # authenticate as ADM using default key (written on the card..) |
| 1102 | if not p['pin_adm']: |
| 1103 | raise ValueError("Please provide a PIN-ADM as there is no default one") |
| 1104 | self._scc.verify_chv(0x0A, h2b(p['pin_adm'])) |
| 1105 | |
| 1106 | # This type of card does not allow to reprogram the ICCID. |
| 1107 | # Reprogramming the ICCID would mess up the card os software |
| 1108 | # license management, so the ICCID must be kept at its factory |
| 1109 | # setting! |
| 1110 | if p.get('iccid'): |
| 1111 | print("Warning: Programming of the ICCID is not implemented for this type of card.") |
| 1112 | |
| 1113 | # select DF_GSM |
| 1114 | self._scc.select_file(['7f20']) |
| 1115 | |
| 1116 | # write EF.IMSI |
| 1117 | if p.get('imsi'): |
| 1118 | self._scc.update_binary('6f07', enc_imsi(p['imsi'])) |
| 1119 | |
| 1120 | # EF.PLMNsel |
| 1121 | if p.get('mcc') and p.get('mnc'): |
| 1122 | sw = self.update_plmnsel(p['mcc'], p['mnc']) |
| 1123 | if sw != '9000': |
| 1124 | print("Programming PLMNsel failed with code %s"%sw) |
| 1125 | |
| 1126 | # EF.PLMNwAcT |
| 1127 | if p.get('mcc') and p.get('mnc'): |
| 1128 | sw = self.update_plmn_act(p['mcc'], p['mnc']) |
| 1129 | if sw != '9000': |
| 1130 | print("Programming PLMNwAcT failed with code %s"%sw) |
| 1131 | |
| 1132 | # EF.OPLMNwAcT |
| 1133 | if p.get('mcc') and p.get('mnc'): |
| 1134 | sw = self.update_oplmn_act(p['mcc'], p['mnc']) |
| 1135 | if sw != '9000': |
| 1136 | print("Programming OPLMNwAcT failed with code %s"%sw) |
| 1137 | |
Harald Welte | 32f0d41 | 2020-05-05 17:35:57 +0200 | [diff] [blame] | 1138 | # EF.HPLMNwAcT |
| 1139 | if p.get('mcc') and p.get('mnc'): |
| 1140 | sw = self.update_hplmn_act(p['mcc'], p['mnc']) |
| 1141 | if sw != '9000': |
| 1142 | print("Programming HPLMNwAcT failed with code %s"%sw) |
| 1143 | |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1144 | # EF.AD |
| 1145 | if p.get('mcc') and p.get('mnc'): |
| 1146 | sw = self.update_ad(p['mnc']) |
| 1147 | if sw != '9000': |
| 1148 | print("Programming AD failed with code %s"%sw) |
| 1149 | |
| 1150 | # EF.SMSP |
| 1151 | if p.get('smsp'): |
| 1152 | r = self._scc.select_file(['3f00', '7f10']) |
| 1153 | data, sw = self._scc.update_record('6f42', 1, lpad(p['smsp'], 104), force_len=True) |
| 1154 | |
| 1155 | # update EF-SIM_AUTH_KEY (and EF-USIM_AUTH_KEY_2G, which is |
| 1156 | # hard linked to EF-USIM_AUTH_KEY) |
| 1157 | self._scc.select_file(['3f00']) |
| 1158 | self._scc.select_file(['a515']) |
| 1159 | if p.get('ki'): |
| 1160 | self._scc.update_binary('6f20', p['ki'], 1) |
| 1161 | if p.get('opc'): |
| 1162 | self._scc.update_binary('6f20', p['opc'], 17) |
| 1163 | |
| 1164 | # update EF-USIM_AUTH_KEY in ADF.ISIM |
| 1165 | self._scc.select_file(['3f00']) |
| 1166 | aid = self.read_aid(isim = True) |
Philipp Maier | d950786 | 2020-03-11 12:18:29 +0100 | [diff] [blame] | 1167 | if (aid): |
| 1168 | self._scc.select_adf(aid) |
| 1169 | if p.get('ki'): |
| 1170 | self._scc.update_binary('af20', p['ki'], 1) |
| 1171 | if p.get('opc'): |
| 1172 | self._scc.update_binary('af20', p['opc'], 17) |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1173 | |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1174 | self._scc.select_file(['3f00']) |
| 1175 | aid = self.read_aid() |
Philipp Maier | d950786 | 2020-03-11 12:18:29 +0100 | [diff] [blame] | 1176 | if (aid): |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 1177 | # update EF-USIM_AUTH_KEY in ADF.USIM |
Philipp Maier | d950786 | 2020-03-11 12:18:29 +0100 | [diff] [blame] | 1178 | self._scc.select_adf(aid) |
| 1179 | if p.get('ki'): |
| 1180 | self._scc.update_binary('af20', p['ki'], 1) |
| 1181 | if p.get('opc'): |
| 1182 | self._scc.update_binary('af20', p['opc'], 17) |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1183 | |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 1184 | # update EF.EHPLMN in ADF.USIM |
Harald Welte | 1e42420 | 2020-08-31 15:04:19 +0200 | [diff] [blame] | 1185 | if self.file_exists(EF_USIM_ADF_map['EHPLMN']): |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 1186 | if p.get('mcc') and p.get('mnc'): |
| 1187 | sw = self.update_ehplmn(p['mcc'], p['mnc']) |
| 1188 | if sw != '9000': |
| 1189 | print("Programming EHPLMN failed with code %s"%sw) |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1190 | return |
| 1191 | |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1192 | |
Todd Neal | 9eeadfc | 2018-04-25 15:36:29 -0500 | [diff] [blame] | 1193 | # In order for autodetection ... |
Harald Welte | e10394b | 2011-12-07 12:34:14 +0100 | [diff] [blame] | 1194 | _cards_classes = [ FakeMagicSim, SuperSim, MagicSim, GrcardSim, |
Alexander Chemeris | e0d9d88 | 2018-01-10 14:18:32 +0900 | [diff] [blame] | 1195 | SysmoSIMgr1, SysmoSIMgr2, SysmoUSIMgr1, SysmoUSIMSJS1, |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1196 | FairwavesSIM, OpenCellsSim, WavemobileSim, SysmoISIMSJA2 ] |
Alexander Chemeris | 8ad124a | 2018-01-10 14:17:55 +0900 | [diff] [blame] | 1197 | |
| 1198 | def card_autodetect(scc): |
| 1199 | for kls in _cards_classes: |
| 1200 | card = kls.autodetect(scc) |
| 1201 | if card is not None: |
| 1202 | card.reset() |
| 1203 | return card |
| 1204 | return None |
Supreeth Herle | 4c306ab | 2020-03-18 11:38:00 +0100 | [diff] [blame] | 1205 | |
| 1206 | def card_detect(ctype, scc): |
| 1207 | # Detect type if needed |
| 1208 | card = None |
| 1209 | ctypes = dict([(kls.name, kls) for kls in _cards_classes]) |
| 1210 | |
| 1211 | if ctype in ("auto", "auto_once"): |
| 1212 | for kls in _cards_classes: |
| 1213 | card = kls.autodetect(scc) |
| 1214 | if card: |
| 1215 | print("Autodetected card type: %s" % card.name) |
| 1216 | card.reset() |
| 1217 | break |
| 1218 | |
| 1219 | if card is None: |
| 1220 | print("Autodetection failed") |
| 1221 | return None |
| 1222 | |
| 1223 | if ctype == "auto_once": |
| 1224 | ctype = card.name |
| 1225 | |
| 1226 | elif ctype in ctypes: |
| 1227 | card = ctypes[ctype](scc) |
| 1228 | |
| 1229 | else: |
| 1230 | raise ValueError("Unknown card type: %s" % ctype) |
| 1231 | |
| 1232 | return card |