Harald Welte | ad41863 | 2012-09-10 10:49:59 +0200 | [diff] [blame] | 1 | /* libosmosim test application - currently simply dumps a USIM */ |
| 2 | /* (C) 2012 by Harald Welte <laforge@gnumonks.org> |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | */ |
| 20 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <errno.h> |
| 24 | #include <string.h> |
Eric Wild | 94cd4ac | 2019-10-31 19:18:45 +0100 | [diff] [blame] | 25 | #include <getopt.h> |
Alexander Huemer | aab4a24 | 2015-11-06 20:55:24 +0100 | [diff] [blame] | 26 | #include <arpa/inet.h> |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 27 | |
| 28 | #include <osmocom/core/msgb.h> |
| 29 | #include <osmocom/core/talloc.h> |
| 30 | #include <osmocom/sim/sim.h> |
| 31 | #include <osmocom/gsm/tlv.h> |
| 32 | |
| 33 | |
Harald Welte | ad41863 | 2012-09-10 10:49:59 +0200 | [diff] [blame] | 34 | /* FIXME: this needs to be moved to card_fs_uicc.c */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 35 | |
Harald Welte | 053bebc | 2020-02-15 18:58:16 +0100 | [diff] [blame] | 36 | static uint8_t g_class = 0x00; /* UICC/USIM */ |
| 37 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 38 | /* 11.1.1 */ |
| 39 | static struct msgb *_select_file(struct osim_chan_hdl *st, uint8_t p1, uint8_t p2, |
| 40 | const uint8_t *data, uint8_t data_len) |
| 41 | { |
Vadim Yanitskiy | 1cd9991 | 2017-05-15 21:37:16 +0300 | [diff] [blame] | 42 | struct msgb *msg; |
Harald Welte | f12d40f | 2017-02-08 15:46:53 +0000 | [diff] [blame] | 43 | uint8_t *dst; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 44 | |
Harald Welte | 053bebc | 2020-02-15 18:58:16 +0100 | [diff] [blame] | 45 | msg = osim_new_apdumsg(g_class, 0xA4, p1, p2, data_len, 256); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 46 | dst = msgb_put(msg, data_len); |
| 47 | memcpy(dst, data, data_len); |
| 48 | |
| 49 | osim_transceive_apdu(st, msg); |
| 50 | |
| 51 | return msg; |
| 52 | } |
| 53 | |
| 54 | /* 11.1.1 */ |
| 55 | static struct msgb *select_adf(struct osim_chan_hdl *st, const uint8_t *adf, uint8_t adf_len) |
| 56 | { |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 57 | return _select_file(st, 0x04, 0x04, adf,adf_len); |
| 58 | } |
| 59 | |
| 60 | /* 11.1.1 */ |
| 61 | static struct msgb *select_file(struct osim_chan_hdl *st, uint16_t fid) |
| 62 | { |
| 63 | uint16_t cfid = htons(fid); |
Harald Welte | 053bebc | 2020-02-15 18:58:16 +0100 | [diff] [blame] | 64 | uint8_t p2 = 0x04; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 65 | |
Harald Welte | 053bebc | 2020-02-15 18:58:16 +0100 | [diff] [blame] | 66 | /* Classic SIM cards don't support 0x04 (Return FCP) */ |
| 67 | if (g_class == 0xA0) |
| 68 | p2 = 0x00; |
| 69 | |
| 70 | return _select_file(st, 0x00, p2, (uint8_t *)&cfid, 2); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /* 11.1.9 */ |
Harald Welte | f12d40f | 2017-02-08 15:46:53 +0000 | [diff] [blame] | 74 | static int verify_pin(struct osim_chan_hdl *st, uint8_t pin_nr, char *pin) |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 75 | { |
| 76 | struct msgb *msg; |
| 77 | char *pindst; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 78 | |
| 79 | if (strlen(pin) > 8) |
| 80 | return -EINVAL; |
| 81 | |
Harald Welte | 053bebc | 2020-02-15 18:58:16 +0100 | [diff] [blame] | 82 | msg = osim_new_apdumsg(g_class, 0x20, 0x00, pin_nr, 8, 0); |
Harald Welte | f12d40f | 2017-02-08 15:46:53 +0000 | [diff] [blame] | 83 | pindst = (char *) msgb_put(msg, 8); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 84 | memset(pindst, 0xFF, 8); |
Neels Hofmeyr | 95fdbc1 | 2018-07-26 17:14:40 +0200 | [diff] [blame] | 85 | /* Do not copy the terminating \0 */ |
| 86 | memcpy(pindst, pin, strlen(pin)); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 87 | |
| 88 | return osim_transceive_apdu(st, msg); |
| 89 | } |
| 90 | |
| 91 | /* 11.1.5 */ |
| 92 | static struct msgb *read_record_nr(struct osim_chan_hdl *st, uint8_t rec_nr, uint16_t rec_size) |
| 93 | { |
| 94 | struct msgb *msg; |
| 95 | |
Harald Welte | 053bebc | 2020-02-15 18:58:16 +0100 | [diff] [blame] | 96 | msg = osim_new_apdumsg(g_class, 0xB2, rec_nr, 0x04, 0, rec_size); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 97 | |
| 98 | osim_transceive_apdu(st, msg); |
| 99 | |
| 100 | return msg; |
| 101 | } |
| 102 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 103 | /* 11.1.3 */ |
| 104 | static struct msgb *read_binary(struct osim_chan_hdl *st, uint16_t offset, uint16_t len) |
| 105 | { |
| 106 | struct msgb *msg; |
| 107 | |
| 108 | if (offset > 0x7fff || len > 256) |
| 109 | return NULL; |
| 110 | |
Harald Welte | 053bebc | 2020-02-15 18:58:16 +0100 | [diff] [blame] | 111 | msg = osim_new_apdumsg(g_class, 0xB0, offset >> 8, offset & 0xff, 0, len & 0xff); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 112 | |
| 113 | osim_transceive_apdu(st, msg); |
| 114 | |
| 115 | return msg; |
| 116 | } |
| 117 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 118 | static int dump_fcp_template(struct tlv_parsed *tp) |
| 119 | { |
| 120 | int i; |
| 121 | |
| 122 | for (i = 0; i < ARRAY_SIZE(tp->lv); i++) { |
| 123 | if (TLVP_PRESENT(tp, i)) |
| 124 | printf("Tag 0x%02x (%s): %s\n", i, |
| 125 | get_value_string(ts102221_fcp_vals, i), |
| 126 | osmo_hexdump(TLVP_VAL(tp, i), TLVP_LEN(tp, i))); |
| 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static int dump_fcp_template_msg(struct msgb *msg) |
| 133 | { |
| 134 | struct tlv_parsed tp; |
| 135 | int rc; |
| 136 | |
Harald Welte | a5c9255 | 2012-09-10 21:05:42 +0200 | [diff] [blame] | 137 | rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-2, 0, 0); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 138 | if (rc < 0) |
| 139 | return rc; |
| 140 | |
| 141 | return dump_fcp_template(&tp); |
| 142 | } |
| 143 | |
| 144 | struct osim_fcp_fd_decoded { |
| 145 | enum osim_file_type type; |
| 146 | enum osim_ef_type ef_type; |
| 147 | uint16_t rec_len; |
| 148 | uint8_t num_rec; |
| 149 | }; |
| 150 | |
| 151 | static const enum osim_file_type iso2ftype[8] = { |
| 152 | [0] = TYPE_EF, |
| 153 | [1] = TYPE_EF_INT, |
| 154 | [7] = TYPE_DF, |
| 155 | }; |
| 156 | |
| 157 | static const enum osim_ef_type iso2eftype[8] = { |
| 158 | [1] = EF_TYPE_TRANSP, |
| 159 | [2] = EF_TYPE_RECORD_FIXED, |
| 160 | [6] = EF_TYPE_RECORD_CYCLIC, |
| 161 | }; |
| 162 | |
| 163 | static int osim_fcp_fd_decode(struct osim_fcp_fd_decoded *ofd, const uint8_t *fcp, int fcp_len) |
| 164 | { |
| 165 | memset(ofd, 0, sizeof(*ofd)); |
| 166 | |
| 167 | if (fcp_len != 2 && fcp_len != 5) |
| 168 | return -EINVAL; |
| 169 | |
| 170 | ofd->type = iso2ftype[(fcp[0] >> 3) & 7]; |
| 171 | if (ofd->type != TYPE_DF) |
| 172 | ofd->ef_type = iso2eftype[fcp[0] & 7]; |
| 173 | |
| 174 | if (fcp[1] != 0x21) |
| 175 | return -EINVAL; |
| 176 | |
| 177 | if (fcp_len >= 5) { |
| 178 | ofd->rec_len = ntohs(*(uint16_t *)(fcp+2)); |
| 179 | ofd->num_rec = fcp[4]; |
| 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
Harald Welte | 053bebc | 2020-02-15 18:58:16 +0100 | [diff] [blame] | 185 | /* TS 51.011 Section 9.3 Type of File */ |
| 186 | static const enum osim_file_type sim2ftype[8] = { |
| 187 | [1] = TYPE_MF, |
| 188 | [2] = TYPE_DF, |
| 189 | [4] = TYPE_EF, |
| 190 | }; |
| 191 | |
| 192 | /* TS 51.011 Section 9.3 Structure of File */ |
| 193 | static const enum osim_ef_type sim2eftype[8] = { |
| 194 | [0] = EF_TYPE_TRANSP, |
| 195 | [1] = EF_TYPE_RECORD_FIXED, |
| 196 | [3] = EF_TYPE_RECORD_CYCLIC, |
| 197 | }; |
| 198 | |
| 199 | /* TS 51.011 Section 9.2.1 */ |
| 200 | static int osim_fcp_fd_decode_sim(struct osim_fcp_fd_decoded *ofd, const uint8_t *fcp, int fcp_len) |
| 201 | { |
| 202 | memset(ofd, 0, sizeof(*ofd)); |
| 203 | |
| 204 | if (fcp_len < 14) |
| 205 | return -EINVAL; |
| 206 | |
| 207 | ofd->type = sim2ftype[fcp[6] & 7]; |
| 208 | switch (ofd->type) { |
| 209 | case TYPE_EF: |
| 210 | ofd->ef_type = sim2eftype[fcp[13] & 7]; |
| 211 | if (fcp_len < 13 + fcp[12]) |
| 212 | return -EINVAL; |
| 213 | switch (ofd->ef_type) { |
| 214 | case EF_TYPE_RECORD_FIXED: |
| 215 | case EF_TYPE_RECORD_CYCLIC: |
| 216 | if (fcp_len < 15) |
| 217 | return -EINVAL; |
| 218 | ofd->rec_len = fcp[14]; |
| 219 | ofd->num_rec = ntohs(*(uint16_t *)(fcp+2)) / ofd->rec_len; |
| 220 | break; |
| 221 | default: |
| 222 | break; |
| 223 | } |
| 224 | break; |
| 225 | case TYPE_MF: |
| 226 | case TYPE_DF: |
| 227 | if (fcp_len < 22) |
| 228 | return -EINVAL; |
| 229 | break; |
| 230 | default: |
| 231 | break; |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | extern struct osim_card_profile *osim_cprof_sim(void *ctx); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 238 | extern struct osim_card_profile *osim_cprof_usim(void *ctx); |
Harald Welte | 053bebc | 2020-02-15 18:58:16 +0100 | [diff] [blame] | 239 | extern struct osim_card_profile *osim_cprof_isim(void *ctx); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 240 | |
| 241 | static struct msgb *try_select_adf_usim(struct osim_chan_hdl *st) |
| 242 | { |
| 243 | struct tlv_parsed tp; |
| 244 | struct osim_fcp_fd_decoded ofd; |
| 245 | struct msgb *msg, *msg2; |
| 246 | uint8_t *cur; |
| 247 | int rc, i; |
| 248 | |
| 249 | msg = select_file(st, 0x2f00); |
Harald Welte | 053bebc | 2020-02-15 18:58:16 +0100 | [diff] [blame] | 250 | if (!msg) |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 251 | return NULL; |
Harald Welte | 053bebc | 2020-02-15 18:58:16 +0100 | [diff] [blame] | 252 | /* return status word in case of error */ |
| 253 | if (msgb_apdu_sw(msg) != 0x9000) |
| 254 | return msg; |
| 255 | |
| 256 | rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-2, 0, 0); |
| 257 | if (rc < 0) { |
| 258 | msgb_free(msg); |
| 259 | return NULL; |
| 260 | } |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 261 | |
| 262 | dump_fcp_template(&tp); |
| 263 | |
| 264 | if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) || |
| 265 | TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 5) { |
| 266 | msgb_free(msg); |
| 267 | return NULL; |
| 268 | } |
| 269 | |
| 270 | rc = osim_fcp_fd_decode(&ofd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC), |
| 271 | TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC)); |
| 272 | if (rc < 0) { |
| 273 | msgb_free(msg); |
| 274 | return NULL; |
| 275 | } |
| 276 | |
| 277 | if (ofd.type != TYPE_EF || ofd.ef_type != EF_TYPE_RECORD_FIXED) { |
| 278 | msgb_free(msg); |
| 279 | return NULL; |
| 280 | } |
| 281 | |
| 282 | msgb_free(msg); |
| 283 | |
| 284 | printf("ofd rec_len = %u, num_rec = %u\n", ofd.rec_len, ofd.num_rec); |
| 285 | |
| 286 | for (i = 0; i < ofd.num_rec; i++) { |
| 287 | msg = read_record_nr(st, i+1, ofd.rec_len); |
| 288 | if (!msg) |
| 289 | return NULL; |
| 290 | |
| 291 | cur = msgb_apdu_de(msg); |
| 292 | if (msgb_apdu_le(msg) < 5) { |
| 293 | msgb_free(msg); |
| 294 | return NULL; |
| 295 | } |
| 296 | |
| 297 | if (cur[0] != 0x61 || cur[1] < 0x03 || cur[1] > 0x7f || |
| 298 | cur[2] != 0x4F || cur[3] < 0x01 || cur[3] > 0x10) { |
| 299 | msgb_free(msg); |
| 300 | return NULL; |
| 301 | } |
| 302 | |
| 303 | /* FIXME: actually check if it is an AID that we support, or |
| 304 | * iterate until we find one that we support */ |
| 305 | |
| 306 | msg2 = select_adf(st, cur+4, cur[3]); |
| 307 | |
| 308 | /* attach the USIM profile, FIXME: do this based on AID match */ |
| 309 | st->card->prof = osim_cprof_usim(st->card); |
Harald Welte | 5ffb503 | 2016-03-11 09:40:56 +0700 | [diff] [blame] | 310 | st->cwd = osim_file_desc_find_name(st->card->prof->mf, "ADF.USIM"); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 311 | |
| 312 | msgb_free(msg); |
| 313 | |
| 314 | return msg2; |
| 315 | } |
| 316 | |
| 317 | return NULL; |
| 318 | } |
| 319 | |
| 320 | static int dump_file(struct osim_chan_hdl *chan, uint16_t fid) |
| 321 | { |
| 322 | struct tlv_parsed tp; |
| 323 | struct osim_fcp_fd_decoded ffdd; |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 324 | struct msgb *msg, *rmsg; |
| 325 | int rc, i, offset; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 326 | |
| 327 | msg = select_file(chan, fid); |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 328 | if (!msg) { |
| 329 | printf("Unable to select file\n"); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 330 | return -EIO; |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 331 | } |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 332 | printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg))); |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 333 | if (msgb_apdu_sw(msg) != 0x9000) { |
| 334 | printf("status 0x%04x selecting file\n", msgb_apdu_sw(msg)); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 335 | goto out; |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 336 | } |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 337 | |
Harald Welte | 053bebc | 2020-02-15 18:58:16 +0100 | [diff] [blame] | 338 | if (g_class != 0xA0) { |
| 339 | rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-2, 0, 0); |
| 340 | if (rc < 0) { |
| 341 | printf("Unable to parse FCP: %s\n", msgb_hexdump(msg)); |
| 342 | goto out; |
| 343 | } |
| 344 | |
| 345 | if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) || |
| 346 | TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 2) { |
| 347 | printf("No file descriptor present ?!?\n"); |
| 348 | goto out; |
| 349 | } |
| 350 | |
| 351 | rc = osim_fcp_fd_decode(&ffdd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC), |
| 352 | TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC)); |
| 353 | } else { |
| 354 | rc = osim_fcp_fd_decode_sim(&ffdd, msgb_apdu_de(msg), msgb_apdu_le(msg)); |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 355 | } |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 356 | |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 357 | if (rc < 0) { |
| 358 | printf("Unable to decode File Descriptor\n"); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 359 | goto out; |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 360 | } |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 361 | |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 362 | if (ffdd.type != TYPE_EF) { |
| 363 | printf("File Type != EF\n"); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 364 | goto out; |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | printf("EF type: %u\n", ffdd.ef_type); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 368 | |
| 369 | switch (ffdd.ef_type) { |
| 370 | case EF_TYPE_RECORD_FIXED: |
| 371 | for (i = 0; i < ffdd.num_rec; i++) { |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 372 | rmsg = read_record_nr(chan, i+1, ffdd.rec_len); |
Harald Welte | 9533631 | 2016-11-26 09:54:40 +0100 | [diff] [blame] | 373 | if (!rmsg) |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 374 | return -EIO; |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 375 | printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg))); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 376 | printf("Rec %03u: %s\n", i+1, |
| 377 | osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg))); |
| 378 | } |
| 379 | break; |
| 380 | case EF_TYPE_TRANSP: |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 381 | if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_SIZE)) |
| 382 | goto out; |
| 383 | i = ntohs(*(uint16_t *)TLVP_VAL(&tp, UICC_FCP_T_FILE_SIZE)); |
| 384 | printf("File size: %d bytes\n", i); |
| 385 | |
| 386 | for (offset = 0; offset < i-1; ) { |
| 387 | uint16_t remain_len = i - offset; |
| 388 | uint16_t read_len = OSMO_MIN(remain_len, 256); |
| 389 | rmsg = read_binary(chan, offset, read_len); |
Harald Welte | d6ec984 | 2014-10-27 20:43:06 +0100 | [diff] [blame] | 390 | if (!rmsg) |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 391 | return -EIO; |
| 392 | offset += read_len; |
| 393 | printf("Content: %s\n", |
| 394 | osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg))); |
| 395 | } |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 396 | break; |
| 397 | default: |
| 398 | goto out; |
| 399 | } |
| 400 | |
| 401 | out: |
| 402 | msgb_free(msg); |
| 403 | return -EINVAL; |
| 404 | } |
| 405 | |
Eric Wild | 94cd4ac | 2019-10-31 19:18:45 +0100 | [diff] [blame] | 406 | static void print_help(void) |
| 407 | { |
| 408 | printf( "osmo-sim-test Usage:\n" |
| 409 | " -h --help This message\n" |
| 410 | " -n --reader-num NR Open reader number NR\n" |
| 411 | ); |
| 412 | } |
| 413 | |
| 414 | static int readernum = 0; |
| 415 | |
| 416 | static void handle_options(int argc, char **argv) |
| 417 | { |
| 418 | while (1) { |
| 419 | int option_index = 0, c; |
| 420 | const struct option long_options[] = { |
| 421 | { "help", 0, 0, 'h' }, |
| 422 | { "reader-num", 1, 0, 'n' }, |
| 423 | {0,0,0,0} |
| 424 | }; |
| 425 | |
| 426 | c = getopt_long(argc, argv, "hn:", |
| 427 | long_options, &option_index); |
| 428 | if (c == -1) |
| 429 | break; |
| 430 | |
| 431 | switch (c) { |
| 432 | case 'h': |
| 433 | print_help(); |
| 434 | exit(0); |
| 435 | break; |
| 436 | case 'n': |
| 437 | readernum = atoi(optarg); |
| 438 | break; |
| 439 | default: |
| 440 | exit(2); |
| 441 | break; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | if (argc > optind) { |
| 446 | fprintf(stderr, "Unsupported positional arguments on command line\n"); |
| 447 | exit(2); |
| 448 | } |
| 449 | } |
| 450 | |
Harald Welte | 3a1a3bb | 2020-02-15 18:56:18 +0100 | [diff] [blame] | 451 | |
| 452 | static void iterate_fs(struct osim_chan_hdl *chan) |
| 453 | { |
| 454 | const struct osim_file_desc *prev_cwd; |
| 455 | struct osim_file_desc *ofd; |
| 456 | |
| 457 | /* iterate over all files in current working directory */ |
| 458 | llist_for_each_entry(ofd, &chan->cwd->child_list, list) { |
| 459 | struct msgb *m; |
| 460 | printf("\n\n================ %s (%s) ==================\n", |
| 461 | ofd->short_name, ofd->long_name); |
| 462 | |
| 463 | m = select_file(chan, ofd->fid); |
| 464 | if (msgb_apdu_sw(m) != 0x9000) { |
| 465 | msgb_free(m); |
| 466 | continue; |
| 467 | } |
| 468 | dump_fcp_template_msg(m); |
| 469 | msgb_free(m); |
| 470 | |
| 471 | /* If this is a DF, recurse into it */ |
| 472 | switch (ofd->type) { |
| 473 | case TYPE_DF: |
| 474 | /* the select above has just changed into this directory */ |
| 475 | prev_cwd = chan->cwd; |
| 476 | chan->cwd = ofd; |
| 477 | iterate_fs(chan); |
| 478 | /* "pop" the directory from the stack */ |
| 479 | chan->cwd = prev_cwd; |
| 480 | break; |
| 481 | default: |
| 482 | dump_file(chan, ofd->fid); |
| 483 | break; |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 489 | int main(int argc, char **argv) |
| 490 | { |
| 491 | struct osim_reader_hdl *reader; |
| 492 | struct osim_card_hdl *card; |
| 493 | struct osim_chan_hdl *chan; |
| 494 | struct msgb *msg; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 495 | |
Eric Wild | 94cd4ac | 2019-10-31 19:18:45 +0100 | [diff] [blame] | 496 | handle_options(argc, argv); |
| 497 | |
| 498 | reader = osim_reader_open(OSIM_READER_DRV_PCSC, readernum, "", NULL); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 499 | if (!reader) |
| 500 | exit(1); |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 501 | card = osim_card_open(reader, OSIM_PROTO_T0); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 502 | if (!card) |
| 503 | exit(2); |
| 504 | chan = llist_entry(card->channels.next, struct osim_chan_hdl, list); |
| 505 | if (!chan) |
| 506 | exit(3); |
| 507 | |
| 508 | msg = try_select_adf_usim(chan); |
Harald Welte | 053bebc | 2020-02-15 18:58:16 +0100 | [diff] [blame] | 509 | if (!msg) { |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 510 | exit(4); |
Harald Welte | 053bebc | 2020-02-15 18:58:16 +0100 | [diff] [blame] | 511 | } else if (msgb_apdu_sw(msg) == 0x6e00) { |
| 512 | /* CLA not supported: must be classic SIM, not USIM */ |
| 513 | g_class = 0xA0; |
| 514 | chan->card->prof = osim_cprof_sim(chan->card); |
| 515 | chan->cwd = chan->card->prof->mf; |
| 516 | msgb_free(msg); |
| 517 | } else if (msgb_apdu_sw(msg) == 0x9000) { |
| 518 | /* normal file */ |
| 519 | dump_fcp_template_msg(msg); |
| 520 | msgb_free(msg); |
| 521 | } |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 522 | |
| 523 | msg = select_file(chan, 0x6fc5); |
| 524 | dump_fcp_template_msg(msg); |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 525 | printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg))); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 526 | msgb_free(msg); |
| 527 | |
| 528 | verify_pin(chan, 1, "1653"); |
| 529 | |
| 530 | msg = select_file(chan, 0x6f06); |
| 531 | dump_fcp_template_msg(msg); |
| 532 | msgb_free(msg); |
| 533 | |
Harald Welte | 3a1a3bb | 2020-02-15 18:56:18 +0100 | [diff] [blame] | 534 | iterate_fs(chan); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 535 | |
| 536 | exit(0); |
| 537 | } |