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