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