blob: ae55b83e5ffdeadf8301b3444a6afb6b578cf929 [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
Harald Welte429adec2020-03-20 13:05:40 +010080#if 0
Harald Welted54c2ee2012-01-17 18:25:50 +010081/* 11.1.9 */
Harald Weltef12d40f2017-02-08 15:46:53 +000082static int verify_pin(struct osim_chan_hdl *st, uint8_t pin_nr, char *pin)
Harald Welted54c2ee2012-01-17 18:25:50 +010083{
84 struct msgb *msg;
85 char *pindst;
Harald Welted54c2ee2012-01-17 18:25:50 +010086
87 if (strlen(pin) > 8)
88 return -EINVAL;
89
Harald Welte053bebc2020-02-15 18:58:16 +010090 msg = osim_new_apdumsg(g_class, 0x20, 0x00, pin_nr, 8, 0);
Harald Weltef12d40f2017-02-08 15:46:53 +000091 pindst = (char *) msgb_put(msg, 8);
Harald Welted54c2ee2012-01-17 18:25:50 +010092 memset(pindst, 0xFF, 8);
Neels Hofmeyr95fdbc12018-07-26 17:14:40 +020093 /* Do not copy the terminating \0 */
94 memcpy(pindst, pin, strlen(pin));
Harald Welted54c2ee2012-01-17 18:25:50 +010095
96 return osim_transceive_apdu(st, msg);
97}
Harald Welte429adec2020-03-20 13:05:40 +010098#endif
Harald Welted54c2ee2012-01-17 18:25:50 +010099
100/* 11.1.5 */
101static struct msgb *read_record_nr(struct osim_chan_hdl *st, uint8_t rec_nr, uint16_t rec_size)
102{
103 struct msgb *msg;
104
Harald Welte053bebc2020-02-15 18:58:16 +0100105 msg = osim_new_apdumsg(g_class, 0xB2, rec_nr, 0x04, 0, rec_size);
Harald Welted54c2ee2012-01-17 18:25:50 +0100106
107 osim_transceive_apdu(st, msg);
108
109 return msg;
110}
111
Harald Welted54c2ee2012-01-17 18:25:50 +0100112/* 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
Harald Welte053bebc2020-02-15 18:58:16 +0100120 msg = osim_new_apdumsg(g_class, 0xB0, offset >> 8, offset & 0xff, 0, len & 0xff);
Harald Welted54c2ee2012-01-17 18:25:50 +0100121
122 osim_transceive_apdu(st, msg);
123
124 return msg;
125}
126
Harald Welted54c2ee2012-01-17 18:25:50 +0100127static int dump_fcp_template(struct tlv_parsed *tp)
128{
129 int i;
130
131 for (i = 0; i < ARRAY_SIZE(tp->lv); i++) {
132 if (TLVP_PRESENT(tp, i))
133 printf("Tag 0x%02x (%s): %s\n", i,
134 get_value_string(ts102221_fcp_vals, i),
135 osmo_hexdump(TLVP_VAL(tp, i), TLVP_LEN(tp, i)));
136 }
137
138 return 0;
139}
140
141static int dump_fcp_template_msg(struct msgb *msg)
142{
143 struct tlv_parsed tp;
144 int rc;
145
Harald Weltea5c92552012-09-10 21:05:42 +0200146 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 +0100147 if (rc < 0)
148 return rc;
149
150 return dump_fcp_template(&tp);
151}
152
153struct osim_fcp_fd_decoded {
154 enum osim_file_type type;
155 enum osim_ef_type ef_type;
156 uint16_t rec_len;
157 uint8_t num_rec;
158};
159
160static const enum osim_file_type iso2ftype[8] = {
161 [0] = TYPE_EF,
162 [1] = TYPE_EF_INT,
163 [7] = TYPE_DF,
164};
165
166static const enum osim_ef_type iso2eftype[8] = {
167 [1] = EF_TYPE_TRANSP,
168 [2] = EF_TYPE_RECORD_FIXED,
169 [6] = EF_TYPE_RECORD_CYCLIC,
170};
171
172static int osim_fcp_fd_decode(struct osim_fcp_fd_decoded *ofd, const uint8_t *fcp, int fcp_len)
173{
174 memset(ofd, 0, sizeof(*ofd));
175
176 if (fcp_len != 2 && fcp_len != 5)
177 return -EINVAL;
178
179 ofd->type = iso2ftype[(fcp[0] >> 3) & 7];
180 if (ofd->type != TYPE_DF)
181 ofd->ef_type = iso2eftype[fcp[0] & 7];
182
183 if (fcp[1] != 0x21)
184 return -EINVAL;
185
186 if (fcp_len >= 5) {
187 ofd->rec_len = ntohs(*(uint16_t *)(fcp+2));
188 ofd->num_rec = fcp[4];
189 }
190
191 return 0;
192}
193
Harald Welte053bebc2020-02-15 18:58:16 +0100194/* TS 51.011 Section 9.3 Type of File */
195static const enum osim_file_type sim2ftype[8] = {
196 [1] = TYPE_MF,
197 [2] = TYPE_DF,
198 [4] = TYPE_EF,
199};
200
201/* TS 51.011 Section 9.3 Structure of File */
202static const enum osim_ef_type sim2eftype[8] = {
203 [0] = EF_TYPE_TRANSP,
204 [1] = EF_TYPE_RECORD_FIXED,
205 [3] = EF_TYPE_RECORD_CYCLIC,
206};
207
208/* TS 51.011 Section 9.2.1 */
209static int osim_fcp_fd_decode_sim(struct osim_fcp_fd_decoded *ofd, const uint8_t *fcp, int fcp_len)
210{
211 memset(ofd, 0, sizeof(*ofd));
212
213 if (fcp_len < 14)
214 return -EINVAL;
215
216 ofd->type = sim2ftype[fcp[6] & 7];
217 switch (ofd->type) {
218 case TYPE_EF:
219 ofd->ef_type = sim2eftype[fcp[13] & 7];
220 if (fcp_len < 13 + fcp[12])
221 return -EINVAL;
222 switch (ofd->ef_type) {
223 case EF_TYPE_RECORD_FIXED:
224 case EF_TYPE_RECORD_CYCLIC:
225 if (fcp_len < 15)
226 return -EINVAL;
227 ofd->rec_len = fcp[14];
228 ofd->num_rec = ntohs(*(uint16_t *)(fcp+2)) / ofd->rec_len;
229 break;
230 default:
231 break;
232 }
233 break;
234 case TYPE_MF:
235 case TYPE_DF:
236 if (fcp_len < 22)
237 return -EINVAL;
238 break;
239 default:
240 break;
241 }
242
243 return 0;
244}
245
Harald Welte429adec2020-03-20 13:05:40 +0100246/*! scan an UICC for all installed apps; allocate osim_card_app_hdl for each of them */
247static int osim_uicc_scan_apps(struct osim_chan_hdl *st)
Harald Welted54c2ee2012-01-17 18:25:50 +0100248{
249 struct tlv_parsed tp;
250 struct osim_fcp_fd_decoded ofd;
Harald Welte429adec2020-03-20 13:05:40 +0100251 struct msgb *msg;
Harald Welted54c2ee2012-01-17 18:25:50 +0100252 uint8_t *cur;
253 int rc, i;
254
Harald Welte429adec2020-03-20 13:05:40 +0100255 /* we don't know where we currently might be; go back to MF */
256 msg = select_file(st, 0x3f00);
257 if (!msg)
258 return -EIO;
259 if (msgb_apdu_sw(msg) != 0x9000)
260 return -msgb_apdu_sw(msg);
261
262 /* select EF.DIR */
Harald Welted54c2ee2012-01-17 18:25:50 +0100263 msg = select_file(st, 0x2f00);
Harald Welte053bebc2020-02-15 18:58:16 +0100264 if (!msg)
Harald Welte429adec2020-03-20 13:05:40 +0100265 return -EIO;
Harald Welte053bebc2020-02-15 18:58:16 +0100266 /* return status word in case of error */
267 if (msgb_apdu_sw(msg) != 0x9000)
Harald Welte429adec2020-03-20 13:05:40 +0100268 return -msgb_apdu_sw(msg);
Harald Welte053bebc2020-02-15 18:58:16 +0100269
Harald Welte429adec2020-03-20 13:05:40 +0100270 /* various FCP related sanity checks */
Harald Welte053bebc2020-02-15 18:58:16 +0100271 rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-2, 0, 0);
272 if (rc < 0) {
Harald Welte429adec2020-03-20 13:05:40 +0100273 fprintf(stderr, "Error decoding EF.DIR FCP TLV\n");
Harald Welte053bebc2020-02-15 18:58:16 +0100274 msgb_free(msg);
Harald Welte429adec2020-03-20 13:05:40 +0100275 return -EINVAL;
Harald Welte053bebc2020-02-15 18:58:16 +0100276 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100277
278 dump_fcp_template(&tp);
279
280 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
281 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 5) {
Harald Welte429adec2020-03-20 13:05:40 +0100282 fprintf(stderr, "No EF.DIR FCP file description\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100283 msgb_free(msg);
Harald Welte429adec2020-03-20 13:05:40 +0100284 return -EINVAL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100285 }
286
287 rc = osim_fcp_fd_decode(&ofd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
288 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
289 if (rc < 0) {
Harald Welte429adec2020-03-20 13:05:40 +0100290 fprintf(stderr, "Error decoding EF.DIR FCP file description\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100291 msgb_free(msg);
Harald Welte429adec2020-03-20 13:05:40 +0100292 return -EINVAL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100293 }
294
295 if (ofd.type != TYPE_EF || ofd.ef_type != EF_TYPE_RECORD_FIXED) {
Harald Welte429adec2020-03-20 13:05:40 +0100296 fprintf(stderr, "EF.DIR is not a fixed record EF!?!\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100297 msgb_free(msg);
Harald Welte429adec2020-03-20 13:05:40 +0100298 return -EINVAL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100299 }
300
301 msgb_free(msg);
302
303 printf("ofd rec_len = %u, num_rec = %u\n", ofd.rec_len, ofd.num_rec);
304
305 for (i = 0; i < ofd.num_rec; i++) {
Harald Welte429adec2020-03-20 13:05:40 +0100306 const uint8_t *aid;
307 uint8_t aid_len;
Harald Welted54c2ee2012-01-17 18:25:50 +0100308 msg = read_record_nr(st, i+1, ofd.rec_len);
Harald Welte429adec2020-03-20 13:05:40 +0100309 if (!msg) {
310 fprintf(stderr, "Error reading Record %u of EF.DIR, skipping\n", i+1);
311 continue;
312 }
313
314 /* Entries look like this:
315 * 61194f10 a0000000871002ffffffff8907090000 5005 5553696d31 ffffffffffffffffffffff */
Harald Welted54c2ee2012-01-17 18:25:50 +0100316
317 cur = msgb_apdu_de(msg);
318 if (msgb_apdu_le(msg) < 5) {
Harald Welte429adec2020-03-20 13:05:40 +0100319 fprintf(stderr, "Record length %u too short for EF.DIR, skipping\n", msgb_apdu_le(msg));
Harald Welted54c2ee2012-01-17 18:25:50 +0100320 msgb_free(msg);
Harald Welte429adec2020-03-20 13:05:40 +0100321 continue;
Harald Welted54c2ee2012-01-17 18:25:50 +0100322 }
323
324 if (cur[0] != 0x61 || cur[1] < 0x03 || cur[1] > 0x7f ||
325 cur[2] != 0x4F || cur[3] < 0x01 || cur[3] > 0x10) {
Harald Welte429adec2020-03-20 13:05:40 +0100326 fprintf(stderr, "Unexpected/unknown record in EF.DIR: %s, skipping\n",
327 osmo_hexdump_nospc(msgb_apdu_de(msg), msgb_apdu_le(msg)));
Harald Welted54c2ee2012-01-17 18:25:50 +0100328 msgb_free(msg);
Harald Welte429adec2020-03-20 13:05:40 +0100329 continue;
Harald Welted54c2ee2012-01-17 18:25:50 +0100330 }
Harald Welte429adec2020-03-20 13:05:40 +0100331 aid_len = cur[3];
332 aid = cur+4;
Harald Welted54c2ee2012-01-17 18:25:50 +0100333
Harald Welte429adec2020-03-20 13:05:40 +0100334 /* FIXME: parse / pass label*/
335 printf("Detected AID %s\n", osmo_hexdump_nospc(aid, aid_len));
336 osim_card_hdl_add_app(st->card, aid, aid_len, NULL);
Harald Welted54c2ee2012-01-17 18:25:50 +0100337 }
338
Harald Welte429adec2020-03-20 13:05:40 +0100339 return i;
Harald Welted54c2ee2012-01-17 18:25:50 +0100340}
341
Harald Welte429adec2020-03-20 13:05:40 +0100342
343extern struct osim_card_profile *osim_cprof_sim(void *ctx);
Harald Welte58d173a2020-03-21 13:40:28 +0100344extern struct osim_card_profile *osim_cprof_uicc(void *ctx, bool have_df_gsm);
Harald Welte429adec2020-03-20 13:05:40 +0100345
Harald Welte870f94d2020-03-19 19:10:34 +0100346static int dump_file(struct osim_chan_hdl *chan, const char *short_name, uint16_t fid)
Harald Welted54c2ee2012-01-17 18:25:50 +0100347{
348 struct tlv_parsed tp;
349 struct osim_fcp_fd_decoded ffdd;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200350 struct msgb *msg, *rmsg;
351 int rc, i, offset;
Harald Welte870f94d2020-03-19 19:10:34 +0100352 FILE *f_data = NULL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100353
Harald Welte870f94d2020-03-19 19:10:34 +0100354 /* Select the file */
Harald Welted54c2ee2012-01-17 18:25:50 +0100355 msg = select_file(chan, fid);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200356 if (!msg) {
Harald Welte835ed962020-03-19 18:00:04 +0100357 fprintf(stderr, "Unable to select file\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100358 return -EIO;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200359 }
360 if (msgb_apdu_sw(msg) != 0x9000) {
Harald Welte835ed962020-03-19 18:00:04 +0100361 fprintf(stderr, "status 0x%04x selecting file\n", msgb_apdu_sw(msg));
Harald Welted54c2ee2012-01-17 18:25:50 +0100362 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200363 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100364
Harald Welte053bebc2020-02-15 18:58:16 +0100365 if (g_class != 0xA0) {
366 rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-2, 0, 0);
367 if (rc < 0) {
Harald Welte835ed962020-03-19 18:00:04 +0100368 fprintf(stderr, "Unable to parse FCP: %s\n", msgb_hexdump(msg));
Harald Welte053bebc2020-02-15 18:58:16 +0100369 goto out;
370 }
371
372 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
373 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 2) {
Harald Welte835ed962020-03-19 18:00:04 +0100374 fprintf(stderr, "No file descriptor present ?!?\n");
Harald Welte053bebc2020-02-15 18:58:16 +0100375 goto out;
376 }
377
378 rc = osim_fcp_fd_decode(&ffdd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
379 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
380 } else {
381 rc = osim_fcp_fd_decode_sim(&ffdd, msgb_apdu_de(msg), msgb_apdu_le(msg));
Harald Weltea0ba4d92012-09-10 10:43:15 +0200382 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100383
Harald Weltea0ba4d92012-09-10 10:43:15 +0200384 if (rc < 0) {
Harald Welte835ed962020-03-19 18:00:04 +0100385 fprintf(stderr, "Unable to decode File Descriptor\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100386 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200387 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100388
Harald Weltea0ba4d92012-09-10 10:43:15 +0200389 if (ffdd.type != TYPE_EF) {
Harald Welte835ed962020-03-19 18:00:04 +0100390 fprintf(stderr, "File Type != EF\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100391 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200392 }
393
Harald Welte870f94d2020-03-19 19:10:34 +0100394 if (g_output_dir) {
395 f_data = fopen(short_name, "w");
396 if (!f_data) {
397 fprintf(stderr, "Couldn't create '%s': %s\n", short_name, strerror(errno));
398 goto out;
399 }
400 }
401
Harald Weltea0ba4d92012-09-10 10:43:15 +0200402 printf("EF type: %u\n", ffdd.ef_type);
Harald Welted54c2ee2012-01-17 18:25:50 +0100403
404 switch (ffdd.ef_type) {
405 case EF_TYPE_RECORD_FIXED:
406 for (i = 0; i < ffdd.num_rec; i++) {
Harald Welte870f94d2020-03-19 19:10:34 +0100407 const char *hex;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200408 rmsg = read_record_nr(chan, i+1, ffdd.rec_len);
Harald Welte870f94d2020-03-19 19:10:34 +0100409 if (!rmsg) {
410 if (f_data)
411 fclose(f_data);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200412 return -EIO;
Harald Welte870f94d2020-03-19 19:10:34 +0100413 }
Harald Welte3a6bedf2020-03-22 10:30:10 +0100414 printf("SW: %s\n", osim_print_sw(chan, msgb_apdu_sw(msg)));
Harald Welte870f94d2020-03-19 19:10:34 +0100415
416 hex = osmo_hexdump_nospc(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg));
417 printf("Rec %03u: %s\n", i+1, hex);
418 if (f_data)
419 fprintf(f_data, "%s\n", hex);
Harald Welted54c2ee2012-01-17 18:25:50 +0100420 }
421 break;
422 case EF_TYPE_TRANSP:
Philipp Maierfde9fdc2020-02-26 12:00:23 +0100423 if (g_class != 0xA0) {
Harald Welte17051402020-03-22 11:25:32 +0100424 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_SIZE))
Philipp Maierfde9fdc2020-02-26 12:00:23 +0100425 goto out;
426 i = ntohs(*(uint16_t *)TLVP_VAL(&tp, UICC_FCP_T_FILE_SIZE));
427 printf("File size: %d bytes\n", i);
428 } else {
Harald Welte835ed962020-03-19 18:00:04 +0100429 fprintf(stderr, "Can not determine file size, invalid EF-type!\n");
Harald Weltea0ba4d92012-09-10 10:43:15 +0200430 goto out;
Philipp Maierfde9fdc2020-02-26 12:00:23 +0100431 }
Harald Weltea0ba4d92012-09-10 10:43:15 +0200432 for (offset = 0; offset < i-1; ) {
433 uint16_t remain_len = i - offset;
434 uint16_t read_len = OSMO_MIN(remain_len, 256);
Harald Welte870f94d2020-03-19 19:10:34 +0100435 const char *hex;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200436 rmsg = read_binary(chan, offset, read_len);
Harald Welte870f94d2020-03-19 19:10:34 +0100437 if (!rmsg) {
438 if (f_data)
439 fclose(f_data);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200440 return -EIO;
Harald Welte870f94d2020-03-19 19:10:34 +0100441 }
Harald Weltea0ba4d92012-09-10 10:43:15 +0200442 offset += read_len;
Harald Welte870f94d2020-03-19 19:10:34 +0100443 hex = osmo_hexdump_nospc(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg));
444 printf("Content: %s\n", hex);
445 if (f_data)
446 fprintf(f_data, "%s", hex);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200447 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100448 break;
449 default:
450 goto out;
451 }
452
453out:
Harald Welte870f94d2020-03-19 19:10:34 +0100454 if (f_data)
455 fclose(f_data);
Harald Welted54c2ee2012-01-17 18:25:50 +0100456 msgb_free(msg);
457 return -EINVAL;
Harald Welte870f94d2020-03-19 19:10:34 +0100458
Harald Welted54c2ee2012-01-17 18:25:50 +0100459}
460
Eric Wild94cd4ac2019-10-31 19:18:45 +0100461static void print_help(void)
462{
463 printf( "osmo-sim-test Usage:\n"
464 " -h --help This message\n"
465 " -n --reader-num NR Open reader number NR\n"
Harald Welte870f94d2020-03-19 19:10:34 +0100466 " -o --output-dir DIR To-be-created output directory for filesystem dump\n"
Eric Wild94cd4ac2019-10-31 19:18:45 +0100467 );
468}
469
470static int readernum = 0;
471
472static void handle_options(int argc, char **argv)
473{
474 while (1) {
475 int option_index = 0, c;
476 const struct option long_options[] = {
477 { "help", 0, 0, 'h' },
478 { "reader-num", 1, 0, 'n' },
Harald Welte870f94d2020-03-19 19:10:34 +0100479 { "output-dir", 1, 0, 'o' },
Eric Wild94cd4ac2019-10-31 19:18:45 +0100480 {0,0,0,0}
481 };
482
Harald Welte870f94d2020-03-19 19:10:34 +0100483 c = getopt_long(argc, argv, "hn:o:",
Eric Wild94cd4ac2019-10-31 19:18:45 +0100484 long_options, &option_index);
485 if (c == -1)
486 break;
487
488 switch (c) {
489 case 'h':
490 print_help();
491 exit(0);
492 break;
493 case 'n':
494 readernum = atoi(optarg);
495 break;
Harald Welte870f94d2020-03-19 19:10:34 +0100496 case 'o':
497 g_output_dir = optarg;
498 break;
Eric Wild94cd4ac2019-10-31 19:18:45 +0100499 default:
500 exit(2);
501 break;
502 }
503 }
504
505 if (argc > optind) {
506 fprintf(stderr, "Unsupported positional arguments on command line\n");
507 exit(2);
508 }
509}
510
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100511
Harald Welte870f94d2020-03-19 19:10:34 +0100512static void mkdir_and_chdir(const char *name, mode_t mode)
513{
514 int rc;
515 rc = mkdir(name, mode);
516 if (rc < 0) {
517 fprintf(stderr, "Cannot create '%s': %s\n", name, strerror(errno));
518 exit(24);
519 }
520 rc = chdir(name);
521 if (rc < 0) {
522 fprintf(stderr, "Cannot change to just-created '%s': %s\n", name, strerror(errno));
523 exit(24);
524 }
525}
526
527
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100528static void iterate_fs(struct osim_chan_hdl *chan)
529{
530 const struct osim_file_desc *prev_cwd;
531 struct osim_file_desc *ofd;
532
533 /* iterate over all files in current working directory */
534 llist_for_each_entry(ofd, &chan->cwd->child_list, list) {
535 struct msgb *m;
Harald Welte870f94d2020-03-19 19:10:34 +0100536 char prev_dir[PATH_MAX];
537
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100538 printf("\n\n================ %s (%s) ==================\n",
539 ofd->short_name, ofd->long_name);
540
541 m = select_file(chan, ofd->fid);
542 if (msgb_apdu_sw(m) != 0x9000) {
543 msgb_free(m);
544 continue;
545 }
546 dump_fcp_template_msg(m);
547 msgb_free(m);
548
549 /* If this is a DF, recurse into it */
550 switch (ofd->type) {
551 case TYPE_DF:
552 /* the select above has just changed into this directory */
553 prev_cwd = chan->cwd;
554 chan->cwd = ofd;
Harald Welte870f94d2020-03-19 19:10:34 +0100555 if (g_output_dir) {
556 if (!getcwd(prev_dir, sizeof(prev_dir))) {
557 fprintf(stderr, "Cannot determine cwd: %s\n", strerror(errno));
558 exit(23);
559 continue;
560 }
561 mkdir_and_chdir(ofd->short_name, 0750);
562 }
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100563 iterate_fs(chan);
564 /* "pop" the directory from the stack */
565 chan->cwd = prev_cwd;
Harald Welte870f94d2020-03-19 19:10:34 +0100566 if (g_output_dir)
567 OSMO_ASSERT(chdir("..") == 0);
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100568 break;
569 default:
Harald Welte870f94d2020-03-19 19:10:34 +0100570 dump_file(chan, ofd->short_name, ofd->fid);
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100571 break;
572 }
573 }
574}
575
Harald Welte429adec2020-03-20 13:05:40 +0100576static void iterate_apps(struct osim_chan_hdl *chan)
577{
578 struct osim_card_app_hdl *cah;
579
580 llist_for_each_entry(cah, &chan->card->apps, list) {
581 const struct osim_card_app_profile *cap = cah->prof;
582 struct msgb *msg;
583
584 if (!cap) {
585 fprintf(stderr, "Unknown AID %s; skipping\n",
586 osmo_hexdump_nospc(cah->aid, cah->aid_len));
587 continue;
588 }
589
590 msg = select_adf(chan, cah->aid, cah->aid_len);
591 if (!msg) {
592 fprintf(stderr, "Error selectiong ADF for AID %s; skipping\n",
593 osmo_hexdump_nospc(cah->aid, cah->aid_len));
594 continue;
595 }
Harald Welte3a6bedf2020-03-22 10:30:10 +0100596 printf("SW: %s\n", osim_print_sw(chan, msgb_apdu_sw(msg)));
Harald Welte429adec2020-03-20 13:05:40 +0100597 chan->cur_app = cah;
598 chan->cwd = cap->adf;
599
600 if (g_output_dir)
601 mkdir_and_chdir(cap->adf->short_name, 0750);
602
603 iterate_fs(chan);
604
605 if (g_output_dir)
606 OSMO_ASSERT(chdir("..") == 0);
607 }
608}
609
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100610
Harald Welted54c2ee2012-01-17 18:25:50 +0100611int main(int argc, char **argv)
612{
613 struct osim_reader_hdl *reader;
614 struct osim_card_hdl *card;
615 struct osim_chan_hdl *chan;
Harald Welte429adec2020-03-20 13:05:40 +0100616 int rc;
Harald Welted54c2ee2012-01-17 18:25:50 +0100617
Eric Wild94cd4ac2019-10-31 19:18:45 +0100618 handle_options(argc, argv);
619
Harald Welte429adec2020-03-20 13:05:40 +0100620 osim_init(NULL);
621
Harald Welte870f94d2020-03-19 19:10:34 +0100622 if (g_output_dir) {
623 int rc;
624 rc = mkdir(g_output_dir, 0750);
625 if (rc < 0) {
626 fprintf(stderr, "Cannot create directory '%s': %s\n", g_output_dir,
627 strerror(errno));
628 exit(5);
629 }
630 rc = chdir(g_output_dir);
631 if (rc < 0) {
632 fprintf(stderr, "Cannot change to just-created directory '%s': %s\n",
633 g_output_dir, strerror(errno));
634 exit(5);
635 }
636 }
637
Eric Wild94cd4ac2019-10-31 19:18:45 +0100638 reader = osim_reader_open(OSIM_READER_DRV_PCSC, readernum, "", NULL);
Harald Welted54c2ee2012-01-17 18:25:50 +0100639 if (!reader)
640 exit(1);
Harald Welte55790aa2014-10-26 18:46:50 +0100641 card = osim_card_open(reader, OSIM_PROTO_T0);
Harald Welted54c2ee2012-01-17 18:25:50 +0100642 if (!card)
643 exit(2);
644 chan = llist_entry(card->channels.next, struct osim_chan_hdl, list);
645 if (!chan)
646 exit(3);
647
Harald Welte429adec2020-03-20 13:05:40 +0100648 //verify_pin(chan, 1, "1653");
649
650 rc = osim_uicc_scan_apps(chan);
651 if (rc >= 0) {
Harald Welte58d173a2020-03-21 13:40:28 +0100652 chan->card->prof = osim_cprof_uicc(chan->card, true);
Harald Welte429adec2020-03-20 13:05:40 +0100653 chan->cwd = chan->card->prof->mf;
654 } else if (rc == -0x6e00) {
Harald Welte053bebc2020-02-15 18:58:16 +0100655 /* CLA not supported: must be classic SIM, not USIM */
656 g_class = 0xA0;
657 chan->card->prof = osim_cprof_sim(chan->card);
658 chan->cwd = chan->card->prof->mf;
Harald Welte429adec2020-03-20 13:05:40 +0100659 } else if (rc < 0) {
660 exit(4);
Harald Welte053bebc2020-02-15 18:58:16 +0100661 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100662
Harald Welte429adec2020-03-20 13:05:40 +0100663 /* first iterate over normal file system */
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100664 iterate_fs(chan);
Harald Welted54c2ee2012-01-17 18:25:50 +0100665
Harald Welte429adec2020-03-20 13:05:40 +0100666 /* then itereate over all apps and their file system */
667 iterate_apps(chan);
668
Harald Welted54c2ee2012-01-17 18:25:50 +0100669 exit(0);
670}