blob: ddddc7dc060f78405b252a201d0bd374627dc81f [file] [log] [blame]
Harald Weltead418632012-09-10 10:49:59 +02001/* Core routines for SIM/UICC/USIM access */
2/*
3 * (C) 2012 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23
Harald Welted54c2ee2012-01-17 18:25:50 +010024#include <stdlib.h>
25#include <stdint.h>
Harald Welte76749602012-09-19 20:55:54 +020026#include <string.h>
Harald Welted54c2ee2012-01-17 18:25:50 +010027
28#include <osmocom/core/talloc.h>
29#include <osmocom/sim/sim.h>
30
Harald Weltead418632012-09-10 10:49:59 +020031struct osim_decoded_data *osim_file_decode(struct osim_file *file,
32 int len, uint8_t *data)
33{
34 struct osim_decoded_data *dd;
35
36 if (!file->desc->ops.parse)
37 return NULL;
38
39 dd = talloc_zero(file, struct osim_decoded_data);
Harald Weltedb2b52e2014-10-26 19:04:41 +010040 if (!dd)
41 return NULL;
Harald Weltead418632012-09-10 10:49:59 +020042 dd->file = file;
43
44 if (file->desc->ops.parse(dd, file->desc, len, data) < 0) {
45 talloc_free(dd);
46 return NULL;
47 } else
48 return dd;
49}
50
51struct msgb *osim_file_encode(const struct osim_file_desc *desc,
52 const struct osim_decoded_data *data)
53{
54 if (!desc->ops.encode)
55 return NULL;
56
57 return desc->ops.encode(desc, data);
58}
59
Harald Welted54c2ee2012-01-17 18:25:50 +010060static struct osim_decoded_element *
61__element_alloc(void *ctx, const char *name, enum osim_element_type type,
62 enum osim_element_repr repr)
63{
64 struct osim_decoded_element *elem;
65
66 elem = talloc_zero(ctx, struct osim_decoded_element);
67 if (!elem)
68 return NULL;
69 elem->name = name;
70 elem->type = type;
71 elem->representation = repr;
72
73 if (elem->type == ELEM_T_GROUP)
74 INIT_LLIST_HEAD(&elem->u.siblings);
75
76 return elem;
77}
78
79
80struct osim_decoded_element *
81element_alloc(struct osim_decoded_data *dd, const char *name,
82 enum osim_element_type type, enum osim_element_repr repr)
83{
84 struct osim_decoded_element *elem;
85
86 elem = __element_alloc(dd, name, type, repr);
87 if (!elem)
88 return NULL;
89
90 llist_add_tail(&elem->list, &dd->decoded_elements);
91
92 return elem;
93}
94
95struct osim_decoded_element *
96element_alloc_sub(struct osim_decoded_element *ee, const char *name,
97 enum osim_element_type type, enum osim_element_repr repr)
98{
99 struct osim_decoded_element *elem;
100
101 elem = __element_alloc(ee, name, type, repr);
102 if (!elem)
103 return NULL;
104
105 llist_add(&elem->list, &ee->u.siblings);
106
107 return elem;
108}
109
110
111void add_filedesc(struct osim_file_desc *root, const struct osim_file_desc *in, int num)
112{
113 int i;
114
115 for (i = 0; i < num; i++) {
116 struct osim_file_desc *ofd = talloc_memdup(root, &in[i], sizeof(*in));
117 llist_add_tail(&ofd->list, &root->child_list);
118 }
119}
120
121struct osim_file_desc *alloc_df(void *ctx, uint16_t fid, const char *name)
122{
123 struct osim_file_desc *mf;
124
125 mf = talloc_zero(ctx, struct osim_file_desc);
Harald Weltedb2b52e2014-10-26 19:04:41 +0100126 if (!mf)
127 return NULL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100128 mf->type = TYPE_DF;
129 mf->fid = fid;
130 mf->short_name = name;
131 INIT_LLIST_HEAD(&mf->child_list);
132
133 return mf;
134}
135
136struct osim_file_desc *
137add_df_with_ef(struct osim_file_desc *parent,
138 uint16_t fid, const char *name,
139 const struct osim_file_desc *in, int num)
140{
141 struct osim_file_desc *df;
142
143 df = alloc_df(parent, fid, name);
Harald Weltedb2b52e2014-10-26 19:04:41 +0100144 if (!df)
145 return NULL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100146 df->parent = parent;
147 llist_add_tail(&df->list, &parent->child_list);
148 add_filedesc(df, in, num);
149
150 return df;
151}
152
153struct osim_file_desc *
154add_adf_with_ef(struct osim_file_desc *parent,
155 const uint8_t *adf_name, uint8_t adf_name_len,
156 const char *name, const struct osim_file_desc *in,
157 int num)
158{
159 struct osim_file_desc *df;
160
161 df = alloc_df(parent, 0xffff, name);
Harald Weltedb2b52e2014-10-26 19:04:41 +0100162 if (!df)
163 return NULL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100164 df->type = TYPE_ADF;
165 df->df_name = adf_name;
166 df->df_name_len = adf_name_len;
167 df->parent = parent;
168 llist_add_tail(&df->list, &parent->child_list);
169 add_filedesc(df, in, num);
170
171 return df;
172}
173
174struct osim_file_desc *
175osim_file_find_name(struct osim_file_desc *parent, const char *name)
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 Weltec28f4cd2016-03-11 09:35:07 +0700186struct osim_file_desc *
187osim_file_find_fid(struct osim_file_desc *parent, uint16_t fid)
188{
189 struct osim_file_desc *ofd;
190 llist_for_each_entry(ofd, &parent->child_list, list) {
191 if (ofd->fid == fid) {
192 return ofd;
193 }
194 }
195 return NULL;
196}
197
198struct osim_file_desc *
199osim_file_find_sfid(struct osim_file_desc *parent, uint8_t sfid)
200{
201 struct osim_file_desc *ofd;
202 llist_for_each_entry(ofd, &parent->child_list, list) {
203 if (ofd->sfid == sfid) {
204 return ofd;
205 }
206 }
207 return NULL;
208}
209
210
Kevin Redon43eabee2012-09-16 18:40:02 +0200211/*! \brief Generate an APDU message and initialize APDU command header
212 * \param[in] cla CLASS byte
213 * \param[in] ins INSTRUCTION byte
214 * \param[in] p1 Parameter 1 byte
215 * \param[in] p2 Parameter 2 byte
216 * \param[in] lc number of bytes in the command data field Nc, which will encoded in 0, 1 or 3 bytes into Lc
217 * \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
218 * \returns an APDU message generated using provided APDU parameters
219 *
220 * This function generates an APDU message, as defined in ISO/IEC 7816-4:2005(E) ยง5.1.
221 * The APDU command header, command and response fields lengths are initialized using the parameters.
222 * The APDU case is determined by the command and response fields lengths.
Kevin Redon0f0ee322012-09-11 11:40:41 +0200223 */
Harald Welted54c2ee2012-01-17 18:25:50 +0100224struct msgb *osim_new_apdumsg(uint8_t cla, uint8_t ins, uint8_t p1,
225 uint8_t p2, uint16_t lc, uint16_t le)
226{
227 struct osim_apdu_cmd_hdr *ch;
228 struct msgb *msg = msgb_alloc(lc+le+sizeof(*ch)+2, "APDU");
229 if (!msg)
230 return NULL;
231
232 ch = (struct osim_apdu_cmd_hdr *) msgb_put(msg, sizeof(*ch));
Harald Welte76749602012-09-19 20:55:54 +0200233 msg->l2h = (uint8_t *) ch;
Harald Welted54c2ee2012-01-17 18:25:50 +0100234
235 ch->cla = cla;
236 ch->ins = ins;
237 ch->p1 = p1;
238 ch->p2 = p2;
239
240 msgb_apdu_lc(msg) = lc;
241 msgb_apdu_le(msg) = le;
242
243 if (lc == 0 && le == 0)
244 msgb_apdu_case(msg) = APDU_CASE_1;
245 else if (lc == 0 && le >= 1) {
246 if (le <= 256)
Kevin Redone07967f2012-09-11 11:44:18 +0200247 msgb_apdu_case(msg) = APDU_CASE_2S;
Harald Welted54c2ee2012-01-17 18:25:50 +0100248 else
Kevin Redone07967f2012-09-11 11:44:18 +0200249 msgb_apdu_case(msg) = APDU_CASE_2E;
Harald Welted54c2ee2012-01-17 18:25:50 +0100250 } else if (le == 0 && lc >= 1) {
251 if (lc <= 255)
Kevin Redone07967f2012-09-11 11:44:18 +0200252 msgb_apdu_case(msg) = APDU_CASE_3S;
Harald Welted54c2ee2012-01-17 18:25:50 +0100253 else
Kevin Redone07967f2012-09-11 11:44:18 +0200254 msgb_apdu_case(msg) = APDU_CASE_3E;
Harald Welted54c2ee2012-01-17 18:25:50 +0100255 } else if (lc >= 1 && le >= 1) {
Harald Welte76749602012-09-19 20:55:54 +0200256 if (lc <= 255 && le <= 256)
Kevin Redone07967f2012-09-11 11:44:18 +0200257 msgb_apdu_case(msg) = APDU_CASE_4S;
Harald Welted54c2ee2012-01-17 18:25:50 +0100258 else
Kevin Redone07967f2012-09-11 11:44:18 +0200259 msgb_apdu_case(msg) = APDU_CASE_4E;
Harald Welted54c2ee2012-01-17 18:25:50 +0100260 }
261
262 return msg;
263}
Harald Welte76749602012-09-19 20:55:54 +0200264
265/* FIXME: do we want to mark this as __thread? */
266static char sw_print_buf[256];
267
268char *osim_print_sw(const struct osim_card_hdl *ch, uint16_t sw_in)
269{
270 const struct osim_card_sw *csw;
271
272 if (!ch || !ch->prof)
273 goto ret_def;
274
275 csw = osim_find_sw(ch->prof, sw_in);
276 if (!csw)
277 goto ret_def;
278
279 switch (csw->type) {
280 case SW_TYPE_STR:
281 snprintf(sw_print_buf, sizeof(sw_print_buf),
282 "%04x (%s)", sw_in, csw->u.str);
283 break;
284 default:
285 goto ret_def;
286 }
287
288 sw_print_buf[sizeof(sw_print_buf)-1] = '\0';
289
290 return sw_print_buf;
291
292ret_def:
293 snprintf(sw_print_buf, sizeof(sw_print_buf),
294 "%04x (Unknown)", sw_in);
295 sw_print_buf[sizeof(sw_print_buf)-1] = '\0';
296
297 return sw_print_buf;
298}
299
300
301const struct osim_card_sw *osim_find_sw(const struct osim_card_profile *cp,
302 uint16_t sw_in)
303{
304 const struct osim_card_sw **sw_lists = cp->sws;
305 const struct osim_card_sw *sw_list, *sw;
306
307 for (sw_list = *sw_lists++; sw_list != NULL; sw = sw_list = *sw_lists++) {
308 for (sw = sw_list; sw->code != 0 && sw->mask != 0; sw++) {
309 if ((sw_in & sw->mask) == sw->code)
310 return sw;
311 }
312 }
313 return NULL;
314}
Harald Welted83d2962013-03-04 17:52:33 +0000315
316enum osim_card_sw_class osim_sw_class(const struct osim_card_profile *cp,
317 uint16_t sw_in)
318{
319 const struct osim_card_sw *csw = osim_find_sw(cp, sw_in);
320
321 if (!csw)
322 return SW_CLS_NONE;
323
324 return csw->class;
325}
Harald Welte30115db2014-05-04 16:30:46 +0200326
327int default_decode(struct osim_decoded_data *dd,
328 const struct osim_file_desc *desc,
329 int len, uint8_t *data)
330{
331 struct osim_decoded_element *elem;
332
333 elem = element_alloc(dd, "Unknown Payload", ELEM_T_BYTES, ELEM_REPR_HEX);
334 elem->u.buf = talloc_memdup(elem, data, len);
335
336 return 0;
337}