blob: 244c2df35b79c5d88b19110978bf0e0bd61ea8b5 [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
Harald Weltea5c92552012-09-10 21:05:42 +0200167 rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-2, 0, 0);
Harald Welted54c2ee2012-01-17 18:25:50 +0100168 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);
Harald Weltea5c92552012-09-10 21:05:42 +0200226 rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-2, 0, 0);
Harald Welted54c2ee2012-01-17 18:25:50 +0100227 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 }
Harald Welte76749602012-09-19 20:55:54 +0200300 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Weltea0ba4d92012-09-10 10:43:15 +0200301 if (msgb_apdu_sw(msg) != 0x9000) {
302 printf("status 0x%04x selecting file\n", msgb_apdu_sw(msg));
Harald Welted54c2ee2012-01-17 18:25:50 +0100303 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200304 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100305
Harald Weltea5c92552012-09-10 21:05:42 +0200306 rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-2, 0, 0);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200307 if (rc < 0) {
308 printf("Unable to parse FCP\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100309 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200310 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100311
312 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
Harald Weltea0ba4d92012-09-10 10:43:15 +0200313 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 2) {
314 printf("No file descriptor present ?!?\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100315 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200316 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100317
318 rc = osim_fcp_fd_decode(&ffdd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
319 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
Harald Weltea0ba4d92012-09-10 10:43:15 +0200320 if (rc < 0) {
321 printf("Unable to decode File Descriptor\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100322 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200323 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100324
Harald Weltea0ba4d92012-09-10 10:43:15 +0200325 if (ffdd.type != TYPE_EF) {
326 printf("File Type != EF\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100327 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200328 }
329
330 printf("EF type: %u\n", ffdd.ef_type);
Harald Welted54c2ee2012-01-17 18:25:50 +0100331
332 switch (ffdd.ef_type) {
333 case EF_TYPE_RECORD_FIXED:
334 for (i = 0; i < ffdd.num_rec; i++) {
Harald Weltea0ba4d92012-09-10 10:43:15 +0200335 rmsg = read_record_nr(chan, i+1, ffdd.rec_len);
Harald Welted54c2ee2012-01-17 18:25:50 +0100336 if (!msg)
Harald Weltea0ba4d92012-09-10 10:43:15 +0200337 return -EIO;
Harald Welte76749602012-09-19 20:55:54 +0200338 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Welted54c2ee2012-01-17 18:25:50 +0100339 printf("Rec %03u: %s\n", i+1,
340 osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg)));
341 }
342 break;
343 case EF_TYPE_TRANSP:
Harald Weltea0ba4d92012-09-10 10:43:15 +0200344 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_SIZE))
345 goto out;
346 i = ntohs(*(uint16_t *)TLVP_VAL(&tp, UICC_FCP_T_FILE_SIZE));
347 printf("File size: %d bytes\n", i);
348
349 for (offset = 0; offset < i-1; ) {
350 uint16_t remain_len = i - offset;
351 uint16_t read_len = OSMO_MIN(remain_len, 256);
352 rmsg = read_binary(chan, offset, read_len);
353 if (!msg)
354 return -EIO;
355 offset += read_len;
356 printf("Content: %s\n",
357 osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg)));
358 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100359 break;
360 default:
361 goto out;
362 }
363
364out:
365 msgb_free(msg);
366 return -EINVAL;
367}
368
369int main(int argc, char **argv)
370{
371 struct osim_reader_hdl *reader;
372 struct osim_card_hdl *card;
373 struct osim_chan_hdl *chan;
374 struct msgb *msg;
375 int rc;
376
Harald Welte55790aa2014-10-26 18:46:50 +0100377 reader = osim_reader_open(OSIM_READER_DRV_PCSC, 0, "", NULL);
Harald Welted54c2ee2012-01-17 18:25:50 +0100378 if (!reader)
379 exit(1);
Harald Welte55790aa2014-10-26 18:46:50 +0100380 card = osim_card_open(reader, OSIM_PROTO_T0);
Harald Welted54c2ee2012-01-17 18:25:50 +0100381 if (!card)
382 exit(2);
383 chan = llist_entry(card->channels.next, struct osim_chan_hdl, list);
384 if (!chan)
385 exit(3);
386
387 msg = try_select_adf_usim(chan);
388 if (!msg || msgb_apdu_sw(msg) != 0x9000)
389 exit(4);
390 dump_fcp_template_msg(msg);
391 msgb_free(msg);
392
393 msg = select_file(chan, 0x6fc5);
394 dump_fcp_template_msg(msg);
Harald Welte76749602012-09-19 20:55:54 +0200395 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Welted54c2ee2012-01-17 18:25:50 +0100396 msgb_free(msg);
397
398 verify_pin(chan, 1, "1653");
399
400 msg = select_file(chan, 0x6f06);
401 dump_fcp_template_msg(msg);
402 msgb_free(msg);
403
Harald Welted54c2ee2012-01-17 18:25:50 +0100404 {
405 struct osim_file_desc *ofd;
406 llist_for_each_entry(ofd, &chan->cwd->child_list, list) {
407 struct msgb *m;
408 printf("\n\n================ %s (%s) ==================\n",
409 ofd->short_name, ofd->long_name);
410
411 m = select_file(chan, ofd->fid);
412 dump_fcp_template_msg(m);
413 msgb_free(m);
414 dump_file(chan, ofd->fid);
415 }
416 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100417
418 exit(0);
419}