FreeCalypso > hg > freecalypso-hwlab
view uicc/select.c @ 132:5af88fa11b54
fc-uicc-tool: SELECT response parsing implemented (basic)
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 04 Feb 2021 01:36:54 +0000 |
parents | d0929cb42e1c |
children | 69628bcfec17 |
line wrap: on
line source
#include <sys/types.h> #include <ctype.h> #include <string.h> #include <strings.h> #include <stdio.h> #include <stdlib.h> #include <pcsclite.h> #include <winscard.h> #include "globals.h" select_op(file_id) unsigned file_id; { u_char cmd[7]; int rc; unsigned expect_resp_len; /* SELECT command APDU */ cmd[0] = 0x00; cmd[1] = 0xA4; cmd[2] = 0x00; cmd[3] = 0x04; cmd[4] = 2; cmd[5] = file_id >> 8; cmd[6] = file_id; rc = apdu_exchange(cmd, 7); if (rc < 0) return(rc); if ((sim_resp_sw & 0xFF00) != 0x6100) { fprintf(stderr, "error or unexpected SW response to SELECT of 0x%04X: %04X\n", file_id, sim_resp_sw); return(-1); } expect_resp_len = sim_resp_sw & 0xFF; /* GET RESPONSE follow-up */ cmd[1] = 0xC0; cmd[2] = 0; cmd[3] = 0; cmd[4] = expect_resp_len; rc = apdu_exchange(cmd, 5); if (rc < 0) return(rc); if (sim_resp_sw != 0x9000) { fprintf(stderr, "bad SW resp to GET RESPONSE after SELECT: %04X\n", sim_resp_sw); return(-1); } if (sim_resp_data_len != expect_resp_len) { fprintf(stderr, "error: GET RESPONSE after SELECT returned %u bytes, expected %u\n", sim_resp_data_len, expect_resp_len); return(-1); } return(0); } select_resp_header_check(ret_offset, ret_length) unsigned *ret_offset, *ret_length; { unsigned offset, len; if (sim_resp_data_len < 2) { tooshort: fprintf(stderr, "error: SELECT response is too short\n"); return(-1); } if (sim_resp_data[0] != 0x62) { fprintf(stderr, "error: SELECT response first byte != 0x62\n"); return(-1); } len = sim_resp_data[1]; if (len <= 0x7F) { offset = 2; return_check: if (offset + len > sim_resp_data_len) goto tooshort; if (ret_offset) *ret_offset = offset; if (ret_length) *ret_length = len; return(0); } if (len != 0x81) { fprintf(stderr, "SELECT response: first length byte is bad\n"); return(-1); } if (sim_resp_data_len < 3) goto tooshort; len = sim_resp_data[2]; offset = 3; goto return_check; } parse_and_display_select_response() { unsigned offset, totlen, reclen, n; u_char *dp, *endp; int rc; rc = select_resp_header_check(&offset, &totlen); if (rc < 0) return(rc); dp = sim_resp_data + offset; endp = sim_resp_data + offset + totlen; while (dp < endp) { if (endp - dp < 2) { trunc_error: fprintf(stderr, "error: truncated TLV record in SELECT response\n"); return(-1); } if ((dp[0] & 0x1F) == 0x1F) { fprintf(stderr, "error: extended tag not supported in SELECT response\n"); return(-1); } if (dp[1] & 0x80) { fprintf(stderr, "error: extended length not supported in SELECT response\n"); return(-1); } reclen = dp[1] + 2; if (endp - dp < reclen) goto trunc_error; for (n = 0; n < reclen; n++) { if (n) putchar(' '); printf("%02X", *dp++); } putchar('\n'); } return(0); } cmd_select(argc, argv) char **argv; { int file_id, rc; if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) && isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4]) file_id = strtoul(argv[1], 0, 16); else file_id = find_symbolic_file_name(argv[1]); if (file_id < 0) { fprintf(stderr, "error: file ID argument is not a hex value or a recognized symbolic name\n"); return(-1); } rc = select_op(file_id); if (rc < 0) return(rc); return parse_and_display_select_response(); }