blob: 1a78a12ad41a663f1f9cb33f8f26b83f64a6ca69 [file] [log] [blame]
Supreeth Herle475dcaa2020-03-20 18:57:39 +01001# -*- coding: utf-8 -*-
2
3"""
4Various constants from ETSI TS 131 102
5"""
6
7#
8# Copyright (C) 2020 Supreeth Herle <herlesupreeth@gmail.com>
Harald Welte790b2702021-04-11 00:01:35 +02009# Copyright (C) 2021 Harald Welte <laforge@osmocom.org>
Supreeth Herle475dcaa2020-03-20 18:57:39 +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
25# Mapping between USIM Service Number and its description
26EF_UST_map = {
27 1: 'Local Phone Book',
28 2: 'Fixed Dialling Numbers (FDN)',
29 3: 'Extension 2',
30 4: 'Service Dialling Numbers (SDN)',
31 5: 'Extension3',
32 6: 'Barred Dialling Numbers (BDN)',
33 7: 'Extension4',
34 8: 'Outgoing Call Information (OCI and OCT)',
35 9: 'Incoming Call Information (ICI and ICT)',
36 10: 'Short Message Storage (SMS)',
37 11: 'Short Message Status Reports (SMSR)',
38 12: 'Short Message Service Parameters (SMSP)',
39 13: 'Advice of Charge (AoC)',
40 14: 'Capability Configuration Parameters 2 (CCP2)',
41 15: 'Cell Broadcast Message Identifier',
42 16: 'Cell Broadcast Message Identifier Ranges',
43 17: 'Group Identifier Level 1',
44 18: 'Group Identifier Level 2',
45 19: 'Service Provider Name',
46 20: 'User controlled PLMN selector with Access Technology',
47 21: 'MSISDN',
48 22: 'Image (IMG)',
49 23: 'Support of Localised Service Areas (SoLSA)',
50 24: 'Enhanced Multi-Level Precedence and Pre-emption Service',
51 25: 'Automatic Answer for eMLPP',
52 26: 'RFU',
53 27: 'GSM Access',
54 28: 'Data download via SMS-PP',
55 29: 'Data download via SMS-CB',
56 30: 'Call Control by USIM',
57 31: 'MO-SMS Control by USIM',
58 32: 'RUN AT COMMAND command',
59 33: 'shall be set to 1',
60 34: 'Enabled Services Table',
61 35: 'APN Control List (ACL)',
62 36: 'Depersonalisation Control Keys',
63 37: 'Co-operative Network List',
64 38: 'GSM security context',
65 39: 'CPBCCH Information',
66 40: 'Investigation Scan',
67 41: 'MexE',
68 42: 'Operator controlled PLMN selector with Access Technology',
69 43: 'HPLMN selector with Access Technology',
70 44: 'Extension 5',
71 45: 'PLMN Network Name',
72 46: 'Operator PLMN List',
73 47: 'Mailbox Dialling Numbers',
74 48: 'Message Waiting Indication Status',
75 49: 'Call Forwarding Indication Status',
76 50: 'Reserved and shall be ignored',
77 51: 'Service Provider Display Information',
78 52: 'Multimedia Messaging Service (MMS)',
79 53: 'Extension 8',
80 54: 'Call control on GPRS by USIM',
81 55: 'MMS User Connectivity Parameters',
82 56: 'Network\'s indication of alerting in the MS (NIA)',
83 57: 'VGCS Group Identifier List (EFVGCS and EFVGCSS)',
84 58: 'VBS Group Identifier List (EFVBS and EFVBSS)',
85 59: 'Pseudonym',
86 60: 'User Controlled PLMN selector for I-WLAN access',
87 61: 'Operator Controlled PLMN selector for I-WLAN access',
88 62: 'User controlled WSID list',
89 63: 'Operator controlled WSID list',
90 64: 'VGCS security',
91 65: 'VBS security',
92 66: 'WLAN Reauthentication Identity',
93 67: 'Multimedia Messages Storage',
94 68: 'Generic Bootstrapping Architecture (GBA)',
95 69: 'MBMS security',
96 70: 'Data download via USSD and USSD application mode',
97 71: 'Equivalent HPLMN',
98 72: 'Additional TERMINAL PROFILE after UICC activation',
99 73: 'Equivalent HPLMN Presentation Indication',
100 74: 'Last RPLMN Selection Indication',
101 75: 'OMA BCAST Smart Card Profile',
102 76: 'GBA-based Local Key Establishment Mechanism',
103 77: 'Terminal Applications',
104 78: 'Service Provider Name Icon',
105 79: 'PLMN Network Name Icon',
106 80: 'Connectivity Parameters for USIM IP connections',
107 81: 'Home I-WLAN Specific Identifier List',
108 82: 'I-WLAN Equivalent HPLMN Presentation Indication',
109 83: 'I-WLAN HPLMN Priority Indication',
110 84: 'I-WLAN Last Registered PLMN',
111 85: 'EPS Mobility Management Information',
112 86: 'Allowed CSG Lists and corresponding indications',
113 87: 'Call control on EPS PDN connection by USIM',
114 88: 'HPLMN Direct Access',
115 89: 'eCall Data',
116 90: 'Operator CSG Lists and corresponding indications',
117 91: 'Support for SM-over-IP',
118 92: 'Support of CSG Display Control',
119 93: 'Communication Control for IMS by USIM',
120 94: 'Extended Terminal Applications',
121 95: 'Support of UICC access to IMS',
122 96: 'Non-Access Stratum configuration by USIM',
123 97: 'PWS configuration by USIM',
124 98: 'RFU',
125 99: 'URI support by UICC',
126 100: 'Extended EARFCN support',
127 101: 'ProSe',
128 102: 'USAT Application Pairing',
129 103: 'Media Type support',
130 104: 'IMS call disconnection cause',
131 105: 'URI support for MO SHORT MESSAGE CONTROL',
132 106: 'ePDG configuration Information support',
133 107: 'ePDG configuration Information configured',
134 108: 'ACDC support',
135 109: 'MCPTT',
136 110: 'ePDG configuration Information for Emergency Service support',
137 111: 'ePDG configuration Information for Emergency Service configured',
Supreeth Herle4d9e6be2020-03-24 12:13:45 +0100138 112: 'eCall Data over IMS',
139 113: 'URI support for SMS-PP DOWNLOAD as defined in 3GPP TS 31.111 [12]',
140 114: 'From Preferred',
141 115: 'IMS configuration data',
142 116: 'TV configuration',
143 117: '3GPP PS Data Off',
144 118: '3GPP PS Data Off Service List',
145 119: 'V2X',
146 120: 'XCAP Configuration Data',
147 121: 'EARFCN list for MTC/NB-IOT UEs',
148 122: '5GS Mobility Management Information',
149 123: '5G Security Parameters',
150 124: 'Subscription identifier privacy support',
151 125: 'SUCI calculation by the USIM',
152 126: 'UAC Access Identities support',
153 127: 'Expect control plane-based Steering of Roaming information during initial registration in VPLMN',
154 128: 'Call control on PDU Session by USIM',
Sebastian Viviani0dc8f692020-05-29 00:14:55 +0100155}
156
Sebastian Vivianie61170c2020-06-03 08:57:00 +0100157LOCI_STATUS_map = {
158 0: 'updated',
159 1: 'not updated',
160 2: 'plmn not allowed',
161 3: 'locatation area not allowed'
162}
herlesupreetha562ea02020-09-16 20:17:22 +0200163
Sebastian Viviani0dc8f692020-05-29 00:14:55 +0100164EF_USIM_ADF_map = {
165 'LI': '6F05',
166 'ARR': '6F06',
167 'IMSI': '6F07',
168 'Keys': '6F08',
169 'KeysPS': '6F09',
170 'DCK': '6F2C',
171 'HPPLMN': '6F31',
172 'CNL': '6F32',
173 'ACMmax': '6F37',
174 'UST': '6F38',
175 'ACM': '6F39',
176 'FDN': '6F3B',
177 'SMS': '6F3C',
178 'GID1': '6F3E',
179 'GID2': '6F3F',
180 'MSISDN': '6F40',
181 'PUCT': '6F41',
182 'SMSP': '6F42',
183 'SMSS': '6F42',
184 'CBMI': '6F45',
185 'SPN': '6F46',
186 'SMSR': '6F47',
187 'CBMID': '6F48',
188 'SDN': '6F49',
189 'EXT2': '6F4B',
190 'EXT3': '6F4C',
191 'BDN': '6F4D',
192 'EXT5': '6F4E',
193 'CCP2': '6F4F',
194 'CBMIR': '6F50',
195 'EXT4': '6F55',
196 'EST': '6F56',
197 'ACL': '6F57',
198 'CMI': '6F58',
199 'START-HFN': '6F5B',
200 'THRESHOLD': '6F5C',
201 'PLMNwAcT': '6F60',
202 'OPLMNwAcT': '6F61',
203 'HPLMNwAcT': '6F62',
204 'PSLOCI': '6F73',
205 'ACC': '6F78',
206 'FPLMN': '6F7B',
207 'LOCI': '6F7E',
208 'ICI': '6F80',
209 'OCI': '6F81',
210 'ICT': '6F82',
211 'OCT': '6F83',
212 'AD': '6FAD',
213 'VGCS': '6FB1',
214 'VGCSS': '6FB2',
215 'VBS': '6FB3',
216 'VBSS': '6FB4',
217 'eMLPP': '6FB5',
218 'AAeM': '6FB6',
219 'ECC': '6FB7',
220 'Hiddenkey': '6FC3',
221 'NETPAR': '6FC4',
222 'PNN': '6FC5',
223 'OPL': '6FC6',
224 'MBDN': '6FC7',
225 'EXT6': '6FC8',
226 'MBI': '6FC9',
227 'MWIS': '6FCA',
228 'CFIS': '6FCB',
229 'EXT7': '6FCC',
230 'SPDI': '6FCD',
231 'MMSN': '6FCE',
232 'EXT8': '6FCF',
233 'MMSICP': '6FD0',
234 'MMSUP': '6FD1',
235 'MMSUCP': '6FD2',
236 'NIA': '6FD3',
237 'VGCSCA': '6FD4',
238 'VBSCA': '6FD5',
239 'GBAP': '6FD6',
240 'MSK': '6FD7',
241 'MUK': '6FD8',
242 'EHPLMN': '6FD9',
243 'GBANL': '6FDA',
244 'EHPLMNPI': '6FDB',
245 'LRPLMNSI': '6FDC',
246 'NAFKCA': '6FDD',
247 'SPNI': '6FDE',
248 'PNNI': '6FDF',
249 'NCP-IP': '6FE2',
250 'EPSLOCI': '6FE3',
251 'EPSNSC': '6FE4',
252 'UFC': '6FE6',
253 'UICCIARI': '6FE7',
254 'NASCONFIG': '6FE8',
255 'PWC': '6FEC',
256 'FDNURI': '6FED',
257 'BDNURI': '6FEE',
258 'SDNURI': '6FEF',
259 'IWL': '6FF0',
260 'IPS': '6FF1',
261 'IPD': '6FF2',
262 'ePDGId': '6FF3',
263 'ePDGSelection': '6FF4',
264 'ePDGIdEm': '6FF5',
265 'ePDGSelectionEm': '6FF6',
266}
Harald Welteb2edd142021-01-08 23:29:35 +0100267
268######################################################################
269# ADF.USIM
270######################################################################
271
Harald Welte15fae982021-04-10 10:22:27 +0200272from struct import unpack, pack
Harald Welte9853e242021-04-10 19:08:25 +0200273from construct import *
Harald Welte790b2702021-04-11 00:01:35 +0200274from pySim.construct import LV, HexAdapter, BcdAdapter
Harald Welteb2edd142021-01-08 23:29:35 +0100275from pySim.filesystem import *
Harald Welte790b2702021-04-11 00:01:35 +0200276from pySim.ts_102_221 import EF_ARR
Harald Welteb2edd142021-01-08 23:29:35 +0100277from pySim.ts_51_011 import EF_IMSI, EF_xPLMNwAcT, EF_SPN, EF_CBMI, EF_ACC, EF_PLMNsel, EF_AD
Harald Welte790b2702021-04-11 00:01:35 +0200278from pySim.ts_51_011 import EF_CBMID, EF_CBMIR, EF_ADN, EF_SMS, EF_MSISDN, EF_SMSP, EF_SMSS
279from pySim.ts_51_011 import EF_SMSR, EF_DCK, EF_EXT, EF_CNL, EF_OPL, EF_MBI, EF_MWIS
280from pySim.ts_51_011 import EF_MMSN, EF_MMSICP, EF_MMSUP, EF_MMSUCP, EF_VGCS, EF_VGCSS, EF_NIA
281from pySim.ts_51_011 import EF_ACMmax, EF_AAeM, EF_eMLPP, EF_CMI
Harald Welteb2edd142021-01-08 23:29:35 +0100282
283import pySim.ts_102_221
284
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200285# TS 31.102 4.4.11.8
286class EF_SUCI_Calc_Info(TransparentEF):
287 def __init__(self, fid="4f07", sfid=0x07, name='EF.SUCI_Calc_Info', size={2, None},
288 desc='SUCI Calc Info'):
289 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
290
291 def _encode_prot_scheme_id_list(self, in_list):
292 out_bytes = [0xa0]
293 out_bytes.append(len(in_list)*2) # two byte per entry
294
295 # position in list determines priority; high-priority items (low index) come first
296 for scheme in sorted(in_list, key=lambda item: item["priority"]):
297 out_bytes.append(scheme["identifier"])
298 out_bytes.append(scheme["key_index"])
299
300 return out_bytes
301
302 def _encode_hnet_pubkey_list(self, hnet_pubkey_list):
303 out_bytes = [0xa1] # pubkey list tag
304 out_bytes.append(0x00) # length filled later
305 length = 0
306
307 for key in hnet_pubkey_list:
308 out_bytes.append(0x80) # identifier tag
309 out_bytes.append(0x01) # TODO size, fixed to 1 byte
310 out_bytes.append(key["hnet_pubkey_identifier"])
311 out_bytes.append(0x81) # key tag
312 out_bytes.append(len(key["hnet_pubkey"])//2)
313 length += 5+len(key["hnet_pubkey"])//2
314
315 pubkey_bytes = h2b(key["hnet_pubkey"])
316 out_bytes += pubkey_bytes
317
318 # fill length
319 out_bytes[1] = length
320 return out_bytes
321
322 def _encode_hex(self, in_json):
323 out_bytes = self._encode_prot_scheme_id_list(in_json['prot_scheme_id_list'])
324 out_bytes += self._encode_hnet_pubkey_list(in_json['hnet_pubkey_list'])
325 return "".join(["%02X" % i for i in out_bytes])
326
327 def _decode_prot_scheme_id_list(self, in_bytes):
328 prot_scheme_id_list = []
329 pos = 0
330 # two bytes per entry
331 while pos < len(in_bytes):
332 prot_scheme = {
333 'priority': pos//2, # first in list: high priority
334 'identifier': in_bytes[pos],
335 'key_index': in_bytes[pos+1]
336 }
337 pos += 2
338 prot_scheme_id_list.append(prot_scheme)
339 return prot_scheme_id_list
340
341 def _decode_hnet_pubkey_list(self, in_bytes):
342 hnet_pubkey_list = []
343 pos = 0
344 if in_bytes[pos] != 0xa1:
345 print("missing Home Network Public Key List data object")
346 return {}
347 pos += 1
348 hnet_pubkey_list_len = in_bytes[pos]
349 pos += 1
350
351 while pos < hnet_pubkey_list_len:
352 if in_bytes[pos] != 0x80:
353 print("missing Home Network Public Key Identifier tag")
354 return {}
355 pos += 1
356 hnet_pubkey_id_len = in_bytes[pos] # TODO might be more than 1 byte?
357 pos += 1
358 hnet_pubkey_id = in_bytes[pos:pos+hnet_pubkey_id_len][0]
359 pos += hnet_pubkey_id_len
360 if in_bytes[pos] != 0x81:
361 print("missing Home Network Public Key tag")
362 return {}
363 pos += 1
364 hnet_pubkey_len = in_bytes[pos]
365 pos += 1
366 hnet_pubkey = in_bytes[pos:pos+hnet_pubkey_len]
367 pos += hnet_pubkey_len
368
369 hnet_pubkey_list.append({
370 'hnet_pubkey_identifier': hnet_pubkey_id,
371 'hnet_pubkey': b2h(hnet_pubkey)
372 })
373
374 return hnet_pubkey_list
375
376 def _decode_bin(self, in_bin):
377 return self._decode_hex(b2h(in_hex))
378
379 def _decode_hex(self, in_hex):
380 in_bytes = h2b(in_hex)
381 pos = 0
382
383 if in_bytes[pos] != 0xa0:
384 print("missing Protection Scheme Identifier List data object tag")
385 return {}
386 pos += 1
387
388 prot_scheme_id_list_len = in_bytes[pos] # TODO maybe more than 1 byte
389 pos += 1
390 # decode Protection Scheme Identifier List data object
391 prot_scheme_id_list = self._decode_prot_scheme_id_list(in_bytes[pos:pos+prot_scheme_id_list_len])
392 pos += prot_scheme_id_list_len
393
394 # remaining data holds Home Network Public Key Data Object
395 hnet_pubkey_list = self._decode_hnet_pubkey_list(in_bytes[pos:])
396
397 return {
398 'prot_scheme_id_list': prot_scheme_id_list,
399 'hnet_pubkey_list': hnet_pubkey_list
400 }
401
402 def _encode_bin(self, in_json):
403 return h2b(self._encode_hex(in_json))
404
Harald Welteb2edd142021-01-08 23:29:35 +0100405class EF_LI(TransRecEF):
406 def __init__(self, fid='6f05', sfid=None, name='EF.LI', size={2,None}, rec_len=2,
407 desc='Language Indication'):
408 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
409 def _decode_record_bin(self, in_bin):
410 if in_bin == b'\xff\xff':
411 return None
412 else:
413 # officially this is 7-bit GSM alphabet with one padding bit in each byte
414 return in_bin.decode('ascii')
415 def _encode_record_bin(self, in_json):
416 if in_json == None:
417 return b'\xff\xff'
418 else:
419 # officially this is 7-bit GSM alphabet with one padding bit in each byte
420 return in_json.encode('ascii')
421
422class EF_Keys(TransparentEF):
423 def __init__(self, fid='6f08', sfid=0x08, name='EF.Keys', size={33,33},
424 desc='Ciphering and Integrity Keys'):
425 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Harald Welte9853e242021-04-10 19:08:25 +0200426 self._construct = Struct('ksi'/Int8ub, 'ck'/HexAdapter(Bytes(16)), 'ik'/HexAdapter(Bytes(16)))
Harald Welteb2edd142021-01-08 23:29:35 +0100427
428# TS 31.103 Section 4.2.7
429class EF_UST(TransparentEF):
Harald Welte790b2702021-04-11 00:01:35 +0200430 def __init__(self, fid='6f38', sfid=0x04, name='EF.UST', desc='USIM Service Table', size={1,17}):
431 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, size=size)
Harald Welteb2edd142021-01-08 23:29:35 +0100432 # add those commands to the general commands of a TransparentEF
433 self.shell_commands += [self.AddlShellCommands()]
434 def _decode_bin(self, in_bin):
435 ret = []
436 for i in range (0, len(in_bin)):
437 byte = in_bin[i]
438 for bitno in range(0,7):
439 if byte & (1 << bitno):
440 ret.append(i * 8 + bitno + 1)
441 return ret
442 def _encode_bin(self, in_json):
443 # FIXME: size this to length of file
444 ret = bytearray(20)
445 for srv in in_json:
446 print("srv=%d"%srv)
447 srv = srv-1
448 byte_nr = srv // 8
449 # FIXME: detect if service out of range was selected
450 bit_nr = srv % 8
451 ret[byte_nr] |= (1 << bit_nr)
452 return ret
453 @with_default_category('File-Specific Commands')
454 class AddlShellCommands(CommandSet):
455 def __init__(self):
456 super().__init__()
457
458 def do_ust_service_activate(self, arg):
459 """Activate a service within EF.UST"""
460 self._cmd.card.update_ust(int(arg), 1)
461
462 def do_ust_service_deactivate(self, arg):
463 """Deactivate a service within EF.UST"""
464 self._cmd.card.update_ust(int(arg), 0)
465
Harald Welte89e59542021-04-02 21:33:13 +0200466# TS 31.103 Section 4.2.7 - *not* the same as DF.GSM/EF.ECC!
467class EF_ECC(LinFixedEF):
468 def __init__(self, fid='6fb7', sfid=0x01, name='EF.ECC',
469 desc='Emergency Call Codes'):
470 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len={4,20})
471
Harald Welte790b2702021-04-11 00:01:35 +0200472# TS 31.102 Section 4.2.17
473class EF_LOCI(TransparentEF):
Harald Weltec9cdce32021-04-11 10:28:28 +0200474 def __init__(self, fid='6f7e', sfid=0x0b, name='EF.LOCI', desc='Location information', size={11,11}):
Harald Welte790b2702021-04-11 00:01:35 +0200475 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
476 self._construct = Struct('tmsi'/HexAdapter(Bytes(4)), 'lai'/HexAdapter(Bytes(5)), 'rfu'/Int8ub,
477 'lu_status'/Int8ub)
478
479# TS 31.102 Section 4.2.23
480class EF_PSLOCI(TransparentEF):
Harald Weltec9cdce32021-04-11 10:28:28 +0200481 def __init__(self, fid='6f73', sfid=0x0c, name='EF.PSLOCI', desc='PS Location information', size={14,14}):
Harald Welte790b2702021-04-11 00:01:35 +0200482 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
483 self._construct = Struct('ptmsi'/HexAdapter(Bytes(4)), 'ptmsi_sig'/HexAdapter(Bytes(3)),
484 'rai'/HexAdapter(Bytes(6)), 'rau_status'/Int8ub)
485
486# TS 31.102 Section 4.2.33
487class EF_ICI(CyclicEF):
488 def __init__(self, fid='6f80', sfid=0x14, name='EF.ICI', rec_len={28,48},
489 desc='Incoming Call Information'):
490 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len)
491 self._construct = Struct('alpha_id'/Bytes(this._.total_len-28),
492 'len_of_bcd_contents'/Int8ub,
493 'ton_npi'/Int8ub,
494 'call_number'/BcdAdapter(Bytes(10)),
495 'cap_cfg2_record_id'/Int8ub,
496 'ext5_record_id'/Int8ub,
497 'date_and_time'/BcdAdapter(Bytes(7)),
498 'duration'/Int24ub,
499 'status'/Byte,
500 'link_to_phonebook'/Bytes(3))
501
502# TS 31.102 Section 4.2.34
503class EF_OCI(CyclicEF):
504 def __init__(self, fid='6f81', sfid=0x15, name='EF.OCI', rec_len={27,47},
505 desc='Outgoing Call Information'):
506 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len)
507 self._construct = Struct('alpha_id'/Bytes(this._.total_len-27),
508 'len_of_bcd_contents'/Int8ub,
509 'ton_npi'/Int8ub,
510 'call_number'/BcdAdapter(Bytes(10)),
511 'cap_cfg2_record_id'/Int8ub,
512 'ext5_record_id'/Int8ub,
513 'date_and_time'/BcdAdapter(Bytes(7)),
514 'duration'/Int24ub,
515 'link_to_phonebook'/Bytes(3))
516
517# TS 31.102 Section 4.2.35
518class EF_ICT(CyclicEF):
519 def __init__(self, fid='6f82', sfid=None, name='EF.ICT', rec_len={3,3},
520 desc='Incoming Call Timer'):
521 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len)
522 self._construct = Struct('accumulated_call_timer'/Int24ub)
523
524# TS 31.102 Section 4.2.38
525class EF_CCP2(LinFixedEF):
526 def __init__(self, fid='6f4f', sfid=0x16, name='EF.CCP2', desc='Capability Configuration Parameters 2'):
527 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len={15,None})
528
529# TS 31.102 Section 4.2.48
530class EF_ACL(TransparentEF):
531 def __init__(self, fid='6f57', sfid=None, name='EF.ACL', size={32,None},
532 desc='Access Point Name Control List'):
533 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
534 self._construct = Struct('num_of_apns'/Int8ub, 'tlvs'/GreedyBytes)
535
536# TS 31.102 Section 4.2.51
537class EF_START_HFN(TransparentEF):
538 def __init__(self, fid='6f5b', sfid=0x0f, name='EF.START-HFN', size={6,6},
539 desc='Initialisation values for Hyperframe number'):
540 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
541 self._construct = Struct('start_cs'/Int24ub, 'start_ps'/Int24ub)
542
543# TS 31.102 Section 4.2.52
544class EF_THRESHOLD(TransparentEF):
545 def __init__(self, fid='6f5c', sfid=0x10, name='EF.THRESHOLD', size={3,3},
546 desc='Maximum value of START'):
547 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
548 self._construct = Struct('max_start'/Int24ub)
549
550# TS 31.102 Section 4.2.77
551class EF_VGCSCA(TransRecEF):
552 def __init__(self, fid='6fd4', sfid=None, name='EF.VGCSCA', size={2,100}, rec_len=2,
553 desc='Voice Group Call Service Ciphering Algorithm'):
554 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
555 self._construct = Struct('alg_v_ki_1'/Int8ub, 'alg_v_ki_2'/Int8ub)
556
557# TS 31.102 Section 4.2.79
558class EF_GBABP(TransparentEF):
559 def __init__(self, fid='6fd6', sfid=None, name='EF.GBABP', size={3,50},
560 desc='GBA Bootstrapping parameters'):
561 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
562 self._construct = Struct('rand'/LV, 'b_tid'/LV, 'key_lifetime'/LV)
563
564# TS 31.102 Section 4.2.80
565class EF_MSK(LinFixedEF):
566 def __init__(self, fid='6fd7', sfid=None, name='EF.MSK', desc='MBMS Service Key List'):
567 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len={20,None})
568 msk_ts_constr = Struct('msk_id'/Int32ub, 'timestamp_counter'/Int32ub)
569 self._construct = Struct('key_domain_id'/Bytes(3),
570 'num_msk_id'/Int8ub,
571 'msk_ids'/msk_ts_constr[this.num_msk_id])
572
573# TS 31.102 Section 4.2.85
574class EF_EHPLMNPI(TransparentEF):
575 def __init__(self, fid='6fdb', sfid=None, name='EF.EHPLMNPI', size={1,1},
576 desc='Equivalent HPLMN Presentation Indication'):
577 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
578 self._construct = Struct('presentation_ind'/
579 Enum(Byte, no_preference=0, display_highest_prio_only=1, display_all=2))
580# TS 31.102 Section 4.2.91
581class EF_EPSLOCI(TransparentEF):
582 def __init__(self, fid='6fe3', sfid=0x1e, name='EF.EPSLOCI', size={18,18},
583 desc='EPS Location Information'):
584 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
585 upd_status_constr = Enum(Byte, updated=0, not_updated=1, roaming_not_allowed=2)
586 self._construct = Struct('guti'/Bytes(12), 'last_visited_registered_tai'/Bytes(5),
587 'eps_update_status'/upd_status_constr)
588
589# TS 31.102 Section 4.2.96
590class EF_PWS(TransparentEF):
591 def __init__(self, fid='6fec', sfid=None, name='EF.PWS', desc='Public Warning System', size={1,1}):
592 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
593 pws_config = FlagsEnum(Byte, ignore_pws_in_hplmn_and_equivalent=1, ignore_pws_in_vplmn=2)
594 self._construct = Struct('pws_configuration'/pws_config)
595
596# TS 31.102 Section 4.2.101
597class EF_IPS(CyclicEF):
598 def __init__(self, fid='6ff1', sfid=None, name='EF.IPS', rec_len={4,4},
599 desc='IMEI(SV) Pairing Status'):
600 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len)
601 self._construct = Struct('status'/PaddedString(2, 'ascii'),
602 'link_to_ef_ipd'/Int8ub, 'rfu'/Byte)
603
604######################################################################
605# DF.5GS
606######################################################################
607
608# TS 31.102 Section 4.4.11.2
609class EF_5GS3GPPLOCI(TransparentEF):
610 def __init__(self, fid='4f01', sfid=0x01, name='EF.5GS3GPPLOCI', size={20,20},
611 desc='5S 3GP location information'):
612 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
613 upd_status_constr = Enum(Byte, updated=0, not_updated=1, roaming_not_allowed=2)
614 self._construct = Struct('5g_guti'/Bytes(13), 'last_visited_registered_tai_in_5gs'/Bytes(6),
615 '5gs_update_status'/upd_status_constr)
616
617# TS 31.102 Section 4.4.11.7
618class EF_UAC_AIC(TransparentEF):
619 def __init__(self, fid='4f06', sfid=0x06, name='EF.UAC_AIC', size={4,4},
620 desc='UAC Access Identities Configuration'):
621 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
622 cfg_constr = FlagsEnum(Byte, multimedia_priority_service=1,
623 mission_critical_service=2)
624 self._construct = Struct('uac_access_id_config'/cfg_constr)
625
626# TS 31.102 Section 4.4.11.10
627class EF_OPL5G(LinFixedEF):
628 def __init__(self, fid='6f08', sfid=0x08, name='EF.OPL5G', desc='5GS Operator PLMN List'):
629 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len={10,None})
630 self._construct = Struct('tai'/Bytes(9), 'pnn_record_id'/Int8ub)
631
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200632class DF_USIM_5GS(CardDF):
633 def __init__(self, fid='5FC0', name='DF.5GS', desc='5GS related files'):
634 super().__init__(fid=fid, name=name, desc=desc)
635 files = [
636 # I'm looking at 31.102 R15.9
Harald Welte790b2702021-04-11 00:01:35 +0200637 EF_5GS3GPPLOCI(),
638 EF_5GS3GPPLOCI('4f02', 0x02, 'EF.5GSN3GPPLOCI', '5GS non-3GPP location information'),
639 LinFixedEF('4F03', 0x03, 'EF.5GS3GPPNSC', '5GS 3GPP Access NAS Security Context', rec_len={57,None}),
640 LinFixedEF('4F04', 0x04, 'EF.5GSN3GPPNSC', '5GS non-3GPP Access NAS Security Context', rec_len={57,None}),
641 TransparentEF('4F05', 0x05, 'EF.5GAUTHKEYS', '5G authentication keys', size={68, None}),
642 EF_UAC_AIC(),
643 EF_SUCI_Calc_Info(),
644 EF_OPL5G(),
645 TransparentEF('4F09', 0x09, 'EF.NSI', 'Network Specific Identifier'),
646 TransparentEF('4F0A', 0x0a, 'EF.Routing_Indicator', 'Routing Indicator', size={4,4}),
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200647 ]
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200648 self.add_files(files)
649
Harald Welteb2edd142021-01-08 23:29:35 +0100650class ADF_USIM(CardADF):
651 def __init__(self, aid='a0000000871002', name='ADF.USIM', fid=None, sfid=None,
652 desc='USIM Application'):
653 super().__init__(aid=aid, fid=fid, sfid=sfid, name=name, desc=desc)
Harald Welte15fae982021-04-10 10:22:27 +0200654 # add those commands to the general commands of a TransparentEF
655 self.shell_commands += [self.AddlShellCommands()]
Harald Welteb2edd142021-01-08 23:29:35 +0100656
657 files = [
658 EF_LI(sfid=0x02),
659 EF_IMSI(sfid=0x07),
660 EF_Keys(),
661 EF_Keys('6f09', 0x09, 'EF.KeysPS', desc='Ciphering and Integrity Keys for PS domain'),
662 EF_xPLMNwAcT('6f60', 0x0a, 'EF.PLMNwAcT',
663 'User controlled PLMN Selector with Access Technology'),
664 TransparentEF('6f31', 0x12, 'EF.HPPLMN', 'Higher Priority PLMN search period'),
Harald Welte790b2702021-04-11 00:01:35 +0200665 EF_ACMmax(),
Harald Welteb2edd142021-01-08 23:29:35 +0100666 EF_UST(),
667 CyclicEF('6f39', None, 'EF.ACM', 'Accumulated call meter', rec_len={3,3}),
668 TransparentEF('6f3e', None, 'EF.GID1', 'Group Identifier Level 1'),
669 TransparentEF('6f3f', None, 'EF.GID2', 'Group Identifier Level 2'),
670 EF_SPN(),
671 TransparentEF('6f41', None, 'EF.PUCT', 'Price per unit and currency table', size={5,5}),
672 EF_CBMI(),
673 EF_ACC(sfid=0x06),
674 EF_PLMNsel('6f7b', 0x0d, 'EF.FPLMN', 'Forbidden PLMNs', size={12,None}),
Harald Welte790b2702021-04-11 00:01:35 +0200675 EF_LOCI(),
Harald Welteb2edd142021-01-08 23:29:35 +0100676 EF_AD(sfid=0x03),
677 EF_CBMID(sfid=0x0e),
Harald Welte89e59542021-04-02 21:33:13 +0200678 EF_ECC(),
Harald Welteb2edd142021-01-08 23:29:35 +0100679 EF_CBMIR(),
Harald Welte790b2702021-04-11 00:01:35 +0200680 EF_PSLOCI(),
681 EF_ADN('6f3b', None, 'EF.FDN', 'Fixed Dialling Numbers'),
682 EF_SMS('6f3c', None),
683 EF_MSISDN(),
684 EF_SMSP(),
685 EF_SMSS(),
686 EF_ADN('6f49', None, 'EF.SDN', 'Service Dialling Numbers'),
687 EF_EXT('6f4b', None, 'EF.EXT2', 'Extension2 (FDN)'),
688 EF_EXT('6f4c', None, 'EF.EXT3', 'Extension2 (SDN)'),
689 EF_SMSR(),
690 EF_ICI(),
691 EF_OCI(),
692 EF_ICT(),
693 EF_ICT('6f83', None, 'EF.OCT', 'Outgoing Call Timer'),
694 EF_EXT('6f4e', None, 'EF.EXT5', 'Extension5 (ICI/OCI/MSISDN)'),
695 EF_CCP2(),
696 EF_eMLPP(),
697 EF_AAeM(),
698 # EF_Hiddenkey
699 EF_ADN('6f4d', None, 'EF.BDN', 'Barred Dialling Numbers'),
700 EF_EXT('6f55', None, 'EF.EXT4', 'Extension4 (BDN/SSC)'),
701 EF_CMI(),
702 EF_UST('6f56', 0x05, 'EF.EST', 'Enabled Services Table', size={1,None}),
703 EF_ACL(),
704 EF_DCK(),
705 EF_CNL(),
706 EF_START_HFN(),
707 EF_THRESHOLD(),
708 EF_xPLMNwAcT('6f61', 0x11, 'EF.OPLMNwAcT',
709 'User controlled PLMN Selector with Access Technology'),
710 EF_ARR('6f06', 0x17),
711 TransparentEF('6fc4', None, 'EF.NETPAR', 'Network Parameters'),
712 LinFixedEF('6fc5', 0x19, 'EF.PNN', 'PLMN Network Name'),
713 EF_OPL(),
714 EF_ADN('6fc7', None, 'EF.MBDN', 'Mailbox Dialling Numbers'),
715 EF_MBI(),
716 EF_MWIS(),
717 EF_ADN('6fcb', None, 'EF.CFIS', 'Call Forwarding Indication Status'),
718 EF_EXT('6fcc', None, 'EF.EXT7', 'Extension7 (CFIS)'),
719 TransparentEF('6fcd', None, 'EF.SPDI', 'Service Provider Display Information'),
720 EF_MMSN(),
721 EF_EXT('6fcf', None, 'EF.EXT8', 'Extension8 (MMSN)'),
722 EF_MMSICP(),
723 EF_MMSUP(),
724 EF_MMSUCP(),
725 EF_NIA(),
726 EF_VGCS(),
727 EF_VGCSS(),
728 EF_VGCS('6fb3', None, 'EF.VBS', 'Voice Broadcast Service'),
729 EF_VGCSS('6fb4', None, 'EF.VBSS', 'Voice Broadcast Service Status'),
730 EF_VGCSCA(),
731 EF_VGCSCA('6fd5', None, 'EF.VBCSCA', 'Voice Broadcast Service Ciphering Algorithm'),
732 EF_GBABP(),
733 EF_MSK(),
734 LinFixedEF('6fd8', None, 'EF.MUK', 'MBMS User Key'),
735 LinFixedEF('6fda', None, 'EF.GBANL', 'GBA NFA List'),
736 EF_PLMNsel('6fd9', 0x1d, 'EF.EHPLMN', 'Equivalent HPLMN', size={12,None}),
737 EF_EHPLMNPI(),
738 LinFixedEF('6fdd', None, 'EF.NAFKCA', 'NAF Key Centre Address'),
739 TransparentEF('6fde', None, 'EF.SPNI', 'Service Provider Name Icon'),
740 LinFixedEF('6fdf', None, 'EF.PNNI', 'PLMN Network Name Icon'),
741 LinFixedEF('6fe2', None, 'EF.NCP-IP', 'Network Connectivity Parameters for USIM IP connections'),
742 EF_EPSLOCI('6fe3', 0x1e, 'EF.EPSLOCI', 'EPS location information'),
743 LinFixedEF('6fe4', 0x18, 'EF.EPSNSC', 'EPS NAS Security Context', rec_len={54,128}),
744 TransparentEF('6fe6', None, 'EF.UFC', 'USAT Facility Control', size={1,16}),
745 TransparentEF('6fe8', None, 'EF.NASCONFIG', 'Non Access Stratum Configuration'),
746 # UICC IARI (only in cards that have no ISIM)
747 EF_PWS(),
748 LinFixedEF('6fed', None, 'EF.FDNURI', 'Fixed Dialling Numbers URI'),
749 LinFixedEF('6fee', None, 'EF.BDNURI', 'Barred Dialling Numbers URI'),
750 LinFixedEF('6fef', None, 'EF.SDNURI', 'Service Dialling Numbers URI'),
751 EF_IPS(),
752 # FIXME: from EF_ePDGid onwards
753 # FIXME: DF_SoLSA
754 # FIXME: DF_PHONEBOOK
755 # FIXME: DF_GSM_ACCESS
756 # FIXME: DF_WLAN
757 # FIXME: DF_HNB
758 # FIXME: DF_ProSe
759 # FIXME: DF_ACDC
760 # FIXME: DF_TV
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200761 DF_USIM_5GS(),
Harald Welteb2edd142021-01-08 23:29:35 +0100762 ]
763 self.add_files(files)
764
765 def decode_select_response(self, data_hex):
766 return pySim.ts_102_221.decode_select_response(data_hex)
767
Harald Welte15fae982021-04-10 10:22:27 +0200768 @with_default_category('Application-Specific Commands')
769 class AddlShellCommands(CommandSet):
770 def __init__(self):
771 super().__init__()
772
773 authenticate_parser = argparse.ArgumentParser()
774 authenticate_parser.add_argument('rand', help='Random challenge')
775 authenticate_parser.add_argument('autn', help='Authentication Nonce')
776 #authenticate_parser.add_argument('--context', help='Authentication context', default='3G')
777 @cmd2.with_argparser(authenticate_parser)
778 def do_authenticate(self, opts):
779 """Perform Authentication and Key Agreement (AKA)."""
780 (data, sw) = self._cmd.card._scc.authenticate(opts.rand, opts.autn)
781 self._cmd.poutput_json(data)
782
783
784
Harald Welteb2edd142021-01-08 23:29:35 +0100785# TS 31.102 Section 7.3
786sw_usim = {
787 'Security management': {
788 '9862': 'Authentication error, incorrect MAC',
789 '9864': 'Authentication error, security context not supported',
790 '9865': 'Key freshness failure',
791 '9866': 'Authentication error, no memory space available',
792 '9867': 'Authentication error, no memory space available in EF MUK',
793 }
794}
795
796CardApplicationUSIM = CardApplication('USIM', adf=ADF_USIM(), sw=sw_usim)