blob: 61a370746362c51516bf24892ab4342a5cb1ab5f [file] [log] [blame]
Sylvain Munaut76504e02010-12-07 00:24:32 +01001#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4""" pySim: Card programmation logic
5"""
6
7#
8# Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com>
Harald Welte3156d902011-03-22 21:48:19 +01009# Copyright (C) 2011 Harald Welte <laforge@gnumonks.org>
Alexander Chemeriseb6807d2017-07-18 17:04:38 +030010# Copyright (C) 2017 Alexander.Chemeris <Alexander.Chemeris@gmail.com>
Sylvain Munaut76504e02010-12-07 00:24:32 +010011#
12# This program is free software: you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation, either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program. If not, see <http://www.gnu.org/licenses/>.
24#
25
Alexander Chemeriseb6807d2017-07-18 17:04:38 +030026from pySim.ts_51_011 import EF, DF
27from pySim.utils import *
Alexander Chemeris8ad124a2018-01-10 14:17:55 +090028from smartcard.util import toBytes
Sylvain Munaut76504e02010-12-07 00:24:32 +010029
30class Card(object):
31
32 def __init__(self, scc):
33 self._scc = scc
Alexander Chemeriseb6807d2017-07-18 17:04:38 +030034 self._adm_chv_num = 4
Supreeth Herlee4e98312020-03-18 11:33:14 +010035 self._aids = []
Sylvain Munaut76504e02010-12-07 00:24:32 +010036
Sylvain Munaut76504e02010-12-07 00:24:32 +010037 def reset(self):
38 self._scc.reset_card()
39
Alexander Chemeriseb6807d2017-07-18 17:04:38 +030040 def verify_adm(self, key):
41 '''
42 Authenticate with ADM key
43 '''
44 (res, sw) = self._scc.verify_chv(self._adm_chv_num, key)
45 return sw
46
47 def read_iccid(self):
48 (res, sw) = self._scc.read_binary(EF['ICCID'])
49 if sw == '9000':
50 return (dec_iccid(res), sw)
51 else:
52 return (None, sw)
53
54 def read_imsi(self):
55 (res, sw) = self._scc.read_binary(EF['IMSI'])
56 if sw == '9000':
57 return (dec_imsi(res), sw)
58 else:
59 return (None, sw)
60
61 def update_imsi(self, imsi):
62 data, sw = self._scc.update_binary(EF['IMSI'], enc_imsi(imsi))
63 return sw
64
65 def update_acc(self, acc):
66 data, sw = self._scc.update_binary(EF['ACC'], lpad(acc, 4))
67 return sw
68
69 def update_hplmn_act(self, mcc, mnc, access_tech='FFFF'):
70 """
71 Update Home PLMN with access technology bit-field
72
73 See Section "10.3.37 EFHPLMNwAcT (HPLMN Selector with Access Technology)"
74 in ETSI TS 151 011 for the details of the access_tech field coding.
75 Some common values:
76 access_tech = '0080' # Only GSM is selected
77 access_tech = 'FFFF' # All technologues selected, even Reserved for Future Use ones
78 """
79 # get size and write EF.HPLMNwAcT
Supreeth Herle2d785972019-11-30 11:00:10 +010080 data = self._scc.read_binary(EF['HPLMNwAcT'], length=None, offset=0)
Vadim Yanitskiy9664b2e2020-02-27 01:49:51 +070081 size = len(data[0]) // 2
Alexander Chemeriseb6807d2017-07-18 17:04:38 +030082 hplmn = enc_plmn(mcc, mnc)
83 content = hplmn + access_tech
Vadim Yanitskiy9664b2e2020-02-27 01:49:51 +070084 data, sw = self._scc.update_binary(EF['HPLMNwAcT'], content + 'ffffff0000' * (size // 5 - 1))
Alexander Chemeriseb6807d2017-07-18 17:04:38 +030085 return sw
86
Philipp Maierc8ce82a2018-07-04 17:57:20 +020087 def update_oplmn_act(self, mcc, mnc, access_tech='FFFF'):
88 """
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +020089 See note in update_hplmn_act()
Philipp Maierc8ce82a2018-07-04 17:57:20 +020090 """
91 # get size and write EF.OPLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +020092 data = self._scc.read_binary(EF['OPLMNwAcT'], length=None, offset=0)
Vadim Yanitskiy99affe12020-02-15 05:03:09 +070093 size = len(data[0]) // 2
Philipp Maierc8ce82a2018-07-04 17:57:20 +020094 hplmn = enc_plmn(mcc, mnc)
95 content = hplmn + access_tech
Vadim Yanitskiy9664b2e2020-02-27 01:49:51 +070096 data, sw = self._scc.update_binary(EF['OPLMNwAcT'], content + 'ffffff0000' * (size // 5 - 1))
Philipp Maierc8ce82a2018-07-04 17:57:20 +020097 return sw
98
99 def update_plmn_act(self, mcc, mnc, access_tech='FFFF'):
100 """
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200101 See note in update_hplmn_act()
Philipp Maierc8ce82a2018-07-04 17:57:20 +0200102 """
103 # get size and write EF.PLMNwAcT
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200104 data = self._scc.read_binary(EF['PLMNwAcT'], length=None, offset=0)
Vadim Yanitskiy99affe12020-02-15 05:03:09 +0700105 size = len(data[0]) // 2
Philipp Maierc8ce82a2018-07-04 17:57:20 +0200106 hplmn = enc_plmn(mcc, mnc)
107 content = hplmn + access_tech
Vadim Yanitskiy9664b2e2020-02-27 01:49:51 +0700108 data, sw = self._scc.update_binary(EF['PLMNwAcT'], content + 'ffffff0000' * (size // 5 - 1))
Philipp Maierc8ce82a2018-07-04 17:57:20 +0200109 return sw
110
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200111 def update_plmnsel(self, mcc, mnc):
112 data = self._scc.read_binary(EF['PLMNsel'], length=None, offset=0)
Vadim Yanitskiy99affe12020-02-15 05:03:09 +0700113 size = len(data[0]) // 2
Philipp Maier5bf42602018-07-11 23:23:40 +0200114 hplmn = enc_plmn(mcc, mnc)
Philipp Maieraf9ae8b2018-07-13 11:15:49 +0200115 data, sw = self._scc.update_binary(EF['PLMNsel'], hplmn + 'ff' * (size-3))
116 return sw
Philipp Maier5bf42602018-07-11 23:23:40 +0200117
Alexander Chemeriseb6807d2017-07-18 17:04:38 +0300118 def update_smsp(self, smsp):
119 data, sw = self._scc.update_record(EF['SMSP'], 1, rpad(smsp, 84))
120 return sw
121
Philipp Maieree908ae2019-03-21 16:21:12 +0100122 def update_ad(self, mnc):
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200123 #See also: 3GPP TS 31.102, chapter 4.2.18
124 mnclen = len(str(mnc))
125 if mnclen == 1:
126 mnclen = 2
127 if mnclen > 3:
Philipp Maieree908ae2019-03-21 16:21:12 +0100128 raise RuntimeError('unable to calculate proper mnclen')
129
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200130 data = self._scc.read_binary(EF['AD'], length=None, offset=0)
Vadim Yanitskiy99affe12020-02-15 05:03:09 +0700131 size = len(data[0]) // 2
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200132 content = data[0][0:6] + "%02X" % mnclen
Philipp Maieree908ae2019-03-21 16:21:12 +0100133 data, sw = self._scc.update_binary(EF['AD'], content)
134 return sw
135
Alexander Chemeriseb6807d2017-07-18 17:04:38 +0300136 def read_spn(self):
137 (spn, sw) = self._scc.read_binary(EF['SPN'])
138 if sw == '9000':
139 return (dec_spn(spn), sw)
140 else:
141 return (None, sw)
142
143 def update_spn(self, name, hplmn_disp=False, oplmn_disp=False):
144 content = enc_spn(name, hplmn_disp, oplmn_disp)
145 data, sw = self._scc.update_binary(EF['SPN'], rpad(content, 32))
146 return sw
147
Supreeth Herle98a69272020-03-18 12:14:48 +0100148 def read_gid1(self):
149 (res, sw) = self._scc.read_binary(EF['GID1'])
150 if sw == '9000':
151 return (res, sw)
152 else:
153 return (None, sw)
154
Philipp Maier0ad5bcf2019-12-31 17:55:47 +0100155 # Read the (full) AID for either ISIM or USIM application
156 def read_aid(self, isim = False):
157
158 # First (known) halves of the AID
159 aid_usim = "a0000000871002"
160 aid_isim = "a0000000871004"
161
162 # Select which one to look for
163 if isim:
164 aid = aid_isim
165 else:
166 aid = aid_usim
167
168 # Find out how many records the EF.DIR has, then go through
169 # all records and try to find the AID we are looking for
170 aid_record_count = self._scc.record_count(['2F00'])
171 for i in range(0, aid_record_count):
172 record = self._scc.read_record(['2F00'], i + 1)
173 if aid in record[0]:
174 aid_len = int(record[0][6:8], 16)
175 return record[0][8:8 + aid_len * 2]
176
177 return None
178
Supreeth Herlee4e98312020-03-18 11:33:14 +0100179 # Fetch all the AIDs present on UICC
180 def read_aids(self):
181 try:
182 # Find out how many records the EF.DIR has
183 # and store all the AIDs in the UICC
184 rec_cnt = self._scc.record_count(['3f00', '2f00'])
185 for i in range(0, rec_cnt):
186 rec = self._scc.read_record(['3f00', '2f00'], i + 1)
187 if (rec[0][0:2], rec[0][4:6]) == ('61', '4f') and len(rec[0]) > 12 \
188 and rec[0][8:8 + int(rec[0][6:8], 16) * 2] not in self._aids:
189 self._aids.append(rec[0][8:8 + int(rec[0][6:8], 16) * 2])
190 except Exception as e:
191 print("Can't read AIDs from SIM -- %s" % (str(e),))
192
Sylvain Munaut76504e02010-12-07 00:24:32 +0100193
194class _MagicSimBase(Card):
195 """
196 Theses cards uses several record based EFs to store the provider infos,
197 each possible provider uses a specific record number in each EF. The
198 indexes used are ( where N is the number of providers supported ) :
199 - [2 .. N+1] for the operator name
Supreeth Herle9ca41c12020-01-21 12:50:30 +0100200 - [1 .. N] for the programable EFs
Sylvain Munaut76504e02010-12-07 00:24:32 +0100201
202 * 3f00/7f4d/8f0c : Operator Name
203
204 bytes 0-15 : provider name, padded with 0xff
205 byte 16 : length of the provider name
206 byte 17 : 01 for valid records, 00 otherwise
207
208 * 3f00/7f4d/8f0d : Programmable Binary EFs
209
210 * 3f00/7f4d/8f0e : Programmable Record EFs
211
212 """
213
214 @classmethod
215 def autodetect(kls, scc):
216 try:
217 for p, l, t in kls._files.values():
218 if not t:
219 continue
220 if scc.record_size(['3f00', '7f4d', p]) != l:
221 return None
222 except:
223 return None
224
225 return kls(scc)
226
227 def _get_count(self):
228 """
229 Selects the file and returns the total number of entries
230 and entry size
231 """
232 f = self._files['name']
233
234 r = self._scc.select_file(['3f00', '7f4d', f[0]])
235 rec_len = int(r[-1][28:30], 16)
236 tlen = int(r[-1][4:8],16)
237 rec_cnt = (tlen / rec_len) - 1;
238
239 if (rec_cnt < 1) or (rec_len != f[1]):
240 raise RuntimeError('Bad card type')
241
242 return rec_cnt
243
244 def program(self, p):
245 # Go to dir
246 self._scc.select_file(['3f00', '7f4d'])
247
248 # Home PLMN in PLMN_Sel format
Alexander Chemeris7be92ff2013-07-10 11:18:06 +0400249 hplmn = enc_plmn(p['mcc'], p['mnc'])
Sylvain Munaut76504e02010-12-07 00:24:32 +0100250
251 # Operator name ( 3f00/7f4d/8f0c )
252 self._scc.update_record(self._files['name'][0], 2,
253 rpad(b2h(p['name']), 32) + ('%02x' % len(p['name'])) + '01'
254 )
255
256 # ICCID/IMSI/Ki/HPLMN ( 3f00/7f4d/8f0d )
257 v = ''
258
259 # inline Ki
260 if self._ki_file is None:
261 v += p['ki']
262
263 # ICCID
Alexander Chemeris7be92ff2013-07-10 11:18:06 +0400264 v += '3f00' + '2fe2' + '0a' + enc_iccid(p['iccid'])
Sylvain Munaut76504e02010-12-07 00:24:32 +0100265
266 # IMSI
Alexander Chemeris7be92ff2013-07-10 11:18:06 +0400267 v += '7f20' + '6f07' + '09' + enc_imsi(p['imsi'])
Sylvain Munaut76504e02010-12-07 00:24:32 +0100268
269 # Ki
270 if self._ki_file:
271 v += self._ki_file + '10' + p['ki']
272
273 # PLMN_Sel
274 v+= '6f30' + '18' + rpad(hplmn, 36)
275
Alexander Chemeris21885242013-07-02 16:56:55 +0400276 # ACC
277 # This doesn't work with "fake" SuperSIM cards,
278 # but will hopefully work with real SuperSIMs.
279 if p.get('acc') is not None:
280 v+= '6f78' + '02' + lpad(p['acc'], 4)
281
Sylvain Munaut76504e02010-12-07 00:24:32 +0100282 self._scc.update_record(self._files['b_ef'][0], 1,
283 rpad(v, self._files['b_ef'][1]*2)
284 )
285
286 # SMSP ( 3f00/7f4d/8f0e )
287 # FIXME
288
289 # Write PLMN_Sel forcefully as well
290 r = self._scc.select_file(['3f00', '7f20', '6f30'])
291 tl = int(r[-1][4:8], 16)
292
Alexander Chemeris7be92ff2013-07-10 11:18:06 +0400293 hplmn = enc_plmn(p['mcc'], p['mnc'])
Sylvain Munaut76504e02010-12-07 00:24:32 +0100294 self._scc.update_binary('6f30', hplmn + 'ff' * (tl-3))
295
296 def erase(self):
297 # Dummy
298 df = {}
299 for k, v in self._files.iteritems():
300 ofs = 1
301 fv = v[1] * 'ff'
302 if k == 'name':
303 ofs = 2
304 fv = fv[0:-4] + '0000'
305 df[v[0]] = (fv, ofs)
306
307 # Write
308 for n in range(0,self._get_count()):
309 for k, (msg, ofs) in df.iteritems():
310 self._scc.update_record(['3f00', '7f4d', k], n + ofs, msg)
311
312
313class SuperSim(_MagicSimBase):
314
315 name = 'supersim'
316
317 _files = {
318 'name' : ('8f0c', 18, True),
319 'b_ef' : ('8f0d', 74, True),
320 'r_ef' : ('8f0e', 50, True),
321 }
322
323 _ki_file = None
324
325
326class MagicSim(_MagicSimBase):
327
328 name = 'magicsim'
329
330 _files = {
331 'name' : ('8f0c', 18, True),
332 'b_ef' : ('8f0d', 130, True),
333 'r_ef' : ('8f0e', 102, False),
334 }
335
336 _ki_file = '6f1b'
337
338
339class FakeMagicSim(Card):
340 """
341 Theses cards have a record based EF 3f00/000c that contains the provider
342 informations. See the program method for its format. The records go from
343 1 to N.
344 """
345
346 name = 'fakemagicsim'
347
348 @classmethod
349 def autodetect(kls, scc):
350 try:
351 if scc.record_size(['3f00', '000c']) != 0x5a:
352 return None
353 except:
354 return None
355
356 return kls(scc)
357
358 def _get_infos(self):
359 """
360 Selects the file and returns the total number of entries
361 and entry size
362 """
363
364 r = self._scc.select_file(['3f00', '000c'])
365 rec_len = int(r[-1][28:30], 16)
366 tlen = int(r[-1][4:8],16)
367 rec_cnt = (tlen / rec_len) - 1;
368
369 if (rec_cnt < 1) or (rec_len != 0x5a):
370 raise RuntimeError('Bad card type')
371
372 return rec_cnt, rec_len
373
374 def program(self, p):
375 # Home PLMN
376 r = self._scc.select_file(['3f00', '7f20', '6f30'])
377 tl = int(r[-1][4:8], 16)
378
Alexander Chemeris7be92ff2013-07-10 11:18:06 +0400379 hplmn = enc_plmn(p['mcc'], p['mnc'])
Sylvain Munaut76504e02010-12-07 00:24:32 +0100380 self._scc.update_binary('6f30', hplmn + 'ff' * (tl-3))
381
382 # Get total number of entries and entry size
383 rec_cnt, rec_len = self._get_infos()
384
385 # Set first entry
386 entry = (
Philipp Maier45daa922019-04-01 15:49:45 +0200387 '81' + # 1b Status: Valid & Active
Sylvain Munaut76504e02010-12-07 00:24:32 +0100388 rpad(b2h(p['name'][0:14]), 28) + # 14b Entry Name
Philipp Maier45daa922019-04-01 15:49:45 +0200389 enc_iccid(p['iccid']) + # 10b ICCID
390 enc_imsi(p['imsi']) + # 9b IMSI_len + id_type(9) + IMSI
391 p['ki'] + # 16b Ki
392 lpad(p['smsp'], 80) # 40b SMSP (padded with ff if needed)
Sylvain Munaut76504e02010-12-07 00:24:32 +0100393 )
394 self._scc.update_record('000c', 1, entry)
395
396 def erase(self):
397 # Get total number of entries and entry size
398 rec_cnt, rec_len = self._get_infos()
399
400 # Erase all entries
401 entry = 'ff' * rec_len
402 for i in range(0, rec_cnt):
403 self._scc.update_record('000c', 1+i, entry)
404
Sylvain Munaut5da8d4e2013-07-02 15:13:24 +0200405
Harald Welte3156d902011-03-22 21:48:19 +0100406class GrcardSim(Card):
407 """
408 Greencard (grcard.cn) HZCOS GSM SIM
409 These cards have a much more regular ISO 7816-4 / TS 11.11 structure,
410 and use standard UPDATE RECORD / UPDATE BINARY commands except for Ki.
411 """
412
413 name = 'grcardsim'
414
415 @classmethod
416 def autodetect(kls, scc):
417 return None
418
419 def program(self, p):
420 # We don't really know yet what ADM PIN 4 is about
421 #self._scc.verify_chv(4, h2b("4444444444444444"))
422
423 # Authenticate using ADM PIN 5
Jan Balkec3ebd332015-01-26 12:22:55 +0100424 if p['pin_adm']:
Philipp Maiera3de5a32018-08-23 10:27:04 +0200425 pin = h2b(p['pin_adm'])
Jan Balkec3ebd332015-01-26 12:22:55 +0100426 else:
427 pin = h2b("4444444444444444")
428 self._scc.verify_chv(5, pin)
Harald Welte3156d902011-03-22 21:48:19 +0100429
430 # EF.ICCID
431 r = self._scc.select_file(['3f00', '2fe2'])
Alexander Chemeris7be92ff2013-07-10 11:18:06 +0400432 data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid']))
Harald Welte3156d902011-03-22 21:48:19 +0100433
434 # EF.IMSI
435 r = self._scc.select_file(['3f00', '7f20', '6f07'])
Alexander Chemeris7be92ff2013-07-10 11:18:06 +0400436 data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi']))
Harald Welte3156d902011-03-22 21:48:19 +0100437
438 # EF.ACC
Alexander Chemeris21885242013-07-02 16:56:55 +0400439 if p.get('acc') is not None:
440 data, sw = self._scc.update_binary('6f78', lpad(p['acc'], 4))
Harald Welte3156d902011-03-22 21:48:19 +0100441
442 # EF.SMSP
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200443 if p.get('smsp'):
Harald Welte23888da2019-08-28 23:19:11 +0200444 r = self._scc.select_file(['3f00', '7f10', '6f42'])
445 data, sw = self._scc.update_record('6f42', 1, lpad(p['smsp'], 80))
Harald Welte3156d902011-03-22 21:48:19 +0100446
447 # Set the Ki using proprietary command
448 pdu = '80d4020010' + p['ki']
449 data, sw = self._scc._tp.send_apdu(pdu)
450
451 # EF.HPLMN
452 r = self._scc.select_file(['3f00', '7f20', '6f30'])
453 size = int(r[-1][4:8], 16)
Alexander Chemeris7be92ff2013-07-10 11:18:06 +0400454 hplmn = enc_plmn(p['mcc'], p['mnc'])
Harald Welte3156d902011-03-22 21:48:19 +0100455 self._scc.update_binary('6f30', hplmn + 'ff' * (size-3))
456
457 # EF.SPN (Service Provider Name)
458 r = self._scc.select_file(['3f00', '7f20', '6f30'])
459 size = int(r[-1][4:8], 16)
460 # FIXME
461
462 # FIXME: EF.MSISDN
463
464 def erase(self):
465 return
Sylvain Munaut76504e02010-12-07 00:24:32 +0100466
Harald Weltee10394b2011-12-07 12:34:14 +0100467class SysmoSIMgr1(GrcardSim):
468 """
469 sysmocom sysmoSIM-GR1
470 These cards have a much more regular ISO 7816-4 / TS 11.11 structure,
471 and use standard UPDATE RECORD / UPDATE BINARY commands except for Ki.
472 """
473 name = 'sysmosim-gr1'
474
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200475 @classmethod
Philipp Maier087feff2018-08-23 09:41:36 +0200476 def autodetect(kls, scc):
477 try:
478 # Look for ATR
479 if scc.get_atr() == toBytes("3B 99 18 00 11 88 22 33 44 55 66 77 60"):
480 return kls(scc)
481 except:
482 return None
483 return None
Sylvain Munaut5da8d4e2013-07-02 15:13:24 +0200484
Holger Hans Peter Freyther4d91bf42012-03-22 14:28:38 +0100485class SysmoUSIMgr1(Card):
486 """
487 sysmocom sysmoUSIM-GR1
488 """
489 name = 'sysmoUSIM-GR1'
490
491 @classmethod
492 def autodetect(kls, scc):
493 # TODO: Access the ATR
494 return None
495
496 def program(self, p):
497 # TODO: check if verify_chv could be used or what it needs
498 # self._scc.verify_chv(0x0A, [0x33,0x32,0x32,0x31,0x33,0x32,0x33,0x32])
499 # Unlock the card..
500 data, sw = self._scc._tp.send_apdu_checksw("0020000A083332323133323332")
501
502 # TODO: move into SimCardCommands
Holger Hans Peter Freyther4d91bf42012-03-22 14:28:38 +0100503 par = ( p['ki'] + # 16b K
Alexander Chemeris7be92ff2013-07-10 11:18:06 +0400504 p['opc'] + # 32b OPC
505 enc_iccid(p['iccid']) + # 10b ICCID
506 enc_imsi(p['imsi']) # 9b IMSI_len + id_type(9) + IMSI
Holger Hans Peter Freyther4d91bf42012-03-22 14:28:38 +0100507 )
508 data, sw = self._scc._tp.send_apdu_checksw("0099000033" + par)
509
510 def erase(self):
511 return
512
Sylvain Munaut053c8952013-07-02 15:12:32 +0200513
Sylvain Munaut2fc205c2013-12-23 17:22:56 +0100514class SysmoSIMgr2(Card):
515 """
516 sysmocom sysmoSIM-GR2
517 """
518
519 name = 'sysmoSIM-GR2'
520
521 @classmethod
522 def autodetect(kls, scc):
Alexander Chemeris8ad124a2018-01-10 14:17:55 +0900523 try:
524 # Look for ATR
525 if scc.get_atr() == toBytes("3B 7D 94 00 00 55 55 53 0A 74 86 93 0B 24 7C 4D 54 68"):
526 return kls(scc)
527 except:
528 return None
Sylvain Munaut2fc205c2013-12-23 17:22:56 +0100529 return None
530
531 def program(self, p):
532
533 # select MF
534 r = self._scc.select_file(['3f00'])
535
536 # authenticate as SUPER ADM using default key
537 self._scc.verify_chv(0x0b, h2b("3838383838383838"))
538
539 # set ADM pin using proprietary command
540 # INS: D4
541 # P1: 3A for PIN, 3B for PUK
542 # P2: CHV number, as in VERIFY CHV for PIN, and as in UNBLOCK CHV for PUK
543 # P3: 08, CHV length (curiously the PUK is also 08 length, instead of 10)
Jan Balkec3ebd332015-01-26 12:22:55 +0100544 if p['pin_adm']:
Daniel Willmann7d38d742018-06-15 07:31:50 +0200545 pin = h2b(p['pin_adm'])
Jan Balkec3ebd332015-01-26 12:22:55 +0100546 else:
547 pin = h2b("4444444444444444")
548
549 pdu = 'A0D43A0508' + b2h(pin)
Sylvain Munaut2fc205c2013-12-23 17:22:56 +0100550 data, sw = self._scc._tp.send_apdu(pdu)
551
552 # authenticate as ADM (enough to write file, and can set PINs)
Jan Balkec3ebd332015-01-26 12:22:55 +0100553
554 self._scc.verify_chv(0x05, pin)
Sylvain Munaut2fc205c2013-12-23 17:22:56 +0100555
556 # write EF.ICCID
557 data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid']))
558
559 # select DF_GSM
560 r = self._scc.select_file(['7f20'])
561
562 # write EF.IMSI
563 data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi']))
564
565 # write EF.ACC
566 if p.get('acc') is not None:
567 data, sw = self._scc.update_binary('6f78', lpad(p['acc'], 4))
568
569 # get size and write EF.HPLMN
570 r = self._scc.select_file(['6f30'])
571 size = int(r[-1][4:8], 16)
572 hplmn = enc_plmn(p['mcc'], p['mnc'])
573 self._scc.update_binary('6f30', hplmn + 'ff' * (size-3))
574
575 # set COMP128 version 0 in proprietary file
576 data, sw = self._scc.update_binary('0001', '001000')
577
578 # set Ki in proprietary file
579 data, sw = self._scc.update_binary('0001', p['ki'], 3)
580
581 # select DF_TELECOM
582 r = self._scc.select_file(['3f00', '7f10'])
583
584 # write EF.SMSP
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200585 if p.get('smsp'):
Harald Welte23888da2019-08-28 23:19:11 +0200586 data, sw = self._scc.update_record('6f42', 1, lpad(p['smsp'], 80))
Sylvain Munaut2fc205c2013-12-23 17:22:56 +0100587
588 def erase(self):
589 return
590
Jan Balke3e840672015-01-26 15:36:27 +0100591class SysmoUSIMSJS1(Card):
592 """
593 sysmocom sysmoUSIM-SJS1
594 """
595
596 name = 'sysmoUSIM-SJS1'
597
598 def __init__(self, ssc):
599 super(SysmoUSIMSJS1, self).__init__(ssc)
600 self._scc.cla_byte = "00"
Philipp Maier2d15ea02019-03-20 12:40:36 +0100601 self._scc.sel_ctrl = "0004" #request an FCP
Jan Balke3e840672015-01-26 15:36:27 +0100602
603 @classmethod
604 def autodetect(kls, scc):
Alexander Chemeris8ad124a2018-01-10 14:17:55 +0900605 try:
606 # Look for ATR
607 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"):
608 return kls(scc)
609 except:
610 return None
Jan Balke3e840672015-01-26 15:36:27 +0100611 return None
612
613 def program(self, p):
614
Philipp Maiere9604882017-03-21 17:24:31 +0100615 # authenticate as ADM using default key (written on the card..)
616 if not p['pin_adm']:
617 raise ValueError("Please provide a PIN-ADM as there is no default one")
618 self._scc.verify_chv(0x0A, h2b(p['pin_adm']))
Jan Balke3e840672015-01-26 15:36:27 +0100619
620 # select MF
621 r = self._scc.select_file(['3f00'])
622
Philipp Maiere9604882017-03-21 17:24:31 +0100623 # write EF.ICCID
624 data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid']))
625
Jan Balke3e840672015-01-26 15:36:27 +0100626 # select DF_GSM
627 r = self._scc.select_file(['7f20'])
628
Jan Balke3e840672015-01-26 15:36:27 +0100629 # set Ki in proprietary file
630 data, sw = self._scc.update_binary('00FF', p['ki'])
631
Philipp Maier1be35bf2018-07-13 11:29:03 +0200632 # set OPc in proprietary file
Daniel Willmann67acdbc2018-06-15 07:42:48 +0200633 if 'opc' in p:
634 content = "01" + p['opc']
635 data, sw = self._scc.update_binary('00F7', content)
Jan Balke3e840672015-01-26 15:36:27 +0100636
Supreeth Herle7947d922019-06-08 07:50:53 +0200637 # set Service Provider Name
Supreeth Herle840a9e22020-01-21 13:32:46 +0100638 if p.get('name') is not None:
639 content = enc_spn(p['name'], True, True)
640 data, sw = self._scc.update_binary('6F46', rpad(content, 32))
Supreeth Herle7947d922019-06-08 07:50:53 +0200641
Supreeth Herlec8796a32019-12-23 12:23:42 +0100642 if p.get('acc') is not None:
643 self.update_acc(p['acc'])
644
Jan Balke3e840672015-01-26 15:36:27 +0100645 # write EF.IMSI
646 data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi']))
647
Philipp Maier2d15ea02019-03-20 12:40:36 +0100648 # EF.PLMNsel
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200649 if p.get('mcc') and p.get('mnc'):
650 sw = self.update_plmnsel(p['mcc'], p['mnc'])
651 if sw != '9000':
Philipp Maier2d15ea02019-03-20 12:40:36 +0100652 print("Programming PLMNsel failed with code %s"%sw)
653
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200654 # EF.PLMNwAcT
655 if p.get('mcc') and p.get('mnc'):
Philipp Maier2d15ea02019-03-20 12:40:36 +0100656 sw = self.update_plmn_act(p['mcc'], p['mnc'])
657 if sw != '9000':
658 print("Programming PLMNwAcT failed with code %s"%sw)
659
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200660 # EF.OPLMNwAcT
661 if p.get('mcc') and p.get('mnc'):
Philipp Maier2d15ea02019-03-20 12:40:36 +0100662 sw = self.update_oplmn_act(p['mcc'], p['mnc'])
663 if sw != '9000':
664 print("Programming OPLMNwAcT failed with code %s"%sw)
665
Supreeth Herlef442fb42020-01-21 12:47:32 +0100666 # EF.HPLMNwAcT
667 if p.get('mcc') and p.get('mnc'):
668 sw = self.update_hplmn_act(p['mcc'], p['mnc'])
669 if sw != '9000':
670 print("Programming HPLMNwAcT failed with code %s"%sw)
671
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200672 # EF.AD
673 if p.get('mcc') and p.get('mnc'):
Philipp Maieree908ae2019-03-21 16:21:12 +0100674 sw = self.update_ad(p['mnc'])
675 if sw != '9000':
676 print("Programming AD failed with code %s"%sw)
Philipp Maier2d15ea02019-03-20 12:40:36 +0100677
Daniel Willmann1d087ef2017-08-31 10:08:45 +0200678 # EF.SMSP
Harald Welte23888da2019-08-28 23:19:11 +0200679 if p.get('smsp'):
680 r = self._scc.select_file(['3f00', '7f10'])
681 data, sw = self._scc.update_record('6f42', 1, lpad(p['smsp'], 104), force_len=True)
Jan Balke3e840672015-01-26 15:36:27 +0100682
Supreeth Herle5a541012019-12-22 08:59:16 +0100683 # EF.MSISDN
684 # TODO: Alpha Identifier (currently 'ff'O * 20)
685 # TODO: Capability/Configuration1 Record Identifier
686 # TODO: Extension1 Record Identifier
687 if p.get('msisdn') is not None:
688 msisdn = enc_msisdn(p['msisdn'])
689 data = 'ff' * 20 + msisdn + 'ff' * 2
690
691 r = self._scc.select_file(['3f00', '7f10'])
692 data, sw = self._scc.update_record('6F40', 1, data, force_len=True)
693
Alexander Chemerise0d9d882018-01-10 14:18:32 +0900694 def erase(self):
695 return
696
697
698class FairwavesSIM(Card):
699 """
700 FairwavesSIM
701
702 The SIM card is operating according to the standard.
703 For Ki/OP/OPC programming the following files are additionally open for writing:
704 3F00/7F20/FF01 – OP/OPC:
705 byte 1 = 0x01, bytes 2-17: OPC;
706 byte 1 = 0x00, bytes 2-17: OP;
707 3F00/7F20/FF02: Ki
708 """
709
Philipp Maier5a876312019-11-11 11:01:46 +0100710 name = 'Fairwaves-SIM'
Alexander Chemerise0d9d882018-01-10 14:18:32 +0900711 # Propriatary files
712 _EF_num = {
713 'Ki': 'FF02',
714 'OP/OPC': 'FF01',
715 }
716 _EF = {
717 'Ki': DF['GSM']+[_EF_num['Ki']],
718 'OP/OPC': DF['GSM']+[_EF_num['OP/OPC']],
719 }
720
721 def __init__(self, ssc):
722 super(FairwavesSIM, self).__init__(ssc)
723 self._adm_chv_num = 0x11
724 self._adm2_chv_num = 0x12
725
726
727 @classmethod
728 def autodetect(kls, scc):
729 try:
730 # Look for ATR
731 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"):
732 return kls(scc)
733 except:
734 return None
735 return None
736
737
738 def verify_adm2(self, key):
739 '''
740 Authenticate with ADM2 key.
741
742 Fairwaves SIM cards support hierarchical key structure and ADM2 key
743 is a key which has access to proprietary files (Ki and OP/OPC).
744 That said, ADM key inherits permissions of ADM2 key and thus we rarely
745 need ADM2 key per se.
746 '''
747 (res, sw) = self._scc.verify_chv(self._adm2_chv_num, key)
748 return sw
749
750
751 def read_ki(self):
752 """
753 Read Ki in proprietary file.
754
755 Requires ADM1 access level
756 """
757 return self._scc.read_binary(self._EF['Ki'])
758
759
760 def update_ki(self, ki):
761 """
762 Set Ki in proprietary file.
763
764 Requires ADM1 access level
765 """
766 data, sw = self._scc.update_binary(self._EF['Ki'], ki)
767 return sw
768
769
770 def read_op_opc(self):
771 """
772 Read Ki in proprietary file.
773
774 Requires ADM1 access level
775 """
776 (ef, sw) = self._scc.read_binary(self._EF['OP/OPC'])
777 type = 'OP' if ef[0:2] == '00' else 'OPC'
778 return ((type, ef[2:]), sw)
779
780
781 def update_op(self, op):
782 """
783 Set OP in proprietary file.
784
785 Requires ADM1 access level
786 """
787 content = '00' + op
788 data, sw = self._scc.update_binary(self._EF['OP/OPC'], content)
789 return sw
790
791
792 def update_opc(self, opc):
793 """
794 Set OPC in proprietary file.
795
796 Requires ADM1 access level
797 """
798 content = '01' + opc
799 data, sw = self._scc.update_binary(self._EF['OP/OPC'], content)
800 return sw
801
802
803 def program(self, p):
804 # authenticate as ADM1
805 if not p['pin_adm']:
806 raise ValueError("Please provide a PIN-ADM as there is no default one")
807 sw = self.verify_adm(h2b(p['pin_adm']))
808 if sw != '9000':
809 raise RuntimeError('Failed to authenticate with ADM key %s'%(p['pin_adm'],))
810
811 # TODO: Set operator name
812 if p.get('smsp') is not None:
813 sw = self.update_smsp(p['smsp'])
814 if sw != '9000':
815 print("Programming SMSP failed with code %s"%sw)
816 # This SIM doesn't support changing ICCID
817 if p.get('mcc') is not None and p.get('mnc') is not None:
818 sw = self.update_hplmn_act(p['mcc'], p['mnc'])
819 if sw != '9000':
820 print("Programming MCC/MNC failed with code %s"%sw)
821 if p.get('imsi') is not None:
822 sw = self.update_imsi(p['imsi'])
823 if sw != '9000':
824 print("Programming IMSI failed with code %s"%sw)
825 if p.get('ki') is not None:
826 sw = self.update_ki(p['ki'])
827 if sw != '9000':
828 print("Programming Ki failed with code %s"%sw)
829 if p.get('opc') is not None:
830 sw = self.update_opc(p['opc'])
831 if sw != '9000':
832 print("Programming OPC failed with code %s"%sw)
833 if p.get('acc') is not None:
834 sw = self.update_acc(p['acc'])
835 if sw != '9000':
836 print("Programming ACC failed with code %s"%sw)
Jan Balke3e840672015-01-26 15:36:27 +0100837
838 def erase(self):
839 return
840
841
Todd Neal9eeadfc2018-04-25 15:36:29 -0500842class OpenCellsSim(Card):
843 """
844 OpenCellsSim
845
846 """
847
Philipp Maier5a876312019-11-11 11:01:46 +0100848 name = 'OpenCells-SIM'
Todd Neal9eeadfc2018-04-25 15:36:29 -0500849
850 def __init__(self, ssc):
851 super(OpenCellsSim, self).__init__(ssc)
852 self._adm_chv_num = 0x0A
853
854
855 @classmethod
856 def autodetect(kls, scc):
857 try:
858 # Look for ATR
859 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"):
860 return kls(scc)
861 except:
862 return None
863 return None
864
865
866 def program(self, p):
867 if not p['pin_adm']:
868 raise ValueError("Please provide a PIN-ADM as there is no default one")
869 self._scc.verify_chv(0x0A, h2b(p['pin_adm']))
870
871 # select MF
872 r = self._scc.select_file(['3f00'])
873
874 # write EF.ICCID
875 data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid']))
876
877 r = self._scc.select_file(['7ff0'])
878
879 # set Ki in proprietary file
880 data, sw = self._scc.update_binary('FF02', p['ki'])
881
882 # set OPC in proprietary file
883 data, sw = self._scc.update_binary('FF01', p['opc'])
884
885 # select DF_GSM
886 r = self._scc.select_file(['7f20'])
887
888 # write EF.IMSI
889 data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi']))
890
Philipp Maierc8ce82a2018-07-04 17:57:20 +0200891class WavemobileSim(Card):
892 """
893 WavemobileSim
894
895 """
896
897 name = 'Wavemobile-SIM'
898
899 def __init__(self, ssc):
900 super(WavemobileSim, self).__init__(ssc)
901 self._adm_chv_num = 0x0A
902 self._scc.cla_byte = "00"
903 self._scc.sel_ctrl = "0004" #request an FCP
904
905 @classmethod
906 def autodetect(kls, scc):
907 try:
908 # Look for ATR
909 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"):
910 return kls(scc)
911 except:
912 return None
913 return None
914
915 def program(self, p):
916 if not p['pin_adm']:
917 raise ValueError("Please provide a PIN-ADM as there is no default one")
918 sw = self.verify_adm(h2b(p['pin_adm']))
919 if sw != '9000':
920 raise RuntimeError('Failed to authenticate with ADM key %s'%(p['pin_adm'],))
921
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200922 # EF.ICCID
923 # TODO: Add programming of the ICCID
924 if p.get('iccid'):
Philipp Maierc8ce82a2018-07-04 17:57:20 +0200925 print("Warning: Programming of the ICCID is not implemented for this type of card.")
926
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200927 # KI (Presumably a propritary file)
928 # TODO: Add programming of KI
929 if p.get('ki'):
Philipp Maierc8ce82a2018-07-04 17:57:20 +0200930 print("Warning: Programming of the KI is not implemented for this type of card.")
931
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200932 # OPc (Presumably a propritary file)
933 # TODO: Add programming of OPc
934 if p.get('opc'):
Philipp Maierc8ce82a2018-07-04 17:57:20 +0200935 print("Warning: Programming of the OPc is not implemented for this type of card.")
936
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200937 # EF.SMSP
Philipp Maierc8ce82a2018-07-04 17:57:20 +0200938 if p.get('smsp'):
939 sw = self.update_smsp(p['smsp'])
940 if sw != '9000':
941 print("Programming SMSP failed with code %s"%sw)
942
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200943 # EF.IMSI
Philipp Maierc8ce82a2018-07-04 17:57:20 +0200944 if p.get('imsi'):
945 sw = self.update_imsi(p['imsi'])
946 if sw != '9000':
947 print("Programming IMSI failed with code %s"%sw)
948
949 # EF.ACC
950 if p.get('acc'):
951 sw = self.update_acc(p['acc'])
952 if sw != '9000':
953 print("Programming ACC failed with code %s"%sw)
954
955 # EF.PLMNsel
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200956 if p.get('mcc') and p.get('mnc'):
957 sw = self.update_plmnsel(p['mcc'], p['mnc'])
958 if sw != '9000':
Philipp Maierc8ce82a2018-07-04 17:57:20 +0200959 print("Programming PLMNsel failed with code %s"%sw)
960
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200961 # EF.PLMNwAcT
962 if p.get('mcc') and p.get('mnc'):
Philipp Maierc8ce82a2018-07-04 17:57:20 +0200963 sw = self.update_plmn_act(p['mcc'], p['mnc'])
964 if sw != '9000':
965 print("Programming PLMNwAcT failed with code %s"%sw)
966
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200967 # EF.OPLMNwAcT
968 if p.get('mcc') and p.get('mnc'):
Philipp Maierc8ce82a2018-07-04 17:57:20 +0200969 sw = self.update_oplmn_act(p['mcc'], p['mnc'])
970 if sw != '9000':
971 print("Programming OPLMNwAcT failed with code %s"%sw)
972
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200973 # EF.AD
974 if p.get('mcc') and p.get('mnc'):
Philipp Maier6e507a72019-04-01 16:33:48 +0200975 sw = self.update_ad(p['mnc'])
976 if sw != '9000':
977 print("Programming AD failed with code %s"%sw)
978
Denis 'GNUtoo' Carikli84d2cb32019-09-12 01:46:25 +0200979 return None
Philipp Maierc8ce82a2018-07-04 17:57:20 +0200980
981 def erase(self):
982 return
983
Todd Neal9eeadfc2018-04-25 15:36:29 -0500984
Philipp Maier0ad5bcf2019-12-31 17:55:47 +0100985class SysmoISIMSJA2(Card):
986 """
987 sysmocom sysmoISIM-SJA2
988 """
989
990 name = 'sysmoISIM-SJA2'
991
992 def __init__(self, ssc):
993 super(SysmoISIMSJA2, self).__init__(ssc)
994 self._scc.cla_byte = "00"
995 self._scc.sel_ctrl = "0004" #request an FCP
996
997 @classmethod
998 def autodetect(kls, scc):
999 try:
1000 # Try card model #1
1001 atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 75 30 34 05 4B A9"
1002 if scc.get_atr() == toBytes(atr):
1003 return kls(scc)
1004
1005 # Try card model #2
1006 atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 75 31 33 02 51 B2"
1007 if scc.get_atr() == toBytes(atr):
1008 return kls(scc)
Philipp Maierb3e11ea2020-03-11 12:32:44 +01001009
1010 # Try card model #3
1011 atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 52 75 31 04 51 D5"
1012 if scc.get_atr() == toBytes(atr):
1013 return kls(scc)
Philipp Maier0ad5bcf2019-12-31 17:55:47 +01001014 except:
1015 return None
1016 return None
1017
1018 def program(self, p):
1019 # authenticate as ADM using default key (written on the card..)
1020 if not p['pin_adm']:
1021 raise ValueError("Please provide a PIN-ADM as there is no default one")
1022 self._scc.verify_chv(0x0A, h2b(p['pin_adm']))
1023
1024 # This type of card does not allow to reprogram the ICCID.
1025 # Reprogramming the ICCID would mess up the card os software
1026 # license management, so the ICCID must be kept at its factory
1027 # setting!
1028 if p.get('iccid'):
1029 print("Warning: Programming of the ICCID is not implemented for this type of card.")
1030
1031 # select DF_GSM
1032 self._scc.select_file(['7f20'])
1033
1034 # write EF.IMSI
1035 if p.get('imsi'):
1036 self._scc.update_binary('6f07', enc_imsi(p['imsi']))
1037
1038 # EF.PLMNsel
1039 if p.get('mcc') and p.get('mnc'):
1040 sw = self.update_plmnsel(p['mcc'], p['mnc'])
1041 if sw != '9000':
1042 print("Programming PLMNsel failed with code %s"%sw)
1043
1044 # EF.PLMNwAcT
1045 if p.get('mcc') and p.get('mnc'):
1046 sw = self.update_plmn_act(p['mcc'], p['mnc'])
1047 if sw != '9000':
1048 print("Programming PLMNwAcT failed with code %s"%sw)
1049
1050 # EF.OPLMNwAcT
1051 if p.get('mcc') and p.get('mnc'):
1052 sw = self.update_oplmn_act(p['mcc'], p['mnc'])
1053 if sw != '9000':
1054 print("Programming OPLMNwAcT failed with code %s"%sw)
1055
1056 # EF.AD
1057 if p.get('mcc') and p.get('mnc'):
1058 sw = self.update_ad(p['mnc'])
1059 if sw != '9000':
1060 print("Programming AD failed with code %s"%sw)
1061
1062 # EF.SMSP
1063 if p.get('smsp'):
1064 r = self._scc.select_file(['3f00', '7f10'])
1065 data, sw = self._scc.update_record('6f42', 1, lpad(p['smsp'], 104), force_len=True)
1066
1067 # update EF-SIM_AUTH_KEY (and EF-USIM_AUTH_KEY_2G, which is
1068 # hard linked to EF-USIM_AUTH_KEY)
1069 self._scc.select_file(['3f00'])
1070 self._scc.select_file(['a515'])
1071 if p.get('ki'):
1072 self._scc.update_binary('6f20', p['ki'], 1)
1073 if p.get('opc'):
1074 self._scc.update_binary('6f20', p['opc'], 17)
1075
1076 # update EF-USIM_AUTH_KEY in ADF.ISIM
1077 self._scc.select_file(['3f00'])
1078 aid = self.read_aid(isim = True)
Philipp Maierd9507862020-03-11 12:18:29 +01001079 if (aid):
1080 self._scc.select_adf(aid)
1081 if p.get('ki'):
1082 self._scc.update_binary('af20', p['ki'], 1)
1083 if p.get('opc'):
1084 self._scc.update_binary('af20', p['opc'], 17)
Philipp Maier0ad5bcf2019-12-31 17:55:47 +01001085
1086 # update EF-USIM_AUTH_KEY in ADF.USIM
1087 self._scc.select_file(['3f00'])
1088 aid = self.read_aid()
Philipp Maierd9507862020-03-11 12:18:29 +01001089 if (aid):
1090 self._scc.select_adf(aid)
1091 if p.get('ki'):
1092 self._scc.update_binary('af20', p['ki'], 1)
1093 if p.get('opc'):
1094 self._scc.update_binary('af20', p['opc'], 17)
Philipp Maier0ad5bcf2019-12-31 17:55:47 +01001095
1096 return
1097
1098 def erase(self):
1099 return
1100
1101
Todd Neal9eeadfc2018-04-25 15:36:29 -05001102# In order for autodetection ...
Harald Weltee10394b2011-12-07 12:34:14 +01001103_cards_classes = [ FakeMagicSim, SuperSim, MagicSim, GrcardSim,
Alexander Chemerise0d9d882018-01-10 14:18:32 +09001104 SysmoSIMgr1, SysmoSIMgr2, SysmoUSIMgr1, SysmoUSIMSJS1,
Philipp Maier0ad5bcf2019-12-31 17:55:47 +01001105 FairwavesSIM, OpenCellsSim, WavemobileSim, SysmoISIMSJA2 ]
Alexander Chemeris8ad124a2018-01-10 14:17:55 +09001106
1107def card_autodetect(scc):
1108 for kls in _cards_classes:
1109 card = kls.autodetect(scc)
1110 if card is not None:
1111 card.reset()
1112 return card
1113 return None
Supreeth Herle4c306ab2020-03-18 11:38:00 +01001114
1115def card_detect(ctype, scc):
1116 # Detect type if needed
1117 card = None
1118 ctypes = dict([(kls.name, kls) for kls in _cards_classes])
1119
1120 if ctype in ("auto", "auto_once"):
1121 for kls in _cards_classes:
1122 card = kls.autodetect(scc)
1123 if card:
1124 print("Autodetected card type: %s" % card.name)
1125 card.reset()
1126 break
1127
1128 if card is None:
1129 print("Autodetection failed")
1130 return None
1131
1132 if ctype == "auto_once":
1133 ctype = card.name
1134
1135 elif ctype in ctypes:
1136 card = ctypes[ctype](scc)
1137
1138 else:
1139 raise ValueError("Unknown card type: %s" % ctype)
1140
1141 return card