Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file core.c |
| 2 | * Core routines for SIM/UICC/USIM access. */ |
Harald Welte | ad41863 | 2012-09-10 10:49:59 +0200 | [diff] [blame] | 3 | /* |
Harald Welte | 429adec | 2020-03-20 13:05:40 +0100 | [diff] [blame] | 4 | * (C) 2012-2020 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | ad41863 | 2012-09-10 10:49:59 +0200 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
Harald Welte | ad41863 | 2012-09-10 10:49:59 +0200 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
Harald Welte | ad41863 | 2012-09-10 10:49:59 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <stdint.h> |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 25 | #include <string.h> |
Harald Welte | 429adec | 2020-03-20 13:05:40 +0100 | [diff] [blame] | 26 | #include <errno.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 | 429adec | 2020-03-20 13:05:40 +0100 | [diff] [blame] | 31 | #include "sim_int.h" |
| 32 | |
Harald Welte | ad41863 | 2012-09-10 10:49:59 +0200 | [diff] [blame] | 33 | struct osim_decoded_data *osim_file_decode(struct osim_file *file, |
| 34 | int len, uint8_t *data) |
| 35 | { |
| 36 | struct osim_decoded_data *dd; |
| 37 | |
| 38 | if (!file->desc->ops.parse) |
| 39 | return NULL; |
| 40 | |
| 41 | dd = talloc_zero(file, struct osim_decoded_data); |
Harald Welte | db2b52e | 2014-10-26 19:04:41 +0100 | [diff] [blame] | 42 | if (!dd) |
| 43 | return NULL; |
Harald Welte | ad41863 | 2012-09-10 10:49:59 +0200 | [diff] [blame] | 44 | dd->file = file; |
| 45 | |
| 46 | if (file->desc->ops.parse(dd, file->desc, len, data) < 0) { |
| 47 | talloc_free(dd); |
| 48 | return NULL; |
| 49 | } else |
| 50 | return dd; |
| 51 | } |
| 52 | |
| 53 | struct msgb *osim_file_encode(const struct osim_file_desc *desc, |
| 54 | const struct osim_decoded_data *data) |
| 55 | { |
| 56 | if (!desc->ops.encode) |
| 57 | return NULL; |
| 58 | |
| 59 | return desc->ops.encode(desc, data); |
| 60 | } |
| 61 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 62 | static struct osim_decoded_element * |
| 63 | __element_alloc(void *ctx, const char *name, enum osim_element_type type, |
| 64 | enum osim_element_repr repr) |
| 65 | { |
| 66 | struct osim_decoded_element *elem; |
| 67 | |
| 68 | elem = talloc_zero(ctx, struct osim_decoded_element); |
| 69 | if (!elem) |
| 70 | return NULL; |
| 71 | elem->name = name; |
| 72 | elem->type = type; |
| 73 | elem->representation = repr; |
| 74 | |
| 75 | if (elem->type == ELEM_T_GROUP) |
| 76 | INIT_LLIST_HEAD(&elem->u.siblings); |
| 77 | |
| 78 | return elem; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | struct osim_decoded_element * |
| 83 | element_alloc(struct osim_decoded_data *dd, const char *name, |
| 84 | enum osim_element_type type, enum osim_element_repr repr) |
| 85 | { |
| 86 | struct osim_decoded_element *elem; |
| 87 | |
| 88 | elem = __element_alloc(dd, name, type, repr); |
| 89 | if (!elem) |
| 90 | return NULL; |
| 91 | |
| 92 | llist_add_tail(&elem->list, &dd->decoded_elements); |
| 93 | |
| 94 | return elem; |
| 95 | } |
| 96 | |
| 97 | struct osim_decoded_element * |
| 98 | element_alloc_sub(struct osim_decoded_element *ee, const char *name, |
| 99 | enum osim_element_type type, enum osim_element_repr repr) |
| 100 | { |
| 101 | struct osim_decoded_element *elem; |
| 102 | |
| 103 | elem = __element_alloc(ee, name, type, repr); |
| 104 | if (!elem) |
| 105 | return NULL; |
| 106 | |
| 107 | llist_add(&elem->list, &ee->u.siblings); |
| 108 | |
| 109 | return elem; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | void add_filedesc(struct osim_file_desc *root, const struct osim_file_desc *in, int num) |
| 114 | { |
| 115 | int i; |
| 116 | |
| 117 | for (i = 0; i < num; i++) { |
| 118 | struct osim_file_desc *ofd = talloc_memdup(root, &in[i], sizeof(*in)); |
| 119 | llist_add_tail(&ofd->list, &root->child_list); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | struct osim_file_desc *alloc_df(void *ctx, uint16_t fid, const char *name) |
| 124 | { |
| 125 | struct osim_file_desc *mf; |
| 126 | |
| 127 | mf = talloc_zero(ctx, struct osim_file_desc); |
Harald Welte | db2b52e | 2014-10-26 19:04:41 +0100 | [diff] [blame] | 128 | if (!mf) |
| 129 | return NULL; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 130 | mf->type = TYPE_DF; |
| 131 | mf->fid = fid; |
| 132 | mf->short_name = name; |
| 133 | INIT_LLIST_HEAD(&mf->child_list); |
| 134 | |
| 135 | return mf; |
| 136 | } |
| 137 | |
| 138 | struct osim_file_desc * |
| 139 | add_df_with_ef(struct osim_file_desc *parent, |
| 140 | uint16_t fid, const char *name, |
| 141 | const struct osim_file_desc *in, int num) |
| 142 | { |
| 143 | struct osim_file_desc *df; |
| 144 | |
| 145 | df = alloc_df(parent, fid, name); |
Harald Welte | db2b52e | 2014-10-26 19:04:41 +0100 | [diff] [blame] | 146 | if (!df) |
| 147 | return NULL; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 148 | df->parent = parent; |
| 149 | llist_add_tail(&df->list, &parent->child_list); |
| 150 | add_filedesc(df, in, num); |
| 151 | |
| 152 | return df; |
| 153 | } |
| 154 | |
| 155 | struct osim_file_desc * |
Harald Welte | 429adec | 2020-03-20 13:05:40 +0100 | [diff] [blame] | 156 | alloc_adf_with_ef(void *ctx, |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 157 | const uint8_t *adf_name, uint8_t adf_name_len, |
| 158 | const char *name, const struct osim_file_desc *in, |
| 159 | int num) |
| 160 | { |
| 161 | struct osim_file_desc *df; |
| 162 | |
Harald Welte | 429adec | 2020-03-20 13:05:40 +0100 | [diff] [blame] | 163 | df = alloc_df(ctx, 0xffff, name); |
Harald Welte | db2b52e | 2014-10-26 19:04:41 +0100 | [diff] [blame] | 164 | if (!df) |
| 165 | return NULL; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 166 | df->type = TYPE_ADF; |
| 167 | df->df_name = adf_name; |
| 168 | df->df_name_len = adf_name_len; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 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 | bf90d74 | 2020-03-20 12:11:03 +0100 | [diff] [blame] | 187 | osim_file_desc_find_aid(struct osim_file_desc *parent, const uint8_t *aid, uint8_t aid_len) |
| 188 | { |
| 189 | struct osim_file_desc *ofd; |
| 190 | llist_for_each_entry(ofd, &parent->child_list, list) { |
| 191 | if (ofd->type != TYPE_ADF) |
| 192 | continue; |
| 193 | if (aid_len > ofd->df_name_len) |
| 194 | continue; |
| 195 | if (!memcmp(ofd->df_name, aid, aid_len)) { |
| 196 | return ofd; |
| 197 | } |
| 198 | } |
| 199 | return NULL; |
| 200 | } |
| 201 | |
| 202 | struct osim_file_desc * |
Harald Welte | 5ffb503 | 2016-03-11 09:40:56 +0700 | [diff] [blame] | 203 | 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] | 204 | { |
| 205 | struct osim_file_desc *ofd; |
| 206 | llist_for_each_entry(ofd, &parent->child_list, list) { |
| 207 | if (ofd->fid == fid) { |
| 208 | return ofd; |
| 209 | } |
| 210 | } |
| 211 | return NULL; |
| 212 | } |
| 213 | |
| 214 | struct osim_file_desc * |
Harald Welte | 5ffb503 | 2016-03-11 09:40:56 +0700 | [diff] [blame] | 215 | 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] | 216 | { |
| 217 | struct osim_file_desc *ofd; |
| 218 | llist_for_each_entry(ofd, &parent->child_list, list) { |
Harald Welte | 5ffb503 | 2016-03-11 09:40:56 +0700 | [diff] [blame] | 219 | if (ofd->sfid == SFI_NONE) |
| 220 | continue; |
Harald Welte | c28f4cd | 2016-03-11 09:35:07 +0700 | [diff] [blame] | 221 | if (ofd->sfid == sfid) { |
| 222 | return ofd; |
| 223 | } |
| 224 | } |
| 225 | return NULL; |
| 226 | } |
| 227 | |
| 228 | |
Harald Welte | 429adec | 2020-03-20 13:05:40 +0100 | [diff] [blame] | 229 | /*********************************************************************** |
| 230 | * Application Profiles + Applications |
| 231 | ***********************************************************************/ |
| 232 | |
| 233 | static LLIST_HEAD(g_app_profiles); |
| 234 | |
| 235 | /*! Register an application profile. Typically called at early start-up. */ |
| 236 | void osim_app_profile_register(struct osim_card_app_profile *aprof) |
| 237 | { |
| 238 | OSMO_ASSERT(!osim_app_profile_find_by_name(aprof->name)); |
| 239 | OSMO_ASSERT(!osim_app_profile_find_by_aid(aprof->aid, aprof->aid_len)); |
| 240 | llist_add_tail(&aprof->list, &g_app_profiles); |
| 241 | } |
| 242 | |
| 243 | /*! Find any registered application profile based on its name (e.g. "ADF.USIM") */ |
| 244 | const struct osim_card_app_profile * |
| 245 | osim_app_profile_find_by_name(const char *name) |
| 246 | { |
| 247 | struct osim_card_app_profile *ap; |
| 248 | |
| 249 | llist_for_each_entry(ap, &g_app_profiles, list) { |
| 250 | if (!strcmp(name, ap->name)) |
| 251 | return ap; |
| 252 | } |
| 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | /*! Find any registered application profile based on its AID */ |
| 257 | const struct osim_card_app_profile * |
| 258 | osim_app_profile_find_by_aid(const uint8_t *aid, uint8_t aid_len) |
| 259 | { |
| 260 | struct osim_card_app_profile *ap; |
| 261 | |
| 262 | llist_for_each_entry(ap, &g_app_profiles, list) { |
| 263 | if (ap->aid_len > aid_len) |
| 264 | continue; |
| 265 | if (!memcmp(ap->aid, aid, ap->aid_len)) |
| 266 | return ap; |
| 267 | } |
| 268 | return NULL; |
| 269 | } |
| 270 | |
| 271 | struct osim_card_app_hdl * |
| 272 | osim_card_hdl_find_app(struct osim_card_hdl *ch, const uint8_t *aid, uint8_t aid_len) |
| 273 | { |
| 274 | struct osim_card_app_hdl *ah; |
| 275 | |
| 276 | if (aid_len > MAX_AID_LEN) |
| 277 | return NULL; |
| 278 | |
| 279 | llist_for_each_entry(ah, &ch->apps, list) { |
| 280 | if (!memcmp(ah->aid, aid, aid_len)) |
| 281 | return ah; |
| 282 | } |
| 283 | return NULL; |
| 284 | } |
| 285 | |
| 286 | /*! Add an application to a given card */ |
| 287 | int osim_card_hdl_add_app(struct osim_card_hdl *ch, const uint8_t *aid, uint8_t aid_len, |
| 288 | const char *label) |
| 289 | { |
| 290 | struct osim_card_app_hdl *ah; |
| 291 | |
| 292 | if (aid_len > MAX_AID_LEN) |
| 293 | return -EINVAL; |
| 294 | |
| 295 | if (osim_card_hdl_find_app(ch, aid, aid_len)) |
| 296 | return -EEXIST; |
| 297 | |
| 298 | ah = talloc_zero(ch, struct osim_card_app_hdl); |
| 299 | if (!ah) |
| 300 | return -ENOMEM; |
| 301 | |
| 302 | memcpy(ah->aid, aid, aid_len); |
| 303 | ah->aid_len = aid_len; |
| 304 | ah->prof = osim_app_profile_find_by_aid(ah->aid, ah->aid_len); |
| 305 | if (label) |
| 306 | ah->label = talloc_strdup(ah, label); |
| 307 | llist_add_tail(&ah->list, &ch->apps); |
| 308 | return 0; |
| 309 | } |
| 310 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 311 | /*! Generate an APDU message and initialize APDU command header |
Kevin Redon | 43eabee | 2012-09-16 18:40:02 +0200 | [diff] [blame] | 312 | * \param[in] cla CLASS byte |
| 313 | * \param[in] ins INSTRUCTION byte |
| 314 | * \param[in] p1 Parameter 1 byte |
| 315 | * \param[in] p2 Parameter 2 byte |
| 316 | * \param[in] lc number of bytes in the command data field Nc, which will encoded in 0, 1 or 3 bytes into Lc |
| 317 | * \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 |
| 318 | * \returns an APDU message generated using provided APDU parameters |
| 319 | * |
| 320 | * This function generates an APDU message, as defined in ISO/IEC 7816-4:2005(E) §5.1. |
| 321 | * The APDU command header, command and response fields lengths are initialized using the parameters. |
| 322 | * The APDU case is determined by the command and response fields lengths. |
Kevin Redon | 0f0ee32 | 2012-09-11 11:40:41 +0200 | [diff] [blame] | 323 | */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 324 | struct msgb *osim_new_apdumsg(uint8_t cla, uint8_t ins, uint8_t p1, |
| 325 | uint8_t p2, uint16_t lc, uint16_t le) |
| 326 | { |
| 327 | struct osim_apdu_cmd_hdr *ch; |
| 328 | struct msgb *msg = msgb_alloc(lc+le+sizeof(*ch)+2, "APDU"); |
| 329 | if (!msg) |
| 330 | return NULL; |
| 331 | |
| 332 | ch = (struct osim_apdu_cmd_hdr *) msgb_put(msg, sizeof(*ch)); |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 333 | msg->l2h = (uint8_t *) ch; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 334 | |
| 335 | ch->cla = cla; |
| 336 | ch->ins = ins; |
| 337 | ch->p1 = p1; |
| 338 | ch->p2 = p2; |
| 339 | |
| 340 | msgb_apdu_lc(msg) = lc; |
| 341 | msgb_apdu_le(msg) = le; |
| 342 | |
| 343 | if (lc == 0 && le == 0) |
| 344 | msgb_apdu_case(msg) = APDU_CASE_1; |
| 345 | else if (lc == 0 && le >= 1) { |
| 346 | if (le <= 256) |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 347 | msgb_apdu_case(msg) = APDU_CASE_2S; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 348 | else |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 349 | msgb_apdu_case(msg) = APDU_CASE_2E; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 350 | } else if (le == 0 && lc >= 1) { |
| 351 | if (lc <= 255) |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 352 | msgb_apdu_case(msg) = APDU_CASE_3S; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 353 | else |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 354 | msgb_apdu_case(msg) = APDU_CASE_3E; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 355 | } else if (lc >= 1 && le >= 1) { |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 356 | if (lc <= 255 && le <= 256) |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 357 | msgb_apdu_case(msg) = APDU_CASE_4S; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 358 | else |
Kevin Redon | e07967f | 2012-09-11 11:44:18 +0200 | [diff] [blame] | 359 | msgb_apdu_case(msg) = APDU_CASE_4E; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | return msg; |
| 363 | } |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 364 | |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 365 | |
Harald Welte | 3a6bedf | 2020-03-22 10:30:10 +0100 | [diff] [blame] | 366 | char *osim_print_sw_buf(char *buf, size_t buf_len, const struct osim_chan_hdl *ch, uint16_t sw_in) |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 367 | { |
Harald Welte | 3a6bedf | 2020-03-22 10:30:10 +0100 | [diff] [blame] | 368 | const struct osim_card_sw *csw = NULL; |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 369 | |
Harald Welte | 3a6bedf | 2020-03-22 10:30:10 +0100 | [diff] [blame] | 370 | if (!ch) |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 371 | goto ret_def; |
| 372 | |
Harald Welte | 3a6bedf | 2020-03-22 10:30:10 +0100 | [diff] [blame] | 373 | if (ch->cur_app && ch->cur_app->prof) |
| 374 | csw = osim_app_profile_find_sw(ch->cur_app->prof, sw_in); |
| 375 | |
| 376 | if (!csw && ch->card->prof) |
| 377 | csw = osim_cprof_find_sw(ch->card->prof, sw_in); |
| 378 | |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 379 | if (!csw) |
| 380 | goto ret_def; |
| 381 | |
| 382 | switch (csw->type) { |
| 383 | case SW_TYPE_STR: |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 384 | snprintf(buf, buf_len, "%04x (%s)", sw_in, csw->u.str); |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 385 | break; |
| 386 | default: |
| 387 | goto ret_def; |
| 388 | } |
| 389 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 390 | buf[buf_len-1] = '\0'; |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 391 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 392 | return buf; |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 393 | |
| 394 | ret_def: |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 395 | snprintf(buf, buf_len, "%04x (Unknown)", sw_in); |
| 396 | buf[buf_len-1] = '\0'; |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 397 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 398 | return buf; |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 399 | } |
| 400 | |
Harald Welte | 3a6bedf | 2020-03-22 10:30:10 +0100 | [diff] [blame] | 401 | char *osim_print_sw(const struct osim_chan_hdl *ch, uint16_t sw_in) |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 402 | { |
Harald Welte | 171ef82 | 2019-03-28 10:49:05 +0100 | [diff] [blame] | 403 | static __thread char sw_print_buf[256]; |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 404 | return osim_print_sw_buf(sw_print_buf, sizeof(sw_print_buf), ch, sw_in); |
| 405 | } |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 406 | |
Harald Welte | 3a6bedf | 2020-03-22 10:30:10 +0100 | [diff] [blame] | 407 | char *osim_print_sw_c(const void *ctx, const struct osim_chan_hdl *ch, uint16_t sw_in) |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 408 | { |
| 409 | char *buf = talloc_size(ctx, 256); |
| 410 | if (!buf) |
| 411 | return NULL; |
| 412 | return osim_print_sw_buf(buf, 256, ch, sw_in); |
| 413 | } |
| 414 | |
Harald Welte | 3a6bedf | 2020-03-22 10:30:10 +0100 | [diff] [blame] | 415 | /*! Find status word within given card profile */ |
| 416 | const struct osim_card_sw *osim_cprof_find_sw(const struct osim_card_profile *cp, uint16_t sw_in) |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 417 | { |
| 418 | const struct osim_card_sw **sw_lists = cp->sws; |
| 419 | const struct osim_card_sw *sw_list, *sw; |
| 420 | |
Vadim Yanitskiy | d1c7323 | 2017-06-12 03:33:07 +0700 | [diff] [blame] | 421 | for (sw_list = *sw_lists++; sw_list != NULL; sw_list = *sw_lists++) { |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 422 | for (sw = sw_list; sw->code != 0 && sw->mask != 0; sw++) { |
| 423 | if ((sw_in & sw->mask) == sw->code) |
| 424 | return sw; |
| 425 | } |
| 426 | } |
| 427 | return NULL; |
| 428 | } |
Harald Welte | d83d296 | 2013-03-04 17:52:33 +0000 | [diff] [blame] | 429 | |
Harald Welte | 3a6bedf | 2020-03-22 10:30:10 +0100 | [diff] [blame] | 430 | /*! Find application-specific status word within given card application profile */ |
| 431 | const struct osim_card_sw *osim_app_profile_find_sw(const struct osim_card_app_profile *ap, uint16_t sw_in) |
Harald Welte | d83d296 | 2013-03-04 17:52:33 +0000 | [diff] [blame] | 432 | { |
Harald Welte | 3a6bedf | 2020-03-22 10:30:10 +0100 | [diff] [blame] | 433 | const struct osim_card_sw *sw_list = ap->sw, *sw; |
| 434 | |
| 435 | for (sw = sw_list; sw->code != 0 && sw->mask != 0; sw++) { |
| 436 | if ((sw_in & sw->mask) == sw->code) |
| 437 | return sw; |
| 438 | } |
| 439 | return NULL; |
| 440 | } |
| 441 | |
| 442 | enum osim_card_sw_class osim_sw_class(const struct osim_chan_hdl *ch, uint16_t sw_in) |
| 443 | { |
| 444 | const struct osim_card_sw *csw = NULL; |
| 445 | |
| 446 | OSMO_ASSERT(ch); |
| 447 | OSMO_ASSERT(ch->card); |
| 448 | |
| 449 | if (ch->cur_app && ch->cur_app->prof) |
| 450 | csw = osim_app_profile_find_sw(ch->cur_app->prof, sw_in); |
| 451 | |
| 452 | if (!csw && ch->card->prof) |
| 453 | csw = osim_cprof_find_sw(ch->card->prof, sw_in); |
Harald Welte | d83d296 | 2013-03-04 17:52:33 +0000 | [diff] [blame] | 454 | |
| 455 | if (!csw) |
| 456 | return SW_CLS_NONE; |
| 457 | |
| 458 | return csw->class; |
| 459 | } |
Harald Welte | 30115db | 2014-05-04 16:30:46 +0200 | [diff] [blame] | 460 | |
| 461 | int default_decode(struct osim_decoded_data *dd, |
| 462 | const struct osim_file_desc *desc, |
| 463 | int len, uint8_t *data) |
| 464 | { |
| 465 | struct osim_decoded_element *elem; |
| 466 | |
| 467 | elem = element_alloc(dd, "Unknown Payload", ELEM_T_BYTES, ELEM_REPR_HEX); |
| 468 | elem->u.buf = talloc_memdup(elem, data, len); |
| 469 | |
| 470 | return 0; |
| 471 | } |
Harald Welte | 429adec | 2020-03-20 13:05:40 +0100 | [diff] [blame] | 472 | |
| 473 | int osim_init(void *ctx) |
| 474 | { |
| 475 | osim_app_profile_register(osim_aprof_usim(ctx)); |
| 476 | osim_app_profile_register(osim_aprof_isim(ctx)); |
Harald Welte | 2954aa9 | 2020-03-21 14:16:10 +0100 | [diff] [blame] | 477 | osim_app_profile_register(osim_aprof_hpsim(ctx)); |
Harald Welte | 429adec | 2020-03-20 13:05:40 +0100 | [diff] [blame] | 478 | |
| 479 | return 0; |
| 480 | } |