blob: 786bd645e39b3dc225cc7d27af14da697390fdd6 [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 Welte790b2702021-04-11 00:01:35 +020013# Copyright (C) 2021 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
36from pySim.ts_102_221 import EF_ARR
37from pySim.tlv import *
38from pySim.filesystem import *
Harald Welte6f8a8702022-07-17 21:50:31 +020039from pySim.ts_31_102_telecom import DF_PHONEBOOK, EF_UServiceTable
Harald Weltec91085e2022-02-10 18:05:45 +010040from pySim.construct import *
41from construct import Optional as COptional
42from construct import *
43from typing import Tuple
44from struct import unpack, pack
45import enum
Supreeth Herle475dcaa2020-03-20 18:57:39 +010046EF_UST_map = {
Harald Weltec91085e2022-02-10 18:05:45 +010047 1: 'Local Phone Book',
48 2: 'Fixed Dialling Numbers (FDN)',
49 3: 'Extension 2',
50 4: 'Service Dialling Numbers (SDN)',
51 5: 'Extension3',
52 6: 'Barred Dialling Numbers (BDN)',
53 7: 'Extension4',
54 8: 'Outgoing Call Information (OCI and OCT)',
55 9: 'Incoming Call Information (ICI and ICT)',
56 10: 'Short Message Storage (SMS)',
57 11: 'Short Message Status Reports (SMSR)',
58 12: 'Short Message Service Parameters (SMSP)',
59 13: 'Advice of Charge (AoC)',
60 14: 'Capability Configuration Parameters 2 (CCP2)',
61 15: 'Cell Broadcast Message Identifier',
62 16: 'Cell Broadcast Message Identifier Ranges',
63 17: 'Group Identifier Level 1',
64 18: 'Group Identifier Level 2',
65 19: 'Service Provider Name',
66 20: 'User controlled PLMN selector with Access Technology',
67 21: 'MSISDN',
68 22: 'Image (IMG)',
69 23: 'Support of Localised Service Areas (SoLSA)',
70 24: 'Enhanced Multi-Level Precedence and Pre-emption Service',
71 25: 'Automatic Answer for eMLPP',
72 26: 'RFU',
73 27: 'GSM Access',
74 28: 'Data download via SMS-PP',
75 29: 'Data download via SMS-CB',
76 30: 'Call Control by USIM',
77 31: 'MO-SMS Control by USIM',
78 32: 'RUN AT COMMAND command',
79 33: 'shall be set to 1',
80 34: 'Enabled Services Table',
81 35: 'APN Control List (ACL)',
82 36: 'Depersonalisation Control Keys',
83 37: 'Co-operative Network List',
84 38: 'GSM security context',
85 39: 'CPBCCH Information',
86 40: 'Investigation Scan',
87 41: 'MexE',
88 42: 'Operator controlled PLMN selector with Access Technology',
89 43: 'HPLMN selector with Access Technology',
90 44: 'Extension 5',
91 45: 'PLMN Network Name',
92 46: 'Operator PLMN List',
93 47: 'Mailbox Dialling Numbers',
94 48: 'Message Waiting Indication Status',
95 49: 'Call Forwarding Indication Status',
96 50: 'Reserved and shall be ignored',
97 51: 'Service Provider Display Information',
98 52: 'Multimedia Messaging Service (MMS)',
99 53: 'Extension 8',
100 54: 'Call control on GPRS by USIM',
101 55: 'MMS User Connectivity Parameters',
102 56: 'Network\'s indication of alerting in the MS (NIA)',
103 57: 'VGCS Group Identifier List (EFVGCS and EFVGCSS)',
104 58: 'VBS Group Identifier List (EFVBS and EFVBSS)',
105 59: 'Pseudonym',
106 60: 'User Controlled PLMN selector for I-WLAN access',
107 61: 'Operator Controlled PLMN selector for I-WLAN access',
108 62: 'User controlled WSID list',
109 63: 'Operator controlled WSID list',
110 64: 'VGCS security',
111 65: 'VBS security',
112 66: 'WLAN Reauthentication Identity',
113 67: 'Multimedia Messages Storage',
114 68: 'Generic Bootstrapping Architecture (GBA)',
115 69: 'MBMS security',
116 70: 'Data download via USSD and USSD application mode',
117 71: 'Equivalent HPLMN',
118 72: 'Additional TERMINAL PROFILE after UICC activation',
119 73: 'Equivalent HPLMN Presentation Indication',
120 74: 'Last RPLMN Selection Indication',
121 75: 'OMA BCAST Smart Card Profile',
122 76: 'GBA-based Local Key Establishment Mechanism',
123 77: 'Terminal Applications',
124 78: 'Service Provider Name Icon',
125 79: 'PLMN Network Name Icon',
126 80: 'Connectivity Parameters for USIM IP connections',
127 81: 'Home I-WLAN Specific Identifier List',
128 82: 'I-WLAN Equivalent HPLMN Presentation Indication',
129 83: 'I-WLAN HPLMN Priority Indication',
130 84: 'I-WLAN Last Registered PLMN',
131 85: 'EPS Mobility Management Information',
132 86: 'Allowed CSG Lists and corresponding indications',
133 87: 'Call control on EPS PDN connection by USIM',
134 88: 'HPLMN Direct Access',
135 89: 'eCall Data',
136 90: 'Operator CSG Lists and corresponding indications',
137 91: 'Support for SM-over-IP',
138 92: 'Support of CSG Display Control',
139 93: 'Communication Control for IMS by USIM',
140 94: 'Extended Terminal Applications',
141 95: 'Support of UICC access to IMS',
142 96: 'Non-Access Stratum configuration by USIM',
143 97: 'PWS configuration by USIM',
144 98: 'RFU',
145 99: 'URI support by UICC',
146 100: 'Extended EARFCN support',
147 101: 'ProSe',
148 102: 'USAT Application Pairing',
149 103: 'Media Type support',
150 104: 'IMS call disconnection cause',
151 105: 'URI support for MO SHORT MESSAGE CONTROL',
152 106: 'ePDG configuration Information support',
153 107: 'ePDG configuration Information configured',
154 108: 'ACDC support',
155 109: 'MCPTT',
156 110: 'ePDG configuration Information for Emergency Service support',
157 111: 'ePDG configuration Information for Emergency Service configured',
158 112: 'eCall Data over IMS',
159 113: 'URI support for SMS-PP DOWNLOAD as defined in 3GPP TS 31.111 [12]',
160 114: 'From Preferred',
161 115: 'IMS configuration data',
162 116: 'TV configuration',
163 117: '3GPP PS Data Off',
164 118: '3GPP PS Data Off Service List',
165 119: 'V2X',
166 120: 'XCAP Configuration Data',
167 121: 'EARFCN list for MTC/NB-IOT UEs',
168 122: '5GS Mobility Management Information',
169 123: '5G Security Parameters',
170 124: 'Subscription identifier privacy support',
171 125: 'SUCI calculation by the USIM',
172 126: 'UAC Access Identities support',
173 127: 'Expect control plane-based Steering of Roaming information during initial registration in VPLMN',
174 128: 'Call control on PDU Session by USIM',
175 129: '5GS Operator PLMN List',
176 130: 'Support for SUPI of type NSI or GLI or GCI',
177 131: '3GPP PS Data Off separate Home and Roaming lists',
178 132: 'Support for URSP by USIM',
179 133: '5G Security Parameters extended',
180 134: 'MuD and MiD configuration data',
181 135: 'Support for Trusted non-3GPP access networks by USIM'
Sebastian Viviani0dc8f692020-05-29 00:14:55 +0100182}
183
Harald Weltee8947492022-02-10 10:33:20 +0100184# Mapping between USIM Enbled Service Number and its description
185EF_EST_map = {
186 1: 'Fixed Dialling Numbers (FDN)',
187 2: 'Barred Dialling Numbers (BDN)',
188 3: 'APN Control List (ACL)'
189}
190
Sebastian Vivianie61170c2020-06-03 08:57:00 +0100191LOCI_STATUS_map = {
Harald Weltec91085e2022-02-10 18:05:45 +0100192 0: 'updated',
193 1: 'not updated',
194 2: 'plmn not allowed',
195 3: 'locatation area not allowed'
Sebastian Vivianie61170c2020-06-03 08:57:00 +0100196}
herlesupreetha562ea02020-09-16 20:17:22 +0200197
Sebastian Viviani0dc8f692020-05-29 00:14:55 +0100198EF_USIM_ADF_map = {
Harald Weltec91085e2022-02-10 18:05:45 +0100199 'LI': '6F05',
200 'ARR': '6F06',
201 'IMSI': '6F07',
202 'Keys': '6F08',
203 'KeysPS': '6F09',
204 'DCK': '6F2C',
205 'HPPLMN': '6F31',
206 'CNL': '6F32',
207 'ACMmax': '6F37',
208 'UST': '6F38',
209 'ACM': '6F39',
210 'FDN': '6F3B',
211 'SMS': '6F3C',
212 'GID1': '6F3E',
213 'GID2': '6F3F',
214 'MSISDN': '6F40',
215 'PUCT': '6F41',
216 'SMSP': '6F42',
217 'SMSS': '6F42',
218 'CBMI': '6F45',
219 'SPN': '6F46',
220 'SMSR': '6F47',
221 'CBMID': '6F48',
222 'SDN': '6F49',
223 'EXT2': '6F4B',
224 'EXT3': '6F4C',
225 'BDN': '6F4D',
226 'EXT5': '6F4E',
227 'CCP2': '6F4F',
228 'CBMIR': '6F50',
229 'EXT4': '6F55',
230 'EST': '6F56',
231 'ACL': '6F57',
232 'CMI': '6F58',
233 'START-HFN': '6F5B',
234 'THRESHOLD': '6F5C',
235 'PLMNwAcT': '6F60',
236 'OPLMNwAcT': '6F61',
237 'HPLMNwAcT': '6F62',
238 'PSLOCI': '6F73',
239 'ACC': '6F78',
240 'FPLMN': '6F7B',
241 'LOCI': '6F7E',
242 'ICI': '6F80',
243 'OCI': '6F81',
244 'ICT': '6F82',
245 'OCT': '6F83',
246 'AD': '6FAD',
247 'VGCS': '6FB1',
248 'VGCSS': '6FB2',
249 'VBS': '6FB3',
250 'VBSS': '6FB4',
251 'eMLPP': '6FB5',
252 'AAeM': '6FB6',
253 'ECC': '6FB7',
254 'Hiddenkey': '6FC3',
255 'NETPAR': '6FC4',
256 'PNN': '6FC5',
257 'OPL': '6FC6',
258 'MBDN': '6FC7',
259 'EXT6': '6FC8',
260 'MBI': '6FC9',
261 'MWIS': '6FCA',
262 'CFIS': '6FCB',
263 'EXT7': '6FCC',
264 'SPDI': '6FCD',
265 'MMSN': '6FCE',
266 'EXT8': '6FCF',
267 'MMSICP': '6FD0',
268 'MMSUP': '6FD1',
269 'MMSUCP': '6FD2',
270 'NIA': '6FD3',
271 'VGCSCA': '6FD4',
272 'VBSCA': '6FD5',
273 'GBAP': '6FD6',
274 'MSK': '6FD7',
275 'MUK': '6FD8',
276 'EHPLMN': '6FD9',
277 'GBANL': '6FDA',
278 'EHPLMNPI': '6FDB',
279 'LRPLMNSI': '6FDC',
280 'NAFKCA': '6FDD',
281 'SPNI': '6FDE',
282 'PNNI': '6FDF',
283 'NCP-IP': '6FE2',
284 'EPSLOCI': '6FE3',
285 'EPSNSC': '6FE4',
286 'UFC': '6FE6',
287 'UICCIARI': '6FE7',
288 'NASCONFIG': '6FE8',
289 'PWC': '6FEC',
290 'FDNURI': '6FED',
291 'BDNURI': '6FEE',
292 'SDNURI': '6FEF',
293 'IWL': '6FF0',
294 'IPS': '6FF1',
295 'IPD': '6FF2',
296 'ePDGId': '6FF3',
297 'ePDGSelection': '6FF4',
298 'ePDGIdEm': '6FF5',
299 'ePDGSelectionEm': '6FF6',
Sebastian Viviani0dc8f692020-05-29 00:14:55 +0100300}
Harald Welteb2edd142021-01-08 23:29:35 +0100301
302######################################################################
303# ADF.USIM
304######################################################################
305
Harald Welteb2edd142021-01-08 23:29:35 +0100306
Harald Weltef12979d2021-05-29 21:47:13 +0200307# 3GPP TS 31.102 Section 4.4.11.4 (EF_5GS3GPPNSC)
308class EF_5GS3GPPNSC(LinFixedEF):
309 class NgKSI(BER_TLV_IE, tag=0x80):
310 _construct = Int8ub
311
312 class K_AMF(BER_TLV_IE, tag=0x81):
313 _construct = HexAdapter(Bytes(32))
314
315 class UplinkNASCount(BER_TLV_IE, tag=0x82):
316 _construct = Int32ub
317
318 class DownlinkNASCount(BER_TLV_IE, tag=0x83):
319 _construct = Int32ub
320
321 class IdsOfSelectedNasAlgos(BER_TLV_IE, tag=0x84):
322 # 3GPP TS 24.501 Section 9.11.3.34
323 _construct = BitStruct('ciphering'/Nibble, 'integrity'/Nibble)
324
325 class IdsOfSelectedEpsAlgos(BER_TLV_IE, tag=0x85):
326 # 3GPP TS 24.301 Section 9.9.3.23
327 _construct = BitStruct('ciphering'/Nibble, 'integrity'/Nibble)
328
329 class FiveGSNasSecurityContext(BER_TLV_IE, tag=0xA0,
Harald Weltec91085e2022-02-10 18:05:45 +0100330 nested=[NgKSI, K_AMF, UplinkNASCount,
331 DownlinkNASCount, IdsOfSelectedNasAlgos,
332 IdsOfSelectedEpsAlgos]):
Harald Weltef12979d2021-05-29 21:47:13 +0200333 pass
334
335 def __init__(self, fid="4f03", sfid=0x03, name='EF.5GS3GPPNSC', rec_len={57, None},
Harald Welte419bb492022-02-12 21:39:35 +0100336 desc='5GS 3GPP Access NAS Security Context', **kwargs):
337 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte65516272022-02-10 17:51:05 +0100338 self._tlv = EF_5GS3GPPNSC.FiveGSNasSecurityContext
Harald Weltef12979d2021-05-29 21:47:13 +0200339
340# 3GPP TS 31.102 Section 4.4.11.6
341class EF_5GAUTHKEYS(TransparentEF):
342 class K_AUSF(BER_TLV_IE, tag=0x80):
343 _construct = HexAdapter(GreedyBytes)
344
345 class K_SEAF(BER_TLV_IE, tag=0x81):
346 _construct = HexAdapter(GreedyBytes)
347
348 class FiveGAuthKeys(TLV_IE_Collection, nested=[K_AUSF, K_SEAF]):
349 pass
350
351 def __init__(self, fid='4f05', sfid=0x05, name='EF.5GAUTHKEYS', size={68, None},
Harald Welte419bb492022-02-12 21:39:35 +0100352 desc='5G authentication keys', **kwargs):
353 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte65516272022-02-10 17:51:05 +0100354 self._tlv = EF_5GAUTHKEYS.FiveGAuthKeys
Harald Weltef12979d2021-05-29 21:47:13 +0200355
356# 3GPP TS 31.102 Section 4.4.11.8
357class ProtSchemeIdList(BER_TLV_IE, tag=0xa0):
358 # FIXME: 3GPP TS 24.501 Protection Scheme Identifier
359 # repeated sequence of (id, index) tuples
Harald Weltec91085e2022-02-10 18:05:45 +0100360 _construct = GreedyRange(
361 Struct('id'/Enum(Byte, null=0, A=1, B=2), 'index'/Int8ub))
362
Harald Weltef12979d2021-05-29 21:47:13 +0200363
364class HomeNetPubKeyId(BER_TLV_IE, tag=0x80):
365 # 3GPP TS 24.501 / 3GPP TS 23.003
366 _construct = Int8ub
367
Harald Weltec91085e2022-02-10 18:05:45 +0100368
Harald Weltef12979d2021-05-29 21:47:13 +0200369class HomeNetPubKey(BER_TLV_IE, tag=0x81):
370 # FIXME: RFC 5480
371 _construct = HexAdapter(GreedyBytes)
372
Harald Weltec91085e2022-02-10 18:05:45 +0100373
Harald Weltef12979d2021-05-29 21:47:13 +0200374class HomeNetPubKeyList(BER_TLV_IE, tag=0xa1,
Harald Weltec91085e2022-02-10 18:05:45 +0100375 nested=[HomeNetPubKeyId, HomeNetPubKey]):
Harald Weltef12979d2021-05-29 21:47:13 +0200376 pass
377
378# 3GPP TS 31.102 Section 4.4.11.6
Harald Weltec91085e2022-02-10 18:05:45 +0100379class SUCI_CalcInfo(TLV_IE_Collection, nested=[ProtSchemeIdList, HomeNetPubKeyList]):
Harald Weltef12979d2021-05-29 21:47:13 +0200380 pass
381
382
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200383# TS 31.102 4.4.11.8
384class EF_SUCI_Calc_Info(TransparentEF):
385 def __init__(self, fid="4f07", sfid=0x07, name='EF.SUCI_Calc_Info', size={2, None},
Harald Welte419bb492022-02-12 21:39:35 +0100386 desc='SUCI Calc Info', **kwargs):
387 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200388
389 def _encode_prot_scheme_id_list(self, in_list):
390 out_bytes = [0xa0]
Harald Weltec91085e2022-02-10 18:05:45 +0100391 out_bytes.append(len(in_list)*2) # two byte per entry
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200392
393 # position in list determines priority; high-priority items (low index) come first
394 for scheme in sorted(in_list, key=lambda item: item["priority"]):
395 out_bytes.append(scheme["identifier"])
396 out_bytes.append(scheme["key_index"])
397
398 return out_bytes
399
400 def _encode_hnet_pubkey_list(self, hnet_pubkey_list):
Harald Weltec91085e2022-02-10 18:05:45 +0100401 out_bytes = [0xa1] # pubkey list tag
402 out_bytes.append(0x00) # length filled later
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200403 length = 0
404
405 for key in hnet_pubkey_list:
Harald Weltec91085e2022-02-10 18:05:45 +0100406 out_bytes.append(0x80) # identifier tag
407 out_bytes.append(0x01) # TODO size, fixed to 1 byte
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200408 out_bytes.append(key["hnet_pubkey_identifier"])
Harald Weltec91085e2022-02-10 18:05:45 +0100409 out_bytes.append(0x81) # key tag
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200410 out_bytes.append(len(key["hnet_pubkey"])//2)
411 length += 5+len(key["hnet_pubkey"])//2
412
413 pubkey_bytes = h2b(key["hnet_pubkey"])
414 out_bytes += pubkey_bytes
415
416 # fill length
417 out_bytes[1] = length
418 return out_bytes
419
420 def _encode_hex(self, in_json):
Harald Weltec91085e2022-02-10 18:05:45 +0100421 out_bytes = self._encode_prot_scheme_id_list(
422 in_json['prot_scheme_id_list'])
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200423 out_bytes += self._encode_hnet_pubkey_list(in_json['hnet_pubkey_list'])
424 return "".join(["%02X" % i for i in out_bytes])
425
426 def _decode_prot_scheme_id_list(self, in_bytes):
427 prot_scheme_id_list = []
428 pos = 0
429 # two bytes per entry
430 while pos < len(in_bytes):
431 prot_scheme = {
Harald Weltec91085e2022-02-10 18:05:45 +0100432 'priority': pos//2, # first in list: high priority
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200433 'identifier': in_bytes[pos],
434 'key_index': in_bytes[pos+1]
435 }
436 pos += 2
437 prot_scheme_id_list.append(prot_scheme)
438 return prot_scheme_id_list
439
440 def _decode_hnet_pubkey_list(self, in_bytes):
441 hnet_pubkey_list = []
442 pos = 0
443 if in_bytes[pos] != 0xa1:
444 print("missing Home Network Public Key List data object")
445 return {}
446 pos += 1
447 hnet_pubkey_list_len = in_bytes[pos]
448 pos += 1
449
450 while pos < hnet_pubkey_list_len:
451 if in_bytes[pos] != 0x80:
452 print("missing Home Network Public Key Identifier tag")
453 return {}
454 pos += 1
Harald Weltec91085e2022-02-10 18:05:45 +0100455 # TODO might be more than 1 byte?
456 hnet_pubkey_id_len = in_bytes[pos]
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200457 pos += 1
458 hnet_pubkey_id = in_bytes[pos:pos+hnet_pubkey_id_len][0]
459 pos += hnet_pubkey_id_len
460 if in_bytes[pos] != 0x81:
461 print("missing Home Network Public Key tag")
462 return {}
463 pos += 1
464 hnet_pubkey_len = in_bytes[pos]
465 pos += 1
466 hnet_pubkey = in_bytes[pos:pos+hnet_pubkey_len]
467 pos += hnet_pubkey_len
468
469 hnet_pubkey_list.append({
470 'hnet_pubkey_identifier': hnet_pubkey_id,
471 'hnet_pubkey': b2h(hnet_pubkey)
472 })
473
474 return hnet_pubkey_list
475
476 def _decode_bin(self, in_bin):
Vadim Yanitskiy5e41eeb2021-05-02 02:20:33 +0200477 return self._decode_hex(b2h(in_bin))
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200478
479 def _decode_hex(self, in_hex):
480 in_bytes = h2b(in_hex)
481 pos = 0
482
483 if in_bytes[pos] != 0xa0:
484 print("missing Protection Scheme Identifier List data object tag")
485 return {}
486 pos += 1
487
Harald Weltec91085e2022-02-10 18:05:45 +0100488 prot_scheme_id_list_len = in_bytes[pos] # TODO maybe more than 1 byte
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200489 pos += 1
490 # decode Protection Scheme Identifier List data object
Harald Weltec91085e2022-02-10 18:05:45 +0100491 prot_scheme_id_list = self._decode_prot_scheme_id_list(
492 in_bytes[pos:pos+prot_scheme_id_list_len])
Merlin Chlosta05ca36b2021-04-01 16:15:28 +0200493 pos += prot_scheme_id_list_len
494
495 # remaining data holds Home Network Public Key Data Object
496 hnet_pubkey_list = self._decode_hnet_pubkey_list(in_bytes[pos:])
497
498 return {
499 'prot_scheme_id_list': prot_scheme_id_list,
500 'hnet_pubkey_list': hnet_pubkey_list
501 }
502
503 def _encode_bin(self, in_json):
504 return h2b(self._encode_hex(in_json))
505
Harald Weltec91085e2022-02-10 18:05:45 +0100506
Harald Welteb2edd142021-01-08 23:29:35 +0100507class EF_LI(TransRecEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100508 def __init__(self, fid='6f05', sfid=None, name='EF.LI', size={2, None}, rec_len=2,
Harald Welteb2edd142021-01-08 23:29:35 +0100509 desc='Language Indication'):
510 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
Harald Weltec91085e2022-02-10 18:05:45 +0100511
Harald Welteb2edd142021-01-08 23:29:35 +0100512 def _decode_record_bin(self, in_bin):
513 if in_bin == b'\xff\xff':
514 return None
515 else:
516 # officially this is 7-bit GSM alphabet with one padding bit in each byte
517 return in_bin.decode('ascii')
Harald Weltec91085e2022-02-10 18:05:45 +0100518
Harald Welteb2edd142021-01-08 23:29:35 +0100519 def _encode_record_bin(self, in_json):
520 if in_json == None:
521 return b'\xff\xff'
522 else:
523 # officially this is 7-bit GSM alphabet with one padding bit in each byte
524 return in_json.encode('ascii')
525
Harald Weltec91085e2022-02-10 18:05:45 +0100526
Harald Welteb2edd142021-01-08 23:29:35 +0100527class EF_Keys(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100528 def __init__(self, fid='6f08', sfid=0x08, name='EF.Keys', size={33, 33},
Harald Welteb2edd142021-01-08 23:29:35 +0100529 desc='Ciphering and Integrity Keys'):
530 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Harald Weltec91085e2022-02-10 18:05:45 +0100531 self._construct = Struct(
532 'ksi'/Int8ub, 'ck'/HexAdapter(Bytes(16)), 'ik'/HexAdapter(Bytes(16)))
Harald Welteb2edd142021-01-08 23:29:35 +0100533
Harald Welte14105dc2021-05-31 08:48:51 +0200534# TS 31.102 Section 4.2.6
535class EF_HPPLMN(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100536 def __init__(self, fid='6f31', sfid=0x12, name='EF.HPPLMN', size={1, 1},
Harald Welte14105dc2021-05-31 08:48:51 +0200537 desc='Higher Priority PLMN search period'):
538 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
539 self._construct = Int8ub
540
Harald Welte6ca2fa72022-02-12 16:29:31 +0100541class EF_UST(EF_UServiceTable):
542 def __init__(self, **kwargs):
543 super().__init__(fid='6f38', sfid=0x04, name='EF.UST', desc='USIM Service Table', size={1,17}, table=EF_UST_map, **kwargs)
544 # add those commands to the general commands of a TransparentEF
545 self.shell_commands += [self.AddlShellCommands()]
546
Harald Welteb2edd142021-01-08 23:29:35 +0100547 @with_default_category('File-Specific Commands')
548 class AddlShellCommands(CommandSet):
549 def __init__(self):
550 super().__init__()
551
552 def do_ust_service_activate(self, arg):
553 """Activate a service within EF.UST"""
554 self._cmd.card.update_ust(int(arg), 1)
555
556 def do_ust_service_deactivate(self, arg):
557 """Deactivate a service within EF.UST"""
558 self._cmd.card.update_ust(int(arg), 0)
559
Harald Welte4c5e2312022-02-12 14:37:48 +0100560 def do_ust_service_check(self, arg):
Harald Welte3bb516b2022-02-12 21:53:18 +0100561 """Check consistency between services of this file and files present/activated.
562
563 Many services determine if one or multiple files shall be present/activated or if they shall be
564 absent/deactivated. This performs a consistency check to ensure that no services are activated
565 for files that are not - and vice-versa, no files are activated for services that are not. Error
566 messages are printed for every inconsistency found."""
Harald Weltea6c0f882022-07-17 14:23:17 +0200567 selected_file = self._cmd.lchan.selected_file
Harald Welte82f75c22022-02-12 18:22:28 +0100568 num_problems = selected_file.ust_service_check(self._cmd)
569 # obtain list of currently active services
570 active_services = selected_file.get_active_services(self._cmd)
571 # Service n°46 can only be declared "available" if service n°45 is declared "available"
572 if 46 in active_services and not 45 in active_services:
Harald Weltefa8b8d12022-02-12 18:30:28 +0100573 self._cmd.perror("ERROR: Service 46 available, but it requires Service 45")
Harald Welte82f75c22022-02-12 18:22:28 +0100574 num_problems += 1
575 # Service n°125 shall only be taken into account if Service n°124 is declared "available"
576 if 125 in active_services and not 124 in active_services:
Harald Weltefa8b8d12022-02-12 18:30:28 +0100577 self._cmd.perror("ERROR: Service 125 is ignored as Service 124 not available")
Harald Welte82f75c22022-02-12 18:22:28 +0100578 num_problems += 1
579 # Service n°95, n°99 and n°115 shall not be declared "available" if an ISIM application is present on the UICC
580 non_isim_services = [95, 99, 115]
581 app_names = selected_file.get_mf().get_app_names()
582 if 'ADF.ISIM' in app_names:
583 for s in non_isim_services:
584 if s in active_services:
Harald Weltefa8b8d12022-02-12 18:30:28 +0100585 self._cmd.perror("ERROR: Service %u shall not be available as ISIM application is present" % s)
Harald Welte82f75c22022-02-12 18:22:28 +0100586 num_problems += 1
587 self._cmd.poutput("===> %u service / file inconsistencies detected" % num_problems)
Harald Welte4c5e2312022-02-12 14:37:48 +0100588
589
Harald Welte89e59542021-04-02 21:33:13 +0200590# TS 31.103 Section 4.2.7 - *not* the same as DF.GSM/EF.ECC!
591class EF_ECC(LinFixedEF):
Harald Welteff2d86d2022-01-21 15:19:47 +0100592 cc_construct = Rpad(BcdAdapter(Rpad(Bytes(3))), pattern='f')
593 category_construct = FlagsEnum(Byte, police=1, ambulance=2, fire_brigade=3, marine_guard=4,
594 mountain_rescue=5, manual_ecall=6, automatic_ecall=7)
595 alpha_construct = GsmStringAdapter(Rpad(GreedyBytes))
Harald Weltec91085e2022-02-10 18:05:45 +0100596
Harald Welte89e59542021-04-02 21:33:13 +0200597 def __init__(self, fid='6fb7', sfid=0x01, name='EF.ECC',
598 desc='Emergency Call Codes'):
Harald Weltec91085e2022-02-10 18:05:45 +0100599 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len={4, 20})
600
Harald Welteff2d86d2022-01-21 15:19:47 +0100601 def _decode_record_bin(self, in_bin):
602 # mandatory parts
603 code = in_bin[:3]
604 if code == b'\xff\xff\xff':
605 return None
606 svc_category = in_bin[-1:]
607 ret = {'call_code': parse_construct(EF_ECC.cc_construct, code),
Harald Weltec91085e2022-02-10 18:05:45 +0100608 'service_category': parse_construct(EF_ECC.category_construct, svc_category)}
Harald Welteff2d86d2022-01-21 15:19:47 +0100609 # optional alpha identifier
610 if len(in_bin) > 4:
611 alpha_id = in_bin[3:-1]
612 ret['alpha_id'] = parse_construct(EF_ECC.alpha_construct, alpha_id)
613 return ret
Harald Weltec91085e2022-02-10 18:05:45 +0100614
Harald Welteff2d86d2022-01-21 15:19:47 +0100615 def _encode_record_bin(self, in_json):
616 if in_json is None:
617 return b'\xff\xff\xff\xff'
618 code = EF_ECC.cc_construct.build(in_json['call_code'])
619 svc_category = EF_ECC.category_construct.build(in_json['category'])
620 alpha_id = EF_ECC.alpha_construct.build(in_json['alpha_id'])
621 # FIXME: alpha_id needs padding up to 'record_length - 4'
622 return code + alpha_id + svc_category
623
Harald Welte89e59542021-04-02 21:33:13 +0200624
Harald Welte790b2702021-04-11 00:01:35 +0200625# TS 31.102 Section 4.2.17
626class EF_LOCI(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100627 def __init__(self, fid='6f7e', sfid=0x0b, name='EF.LOCI', desc='Location information', size={11, 11}):
Harald Welte790b2702021-04-11 00:01:35 +0200628 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Harald Welte3a5afff2022-02-25 15:33:46 +0100629 Lai = Struct('mcc_mnc'/BcdAdapter(Bytes(3)), 'lac'/HexAdapter(Bytes(2)))
630 self._construct = Struct('tmsi'/HexAdapter(Bytes(4)), 'lai'/Lai, 'rfu'/Int8ub, 'lu_status'/Int8ub)
Harald Welte592b32e2021-06-05 11:26:36 +0200631# TS 31.102 Section 4.2.18
632class EF_AD(TransparentEF):
633 class OP_MODE(enum.IntEnum):
Harald Weltec91085e2022-02-10 18:05:45 +0100634 normal = 0x00
635 type_approval = 0x80
636 normal_and_specific_facilities = 0x01
637 type_approval_and_specific_facilities = 0x81
638 maintenance_off_line = 0x02
639 cell_test = 0x04
Harald Welte592b32e2021-06-05 11:26:36 +0200640
Harald Weltec91085e2022-02-10 18:05:45 +0100641 def __init__(self, fid='6fad', sfid=0x03, name='EF.AD', desc='Administrative Data', size={4, 6}):
Harald Welte592b32e2021-06-05 11:26:36 +0200642 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
643 self._construct = BitStruct(
644 # Byte 1
645 'ms_operation_mode'/Bytewise(Enum(Byte, EF_AD.OP_MODE)),
646 # Byte 2 + 3
647 'additional_info'/Bytewise(FlagsEnum(Int16ub, ciphering_indicator=1, csg_display_control=2,
648 prose_services=4, extended_drx=8)),
649 'rfu'/BitsRFU(4),
650 'mnc_len'/BitsInteger(4),
651 'extensions'/COptional(Bytewise(GreedyBytesRFU))
652 )
Harald Welte790b2702021-04-11 00:01:35 +0200653
654# TS 31.102 Section 4.2.23
655class EF_PSLOCI(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100656 def __init__(self, fid='6f73', sfid=0x0c, name='EF.PSLOCI', desc='PS Location information', size={14, 14}):
Harald Welte790b2702021-04-11 00:01:35 +0200657 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
658 self._construct = Struct('ptmsi'/HexAdapter(Bytes(4)), 'ptmsi_sig'/HexAdapter(Bytes(3)),
659 'rai'/HexAdapter(Bytes(6)), 'rau_status'/Int8ub)
660
661# TS 31.102 Section 4.2.33
662class EF_ICI(CyclicEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100663 def __init__(self, fid='6f80', sfid=0x14, name='EF.ICI', rec_len={28, 48},
Harald Welte6169c722022-02-12 09:05:15 +0100664 desc='Incoming Call Information', **kwargs):
665 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200666 self._construct = Struct('alpha_id'/Bytes(this._.total_len-28),
667 'len_of_bcd_contents'/Int8ub,
668 'ton_npi'/Int8ub,
669 'call_number'/BcdAdapter(Bytes(10)),
670 'cap_cfg2_record_id'/Int8ub,
671 'ext5_record_id'/Int8ub,
672 'date_and_time'/BcdAdapter(Bytes(7)),
673 'duration'/Int24ub,
674 'status'/Byte,
675 'link_to_phonebook'/Bytes(3))
676
677# TS 31.102 Section 4.2.34
678class EF_OCI(CyclicEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100679 def __init__(self, fid='6f81', sfid=0x15, name='EF.OCI', rec_len={27, 47},
Harald Welte6169c722022-02-12 09:05:15 +0100680 desc='Outgoing Call Information', **kwargs):
681 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200682 self._construct = Struct('alpha_id'/Bytes(this._.total_len-27),
683 'len_of_bcd_contents'/Int8ub,
684 'ton_npi'/Int8ub,
685 'call_number'/BcdAdapter(Bytes(10)),
686 'cap_cfg2_record_id'/Int8ub,
687 'ext5_record_id'/Int8ub,
688 'date_and_time'/BcdAdapter(Bytes(7)),
689 'duration'/Int24ub,
690 'link_to_phonebook'/Bytes(3))
691
692# TS 31.102 Section 4.2.35
693class EF_ICT(CyclicEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100694 def __init__(self, fid='6f82', sfid=None, name='EF.ICT', rec_len={3, 3},
Harald Welte6169c722022-02-12 09:05:15 +0100695 desc='Incoming Call Timer', **kwargs):
696 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200697 self._construct = Struct('accumulated_call_timer'/Int24ub)
698
699# TS 31.102 Section 4.2.38
700class EF_CCP2(LinFixedEF):
Harald Welte6169c722022-02-12 09:05:15 +0100701 def __init__(self, fid='6f4f', sfid=0x16, name='EF.CCP2', desc='Capability Configuration Parameters 2', **kwargs):
702 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len={15, None}, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200703
Harald Welte6ca2fa72022-02-12 16:29:31 +0100704# TS 31.102 Section 4.2.47
705class EF_EST(EF_UServiceTable):
706 def __init__(self, **kwargs):
707 super().__init__(fid='6f56', sfid=0x05, name='EF.EST', desc='Enabled Services Table', size={1,None}, table=EF_EST_map, **kwargs)
708 # add those commands to the general commands of a TransparentEF
709 self.shell_commands += [self.AddlShellCommands()]
710
711 @with_default_category('File-Specific Commands')
712 class AddlShellCommands(CommandSet):
713 def __init__(self):
714 super().__init__()
715
716 def do_est_service_activate(self, arg):
717 """Activate a service within EF.UST"""
718 self._cmd.card.update_est(int(arg), 1)
719
720 def do_est_service_deactivate(self, arg):
721 """Deactivate a service within EF.UST"""
722 self._cmd.card.update_est(int(arg), 0)
723
Harald Welte790b2702021-04-11 00:01:35 +0200724# TS 31.102 Section 4.2.48
725class EF_ACL(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100726 def __init__(self, fid='6f57', sfid=None, name='EF.ACL', size={32, None},
Harald Welte6169c722022-02-12 09:05:15 +0100727 desc='Access Point Name Control List', **kwargs):
728 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200729 self._construct = Struct('num_of_apns'/Int8ub, 'tlvs'/GreedyBytes)
730
731# TS 31.102 Section 4.2.51
732class EF_START_HFN(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100733 def __init__(self, fid='6f5b', sfid=0x0f, name='EF.START-HFN', size={6, 6},
Harald Welte6169c722022-02-12 09:05:15 +0100734 desc='Initialisation values for Hyperframe number', **kwargs):
735 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200736 self._construct = Struct('start_cs'/Int24ub, 'start_ps'/Int24ub)
737
738# TS 31.102 Section 4.2.52
739class EF_THRESHOLD(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100740 def __init__(self, fid='6f5c', sfid=0x10, name='EF.THRESHOLD', size={3, 3},
Harald Welte6169c722022-02-12 09:05:15 +0100741 desc='Maximum value of START', **kwargs):
742 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200743 self._construct = Struct('max_start'/Int24ub)
744
745# TS 31.102 Section 4.2.77
746class EF_VGCSCA(TransRecEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100747 def __init__(self, fid='6fd4', sfid=None, name='EF.VGCSCA', size={2, 100}, rec_len=2,
Harald Welte6169c722022-02-12 09:05:15 +0100748 desc='Voice Group Call Service Ciphering Algorithm', **kwargs):
749 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200750 self._construct = Struct('alg_v_ki_1'/Int8ub, 'alg_v_ki_2'/Int8ub)
751
752# TS 31.102 Section 4.2.79
753class EF_GBABP(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100754 def __init__(self, fid='6fd6', sfid=None, name='EF.GBABP', size={3, 50},
Harald Welte6169c722022-02-12 09:05:15 +0100755 desc='GBA Bootstrapping parameters', **kwargs):
756 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200757 self._construct = Struct('rand'/LV, 'b_tid'/LV, 'key_lifetime'/LV)
758
759# TS 31.102 Section 4.2.80
760class EF_MSK(LinFixedEF):
Harald Welte6169c722022-02-12 09:05:15 +0100761 def __init__(self, fid='6fd7', sfid=None, name='EF.MSK', desc='MBMS Service Key List', **kwargs):
762 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len={20, None}, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200763 msk_ts_constr = Struct('msk_id'/Int32ub, 'timestamp_counter'/Int32ub)
764 self._construct = Struct('key_domain_id'/Bytes(3),
765 'num_msk_id'/Int8ub,
766 'msk_ids'/msk_ts_constr[this.num_msk_id])
Harald Welte14105dc2021-05-31 08:48:51 +0200767# TS 31.102 Section 4.2.81
768class EF_MUK(LinFixedEF):
769 class MUK_Idr(BER_TLV_IE, tag=0x80):
770 _construct = HexAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100771
Harald Welte14105dc2021-05-31 08:48:51 +0200772 class MUK_Idi(BER_TLV_IE, tag=0x82):
773 _construct = HexAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100774
Harald Welte14105dc2021-05-31 08:48:51 +0200775 class MUK_ID(BER_TLV_IE, tag=0xA0, nested=[MUK_Idr, MUK_Idi]):
776 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100777
Harald Welte14105dc2021-05-31 08:48:51 +0200778 class TimeStampCounter(BER_TLV_IE, tag=0x81):
779 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100780
Harald Welte14105dc2021-05-31 08:48:51 +0200781 class EF_MUK_Collection(TLV_IE_Collection, nested=[MUK_ID, TimeStampCounter]):
782 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100783
Harald Welte6169c722022-02-12 09:05:15 +0100784 def __init__(self, fid='6fd8', sfid=None, name='EF.MUK', desc='MBMS User Key', **kwargs):
785 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len={None, None}, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200786 self._tlv = EF_MUK.EF_MUK_Collection
787
788# TS 31.102 Section 4.2.83
789class EF_GBANL(LinFixedEF):
790 class NAF_ID(BER_TLV_IE, tag=0x80):
791 _construct = HexAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100792
Harald Welte14105dc2021-05-31 08:48:51 +0200793 class B_TID(BER_TLV_IE, tag=0x81):
794 _construct = HexAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100795
Harald Welte14105dc2021-05-31 08:48:51 +0200796 class EF_GBANL_Collection(BER_TLV_IE, nested=[NAF_ID, B_TID]):
797 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100798
Harald Welte6169c722022-02-12 09:05:15 +0100799 def __init__(self, fid='6fda', sfid=None, name='EF.GBANL', desc='GBA NAF List', **kwargs):
800 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len={None, None}, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200801 self._tlv = EF_GBANL.EF_GBANL_Collection
Harald Welte790b2702021-04-11 00:01:35 +0200802
803# TS 31.102 Section 4.2.85
804class EF_EHPLMNPI(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100805 def __init__(self, fid='6fdb', sfid=None, name='EF.EHPLMNPI', size={1, 1},
Harald Welte6169c722022-02-12 09:05:15 +0100806 desc='Equivalent HPLMN Presentation Indication', **kwargs):
807 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100808 self._construct = Struct('presentation_ind' /
Harald Welte790b2702021-04-11 00:01:35 +0200809 Enum(Byte, no_preference=0, display_highest_prio_only=1, display_all=2))
Harald Welte14105dc2021-05-31 08:48:51 +0200810
811# TS 31.102 Section 4.2.87
812class EF_NAFKCA(LinFixedEF):
813 class NAF_KeyCentreAddress(BER_TLV_IE, tag=0x80):
814 _construct = HexAdapter(GreedyBytes)
815 def __init__(self, fid='6fdd', sfid=None, name='EF.NAFKCA', rec_len={None, None},
Harald Welte6169c722022-02-12 09:05:15 +0100816 desc='NAF Key Centre Address', **kwargs):
817 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200818 self._tlv = EF_NAFKCA.NAF_KeyCentreAddress
819
820# TS 31.102 Section 4.2.90
821class EF_NCP_IP(LinFixedEF):
822 class DataDestAddrRange(TLV_IE, tag=0x83):
823 _construct = Struct('type_of_address'/Enum(Byte, IPv4=0x21, IPv6=0x56),
824 'prefix_length'/Int8ub,
825 'prefix'/HexAdapter(GreedyBytes))
Harald Weltec91085e2022-02-10 18:05:45 +0100826
Harald Welte14105dc2021-05-31 08:48:51 +0200827 class AccessPointName(TLV_IE, tag=0x80):
828 # coded as per TS 23.003
829 _construct = HexAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100830
Harald Welte14105dc2021-05-31 08:48:51 +0200831 class Login(TLV_IE, tag=0x81):
832 # as per SMS DCS TS 23.038
833 _construct = GsmStringAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100834
Harald Welte14105dc2021-05-31 08:48:51 +0200835 class Password(TLV_IE, tag=0x82):
836 # as per SMS DCS TS 23.038
837 _construct = GsmStringAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100838
Harald Welte14105dc2021-05-31 08:48:51 +0200839 class BearerDescription(TLV_IE, tag=0x84):
840 # Bearer descriptionTLV DO as per TS 31.111
841 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100842
Harald Welte14105dc2021-05-31 08:48:51 +0200843 class EF_NCP_IP_Collection(TLV_IE_Collection,
844 nested=[AccessPointName, Login, Password, BearerDescription]):
845 pass
846 def __init__(self, fid='6fe2', sfid=None, name='EF.NCP-IP', rec_len={None, None},
Harald Welte6169c722022-02-12 09:05:15 +0100847 desc='Network Connectivity Parameters for USIM IP connections', **kwargs):
848 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200849 self._tlv = EF_NCP_IP.EF_NCP_IP_Collection
850
Harald Welte790b2702021-04-11 00:01:35 +0200851# TS 31.102 Section 4.2.91
852class EF_EPSLOCI(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100853 def __init__(self, fid='6fe3', sfid=0x1e, name='EF.EPSLOCI', size={18, 18},
Harald Welte6169c722022-02-12 09:05:15 +0100854 desc='EPS Location Information', **kwargs):
855 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100856 upd_status_constr = Enum(
857 Byte, updated=0, not_updated=1, roaming_not_allowed=2)
Harald Welte790b2702021-04-11 00:01:35 +0200858 self._construct = Struct('guti'/Bytes(12), 'last_visited_registered_tai'/Bytes(5),
859 'eps_update_status'/upd_status_constr)
860
Harald Welte14105dc2021-05-31 08:48:51 +0200861# TS 31.102 Section 4.2.92
862class EF_EPSNSC(LinFixedEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100863 class KSI_ASME(BER_TLV_IE, tag=0x80):
Harald Welte14105dc2021-05-31 08:48:51 +0200864 _construct = Int8ub
Harald Weltec91085e2022-02-10 18:05:45 +0100865
866 class K_ASME(BER_TLV_IE, tag=0x81):
Harald Welte14105dc2021-05-31 08:48:51 +0200867 _construct = HexAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100868
Harald Welte14105dc2021-05-31 08:48:51 +0200869 class UplinkNASCount(BER_TLV_IE, tag=0x82):
870 _construct = Int32ub
Harald Weltec91085e2022-02-10 18:05:45 +0100871
Harald Welte14105dc2021-05-31 08:48:51 +0200872 class DownlinkNASCount(BER_TLV_IE, tag=0x83):
873 _construct = Int32ub
Harald Weltec91085e2022-02-10 18:05:45 +0100874
Harald Welte14105dc2021-05-31 08:48:51 +0200875 class IDofNASAlgorithms(BER_TLV_IE, tag=0x84):
876 _construct = HexAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100877
Harald Welte14105dc2021-05-31 08:48:51 +0200878 class EPS_NAS_Security_Context(BER_TLV_IE, tag=0xa0,
Harald Weltec91085e2022-02-10 18:05:45 +0100879 nested=[KSI_ASME, K_ASME, UplinkNASCount, DownlinkNASCount,
880 IDofNASAlgorithms]):
Harald Welte14105dc2021-05-31 08:48:51 +0200881 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100882 def __init__(self, fid='6fe4', sfid=0x18, name='EF.EPSNSC', rec_len={54, 128},
Harald Welte6169c722022-02-12 09:05:15 +0100883 desc='EPS NAS Security Context', **kwargs):
884 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200885 self._tlv = EF_EPSNSC.EPS_NAS_Security_Context
886
Harald Welte790b2702021-04-11 00:01:35 +0200887# TS 31.102 Section 4.2.96
888class EF_PWS(TransparentEF):
Harald Welte6169c722022-02-12 09:05:15 +0100889 def __init__(self, fid='6fec', sfid=None, name='EF.PWS', desc='Public Warning System', size={1, 1}, **kwargs):
890 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100891 pws_config = FlagsEnum(
892 Byte, ignore_pws_in_hplmn_and_equivalent=1, ignore_pws_in_vplmn=2)
Harald Welte790b2702021-04-11 00:01:35 +0200893 self._construct = Struct('pws_configuration'/pws_config)
894
895# TS 31.102 Section 4.2.101
896class EF_IPS(CyclicEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100897 def __init__(self, fid='6ff1', sfid=None, name='EF.IPS', rec_len={4, 4},
Harald Welte6169c722022-02-12 09:05:15 +0100898 desc='IMEI(SV) Pairing Status', **kwargs):
899 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200900 self._construct = Struct('status'/PaddedString(2, 'ascii'),
901 'link_to_ef_ipd'/Int8ub, 'rfu'/Byte)
902
Harald Welte14105dc2021-05-31 08:48:51 +0200903# TS 31.102 Section 4.2.103
904class EF_ePDGId(TransparentEF):
905 class ePDGId(BER_TLV_IE, tag=0x80, nested=[]):
906 _construct = Struct('type_of_ePDG_address'/Enum(Byte, FQDN=0, IPv4=1, IPv6=2),
907 'ePDG_address'/Switch(this.type_of_address,
Harald Weltec91085e2022-02-10 18:05:45 +0100908 {'FQDN': GreedyString("utf8"),
909 'IPv4': HexAdapter(GreedyBytes),
910 'IPv6': HexAdapter(GreedyBytes)}))
911
Harald Welte6169c722022-02-12 09:05:15 +0100912 def __init__(self, fid='6ff3', sfid=None, name='EF.eDPDGId', desc='Home ePDG Identifier', **kwargs):
913 super().__init__(fid, sfid=sfid, name=name, desc=desc, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200914 self._tlv = EF_ePDGId.ePDGId
915
Harald Welte71290072021-04-21 10:58:24 +0200916# TS 31.102 Section 4.2.106
917class EF_FromPreferred(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100918 def __init__(self, fid='6ff7', sfid=None, name='EF.FromPreferred', size={1, 1},
Harald Welte6169c722022-02-12 09:05:15 +0100919 desc='From Preferred', **kwargs):
920 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte71290072021-04-21 10:58:24 +0200921 self._construct = BitStruct('rfu'/BitsRFU(7), 'from_preferred'/Bit)
922
Harald Welte790b2702021-04-11 00:01:35 +0200923######################################################################
924# DF.5GS
925######################################################################
926
927# TS 31.102 Section 4.4.11.2
928class EF_5GS3GPPLOCI(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100929 def __init__(self, fid='4f01', sfid=0x01, name='EF.5GS3GPPLOCI', size={20, 20},
Harald Welte419bb492022-02-12 21:39:35 +0100930 desc='5S 3GP location information', **kwargs):
931 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100932 upd_status_constr = Enum(
933 Byte, updated=0, not_updated=1, roaming_not_allowed=2)
Harald Welte790b2702021-04-11 00:01:35 +0200934 self._construct = Struct('5g_guti'/Bytes(13), 'last_visited_registered_tai_in_5gs'/Bytes(6),
935 '5gs_update_status'/upd_status_constr)
936
937# TS 31.102 Section 4.4.11.7
938class EF_UAC_AIC(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100939 def __init__(self, fid='4f06', sfid=0x06, name='EF.UAC_AIC', size={4, 4},
Harald Welte419bb492022-02-12 21:39:35 +0100940 desc='UAC Access Identities Configuration', **kwargs):
941 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200942 cfg_constr = FlagsEnum(Byte, multimedia_priority_service=1,
Harald Weltec91085e2022-02-10 18:05:45 +0100943 mission_critical_service=2)
Harald Welte790b2702021-04-11 00:01:35 +0200944 self._construct = Struct('uac_access_id_config'/cfg_constr)
945
Harald Welte14105dc2021-05-31 08:48:51 +0200946# TS 31.102 Section 4.4.11.9
Harald Welte790b2702021-04-11 00:01:35 +0200947class EF_OPL5G(LinFixedEF):
Harald Welte419bb492022-02-12 21:39:35 +0100948 def __init__(self, fid='6f08', sfid=0x08, name='EF.OPL5G', desc='5GS Operator PLMN List', **kwargs):
949 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len={10, None}, **kwargs)
Harald Weltea0377622022-02-25 15:36:44 +0100950 Tai = Struct('mcc_mnc'/BcdAdapter(Bytes(3)), 'tac_min'/Bytes(3), 'tac_max'/Bytes(3))
951 self._construct = Struct('tai'/Tai, 'pnn_record_id'/Int8ub)
Harald Welte790b2702021-04-11 00:01:35 +0200952
Harald Welte14105dc2021-05-31 08:48:51 +0200953# TS 31.102 Section 4.4.11.10
954class EF_SUPI_NAI(TransparentEF):
955 class NetworkSpecificIdentifier(TLV_IE, tag=0x80):
956 # RFC 7542 encoded as UTF-8 string
957 _construct = GreedyString("utf8")
Harald Weltec91085e2022-02-10 18:05:45 +0100958
Harald Welte14105dc2021-05-31 08:48:51 +0200959 class GlobalLineIdentifier(TLV_IE, tag=0x81):
960 # TS 23.003 clause 28.16.2
961 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100962
Harald Welte14105dc2021-05-31 08:48:51 +0200963 class GlobalCableIdentifier(TLV_IE, tag=0x82):
964 # TS 23.003 clause 28.15.2
965 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100966
Harald Welte14105dc2021-05-31 08:48:51 +0200967 class NAI_TLV_Collection(TLV_IE_Collection,
Harald Weltec91085e2022-02-10 18:05:45 +0100968 nested=[NetworkSpecificIdentifier, GlobalLineIdentifier, GlobalCableIdentifier]):
Harald Welte14105dc2021-05-31 08:48:51 +0200969 pass
970 def __init__(self, fid='4f09', sfid=0x09, name='EF.SUPI_NAI',
Harald Welte419bb492022-02-12 21:39:35 +0100971 desc='SUPI as Network Access Identifier', **kwargs):
972 super().__init__(fid, sfid=sfid, name=name, desc=desc, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200973 self._tlv = EF_SUPI_NAI.NAI_TLV_Collection
974
Harald Weltec91085e2022-02-10 18:05:45 +0100975
Harald Welte14105dc2021-05-31 08:48:51 +0200976class EF_TN3GPPSNN(TransparentEF):
977 class ServingNetworkName(BER_TLV_IE, tag=0x80):
978 _construct = GreedyString("utf8")
979 def __init__(self, fid='4f0c', sfid=0x0c, name='EF.TN3GPPSNN',
Harald Welte419bb492022-02-12 21:39:35 +0100980 desc='Trusted non-3GPP Serving network names list', **kwargs):
981 super().__init__(fid, sfid=sfid, name=name, desc=desc, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200982 self._tlv = EF_TN3GPPSNN.ServingNetworkName
983
Harald Welte3990ebb2021-04-20 23:55:14 +0200984# TS 31.102 Section 4.4.5
985class DF_WLAN(CardDF):
Harald Welte6169c722022-02-12 09:05:15 +0100986 def __init__(self, fid='5f40', name='DF.WLAN', desc='Files for WLAN purpose', **kwargs):
987 super().__init__(fid=fid, name=name, desc=desc, **kwargs)
Harald Welte3990ebb2021-04-20 23:55:14 +0200988 files = [
Harald Welte419bb492022-02-12 21:39:35 +0100989 TransparentEF('4f41', 0x01, 'EF.Pseudo', 'Pseudonym', service=59),
Harald Weltec91085e2022-02-10 18:05:45 +0100990 TransparentEF('4f42', 0x02, 'EF.UPLMNWLAN',
Harald Welte419bb492022-02-12 21:39:35 +0100991 'User controlled PLMN selector for I-WLAN Access', service=60),
Harald Weltec91085e2022-02-10 18:05:45 +0100992 TransparentEF('4f43', 0x03, 'EF.OPLMNWLAN',
Harald Welte419bb492022-02-12 21:39:35 +0100993 'Operator controlled PLMN selector for I-WLAN Access', service=61),
Harald Weltec91085e2022-02-10 18:05:45 +0100994 LinFixedEF('4f44', 0x04, 'EF.UWSIDL',
Harald Welte419bb492022-02-12 21:39:35 +0100995 'User controlled WLAN Specific Identifier List', service=62),
Harald Weltec91085e2022-02-10 18:05:45 +0100996 LinFixedEF('4f45', 0x05, 'EF.OWSIDL',
Harald Welte419bb492022-02-12 21:39:35 +0100997 'Operator controlled WLAN Specific Identifier List', service=63),
Harald Weltec91085e2022-02-10 18:05:45 +0100998 TransparentEF('4f46', 0x06, 'EF.WRI',
Harald Welte419bb492022-02-12 21:39:35 +0100999 'WLAN Reauthentication Identity', service=66),
Harald Weltec91085e2022-02-10 18:05:45 +01001000 LinFixedEF('4f47', 0x07, 'EF.HWSIDL',
Harald Welte419bb492022-02-12 21:39:35 +01001001 'Home I-WLAN Specific Identifier List', service=81),
Harald Weltec91085e2022-02-10 18:05:45 +01001002 TransparentEF('4f48', 0x08, 'EF.WEHPLMNPI',
Harald Welte419bb492022-02-12 21:39:35 +01001003 'I-WLAN Equivalent HPLMN Presentation Indication', service=82),
Harald Weltec91085e2022-02-10 18:05:45 +01001004 TransparentEF('4f49', 0x09, 'EF.WHPI',
Harald Welte419bb492022-02-12 21:39:35 +01001005 'I-WLAN HPLMN Priority Indication', service=83),
Harald Weltec91085e2022-02-10 18:05:45 +01001006 TransparentEF('4f4a', 0x0a, 'EF.WLRPLMN',
Harald Welte419bb492022-02-12 21:39:35 +01001007 'I-WLAN Last Registered PLMN', service=84),
Harald Weltec91085e2022-02-10 18:05:45 +01001008 TransparentEF('4f4b', 0x0b, 'EF.HPLMNDAI',
Harald Welte419bb492022-02-12 21:39:35 +01001009 'HPLMN Direct Access Indicator', service=88),
Harald Weltec91085e2022-02-10 18:05:45 +01001010 ]
Harald Welte3990ebb2021-04-20 23:55:14 +02001011 self.add_files(files)
1012
1013# TS 31.102 Section 4.4.6
1014class DF_HNB(CardDF):
Harald Welte6169c722022-02-12 09:05:15 +01001015 def __init__(self, fid='5f50', name='DF.HNB', desc='Files for HomeNodeB purpose', **kwargs):
1016 super().__init__(fid=fid, name=name, desc=desc, **kwargs)
Harald Welte3990ebb2021-04-20 23:55:14 +02001017 files = [
Harald Welteeb882052022-07-17 21:13:57 +02001018 LinFixedEF('4f81', 0x01, 'EF.ACSGL', 'Allowed CSG Lists', service=86),
1019 LinFixedEF('4f82', 0x02, 'EF.CSGTL', 'CSG Types', service=86),
1020 LinFixedEF('4f83', 0x03, 'EF.HNBN', 'Home NodeB Name', service=86),
1021 LinFixedEF('4f84', 0x04, 'EF.OCSGL', 'Operator CSG Lists', service=90),
1022 LinFixedEF('4f85', 0x05, 'EF.OCSGT', 'Operator CSG Type', service=90),
1023 LinFixedEF('4f86', 0x06, 'EF.OHNBN', 'Operator Home NodeB Name', service=90),
Harald Weltec91085e2022-02-10 18:05:45 +01001024 ]
Harald Welte3990ebb2021-04-20 23:55:14 +02001025 self.add_files(files)
1026
1027# TS 31.102 Section 4.4.8
1028class DF_ProSe(CardDF):
Harald Welte6169c722022-02-12 09:05:15 +01001029 def __init__(self, fid='5f90', name='DF.ProSe', desc='Files for ProSe purpose', **kwargs):
1030 super().__init__(fid=fid, name=name, desc=desc, **kwargs)
Harald Welte3990ebb2021-04-20 23:55:14 +02001031 files = [
Harald Weltec91085e2022-02-10 18:05:45 +01001032 LinFixedEF('4f01', 0x01, 'EF.PROSE_MON',
1033 'ProSe Monitoring Parameters'),
1034 LinFixedEF('4f02', 0x02, 'EF.PROSE_ANN',
1035 'ProSe Announcing Parameters'),
Harald Welte3990ebb2021-04-20 23:55:14 +02001036 LinFixedEF('4f03', 0x03, 'EF.PROSEFUNC', 'HPLMN ProSe Function'),
Harald Weltec91085e2022-02-10 18:05:45 +01001037 TransparentEF('4f04', 0x04, 'EF.PROSE_RADIO_COM',
1038 'ProSe Direct Communication Radio Parameters'),
1039 TransparentEF('4f05', 0x05, 'EF.PROSE_RADIO_MON',
1040 'ProSe Direct Discovery Monitoring Radio Parameters'),
1041 TransparentEF('4f06', 0x06, 'EF.PROSE_RADIO_ANN',
1042 'ProSe Direct Discovery Announcing Radio Parameters'),
1043 LinFixedEF('4f07', 0x07, 'EF.PROSE_POLICY',
1044 'ProSe Policy Parameters'),
Harald Welte3990ebb2021-04-20 23:55:14 +02001045 LinFixedEF('4f08', 0x08, 'EF.PROSE_PLMN', 'ProSe PLMN Parameters'),
1046 TransparentEF('4f09', 0x09, 'EF.PROSE_GC', 'ProSe Group Counter'),
1047 TransparentEF('4f10', 0x10, 'EF.PST', 'ProSe Service Table'),
Harald Weltec91085e2022-02-10 18:05:45 +01001048 TransparentEF('4f11', 0x11, 'EF.UIRC',
1049 'ProSe UsageInformationReportingConfiguration'),
1050 LinFixedEF('4f12', 0x12, 'EF.PROSE_GM_DISCOVERY',
1051 'ProSe Group Member Discovery Parameters'),
1052 LinFixedEF('4f13', 0x13, 'EF.PROSE_RELAY',
1053 'ProSe Relay Parameters'),
1054 TransparentEF('4f14', 0x14, 'EF.PROSE_RELAY_DISCOVERY',
1055 'ProSe Relay Discovery Parameters'),
1056 ]
Harald Welte3990ebb2021-04-20 23:55:14 +02001057 self.add_files(files)
1058
Harald Weltec91085e2022-02-10 18:05:45 +01001059
Merlin Chlosta05ca36b2021-04-01 16:15:28 +02001060class DF_USIM_5GS(CardDF):
Harald Welte6169c722022-02-12 09:05:15 +01001061 def __init__(self, fid='5FC0', name='DF.5GS', desc='5GS related files', **kwargs):
1062 super().__init__(fid=fid, name=name, desc=desc, **kwargs)
Merlin Chlosta05ca36b2021-04-01 16:15:28 +02001063 files = [
Harald Weltec91085e2022-02-10 18:05:45 +01001064 # I'm looking at 31.102 R16.6
Harald Welte419bb492022-02-12 21:39:35 +01001065 EF_5GS3GPPLOCI(service=122),
Harald Weltec91085e2022-02-10 18:05:45 +01001066 EF_5GS3GPPLOCI('4f02', 0x02, 'EF.5GSN3GPPLOCI',
Harald Welte419bb492022-02-12 21:39:35 +01001067 '5GS non-3GPP location information', service=122),
1068 EF_5GS3GPPNSC(service=122),
Harald Weltec91085e2022-02-10 18:05:45 +01001069 EF_5GS3GPPNSC('4f04', 0x04, 'EF.5GSN3GPPNSC',
Harald Welte419bb492022-02-12 21:39:35 +01001070 '5GS non-3GPP Access NAS Security Context', service=122),
1071 EF_5GAUTHKEYS(service=123),
1072 EF_UAC_AIC(service=126),
1073 EF_SUCI_Calc_Info(service=124),
1074 EF_OPL5G(service=129),
1075 EF_SUPI_NAI(service=130),
Harald Weltec91085e2022-02-10 18:05:45 +01001076 TransparentEF('4F0A', 0x0a, 'EF.Routing_Indicator',
Harald Welte419bb492022-02-12 21:39:35 +01001077 'Routing Indicator', size={4, 4}, service=124),
Harald Weltec91085e2022-02-10 18:05:45 +01001078 TransparentEF('4F0B', 0x0b, 'EF.URSP',
Harald Welte419bb492022-02-12 21:39:35 +01001079 'UE Route Selector Policies per PLMN', service=132),
1080 EF_TN3GPPSNN(service=133),
Merlin Chlosta05ca36b2021-04-01 16:15:28 +02001081 ]
Merlin Chlosta05ca36b2021-04-01 16:15:28 +02001082 self.add_files(files)
1083
Harald Weltec91085e2022-02-10 18:05:45 +01001084
Harald Welteb2edd142021-01-08 23:29:35 +01001085class ADF_USIM(CardADF):
1086 def __init__(self, aid='a0000000871002', name='ADF.USIM', fid=None, sfid=None,
1087 desc='USIM Application'):
1088 super().__init__(aid=aid, fid=fid, sfid=sfid, name=name, desc=desc)
Harald Welte15fae982021-04-10 10:22:27 +02001089 # add those commands to the general commands of a TransparentEF
1090 self.shell_commands += [self.AddlShellCommands()]
Harald Welteb2edd142021-01-08 23:29:35 +01001091
1092 files = [
Harald Weltec91085e2022-02-10 18:05:45 +01001093 EF_LI(sfid=0x02),
1094 EF_IMSI(sfid=0x07),
1095 EF_Keys(),
1096 EF_Keys('6f09', 0x09, 'EF.KeysPS',
1097 desc='Ciphering and Integrity Keys for PS domain'),
1098 EF_xPLMNwAcT('6f60', 0x0a, 'EF.PLMNwAcT',
Harald Welte6169c722022-02-12 09:05:15 +01001099 'User controlled PLMN Selector with Access Technology', service=20),
Harald Weltec91085e2022-02-10 18:05:45 +01001100 EF_HPPLMN(),
Harald Welte6169c722022-02-12 09:05:15 +01001101 EF_ACMmax(service=13),
Harald Welte6ca2fa72022-02-12 16:29:31 +01001102 EF_UST(),
Harald Weltec91085e2022-02-10 18:05:45 +01001103 CyclicEF('6f39', None, 'EF.ACM',
Harald Welte6169c722022-02-12 09:05:15 +01001104 'Accumulated call meter', rec_len={3, 3}, service=13),
1105 TransparentEF('6f3e', None, 'EF.GID1', 'Group Identifier Level 1', service=17),
1106 TransparentEF('6f3f', None, 'EF.GID2', 'Group Identifier Level 2', service=18),
1107 EF_SPN(service=19),
Harald Weltec91085e2022-02-10 18:05:45 +01001108 TransparentEF('6f41', None, 'EF.PUCT',
Harald Welte6169c722022-02-12 09:05:15 +01001109 'Price per unit and currency table', size={5, 5}, service=13),
1110 EF_CBMI(service=15),
Harald Weltec91085e2022-02-10 18:05:45 +01001111 EF_ACC(sfid=0x06),
1112 EF_PLMNsel('6f7b', 0x0d, 'EF.FPLMN',
1113 'Forbidden PLMNs', size={12, None}),
1114 EF_LOCI(),
1115 EF_AD(),
Harald Welte6169c722022-02-12 09:05:15 +01001116 EF_CBMID(sfid=0x0e, service=29),
Harald Weltec91085e2022-02-10 18:05:45 +01001117 EF_ECC(),
Harald Welte6169c722022-02-12 09:05:15 +01001118 EF_CBMIR(service=16),
Harald Weltec91085e2022-02-10 18:05:45 +01001119 EF_PSLOCI(),
Harald Welte6169c722022-02-12 09:05:15 +01001120 EF_ADN('6f3b', None, 'EF.FDN', 'Fixed Dialling Numbers', service=[2, 89]),
1121 EF_SMS('6f3c', None, service=10),
1122 EF_MSISDN(service=21),
1123 EF_SMSP(service=12),
1124 EF_SMSS(service=10),
1125 EF_ADN('6f49', None, 'EF.SDN', 'Service Dialling Numbers', service=[4, 89]),
1126 EF_EXT('6f4b', None, 'EF.EXT2', 'Extension2 (FDN)', service=3),
1127 EF_EXT('6f4c', None, 'EF.EXT3', 'Extension2 (SDN)', service=5),
1128 EF_SMSR(service=11),
1129 EF_ICI(service=9),
1130 EF_OCI(service=8),
1131 EF_ICT(service=9),
1132 EF_ICT('6f83', None, 'EF.OCT', 'Outgoing Call Timer', service=8),
1133 EF_EXT('6f4e', None, 'EF.EXT5', 'Extension5 (ICI/OCI/MSISDN)', service=44),
1134 EF_CCP2(service=14),
1135 EF_eMLPP(service=24),
1136 EF_AAeM(service=25),
Harald Weltec91085e2022-02-10 18:05:45 +01001137 # EF_Hiddenkey
Harald Welte6169c722022-02-12 09:05:15 +01001138 EF_ADN('6f4d', None, 'EF.BDN', 'Barred Dialling Numbers', service=6),
1139 EF_EXT('6f55', None, 'EF.EXT4', 'Extension4 (BDN/SSC)', service=7),
1140 EF_CMI(service=6),
Harald Welte6ca2fa72022-02-12 16:29:31 +01001141 EF_EST(service=[2, 6, 34, 35]),
Harald Welte6169c722022-02-12 09:05:15 +01001142 EF_ACL(service=35),
1143 EF_DCK(service=36),
1144 EF_CNL(service=37),
Harald Weltec91085e2022-02-10 18:05:45 +01001145 EF_START_HFN(),
1146 EF_THRESHOLD(),
Harald Welte6169c722022-02-12 09:05:15 +01001147 EF_xPLMNwAcT('6f61', 0x11, 'EF.OPLMNwAcT', 'User controlled PLMN Selector with Access Technology', service=42),
1148 EF_xPLMNwAcT('6f62', 0x13, 'EF.HPLMNwAcT', 'HPLMN Selector with Access Technology', service=43),
Harald Weltec91085e2022-02-10 18:05:45 +01001149 EF_ARR('6f06', 0x17),
1150 TransparentEF('6fc4', None, 'EF.NETPAR', 'Network Parameters'),
Harald Welte6169c722022-02-12 09:05:15 +01001151 EF_PNN('6fc5', 0x19, service=45),
1152 EF_OPL(service=46),
1153 EF_ADN('6fc7', None, 'EF.MBDN', 'Mailbox Dialling Numbers', service=47),
1154 EF_EXT('6fc8', None, 'EF.EXT6', 'Extension6 (MBDN)'),
1155 EF_MBI(service=47),
1156 EF_MWIS(service=48),
1157 EF_ADN('6fcb', None, 'EF.CFIS', 'Call Forwarding Indication Status', service=49),
Harald Weltec91085e2022-02-10 18:05:45 +01001158 EF_EXT('6fcc', None, 'EF.EXT7', 'Extension7 (CFIS)'),
Harald Welte6169c722022-02-12 09:05:15 +01001159 TransparentEF('6fcd', None, 'EF.SPDI', 'Service Provider Display Information', service=51),
1160 EF_MMSN(service=52),
1161 EF_EXT('6fcf', None, 'EF.EXT8', 'Extension8 (MMSN)', service=53),
1162 EF_MMSICP(service=52),
1163 EF_MMSUP(service=52),
1164 EF_MMSUCP(service=(52, 55)),
1165 EF_NIA(service=56),
1166 EF_VGCS(service=57),
1167 EF_VGCSS(service=57),
1168 EF_VGCS('6fb3', None, 'EF.VBS', 'Voice Broadcast Service', service=58),
1169 EF_VGCSS('6fb4', None, 'EF.VBSS', 'Voice Broadcast Service Status', service=58),
1170 EF_VGCSCA(service=64),
1171 EF_VGCSCA('6fd5', None, 'EF.VBCSCA', 'Voice Broadcast Service Ciphering Algorithm', service=65),
1172 EF_GBABP(service=68),
1173 EF_MSK(service=69),
1174 EF_MUK(service=69),
1175 EF_GBANL(service=68),
1176 EF_PLMNsel('6fd9', 0x1d, 'EF.EHPLMN', 'Equivalent HPLMN', size={12, None}, service=71),
1177 EF_EHPLMNPI(service=(71, 73)),
1178 # EF_LRPLMNSI ('6fdc', service=74)
1179 EF_NAFKCA(service=(68, 76)),
1180 TransparentEF('6fde', None, 'EF.SPNI', 'Service Provider Name Icon', service=78),
1181 LinFixedEF('6fdf', None, 'EF.PNNI', 'PLMN Network Name Icon', service=79),
1182 EF_NCP_IP(service=80),
1183 EF_EPSLOCI('6fe3', 0x1e, 'EF.EPSLOCI', 'EPS location information', service=85),
1184 EF_EPSNSC(service=85),
1185 TransparentEF('6fe6', None, 'EF.UFC', 'USAT Facility Control', size={1, 16}),
1186 TransparentEF('6fe8', None, 'EF.NASCONFIG', 'Non Access Stratum Configuration', service=96),
1187 # UICC IARI (only in cards that have no ISIM) service=95
1188 EF_PWS(service=97),
1189 LinFixedEF('6fed', None, 'EF.FDNURI', 'Fixed Dialling Numbers URI', service=(2, 99)),
1190 LinFixedEF('6fee', None, 'EF.BDNURI', 'Barred Dialling Numbers URI', service=(6, 99)),
1191 LinFixedEF('6fef', None, 'EF.SDNURI', 'Service Dialling Numbers URI', service=(4, 99)),
1192 # EF_IWL (IMEI(SV) White List)
Harald Weltec91085e2022-02-10 18:05:45 +01001193 EF_IPS(),
Harald Welte6169c722022-02-12 09:05:15 +01001194 EF_ePDGId(service=(106, 107)),
Harald Weltec91085e2022-02-10 18:05:45 +01001195 # FIXME: from EF_ePDGSelection onwards
Harald Welte6169c722022-02-12 09:05:15 +01001196 EF_FromPreferred(service=114),
1197 # FIXME: DF_SoLSA service=23
Harald Weltede4c14c2022-07-16 11:53:59 +02001198 DF_PHONEBOOK(),
Harald Welte6169c722022-02-12 09:05:15 +01001199 # FIXME: DF_GSM_ACCESS service=27
1200 DF_WLAN(service=[59, 60, 61, 62, 63, 66, 81, 82, 83, 84, 88]),
1201 DF_HNB(service=[86, 90]),
1202 DF_ProSe(service=101),
1203 # FIXME: DF_ACDC service=108
1204 # FIXME: DF_TV service=116
1205 DF_USIM_5GS(service=[122, 123, 124, 125, 126, 127, 129, 130]),
Harald Weltec91085e2022-02-10 18:05:45 +01001206 ]
Harald Welteb2edd142021-01-08 23:29:35 +01001207 self.add_files(files)
1208
1209 def decode_select_response(self, data_hex):
Philipp Maier5998a3a2021-11-16 15:16:39 +01001210 return pySim.ts_102_221.CardProfileUICC.decode_select_response(data_hex)
Harald Welteb2edd142021-01-08 23:29:35 +01001211
Harald Welte15fae982021-04-10 10:22:27 +02001212 @with_default_category('Application-Specific Commands')
1213 class AddlShellCommands(CommandSet):
1214 def __init__(self):
1215 super().__init__()
1216
1217 authenticate_parser = argparse.ArgumentParser()
1218 authenticate_parser.add_argument('rand', help='Random challenge')
1219 authenticate_parser.add_argument('autn', help='Authentication Nonce')
1220 #authenticate_parser.add_argument('--context', help='Authentication context', default='3G')
Harald Weltec91085e2022-02-10 18:05:45 +01001221
Harald Welte15fae982021-04-10 10:22:27 +02001222 @cmd2.with_argparser(authenticate_parser)
1223 def do_authenticate(self, opts):
1224 """Perform Authentication and Key Agreement (AKA)."""
1225 (data, sw) = self._cmd.card._scc.authenticate(opts.rand, opts.autn)
1226 self._cmd.poutput_json(data)
1227
Harald Welte12af7932022-02-15 16:39:08 +01001228 term_prof_parser = argparse.ArgumentParser()
1229 term_prof_parser.add_argument('PROFILE', help='Hexstring of encoded terminal profile')
1230
1231 @cmd2.with_argparser(term_prof_parser)
Harald Welte846a8982021-10-08 15:47:16 +02001232 def do_terminal_profile(self, arg):
Harald Welte12af7932022-02-15 16:39:08 +01001233 """Send a TERMINAL PROFILE command to the card.
1234 This is used to inform the card about which optional
1235 features the terminal (modem/phone) supports, particularly
1236 in the context of SIM Toolkit, Proactive SIM and OTA. You
1237 must specify a hex-string with the encoded terminal profile
1238 you want to send to the card."""
Harald Welte846a8982021-10-08 15:47:16 +02001239 (data, sw) = self._cmd.card._scc.terminal_profile(arg)
1240 self._cmd.poutput('SW: %s, data: %s' % (sw, data))
Harald Welte15fae982021-04-10 10:22:27 +02001241
Harald Welte12af7932022-02-15 16:39:08 +01001242 envelope_parser = argparse.ArgumentParser()
1243 envelope_parser.add_argument('PAYLOAD', help='Hexstring of encoded payload to ENVELOPE')
1244
1245 @cmd2.with_argparser(envelope_parser)
Harald Welte7cb94e42021-10-08 15:47:57 +02001246 def do_envelope(self, arg):
Harald Welte12af7932022-02-15 16:39:08 +01001247 """Send an ENVELOPE command to the card. This is how a
1248 variety of information is communicated from the terminal
1249 (modem/phone) to the card, particularly in the context of
1250 SIM Toolkit, Proactive SIM and OTA."""
Harald Welte7cb94e42021-10-08 15:47:57 +02001251 (data, sw) = self._cmd.card._scc.envelope(arg)
1252 self._cmd.poutput('SW: %s, data: %s' % (sw, data))
1253
Harald Welte12af7932022-02-15 16:39:08 +01001254 envelope_sms_parser = argparse.ArgumentParser()
1255 envelope_sms_parser.add_argument('TPDU', help='Hexstring of encoded SMS TPDU')
1256
1257 @cmd2.with_argparser(envelope_sms_parser)
Harald Welte7cb94e42021-10-08 15:47:57 +02001258 def do_envelope_sms(self, arg):
Harald Welte12af7932022-02-15 16:39:08 +01001259 """Send an ENVELOPE(SMS-PP-Download) command to the card.
1260 This emulates a terminal (modem/phone) having received a SMS
1261 with a PID of 'SMS for the SIM card'. You can use this
1262 command in the context of testing OTA related features
1263 without a modem/phone or a cellular netwokr."""
Harald Welte7cb94e42021-10-08 15:47:57 +02001264 tpdu_ie = SMS_TPDU()
1265 tpdu_ie.from_bytes(h2b(arg))
Harald Weltec91085e2022-02-10 18:05:45 +01001266 dev_ids = DeviceIdentities(
1267 decoded={'source_dev_id': 'network', 'dest_dev_id': 'uicc'})
Harald Welte7cb94e42021-10-08 15:47:57 +02001268 sms_dl = SMSPPDownload(children=[dev_ids, tpdu_ie])
1269 (data, sw) = self._cmd.card._scc.envelope(b2h(sms_dl.to_tlv()))
1270 self._cmd.poutput('SW: %s, data: %s' % (sw, data))
1271
Harald Welte15fae982021-04-10 10:22:27 +02001272
Harald Welteb2edd142021-01-08 23:29:35 +01001273# TS 31.102 Section 7.3
1274sw_usim = {
1275 'Security management': {
1276 '9862': 'Authentication error, incorrect MAC',
1277 '9864': 'Authentication error, security context not supported',
1278 '9865': 'Key freshness failure',
1279 '9866': 'Authentication error, no memory space available',
1280 '9867': 'Authentication error, no memory space available in EF MUK',
1281 }
1282}
1283
Harald Weltec91085e2022-02-10 18:05:45 +01001284
Philipp Maier57f65ee2021-10-18 14:09:02 +02001285class CardApplicationUSIM(CardApplication):
1286 def __init__(self):
Harald Weltec91085e2022-02-10 18:05:45 +01001287 super().__init__('USIM', adf=ADF_USIM(), sw=sw_usim)