FreeCalypso > hg > freecalypso-hwlab
comparison simtool/select.c @ 85:b57cf64ece29
fc-simtool project started
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 24 Jan 2021 00:32:10 +0000 |
parents | |
children | 54c444eb084b |
comparison
equal
deleted
inserted
replaced
84:1d7d8615d628 | 85:b57cf64ece29 |
---|---|
1 #include <sys/types.h> | |
2 #include <ctype.h> | |
3 #include <string.h> | |
4 #include <strings.h> | |
5 #include <stdio.h> | |
6 #include <stdlib.h> | |
7 #include <pcsclite.h> | |
8 #include <winscard.h> | |
9 #include "globals.h" | |
10 | |
11 select_op(file_id) | |
12 unsigned file_id; | |
13 { | |
14 u_char cmd[7]; | |
15 int rc; | |
16 unsigned expect_resp_len; | |
17 | |
18 /* SELECT command APDU */ | |
19 cmd[0] = 0xA0; | |
20 cmd[1] = 0xA4; | |
21 cmd[2] = 0; | |
22 cmd[3] = 0; | |
23 cmd[4] = 2; | |
24 cmd[5] = file_id >> 8; | |
25 cmd[6] = file_id; | |
26 rc = apdu_exchange(cmd, 7); | |
27 if (rc < 0) | |
28 return(rc); | |
29 if ((sim_resp_sw & 0xFF00) != 0x9F00) { | |
30 fprintf(stderr, | |
31 "error or unexpected SW response to SELECT of 0x%04X: %04X\n", | |
32 file_id, sim_resp_sw); | |
33 return(-1); | |
34 } | |
35 expect_resp_len = sim_resp_sw & 0xFF; | |
36 /* GET RESPONSE follow-up */ | |
37 cmd[1] = 0xC0; | |
38 cmd[4] = expect_resp_len; | |
39 rc = apdu_exchange(cmd, 5); | |
40 if (rc < 0) | |
41 return(rc); | |
42 if (sim_resp_sw != 0x9000) { | |
43 fprintf(stderr, | |
44 "bad SW resp to GET RESPONSE after SELECT: %04X\n", | |
45 sim_resp_sw); | |
46 return(-1); | |
47 } | |
48 if (sim_resp_data_len != expect_resp_len) { | |
49 fprintf(stderr, | |
50 "error: GET RESPONSE after SELECT returned %u bytes, expected %u\n", | |
51 sim_resp_data_len, expect_resp_len); | |
52 return(-1); | |
53 } | |
54 return(0); | |
55 } | |
56 | |
57 cmd_select(argc, argv) | |
58 char **argv; | |
59 { | |
60 unsigned file_id; | |
61 | |
62 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) && | |
63 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4]) | |
64 file_id = strtoul(argv[1], 0, 16); | |
65 else { | |
66 fprintf(stderr, | |
67 "select: only hex file IDs are currently supported\n"); | |
68 return(-1); | |
69 } | |
70 return select_op(file_id); | |
71 } |