blob: f7dc48d8f519a69acb6354a30b7ef6107b81cc7e [file] [log] [blame]
Harald Weltead418632012-09-10 10:49:59 +02001/* libosmosim test application - currently simply dumps a USIM */
2/* (C) 2012 by Harald Welte <laforge@gnumonks.org>
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
Harald Welted54c2ee2012-01-17 18:25:50 +010021#include <stdio.h>
22#include <stdlib.h>
23#include <errno.h>
24#include <string.h>
25
26#include <osmocom/core/msgb.h>
27#include <osmocom/core/talloc.h>
28#include <osmocom/sim/sim.h>
29#include <osmocom/gsm/tlv.h>
30
31
Harald Weltead418632012-09-10 10:49:59 +020032/* FIXME: this needs to be moved to card_fs_uicc.c */
Harald Welted54c2ee2012-01-17 18:25:50 +010033
34/* 11.1.1 */
35static struct msgb *_select_file(struct osim_chan_hdl *st, uint8_t p1, uint8_t p2,
36 const uint8_t *data, uint8_t data_len)
37{
38 struct msgb *msg, *resp;
39 char *dst;
40
41 msg = osim_new_apdumsg(0x00, 0xA4, p1, p2, data_len, 256);
42 dst = msgb_put(msg, data_len);
43 memcpy(dst, data, data_len);
44
45 osim_transceive_apdu(st, msg);
46
47 return msg;
48}
49
50/* 11.1.1 */
51static struct msgb *select_adf(struct osim_chan_hdl *st, const uint8_t *adf, uint8_t adf_len)
52{
53 int sw;
54
55 return _select_file(st, 0x04, 0x04, adf,adf_len);
56}
57
58/* 11.1.1 */
59static struct msgb *select_file(struct osim_chan_hdl *st, uint16_t fid)
60{
61 uint16_t cfid = htons(fid);
62
63 return _select_file(st, 0x00, 0x04, (uint8_t *)&cfid, 2);
64}
65
66/* 11.1.9 */
67static int verify_pin(struct osim_chan_hdl *st, uint8_t pin_nr, uint8_t *pin)
68{
69 struct msgb *msg;
70 char *pindst;
71 int sw;
72
73 if (strlen(pin) > 8)
74 return -EINVAL;
75
76 msg = osim_new_apdumsg(0x00, 0x20, 0x00, pin_nr, 8, 0);
77 pindst = msgb_put(msg, 8);
78 memset(pindst, 0xFF, 8);
79 strncpy(pindst, pin, strlen(pin));
80
81 return osim_transceive_apdu(st, msg);
82}
83
84/* 11.1.5 */
85static struct msgb *read_record_nr(struct osim_chan_hdl *st, uint8_t rec_nr, uint16_t rec_size)
86{
87 struct msgb *msg;
88
89 msg = osim_new_apdumsg(0x00, 0xB2, rec_nr, 0x04, 0, rec_size);
90
91 osim_transceive_apdu(st, msg);
92
93 return msg;
94}
95
96/* 11.1.6 */
97static struct msgb *update_record_nr(struct osim_chan_hdl *st, uint8_t rec_nr,
98 const uint8_t *data, uint16_t rec_size)
99{
100 struct msgb *msg;
101 uint8_t *cur;
102
103 msg = osim_new_apdumsg(0x00, 0xDC, rec_nr, 0x04, rec_size, 0);
104 cur = msgb_put(msg, rec_size);
105 memcpy(cur, data, rec_size);
106
107 osim_transceive_apdu(st, msg);
108
109 return msg;
110}
111
112/* 11.1.3 */
113static struct msgb *read_binary(struct osim_chan_hdl *st, uint16_t offset, uint16_t len)
114{
115 struct msgb *msg;
116
117 if (offset > 0x7fff || len > 256)
118 return NULL;
119
120 msg = osim_new_apdumsg(0x00, 0xB0, offset >> 8, offset & 0xff, 0, len & 0xff);
121
122 osim_transceive_apdu(st, msg);
123
124 return msg;
125}
126
127/* 11.1.4 */
128static struct msgb *update_binary(struct osim_chan_hdl *st, uint16_t offset,
129 const uint8_t *data, uint16_t len)
130{
131 struct msgb *msg;
132 uint8_t *cur;
133
134 if (offset > 0x7fff || len > 256)
135 return NULL;
136
137 msg = osim_new_apdumsg(0x00, 0xD6, offset >> 8, offset & 0xff, len & 0xff, 0);
138 cur = msgb_put(msg, len);
139 memcpy(cur, data, len);
140
141 osim_transceive_apdu(st, msg);
142
143 return msg;
144}
145
Harald Weltead418632012-09-10 10:49:59 +0200146
147
Harald Welted54c2ee2012-01-17 18:25:50 +0100148static int dump_fcp_template(struct tlv_parsed *tp)
149{
150 int i;
151
152 for (i = 0; i < ARRAY_SIZE(tp->lv); i++) {
153 if (TLVP_PRESENT(tp, i))
154 printf("Tag 0x%02x (%s): %s\n", i,
155 get_value_string(ts102221_fcp_vals, i),
156 osmo_hexdump(TLVP_VAL(tp, i), TLVP_LEN(tp, i)));
157 }
158
159 return 0;
160}
161
162static int dump_fcp_template_msg(struct msgb *msg)
163{
164 struct tlv_parsed tp;
165 int rc;
166
167 rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-4, 0, 0);
168 if (rc < 0)
169 return rc;
170
171 return dump_fcp_template(&tp);
172}
173
174struct osim_fcp_fd_decoded {
175 enum osim_file_type type;
176 enum osim_ef_type ef_type;
177 uint16_t rec_len;
178 uint8_t num_rec;
179};
180
181static const enum osim_file_type iso2ftype[8] = {
182 [0] = TYPE_EF,
183 [1] = TYPE_EF_INT,
184 [7] = TYPE_DF,
185};
186
187static const enum osim_ef_type iso2eftype[8] = {
188 [1] = EF_TYPE_TRANSP,
189 [2] = EF_TYPE_RECORD_FIXED,
190 [6] = EF_TYPE_RECORD_CYCLIC,
191};
192
193static int osim_fcp_fd_decode(struct osim_fcp_fd_decoded *ofd, const uint8_t *fcp, int fcp_len)
194{
195 memset(ofd, 0, sizeof(*ofd));
196
197 if (fcp_len != 2 && fcp_len != 5)
198 return -EINVAL;
199
200 ofd->type = iso2ftype[(fcp[0] >> 3) & 7];
201 if (ofd->type != TYPE_DF)
202 ofd->ef_type = iso2eftype[fcp[0] & 7];
203
204 if (fcp[1] != 0x21)
205 return -EINVAL;
206
207 if (fcp_len >= 5) {
208 ofd->rec_len = ntohs(*(uint16_t *)(fcp+2));
209 ofd->num_rec = fcp[4];
210 }
211
212 return 0;
213}
214
215extern struct osim_card_profile *osim_cprof_usim(void *ctx);
216
217static struct msgb *try_select_adf_usim(struct osim_chan_hdl *st)
218{
219 struct tlv_parsed tp;
220 struct osim_fcp_fd_decoded ofd;
221 struct msgb *msg, *msg2;
222 uint8_t *cur;
223 int rc, i;
224
225 msg = select_file(st, 0x2f00);
226 rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-4, 0, 0);
227 if (rc < 0)
228 return NULL;
229
230 dump_fcp_template(&tp);
231
232 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
233 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 5) {
234 msgb_free(msg);
235 return NULL;
236 }
237
238 rc = osim_fcp_fd_decode(&ofd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
239 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
240 if (rc < 0) {
241 msgb_free(msg);
242 return NULL;
243 }
244
245 if (ofd.type != TYPE_EF || ofd.ef_type != EF_TYPE_RECORD_FIXED) {
246 msgb_free(msg);
247 return NULL;
248 }
249
250 msgb_free(msg);
251
252 printf("ofd rec_len = %u, num_rec = %u\n", ofd.rec_len, ofd.num_rec);
253
254 for (i = 0; i < ofd.num_rec; i++) {
255 msg = read_record_nr(st, i+1, ofd.rec_len);
256 if (!msg)
257 return NULL;
258
259 cur = msgb_apdu_de(msg);
260 if (msgb_apdu_le(msg) < 5) {
261 msgb_free(msg);
262 return NULL;
263 }
264
265 if (cur[0] != 0x61 || cur[1] < 0x03 || cur[1] > 0x7f ||
266 cur[2] != 0x4F || cur[3] < 0x01 || cur[3] > 0x10) {
267 msgb_free(msg);
268 return NULL;
269 }
270
271 /* FIXME: actually check if it is an AID that we support, or
272 * iterate until we find one that we support */
273
274 msg2 = select_adf(st, cur+4, cur[3]);
275
276 /* attach the USIM profile, FIXME: do this based on AID match */
277 st->card->prof = osim_cprof_usim(st->card);
278 st->cwd = osim_file_find_name(st->card->prof->mf, "ADF.USIM");
279
280 msgb_free(msg);
281
282 return msg2;
283 }
284
285 return NULL;
286}
287
288static int dump_file(struct osim_chan_hdl *chan, uint16_t fid)
289{
290 struct tlv_parsed tp;
291 struct osim_fcp_fd_decoded ffdd;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200292 struct msgb *msg, *rmsg;
293 int rc, i, offset;
Harald Welted54c2ee2012-01-17 18:25:50 +0100294
295 msg = select_file(chan, fid);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200296 if (!msg) {
297 printf("Unable to select file\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100298 return -EIO;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200299 }
300 if (msgb_apdu_sw(msg) != 0x9000) {
301 printf("status 0x%04x selecting file\n", msgb_apdu_sw(msg));
Harald Welted54c2ee2012-01-17 18:25:50 +0100302 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200303 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100304
305 rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-4, 0, 0);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200306 if (rc < 0) {
307 printf("Unable to parse FCP\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100308 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200309 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100310
311 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
Harald Weltea0ba4d92012-09-10 10:43:15 +0200312 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 2) {
313 printf("No file descriptor present ?!?\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100314 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200315 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100316
317 rc = osim_fcp_fd_decode(&ffdd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
318 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
Harald Weltea0ba4d92012-09-10 10:43:15 +0200319 if (rc < 0) {
320 printf("Unable to decode File Descriptor\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100321 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200322 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100323
Harald Weltea0ba4d92012-09-10 10:43:15 +0200324 if (ffdd.type != TYPE_EF) {
325 printf("File Type != EF\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100326 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200327 }
328
329 printf("EF type: %u\n", ffdd.ef_type);
Harald Welted54c2ee2012-01-17 18:25:50 +0100330
331 switch (ffdd.ef_type) {
332 case EF_TYPE_RECORD_FIXED:
333 for (i = 0; i < ffdd.num_rec; i++) {
Harald Weltea0ba4d92012-09-10 10:43:15 +0200334 rmsg = read_record_nr(chan, i+1, ffdd.rec_len);
Harald Welted54c2ee2012-01-17 18:25:50 +0100335 if (!msg)
Harald Weltea0ba4d92012-09-10 10:43:15 +0200336 return -EIO;
Harald Welted54c2ee2012-01-17 18:25:50 +0100337 printf("Rec %03u: %s\n", i+1,
338 osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg)));
339 }
340 break;
341 case EF_TYPE_TRANSP:
Harald Weltea0ba4d92012-09-10 10:43:15 +0200342 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_SIZE))
343 goto out;
344 i = ntohs(*(uint16_t *)TLVP_VAL(&tp, UICC_FCP_T_FILE_SIZE));
345 printf("File size: %d bytes\n", i);
346
347 for (offset = 0; offset < i-1; ) {
348 uint16_t remain_len = i - offset;
349 uint16_t read_len = OSMO_MIN(remain_len, 256);
350 rmsg = read_binary(chan, offset, read_len);
351 if (!msg)
352 return -EIO;
353 offset += read_len;
354 printf("Content: %s\n",
355 osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg)));
356 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100357 break;
358 default:
359 goto out;
360 }
361
362out:
363 msgb_free(msg);
364 return -EINVAL;
365}
366
367int main(int argc, char **argv)
368{
369 struct osim_reader_hdl *reader;
370 struct osim_card_hdl *card;
371 struct osim_chan_hdl *chan;
372 struct msgb *msg;
373 int rc;
374
375 reader = osim_reader_open(0, NULL);
376 if (!reader)
377 exit(1);
378 card = osim_card_open(reader);
379 if (!card)
380 exit(2);
381 chan = llist_entry(card->channels.next, struct osim_chan_hdl, list);
382 if (!chan)
383 exit(3);
384
385 msg = try_select_adf_usim(chan);
386 if (!msg || msgb_apdu_sw(msg) != 0x9000)
387 exit(4);
388 dump_fcp_template_msg(msg);
389 msgb_free(msg);
390
391 msg = select_file(chan, 0x6fc5);
392 dump_fcp_template_msg(msg);
393 msgb_free(msg);
394
395 verify_pin(chan, 1, "1653");
396
397 msg = select_file(chan, 0x6f06);
398 dump_fcp_template_msg(msg);
399 msgb_free(msg);
400
Harald Welted54c2ee2012-01-17 18:25:50 +0100401 {
402 struct osim_file_desc *ofd;
403 llist_for_each_entry(ofd, &chan->cwd->child_list, list) {
404 struct msgb *m;
405 printf("\n\n================ %s (%s) ==================\n",
406 ofd->short_name, ofd->long_name);
407
408 m = select_file(chan, ofd->fid);
409 dump_fcp_template_msg(m);
410 msgb_free(m);
411 dump_file(chan, ofd->fid);
412 }
413 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100414
415 exit(0);
416}