Harald Welte | ad41863 | 2012-09-10 10:49:59 +0200 | [diff] [blame] | 1 | /* Core routines for SIM/UICC/USIM access */ |
| 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 | */ |
| 22 | |
| 23 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <stdint.h> |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 26 | #include <string.h> |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 27 | |
| 28 | #include <osmocom/core/talloc.h> |
| 29 | #include <osmocom/sim/sim.h> |
| 30 | |
Harald Welte | ad41863 | 2012-09-10 10:49:59 +0200 | [diff] [blame] | 31 | struct osim_decoded_data *osim_file_decode(struct osim_file *file, |
| 32 | int len, uint8_t *data) |
| 33 | { |
| 34 | struct osim_decoded_data *dd; |
| 35 | |
| 36 | if (!file->desc->ops.parse) |
| 37 | return NULL; |
| 38 | |
| 39 | dd = talloc_zero(file, struct osim_decoded_data); |
Harald Welte | db2b52e | 2014-10-26 19:04:41 +0100 | [diff] [blame] | 40 | if (!dd) |
| 41 | return NULL; |
Harald Welte | ad41863 | 2012-09-10 10:49:59 +0200 | [diff] [blame] | 42 | dd->file = file; |
| 43 | |
| 44 | if (file->desc->ops.parse(dd, file->desc, len, data) < 0) { |
| 45 | talloc_free(dd); |
| 46 | return NULL; |
| 47 | } else |
| 48 | return dd; |
| 49 | } |
| 50 | |
| 51 | struct msgb *osim_file_encode(const struct osim_file_desc *desc, |
| 52 | const struct osim_decoded_data *data) |
| 53 | { |
| 54 | if (!desc->ops.encode) |
| 55 | return NULL; |
| 56 | |
| 57 | return desc->ops.encode(desc, data); |
| 58 | } |
| 59 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 60 | static struct osim_decoded_element * |
| 61 | __element_alloc(void *ctx, const char *name, enum osim_element_type type, |
| 62 | enum osim_element_repr repr) |
| 63 | { |
| 64 | struct osim_decoded_element *elem; |
| 65 | |
| 66 | elem = talloc_zero(ctx, struct osim_decoded_element); |
| 67 | if (!elem) |
| 68 | return NULL; |
| 69 | elem->name = name; |
| 70 | elem->type = type; |
| 71 | elem->representation = repr; |
| 72 | |
| 73 | if (elem->type == ELEM_T_GROUP) |
| 74 | INIT_LLIST_HEAD(&elem->u.siblings); |
| 75 | |
| 76 | return elem; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | struct osim_decoded_element * |
| 81 | element_alloc(struct osim_decoded_data *dd, const char *name, |
| 82 | enum osim_element_type type, enum osim_element_repr repr) |
| 83 | { |
| 84 | struct osim_decoded_element *elem; |
| 85 | |
| 86 | elem = __element_alloc(dd, name, type, repr); |
| 87 | if (!elem) |
| 88 | return NULL; |
| 89 | |
| 90 | llist_add_tail(&elem->list, &dd->decoded_elements); |
| 91 | |
| 92 | return elem; |
| 93 | } |
| 94 | |
| 95 | struct osim_decoded_element * |
| 96 | element_alloc_sub(struct osim_decoded_element *ee, const char *name, |
| 97 | enum osim_element_type type, enum osim_element_repr repr) |
| 98 | { |
| 99 | struct osim_decoded_element *elem; |
| 100 | |
| 101 | elem = __element_alloc(ee, name, type, repr); |
| 102 | if (!elem) |
| 103 | return NULL; |
| 104 | |
| 105 | llist_add(&elem->list, &ee->u.siblings); |
| 106 | |
| 107 | return elem; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | void add_filedesc(struct osim_file_desc *root, const struct osim_file_desc *in, int num) |
| 112 | { |
| 113 | int i; |
| 114 | |
| 115 | for (i = 0; i < num; i++) { |
| 116 | struct osim_file_desc *ofd = talloc_memdup(root, &in[i], sizeof(*in)); |
| 117 | llist_add_tail(&ofd->list, &root->child_list); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | struct osim_file_desc *alloc_df(void *ctx, uint16_t fid, const char *name) |
| 122 | { |
| 123 | struct osim_file_desc *mf; |
| 124 | |
| 125 | mf = talloc_zero(ctx, struct osim_file_desc); |
Harald Welte | db2b52e | 2014-10-26 19:04:41 +0100 | [diff] [blame] | 126 | if (!mf) |
| 127 | return NULL; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 128 | mf->type = TYPE_DF; |
| 129 | mf->fid = fid; |
| 130 | mf->short_name = name; |
| 131 | INIT_LLIST_HEAD(&mf->child_list); |
| 132 | |
| 133 | return mf; |
| 134 | } |
| 135 | |
| 136 | struct osim_file_desc * |
| 137 | add_df_with_ef(struct osim_file_desc *parent, |
| 138 | uint16_t fid, const char *name, |
| 139 | const struct osim_file_desc *in, int num) |
| 140 | { |
| 141 | struct osim_file_desc *df; |
| 142 | |
| 143 | df = alloc_df(parent, fid, name); |
Harald Welte | db2b52e | 2014-10-26 19:04:41 +0100 | [diff] [blame] | 144 | if (!df) |
| 145 | return NULL; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 146 | df->parent = parent; |
| 147 | llist_add_tail(&df->list, &parent->child_list); |
| 148 | add_filedesc(df, in, num); |
| 149 | |
| 150 | return df; |
| 151 | } |
| 152 | |
| 153 | struct osim_file_desc * |
| 154 | add_adf_with_ef(struct osim_file_desc *parent, |
| 155 | const uint8_t *adf_name, uint8_t adf_name_len, |
| 156 | const char *name, const struct osim_file_desc *in, |
| 157 | int num) |
| 158 | { |
| 159 | struct osim_file_desc *df; |
| 160 | |
| 161 | df = alloc_df(parent, 0xffff, name); |
Harald Welte | db2b52e | 2014-10-26 19:04:41 +0100 | [diff] [blame] | 162 | if (!df) |
| 163 | return NULL; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 164 | df->type = TYPE_ADF; |
| 165 | df->df_name = adf_name; |
| 166 | df->df_name_len = adf_name_len; |
| 167 | df->parent = parent; |
| 168 | llist_add_tail(&df->list, &parent->child_list); |
| 169 | add_filedesc(df, in, num); |
| 170 | |
| 171 | return df; |
| 172 | } |
| 173 | |
| 174 | struct osim_file_desc * |
Harald Welte | 5ffb503 | 2016-03-11 09:40:56 +0700 | [diff] [blame] | 175 | osim_file_desc_find_name(struct osim_file_desc *parent, const char *name) |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 176 | { |
| 177 | struct osim_file_desc *ofd; |
| 178 | llist_for_each_entry(ofd, &parent->child_list, list) { |
| 179 | if (!strcmp(ofd->short_name, name)) { |
| 180 | return ofd; |
| 181 | } |
| 182 | } |
| 183 | return NULL; |
| 184 | } |
| 185 | |
Harald Welte | c28f4cd | 2016-03-11 09:35:07 +0700 | [diff] [blame] | 186 | struct osim_file_desc * |
Harald Welte | 5ffb503 | 2016-03-11 09:40:56 +0700 | [diff] [blame] | 187 | osim_file_desc_find_fid(struct osim_file_desc *parent, uint16_t fid) |
Harald Welte | c28f4cd | 2016-03-11 09:35:07 +0700 | [diff] [blame] | 188 | { |
| 189 | struct osim_file_desc *ofd; |
| 190 | llist_for_each_entry(ofd, &parent->child_list, list) { |
| 191 | if (ofd->fid == fid) { |
| 192 | return ofd; |
| 193 | } |
| 194 | } |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | struct osim_file_desc * |
Harald Welte | 5ffb503 | 2016-03-11 09:40:56 +0700 | [diff] [blame] | 199 | osim_file_desc_find_sfid(struct osim_file_desc *parent, uint8_t sfid) |
Harald Welte | c28f4cd | 2016-03-11 09:35:07 +0700 | [diff] [blame] | 200 | { |
| 201 | struct osim_file_desc *ofd; |
| 202 | llist_for_each_entry(ofd, &parent->child_list, list) { |
Harald Welte | 5ffb503 | 2016-03-11 09:40:56 +0700 | [diff] [blame] | 203 | if (ofd->sfid == SFI_NONE) |
| 204 | continue; |
Harald Welte | c28f4cd | 2016-03-11 09:35:07 +0700 | [diff] [blame] | 205 | if (ofd->sfid == sfid) { |
| 206 | return ofd; |
| 207 | } |
| 208 | } |
| 209 | return NULL; |
| 210 | } |
| 211 | |
| 212 | |
Kevin Redon | 43eabee | 2012-09-16 18:40:02 +0200 | [diff] [blame] | 213 | /*! \brief Generate an APDU message and initialize APDU command header |
| 214 | * \param[in] cla CLASS byte |
| 215 | * \param[in] ins INSTRUCTION byte |
| 216 | * \param[in] p1 Parameter 1 byte |
| 217 | * \param[in] p2 Parameter 2 byte |
| 218 | * \param[in] lc number of bytes in the command data field Nc, which will encoded in 0, 1 or 3 bytes into Lc |
| 219 | * \param[in] le maximum number of bytes expected in the response data field, which will encoded in 0, 1, 2 or 3 bytes into Le |
| 220 | * \returns an APDU message generated using provided APDU parameters |
| 221 | * |
| 222 | * This function generates an APDU message, as defined in ISO/IEC 7816-4:2005(E) ยง5.1. |
| 223 | * The APDU command header, command and response fields lengths are initialized using the parameters. |
| 224 | * The APDU case is determined by the command and response fields lengths. |
Kevin Redon | 0f0ee32 | 2012-09-11 11:40:41 +0200 | [diff] [blame] | 225 | */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 226 | struct msgb *osim_new_apdumsg(uint8_t cla, uint8_t ins, uint8_t p1, |
| 227 | uint8_t p2, uint16_t lc, uint16_t le) |
| 228 | { |
| 229 | struct osim_apdu_cmd_hdr *ch; |
| 230 | struct msgb *msg = msgb_alloc(lc+le+sizeof(*ch)+2, "APDU"); |
| 231 | if (!msg) |
| 232 | return NULL; |
| 233 | |
| 234 | ch = (struct osim_apdu_cmd_hdr *) msgb_put(msg, sizeof(*ch)); |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 235 | msg->l2h = (uint8_t *) ch; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 236 | |
| 237 | ch->cla = cla; |
| 238 | ch->ins = ins; |
| 239 | ch->p1 = p1; |
| 240 | ch->p2 = p2; |
| 241 | |
| 242 | msgb_apdu_lc(msg) = lc; |
| 243 | msgb_apdu_le(msg) = le; |
| 244 | |
| 245 | if (lc == 0 && le == 0) |
| 246 | msgb_apdu_case(msg) = APDU_CASE_1; |
| 247 | else if (lc == 0 && le >= 1) { |
| 248 | if (le <= 256) |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 249 | msgb_apdu_case(msg) = APDU_CASE_2S; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 250 | else |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 251 | msgb_apdu_case(msg) = APDU_CASE_2E; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 252 | } else if (le == 0 && lc >= 1) { |
| 253 | if (lc <= 255) |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 254 | msgb_apdu_case(msg) = APDU_CASE_3S; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 255 | else |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 256 | msgb_apdu_case(msg) = APDU_CASE_3E; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 257 | } else if (lc >= 1 && le >= 1) { |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 258 | if (lc <= 255 && le <= 256) |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 259 | msgb_apdu_case(msg) = APDU_CASE_4S; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 260 | else |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 261 | msgb_apdu_case(msg) = APDU_CASE_4E; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | return msg; |
| 265 | } |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 266 | |
| 267 | /* FIXME: do we want to mark this as __thread? */ |
| 268 | static char sw_print_buf[256]; |
| 269 | |
| 270 | char *osim_print_sw(const struct osim_card_hdl *ch, uint16_t sw_in) |
| 271 | { |
| 272 | const struct osim_card_sw *csw; |
| 273 | |
| 274 | if (!ch || !ch->prof) |
| 275 | goto ret_def; |
| 276 | |
| 277 | csw = osim_find_sw(ch->prof, sw_in); |
| 278 | if (!csw) |
| 279 | goto ret_def; |
| 280 | |
| 281 | switch (csw->type) { |
| 282 | case SW_TYPE_STR: |
| 283 | snprintf(sw_print_buf, sizeof(sw_print_buf), |
| 284 | "%04x (%s)", sw_in, csw->u.str); |
| 285 | break; |
| 286 | default: |
| 287 | goto ret_def; |
| 288 | } |
| 289 | |
| 290 | sw_print_buf[sizeof(sw_print_buf)-1] = '\0'; |
| 291 | |
| 292 | return sw_print_buf; |
| 293 | |
| 294 | ret_def: |
| 295 | snprintf(sw_print_buf, sizeof(sw_print_buf), |
| 296 | "%04x (Unknown)", sw_in); |
| 297 | sw_print_buf[sizeof(sw_print_buf)-1] = '\0'; |
| 298 | |
| 299 | return sw_print_buf; |
| 300 | } |
| 301 | |
| 302 | |
| 303 | const struct osim_card_sw *osim_find_sw(const struct osim_card_profile *cp, |
| 304 | uint16_t sw_in) |
| 305 | { |
| 306 | const struct osim_card_sw **sw_lists = cp->sws; |
| 307 | const struct osim_card_sw *sw_list, *sw; |
| 308 | |
| 309 | for (sw_list = *sw_lists++; sw_list != NULL; sw = sw_list = *sw_lists++) { |
| 310 | for (sw = sw_list; sw->code != 0 && sw->mask != 0; sw++) { |
| 311 | if ((sw_in & sw->mask) == sw->code) |
| 312 | return sw; |
| 313 | } |
| 314 | } |
| 315 | return NULL; |
| 316 | } |
Harald Welte | d83d296 | 2013-03-04 17:52:33 +0000 | [diff] [blame] | 317 | |
| 318 | enum osim_card_sw_class osim_sw_class(const struct osim_card_profile *cp, |
| 319 | uint16_t sw_in) |
| 320 | { |
| 321 | const struct osim_card_sw *csw = osim_find_sw(cp, sw_in); |
| 322 | |
| 323 | if (!csw) |
| 324 | return SW_CLS_NONE; |
| 325 | |
| 326 | return csw->class; |
| 327 | } |
Harald Welte | 30115db | 2014-05-04 16:30:46 +0200 | [diff] [blame] | 328 | |
| 329 | int default_decode(struct osim_decoded_data *dd, |
| 330 | const struct osim_file_desc *desc, |
| 331 | int len, uint8_t *data) |
| 332 | { |
| 333 | struct osim_decoded_element *elem; |
| 334 | |
| 335 | elem = element_alloc(dd, "Unknown Payload", ELEM_T_BYTES, ELEM_REPR_HEX); |
| 336 | elem->u.buf = talloc_memdup(elem, data, len); |
| 337 | |
| 338 | return 0; |
| 339 | } |