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