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 |
| 34 | from pySim.ts_51_011 import DF_GSM, DF_TELECOM |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 35 | |
| 36 | ts_102_22x_cmdset = CardCommandSet('TS 102 22x', [ |
| 37 | # TS 102 221 Section 10.1.2 Table 10.5 "Coding of Instruction Byte" |
| 38 | CardCommand('SELECT', 0xA4, ['0X', '4X', '6X']), |
| 39 | CardCommand('STATUS', 0xF2, ['8X', 'CX', 'EX']), |
| 40 | CardCommand('READ BINARY', 0xB0, ['0X', '4X', '6X']), |
| 41 | CardCommand('UPDATE BINARY', 0xD6, ['0X', '4X', '6X']), |
| 42 | CardCommand('READ RECORD', 0xB2, ['0X', '4X', '6X']), |
| 43 | CardCommand('UPDATE RECORD', 0xDC, ['0X', '4X', '6X']), |
| 44 | CardCommand('SEARCH RECORD', 0xA2, ['0X', '4X', '6X']), |
| 45 | CardCommand('INCREASE', 0x32, ['8X', 'CX', 'EX']), |
| 46 | CardCommand('RETRIEVE DATA', 0xCB, ['8X', 'CX', 'EX']), |
| 47 | CardCommand('SET DATA', 0xDB, ['8X', 'CX', 'EX']), |
| 48 | CardCommand('VERIFY PIN', 0x20, ['0X', '4X', '6X']), |
| 49 | CardCommand('CHANGE PIN', 0x24, ['0X', '4X', '6X']), |
| 50 | CardCommand('DISABLE PIN', 0x26, ['0X', '4X', '6X']), |
| 51 | CardCommand('ENABLE PIN', 0x28, ['0X', '4X', '6X']), |
| 52 | CardCommand('UNBLOCK PIN', 0x2C, ['0X', '4X', '6X']), |
| 53 | CardCommand('DEACTIVATE FILE', 0x04, ['0X', '4X', '6X']), |
| 54 | CardCommand('ACTIVATE FILE', 0x44, ['0X', '4X', '6X']), |
| 55 | CardCommand('AUTHENTICATE', 0x88, ['0X', '4X', '6X']), |
| 56 | CardCommand('AUTHENTICATE', 0x89, ['0X', '4X', '6X']), |
| 57 | CardCommand('GET CHALLENGE', 0x84, ['0X', '4X', '6X']), |
| 58 | CardCommand('TERMINAL CAPABILITY', 0xAA, ['8X', 'CX', 'EX']), |
| 59 | CardCommand('TERMINAL PROFILE', 0x10, ['80']), |
| 60 | CardCommand('ENVELOPE', 0xC2, ['80']), |
| 61 | CardCommand('FETCH', 0x12, ['80']), |
| 62 | CardCommand('TERMINAL RESPONSE', 0x14, ['80']), |
| 63 | CardCommand('MANAGE CHANNEL', 0x70, ['0X', '4X', '6X']), |
| 64 | CardCommand('MANAGE SECURE CHANNEL', 0x73, ['0X', '4X', '6X']), |
| 65 | CardCommand('TRANSACT DATA', 0x75, ['0X', '4X', '6X']), |
| 66 | CardCommand('SUSPEND UICC', 0x76, ['80']), |
| 67 | CardCommand('GET IDENTITY', 0x78, ['8X', 'CX', 'EX']), |
| 68 | CardCommand('EXCHANGE CAPABILITIES', 0x7A, ['80']), |
| 69 | CardCommand('GET RESPONSE', 0xC0, ['0X', '4X', '6X']), |
| 70 | # TS 102 222 Section 6.1 Table 1 "Coding of the commands" |
| 71 | CardCommand('CREATE FILE', 0xE0, ['0X', '4X']), |
| 72 | CardCommand('DELETE FILE', 0xE4, ['0X', '4X']), |
| 73 | CardCommand('DEACTIVATE FILE', 0x04, ['0X', '4X']), |
| 74 | CardCommand('ACTIVATE FILE', 0x44, ['0X', '4X']), |
| 75 | CardCommand('TERMINATE DF', 0xE6, ['0X', '4X']), |
| 76 | CardCommand('TERMINATE EF', 0xE8, ['0X', '4X']), |
| 77 | CardCommand('TERMINATE CARD USAGE', 0xFE, ['0X', '4X']), |
| 78 | CardCommand('RESIZE FILE', 0xD4, ['8X', 'CX']), |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 79 | ]) |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 80 | |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 81 | # ETSI TS 102 221 11.1.1.4.2 |
| 82 | class FileSize(BER_TLV_IE, tag=0x80): |
| 83 | _construct = GreedyInteger() |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 84 | |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 85 | # ETSI TS 102 221 11.1.1.4.2 |
| 86 | class TotalFileSize(BER_TLV_IE, tag=0x81): |
| 87 | _construct = GreedyInteger() |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 88 | |
| 89 | # ETSI TS 102 221 11.1.1.4.3 |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 90 | class FileDescriptor(BER_TLV_IE, tag=0x82): |
Harald Welte | 747a978 | 2022-02-13 17:52:28 +0100 | [diff] [blame] | 91 | class BerTlvAdapter(Adapter): |
| 92 | def _parse(self, obj, context, path): |
| 93 | if obj == 0x39: |
| 94 | return 'ber_tlv' |
| 95 | raise ValidationError |
| 96 | def _build(self, obj, context, path): |
| 97 | if obj == 'ber_tlv': |
| 98 | return 0x39 |
| 99 | raise ValidationError |
| 100 | |
| 101 | FDB = Select(BitStruct(Const(0, Bit), 'shareable'/Flag, 'structure'/BerTlvAdapter(Const(0x39, BitsInteger(6)))), |
| 102 | BitStruct(Const(0, Bit), 'shareable'/Flag, 'file_type'/Enum(BitsInteger(3), working_ef=0, internal_ef=1, df=7), |
| 103 | 'structure'/Enum(BitsInteger(3), no_info_given=0, transparent=1, linear_fixed=2, cyclic=6)) |
| 104 | ) |
| 105 | _construct = Struct('file_descriptor_byte'/FDB, Const(b'\x21'), |
Harald Welte | 4ebeebf | 2022-02-25 09:47:51 +0100 | [diff] [blame] | 106 | 'record_len'/COptional(Int16ub), 'num_of_rec'/COptional(Int8ub)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 107 | |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 108 | # ETSI TS 102 221 11.1.1.4.4 |
| 109 | class FileIdentifier(BER_TLV_IE, tag=0x83): |
Harald Welte | 3c9b784 | 2021-10-19 21:44:24 +0200 | [diff] [blame] | 110 | _construct = HexAdapter(GreedyBytes) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 111 | |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 112 | # ETSI TS 102 221 11.1.1.4.5 |
| 113 | class DfName(BER_TLV_IE, tag=0x84): |
Harald Welte | 3c9b784 | 2021-10-19 21:44:24 +0200 | [diff] [blame] | 114 | _construct = HexAdapter(GreedyBytes) |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 115 | |
| 116 | # ETSI TS 102 221 11.1.1.4.6.1 |
| 117 | class UiccCharacteristics(BER_TLV_IE, tag=0x80): |
| 118 | _construct = GreedyBytes |
| 119 | |
| 120 | # ETSI TS 102 221 11.1.1.4.6.2 |
| 121 | class ApplicationPowerConsumption(BER_TLV_IE, tag=0x81): |
| 122 | _construct = Struct('voltage_class'/Int8ub, |
| 123 | 'power_consumption_ma'/Int8ub, |
| 124 | 'reference_freq_100k'/Int8ub) |
| 125 | |
| 126 | # ETSI TS 102 221 11.1.1.4.6.3 |
| 127 | class MinApplicationClockFrequency(BER_TLV_IE, tag=0x82): |
| 128 | _construct = Int8ub |
| 129 | |
| 130 | # ETSI TS 102 221 11.1.1.4.6.4 |
| 131 | class AvailableMemory(BER_TLV_IE, tag=0x83): |
| 132 | _construct = GreedyInteger() |
| 133 | |
| 134 | # ETSI TS 102 221 11.1.1.4.6.5 |
| 135 | class FileDetails(BER_TLV_IE, tag=0x84): |
| 136 | _construct = FlagsEnum(Byte, der_coding_only=1) |
| 137 | |
| 138 | # ETSI TS 102 221 11.1.1.4.6.6 |
| 139 | class ReservedFileSize(BER_TLV_IE, tag=0x85): |
| 140 | _construct = GreedyInteger() |
| 141 | |
| 142 | # ETSI TS 102 221 11.1.1.4.6.7 |
| 143 | class MaximumFileSize(BER_TLV_IE, tag=0x86): |
| 144 | _construct = GreedyInteger() |
| 145 | |
| 146 | # ETSI TS 102 221 11.1.1.4.6.8 |
| 147 | class SupportedFilesystemCommands(BER_TLV_IE, tag=0x87): |
| 148 | _construct = FlagsEnum(Byte, terminal_capability=1) |
| 149 | |
| 150 | # ETSI TS 102 221 11.1.1.4.6.9 |
| 151 | class SpecificUiccEnvironmentConditions(BER_TLV_IE, tag=0x88): |
| 152 | _construct = BitStruct('rfu'/BitsRFU(4), |
| 153 | 'high_humidity_supported'/Flag, |
| 154 | 'temperature_class'/Enum(BitsInteger(3), standard=0, class_A=1, class_B=2, class_C=3)) |
| 155 | |
| 156 | # ETSI TS 102 221 11.1.1.4.6.10 |
| 157 | class Platform2PlatformCatSecuredApdu(BER_TLV_IE, tag=0x89): |
| 158 | _construct = GreedyBytes |
| 159 | |
Harald Welte | 3c9b784 | 2021-10-19 21:44:24 +0200 | [diff] [blame] | 160 | # sysmoISIM-SJA2 specific |
| 161 | class ToolkitAccessConditions(BER_TLV_IE, tag=0xD2): |
| 162 | _construct = FlagsEnum(Byte, rfm_create=1, rfm_delete_terminate=2, other_applet_create=4, |
| 163 | other_applet_delete_terminate=8) |
| 164 | |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 165 | # ETSI TS 102 221 11.1.1.4.6.0 |
| 166 | class ProprietaryInformation(BER_TLV_IE, tag=0xA5, |
| 167 | nested=[UiccCharacteristics, ApplicationPowerConsumption, |
| 168 | MinApplicationClockFrequency, AvailableMemory, |
| 169 | FileDetails, ReservedFileSize, MaximumFileSize, |
Harald Welte | 3c9b784 | 2021-10-19 21:44:24 +0200 | [diff] [blame] | 170 | SupportedFilesystemCommands, SpecificUiccEnvironmentConditions, |
| 171 | ToolkitAccessConditions]): |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 172 | pass |
| 173 | |
| 174 | # ETSI TS 102 221 11.1.1.4.7.1 |
| 175 | class SecurityAttribCompact(BER_TLV_IE, tag=0x8c): |
| 176 | _construct = GreedyBytes |
| 177 | |
| 178 | # ETSI TS 102 221 11.1.1.4.7.2 |
| 179 | class SecurityAttribExpanded(BER_TLV_IE, tag=0xab): |
| 180 | _construct = GreedyBytes |
| 181 | |
| 182 | # ETSI TS 102 221 11.1.1.4.7.3 |
| 183 | class SecurityAttribReferenced(BER_TLV_IE, tag=0x8b): |
| 184 | # TODO: longer format with SEID |
Harald Welte | 3c9b784 | 2021-10-19 21:44:24 +0200 | [diff] [blame] | 185 | _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] | 186 | |
| 187 | # ETSI TS 102 221 11.1.1.4.8 |
| 188 | class ShortFileIdentifier(BER_TLV_IE, tag=0x88): |
Harald Welte | 5e9bd93 | 2022-02-25 09:35:28 +0100 | [diff] [blame] | 189 | # If the length of the TLV is 1, the SFI value is indicated in the 5 most significant bits (bits b8 to b4) |
| 190 | # of the TLV value field. In this case, bits b3 to b1 shall be set to 0 |
| 191 | class Shift3RAdapter(Adapter): |
| 192 | def _decode(self, obj, context, path): |
| 193 | return obj >> 3 |
| 194 | def _encode(self, obj, context, path): |
| 195 | return obj << 3 |
| 196 | _construct = COptional(Shift3RAdapter(Byte)) |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 197 | |
| 198 | # ETSI TS 102 221 11.1.1.4.9 |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 199 | class LifeCycleStatusInteger(BER_TLV_IE, tag=0x8A): |
| 200 | def _from_bytes(self, do: bytes): |
| 201 | lcsi = int.from_bytes(do, 'big') |
| 202 | if lcsi == 0x00: |
| 203 | ret = 'no_information' |
| 204 | elif lcsi == 0x01: |
| 205 | ret = 'creation' |
| 206 | elif lcsi == 0x03: |
| 207 | ret = 'initialization' |
| 208 | elif lcsi & 0x05 == 0x05: |
| 209 | ret = 'operational_activated' |
| 210 | elif lcsi & 0x05 == 0x04: |
| 211 | ret = 'operational_deactivated' |
| 212 | elif lcsi & 0xc0 == 0xc0: |
| 213 | ret = 'termination' |
| 214 | else: |
| 215 | ret = lcsi |
| 216 | self.decoded = ret |
| 217 | return self.decoded |
Harald Welte | 3c9b784 | 2021-10-19 21:44:24 +0200 | [diff] [blame] | 218 | def _to_bytes(self): |
| 219 | if self.decoded == 'no_information': |
| 220 | return b'\x00' |
| 221 | elif self.decoded == 'creation': |
| 222 | return b'\x01' |
| 223 | elif self.decoded == 'initialization': |
| 224 | return b'\x03' |
| 225 | elif self.decoded == 'operational_activated': |
| 226 | return b'\x05' |
| 227 | elif self.decoded == 'operational_deactivated': |
| 228 | return b'\x04' |
| 229 | elif self.decoded == 'termination': |
| 230 | return b'\x0c' |
| 231 | elif isinstance(self.decoded, int): |
| 232 | return self.decoded.to_bytes(1, 'big') |
| 233 | else: |
| 234 | raise ValueError |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 235 | |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 236 | # ETSI TS 102 221 11.1.1.4.9 |
| 237 | class PS_DO(BER_TLV_IE, tag=0x90): |
| 238 | _construct = GreedyBytes |
| 239 | class UsageQualifier_DO(BER_TLV_IE, tag=0x95): |
| 240 | _construct = GreedyBytes |
| 241 | class KeyReference(BER_TLV_IE, tag=0x83): |
| 242 | _construct = Byte |
| 243 | class PinStatusTemplate_DO(BER_TLV_IE, tag=0xC6, nested=[PS_DO, UsageQualifier_DO, KeyReference]): |
| 244 | pass |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 245 | |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 246 | class FcpTemplate(BER_TLV_IE, tag=0x62, nested=[FileSize, TotalFileSize, FileDescriptor, FileIdentifier, |
| 247 | DfName, ProprietaryInformation, SecurityAttribCompact, |
| 248 | SecurityAttribExpanded, SecurityAttribReferenced, |
| 249 | ShortFileIdentifier, LifeCycleStatusInteger, |
| 250 | PinStatusTemplate_DO]): |
| 251 | pass |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 252 | |
| 253 | |
| 254 | def tlv_key_replace(inmap, indata): |
| 255 | def newkey(inmap, key): |
| 256 | if key in inmap: |
| 257 | return inmap[key] |
| 258 | else: |
| 259 | return key |
| 260 | return {newkey(inmap, d[0]): d[1] for d in indata.items()} |
| 261 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 262 | |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 263 | def tlv_val_interpret(inmap, indata): |
| 264 | def newval(inmap, key, val): |
| 265 | if key in inmap: |
| 266 | return inmap[key](val) |
| 267 | else: |
| 268 | return val |
| 269 | return {d[0]: newval(inmap, d[0], d[1]) for d in indata.items()} |
| 270 | |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 271 | # 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] | 272 | class _AM_DO_DF(DataObject): |
| 273 | def __init__(self): |
| 274 | super().__init__('access_mode', 'Access Mode', tag=0x80) |
| 275 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 276 | def from_bytes(self, do: bytes): |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 277 | res = [] |
| 278 | if len(do) != 1: |
| 279 | raise ValueError("We only support single-byte AMF inside AM-DO") |
| 280 | amf = do[0] |
| 281 | # tables 17..29 and 41..44 of 7816-4 |
| 282 | if amf & 0x80 == 0: |
| 283 | if amf & 0x40: |
| 284 | res.append('delete_file') |
| 285 | if amf & 0x20: |
| 286 | res.append('terminate_df') |
| 287 | if amf & 0x10: |
| 288 | res.append('activate_file') |
| 289 | if amf & 0x08: |
| 290 | res.append('deactivate_file') |
| 291 | if amf & 0x04: |
| 292 | res.append('create_file_df') |
| 293 | if amf & 0x02: |
| 294 | res.append('create_file_ef') |
| 295 | if amf & 0x01: |
| 296 | res.append('delete_file_child') |
| 297 | self.decoded = res |
| 298 | |
| 299 | def to_bytes(self): |
| 300 | val = 0 |
| 301 | if 'delete_file' in self.decoded: |
| 302 | val |= 0x40 |
| 303 | if 'terminate_df' in self.decoded: |
| 304 | val |= 0x20 |
| 305 | if 'activate_file' in self.decoded: |
| 306 | val |= 0x10 |
| 307 | if 'deactivate_file' in self.decoded: |
| 308 | val |= 0x08 |
| 309 | if 'create_file_df' in self.decoded: |
| 310 | val |= 0x04 |
| 311 | if 'create_file_ef' in self.decoded: |
| 312 | val |= 0x02 |
| 313 | if 'delete_file_child' in self.decoded: |
| 314 | val |= 0x01 |
| 315 | return val.to_bytes(1, 'big') |
| 316 | |
| 317 | |
| 318 | class _AM_DO_EF(DataObject): |
| 319 | """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] | 320 | |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 321 | def __init__(self): |
| 322 | super().__init__('access_mode', 'Access Mode', tag=0x80) |
| 323 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 324 | def from_bytes(self, do: bytes): |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 325 | res = [] |
| 326 | if len(do) != 1: |
| 327 | raise ValueError("We only support single-byte AMF inside AM-DO") |
| 328 | amf = do[0] |
| 329 | # tables 17..29 and 41..44 of 7816-4 |
| 330 | if amf & 0x80 == 0: |
| 331 | if amf & 0x40: |
| 332 | res.append('delete_file') |
| 333 | if amf & 0x20: |
| 334 | res.append('terminate_ef') |
| 335 | if amf & 0x10: |
| 336 | res.append('activate_file_or_record') |
| 337 | if amf & 0x08: |
| 338 | res.append('deactivate_file_or_record') |
| 339 | if amf & 0x04: |
| 340 | res.append('write_append') |
| 341 | if amf & 0x02: |
| 342 | res.append('update_erase') |
| 343 | if amf & 0x01: |
| 344 | res.append('read_search_compare') |
| 345 | self.decoded = res |
| 346 | |
| 347 | def to_bytes(self): |
| 348 | val = 0 |
| 349 | if 'delete_file' in self.decoded: |
| 350 | val |= 0x40 |
| 351 | if 'terminate_ef' in self.decoded: |
| 352 | val |= 0x20 |
| 353 | if 'activate_file_or_record' in self.decoded: |
| 354 | val |= 0x10 |
| 355 | if 'deactivate_file_or_record' in self.decoded: |
| 356 | val |= 0x08 |
| 357 | if 'write_append' in self.decoded: |
| 358 | val |= 0x04 |
| 359 | if 'update_erase' in self.decoded: |
| 360 | val |= 0x02 |
| 361 | if 'read_search_compare' in self.decoded: |
| 362 | val |= 0x01 |
| 363 | return val.to_bytes(1, 'big') |
| 364 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 365 | |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 366 | class _AM_DO_CHDR(DataObject): |
| 367 | """Command Header Access Mode DO according to ISO 7816-4 Table 32.""" |
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 | def __init__(self, tag): |
| 370 | super().__init__('command_header', 'Command Header Description', tag=tag) |
| 371 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 372 | def from_bytes(self, do: bytes): |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 373 | res = {} |
| 374 | i = 0 |
| 375 | if self.tag & 0x08: |
| 376 | res['CLA'] = do[i] |
| 377 | i += 1 |
| 378 | if self.tag & 0x04: |
| 379 | res['INS'] = do[i] |
| 380 | i += 1 |
| 381 | if self.tag & 0x02: |
| 382 | res['P1'] = do[i] |
| 383 | i += 1 |
| 384 | if self.tag & 0x01: |
| 385 | res['P2'] = do[i] |
| 386 | i += 1 |
| 387 | self.decoded = res |
| 388 | |
| 389 | def _compute_tag(self): |
| 390 | """Override to encode the tag, as it depends on the value.""" |
| 391 | tag = 0x80 |
| 392 | if 'CLA' in self.decoded: |
| 393 | tag |= 0x08 |
| 394 | if 'INS' in self.decoded: |
| 395 | tag |= 0x04 |
| 396 | if 'P1' in self.decoded: |
| 397 | tag |= 0x02 |
| 398 | if 'P2' in self.decoded: |
| 399 | tag |= 0x01 |
| 400 | return tag |
| 401 | |
| 402 | def to_bytes(self): |
| 403 | res = bytearray() |
| 404 | if 'CLA' in self.decoded: |
| 405 | res.append(self.decoded['CLA']) |
| 406 | if 'INS' in self.decoded: |
| 407 | res.append(self.decoded['INS']) |
| 408 | if 'P1' in self.decoded: |
| 409 | res.append(self.decoded['P1']) |
| 410 | if 'P2' in self.decoded: |
| 411 | res.append(self.decoded['P2']) |
| 412 | return res |
| 413 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 414 | |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 415 | AM_DO_CHDR = DataObjectChoice('am_do_chdr', members=[ |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 416 | _AM_DO_CHDR(0x81), _AM_DO_CHDR(0x82), _AM_DO_CHDR(0x83), _AM_DO_CHDR(0x84), |
| 417 | _AM_DO_CHDR(0x85), _AM_DO_CHDR(0x86), _AM_DO_CHDR(0x87), _AM_DO_CHDR(0x88), |
| 418 | _AM_DO_CHDR(0x89), _AM_DO_CHDR(0x8a), _AM_DO_CHDR(0x8b), _AM_DO_CHDR(0x8c), |
| 419 | _AM_DO_CHDR(0x8d), _AM_DO_CHDR(0x8e), _AM_DO_CHDR(0x8f)]) |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 420 | |
| 421 | AM_DO_DF = AM_DO_CHDR | _AM_DO_DF() |
| 422 | AM_DO_EF = AM_DO_CHDR | _AM_DO_EF() |
| 423 | |
| 424 | |
| 425 | # TS 102 221 Section 9.5.1 / Table 9.3 |
| 426 | pin_names = bidict({ |
| 427 | 0x01: 'PIN1', |
| 428 | 0x02: 'PIN2', |
| 429 | 0x03: 'PIN3', |
| 430 | 0x04: 'PIN4', |
| 431 | 0x05: 'PIN5', |
| 432 | 0x06: 'PIN6', |
| 433 | 0x07: 'PIN7', |
| 434 | 0x08: 'PIN8', |
| 435 | 0x0a: 'ADM1', |
| 436 | 0x0b: 'ADM2', |
| 437 | 0x0c: 'ADM3', |
| 438 | 0x0d: 'ADM4', |
| 439 | 0x0e: 'ADM5', |
| 440 | |
| 441 | 0x11: 'UNIVERSAL_PIN', |
| 442 | 0x81: '2PIN1', |
| 443 | 0x82: '2PIN2', |
| 444 | 0x83: '2PIN3', |
| 445 | 0x84: '2PIN4', |
| 446 | 0x85: '2PIN5', |
| 447 | 0x86: '2PIN6', |
| 448 | 0x87: '2PIN7', |
| 449 | 0x88: '2PIN8', |
| 450 | 0x8a: 'ADM6', |
| 451 | 0x8b: 'ADM7', |
| 452 | 0x8c: 'ADM8', |
| 453 | 0x8d: 'ADM9', |
| 454 | 0x8e: 'ADM10', |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 455 | }) |
| 456 | |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 457 | |
| 458 | class CRT_DO(DataObject): |
| 459 | """Control Reference Template as per TS 102 221 9.5.1""" |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 460 | |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 461 | def __init__(self): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 462 | super().__init__('control_reference_template', |
| 463 | 'Control Reference Template', tag=0xA4) |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 464 | |
| 465 | def from_bytes(self, do: bytes): |
| 466 | """Decode a Control Reference Template DO.""" |
| 467 | if len(do) != 6: |
| 468 | raise ValueError('Unsupported CRT DO length: %s', do) |
| 469 | if do[0] != 0x83 or do[1] != 0x01: |
| 470 | raise ValueError('Unsupported Key Ref Tag or Len in CRT DO %s', do) |
| 471 | if do[3:] != b'\x95\x01\x08': |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 472 | raise ValueError( |
| 473 | 'Unsupported Usage Qualifier Tag or Len in CRT DO %s', do) |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 474 | self.encoded = do[0:6] |
| 475 | self.decoded = pin_names[do[2]] |
| 476 | return do[6:] |
| 477 | |
| 478 | def to_bytes(self): |
| 479 | pin = pin_names.inverse[self.decoded] |
| 480 | return b'\x83\x01' + pin.to_bytes(1, 'big') + b'\x95\x01\x08' |
| 481 | |
| 482 | # ISO7816-4 9.3.3 Table 33 |
| 483 | class SecCondByte_DO(DataObject): |
| 484 | def __init__(self, tag=0x9d): |
| 485 | super().__init__('security_condition_byte', tag=tag) |
| 486 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 487 | def from_bytes(self, binary: bytes): |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 488 | if len(binary) != 1: |
| 489 | raise ValueError |
| 490 | inb = binary[0] |
| 491 | if inb == 0: |
| 492 | cond = 'always' |
| 493 | if inb == 0xff: |
| 494 | cond = 'never' |
| 495 | res = [] |
| 496 | if inb & 0x80: |
| 497 | cond = 'and' |
| 498 | else: |
| 499 | cond = 'or' |
| 500 | if inb & 0x40: |
| 501 | res.append('secure_messaging') |
| 502 | if inb & 0x20: |
| 503 | res.append('external_auth') |
| 504 | if inb & 0x10: |
| 505 | res.append('user_auth') |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 506 | rd = {'mode': cond} |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 507 | if len(res): |
| 508 | rd['conditions'] = res |
| 509 | self.decoded = rd |
| 510 | |
| 511 | def to_bytes(self): |
| 512 | mode = self.decoded['mode'] |
| 513 | if mode == 'always': |
| 514 | res = 0 |
| 515 | elif mode == 'never': |
| 516 | res = 0xff |
| 517 | else: |
| 518 | res = 0 |
| 519 | if mode == 'and': |
| 520 | res |= 0x80 |
| 521 | elif mode == 'or': |
| 522 | pass |
| 523 | else: |
| 524 | raise ValueError('Unknown mode %s' % mode) |
| 525 | for c in self.decoded['conditions']: |
| 526 | if c == 'secure_messaging': |
| 527 | res |= 0x40 |
| 528 | elif c == 'external_auth': |
| 529 | res |= 0x20 |
| 530 | elif c == 'user_auth': |
| 531 | res |= 0x10 |
| 532 | else: |
| 533 | raise ValueError('Unknown condition %s' % c) |
| 534 | return res.to_bytes(1, 'big') |
| 535 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 536 | |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 537 | Always_DO = TL0_DataObject('always', 'Always', 0x90) |
| 538 | Never_DO = TL0_DataObject('never', 'Never', 0x97) |
Harald Welte | b060833 | 2022-02-10 12:45:37 +0100 | [diff] [blame] | 539 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 540 | |
Harald Welte | b060833 | 2022-02-10 12:45:37 +0100 | [diff] [blame] | 541 | class Nested_DO(DataObject): |
| 542 | """A DO that nests another DO/Choice/Sequence""" |
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 | def __init__(self, name, tag, choice): |
| 545 | super().__init__(name, tag=tag) |
| 546 | self.children = choice |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 547 | |
| 548 | def from_bytes(self, binary: bytes) -> list: |
Harald Welte | b060833 | 2022-02-10 12:45:37 +0100 | [diff] [blame] | 549 | remainder = binary |
| 550 | self.decoded = [] |
| 551 | while remainder: |
| 552 | rc, remainder = self.children.decode(remainder) |
| 553 | self.decoded.append(rc) |
| 554 | return self.decoded |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 555 | |
Harald Welte | b060833 | 2022-02-10 12:45:37 +0100 | [diff] [blame] | 556 | def to_bytes(self) -> bytes: |
| 557 | encoded = [self.children.encode(d) for d in self.decoded] |
| 558 | return b''.join(encoded) |
| 559 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 560 | |
Harald Welte | b060833 | 2022-02-10 12:45:37 +0100 | [diff] [blame] | 561 | OR_Template = DataObjectChoice('or_template', 'OR-Template', |
| 562 | members=[Always_DO, Never_DO, SecCondByte_DO(), SecCondByte_DO(0x9e), CRT_DO()]) |
| 563 | OR_DO = Nested_DO('or', 0xa0, OR_Template) |
| 564 | AND_Template = DataObjectChoice('and_template', 'AND-Template', |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 565 | members=[Always_DO, Never_DO, SecCondByte_DO(), SecCondByte_DO(0x9e), CRT_DO()]) |
Harald Welte | b060833 | 2022-02-10 12:45:37 +0100 | [diff] [blame] | 566 | AND_DO = Nested_DO('and', 0xa7, AND_Template) |
| 567 | NOT_Template = DataObjectChoice('not_template', 'NOT-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 | NOT_DO = Nested_DO('not', 0xaf, NOT_Template) |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 570 | SC_DO = DataObjectChoice('security_condition', 'Security Condition', |
Harald Welte | b060833 | 2022-02-10 12:45:37 +0100 | [diff] [blame] | 571 | members=[Always_DO, Never_DO, SecCondByte_DO(), SecCondByte_DO(0x9e), CRT_DO(), |
| 572 | OR_DO, AND_DO, NOT_DO]) |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 573 | |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 574 | # TS 102 221 Section 13.1 |
| 575 | class EF_DIR(LinFixedEF): |
Harald Welte | 181c7c5 | 2022-02-10 14:18:32 +0100 | [diff] [blame] | 576 | class ApplicationLabel(BER_TLV_IE, tag=0x50): |
| 577 | # TODO: UCS-2 coding option as per Annex A of TS 102 221 |
| 578 | _construct = GreedyString('ascii') |
| 579 | |
| 580 | # see https://github.com/PyCQA/pylint/issues/5794 |
| 581 | #pylint: disable=undefined-variable |
| 582 | class ApplicationTemplate(BER_TLV_IE, tag=0x61, |
| 583 | nested=[iso7816_4.ApplicationId, ApplicationLabel, iso7816_4.FileReference, |
| 584 | iso7816_4.CommandApdu, iso7816_4.DiscretionaryData, |
| 585 | iso7816_4.DiscretionaryTemplate, iso7816_4.URL, |
| 586 | iso7816_4.ApplicationRelatedDOSet]): |
| 587 | pass |
| 588 | |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 589 | def __init__(self, fid='2f00', sfid=0x1e, name='EF.DIR', desc='Application Directory'): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 590 | 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] | 591 | self._tlv = EF_DIR.ApplicationTemplate |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 592 | |
| 593 | # TS 102 221 Section 13.2 |
| 594 | class EF_ICCID(TransparentEF): |
| 595 | def __init__(self, fid='2fe2', sfid=0x02, name='EF.ICCID', desc='ICC Identification'): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 596 | super().__init__(fid, sfid=sfid, name=name, desc=desc, size={10, 10}) |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 597 | |
| 598 | def _decode_hex(self, raw_hex): |
| 599 | return {'iccid': dec_iccid(raw_hex)} |
| 600 | |
| 601 | def _encode_hex(self, abstract): |
| 602 | return enc_iccid(abstract['iccid']) |
| 603 | |
| 604 | # TS 102 221 Section 13.3 |
| 605 | class EF_PL(TransRecEF): |
| 606 | 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] | 607 | super().__init__(fid, sfid=sfid, name=name, |
| 608 | desc=desc, rec_len=2, size={2, None}) |
| 609 | |
Harald Welte | 0c840f0 | 2022-01-21 15:42:22 +0100 | [diff] [blame] | 610 | def _decode_record_bin(self, bin_data): |
| 611 | if bin_data == b'\xff\xff': |
| 612 | return None |
| 613 | else: |
| 614 | return bin_data.decode('ascii') |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 615 | |
Harald Welte | 0c840f0 | 2022-01-21 15:42:22 +0100 | [diff] [blame] | 616 | def _encode_record_bin(self, in_json): |
| 617 | if in_json is None: |
| 618 | return b'\xff\xff' |
| 619 | else: |
| 620 | return in_json.encode('ascii') |
| 621 | |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 622 | |
| 623 | # TS 102 221 Section 13.4 |
| 624 | class EF_ARR(LinFixedEF): |
| 625 | def __init__(self, fid='2f06', sfid=0x06, name='EF.ARR', desc='Access Rule Reference'): |
| 626 | super().__init__(fid, sfid=sfid, name=name, desc=desc) |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 627 | # add those commands to the general commands of a TransparentEF |
| 628 | self.shell_commands += [self.AddlShellCommands()] |
| 629 | |
| 630 | @staticmethod |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 631 | def flatten(inp: list): |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 632 | """Flatten the somewhat deep/complex/nested data returned from decoder.""" |
| 633 | def sc_abbreviate(sc): |
| 634 | if 'always' in sc: |
| 635 | return 'always' |
| 636 | elif 'never' in sc: |
| 637 | return 'never' |
| 638 | elif 'control_reference_template' in sc: |
| 639 | return sc['control_reference_template'] |
| 640 | else: |
| 641 | return sc |
| 642 | |
| 643 | by_mode = {} |
| 644 | for t in inp: |
| 645 | am = t[0] |
| 646 | sc = t[1] |
| 647 | sc_abbr = sc_abbreviate(sc) |
| 648 | if 'access_mode' in am: |
| 649 | for m in am['access_mode']: |
| 650 | by_mode[m] = sc_abbr |
| 651 | elif 'command_header' in am: |
| 652 | ins = am['command_header']['INS'] |
| 653 | if 'CLA' in am['command_header']: |
| 654 | cla = am['command_header']['CLA'] |
| 655 | else: |
| 656 | cla = None |
| 657 | cmd = ts_102_22x_cmdset.lookup(ins, cla) |
| 658 | if cmd: |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 659 | name = cmd.name.lower().replace(' ', '_') |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 660 | by_mode[name] = sc_abbr |
| 661 | else: |
| 662 | raise ValueError |
| 663 | else: |
| 664 | raise ValueError |
| 665 | return by_mode |
| 666 | |
| 667 | def _decode_record_bin(self, raw_bin_data): |
| 668 | # 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] | 669 | arr_seq = DataObjectSequence('arr', sequence=[AM_DO_EF, SC_DO]) |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 670 | dec = arr_seq.decode_multi(raw_bin_data) |
| 671 | # we cannot pass the result through flatten() here, as we don't have a related |
| 672 | # 'un-flattening' decoder, and hence would be unable to encode :( |
| 673 | return dec[0] |
| 674 | |
| 675 | @with_default_category('File-Specific Commands') |
| 676 | class AddlShellCommands(CommandSet): |
| 677 | def __init__(self): |
| 678 | super().__init__() |
| 679 | |
| 680 | @cmd2.with_argparser(LinFixedEF.ShellCommands.read_rec_dec_parser) |
| 681 | def do_read_arr_record(self, opts): |
| 682 | """Read one EF.ARR record in flattened, human-friendly form.""" |
| 683 | (data, sw) = self._cmd.rs.read_record_dec(opts.record_nr) |
| 684 | data = self._cmd.rs.selected_file.flatten(data) |
| 685 | self._cmd.poutput_json(data, opts.oneline) |
| 686 | |
| 687 | @cmd2.with_argparser(LinFixedEF.ShellCommands.read_recs_dec_parser) |
| 688 | def do_read_arr_records(self, opts): |
| 689 | """Read + decode all EF.ARR records in flattened, human-friendly form.""" |
Harald Welte | 747a978 | 2022-02-13 17:52:28 +0100 | [diff] [blame] | 690 | num_of_rec = self._cmd.rs.selected_file_num_of_rec() |
Harald Welte | 4ae228a | 2021-05-02 21:29:04 +0200 | [diff] [blame] | 691 | # collect all results in list so they are rendered as JSON list when printing |
| 692 | data_list = [] |
| 693 | for recnr in range(1, 1 + num_of_rec): |
| 694 | (data, sw) = self._cmd.rs.read_record_dec(recnr) |
| 695 | data = self._cmd.rs.selected_file.flatten(data) |
| 696 | data_list.append(data) |
| 697 | self._cmd.poutput_json(data_list, opts.oneline) |
| 698 | |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 699 | |
| 700 | # TS 102 221 Section 13.6 |
| 701 | class EF_UMPC(TransparentEF): |
| 702 | def __init__(self, fid='2f08', sfid=0x08, name='EF.UMPC', desc='UICC Maximum Power Consumption'): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 703 | super().__init__(fid, sfid=sfid, name=name, desc=desc, size={5, 5}) |
| 704 | addl_info = FlagsEnum(Byte, req_inc_idle_current=1, |
| 705 | support_uicc_suspend=2) |
| 706 | self._construct = Struct( |
| 707 | 'max_current_mA'/Int8ub, 't_op_s'/Int8ub, 'addl_info'/addl_info) |
| 708 | |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 709 | |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 710 | class CardProfileUICC(CardProfile): |
Philipp Maier | a028c7d | 2021-11-08 16:12:03 +0100 | [diff] [blame] | 711 | |
| 712 | ORDER = 1 |
| 713 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 714 | def __init__(self, name='UICC'): |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 715 | files = [ |
| 716 | EF_DIR(), |
| 717 | EF_ICCID(), |
| 718 | EF_PL(), |
| 719 | EF_ARR(), |
| 720 | # FIXME: DF.CD |
| 721 | EF_UMPC(), |
| 722 | ] |
| 723 | sw = { |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 724 | 'Normal': { |
| 725 | '9000': 'Normal ending of the command', |
| 726 | '91xx': 'Normal ending of the command, with extra information from the proactive UICC containing a command for the terminal', |
| 727 | '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] | 728 | }, |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 729 | 'Postponed processing': { |
| 730 | '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] | 731 | }, |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 732 | 'Warnings': { |
| 733 | '6200': 'No information given, state of non-volatile memory unchanged', |
| 734 | '6281': 'Part of returned data may be corrupted', |
| 735 | '6282': 'End of file/record reached before reading Le bytes or unsuccessful search', |
| 736 | '6283': 'Selected file invalidated', |
| 737 | '6284': 'Selected file in termination state', |
| 738 | '62f1': 'More data available', |
| 739 | '62f2': 'More data available and proactive command pending', |
| 740 | '62f3': 'Response data available', |
| 741 | '63f1': 'More data expected', |
| 742 | '63f2': 'More data expected and proactive command pending', |
| 743 | '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] | 744 | }, |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 745 | 'Execution errors': { |
| 746 | '6400': 'No information given, state of non-volatile memory unchanged', |
| 747 | '6500': 'No information given, state of non-volatile memory changed', |
| 748 | '6581': 'Memory problem', |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 749 | }, |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 750 | 'Checking errors': { |
| 751 | '6700': 'Wrong length', |
| 752 | '67xx': 'The interpretation of this status word is command dependent', |
| 753 | '6b00': 'Wrong parameter(s) P1-P2', |
| 754 | '6d00': 'Instruction code not supported or invalid', |
| 755 | '6e00': 'Class not supported', |
| 756 | '6f00': 'Technical problem, no precise diagnosis', |
| 757 | '6fxx': 'The interpretation of this status word is command dependent', |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 758 | }, |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 759 | 'Functions in CLA not supported': { |
| 760 | '6800': 'No information given', |
| 761 | '6881': 'Logical channel not supported', |
| 762 | '6882': 'Secure messaging not supported', |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 763 | }, |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 764 | 'Command not allowed': { |
| 765 | '6900': 'No information given', |
| 766 | '6981': 'Command incompatible with file structure', |
| 767 | '6982': 'Security status not satisfied', |
| 768 | '6983': 'Authentication/PIN method blocked', |
| 769 | '6984': 'Referenced data invalidated', |
| 770 | '6985': 'Conditions of use not satisfied', |
| 771 | '6986': 'Command not allowed (no EF selected)', |
| 772 | '6989': 'Command not allowed - secure channel - security not satisfied', |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 773 | }, |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 774 | 'Wrong parameters': { |
| 775 | '6a80': 'Incorrect parameters in the data field', |
| 776 | '6a81': 'Function not supported', |
| 777 | '6a82': 'File not found', |
| 778 | '6a83': 'Record not found', |
| 779 | '6a84': 'Not enough memory space', |
| 780 | '6a86': 'Incorrect parameters P1 to P2', |
| 781 | '6a87': 'Lc inconsistent with P1 to P2', |
| 782 | '6a88': 'Referenced data not found', |
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 | 'Application errors': { |
| 785 | '9850': 'INCREASE cannot be performed, max value reached', |
| 786 | '9862': 'Authentication error, application specific', |
| 787 | '9863': 'Security session or association expired', |
| 788 | '9864': 'Minimum UICC suspension time is too long', |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 789 | }, |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 790 | } |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 791 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 792 | super().__init__(name, desc='ETSI TS 102 221', cla="00", |
| 793 | sel_ctrl="0004", files_in_mf=files, sw=sw) |
Philipp Maier | 5af7bdf | 2021-11-04 12:48:41 +0100 | [diff] [blame] | 794 | |
Philipp Maier | 5998a3a | 2021-11-16 15:16:39 +0100 | [diff] [blame] | 795 | @staticmethod |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 796 | def decode_select_response(resp_hex: str) -> object: |
Philipp Maier | 5998a3a | 2021-11-16 15:16:39 +0100 | [diff] [blame] | 797 | """ETSI TS 102 221 Section 11.1.1.3""" |
Harald Welte | c8c3327 | 2022-02-11 14:45:23 +0100 | [diff] [blame] | 798 | t = FcpTemplate() |
| 799 | t.from_tlv(h2b(resp_hex)) |
| 800 | d = t.to_dict() |
| 801 | return flatten_dict_lists(d['fcp_template']) |
Philipp Maier | a028c7d | 2021-11-08 16:12:03 +0100 | [diff] [blame] | 802 | |
| 803 | @staticmethod |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 804 | def match_with_card(scc: SimCardCommands) -> bool: |
Philipp Maier | a028c7d | 2021-11-08 16:12:03 +0100 | [diff] [blame] | 805 | return match_uicc(scc) |
| 806 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 807 | |
Philipp Maier | a028c7d | 2021-11-08 16:12:03 +0100 | [diff] [blame] | 808 | class CardProfileUICCSIM(CardProfileUICC): |
| 809 | """Same as above, but including 2G SIM support""" |
| 810 | |
| 811 | ORDER = 0 |
| 812 | |
| 813 | def __init__(self): |
| 814 | super().__init__('UICC-SIM') |
| 815 | |
| 816 | # Add GSM specific files |
| 817 | self.files_in_mf.append(DF_TELECOM()) |
| 818 | self.files_in_mf.append(DF_GSM()) |
| 819 | |
| 820 | @staticmethod |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 821 | def match_with_card(scc: SimCardCommands) -> bool: |
Philipp Maier | a028c7d | 2021-11-08 16:12:03 +0100 | [diff] [blame] | 822 | return match_uicc(scc) and match_sim(scc) |