blob: 2f129c9e48d360efa13a4e413a3538964349c87c [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 *
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 Welte5ffb5032016-03-11 09:40:56 +0700187osim_file_desc_find_fid(struct osim_file_desc *parent, uint16_t fid)
Harald Weltec28f4cd2016-03-11 09:35:07 +0700188{
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 *
Harald Welte5ffb5032016-03-11 09:40:56 +0700199osim_file_desc_find_sfid(struct osim_file_desc *parent, uint8_t sfid)
Harald Weltec28f4cd2016-03-11 09:35:07 +0700200{
201 struct osim_file_desc *ofd;
202 llist_for_each_entry(ofd, &parent->child_list, list) {
Harald Welte5ffb5032016-03-11 09:40:56 +0700203 if (ofd->sfid == SFI_NONE)
204 continue;
Harald Weltec28f4cd2016-03-11 09:35:07 +0700205 if (ofd->sfid == sfid) {
206 return ofd;
207 }
208 }
209 return NULL;
210}
211
212
Kevin Redon43eabee2012-09-16 18:40:02 +0200213/*! \brief Generate an APDU message and initialize APDU command header
214 * \param[in] cla CLASS byte
215 * \param[in] ins INSTRUCTION byte
216 * \param[in] p1 Parameter 1 byte
217 * \param[in] p2 Parameter 2 byte
218 * \param[in] lc number of bytes in the command data field Nc, which will encoded in 0, 1 or 3 bytes into Lc
219 * \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
220 * \returns an APDU message generated using provided APDU parameters
221 *
222 * This function generates an APDU message, as defined in ISO/IEC 7816-4:2005(E) ยง5.1.
223 * The APDU command header, command and response fields lengths are initialized using the parameters.
224 * The APDU case is determined by the command and response fields lengths.
Kevin Redon0f0ee322012-09-11 11:40:41 +0200225 */
Harald Welted54c2ee2012-01-17 18:25:50 +0100226struct msgb *osim_new_apdumsg(uint8_t cla, uint8_t ins, uint8_t p1,
227 uint8_t p2, uint16_t lc, uint16_t le)
228{
229 struct osim_apdu_cmd_hdr *ch;
230 struct msgb *msg = msgb_alloc(lc+le+sizeof(*ch)+2, "APDU");
231 if (!msg)
232 return NULL;
233
234 ch = (struct osim_apdu_cmd_hdr *) msgb_put(msg, sizeof(*ch));
Harald Welte76749602012-09-19 20:55:54 +0200235 msg->l2h = (uint8_t *) ch;
Harald Welted54c2ee2012-01-17 18:25:50 +0100236
237 ch->cla = cla;
238 ch->ins = ins;
239 ch->p1 = p1;
240 ch->p2 = p2;
241
242 msgb_apdu_lc(msg) = lc;
243 msgb_apdu_le(msg) = le;
244
245 if (lc == 0 && le == 0)
246 msgb_apdu_case(msg) = APDU_CASE_1;
247 else if (lc == 0 && le >= 1) {
248 if (le <= 256)
Kevin Redone07967f2012-09-11 11:44:18 +0200249 msgb_apdu_case(msg) = APDU_CASE_2S;
Harald Welted54c2ee2012-01-17 18:25:50 +0100250 else
Kevin Redone07967f2012-09-11 11:44:18 +0200251 msgb_apdu_case(msg) = APDU_CASE_2E;
Harald Welted54c2ee2012-01-17 18:25:50 +0100252 } else if (le == 0 && lc >= 1) {
253 if (lc <= 255)
Kevin Redone07967f2012-09-11 11:44:18 +0200254 msgb_apdu_case(msg) = APDU_CASE_3S;
Harald Welted54c2ee2012-01-17 18:25:50 +0100255 else
Kevin Redone07967f2012-09-11 11:44:18 +0200256 msgb_apdu_case(msg) = APDU_CASE_3E;
Harald Welted54c2ee2012-01-17 18:25:50 +0100257 } else if (lc >= 1 && le >= 1) {
Harald Welte76749602012-09-19 20:55:54 +0200258 if (lc <= 255 && le <= 256)
Kevin Redone07967f2012-09-11 11:44:18 +0200259 msgb_apdu_case(msg) = APDU_CASE_4S;
Harald Welted54c2ee2012-01-17 18:25:50 +0100260 else
Kevin Redone07967f2012-09-11 11:44:18 +0200261 msgb_apdu_case(msg) = APDU_CASE_4E;
Harald Welted54c2ee2012-01-17 18:25:50 +0100262 }
263
264 return msg;
265}
Harald Welte76749602012-09-19 20:55:54 +0200266
267/* FIXME: do we want to mark this as __thread? */
268static char sw_print_buf[256];
269
270char *osim_print_sw(const struct osim_card_hdl *ch, uint16_t sw_in)
271{
272 const struct osim_card_sw *csw;
273
274 if (!ch || !ch->prof)
275 goto ret_def;
276
277 csw = osim_find_sw(ch->prof, sw_in);
278 if (!csw)
279 goto ret_def;
280
281 switch (csw->type) {
282 case SW_TYPE_STR:
283 snprintf(sw_print_buf, sizeof(sw_print_buf),
284 "%04x (%s)", sw_in, csw->u.str);
285 break;
286 default:
287 goto ret_def;
288 }
289
290 sw_print_buf[sizeof(sw_print_buf)-1] = '\0';
291
292 return sw_print_buf;
293
294ret_def:
295 snprintf(sw_print_buf, sizeof(sw_print_buf),
296 "%04x (Unknown)", sw_in);
297 sw_print_buf[sizeof(sw_print_buf)-1] = '\0';
298
299 return sw_print_buf;
300}
301
302
303const struct osim_card_sw *osim_find_sw(const struct osim_card_profile *cp,
304 uint16_t sw_in)
305{
306 const struct osim_card_sw **sw_lists = cp->sws;
307 const struct osim_card_sw *sw_list, *sw;
308
309 for (sw_list = *sw_lists++; sw_list != NULL; sw = sw_list = *sw_lists++) {
310 for (sw = sw_list; sw->code != 0 && sw->mask != 0; sw++) {
311 if ((sw_in & sw->mask) == sw->code)
312 return sw;
313 }
314 }
315 return NULL;
316}
Harald Welted83d2962013-03-04 17:52:33 +0000317
318enum osim_card_sw_class osim_sw_class(const struct osim_card_profile *cp,
319 uint16_t sw_in)
320{
321 const struct osim_card_sw *csw = osim_find_sw(cp, sw_in);
322
323 if (!csw)
324 return SW_CLS_NONE;
325
326 return csw->class;
327}
Harald Welte30115db2014-05-04 16:30:46 +0200328
329int default_decode(struct osim_decoded_data *dd,
330 const struct osim_file_desc *desc,
331 int len, uint8_t *data)
332{
333 struct osim_decoded_element *elem;
334
335 elem = element_alloc(dd, "Unknown Payload", ELEM_T_BYTES, ELEM_REPR_HEX);
336 elem->u.buf = talloc_memdup(elem, data, len);
337
338 return 0;
339}