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 | |
| 20 | from pytlv.TLV import * |
| 21 | from struct import pack, unpack |
| 22 | from pySim.utils import * |
| 23 | from pySim.filesystem import * |
| 24 | |
| 25 | |
| 26 | FCP_TLV_MAP = { |
| 27 | '82': 'file_descriptor', |
| 28 | '83': 'file_identifier', |
| 29 | '84': 'df_name', |
| 30 | 'A5': 'proprietary_info', |
| 31 | '8A': 'life_cycle_status_int', |
| 32 | '8B': 'security_attrib_ref_expanded', |
| 33 | '8C': 'security_attrib_compact', |
| 34 | 'AB': 'security_attrib_espanded', |
| 35 | 'C6': 'pin_status_template_do', |
| 36 | '80': 'file_size', |
| 37 | '81': 'total_file_size', |
| 38 | '88': 'short_file_id', |
| 39 | } |
| 40 | |
| 41 | # ETSI TS 102 221 11.1.1.4.6 |
| 42 | FCP_Proprietary_TLV_MAP = { |
| 43 | '80': 'uicc_characteristics', |
| 44 | '81': 'application_power_consumption', |
| 45 | '82': 'minimum_app_clock_freq', |
| 46 | '83': 'available_memory', |
| 47 | '84': 'file_details', |
| 48 | '85': 'reserved_file_size', |
| 49 | '86': 'maximum_file_size', |
| 50 | '87': 'suported_system_commands', |
| 51 | '88': 'specific_uicc_env_cond', |
| 52 | '89': 'p2p_cat_secured_apdu', |
| 53 | # Additional private TLV objects (bits b7 and b8 of the first byte of the tag set to '1') |
| 54 | } |
| 55 | |
| 56 | # ETSI TS 102 221 11.1.1.4.3 |
| 57 | def interpret_file_descriptor(in_hex): |
| 58 | in_bin = h2b(in_hex) |
| 59 | out = {} |
| 60 | ft_dict = { |
| 61 | 0: 'working_ef', |
| 62 | 1: 'internal_ef', |
| 63 | 7: 'df' |
| 64 | } |
| 65 | fs_dict = { |
| 66 | 0: 'no_info_given', |
| 67 | 1: 'transparent', |
| 68 | 2: 'linear_fixed', |
| 69 | 6: 'cyclic', |
| 70 | } |
| 71 | fdb = in_bin[0] |
| 72 | ftype = (fdb >> 3) & 7 |
| 73 | fstruct = fdb & 7 |
| 74 | out['shareable'] = True if fdb & 0x40 else False |
| 75 | out['file_type'] = ft_dict[ftype] if ftype in ft_dict else ftype |
| 76 | out['structure'] = fs_dict[fstruct] if fstruct in fs_dict else fstruct |
| 77 | if len(in_bin) >= 5: |
| 78 | out['record_len'] = int.from_bytes(in_bin[2:4], 'big') |
| 79 | out['num_of_rec'] = int.from_bytes(in_bin[4:5], 'big') |
| 80 | return out |
| 81 | |
| 82 | # ETSI TS 102 221 11.1.1.4.9 |
| 83 | def interpret_life_cycle_sts_int(in_hex): |
| 84 | lcsi = int(in_hex, 16) |
| 85 | if lcsi == 0x00: |
| 86 | return 'no_information' |
| 87 | elif lcsi == 0x01: |
| 88 | return 'creation' |
| 89 | elif lcsi == 0x03: |
| 90 | return 'initialization' |
| 91 | elif lcsi & 0x05 == 0x05: |
| 92 | return 'operational_activated' |
| 93 | elif lcsi & 0x05 == 0x04: |
| 94 | return 'operational_deactivated' |
| 95 | elif lcsi & 0xc0 == 0xc0: |
| 96 | return 'termination' |
| 97 | else: |
| 98 | return in_hex |
| 99 | |
| 100 | # ETSI TS 102 221 11.1.1.4.10 |
| 101 | FCP_Pin_Status_TLV_MAP = { |
| 102 | '90': 'ps_do', |
| 103 | '95': 'usage_qualifier', |
| 104 | '83': 'key_reference', |
| 105 | } |
| 106 | |
| 107 | def interpret_ps_templ_do(in_hex): |
| 108 | # cannot use the 'TLV' parser due to repeating tags |
| 109 | #psdo_tlv = TLV(FCP_Pin_Status_TLV_MAP) |
| 110 | #return psdo_tlv.parse(in_hex) |
| 111 | return in_hex |
| 112 | |
| 113 | # 'interpreter' functions for each tag |
| 114 | FCP_interpreter_map = { |
| 115 | '80': lambda x: int(x, 16), |
| 116 | '82': interpret_file_descriptor, |
| 117 | '8A': interpret_life_cycle_sts_int, |
| 118 | 'C6': interpret_ps_templ_do, |
| 119 | } |
| 120 | |
| 121 | FCP_prorietary_interpreter_map = { |
| 122 | '83': lambda x: int(x, 16), |
| 123 | } |
| 124 | |
| 125 | # pytlv unfortunately doesn't have a setting using which we can make it |
| 126 | # accept unknown tags. It also doesn't raise a specific exception type but |
| 127 | # just the generic ValueError, so we cannot ignore those either. Instead, |
| 128 | # we insert a dict entry for every possible proprietary tag permitted |
| 129 | def fixup_fcp_proprietary_tlv_map(tlv_map): |
| 130 | if 'D0' in tlv_map: |
| 131 | return |
Philipp Maier | c98ef8a | 2021-04-07 10:51:22 +0200 | [diff] [blame] | 132 | for i in range(0xc0, 0xff): |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 133 | i_hex = i2h([i]).upper() |
| 134 | tlv_map[i_hex] = 'proprietary_' + i_hex |
| 135 | |
| 136 | |
| 137 | def tlv_key_replace(inmap, indata): |
| 138 | def newkey(inmap, key): |
| 139 | if key in inmap: |
| 140 | return inmap[key] |
| 141 | else: |
| 142 | return key |
| 143 | return {newkey(inmap, d[0]): d[1] for d in indata.items()} |
| 144 | |
| 145 | def tlv_val_interpret(inmap, indata): |
| 146 | def newval(inmap, key, val): |
| 147 | if key in inmap: |
| 148 | return inmap[key](val) |
| 149 | else: |
| 150 | return val |
| 151 | return {d[0]: newval(inmap, d[0], d[1]) for d in indata.items()} |
| 152 | |
| 153 | |
| 154 | # ETSI TS 102 221 Section 11.1.1.3 |
| 155 | def decode_select_response(resp_hex): |
| 156 | fixup_fcp_proprietary_tlv_map(FCP_Proprietary_TLV_MAP) |
| 157 | resp_hex = resp_hex.upper() |
| 158 | # outer layer |
| 159 | fcp_base_tlv = TLV(['62']) |
| 160 | fcp_base = fcp_base_tlv.parse(resp_hex) |
| 161 | # actual FCP |
| 162 | fcp_tlv = TLV(FCP_TLV_MAP) |
| 163 | fcp = fcp_tlv.parse(fcp_base['62']) |
| 164 | # further decode the proprietary information |
| 165 | if fcp['A5']: |
| 166 | prop_tlv = TLV(FCP_Proprietary_TLV_MAP) |
| 167 | prop = prop_tlv.parse(fcp['A5']) |
| 168 | fcp['A5'] = tlv_val_interpret(FCP_prorietary_interpreter_map, prop) |
| 169 | fcp['A5'] = tlv_key_replace(FCP_Proprietary_TLV_MAP, fcp['A5']) |
| 170 | # finally make sure we get human-readable keys in the output dict |
| 171 | r = tlv_val_interpret(FCP_interpreter_map, fcp) |
| 172 | return tlv_key_replace(FCP_TLV_MAP, r) |
| 173 | |
| 174 | |
| 175 | # TS 102 221 Section 13.1 |
| 176 | class EF_DIR(LinFixedEF): |
| 177 | def __init__(self, fid='2f00', sfid=0x1e, name='EF.DIR', desc='Application Directory'): |
| 178 | super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len={5,54}) |
| 179 | |
| 180 | def _decode_record_hex(self, raw_hex_data): |
| 181 | raw_hex_data = raw_hex_data.upper() |
| 182 | atempl_base_tlv = TLV(['61']) |
| 183 | atempl_base = atempl_base_tlv.parse(raw_hex_data) |
| 184 | atempl_TLV_MAP = {'4F': 'aid_value', 50:'label'} |
| 185 | atempl_tlv = TLV(atempl_TLV_MAP) |
| 186 | atempl = atempl_tlv.parse(atempl_base['61']) |
| 187 | # FIXME: "All other Dos are according to ISO/IEC 7816-4" |
| 188 | return tlv_key_replace(atempl_TLV_MAP, atempl) |
| 189 | |
| 190 | # TS 102 221 Section 13.2 |
| 191 | class EF_ICCID(TransparentEF): |
| 192 | def __init__(self, fid='2fe2', sfid=0x02, name='EF.ICCID', desc='ICC Identification'): |
| 193 | super().__init__(fid, sfid=sfid, name=name, desc=desc, size={10,10}) |
| 194 | |
| 195 | def _decode_hex(self, raw_hex): |
| 196 | return {'iccid': dec_iccid(raw_hex)} |
| 197 | |
| 198 | def _encode_hex(self, abstract): |
| 199 | return enc_iccid(abstract['iccid']) |
| 200 | |
| 201 | # TS 102 221 Section 13.3 |
| 202 | class EF_PL(TransRecEF): |
| 203 | def __init__(self, fid='2f05', sfid=0x05, name='EF.PL', desc='Preferred Languages'): |
| 204 | super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=2, size={2,None}) |
| 205 | |
| 206 | # TS 102 221 Section 13.4 |
| 207 | class EF_ARR(LinFixedEF): |
| 208 | def __init__(self, fid='2f06', sfid=0x06, name='EF.ARR', desc='Access Rule Reference'): |
| 209 | super().__init__(fid, sfid=sfid, name=name, desc=desc) |
| 210 | |
| 211 | # TS 102 221 Section 13.6 |
| 212 | class EF_UMPC(TransparentEF): |
| 213 | def __init__(self, fid='2f08', sfid=0x08, name='EF.UMPC', desc='UICC Maximum Power Consumption'): |
| 214 | super().__init__(fid, sfid=sfid, name=name, desc=desc, size={5,5}) |
| 215 | |
| 216 | |
| 217 | |
| 218 | class CardProfileUICC(CardProfile): |
| 219 | def __init__(self): |
| 220 | files = [ |
| 221 | EF_DIR(), |
| 222 | EF_ICCID(), |
| 223 | EF_PL(), |
| 224 | EF_ARR(), |
| 225 | # FIXME: DF.CD |
| 226 | EF_UMPC(), |
| 227 | ] |
| 228 | sw = { |
| 229 | 'Normal': { |
| 230 | '9000': 'Normal ending of the command', |
| 231 | '91xx': 'Normal ending of the command, with extra information from the proactive UICC containing a command for the terminal', |
| 232 | '92xx': 'Normal ending of the command, with extra information concerning an ongoing data transfer session', |
| 233 | }, |
| 234 | 'Postponed processing': { |
| 235 | '9300': 'SIM Application Toolkit is busy. Command cannot be executed at present, further normal commands are allowed', |
| 236 | }, |
| 237 | 'Warnings': { |
| 238 | '6200': 'No information given, state of non-volatile memory unchanged', |
| 239 | '6281': 'Part of returned data may be corrupted', |
| 240 | '6282': 'End of file/record reached before reading Le bytes or unsuccessful search', |
| 241 | '6283': 'Selected file invalidated', |
| 242 | '6284': 'Selected file in termination state', |
| 243 | '62f1': 'More data available', |
| 244 | '62f2': 'More data available and proactive command pending', |
| 245 | '62f3': 'Response data available', |
| 246 | '63f1': 'More data expected', |
| 247 | '63f2': 'More data expected and proactive command pending', |
| 248 | '63cx': 'Command successful but after using an internal update retry routine X times', |
| 249 | }, |
| 250 | 'Execution errors': { |
| 251 | '6400': 'No information given, state of non-volatile memory unchanged', |
| 252 | '6500': 'No information given, state of non-volatile memory changed', |
| 253 | '6581': 'Memory problem', |
| 254 | }, |
| 255 | 'Checking errors': { |
| 256 | '6700': 'Wrong length', |
| 257 | '67xx': 'The interpretation of this status word is command dependent', |
| 258 | '6b00': 'Wrong parameter(s) P1-P2', |
| 259 | '6d00': 'Instruction code not supported or invalid', |
| 260 | '6e00': 'Class not supported', |
| 261 | '6f00': 'Technical problem, no precise diagnosis', |
| 262 | '6fxx': 'The interpretation of this status word is command dependent', |
| 263 | }, |
| 264 | 'Functions in CLA not supported': { |
| 265 | '6800': 'No information given', |
| 266 | '6881': 'Logical channel not supported', |
| 267 | '6882': 'Secure messaging not supported', |
| 268 | }, |
| 269 | 'Command not allowed': { |
| 270 | '6900': 'No information given', |
| 271 | '6981': 'Command incompatible with file structure', |
| 272 | '6982': 'Security status not satisfied', |
| 273 | '6983': 'Authentication/PIN method blocked', |
| 274 | '6984': 'Referenced data invalidated', |
| 275 | '6985': 'Conditions of use not satisfied', |
| 276 | '6986': 'Command not allowed (no EF selected)', |
| 277 | '6989': 'Command not allowed - secure channel - security not satisfied', |
| 278 | }, |
| 279 | 'Wrong parameters': { |
| 280 | '6a80': 'Incorrect parameters in the data field', |
| 281 | '6a81': 'Function not supported', |
| 282 | '6a82': 'File not found', |
| 283 | '6a83': 'Record not found', |
| 284 | '6a84': 'Not enough memory space', |
| 285 | '6a86': 'Incorrect parameters P1 to P2', |
| 286 | '6a87': 'Lc inconsistent with P1 to P2', |
| 287 | '6a88': 'Referenced data not found', |
| 288 | }, |
| 289 | 'Application errors': { |
| 290 | '9850': 'INCREASE cannot be performed, max value reached', |
| 291 | '9862': 'Authentication error, application specific', |
| 292 | '9863': 'Security session or association expired', |
| 293 | '9864': 'Minimum UICC suspension time is too long', |
| 294 | }, |
| 295 | } |
| 296 | |
Philipp Maier | dd2091a | 2021-03-31 17:28:06 +0200 | [diff] [blame] | 297 | super().__init__('UICC', desc='ETSI TS 102 221', files_in_mf=files, sw=sw) |