blob: 6cf36a6a0b6d16f3f14afc883eed88772a72ca55 [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 Welte76749602012-09-19 20:55:54 +0200414 printf("SW: %s\n", osim_print_sw(chan->card, 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 Welte870f94d2020-03-19 19:10:34 +0100424 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_SIZE)) {
425 if (f_data)
426 fclose(f_data);
Philipp Maierfde9fdc2020-02-26 12:00:23 +0100427 goto out;
Harald Welte870f94d2020-03-19 19:10:34 +0100428 }
Philipp Maierfde9fdc2020-02-26 12:00:23 +0100429 i = ntohs(*(uint16_t *)TLVP_VAL(&tp, UICC_FCP_T_FILE_SIZE));
430 printf("File size: %d bytes\n", i);
431 } else {
Harald Welte835ed962020-03-19 18:00:04 +0100432 fprintf(stderr, "Can not determine file size, invalid EF-type!\n");
Harald Weltea0ba4d92012-09-10 10:43:15 +0200433 goto out;
Philipp Maierfde9fdc2020-02-26 12:00:23 +0100434 }
Harald Weltea0ba4d92012-09-10 10:43:15 +0200435 for (offset = 0; offset < i-1; ) {
436 uint16_t remain_len = i - offset;
437 uint16_t read_len = OSMO_MIN(remain_len, 256);
Harald Welte870f94d2020-03-19 19:10:34 +0100438 const char *hex;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200439 rmsg = read_binary(chan, offset, read_len);
Harald Welte870f94d2020-03-19 19:10:34 +0100440 if (!rmsg) {
441 if (f_data)
442 fclose(f_data);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200443 return -EIO;
Harald Welte870f94d2020-03-19 19:10:34 +0100444 }
Harald Weltea0ba4d92012-09-10 10:43:15 +0200445 offset += read_len;
Harald Welte870f94d2020-03-19 19:10:34 +0100446 hex = osmo_hexdump_nospc(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg));
447 printf("Content: %s\n", hex);
448 if (f_data)
449 fprintf(f_data, "%s", hex);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200450 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100451 break;
452 default:
453 goto out;
454 }
455
456out:
Harald Welte870f94d2020-03-19 19:10:34 +0100457 if (f_data)
458 fclose(f_data);
Harald Welted54c2ee2012-01-17 18:25:50 +0100459 msgb_free(msg);
460 return -EINVAL;
Harald Welte870f94d2020-03-19 19:10:34 +0100461
Harald Welted54c2ee2012-01-17 18:25:50 +0100462}
463
Eric Wild94cd4ac2019-10-31 19:18:45 +0100464static void print_help(void)
465{
466 printf( "osmo-sim-test Usage:\n"
467 " -h --help This message\n"
468 " -n --reader-num NR Open reader number NR\n"
Harald Welte870f94d2020-03-19 19:10:34 +0100469 " -o --output-dir DIR To-be-created output directory for filesystem dump\n"
Eric Wild94cd4ac2019-10-31 19:18:45 +0100470 );
471}
472
473static int readernum = 0;
474
475static void handle_options(int argc, char **argv)
476{
477 while (1) {
478 int option_index = 0, c;
479 const struct option long_options[] = {
480 { "help", 0, 0, 'h' },
481 { "reader-num", 1, 0, 'n' },
Harald Welte870f94d2020-03-19 19:10:34 +0100482 { "output-dir", 1, 0, 'o' },
Eric Wild94cd4ac2019-10-31 19:18:45 +0100483 {0,0,0,0}
484 };
485
Harald Welte870f94d2020-03-19 19:10:34 +0100486 c = getopt_long(argc, argv, "hn:o:",
Eric Wild94cd4ac2019-10-31 19:18:45 +0100487 long_options, &option_index);
488 if (c == -1)
489 break;
490
491 switch (c) {
492 case 'h':
493 print_help();
494 exit(0);
495 break;
496 case 'n':
497 readernum = atoi(optarg);
498 break;
Harald Welte870f94d2020-03-19 19:10:34 +0100499 case 'o':
500 g_output_dir = optarg;
501 break;
Eric Wild94cd4ac2019-10-31 19:18:45 +0100502 default:
503 exit(2);
504 break;
505 }
506 }
507
508 if (argc > optind) {
509 fprintf(stderr, "Unsupported positional arguments on command line\n");
510 exit(2);
511 }
512}
513
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100514
Harald Welte870f94d2020-03-19 19:10:34 +0100515static void mkdir_and_chdir(const char *name, mode_t mode)
516{
517 int rc;
518 rc = mkdir(name, mode);
519 if (rc < 0) {
520 fprintf(stderr, "Cannot create '%s': %s\n", name, strerror(errno));
521 exit(24);
522 }
523 rc = chdir(name);
524 if (rc < 0) {
525 fprintf(stderr, "Cannot change to just-created '%s': %s\n", name, strerror(errno));
526 exit(24);
527 }
528}
529
530
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100531static void iterate_fs(struct osim_chan_hdl *chan)
532{
533 const struct osim_file_desc *prev_cwd;
534 struct osim_file_desc *ofd;
535
536 /* iterate over all files in current working directory */
537 llist_for_each_entry(ofd, &chan->cwd->child_list, list) {
538 struct msgb *m;
Harald Welte870f94d2020-03-19 19:10:34 +0100539 char prev_dir[PATH_MAX];
540
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100541 printf("\n\n================ %s (%s) ==================\n",
542 ofd->short_name, ofd->long_name);
543
544 m = select_file(chan, ofd->fid);
545 if (msgb_apdu_sw(m) != 0x9000) {
546 msgb_free(m);
547 continue;
548 }
549 dump_fcp_template_msg(m);
550 msgb_free(m);
551
552 /* If this is a DF, recurse into it */
553 switch (ofd->type) {
554 case TYPE_DF:
555 /* the select above has just changed into this directory */
556 prev_cwd = chan->cwd;
557 chan->cwd = ofd;
Harald Welte870f94d2020-03-19 19:10:34 +0100558 if (g_output_dir) {
559 if (!getcwd(prev_dir, sizeof(prev_dir))) {
560 fprintf(stderr, "Cannot determine cwd: %s\n", strerror(errno));
561 exit(23);
562 continue;
563 }
564 mkdir_and_chdir(ofd->short_name, 0750);
565 }
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100566 iterate_fs(chan);
567 /* "pop" the directory from the stack */
568 chan->cwd = prev_cwd;
Harald Welte870f94d2020-03-19 19:10:34 +0100569 if (g_output_dir)
570 OSMO_ASSERT(chdir("..") == 0);
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100571 break;
572 default:
Harald Welte870f94d2020-03-19 19:10:34 +0100573 dump_file(chan, ofd->short_name, ofd->fid);
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100574 break;
575 }
576 }
577}
578
Harald Welte429adec2020-03-20 13:05:40 +0100579static void iterate_apps(struct osim_chan_hdl *chan)
580{
581 struct osim_card_app_hdl *cah;
582
583 llist_for_each_entry(cah, &chan->card->apps, list) {
584 const struct osim_card_app_profile *cap = cah->prof;
585 struct msgb *msg;
586
587 if (!cap) {
588 fprintf(stderr, "Unknown AID %s; skipping\n",
589 osmo_hexdump_nospc(cah->aid, cah->aid_len));
590 continue;
591 }
592
593 msg = select_adf(chan, cah->aid, cah->aid_len);
594 if (!msg) {
595 fprintf(stderr, "Error selectiong ADF for AID %s; skipping\n",
596 osmo_hexdump_nospc(cah->aid, cah->aid_len));
597 continue;
598 }
599 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
600 chan->cur_app = cah;
601 chan->cwd = cap->adf;
602
603 if (g_output_dir)
604 mkdir_and_chdir(cap->adf->short_name, 0750);
605
606 iterate_fs(chan);
607
608 if (g_output_dir)
609 OSMO_ASSERT(chdir("..") == 0);
610 }
611}
612
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100613
Harald Welted54c2ee2012-01-17 18:25:50 +0100614int main(int argc, char **argv)
615{
616 struct osim_reader_hdl *reader;
617 struct osim_card_hdl *card;
618 struct osim_chan_hdl *chan;
Harald Welte429adec2020-03-20 13:05:40 +0100619 int rc;
Harald Welted54c2ee2012-01-17 18:25:50 +0100620
Eric Wild94cd4ac2019-10-31 19:18:45 +0100621 handle_options(argc, argv);
622
Harald Welte429adec2020-03-20 13:05:40 +0100623 osim_init(NULL);
624
Harald Welte870f94d2020-03-19 19:10:34 +0100625 if (g_output_dir) {
626 int rc;
627 rc = mkdir(g_output_dir, 0750);
628 if (rc < 0) {
629 fprintf(stderr, "Cannot create directory '%s': %s\n", g_output_dir,
630 strerror(errno));
631 exit(5);
632 }
633 rc = chdir(g_output_dir);
634 if (rc < 0) {
635 fprintf(stderr, "Cannot change to just-created directory '%s': %s\n",
636 g_output_dir, strerror(errno));
637 exit(5);
638 }
639 }
640
Eric Wild94cd4ac2019-10-31 19:18:45 +0100641 reader = osim_reader_open(OSIM_READER_DRV_PCSC, readernum, "", NULL);
Harald Welted54c2ee2012-01-17 18:25:50 +0100642 if (!reader)
643 exit(1);
Harald Welte55790aa2014-10-26 18:46:50 +0100644 card = osim_card_open(reader, OSIM_PROTO_T0);
Harald Welted54c2ee2012-01-17 18:25:50 +0100645 if (!card)
646 exit(2);
647 chan = llist_entry(card->channels.next, struct osim_chan_hdl, list);
648 if (!chan)
649 exit(3);
650
Harald Welte429adec2020-03-20 13:05:40 +0100651 //verify_pin(chan, 1, "1653");
652
653 rc = osim_uicc_scan_apps(chan);
654 if (rc >= 0) {
Harald Welte58d173a2020-03-21 13:40:28 +0100655 chan->card->prof = osim_cprof_uicc(chan->card, true);
Harald Welte429adec2020-03-20 13:05:40 +0100656 chan->cwd = chan->card->prof->mf;
657 } else if (rc == -0x6e00) {
Harald Welte053bebc2020-02-15 18:58:16 +0100658 /* CLA not supported: must be classic SIM, not USIM */
659 g_class = 0xA0;
660 chan->card->prof = osim_cprof_sim(chan->card);
661 chan->cwd = chan->card->prof->mf;
Harald Welte429adec2020-03-20 13:05:40 +0100662 } else if (rc < 0) {
663 exit(4);
Harald Welte053bebc2020-02-15 18:58:16 +0100664 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100665
Harald Welte429adec2020-03-20 13:05:40 +0100666 /* first iterate over normal file system */
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100667 iterate_fs(chan);
Harald Welted54c2ee2012-01-17 18:25:50 +0100668
Harald Welte429adec2020-03-20 13:05:40 +0100669 /* then itereate over all apps and their file system */
670 iterate_apps(chan);
671
Harald Welted54c2ee2012-01-17 18:25:50 +0100672 exit(0);
673}