blob: 8da839c08fea63118673247e12a162912c8a9db0 [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/*
4 * (C) 2012 by Harald Welte <laforge@gnumonks.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24
Harald Welted54c2ee2012-01-17 18:25:50 +010025#include <stdlib.h>
26#include <stdint.h>
Harald Welte76749602012-09-19 20:55:54 +020027#include <string.h>
Harald Welted54c2ee2012-01-17 18:25:50 +010028
29#include <osmocom/core/talloc.h>
30#include <osmocom/sim/sim.h>
31
Harald Weltead418632012-09-10 10:49:59 +020032struct osim_decoded_data *osim_file_decode(struct osim_file *file,
33 int len, uint8_t *data)
34{
35 struct osim_decoded_data *dd;
36
37 if (!file->desc->ops.parse)
38 return NULL;
39
40 dd = talloc_zero(file, struct osim_decoded_data);
Harald Weltedb2b52e2014-10-26 19:04:41 +010041 if (!dd)
42 return NULL;
Harald Weltead418632012-09-10 10:49:59 +020043 dd->file = file;
44
45 if (file->desc->ops.parse(dd, file->desc, len, data) < 0) {
46 talloc_free(dd);
47 return NULL;
48 } else
49 return dd;
50}
51
52struct msgb *osim_file_encode(const struct osim_file_desc *desc,
53 const struct osim_decoded_data *data)
54{
55 if (!desc->ops.encode)
56 return NULL;
57
58 return desc->ops.encode(desc, data);
59}
60
Harald Welted54c2ee2012-01-17 18:25:50 +010061static struct osim_decoded_element *
62__element_alloc(void *ctx, const char *name, enum osim_element_type type,
63 enum osim_element_repr repr)
64{
65 struct osim_decoded_element *elem;
66
67 elem = talloc_zero(ctx, struct osim_decoded_element);
68 if (!elem)
69 return NULL;
70 elem->name = name;
71 elem->type = type;
72 elem->representation = repr;
73
74 if (elem->type == ELEM_T_GROUP)
75 INIT_LLIST_HEAD(&elem->u.siblings);
76
77 return elem;
78}
79
80
81struct osim_decoded_element *
82element_alloc(struct osim_decoded_data *dd, const char *name,
83 enum osim_element_type type, enum osim_element_repr repr)
84{
85 struct osim_decoded_element *elem;
86
87 elem = __element_alloc(dd, name, type, repr);
88 if (!elem)
89 return NULL;
90
91 llist_add_tail(&elem->list, &dd->decoded_elements);
92
93 return elem;
94}
95
96struct osim_decoded_element *
97element_alloc_sub(struct osim_decoded_element *ee, const char *name,
98 enum osim_element_type type, enum osim_element_repr repr)
99{
100 struct osim_decoded_element *elem;
101
102 elem = __element_alloc(ee, name, type, repr);
103 if (!elem)
104 return NULL;
105
106 llist_add(&elem->list, &ee->u.siblings);
107
108 return elem;
109}
110
111
112void add_filedesc(struct osim_file_desc *root, const struct osim_file_desc *in, int num)
113{
114 int i;
115
116 for (i = 0; i < num; i++) {
117 struct osim_file_desc *ofd = talloc_memdup(root, &in[i], sizeof(*in));
118 llist_add_tail(&ofd->list, &root->child_list);
119 }
120}
121
122struct osim_file_desc *alloc_df(void *ctx, uint16_t fid, const char *name)
123{
124 struct osim_file_desc *mf;
125
126 mf = talloc_zero(ctx, struct osim_file_desc);
Harald Weltedb2b52e2014-10-26 19:04:41 +0100127 if (!mf)
128 return NULL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100129 mf->type = TYPE_DF;
130 mf->fid = fid;
131 mf->short_name = name;
132 INIT_LLIST_HEAD(&mf->child_list);
133
134 return mf;
135}
136
137struct osim_file_desc *
138add_df_with_ef(struct osim_file_desc *parent,
139 uint16_t fid, const char *name,
140 const struct osim_file_desc *in, int num)
141{
142 struct osim_file_desc *df;
143
144 df = alloc_df(parent, fid, name);
Harald Weltedb2b52e2014-10-26 19:04:41 +0100145 if (!df)
146 return NULL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100147 df->parent = parent;
148 llist_add_tail(&df->list, &parent->child_list);
149 add_filedesc(df, in, num);
150
151 return df;
152}
153
154struct osim_file_desc *
155add_adf_with_ef(struct osim_file_desc *parent,
156 const uint8_t *adf_name, uint8_t adf_name_len,
157 const char *name, const struct osim_file_desc *in,
158 int num)
159{
160 struct osim_file_desc *df;
161
162 df = alloc_df(parent, 0xffff, name);
Harald Weltedb2b52e2014-10-26 19:04:41 +0100163 if (!df)
164 return NULL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100165 df->type = TYPE_ADF;
166 df->df_name = adf_name;
167 df->df_name_len = adf_name_len;
168 df->parent = parent;
169 llist_add_tail(&df->list, &parent->child_list);
170 add_filedesc(df, in, num);
171
172 return df;
173}
174
175struct osim_file_desc *
Harald Welte5ffb5032016-03-11 09:40:56 +0700176osim_file_desc_find_name(struct osim_file_desc *parent, const char *name)
Harald Welted54c2ee2012-01-17 18:25:50 +0100177{
178 struct osim_file_desc *ofd;
179 llist_for_each_entry(ofd, &parent->child_list, list) {
180 if (!strcmp(ofd->short_name, name)) {
181 return ofd;
182 }
183 }
184 return NULL;
185}
186
Harald Weltec28f4cd2016-03-11 09:35:07 +0700187struct osim_file_desc *
Harald Welte5ffb5032016-03-11 09:40:56 +0700188osim_file_desc_find_fid(struct osim_file_desc *parent, uint16_t fid)
Harald Weltec28f4cd2016-03-11 09:35:07 +0700189{
190 struct osim_file_desc *ofd;
191 llist_for_each_entry(ofd, &parent->child_list, list) {
192 if (ofd->fid == fid) {
193 return ofd;
194 }
195 }
196 return NULL;
197}
198
199struct osim_file_desc *
Harald Welte5ffb5032016-03-11 09:40:56 +0700200osim_file_desc_find_sfid(struct osim_file_desc *parent, uint8_t sfid)
Harald Weltec28f4cd2016-03-11 09:35:07 +0700201{
202 struct osim_file_desc *ofd;
203 llist_for_each_entry(ofd, &parent->child_list, list) {
Harald Welte5ffb5032016-03-11 09:40:56 +0700204 if (ofd->sfid == SFI_NONE)
205 continue;
Harald Weltec28f4cd2016-03-11 09:35:07 +0700206 if (ofd->sfid == sfid) {
207 return ofd;
208 }
209 }
210 return NULL;
211}
212
213
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200214/*! Generate an APDU message and initialize APDU command header
Kevin Redon43eabee2012-09-16 18:40:02 +0200215 * \param[in] cla CLASS byte
216 * \param[in] ins INSTRUCTION byte
217 * \param[in] p1 Parameter 1 byte
218 * \param[in] p2 Parameter 2 byte
219 * \param[in] lc number of bytes in the command data field Nc, which will encoded in 0, 1 or 3 bytes into Lc
220 * \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
221 * \returns an APDU message generated using provided APDU parameters
222 *
223 * This function generates an APDU message, as defined in ISO/IEC 7816-4:2005(E) ยง5.1.
224 * The APDU command header, command and response fields lengths are initialized using the parameters.
225 * The APDU case is determined by the command and response fields lengths.
Kevin Redon0f0ee322012-09-11 11:40:41 +0200226 */
Harald Welted54c2ee2012-01-17 18:25:50 +0100227struct msgb *osim_new_apdumsg(uint8_t cla, uint8_t ins, uint8_t p1,
228 uint8_t p2, uint16_t lc, uint16_t le)
229{
230 struct osim_apdu_cmd_hdr *ch;
231 struct msgb *msg = msgb_alloc(lc+le+sizeof(*ch)+2, "APDU");
232 if (!msg)
233 return NULL;
234
235 ch = (struct osim_apdu_cmd_hdr *) msgb_put(msg, sizeof(*ch));
Harald Welte76749602012-09-19 20:55:54 +0200236 msg->l2h = (uint8_t *) ch;
Harald Welted54c2ee2012-01-17 18:25:50 +0100237
238 ch->cla = cla;
239 ch->ins = ins;
240 ch->p1 = p1;
241 ch->p2 = p2;
242
243 msgb_apdu_lc(msg) = lc;
244 msgb_apdu_le(msg) = le;
245
246 if (lc == 0 && le == 0)
247 msgb_apdu_case(msg) = APDU_CASE_1;
248 else if (lc == 0 && le >= 1) {
249 if (le <= 256)
Kevin Redone07967f2012-09-11 11:44:18 +0200250 msgb_apdu_case(msg) = APDU_CASE_2S;
Harald Welted54c2ee2012-01-17 18:25:50 +0100251 else
Kevin Redone07967f2012-09-11 11:44:18 +0200252 msgb_apdu_case(msg) = APDU_CASE_2E;
Harald Welted54c2ee2012-01-17 18:25:50 +0100253 } else if (le == 0 && lc >= 1) {
254 if (lc <= 255)
Kevin Redone07967f2012-09-11 11:44:18 +0200255 msgb_apdu_case(msg) = APDU_CASE_3S;
Harald Welted54c2ee2012-01-17 18:25:50 +0100256 else
Kevin Redone07967f2012-09-11 11:44:18 +0200257 msgb_apdu_case(msg) = APDU_CASE_3E;
Harald Welted54c2ee2012-01-17 18:25:50 +0100258 } else if (lc >= 1 && le >= 1) {
Harald Welte76749602012-09-19 20:55:54 +0200259 if (lc <= 255 && le <= 256)
Kevin Redone07967f2012-09-11 11:44:18 +0200260 msgb_apdu_case(msg) = APDU_CASE_4S;
Harald Welted54c2ee2012-01-17 18:25:50 +0100261 else
Kevin Redone07967f2012-09-11 11:44:18 +0200262 msgb_apdu_case(msg) = APDU_CASE_4E;
Harald Welted54c2ee2012-01-17 18:25:50 +0100263 }
264
265 return msg;
266}
Harald Welte76749602012-09-19 20:55:54 +0200267
268/* FIXME: do we want to mark this as __thread? */
269static char sw_print_buf[256];
270
271char *osim_print_sw(const struct osim_card_hdl *ch, uint16_t sw_in)
272{
273 const struct osim_card_sw *csw;
274
275 if (!ch || !ch->prof)
276 goto ret_def;
277
278 csw = osim_find_sw(ch->prof, sw_in);
279 if (!csw)
280 goto ret_def;
281
282 switch (csw->type) {
283 case SW_TYPE_STR:
284 snprintf(sw_print_buf, sizeof(sw_print_buf),
285 "%04x (%s)", sw_in, csw->u.str);
286 break;
287 default:
288 goto ret_def;
289 }
290
291 sw_print_buf[sizeof(sw_print_buf)-1] = '\0';
292
293 return sw_print_buf;
294
295ret_def:
296 snprintf(sw_print_buf, sizeof(sw_print_buf),
297 "%04x (Unknown)", sw_in);
298 sw_print_buf[sizeof(sw_print_buf)-1] = '\0';
299
300 return sw_print_buf;
301}
302
303
304const struct osim_card_sw *osim_find_sw(const struct osim_card_profile *cp,
305 uint16_t sw_in)
306{
307 const struct osim_card_sw **sw_lists = cp->sws;
308 const struct osim_card_sw *sw_list, *sw;
309
Vadim Yanitskiyd1c73232017-06-12 03:33:07 +0700310 for (sw_list = *sw_lists++; sw_list != NULL; sw_list = *sw_lists++) {
Harald Welte76749602012-09-19 20:55:54 +0200311 for (sw = sw_list; sw->code != 0 && sw->mask != 0; sw++) {
312 if ((sw_in & sw->mask) == sw->code)
313 return sw;
314 }
315 }
316 return NULL;
317}
Harald Welted83d2962013-03-04 17:52:33 +0000318
319enum osim_card_sw_class osim_sw_class(const struct osim_card_profile *cp,
320 uint16_t sw_in)
321{
322 const struct osim_card_sw *csw = osim_find_sw(cp, sw_in);
323
324 if (!csw)
325 return SW_CLS_NONE;
326
327 return csw->class;
328}
Harald Welte30115db2014-05-04 16:30:46 +0200329
330int default_decode(struct osim_decoded_data *dd,
331 const struct osim_file_desc *desc,
332 int len, uint8_t *data)
333{
334 struct osim_decoded_element *elem;
335
336 elem = element_alloc(dd, "Unknown Payload", ELEM_T_BYTES, ELEM_REPR_HEX);
337 elem->u.buf = talloc_memdup(elem, data, len);
338
339 return 0;
340}