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