Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 1 | # 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 | |
| 6 | This program is free software: you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation, either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | """ |
| 19 | |
Harald Welte | 8f892fb | 2021-06-05 10:12:43 +0200 | [diff] [blame] | 20 | from construct import * |
Harald Welte | 747a978 | 2022-02-13 17:52:28 +0100 | [diff] [blame] | 21 | from construct import Optional as COptional |
Harald Welte | 8f892fb | 2021-06-05 10:12:43 +0200 | [diff] [blame] | 22 | from pySim.construct import * |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 23 | from pySim.utils import * |
| 24 | from pySim.filesystem import * |
Harald Welte | 181c7c5 | 2022-02-10 14:18:32 +0100 | [diff] [blame] | 25 | from pySim.tlv import * |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 26 | from bidict import bidict |
Philipp Maier | a028c7d | 2021-11-08 16:12:03 +0100 | [diff] [blame] | 27 | from pySim.profile import CardProfile |
| 28 | from pySim.profile import match_uicc |
| 29 | from pySim.profile import match_sim |
Harald Welte | 181c7c5 | 2022-02-10 14:18:32 +0100 | [diff] [blame] | 30 | import pySim.iso7816_4 as iso7816_4 |
Philipp Maier | a028c7d | 2021-11-08 16:12:03 +0100 | [diff] [blame] | 31 | |
| 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 Welte | 323a350 | 2023-07-11 17:26:39 +0200 | [diff] [blame] | 34 | from pySim.ts_51_011 import DF_GSM, DF_TELECOM, AddonSIM |
| 35 | from pySim.gsm_r import AddonGSMR |
| 36 | from pySim.cdma_ruim import AddonRUIM |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 37 | |
| 38 | ts_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 Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 81 | ]) |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 82 | |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 83 | # ETSI TS 102 221 11.1.1.4.2 |
| 84 | class FileSize(BER_TLV_IE, tag=0x80): |
Philipp Maier | 541a915 | 2022-06-01 18:21:17 +0200 | [diff] [blame] | 85 | _construct = GreedyInteger(minlen=2) |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 86 | |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 87 | # ETSI TS 102 221 11.1.1.4.2 |
| 88 | class TotalFileSize(BER_TLV_IE, tag=0x81): |
Philipp Maier | 541a915 | 2022-06-01 18:21:17 +0200 | [diff] [blame] | 89 | _construct = GreedyInteger(minlen=2) |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 90 | |
| 91 | # ETSI TS 102 221 11.1.1.4.3 |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 92 | class FileDescriptor(BER_TLV_IE, tag=0x82): |
Harald Welte | 747a978 | 2022-02-13 17:52:28 +0100 | [diff] [blame] | 93 | 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 Welte | 4ebeebf | 2022-02-25 09:47:51 +0100 | [diff] [blame] | 108 | 'record_len'/COptional(Int16ub), 'num_of_rec'/COptional(Int8ub)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 109 | |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 110 | # ETSI TS 102 221 11.1.1.4.4 |
| 111 | class FileIdentifier(BER_TLV_IE, tag=0x83): |
Harald Welte | 3c9b784 | 2021-10-19 21:44:24 +0200 | [diff] [blame] | 112 | _construct = HexAdapter(GreedyBytes) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 113 | |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 114 | # ETSI TS 102 221 11.1.1.4.5 |
| 115 | class DfName(BER_TLV_IE, tag=0x84): |
Harald Welte | 3c9b784 | 2021-10-19 21:44:24 +0200 | [diff] [blame] | 116 | _construct = HexAdapter(GreedyBytes) |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 117 | |
| 118 | # ETSI TS 102 221 11.1.1.4.6.1 |
| 119 | class UiccCharacteristics(BER_TLV_IE, tag=0x80): |
| 120 | _construct = GreedyBytes |
| 121 | |
| 122 | # ETSI TS 102 221 11.1.1.4.6.2 |
| 123 | class 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 |
| 129 | class MinApplicationClockFrequency(BER_TLV_IE, tag=0x82): |
| 130 | _construct = Int8ub |
| 131 | |
| 132 | # ETSI TS 102 221 11.1.1.4.6.4 |
| 133 | class AvailableMemory(BER_TLV_IE, tag=0x83): |
| 134 | _construct = GreedyInteger() |
| 135 | |
| 136 | # ETSI TS 102 221 11.1.1.4.6.5 |
| 137 | class 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 |
| 141 | class ReservedFileSize(BER_TLV_IE, tag=0x85): |
| 142 | _construct = GreedyInteger() |
| 143 | |
| 144 | # ETSI TS 102 221 11.1.1.4.6.7 |
| 145 | class MaximumFileSize(BER_TLV_IE, tag=0x86): |
| 146 | _construct = GreedyInteger() |
| 147 | |
| 148 | # ETSI TS 102 221 11.1.1.4.6.8 |
| 149 | class 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 |
| 153 | class 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 |
| 159 | class Platform2PlatformCatSecuredApdu(BER_TLV_IE, tag=0x89): |
| 160 | _construct = GreedyBytes |
| 161 | |
Harald Welte | 3c9b784 | 2021-10-19 21:44:24 +0200 | [diff] [blame] | 162 | # sysmoISIM-SJA2 specific |
| 163 | class 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 Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 167 | # ETSI TS 102 221 11.1.1.4.6.0 |
| 168 | class ProprietaryInformation(BER_TLV_IE, tag=0xA5, |
| 169 | nested=[UiccCharacteristics, ApplicationPowerConsumption, |
| 170 | MinApplicationClockFrequency, AvailableMemory, |
| 171 | FileDetails, ReservedFileSize, MaximumFileSize, |
Harald Welte | 3c9b784 | 2021-10-19 21:44:24 +0200 | [diff] [blame] | 172 | SupportedFilesystemCommands, SpecificUiccEnvironmentConditions, |
| 173 | ToolkitAccessConditions]): |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 174 | pass |
| 175 | |
| 176 | # ETSI TS 102 221 11.1.1.4.7.1 |
| 177 | class SecurityAttribCompact(BER_TLV_IE, tag=0x8c): |
| 178 | _construct = GreedyBytes |
| 179 | |
| 180 | # ETSI TS 102 221 11.1.1.4.7.2 |
| 181 | class SecurityAttribExpanded(BER_TLV_IE, tag=0xab): |
| 182 | _construct = GreedyBytes |
| 183 | |
| 184 | # ETSI TS 102 221 11.1.1.4.7.3 |
| 185 | class SecurityAttribReferenced(BER_TLV_IE, tag=0x8b): |
| 186 | # TODO: longer format with SEID |
Harald Welte | 3c9b784 | 2021-10-19 21:44:24 +0200 | [diff] [blame] | 187 | _construct = Struct('ef_arr_file_id'/HexAdapter(Bytes(2)), 'ef_arr_record_nr'/Int8ub) |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 188 | |
| 189 | # ETSI TS 102 221 11.1.1.4.8 |
| 190 | class ShortFileIdentifier(BER_TLV_IE, tag=0x88): |
Harald Welte | 5e9bd93 | 2022-02-25 09:35:28 +0100 | [diff] [blame] | 191 | # 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 Maier | 373b23c | 2022-06-01 18:16:02 +0200 | [diff] [blame] | 195 | return int.from_bytes(obj, 'big') >> 3 |
Harald Welte | 5e9bd93 | 2022-02-25 09:35:28 +0100 | [diff] [blame] | 196 | def _encode(self, obj, context, path): |
Philipp Maier | 373b23c | 2022-06-01 18:16:02 +0200 | [diff] [blame] | 197 | val = int(obj) << 3 |
| 198 | return val.to_bytes(1, 'big') |
| 199 | _construct = COptional(Shift3RAdapter(Bytes(1))) |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 200 | |
| 201 | # ETSI TS 102 221 11.1.1.4.9 |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 202 | class 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 Welte | 3c9b784 | 2021-10-19 21:44:24 +0200 | [diff] [blame] | 221 | 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 Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 238 | |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 239 | # ETSI TS 102 221 11.1.1.4.9 |
| 240 | class PS_DO(BER_TLV_IE, tag=0x90): |
| 241 | _construct = GreedyBytes |
| 242 | class UsageQualifier_DO(BER_TLV_IE, tag=0x95): |
| 243 | _construct = GreedyBytes |
| 244 | class KeyReference(BER_TLV_IE, tag=0x83): |
| 245 | _construct = Byte |
| 246 | class PinStatusTemplate_DO(BER_TLV_IE, tag=0xC6, nested=[PS_DO, UsageQualifier_DO, KeyReference]): |
| 247 | pass |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 248 | |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 249 | class 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 Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 255 | |
| 256 | |
| 257 | def 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 Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 265 | |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 266 | def 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 Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 274 | # ETSI TS 102 221 Section 9.2.7 + ISO7816-4 9.3.3/9.3.4 |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 275 | class _AM_DO_DF(DataObject): |
| 276 | def __init__(self): |
| 277 | super().__init__('access_mode', 'Access Mode', tag=0x80) |
| 278 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 279 | def from_bytes(self, do: bytes): |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 280 | 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 | |
| 321 | class _AM_DO_EF(DataObject): |
| 322 | """ISO7816-4 9.3.2 Table 18 + 9.3.3.1 Table 31""" |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 323 | |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 324 | def __init__(self): |
| 325 | super().__init__('access_mode', 'Access Mode', tag=0x80) |
| 326 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 327 | def from_bytes(self, do: bytes): |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 328 | 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 Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 368 | |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 369 | class _AM_DO_CHDR(DataObject): |
| 370 | """Command Header Access Mode DO according to ISO 7816-4 Table 32.""" |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 371 | |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 372 | def __init__(self, tag): |
| 373 | super().__init__('command_header', 'Command Header Description', tag=tag) |
| 374 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 375 | def from_bytes(self, do: bytes): |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 376 | 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 Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 417 | |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 418 | AM_DO_CHDR = DataObjectChoice('am_do_chdr', members=[ |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 419 | _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 Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 423 | |
| 424 | AM_DO_DF = AM_DO_CHDR | _AM_DO_DF() |
| 425 | AM_DO_EF = AM_DO_CHDR | _AM_DO_EF() |
| 426 | |
| 427 | |
| 428 | # TS 102 221 Section 9.5.1 / Table 9.3 |
| 429 | pin_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 Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 458 | }) |
| 459 | |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 460 | |
| 461 | class CRT_DO(DataObject): |
| 462 | """Control Reference Template as per TS 102 221 9.5.1""" |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 463 | |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 464 | def __init__(self): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 465 | super().__init__('control_reference_template', |
| 466 | 'Control Reference Template', tag=0xA4) |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 467 | |
| 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 Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 475 | raise ValueError( |
| 476 | 'Unsupported Usage Qualifier Tag or Len in CRT DO %s', do) |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 477 | 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 |
| 486 | class SecCondByte_DO(DataObject): |
| 487 | def __init__(self, tag=0x9d): |
| 488 | super().__init__('security_condition_byte', tag=tag) |
| 489 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 490 | def from_bytes(self, binary: bytes): |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 491 | 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 Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 509 | rd = {'mode': cond} |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 510 | 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 Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 539 | |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 540 | Always_DO = TL0_DataObject('always', 'Always', 0x90) |
| 541 | Never_DO = TL0_DataObject('never', 'Never', 0x97) |
Harald Welte | b060833 | 2022-02-10 12:45:37 +0100 | [diff] [blame] | 542 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 543 | |
Harald Welte | b060833 | 2022-02-10 12:45:37 +0100 | [diff] [blame] | 544 | class Nested_DO(DataObject): |
| 545 | """A DO that nests another DO/Choice/Sequence""" |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 546 | |
Harald Welte | b060833 | 2022-02-10 12:45:37 +0100 | [diff] [blame] | 547 | def __init__(self, name, tag, choice): |
| 548 | super().__init__(name, tag=tag) |
| 549 | self.children = choice |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 550 | |
| 551 | def from_bytes(self, binary: bytes) -> list: |
Harald Welte | b060833 | 2022-02-10 12:45:37 +0100 | [diff] [blame] | 552 | 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 Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 558 | |
Harald Welte | b060833 | 2022-02-10 12:45:37 +0100 | [diff] [blame] | 559 | def to_bytes(self) -> bytes: |
| 560 | encoded = [self.children.encode(d) for d in self.decoded] |
| 561 | return b''.join(encoded) |
| 562 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 563 | |
Harald Welte | b060833 | 2022-02-10 12:45:37 +0100 | [diff] [blame] | 564 | OR_Template = DataObjectChoice('or_template', 'OR-Template', |
| 565 | members=[Always_DO, Never_DO, SecCondByte_DO(), SecCondByte_DO(0x9e), CRT_DO()]) |
| 566 | OR_DO = Nested_DO('or', 0xa0, OR_Template) |
| 567 | AND_Template = DataObjectChoice('and_template', 'AND-Template', |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 568 | members=[Always_DO, Never_DO, SecCondByte_DO(), SecCondByte_DO(0x9e), CRT_DO()]) |
Harald Welte | b060833 | 2022-02-10 12:45:37 +0100 | [diff] [blame] | 569 | AND_DO = Nested_DO('and', 0xa7, AND_Template) |
| 570 | NOT_Template = DataObjectChoice('not_template', 'NOT-Template', |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 571 | members=[Always_DO, Never_DO, SecCondByte_DO(), SecCondByte_DO(0x9e), CRT_DO()]) |
Harald Welte | b060833 | 2022-02-10 12:45:37 +0100 | [diff] [blame] | 572 | NOT_DO = Nested_DO('not', 0xaf, NOT_Template) |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 573 | SC_DO = DataObjectChoice('security_condition', 'Security Condition', |
Harald Welte | b060833 | 2022-02-10 12:45:37 +0100 | [diff] [blame] | 574 | members=[Always_DO, Never_DO, SecCondByte_DO(), SecCondByte_DO(0x9e), CRT_DO(), |
| 575 | OR_DO, AND_DO, NOT_DO]) |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 576 | |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 577 | # TS 102 221 Section 13.1 |
| 578 | class EF_DIR(LinFixedEF): |
Harald Welte | 865eea6 | 2023-01-27 19:26:12 +0100 | [diff] [blame] | 579 | # 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 Welte | 181c7c5 | 2022-02-10 14:18:32 +0100 | [diff] [blame] | 587 | 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 Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 600 | def __init__(self, fid='2f00', sfid=0x1e, name='EF.DIR', desc='Application Directory'): |
Harald Welte | 99e4cc0 | 2022-07-21 15:25:47 +0200 | [diff] [blame] | 601 | super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=(5, 54)) |
Harald Welte | 181c7c5 | 2022-02-10 14:18:32 +0100 | [diff] [blame] | 602 | self._tlv = EF_DIR.ApplicationTemplate |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 603 | |
| 604 | # TS 102 221 Section 13.2 |
| 605 | class EF_ICCID(TransparentEF): |
Harald Welte | 865eea6 | 2023-01-27 19:26:12 +0100 | [diff] [blame] | 606 | _test_de_encode = [ |
| 607 | ( '988812010000400310f0', { "iccid": "8988211000000430010" } ), |
| 608 | ] |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 609 | def __init__(self, fid='2fe2', sfid=0x02, name='EF.ICCID', desc='ICC Identification'): |
Harald Welte | 13edf30 | 2022-07-21 15:19:23 +0200 | [diff] [blame] | 610 | super().__init__(fid, sfid=sfid, name=name, desc=desc, size=(10, 10)) |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 611 | |
| 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 |
| 619 | class EF_PL(TransRecEF): |
Harald Welte | 865eea6 | 2023-01-27 19:26:12 +0100 | [diff] [blame] | 620 | _test_de_encode = [ |
| 621 | ( '6465', "de" ), |
| 622 | ( '656e', "en" ), |
| 623 | ( 'ffff', None ), |
| 624 | ] |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 625 | def __init__(self, fid='2f05', sfid=0x05, name='EF.PL', desc='Preferred Languages'): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 626 | super().__init__(fid, sfid=sfid, name=name, |
Harald Welte | 13edf30 | 2022-07-21 15:19:23 +0200 | [diff] [blame] | 627 | desc=desc, rec_len=2, size=(2, None)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 628 | |
Harald Welte | f6b37af | 2023-01-24 15:42:26 +0100 | [diff] [blame] | 629 | def _decode_record_bin(self, bin_data, **kwargs): |
Harald Welte | 0c840f0 | 2022-01-21 15:42:22 +0100 | [diff] [blame] | 630 | if bin_data == b'\xff\xff': |
| 631 | return None |
| 632 | else: |
| 633 | return bin_data.decode('ascii') |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 634 | |
Harald Welte | f6b37af | 2023-01-24 15:42:26 +0100 | [diff] [blame] | 635 | def _encode_record_bin(self, in_json, **kwargs): |
Harald Welte | 0c840f0 | 2022-01-21 15:42:22 +0100 | [diff] [blame] | 636 | if in_json is None: |
| 637 | return b'\xff\xff' |
| 638 | else: |
| 639 | return in_json.encode('ascii') |
| 640 | |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 641 | |
| 642 | # TS 102 221 Section 13.4 |
| 643 | class EF_ARR(LinFixedEF): |
Harald Welte | 865eea6 | 2023-01-27 19:26:12 +0100 | [diff] [blame] | 644 | _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 Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 666 | 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 Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 668 | # add those commands to the general commands of a TransparentEF |
| 669 | self.shell_commands += [self.AddlShellCommands()] |
| 670 | |
| 671 | @staticmethod |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 672 | def flatten(inp: list): |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 673 | """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 Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 700 | name = cmd.name.lower().replace(' ', '_') |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 701 | by_mode[name] = sc_abbr |
| 702 | else: |
| 703 | raise ValueError |
| 704 | else: |
| 705 | raise ValueError |
| 706 | return by_mode |
| 707 | |
Harald Welte | f6b37af | 2023-01-24 15:42:26 +0100 | [diff] [blame] | 708 | def _decode_record_bin(self, raw_bin_data, **kwargs): |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 709 | # we can only guess if we should decode for EF or DF here :( |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 710 | arr_seq = DataObjectSequence('arr', sequence=[AM_DO_EF, SC_DO]) |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 711 | 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 Welte | f6b37af | 2023-01-24 15:42:26 +0100 | [diff] [blame] | 716 | def _encode_record_bin(self, in_json, **kwargs): |
Harald Welte | c30bed2 | 2022-04-05 14:45:18 +0200 | [diff] [blame] | 717 | # 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 Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 721 | @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 Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 729 | (data, sw) = self._cmd.lchan.read_record_dec(opts.record_nr) |
| 730 | data = self._cmd.lchan.selected_file.flatten(data) |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 731 | 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 Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 736 | num_of_rec = self._cmd.lchan.selected_file_num_of_rec() |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 737 | # 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 Welte | a6c0f88 | 2022-07-17 14:23:17 +0200 | [diff] [blame] | 740 | (data, sw) = self._cmd.lchan.read_record_dec(recnr) |
| 741 | data = self._cmd.lchan.selected_file.flatten(data) |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 742 | data_list.append(data) |
| 743 | self._cmd.poutput_json(data_list, opts.oneline) |
| 744 | |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 745 | |
| 746 | # TS 102 221 Section 13.6 |
| 747 | class EF_UMPC(TransparentEF): |
Harald Welte | 865eea6 | 2023-01-27 19:26:12 +0100 | [diff] [blame] | 748 | _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 Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 752 | def __init__(self, fid='2f08', sfid=0x08, name='EF.UMPC', desc='UICC Maximum Power Consumption'): |
Harald Welte | 13edf30 | 2022-07-21 15:19:23 +0200 | [diff] [blame] | 753 | super().__init__(fid, sfid=sfid, name=name, desc=desc, size=(5, 5)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 754 | 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 Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 759 | |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 760 | class CardProfileUICC(CardProfile): |
Philipp Maier | a028c7d | 2021-11-08 16:12:03 +0100 | [diff] [blame] | 761 | |
| 762 | ORDER = 1 |
| 763 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 764 | def __init__(self, name='UICC'): |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 765 | files = [ |
| 766 | EF_DIR(), |
| 767 | EF_ICCID(), |
| 768 | EF_PL(), |
| 769 | EF_ARR(), |
| 770 | # FIXME: DF.CD |
| 771 | EF_UMPC(), |
| 772 | ] |
Harald Welte | 323a350 | 2023-07-11 17:26:39 +0200 | [diff] [blame] | 773 | addons = [ |
| 774 | AddonSIM, |
| 775 | AddonGSMR, |
| 776 | AddonRUIM, |
| 777 | ] |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 778 | sw = { |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 779 | '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 Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 783 | }, |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 784 | 'Postponed processing': { |
| 785 | '9300': 'SIM Application Toolkit is busy. Command cannot be executed at present, further normal commands are allowed', |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 786 | }, |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 787 | '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 Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 799 | }, |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 800 | '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 Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 804 | }, |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 805 | '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 Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 813 | }, |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 814 | 'Functions in CLA not supported': { |
| 815 | '6800': 'No information given', |
| 816 | '6881': 'Logical channel not supported', |
| 817 | '6882': 'Secure messaging not supported', |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 818 | }, |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 819 | '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 Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 828 | }, |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 829 | '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 Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 838 | }, |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 839 | '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 Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 844 | }, |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 845 | } |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 846 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 847 | super().__init__(name, desc='ETSI TS 102 221', cla="00", |
Harald Welte | 659781c | 2023-06-06 17:00:51 +0200 | [diff] [blame] | 848 | sel_ctrl="0004", files_in_mf=files, sw=sw, |
Harald Welte | 323a350 | 2023-07-11 17:26:39 +0200 | [diff] [blame] | 849 | shell_cmdsets = [self.AddlShellCommands()], addons = addons) |
Philipp Maier | 5af7bdf | 2021-11-04 12:48:41 +0100 | [diff] [blame] | 850 | |
Philipp Maier | 5998a3a | 2021-11-16 15:16:39 +0100 | [diff] [blame] | 851 | @staticmethod |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 852 | def decode_select_response(resp_hex: str) -> object: |
Philipp Maier | 5998a3a | 2021-11-16 15:16:39 +0100 | [diff] [blame] | 853 | """ETSI TS 102 221 Section 11.1.1.3""" |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 854 | t = FcpTemplate() |
| 855 | t.from_tlv(h2b(resp_hex)) |
| 856 | d = t.to_dict() |
| 857 | return flatten_dict_lists(d['fcp_template']) |
Philipp Maier | a028c7d | 2021-11-08 16:12:03 +0100 | [diff] [blame] | 858 | |
| 859 | @staticmethod |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 860 | def match_with_card(scc: SimCardCommands) -> bool: |
Philipp Maier | a028c7d | 2021-11-08 16:12:03 +0100 | [diff] [blame] | 861 | return match_uicc(scc) |
| 862 | |
Harald Welte | 659781c | 2023-06-06 17:00:51 +0200 | [diff] [blame] | 863 | @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 Welte | b0e0dce | 2023-06-06 17:21:13 +0200 | [diff] [blame] | 874 | """Perform the SUSPEND UICC command. Only supported on some UICC (check EF.UMPC).""" |
Harald Welte | 659781c | 2023-06-06 17:00:51 +0200 | [diff] [blame] | 875 | (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 Welte | b0e0dce | 2023-06-06 17:21:13 +0200 | [diff] [blame] | 880 | 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) |