blob: fa17e12d407478a534c0538b71be3345e303097c [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file core.c
2 * Core routines for SIM/UICC/USIM access. */
Harald Weltead418632012-09-10 10:49:59 +02003/*
Harald Welte429adec2020-03-20 13:05:40 +01004 * (C) 2012-2020 by Harald Welte <laforge@gnumonks.org>
Harald Weltead418632012-09-10 10:49:59 +02005 *
6 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Harald Weltead418632012-09-10 10:49:59 +020010 * 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 Weltead418632012-09-10 10:49:59 +020020 */
21
22
Harald Welted54c2ee2012-01-17 18:25:50 +010023#include <stdlib.h>
24#include <stdint.h>
Harald Welte76749602012-09-19 20:55:54 +020025#include <string.h>
Harald Welte429adec2020-03-20 13:05:40 +010026#include <errno.h>
Harald Welted54c2ee2012-01-17 18:25:50 +010027
28#include <osmocom/core/talloc.h>
29#include <osmocom/sim/sim.h>
30
Harald Welte429adec2020-03-20 13:05:40 +010031#include "sim_int.h"
32
Harald Weltead418632012-09-10 10:49:59 +020033struct 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 Weltedb2b52e2014-10-26 19:04:41 +010042 if (!dd)
43 return NULL;
Harald Weltead418632012-09-10 10:49:59 +020044 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
53struct 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 Welted54c2ee2012-01-17 18:25:50 +010062static 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
82struct osim_decoded_element *
83element_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
97struct osim_decoded_element *
98element_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
113void 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
123struct 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 Weltedb2b52e2014-10-26 19:04:41 +0100128 if (!mf)
129 return NULL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100130 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
138struct osim_file_desc *
139add_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 Weltedb2b52e2014-10-26 19:04:41 +0100146 if (!df)
147 return NULL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100148 df->parent = parent;
149 llist_add_tail(&df->list, &parent->child_list);
150 add_filedesc(df, in, num);
151
152 return df;
153}
154
155struct osim_file_desc *
Harald Welte429adec2020-03-20 13:05:40 +0100156alloc_adf_with_ef(void *ctx,
Harald Welted54c2ee2012-01-17 18:25:50 +0100157 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 Welte429adec2020-03-20 13:05:40 +0100163 df = alloc_df(ctx, 0xffff, name);
Harald Weltedb2b52e2014-10-26 19:04:41 +0100164 if (!df)
165 return NULL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100166 df->type = TYPE_ADF;
167 df->df_name = adf_name;
168 df->df_name_len = adf_name_len;
Harald Welted54c2ee2012-01-17 18:25:50 +0100169 add_filedesc(df, in, num);
170
171 return df;
172}
173
174struct osim_file_desc *
Harald Welte5ffb5032016-03-11 09:40:56 +0700175osim_file_desc_find_name(struct osim_file_desc *parent, const char *name)
Harald Welted54c2ee2012-01-17 18:25:50 +0100176{
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 Weltec28f4cd2016-03-11 09:35:07 +0700186struct osim_file_desc *
Harald Weltebf90d742020-03-20 12:11:03 +0100187osim_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
202struct osim_file_desc *
Harald Welte5ffb5032016-03-11 09:40:56 +0700203osim_file_desc_find_fid(struct osim_file_desc *parent, uint16_t fid)
Harald Weltec28f4cd2016-03-11 09:35:07 +0700204{
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
214struct osim_file_desc *
Harald Welte5ffb5032016-03-11 09:40:56 +0700215osim_file_desc_find_sfid(struct osim_file_desc *parent, uint8_t sfid)
Harald Weltec28f4cd2016-03-11 09:35:07 +0700216{
217 struct osim_file_desc *ofd;
218 llist_for_each_entry(ofd, &parent->child_list, list) {
Harald Welte5ffb5032016-03-11 09:40:56 +0700219 if (ofd->sfid == SFI_NONE)
220 continue;
Harald Weltec28f4cd2016-03-11 09:35:07 +0700221 if (ofd->sfid == sfid) {
222 return ofd;
223 }
224 }
225 return NULL;
226}
227
228
Harald Welte429adec2020-03-20 13:05:40 +0100229/***********************************************************************
230 * Application Profiles + Applications
231 ***********************************************************************/
232
233static LLIST_HEAD(g_app_profiles);
234
235/*! Register an application profile. Typically called at early start-up. */
236void 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") */
244const struct osim_card_app_profile *
245osim_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 */
257const struct osim_card_app_profile *
258osim_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
271struct osim_card_app_hdl *
272osim_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 */
287int 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 Hofmeyr87e45502017-06-20 00:17:59 +0200311/*! Generate an APDU message and initialize APDU command header
Kevin Redon43eabee2012-09-16 18:40:02 +0200312 * \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 Redon0f0ee322012-09-11 11:40:41 +0200323 */
Harald Welted54c2ee2012-01-17 18:25:50 +0100324struct 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 Welte76749602012-09-19 20:55:54 +0200333 msg->l2h = (uint8_t *) ch;
Harald Welted54c2ee2012-01-17 18:25:50 +0100334
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 Redone07967f2012-09-11 11:44:18 +0200347 msgb_apdu_case(msg) = APDU_CASE_2S;
Harald Welted54c2ee2012-01-17 18:25:50 +0100348 else
Kevin Redone07967f2012-09-11 11:44:18 +0200349 msgb_apdu_case(msg) = APDU_CASE_2E;
Harald Welted54c2ee2012-01-17 18:25:50 +0100350 } else if (le == 0 && lc >= 1) {
351 if (lc <= 255)
Kevin Redone07967f2012-09-11 11:44:18 +0200352 msgb_apdu_case(msg) = APDU_CASE_3S;
Harald Welted54c2ee2012-01-17 18:25:50 +0100353 else
Kevin Redone07967f2012-09-11 11:44:18 +0200354 msgb_apdu_case(msg) = APDU_CASE_3E;
Harald Welted54c2ee2012-01-17 18:25:50 +0100355 } else if (lc >= 1 && le >= 1) {
Harald Welte76749602012-09-19 20:55:54 +0200356 if (lc <= 255 && le <= 256)
Kevin Redone07967f2012-09-11 11:44:18 +0200357 msgb_apdu_case(msg) = APDU_CASE_4S;
Harald Welted54c2ee2012-01-17 18:25:50 +0100358 else
Kevin Redone07967f2012-09-11 11:44:18 +0200359 msgb_apdu_case(msg) = APDU_CASE_4E;
Harald Welted54c2ee2012-01-17 18:25:50 +0100360 }
361
362 return msg;
363}
Harald Welte76749602012-09-19 20:55:54 +0200364
Harald Welte76749602012-09-19 20:55:54 +0200365
Harald Welte3a6bedf2020-03-22 10:30:10 +0100366char *osim_print_sw_buf(char *buf, size_t buf_len, const struct osim_chan_hdl *ch, uint16_t sw_in)
Harald Welte76749602012-09-19 20:55:54 +0200367{
Harald Welte3a6bedf2020-03-22 10:30:10 +0100368 const struct osim_card_sw *csw = NULL;
Harald Welte76749602012-09-19 20:55:54 +0200369
Harald Welte3a6bedf2020-03-22 10:30:10 +0100370 if (!ch)
Harald Welte76749602012-09-19 20:55:54 +0200371 goto ret_def;
372
Harald Welte3a6bedf2020-03-22 10:30:10 +0100373 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 Welte76749602012-09-19 20:55:54 +0200379 if (!csw)
380 goto ret_def;
381
382 switch (csw->type) {
383 case SW_TYPE_STR:
Harald Welte4a62eda2019-03-18 18:27:00 +0100384 snprintf(buf, buf_len, "%04x (%s)", sw_in, csw->u.str);
Harald Welte76749602012-09-19 20:55:54 +0200385 break;
386 default:
387 goto ret_def;
388 }
389
Harald Welte4a62eda2019-03-18 18:27:00 +0100390 buf[buf_len-1] = '\0';
Harald Welte76749602012-09-19 20:55:54 +0200391
Harald Welte4a62eda2019-03-18 18:27:00 +0100392 return buf;
Harald Welte76749602012-09-19 20:55:54 +0200393
394ret_def:
Harald Welte4a62eda2019-03-18 18:27:00 +0100395 snprintf(buf, buf_len, "%04x (Unknown)", sw_in);
396 buf[buf_len-1] = '\0';
Harald Welte76749602012-09-19 20:55:54 +0200397
Harald Welte4a62eda2019-03-18 18:27:00 +0100398 return buf;
Harald Welte76749602012-09-19 20:55:54 +0200399}
400
Harald Welte3a6bedf2020-03-22 10:30:10 +0100401char *osim_print_sw(const struct osim_chan_hdl *ch, uint16_t sw_in)
Harald Welte4a62eda2019-03-18 18:27:00 +0100402{
Harald Welte171ef822019-03-28 10:49:05 +0100403 static __thread char sw_print_buf[256];
Harald Welte4a62eda2019-03-18 18:27:00 +0100404 return osim_print_sw_buf(sw_print_buf, sizeof(sw_print_buf), ch, sw_in);
405}
Harald Welte76749602012-09-19 20:55:54 +0200406
Harald Welte3a6bedf2020-03-22 10:30:10 +0100407char *osim_print_sw_c(const void *ctx, const struct osim_chan_hdl *ch, uint16_t sw_in)
Harald Welte179f3572019-03-18 18:38:47 +0100408{
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 Welte3a6bedf2020-03-22 10:30:10 +0100415/*! Find status word within given card profile */
416const struct osim_card_sw *osim_cprof_find_sw(const struct osim_card_profile *cp, uint16_t sw_in)
Harald Welte76749602012-09-19 20:55:54 +0200417{
418 const struct osim_card_sw **sw_lists = cp->sws;
419 const struct osim_card_sw *sw_list, *sw;
420
Vadim Yanitskiyd1c73232017-06-12 03:33:07 +0700421 for (sw_list = *sw_lists++; sw_list != NULL; sw_list = *sw_lists++) {
Harald Welte76749602012-09-19 20:55:54 +0200422 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 Welted83d2962013-03-04 17:52:33 +0000429
Harald Welte3a6bedf2020-03-22 10:30:10 +0100430/*! Find application-specific status word within given card application profile */
431const struct osim_card_sw *osim_app_profile_find_sw(const struct osim_card_app_profile *ap, uint16_t sw_in)
Harald Welted83d2962013-03-04 17:52:33 +0000432{
Harald Welte3a6bedf2020-03-22 10:30:10 +0100433 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
442enum 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 Welted83d2962013-03-04 17:52:33 +0000454
455 if (!csw)
456 return SW_CLS_NONE;
457
458 return csw->class;
459}
Harald Welte30115db2014-05-04 16:30:46 +0200460
461int 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 Welte429adec2020-03-20 13:05:40 +0100472
473int osim_init(void *ctx)
474{
475 osim_app_profile_register(osim_aprof_usim(ctx));
476 osim_app_profile_register(osim_aprof_isim(ctx));
Harald Welte2954aa92020-03-21 14:16:10 +0100477 osim_app_profile_register(osim_aprof_hpsim(ctx));
Harald Welte429adec2020-03-20 13:05:40 +0100478
479 return 0;
480}