blob: 80a168f4771a9ef04a30d07bfed58d0e82c018f8 [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 *
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 Welted54c2ee2012-01-17 18:25:50 +010027#include <stdlib.h>
28#include <stdint.h>
Harald Welte76749602012-09-19 20:55:54 +020029#include <string.h>
Harald Welte429adec2020-03-20 13:05:40 +010030#include <errno.h>
Harald Welted54c2ee2012-01-17 18:25:50 +010031
32#include <osmocom/core/talloc.h>
33#include <osmocom/sim/sim.h>
34
Harald Welte429adec2020-03-20 13:05:40 +010035#include "sim_int.h"
36
Harald Weltead418632012-09-10 10:49:59 +020037struct 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 Weltedb2b52e2014-10-26 19:04:41 +010046 if (!dd)
47 return NULL;
Harald Weltead418632012-09-10 10:49:59 +020048 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
57struct 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 Welted54c2ee2012-01-17 18:25:50 +010066static 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
86struct osim_decoded_element *
87element_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
101struct osim_decoded_element *
102element_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
117void 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
127struct 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 Weltedb2b52e2014-10-26 19:04:41 +0100132 if (!mf)
133 return NULL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100134 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
142struct osim_file_desc *
143add_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 Weltedb2b52e2014-10-26 19:04:41 +0100150 if (!df)
151 return NULL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100152 df->parent = parent;
153 llist_add_tail(&df->list, &parent->child_list);
154 add_filedesc(df, in, num);
155
156 return df;
157}
158
159struct osim_file_desc *
Harald Welte429adec2020-03-20 13:05:40 +0100160alloc_adf_with_ef(void *ctx,
Harald Welted54c2ee2012-01-17 18:25:50 +0100161 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 Welte429adec2020-03-20 13:05:40 +0100167 df = alloc_df(ctx, 0xffff, name);
Harald Weltedb2b52e2014-10-26 19:04:41 +0100168 if (!df)
169 return NULL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100170 df->type = TYPE_ADF;
171 df->df_name = adf_name;
172 df->df_name_len = adf_name_len;
Harald Welted54c2ee2012-01-17 18:25:50 +0100173 add_filedesc(df, in, num);
174
175 return df;
176}
177
178struct osim_file_desc *
Harald Welte5ffb5032016-03-11 09:40:56 +0700179osim_file_desc_find_name(struct osim_file_desc *parent, const char *name)
Harald Welted54c2ee2012-01-17 18:25:50 +0100180{
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 Weltec28f4cd2016-03-11 09:35:07 +0700190struct osim_file_desc *
Harald Weltebf90d742020-03-20 12:11:03 +0100191osim_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
206struct osim_file_desc *
Harald Welte5ffb5032016-03-11 09:40:56 +0700207osim_file_desc_find_fid(struct osim_file_desc *parent, uint16_t fid)
Harald Weltec28f4cd2016-03-11 09:35:07 +0700208{
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
218struct osim_file_desc *
Harald Welte5ffb5032016-03-11 09:40:56 +0700219osim_file_desc_find_sfid(struct osim_file_desc *parent, uint8_t sfid)
Harald Weltec28f4cd2016-03-11 09:35:07 +0700220{
221 struct osim_file_desc *ofd;
222 llist_for_each_entry(ofd, &parent->child_list, list) {
Harald Welte5ffb5032016-03-11 09:40:56 +0700223 if (ofd->sfid == SFI_NONE)
224 continue;
Harald Weltec28f4cd2016-03-11 09:35:07 +0700225 if (ofd->sfid == sfid) {
226 return ofd;
227 }
228 }
229 return NULL;
230}
231
232
Harald Welte429adec2020-03-20 13:05:40 +0100233/***********************************************************************
234 * Application Profiles + Applications
235 ***********************************************************************/
236
237static LLIST_HEAD(g_app_profiles);
238
239/*! Register an application profile. Typically called at early start-up. */
240void 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") */
248const struct osim_card_app_profile *
249osim_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 */
261const struct osim_card_app_profile *
262osim_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
275struct osim_card_app_hdl *
276osim_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 */
291int 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 Hofmeyr87e45502017-06-20 00:17:59 +0200315/*! Generate an APDU message and initialize APDU command header
Kevin Redon43eabee2012-09-16 18:40:02 +0200316 * \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 Redon0f0ee322012-09-11 11:40:41 +0200327 */
Harald Welted54c2ee2012-01-17 18:25:50 +0100328struct 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 Welte76749602012-09-19 20:55:54 +0200337 msg->l2h = (uint8_t *) ch;
Harald Welted54c2ee2012-01-17 18:25:50 +0100338
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 Redone07967f2012-09-11 11:44:18 +0200351 msgb_apdu_case(msg) = APDU_CASE_2S;
Harald Welted54c2ee2012-01-17 18:25:50 +0100352 else
Kevin Redone07967f2012-09-11 11:44:18 +0200353 msgb_apdu_case(msg) = APDU_CASE_2E;
Harald Welted54c2ee2012-01-17 18:25:50 +0100354 } else if (le == 0 && lc >= 1) {
355 if (lc <= 255)
Kevin Redone07967f2012-09-11 11:44:18 +0200356 msgb_apdu_case(msg) = APDU_CASE_3S;
Harald Welted54c2ee2012-01-17 18:25:50 +0100357 else
Kevin Redone07967f2012-09-11 11:44:18 +0200358 msgb_apdu_case(msg) = APDU_CASE_3E;
Harald Welted54c2ee2012-01-17 18:25:50 +0100359 } else if (lc >= 1 && le >= 1) {
Harald Welte76749602012-09-19 20:55:54 +0200360 if (lc <= 255 && le <= 256)
Kevin Redone07967f2012-09-11 11:44:18 +0200361 msgb_apdu_case(msg) = APDU_CASE_4S;
Harald Welted54c2ee2012-01-17 18:25:50 +0100362 else
Kevin Redone07967f2012-09-11 11:44:18 +0200363 msgb_apdu_case(msg) = APDU_CASE_4E;
Harald Welted54c2ee2012-01-17 18:25:50 +0100364 }
365
366 return msg;
367}
Harald Welte76749602012-09-19 20:55:54 +0200368
Harald Welte76749602012-09-19 20:55:54 +0200369
Harald Welte4a62eda2019-03-18 18:27:00 +0100370char *osim_print_sw_buf(char *buf, size_t buf_len, const struct osim_card_hdl *ch, uint16_t sw_in)
Harald Welte76749602012-09-19 20:55:54 +0200371{
372 const struct osim_card_sw *csw;
373
374 if (!ch || !ch->prof)
375 goto ret_def;
376
377 csw = osim_find_sw(ch->prof, sw_in);
378 if (!csw)
379 goto ret_def;
380
381 switch (csw->type) {
382 case SW_TYPE_STR:
Harald Welte4a62eda2019-03-18 18:27:00 +0100383 snprintf(buf, buf_len, "%04x (%s)", sw_in, csw->u.str);
Harald Welte76749602012-09-19 20:55:54 +0200384 break;
385 default:
386 goto ret_def;
387 }
388
Harald Welte4a62eda2019-03-18 18:27:00 +0100389 buf[buf_len-1] = '\0';
Harald Welte76749602012-09-19 20:55:54 +0200390
Harald Welte4a62eda2019-03-18 18:27:00 +0100391 return buf;
Harald Welte76749602012-09-19 20:55:54 +0200392
393ret_def:
Harald Welte4a62eda2019-03-18 18:27:00 +0100394 snprintf(buf, buf_len, "%04x (Unknown)", sw_in);
395 buf[buf_len-1] = '\0';
Harald Welte76749602012-09-19 20:55:54 +0200396
Harald Welte4a62eda2019-03-18 18:27:00 +0100397 return buf;
Harald Welte76749602012-09-19 20:55:54 +0200398}
399
Harald Welte4a62eda2019-03-18 18:27:00 +0100400char *osim_print_sw(const struct osim_card_hdl *ch, uint16_t sw_in)
401{
Harald Welte171ef822019-03-28 10:49:05 +0100402 static __thread char sw_print_buf[256];
Harald Welte4a62eda2019-03-18 18:27:00 +0100403 return osim_print_sw_buf(sw_print_buf, sizeof(sw_print_buf), ch, sw_in);
404}
Harald Welte76749602012-09-19 20:55:54 +0200405
Harald Welte179f3572019-03-18 18:38:47 +0100406char *osim_print_sw_c(const void *ctx, const struct osim_card_hdl *ch, uint16_t sw_in)
407{
408 char *buf = talloc_size(ctx, 256);
409 if (!buf)
410 return NULL;
411 return osim_print_sw_buf(buf, 256, ch, sw_in);
412}
413
Harald Welte76749602012-09-19 20:55:54 +0200414const struct osim_card_sw *osim_find_sw(const struct osim_card_profile *cp,
415 uint16_t sw_in)
416{
417 const struct osim_card_sw **sw_lists = cp->sws;
418 const struct osim_card_sw *sw_list, *sw;
419
Vadim Yanitskiyd1c73232017-06-12 03:33:07 +0700420 for (sw_list = *sw_lists++; sw_list != NULL; sw_list = *sw_lists++) {
Harald Welte76749602012-09-19 20:55:54 +0200421 for (sw = sw_list; sw->code != 0 && sw->mask != 0; sw++) {
422 if ((sw_in & sw->mask) == sw->code)
423 return sw;
424 }
425 }
426 return NULL;
427}
Harald Welted83d2962013-03-04 17:52:33 +0000428
429enum osim_card_sw_class osim_sw_class(const struct osim_card_profile *cp,
430 uint16_t sw_in)
431{
432 const struct osim_card_sw *csw = osim_find_sw(cp, sw_in);
433
434 if (!csw)
435 return SW_CLS_NONE;
436
437 return csw->class;
438}
Harald Welte30115db2014-05-04 16:30:46 +0200439
440int default_decode(struct osim_decoded_data *dd,
441 const struct osim_file_desc *desc,
442 int len, uint8_t *data)
443{
444 struct osim_decoded_element *elem;
445
446 elem = element_alloc(dd, "Unknown Payload", ELEM_T_BYTES, ELEM_REPR_HEX);
447 elem->u.buf = talloc_memdup(elem, data, len);
448
449 return 0;
450}
Harald Welte429adec2020-03-20 13:05:40 +0100451
452int osim_init(void *ctx)
453{
454 osim_app_profile_register(osim_aprof_usim(ctx));
455 osim_app_profile_register(osim_aprof_isim(ctx));
456
457 return 0;
458}