blob: ae255f19316b5e1efd75864190ecb7e736d37ff0 [file] [log] [blame]
Supreeth Herle475dcaa2020-03-20 18:57:39 +01001# -*- coding: utf-8 -*-
2
3"""
Harald Welte2f831032021-04-20 23:54:53 +02004Various constants from 3GPP TS 31.102 V16.6.0
Supreeth Herle475dcaa2020-03-20 18:57:39 +01005"""
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',
Harald Welte2f831032021-04-20 23:54:53 +0200155 129: '5GS Operator PLMN List',
156 130: 'Support for SUPI of type NSI or GLI or GCI',
157 131: '3GPP PS Data Off separate Home and Roaming lists',
158 132: 'Support for URSP by USIM',
159 133: '5G Security Parameters extended',
160 134: 'MuD and MiD configuration data',
161 135: 'Support for Trusted non-3GPP access networks by USIM'
Sebastian Viviani0dc8f692020-05-29 00:14:55 +0100162}
163
Sebastian Vivianie61170c2020-06-03 08:57:00 +0100164LOCI_STATUS_map = {
165 0: 'updated',
166 1: 'not updated',
167 2: 'plmn not allowed',
168 3: 'locatation area not allowed'
169}
herlesupreetha562ea02020-09-16 20:17:22 +0200170
Sebastian Viviani0dc8f692020-05-29 00:14:55 +0100171EF_USIM_ADF_map = {
172 'LI': '6F05',
173 'ARR': '6F06',
174 'IMSI': '6F07',
175 'Keys': '6F08',
176 'KeysPS': '6F09',
177 'DCK': '6F2C',
178 'HPPLMN': '6F31',
179 'CNL': '6F32',
180 'ACMmax': '6F37',
181 'UST': '6F38',
182 'ACM': '6F39',
183 'FDN': '6F3B',
184 'SMS': '6F3C',
185 'GID1': '6F3E',
186 'GID2': '6F3F',
187 'MSISDN': '6F40',
188 'PUCT': '6F41',
189 'SMSP': '6F42',
190 'SMSS': '6F42',
191 'CBMI': '6F45',
192 'SPN': '6F46',
193 'SMSR': '6F47',
194 'CBMID': '6F48',
195 'SDN': '6F49',
196 'EXT2': '6F4B',
197 'EXT3': '6F4C',
198 'BDN': '6F4D',
199 'EXT5': '6F4E',
200 'CCP2': '6F4F',
201 'CBMIR': '6F50',
202 'EXT4': '6F55',
203 'EST': '6F56',
204 'ACL': '6F57',
205 'CMI': '6F58',
206 'START-HFN': '6F5B',
207 'THRESHOLD': '6F5C',
208 'PLMNwAcT': '6F60',
209 'OPLMNwAcT': '6F61',
210 'HPLMNwAcT': '6F62',
211 'PSLOCI': '6F73',
212 'ACC': '6F78',
213 'FPLMN': '6F7B',
214 'LOCI': '6F7E',
215 'ICI': '6F80',
216 'OCI': '6F81',
217 'ICT': '6F82',
218 'OCT': '6F83',
219 'AD': '6FAD',
220 'VGCS': '6FB1',
221 'VGCSS': '6FB2',
222 'VBS': '6FB3',
223 'VBSS': '6FB4',
224 'eMLPP': '6FB5',
225 'AAeM': '6FB6',
226 'ECC': '6FB7',
227 'Hiddenkey': '6FC3',
228 'NETPAR': '6FC4',
229 'PNN': '6FC5',
230 'OPL': '6FC6',
231 'MBDN': '6FC7',
232 'EXT6': '6FC8',
233 'MBI': '6FC9',
234 'MWIS': '6FCA',
235 'CFIS': '6FCB',
236 'EXT7': '6FCC',
237 'SPDI': '6FCD',
238 'MMSN': '6FCE',
239 'EXT8': '6FCF',
240 'MMSICP': '6FD0',
241 'MMSUP': '6FD1',
242 'MMSUCP': '6FD2',
243 'NIA': '6FD3',
244 'VGCSCA': '6FD4',
245 'VBSCA': '6FD5',
246 'GBAP': '6FD6',
247 'MSK': '6FD7',
248 'MUK': '6FD8',
249 'EHPLMN': '6FD9',
250 'GBANL': '6FDA',
251 'EHPLMNPI': '6FDB',
252 'LRPLMNSI': '6FDC',
253 'NAFKCA': '6FDD',
254 'SPNI': '6FDE',
255 'PNNI': '6FDF',
256 'NCP-IP': '6FE2',
257 'EPSLOCI': '6FE3',
258 'EPSNSC': '6FE4',
259 'UFC': '6FE6',
260 'UICCIARI': '6FE7',
261 'NASCONFIG': '6FE8',
262 'PWC': '6FEC',
263 'FDNURI': '6FED',
264 'BDNURI': '6FEE',
265 'SDNURI': '6FEF',
266 'IWL': '6FF0',
267 'IPS': '6FF1',
268 'IPD': '6FF2',
269 'ePDGId': '6FF3',
270 'ePDGSelection': '6FF4',
271 'ePDGIdEm': '6FF5',
272 'ePDGSelectionEm': '6FF6',
273}
Harald Welteb2edd142021-01-08 23:29:35 +0100274
275######################################################################
276# ADF.USIM
277######################################################################
278
Harald Welte15fae982021-04-10 10:22:27 +0200279from struct import unpack, pack
Harald Welte9853e242021-04-10 19:08:25 +0200280from construct import *
Harald Welte71290072021-04-21 10:58:24 +0200281from pySim.construct import LV, HexAdapter, BcdAdapter, BitsRFU
Harald Welteb2edd142021-01-08 23:29:35 +0100282from pySim.filesystem import *
Harald Welte790b2702021-04-11 00:01:35 +0200283from pySim.ts_102_221 import EF_ARR
Harald Welteb2edd142021-01-08 23:29:35 +0100284from 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 +0200285from pySim.ts_51_011 import EF_CBMID, EF_CBMIR, EF_ADN, EF_SMS, EF_MSISDN, EF_SMSP, EF_SMSS
286from pySim.ts_51_011 import EF_SMSR, EF_DCK, EF_EXT, EF_CNL, EF_OPL, EF_MBI, EF_MWIS
287from pySim.ts_51_011 import EF_MMSN, EF_MMSICP, EF_MMSUP, EF_MMSUCP, EF_VGCS, EF_VGCSS, EF_NIA
288from pySim.ts_51_011 import EF_ACMmax, EF_AAeM, EF_eMLPP, EF_CMI
Harald Welteb2edd142021-01-08 23:29:35 +0100289
290import pySim.ts_102_221
291
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200292# TS 31.102 4.4.11.8
293class EF_SUCI_Calc_Info(TransparentEF):
294 def __init__(self, fid="4f07", sfid=0x07, name='EF.SUCI_Calc_Info', size={2, None},
295 desc='SUCI Calc Info'):
296 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
297
298 def _encode_prot_scheme_id_list(self, in_list):
299 out_bytes = [0xa0]
300 out_bytes.append(len(in_list)*2) # two byte per entry
301
302 # position in list determines priority; high-priority items (low index) come first
303 for scheme in sorted(in_list, key=lambda item: item["priority"]):
304 out_bytes.append(scheme["identifier"])
305 out_bytes.append(scheme["key_index"])
306
307 return out_bytes
308
309 def _encode_hnet_pubkey_list(self, hnet_pubkey_list):
310 out_bytes = [0xa1] # pubkey list tag
311 out_bytes.append(0x00) # length filled later
312 length = 0
313
314 for key in hnet_pubkey_list:
315 out_bytes.append(0x80) # identifier tag
316 out_bytes.append(0x01) # TODO size, fixed to 1 byte
317 out_bytes.append(key["hnet_pubkey_identifier"])
318 out_bytes.append(0x81) # key tag
319 out_bytes.append(len(key["hnet_pubkey"])//2)
320 length += 5+len(key["hnet_pubkey"])//2
321
322 pubkey_bytes = h2b(key["hnet_pubkey"])
323 out_bytes += pubkey_bytes
324
325 # fill length
326 out_bytes[1] = length
327 return out_bytes
328
329 def _encode_hex(self, in_json):
330 out_bytes = self._encode_prot_scheme_id_list(in_json['prot_scheme_id_list'])
331 out_bytes += self._encode_hnet_pubkey_list(in_json['hnet_pubkey_list'])
332 return "".join(["%02X" % i for i in out_bytes])
333
334 def _decode_prot_scheme_id_list(self, in_bytes):
335 prot_scheme_id_list = []
336 pos = 0
337 # two bytes per entry
338 while pos < len(in_bytes):
339 prot_scheme = {
340 'priority': pos//2, # first in list: high priority
341 'identifier': in_bytes[pos],
342 'key_index': in_bytes[pos+1]
343 }
344 pos += 2
345 prot_scheme_id_list.append(prot_scheme)
346 return prot_scheme_id_list
347
348 def _decode_hnet_pubkey_list(self, in_bytes):
349 hnet_pubkey_list = []
350 pos = 0
351 if in_bytes[pos] != 0xa1:
352 print("missing Home Network Public Key List data object")
353 return {}
354 pos += 1
355 hnet_pubkey_list_len = in_bytes[pos]
356 pos += 1
357
358 while pos < hnet_pubkey_list_len:
359 if in_bytes[pos] != 0x80:
360 print("missing Home Network Public Key Identifier tag")
361 return {}
362 pos += 1
363 hnet_pubkey_id_len = in_bytes[pos] # TODO might be more than 1 byte?
364 pos += 1
365 hnet_pubkey_id = in_bytes[pos:pos+hnet_pubkey_id_len][0]
366 pos += hnet_pubkey_id_len
367 if in_bytes[pos] != 0x81:
368 print("missing Home Network Public Key tag")
369 return {}
370 pos += 1
371 hnet_pubkey_len = in_bytes[pos]
372 pos += 1
373 hnet_pubkey = in_bytes[pos:pos+hnet_pubkey_len]
374 pos += hnet_pubkey_len
375
376 hnet_pubkey_list.append({
377 'hnet_pubkey_identifier': hnet_pubkey_id,
378 'hnet_pubkey': b2h(hnet_pubkey)
379 })
380
381 return hnet_pubkey_list
382
383 def _decode_bin(self, in_bin):
Vadim Yanitskiy5e41eeb2021-05-02 02:20:33 +0200384 return self._decode_hex(b2h(in_bin))
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200385
386 def _decode_hex(self, in_hex):
387 in_bytes = h2b(in_hex)
388 pos = 0
389
390 if in_bytes[pos] != 0xa0:
391 print("missing Protection Scheme Identifier List data object tag")
392 return {}
393 pos += 1
394
395 prot_scheme_id_list_len = in_bytes[pos] # TODO maybe more than 1 byte
396 pos += 1
397 # decode Protection Scheme Identifier List data object
398 prot_scheme_id_list = self._decode_prot_scheme_id_list(in_bytes[pos:pos+prot_scheme_id_list_len])
399 pos += prot_scheme_id_list_len
400
401 # remaining data holds Home Network Public Key Data Object
402 hnet_pubkey_list = self._decode_hnet_pubkey_list(in_bytes[pos:])
403
404 return {
405 'prot_scheme_id_list': prot_scheme_id_list,
406 'hnet_pubkey_list': hnet_pubkey_list
407 }
408
409 def _encode_bin(self, in_json):
410 return h2b(self._encode_hex(in_json))
411
Harald Welteb2edd142021-01-08 23:29:35 +0100412class EF_LI(TransRecEF):
413 def __init__(self, fid='6f05', sfid=None, name='EF.LI', size={2,None}, rec_len=2,
414 desc='Language Indication'):
415 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
416 def _decode_record_bin(self, in_bin):
417 if in_bin == b'\xff\xff':
418 return None
419 else:
420 # officially this is 7-bit GSM alphabet with one padding bit in each byte
421 return in_bin.decode('ascii')
422 def _encode_record_bin(self, in_json):
423 if in_json == None:
424 return b'\xff\xff'
425 else:
426 # officially this is 7-bit GSM alphabet with one padding bit in each byte
427 return in_json.encode('ascii')
428
429class EF_Keys(TransparentEF):
430 def __init__(self, fid='6f08', sfid=0x08, name='EF.Keys', size={33,33},
431 desc='Ciphering and Integrity Keys'):
432 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Harald Welte9853e242021-04-10 19:08:25 +0200433 self._construct = Struct('ksi'/Int8ub, 'ck'/HexAdapter(Bytes(16)), 'ik'/HexAdapter(Bytes(16)))
Harald Welteb2edd142021-01-08 23:29:35 +0100434
435# TS 31.103 Section 4.2.7
436class EF_UST(TransparentEF):
Harald Welte790b2702021-04-11 00:01:35 +0200437 def __init__(self, fid='6f38', sfid=0x04, name='EF.UST', desc='USIM Service Table', size={1,17}):
438 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, size=size)
Harald Welteb2edd142021-01-08 23:29:35 +0100439 # add those commands to the general commands of a TransparentEF
440 self.shell_commands += [self.AddlShellCommands()]
441 def _decode_bin(self, in_bin):
442 ret = []
443 for i in range (0, len(in_bin)):
444 byte = in_bin[i]
445 for bitno in range(0,7):
446 if byte & (1 << bitno):
447 ret.append(i * 8 + bitno + 1)
448 return ret
449 def _encode_bin(self, in_json):
450 # FIXME: size this to length of file
451 ret = bytearray(20)
452 for srv in in_json:
453 print("srv=%d"%srv)
454 srv = srv-1
455 byte_nr = srv // 8
456 # FIXME: detect if service out of range was selected
457 bit_nr = srv % 8
458 ret[byte_nr] |= (1 << bit_nr)
459 return ret
460 @with_default_category('File-Specific Commands')
461 class AddlShellCommands(CommandSet):
462 def __init__(self):
463 super().__init__()
464
465 def do_ust_service_activate(self, arg):
466 """Activate a service within EF.UST"""
467 self._cmd.card.update_ust(int(arg), 1)
468
469 def do_ust_service_deactivate(self, arg):
470 """Deactivate a service within EF.UST"""
471 self._cmd.card.update_ust(int(arg), 0)
472
Harald Welte89e59542021-04-02 21:33:13 +0200473# TS 31.103 Section 4.2.7 - *not* the same as DF.GSM/EF.ECC!
474class EF_ECC(LinFixedEF):
475 def __init__(self, fid='6fb7', sfid=0x01, name='EF.ECC',
476 desc='Emergency Call Codes'):
477 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len={4,20})
478
Harald Welte790b2702021-04-11 00:01:35 +0200479# TS 31.102 Section 4.2.17
480class EF_LOCI(TransparentEF):
Harald Weltec9cdce32021-04-11 10:28:28 +0200481 def __init__(self, fid='6f7e', sfid=0x0b, name='EF.LOCI', desc='Location information', size={11,11}):
Harald Welte790b2702021-04-11 00:01:35 +0200482 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
483 self._construct = Struct('tmsi'/HexAdapter(Bytes(4)), 'lai'/HexAdapter(Bytes(5)), 'rfu'/Int8ub,
484 'lu_status'/Int8ub)
485
486# TS 31.102 Section 4.2.23
487class EF_PSLOCI(TransparentEF):
Harald Weltec9cdce32021-04-11 10:28:28 +0200488 def __init__(self, fid='6f73', sfid=0x0c, name='EF.PSLOCI', desc='PS Location information', size={14,14}):
Harald Welte790b2702021-04-11 00:01:35 +0200489 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
490 self._construct = Struct('ptmsi'/HexAdapter(Bytes(4)), 'ptmsi_sig'/HexAdapter(Bytes(3)),
491 'rai'/HexAdapter(Bytes(6)), 'rau_status'/Int8ub)
492
493# TS 31.102 Section 4.2.33
494class EF_ICI(CyclicEF):
495 def __init__(self, fid='6f80', sfid=0x14, name='EF.ICI', rec_len={28,48},
496 desc='Incoming Call Information'):
497 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len)
498 self._construct = Struct('alpha_id'/Bytes(this._.total_len-28),
499 'len_of_bcd_contents'/Int8ub,
500 'ton_npi'/Int8ub,
501 'call_number'/BcdAdapter(Bytes(10)),
502 'cap_cfg2_record_id'/Int8ub,
503 'ext5_record_id'/Int8ub,
504 'date_and_time'/BcdAdapter(Bytes(7)),
505 'duration'/Int24ub,
506 'status'/Byte,
507 'link_to_phonebook'/Bytes(3))
508
509# TS 31.102 Section 4.2.34
510class EF_OCI(CyclicEF):
511 def __init__(self, fid='6f81', sfid=0x15, name='EF.OCI', rec_len={27,47},
512 desc='Outgoing Call Information'):
513 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len)
514 self._construct = Struct('alpha_id'/Bytes(this._.total_len-27),
515 'len_of_bcd_contents'/Int8ub,
516 'ton_npi'/Int8ub,
517 'call_number'/BcdAdapter(Bytes(10)),
518 'cap_cfg2_record_id'/Int8ub,
519 'ext5_record_id'/Int8ub,
520 'date_and_time'/BcdAdapter(Bytes(7)),
521 'duration'/Int24ub,
522 'link_to_phonebook'/Bytes(3))
523
524# TS 31.102 Section 4.2.35
525class EF_ICT(CyclicEF):
526 def __init__(self, fid='6f82', sfid=None, name='EF.ICT', rec_len={3,3},
527 desc='Incoming Call Timer'):
528 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len)
529 self._construct = Struct('accumulated_call_timer'/Int24ub)
530
531# TS 31.102 Section 4.2.38
532class EF_CCP2(LinFixedEF):
533 def __init__(self, fid='6f4f', sfid=0x16, name='EF.CCP2', desc='Capability Configuration Parameters 2'):
534 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len={15,None})
535
536# TS 31.102 Section 4.2.48
537class EF_ACL(TransparentEF):
538 def __init__(self, fid='6f57', sfid=None, name='EF.ACL', size={32,None},
539 desc='Access Point Name Control List'):
540 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
541 self._construct = Struct('num_of_apns'/Int8ub, 'tlvs'/GreedyBytes)
542
543# TS 31.102 Section 4.2.51
544class EF_START_HFN(TransparentEF):
545 def __init__(self, fid='6f5b', sfid=0x0f, name='EF.START-HFN', size={6,6},
546 desc='Initialisation values for Hyperframe number'):
547 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
548 self._construct = Struct('start_cs'/Int24ub, 'start_ps'/Int24ub)
549
550# TS 31.102 Section 4.2.52
551class EF_THRESHOLD(TransparentEF):
552 def __init__(self, fid='6f5c', sfid=0x10, name='EF.THRESHOLD', size={3,3},
553 desc='Maximum value of START'):
554 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
555 self._construct = Struct('max_start'/Int24ub)
556
557# TS 31.102 Section 4.2.77
558class EF_VGCSCA(TransRecEF):
559 def __init__(self, fid='6fd4', sfid=None, name='EF.VGCSCA', size={2,100}, rec_len=2,
560 desc='Voice Group Call Service Ciphering Algorithm'):
561 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
562 self._construct = Struct('alg_v_ki_1'/Int8ub, 'alg_v_ki_2'/Int8ub)
563
564# TS 31.102 Section 4.2.79
565class EF_GBABP(TransparentEF):
566 def __init__(self, fid='6fd6', sfid=None, name='EF.GBABP', size={3,50},
567 desc='GBA Bootstrapping parameters'):
568 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
569 self._construct = Struct('rand'/LV, 'b_tid'/LV, 'key_lifetime'/LV)
570
571# TS 31.102 Section 4.2.80
572class EF_MSK(LinFixedEF):
573 def __init__(self, fid='6fd7', sfid=None, name='EF.MSK', desc='MBMS Service Key List'):
574 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len={20,None})
575 msk_ts_constr = Struct('msk_id'/Int32ub, 'timestamp_counter'/Int32ub)
576 self._construct = Struct('key_domain_id'/Bytes(3),
577 'num_msk_id'/Int8ub,
578 'msk_ids'/msk_ts_constr[this.num_msk_id])
579
580# TS 31.102 Section 4.2.85
581class EF_EHPLMNPI(TransparentEF):
582 def __init__(self, fid='6fdb', sfid=None, name='EF.EHPLMNPI', size={1,1},
583 desc='Equivalent HPLMN Presentation Indication'):
584 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
585 self._construct = Struct('presentation_ind'/
586 Enum(Byte, no_preference=0, display_highest_prio_only=1, display_all=2))
587# TS 31.102 Section 4.2.91
588class EF_EPSLOCI(TransparentEF):
589 def __init__(self, fid='6fe3', sfid=0x1e, name='EF.EPSLOCI', size={18,18},
590 desc='EPS Location Information'):
591 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
592 upd_status_constr = Enum(Byte, updated=0, not_updated=1, roaming_not_allowed=2)
593 self._construct = Struct('guti'/Bytes(12), 'last_visited_registered_tai'/Bytes(5),
594 'eps_update_status'/upd_status_constr)
595
596# TS 31.102 Section 4.2.96
597class EF_PWS(TransparentEF):
598 def __init__(self, fid='6fec', sfid=None, name='EF.PWS', desc='Public Warning System', size={1,1}):
599 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
600 pws_config = FlagsEnum(Byte, ignore_pws_in_hplmn_and_equivalent=1, ignore_pws_in_vplmn=2)
601 self._construct = Struct('pws_configuration'/pws_config)
602
603# TS 31.102 Section 4.2.101
604class EF_IPS(CyclicEF):
605 def __init__(self, fid='6ff1', sfid=None, name='EF.IPS', rec_len={4,4},
606 desc='IMEI(SV) Pairing Status'):
607 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len)
608 self._construct = Struct('status'/PaddedString(2, 'ascii'),
609 'link_to_ef_ipd'/Int8ub, 'rfu'/Byte)
610
Harald Welte71290072021-04-21 10:58:24 +0200611# TS 31.102 Section 4.2.106
612class EF_FromPreferred(TransparentEF):
613 def __init__(self, fid='6ff7', sfid=None, name='EF.FromPreferred', size={1,1},
614 desc='From Preferred'):
615 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
616 self._construct = BitStruct('rfu'/BitsRFU(7), 'from_preferred'/Bit)
617
Harald Welte790b2702021-04-11 00:01:35 +0200618######################################################################
619# DF.5GS
620######################################################################
621
622# TS 31.102 Section 4.4.11.2
623class EF_5GS3GPPLOCI(TransparentEF):
624 def __init__(self, fid='4f01', sfid=0x01, name='EF.5GS3GPPLOCI', size={20,20},
625 desc='5S 3GP location information'):
626 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
627 upd_status_constr = Enum(Byte, updated=0, not_updated=1, roaming_not_allowed=2)
628 self._construct = Struct('5g_guti'/Bytes(13), 'last_visited_registered_tai_in_5gs'/Bytes(6),
629 '5gs_update_status'/upd_status_constr)
630
631# TS 31.102 Section 4.4.11.7
632class EF_UAC_AIC(TransparentEF):
633 def __init__(self, fid='4f06', sfid=0x06, name='EF.UAC_AIC', size={4,4},
634 desc='UAC Access Identities Configuration'):
635 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
636 cfg_constr = FlagsEnum(Byte, multimedia_priority_service=1,
637 mission_critical_service=2)
638 self._construct = Struct('uac_access_id_config'/cfg_constr)
639
640# TS 31.102 Section 4.4.11.10
641class EF_OPL5G(LinFixedEF):
642 def __init__(self, fid='6f08', sfid=0x08, name='EF.OPL5G', desc='5GS Operator PLMN List'):
643 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len={10,None})
644 self._construct = Struct('tai'/Bytes(9), 'pnn_record_id'/Int8ub)
645
Harald Welte3990ebb2021-04-20 23:55:14 +0200646# TS 31.102 Section 4.4.5
647class DF_WLAN(CardDF):
648 def __init__(self, fid='5f40', name='DF.WLAN', desc='Files for WLAN purpose'):
649 super().__init__(fid=fid, name=name, desc=desc)
650 files = [
651 TransparentEF('4f41', 0x01, 'EF.Pseudo', 'Pseudonym'),
652 TransparentEF('4f42', 0x02, 'EF.UPLMNWLAN', 'User controlled PLMN selector for I-WLAN Access'),
653 TransparentEF('4f43', 0x03, 'EF.OPLMNWLAN', 'Operator controlled PLMN selector for I-WLAN Access'),
654 LinFixedEF('4f44', 0x04, 'EF.UWSIDL', 'User controlled WLAN Specific Identifier List'),
655 LinFixedEF('4f45', 0x05, 'EF.OWSIDL', 'Operator controlled WLAN Specific Identifier List'),
656 TransparentEF('4f46', 0x06, 'EF.WRI', 'WLAN Reauthentication Identity'),
657 LinFixedEF('4f47', 0x07, 'EF.HWSIDL', 'Home I-WLAN Specific Identifier List'),
658 TransparentEF('4f48', 0x08, 'EF.WEHPLMNPI', 'I-WLAN Equivalent HPLMN Presentation Indication'),
659 TransparentEF('4f49', 0x09, 'EF.WHPI', 'I-WLAN HPLMN Priority Indication'),
660 TransparentEF('4f4a', 0x0a, 'EF.WLRPLMN', 'I-WLAN Last Registered PLMN'),
661 TransparentEF('4f4b', 0x0b, 'EF.HPLMNDAI', 'HPLMN Direct Access Indicator'),
662 ]
663 self.add_files(files)
664
665# TS 31.102 Section 4.4.6
666class DF_HNB(CardDF):
667 def __init__(self, fid='5f50', name='DF.HNB', desc='Files for HomeNodeB purpose'):
668 super().__init__(fid=fid, name=name, desc=desc)
669 files = [
670 LinFixedEF('4f01', 0x01, 'EF.ACSGL', 'Allowed CSG Lists'),
671 LinFixedEF('4f02', 0x02, 'EF.CSGTL', 'CSG Types'),
672 LinFixedEF('4f03', 0x03, 'EF.HNBN', 'Home NodeB Name'),
673 LinFixedEF('4f04', 0x04, 'EF.OCSGL', 'Operator CSG Lists'),
674 LinFixedEF('4f05', 0x05, 'EF.OCSGT', 'Operator CSG Type'),
675 LinFixedEF('4f06', 0x06, 'EF.OHNBN', 'Operator Home NodeB Name'),
676 ]
677 self.add_files(files)
678
679# TS 31.102 Section 4.4.8
680class DF_ProSe(CardDF):
681 def __init__(self, fid='5f90', name='DF.ProSe', desc='Files for ProSe purpose'):
682 super().__init__(fid=fid, name=name, desc=desc)
683 files = [
684 LinFixedEF('4f01', 0x01, 'EF.PROSE_MON', 'ProSe Monitoring Parameters'),
685 LinFixedEF('4f02', 0x02, 'EF.PROSE_ANN', 'ProSe Announcing Parameters'),
686 LinFixedEF('4f03', 0x03, 'EF.PROSEFUNC', 'HPLMN ProSe Function'),
687 TransparentEF('4f04', 0x04, 'EF.PROSE_RADIO_COM', 'ProSe Direct Communication Radio Parameters'),
688 TransparentEF('4f05', 0x05, 'EF.PROSE_RADIO_MON', 'ProSe Direct Discovery Monitoring Radio Parameters'),
689 TransparentEF('4f06', 0x06, 'EF.PROSE_RADIO_ANN', 'ProSe Direct Discovery Announcing Radio Parameters'),
690 LinFixedEF('4f07', 0x07, 'EF.PROSE_POLICY', 'ProSe Policy Parameters'),
691 LinFixedEF('4f08', 0x08, 'EF.PROSE_PLMN', 'ProSe PLMN Parameters'),
692 TransparentEF('4f09', 0x09, 'EF.PROSE_GC', 'ProSe Group Counter'),
693 TransparentEF('4f10', 0x10, 'EF.PST', 'ProSe Service Table'),
694 TransparentEF('4f11', 0x11, 'EF.UIRC', 'ProSe UsageInformationReportingConfiguration'),
695 LinFixedEF('4f12', 0x12, 'EF.PROSE_GM_DISCOVERY', 'ProSe Group Member Discovery Parameters'),
696 LinFixedEF('4f13', 0x13, 'EF.PROSE_RELAY', 'ProSe Relay Parameters'),
697 TransparentEF('4f14', 0x14, 'EF.PROSE_RELAY_DISCOVERY', 'ProSe Relay Discovery Parameters'),
698 ]
699 self.add_files(files)
700
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200701class DF_USIM_5GS(CardDF):
702 def __init__(self, fid='5FC0', name='DF.5GS', desc='5GS related files'):
703 super().__init__(fid=fid, name=name, desc=desc)
704 files = [
Harald Welte2f831032021-04-20 23:54:53 +0200705 # I'm looking at 31.102 R16.6
Harald Welte790b2702021-04-11 00:01:35 +0200706 EF_5GS3GPPLOCI(),
707 EF_5GS3GPPLOCI('4f02', 0x02, 'EF.5GSN3GPPLOCI', '5GS non-3GPP location information'),
708 LinFixedEF('4F03', 0x03, 'EF.5GS3GPPNSC', '5GS 3GPP Access NAS Security Context', rec_len={57,None}),
709 LinFixedEF('4F04', 0x04, 'EF.5GSN3GPPNSC', '5GS non-3GPP Access NAS Security Context', rec_len={57,None}),
710 TransparentEF('4F05', 0x05, 'EF.5GAUTHKEYS', '5G authentication keys', size={68, None}),
711 EF_UAC_AIC(),
712 EF_SUCI_Calc_Info(),
713 EF_OPL5G(),
Harald Welte2f831032021-04-20 23:54:53 +0200714 TransparentEF('4F09', 0x09, 'EF.SUPI_NAI', 'SUPI as Network Access Identifier'),
Harald Welte790b2702021-04-11 00:01:35 +0200715 TransparentEF('4F0A', 0x0a, 'EF.Routing_Indicator', 'Routing Indicator', size={4,4}),
Harald Welte2f831032021-04-20 23:54:53 +0200716 TransparentEF('4F0B', 0x0b, 'EF.URSP', 'UE Route Selector Policies per PLMN'),
717 TransparentEF('4F0C', 0x0c, 'EF.TN3GPPSNN', 'Trusted non-3GPP Serving network names list'),
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200718 ]
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200719 self.add_files(files)
720
Harald Welteb2edd142021-01-08 23:29:35 +0100721class ADF_USIM(CardADF):
722 def __init__(self, aid='a0000000871002', name='ADF.USIM', fid=None, sfid=None,
723 desc='USIM Application'):
724 super().__init__(aid=aid, fid=fid, sfid=sfid, name=name, desc=desc)
Harald Welte15fae982021-04-10 10:22:27 +0200725 # add those commands to the general commands of a TransparentEF
726 self.shell_commands += [self.AddlShellCommands()]
Harald Welteb2edd142021-01-08 23:29:35 +0100727
728 files = [
729 EF_LI(sfid=0x02),
730 EF_IMSI(sfid=0x07),
731 EF_Keys(),
732 EF_Keys('6f09', 0x09, 'EF.KeysPS', desc='Ciphering and Integrity Keys for PS domain'),
733 EF_xPLMNwAcT('6f60', 0x0a, 'EF.PLMNwAcT',
734 'User controlled PLMN Selector with Access Technology'),
735 TransparentEF('6f31', 0x12, 'EF.HPPLMN', 'Higher Priority PLMN search period'),
Harald Welte790b2702021-04-11 00:01:35 +0200736 EF_ACMmax(),
Harald Welteb2edd142021-01-08 23:29:35 +0100737 EF_UST(),
738 CyclicEF('6f39', None, 'EF.ACM', 'Accumulated call meter', rec_len={3,3}),
739 TransparentEF('6f3e', None, 'EF.GID1', 'Group Identifier Level 1'),
740 TransparentEF('6f3f', None, 'EF.GID2', 'Group Identifier Level 2'),
741 EF_SPN(),
742 TransparentEF('6f41', None, 'EF.PUCT', 'Price per unit and currency table', size={5,5}),
743 EF_CBMI(),
744 EF_ACC(sfid=0x06),
745 EF_PLMNsel('6f7b', 0x0d, 'EF.FPLMN', 'Forbidden PLMNs', size={12,None}),
Harald Welte790b2702021-04-11 00:01:35 +0200746 EF_LOCI(),
Harald Welteb2edd142021-01-08 23:29:35 +0100747 EF_AD(sfid=0x03),
748 EF_CBMID(sfid=0x0e),
Harald Welte89e59542021-04-02 21:33:13 +0200749 EF_ECC(),
Harald Welteb2edd142021-01-08 23:29:35 +0100750 EF_CBMIR(),
Harald Welte790b2702021-04-11 00:01:35 +0200751 EF_PSLOCI(),
752 EF_ADN('6f3b', None, 'EF.FDN', 'Fixed Dialling Numbers'),
753 EF_SMS('6f3c', None),
754 EF_MSISDN(),
755 EF_SMSP(),
756 EF_SMSS(),
757 EF_ADN('6f49', None, 'EF.SDN', 'Service Dialling Numbers'),
758 EF_EXT('6f4b', None, 'EF.EXT2', 'Extension2 (FDN)'),
759 EF_EXT('6f4c', None, 'EF.EXT3', 'Extension2 (SDN)'),
760 EF_SMSR(),
761 EF_ICI(),
762 EF_OCI(),
763 EF_ICT(),
764 EF_ICT('6f83', None, 'EF.OCT', 'Outgoing Call Timer'),
765 EF_EXT('6f4e', None, 'EF.EXT5', 'Extension5 (ICI/OCI/MSISDN)'),
766 EF_CCP2(),
767 EF_eMLPP(),
768 EF_AAeM(),
769 # EF_Hiddenkey
770 EF_ADN('6f4d', None, 'EF.BDN', 'Barred Dialling Numbers'),
771 EF_EXT('6f55', None, 'EF.EXT4', 'Extension4 (BDN/SSC)'),
772 EF_CMI(),
773 EF_UST('6f56', 0x05, 'EF.EST', 'Enabled Services Table', size={1,None}),
774 EF_ACL(),
775 EF_DCK(),
776 EF_CNL(),
777 EF_START_HFN(),
778 EF_THRESHOLD(),
779 EF_xPLMNwAcT('6f61', 0x11, 'EF.OPLMNwAcT',
780 'User controlled PLMN Selector with Access Technology'),
781 EF_ARR('6f06', 0x17),
782 TransparentEF('6fc4', None, 'EF.NETPAR', 'Network Parameters'),
783 LinFixedEF('6fc5', 0x19, 'EF.PNN', 'PLMN Network Name'),
784 EF_OPL(),
785 EF_ADN('6fc7', None, 'EF.MBDN', 'Mailbox Dialling Numbers'),
786 EF_MBI(),
787 EF_MWIS(),
788 EF_ADN('6fcb', None, 'EF.CFIS', 'Call Forwarding Indication Status'),
789 EF_EXT('6fcc', None, 'EF.EXT7', 'Extension7 (CFIS)'),
790 TransparentEF('6fcd', None, 'EF.SPDI', 'Service Provider Display Information'),
791 EF_MMSN(),
792 EF_EXT('6fcf', None, 'EF.EXT8', 'Extension8 (MMSN)'),
793 EF_MMSICP(),
794 EF_MMSUP(),
795 EF_MMSUCP(),
796 EF_NIA(),
797 EF_VGCS(),
798 EF_VGCSS(),
799 EF_VGCS('6fb3', None, 'EF.VBS', 'Voice Broadcast Service'),
800 EF_VGCSS('6fb4', None, 'EF.VBSS', 'Voice Broadcast Service Status'),
801 EF_VGCSCA(),
802 EF_VGCSCA('6fd5', None, 'EF.VBCSCA', 'Voice Broadcast Service Ciphering Algorithm'),
803 EF_GBABP(),
804 EF_MSK(),
805 LinFixedEF('6fd8', None, 'EF.MUK', 'MBMS User Key'),
806 LinFixedEF('6fda', None, 'EF.GBANL', 'GBA NFA List'),
807 EF_PLMNsel('6fd9', 0x1d, 'EF.EHPLMN', 'Equivalent HPLMN', size={12,None}),
808 EF_EHPLMNPI(),
809 LinFixedEF('6fdd', None, 'EF.NAFKCA', 'NAF Key Centre Address'),
810 TransparentEF('6fde', None, 'EF.SPNI', 'Service Provider Name Icon'),
811 LinFixedEF('6fdf', None, 'EF.PNNI', 'PLMN Network Name Icon'),
812 LinFixedEF('6fe2', None, 'EF.NCP-IP', 'Network Connectivity Parameters for USIM IP connections'),
813 EF_EPSLOCI('6fe3', 0x1e, 'EF.EPSLOCI', 'EPS location information'),
814 LinFixedEF('6fe4', 0x18, 'EF.EPSNSC', 'EPS NAS Security Context', rec_len={54,128}),
815 TransparentEF('6fe6', None, 'EF.UFC', 'USAT Facility Control', size={1,16}),
816 TransparentEF('6fe8', None, 'EF.NASCONFIG', 'Non Access Stratum Configuration'),
817 # UICC IARI (only in cards that have no ISIM)
818 EF_PWS(),
819 LinFixedEF('6fed', None, 'EF.FDNURI', 'Fixed Dialling Numbers URI'),
820 LinFixedEF('6fee', None, 'EF.BDNURI', 'Barred Dialling Numbers URI'),
821 LinFixedEF('6fef', None, 'EF.SDNURI', 'Service Dialling Numbers URI'),
822 EF_IPS(),
823 # FIXME: from EF_ePDGid onwards
Harald Welte71290072021-04-21 10:58:24 +0200824 EF_FromPreferred(),
Harald Welte790b2702021-04-11 00:01:35 +0200825 # FIXME: DF_SoLSA
826 # FIXME: DF_PHONEBOOK
827 # FIXME: DF_GSM_ACCESS
Harald Welte3990ebb2021-04-20 23:55:14 +0200828 DF_WLAN(),
829 DF_HNB(),
830 DF_ProSe(),
Harald Welte790b2702021-04-11 00:01:35 +0200831 # FIXME: DF_ACDC
832 # FIXME: DF_TV
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200833 DF_USIM_5GS(),
Harald Welteb2edd142021-01-08 23:29:35 +0100834 ]
835 self.add_files(files)
836
837 def decode_select_response(self, data_hex):
838 return pySim.ts_102_221.decode_select_response(data_hex)
839
Harald Welte15fae982021-04-10 10:22:27 +0200840 @with_default_category('Application-Specific Commands')
841 class AddlShellCommands(CommandSet):
842 def __init__(self):
843 super().__init__()
844
845 authenticate_parser = argparse.ArgumentParser()
846 authenticate_parser.add_argument('rand', help='Random challenge')
847 authenticate_parser.add_argument('autn', help='Authentication Nonce')
848 #authenticate_parser.add_argument('--context', help='Authentication context', default='3G')
849 @cmd2.with_argparser(authenticate_parser)
850 def do_authenticate(self, opts):
851 """Perform Authentication and Key Agreement (AKA)."""
852 (data, sw) = self._cmd.card._scc.authenticate(opts.rand, opts.autn)
853 self._cmd.poutput_json(data)
854
855
856
Harald Welteb2edd142021-01-08 23:29:35 +0100857# TS 31.102 Section 7.3
858sw_usim = {
859 'Security management': {
860 '9862': 'Authentication error, incorrect MAC',
861 '9864': 'Authentication error, security context not supported',
862 '9865': 'Key freshness failure',
863 '9866': 'Authentication error, no memory space available',
864 '9867': 'Authentication error, no memory space available in EF MUK',
865 }
866}
867
868CardApplicationUSIM = CardApplication('USIM', adf=ADF_USIM(), sw=sw_usim)