blob: 5eb288424b5a6ac3c5ba31a8a2104b7e21e84e51 [file] [log] [blame]
Sylvain Munaut76504e02010-12-07 00:24:32 +01001# -*- coding: utf-8 -*-
2
3""" pySim: Card programmation logic
4"""
5
6#
7# Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com>
Harald Welte3156d902011-03-22 21:48:19 +01008# Copyright (C) 2011 Harald Welte <laforge@gnumonks.org>
Alexander Chemeriseb6807d2017-07-18 17:04:38 +03009# Copyright (C) 2017 Alexander.Chemeris <Alexander.Chemeris@gmail.com>
Sylvain Munaut76504e02010-12-07 00:24:32 +010010#
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 Chemeriseb6807d2017-07-18 17:04:38 +030025from pySim.ts_51_011 import EF, DF
Harald Welteca673942020-06-03 15:19:40 +020026from pySim.ts_31_102 import EF_USIM_ADF_map
Supreeth Herle5ad9aec2020-03-24 17:26:40 +010027from pySim.ts_31_103 import EF_ISIM_ADF_map
Alexander Chemeriseb6807d2017-07-18 17:04:38 +030028from pySim.utils import *
Alexander Chemeris8ad124a2018-01-10 14:17:55 +090029from smartcard.util import toBytes
Supreeth Herle79f43dd2020-03-25 11:43:19 +010030from pytlv.TLV import *
Sylvain Munaut76504e02010-12-07 00:24:32 +010031
32class Card(object):
33
34 def __init__(self, scc):
35 self._scc = scc
Alexander Chemeriseb6807d2017-07-18 17:04:38 +030036 self._adm_chv_num = 4
Supreeth Herlee4e98312020-03-18 11:33:14 +010037 self._aids = []
Sylvain Munaut76504e02010-12-07 00:24:32 +010038
Sylvain Munaut76504e02010-12-07 00:24:32 +010039 def reset(self):
40 self._scc.reset_card()
41
Philipp Maierd58c6322020-05-12 16:47:45 +020042 def erase(self):
43 print("warning: erasing is not supported for specified card type!")
44 return
45
Harald Welteca673942020-06-03 15:19:40 +020046 def file_exists(self, fid):
Harald Weltec0499c82021-01-21 16:06:50 +010047 res_arr = self._scc.try_select_path(fid)
Harald Welteca673942020-06-03 15:19:40 +020048 for res in res_arr:
Harald Welte1e424202020-08-31 15:04:19 +020049 if res[1] != '9000':
50 return False
Harald Welteca673942020-06-03 15:19:40 +020051 return True
52
Alexander Chemeriseb6807d2017-07-18 17:04:38 +030053 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 Herlea850a472020-03-19 12:44:11 +010082 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 Chemeriseb6807d2017-07-18 17:04:38 +030089 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 Herle2d785972019-11-30 11:00:10 +0100100 data = self._scc.read_binary(EF['HPLMNwAcT'], length=None, offset=0)
Vadim Yanitskiy9664b2e2020-02-27 01:49:51 +0700101 size = len(data[0]) // 2
Alexander Chemeriseb6807d2017-07-18 17:04:38 +0300102 hplmn = enc_plmn(mcc, mnc)
103 content = hplmn + access_tech
Vadim Yanitskiy9664b2e2020-02-27 01:49:51 +0700104 data, sw = self._scc.update_binary(EF['HPLMNwAcT'], content + 'ffffff0000' * (size // 5 - 1))
Alexander Chemeriseb6807d2017-07-18 17:04:38 +0300105 return sw
106
Supreeth Herle1757b262020-03-19 12:43:11 +0100107 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 Maierc8ce82a2018-07-04 17:57:20 +0200114 def update_oplmn_act(self, mcc, mnc, access_tech='FFFF'):
115 """
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200116 See note in update_hplmn_act()
Philipp Maierc8ce82a2018-07-04 17:57:20 +0200117 """
118 # get size and write EF.OPLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200119 data = self._scc.read_binary(EF['OPLMNwAcT'], length=None, offset=0)
Vadim Yanitskiy99affe12020-02-15 05:03:09 +0700120 size = len(data[0]) // 2
Philipp Maierc8ce82a2018-07-04 17:57:20 +0200121 hplmn = enc_plmn(mcc, mnc)
122 content = hplmn + access_tech
Vadim Yanitskiy9664b2e2020-02-27 01:49:51 +0700123 data, sw = self._scc.update_binary(EF['OPLMNwAcT'], content + 'ffffff0000' * (size // 5 - 1))
Philipp Maierc8ce82a2018-07-04 17:57:20 +0200124 return sw
125
Supreeth Herle14084402020-03-19 12:42:10 +0100126 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 Maierc8ce82a2018-07-04 17:57:20 +0200133 def update_plmn_act(self, mcc, mnc, access_tech='FFFF'):
134 """
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200135 See note in update_hplmn_act()
Philipp Maierc8ce82a2018-07-04 17:57:20 +0200136 """
137 # get size and write EF.PLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200138 data = self._scc.read_binary(EF['PLMNwAcT'], length=None, offset=0)
Vadim Yanitskiy99affe12020-02-15 05:03:09 +0700139 size = len(data[0]) // 2
Philipp Maierc8ce82a2018-07-04 17:57:20 +0200140 hplmn = enc_plmn(mcc, mnc)
141 content = hplmn + access_tech
Vadim Yanitskiy9664b2e2020-02-27 01:49:51 +0700142 data, sw = self._scc.update_binary(EF['PLMNwAcT'], content + 'ffffff0000' * (size // 5 - 1))
Philipp Maierc8ce82a2018-07-04 17:57:20 +0200143 return sw
144
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200145 def update_plmnsel(self, mcc, mnc):
146 data = self._scc.read_binary(EF['PLMNsel'], length=None, offset=0)
Vadim Yanitskiy99affe12020-02-15 05:03:09 +0700147 size = len(data[0]) // 2
Philipp Maier5bf42602018-07-11 23:23:40 +0200148 hplmn = enc_plmn(mcc, mnc)
Philipp Maieraf9ae8b2018-07-13 11:15:49 +0200149 data, sw = self._scc.update_binary(EF['PLMNsel'], hplmn + 'ff' * (size-3))
150 return sw
Philipp Maier5bf42602018-07-11 23:23:40 +0200151
Alexander Chemeriseb6807d2017-07-18 17:04:38 +0300152 def update_smsp(self, smsp):
153 data, sw = self._scc.update_record(EF['SMSP'], 1, rpad(smsp, 84))
154 return sw
155
Philipp Maieree908ae2019-03-21 16:21:12 +0100156 def update_ad(self, mnc):
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200157 #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 Maieree908ae2019-03-21 16:21:12 +0100162 raise RuntimeError('unable to calculate proper mnclen')
163
Philipp Maier7f9f64a2020-05-11 21:28:52 +0200164 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 Maieree908ae2019-03-21 16:21:12 +0100171 data, sw = self._scc.update_binary(EF['AD'], content)
172 return sw
173
Alexander Chemeriseb6807d2017-07-18 17:04:38 +0300174 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 Herled21349a2020-04-01 08:37:47 +0200186 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 Herlead10d662020-04-01 08:43:08 +0200190 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 Herle98a69272020-03-18 12:14:48 +0100194 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 Herle6d66af62020-03-19 12:49:16 +0100201 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 Herlee4e98312020-03-18 11:33:14 +0100208 # Fetch all the AIDs present on UICC
209 def read_aids(self):
Philipp Maier1e896f32021-03-10 17:02:53 +0100210 self._aids = []
Supreeth Herlee4e98312020-03-18 11:33:14 +0100211 try:
212 # Find out how many records the EF.DIR has
213 # and store all the AIDs in the UICC
Sebastian Viviani0dc8f692020-05-29 00:14:55 +0100214 rec_cnt = self._scc.record_count(EF['DIR'])
Supreeth Herlee4e98312020-03-18 11:33:14 +0100215 for i in range(0, rec_cnt):
Sebastian Viviani0dc8f692020-05-29 00:14:55 +0100216 rec = self._scc.read_record(EF['DIR'], i + 1)
Supreeth Herlee4e98312020-03-18 11:33:14 +0100217 if (rec[0][0:2], rec[0][4:6]) == ('61', '4f') and len(rec[0]) > 12 \
218 and rec[0][8:8 + int(rec[0][6:8], 16) * 2] not in self._aids:
219 self._aids.append(rec[0][8:8 + int(rec[0][6:8], 16) * 2])
220 except Exception as e:
221 print("Can't read AIDs from SIM -- %s" % (str(e),))
Philipp Maier1e896f32021-03-10 17:02:53 +0100222 self._aids = []
223 return self._aids
Supreeth Herlee4e98312020-03-18 11:33:14 +0100224
Supreeth Herlef9f3e5e2020-03-22 08:04:59 +0100225 # Select ADF.U/ISIM in the Card using its full AID
226 def select_adf_by_aid(self, adf="usim"):
Philipp Maiercba6dbc2021-03-11 13:03:18 +0100227 # Find full AID by partial AID:
228 if is_hex(adf):
229 for aid in self._aids:
230 if len(aid) >= len(adf) and adf == aid[0:len(adf)]:
231 return self._scc.select_adf(aid)
232 # Find full AID by application name:
233 elif adf in ["usim", "isim"]:
234 # First (known) halves of the U/ISIM AID
235 aid_map = {}
236 aid_map["usim"] = "a0000000871002"
237 aid_map["isim"] = "a0000000871004"
238 for aid in self._aids:
239 if aid_map[adf] in aid:
240 return self._scc.select_adf(aid)
241 return (None, None)
Supreeth Herlef9f3e5e2020-03-22 08:04:59 +0100242
Philipp Maier5c2cc662020-05-12 16:27:12 +0200243 # Erase the contents of a file
244 def erase_binary(self, ef):
245 len = self._scc.binary_size(ef)
246 self._scc.update_binary(ef, "ff" * len, offset=0, verify=True)
247
248 # Erase the contents of a single record
249 def erase_record(self, ef, rec_no):
250 len = self._scc.record_size(ef)
251 self._scc.update_record(ef, rec_no, "ff" * len, force_len=False, verify=True)
252
Harald Welteca673942020-06-03 15:19:40 +0200253class UsimCard(Card):
254 def __init__(self, ssc):
255 super(UsimCard, self).__init__(ssc)
256
257 def read_ehplmn(self):
258 (res, sw) = self._scc.read_binary(EF_USIM_ADF_map['EHPLMN'])
259 if sw == '9000':
260 return (format_xplmn(res), sw)
261 else:
262 return (None, sw)
263
264 def update_ehplmn(self, mcc, mnc):
265 data = self._scc.read_binary(EF_USIM_ADF_map['EHPLMN'], length=None, offset=0)
266 size = len(data[0]) // 2
267 ehplmn = enc_plmn(mcc, mnc)
268 data, sw = self._scc.update_binary(EF_USIM_ADF_map['EHPLMN'], ehplmn)
269 return sw
270
herlesupreethf8232db2020-09-29 10:03:06 +0200271 def read_epdgid(self):
272 (res, sw) = self._scc.read_binary(EF_USIM_ADF_map['ePDGId'])
273 if sw == '9000':
Supreeth Herle3b342c22020-03-24 16:15:02 +0100274 return (dec_addr_tlv(res), sw)
herlesupreethf8232db2020-09-29 10:03:06 +0200275 else:
276 return (None, sw)
277
herlesupreeth5d0a30c2020-09-29 09:44:24 +0200278 def update_epdgid(self, epdgid):
Supreeth Herle47790342020-03-25 12:51:38 +0100279 size = self._scc.binary_size(EF_USIM_ADF_map['ePDGId']) * 2
280 if len(epdgid) > 0:
Supreeth Herlec491dc02020-03-25 14:56:13 +0100281 addr_type = get_addr_type(epdgid)
282 if addr_type == None:
283 raise ValueError("Unknown ePDG Id address type or invalid address provided")
284 epdgid_tlv = rpad(enc_addr_tlv(epdgid, ('%02x' % addr_type)), size)
Supreeth Herle47790342020-03-25 12:51:38 +0100285 else:
286 epdgid_tlv = rpad('ff', size)
herlesupreeth5d0a30c2020-09-29 09:44:24 +0200287 data, sw = self._scc.update_binary(
288 EF_USIM_ADF_map['ePDGId'], epdgid_tlv)
289 return sw
Harald Welteca673942020-06-03 15:19:40 +0200290
Supreeth Herle99d55552020-03-24 13:03:43 +0100291 def read_ePDGSelection(self):
292 (res, sw) = self._scc.read_binary(EF_USIM_ADF_map['ePDGSelection'])
293 if sw == '9000':
294 return (format_ePDGSelection(res), sw)
295 else:
296 return (None, sw)
297
Supreeth Herlef964df42020-03-24 13:15:37 +0100298 def update_ePDGSelection(self, mcc, mnc):
299 (res, sw) = self._scc.read_binary(EF_USIM_ADF_map['ePDGSelection'], length=None, offset=0)
300 if sw == '9000' and (len(mcc) == 0 or len(mnc) == 0):
301 # Reset contents
302 # 80 - Tag value
303 (res, sw) = self._scc.update_binary(EF_USIM_ADF_map['ePDGSelection'], rpad('', len(res)))
304 elif sw == '9000':
305 (res, sw) = self._scc.update_binary(EF_USIM_ADF_map['ePDGSelection'], enc_ePDGSelection(res, mcc, mnc))
306 return sw
307
herlesupreeth4a3580b2020-09-29 10:11:36 +0200308 def read_ust(self):
309 (res, sw) = self._scc.read_binary(EF_USIM_ADF_map['UST'])
310 if sw == '9000':
311 # Print those which are available
312 return ([res, dec_st(res, table="usim")], sw)
313 else:
314 return ([None, None], sw)
315
Supreeth Herleacc222f2020-03-24 13:26:53 +0100316 def update_ust(self, service, bit=1):
317 (res, sw) = self._scc.read_binary(EF_USIM_ADF_map['UST'])
318 if sw == '9000':
319 content = enc_st(res, service, bit)
320 (res, sw) = self._scc.update_binary(EF_USIM_ADF_map['UST'], content)
321 return sw
322
herlesupreethecbada92020-12-23 09:24:29 +0100323class IsimCard(Card):
324 def __init__(self, ssc):
325 super(IsimCard, self).__init__(ssc)
326
Supreeth Herle5ad9aec2020-03-24 17:26:40 +0100327 def read_pcscf(self):
328 rec_cnt = self._scc.record_count(EF_ISIM_ADF_map['PCSCF'])
329 pcscf_recs = ""
330 for i in range(0, rec_cnt):
331 (res, sw) = self._scc.read_record(EF_ISIM_ADF_map['PCSCF'], i + 1)
332 if sw == '9000':
333 content = dec_addr_tlv(res)
334 pcscf_recs += "%s" % (len(content) and content or '\tNot available\n')
335 else:
336 pcscf_recs += "\tP-CSCF: Can't read, response code = %s\n" % (sw)
337 return pcscf_recs
338
Supreeth Herlecf727f22020-03-24 17:32:21 +0100339 def update_pcscf(self, pcscf):
340 if len(pcscf) > 0:
herlesupreeth12790852020-12-24 09:38:42 +0100341 addr_type = get_addr_type(pcscf)
342 if addr_type == None:
343 raise ValueError("Unknown PCSCF address type or invalid address provided")
344 content = enc_addr_tlv(pcscf, ('%02x' % addr_type))
Supreeth Herlecf727f22020-03-24 17:32:21 +0100345 else:
346 # Just the tag value
347 content = '80'
348 rec_size_bytes = self._scc.record_size(EF_ISIM_ADF_map['PCSCF'])
herlesupreeth12790852020-12-24 09:38:42 +0100349 pcscf_tlv = rpad(content, rec_size_bytes*2)
350 data, sw = self._scc.update_record(EF_ISIM_ADF_map['PCSCF'], 1, pcscf_tlv)
Supreeth Herlecf727f22020-03-24 17:32:21 +0100351 return sw
352
Supreeth Herle05b28072020-03-25 10:23:48 +0100353 def read_domain(self):
354 (res, sw) = self._scc.read_binary(EF_ISIM_ADF_map['DOMAIN'])
355 if sw == '9000':
356 # Skip the inital tag value ('80') byte and get length of contents
357 length = int(res[2:4], 16)
358 content = h2s(res[4:4+(length*2)])
359 return (content, sw)
360 else:
361 return (None, sw)
362
Supreeth Herle79f43dd2020-03-25 11:43:19 +0100363 def update_domain(self, domain=None, mcc=None, mnc=None):
364 hex_str = ""
365 if domain:
366 hex_str = s2h(domain)
367 elif mcc and mnc:
368 # MCC and MNC always has 3 digits in domain form
369 plmn_str = 'mnc' + lpad(mnc, 3, "0") + '.mcc' + lpad(mcc, 3, "0")
370 hex_str = s2h('ims.' + plmn_str + '.3gppnetwork.org')
371
372 # Build TLV
373 tlv = TLV(['80'])
374 content = tlv.build({'80': hex_str})
375
376 bin_size_bytes = self._scc.binary_size(EF_ISIM_ADF_map['DOMAIN'])
377 data, sw = self._scc.update_binary(EF_ISIM_ADF_map['DOMAIN'], rpad(content, bin_size_bytes*2))
378 return sw
379
Supreeth Herle3f67f9c2020-03-25 15:38:02 +0100380 def read_impi(self):
381 (res, sw) = self._scc.read_binary(EF_ISIM_ADF_map['IMPI'])
382 if sw == '9000':
383 # Skip the inital tag value ('80') byte and get length of contents
384 length = int(res[2:4], 16)
385 content = h2s(res[4:4+(length*2)])
386 return (content, sw)
387 else:
388 return (None, sw)
389
Supreeth Herlea5bd9682020-03-26 09:16:14 +0100390 def update_impi(self, impi=None):
391 hex_str = ""
392 if impi:
393 hex_str = s2h(impi)
394 # Build TLV
395 tlv = TLV(['80'])
396 content = tlv.build({'80': hex_str})
397
398 bin_size_bytes = self._scc.binary_size(EF_ISIM_ADF_map['IMPI'])
399 data, sw = self._scc.update_binary(EF_ISIM_ADF_map['IMPI'], rpad(content, bin_size_bytes*2))
400 return sw
401
Supreeth Herle0c02d8a2020-03-26 09:00:06 +0100402 def read_impu(self):
403 rec_cnt = self._scc.record_count(EF_ISIM_ADF_map['IMPU'])
404 impu_recs = ""
405 for i in range(0, rec_cnt):
406 (res, sw) = self._scc.read_record(EF_ISIM_ADF_map['IMPU'], i + 1)
407 if sw == '9000':
408 # Skip the inital tag value ('80') byte and get length of contents
409 length = int(res[2:4], 16)
410 content = h2s(res[4:4+(length*2)])
411 impu_recs += "\t%s\n" % (len(content) and content or 'Not available')
412 else:
413 impu_recs += "IMS public user identity: Can't read, response code = %s\n" % (sw)
414 return impu_recs
415
Supreeth Herlebe7007e2020-03-26 09:27:45 +0100416 def update_impu(self, impu=None):
417 hex_str = ""
418 if impu:
419 hex_str = s2h(impu)
420 # Build TLV
421 tlv = TLV(['80'])
422 content = tlv.build({'80': hex_str})
423
424 rec_size_bytes = self._scc.record_size(EF_ISIM_ADF_map['IMPU'])
425 impu_tlv = rpad(content, rec_size_bytes*2)
426 data, sw = self._scc.update_record(EF_ISIM_ADF_map['IMPU'], 1, impu_tlv)
427 return sw
428
Supreeth Herlebe3b6412020-06-01 12:53:57 +0200429 def read_iari(self):
430 rec_cnt = self._scc.record_count(EF_ISIM_ADF_map['UICCIARI'])
431 uiari_recs = ""
432 for i in range(0, rec_cnt):
433 (res, sw) = self._scc.read_record(EF_ISIM_ADF_map['UICCIARI'], i + 1)
434 if sw == '9000':
435 # Skip the inital tag value ('80') byte and get length of contents
436 length = int(res[2:4], 16)
437 content = h2s(res[4:4+(length*2)])
438 uiari_recs += "\t%s\n" % (len(content) and content or 'Not available')
439 else:
440 uiari_recs += "UICC IARI: Can't read, response code = %s\n" % (sw)
441 return uiari_recs
Sylvain Munaut76504e02010-12-07 00:24:32 +0100442
443class _MagicSimBase(Card):
444 """
445 Theses cards uses several record based EFs to store the provider infos,
446 each possible provider uses a specific record number in each EF. The
447 indexes used are ( where N is the number of providers supported ) :
448 - [2 .. N+1] for the operator name
Supreeth Herle9ca41c12020-01-21 12:50:30 +0100449 - [1 .. N] for the programable EFs
Sylvain Munaut76504e02010-12-07 00:24:32 +0100450
451 * 3f00/7f4d/8f0c : Operator Name
452
453 bytes 0-15 : provider name, padded with 0xff
454 byte 16 : length of the provider name
455 byte 17 : 01 for valid records, 00 otherwise
456
457 * 3f00/7f4d/8f0d : Programmable Binary EFs
458
459 * 3f00/7f4d/8f0e : Programmable Record EFs
460
461 """
462
463 @classmethod
464 def autodetect(kls, scc):
465 try:
466 for p, l, t in kls._files.values():
467 if not t:
468 continue
469 if scc.record_size(['3f00', '7f4d', p]) != l:
470 return None
471 except:
472 return None
473
474 return kls(scc)
475
476 def _get_count(self):
477 """
478 Selects the file and returns the total number of entries
479 and entry size
480 """
481 f = self._files['name']
482
Harald Weltec0499c82021-01-21 16:06:50 +0100483 r = self._scc.select_path(['3f00', '7f4d', f[0]])
Sylvain Munaut76504e02010-12-07 00:24:32 +0100484 rec_len = int(r[-1][28:30], 16)
485 tlen = int(r[-1][4:8],16)
Daniel Willmann677d41b2020-10-19 10:34:31 +0200486 rec_cnt = (tlen / rec_len) - 1
Sylvain Munaut76504e02010-12-07 00:24:32 +0100487
488 if (rec_cnt < 1) or (rec_len != f[1]):
489 raise RuntimeError('Bad card type')
490
491 return rec_cnt
492
493 def program(self, p):
494 # Go to dir
Harald Weltec0499c82021-01-21 16:06:50 +0100495 self._scc.select_path(['3f00', '7f4d'])
Sylvain Munaut76504e02010-12-07 00:24:32 +0100496
497 # Home PLMN in PLMN_Sel format
Alexander Chemeris7be92ff2013-07-10 11:18:06 +0400498 hplmn = enc_plmn(p['mcc'], p['mnc'])
Sylvain Munaut76504e02010-12-07 00:24:32 +0100499
500 # Operator name ( 3f00/7f4d/8f0c )
501 self._scc.update_record(self._files['name'][0], 2,
502 rpad(b2h(p['name']), 32) + ('%02x' % len(p['name'])) + '01'
503 )
504
505 # ICCID/IMSI/Ki/HPLMN ( 3f00/7f4d/8f0d )
506 v = ''
507
508 # inline Ki
509 if self._ki_file is None:
510 v += p['ki']
511
512 # ICCID
Alexander Chemeris7be92ff2013-07-10 11:18:06 +0400513 v += '3f00' + '2fe2' + '0a' + enc_iccid(p['iccid'])
Sylvain Munaut76504e02010-12-07 00:24:32 +0100514
515 # IMSI
Alexander Chemeris7be92ff2013-07-10 11:18:06 +0400516 v += '7f20' + '6f07' + '09' + enc_imsi(p['imsi'])
Sylvain Munaut76504e02010-12-07 00:24:32 +0100517
518 # Ki
519 if self._ki_file:
520 v += self._ki_file + '10' + p['ki']
521
522 # PLMN_Sel
523 v+= '6f30' + '18' + rpad(hplmn, 36)
524
Alexander Chemeris21885242013-07-02 16:56:55 +0400525 # ACC
526 # This doesn't work with "fake" SuperSIM cards,
527 # but will hopefully work with real SuperSIMs.
528 if p.get('acc') is not None:
529 v+= '6f78' + '02' + lpad(p['acc'], 4)
530
Sylvain Munaut76504e02010-12-07 00:24:32 +0100531 self._scc.update_record(self._files['b_ef'][0], 1,
532 rpad(v, self._files['b_ef'][1]*2)
533 )
534
535 # SMSP ( 3f00/7f4d/8f0e )
536 # FIXME
537
538 # Write PLMN_Sel forcefully as well
Harald Weltec0499c82021-01-21 16:06:50 +0100539 r = self._scc.select_path(['3f00', '7f20', '6f30'])
Sylvain Munaut76504e02010-12-07 00:24:32 +0100540 tl = int(r[-1][4:8], 16)
541
Alexander Chemeris7be92ff2013-07-10 11:18:06 +0400542 hplmn = enc_plmn(p['mcc'], p['mnc'])
Sylvain Munaut76504e02010-12-07 00:24:32 +0100543 self._scc.update_binary('6f30', hplmn + 'ff' * (tl-3))
544
545 def erase(self):
546 # Dummy
547 df = {}
548 for k, v in self._files.iteritems():
549 ofs = 1
550 fv = v[1] * 'ff'
551 if k == 'name':
552 ofs = 2
553 fv = fv[0:-4] + '0000'
554 df[v[0]] = (fv, ofs)
555
556 # Write
557 for n in range(0,self._get_count()):
558 for k, (msg, ofs) in df.iteritems():
559 self._scc.update_record(['3f00', '7f4d', k], n + ofs, msg)
560
561
562class SuperSim(_MagicSimBase):
563
564 name = 'supersim'
565
566 _files = {
567 'name' : ('8f0c', 18, True),
568 'b_ef' : ('8f0d', 74, True),
569 'r_ef' : ('8f0e', 50, True),
570 }
571
572 _ki_file = None
573
574
575class MagicSim(_MagicSimBase):
576
577 name = 'magicsim'
578
579 _files = {
580 'name' : ('8f0c', 18, True),
581 'b_ef' : ('8f0d', 130, True),
582 'r_ef' : ('8f0e', 102, False),
583 }
584
585 _ki_file = '6f1b'
586
587
588class FakeMagicSim(Card):
589 """
590 Theses cards have a record based EF 3f00/000c that contains the provider
591 informations. See the program method for its format. The records go from
592 1 to N.
593 """
594
595 name = 'fakemagicsim'
596
597 @classmethod
598 def autodetect(kls, scc):
599 try:
600 if scc.record_size(['3f00', '000c']) != 0x5a:
601 return None
602 except:
603 return None
604
605 return kls(scc)
606
607 def _get_infos(self):
608 """
609 Selects the file and returns the total number of entries
610 and entry size
611 """
612
Harald Weltec0499c82021-01-21 16:06:50 +0100613 r = self._scc.select_path(['3f00', '000c'])
Sylvain Munaut76504e02010-12-07 00:24:32 +0100614 rec_len = int(r[-1][28:30], 16)
615 tlen = int(r[-1][4:8],16)
Daniel Willmann677d41b2020-10-19 10:34:31 +0200616 rec_cnt = (tlen / rec_len) - 1
Sylvain Munaut76504e02010-12-07 00:24:32 +0100617
618 if (rec_cnt < 1) or (rec_len != 0x5a):
619 raise RuntimeError('Bad card type')
620
621 return rec_cnt, rec_len
622
623 def program(self, p):
624 # Home PLMN
Harald Weltec0499c82021-01-21 16:06:50 +0100625 r = self._scc.select_path(['3f00', '7f20', '6f30'])
Sylvain Munaut76504e02010-12-07 00:24:32 +0100626 tl = int(r[-1][4:8], 16)
627
Alexander Chemeris7be92ff2013-07-10 11:18:06 +0400628 hplmn = enc_plmn(p['mcc'], p['mnc'])
Sylvain Munaut76504e02010-12-07 00:24:32 +0100629 self._scc.update_binary('6f30', hplmn + 'ff' * (tl-3))
630
631 # Get total number of entries and entry size
632 rec_cnt, rec_len = self._get_infos()
633
634 # Set first entry
635 entry = (
Philipp Maier45daa922019-04-01 15:49:45 +0200636 '81' + # 1b Status: Valid & Active
Harald Welte4f6ca432021-02-01 17:51:56 +0100637 rpad(s2h(p['name'][0:14]), 28) + # 14b Entry Name
Philipp Maier45daa922019-04-01 15:49:45 +0200638 enc_iccid(p['iccid']) + # 10b ICCID
639 enc_imsi(p['imsi']) + # 9b IMSI_len + id_type(9) + IMSI
640 p['ki'] + # 16b Ki
641 lpad(p['smsp'], 80) # 40b SMSP (padded with ff if needed)
Sylvain Munaut76504e02010-12-07 00:24:32 +0100642 )
643 self._scc.update_record('000c', 1, entry)
644
645 def erase(self):
646 # Get total number of entries and entry size
647 rec_cnt, rec_len = self._get_infos()
648
649 # Erase all entries
650 entry = 'ff' * rec_len
651 for i in range(0, rec_cnt):
652 self._scc.update_record('000c', 1+i, entry)
653
Sylvain Munaut5da8d4e2013-07-02 15:13:24 +0200654
Harald Welte3156d902011-03-22 21:48:19 +0100655class GrcardSim(Card):
656 """
657 Greencard (grcard.cn) HZCOS GSM SIM
658 These cards have a much more regular ISO 7816-4 / TS 11.11 structure,
659 and use standard UPDATE RECORD / UPDATE BINARY commands except for Ki.
660 """
661
662 name = 'grcardsim'
663
664 @classmethod
665 def autodetect(kls, scc):
666 return None
667
668 def program(self, p):
669 # We don't really know yet what ADM PIN 4 is about
670 #self._scc.verify_chv(4, h2b("4444444444444444"))
671
672 # Authenticate using ADM PIN 5
Jan Balkec3ebd332015-01-26 12:22:55 +0100673 if p['pin_adm']:
Philipp Maiera3de5a32018-08-23 10:27:04 +0200674 pin = h2b(p['pin_adm'])
Jan Balkec3ebd332015-01-26 12:22:55 +0100675 else:
676 pin = h2b("4444444444444444")
677 self._scc.verify_chv(5, pin)
Harald Welte3156d902011-03-22 21:48:19 +0100678
679 # EF.ICCID
Harald Weltec0499c82021-01-21 16:06:50 +0100680 r = self._scc.select_path(['3f00', '2fe2'])
Alexander Chemeris7be92ff2013-07-10 11:18:06 +0400681 data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid']))
Harald Welte3156d902011-03-22 21:48:19 +0100682
683 # EF.IMSI
Harald Weltec0499c82021-01-21 16:06:50 +0100684 r = self._scc.select_path(['3f00', '7f20', '6f07'])
Alexander Chemeris7be92ff2013-07-10 11:18:06 +0400685 data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi']))
Harald Welte3156d902011-03-22 21:48:19 +0100686
687 # EF.ACC
Alexander Chemeris21885242013-07-02 16:56:55 +0400688 if p.get('acc') is not None:
689 data, sw = self._scc.update_binary('6f78', lpad(p['acc'], 4))
Harald Welte3156d902011-03-22 21:48:19 +0100690
691 # EF.SMSP
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200692 if p.get('smsp'):
Harald Weltec0499c82021-01-21 16:06:50 +0100693 r = self._scc.select_path(['3f00', '7f10', '6f42'])
Harald Welte23888da2019-08-28 23:19:11 +0200694 data, sw = self._scc.update_record('6f42', 1, lpad(p['smsp'], 80))
Harald Welte3156d902011-03-22 21:48:19 +0100695
696 # Set the Ki using proprietary command
697 pdu = '80d4020010' + p['ki']
698 data, sw = self._scc._tp.send_apdu(pdu)
699
700 # EF.HPLMN
Harald Weltec0499c82021-01-21 16:06:50 +0100701 r = self._scc.select_path(['3f00', '7f20', '6f30'])
Harald Welte3156d902011-03-22 21:48:19 +0100702 size = int(r[-1][4:8], 16)
Alexander Chemeris7be92ff2013-07-10 11:18:06 +0400703 hplmn = enc_plmn(p['mcc'], p['mnc'])
Harald Welte3156d902011-03-22 21:48:19 +0100704 self._scc.update_binary('6f30', hplmn + 'ff' * (size-3))
705
706 # EF.SPN (Service Provider Name)
Harald Weltec0499c82021-01-21 16:06:50 +0100707 r = self._scc.select_path(['3f00', '7f20', '6f30'])
Harald Welte3156d902011-03-22 21:48:19 +0100708 size = int(r[-1][4:8], 16)
709 # FIXME
710
711 # FIXME: EF.MSISDN
712
Sylvain Munaut76504e02010-12-07 00:24:32 +0100713
Harald Weltee10394b2011-12-07 12:34:14 +0100714class SysmoSIMgr1(GrcardSim):
715 """
716 sysmocom sysmoSIM-GR1
717 These cards have a much more regular ISO 7816-4 / TS 11.11 structure,
718 and use standard UPDATE RECORD / UPDATE BINARY commands except for Ki.
719 """
720 name = 'sysmosim-gr1'
721
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200722 @classmethod
Philipp Maier087feff2018-08-23 09:41:36 +0200723 def autodetect(kls, scc):
724 try:
725 # Look for ATR
726 if scc.get_atr() == toBytes("3B 99 18 00 11 88 22 33 44 55 66 77 60"):
727 return kls(scc)
728 except:
729 return None
730 return None
Sylvain Munaut5da8d4e2013-07-02 15:13:24 +0200731
Harald Welteca673942020-06-03 15:19:40 +0200732class SysmoUSIMgr1(UsimCard):
Holger Hans Peter Freyther4d91bf42012-03-22 14:28:38 +0100733 """
734 sysmocom sysmoUSIM-GR1
735 """
736 name = 'sysmoUSIM-GR1'
737
738 @classmethod
739 def autodetect(kls, scc):
740 # TODO: Access the ATR
741 return None
742
743 def program(self, p):
744 # TODO: check if verify_chv could be used or what it needs
745 # self._scc.verify_chv(0x0A, [0x33,0x32,0x32,0x31,0x33,0x32,0x33,0x32])
746 # Unlock the card..
747 data, sw = self._scc._tp.send_apdu_checksw("0020000A083332323133323332")
748
749 # TODO: move into SimCardCommands
Holger Hans Peter Freyther4d91bf42012-03-22 14:28:38 +0100750 par = ( p['ki'] + # 16b K
Alexander Chemeris7be92ff2013-07-10 11:18:06 +0400751 p['opc'] + # 32b OPC
752 enc_iccid(p['iccid']) + # 10b ICCID
753 enc_imsi(p['imsi']) # 9b IMSI_len + id_type(9) + IMSI
Holger Hans Peter Freyther4d91bf42012-03-22 14:28:38 +0100754 )
755 data, sw = self._scc._tp.send_apdu_checksw("0099000033" + par)
756
Sylvain Munaut053c8952013-07-02 15:12:32 +0200757
Sylvain Munaut2fc205c2013-12-23 17:22:56 +0100758class SysmoSIMgr2(Card):
759 """
760 sysmocom sysmoSIM-GR2
761 """
762
763 name = 'sysmoSIM-GR2'
764
765 @classmethod
766 def autodetect(kls, scc):
Alexander Chemeris8ad124a2018-01-10 14:17:55 +0900767 try:
768 # Look for ATR
769 if scc.get_atr() == toBytes("3B 7D 94 00 00 55 55 53 0A 74 86 93 0B 24 7C 4D 54 68"):
770 return kls(scc)
771 except:
772 return None
Sylvain Munaut2fc205c2013-12-23 17:22:56 +0100773 return None
774
775 def program(self, p):
776
Daniel Willmann5d8cd9b2020-10-19 11:01:49 +0200777 # select MF
Harald Weltec0499c82021-01-21 16:06:50 +0100778 r = self._scc.select_path(['3f00'])
Daniel Willmann5d8cd9b2020-10-19 11:01:49 +0200779
Sylvain Munaut2fc205c2013-12-23 17:22:56 +0100780 # authenticate as SUPER ADM using default key
781 self._scc.verify_chv(0x0b, h2b("3838383838383838"))
782
783 # set ADM pin using proprietary command
784 # INS: D4
785 # P1: 3A for PIN, 3B for PUK
786 # P2: CHV number, as in VERIFY CHV for PIN, and as in UNBLOCK CHV for PUK
787 # P3: 08, CHV length (curiously the PUK is also 08 length, instead of 10)
Jan Balkec3ebd332015-01-26 12:22:55 +0100788 if p['pin_adm']:
Daniel Willmann7d38d742018-06-15 07:31:50 +0200789 pin = h2b(p['pin_adm'])
Jan Balkec3ebd332015-01-26 12:22:55 +0100790 else:
791 pin = h2b("4444444444444444")
792
793 pdu = 'A0D43A0508' + b2h(pin)
Sylvain Munaut2fc205c2013-12-23 17:22:56 +0100794 data, sw = self._scc._tp.send_apdu(pdu)
Daniel Willmann5d8cd9b2020-10-19 11:01:49 +0200795
Sylvain Munaut2fc205c2013-12-23 17:22:56 +0100796 # authenticate as ADM (enough to write file, and can set PINs)
Jan Balkec3ebd332015-01-26 12:22:55 +0100797
798 self._scc.verify_chv(0x05, pin)
Sylvain Munaut2fc205c2013-12-23 17:22:56 +0100799
800 # write EF.ICCID
801 data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid']))
802
803 # select DF_GSM
Harald Weltec0499c82021-01-21 16:06:50 +0100804 r = self._scc.select_path(['7f20'])
Daniel Willmann5d8cd9b2020-10-19 11:01:49 +0200805
Sylvain Munaut2fc205c2013-12-23 17:22:56 +0100806 # write EF.IMSI
807 data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi']))
808
809 # write EF.ACC
810 if p.get('acc') is not None:
811 data, sw = self._scc.update_binary('6f78', lpad(p['acc'], 4))
812
813 # get size and write EF.HPLMN
Harald Weltec0499c82021-01-21 16:06:50 +0100814 r = self._scc.select_path(['6f30'])
Sylvain Munaut2fc205c2013-12-23 17:22:56 +0100815 size = int(r[-1][4:8], 16)
816 hplmn = enc_plmn(p['mcc'], p['mnc'])
817 self._scc.update_binary('6f30', hplmn + 'ff' * (size-3))
818
819 # set COMP128 version 0 in proprietary file
820 data, sw = self._scc.update_binary('0001', '001000')
821
822 # set Ki in proprietary file
823 data, sw = self._scc.update_binary('0001', p['ki'], 3)
824
825 # select DF_TELECOM
Harald Weltec0499c82021-01-21 16:06:50 +0100826 r = self._scc.select_path(['3f00', '7f10'])
Daniel Willmann5d8cd9b2020-10-19 11:01:49 +0200827
Sylvain Munaut2fc205c2013-12-23 17:22:56 +0100828 # write EF.SMSP
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200829 if p.get('smsp'):
Harald Welte23888da2019-08-28 23:19:11 +0200830 data, sw = self._scc.update_record('6f42', 1, lpad(p['smsp'], 80))
Sylvain Munaut2fc205c2013-12-23 17:22:56 +0100831
Sylvain Munaut2fc205c2013-12-23 17:22:56 +0100832
Harald Welteca673942020-06-03 15:19:40 +0200833class SysmoUSIMSJS1(UsimCard):
Jan Balke3e840672015-01-26 15:36:27 +0100834 """
835 sysmocom sysmoUSIM-SJS1
836 """
837
838 name = 'sysmoUSIM-SJS1'
839
840 def __init__(self, ssc):
841 super(SysmoUSIMSJS1, self).__init__(ssc)
842 self._scc.cla_byte = "00"
Philipp Maier2d15ea02019-03-20 12:40:36 +0100843 self._scc.sel_ctrl = "0004" #request an FCP
Jan Balke3e840672015-01-26 15:36:27 +0100844
845 @classmethod
846 def autodetect(kls, scc):
Alexander Chemeris8ad124a2018-01-10 14:17:55 +0900847 try:
848 # Look for ATR
849 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"):
850 return kls(scc)
851 except:
852 return None
Jan Balke3e840672015-01-26 15:36:27 +0100853 return None
854
Harald Weltea6704252021-01-08 20:19:11 +0100855 def verify_adm(self, key):
Philipp Maiere9604882017-03-21 17:24:31 +0100856 # authenticate as ADM using default key (written on the card..)
Harald Weltea6704252021-01-08 20:19:11 +0100857 if not key:
Philipp Maiere9604882017-03-21 17:24:31 +0100858 raise ValueError("Please provide a PIN-ADM as there is no default one")
Harald Weltea6704252021-01-08 20:19:11 +0100859 (res, sw) = self._scc.verify_chv(0x0A, key)
Harald Weltea6704252021-01-08 20:19:11 +0100860 return sw
861
862 def program(self, p):
863 self.verify_adm(h2b(p['pin_adm']))
Jan Balke3e840672015-01-26 15:36:27 +0100864
865 # select MF
Harald Weltec0499c82021-01-21 16:06:50 +0100866 r = self._scc.select_path(['3f00'])
Jan Balke3e840672015-01-26 15:36:27 +0100867
Philipp Maiere9604882017-03-21 17:24:31 +0100868 # write EF.ICCID
869 data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid']))
870
Jan Balke3e840672015-01-26 15:36:27 +0100871 # select DF_GSM
Harald Weltec0499c82021-01-21 16:06:50 +0100872 r = self._scc.select_path(['7f20'])
Jan Balke3e840672015-01-26 15:36:27 +0100873
Jan Balke3e840672015-01-26 15:36:27 +0100874 # set Ki in proprietary file
875 data, sw = self._scc.update_binary('00FF', p['ki'])
876
Philipp Maier1be35bf2018-07-13 11:29:03 +0200877 # set OPc in proprietary file
Daniel Willmann67acdbc2018-06-15 07:42:48 +0200878 if 'opc' in p:
879 content = "01" + p['opc']
880 data, sw = self._scc.update_binary('00F7', content)
Jan Balke3e840672015-01-26 15:36:27 +0100881
Supreeth Herle7947d922019-06-08 07:50:53 +0200882 # set Service Provider Name
Supreeth Herle840a9e22020-01-21 13:32:46 +0100883 if p.get('name') is not None:
884 content = enc_spn(p['name'], True, True)
885 data, sw = self._scc.update_binary('6F46', rpad(content, 32))
Supreeth Herle7947d922019-06-08 07:50:53 +0200886
Supreeth Herlec8796a32019-12-23 12:23:42 +0100887 if p.get('acc') is not None:
888 self.update_acc(p['acc'])
889
Jan Balke3e840672015-01-26 15:36:27 +0100890 # write EF.IMSI
891 data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi']))
892
Philipp Maier2d15ea02019-03-20 12:40:36 +0100893 # EF.PLMNsel
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200894 if p.get('mcc') and p.get('mnc'):
895 sw = self.update_plmnsel(p['mcc'], p['mnc'])
896 if sw != '9000':
Philipp Maier2d15ea02019-03-20 12:40:36 +0100897 print("Programming PLMNsel failed with code %s"%sw)
898
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200899 # EF.PLMNwAcT
900 if p.get('mcc') and p.get('mnc'):
Philipp Maier2d15ea02019-03-20 12:40:36 +0100901 sw = self.update_plmn_act(p['mcc'], p['mnc'])
902 if sw != '9000':
903 print("Programming PLMNwAcT failed with code %s"%sw)
904
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200905 # EF.OPLMNwAcT
906 if p.get('mcc') and p.get('mnc'):
Philipp Maier2d15ea02019-03-20 12:40:36 +0100907 sw = self.update_oplmn_act(p['mcc'], p['mnc'])
908 if sw != '9000':
909 print("Programming OPLMNwAcT failed with code %s"%sw)
910
Supreeth Herlef442fb42020-01-21 12:47:32 +0100911 # EF.HPLMNwAcT
912 if p.get('mcc') and p.get('mnc'):
913 sw = self.update_hplmn_act(p['mcc'], p['mnc'])
914 if sw != '9000':
915 print("Programming HPLMNwAcT failed with code %s"%sw)
916
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200917 # EF.AD
918 if p.get('mcc') and p.get('mnc'):
Philipp Maieree908ae2019-03-21 16:21:12 +0100919 sw = self.update_ad(p['mnc'])
920 if sw != '9000':
921 print("Programming AD failed with code %s"%sw)
Philipp Maier2d15ea02019-03-20 12:40:36 +0100922
Daniel Willmann1d087ef2017-08-31 10:08:45 +0200923 # EF.SMSP
Harald Welte23888da2019-08-28 23:19:11 +0200924 if p.get('smsp'):
Harald Weltec0499c82021-01-21 16:06:50 +0100925 r = self._scc.select_path(['3f00', '7f10'])
Harald Welte23888da2019-08-28 23:19:11 +0200926 data, sw = self._scc.update_record('6f42', 1, lpad(p['smsp'], 104), force_len=True)
Jan Balke3e840672015-01-26 15:36:27 +0100927
Supreeth Herle5a541012019-12-22 08:59:16 +0100928 # EF.MSISDN
929 # TODO: Alpha Identifier (currently 'ff'O * 20)
930 # TODO: Capability/Configuration1 Record Identifier
931 # TODO: Extension1 Record Identifier
932 if p.get('msisdn') is not None:
933 msisdn = enc_msisdn(p['msisdn'])
934 data = 'ff' * 20 + msisdn + 'ff' * 2
935
Harald Weltec0499c82021-01-21 16:06:50 +0100936 r = self._scc.select_path(['3f00', '7f10'])
Supreeth Herle5a541012019-12-22 08:59:16 +0100937 data, sw = self._scc.update_record('6F40', 1, data, force_len=True)
938
Alexander Chemerise0d9d882018-01-10 14:18:32 +0900939
herlesupreeth4a3580b2020-09-29 10:11:36 +0200940class FairwavesSIM(UsimCard):
Alexander Chemerise0d9d882018-01-10 14:18:32 +0900941 """
942 FairwavesSIM
943
944 The SIM card is operating according to the standard.
945 For Ki/OP/OPC programming the following files are additionally open for writing:
946 3F00/7F20/FF01 – OP/OPC:
947 byte 1 = 0x01, bytes 2-17: OPC;
948 byte 1 = 0x00, bytes 2-17: OP;
949 3F00/7F20/FF02: Ki
950 """
951
Philipp Maier5a876312019-11-11 11:01:46 +0100952 name = 'Fairwaves-SIM'
Alexander Chemerise0d9d882018-01-10 14:18:32 +0900953 # Propriatary files
954 _EF_num = {
955 'Ki': 'FF02',
956 'OP/OPC': 'FF01',
957 }
958 _EF = {
959 'Ki': DF['GSM']+[_EF_num['Ki']],
960 'OP/OPC': DF['GSM']+[_EF_num['OP/OPC']],
961 }
962
963 def __init__(self, ssc):
964 super(FairwavesSIM, self).__init__(ssc)
965 self._adm_chv_num = 0x11
966 self._adm2_chv_num = 0x12
967
968
969 @classmethod
970 def autodetect(kls, scc):
971 try:
972 # Look for ATR
973 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"):
974 return kls(scc)
975 except:
976 return None
977 return None
978
979
980 def verify_adm2(self, key):
981 '''
982 Authenticate with ADM2 key.
983
984 Fairwaves SIM cards support hierarchical key structure and ADM2 key
985 is a key which has access to proprietary files (Ki and OP/OPC).
986 That said, ADM key inherits permissions of ADM2 key and thus we rarely
987 need ADM2 key per se.
988 '''
989 (res, sw) = self._scc.verify_chv(self._adm2_chv_num, key)
990 return sw
991
992
993 def read_ki(self):
994 """
995 Read Ki in proprietary file.
996
997 Requires ADM1 access level
998 """
999 return self._scc.read_binary(self._EF['Ki'])
1000
1001
1002 def update_ki(self, ki):
1003 """
1004 Set Ki in proprietary file.
1005
1006 Requires ADM1 access level
1007 """
1008 data, sw = self._scc.update_binary(self._EF['Ki'], ki)
1009 return sw
1010
1011
1012 def read_op_opc(self):
1013 """
1014 Read Ki in proprietary file.
1015
1016 Requires ADM1 access level
1017 """
1018 (ef, sw) = self._scc.read_binary(self._EF['OP/OPC'])
1019 type = 'OP' if ef[0:2] == '00' else 'OPC'
1020 return ((type, ef[2:]), sw)
1021
1022
1023 def update_op(self, op):
1024 """
1025 Set OP in proprietary file.
1026
1027 Requires ADM1 access level
1028 """
1029 content = '00' + op
1030 data, sw = self._scc.update_binary(self._EF['OP/OPC'], content)
1031 return sw
1032
1033
1034 def update_opc(self, opc):
1035 """
1036 Set OPC in proprietary file.
1037
1038 Requires ADM1 access level
1039 """
1040 content = '01' + opc
1041 data, sw = self._scc.update_binary(self._EF['OP/OPC'], content)
1042 return sw
1043
1044
1045 def program(self, p):
1046 # authenticate as ADM1
1047 if not p['pin_adm']:
1048 raise ValueError("Please provide a PIN-ADM as there is no default one")
Philipp Maier05f42ee2021-03-11 13:59:44 +01001049 self.verify_adm(h2b(p['pin_adm']))
Alexander Chemerise0d9d882018-01-10 14:18:32 +09001050
1051 # TODO: Set operator name
1052 if p.get('smsp') is not None:
1053 sw = self.update_smsp(p['smsp'])
1054 if sw != '9000':
1055 print("Programming SMSP failed with code %s"%sw)
1056 # This SIM doesn't support changing ICCID
1057 if p.get('mcc') is not None and p.get('mnc') is not None:
1058 sw = self.update_hplmn_act(p['mcc'], p['mnc'])
1059 if sw != '9000':
1060 print("Programming MCC/MNC failed with code %s"%sw)
1061 if p.get('imsi') is not None:
1062 sw = self.update_imsi(p['imsi'])
1063 if sw != '9000':
1064 print("Programming IMSI failed with code %s"%sw)
1065 if p.get('ki') is not None:
1066 sw = self.update_ki(p['ki'])
1067 if sw != '9000':
1068 print("Programming Ki failed with code %s"%sw)
1069 if p.get('opc') is not None:
1070 sw = self.update_opc(p['opc'])
1071 if sw != '9000':
1072 print("Programming OPC failed with code %s"%sw)
1073 if p.get('acc') is not None:
1074 sw = self.update_acc(p['acc'])
1075 if sw != '9000':
1076 print("Programming ACC failed with code %s"%sw)
Jan Balke3e840672015-01-26 15:36:27 +01001077
Todd Neal9eeadfc2018-04-25 15:36:29 -05001078class OpenCellsSim(Card):
1079 """
1080 OpenCellsSim
1081
1082 """
1083
Philipp Maier5a876312019-11-11 11:01:46 +01001084 name = 'OpenCells-SIM'
Todd Neal9eeadfc2018-04-25 15:36:29 -05001085
1086 def __init__(self, ssc):
1087 super(OpenCellsSim, self).__init__(ssc)
1088 self._adm_chv_num = 0x0A
1089
1090
1091 @classmethod
1092 def autodetect(kls, scc):
1093 try:
1094 # Look for ATR
1095 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"):
1096 return kls(scc)
1097 except:
1098 return None
1099 return None
1100
1101
1102 def program(self, p):
1103 if not p['pin_adm']:
1104 raise ValueError("Please provide a PIN-ADM as there is no default one")
1105 self._scc.verify_chv(0x0A, h2b(p['pin_adm']))
1106
1107 # select MF
Harald Weltec0499c82021-01-21 16:06:50 +01001108 r = self._scc.select_path(['3f00'])
Todd Neal9eeadfc2018-04-25 15:36:29 -05001109
1110 # write EF.ICCID
1111 data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid']))
1112
Harald Weltec0499c82021-01-21 16:06:50 +01001113 r = self._scc.select_path(['7ff0'])
Todd Neal9eeadfc2018-04-25 15:36:29 -05001114
1115 # set Ki in proprietary file
1116 data, sw = self._scc.update_binary('FF02', p['ki'])
1117
1118 # set OPC in proprietary file
1119 data, sw = self._scc.update_binary('FF01', p['opc'])
1120
1121 # select DF_GSM
Harald Weltec0499c82021-01-21 16:06:50 +01001122 r = self._scc.select_path(['7f20'])
Todd Neal9eeadfc2018-04-25 15:36:29 -05001123
1124 # write EF.IMSI
1125 data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi']))
1126
herlesupreeth4a3580b2020-09-29 10:11:36 +02001127class WavemobileSim(UsimCard):
Philipp Maierc8ce82a2018-07-04 17:57:20 +02001128 """
1129 WavemobileSim
1130
1131 """
1132
1133 name = 'Wavemobile-SIM'
1134
1135 def __init__(self, ssc):
1136 super(WavemobileSim, self).__init__(ssc)
1137 self._adm_chv_num = 0x0A
1138 self._scc.cla_byte = "00"
1139 self._scc.sel_ctrl = "0004" #request an FCP
1140
1141 @classmethod
1142 def autodetect(kls, scc):
1143 try:
1144 # Look for ATR
1145 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"):
1146 return kls(scc)
1147 except:
1148 return None
1149 return None
1150
1151 def program(self, p):
1152 if not p['pin_adm']:
1153 raise ValueError("Please provide a PIN-ADM as there is no default one")
Philipp Maier05f42ee2021-03-11 13:59:44 +01001154 self.verify_adm(h2b(p['pin_adm']))
Philipp Maierc8ce82a2018-07-04 17:57:20 +02001155
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +02001156 # EF.ICCID
1157 # TODO: Add programming of the ICCID
1158 if p.get('iccid'):
Philipp Maierc8ce82a2018-07-04 17:57:20 +02001159 print("Warning: Programming of the ICCID is not implemented for this type of card.")
1160
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +02001161 # KI (Presumably a propritary file)
1162 # TODO: Add programming of KI
1163 if p.get('ki'):
Philipp Maierc8ce82a2018-07-04 17:57:20 +02001164 print("Warning: Programming of the KI is not implemented for this type of card.")
1165
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +02001166 # OPc (Presumably a propritary file)
1167 # TODO: Add programming of OPc
1168 if p.get('opc'):
Philipp Maierc8ce82a2018-07-04 17:57:20 +02001169 print("Warning: Programming of the OPc is not implemented for this type of card.")
1170
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +02001171 # EF.SMSP
Philipp Maierc8ce82a2018-07-04 17:57:20 +02001172 if p.get('smsp'):
1173 sw = self.update_smsp(p['smsp'])
1174 if sw != '9000':
1175 print("Programming SMSP failed with code %s"%sw)
1176
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +02001177 # EF.IMSI
Philipp Maierc8ce82a2018-07-04 17:57:20 +02001178 if p.get('imsi'):
1179 sw = self.update_imsi(p['imsi'])
1180 if sw != '9000':
1181 print("Programming IMSI failed with code %s"%sw)
1182
1183 # EF.ACC
1184 if p.get('acc'):
1185 sw = self.update_acc(p['acc'])
1186 if sw != '9000':
1187 print("Programming ACC failed with code %s"%sw)
1188
1189 # EF.PLMNsel
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +02001190 if p.get('mcc') and p.get('mnc'):
1191 sw = self.update_plmnsel(p['mcc'], p['mnc'])
1192 if sw != '9000':
Philipp Maierc8ce82a2018-07-04 17:57:20 +02001193 print("Programming PLMNsel failed with code %s"%sw)
1194
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +02001195 # EF.PLMNwAcT
1196 if p.get('mcc') and p.get('mnc'):
Philipp Maierc8ce82a2018-07-04 17:57:20 +02001197 sw = self.update_plmn_act(p['mcc'], p['mnc'])
1198 if sw != '9000':
1199 print("Programming PLMNwAcT failed with code %s"%sw)
1200
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +02001201 # EF.OPLMNwAcT
1202 if p.get('mcc') and p.get('mnc'):
Philipp Maierc8ce82a2018-07-04 17:57:20 +02001203 sw = self.update_oplmn_act(p['mcc'], p['mnc'])
1204 if sw != '9000':
1205 print("Programming OPLMNwAcT failed with code %s"%sw)
1206
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +02001207 # EF.AD
1208 if p.get('mcc') and p.get('mnc'):
Philipp Maier6e507a72019-04-01 16:33:48 +02001209 sw = self.update_ad(p['mnc'])
1210 if sw != '9000':
1211 print("Programming AD failed with code %s"%sw)
1212
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +02001213 return None
Philipp Maierc8ce82a2018-07-04 17:57:20 +02001214
Todd Neal9eeadfc2018-04-25 15:36:29 -05001215
herlesupreethb0c7d122020-12-23 09:25:46 +01001216class SysmoISIMSJA2(UsimCard, IsimCard):
Philipp Maier0ad5bcf2019-12-31 17:55:47 +01001217 """
1218 sysmocom sysmoISIM-SJA2
1219 """
1220
1221 name = 'sysmoISIM-SJA2'
1222
1223 def __init__(self, ssc):
1224 super(SysmoISIMSJA2, self).__init__(ssc)
1225 self._scc.cla_byte = "00"
1226 self._scc.sel_ctrl = "0004" #request an FCP
1227
1228 @classmethod
1229 def autodetect(kls, scc):
1230 try:
1231 # Try card model #1
1232 atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 75 30 34 05 4B A9"
1233 if scc.get_atr() == toBytes(atr):
1234 return kls(scc)
1235
1236 # Try card model #2
1237 atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 75 31 33 02 51 B2"
1238 if scc.get_atr() == toBytes(atr):
1239 return kls(scc)
Philipp Maierb3e11ea2020-03-11 12:32:44 +01001240
1241 # Try card model #3
1242 atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 52 75 31 04 51 D5"
1243 if scc.get_atr() == toBytes(atr):
1244 return kls(scc)
Philipp Maier0ad5bcf2019-12-31 17:55:47 +01001245 except:
1246 return None
1247 return None
1248
Harald Weltea6704252021-01-08 20:19:11 +01001249 def verify_adm(self, key):
Philipp Maier0ad5bcf2019-12-31 17:55:47 +01001250 # authenticate as ADM using default key (written on the card..)
Harald Weltea6704252021-01-08 20:19:11 +01001251 if not key:
Philipp Maier0ad5bcf2019-12-31 17:55:47 +01001252 raise ValueError("Please provide a PIN-ADM as there is no default one")
Harald Weltea6704252021-01-08 20:19:11 +01001253 (res, sw) = self._scc.verify_chv(0x0A, key)
Harald Weltea6704252021-01-08 20:19:11 +01001254 return sw
1255
1256 def program(self, p):
1257 self.verify_adm(h2b(p['pin_adm']))
Philipp Maier0ad5bcf2019-12-31 17:55:47 +01001258
1259 # This type of card does not allow to reprogram the ICCID.
1260 # Reprogramming the ICCID would mess up the card os software
1261 # license management, so the ICCID must be kept at its factory
1262 # setting!
1263 if p.get('iccid'):
1264 print("Warning: Programming of the ICCID is not implemented for this type of card.")
1265
1266 # select DF_GSM
Harald Weltec0499c82021-01-21 16:06:50 +01001267 self._scc.select_path(['7f20'])
Philipp Maier0ad5bcf2019-12-31 17:55:47 +01001268
1269 # write EF.IMSI
1270 if p.get('imsi'):
1271 self._scc.update_binary('6f07', enc_imsi(p['imsi']))
1272
1273 # EF.PLMNsel
1274 if p.get('mcc') and p.get('mnc'):
1275 sw = self.update_plmnsel(p['mcc'], p['mnc'])
1276 if sw != '9000':
1277 print("Programming PLMNsel failed with code %s"%sw)
1278
1279 # EF.PLMNwAcT
1280 if p.get('mcc') and p.get('mnc'):
1281 sw = self.update_plmn_act(p['mcc'], p['mnc'])
1282 if sw != '9000':
1283 print("Programming PLMNwAcT failed with code %s"%sw)
1284
1285 # EF.OPLMNwAcT
1286 if p.get('mcc') and p.get('mnc'):
1287 sw = self.update_oplmn_act(p['mcc'], p['mnc'])
1288 if sw != '9000':
1289 print("Programming OPLMNwAcT failed with code %s"%sw)
1290
Harald Welte32f0d412020-05-05 17:35:57 +02001291 # EF.HPLMNwAcT
1292 if p.get('mcc') and p.get('mnc'):
1293 sw = self.update_hplmn_act(p['mcc'], p['mnc'])
1294 if sw != '9000':
1295 print("Programming HPLMNwAcT failed with code %s"%sw)
1296
Philipp Maier0ad5bcf2019-12-31 17:55:47 +01001297 # EF.AD
1298 if p.get('mcc') and p.get('mnc'):
1299 sw = self.update_ad(p['mnc'])
1300 if sw != '9000':
1301 print("Programming AD failed with code %s"%sw)
1302
1303 # EF.SMSP
1304 if p.get('smsp'):
Harald Weltec0499c82021-01-21 16:06:50 +01001305 r = self._scc.select_path(['3f00', '7f10'])
Philipp Maier0ad5bcf2019-12-31 17:55:47 +01001306 data, sw = self._scc.update_record('6f42', 1, lpad(p['smsp'], 104), force_len=True)
1307
Supreeth Herlec6019232020-03-26 10:00:45 +01001308 # EF.MSISDN
1309 # TODO: Alpha Identifier (currently 'ff'O * 20)
1310 # TODO: Capability/Configuration1 Record Identifier
1311 # TODO: Extension1 Record Identifier
1312 if p.get('msisdn') is not None:
1313 msisdn = enc_msisdn(p['msisdn'])
1314 content = 'ff' * 20 + msisdn + 'ff' * 2
1315
Harald Weltec0499c82021-01-21 16:06:50 +01001316 r = self._scc.select_path(['3f00', '7f10'])
Supreeth Herlec6019232020-03-26 10:00:45 +01001317 data, sw = self._scc.update_record('6F40', 1, content, force_len=True)
1318
Supreeth Herlea97944b2020-03-26 10:03:25 +01001319 # EF.ACC
1320 if p.get('acc'):
1321 sw = self.update_acc(p['acc'])
1322 if sw != '9000':
1323 print("Programming ACC failed with code %s"%sw)
1324
Supreeth Herle80164052020-03-23 12:06:29 +01001325 # Populate AIDs
1326 self.read_aids()
1327
Philipp Maier0ad5bcf2019-12-31 17:55:47 +01001328 # update EF-SIM_AUTH_KEY (and EF-USIM_AUTH_KEY_2G, which is
1329 # hard linked to EF-USIM_AUTH_KEY)
Harald Weltec0499c82021-01-21 16:06:50 +01001330 self._scc.select_path(['3f00'])
1331 self._scc.select_path(['a515'])
Philipp Maier0ad5bcf2019-12-31 17:55:47 +01001332 if p.get('ki'):
1333 self._scc.update_binary('6f20', p['ki'], 1)
1334 if p.get('opc'):
1335 self._scc.update_binary('6f20', p['opc'], 17)
1336
1337 # update EF-USIM_AUTH_KEY in ADF.ISIM
Philipp Maiercba6dbc2021-03-11 13:03:18 +01001338 data, sw = self.select_adf_by_aid(adf="isim")
1339 if sw == '9000':
Philipp Maierd9507862020-03-11 12:18:29 +01001340 if p.get('ki'):
1341 self._scc.update_binary('af20', p['ki'], 1)
1342 if p.get('opc'):
1343 self._scc.update_binary('af20', p['opc'], 17)
Philipp Maier0ad5bcf2019-12-31 17:55:47 +01001344
Supreeth Herlecf727f22020-03-24 17:32:21 +01001345 # update EF.P-CSCF in ADF.ISIM
1346 if self.file_exists(EF_ISIM_ADF_map['PCSCF']):
1347 if p.get('pcscf'):
1348 sw = self.update_pcscf(p['pcscf'])
1349 else:
1350 sw = self.update_pcscf("")
1351 if sw != '9000':
1352 print("Programming P-CSCF failed with code %s"%sw)
1353
1354
Supreeth Herle79f43dd2020-03-25 11:43:19 +01001355 # update EF.DOMAIN in ADF.ISIM
1356 if self.file_exists(EF_ISIM_ADF_map['DOMAIN']):
1357 if p.get('ims_hdomain'):
1358 sw = self.update_domain(domain=p['ims_hdomain'])
1359 else:
1360 sw = self.update_domain()
1361
1362 if sw != '9000':
1363 print("Programming Home Network Domain Name failed with code %s"%sw)
1364
Supreeth Herlea5bd9682020-03-26 09:16:14 +01001365 # update EF.IMPI in ADF.ISIM
1366 # TODO: Validate IMPI input
1367 if self.file_exists(EF_ISIM_ADF_map['IMPI']):
1368 if p.get('impi'):
1369 sw = self.update_impi(p['impi'])
1370 else:
1371 sw = self.update_impi()
1372 if sw != '9000':
1373 print("Programming IMPI failed with code %s"%sw)
1374
Supreeth Herlebe7007e2020-03-26 09:27:45 +01001375 # update EF.IMPU in ADF.ISIM
1376 # TODO: Validate IMPU input
1377 # Support multiple IMPU if there is enough space
1378 if self.file_exists(EF_ISIM_ADF_map['IMPU']):
1379 if p.get('impu'):
1380 sw = self.update_impu(p['impu'])
1381 else:
1382 sw = self.update_impu()
1383 if sw != '9000':
1384 print("Programming IMPU failed with code %s"%sw)
1385
Philipp Maiercba6dbc2021-03-11 13:03:18 +01001386 data, sw = self.select_adf_by_aid(adf="usim")
1387 if sw == '9000':
Harald Welteca673942020-06-03 15:19:40 +02001388 # update EF-USIM_AUTH_KEY in ADF.USIM
Philipp Maierd9507862020-03-11 12:18:29 +01001389 if p.get('ki'):
1390 self._scc.update_binary('af20', p['ki'], 1)
1391 if p.get('opc'):
1392 self._scc.update_binary('af20', p['opc'], 17)
Philipp Maier0ad5bcf2019-12-31 17:55:47 +01001393
Harald Welteca673942020-06-03 15:19:40 +02001394 # update EF.EHPLMN in ADF.USIM
Harald Welte1e424202020-08-31 15:04:19 +02001395 if self.file_exists(EF_USIM_ADF_map['EHPLMN']):
Harald Welteca673942020-06-03 15:19:40 +02001396 if p.get('mcc') and p.get('mnc'):
1397 sw = self.update_ehplmn(p['mcc'], p['mnc'])
1398 if sw != '9000':
1399 print("Programming EHPLMN failed with code %s"%sw)
Supreeth Herle8e0fccd2020-03-23 12:10:56 +01001400
1401 # update EF.ePDGId in ADF.USIM
1402 if self.file_exists(EF_USIM_ADF_map['ePDGId']):
1403 if p.get('epdgid'):
herlesupreeth5d0a30c2020-09-29 09:44:24 +02001404 sw = self.update_epdgid(p['epdgid'])
Supreeth Herle47790342020-03-25 12:51:38 +01001405 else:
1406 sw = self.update_epdgid("")
1407 if sw != '9000':
1408 print("Programming ePDGId failed with code %s"%sw)
Supreeth Herle8e0fccd2020-03-23 12:10:56 +01001409
Supreeth Herlef964df42020-03-24 13:15:37 +01001410 # update EF.ePDGSelection in ADF.USIM
1411 if self.file_exists(EF_USIM_ADF_map['ePDGSelection']):
1412 if p.get('epdgSelection'):
1413 epdg_plmn = p['epdgSelection']
1414 sw = self.update_ePDGSelection(epdg_plmn[:3], epdg_plmn[3:])
1415 else:
1416 sw = self.update_ePDGSelection("", "")
1417 if sw != '9000':
1418 print("Programming ePDGSelection failed with code %s"%sw)
1419
1420
Supreeth Herleacc222f2020-03-24 13:26:53 +01001421 # After successfully programming EF.ePDGId and EF.ePDGSelection,
1422 # Set service 106 and 107 as available in EF.UST
Supreeth Herle44e04622020-03-25 10:34:28 +01001423 # Disable service 95, 99, 115 if ISIM application is present
Supreeth Herleacc222f2020-03-24 13:26:53 +01001424 if self.file_exists(EF_USIM_ADF_map['UST']):
1425 if p.get('epdgSelection') and p.get('epdgid'):
1426 sw = self.update_ust(106, 1)
1427 if sw != '9000':
1428 print("Programming UST failed with code %s"%sw)
1429 sw = self.update_ust(107, 1)
1430 if sw != '9000':
1431 print("Programming UST failed with code %s"%sw)
1432
Supreeth Herle44e04622020-03-25 10:34:28 +01001433 sw = self.update_ust(95, 0)
1434 if sw != '9000':
1435 print("Programming UST failed with code %s"%sw)
1436 sw = self.update_ust(99, 0)
1437 if sw != '9000':
1438 print("Programming UST failed with code %s"%sw)
1439 sw = self.update_ust(115, 0)
1440 if sw != '9000':
1441 print("Programming UST failed with code %s"%sw)
1442
Philipp Maier0ad5bcf2019-12-31 17:55:47 +01001443 return
1444
Philipp Maier0ad5bcf2019-12-31 17:55:47 +01001445
Todd Neal9eeadfc2018-04-25 15:36:29 -05001446# In order for autodetection ...
Harald Weltee10394b2011-12-07 12:34:14 +01001447_cards_classes = [ FakeMagicSim, SuperSim, MagicSim, GrcardSim,
Alexander Chemerise0d9d882018-01-10 14:18:32 +09001448 SysmoSIMgr1, SysmoSIMgr2, SysmoUSIMgr1, SysmoUSIMSJS1,
Philipp Maier0ad5bcf2019-12-31 17:55:47 +01001449 FairwavesSIM, OpenCellsSim, WavemobileSim, SysmoISIMSJA2 ]
Alexander Chemeris8ad124a2018-01-10 14:17:55 +09001450
1451def card_autodetect(scc):
1452 for kls in _cards_classes:
1453 card = kls.autodetect(scc)
1454 if card is not None:
1455 card.reset()
1456 return card
1457 return None
Supreeth Herle4c306ab2020-03-18 11:38:00 +01001458
1459def card_detect(ctype, scc):
1460 # Detect type if needed
1461 card = None
1462 ctypes = dict([(kls.name, kls) for kls in _cards_classes])
1463
1464 if ctype in ("auto", "auto_once"):
1465 for kls in _cards_classes:
1466 card = kls.autodetect(scc)
1467 if card:
1468 print("Autodetected card type: %s" % card.name)
1469 card.reset()
1470 break
1471
1472 if card is None:
1473 print("Autodetection failed")
1474 return None
1475
1476 if ctype == "auto_once":
1477 ctype = card.name
1478
1479 elif ctype in ctypes:
1480 card = ctypes[ctype](scc)
1481
1482 else:
1483 raise ValueError("Unknown card type: %s" % ctype)
1484
1485 return card