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 * |
| 175 | osim_file_find_name(struct osim_file_desc *parent, const char *name) |
| 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 | |
Kevin Redon | 43eabee | 2012-09-16 18:40:02 +0200 | [diff] [blame] | 186 | /*! \brief Generate an APDU message and initialize APDU command header |
| 187 | * \param[in] cla CLASS byte |
| 188 | * \param[in] ins INSTRUCTION byte |
| 189 | * \param[in] p1 Parameter 1 byte |
| 190 | * \param[in] p2 Parameter 2 byte |
| 191 | * \param[in] lc number of bytes in the command data field Nc, which will encoded in 0, 1 or 3 bytes into Lc |
| 192 | * \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 |
| 193 | * \returns an APDU message generated using provided APDU parameters |
| 194 | * |
| 195 | * This function generates an APDU message, as defined in ISO/IEC 7816-4:2005(E) ยง5.1. |
| 196 | * The APDU command header, command and response fields lengths are initialized using the parameters. |
| 197 | * The APDU case is determined by the command and response fields lengths. |
Kevin Redon | 0f0ee32 | 2012-09-11 11:40:41 +0200 | [diff] [blame] | 198 | */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 199 | struct msgb *osim_new_apdumsg(uint8_t cla, uint8_t ins, uint8_t p1, |
| 200 | uint8_t p2, uint16_t lc, uint16_t le) |
| 201 | { |
| 202 | struct osim_apdu_cmd_hdr *ch; |
| 203 | struct msgb *msg = msgb_alloc(lc+le+sizeof(*ch)+2, "APDU"); |
| 204 | if (!msg) |
| 205 | return NULL; |
| 206 | |
| 207 | ch = (struct osim_apdu_cmd_hdr *) msgb_put(msg, sizeof(*ch)); |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 208 | msg->l2h = (uint8_t *) ch; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 209 | |
| 210 | ch->cla = cla; |
| 211 | ch->ins = ins; |
| 212 | ch->p1 = p1; |
| 213 | ch->p2 = p2; |
| 214 | |
| 215 | msgb_apdu_lc(msg) = lc; |
| 216 | msgb_apdu_le(msg) = le; |
| 217 | |
| 218 | if (lc == 0 && le == 0) |
| 219 | msgb_apdu_case(msg) = APDU_CASE_1; |
| 220 | else if (lc == 0 && le >= 1) { |
| 221 | if (le <= 256) |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 222 | msgb_apdu_case(msg) = APDU_CASE_2S; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 223 | else |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 224 | msgb_apdu_case(msg) = APDU_CASE_2E; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 225 | } else if (le == 0 && lc >= 1) { |
| 226 | if (lc <= 255) |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 227 | msgb_apdu_case(msg) = APDU_CASE_3S; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 228 | else |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 229 | msgb_apdu_case(msg) = APDU_CASE_3E; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 230 | } else if (lc >= 1 && le >= 1) { |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 231 | if (lc <= 255 && le <= 256) |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 232 | msgb_apdu_case(msg) = APDU_CASE_4S; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 233 | else |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 234 | msgb_apdu_case(msg) = APDU_CASE_4E; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | return msg; |
| 238 | } |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 239 | |
| 240 | /* FIXME: do we want to mark this as __thread? */ |
| 241 | static char sw_print_buf[256]; |
| 242 | |
| 243 | char *osim_print_sw(const struct osim_card_hdl *ch, uint16_t sw_in) |
| 244 | { |
| 245 | const struct osim_card_sw *csw; |
| 246 | |
| 247 | if (!ch || !ch->prof) |
| 248 | goto ret_def; |
| 249 | |
| 250 | csw = osim_find_sw(ch->prof, sw_in); |
| 251 | if (!csw) |
| 252 | goto ret_def; |
| 253 | |
| 254 | switch (csw->type) { |
| 255 | case SW_TYPE_STR: |
| 256 | snprintf(sw_print_buf, sizeof(sw_print_buf), |
| 257 | "%04x (%s)", sw_in, csw->u.str); |
| 258 | break; |
| 259 | default: |
| 260 | goto ret_def; |
| 261 | } |
| 262 | |
| 263 | sw_print_buf[sizeof(sw_print_buf)-1] = '\0'; |
| 264 | |
| 265 | return sw_print_buf; |
| 266 | |
| 267 | ret_def: |
| 268 | snprintf(sw_print_buf, sizeof(sw_print_buf), |
| 269 | "%04x (Unknown)", sw_in); |
| 270 | sw_print_buf[sizeof(sw_print_buf)-1] = '\0'; |
| 271 | |
| 272 | return sw_print_buf; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | const struct osim_card_sw *osim_find_sw(const struct osim_card_profile *cp, |
| 277 | uint16_t sw_in) |
| 278 | { |
| 279 | const struct osim_card_sw **sw_lists = cp->sws; |
| 280 | const struct osim_card_sw *sw_list, *sw; |
| 281 | |
| 282 | for (sw_list = *sw_lists++; sw_list != NULL; sw = sw_list = *sw_lists++) { |
| 283 | for (sw = sw_list; sw->code != 0 && sw->mask != 0; sw++) { |
| 284 | if ((sw_in & sw->mask) == sw->code) |
| 285 | return sw; |
| 286 | } |
| 287 | } |
| 288 | return NULL; |
| 289 | } |
Harald Welte | d83d296 | 2013-03-04 17:52:33 +0000 | [diff] [blame] | 290 | |
| 291 | enum osim_card_sw_class osim_sw_class(const struct osim_card_profile *cp, |
| 292 | uint16_t sw_in) |
| 293 | { |
| 294 | const struct osim_card_sw *csw = osim_find_sw(cp, sw_in); |
| 295 | |
| 296 | if (!csw) |
| 297 | return SW_CLS_NONE; |
| 298 | |
| 299 | return csw->class; |
| 300 | } |
Harald Welte | 30115db | 2014-05-04 16:30:46 +0200 | [diff] [blame] | 301 | |
| 302 | int default_decode(struct osim_decoded_data *dd, |
| 303 | const struct osim_file_desc *desc, |
| 304 | int len, uint8_t *data) |
| 305 | { |
| 306 | struct osim_decoded_element *elem; |
| 307 | |
| 308 | elem = element_alloc(dd, "Unknown Payload", ELEM_T_BYTES, ELEM_REPR_HEX); |
| 309 | elem->u.buf = talloc_memdup(elem, data, len); |
| 310 | |
| 311 | return 0; |
| 312 | } |