Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame^] | 1 | |
| 2 | #include <errno.h> |
| 3 | #include <string.h> |
| 4 | |
| 5 | #include <osmocom/sim/sim.h> |
| 6 | #include <osmocom/core/talloc.h> |
| 7 | #include <osmocom/gsm/gsm48.h> |
| 8 | |
| 9 | #include "sim_int.h" |
| 10 | |
| 11 | /* TS 11.11 / Chapter 9.4 */ |
| 12 | static const struct osim_card_sw ts11_11_sw[] = { |
| 13 | { |
| 14 | 0x9000, 0xffff, SW_TYPE_STR, SW_CLS_OK, |
| 15 | .u.str = "Normal ending of the command", |
| 16 | }, { |
| 17 | 0x9100, 0xff00, SW_TYPE_STR, SW_CLS_OK, |
| 18 | .u.str = "Normal ending of the command - proactive command from SIM pending", |
| 19 | }, { |
| 20 | 0x9e00, 0xff00, SW_TYPE_STR, SW_CLS_OK, |
| 21 | .u.str = "Normal ending of the command - response data for SIM data download", |
| 22 | }, { |
| 23 | 0x9f00, 0xff00, SW_TYPE_STR, SW_CLS_OK, |
| 24 | .u.str = "Normal ending of the command - response data available", |
| 25 | }, { |
| 26 | 0x9300, 0xffff, SW_TYPE_STR, SW_CLS_POSTP, |
| 27 | .u.str = "SIM Application Toolkit is busy, command cannot be executed at present", |
| 28 | }, { |
| 29 | 0x9200, 0xfff0, SW_TYPE_STR, SW_CLS_WARN, |
| 30 | .u.str = "Memory management - Command successful but after using an internal updat retry X times", |
| 31 | }, { |
| 32 | 0x9240, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, |
| 33 | .u.str = "Memory management - Memory problem", |
| 34 | }, { |
| 35 | 0x9400, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, |
| 36 | .u.str = "Referencing management - no EF selected", |
| 37 | }, { |
| 38 | 0x9402, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, |
| 39 | .u.str = "Referencing management - out of range (invalid address)", |
| 40 | }, { |
| 41 | 0x9404, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, |
| 42 | .u.str = "Referencing management - file ID not found / pattern not found", |
| 43 | }, { |
| 44 | 0x9802, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, |
| 45 | .u.str = "Security management - no CHV initialized", |
| 46 | }, { |
| 47 | 0x9804, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, |
| 48 | .u.str = "Security management - access condition not fulfilled", |
| 49 | }, { |
| 50 | 0x9808, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, |
| 51 | .u.str = "Security management - in contradiction with CHV status", |
| 52 | }, { |
| 53 | 0x9810, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, |
| 54 | .u.str = "Security management - in contradiction with invalidation status", |
| 55 | }, { |
| 56 | 0x9840, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, |
| 57 | .u.str = "Security management - unsuccessful CHV verification, no attempt left", |
| 58 | }, { |
| 59 | 0x9850, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, |
| 60 | .u.str = "Security management - increase cannot be performed, max value reached", |
| 61 | }, { |
| 62 | 0x6700, 0xff00, SW_TYPE_STR, SW_CLS_ERROR, |
| 63 | .u.str = "Application independent - incorrect parameter P3", |
| 64 | }, { |
| 65 | 0x6b00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR, |
| 66 | .u.str = "Application independent - incorrect parameter P1 or P2", |
| 67 | }, { |
| 68 | 0x6d00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR, |
| 69 | .u.str = "Application independent - unknown instruction code", |
| 70 | }, { |
| 71 | 0x6e00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR, |
| 72 | .u.str = "Application independent - wrong instruction class", |
| 73 | }, { |
| 74 | 0x6f00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR, |
| 75 | .u.str = "Application independent - technical problem with no diagnostic given", |
| 76 | }, |
| 77 | OSIM_CARD_SW_LAST |
| 78 | }; |
| 79 | |
| 80 | static const struct osim_card_sw *sim_card_sws[] = { |
| 81 | ts11_11_sw, |
| 82 | NULL |
| 83 | }; |
| 84 | |
| 85 | static int iccid_decode(struct osim_decoded_data *dd, |
| 86 | const struct osim_file_desc *desc, |
| 87 | int len, uint8_t *data) |
| 88 | { |
| 89 | struct osim_decoded_element *elem; |
| 90 | |
| 91 | elem = element_alloc(dd, "ICCID", ELEM_T_BCD, ELEM_REPR_DEC); |
| 92 | elem->length = len; |
| 93 | elem->u.buf = talloc_memdup(elem, data, len); |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static int elp_decode(struct osim_decoded_data *dd, |
| 99 | const struct osim_file_desc *desc, |
| 100 | int len, uint8_t *data) |
| 101 | { |
| 102 | int i, num_lp = len / 2; |
| 103 | |
| 104 | for (i = 0; i < num_lp; i++) { |
| 105 | uint8_t *cur = data + i*2; |
| 106 | struct osim_decoded_element *elem; |
| 107 | elem = element_alloc(dd, "Language Code", ELEM_T_STRING, ELEM_REPR_NONE); |
| 108 | elem->u.buf = (uint8_t *) talloc_strndup(elem, (const char *) cur, 2); |
| 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int default_decode(struct osim_decoded_data *dd, |
| 115 | const struct osim_file_desc *desc, |
| 116 | int len, uint8_t *data) |
| 117 | { |
| 118 | struct osim_decoded_element *elem; |
| 119 | |
| 120 | elem = element_alloc(dd, "Unknown Payload", ELEM_T_BYTES, ELEM_REPR_HEX); |
| 121 | elem->u.buf = talloc_memdup(elem, data, len); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /* 10.3.1 */ |
| 128 | int gsm_lp_decode(struct osim_decoded_data *dd, |
| 129 | const struct osim_file_desc *desc, |
| 130 | int len, uint8_t *data) |
| 131 | { |
| 132 | int i; |
| 133 | |
| 134 | for (i = 0; i < len; i++) { |
| 135 | struct osim_decoded_element *elem; |
| 136 | elem = element_alloc(dd, "Language Code", ELEM_T_UINT8, ELEM_REPR_DEC); |
| 137 | elem->u.u8 = data[i]; |
| 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | /* 10.3.2 */ |
| 144 | int gsm_imsi_decode(struct osim_decoded_data *dd, |
| 145 | const struct osim_file_desc *desc, |
| 146 | int len, uint8_t *data) |
| 147 | { |
| 148 | struct osim_decoded_element *elem; |
| 149 | |
| 150 | if (len < 2) |
| 151 | return -EINVAL; |
| 152 | |
| 153 | elem = element_alloc(dd, "IMSI", ELEM_T_BCD, ELEM_REPR_DEC); |
| 154 | elem->length = data[0]; |
| 155 | elem->u.buf = talloc_memdup(elem, data+1, len-1); |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | /* 10.3.3 */ |
| 161 | static int gsm_kc_decode(struct osim_decoded_data *dd, |
| 162 | const struct osim_file_desc *desc, |
| 163 | int len, uint8_t *data) |
| 164 | { |
| 165 | struct osim_decoded_element *kc, *cksn; |
| 166 | |
| 167 | if (len < 9) |
| 168 | return -EINVAL; |
| 169 | |
| 170 | kc = element_alloc(dd, "Kc", ELEM_T_BYTES, ELEM_REPR_HEX); |
| 171 | kc->u.buf = talloc_memdup(kc, data, 8); |
| 172 | cksn = element_alloc(dd, "CKSN", ELEM_T_UINT8, ELEM_REPR_DEC); |
| 173 | cksn->u.u8 = data[8]; |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | /* 10.3.4 */ |
| 179 | static int gsm_plmnsel_decode(struct osim_decoded_data *dd, |
| 180 | const struct osim_file_desc *desc, |
| 181 | int len, uint8_t *data) |
| 182 | { |
| 183 | int i, n_plmn = len / 3; |
| 184 | |
| 185 | if (n_plmn < 1) |
| 186 | return -EINVAL; |
| 187 | |
| 188 | for (i = 0; i < n_plmn; i++) { |
| 189 | uint8_t *cur = data + 3*i; |
| 190 | struct osim_decoded_element *elem, *mcc, *mnc; |
| 191 | uint8_t ra_buf[6]; |
| 192 | struct gprs_ra_id ra_id; |
| 193 | |
| 194 | memset(ra_buf, 0, sizeof(ra_buf)); |
| 195 | memcpy(ra_buf, cur, 3); |
| 196 | gsm48_parse_ra(&ra_id, ra_buf); |
| 197 | |
| 198 | elem = element_alloc(dd, "PLMN", ELEM_T_GROUP, ELEM_REPR_NONE); |
| 199 | |
| 200 | mcc = element_alloc_sub(elem, "MCC", ELEM_T_UINT16, ELEM_REPR_DEC); |
| 201 | mcc->u.u16 = ra_id.mcc; |
| 202 | |
| 203 | mnc = element_alloc_sub(elem, "MNC", ELEM_T_UINT16, ELEM_REPR_DEC); |
| 204 | mnc->u.u16 = ra_id.mnc; |
| 205 | } |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | /* 10.3.5 */ |
| 211 | int gsm_hpplmn_decode(struct osim_decoded_data *dd, |
| 212 | const struct osim_file_desc *desc, |
| 213 | int len, uint8_t *data) |
| 214 | { |
| 215 | struct osim_decoded_element *elem; |
| 216 | |
| 217 | elem = element_alloc(dd, "Time interval", ELEM_T_UINT8, ELEM_REPR_DEC); |
| 218 | elem->u.u8 = *data; |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /* Chapter 10.2.x */ |
| 224 | static const struct osim_file_desc sim_ef_in_mf[] = { |
| 225 | EF_TRANSP(0x2FE2, "EF.ICCID", 0, |
| 226 | "ICC Identification", &iccid_decode, NULL), |
| 227 | EF_TRANSP(0x2F05, "EF.ELP", F_OPTIONAL, |
| 228 | "Extended language preference", &elp_decode, NULL), |
| 229 | }; |
| 230 | |
| 231 | /* Chapter 10.3.x */ |
| 232 | static const struct osim_file_desc sim_ef_in_gsm[] = { |
| 233 | EF_TRANSP(0x6F05, "EF.LP", 0, |
| 234 | "Language preference", &gsm_lp_decode, NULL), |
| 235 | EF_TRANSP(0x6F07, "EF.IMSI", 0, |
| 236 | "IMSI", &gsm_imsi_decode, NULL), |
| 237 | EF_TRANSP(0x6F20, "EF.Kc", 0, |
| 238 | "Ciphering key Kc", &gsm_kc_decode, NULL), |
| 239 | EF_TRANSP(0x6F30, "EF.PLMNsel", F_OPTIONAL, |
| 240 | "PLMN selector", &gsm_plmnsel_decode, NULL), |
| 241 | EF_TRANSP(0x6F31, "EF.HPPLMN", 0, |
| 242 | "Higher Priority PLMN search period", &gsm_hpplmn_decode, NULL), |
| 243 | EF_TRANSP_N(0x6F37, "EF.ACMmax", F_OPTIONAL, |
| 244 | "ACM maximum value"), |
| 245 | EF_TRANSP_N(0x6F38, "EF.SST", 0, |
| 246 | "SIM service table"), |
| 247 | EF_CYCLIC_N(0x6F39, "EF.ACM", F_OPTIONAL, |
| 248 | "Accumulated call meter"), |
| 249 | EF_TRANSP_N(0x6F3E, "EF.GID1", F_OPTIONAL, |
| 250 | "Group Identifier Level 1"), |
| 251 | EF_TRANSP_N(0x6F3F, "EF.GID2", F_OPTIONAL, |
| 252 | "Group Identifier Level 2"), |
| 253 | EF_TRANSP_N(0x6F46, "EF.SPN", F_OPTIONAL, |
| 254 | "Service Provider Name"), |
| 255 | EF_TRANSP_N(0x6F41, "EF.PUCT", F_OPTIONAL, |
| 256 | "Price per unit and currency table"), |
| 257 | EF_TRANSP_N(0x6F45, "EF.CBMI", F_OPTIONAL, |
| 258 | "Cell broadcast massage identifier selection"), |
| 259 | EF_TRANSP_N(0x6F74, "EF.BCCH", 0, |
| 260 | "Broadcast control channels"), |
| 261 | EF_TRANSP_N(0x6F78, "EF.ACC", 0, |
| 262 | "Access control class"), |
| 263 | EF_TRANSP_N(0x6F7B, "EF.FPLMN", 0, |
| 264 | "Forbidden PLMNs"), |
| 265 | EF_TRANSP_N(0x6F7E, "EF.LOCI", 0, |
| 266 | "Location information"), |
| 267 | EF_TRANSP_N(0x6FAD, "EF.AD", 0, |
| 268 | "Administrative data"), |
| 269 | EF_TRANSP_N(0x6FAE, "EF.Phase", 0, |
| 270 | "Phase identification"), |
| 271 | EF_TRANSP_N(0x6FB1, "EF.VGCS", F_OPTIONAL, |
| 272 | "Voice Group Call Service"), |
| 273 | EF_TRANSP_N(0x6FB2, "EF.VGCSS", F_OPTIONAL, |
| 274 | "Voice Group Call Service Status"), |
| 275 | EF_TRANSP_N(0x6FB3, "EF.VBS", F_OPTIONAL, |
| 276 | "Voice Broadcast Service"), |
| 277 | EF_TRANSP_N(0x6FB4, "EF.VBSS", F_OPTIONAL, |
| 278 | "Voice Broadcast Service Status"), |
| 279 | EF_TRANSP_N(0x6FB5, "EF.eMLPP", F_OPTIONAL, |
| 280 | "enhanced Mult Level Pre-emption and Priority"), |
| 281 | EF_TRANSP_N(0x6FB6, "EF.AAeM", F_OPTIONAL, |
| 282 | "Automatic Answer for eMLPP Service"), |
| 283 | EF_TRANSP_N(0x6F48, "EF.CBMID", F_OPTIONAL, |
| 284 | "Cell Broadcast Message Identifier for Data Download"), |
| 285 | EF_TRANSP_N(0x6FB7, "EF.ECC", F_OPTIONAL, |
| 286 | "Emergency Call Code"), |
| 287 | EF_TRANSP_N(0x6F50, "EF.CBMIR", F_OPTIONAL, |
| 288 | "Cell broadcast message identifier range selection"), |
| 289 | EF_TRANSP_N(0x6F2C, "EF.DCK", F_OPTIONAL, |
| 290 | "De-personalization Control Keys"), |
| 291 | EF_TRANSP_N(0x6F32, "EF.CNL", F_OPTIONAL, |
| 292 | "Co-operative Network List"), |
| 293 | EF_LIN_FIX_N(0x6F51, "EF.NIA", F_OPTIONAL, |
| 294 | "Network's Indication of Alerting"), |
| 295 | EF_TRANSP_N(0x6F52, "EF.KcGPRS", F_OPTIONAL, |
| 296 | "GPRS Ciphering key KcGPRS"), |
| 297 | EF_TRANSP_N(0x6F53, "EF.LOCIGPRS", F_OPTIONAL, |
| 298 | "GPRS location information"), |
| 299 | EF_TRANSP_N(0x6F54, "EF.SUME", F_OPTIONAL, |
| 300 | "SetUpMenu Elements"), |
| 301 | EF_TRANSP_N(0x6F60, "EF.PLMNwAcT", F_OPTIONAL, |
| 302 | "User controlled PLMN Selector with Access Technology"), |
| 303 | EF_TRANSP_N(0x6F61, "EF.OPLMNwAcT", F_OPTIONAL, |
| 304 | "Operator controlled PLMN Selector with Access Technology"), |
| 305 | EF_TRANSP_N(0x6F62, "EF.HPLMNwAcT", F_OPTIONAL, |
| 306 | "HPLMN Selector with Access Technology"), |
| 307 | EF_TRANSP_N(0x6F63, "EF.CPBCCH", F_OPTIONAL, |
| 308 | "CPBCCH Information"), |
| 309 | EF_TRANSP_N(0x6F64, "EF.InvScan", F_OPTIONAL, |
| 310 | "Investigation Scan"), |
| 311 | }; |
| 312 | |
| 313 | /* 10.5. */ |
| 314 | static const struct osim_file_desc sim_ef_in_telecom[] = { |
| 315 | EF_LIN_FIX_N(0x6F3A, "EF.ADN", F_OPTIONAL, |
| 316 | "Abbreviated dialling numbers"), |
| 317 | EF_LIN_FIX_N(0x6F3B, "EF.FDN", F_OPTIONAL, |
| 318 | "Fixed dialling numbers"), |
| 319 | EF_LIN_FIX_N(0x6F3C, "EF.SMS", F_OPTIONAL, |
| 320 | "Short messages"), |
| 321 | EF_LIN_FIX_N(0x6F3D, "EF.CCP", F_OPTIONAL, |
| 322 | "Capability configuration parameters"), |
| 323 | EF_LIN_FIX_N(0x6F4F, "EF.ECCP", F_OPTIONAL, |
| 324 | "Extended Capability configuration parameters"), |
| 325 | EF_LIN_FIX_N(0x6F40, "EF.MSISDN", F_OPTIONAL, |
| 326 | "MSISDN"), |
| 327 | EF_LIN_FIX_N(0x6F42, "EF.SMSP", F_OPTIONAL, |
| 328 | "Short message service parameters"), |
| 329 | EF_TRANSP_N(0x6F43, "EF.SMSS", F_OPTIONAL, |
| 330 | "SMS Status"), |
| 331 | EF_CYCLIC_N(0x6F44, "EF.LND", F_OPTIONAL, |
| 332 | "Last number dialled"), |
| 333 | EF_LIN_FIX_N(0x6F4A, "EF.EXT1", F_OPTIONAL, |
| 334 | "Extension 1"), |
| 335 | EF_LIN_FIX_N(0x6F4B, "EF.EXT2", F_OPTIONAL, |
| 336 | "Extension 2"), |
| 337 | EF_LIN_FIX_N(0x6F4C, "EF.EXT3", F_OPTIONAL, |
| 338 | "Extension 3"), |
| 339 | EF_LIN_FIX_N(0x6F4D, "EF.BDN", F_OPTIONAL, |
| 340 | "Barred dialling numbers"), |
| 341 | EF_LIN_FIX_N(0x6F4E, "EF.EXT4", F_OPTIONAL, |
| 342 | "Extension 4"), |
| 343 | EF_LIN_FIX_N(0x6F47, "EF.SMSR", F_OPTIONAL, |
| 344 | "Short message status reports"), |
| 345 | EF_LIN_FIX_N(0x6F58, "EF.CMI", F_OPTIONAL, |
| 346 | "Comparison Method Information"), |
| 347 | }; |
| 348 | |
| 349 | |
| 350 | /* 10.6. */ |
| 351 | static const struct osim_file_desc sim_ef_in_graphics[] = { |
| 352 | EF_LIN_FIX_N(0x4F20, "EF.IMG", F_OPTIONAL, |
| 353 | "Image"), |
| 354 | }; |
| 355 | |
| 356 | struct osim_card_profile *osim_cprof_sim(void *ctx) |
| 357 | { |
| 358 | struct osim_card_profile *cprof; |
| 359 | struct osim_file_desc *mf, *gsm, *tc; |
| 360 | |
| 361 | cprof = talloc_zero(ctx, struct osim_card_profile); |
| 362 | cprof->name = "GSM SIM"; |
| 363 | cprof->sws = sim_card_sws; |
| 364 | |
| 365 | mf = alloc_df(cprof, 0x3f00, "MF"); |
| 366 | |
| 367 | cprof->mf = mf; |
| 368 | |
| 369 | add_filedesc(mf, sim_ef_in_mf, ARRAY_SIZE(sim_ef_in_mf)); |
| 370 | gsm = add_df_with_ef(mf, 0x7F20, "DF.GSM", sim_ef_in_gsm, |
| 371 | ARRAY_SIZE(sim_ef_in_gsm)); |
| 372 | add_df_with_ef(gsm, 0x5F30, "DF.IRIDIUM", NULL, 0); |
| 373 | add_df_with_ef(gsm, 0x5F31, "DF.GLOBST", NULL, 0); |
| 374 | add_df_with_ef(gsm, 0x5F32, "DF.ICO", NULL, 0); |
| 375 | add_df_with_ef(gsm, 0x5F33, "DF.ACeS", NULL, 0); |
| 376 | add_df_with_ef(gsm, 0x5F40, "DF.ACeS", NULL, 0); |
| 377 | add_df_with_ef(gsm, 0x5F60, "DF.CTS", NULL, 0); |
| 378 | add_df_with_ef(gsm, 0x5F70, "DF.SoLSA", NULL, 0); |
| 379 | tc = add_df_with_ef(mf, 0x7F10, "DF.TELECOM", sim_ef_in_telecom, |
| 380 | ARRAY_SIZE(sim_ef_in_telecom)); |
| 381 | add_df_with_ef(tc, 0x5F50, "DF.GRAPHICS", sim_ef_in_graphics, |
| 382 | ARRAY_SIZE(sim_ef_in_graphics)); |
| 383 | |
| 384 | return cprof; |
| 385 | } |