blob: ea3ce2a62267a8d2903e064b2e6608e088d3de68 [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>
Eric Wild94cd4ac2019-10-31 19:18:45 +010025#include <getopt.h>
Alexander Huemeraab4a242015-11-06 20:55:24 +010026#include <arpa/inet.h>
Harald Welted54c2ee2012-01-17 18:25:50 +010027
28#include <osmocom/core/msgb.h>
29#include <osmocom/core/talloc.h>
30#include <osmocom/sim/sim.h>
31#include <osmocom/gsm/tlv.h>
32
33
Harald Weltead418632012-09-10 10:49:59 +020034/* FIXME: this needs to be moved to card_fs_uicc.c */
Harald Welted54c2ee2012-01-17 18:25:50 +010035
36/* 11.1.1 */
37static struct msgb *_select_file(struct osim_chan_hdl *st, uint8_t p1, uint8_t p2,
38 const uint8_t *data, uint8_t data_len)
39{
Vadim Yanitskiy1cd99912017-05-15 21:37:16 +030040 struct msgb *msg;
Harald Weltef12d40f2017-02-08 15:46:53 +000041 uint8_t *dst;
Harald Welted54c2ee2012-01-17 18:25:50 +010042
43 msg = osim_new_apdumsg(0x00, 0xA4, p1, p2, data_len, 256);
44 dst = msgb_put(msg, data_len);
45 memcpy(dst, data, data_len);
46
47 osim_transceive_apdu(st, msg);
48
49 return msg;
50}
51
52/* 11.1.1 */
53static struct msgb *select_adf(struct osim_chan_hdl *st, const uint8_t *adf, uint8_t adf_len)
54{
Harald Welted54c2ee2012-01-17 18:25:50 +010055 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 */
Harald Weltef12d40f2017-02-08 15:46:53 +000067static int verify_pin(struct osim_chan_hdl *st, uint8_t pin_nr, char *pin)
Harald Welted54c2ee2012-01-17 18:25:50 +010068{
69 struct msgb *msg;
70 char *pindst;
Harald Welted54c2ee2012-01-17 18:25:50 +010071
72 if (strlen(pin) > 8)
73 return -EINVAL;
74
75 msg = osim_new_apdumsg(0x00, 0x20, 0x00, pin_nr, 8, 0);
Harald Weltef12d40f2017-02-08 15:46:53 +000076 pindst = (char *) msgb_put(msg, 8);
Harald Welted54c2ee2012-01-17 18:25:50 +010077 memset(pindst, 0xFF, 8);
Neels Hofmeyr95fdbc12018-07-26 17:14:40 +020078 /* Do not copy the terminating \0 */
79 memcpy(pindst, pin, strlen(pin));
Harald Welted54c2ee2012-01-17 18:25:50 +010080
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
Harald Welted54c2ee2012-01-17 18:25:50 +010096/* 11.1.3 */
97static struct msgb *read_binary(struct osim_chan_hdl *st, uint16_t offset, uint16_t len)
98{
99 struct msgb *msg;
100
101 if (offset > 0x7fff || len > 256)
102 return NULL;
103
104 msg = osim_new_apdumsg(0x00, 0xB0, offset >> 8, offset & 0xff, 0, len & 0xff);
105
106 osim_transceive_apdu(st, msg);
107
108 return msg;
109}
110
Harald Welted54c2ee2012-01-17 18:25:50 +0100111static int dump_fcp_template(struct tlv_parsed *tp)
112{
113 int i;
114
115 for (i = 0; i < ARRAY_SIZE(tp->lv); i++) {
116 if (TLVP_PRESENT(tp, i))
117 printf("Tag 0x%02x (%s): %s\n", i,
118 get_value_string(ts102221_fcp_vals, i),
119 osmo_hexdump(TLVP_VAL(tp, i), TLVP_LEN(tp, i)));
120 }
121
122 return 0;
123}
124
125static int dump_fcp_template_msg(struct msgb *msg)
126{
127 struct tlv_parsed tp;
128 int rc;
129
Harald Weltea5c92552012-09-10 21:05:42 +0200130 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 +0100131 if (rc < 0)
132 return rc;
133
134 return dump_fcp_template(&tp);
135}
136
137struct osim_fcp_fd_decoded {
138 enum osim_file_type type;
139 enum osim_ef_type ef_type;
140 uint16_t rec_len;
141 uint8_t num_rec;
142};
143
144static const enum osim_file_type iso2ftype[8] = {
145 [0] = TYPE_EF,
146 [1] = TYPE_EF_INT,
147 [7] = TYPE_DF,
148};
149
150static const enum osim_ef_type iso2eftype[8] = {
151 [1] = EF_TYPE_TRANSP,
152 [2] = EF_TYPE_RECORD_FIXED,
153 [6] = EF_TYPE_RECORD_CYCLIC,
154};
155
156static int osim_fcp_fd_decode(struct osim_fcp_fd_decoded *ofd, const uint8_t *fcp, int fcp_len)
157{
158 memset(ofd, 0, sizeof(*ofd));
159
160 if (fcp_len != 2 && fcp_len != 5)
161 return -EINVAL;
162
163 ofd->type = iso2ftype[(fcp[0] >> 3) & 7];
164 if (ofd->type != TYPE_DF)
165 ofd->ef_type = iso2eftype[fcp[0] & 7];
166
167 if (fcp[1] != 0x21)
168 return -EINVAL;
169
170 if (fcp_len >= 5) {
171 ofd->rec_len = ntohs(*(uint16_t *)(fcp+2));
172 ofd->num_rec = fcp[4];
173 }
174
175 return 0;
176}
177
178extern struct osim_card_profile *osim_cprof_usim(void *ctx);
179
180static struct msgb *try_select_adf_usim(struct osim_chan_hdl *st)
181{
182 struct tlv_parsed tp;
183 struct osim_fcp_fd_decoded ofd;
184 struct msgb *msg, *msg2;
185 uint8_t *cur;
186 int rc, i;
187
188 msg = select_file(st, 0x2f00);
Harald Weltea5c92552012-09-10 21:05:42 +0200189 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 +0100190 if (rc < 0)
191 return NULL;
192
193 dump_fcp_template(&tp);
194
195 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
196 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 5) {
197 msgb_free(msg);
198 return NULL;
199 }
200
201 rc = osim_fcp_fd_decode(&ofd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
202 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
203 if (rc < 0) {
204 msgb_free(msg);
205 return NULL;
206 }
207
208 if (ofd.type != TYPE_EF || ofd.ef_type != EF_TYPE_RECORD_FIXED) {
209 msgb_free(msg);
210 return NULL;
211 }
212
213 msgb_free(msg);
214
215 printf("ofd rec_len = %u, num_rec = %u\n", ofd.rec_len, ofd.num_rec);
216
217 for (i = 0; i < ofd.num_rec; i++) {
218 msg = read_record_nr(st, i+1, ofd.rec_len);
219 if (!msg)
220 return NULL;
221
222 cur = msgb_apdu_de(msg);
223 if (msgb_apdu_le(msg) < 5) {
224 msgb_free(msg);
225 return NULL;
226 }
227
228 if (cur[0] != 0x61 || cur[1] < 0x03 || cur[1] > 0x7f ||
229 cur[2] != 0x4F || cur[3] < 0x01 || cur[3] > 0x10) {
230 msgb_free(msg);
231 return NULL;
232 }
233
234 /* FIXME: actually check if it is an AID that we support, or
235 * iterate until we find one that we support */
236
237 msg2 = select_adf(st, cur+4, cur[3]);
238
239 /* attach the USIM profile, FIXME: do this based on AID match */
240 st->card->prof = osim_cprof_usim(st->card);
Harald Welte5ffb5032016-03-11 09:40:56 +0700241 st->cwd = osim_file_desc_find_name(st->card->prof->mf, "ADF.USIM");
Harald Welted54c2ee2012-01-17 18:25:50 +0100242
243 msgb_free(msg);
244
245 return msg2;
246 }
247
248 return NULL;
249}
250
251static int dump_file(struct osim_chan_hdl *chan, uint16_t fid)
252{
253 struct tlv_parsed tp;
254 struct osim_fcp_fd_decoded ffdd;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200255 struct msgb *msg, *rmsg;
256 int rc, i, offset;
Harald Welted54c2ee2012-01-17 18:25:50 +0100257
258 msg = select_file(chan, fid);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200259 if (!msg) {
260 printf("Unable to select file\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100261 return -EIO;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200262 }
Harald Welte76749602012-09-19 20:55:54 +0200263 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Weltea0ba4d92012-09-10 10:43:15 +0200264 if (msgb_apdu_sw(msg) != 0x9000) {
265 printf("status 0x%04x selecting file\n", msgb_apdu_sw(msg));
Harald Welted54c2ee2012-01-17 18:25:50 +0100266 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200267 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100268
Harald Weltea5c92552012-09-10 21:05:42 +0200269 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 +0200270 if (rc < 0) {
271 printf("Unable to parse FCP\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100272 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200273 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100274
275 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
Harald Weltea0ba4d92012-09-10 10:43:15 +0200276 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 2) {
277 printf("No file descriptor present ?!?\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100278 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200279 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100280
281 rc = osim_fcp_fd_decode(&ffdd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
282 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
Harald Weltea0ba4d92012-09-10 10:43:15 +0200283 if (rc < 0) {
284 printf("Unable to decode File Descriptor\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100285 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200286 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100287
Harald Weltea0ba4d92012-09-10 10:43:15 +0200288 if (ffdd.type != TYPE_EF) {
289 printf("File Type != EF\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100290 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200291 }
292
293 printf("EF type: %u\n", ffdd.ef_type);
Harald Welted54c2ee2012-01-17 18:25:50 +0100294
295 switch (ffdd.ef_type) {
296 case EF_TYPE_RECORD_FIXED:
297 for (i = 0; i < ffdd.num_rec; i++) {
Harald Weltea0ba4d92012-09-10 10:43:15 +0200298 rmsg = read_record_nr(chan, i+1, ffdd.rec_len);
Harald Welte95336312016-11-26 09:54:40 +0100299 if (!rmsg)
Harald Weltea0ba4d92012-09-10 10:43:15 +0200300 return -EIO;
Harald Welte76749602012-09-19 20:55:54 +0200301 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Welted54c2ee2012-01-17 18:25:50 +0100302 printf("Rec %03u: %s\n", i+1,
303 osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg)));
304 }
305 break;
306 case EF_TYPE_TRANSP:
Harald Weltea0ba4d92012-09-10 10:43:15 +0200307 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_SIZE))
308 goto out;
309 i = ntohs(*(uint16_t *)TLVP_VAL(&tp, UICC_FCP_T_FILE_SIZE));
310 printf("File size: %d bytes\n", i);
311
312 for (offset = 0; offset < i-1; ) {
313 uint16_t remain_len = i - offset;
314 uint16_t read_len = OSMO_MIN(remain_len, 256);
315 rmsg = read_binary(chan, offset, read_len);
Harald Welted6ec9842014-10-27 20:43:06 +0100316 if (!rmsg)
Harald Weltea0ba4d92012-09-10 10:43:15 +0200317 return -EIO;
318 offset += read_len;
319 printf("Content: %s\n",
320 osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg)));
321 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100322 break;
323 default:
324 goto out;
325 }
326
327out:
328 msgb_free(msg);
329 return -EINVAL;
330}
331
Eric Wild94cd4ac2019-10-31 19:18:45 +0100332static void print_help(void)
333{
334 printf( "osmo-sim-test Usage:\n"
335 " -h --help This message\n"
336 " -n --reader-num NR Open reader number NR\n"
337 );
338}
339
340static int readernum = 0;
341
342static void handle_options(int argc, char **argv)
343{
344 while (1) {
345 int option_index = 0, c;
346 const struct option long_options[] = {
347 { "help", 0, 0, 'h' },
348 { "reader-num", 1, 0, 'n' },
349 {0,0,0,0}
350 };
351
352 c = getopt_long(argc, argv, "hn:",
353 long_options, &option_index);
354 if (c == -1)
355 break;
356
357 switch (c) {
358 case 'h':
359 print_help();
360 exit(0);
361 break;
362 case 'n':
363 readernum = atoi(optarg);
364 break;
365 default:
366 exit(2);
367 break;
368 }
369 }
370
371 if (argc > optind) {
372 fprintf(stderr, "Unsupported positional arguments on command line\n");
373 exit(2);
374 }
375}
376
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100377
378static void iterate_fs(struct osim_chan_hdl *chan)
379{
380 const struct osim_file_desc *prev_cwd;
381 struct osim_file_desc *ofd;
382
383 /* iterate over all files in current working directory */
384 llist_for_each_entry(ofd, &chan->cwd->child_list, list) {
385 struct msgb *m;
386 printf("\n\n================ %s (%s) ==================\n",
387 ofd->short_name, ofd->long_name);
388
389 m = select_file(chan, ofd->fid);
390 if (msgb_apdu_sw(m) != 0x9000) {
391 msgb_free(m);
392 continue;
393 }
394 dump_fcp_template_msg(m);
395 msgb_free(m);
396
397 /* If this is a DF, recurse into it */
398 switch (ofd->type) {
399 case TYPE_DF:
400 /* the select above has just changed into this directory */
401 prev_cwd = chan->cwd;
402 chan->cwd = ofd;
403 iterate_fs(chan);
404 /* "pop" the directory from the stack */
405 chan->cwd = prev_cwd;
406 break;
407 default:
408 dump_file(chan, ofd->fid);
409 break;
410 }
411 }
412}
413
414
Harald Welted54c2ee2012-01-17 18:25:50 +0100415int main(int argc, char **argv)
416{
417 struct osim_reader_hdl *reader;
418 struct osim_card_hdl *card;
419 struct osim_chan_hdl *chan;
420 struct msgb *msg;
Harald Welted54c2ee2012-01-17 18:25:50 +0100421
Eric Wild94cd4ac2019-10-31 19:18:45 +0100422 handle_options(argc, argv);
423
424 reader = osim_reader_open(OSIM_READER_DRV_PCSC, readernum, "", NULL);
Harald Welted54c2ee2012-01-17 18:25:50 +0100425 if (!reader)
426 exit(1);
Harald Welte55790aa2014-10-26 18:46:50 +0100427 card = osim_card_open(reader, OSIM_PROTO_T0);
Harald Welted54c2ee2012-01-17 18:25:50 +0100428 if (!card)
429 exit(2);
430 chan = llist_entry(card->channels.next, struct osim_chan_hdl, list);
431 if (!chan)
432 exit(3);
433
434 msg = try_select_adf_usim(chan);
435 if (!msg || msgb_apdu_sw(msg) != 0x9000)
436 exit(4);
437 dump_fcp_template_msg(msg);
438 msgb_free(msg);
439
440 msg = select_file(chan, 0x6fc5);
441 dump_fcp_template_msg(msg);
Harald Welte76749602012-09-19 20:55:54 +0200442 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Welted54c2ee2012-01-17 18:25:50 +0100443 msgb_free(msg);
444
445 verify_pin(chan, 1, "1653");
446
447 msg = select_file(chan, 0x6f06);
448 dump_fcp_template_msg(msg);
449 msgb_free(msg);
450
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100451 iterate_fs(chan);
Harald Welted54c2ee2012-01-17 18:25:50 +0100452
453 exit(0);
454}