blob: dba378b6a38c6085f26b7b78354f0c32d7ce97f1 [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);
40 dd->file = file;
41
42 if (file->desc->ops.parse(dd, file->desc, len, data) < 0) {
43 talloc_free(dd);
44 return NULL;
45 } else
46 return dd;
47}
48
49struct msgb *osim_file_encode(const struct osim_file_desc *desc,
50 const struct osim_decoded_data *data)
51{
52 if (!desc->ops.encode)
53 return NULL;
54
55 return desc->ops.encode(desc, data);
56}
57
Harald Welted54c2ee2012-01-17 18:25:50 +010058static struct osim_decoded_element *
59__element_alloc(void *ctx, const char *name, enum osim_element_type type,
60 enum osim_element_repr repr)
61{
62 struct osim_decoded_element *elem;
63
64 elem = talloc_zero(ctx, struct osim_decoded_element);
65 if (!elem)
66 return NULL;
67 elem->name = name;
68 elem->type = type;
69 elem->representation = repr;
70
71 if (elem->type == ELEM_T_GROUP)
72 INIT_LLIST_HEAD(&elem->u.siblings);
73
74 return elem;
75}
76
77
78struct osim_decoded_element *
79element_alloc(struct osim_decoded_data *dd, const char *name,
80 enum osim_element_type type, enum osim_element_repr repr)
81{
82 struct osim_decoded_element *elem;
83
84 elem = __element_alloc(dd, name, type, repr);
85 if (!elem)
86 return NULL;
87
88 llist_add_tail(&elem->list, &dd->decoded_elements);
89
90 return elem;
91}
92
93struct osim_decoded_element *
94element_alloc_sub(struct osim_decoded_element *ee, const char *name,
95 enum osim_element_type type, enum osim_element_repr repr)
96{
97 struct osim_decoded_element *elem;
98
99 elem = __element_alloc(ee, name, type, repr);
100 if (!elem)
101 return NULL;
102
103 llist_add(&elem->list, &ee->u.siblings);
104
105 return elem;
106}
107
108
109void add_filedesc(struct osim_file_desc *root, const struct osim_file_desc *in, int num)
110{
111 int i;
112
113 for (i = 0; i < num; i++) {
114 struct osim_file_desc *ofd = talloc_memdup(root, &in[i], sizeof(*in));
115 llist_add_tail(&ofd->list, &root->child_list);
116 }
117}
118
119struct osim_file_desc *alloc_df(void *ctx, uint16_t fid, const char *name)
120{
121 struct osim_file_desc *mf;
122
123 mf = talloc_zero(ctx, struct osim_file_desc);
124 mf->type = TYPE_DF;
125 mf->fid = fid;
126 mf->short_name = name;
127 INIT_LLIST_HEAD(&mf->child_list);
128
129 return mf;
130}
131
132struct osim_file_desc *
133add_df_with_ef(struct osim_file_desc *parent,
134 uint16_t fid, const char *name,
135 const struct osim_file_desc *in, int num)
136{
137 struct osim_file_desc *df;
138
139 df = alloc_df(parent, fid, name);
140 df->parent = parent;
141 llist_add_tail(&df->list, &parent->child_list);
142 add_filedesc(df, in, num);
143
144 return df;
145}
146
147struct osim_file_desc *
148add_adf_with_ef(struct osim_file_desc *parent,
149 const uint8_t *adf_name, uint8_t adf_name_len,
150 const char *name, const struct osim_file_desc *in,
151 int num)
152{
153 struct osim_file_desc *df;
154
155 df = alloc_df(parent, 0xffff, name);
156 df->type = TYPE_ADF;
157 df->df_name = adf_name;
158 df->df_name_len = adf_name_len;
159 df->parent = parent;
160 llist_add_tail(&df->list, &parent->child_list);
161 add_filedesc(df, in, num);
162
163 return df;
164}
165
166struct osim_file_desc *
167osim_file_find_name(struct osim_file_desc *parent, const char *name)
168{
169 struct osim_file_desc *ofd;
170 llist_for_each_entry(ofd, &parent->child_list, list) {
171 if (!strcmp(ofd->short_name, name)) {
172 return ofd;
173 }
174 }
175 return NULL;
176}
177
Kevin Redon43eabee2012-09-16 18:40:02 +0200178/*! \brief Generate an APDU message and initialize APDU command header
179 * \param[in] cla CLASS byte
180 * \param[in] ins INSTRUCTION byte
181 * \param[in] p1 Parameter 1 byte
182 * \param[in] p2 Parameter 2 byte
183 * \param[in] lc number of bytes in the command data field Nc, which will encoded in 0, 1 or 3 bytes into Lc
184 * \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
185 * \returns an APDU message generated using provided APDU parameters
186 *
187 * This function generates an APDU message, as defined in ISO/IEC 7816-4:2005(E) ยง5.1.
188 * The APDU command header, command and response fields lengths are initialized using the parameters.
189 * The APDU case is determined by the command and response fields lengths.
Kevin Redon0f0ee322012-09-11 11:40:41 +0200190 */
Harald Welted54c2ee2012-01-17 18:25:50 +0100191struct msgb *osim_new_apdumsg(uint8_t cla, uint8_t ins, uint8_t p1,
192 uint8_t p2, uint16_t lc, uint16_t le)
193{
194 struct osim_apdu_cmd_hdr *ch;
195 struct msgb *msg = msgb_alloc(lc+le+sizeof(*ch)+2, "APDU");
196 if (!msg)
197 return NULL;
198
199 ch = (struct osim_apdu_cmd_hdr *) msgb_put(msg, sizeof(*ch));
Harald Welte76749602012-09-19 20:55:54 +0200200 msg->l2h = (uint8_t *) ch;
Harald Welted54c2ee2012-01-17 18:25:50 +0100201
202 ch->cla = cla;
203 ch->ins = ins;
204 ch->p1 = p1;
205 ch->p2 = p2;
206
207 msgb_apdu_lc(msg) = lc;
208 msgb_apdu_le(msg) = le;
209
210 if (lc == 0 && le == 0)
211 msgb_apdu_case(msg) = APDU_CASE_1;
212 else if (lc == 0 && le >= 1) {
213 if (le <= 256)
Kevin Redone07967f2012-09-11 11:44:18 +0200214 msgb_apdu_case(msg) = APDU_CASE_2S;
Harald Welted54c2ee2012-01-17 18:25:50 +0100215 else
Kevin Redone07967f2012-09-11 11:44:18 +0200216 msgb_apdu_case(msg) = APDU_CASE_2E;
Harald Welted54c2ee2012-01-17 18:25:50 +0100217 } else if (le == 0 && lc >= 1) {
218 if (lc <= 255)
Kevin Redone07967f2012-09-11 11:44:18 +0200219 msgb_apdu_case(msg) = APDU_CASE_3S;
Harald Welted54c2ee2012-01-17 18:25:50 +0100220 else
Kevin Redone07967f2012-09-11 11:44:18 +0200221 msgb_apdu_case(msg) = APDU_CASE_3E;
Harald Welted54c2ee2012-01-17 18:25:50 +0100222 } else if (lc >= 1 && le >= 1) {
Harald Welte76749602012-09-19 20:55:54 +0200223 if (lc <= 255 && le <= 256)
Kevin Redone07967f2012-09-11 11:44:18 +0200224 msgb_apdu_case(msg) = APDU_CASE_4S;
Harald Welted54c2ee2012-01-17 18:25:50 +0100225 else
Kevin Redone07967f2012-09-11 11:44:18 +0200226 msgb_apdu_case(msg) = APDU_CASE_4E;
Harald Welted54c2ee2012-01-17 18:25:50 +0100227 }
228
229 return msg;
230}
Harald Welte76749602012-09-19 20:55:54 +0200231
232/* FIXME: do we want to mark this as __thread? */
233static char sw_print_buf[256];
234
235char *osim_print_sw(const struct osim_card_hdl *ch, uint16_t sw_in)
236{
237 const struct osim_card_sw *csw;
238
239 if (!ch || !ch->prof)
240 goto ret_def;
241
242 csw = osim_find_sw(ch->prof, sw_in);
243 if (!csw)
244 goto ret_def;
245
246 switch (csw->type) {
247 case SW_TYPE_STR:
248 snprintf(sw_print_buf, sizeof(sw_print_buf),
249 "%04x (%s)", sw_in, csw->u.str);
250 break;
251 default:
252 goto ret_def;
253 }
254
255 sw_print_buf[sizeof(sw_print_buf)-1] = '\0';
256
257 return sw_print_buf;
258
259ret_def:
260 snprintf(sw_print_buf, sizeof(sw_print_buf),
261 "%04x (Unknown)", sw_in);
262 sw_print_buf[sizeof(sw_print_buf)-1] = '\0';
263
264 return sw_print_buf;
265}
266
267
268const struct osim_card_sw *osim_find_sw(const struct osim_card_profile *cp,
269 uint16_t sw_in)
270{
271 const struct osim_card_sw **sw_lists = cp->sws;
272 const struct osim_card_sw *sw_list, *sw;
273
274 for (sw_list = *sw_lists++; sw_list != NULL; sw = sw_list = *sw_lists++) {
275 for (sw = sw_list; sw->code != 0 && sw->mask != 0; sw++) {
276 if ((sw_in & sw->mask) == sw->code)
277 return sw;
278 }
279 }
280 return NULL;
281}
Harald Welted83d2962013-03-04 17:52:33 +0000282
283enum osim_card_sw_class osim_sw_class(const struct osim_card_profile *cp,
284 uint16_t sw_in)
285{
286 const struct osim_card_sw *csw = osim_find_sw(cp, sw_in);
287
288 if (!csw)
289 return SW_CLS_NONE;
290
291 return csw->class;
292}
Harald Welte30115db2014-05-04 16:30:46 +0200293
294int default_decode(struct osim_decoded_data *dd,
295 const struct osim_file_desc *desc,
296 int len, uint8_t *data)
297{
298 struct osim_decoded_element *elem;
299
300 elem = element_alloc(dd, "Unknown Payload", ELEM_T_BYTES, ELEM_REPR_HEX);
301 elem->u.buf = talloc_memdup(elem, data, len);
302
303 return 0;
304}