blob: 7850459d661deafed51b41b19f6a580072590436 [file] [log] [blame]
Supreeth Herle475dcaa2020-03-20 18:57:39 +01001# -*- coding: utf-8 -*-
2
Harald Weltef12979d2021-05-29 21:47:13 +02003# without this, pylint will fail when inner classes are used
4# within the 'nested' kwarg of our TlvMeta metaclass on python 3.7 :(
5# pylint: disable=undefined-variable
6
Supreeth Herle475dcaa2020-03-20 18:57:39 +01007"""
Harald Welte2f831032021-04-20 23:54:53 +02008Various constants from 3GPP TS 31.102 V16.6.0
Supreeth Herle475dcaa2020-03-20 18:57:39 +01009"""
10
11#
12# Copyright (C) 2020 Supreeth Herle <herlesupreeth@gmail.com>
Harald Welte21caf322022-07-16 14:06:46 +020013# Copyright (C) 2021-2022 Harald Welte <laforge@osmocom.org>
Supreeth Herle475dcaa2020-03-20 18:57:39 +010014#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 2 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
27#
28
29# Mapping between USIM Service Number and its description
Harald Weltec91085e2022-02-10 18:05:45 +010030import pySim.ts_102_221
31from pySim.ts_51_011 import EF_ACMmax, EF_AAeM, EF_eMLPP, EF_CMI, EF_PNN
32from pySim.ts_51_011 import EF_MMSN, EF_MMSICP, EF_MMSUP, EF_MMSUCP, EF_VGCS, EF_VGCSS, EF_NIA
33from pySim.ts_51_011 import EF_SMSR, EF_DCK, EF_EXT, EF_CNL, EF_OPL, EF_MBI, EF_MWIS
34from pySim.ts_51_011 import EF_CBMID, EF_CBMIR, EF_ADN, EF_SMS, EF_MSISDN, EF_SMSP, EF_SMSS
35from pySim.ts_51_011 import EF_IMSI, EF_xPLMNwAcT, EF_SPN, EF_CBMI, EF_ACC, EF_PLMNsel
Harald Welted90ceb82022-07-17 22:10:58 +020036from pySim.ts_51_011 import EF_Kc, EF_CPBCCH, EF_InvScan
Harald Weltec91085e2022-02-10 18:05:45 +010037from pySim.ts_102_221 import EF_ARR
38from pySim.tlv import *
39from pySim.filesystem import *
Harald Welte6f8a8702022-07-17 21:50:31 +020040from pySim.ts_31_102_telecom import DF_PHONEBOOK, EF_UServiceTable
Harald Weltec91085e2022-02-10 18:05:45 +010041from pySim.construct import *
42from construct import Optional as COptional
43from construct import *
44from typing import Tuple
45from struct import unpack, pack
46import enum
Supreeth Herle475dcaa2020-03-20 18:57:39 +010047EF_UST_map = {
Harald Weltec91085e2022-02-10 18:05:45 +010048 1: 'Local Phone Book',
49 2: 'Fixed Dialling Numbers (FDN)',
50 3: 'Extension 2',
51 4: 'Service Dialling Numbers (SDN)',
52 5: 'Extension3',
53 6: 'Barred Dialling Numbers (BDN)',
54 7: 'Extension4',
55 8: 'Outgoing Call Information (OCI and OCT)',
56 9: 'Incoming Call Information (ICI and ICT)',
57 10: 'Short Message Storage (SMS)',
58 11: 'Short Message Status Reports (SMSR)',
59 12: 'Short Message Service Parameters (SMSP)',
60 13: 'Advice of Charge (AoC)',
61 14: 'Capability Configuration Parameters 2 (CCP2)',
62 15: 'Cell Broadcast Message Identifier',
63 16: 'Cell Broadcast Message Identifier Ranges',
64 17: 'Group Identifier Level 1',
65 18: 'Group Identifier Level 2',
66 19: 'Service Provider Name',
67 20: 'User controlled PLMN selector with Access Technology',
68 21: 'MSISDN',
69 22: 'Image (IMG)',
70 23: 'Support of Localised Service Areas (SoLSA)',
71 24: 'Enhanced Multi-Level Precedence and Pre-emption Service',
72 25: 'Automatic Answer for eMLPP',
73 26: 'RFU',
74 27: 'GSM Access',
75 28: 'Data download via SMS-PP',
76 29: 'Data download via SMS-CB',
77 30: 'Call Control by USIM',
78 31: 'MO-SMS Control by USIM',
79 32: 'RUN AT COMMAND command',
80 33: 'shall be set to 1',
81 34: 'Enabled Services Table',
82 35: 'APN Control List (ACL)',
83 36: 'Depersonalisation Control Keys',
84 37: 'Co-operative Network List',
85 38: 'GSM security context',
86 39: 'CPBCCH Information',
87 40: 'Investigation Scan',
88 41: 'MexE',
89 42: 'Operator controlled PLMN selector with Access Technology',
90 43: 'HPLMN selector with Access Technology',
91 44: 'Extension 5',
92 45: 'PLMN Network Name',
93 46: 'Operator PLMN List',
94 47: 'Mailbox Dialling Numbers',
95 48: 'Message Waiting Indication Status',
96 49: 'Call Forwarding Indication Status',
97 50: 'Reserved and shall be ignored',
98 51: 'Service Provider Display Information',
99 52: 'Multimedia Messaging Service (MMS)',
100 53: 'Extension 8',
101 54: 'Call control on GPRS by USIM',
102 55: 'MMS User Connectivity Parameters',
103 56: 'Network\'s indication of alerting in the MS (NIA)',
104 57: 'VGCS Group Identifier List (EFVGCS and EFVGCSS)',
105 58: 'VBS Group Identifier List (EFVBS and EFVBSS)',
106 59: 'Pseudonym',
107 60: 'User Controlled PLMN selector for I-WLAN access',
108 61: 'Operator Controlled PLMN selector for I-WLAN access',
109 62: 'User controlled WSID list',
110 63: 'Operator controlled WSID list',
111 64: 'VGCS security',
112 65: 'VBS security',
113 66: 'WLAN Reauthentication Identity',
114 67: 'Multimedia Messages Storage',
115 68: 'Generic Bootstrapping Architecture (GBA)',
116 69: 'MBMS security',
117 70: 'Data download via USSD and USSD application mode',
118 71: 'Equivalent HPLMN',
119 72: 'Additional TERMINAL PROFILE after UICC activation',
120 73: 'Equivalent HPLMN Presentation Indication',
121 74: 'Last RPLMN Selection Indication',
122 75: 'OMA BCAST Smart Card Profile',
123 76: 'GBA-based Local Key Establishment Mechanism',
124 77: 'Terminal Applications',
125 78: 'Service Provider Name Icon',
126 79: 'PLMN Network Name Icon',
127 80: 'Connectivity Parameters for USIM IP connections',
128 81: 'Home I-WLAN Specific Identifier List',
129 82: 'I-WLAN Equivalent HPLMN Presentation Indication',
130 83: 'I-WLAN HPLMN Priority Indication',
131 84: 'I-WLAN Last Registered PLMN',
132 85: 'EPS Mobility Management Information',
133 86: 'Allowed CSG Lists and corresponding indications',
134 87: 'Call control on EPS PDN connection by USIM',
135 88: 'HPLMN Direct Access',
136 89: 'eCall Data',
137 90: 'Operator CSG Lists and corresponding indications',
138 91: 'Support for SM-over-IP',
139 92: 'Support of CSG Display Control',
140 93: 'Communication Control for IMS by USIM',
141 94: 'Extended Terminal Applications',
142 95: 'Support of UICC access to IMS',
143 96: 'Non-Access Stratum configuration by USIM',
144 97: 'PWS configuration by USIM',
145 98: 'RFU',
146 99: 'URI support by UICC',
147 100: 'Extended EARFCN support',
148 101: 'ProSe',
149 102: 'USAT Application Pairing',
150 103: 'Media Type support',
151 104: 'IMS call disconnection cause',
152 105: 'URI support for MO SHORT MESSAGE CONTROL',
153 106: 'ePDG configuration Information support',
154 107: 'ePDG configuration Information configured',
155 108: 'ACDC support',
156 109: 'MCPTT',
157 110: 'ePDG configuration Information for Emergency Service support',
158 111: 'ePDG configuration Information for Emergency Service configured',
159 112: 'eCall Data over IMS',
160 113: 'URI support for SMS-PP DOWNLOAD as defined in 3GPP TS 31.111 [12]',
161 114: 'From Preferred',
162 115: 'IMS configuration data',
163 116: 'TV configuration',
164 117: '3GPP PS Data Off',
165 118: '3GPP PS Data Off Service List',
166 119: 'V2X',
167 120: 'XCAP Configuration Data',
168 121: 'EARFCN list for MTC/NB-IOT UEs',
169 122: '5GS Mobility Management Information',
170 123: '5G Security Parameters',
171 124: 'Subscription identifier privacy support',
172 125: 'SUCI calculation by the USIM',
173 126: 'UAC Access Identities support',
174 127: 'Expect control plane-based Steering of Roaming information during initial registration in VPLMN',
175 128: 'Call control on PDU Session by USIM',
176 129: '5GS Operator PLMN List',
177 130: 'Support for SUPI of type NSI or GLI or GCI',
178 131: '3GPP PS Data Off separate Home and Roaming lists',
179 132: 'Support for URSP by USIM',
180 133: '5G Security Parameters extended',
181 134: 'MuD and MiD configuration data',
182 135: 'Support for Trusted non-3GPP access networks by USIM'
Sebastian Viviani0dc8f692020-05-29 00:14:55 +0100183}
184
Harald Weltee8947492022-02-10 10:33:20 +0100185# Mapping between USIM Enbled Service Number and its description
186EF_EST_map = {
187 1: 'Fixed Dialling Numbers (FDN)',
188 2: 'Barred Dialling Numbers (BDN)',
189 3: 'APN Control List (ACL)'
190}
191
Sebastian Vivianie61170c2020-06-03 08:57:00 +0100192LOCI_STATUS_map = {
Harald Weltec91085e2022-02-10 18:05:45 +0100193 0: 'updated',
194 1: 'not updated',
195 2: 'plmn not allowed',
196 3: 'locatation area not allowed'
Sebastian Vivianie61170c2020-06-03 08:57:00 +0100197}
herlesupreetha562ea02020-09-16 20:17:22 +0200198
Sebastian Viviani0dc8f692020-05-29 00:14:55 +0100199EF_USIM_ADF_map = {
Harald Weltec91085e2022-02-10 18:05:45 +0100200 'LI': '6F05',
201 'ARR': '6F06',
202 'IMSI': '6F07',
203 'Keys': '6F08',
204 'KeysPS': '6F09',
205 'DCK': '6F2C',
206 'HPPLMN': '6F31',
207 'CNL': '6F32',
208 'ACMmax': '6F37',
209 'UST': '6F38',
210 'ACM': '6F39',
211 'FDN': '6F3B',
212 'SMS': '6F3C',
213 'GID1': '6F3E',
214 'GID2': '6F3F',
215 'MSISDN': '6F40',
216 'PUCT': '6F41',
217 'SMSP': '6F42',
218 'SMSS': '6F42',
219 'CBMI': '6F45',
220 'SPN': '6F46',
221 'SMSR': '6F47',
222 'CBMID': '6F48',
223 'SDN': '6F49',
224 'EXT2': '6F4B',
225 'EXT3': '6F4C',
226 'BDN': '6F4D',
227 'EXT5': '6F4E',
228 'CCP2': '6F4F',
229 'CBMIR': '6F50',
230 'EXT4': '6F55',
231 'EST': '6F56',
232 'ACL': '6F57',
233 'CMI': '6F58',
234 'START-HFN': '6F5B',
235 'THRESHOLD': '6F5C',
236 'PLMNwAcT': '6F60',
237 'OPLMNwAcT': '6F61',
238 'HPLMNwAcT': '6F62',
239 'PSLOCI': '6F73',
240 'ACC': '6F78',
241 'FPLMN': '6F7B',
242 'LOCI': '6F7E',
243 'ICI': '6F80',
244 'OCI': '6F81',
245 'ICT': '6F82',
246 'OCT': '6F83',
247 'AD': '6FAD',
248 'VGCS': '6FB1',
249 'VGCSS': '6FB2',
250 'VBS': '6FB3',
251 'VBSS': '6FB4',
252 'eMLPP': '6FB5',
253 'AAeM': '6FB6',
254 'ECC': '6FB7',
255 'Hiddenkey': '6FC3',
256 'NETPAR': '6FC4',
257 'PNN': '6FC5',
258 'OPL': '6FC6',
259 'MBDN': '6FC7',
260 'EXT6': '6FC8',
261 'MBI': '6FC9',
262 'MWIS': '6FCA',
263 'CFIS': '6FCB',
264 'EXT7': '6FCC',
265 'SPDI': '6FCD',
266 'MMSN': '6FCE',
267 'EXT8': '6FCF',
268 'MMSICP': '6FD0',
269 'MMSUP': '6FD1',
270 'MMSUCP': '6FD2',
271 'NIA': '6FD3',
272 'VGCSCA': '6FD4',
273 'VBSCA': '6FD5',
274 'GBAP': '6FD6',
275 'MSK': '6FD7',
276 'MUK': '6FD8',
277 'EHPLMN': '6FD9',
278 'GBANL': '6FDA',
279 'EHPLMNPI': '6FDB',
280 'LRPLMNSI': '6FDC',
281 'NAFKCA': '6FDD',
282 'SPNI': '6FDE',
283 'PNNI': '6FDF',
284 'NCP-IP': '6FE2',
285 'EPSLOCI': '6FE3',
286 'EPSNSC': '6FE4',
287 'UFC': '6FE6',
288 'UICCIARI': '6FE7',
289 'NASCONFIG': '6FE8',
290 'PWC': '6FEC',
291 'FDNURI': '6FED',
292 'BDNURI': '6FEE',
293 'SDNURI': '6FEF',
294 'IWL': '6FF0',
295 'IPS': '6FF1',
296 'IPD': '6FF2',
297 'ePDGId': '6FF3',
298 'ePDGSelection': '6FF4',
299 'ePDGIdEm': '6FF5',
300 'ePDGSelectionEm': '6FF6',
Sebastian Viviani0dc8f692020-05-29 00:14:55 +0100301}
Harald Welteb2edd142021-01-08 23:29:35 +0100302
Harald Welte21caf322022-07-16 14:06:46 +0200303# 3gPP TS 31.102 Section 7.5.2.1
304class SUCI_TlvDataObject(BER_TLV_IE, tag=0xA1):
305 _construct = HexAdapter(GreedyBytes)
306
Harald Welteb2edd142021-01-08 23:29:35 +0100307######################################################################
308# ADF.USIM
309######################################################################
310
Harald Welteb2edd142021-01-08 23:29:35 +0100311
Harald Weltef12979d2021-05-29 21:47:13 +0200312# 3GPP TS 31.102 Section 4.4.11.4 (EF_5GS3GPPNSC)
313class EF_5GS3GPPNSC(LinFixedEF):
314 class NgKSI(BER_TLV_IE, tag=0x80):
315 _construct = Int8ub
316
317 class K_AMF(BER_TLV_IE, tag=0x81):
318 _construct = HexAdapter(Bytes(32))
319
320 class UplinkNASCount(BER_TLV_IE, tag=0x82):
321 _construct = Int32ub
322
323 class DownlinkNASCount(BER_TLV_IE, tag=0x83):
324 _construct = Int32ub
325
326 class IdsOfSelectedNasAlgos(BER_TLV_IE, tag=0x84):
327 # 3GPP TS 24.501 Section 9.11.3.34
328 _construct = BitStruct('ciphering'/Nibble, 'integrity'/Nibble)
329
330 class IdsOfSelectedEpsAlgos(BER_TLV_IE, tag=0x85):
331 # 3GPP TS 24.301 Section 9.9.3.23
332 _construct = BitStruct('ciphering'/Nibble, 'integrity'/Nibble)
333
334 class FiveGSNasSecurityContext(BER_TLV_IE, tag=0xA0,
Harald Weltec91085e2022-02-10 18:05:45 +0100335 nested=[NgKSI, K_AMF, UplinkNASCount,
336 DownlinkNASCount, IdsOfSelectedNasAlgos,
337 IdsOfSelectedEpsAlgos]):
Harald Weltef12979d2021-05-29 21:47:13 +0200338 pass
339
Harald Welte99e4cc02022-07-21 15:25:47 +0200340 def __init__(self, fid="4f03", sfid=0x03, name='EF.5GS3GPPNSC', rec_len=(57, None),
Harald Welte419bb492022-02-12 21:39:35 +0100341 desc='5GS 3GPP Access NAS Security Context', **kwargs):
342 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte65516272022-02-10 17:51:05 +0100343 self._tlv = EF_5GS3GPPNSC.FiveGSNasSecurityContext
Harald Weltef12979d2021-05-29 21:47:13 +0200344
345# 3GPP TS 31.102 Section 4.4.11.6
346class EF_5GAUTHKEYS(TransparentEF):
347 class K_AUSF(BER_TLV_IE, tag=0x80):
348 _construct = HexAdapter(GreedyBytes)
349
350 class K_SEAF(BER_TLV_IE, tag=0x81):
351 _construct = HexAdapter(GreedyBytes)
352
353 class FiveGAuthKeys(TLV_IE_Collection, nested=[K_AUSF, K_SEAF]):
354 pass
355
Harald Welte13edf302022-07-21 15:19:23 +0200356 def __init__(self, fid='4f05', sfid=0x05, name='EF.5GAUTHKEYS', size=(68, None),
Harald Welte419bb492022-02-12 21:39:35 +0100357 desc='5G authentication keys', **kwargs):
358 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte65516272022-02-10 17:51:05 +0100359 self._tlv = EF_5GAUTHKEYS.FiveGAuthKeys
Harald Weltef12979d2021-05-29 21:47:13 +0200360
361# 3GPP TS 31.102 Section 4.4.11.8
362class ProtSchemeIdList(BER_TLV_IE, tag=0xa0):
363 # FIXME: 3GPP TS 24.501 Protection Scheme Identifier
364 # repeated sequence of (id, index) tuples
Harald Weltec91085e2022-02-10 18:05:45 +0100365 _construct = GreedyRange(
366 Struct('id'/Enum(Byte, null=0, A=1, B=2), 'index'/Int8ub))
367
Harald Weltef12979d2021-05-29 21:47:13 +0200368
369class HomeNetPubKeyId(BER_TLV_IE, tag=0x80):
370 # 3GPP TS 24.501 / 3GPP TS 23.003
371 _construct = Int8ub
372
Harald Weltec91085e2022-02-10 18:05:45 +0100373
Harald Weltef12979d2021-05-29 21:47:13 +0200374class HomeNetPubKey(BER_TLV_IE, tag=0x81):
375 # FIXME: RFC 5480
376 _construct = HexAdapter(GreedyBytes)
377
Harald Weltec91085e2022-02-10 18:05:45 +0100378
Harald Weltef12979d2021-05-29 21:47:13 +0200379class HomeNetPubKeyList(BER_TLV_IE, tag=0xa1,
Harald Weltec91085e2022-02-10 18:05:45 +0100380 nested=[HomeNetPubKeyId, HomeNetPubKey]):
Harald Weltef12979d2021-05-29 21:47:13 +0200381 pass
382
383# 3GPP TS 31.102 Section 4.4.11.6
Harald Weltec91085e2022-02-10 18:05:45 +0100384class SUCI_CalcInfo(TLV_IE_Collection, nested=[ProtSchemeIdList, HomeNetPubKeyList]):
Harald Weltef12979d2021-05-29 21:47:13 +0200385 pass
386
387
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200388# TS 31.102 4.4.11.8
389class EF_SUCI_Calc_Info(TransparentEF):
Harald Welte13edf302022-07-21 15:19:23 +0200390 def __init__(self, fid="4f07", sfid=0x07, name='EF.SUCI_Calc_Info', size=(2, None),
Harald Welte419bb492022-02-12 21:39:35 +0100391 desc='SUCI Calc Info', **kwargs):
392 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200393
394 def _encode_prot_scheme_id_list(self, in_list):
395 out_bytes = [0xa0]
Harald Weltec91085e2022-02-10 18:05:45 +0100396 out_bytes.append(len(in_list)*2) # two byte per entry
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200397
398 # position in list determines priority; high-priority items (low index) come first
399 for scheme in sorted(in_list, key=lambda item: item["priority"]):
400 out_bytes.append(scheme["identifier"])
401 out_bytes.append(scheme["key_index"])
402
403 return out_bytes
404
405 def _encode_hnet_pubkey_list(self, hnet_pubkey_list):
Harald Weltec91085e2022-02-10 18:05:45 +0100406 out_bytes = [0xa1] # pubkey list tag
407 out_bytes.append(0x00) # length filled later
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200408 length = 0
409
410 for key in hnet_pubkey_list:
Harald Weltec91085e2022-02-10 18:05:45 +0100411 out_bytes.append(0x80) # identifier tag
412 out_bytes.append(0x01) # TODO size, fixed to 1 byte
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200413 out_bytes.append(key["hnet_pubkey_identifier"])
Harald Weltec91085e2022-02-10 18:05:45 +0100414 out_bytes.append(0x81) # key tag
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200415 out_bytes.append(len(key["hnet_pubkey"])//2)
416 length += 5+len(key["hnet_pubkey"])//2
417
418 pubkey_bytes = h2b(key["hnet_pubkey"])
419 out_bytes += pubkey_bytes
420
421 # fill length
422 out_bytes[1] = length
423 return out_bytes
424
425 def _encode_hex(self, in_json):
Harald Weltec91085e2022-02-10 18:05:45 +0100426 out_bytes = self._encode_prot_scheme_id_list(
427 in_json['prot_scheme_id_list'])
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200428 out_bytes += self._encode_hnet_pubkey_list(in_json['hnet_pubkey_list'])
429 return "".join(["%02X" % i for i in out_bytes])
430
431 def _decode_prot_scheme_id_list(self, in_bytes):
432 prot_scheme_id_list = []
433 pos = 0
434 # two bytes per entry
435 while pos < len(in_bytes):
436 prot_scheme = {
Harald Weltec91085e2022-02-10 18:05:45 +0100437 'priority': pos//2, # first in list: high priority
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200438 'identifier': in_bytes[pos],
439 'key_index': in_bytes[pos+1]
440 }
441 pos += 2
442 prot_scheme_id_list.append(prot_scheme)
443 return prot_scheme_id_list
444
445 def _decode_hnet_pubkey_list(self, in_bytes):
446 hnet_pubkey_list = []
447 pos = 0
448 if in_bytes[pos] != 0xa1:
449 print("missing Home Network Public Key List data object")
450 return {}
451 pos += 1
452 hnet_pubkey_list_len = in_bytes[pos]
453 pos += 1
454
455 while pos < hnet_pubkey_list_len:
456 if in_bytes[pos] != 0x80:
457 print("missing Home Network Public Key Identifier tag")
458 return {}
459 pos += 1
Harald Weltec91085e2022-02-10 18:05:45 +0100460 # TODO might be more than 1 byte?
461 hnet_pubkey_id_len = in_bytes[pos]
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200462 pos += 1
463 hnet_pubkey_id = in_bytes[pos:pos+hnet_pubkey_id_len][0]
464 pos += hnet_pubkey_id_len
465 if in_bytes[pos] != 0x81:
466 print("missing Home Network Public Key tag")
467 return {}
468 pos += 1
469 hnet_pubkey_len = in_bytes[pos]
470 pos += 1
471 hnet_pubkey = in_bytes[pos:pos+hnet_pubkey_len]
472 pos += hnet_pubkey_len
473
474 hnet_pubkey_list.append({
475 'hnet_pubkey_identifier': hnet_pubkey_id,
476 'hnet_pubkey': b2h(hnet_pubkey)
477 })
478
479 return hnet_pubkey_list
480
481 def _decode_bin(self, in_bin):
Vadim Yanitskiy5e41eeb2021-05-02 02:20:33 +0200482 return self._decode_hex(b2h(in_bin))
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200483
484 def _decode_hex(self, in_hex):
485 in_bytes = h2b(in_hex)
486 pos = 0
487
488 if in_bytes[pos] != 0xa0:
489 print("missing Protection Scheme Identifier List data object tag")
490 return {}
491 pos += 1
492
Harald Weltec91085e2022-02-10 18:05:45 +0100493 prot_scheme_id_list_len = in_bytes[pos] # TODO maybe more than 1 byte
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200494 pos += 1
495 # decode Protection Scheme Identifier List data object
Harald Weltec91085e2022-02-10 18:05:45 +0100496 prot_scheme_id_list = self._decode_prot_scheme_id_list(
497 in_bytes[pos:pos+prot_scheme_id_list_len])
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200498 pos += prot_scheme_id_list_len
499
500 # remaining data holds Home Network Public Key Data Object
501 hnet_pubkey_list = self._decode_hnet_pubkey_list(in_bytes[pos:])
502
503 return {
504 'prot_scheme_id_list': prot_scheme_id_list,
505 'hnet_pubkey_list': hnet_pubkey_list
506 }
507
508 def _encode_bin(self, in_json):
509 return h2b(self._encode_hex(in_json))
510
Harald Weltec91085e2022-02-10 18:05:45 +0100511
Harald Welteb2edd142021-01-08 23:29:35 +0100512class EF_LI(TransRecEF):
Harald Welte13edf302022-07-21 15:19:23 +0200513 def __init__(self, fid='6f05', sfid=None, name='EF.LI', size=(2, None), rec_len=2,
Harald Welteb2edd142021-01-08 23:29:35 +0100514 desc='Language Indication'):
515 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
Harald Weltec91085e2022-02-10 18:05:45 +0100516
Harald Welteb2edd142021-01-08 23:29:35 +0100517 def _decode_record_bin(self, in_bin):
518 if in_bin == b'\xff\xff':
519 return None
520 else:
521 # officially this is 7-bit GSM alphabet with one padding bit in each byte
522 return in_bin.decode('ascii')
Harald Weltec91085e2022-02-10 18:05:45 +0100523
Harald Welteb2edd142021-01-08 23:29:35 +0100524 def _encode_record_bin(self, in_json):
525 if in_json == None:
526 return b'\xff\xff'
527 else:
528 # officially this is 7-bit GSM alphabet with one padding bit in each byte
529 return in_json.encode('ascii')
530
Harald Weltec91085e2022-02-10 18:05:45 +0100531
Harald Welteb2edd142021-01-08 23:29:35 +0100532class EF_Keys(TransparentEF):
Harald Welte13edf302022-07-21 15:19:23 +0200533 def __init__(self, fid='6f08', sfid=0x08, name='EF.Keys', size=(33, 33),
Harald Welteb2edd142021-01-08 23:29:35 +0100534 desc='Ciphering and Integrity Keys'):
535 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Harald Weltec91085e2022-02-10 18:05:45 +0100536 self._construct = Struct(
537 'ksi'/Int8ub, 'ck'/HexAdapter(Bytes(16)), 'ik'/HexAdapter(Bytes(16)))
Harald Welteb2edd142021-01-08 23:29:35 +0100538
Harald Welte14105dc2021-05-31 08:48:51 +0200539# TS 31.102 Section 4.2.6
540class EF_HPPLMN(TransparentEF):
Harald Welte13edf302022-07-21 15:19:23 +0200541 def __init__(self, fid='6f31', sfid=0x12, name='EF.HPPLMN', size=(1, 1),
Harald Welte14105dc2021-05-31 08:48:51 +0200542 desc='Higher Priority PLMN search period'):
543 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
544 self._construct = Int8ub
545
Harald Welte6ca2fa72022-02-12 16:29:31 +0100546class EF_UST(EF_UServiceTable):
547 def __init__(self, **kwargs):
Harald Welte13edf302022-07-21 15:19:23 +0200548 super().__init__(fid='6f38', sfid=0x04, name='EF.UST', desc='USIM Service Table', size=(1,17), table=EF_UST_map, **kwargs)
Harald Welte6ca2fa72022-02-12 16:29:31 +0100549 # add those commands to the general commands of a TransparentEF
550 self.shell_commands += [self.AddlShellCommands()]
551
Harald Welteb2edd142021-01-08 23:29:35 +0100552 @with_default_category('File-Specific Commands')
553 class AddlShellCommands(CommandSet):
554 def __init__(self):
555 super().__init__()
556
557 def do_ust_service_activate(self, arg):
558 """Activate a service within EF.UST"""
559 self._cmd.card.update_ust(int(arg), 1)
560
561 def do_ust_service_deactivate(self, arg):
562 """Deactivate a service within EF.UST"""
563 self._cmd.card.update_ust(int(arg), 0)
564
Harald Welte4c5e2312022-02-12 14:37:48 +0100565 def do_ust_service_check(self, arg):
Harald Welte3bb516b2022-02-12 21:53:18 +0100566 """Check consistency between services of this file and files present/activated.
567
568 Many services determine if one or multiple files shall be present/activated or if they shall be
569 absent/deactivated. This performs a consistency check to ensure that no services are activated
570 for files that are not - and vice-versa, no files are activated for services that are not. Error
571 messages are printed for every inconsistency found."""
Harald Weltea6c0f882022-07-17 14:23:17 +0200572 selected_file = self._cmd.lchan.selected_file
Harald Welte82f75c22022-02-12 18:22:28 +0100573 num_problems = selected_file.ust_service_check(self._cmd)
574 # obtain list of currently active services
575 active_services = selected_file.get_active_services(self._cmd)
576 # Service n°46 can only be declared "available" if service n°45 is declared "available"
577 if 46 in active_services and not 45 in active_services:
Harald Weltefa8b8d12022-02-12 18:30:28 +0100578 self._cmd.perror("ERROR: Service 46 available, but it requires Service 45")
Harald Welte82f75c22022-02-12 18:22:28 +0100579 num_problems += 1
580 # Service n°125 shall only be taken into account if Service n°124 is declared "available"
581 if 125 in active_services and not 124 in active_services:
Harald Weltefa8b8d12022-02-12 18:30:28 +0100582 self._cmd.perror("ERROR: Service 125 is ignored as Service 124 not available")
Harald Welte82f75c22022-02-12 18:22:28 +0100583 num_problems += 1
584 # Service n°95, n°99 and n°115 shall not be declared "available" if an ISIM application is present on the UICC
585 non_isim_services = [95, 99, 115]
586 app_names = selected_file.get_mf().get_app_names()
587 if 'ADF.ISIM' in app_names:
588 for s in non_isim_services:
589 if s in active_services:
Harald Weltefa8b8d12022-02-12 18:30:28 +0100590 self._cmd.perror("ERROR: Service %u shall not be available as ISIM application is present" % s)
Harald Welte82f75c22022-02-12 18:22:28 +0100591 num_problems += 1
592 self._cmd.poutput("===> %u service / file inconsistencies detected" % num_problems)
Harald Welte4c5e2312022-02-12 14:37:48 +0100593
594
Harald Welte89e59542021-04-02 21:33:13 +0200595# TS 31.103 Section 4.2.7 - *not* the same as DF.GSM/EF.ECC!
596class EF_ECC(LinFixedEF):
Harald Welteff2d86d2022-01-21 15:19:47 +0100597 cc_construct = Rpad(BcdAdapter(Rpad(Bytes(3))), pattern='f')
598 category_construct = FlagsEnum(Byte, police=1, ambulance=2, fire_brigade=3, marine_guard=4,
599 mountain_rescue=5, manual_ecall=6, automatic_ecall=7)
600 alpha_construct = GsmStringAdapter(Rpad(GreedyBytes))
Harald Weltec91085e2022-02-10 18:05:45 +0100601
Harald Welte89e59542021-04-02 21:33:13 +0200602 def __init__(self, fid='6fb7', sfid=0x01, name='EF.ECC',
603 desc='Emergency Call Codes'):
Harald Welte99e4cc02022-07-21 15:25:47 +0200604 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=(4, 20))
Harald Weltec91085e2022-02-10 18:05:45 +0100605
Harald Welteff2d86d2022-01-21 15:19:47 +0100606 def _decode_record_bin(self, in_bin):
607 # mandatory parts
608 code = in_bin[:3]
609 if code == b'\xff\xff\xff':
610 return None
611 svc_category = in_bin[-1:]
612 ret = {'call_code': parse_construct(EF_ECC.cc_construct, code),
Harald Weltec91085e2022-02-10 18:05:45 +0100613 'service_category': parse_construct(EF_ECC.category_construct, svc_category)}
Harald Welteff2d86d2022-01-21 15:19:47 +0100614 # optional alpha identifier
615 if len(in_bin) > 4:
616 alpha_id = in_bin[3:-1]
617 ret['alpha_id'] = parse_construct(EF_ECC.alpha_construct, alpha_id)
618 return ret
Harald Weltec91085e2022-02-10 18:05:45 +0100619
Harald Welteff2d86d2022-01-21 15:19:47 +0100620 def _encode_record_bin(self, in_json):
621 if in_json is None:
622 return b'\xff\xff\xff\xff'
623 code = EF_ECC.cc_construct.build(in_json['call_code'])
624 svc_category = EF_ECC.category_construct.build(in_json['category'])
625 alpha_id = EF_ECC.alpha_construct.build(in_json['alpha_id'])
626 # FIXME: alpha_id needs padding up to 'record_length - 4'
627 return code + alpha_id + svc_category
628
Harald Welte89e59542021-04-02 21:33:13 +0200629
Harald Welte790b2702021-04-11 00:01:35 +0200630# TS 31.102 Section 4.2.17
631class EF_LOCI(TransparentEF):
Harald Welte13edf302022-07-21 15:19:23 +0200632 def __init__(self, fid='6f7e', sfid=0x0b, name='EF.LOCI', desc='Location information', size=(11, 11)):
Harald Welte790b2702021-04-11 00:01:35 +0200633 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Harald Welte3a5afff2022-02-25 15:33:46 +0100634 Lai = Struct('mcc_mnc'/BcdAdapter(Bytes(3)), 'lac'/HexAdapter(Bytes(2)))
635 self._construct = Struct('tmsi'/HexAdapter(Bytes(4)), 'lai'/Lai, 'rfu'/Int8ub, 'lu_status'/Int8ub)
Harald Welte592b32e2021-06-05 11:26:36 +0200636# TS 31.102 Section 4.2.18
637class EF_AD(TransparentEF):
638 class OP_MODE(enum.IntEnum):
Harald Weltec91085e2022-02-10 18:05:45 +0100639 normal = 0x00
640 type_approval = 0x80
641 normal_and_specific_facilities = 0x01
642 type_approval_and_specific_facilities = 0x81
643 maintenance_off_line = 0x02
644 cell_test = 0x04
Harald Welte592b32e2021-06-05 11:26:36 +0200645
Harald Welte13edf302022-07-21 15:19:23 +0200646 def __init__(self, fid='6fad', sfid=0x03, name='EF.AD', desc='Administrative Data', size=(4, 6)):
Harald Welte592b32e2021-06-05 11:26:36 +0200647 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
648 self._construct = BitStruct(
649 # Byte 1
650 'ms_operation_mode'/Bytewise(Enum(Byte, EF_AD.OP_MODE)),
651 # Byte 2 + 3
652 'additional_info'/Bytewise(FlagsEnum(Int16ub, ciphering_indicator=1, csg_display_control=2,
653 prose_services=4, extended_drx=8)),
654 'rfu'/BitsRFU(4),
655 'mnc_len'/BitsInteger(4),
656 'extensions'/COptional(Bytewise(GreedyBytesRFU))
657 )
Harald Welte790b2702021-04-11 00:01:35 +0200658
659# TS 31.102 Section 4.2.23
660class EF_PSLOCI(TransparentEF):
Harald Welte13edf302022-07-21 15:19:23 +0200661 def __init__(self, fid='6f73', sfid=0x0c, name='EF.PSLOCI', desc='PS Location information', size=(14, 14)):
Harald Welte790b2702021-04-11 00:01:35 +0200662 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
663 self._construct = Struct('ptmsi'/HexAdapter(Bytes(4)), 'ptmsi_sig'/HexAdapter(Bytes(3)),
664 'rai'/HexAdapter(Bytes(6)), 'rau_status'/Int8ub)
665
666# TS 31.102 Section 4.2.33
667class EF_ICI(CyclicEF):
Harald Welte99e4cc02022-07-21 15:25:47 +0200668 def __init__(self, fid='6f80', sfid=0x14, name='EF.ICI', rec_len=(28, 48),
Harald Welte6169c722022-02-12 09:05:15 +0100669 desc='Incoming Call Information', **kwargs):
670 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte3c98d5e2022-07-20 07:40:05 +0200671 self._construct = Struct('alpha_id'/HexAdapter(Bytes(this._.total_len-28)),
Harald Welte790b2702021-04-11 00:01:35 +0200672 'len_of_bcd_contents'/Int8ub,
673 'ton_npi'/Int8ub,
674 'call_number'/BcdAdapter(Bytes(10)),
675 'cap_cfg2_record_id'/Int8ub,
676 'ext5_record_id'/Int8ub,
677 'date_and_time'/BcdAdapter(Bytes(7)),
678 'duration'/Int24ub,
679 'status'/Byte,
Harald Welte3c98d5e2022-07-20 07:40:05 +0200680 'link_to_phonebook'/HexAdapter(Bytes(3)))
Harald Welte790b2702021-04-11 00:01:35 +0200681
682# TS 31.102 Section 4.2.34
683class EF_OCI(CyclicEF):
Harald Welte99e4cc02022-07-21 15:25:47 +0200684 def __init__(self, fid='6f81', sfid=0x15, name='EF.OCI', rec_len=(27, 47),
Harald Welte6169c722022-02-12 09:05:15 +0100685 desc='Outgoing Call Information', **kwargs):
686 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte3c98d5e2022-07-20 07:40:05 +0200687 self._construct = Struct('alpha_id'/HexAdapter(Bytes(this._.total_len-27)),
Harald Welte790b2702021-04-11 00:01:35 +0200688 'len_of_bcd_contents'/Int8ub,
689 'ton_npi'/Int8ub,
690 'call_number'/BcdAdapter(Bytes(10)),
691 'cap_cfg2_record_id'/Int8ub,
692 'ext5_record_id'/Int8ub,
693 'date_and_time'/BcdAdapter(Bytes(7)),
694 'duration'/Int24ub,
Harald Welte3c98d5e2022-07-20 07:40:05 +0200695 'link_to_phonebook'/HexAdapter(Bytes(3)))
Harald Welte790b2702021-04-11 00:01:35 +0200696
697# TS 31.102 Section 4.2.35
698class EF_ICT(CyclicEF):
Harald Welte99e4cc02022-07-21 15:25:47 +0200699 def __init__(self, fid='6f82', sfid=None, name='EF.ICT', rec_len=(3, 3),
Harald Welte6169c722022-02-12 09:05:15 +0100700 desc='Incoming Call Timer', **kwargs):
701 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200702 self._construct = Struct('accumulated_call_timer'/Int24ub)
703
704# TS 31.102 Section 4.2.38
705class EF_CCP2(LinFixedEF):
Harald Welte6169c722022-02-12 09:05:15 +0100706 def __init__(self, fid='6f4f', sfid=0x16, name='EF.CCP2', desc='Capability Configuration Parameters 2', **kwargs):
Harald Welte99e4cc02022-07-21 15:25:47 +0200707 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=(15, None), **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200708
Harald Welte6ca2fa72022-02-12 16:29:31 +0100709# TS 31.102 Section 4.2.47
710class EF_EST(EF_UServiceTable):
711 def __init__(self, **kwargs):
Harald Welte13edf302022-07-21 15:19:23 +0200712 super().__init__(fid='6f56', sfid=0x05, name='EF.EST', desc='Enabled Services Table', size=(1,None), table=EF_EST_map, **kwargs)
Harald Welte6ca2fa72022-02-12 16:29:31 +0100713 # add those commands to the general commands of a TransparentEF
714 self.shell_commands += [self.AddlShellCommands()]
715
716 @with_default_category('File-Specific Commands')
717 class AddlShellCommands(CommandSet):
718 def __init__(self):
719 super().__init__()
720
721 def do_est_service_activate(self, arg):
722 """Activate a service within EF.UST"""
723 self._cmd.card.update_est(int(arg), 1)
724
725 def do_est_service_deactivate(self, arg):
726 """Deactivate a service within EF.UST"""
727 self._cmd.card.update_est(int(arg), 0)
728
Harald Welte790b2702021-04-11 00:01:35 +0200729# TS 31.102 Section 4.2.48
730class EF_ACL(TransparentEF):
Harald Welte13edf302022-07-21 15:19:23 +0200731 def __init__(self, fid='6f57', sfid=None, name='EF.ACL', size=(32, None),
Harald Welte6169c722022-02-12 09:05:15 +0100732 desc='Access Point Name Control List', **kwargs):
733 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte3c98d5e2022-07-20 07:40:05 +0200734 self._construct = Struct('num_of_apns'/Int8ub, 'tlvs'/HexAdapter(GreedyBytes))
Harald Welte790b2702021-04-11 00:01:35 +0200735
736# TS 31.102 Section 4.2.51
737class EF_START_HFN(TransparentEF):
Harald Welte13edf302022-07-21 15:19:23 +0200738 def __init__(self, fid='6f5b', sfid=0x0f, name='EF.START-HFN', size=(6, 6),
Harald Welte6169c722022-02-12 09:05:15 +0100739 desc='Initialisation values for Hyperframe number', **kwargs):
740 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200741 self._construct = Struct('start_cs'/Int24ub, 'start_ps'/Int24ub)
742
743# TS 31.102 Section 4.2.52
744class EF_THRESHOLD(TransparentEF):
Harald Welte13edf302022-07-21 15:19:23 +0200745 def __init__(self, fid='6f5c', sfid=0x10, name='EF.THRESHOLD', size=(3, 3),
Harald Welte6169c722022-02-12 09:05:15 +0100746 desc='Maximum value of START', **kwargs):
747 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200748 self._construct = Struct('max_start'/Int24ub)
749
Harald Welte363edd92022-07-17 22:24:03 +0200750# TS 31.102 (old releases like 3.8.0) Section 4.2.56
751class EF_RPLMNAcT(TransRecEF):
Harald Welte13edf302022-07-21 15:19:23 +0200752 def __init__(self, fid='6f65', sfid=None, name='EF.RPLMNAcTD', size=(2, 4), rec_len=2,
Harald Welte363edd92022-07-17 22:24:03 +0200753 desc='RPLMN Last used Access Technology', **kwargs):
754 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len, **kwargs)
755 def _decode_record_hex(self, in_hex):
756 return dec_act(in_hex)
757 # TODO: Encode
758
Harald Welte790b2702021-04-11 00:01:35 +0200759# TS 31.102 Section 4.2.77
760class EF_VGCSCA(TransRecEF):
Harald Welte13edf302022-07-21 15:19:23 +0200761 def __init__(self, fid='6fd4', sfid=None, name='EF.VGCSCA', size=(2, 100), rec_len=2,
Harald Welte6169c722022-02-12 09:05:15 +0100762 desc='Voice Group Call Service Ciphering Algorithm', **kwargs):
763 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200764 self._construct = Struct('alg_v_ki_1'/Int8ub, 'alg_v_ki_2'/Int8ub)
765
766# TS 31.102 Section 4.2.79
767class EF_GBABP(TransparentEF):
Harald Welte13edf302022-07-21 15:19:23 +0200768 def __init__(self, fid='6fd6', sfid=None, name='EF.GBABP', size=(3, 50),
Harald Welte6169c722022-02-12 09:05:15 +0100769 desc='GBA Bootstrapping parameters', **kwargs):
770 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200771 self._construct = Struct('rand'/LV, 'b_tid'/LV, 'key_lifetime'/LV)
772
773# TS 31.102 Section 4.2.80
774class EF_MSK(LinFixedEF):
Harald Welte6169c722022-02-12 09:05:15 +0100775 def __init__(self, fid='6fd7', sfid=None, name='EF.MSK', desc='MBMS Service Key List', **kwargs):
Harald Welte99e4cc02022-07-21 15:25:47 +0200776 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=(20, None), **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200777 msk_ts_constr = Struct('msk_id'/Int32ub, 'timestamp_counter'/Int32ub)
Harald Welte3c98d5e2022-07-20 07:40:05 +0200778 self._construct = Struct('key_domain_id'/HexAdapter(Bytes(3)),
Harald Welte790b2702021-04-11 00:01:35 +0200779 'num_msk_id'/Int8ub,
780 'msk_ids'/msk_ts_constr[this.num_msk_id])
Harald Welte14105dc2021-05-31 08:48:51 +0200781# TS 31.102 Section 4.2.81
782class EF_MUK(LinFixedEF):
783 class MUK_Idr(BER_TLV_IE, tag=0x80):
784 _construct = HexAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100785
Harald Welte14105dc2021-05-31 08:48:51 +0200786 class MUK_Idi(BER_TLV_IE, tag=0x82):
787 _construct = HexAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100788
Harald Welte14105dc2021-05-31 08:48:51 +0200789 class MUK_ID(BER_TLV_IE, tag=0xA0, nested=[MUK_Idr, MUK_Idi]):
790 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100791
Harald Welte14105dc2021-05-31 08:48:51 +0200792 class TimeStampCounter(BER_TLV_IE, tag=0x81):
793 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100794
Harald Welte14105dc2021-05-31 08:48:51 +0200795 class EF_MUK_Collection(TLV_IE_Collection, nested=[MUK_ID, TimeStampCounter]):
796 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100797
Harald Welte6169c722022-02-12 09:05:15 +0100798 def __init__(self, fid='6fd8', sfid=None, name='EF.MUK', desc='MBMS User Key', **kwargs):
Harald Welte99e4cc02022-07-21 15:25:47 +0200799 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=(None, None), **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200800 self._tlv = EF_MUK.EF_MUK_Collection
801
802# TS 31.102 Section 4.2.83
803class EF_GBANL(LinFixedEF):
804 class NAF_ID(BER_TLV_IE, tag=0x80):
805 _construct = HexAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100806
Harald Welte14105dc2021-05-31 08:48:51 +0200807 class B_TID(BER_TLV_IE, tag=0x81):
808 _construct = HexAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100809
Harald Welte14105dc2021-05-31 08:48:51 +0200810 class EF_GBANL_Collection(BER_TLV_IE, nested=[NAF_ID, B_TID]):
811 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100812
Harald Welte6169c722022-02-12 09:05:15 +0100813 def __init__(self, fid='6fda', sfid=None, name='EF.GBANL', desc='GBA NAF List', **kwargs):
Harald Welte99e4cc02022-07-21 15:25:47 +0200814 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=(None, None), **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200815 self._tlv = EF_GBANL.EF_GBANL_Collection
Harald Welte790b2702021-04-11 00:01:35 +0200816
817# TS 31.102 Section 4.2.85
818class EF_EHPLMNPI(TransparentEF):
Harald Welte13edf302022-07-21 15:19:23 +0200819 def __init__(self, fid='6fdb', sfid=None, name='EF.EHPLMNPI', size=(1, 1),
Harald Welte6169c722022-02-12 09:05:15 +0100820 desc='Equivalent HPLMN Presentation Indication', **kwargs):
821 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100822 self._construct = Struct('presentation_ind' /
Harald Welte790b2702021-04-11 00:01:35 +0200823 Enum(Byte, no_preference=0, display_highest_prio_only=1, display_all=2))
Harald Welte14105dc2021-05-31 08:48:51 +0200824
825# TS 31.102 Section 4.2.87
826class EF_NAFKCA(LinFixedEF):
827 class NAF_KeyCentreAddress(BER_TLV_IE, tag=0x80):
828 _construct = HexAdapter(GreedyBytes)
Harald Welte99e4cc02022-07-21 15:25:47 +0200829 def __init__(self, fid='6fdd', sfid=None, name='EF.NAFKCA', rec_len=(None, None),
Harald Welte6169c722022-02-12 09:05:15 +0100830 desc='NAF Key Centre Address', **kwargs):
831 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200832 self._tlv = EF_NAFKCA.NAF_KeyCentreAddress
833
834# TS 31.102 Section 4.2.90
835class EF_NCP_IP(LinFixedEF):
836 class DataDestAddrRange(TLV_IE, tag=0x83):
837 _construct = Struct('type_of_address'/Enum(Byte, IPv4=0x21, IPv6=0x56),
838 'prefix_length'/Int8ub,
839 'prefix'/HexAdapter(GreedyBytes))
Harald Weltec91085e2022-02-10 18:05:45 +0100840
Harald Welte14105dc2021-05-31 08:48:51 +0200841 class AccessPointName(TLV_IE, tag=0x80):
842 # coded as per TS 23.003
843 _construct = HexAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100844
Harald Welte14105dc2021-05-31 08:48:51 +0200845 class Login(TLV_IE, tag=0x81):
846 # as per SMS DCS TS 23.038
847 _construct = GsmStringAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100848
Harald Welte14105dc2021-05-31 08:48:51 +0200849 class Password(TLV_IE, tag=0x82):
850 # as per SMS DCS TS 23.038
851 _construct = GsmStringAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100852
Harald Welte14105dc2021-05-31 08:48:51 +0200853 class BearerDescription(TLV_IE, tag=0x84):
854 # Bearer descriptionTLV DO as per TS 31.111
855 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100856
Harald Welte14105dc2021-05-31 08:48:51 +0200857 class EF_NCP_IP_Collection(TLV_IE_Collection,
858 nested=[AccessPointName, Login, Password, BearerDescription]):
859 pass
Harald Welte99e4cc02022-07-21 15:25:47 +0200860 def __init__(self, fid='6fe2', sfid=None, name='EF.NCP-IP', rec_len=(None, None),
Harald Welte6169c722022-02-12 09:05:15 +0100861 desc='Network Connectivity Parameters for USIM IP connections', **kwargs):
862 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200863 self._tlv = EF_NCP_IP.EF_NCP_IP_Collection
864
Harald Welte790b2702021-04-11 00:01:35 +0200865# TS 31.102 Section 4.2.91
866class EF_EPSLOCI(TransparentEF):
Harald Welte12721292022-07-21 15:33:06 +0200867 def __init__(self, fid='6fe3', sfid=0x1e, name='EF.EPSLOCI',
868 desc='EPS Location Information', size=(18,18), **kwargs):
Harald Welte6169c722022-02-12 09:05:15 +0100869 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100870 upd_status_constr = Enum(
871 Byte, updated=0, not_updated=1, roaming_not_allowed=2)
Harald Welte3c98d5e2022-07-20 07:40:05 +0200872 self._construct = Struct('guti'/HexAdapter(Bytes(12)),
873 'last_visited_registered_tai'/HexAdapter(Bytes(5)),
Harald Welte790b2702021-04-11 00:01:35 +0200874 'eps_update_status'/upd_status_constr)
875
Harald Welte14105dc2021-05-31 08:48:51 +0200876# TS 31.102 Section 4.2.92
877class EF_EPSNSC(LinFixedEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100878 class KSI_ASME(BER_TLV_IE, tag=0x80):
Harald Welte14105dc2021-05-31 08:48:51 +0200879 _construct = Int8ub
Harald Weltec91085e2022-02-10 18:05:45 +0100880
881 class K_ASME(BER_TLV_IE, tag=0x81):
Harald Welte14105dc2021-05-31 08:48:51 +0200882 _construct = HexAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100883
Harald Welte14105dc2021-05-31 08:48:51 +0200884 class UplinkNASCount(BER_TLV_IE, tag=0x82):
885 _construct = Int32ub
Harald Weltec91085e2022-02-10 18:05:45 +0100886
Harald Welte14105dc2021-05-31 08:48:51 +0200887 class DownlinkNASCount(BER_TLV_IE, tag=0x83):
888 _construct = Int32ub
Harald Weltec91085e2022-02-10 18:05:45 +0100889
Harald Welte14105dc2021-05-31 08:48:51 +0200890 class IDofNASAlgorithms(BER_TLV_IE, tag=0x84):
891 _construct = HexAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100892
Harald Welte14105dc2021-05-31 08:48:51 +0200893 class EPS_NAS_Security_Context(BER_TLV_IE, tag=0xa0,
Harald Weltec91085e2022-02-10 18:05:45 +0100894 nested=[KSI_ASME, K_ASME, UplinkNASCount, DownlinkNASCount,
895 IDofNASAlgorithms]):
Harald Welte14105dc2021-05-31 08:48:51 +0200896 pass
Harald Welte99e4cc02022-07-21 15:25:47 +0200897 def __init__(self, fid='6fe4', sfid=0x18, name='EF.EPSNSC', rec_len=(54, 128),
Harald Welte6169c722022-02-12 09:05:15 +0100898 desc='EPS NAS Security Context', **kwargs):
899 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200900 self._tlv = EF_EPSNSC.EPS_NAS_Security_Context
901
Harald Welte790b2702021-04-11 00:01:35 +0200902# TS 31.102 Section 4.2.96
903class EF_PWS(TransparentEF):
Harald Welte13edf302022-07-21 15:19:23 +0200904 def __init__(self, fid='6fec', sfid=None, name='EF.PWS', desc='Public Warning System', size=(1, 1), **kwargs):
Harald Welte6169c722022-02-12 09:05:15 +0100905 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100906 pws_config = FlagsEnum(
907 Byte, ignore_pws_in_hplmn_and_equivalent=1, ignore_pws_in_vplmn=2)
Harald Welte790b2702021-04-11 00:01:35 +0200908 self._construct = Struct('pws_configuration'/pws_config)
909
910# TS 31.102 Section 4.2.101
911class EF_IPS(CyclicEF):
Harald Welte99e4cc02022-07-21 15:25:47 +0200912 def __init__(self, fid='6ff1', sfid=None, name='EF.IPS', rec_len=(4, 4),
Harald Welte6169c722022-02-12 09:05:15 +0100913 desc='IMEI(SV) Pairing Status', **kwargs):
914 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200915 self._construct = Struct('status'/PaddedString(2, 'ascii'),
916 'link_to_ef_ipd'/Int8ub, 'rfu'/Byte)
917
Harald Welte14105dc2021-05-31 08:48:51 +0200918# TS 31.102 Section 4.2.103
919class EF_ePDGId(TransparentEF):
920 class ePDGId(BER_TLV_IE, tag=0x80, nested=[]):
921 _construct = Struct('type_of_ePDG_address'/Enum(Byte, FQDN=0, IPv4=1, IPv6=2),
922 'ePDG_address'/Switch(this.type_of_address,
Harald Weltec91085e2022-02-10 18:05:45 +0100923 {'FQDN': GreedyString("utf8"),
924 'IPv4': HexAdapter(GreedyBytes),
925 'IPv6': HexAdapter(GreedyBytes)}))
926
Harald Welte6169c722022-02-12 09:05:15 +0100927 def __init__(self, fid='6ff3', sfid=None, name='EF.eDPDGId', desc='Home ePDG Identifier', **kwargs):
928 super().__init__(fid, sfid=sfid, name=name, desc=desc, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200929 self._tlv = EF_ePDGId.ePDGId
930
Harald Welte71290072021-04-21 10:58:24 +0200931# TS 31.102 Section 4.2.106
932class EF_FromPreferred(TransparentEF):
Harald Welte13edf302022-07-21 15:19:23 +0200933 def __init__(self, fid='6ff7', sfid=None, name='EF.FromPreferred', size=(1, 1),
Harald Welte6169c722022-02-12 09:05:15 +0100934 desc='From Preferred', **kwargs):
935 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte71290072021-04-21 10:58:24 +0200936 self._construct = BitStruct('rfu'/BitsRFU(7), 'from_preferred'/Bit)
937
Harald Welted90ceb82022-07-17 22:10:58 +0200938
939######################################################################
940# DF.GSM-ACCESS
941######################################################################
942
943class DF_GSM_ACCESS(CardDF):
944 def __init__(self, fid='5F3B', name='DF.GSM-ACCESS', desc='GSM Access', **kwargs):
945 super().__init__(fid=fid, name=name, desc=desc, service=27, **kwargs)
946 files = [
947 EF_Kc(fid='4f20', sfid=0x01, service=27),
948 EF_Kc(fid='4f52', sfid=0x02, name='EF.KcGPRS', desc='GPRS Ciphering key KcGPRS', service=27),
949 EF_CPBCCH(fid='4f63', service=39),
950 EF_InvScan(fid='4f64', service=40),
951 ]
952 self.add_files(files)
953
954
Harald Welte790b2702021-04-11 00:01:35 +0200955######################################################################
956# DF.5GS
957######################################################################
958
959# TS 31.102 Section 4.4.11.2
960class EF_5GS3GPPLOCI(TransparentEF):
Harald Welte13edf302022-07-21 15:19:23 +0200961 def __init__(self, fid='4f01', sfid=0x01, name='EF.5GS3GPPLOCI', size=(20, 20),
Harald Welte419bb492022-02-12 21:39:35 +0100962 desc='5S 3GP location information', **kwargs):
963 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100964 upd_status_constr = Enum(
965 Byte, updated=0, not_updated=1, roaming_not_allowed=2)
Harald Welte3c98d5e2022-07-20 07:40:05 +0200966 self._construct = Struct('5g_guti'/HexAdapter(Bytes(13)),
967 'last_visited_registered_tai_in_5gs'/HexAdapter(Bytes(6)),
Harald Welte790b2702021-04-11 00:01:35 +0200968 '5gs_update_status'/upd_status_constr)
969
970# TS 31.102 Section 4.4.11.7
971class EF_UAC_AIC(TransparentEF):
Harald Welte13edf302022-07-21 15:19:23 +0200972 def __init__(self, fid='4f06', sfid=0x06, name='EF.UAC_AIC', size=(4, 4),
Harald Welte419bb492022-02-12 21:39:35 +0100973 desc='UAC Access Identities Configuration', **kwargs):
974 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200975 cfg_constr = FlagsEnum(Byte, multimedia_priority_service=1,
Harald Weltec91085e2022-02-10 18:05:45 +0100976 mission_critical_service=2)
Harald Welte790b2702021-04-11 00:01:35 +0200977 self._construct = Struct('uac_access_id_config'/cfg_constr)
978
Harald Welte14105dc2021-05-31 08:48:51 +0200979# TS 31.102 Section 4.4.11.9
Harald Welte790b2702021-04-11 00:01:35 +0200980class EF_OPL5G(LinFixedEF):
Harald Welte419bb492022-02-12 21:39:35 +0100981 def __init__(self, fid='6f08', sfid=0x08, name='EF.OPL5G', desc='5GS Operator PLMN List', **kwargs):
Harald Welte99e4cc02022-07-21 15:25:47 +0200982 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=(10, None), **kwargs)
Harald Welte3c98d5e2022-07-20 07:40:05 +0200983 Tai = Struct('mcc_mnc'/BcdAdapter(Bytes(3)), 'tac_min'/HexAdapter(Bytes(3)),
984 'tac_max'/HexAdapter(Bytes(3)))
Harald Weltea0377622022-02-25 15:36:44 +0100985 self._construct = Struct('tai'/Tai, 'pnn_record_id'/Int8ub)
Harald Welte790b2702021-04-11 00:01:35 +0200986
Harald Welte14105dc2021-05-31 08:48:51 +0200987# TS 31.102 Section 4.4.11.10
988class EF_SUPI_NAI(TransparentEF):
989 class NetworkSpecificIdentifier(TLV_IE, tag=0x80):
990 # RFC 7542 encoded as UTF-8 string
991 _construct = GreedyString("utf8")
Harald Weltec91085e2022-02-10 18:05:45 +0100992
Harald Welte14105dc2021-05-31 08:48:51 +0200993 class GlobalLineIdentifier(TLV_IE, tag=0x81):
994 # TS 23.003 clause 28.16.2
995 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100996
Harald Welte14105dc2021-05-31 08:48:51 +0200997 class GlobalCableIdentifier(TLV_IE, tag=0x82):
998 # TS 23.003 clause 28.15.2
999 pass
Harald Weltec91085e2022-02-10 18:05:45 +01001000
Harald Welte14105dc2021-05-31 08:48:51 +02001001 class NAI_TLV_Collection(TLV_IE_Collection,
Harald Weltec91085e2022-02-10 18:05:45 +01001002 nested=[NetworkSpecificIdentifier, GlobalLineIdentifier, GlobalCableIdentifier]):
Harald Welte14105dc2021-05-31 08:48:51 +02001003 pass
1004 def __init__(self, fid='4f09', sfid=0x09, name='EF.SUPI_NAI',
Harald Welte419bb492022-02-12 21:39:35 +01001005 desc='SUPI as Network Access Identifier', **kwargs):
1006 super().__init__(fid, sfid=sfid, name=name, desc=desc, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +02001007 self._tlv = EF_SUPI_NAI.NAI_TLV_Collection
1008
Harald Weltec91085e2022-02-10 18:05:45 +01001009
Harald Welte14105dc2021-05-31 08:48:51 +02001010class EF_TN3GPPSNN(TransparentEF):
1011 class ServingNetworkName(BER_TLV_IE, tag=0x80):
1012 _construct = GreedyString("utf8")
1013 def __init__(self, fid='4f0c', sfid=0x0c, name='EF.TN3GPPSNN',
Harald Welte419bb492022-02-12 21:39:35 +01001014 desc='Trusted non-3GPP Serving network names list', **kwargs):
1015 super().__init__(fid, sfid=sfid, name=name, desc=desc, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +02001016 self._tlv = EF_TN3GPPSNN.ServingNetworkName
1017
Harald Welte3990ebb2021-04-20 23:55:14 +02001018# TS 31.102 Section 4.4.5
1019class DF_WLAN(CardDF):
Harald Welte6169c722022-02-12 09:05:15 +01001020 def __init__(self, fid='5f40', name='DF.WLAN', desc='Files for WLAN purpose', **kwargs):
1021 super().__init__(fid=fid, name=name, desc=desc, **kwargs)
Harald Welte3990ebb2021-04-20 23:55:14 +02001022 files = [
Harald Welte419bb492022-02-12 21:39:35 +01001023 TransparentEF('4f41', 0x01, 'EF.Pseudo', 'Pseudonym', service=59),
Harald Weltec91085e2022-02-10 18:05:45 +01001024 TransparentEF('4f42', 0x02, 'EF.UPLMNWLAN',
Harald Welte419bb492022-02-12 21:39:35 +01001025 'User controlled PLMN selector for I-WLAN Access', service=60),
Harald Weltec91085e2022-02-10 18:05:45 +01001026 TransparentEF('4f43', 0x03, 'EF.OPLMNWLAN',
Harald Welte419bb492022-02-12 21:39:35 +01001027 'Operator controlled PLMN selector for I-WLAN Access', service=61),
Harald Weltec91085e2022-02-10 18:05:45 +01001028 LinFixedEF('4f44', 0x04, 'EF.UWSIDL',
Harald Welte419bb492022-02-12 21:39:35 +01001029 'User controlled WLAN Specific Identifier List', service=62),
Harald Weltec91085e2022-02-10 18:05:45 +01001030 LinFixedEF('4f45', 0x05, 'EF.OWSIDL',
Harald Welte419bb492022-02-12 21:39:35 +01001031 'Operator controlled WLAN Specific Identifier List', service=63),
Harald Weltec91085e2022-02-10 18:05:45 +01001032 TransparentEF('4f46', 0x06, 'EF.WRI',
Harald Welte419bb492022-02-12 21:39:35 +01001033 'WLAN Reauthentication Identity', service=66),
Harald Weltec91085e2022-02-10 18:05:45 +01001034 LinFixedEF('4f47', 0x07, 'EF.HWSIDL',
Harald Welte419bb492022-02-12 21:39:35 +01001035 'Home I-WLAN Specific Identifier List', service=81),
Harald Weltec91085e2022-02-10 18:05:45 +01001036 TransparentEF('4f48', 0x08, 'EF.WEHPLMNPI',
Harald Welte419bb492022-02-12 21:39:35 +01001037 'I-WLAN Equivalent HPLMN Presentation Indication', service=82),
Harald Weltec91085e2022-02-10 18:05:45 +01001038 TransparentEF('4f49', 0x09, 'EF.WHPI',
Harald Welte419bb492022-02-12 21:39:35 +01001039 'I-WLAN HPLMN Priority Indication', service=83),
Harald Weltec91085e2022-02-10 18:05:45 +01001040 TransparentEF('4f4a', 0x0a, 'EF.WLRPLMN',
Harald Welte419bb492022-02-12 21:39:35 +01001041 'I-WLAN Last Registered PLMN', service=84),
Harald Weltec91085e2022-02-10 18:05:45 +01001042 TransparentEF('4f4b', 0x0b, 'EF.HPLMNDAI',
Harald Welte419bb492022-02-12 21:39:35 +01001043 'HPLMN Direct Access Indicator', service=88),
Harald Weltec91085e2022-02-10 18:05:45 +01001044 ]
Harald Welte3990ebb2021-04-20 23:55:14 +02001045 self.add_files(files)
1046
1047# TS 31.102 Section 4.4.6
1048class DF_HNB(CardDF):
Harald Welte6169c722022-02-12 09:05:15 +01001049 def __init__(self, fid='5f50', name='DF.HNB', desc='Files for HomeNodeB purpose', **kwargs):
1050 super().__init__(fid=fid, name=name, desc=desc, **kwargs)
Harald Welte3990ebb2021-04-20 23:55:14 +02001051 files = [
Harald Welteeb882052022-07-17 21:13:57 +02001052 LinFixedEF('4f81', 0x01, 'EF.ACSGL', 'Allowed CSG Lists', service=86),
1053 LinFixedEF('4f82', 0x02, 'EF.CSGTL', 'CSG Types', service=86),
1054 LinFixedEF('4f83', 0x03, 'EF.HNBN', 'Home NodeB Name', service=86),
1055 LinFixedEF('4f84', 0x04, 'EF.OCSGL', 'Operator CSG Lists', service=90),
1056 LinFixedEF('4f85', 0x05, 'EF.OCSGT', 'Operator CSG Type', service=90),
1057 LinFixedEF('4f86', 0x06, 'EF.OHNBN', 'Operator Home NodeB Name', service=90),
Harald Weltec91085e2022-02-10 18:05:45 +01001058 ]
Harald Welte3990ebb2021-04-20 23:55:14 +02001059 self.add_files(files)
1060
1061# TS 31.102 Section 4.4.8
1062class DF_ProSe(CardDF):
Harald Welte6169c722022-02-12 09:05:15 +01001063 def __init__(self, fid='5f90', name='DF.ProSe', desc='Files for ProSe purpose', **kwargs):
1064 super().__init__(fid=fid, name=name, desc=desc, **kwargs)
Harald Welte3990ebb2021-04-20 23:55:14 +02001065 files = [
Harald Weltec91085e2022-02-10 18:05:45 +01001066 LinFixedEF('4f01', 0x01, 'EF.PROSE_MON',
1067 'ProSe Monitoring Parameters'),
1068 LinFixedEF('4f02', 0x02, 'EF.PROSE_ANN',
1069 'ProSe Announcing Parameters'),
Harald Welte3990ebb2021-04-20 23:55:14 +02001070 LinFixedEF('4f03', 0x03, 'EF.PROSEFUNC', 'HPLMN ProSe Function'),
Harald Weltec91085e2022-02-10 18:05:45 +01001071 TransparentEF('4f04', 0x04, 'EF.PROSE_RADIO_COM',
1072 'ProSe Direct Communication Radio Parameters'),
1073 TransparentEF('4f05', 0x05, 'EF.PROSE_RADIO_MON',
1074 'ProSe Direct Discovery Monitoring Radio Parameters'),
1075 TransparentEF('4f06', 0x06, 'EF.PROSE_RADIO_ANN',
1076 'ProSe Direct Discovery Announcing Radio Parameters'),
1077 LinFixedEF('4f07', 0x07, 'EF.PROSE_POLICY',
1078 'ProSe Policy Parameters'),
Harald Welte3990ebb2021-04-20 23:55:14 +02001079 LinFixedEF('4f08', 0x08, 'EF.PROSE_PLMN', 'ProSe PLMN Parameters'),
1080 TransparentEF('4f09', 0x09, 'EF.PROSE_GC', 'ProSe Group Counter'),
1081 TransparentEF('4f10', 0x10, 'EF.PST', 'ProSe Service Table'),
Harald Weltec91085e2022-02-10 18:05:45 +01001082 TransparentEF('4f11', 0x11, 'EF.UIRC',
1083 'ProSe UsageInformationReportingConfiguration'),
1084 LinFixedEF('4f12', 0x12, 'EF.PROSE_GM_DISCOVERY',
1085 'ProSe Group Member Discovery Parameters'),
1086 LinFixedEF('4f13', 0x13, 'EF.PROSE_RELAY',
1087 'ProSe Relay Parameters'),
1088 TransparentEF('4f14', 0x14, 'EF.PROSE_RELAY_DISCOVERY',
1089 'ProSe Relay Discovery Parameters'),
1090 ]
Harald Welte3990ebb2021-04-20 23:55:14 +02001091 self.add_files(files)
1092
Harald Weltec91085e2022-02-10 18:05:45 +01001093
Merlin Chlosta05ca36b2021-04-01 16:15:28 +02001094class DF_USIM_5GS(CardDF):
Harald Welte6169c722022-02-12 09:05:15 +01001095 def __init__(self, fid='5FC0', name='DF.5GS', desc='5GS related files', **kwargs):
1096 super().__init__(fid=fid, name=name, desc=desc, **kwargs)
Merlin Chlosta05ca36b2021-04-01 16:15:28 +02001097 files = [
Harald Weltec91085e2022-02-10 18:05:45 +01001098 # I'm looking at 31.102 R16.6
Harald Welte419bb492022-02-12 21:39:35 +01001099 EF_5GS3GPPLOCI(service=122),
Harald Weltec91085e2022-02-10 18:05:45 +01001100 EF_5GS3GPPLOCI('4f02', 0x02, 'EF.5GSN3GPPLOCI',
Harald Welte419bb492022-02-12 21:39:35 +01001101 '5GS non-3GPP location information', service=122),
1102 EF_5GS3GPPNSC(service=122),
Harald Weltec91085e2022-02-10 18:05:45 +01001103 EF_5GS3GPPNSC('4f04', 0x04, 'EF.5GSN3GPPNSC',
Harald Welte419bb492022-02-12 21:39:35 +01001104 '5GS non-3GPP Access NAS Security Context', service=122),
1105 EF_5GAUTHKEYS(service=123),
1106 EF_UAC_AIC(service=126),
1107 EF_SUCI_Calc_Info(service=124),
1108 EF_OPL5G(service=129),
1109 EF_SUPI_NAI(service=130),
Harald Weltec91085e2022-02-10 18:05:45 +01001110 TransparentEF('4F0A', 0x0a, 'EF.Routing_Indicator',
Harald Welte13edf302022-07-21 15:19:23 +02001111 'Routing Indicator', size=(4, 4), service=124),
Harald Weltec91085e2022-02-10 18:05:45 +01001112 TransparentEF('4F0B', 0x0b, 'EF.URSP',
Harald Welte419bb492022-02-12 21:39:35 +01001113 'UE Route Selector Policies per PLMN', service=132),
1114 EF_TN3GPPSNN(service=133),
Merlin Chlosta05ca36b2021-04-01 16:15:28 +02001115 ]
Merlin Chlosta05ca36b2021-04-01 16:15:28 +02001116 self.add_files(files)
1117
Harald Weltec91085e2022-02-10 18:05:45 +01001118
Harald Welteb2edd142021-01-08 23:29:35 +01001119class ADF_USIM(CardADF):
1120 def __init__(self, aid='a0000000871002', name='ADF.USIM', fid=None, sfid=None,
1121 desc='USIM Application'):
1122 super().__init__(aid=aid, fid=fid, sfid=sfid, name=name, desc=desc)
Harald Welte15fae982021-04-10 10:22:27 +02001123 # add those commands to the general commands of a TransparentEF
1124 self.shell_commands += [self.AddlShellCommands()]
Harald Welteb2edd142021-01-08 23:29:35 +01001125
1126 files = [
Harald Weltec91085e2022-02-10 18:05:45 +01001127 EF_LI(sfid=0x02),
1128 EF_IMSI(sfid=0x07),
1129 EF_Keys(),
1130 EF_Keys('6f09', 0x09, 'EF.KeysPS',
1131 desc='Ciphering and Integrity Keys for PS domain'),
1132 EF_xPLMNwAcT('6f60', 0x0a, 'EF.PLMNwAcT',
Harald Welte6169c722022-02-12 09:05:15 +01001133 'User controlled PLMN Selector with Access Technology', service=20),
Harald Weltec91085e2022-02-10 18:05:45 +01001134 EF_HPPLMN(),
Harald Welte6169c722022-02-12 09:05:15 +01001135 EF_ACMmax(service=13),
Harald Welte6ca2fa72022-02-12 16:29:31 +01001136 EF_UST(),
Harald Weltec91085e2022-02-10 18:05:45 +01001137 CyclicEF('6f39', None, 'EF.ACM',
Harald Welte99e4cc02022-07-21 15:25:47 +02001138 'Accumulated call meter', rec_len=(3, 3), service=13),
Harald Welte6169c722022-02-12 09:05:15 +01001139 TransparentEF('6f3e', None, 'EF.GID1', 'Group Identifier Level 1', service=17),
1140 TransparentEF('6f3f', None, 'EF.GID2', 'Group Identifier Level 2', service=18),
1141 EF_SPN(service=19),
Harald Weltec91085e2022-02-10 18:05:45 +01001142 TransparentEF('6f41', None, 'EF.PUCT',
Harald Welte13edf302022-07-21 15:19:23 +02001143 'Price per unit and currency table', size=(5, 5), service=13),
Harald Welte6169c722022-02-12 09:05:15 +01001144 EF_CBMI(service=15),
Harald Weltec91085e2022-02-10 18:05:45 +01001145 EF_ACC(sfid=0x06),
1146 EF_PLMNsel('6f7b', 0x0d, 'EF.FPLMN',
Harald Welte13edf302022-07-21 15:19:23 +02001147 'Forbidden PLMNs', size=(12, None)),
Harald Weltec91085e2022-02-10 18:05:45 +01001148 EF_LOCI(),
1149 EF_AD(),
Harald Welte6169c722022-02-12 09:05:15 +01001150 EF_CBMID(sfid=0x0e, service=29),
Harald Weltec91085e2022-02-10 18:05:45 +01001151 EF_ECC(),
Harald Welte6169c722022-02-12 09:05:15 +01001152 EF_CBMIR(service=16),
Harald Weltec91085e2022-02-10 18:05:45 +01001153 EF_PSLOCI(),
Harald Welte6169c722022-02-12 09:05:15 +01001154 EF_ADN('6f3b', None, 'EF.FDN', 'Fixed Dialling Numbers', service=[2, 89]),
1155 EF_SMS('6f3c', None, service=10),
1156 EF_MSISDN(service=21),
1157 EF_SMSP(service=12),
1158 EF_SMSS(service=10),
1159 EF_ADN('6f49', None, 'EF.SDN', 'Service Dialling Numbers', service=[4, 89]),
1160 EF_EXT('6f4b', None, 'EF.EXT2', 'Extension2 (FDN)', service=3),
1161 EF_EXT('6f4c', None, 'EF.EXT3', 'Extension2 (SDN)', service=5),
1162 EF_SMSR(service=11),
1163 EF_ICI(service=9),
1164 EF_OCI(service=8),
1165 EF_ICT(service=9),
1166 EF_ICT('6f83', None, 'EF.OCT', 'Outgoing Call Timer', service=8),
1167 EF_EXT('6f4e', None, 'EF.EXT5', 'Extension5 (ICI/OCI/MSISDN)', service=44),
1168 EF_CCP2(service=14),
1169 EF_eMLPP(service=24),
1170 EF_AAeM(service=25),
Harald Weltec91085e2022-02-10 18:05:45 +01001171 # EF_Hiddenkey
Harald Welte6169c722022-02-12 09:05:15 +01001172 EF_ADN('6f4d', None, 'EF.BDN', 'Barred Dialling Numbers', service=6),
1173 EF_EXT('6f55', None, 'EF.EXT4', 'Extension4 (BDN/SSC)', service=7),
1174 EF_CMI(service=6),
Harald Welte6ca2fa72022-02-12 16:29:31 +01001175 EF_EST(service=[2, 6, 34, 35]),
Harald Welte6169c722022-02-12 09:05:15 +01001176 EF_ACL(service=35),
1177 EF_DCK(service=36),
1178 EF_CNL(service=37),
Harald Weltec91085e2022-02-10 18:05:45 +01001179 EF_START_HFN(),
1180 EF_THRESHOLD(),
Harald Welte6169c722022-02-12 09:05:15 +01001181 EF_xPLMNwAcT('6f61', 0x11, 'EF.OPLMNwAcT', 'User controlled PLMN Selector with Access Technology', service=42),
1182 EF_xPLMNwAcT('6f62', 0x13, 'EF.HPLMNwAcT', 'HPLMN Selector with Access Technology', service=43),
Harald Weltec91085e2022-02-10 18:05:45 +01001183 EF_ARR('6f06', 0x17),
Harald Welte363edd92022-07-17 22:24:03 +02001184 EF_RPLMNAcT(),
Harald Weltec91085e2022-02-10 18:05:45 +01001185 TransparentEF('6fc4', None, 'EF.NETPAR', 'Network Parameters'),
Harald Welte6169c722022-02-12 09:05:15 +01001186 EF_PNN('6fc5', 0x19, service=45),
1187 EF_OPL(service=46),
1188 EF_ADN('6fc7', None, 'EF.MBDN', 'Mailbox Dialling Numbers', service=47),
1189 EF_EXT('6fc8', None, 'EF.EXT6', 'Extension6 (MBDN)'),
1190 EF_MBI(service=47),
1191 EF_MWIS(service=48),
1192 EF_ADN('6fcb', None, 'EF.CFIS', 'Call Forwarding Indication Status', service=49),
Harald Weltec91085e2022-02-10 18:05:45 +01001193 EF_EXT('6fcc', None, 'EF.EXT7', 'Extension7 (CFIS)'),
Harald Welte6169c722022-02-12 09:05:15 +01001194 TransparentEF('6fcd', None, 'EF.SPDI', 'Service Provider Display Information', service=51),
1195 EF_MMSN(service=52),
1196 EF_EXT('6fcf', None, 'EF.EXT8', 'Extension8 (MMSN)', service=53),
1197 EF_MMSICP(service=52),
1198 EF_MMSUP(service=52),
1199 EF_MMSUCP(service=(52, 55)),
1200 EF_NIA(service=56),
1201 EF_VGCS(service=57),
1202 EF_VGCSS(service=57),
1203 EF_VGCS('6fb3', None, 'EF.VBS', 'Voice Broadcast Service', service=58),
1204 EF_VGCSS('6fb4', None, 'EF.VBSS', 'Voice Broadcast Service Status', service=58),
1205 EF_VGCSCA(service=64),
1206 EF_VGCSCA('6fd5', None, 'EF.VBCSCA', 'Voice Broadcast Service Ciphering Algorithm', service=65),
1207 EF_GBABP(service=68),
1208 EF_MSK(service=69),
1209 EF_MUK(service=69),
1210 EF_GBANL(service=68),
Harald Welte13edf302022-07-21 15:19:23 +02001211 EF_PLMNsel('6fd9', 0x1d, 'EF.EHPLMN', 'Equivalent HPLMN', size=(12, None), service=71),
Harald Welte6169c722022-02-12 09:05:15 +01001212 EF_EHPLMNPI(service=(71, 73)),
1213 # EF_LRPLMNSI ('6fdc', service=74)
1214 EF_NAFKCA(service=(68, 76)),
1215 TransparentEF('6fde', None, 'EF.SPNI', 'Service Provider Name Icon', service=78),
1216 LinFixedEF('6fdf', None, 'EF.PNNI', 'PLMN Network Name Icon', service=79),
1217 EF_NCP_IP(service=80),
1218 EF_EPSLOCI('6fe3', 0x1e, 'EF.EPSLOCI', 'EPS location information', service=85),
1219 EF_EPSNSC(service=85),
Harald Welte13edf302022-07-21 15:19:23 +02001220 TransparentEF('6fe6', None, 'EF.UFC', 'USAT Facility Control', size=(1, 16)),
Harald Welte6169c722022-02-12 09:05:15 +01001221 TransparentEF('6fe8', None, 'EF.NASCONFIG', 'Non Access Stratum Configuration', service=96),
1222 # UICC IARI (only in cards that have no ISIM) service=95
1223 EF_PWS(service=97),
1224 LinFixedEF('6fed', None, 'EF.FDNURI', 'Fixed Dialling Numbers URI', service=(2, 99)),
1225 LinFixedEF('6fee', None, 'EF.BDNURI', 'Barred Dialling Numbers URI', service=(6, 99)),
1226 LinFixedEF('6fef', None, 'EF.SDNURI', 'Service Dialling Numbers URI', service=(4, 99)),
1227 # EF_IWL (IMEI(SV) White List)
Harald Weltec91085e2022-02-10 18:05:45 +01001228 EF_IPS(),
Harald Welte6169c722022-02-12 09:05:15 +01001229 EF_ePDGId(service=(106, 107)),
Harald Weltec91085e2022-02-10 18:05:45 +01001230 # FIXME: from EF_ePDGSelection onwards
Harald Welte6169c722022-02-12 09:05:15 +01001231 EF_FromPreferred(service=114),
1232 # FIXME: DF_SoLSA service=23
Harald Weltede4c14c2022-07-16 11:53:59 +02001233 DF_PHONEBOOK(),
Harald Welted90ceb82022-07-17 22:10:58 +02001234 DF_GSM_ACCESS(),
Harald Welte6169c722022-02-12 09:05:15 +01001235 DF_WLAN(service=[59, 60, 61, 62, 63, 66, 81, 82, 83, 84, 88]),
1236 DF_HNB(service=[86, 90]),
1237 DF_ProSe(service=101),
1238 # FIXME: DF_ACDC service=108
1239 # FIXME: DF_TV service=116
1240 DF_USIM_5GS(service=[122, 123, 124, 125, 126, 127, 129, 130]),
Harald Weltec91085e2022-02-10 18:05:45 +01001241 ]
Harald Welteb2edd142021-01-08 23:29:35 +01001242 self.add_files(files)
1243
1244 def decode_select_response(self, data_hex):
Philipp Maier5998a3a2021-11-16 15:16:39 +01001245 return pySim.ts_102_221.CardProfileUICC.decode_select_response(data_hex)
Harald Welteb2edd142021-01-08 23:29:35 +01001246
Harald Welte15fae982021-04-10 10:22:27 +02001247 @with_default_category('Application-Specific Commands')
1248 class AddlShellCommands(CommandSet):
1249 def __init__(self):
1250 super().__init__()
1251
1252 authenticate_parser = argparse.ArgumentParser()
1253 authenticate_parser.add_argument('rand', help='Random challenge')
1254 authenticate_parser.add_argument('autn', help='Authentication Nonce')
1255 #authenticate_parser.add_argument('--context', help='Authentication context', default='3G')
Harald Weltec91085e2022-02-10 18:05:45 +01001256
Harald Welte15fae982021-04-10 10:22:27 +02001257 @cmd2.with_argparser(authenticate_parser)
1258 def do_authenticate(self, opts):
1259 """Perform Authentication and Key Agreement (AKA)."""
1260 (data, sw) = self._cmd.card._scc.authenticate(opts.rand, opts.autn)
1261 self._cmd.poutput_json(data)
1262
Harald Welte12af7932022-02-15 16:39:08 +01001263 term_prof_parser = argparse.ArgumentParser()
1264 term_prof_parser.add_argument('PROFILE', help='Hexstring of encoded terminal profile')
1265
1266 @cmd2.with_argparser(term_prof_parser)
Harald Welte846a8982021-10-08 15:47:16 +02001267 def do_terminal_profile(self, arg):
Harald Welte12af7932022-02-15 16:39:08 +01001268 """Send a TERMINAL PROFILE command to the card.
1269 This is used to inform the card about which optional
1270 features the terminal (modem/phone) supports, particularly
1271 in the context of SIM Toolkit, Proactive SIM and OTA. You
1272 must specify a hex-string with the encoded terminal profile
1273 you want to send to the card."""
Harald Welte846a8982021-10-08 15:47:16 +02001274 (data, sw) = self._cmd.card._scc.terminal_profile(arg)
1275 self._cmd.poutput('SW: %s, data: %s' % (sw, data))
Harald Welte15fae982021-04-10 10:22:27 +02001276
Harald Welte12af7932022-02-15 16:39:08 +01001277 envelope_parser = argparse.ArgumentParser()
1278 envelope_parser.add_argument('PAYLOAD', help='Hexstring of encoded payload to ENVELOPE')
1279
1280 @cmd2.with_argparser(envelope_parser)
Harald Welte7cb94e42021-10-08 15:47:57 +02001281 def do_envelope(self, arg):
Harald Welte12af7932022-02-15 16:39:08 +01001282 """Send an ENVELOPE command to the card. This is how a
1283 variety of information is communicated from the terminal
1284 (modem/phone) to the card, particularly in the context of
1285 SIM Toolkit, Proactive SIM and OTA."""
Harald Welte7cb94e42021-10-08 15:47:57 +02001286 (data, sw) = self._cmd.card._scc.envelope(arg)
1287 self._cmd.poutput('SW: %s, data: %s' % (sw, data))
1288
Harald Welte12af7932022-02-15 16:39:08 +01001289 envelope_sms_parser = argparse.ArgumentParser()
1290 envelope_sms_parser.add_argument('TPDU', help='Hexstring of encoded SMS TPDU')
1291
1292 @cmd2.with_argparser(envelope_sms_parser)
Harald Welte7cb94e42021-10-08 15:47:57 +02001293 def do_envelope_sms(self, arg):
Harald Welte12af7932022-02-15 16:39:08 +01001294 """Send an ENVELOPE(SMS-PP-Download) command to the card.
1295 This emulates a terminal (modem/phone) having received a SMS
1296 with a PID of 'SMS for the SIM card'. You can use this
1297 command in the context of testing OTA related features
1298 without a modem/phone or a cellular netwokr."""
Harald Welte7cb94e42021-10-08 15:47:57 +02001299 tpdu_ie = SMS_TPDU()
1300 tpdu_ie.from_bytes(h2b(arg))
Harald Weltec91085e2022-02-10 18:05:45 +01001301 dev_ids = DeviceIdentities(
1302 decoded={'source_dev_id': 'network', 'dest_dev_id': 'uicc'})
Harald Welte7cb94e42021-10-08 15:47:57 +02001303 sms_dl = SMSPPDownload(children=[dev_ids, tpdu_ie])
1304 (data, sw) = self._cmd.card._scc.envelope(b2h(sms_dl.to_tlv()))
1305 self._cmd.poutput('SW: %s, data: %s' % (sw, data))
1306
Harald Welte15fae982021-04-10 10:22:27 +02001307
Harald Welteb2edd142021-01-08 23:29:35 +01001308# TS 31.102 Section 7.3
1309sw_usim = {
1310 'Security management': {
1311 '9862': 'Authentication error, incorrect MAC',
1312 '9864': 'Authentication error, security context not supported',
1313 '9865': 'Key freshness failure',
1314 '9866': 'Authentication error, no memory space available',
1315 '9867': 'Authentication error, no memory space available in EF MUK',
1316 }
1317}
1318
Harald Weltec91085e2022-02-10 18:05:45 +01001319
Philipp Maier57f65ee2021-10-18 14:09:02 +02001320class CardApplicationUSIM(CardApplication):
1321 def __init__(self):
Harald Weltec91085e2022-02-10 18:05:45 +01001322 super().__init__('USIM', adf=ADF_USIM(), sw=sw_usim)