blob: 2c6c76415c244c2868dd39504cc54963cba92d93 [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 *
Harald Weltead418632012-09-10 10:49:59 +020015 */
16
Harald Welted54c2ee2012-01-17 18:25:50 +010017#include <stdio.h>
18#include <stdlib.h>
19#include <errno.h>
20#include <string.h>
Eric Wild94cd4ac2019-10-31 19:18:45 +010021#include <getopt.h>
Alexander Huemeraab4a242015-11-06 20:55:24 +010022#include <arpa/inet.h>
Harald Welted54c2ee2012-01-17 18:25:50 +010023
Harald Welte870f94d2020-03-19 19:10:34 +010024#include <unistd.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <fcntl.h>
28#include <limits.h>
29
Harald Welted54c2ee2012-01-17 18:25:50 +010030#include <osmocom/core/msgb.h>
31#include <osmocom/core/talloc.h>
32#include <osmocom/sim/sim.h>
33#include <osmocom/gsm/tlv.h>
34
35
Harald Weltead418632012-09-10 10:49:59 +020036/* FIXME: this needs to be moved to card_fs_uicc.c */
Harald Welted54c2ee2012-01-17 18:25:50 +010037
Harald Welte053bebc2020-02-15 18:58:16 +010038static uint8_t g_class = 0x00; /* UICC/USIM */
Harald Welte870f94d2020-03-19 19:10:34 +010039static const char *g_output_dir;
Harald Welte053bebc2020-02-15 18:58:16 +010040
Harald Welted54c2ee2012-01-17 18:25:50 +010041/* 11.1.1 */
42static struct msgb *_select_file(struct osim_chan_hdl *st, uint8_t p1, uint8_t p2,
43 const uint8_t *data, uint8_t data_len)
44{
Vadim Yanitskiy1cd99912017-05-15 21:37:16 +030045 struct msgb *msg;
Harald Weltef12d40f2017-02-08 15:46:53 +000046 uint8_t *dst;
Harald Welted54c2ee2012-01-17 18:25:50 +010047
Harald Welte053bebc2020-02-15 18:58:16 +010048 msg = osim_new_apdumsg(g_class, 0xA4, p1, p2, data_len, 256);
Harald Welted54c2ee2012-01-17 18:25:50 +010049 dst = msgb_put(msg, data_len);
50 memcpy(dst, data, data_len);
51
52 osim_transceive_apdu(st, msg);
53
54 return msg;
55}
56
57/* 11.1.1 */
58static struct msgb *select_adf(struct osim_chan_hdl *st, const uint8_t *adf, uint8_t adf_len)
59{
Harald Welted54c2ee2012-01-17 18:25:50 +010060 return _select_file(st, 0x04, 0x04, adf,adf_len);
61}
62
63/* 11.1.1 */
64static struct msgb *select_file(struct osim_chan_hdl *st, uint16_t fid)
65{
66 uint16_t cfid = htons(fid);
Harald Welte053bebc2020-02-15 18:58:16 +010067 uint8_t p2 = 0x04;
Harald Welted54c2ee2012-01-17 18:25:50 +010068
Harald Welte053bebc2020-02-15 18:58:16 +010069 /* Classic SIM cards don't support 0x04 (Return FCP) */
70 if (g_class == 0xA0)
71 p2 = 0x00;
72
73 return _select_file(st, 0x00, p2, (uint8_t *)&cfid, 2);
Harald Welted54c2ee2012-01-17 18:25:50 +010074}
75
Harald Welte429adec2020-03-20 13:05:40 +010076#if 0
Harald Welted54c2ee2012-01-17 18:25:50 +010077/* 11.1.9 */
Harald Weltef12d40f2017-02-08 15:46:53 +000078static int verify_pin(struct osim_chan_hdl *st, uint8_t pin_nr, char *pin)
Harald Welted54c2ee2012-01-17 18:25:50 +010079{
80 struct msgb *msg;
81 char *pindst;
Harald Welted54c2ee2012-01-17 18:25:50 +010082
83 if (strlen(pin) > 8)
84 return -EINVAL;
85
Harald Welte053bebc2020-02-15 18:58:16 +010086 msg = osim_new_apdumsg(g_class, 0x20, 0x00, pin_nr, 8, 0);
Harald Weltef12d40f2017-02-08 15:46:53 +000087 pindst = (char *) msgb_put(msg, 8);
Harald Welted54c2ee2012-01-17 18:25:50 +010088 memset(pindst, 0xFF, 8);
Neels Hofmeyr95fdbc12018-07-26 17:14:40 +020089 /* Do not copy the terminating \0 */
90 memcpy(pindst, pin, strlen(pin));
Harald Welted54c2ee2012-01-17 18:25:50 +010091
92 return osim_transceive_apdu(st, msg);
93}
Harald Welte429adec2020-03-20 13:05:40 +010094#endif
Harald Welted54c2ee2012-01-17 18:25:50 +010095
96/* 11.1.5 */
97static struct msgb *read_record_nr(struct osim_chan_hdl *st, uint8_t rec_nr, uint16_t rec_size)
98{
99 struct msgb *msg;
100
Harald Welte053bebc2020-02-15 18:58:16 +0100101 msg = osim_new_apdumsg(g_class, 0xB2, rec_nr, 0x04, 0, rec_size);
Harald Welted54c2ee2012-01-17 18:25:50 +0100102
103 osim_transceive_apdu(st, msg);
104
105 return msg;
106}
107
Harald Welted54c2ee2012-01-17 18:25:50 +0100108/* 11.1.3 */
109static struct msgb *read_binary(struct osim_chan_hdl *st, uint16_t offset, uint16_t len)
110{
111 struct msgb *msg;
112
113 if (offset > 0x7fff || len > 256)
114 return NULL;
115
Harald Welte053bebc2020-02-15 18:58:16 +0100116 msg = osim_new_apdumsg(g_class, 0xB0, offset >> 8, offset & 0xff, 0, len & 0xff);
Harald Welted54c2ee2012-01-17 18:25:50 +0100117
118 osim_transceive_apdu(st, msg);
119
120 return msg;
121}
122
Harald Welted54c2ee2012-01-17 18:25:50 +0100123static int dump_fcp_template(struct tlv_parsed *tp)
124{
125 int i;
126
127 for (i = 0; i < ARRAY_SIZE(tp->lv); i++) {
128 if (TLVP_PRESENT(tp, i))
129 printf("Tag 0x%02x (%s): %s\n", i,
130 get_value_string(ts102221_fcp_vals, i),
131 osmo_hexdump(TLVP_VAL(tp, i), TLVP_LEN(tp, i)));
132 }
133
134 return 0;
135}
136
137static int dump_fcp_template_msg(struct msgb *msg)
138{
139 struct tlv_parsed tp;
140 int rc;
141
Harald Weltea5c92552012-09-10 21:05:42 +0200142 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 +0100143 if (rc < 0)
144 return rc;
145
146 return dump_fcp_template(&tp);
147}
148
149struct osim_fcp_fd_decoded {
150 enum osim_file_type type;
151 enum osim_ef_type ef_type;
152 uint16_t rec_len;
153 uint8_t num_rec;
154};
155
156static const enum osim_file_type iso2ftype[8] = {
157 [0] = TYPE_EF,
158 [1] = TYPE_EF_INT,
159 [7] = TYPE_DF,
160};
161
162static const enum osim_ef_type iso2eftype[8] = {
163 [1] = EF_TYPE_TRANSP,
164 [2] = EF_TYPE_RECORD_FIXED,
165 [6] = EF_TYPE_RECORD_CYCLIC,
166};
167
168static int osim_fcp_fd_decode(struct osim_fcp_fd_decoded *ofd, const uint8_t *fcp, int fcp_len)
169{
170 memset(ofd, 0, sizeof(*ofd));
171
172 if (fcp_len != 2 && fcp_len != 5)
173 return -EINVAL;
174
175 ofd->type = iso2ftype[(fcp[0] >> 3) & 7];
176 if (ofd->type != TYPE_DF)
177 ofd->ef_type = iso2eftype[fcp[0] & 7];
178
179 if (fcp[1] != 0x21)
180 return -EINVAL;
181
182 if (fcp_len >= 5) {
183 ofd->rec_len = ntohs(*(uint16_t *)(fcp+2));
184 ofd->num_rec = fcp[4];
185 }
186
187 return 0;
188}
189
Harald Welte053bebc2020-02-15 18:58:16 +0100190/* TS 51.011 Section 9.3 Type of File */
191static const enum osim_file_type sim2ftype[8] = {
192 [1] = TYPE_MF,
193 [2] = TYPE_DF,
194 [4] = TYPE_EF,
195};
196
197/* TS 51.011 Section 9.3 Structure of File */
198static const enum osim_ef_type sim2eftype[8] = {
199 [0] = EF_TYPE_TRANSP,
200 [1] = EF_TYPE_RECORD_FIXED,
201 [3] = EF_TYPE_RECORD_CYCLIC,
202};
203
204/* TS 51.011 Section 9.2.1 */
205static int osim_fcp_fd_decode_sim(struct osim_fcp_fd_decoded *ofd, const uint8_t *fcp, int fcp_len)
206{
207 memset(ofd, 0, sizeof(*ofd));
208
209 if (fcp_len < 14)
210 return -EINVAL;
211
212 ofd->type = sim2ftype[fcp[6] & 7];
213 switch (ofd->type) {
214 case TYPE_EF:
215 ofd->ef_type = sim2eftype[fcp[13] & 7];
216 if (fcp_len < 13 + fcp[12])
217 return -EINVAL;
218 switch (ofd->ef_type) {
219 case EF_TYPE_RECORD_FIXED:
220 case EF_TYPE_RECORD_CYCLIC:
221 if (fcp_len < 15)
222 return -EINVAL;
223 ofd->rec_len = fcp[14];
224 ofd->num_rec = ntohs(*(uint16_t *)(fcp+2)) / ofd->rec_len;
225 break;
226 default:
227 break;
228 }
229 break;
230 case TYPE_MF:
231 case TYPE_DF:
232 if (fcp_len < 22)
233 return -EINVAL;
234 break;
235 default:
236 break;
237 }
238
239 return 0;
240}
241
Harald Welte429adec2020-03-20 13:05:40 +0100242/*! scan an UICC for all installed apps; allocate osim_card_app_hdl for each of them */
243static int osim_uicc_scan_apps(struct osim_chan_hdl *st)
Harald Welted54c2ee2012-01-17 18:25:50 +0100244{
245 struct tlv_parsed tp;
246 struct osim_fcp_fd_decoded ofd;
Harald Welte429adec2020-03-20 13:05:40 +0100247 struct msgb *msg;
Harald Welted54c2ee2012-01-17 18:25:50 +0100248 uint8_t *cur;
249 int rc, i;
250
Harald Welte429adec2020-03-20 13:05:40 +0100251 /* we don't know where we currently might be; go back to MF */
252 msg = select_file(st, 0x3f00);
253 if (!msg)
254 return -EIO;
255 if (msgb_apdu_sw(msg) != 0x9000)
256 return -msgb_apdu_sw(msg);
257
258 /* select EF.DIR */
Harald Welted54c2ee2012-01-17 18:25:50 +0100259 msg = select_file(st, 0x2f00);
Harald Welte053bebc2020-02-15 18:58:16 +0100260 if (!msg)
Harald Welte429adec2020-03-20 13:05:40 +0100261 return -EIO;
Harald Welte053bebc2020-02-15 18:58:16 +0100262 /* return status word in case of error */
263 if (msgb_apdu_sw(msg) != 0x9000)
Harald Welte429adec2020-03-20 13:05:40 +0100264 return -msgb_apdu_sw(msg);
Harald Welte053bebc2020-02-15 18:58:16 +0100265
Harald Welte429adec2020-03-20 13:05:40 +0100266 /* various FCP related sanity checks */
Harald Welte053bebc2020-02-15 18:58:16 +0100267 rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-2, 0, 0);
268 if (rc < 0) {
Harald Welte429adec2020-03-20 13:05:40 +0100269 fprintf(stderr, "Error decoding EF.DIR FCP TLV\n");
Harald Welte053bebc2020-02-15 18:58:16 +0100270 msgb_free(msg);
Harald Welte429adec2020-03-20 13:05:40 +0100271 return -EINVAL;
Harald Welte053bebc2020-02-15 18:58:16 +0100272 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100273
274 dump_fcp_template(&tp);
275
276 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
277 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 5) {
Harald Welte429adec2020-03-20 13:05:40 +0100278 fprintf(stderr, "No EF.DIR FCP file description\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100279 msgb_free(msg);
Harald Welte429adec2020-03-20 13:05:40 +0100280 return -EINVAL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100281 }
282
283 rc = osim_fcp_fd_decode(&ofd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
284 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
285 if (rc < 0) {
Harald Welte429adec2020-03-20 13:05:40 +0100286 fprintf(stderr, "Error decoding EF.DIR FCP file description\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100287 msgb_free(msg);
Harald Welte429adec2020-03-20 13:05:40 +0100288 return -EINVAL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100289 }
290
291 if (ofd.type != TYPE_EF || ofd.ef_type != EF_TYPE_RECORD_FIXED) {
Harald Welte429adec2020-03-20 13:05:40 +0100292 fprintf(stderr, "EF.DIR is not a fixed record EF!?!\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100293 msgb_free(msg);
Harald Welte429adec2020-03-20 13:05:40 +0100294 return -EINVAL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100295 }
296
297 msgb_free(msg);
298
299 printf("ofd rec_len = %u, num_rec = %u\n", ofd.rec_len, ofd.num_rec);
300
301 for (i = 0; i < ofd.num_rec; i++) {
Harald Welte429adec2020-03-20 13:05:40 +0100302 const uint8_t *aid;
303 uint8_t aid_len;
Harald Welted54c2ee2012-01-17 18:25:50 +0100304 msg = read_record_nr(st, i+1, ofd.rec_len);
Harald Welte429adec2020-03-20 13:05:40 +0100305 if (!msg) {
306 fprintf(stderr, "Error reading Record %u of EF.DIR, skipping\n", i+1);
307 continue;
308 }
309
310 /* Entries look like this:
311 * 61194f10 a0000000871002ffffffff8907090000 5005 5553696d31 ffffffffffffffffffffff */
Harald Welted54c2ee2012-01-17 18:25:50 +0100312
313 cur = msgb_apdu_de(msg);
314 if (msgb_apdu_le(msg) < 5) {
Harald Welte429adec2020-03-20 13:05:40 +0100315 fprintf(stderr, "Record length %u too short for EF.DIR, skipping\n", msgb_apdu_le(msg));
Harald Welted54c2ee2012-01-17 18:25:50 +0100316 msgb_free(msg);
Harald Welte429adec2020-03-20 13:05:40 +0100317 continue;
Harald Welted54c2ee2012-01-17 18:25:50 +0100318 }
319
320 if (cur[0] != 0x61 || cur[1] < 0x03 || cur[1] > 0x7f ||
321 cur[2] != 0x4F || cur[3] < 0x01 || cur[3] > 0x10) {
Harald Welte429adec2020-03-20 13:05:40 +0100322 fprintf(stderr, "Unexpected/unknown record in EF.DIR: %s, skipping\n",
323 osmo_hexdump_nospc(msgb_apdu_de(msg), msgb_apdu_le(msg)));
Harald Welted54c2ee2012-01-17 18:25:50 +0100324 msgb_free(msg);
Harald Welte429adec2020-03-20 13:05:40 +0100325 continue;
Harald Welted54c2ee2012-01-17 18:25:50 +0100326 }
Harald Welte429adec2020-03-20 13:05:40 +0100327 aid_len = cur[3];
328 aid = cur+4;
Harald Welted54c2ee2012-01-17 18:25:50 +0100329
Harald Welte429adec2020-03-20 13:05:40 +0100330 /* FIXME: parse / pass label*/
331 printf("Detected AID %s\n", osmo_hexdump_nospc(aid, aid_len));
332 osim_card_hdl_add_app(st->card, aid, aid_len, NULL);
Harald Welted54c2ee2012-01-17 18:25:50 +0100333 }
334
Harald Welte429adec2020-03-20 13:05:40 +0100335 return i;
Harald Welted54c2ee2012-01-17 18:25:50 +0100336}
337
Harald Welte429adec2020-03-20 13:05:40 +0100338
339extern struct osim_card_profile *osim_cprof_sim(void *ctx);
Harald Welte58d173a2020-03-21 13:40:28 +0100340extern struct osim_card_profile *osim_cprof_uicc(void *ctx, bool have_df_gsm);
Harald Welte429adec2020-03-20 13:05:40 +0100341
Harald Welte870f94d2020-03-19 19:10:34 +0100342static int dump_file(struct osim_chan_hdl *chan, const char *short_name, uint16_t fid)
Harald Welted54c2ee2012-01-17 18:25:50 +0100343{
344 struct tlv_parsed tp;
345 struct osim_fcp_fd_decoded ffdd;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200346 struct msgb *msg, *rmsg;
347 int rc, i, offset;
Harald Welte870f94d2020-03-19 19:10:34 +0100348 FILE *f_data = NULL;
Harald Welted54c2ee2012-01-17 18:25:50 +0100349
Harald Welte870f94d2020-03-19 19:10:34 +0100350 /* Select the file */
Harald Welted54c2ee2012-01-17 18:25:50 +0100351 msg = select_file(chan, fid);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200352 if (!msg) {
Harald Welte835ed962020-03-19 18:00:04 +0100353 fprintf(stderr, "Unable to select file\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100354 return -EIO;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200355 }
356 if (msgb_apdu_sw(msg) != 0x9000) {
Harald Welte835ed962020-03-19 18:00:04 +0100357 fprintf(stderr, "status 0x%04x selecting file\n", msgb_apdu_sw(msg));
Harald Welted54c2ee2012-01-17 18:25:50 +0100358 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200359 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100360
Harald Welte053bebc2020-02-15 18:58:16 +0100361 if (g_class != 0xA0) {
362 rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-2, 0, 0);
363 if (rc < 0) {
Harald Welte835ed962020-03-19 18:00:04 +0100364 fprintf(stderr, "Unable to parse FCP: %s\n", msgb_hexdump(msg));
Harald Welte053bebc2020-02-15 18:58:16 +0100365 goto out;
366 }
367
368 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
369 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 2) {
Harald Welte835ed962020-03-19 18:00:04 +0100370 fprintf(stderr, "No file descriptor present ?!?\n");
Harald Welte053bebc2020-02-15 18:58:16 +0100371 goto out;
372 }
373
374 rc = osim_fcp_fd_decode(&ffdd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
375 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
376 } else {
377 rc = osim_fcp_fd_decode_sim(&ffdd, msgb_apdu_de(msg), msgb_apdu_le(msg));
Harald Weltea0ba4d92012-09-10 10:43:15 +0200378 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100379
Harald Weltea0ba4d92012-09-10 10:43:15 +0200380 if (rc < 0) {
Harald Welte835ed962020-03-19 18:00:04 +0100381 fprintf(stderr, "Unable to decode File Descriptor\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100382 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200383 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100384
Harald Weltea0ba4d92012-09-10 10:43:15 +0200385 if (ffdd.type != TYPE_EF) {
Harald Welte835ed962020-03-19 18:00:04 +0100386 fprintf(stderr, "File Type != EF\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100387 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200388 }
389
Harald Welte870f94d2020-03-19 19:10:34 +0100390 if (g_output_dir) {
391 f_data = fopen(short_name, "w");
392 if (!f_data) {
393 fprintf(stderr, "Couldn't create '%s': %s\n", short_name, strerror(errno));
394 goto out;
395 }
396 }
397
Harald Weltea0ba4d92012-09-10 10:43:15 +0200398 printf("EF type: %u\n", ffdd.ef_type);
Harald Welted54c2ee2012-01-17 18:25:50 +0100399
400 switch (ffdd.ef_type) {
401 case EF_TYPE_RECORD_FIXED:
402 for (i = 0; i < ffdd.num_rec; i++) {
Harald Welte870f94d2020-03-19 19:10:34 +0100403 const char *hex;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200404 rmsg = read_record_nr(chan, i+1, ffdd.rec_len);
Harald Welte870f94d2020-03-19 19:10:34 +0100405 if (!rmsg) {
406 if (f_data)
407 fclose(f_data);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200408 return -EIO;
Harald Welte870f94d2020-03-19 19:10:34 +0100409 }
Harald Welte3a6bedf2020-03-22 10:30:10 +0100410 printf("SW: %s\n", osim_print_sw(chan, msgb_apdu_sw(msg)));
Harald Welte870f94d2020-03-19 19:10:34 +0100411
412 hex = osmo_hexdump_nospc(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg));
413 printf("Rec %03u: %s\n", i+1, hex);
414 if (f_data)
415 fprintf(f_data, "%s\n", hex);
Harald Welted54c2ee2012-01-17 18:25:50 +0100416 }
417 break;
418 case EF_TYPE_TRANSP:
Philipp Maierfde9fdc2020-02-26 12:00:23 +0100419 if (g_class != 0xA0) {
Harald Welte17051402020-03-22 11:25:32 +0100420 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_SIZE))
Philipp Maierfde9fdc2020-02-26 12:00:23 +0100421 goto out;
422 i = ntohs(*(uint16_t *)TLVP_VAL(&tp, UICC_FCP_T_FILE_SIZE));
423 printf("File size: %d bytes\n", i);
424 } else {
Harald Welte835ed962020-03-19 18:00:04 +0100425 fprintf(stderr, "Can not determine file size, invalid EF-type!\n");
Harald Weltea0ba4d92012-09-10 10:43:15 +0200426 goto out;
Philipp Maierfde9fdc2020-02-26 12:00:23 +0100427 }
Harald Weltea0ba4d92012-09-10 10:43:15 +0200428 for (offset = 0; offset < i-1; ) {
429 uint16_t remain_len = i - offset;
430 uint16_t read_len = OSMO_MIN(remain_len, 256);
Harald Welte870f94d2020-03-19 19:10:34 +0100431 const char *hex;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200432 rmsg = read_binary(chan, offset, read_len);
Harald Welte870f94d2020-03-19 19:10:34 +0100433 if (!rmsg) {
434 if (f_data)
435 fclose(f_data);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200436 return -EIO;
Harald Welte870f94d2020-03-19 19:10:34 +0100437 }
Harald Weltea0ba4d92012-09-10 10:43:15 +0200438 offset += read_len;
Harald Welte870f94d2020-03-19 19:10:34 +0100439 hex = osmo_hexdump_nospc(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg));
440 printf("Content: %s\n", hex);
441 if (f_data)
442 fprintf(f_data, "%s", hex);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200443 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100444 break;
445 default:
446 goto out;
447 }
448
449out:
Harald Welte870f94d2020-03-19 19:10:34 +0100450 if (f_data)
451 fclose(f_data);
Harald Welted54c2ee2012-01-17 18:25:50 +0100452 msgb_free(msg);
453 return -EINVAL;
Harald Welte870f94d2020-03-19 19:10:34 +0100454
Harald Welted54c2ee2012-01-17 18:25:50 +0100455}
456
Eric Wild94cd4ac2019-10-31 19:18:45 +0100457static void print_help(void)
458{
459 printf( "osmo-sim-test Usage:\n"
460 " -h --help This message\n"
461 " -n --reader-num NR Open reader number NR\n"
Harald Welte870f94d2020-03-19 19:10:34 +0100462 " -o --output-dir DIR To-be-created output directory for filesystem dump\n"
Eric Wild94cd4ac2019-10-31 19:18:45 +0100463 );
464}
465
466static int readernum = 0;
467
468static void handle_options(int argc, char **argv)
469{
470 while (1) {
471 int option_index = 0, c;
472 const struct option long_options[] = {
473 { "help", 0, 0, 'h' },
474 { "reader-num", 1, 0, 'n' },
Harald Welte870f94d2020-03-19 19:10:34 +0100475 { "output-dir", 1, 0, 'o' },
Eric Wild94cd4ac2019-10-31 19:18:45 +0100476 {0,0,0,0}
477 };
478
Harald Welte870f94d2020-03-19 19:10:34 +0100479 c = getopt_long(argc, argv, "hn:o:",
Eric Wild94cd4ac2019-10-31 19:18:45 +0100480 long_options, &option_index);
481 if (c == -1)
482 break;
483
484 switch (c) {
485 case 'h':
486 print_help();
487 exit(0);
488 break;
489 case 'n':
490 readernum = atoi(optarg);
491 break;
Harald Welte870f94d2020-03-19 19:10:34 +0100492 case 'o':
493 g_output_dir = optarg;
494 break;
Eric Wild94cd4ac2019-10-31 19:18:45 +0100495 default:
496 exit(2);
497 break;
498 }
499 }
500
501 if (argc > optind) {
502 fprintf(stderr, "Unsupported positional arguments on command line\n");
503 exit(2);
504 }
505}
506
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100507
Harald Welte870f94d2020-03-19 19:10:34 +0100508static void mkdir_and_chdir(const char *name, mode_t mode)
509{
510 int rc;
511 rc = mkdir(name, mode);
512 if (rc < 0) {
513 fprintf(stderr, "Cannot create '%s': %s\n", name, strerror(errno));
514 exit(24);
515 }
516 rc = chdir(name);
517 if (rc < 0) {
518 fprintf(stderr, "Cannot change to just-created '%s': %s\n", name, strerror(errno));
519 exit(24);
520 }
521}
522
523
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100524static void iterate_fs(struct osim_chan_hdl *chan)
525{
526 const struct osim_file_desc *prev_cwd;
527 struct osim_file_desc *ofd;
528
529 /* iterate over all files in current working directory */
530 llist_for_each_entry(ofd, &chan->cwd->child_list, list) {
531 struct msgb *m;
Harald Welte870f94d2020-03-19 19:10:34 +0100532 char prev_dir[PATH_MAX];
533
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100534 printf("\n\n================ %s (%s) ==================\n",
535 ofd->short_name, ofd->long_name);
536
537 m = select_file(chan, ofd->fid);
538 if (msgb_apdu_sw(m) != 0x9000) {
539 msgb_free(m);
540 continue;
541 }
542 dump_fcp_template_msg(m);
543 msgb_free(m);
544
545 /* If this is a DF, recurse into it */
546 switch (ofd->type) {
547 case TYPE_DF:
548 /* the select above has just changed into this directory */
549 prev_cwd = chan->cwd;
550 chan->cwd = ofd;
Harald Welte870f94d2020-03-19 19:10:34 +0100551 if (g_output_dir) {
552 if (!getcwd(prev_dir, sizeof(prev_dir))) {
553 fprintf(stderr, "Cannot determine cwd: %s\n", strerror(errno));
554 exit(23);
555 continue;
556 }
557 mkdir_and_chdir(ofd->short_name, 0750);
558 }
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100559 iterate_fs(chan);
560 /* "pop" the directory from the stack */
561 chan->cwd = prev_cwd;
Harald Welte870f94d2020-03-19 19:10:34 +0100562 if (g_output_dir)
563 OSMO_ASSERT(chdir("..") == 0);
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100564 break;
565 default:
Harald Welte870f94d2020-03-19 19:10:34 +0100566 dump_file(chan, ofd->short_name, ofd->fid);
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100567 break;
568 }
569 }
570}
571
Harald Welte429adec2020-03-20 13:05:40 +0100572static void iterate_apps(struct osim_chan_hdl *chan)
573{
574 struct osim_card_app_hdl *cah;
575
576 llist_for_each_entry(cah, &chan->card->apps, list) {
577 const struct osim_card_app_profile *cap = cah->prof;
578 struct msgb *msg;
579
580 if (!cap) {
581 fprintf(stderr, "Unknown AID %s; skipping\n",
582 osmo_hexdump_nospc(cah->aid, cah->aid_len));
583 continue;
584 }
585
586 msg = select_adf(chan, cah->aid, cah->aid_len);
587 if (!msg) {
588 fprintf(stderr, "Error selectiong ADF for AID %s; skipping\n",
589 osmo_hexdump_nospc(cah->aid, cah->aid_len));
590 continue;
591 }
Harald Welte3a6bedf2020-03-22 10:30:10 +0100592 printf("SW: %s\n", osim_print_sw(chan, msgb_apdu_sw(msg)));
Harald Welte429adec2020-03-20 13:05:40 +0100593 chan->cur_app = cah;
594 chan->cwd = cap->adf;
595
596 if (g_output_dir)
597 mkdir_and_chdir(cap->adf->short_name, 0750);
598
599 iterate_fs(chan);
600
601 if (g_output_dir)
602 OSMO_ASSERT(chdir("..") == 0);
603 }
604}
605
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100606
Harald Welted54c2ee2012-01-17 18:25:50 +0100607int main(int argc, char **argv)
608{
609 struct osim_reader_hdl *reader;
610 struct osim_card_hdl *card;
611 struct osim_chan_hdl *chan;
Harald Welte429adec2020-03-20 13:05:40 +0100612 int rc;
Harald Welted54c2ee2012-01-17 18:25:50 +0100613
Eric Wild94cd4ac2019-10-31 19:18:45 +0100614 handle_options(argc, argv);
615
Harald Welte429adec2020-03-20 13:05:40 +0100616 osim_init(NULL);
617
Harald Welte870f94d2020-03-19 19:10:34 +0100618 if (g_output_dir) {
619 int rc;
620 rc = mkdir(g_output_dir, 0750);
621 if (rc < 0) {
622 fprintf(stderr, "Cannot create directory '%s': %s\n", g_output_dir,
623 strerror(errno));
624 exit(5);
625 }
626 rc = chdir(g_output_dir);
627 if (rc < 0) {
628 fprintf(stderr, "Cannot change to just-created directory '%s': %s\n",
629 g_output_dir, strerror(errno));
630 exit(5);
631 }
632 }
633
Eric Wild94cd4ac2019-10-31 19:18:45 +0100634 reader = osim_reader_open(OSIM_READER_DRV_PCSC, readernum, "", NULL);
Harald Welted54c2ee2012-01-17 18:25:50 +0100635 if (!reader)
636 exit(1);
Harald Welte55790aa2014-10-26 18:46:50 +0100637 card = osim_card_open(reader, OSIM_PROTO_T0);
Harald Welted54c2ee2012-01-17 18:25:50 +0100638 if (!card)
639 exit(2);
640 chan = llist_entry(card->channels.next, struct osim_chan_hdl, list);
641 if (!chan)
642 exit(3);
643
Harald Welte429adec2020-03-20 13:05:40 +0100644 //verify_pin(chan, 1, "1653");
645
646 rc = osim_uicc_scan_apps(chan);
647 if (rc >= 0) {
Harald Welte58d173a2020-03-21 13:40:28 +0100648 chan->card->prof = osim_cprof_uicc(chan->card, true);
Harald Welte429adec2020-03-20 13:05:40 +0100649 chan->cwd = chan->card->prof->mf;
650 } else if (rc == -0x6e00) {
Harald Welte053bebc2020-02-15 18:58:16 +0100651 /* CLA not supported: must be classic SIM, not USIM */
652 g_class = 0xA0;
653 chan->card->prof = osim_cprof_sim(chan->card);
654 chan->cwd = chan->card->prof->mf;
Harald Welte429adec2020-03-20 13:05:40 +0100655 } else if (rc < 0) {
656 exit(4);
Harald Welte053bebc2020-02-15 18:58:16 +0100657 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100658
Harald Welte429adec2020-03-20 13:05:40 +0100659 /* first iterate over normal file system */
Harald Welte3a1a3bb2020-02-15 18:56:18 +0100660 iterate_fs(chan);
Harald Welted54c2ee2012-01-17 18:25:50 +0100661
Harald Welte429adec2020-03-20 13:05:40 +0100662 /* then itereate over all apps and their file system */
663 iterate_apps(chan);
664
Harald Welted54c2ee2012-01-17 18:25:50 +0100665 exit(0);
666}