blob: ac0dc84c403a69f11d91b240636d0170d8ae5cd7 [file] [log] [blame]
Harald Welteb2edd142021-01-08 23:29:35 +01001# coding=utf-8
2"""Utilities / Functions related to ETSI TS 102 221, the core UICC spec.
3
4(C) 2021 by Harald Welte <laforge@osmocom.org>
5
6This program is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program. If not, see <http://www.gnu.org/licenses/>.
18"""
19
Harald Welte8f892fb2021-06-05 10:12:43 +020020from construct import *
Harald Welte747a9782022-02-13 17:52:28 +010021from construct import Optional as COptional
Harald Welte8f892fb2021-06-05 10:12:43 +020022from pySim.construct import *
Harald Welteb2edd142021-01-08 23:29:35 +010023from pySim.utils import *
24from pySim.filesystem import *
Harald Welte181c7c52022-02-10 14:18:32 +010025from pySim.tlv import *
Harald Welte4ae228a2021-05-02 21:29:04 +020026from bidict import bidict
Philipp Maiera028c7d2021-11-08 16:12:03 +010027from pySim.profile import CardProfile
28from pySim.profile import match_uicc
29from pySim.profile import match_sim
Harald Welte181c7c52022-02-10 14:18:32 +010030import pySim.iso7816_4 as iso7816_4
Philipp Maiera028c7d2021-11-08 16:12:03 +010031
32# A UICC will usually also support 2G functionality. If this is the case, we
33# need to add DF_GSM and DF_TELECOM along with the UICC related files
Harald Welte323a3502023-07-11 17:26:39 +020034from pySim.ts_51_011 import DF_GSM, DF_TELECOM, AddonSIM
35from pySim.gsm_r import AddonGSMR
36from pySim.cdma_ruim import AddonRUIM
Harald Welte4ae228a2021-05-02 21:29:04 +020037
38ts_102_22x_cmdset = CardCommandSet('TS 102 22x', [
39 # TS 102 221 Section 10.1.2 Table 10.5 "Coding of Instruction Byte"
40 CardCommand('SELECT', 0xA4, ['0X', '4X', '6X']),
41 CardCommand('STATUS', 0xF2, ['8X', 'CX', 'EX']),
42 CardCommand('READ BINARY', 0xB0, ['0X', '4X', '6X']),
43 CardCommand('UPDATE BINARY', 0xD6, ['0X', '4X', '6X']),
44 CardCommand('READ RECORD', 0xB2, ['0X', '4X', '6X']),
45 CardCommand('UPDATE RECORD', 0xDC, ['0X', '4X', '6X']),
46 CardCommand('SEARCH RECORD', 0xA2, ['0X', '4X', '6X']),
47 CardCommand('INCREASE', 0x32, ['8X', 'CX', 'EX']),
48 CardCommand('RETRIEVE DATA', 0xCB, ['8X', 'CX', 'EX']),
49 CardCommand('SET DATA', 0xDB, ['8X', 'CX', 'EX']),
50 CardCommand('VERIFY PIN', 0x20, ['0X', '4X', '6X']),
51 CardCommand('CHANGE PIN', 0x24, ['0X', '4X', '6X']),
52 CardCommand('DISABLE PIN', 0x26, ['0X', '4X', '6X']),
53 CardCommand('ENABLE PIN', 0x28, ['0X', '4X', '6X']),
54 CardCommand('UNBLOCK PIN', 0x2C, ['0X', '4X', '6X']),
55 CardCommand('DEACTIVATE FILE', 0x04, ['0X', '4X', '6X']),
56 CardCommand('ACTIVATE FILE', 0x44, ['0X', '4X', '6X']),
57 CardCommand('AUTHENTICATE', 0x88, ['0X', '4X', '6X']),
58 CardCommand('AUTHENTICATE', 0x89, ['0X', '4X', '6X']),
59 CardCommand('GET CHALLENGE', 0x84, ['0X', '4X', '6X']),
60 CardCommand('TERMINAL CAPABILITY', 0xAA, ['8X', 'CX', 'EX']),
61 CardCommand('TERMINAL PROFILE', 0x10, ['80']),
62 CardCommand('ENVELOPE', 0xC2, ['80']),
63 CardCommand('FETCH', 0x12, ['80']),
64 CardCommand('TERMINAL RESPONSE', 0x14, ['80']),
65 CardCommand('MANAGE CHANNEL', 0x70, ['0X', '4X', '6X']),
66 CardCommand('MANAGE SECURE CHANNEL', 0x73, ['0X', '4X', '6X']),
67 CardCommand('TRANSACT DATA', 0x75, ['0X', '4X', '6X']),
68 CardCommand('SUSPEND UICC', 0x76, ['80']),
69 CardCommand('GET IDENTITY', 0x78, ['8X', 'CX', 'EX']),
70 CardCommand('EXCHANGE CAPABILITIES', 0x7A, ['80']),
71 CardCommand('GET RESPONSE', 0xC0, ['0X', '4X', '6X']),
72 # TS 102 222 Section 6.1 Table 1 "Coding of the commands"
73 CardCommand('CREATE FILE', 0xE0, ['0X', '4X']),
74 CardCommand('DELETE FILE', 0xE4, ['0X', '4X']),
75 CardCommand('DEACTIVATE FILE', 0x04, ['0X', '4X']),
76 CardCommand('ACTIVATE FILE', 0x44, ['0X', '4X']),
77 CardCommand('TERMINATE DF', 0xE6, ['0X', '4X']),
78 CardCommand('TERMINATE EF', 0xE8, ['0X', '4X']),
79 CardCommand('TERMINATE CARD USAGE', 0xFE, ['0X', '4X']),
80 CardCommand('RESIZE FILE', 0xD4, ['8X', 'CX']),
Harald Weltec91085e2022-02-10 18:05:45 +010081])
Harald Welteb2edd142021-01-08 23:29:35 +010082
Harald Weltec8c33272022-02-11 14:45:23 +010083# ETSI TS 102 221 11.1.1.4.2
84class FileSize(BER_TLV_IE, tag=0x80):
Philipp Maier541a9152022-06-01 18:21:17 +020085 _construct = GreedyInteger(minlen=2)
Harald Welteb2edd142021-01-08 23:29:35 +010086
Harald Weltec8c33272022-02-11 14:45:23 +010087# ETSI TS 102 221 11.1.1.4.2
88class TotalFileSize(BER_TLV_IE, tag=0x81):
Philipp Maier541a9152022-06-01 18:21:17 +020089 _construct = GreedyInteger(minlen=2)
Harald Welteb2edd142021-01-08 23:29:35 +010090
91# ETSI TS 102 221 11.1.1.4.3
Harald Weltec8c33272022-02-11 14:45:23 +010092class FileDescriptor(BER_TLV_IE, tag=0x82):
Harald Welte747a9782022-02-13 17:52:28 +010093 class BerTlvAdapter(Adapter):
94 def _parse(self, obj, context, path):
95 if obj == 0x39:
96 return 'ber_tlv'
97 raise ValidationError
98 def _build(self, obj, context, path):
99 if obj == 'ber_tlv':
100 return 0x39
101 raise ValidationError
102
103 FDB = Select(BitStruct(Const(0, Bit), 'shareable'/Flag, 'structure'/BerTlvAdapter(Const(0x39, BitsInteger(6)))),
104 BitStruct(Const(0, Bit), 'shareable'/Flag, 'file_type'/Enum(BitsInteger(3), working_ef=0, internal_ef=1, df=7),
105 'structure'/Enum(BitsInteger(3), no_info_given=0, transparent=1, linear_fixed=2, cyclic=6))
106 )
107 _construct = Struct('file_descriptor_byte'/FDB, Const(b'\x21'),
Harald Welte4ebeebf2022-02-25 09:47:51 +0100108 'record_len'/COptional(Int16ub), 'num_of_rec'/COptional(Int8ub))
Harald Weltec91085e2022-02-10 18:05:45 +0100109
Harald Weltec8c33272022-02-11 14:45:23 +0100110# ETSI TS 102 221 11.1.1.4.4
111class FileIdentifier(BER_TLV_IE, tag=0x83):
Harald Welte3c9b7842021-10-19 21:44:24 +0200112 _construct = HexAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100113
Harald Weltec8c33272022-02-11 14:45:23 +0100114# ETSI TS 102 221 11.1.1.4.5
115class DfName(BER_TLV_IE, tag=0x84):
Harald Welte3c9b7842021-10-19 21:44:24 +0200116 _construct = HexAdapter(GreedyBytes)
Harald Weltec8c33272022-02-11 14:45:23 +0100117
118# ETSI TS 102 221 11.1.1.4.6.1
119class UiccCharacteristics(BER_TLV_IE, tag=0x80):
120 _construct = GreedyBytes
121
122# ETSI TS 102 221 11.1.1.4.6.2
123class ApplicationPowerConsumption(BER_TLV_IE, tag=0x81):
124 _construct = Struct('voltage_class'/Int8ub,
125 'power_consumption_ma'/Int8ub,
126 'reference_freq_100k'/Int8ub)
127
128# ETSI TS 102 221 11.1.1.4.6.3
129class MinApplicationClockFrequency(BER_TLV_IE, tag=0x82):
130 _construct = Int8ub
131
132# ETSI TS 102 221 11.1.1.4.6.4
133class AvailableMemory(BER_TLV_IE, tag=0x83):
134 _construct = GreedyInteger()
135
136# ETSI TS 102 221 11.1.1.4.6.5
137class FileDetails(BER_TLV_IE, tag=0x84):
138 _construct = FlagsEnum(Byte, der_coding_only=1)
139
140# ETSI TS 102 221 11.1.1.4.6.6
141class ReservedFileSize(BER_TLV_IE, tag=0x85):
142 _construct = GreedyInteger()
143
144# ETSI TS 102 221 11.1.1.4.6.7
145class MaximumFileSize(BER_TLV_IE, tag=0x86):
146 _construct = GreedyInteger()
147
148# ETSI TS 102 221 11.1.1.4.6.8
149class SupportedFilesystemCommands(BER_TLV_IE, tag=0x87):
150 _construct = FlagsEnum(Byte, terminal_capability=1)
151
152# ETSI TS 102 221 11.1.1.4.6.9
153class SpecificUiccEnvironmentConditions(BER_TLV_IE, tag=0x88):
154 _construct = BitStruct('rfu'/BitsRFU(4),
155 'high_humidity_supported'/Flag,
156 'temperature_class'/Enum(BitsInteger(3), standard=0, class_A=1, class_B=2, class_C=3))
157
158# ETSI TS 102 221 11.1.1.4.6.10
159class Platform2PlatformCatSecuredApdu(BER_TLV_IE, tag=0x89):
160 _construct = GreedyBytes
161
Harald Welte3c9b7842021-10-19 21:44:24 +0200162# sysmoISIM-SJA2 specific
163class ToolkitAccessConditions(BER_TLV_IE, tag=0xD2):
164 _construct = FlagsEnum(Byte, rfm_create=1, rfm_delete_terminate=2, other_applet_create=4,
165 other_applet_delete_terminate=8)
166
Harald Weltec8c33272022-02-11 14:45:23 +0100167# ETSI TS 102 221 11.1.1.4.6.0
168class ProprietaryInformation(BER_TLV_IE, tag=0xA5,
169 nested=[UiccCharacteristics, ApplicationPowerConsumption,
170 MinApplicationClockFrequency, AvailableMemory,
171 FileDetails, ReservedFileSize, MaximumFileSize,
Harald Welte3c9b7842021-10-19 21:44:24 +0200172 SupportedFilesystemCommands, SpecificUiccEnvironmentConditions,
173 ToolkitAccessConditions]):
Harald Weltec8c33272022-02-11 14:45:23 +0100174 pass
175
176# ETSI TS 102 221 11.1.1.4.7.1
177class SecurityAttribCompact(BER_TLV_IE, tag=0x8c):
178 _construct = GreedyBytes
179
180# ETSI TS 102 221 11.1.1.4.7.2
181class SecurityAttribExpanded(BER_TLV_IE, tag=0xab):
182 _construct = GreedyBytes
183
184# ETSI TS 102 221 11.1.1.4.7.3
185class SecurityAttribReferenced(BER_TLV_IE, tag=0x8b):
186 # TODO: longer format with SEID
Harald Welte3c9b7842021-10-19 21:44:24 +0200187 _construct = Struct('ef_arr_file_id'/HexAdapter(Bytes(2)), 'ef_arr_record_nr'/Int8ub)
Harald Weltec8c33272022-02-11 14:45:23 +0100188
189# ETSI TS 102 221 11.1.1.4.8
190class ShortFileIdentifier(BER_TLV_IE, tag=0x88):
Harald Welte5e9bd932022-02-25 09:35:28 +0100191 # If the length of the TLV is 1, the SFI value is indicated in the 5 most significant bits (bits b8 to b4)
192 # of the TLV value field. In this case, bits b3 to b1 shall be set to 0
193 class Shift3RAdapter(Adapter):
194 def _decode(self, obj, context, path):
Philipp Maier373b23c2022-06-01 18:16:02 +0200195 return int.from_bytes(obj, 'big') >> 3
Harald Welte5e9bd932022-02-25 09:35:28 +0100196 def _encode(self, obj, context, path):
Philipp Maier373b23c2022-06-01 18:16:02 +0200197 val = int(obj) << 3
198 return val.to_bytes(1, 'big')
199 _construct = COptional(Shift3RAdapter(Bytes(1)))
Harald Welteb2edd142021-01-08 23:29:35 +0100200
201# ETSI TS 102 221 11.1.1.4.9
Harald Weltec8c33272022-02-11 14:45:23 +0100202class LifeCycleStatusInteger(BER_TLV_IE, tag=0x8A):
203 def _from_bytes(self, do: bytes):
204 lcsi = int.from_bytes(do, 'big')
205 if lcsi == 0x00:
206 ret = 'no_information'
207 elif lcsi == 0x01:
208 ret = 'creation'
209 elif lcsi == 0x03:
210 ret = 'initialization'
211 elif lcsi & 0x05 == 0x05:
212 ret = 'operational_activated'
213 elif lcsi & 0x05 == 0x04:
214 ret = 'operational_deactivated'
215 elif lcsi & 0xc0 == 0xc0:
216 ret = 'termination'
217 else:
218 ret = lcsi
219 self.decoded = ret
220 return self.decoded
Harald Welte3c9b7842021-10-19 21:44:24 +0200221 def _to_bytes(self):
222 if self.decoded == 'no_information':
223 return b'\x00'
224 elif self.decoded == 'creation':
225 return b'\x01'
226 elif self.decoded == 'initialization':
227 return b'\x03'
228 elif self.decoded == 'operational_activated':
229 return b'\x05'
230 elif self.decoded == 'operational_deactivated':
231 return b'\x04'
232 elif self.decoded == 'termination':
233 return b'\x0c'
234 elif isinstance(self.decoded, int):
235 return self.decoded.to_bytes(1, 'big')
236 else:
237 raise ValueError
Harald Weltec91085e2022-02-10 18:05:45 +0100238
Harald Weltec8c33272022-02-11 14:45:23 +0100239# ETSI TS 102 221 11.1.1.4.9
240class PS_DO(BER_TLV_IE, tag=0x90):
241 _construct = GreedyBytes
242class UsageQualifier_DO(BER_TLV_IE, tag=0x95):
243 _construct = GreedyBytes
244class KeyReference(BER_TLV_IE, tag=0x83):
245 _construct = Byte
246class PinStatusTemplate_DO(BER_TLV_IE, tag=0xC6, nested=[PS_DO, UsageQualifier_DO, KeyReference]):
247 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100248
Harald Weltec8c33272022-02-11 14:45:23 +0100249class FcpTemplate(BER_TLV_IE, tag=0x62, nested=[FileSize, TotalFileSize, FileDescriptor, FileIdentifier,
250 DfName, ProprietaryInformation, SecurityAttribCompact,
251 SecurityAttribExpanded, SecurityAttribReferenced,
252 ShortFileIdentifier, LifeCycleStatusInteger,
253 PinStatusTemplate_DO]):
254 pass
Harald Welteb2edd142021-01-08 23:29:35 +0100255
256
257def tlv_key_replace(inmap, indata):
258 def newkey(inmap, key):
259 if key in inmap:
260 return inmap[key]
261 else:
262 return key
263 return {newkey(inmap, d[0]): d[1] for d in indata.items()}
264
Harald Weltec91085e2022-02-10 18:05:45 +0100265
Harald Welteb2edd142021-01-08 23:29:35 +0100266def tlv_val_interpret(inmap, indata):
267 def newval(inmap, key, val):
268 if key in inmap:
269 return inmap[key](val)
270 else:
271 return val
272 return {d[0]: newval(inmap, d[0], d[1]) for d in indata.items()}
273
Harald Welte4ae228a2021-05-02 21:29:04 +0200274# ETSI TS 102 221 Section 9.2.7 + ISO7816-4 9.3.3/9.3.4
Harald Welte4ae228a2021-05-02 21:29:04 +0200275class _AM_DO_DF(DataObject):
276 def __init__(self):
277 super().__init__('access_mode', 'Access Mode', tag=0x80)
278
Harald Weltec91085e2022-02-10 18:05:45 +0100279 def from_bytes(self, do: bytes):
Harald Welte4ae228a2021-05-02 21:29:04 +0200280 res = []
281 if len(do) != 1:
282 raise ValueError("We only support single-byte AMF inside AM-DO")
283 amf = do[0]
284 # tables 17..29 and 41..44 of 7816-4
285 if amf & 0x80 == 0:
286 if amf & 0x40:
287 res.append('delete_file')
288 if amf & 0x20:
289 res.append('terminate_df')
290 if amf & 0x10:
291 res.append('activate_file')
292 if amf & 0x08:
293 res.append('deactivate_file')
294 if amf & 0x04:
295 res.append('create_file_df')
296 if amf & 0x02:
297 res.append('create_file_ef')
298 if amf & 0x01:
299 res.append('delete_file_child')
300 self.decoded = res
301
302 def to_bytes(self):
303 val = 0
304 if 'delete_file' in self.decoded:
305 val |= 0x40
306 if 'terminate_df' in self.decoded:
307 val |= 0x20
308 if 'activate_file' in self.decoded:
309 val |= 0x10
310 if 'deactivate_file' in self.decoded:
311 val |= 0x08
312 if 'create_file_df' in self.decoded:
313 val |= 0x04
314 if 'create_file_ef' in self.decoded:
315 val |= 0x02
316 if 'delete_file_child' in self.decoded:
317 val |= 0x01
318 return val.to_bytes(1, 'big')
319
320
321class _AM_DO_EF(DataObject):
322 """ISO7816-4 9.3.2 Table 18 + 9.3.3.1 Table 31"""
Harald Weltec91085e2022-02-10 18:05:45 +0100323
Harald Welte4ae228a2021-05-02 21:29:04 +0200324 def __init__(self):
325 super().__init__('access_mode', 'Access Mode', tag=0x80)
326
Harald Weltec91085e2022-02-10 18:05:45 +0100327 def from_bytes(self, do: bytes):
Harald Welte4ae228a2021-05-02 21:29:04 +0200328 res = []
329 if len(do) != 1:
330 raise ValueError("We only support single-byte AMF inside AM-DO")
331 amf = do[0]
332 # tables 17..29 and 41..44 of 7816-4
333 if amf & 0x80 == 0:
334 if amf & 0x40:
335 res.append('delete_file')
336 if amf & 0x20:
337 res.append('terminate_ef')
338 if amf & 0x10:
339 res.append('activate_file_or_record')
340 if amf & 0x08:
341 res.append('deactivate_file_or_record')
342 if amf & 0x04:
343 res.append('write_append')
344 if amf & 0x02:
345 res.append('update_erase')
346 if amf & 0x01:
347 res.append('read_search_compare')
348 self.decoded = res
349
350 def to_bytes(self):
351 val = 0
352 if 'delete_file' in self.decoded:
353 val |= 0x40
354 if 'terminate_ef' in self.decoded:
355 val |= 0x20
356 if 'activate_file_or_record' in self.decoded:
357 val |= 0x10
358 if 'deactivate_file_or_record' in self.decoded:
359 val |= 0x08
360 if 'write_append' in self.decoded:
361 val |= 0x04
362 if 'update_erase' in self.decoded:
363 val |= 0x02
364 if 'read_search_compare' in self.decoded:
365 val |= 0x01
366 return val.to_bytes(1, 'big')
367
Harald Weltec91085e2022-02-10 18:05:45 +0100368
Harald Welte4ae228a2021-05-02 21:29:04 +0200369class _AM_DO_CHDR(DataObject):
370 """Command Header Access Mode DO according to ISO 7816-4 Table 32."""
Harald Weltec91085e2022-02-10 18:05:45 +0100371
Harald Welte4ae228a2021-05-02 21:29:04 +0200372 def __init__(self, tag):
373 super().__init__('command_header', 'Command Header Description', tag=tag)
374
Harald Weltec91085e2022-02-10 18:05:45 +0100375 def from_bytes(self, do: bytes):
Harald Welte4ae228a2021-05-02 21:29:04 +0200376 res = {}
377 i = 0
378 if self.tag & 0x08:
379 res['CLA'] = do[i]
380 i += 1
381 if self.tag & 0x04:
382 res['INS'] = do[i]
383 i += 1
384 if self.tag & 0x02:
385 res['P1'] = do[i]
386 i += 1
387 if self.tag & 0x01:
388 res['P2'] = do[i]
389 i += 1
390 self.decoded = res
391
392 def _compute_tag(self):
393 """Override to encode the tag, as it depends on the value."""
394 tag = 0x80
395 if 'CLA' in self.decoded:
396 tag |= 0x08
397 if 'INS' in self.decoded:
398 tag |= 0x04
399 if 'P1' in self.decoded:
400 tag |= 0x02
401 if 'P2' in self.decoded:
402 tag |= 0x01
403 return tag
404
405 def to_bytes(self):
406 res = bytearray()
407 if 'CLA' in self.decoded:
408 res.append(self.decoded['CLA'])
409 if 'INS' in self.decoded:
410 res.append(self.decoded['INS'])
411 if 'P1' in self.decoded:
412 res.append(self.decoded['P1'])
413 if 'P2' in self.decoded:
414 res.append(self.decoded['P2'])
415 return res
416
Harald Weltec91085e2022-02-10 18:05:45 +0100417
Harald Welte4ae228a2021-05-02 21:29:04 +0200418AM_DO_CHDR = DataObjectChoice('am_do_chdr', members=[
Harald Weltec91085e2022-02-10 18:05:45 +0100419 _AM_DO_CHDR(0x81), _AM_DO_CHDR(0x82), _AM_DO_CHDR(0x83), _AM_DO_CHDR(0x84),
420 _AM_DO_CHDR(0x85), _AM_DO_CHDR(0x86), _AM_DO_CHDR(0x87), _AM_DO_CHDR(0x88),
421 _AM_DO_CHDR(0x89), _AM_DO_CHDR(0x8a), _AM_DO_CHDR(0x8b), _AM_DO_CHDR(0x8c),
422 _AM_DO_CHDR(0x8d), _AM_DO_CHDR(0x8e), _AM_DO_CHDR(0x8f)])
Harald Welte4ae228a2021-05-02 21:29:04 +0200423
424AM_DO_DF = AM_DO_CHDR | _AM_DO_DF()
425AM_DO_EF = AM_DO_CHDR | _AM_DO_EF()
426
427
428# TS 102 221 Section 9.5.1 / Table 9.3
429pin_names = bidict({
430 0x01: 'PIN1',
431 0x02: 'PIN2',
432 0x03: 'PIN3',
433 0x04: 'PIN4',
434 0x05: 'PIN5',
435 0x06: 'PIN6',
436 0x07: 'PIN7',
437 0x08: 'PIN8',
438 0x0a: 'ADM1',
439 0x0b: 'ADM2',
440 0x0c: 'ADM3',
441 0x0d: 'ADM4',
442 0x0e: 'ADM5',
443
444 0x11: 'UNIVERSAL_PIN',
445 0x81: '2PIN1',
446 0x82: '2PIN2',
447 0x83: '2PIN3',
448 0x84: '2PIN4',
449 0x85: '2PIN5',
450 0x86: '2PIN6',
451 0x87: '2PIN7',
452 0x88: '2PIN8',
453 0x8a: 'ADM6',
454 0x8b: 'ADM7',
455 0x8c: 'ADM8',
456 0x8d: 'ADM9',
457 0x8e: 'ADM10',
Harald Weltec91085e2022-02-10 18:05:45 +0100458})
459
Harald Welte4ae228a2021-05-02 21:29:04 +0200460
461class CRT_DO(DataObject):
462 """Control Reference Template as per TS 102 221 9.5.1"""
Harald Weltec91085e2022-02-10 18:05:45 +0100463
Harald Welte4ae228a2021-05-02 21:29:04 +0200464 def __init__(self):
Harald Weltec91085e2022-02-10 18:05:45 +0100465 super().__init__('control_reference_template',
466 'Control Reference Template', tag=0xA4)
Harald Welte4ae228a2021-05-02 21:29:04 +0200467
468 def from_bytes(self, do: bytes):
469 """Decode a Control Reference Template DO."""
470 if len(do) != 6:
471 raise ValueError('Unsupported CRT DO length: %s', do)
472 if do[0] != 0x83 or do[1] != 0x01:
473 raise ValueError('Unsupported Key Ref Tag or Len in CRT DO %s', do)
474 if do[3:] != b'\x95\x01\x08':
Harald Weltec91085e2022-02-10 18:05:45 +0100475 raise ValueError(
476 'Unsupported Usage Qualifier Tag or Len in CRT DO %s', do)
Harald Welte4ae228a2021-05-02 21:29:04 +0200477 self.encoded = do[0:6]
478 self.decoded = pin_names[do[2]]
479 return do[6:]
480
481 def to_bytes(self):
482 pin = pin_names.inverse[self.decoded]
483 return b'\x83\x01' + pin.to_bytes(1, 'big') + b'\x95\x01\x08'
484
485# ISO7816-4 9.3.3 Table 33
486class SecCondByte_DO(DataObject):
487 def __init__(self, tag=0x9d):
488 super().__init__('security_condition_byte', tag=tag)
489
Harald Weltec91085e2022-02-10 18:05:45 +0100490 def from_bytes(self, binary: bytes):
Harald Welte4ae228a2021-05-02 21:29:04 +0200491 if len(binary) != 1:
492 raise ValueError
493 inb = binary[0]
494 if inb == 0:
495 cond = 'always'
496 if inb == 0xff:
497 cond = 'never'
498 res = []
499 if inb & 0x80:
500 cond = 'and'
501 else:
502 cond = 'or'
503 if inb & 0x40:
504 res.append('secure_messaging')
505 if inb & 0x20:
506 res.append('external_auth')
507 if inb & 0x10:
508 res.append('user_auth')
Harald Weltec91085e2022-02-10 18:05:45 +0100509 rd = {'mode': cond}
Harald Welte4ae228a2021-05-02 21:29:04 +0200510 if len(res):
511 rd['conditions'] = res
512 self.decoded = rd
513
514 def to_bytes(self):
515 mode = self.decoded['mode']
516 if mode == 'always':
517 res = 0
518 elif mode == 'never':
519 res = 0xff
520 else:
521 res = 0
522 if mode == 'and':
523 res |= 0x80
524 elif mode == 'or':
525 pass
526 else:
527 raise ValueError('Unknown mode %s' % mode)
528 for c in self.decoded['conditions']:
529 if c == 'secure_messaging':
530 res |= 0x40
531 elif c == 'external_auth':
532 res |= 0x20
533 elif c == 'user_auth':
534 res |= 0x10
535 else:
536 raise ValueError('Unknown condition %s' % c)
537 return res.to_bytes(1, 'big')
538
Harald Weltec91085e2022-02-10 18:05:45 +0100539
Harald Welte4ae228a2021-05-02 21:29:04 +0200540Always_DO = TL0_DataObject('always', 'Always', 0x90)
541Never_DO = TL0_DataObject('never', 'Never', 0x97)
Harald Welteb0608332022-02-10 12:45:37 +0100542
Harald Weltec91085e2022-02-10 18:05:45 +0100543
Harald Welteb0608332022-02-10 12:45:37 +0100544class Nested_DO(DataObject):
545 """A DO that nests another DO/Choice/Sequence"""
Harald Weltec91085e2022-02-10 18:05:45 +0100546
Harald Welteb0608332022-02-10 12:45:37 +0100547 def __init__(self, name, tag, choice):
548 super().__init__(name, tag=tag)
549 self.children = choice
Harald Weltec91085e2022-02-10 18:05:45 +0100550
551 def from_bytes(self, binary: bytes) -> list:
Harald Welteb0608332022-02-10 12:45:37 +0100552 remainder = binary
553 self.decoded = []
554 while remainder:
555 rc, remainder = self.children.decode(remainder)
556 self.decoded.append(rc)
557 return self.decoded
Harald Weltec91085e2022-02-10 18:05:45 +0100558
Harald Welteb0608332022-02-10 12:45:37 +0100559 def to_bytes(self) -> bytes:
560 encoded = [self.children.encode(d) for d in self.decoded]
561 return b''.join(encoded)
562
Harald Weltec91085e2022-02-10 18:05:45 +0100563
Harald Welteb0608332022-02-10 12:45:37 +0100564OR_Template = DataObjectChoice('or_template', 'OR-Template',
565 members=[Always_DO, Never_DO, SecCondByte_DO(), SecCondByte_DO(0x9e), CRT_DO()])
566OR_DO = Nested_DO('or', 0xa0, OR_Template)
567AND_Template = DataObjectChoice('and_template', 'AND-Template',
Harald Weltec91085e2022-02-10 18:05:45 +0100568 members=[Always_DO, Never_DO, SecCondByte_DO(), SecCondByte_DO(0x9e), CRT_DO()])
Harald Welteb0608332022-02-10 12:45:37 +0100569AND_DO = Nested_DO('and', 0xa7, AND_Template)
570NOT_Template = DataObjectChoice('not_template', 'NOT-Template',
Harald Weltec91085e2022-02-10 18:05:45 +0100571 members=[Always_DO, Never_DO, SecCondByte_DO(), SecCondByte_DO(0x9e), CRT_DO()])
Harald Welteb0608332022-02-10 12:45:37 +0100572NOT_DO = Nested_DO('not', 0xaf, NOT_Template)
Harald Welte4ae228a2021-05-02 21:29:04 +0200573SC_DO = DataObjectChoice('security_condition', 'Security Condition',
Harald Welteb0608332022-02-10 12:45:37 +0100574 members=[Always_DO, Never_DO, SecCondByte_DO(), SecCondByte_DO(0x9e), CRT_DO(),
575 OR_DO, AND_DO, NOT_DO])
Harald Welte4ae228a2021-05-02 21:29:04 +0200576
Harald Welteb2edd142021-01-08 23:29:35 +0100577# TS 102 221 Section 13.1
578class EF_DIR(LinFixedEF):
Harald Welte865eea62023-01-27 19:26:12 +0100579 # FIXME: re-encode failure when changing to _test_de_encode
580 _test_decode = [
581 ( '61294f10a0000000871002ffffffff890709000050055553696d31730ea00c80011781025f608203454150',
582 { "application_template": [ { "application_id": h2b("a0000000871002ffffffff8907090000") },
583 { "application_label": "USim1" },
584 { "discretionary_template": h2b("a00c80011781025f608203454150") } ] }
585 ),
586 ]
Harald Welte181c7c52022-02-10 14:18:32 +0100587 class ApplicationLabel(BER_TLV_IE, tag=0x50):
588 # TODO: UCS-2 coding option as per Annex A of TS 102 221
589 _construct = GreedyString('ascii')
590
591 # see https://github.com/PyCQA/pylint/issues/5794
592 #pylint: disable=undefined-variable
593 class ApplicationTemplate(BER_TLV_IE, tag=0x61,
594 nested=[iso7816_4.ApplicationId, ApplicationLabel, iso7816_4.FileReference,
595 iso7816_4.CommandApdu, iso7816_4.DiscretionaryData,
596 iso7816_4.DiscretionaryTemplate, iso7816_4.URL,
597 iso7816_4.ApplicationRelatedDOSet]):
598 pass
599
Harald Welteb2edd142021-01-08 23:29:35 +0100600 def __init__(self, fid='2f00', sfid=0x1e, name='EF.DIR', desc='Application Directory'):
Harald Welte99e4cc02022-07-21 15:25:47 +0200601 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=(5, 54))
Harald Welte181c7c52022-02-10 14:18:32 +0100602 self._tlv = EF_DIR.ApplicationTemplate
Harald Welteb2edd142021-01-08 23:29:35 +0100603
604# TS 102 221 Section 13.2
605class EF_ICCID(TransparentEF):
Harald Welte865eea62023-01-27 19:26:12 +0100606 _test_de_encode = [
607 ( '988812010000400310f0', { "iccid": "8988211000000430010" } ),
608 ]
Harald Welteb2edd142021-01-08 23:29:35 +0100609 def __init__(self, fid='2fe2', sfid=0x02, name='EF.ICCID', desc='ICC Identification'):
Harald Welte13edf302022-07-21 15:19:23 +0200610 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=(10, 10))
Harald Welteb2edd142021-01-08 23:29:35 +0100611
612 def _decode_hex(self, raw_hex):
613 return {'iccid': dec_iccid(raw_hex)}
614
615 def _encode_hex(self, abstract):
616 return enc_iccid(abstract['iccid'])
617
618# TS 102 221 Section 13.3
619class EF_PL(TransRecEF):
Harald Welte865eea62023-01-27 19:26:12 +0100620 _test_de_encode = [
621 ( '6465', "de" ),
622 ( '656e', "en" ),
623 ( 'ffff', None ),
624 ]
Harald Welteb2edd142021-01-08 23:29:35 +0100625 def __init__(self, fid='2f05', sfid=0x05, name='EF.PL', desc='Preferred Languages'):
Harald Weltec91085e2022-02-10 18:05:45 +0100626 super().__init__(fid, sfid=sfid, name=name,
Harald Welte13edf302022-07-21 15:19:23 +0200627 desc=desc, rec_len=2, size=(2, None))
Harald Weltec91085e2022-02-10 18:05:45 +0100628
Harald Weltef6b37af2023-01-24 15:42:26 +0100629 def _decode_record_bin(self, bin_data, **kwargs):
Harald Welte0c840f02022-01-21 15:42:22 +0100630 if bin_data == b'\xff\xff':
631 return None
632 else:
633 return bin_data.decode('ascii')
Harald Weltec91085e2022-02-10 18:05:45 +0100634
Harald Weltef6b37af2023-01-24 15:42:26 +0100635 def _encode_record_bin(self, in_json, **kwargs):
Harald Welte0c840f02022-01-21 15:42:22 +0100636 if in_json is None:
637 return b'\xff\xff'
638 else:
639 return in_json.encode('ascii')
640
Harald Welteb2edd142021-01-08 23:29:35 +0100641
642# TS 102 221 Section 13.4
643class EF_ARR(LinFixedEF):
Harald Welte865eea62023-01-27 19:26:12 +0100644 _test_de_encode = [
645 ( '800101a40683010a950108800106900080016097008401d4a40683010a950108',
646 [ [ { "access_mode": [ "read_search_compare" ] },
647 { "control_reference_template": "ADM1" } ],
648 [ { "access_mode": [ "write_append", "update_erase" ] },
649 { "always": None } ],
650 [ { "access_mode": [ "delete_file", "terminate_ef" ] },
651 { "never": None } ],
652 [ { "command_header": { "INS": 212 } },
653 { "control_reference_template": "ADM1" } ]
654 ] ),
655 ( '80010190008001029700800118a40683010a9501088401d4a40683010a950108',
656 [ [ { "access_mode": [ "read_search_compare" ] },
657 { "always": None } ],
658 [ { "access_mode": [ "update_erase" ] },
659 { "never": None } ],
660 [ { "access_mode": [ "activate_file_or_record", "deactivate_file_or_record" ] },
661 { "control_reference_template": "ADM1" } ],
662 [ { "command_header": { "INS": 212 } },
663 { "control_reference_template": "ADM1" } ]
664 ] ),
665 ]
Harald Welteb2edd142021-01-08 23:29:35 +0100666 def __init__(self, fid='2f06', sfid=0x06, name='EF.ARR', desc='Access Rule Reference'):
667 super().__init__(fid, sfid=sfid, name=name, desc=desc)
Harald Welte4ae228a2021-05-02 21:29:04 +0200668 # add those commands to the general commands of a TransparentEF
669 self.shell_commands += [self.AddlShellCommands()]
670
671 @staticmethod
Harald Weltec91085e2022-02-10 18:05:45 +0100672 def flatten(inp: list):
Harald Welte4ae228a2021-05-02 21:29:04 +0200673 """Flatten the somewhat deep/complex/nested data returned from decoder."""
674 def sc_abbreviate(sc):
675 if 'always' in sc:
676 return 'always'
677 elif 'never' in sc:
678 return 'never'
679 elif 'control_reference_template' in sc:
680 return sc['control_reference_template']
681 else:
682 return sc
683
684 by_mode = {}
685 for t in inp:
686 am = t[0]
687 sc = t[1]
688 sc_abbr = sc_abbreviate(sc)
689 if 'access_mode' in am:
690 for m in am['access_mode']:
691 by_mode[m] = sc_abbr
692 elif 'command_header' in am:
693 ins = am['command_header']['INS']
694 if 'CLA' in am['command_header']:
695 cla = am['command_header']['CLA']
696 else:
697 cla = None
698 cmd = ts_102_22x_cmdset.lookup(ins, cla)
699 if cmd:
Harald Weltec91085e2022-02-10 18:05:45 +0100700 name = cmd.name.lower().replace(' ', '_')
Harald Welte4ae228a2021-05-02 21:29:04 +0200701 by_mode[name] = sc_abbr
702 else:
703 raise ValueError
704 else:
705 raise ValueError
706 return by_mode
707
Harald Weltef6b37af2023-01-24 15:42:26 +0100708 def _decode_record_bin(self, raw_bin_data, **kwargs):
Harald Welte4ae228a2021-05-02 21:29:04 +0200709 # we can only guess if we should decode for EF or DF here :(
Harald Weltec91085e2022-02-10 18:05:45 +0100710 arr_seq = DataObjectSequence('arr', sequence=[AM_DO_EF, SC_DO])
Harald Welte4ae228a2021-05-02 21:29:04 +0200711 dec = arr_seq.decode_multi(raw_bin_data)
712 # we cannot pass the result through flatten() here, as we don't have a related
713 # 'un-flattening' decoder, and hence would be unable to encode :(
714 return dec[0]
715
Harald Weltef6b37af2023-01-24 15:42:26 +0100716 def _encode_record_bin(self, in_json, **kwargs):
Harald Weltec30bed22022-04-05 14:45:18 +0200717 # we can only guess if we should decode for EF or DF here :(
718 arr_seq = DataObjectSequence('arr', sequence=[AM_DO_EF, SC_DO])
719 return arr_seq.encode_multi(in_json)
720
Harald Welte4ae228a2021-05-02 21:29:04 +0200721 @with_default_category('File-Specific Commands')
722 class AddlShellCommands(CommandSet):
723 def __init__(self):
724 super().__init__()
725
726 @cmd2.with_argparser(LinFixedEF.ShellCommands.read_rec_dec_parser)
727 def do_read_arr_record(self, opts):
728 """Read one EF.ARR record in flattened, human-friendly form."""
Harald Weltea6c0f882022-07-17 14:23:17 +0200729 (data, sw) = self._cmd.lchan.read_record_dec(opts.record_nr)
730 data = self._cmd.lchan.selected_file.flatten(data)
Harald Welte4ae228a2021-05-02 21:29:04 +0200731 self._cmd.poutput_json(data, opts.oneline)
732
733 @cmd2.with_argparser(LinFixedEF.ShellCommands.read_recs_dec_parser)
734 def do_read_arr_records(self, opts):
735 """Read + decode all EF.ARR records in flattened, human-friendly form."""
Harald Weltea6c0f882022-07-17 14:23:17 +0200736 num_of_rec = self._cmd.lchan.selected_file_num_of_rec()
Harald Welte4ae228a2021-05-02 21:29:04 +0200737 # collect all results in list so they are rendered as JSON list when printing
738 data_list = []
739 for recnr in range(1, 1 + num_of_rec):
Harald Weltea6c0f882022-07-17 14:23:17 +0200740 (data, sw) = self._cmd.lchan.read_record_dec(recnr)
741 data = self._cmd.lchan.selected_file.flatten(data)
Harald Welte4ae228a2021-05-02 21:29:04 +0200742 data_list.append(data)
743 self._cmd.poutput_json(data_list, opts.oneline)
744
Harald Welteb2edd142021-01-08 23:29:35 +0100745
746# TS 102 221 Section 13.6
747class EF_UMPC(TransparentEF):
Harald Welte865eea62023-01-27 19:26:12 +0100748 _test_de_encode = [
749 ( '3cff02', { "max_current_mA": 60, "t_op_s": 255,
750 "addl_info": { "req_inc_idle_current": False, "support_uicc_suspend": True } } ),
751 ]
Harald Welteb2edd142021-01-08 23:29:35 +0100752 def __init__(self, fid='2f08', sfid=0x08, name='EF.UMPC', desc='UICC Maximum Power Consumption'):
Harald Welte13edf302022-07-21 15:19:23 +0200753 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=(5, 5))
Harald Weltec91085e2022-02-10 18:05:45 +0100754 addl_info = FlagsEnum(Byte, req_inc_idle_current=1,
755 support_uicc_suspend=2)
756 self._construct = Struct(
757 'max_current_mA'/Int8ub, 't_op_s'/Int8ub, 'addl_info'/addl_info)
758
Harald Welteb2edd142021-01-08 23:29:35 +0100759
Harald Welteb2edd142021-01-08 23:29:35 +0100760class CardProfileUICC(CardProfile):
Philipp Maiera028c7d2021-11-08 16:12:03 +0100761
762 ORDER = 1
763
Harald Weltec91085e2022-02-10 18:05:45 +0100764 def __init__(self, name='UICC'):
Harald Welteb2edd142021-01-08 23:29:35 +0100765 files = [
766 EF_DIR(),
767 EF_ICCID(),
768 EF_PL(),
769 EF_ARR(),
770 # FIXME: DF.CD
771 EF_UMPC(),
772 ]
Harald Welte323a3502023-07-11 17:26:39 +0200773 addons = [
774 AddonSIM,
775 AddonGSMR,
776 AddonRUIM,
777 ]
Harald Welteb2edd142021-01-08 23:29:35 +0100778 sw = {
Harald Weltec91085e2022-02-10 18:05:45 +0100779 'Normal': {
780 '9000': 'Normal ending of the command',
781 '91xx': 'Normal ending of the command, with extra information from the proactive UICC containing a command for the terminal',
782 '92xx': 'Normal ending of the command, with extra information concerning an ongoing data transfer session',
Harald Welteb2edd142021-01-08 23:29:35 +0100783 },
Harald Weltec91085e2022-02-10 18:05:45 +0100784 'Postponed processing': {
785 '9300': 'SIM Application Toolkit is busy. Command cannot be executed at present, further normal commands are allowed',
Harald Welteb2edd142021-01-08 23:29:35 +0100786 },
Harald Weltec91085e2022-02-10 18:05:45 +0100787 'Warnings': {
788 '6200': 'No information given, state of non-volatile memory unchanged',
789 '6281': 'Part of returned data may be corrupted',
790 '6282': 'End of file/record reached before reading Le bytes or unsuccessful search',
791 '6283': 'Selected file invalidated',
792 '6284': 'Selected file in termination state',
793 '62f1': 'More data available',
794 '62f2': 'More data available and proactive command pending',
795 '62f3': 'Response data available',
796 '63f1': 'More data expected',
797 '63f2': 'More data expected and proactive command pending',
798 '63cx': 'Command successful but after using an internal update retry routine X times',
Harald Welteb2edd142021-01-08 23:29:35 +0100799 },
Harald Weltec91085e2022-02-10 18:05:45 +0100800 'Execution errors': {
801 '6400': 'No information given, state of non-volatile memory unchanged',
802 '6500': 'No information given, state of non-volatile memory changed',
803 '6581': 'Memory problem',
Harald Welteb2edd142021-01-08 23:29:35 +0100804 },
Harald Weltec91085e2022-02-10 18:05:45 +0100805 'Checking errors': {
806 '6700': 'Wrong length',
807 '67xx': 'The interpretation of this status word is command dependent',
808 '6b00': 'Wrong parameter(s) P1-P2',
809 '6d00': 'Instruction code not supported or invalid',
810 '6e00': 'Class not supported',
811 '6f00': 'Technical problem, no precise diagnosis',
812 '6fxx': 'The interpretation of this status word is command dependent',
Harald Welteb2edd142021-01-08 23:29:35 +0100813 },
Harald Weltec91085e2022-02-10 18:05:45 +0100814 'Functions in CLA not supported': {
815 '6800': 'No information given',
816 '6881': 'Logical channel not supported',
817 '6882': 'Secure messaging not supported',
Harald Welteb2edd142021-01-08 23:29:35 +0100818 },
Harald Weltec91085e2022-02-10 18:05:45 +0100819 'Command not allowed': {
820 '6900': 'No information given',
821 '6981': 'Command incompatible with file structure',
822 '6982': 'Security status not satisfied',
823 '6983': 'Authentication/PIN method blocked',
824 '6984': 'Referenced data invalidated',
825 '6985': 'Conditions of use not satisfied',
826 '6986': 'Command not allowed (no EF selected)',
827 '6989': 'Command not allowed - secure channel - security not satisfied',
Harald Welteb2edd142021-01-08 23:29:35 +0100828 },
Harald Weltec91085e2022-02-10 18:05:45 +0100829 'Wrong parameters': {
830 '6a80': 'Incorrect parameters in the data field',
831 '6a81': 'Function not supported',
832 '6a82': 'File not found',
833 '6a83': 'Record not found',
834 '6a84': 'Not enough memory space',
835 '6a86': 'Incorrect parameters P1 to P2',
836 '6a87': 'Lc inconsistent with P1 to P2',
837 '6a88': 'Referenced data not found',
Harald Welteb2edd142021-01-08 23:29:35 +0100838 },
Harald Weltec91085e2022-02-10 18:05:45 +0100839 'Application errors': {
840 '9850': 'INCREASE cannot be performed, max value reached',
841 '9862': 'Authentication error, application specific',
842 '9863': 'Security session or association expired',
843 '9864': 'Minimum UICC suspension time is too long',
Harald Welteb2edd142021-01-08 23:29:35 +0100844 },
Harald Weltec91085e2022-02-10 18:05:45 +0100845 }
Harald Welteb2edd142021-01-08 23:29:35 +0100846
Harald Weltec91085e2022-02-10 18:05:45 +0100847 super().__init__(name, desc='ETSI TS 102 221', cla="00",
Harald Welte659781c2023-06-06 17:00:51 +0200848 sel_ctrl="0004", files_in_mf=files, sw=sw,
Harald Welte323a3502023-07-11 17:26:39 +0200849 shell_cmdsets = [self.AddlShellCommands()], addons = addons)
Philipp Maier5af7bdf2021-11-04 12:48:41 +0100850
Philipp Maier5998a3a2021-11-16 15:16:39 +0100851 @staticmethod
Harald Weltec91085e2022-02-10 18:05:45 +0100852 def decode_select_response(resp_hex: str) -> object:
Philipp Maier5998a3a2021-11-16 15:16:39 +0100853 """ETSI TS 102 221 Section 11.1.1.3"""
Harald Weltec8c33272022-02-11 14:45:23 +0100854 t = FcpTemplate()
855 t.from_tlv(h2b(resp_hex))
856 d = t.to_dict()
857 return flatten_dict_lists(d['fcp_template'])
Philipp Maiera028c7d2021-11-08 16:12:03 +0100858
859 @staticmethod
Harald Weltec91085e2022-02-10 18:05:45 +0100860 def match_with_card(scc: SimCardCommands) -> bool:
Philipp Maiera028c7d2021-11-08 16:12:03 +0100861 return match_uicc(scc)
862
Harald Welte659781c2023-06-06 17:00:51 +0200863 @with_default_category('TS 102 221 Specific Commands')
864 class AddlShellCommands(CommandSet):
865 suspend_uicc_parser = argparse.ArgumentParser()
866 suspend_uicc_parser.add_argument('--min-duration-secs', type=int, default=60,
867 help='Proposed minimum duration of suspension')
868 suspend_uicc_parser.add_argument('--max-duration-secs', type=int, default=24*60*60,
869 help='Proposed maximum duration of suspension')
870
871 # not ISO7816-4 but TS 102 221
872 @cmd2.with_argparser(suspend_uicc_parser)
873 def do_suspend_uicc(self, opts):
Harald Welteb0e0dce2023-06-06 17:21:13 +0200874 """Perform the SUSPEND UICC command. Only supported on some UICC (check EF.UMPC)."""
Harald Welte659781c2023-06-06 17:00:51 +0200875 (duration, token, sw) = self._cmd.card._scc.suspend_uicc(min_len_secs=opts.min_duration_secs,
876 max_len_secs=opts.max_duration_secs)
877 self._cmd.poutput(
878 'Negotiated Duration: %u secs, Token: %s, SW: %s' % (duration, token, sw))
879
Harald Welteb0e0dce2023-06-06 17:21:13 +0200880 resume_uicc_parser = argparse.ArgumentParser()
881 resume_uicc_parser.add_argument('token', type=str, help='Token provided during SUSPEND')
882
883 @cmd2.with_argparser(resume_uicc_parser)
884 def do_resume_uicc(self, opts):
885 """Perform the REUSME UICC operation. Only supported on some UICC. Also: A power-cycle
886 of the card is required between SUSPEND and RESUME, and only very few non-RESUME
887 commands are permitted between SUSPEND and RESUME. See TS 102 221 Section 11.1.22."""
888 self._cmd.card._scc.resume_uicc(opts.token)