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