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> |
Alexander Huemer | aab4a24 | 2015-11-06 20:55:24 +0100 | [diff] [blame] | 25 | #include <arpa/inet.h> |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 26 | |
| 27 | #include <osmocom/core/msgb.h> |
| 28 | #include <osmocom/core/talloc.h> |
| 29 | #include <osmocom/sim/sim.h> |
| 30 | #include <osmocom/gsm/tlv.h> |
| 31 | |
| 32 | |
Harald Welte | ad41863 | 2012-09-10 10:49:59 +0200 | [diff] [blame] | 33 | /* FIXME: this needs to be moved to card_fs_uicc.c */ |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 34 | |
| 35 | /* 11.1.1 */ |
| 36 | static struct msgb *_select_file(struct osim_chan_hdl *st, uint8_t p1, uint8_t p2, |
| 37 | const uint8_t *data, uint8_t data_len) |
| 38 | { |
Vadim Yanitskiy | 1cd9991 | 2017-05-15 21:37:16 +0300 | [diff] [blame] | 39 | struct msgb *msg; |
Harald Welte | f12d40f | 2017-02-08 15:46:53 +0000 | [diff] [blame] | 40 | uint8_t *dst; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 41 | |
| 42 | msg = osim_new_apdumsg(0x00, 0xA4, p1, p2, data_len, 256); |
| 43 | dst = msgb_put(msg, data_len); |
| 44 | memcpy(dst, data, data_len); |
| 45 | |
| 46 | osim_transceive_apdu(st, msg); |
| 47 | |
| 48 | return msg; |
| 49 | } |
| 50 | |
| 51 | /* 11.1.1 */ |
| 52 | static struct msgb *select_adf(struct osim_chan_hdl *st, const uint8_t *adf, uint8_t adf_len) |
| 53 | { |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 54 | return _select_file(st, 0x04, 0x04, adf,adf_len); |
| 55 | } |
| 56 | |
| 57 | /* 11.1.1 */ |
| 58 | static struct msgb *select_file(struct osim_chan_hdl *st, uint16_t fid) |
| 59 | { |
| 60 | uint16_t cfid = htons(fid); |
| 61 | |
| 62 | return _select_file(st, 0x00, 0x04, (uint8_t *)&cfid, 2); |
| 63 | } |
| 64 | |
| 65 | /* 11.1.9 */ |
Harald Welte | f12d40f | 2017-02-08 15:46:53 +0000 | [diff] [blame] | 66 | 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] | 67 | { |
| 68 | struct msgb *msg; |
| 69 | char *pindst; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 70 | |
| 71 | if (strlen(pin) > 8) |
| 72 | return -EINVAL; |
| 73 | |
| 74 | msg = osim_new_apdumsg(0x00, 0x20, 0x00, pin_nr, 8, 0); |
Harald Welte | f12d40f | 2017-02-08 15:46:53 +0000 | [diff] [blame] | 75 | pindst = (char *) msgb_put(msg, 8); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 76 | memset(pindst, 0xFF, 8); |
| 77 | strncpy(pindst, pin, strlen(pin)); |
| 78 | |
| 79 | return osim_transceive_apdu(st, msg); |
| 80 | } |
| 81 | |
| 82 | /* 11.1.5 */ |
| 83 | static struct msgb *read_record_nr(struct osim_chan_hdl *st, uint8_t rec_nr, uint16_t rec_size) |
| 84 | { |
| 85 | struct msgb *msg; |
| 86 | |
| 87 | msg = osim_new_apdumsg(0x00, 0xB2, rec_nr, 0x04, 0, rec_size); |
| 88 | |
| 89 | osim_transceive_apdu(st, msg); |
| 90 | |
| 91 | return msg; |
| 92 | } |
| 93 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 94 | /* 11.1.3 */ |
| 95 | static struct msgb *read_binary(struct osim_chan_hdl *st, uint16_t offset, uint16_t len) |
| 96 | { |
| 97 | struct msgb *msg; |
| 98 | |
| 99 | if (offset > 0x7fff || len > 256) |
| 100 | return NULL; |
| 101 | |
| 102 | msg = osim_new_apdumsg(0x00, 0xB0, offset >> 8, offset & 0xff, 0, len & 0xff); |
| 103 | |
| 104 | osim_transceive_apdu(st, msg); |
| 105 | |
| 106 | return msg; |
| 107 | } |
| 108 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 109 | static int dump_fcp_template(struct tlv_parsed *tp) |
| 110 | { |
| 111 | int i; |
| 112 | |
| 113 | for (i = 0; i < ARRAY_SIZE(tp->lv); i++) { |
| 114 | if (TLVP_PRESENT(tp, i)) |
| 115 | printf("Tag 0x%02x (%s): %s\n", i, |
| 116 | get_value_string(ts102221_fcp_vals, i), |
| 117 | osmo_hexdump(TLVP_VAL(tp, i), TLVP_LEN(tp, i))); |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int dump_fcp_template_msg(struct msgb *msg) |
| 124 | { |
| 125 | struct tlv_parsed tp; |
| 126 | int rc; |
| 127 | |
Harald Welte | a5c9255 | 2012-09-10 21:05:42 +0200 | [diff] [blame] | 128 | 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] | 129 | if (rc < 0) |
| 130 | return rc; |
| 131 | |
| 132 | return dump_fcp_template(&tp); |
| 133 | } |
| 134 | |
| 135 | struct osim_fcp_fd_decoded { |
| 136 | enum osim_file_type type; |
| 137 | enum osim_ef_type ef_type; |
| 138 | uint16_t rec_len; |
| 139 | uint8_t num_rec; |
| 140 | }; |
| 141 | |
| 142 | static const enum osim_file_type iso2ftype[8] = { |
| 143 | [0] = TYPE_EF, |
| 144 | [1] = TYPE_EF_INT, |
| 145 | [7] = TYPE_DF, |
| 146 | }; |
| 147 | |
| 148 | static const enum osim_ef_type iso2eftype[8] = { |
| 149 | [1] = EF_TYPE_TRANSP, |
| 150 | [2] = EF_TYPE_RECORD_FIXED, |
| 151 | [6] = EF_TYPE_RECORD_CYCLIC, |
| 152 | }; |
| 153 | |
| 154 | static int osim_fcp_fd_decode(struct osim_fcp_fd_decoded *ofd, const uint8_t *fcp, int fcp_len) |
| 155 | { |
| 156 | memset(ofd, 0, sizeof(*ofd)); |
| 157 | |
| 158 | if (fcp_len != 2 && fcp_len != 5) |
| 159 | return -EINVAL; |
| 160 | |
| 161 | ofd->type = iso2ftype[(fcp[0] >> 3) & 7]; |
| 162 | if (ofd->type != TYPE_DF) |
| 163 | ofd->ef_type = iso2eftype[fcp[0] & 7]; |
| 164 | |
| 165 | if (fcp[1] != 0x21) |
| 166 | return -EINVAL; |
| 167 | |
| 168 | if (fcp_len >= 5) { |
| 169 | ofd->rec_len = ntohs(*(uint16_t *)(fcp+2)); |
| 170 | ofd->num_rec = fcp[4]; |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | extern struct osim_card_profile *osim_cprof_usim(void *ctx); |
| 177 | |
| 178 | static struct msgb *try_select_adf_usim(struct osim_chan_hdl *st) |
| 179 | { |
| 180 | struct tlv_parsed tp; |
| 181 | struct osim_fcp_fd_decoded ofd; |
| 182 | struct msgb *msg, *msg2; |
| 183 | uint8_t *cur; |
| 184 | int rc, i; |
| 185 | |
| 186 | msg = select_file(st, 0x2f00); |
Harald Welte | a5c9255 | 2012-09-10 21:05:42 +0200 | [diff] [blame] | 187 | 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] | 188 | if (rc < 0) |
| 189 | return NULL; |
| 190 | |
| 191 | dump_fcp_template(&tp); |
| 192 | |
| 193 | if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) || |
| 194 | TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 5) { |
| 195 | msgb_free(msg); |
| 196 | return NULL; |
| 197 | } |
| 198 | |
| 199 | rc = osim_fcp_fd_decode(&ofd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC), |
| 200 | TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC)); |
| 201 | if (rc < 0) { |
| 202 | msgb_free(msg); |
| 203 | return NULL; |
| 204 | } |
| 205 | |
| 206 | if (ofd.type != TYPE_EF || ofd.ef_type != EF_TYPE_RECORD_FIXED) { |
| 207 | msgb_free(msg); |
| 208 | return NULL; |
| 209 | } |
| 210 | |
| 211 | msgb_free(msg); |
| 212 | |
| 213 | printf("ofd rec_len = %u, num_rec = %u\n", ofd.rec_len, ofd.num_rec); |
| 214 | |
| 215 | for (i = 0; i < ofd.num_rec; i++) { |
| 216 | msg = read_record_nr(st, i+1, ofd.rec_len); |
| 217 | if (!msg) |
| 218 | return NULL; |
| 219 | |
| 220 | cur = msgb_apdu_de(msg); |
| 221 | if (msgb_apdu_le(msg) < 5) { |
| 222 | msgb_free(msg); |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | if (cur[0] != 0x61 || cur[1] < 0x03 || cur[1] > 0x7f || |
| 227 | cur[2] != 0x4F || cur[3] < 0x01 || cur[3] > 0x10) { |
| 228 | msgb_free(msg); |
| 229 | return NULL; |
| 230 | } |
| 231 | |
| 232 | /* FIXME: actually check if it is an AID that we support, or |
| 233 | * iterate until we find one that we support */ |
| 234 | |
| 235 | msg2 = select_adf(st, cur+4, cur[3]); |
| 236 | |
| 237 | /* attach the USIM profile, FIXME: do this based on AID match */ |
| 238 | st->card->prof = osim_cprof_usim(st->card); |
Harald Welte | 5ffb503 | 2016-03-11 09:40:56 +0700 | [diff] [blame] | 239 | 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] | 240 | |
| 241 | msgb_free(msg); |
| 242 | |
| 243 | return msg2; |
| 244 | } |
| 245 | |
| 246 | return NULL; |
| 247 | } |
| 248 | |
| 249 | static int dump_file(struct osim_chan_hdl *chan, uint16_t fid) |
| 250 | { |
| 251 | struct tlv_parsed tp; |
| 252 | struct osim_fcp_fd_decoded ffdd; |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 253 | struct msgb *msg, *rmsg; |
| 254 | int rc, i, offset; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 255 | |
| 256 | msg = select_file(chan, fid); |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 257 | if (!msg) { |
| 258 | printf("Unable to select file\n"); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 259 | return -EIO; |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 260 | } |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 261 | 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] | 262 | if (msgb_apdu_sw(msg) != 0x9000) { |
| 263 | printf("status 0x%04x selecting file\n", msgb_apdu_sw(msg)); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 264 | goto out; |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 265 | } |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 266 | |
Harald Welte | a5c9255 | 2012-09-10 21:05:42 +0200 | [diff] [blame] | 267 | rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-2, 0, 0); |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 268 | if (rc < 0) { |
| 269 | printf("Unable to parse FCP\n"); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 270 | goto out; |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 271 | } |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 272 | |
| 273 | if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) || |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 274 | TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 2) { |
| 275 | printf("No file descriptor present ?!?\n"); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 276 | goto out; |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 277 | } |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 278 | |
| 279 | rc = osim_fcp_fd_decode(&ffdd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC), |
| 280 | TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC)); |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 281 | if (rc < 0) { |
| 282 | printf("Unable to decode File Descriptor\n"); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 283 | goto out; |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 284 | } |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 285 | |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 286 | if (ffdd.type != TYPE_EF) { |
| 287 | printf("File Type != EF\n"); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 288 | goto out; |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | printf("EF type: %u\n", ffdd.ef_type); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 292 | |
| 293 | switch (ffdd.ef_type) { |
| 294 | case EF_TYPE_RECORD_FIXED: |
| 295 | for (i = 0; i < ffdd.num_rec; i++) { |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 296 | rmsg = read_record_nr(chan, i+1, ffdd.rec_len); |
Harald Welte | 9533631 | 2016-11-26 09:54:40 +0100 | [diff] [blame] | 297 | if (!rmsg) |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 298 | return -EIO; |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 299 | 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] | 300 | printf("Rec %03u: %s\n", i+1, |
| 301 | osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg))); |
| 302 | } |
| 303 | break; |
| 304 | case EF_TYPE_TRANSP: |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 305 | if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_SIZE)) |
| 306 | goto out; |
| 307 | i = ntohs(*(uint16_t *)TLVP_VAL(&tp, UICC_FCP_T_FILE_SIZE)); |
| 308 | printf("File size: %d bytes\n", i); |
| 309 | |
| 310 | for (offset = 0; offset < i-1; ) { |
| 311 | uint16_t remain_len = i - offset; |
| 312 | uint16_t read_len = OSMO_MIN(remain_len, 256); |
| 313 | rmsg = read_binary(chan, offset, read_len); |
Harald Welte | d6ec984 | 2014-10-27 20:43:06 +0100 | [diff] [blame] | 314 | if (!rmsg) |
Harald Welte | a0ba4d9 | 2012-09-10 10:43:15 +0200 | [diff] [blame] | 315 | return -EIO; |
| 316 | offset += read_len; |
| 317 | printf("Content: %s\n", |
| 318 | osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg))); |
| 319 | } |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 320 | break; |
| 321 | default: |
| 322 | goto out; |
| 323 | } |
| 324 | |
| 325 | out: |
| 326 | msgb_free(msg); |
| 327 | return -EINVAL; |
| 328 | } |
| 329 | |
| 330 | int main(int argc, char **argv) |
| 331 | { |
| 332 | struct osim_reader_hdl *reader; |
| 333 | struct osim_card_hdl *card; |
| 334 | struct osim_chan_hdl *chan; |
| 335 | struct msgb *msg; |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 336 | |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 337 | reader = osim_reader_open(OSIM_READER_DRV_PCSC, 0, "", NULL); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 338 | if (!reader) |
| 339 | exit(1); |
Harald Welte | 55790aa | 2014-10-26 18:46:50 +0100 | [diff] [blame] | 340 | card = osim_card_open(reader, OSIM_PROTO_T0); |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 341 | if (!card) |
| 342 | exit(2); |
| 343 | chan = llist_entry(card->channels.next, struct osim_chan_hdl, list); |
| 344 | if (!chan) |
| 345 | exit(3); |
| 346 | |
| 347 | msg = try_select_adf_usim(chan); |
| 348 | if (!msg || msgb_apdu_sw(msg) != 0x9000) |
| 349 | exit(4); |
| 350 | dump_fcp_template_msg(msg); |
| 351 | msgb_free(msg); |
| 352 | |
| 353 | msg = select_file(chan, 0x6fc5); |
| 354 | dump_fcp_template_msg(msg); |
Harald Welte | 7674960 | 2012-09-19 20:55:54 +0200 | [diff] [blame] | 355 | 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] | 356 | msgb_free(msg); |
| 357 | |
| 358 | verify_pin(chan, 1, "1653"); |
| 359 | |
| 360 | msg = select_file(chan, 0x6f06); |
| 361 | dump_fcp_template_msg(msg); |
| 362 | msgb_free(msg); |
| 363 | |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 364 | { |
| 365 | struct osim_file_desc *ofd; |
| 366 | llist_for_each_entry(ofd, &chan->cwd->child_list, list) { |
| 367 | struct msgb *m; |
| 368 | printf("\n\n================ %s (%s) ==================\n", |
| 369 | ofd->short_name, ofd->long_name); |
| 370 | |
| 371 | m = select_file(chan, ofd->fid); |
| 372 | dump_fcp_template_msg(m); |
| 373 | msgb_free(m); |
| 374 | dump_file(chan, ofd->fid); |
| 375 | } |
| 376 | } |
Harald Welte | d54c2ee | 2012-01-17 18:25:50 +0100 | [diff] [blame] | 377 | |
| 378 | exit(0); |
| 379 | } |