blob: a9ab0d027ed247e5573577ade93363d73e9d95a1 [file] [log] [blame]
Harald Weltead418632012-09-10 10:49:59 +02001/* libosmosim test application - currently simply dumps a USIM */
Harald Welte870f94d2020-03-19 19:10:34 +01002/* (C) 2012-2020 by Harald Welte <laforge@gnumonks.org>
Harald Weltead418632012-09-10 10:49:59 +02003 * 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
Harald Welte870f94d2020-03-19 19:10:34 +010028#include <unistd.h>
29#include <sys/stat.h>
30#include <sys/types.h>
31#include <fcntl.h>
32#include <limits.h>
33
Harald Welted54c2ee2012-01-17 18:25:50 +010034#include <osmocom/core/msgb.h>
35#include <osmocom/core/talloc.h>
36#include <osmocom/sim/sim.h>
37#include <osmocom/gsm/tlv.h>
38
39
Harald Weltead418632012-09-10 10:49:59 +020040/* FIXME: this needs to be moved to card_fs_uicc.c */
Harald Welted54c2ee2012-01-17 18:25:50 +010041
Harald Welte053bebc2020-02-15 18:58:16 +010042static uint8_t g_class = 0x00; /* UICC/USIM */
Harald Welte870f94d2020-03-19 19:10:34 +010043static const char *g_output_dir;
Harald Welte053bebc2020-02-15 18:58:16 +010044
Harald Welted54c2ee2012-01-17 18:25:50 +010045/* 11.1.1 */
46static struct msgb *_select_file(struct osim_chan_hdl *st, uint8_t p1, uint8_t p2,
47 const uint8_t *data, uint8_t data_len)
48{
Vadim Yanitskiy1cd99912017-05-15 21:37:16 +030049 struct msgb *msg;
Harald Weltef12d40f2017-02-08 15:46:53 +000050 uint8_t *dst;
Harald Welted54c2ee2012-01-17 18:25:50 +010051
Harald Welte053bebc2020-02-15 18:58:16 +010052 msg = osim_new_apdumsg(g_class, 0xA4, p1, p2, data_len, 256);
Harald Welted54c2ee2012-01-17 18:25:50 +010053 dst = msgb_put(msg, data_len);
54 memcpy(dst, data, data_len);
55
56 osim_transceive_apdu(st, msg);
57
58 return msg;
59}
60
61/* 11.1.1 */
62static struct msgb *select_adf(struct osim_chan_hdl *st, const uint8_t *adf, uint8_t adf_len)
63{
Harald Welted54c2ee2012-01-17 18:25:50 +010064 return _select_file(st, 0x04, 0x04, adf,adf_len);
65}
66
67/* 11.1.1 */
68static struct msgb *select_file(struct osim_chan_hdl *st, uint16_t fid)
69{
70 uint16_t cfid = htons(fid);
Harald Welte053bebc2020-02-15 18:58:16 +010071 uint8_t p2 = 0x04;
Harald Welted54c2ee2012-01-17 18:25:50 +010072
Harald Welte053bebc2020-02-15 18:58:16 +010073 /* Classic SIM cards don't support 0x04 (Return FCP) */
74 if (g_class == 0xA0)
75 p2 = 0x00;
76
77 return _select_file(st, 0x00, p2, (uint8_t *)&cfid, 2);
Harald Welted54c2ee2012-01-17 18:25:50 +010078}
79
80/* 11.1.9 */
Harald Weltef12d40f2017-02-08 15:46:53 +000081static int verify_pin(struct osim_chan_hdl *st, uint8_t pin_nr, char *pin)
Harald Welted54c2ee2012-01-17 18:25:50 +010082{
83 struct msgb *msg;
84 char *pindst;
Harald Welted54c2ee2012-01-17 18:25:50 +010085
86 if (strlen(pin) > 8)
87 return -EINVAL;
88
Harald Welte053bebc2020-02-15 18:58:16 +010089 msg = osim_new_apdumsg(g_class, 0x20, 0x00, pin_nr, 8, 0);
Harald Weltef12d40f2017-02-08 15:46:53 +000090 pindst = (char *) msgb_put(msg, 8);
Harald Welted54c2ee2012-01-17 18:25:50 +010091 memset(pindst, 0xFF, 8);
Neels Hofmeyr95fdbc12018-07-26 17:14:40 +020092 /* Do not copy the terminating \0 */
93 memcpy(pindst, pin, strlen(pin));
Harald Welted54c2ee2012-01-17 18:25:50 +010094
95 return osim_transceive_apdu(st, msg);
96}
97
98/* 11.1.5 */
99static struct msgb *read_record_nr(struct osim_chan_hdl *st, uint8_t rec_nr, uint16_t rec_size)
100{
101 struct msgb *msg;
102
Harald Welte053bebc2020-02-15 18:58:16 +0100103 msg = osim_new_apdumsg(g_class, 0xB2, rec_nr, 0x04, 0, rec_size);
Harald Welted54c2ee2012-01-17 18:25:50 +0100104
105 osim_transceive_apdu(st, msg);
106
107 return msg;
108}
109
Harald Welted54c2ee2012-01-17 18:25:50 +0100110/* 11.1.3 */
111static struct msgb *read_binary(struct osim_chan_hdl *st, uint16_t offset, uint16_t len)
112{
113 struct msgb *msg;
114
115 if (offset > 0x7fff || len > 256)
116 return NULL;
117
Harald Welte053bebc2020-02-15 18:58:16 +0100118 msg = osim_new_apdumsg(g_class, 0xB0, offset >> 8, offset & 0xff, 0, len & 0xff);
Harald Welted54c2ee2012-01-17 18:25:50 +0100119
120 osim_transceive_apdu(st, msg);
121
122 return msg;
123}
124
Harald Welted54c2ee2012-01-17 18:25:50 +0100125static int dump_fcp_template(struct tlv_parsed *tp)
126{
127 int i;
128
129 for (i = 0; i < ARRAY_SIZE(tp->lv); i++) {
130 if (TLVP_PRESENT(tp, i))
131 printf("Tag 0x%02x (%s): %s\n", i,
132 get_value_string(ts102221_fcp_vals, i),
133 osmo_hexdump(TLVP_VAL(tp, i), TLVP_LEN(tp, i)));
134 }
135
136 return 0;
137}
138
139static int dump_fcp_template_msg(struct msgb *msg)
140{
141 struct tlv_parsed tp;
142 int rc;
143
Harald Weltea5c92552012-09-10 21:05:42 +0200144 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 +0100145 if (rc < 0)
146 return rc;
147
148 return dump_fcp_template(&tp);
149}
150
151struct osim_fcp_fd_decoded {
152 enum osim_file_type type;
153 enum osim_ef_type ef_type;
154 uint16_t rec_len;
155 uint8_t num_rec;
156};
157
158static const enum osim_file_type iso2ftype[8] = {
159 [0] = TYPE_EF,
160 [1] = TYPE_EF_INT,
161 [7] = TYPE_DF,
162};
163
164static const enum osim_ef_type iso2eftype[8] = {
165 [1] = EF_TYPE_TRANSP,
166 [2] = EF_TYPE_RECORD_FIXED,
167 [6] = EF_TYPE_RECORD_CYCLIC,
168};
169
170static int osim_fcp_fd_decode(struct osim_fcp_fd_decoded *ofd, const uint8_t *fcp, int fcp_len)
171{
172 memset(ofd, 0, sizeof(*ofd));
173
174 if (fcp_len != 2 && fcp_len != 5)
175 return -EINVAL;
176
177 ofd->type = iso2ftype[(fcp[0] >> 3) & 7];
178 if (ofd->type != TYPE_DF)
179 ofd->ef_type = iso2eftype[fcp[0] & 7];
180
181 if (fcp[1] != 0x21)
182 return -EINVAL;
183
184 if (fcp_len >= 5) {
185 ofd->rec_len = ntohs(*(uint16_t *)(fcp+2));
186 ofd->num_rec = fcp[4];
187 }
188
189 return 0;
190}
191
Harald Welte053bebc2020-02-15 18:58:16 +0100192/* TS 51.011 Section 9.3 Type of File */
193static const enum osim_file_type sim2ftype[8] = {
194 [1] = TYPE_MF,
195 [2] = TYPE_DF,
196 [4] = TYPE_EF,
197};
198
199/* TS 51.011 Section 9.3 Structure of File */
200static const enum osim_ef_type sim2eftype[8] = {
201 [0] = EF_TYPE_TRANSP,
202 [1] = EF_TYPE_RECORD_FIXED,
203 [3] = EF_TYPE_RECORD_CYCLIC,
204};
205
206/* TS 51.011 Section 9.2.1 */
207static int osim_fcp_fd_decode_sim(struct osim_fcp_fd_decoded *ofd, const uint8_t *fcp, int fcp_len)
208{
209 memset(ofd, 0, sizeof(*ofd));
210
211 if (fcp_len < 14)
212 return -EINVAL;
213
214 ofd->type = sim2ftype[fcp[6] & 7];
215 switch (ofd->type) {
216 case TYPE_EF:
217 ofd->ef_type = sim2eftype[fcp[13] & 7];
218 if (fcp_len < 13 + fcp[12])
219 return -EINVAL;
220 switch (ofd->ef_type) {
221 case EF_TYPE_RECORD_FIXED:
222 case EF_TYPE_RECORD_CYCLIC:
223 if (fcp_len < 15)
224 return -EINVAL;
225 ofd->rec_len = fcp[14];
226 ofd->num_rec = ntohs(*(uint16_t *)(fcp+2)) / ofd->rec_len;
227 break;
228 default:
229 break;
230 }
231 break;
232 case TYPE_MF:
233 case TYPE_DF:
234 if (fcp_len < 22)
235 return -EINVAL;
236 break;
237 default:
238 break;
239 }
240
241 return 0;
242}
243
244extern struct osim_card_profile *osim_cprof_sim(void *ctx);
Harald Welted54c2ee2012-01-17 18:25:50 +0100245extern struct osim_card_profile *osim_cprof_usim(void *ctx);
Harald Welte053bebc2020-02-15 18:58:16 +0100246extern struct osim_card_profile *osim_cprof_isim(void *ctx);
Harald Welted54c2ee2012-01-17 18:25:50 +0100247
248static struct msgb *try_select_adf_usim(struct osim_chan_hdl *st)
249{
250 struct tlv_parsed tp;
251 struct osim_fcp_fd_decoded ofd;
252 struct msgb *msg, *msg2;
253 uint8_t *cur;
254 int rc, i;
255
256 msg = select_file(st, 0x2f00);
Harald Welte053bebc2020-02-15 18:58:16 +0100257 if (!msg)
Harald Welted54c2ee2012-01-17 18:25:50 +0100258 return NULL;
Harald Welte053bebc2020-02-15 18:58:16 +0100259 /* return status word in case of error */
260 if (msgb_apdu_sw(msg) != 0x9000)
261 return msg;
262
263 rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-2, 0, 0);
264 if (rc < 0) {
265 msgb_free(msg);
266 return NULL;
267 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100268
269 dump_fcp_template(&tp);
270
271 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
272 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 5) {
273 msgb_free(msg);
274 return NULL;
275 }
276
277 rc = osim_fcp_fd_decode(&ofd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
278 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
279 if (rc < 0) {
280 msgb_free(msg);
281 return NULL;
282 }
283
284 if (ofd.type != TYPE_EF || ofd.ef_type != EF_TYPE_RECORD_FIXED) {
285 msgb_free(msg);
286 return NULL;
287 }
288
289 msgb_free(msg);
290
291 printf("ofd rec_len = %u, num_rec = %u\n", ofd.rec_len, ofd.num_rec);
292
293 for (i = 0; i < ofd.num_rec; i++) {
294 msg = read_record_nr(st, i+1, ofd.rec_len);
295 if (!msg)
296 return NULL;
297
298 cur = msgb_apdu_de(msg);
299 if (msgb_apdu_le(msg) < 5) {
300 msgb_free(msg);
301 return NULL;
302 }
303
304 if (cur[0] != 0x61 || cur[1] < 0x03 || cur[1] > 0x7f ||
305 cur[2] != 0x4F || cur[3] < 0x01 || cur[3] > 0x10) {
306 msgb_free(msg);
307 return NULL;
308 }
309
310 /* FIXME: actually check if it is an AID that we support, or
311 * iterate until we find one that we support */
312
313 msg2 = select_adf(st, cur+4, cur[3]);
314
315 /* attach the USIM profile, FIXME: do this based on AID match */
316 st->card->prof = osim_cprof_usim(st->card);
Harald Welte5ffb5032016-03-11 09:40:56 +0700317 st->cwd = osim_file_desc_find_name(st->card->prof->mf, "ADF.USIM");
Harald Welted54c2ee2012-01-17 18:25:50 +0100318
319 msgb_free(msg);
320
321 return msg2;
322 }
323
324 return NULL;
325}
326
Harald Welte870f94d2020-03-19 19:10:34 +0100327static int dump_file(struct osim_chan_hdl *chan, const char *short_name, uint16_t fid)
Harald Welted54c2ee2012-01-17 18:25:50 +0100328{
329 struct tlv_parsed tp;
330 struct osim_fcp_fd_decoded ffdd;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200331 struct msgb *msg, *rmsg;
332 int rc, i, offset;
Harald Welte870f94d2020-03-19 19:10:34 +0100333 FILE *f_data = NULL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100334
Harald Welte870f94d2020-03-19 19:10:34 +0100335 /* Select the file */
Harald Welted54c2ee2012-01-17 18:25:50 +0100336 msg = select_file(chan, fid);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200337 if (!msg) {
Harald Welte835ed962020-03-19 18:00:04 +0100338 fprintf(stderr, "Unable to select file\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100339 return -EIO;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200340 }
341 if (msgb_apdu_sw(msg) != 0x9000) {
Harald Welte835ed962020-03-19 18:00:04 +0100342 fprintf(stderr, "status 0x%04x selecting file\n", msgb_apdu_sw(msg));
Harald Welted54c2ee2012-01-17 18:25:50 +0100343 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200344 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100345
Harald Welte053bebc2020-02-15 18:58:16 +0100346 if (g_class != 0xA0) {
347 rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-2, 0, 0);
348 if (rc < 0) {
Harald Welte835ed962020-03-19 18:00:04 +0100349 fprintf(stderr, "Unable to parse FCP: %s\n", msgb_hexdump(msg));
Harald Welte053bebc2020-02-15 18:58:16 +0100350 goto out;
351 }
352
353 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
354 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 2) {
Harald Welte835ed962020-03-19 18:00:04 +0100355 fprintf(stderr, "No file descriptor present ?!?\n");
Harald Welte053bebc2020-02-15 18:58:16 +0100356 goto out;
357 }
358
359 rc = osim_fcp_fd_decode(&ffdd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
360 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
361 } else {
362 rc = osim_fcp_fd_decode_sim(&ffdd, msgb_apdu_de(msg), msgb_apdu_le(msg));
Harald Weltea0ba4d92012-09-10 10:43:15 +0200363 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100364
Harald Weltea0ba4d92012-09-10 10:43:15 +0200365 if (rc < 0) {
Harald Welte835ed962020-03-19 18:00:04 +0100366 fprintf(stderr, "Unable to decode File Descriptor\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100367 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200368 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100369
Harald Weltea0ba4d92012-09-10 10:43:15 +0200370 if (ffdd.type != TYPE_EF) {
Harald Welte835ed962020-03-19 18:00:04 +0100371 fprintf(stderr, "File Type != EF\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100372 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200373 }
374
Harald Welte870f94d2020-03-19 19:10:34 +0100375 if (g_output_dir) {
376 f_data = fopen(short_name, "w");
377 if (!f_data) {
378 fprintf(stderr, "Couldn't create '%s': %s\n", short_name, strerror(errno));
379 goto out;
380 }
381 }
382
Harald Weltea0ba4d92012-09-10 10:43:15 +0200383 printf("EF type: %u\n", ffdd.ef_type);
Harald Welted54c2ee2012-01-17 18:25:50 +0100384
385 switch (ffdd.ef_type) {
386 case EF_TYPE_RECORD_FIXED:
387 for (i = 0; i < ffdd.num_rec; i++) {
Harald Welte870f94d2020-03-19 19:10:34 +0100388 const char *hex;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200389 rmsg = read_record_nr(chan, i+1, ffdd.rec_len);
Harald Welte870f94d2020-03-19 19:10:34 +0100390 if (!rmsg) {
391 if (f_data)
392 fclose(f_data);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200393 return -EIO;
Harald Welte870f94d2020-03-19 19:10:34 +0100394 }
Harald Welte76749602012-09-19 20:55:54 +0200395 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Welte870f94d2020-03-19 19:10:34 +0100396
397 hex = osmo_hexdump_nospc(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg));
398 printf("Rec %03u: %s\n", i+1, hex);
399 if (f_data)
400 fprintf(f_data, "%s\n", hex);
Harald Welted54c2ee2012-01-17 18:25:50 +0100401 }
402 break;
403 case EF_TYPE_TRANSP:
Philipp Maierfde9fdc2020-02-26 12:00:23 +0100404 if (g_class != 0xA0) {
Harald Welte870f94d2020-03-19 19:10:34 +0100405 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_SIZE)) {
406 if (f_data)
407 fclose(f_data);
Philipp Maierfde9fdc2020-02-26 12:00:23 +0100408 goto out;
Harald Welte870f94d2020-03-19 19:10:34 +0100409 }
Philipp Maierfde9fdc2020-02-26 12:00:23 +0100410 i = ntohs(*(uint16_t *)TLVP_VAL(&tp, UICC_FCP_T_FILE_SIZE));
411 printf("File size: %d bytes\n", i);
412 } else {
Harald Welte835ed962020-03-19 18:00:04 +0100413 fprintf(stderr, "Can not determine file size, invalid EF-type!\n");
Harald Weltea0ba4d92012-09-10 10:43:15 +0200414 goto out;
Philipp Maierfde9fdc2020-02-26 12:00:23 +0100415 }
Harald Weltea0ba4d92012-09-10 10:43:15 +0200416 for (offset = 0; offset < i-1; ) {
417 uint16_t remain_len = i - offset;
418 uint16_t read_len = OSMO_MIN(remain_len, 256);
Harald Welte870f94d2020-03-19 19:10:34 +0100419 const char *hex;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200420 rmsg = read_binary(chan, offset, read_len);
Harald Welte870f94d2020-03-19 19:10:34 +0100421 if (!rmsg) {
422 if (f_data)
423 fclose(f_data);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200424 return -EIO;
Harald Welte870f94d2020-03-19 19:10:34 +0100425 }
Harald Weltea0ba4d92012-09-10 10:43:15 +0200426 offset += read_len;
Harald Welte870f94d2020-03-19 19:10:34 +0100427 hex = osmo_hexdump_nospc(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg));
428 printf("Content: %s\n", hex);
429 if (f_data)
430 fprintf(f_data, "%s", hex);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200431 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100432 break;
433 default:
434 goto out;
435 }
436
437out:
Harald Welte870f94d2020-03-19 19:10:34 +0100438 if (f_data)
439 fclose(f_data);
Harald Welted54c2ee2012-01-17 18:25:50 +0100440 msgb_free(msg);
441 return -EINVAL;
Harald Welte870f94d2020-03-19 19:10:34 +0100442
Harald Welted54c2ee2012-01-17 18:25:50 +0100443}
444
Eric Wild94cd4ac2019-10-31 19:18:45 +0100445static void print_help(void)
446{
447 printf( "osmo-sim-test Usage:\n"
448 " -h --help This message\n"
449 " -n --reader-num NR Open reader number NR\n"
Harald Welte870f94d2020-03-19 19:10:34 +0100450 " -o --output-dir DIR To-be-created output directory for filesystem dump\n"
Eric Wild94cd4ac2019-10-31 19:18:45 +0100451 );
452}
453
454static int readernum = 0;
455
456static void handle_options(int argc, char **argv)
457{
458 while (1) {
459 int option_index = 0, c;
460 const struct option long_options[] = {
461 { "help", 0, 0, 'h' },
462 { "reader-num", 1, 0, 'n' },
Harald Welte870f94d2020-03-19 19:10:34 +0100463 { "output-dir", 1, 0, 'o' },
Eric Wild94cd4ac2019-10-31 19:18:45 +0100464 {0,0,0,0}
465 };
466
Harald Welte870f94d2020-03-19 19:10:34 +0100467 c = getopt_long(argc, argv, "hn:o:",
Eric Wild94cd4ac2019-10-31 19:18:45 +0100468 long_options, &option_index);
469 if (c == -1)
470 break;
471
472 switch (c) {
473 case 'h':
474 print_help();
475 exit(0);
476 break;
477 case 'n':
478 readernum = atoi(optarg);
479 break;
Harald Welte870f94d2020-03-19 19:10:34 +0100480 case 'o':
481 g_output_dir = optarg;
482 break;
Eric Wild94cd4ac2019-10-31 19:18:45 +0100483 default:
484 exit(2);
485 break;
486 }
487 }
488
489 if (argc > optind) {
490 fprintf(stderr, "Unsupported positional arguments on command line\n");
491 exit(2);
492 }
493}
494
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100495
Harald Welte870f94d2020-03-19 19:10:34 +0100496static void mkdir_and_chdir(const char *name, mode_t mode)
497{
498 int rc;
499 rc = mkdir(name, mode);
500 if (rc < 0) {
501 fprintf(stderr, "Cannot create '%s': %s\n", name, strerror(errno));
502 exit(24);
503 }
504 rc = chdir(name);
505 if (rc < 0) {
506 fprintf(stderr, "Cannot change to just-created '%s': %s\n", name, strerror(errno));
507 exit(24);
508 }
509}
510
511
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100512static void iterate_fs(struct osim_chan_hdl *chan)
513{
514 const struct osim_file_desc *prev_cwd;
515 struct osim_file_desc *ofd;
516
517 /* iterate over all files in current working directory */
518 llist_for_each_entry(ofd, &chan->cwd->child_list, list) {
519 struct msgb *m;
Harald Welte870f94d2020-03-19 19:10:34 +0100520 char prev_dir[PATH_MAX];
521
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100522 printf("\n\n================ %s (%s) ==================\n",
523 ofd->short_name, ofd->long_name);
524
525 m = select_file(chan, ofd->fid);
526 if (msgb_apdu_sw(m) != 0x9000) {
527 msgb_free(m);
528 continue;
529 }
530 dump_fcp_template_msg(m);
531 msgb_free(m);
532
533 /* If this is a DF, recurse into it */
534 switch (ofd->type) {
535 case TYPE_DF:
536 /* the select above has just changed into this directory */
537 prev_cwd = chan->cwd;
538 chan->cwd = ofd;
Harald Welte870f94d2020-03-19 19:10:34 +0100539 if (g_output_dir) {
540 if (!getcwd(prev_dir, sizeof(prev_dir))) {
541 fprintf(stderr, "Cannot determine cwd: %s\n", strerror(errno));
542 exit(23);
543 continue;
544 }
545 mkdir_and_chdir(ofd->short_name, 0750);
546 }
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100547 iterate_fs(chan);
548 /* "pop" the directory from the stack */
549 chan->cwd = prev_cwd;
Harald Welte870f94d2020-03-19 19:10:34 +0100550 if (g_output_dir)
551 OSMO_ASSERT(chdir("..") == 0);
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100552 break;
553 default:
Harald Welte870f94d2020-03-19 19:10:34 +0100554 dump_file(chan, ofd->short_name, ofd->fid);
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100555 break;
556 }
557 }
558}
559
560
Harald Welted54c2ee2012-01-17 18:25:50 +0100561int main(int argc, char **argv)
562{
563 struct osim_reader_hdl *reader;
564 struct osim_card_hdl *card;
565 struct osim_chan_hdl *chan;
566 struct msgb *msg;
Harald Welted54c2ee2012-01-17 18:25:50 +0100567
Eric Wild94cd4ac2019-10-31 19:18:45 +0100568 handle_options(argc, argv);
569
Harald Welte870f94d2020-03-19 19:10:34 +0100570 if (g_output_dir) {
571 int rc;
572 rc = mkdir(g_output_dir, 0750);
573 if (rc < 0) {
574 fprintf(stderr, "Cannot create directory '%s': %s\n", g_output_dir,
575 strerror(errno));
576 exit(5);
577 }
578 rc = chdir(g_output_dir);
579 if (rc < 0) {
580 fprintf(stderr, "Cannot change to just-created directory '%s': %s\n",
581 g_output_dir, strerror(errno));
582 exit(5);
583 }
584 }
585
Eric Wild94cd4ac2019-10-31 19:18:45 +0100586 reader = osim_reader_open(OSIM_READER_DRV_PCSC, readernum, "", NULL);
Harald Welted54c2ee2012-01-17 18:25:50 +0100587 if (!reader)
588 exit(1);
Harald Welte55790aa2014-10-26 18:46:50 +0100589 card = osim_card_open(reader, OSIM_PROTO_T0);
Harald Welted54c2ee2012-01-17 18:25:50 +0100590 if (!card)
591 exit(2);
592 chan = llist_entry(card->channels.next, struct osim_chan_hdl, list);
593 if (!chan)
594 exit(3);
595
596 msg = try_select_adf_usim(chan);
Harald Welte053bebc2020-02-15 18:58:16 +0100597 if (!msg) {
Harald Welted54c2ee2012-01-17 18:25:50 +0100598 exit(4);
Harald Welte053bebc2020-02-15 18:58:16 +0100599 } else if (msgb_apdu_sw(msg) == 0x6e00) {
600 /* CLA not supported: must be classic SIM, not USIM */
601 g_class = 0xA0;
602 chan->card->prof = osim_cprof_sim(chan->card);
603 chan->cwd = chan->card->prof->mf;
604 msgb_free(msg);
605 } else if (msgb_apdu_sw(msg) == 0x9000) {
606 /* normal file */
607 dump_fcp_template_msg(msg);
608 msgb_free(msg);
Harald Welte870f94d2020-03-19 19:10:34 +0100609 mkdir_and_chdir("ADF_USIM", 0750);
Harald Welte053bebc2020-02-15 18:58:16 +0100610 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100611
612 msg = select_file(chan, 0x6fc5);
613 dump_fcp_template_msg(msg);
Harald Welte76749602012-09-19 20:55:54 +0200614 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Welted54c2ee2012-01-17 18:25:50 +0100615 msgb_free(msg);
616
617 verify_pin(chan, 1, "1653");
618
619 msg = select_file(chan, 0x6f06);
620 dump_fcp_template_msg(msg);
621 msgb_free(msg);
622
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100623 iterate_fs(chan);
Harald Welted54c2ee2012-01-17 18:25:50 +0100624
625 exit(0);
626}