annotate simtool/select.c @ 212:43b57865fdb5

fc-simtool spn command: fix ancient bug
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 07 Mar 2021 06:46:21 +0000
parents e7e0d8c06654
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 #include <sys/types.h>
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 #include <ctype.h>
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 #include <string.h>
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 #include <strings.h>
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <stdio.h>
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdlib.h>
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include "simresp.h"
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include "curfile.h"
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9
143
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
10 elem_select_op(file_id)
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
11 unsigned file_id;
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
12 {
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
13 u_char cmd[7];
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
14 int rc;
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
15 unsigned expect_resp_len;
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
16
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
17 /* SELECT command APDU */
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
18 cmd[0] = 0xA0;
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
19 cmd[1] = 0xA4;
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
20 cmd[2] = 0;
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
21 cmd[3] = 0;
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
22 cmd[4] = 2;
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
23 cmd[5] = file_id >> 8;
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
24 cmd[6] = file_id;
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
25 rc = apdu_exchange(cmd, 7);
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
26 if (rc < 0)
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
27 return(rc);
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
28 if (sim_resp_sw == 0x9404)
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
29 return(0);
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
30 if ((sim_resp_sw & 0xFF00) == 0x9F00)
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
31 return(1);
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
32 fprintf(stderr,
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
33 "error or unexpected SW response to SELECT of 0x%04X: %04X\n",
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
34 file_id, sim_resp_sw);
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
35 return(-1);
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
36 }
be1a759453ef simtool code: added elem_select_op() function
Mychaela Falconia <falcon@freecalypso.org>
parents: 79
diff changeset
37
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 select_op(file_id)
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 unsigned file_id;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 u_char cmd[7];
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 int rc;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 unsigned expect_resp_len;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 /* SELECT command APDU */
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 cmd[0] = 0xA0;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 cmd[1] = 0xA4;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 cmd[2] = 0;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 cmd[3] = 0;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 cmd[4] = 2;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 cmd[5] = file_id >> 8;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 cmd[6] = file_id;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 rc = apdu_exchange(cmd, 7);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 if (rc < 0)
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 return(rc);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 if ((sim_resp_sw & 0xFF00) != 0x9F00) {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 fprintf(stderr,
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 "error or unexpected SW response to SELECT of 0x%04X: %04X\n",
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 file_id, sim_resp_sw);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 return(-1);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 expect_resp_len = sim_resp_sw & 0xFF;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 /* GET RESPONSE follow-up */
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 cmd[1] = 0xC0;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 cmd[4] = expect_resp_len;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 rc = apdu_exchange(cmd, 5);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 if (rc < 0)
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 return(rc);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 if (sim_resp_sw != 0x9000) {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 fprintf(stderr,
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 "bad SW resp to GET RESPONSE after SELECT: %04X\n",
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 sim_resp_sw);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 return(-1);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 if (sim_resp_data_len != expect_resp_len) {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 fprintf(stderr,
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 "error: GET RESPONSE after SELECT returned %u bytes, expected %u\n",
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 sim_resp_data_len, expect_resp_len);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 return(-1);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 return(0);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 static void
172
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
85 show_secret_code_status(outf, name, byte)
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
86 FILE *outf;
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 char *name;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 unsigned byte;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 {
172
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
90 fprintf(outf, "Status of %s: %s, %u attempts left\n", name,
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 byte & 0x80 ? "initialized" : "not initialized",
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 byte & 0x0F);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 void
172
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
96 show_access_conditions(outf, oper_name, cond_code)
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
97 FILE *outf;
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 char *oper_name;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 unsigned cond_code;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 static char *cond_names[16] =
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 {"ALW", "CHV1", "CHV2", "RFU3",
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 "ADM4", "ADM5", "ADM6", "ADM7",
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 "ADM8", "ADM9", "ADM10", "ADM11",
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 "ADM12", "ADM13", "ADM14", "NEV"};
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106
172
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
107 fprintf(outf, "Access condition for %s: %s\n", oper_name,
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 cond_names[cond_code]);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110
172
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
111 cmd_select(argc, argv, outf)
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 char **argv;
172
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
113 FILE *outf;
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 int file_id, rc;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) &&
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4])
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 file_id = strtoul(argv[1], 0, 16);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 else
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 file_id = find_symbolic_file_name(argv[1]);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 if (file_id < 0) {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 fprintf(stderr,
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 "error: file ID argument is not a hex value or a recognized symbolic name\n");
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 return(-1);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 rc = select_op(file_id);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 if (rc < 0)
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 return(rc);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 if (sim_resp_data_len < 14) {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 fprintf(stderr,
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 "error: response length of %u bytes is too short for any file type\n",
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 sim_resp_data_len);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 return(-1);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 switch (sim_resp_data[6]) {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 case 0x01:
172
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
138 fprintf(outf, "File type: MF\n");
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 goto mf_or_df;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 case 0x02:
172
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
141 fprintf(outf, "File type: DF\n");
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 mf_or_df:
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 if (sim_resp_data_len < 22) {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 fprintf(stderr,
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 "error: response length of %u bytes is too short for MF/DF\n",
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146 sim_resp_data_len);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147 return(-1);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 }
172
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
149 fprintf(outf, "File characteristics: %02X\n",
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
150 sim_resp_data[13]);
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
151 fprintf(outf, "Number of DF children: %u\n", sim_resp_data[14]);
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
152 fprintf(outf, "Number of EF children: %u\n", sim_resp_data[15]);
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
153 fprintf(outf, "Number of secret codes: %u\n",
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
154 sim_resp_data[16]);
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
155 show_secret_code_status(outf, "PIN1", sim_resp_data[18]);
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
156 show_secret_code_status(outf, "PUK1", sim_resp_data[19]);
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
157 show_secret_code_status(outf, "PIN2", sim_resp_data[20]);
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
158 show_secret_code_status(outf, "PUK2", sim_resp_data[21]);
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
159 break;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
160 case 0x04:
172
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
161 fprintf(outf, "File type: EF\n");
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
162 curfile_total_size = (sim_resp_data[2] << 8) | sim_resp_data[3];
172
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
163 fprintf(outf, "File size: %u\n", curfile_total_size);
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
164 curfile_structure = sim_resp_data[13];
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
165 switch (curfile_structure) {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
166 case 0x00:
172
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
167 fprintf(outf, "Structure: transparent\n");
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
168 break;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
169 case 0x01:
172
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
170 fprintf(outf, "Structure: linear fixed\n");
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
171 goto ef_record_based;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
172 case 0x03:
172
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
173 fprintf(outf, "Structure: cyclic\n");
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
174 ef_record_based:
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
175 if (sim_resp_data_len < 15) {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
176 fprintf(stderr,
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
177 "error: response length of %u bytes is too short for record-based EF\n",
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
178 sim_resp_data_len);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
179 return(-1);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
180 }
172
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
181 fprintf(outf, "Record length: %u\n", sim_resp_data[14]);
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
182 curfile_record_len = sim_resp_data[14];
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
183 if (curfile_record_len &&
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
184 curfile_total_size % curfile_record_len == 0) {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
185 curfile_record_count =
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
186 curfile_total_size / curfile_record_len;
172
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
187 fprintf(outf, "Number of records: %u\n",
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
188 curfile_record_count);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
189 } else
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
190 curfile_record_count = 0;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
191 break;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
192 default:
172
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
193 fprintf(outf, "Structure: %02X (unknown)\n",
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
194 curfile_structure);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
195 }
172
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
196 fprintf(outf, "File status: %02X\n", sim_resp_data[11]);
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
197 show_access_conditions(outf, "UPDATE", sim_resp_data[8] & 0xF);
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
198 show_access_conditions(outf, "READ & SEEK",
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
199 sim_resp_data[8] >> 4);
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
200 show_access_conditions(outf, "INCREASE", sim_resp_data[9] >> 4);
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
201 show_access_conditions(outf, "INVALIDATE",
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
202 sim_resp_data[10] & 0xF);
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
203 show_access_conditions(outf, "REHABILITATE",
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
204 sim_resp_data[10] >> 4);
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
205 break;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
206 default:
172
e7e0d8c06654 fc-simtool: support output redirection in select command
Mychaela Falconia <falcon@freecalypso.org>
parents: 143
diff changeset
207 fprintf(outf, "File type: %02X (unknown)\n", sim_resp_data[6]);
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
208 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
209 return(0);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
210 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
211
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
212 parse_ef_select_response()
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
213 {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
214 if (sim_resp_data_len < 14) {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
215 fprintf(stderr,
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
216 "error: SELECT response length of %u bytes is too short\n",
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
217 sim_resp_data_len);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
218 return(-1);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
219 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
220 if (sim_resp_data[6] != 0x04) {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
221 fprintf(stderr, "error: selected file is not an EF\n");
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
222 return(-1);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
223 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
224 curfile_total_size = (sim_resp_data[2] << 8) | sim_resp_data[3];
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
225 curfile_structure = sim_resp_data[13];
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
226 switch (curfile_structure) {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
227 case 0x00:
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
228 /* transparent */
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
229 break;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
230 case 0x01:
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
231 case 0x03:
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
232 /* record-based */
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
233 if (sim_resp_data_len < 15) {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
234 fprintf(stderr,
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
235 "error: SELECT response length of %u bytes is too short for record-based EF\n",
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
236 sim_resp_data_len);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
237 return(-1);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
238 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
239 curfile_record_len = sim_resp_data[14];
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
240 if (!curfile_record_len) {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
241 fprintf(stderr,
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
242 "error: SELECT response indicates record length of 0\n");
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
243 return(-1);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
244 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
245 if (curfile_total_size % curfile_record_len) {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
246 fprintf(stderr,
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
247 "error: returned file size is not divisible by record length\n");
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
248 return(-1);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
249 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
250 curfile_record_count = curfile_total_size / curfile_record_len;
79
b0982c0cf54d EF select logic: detect invalid record count > 255
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
251 if (curfile_record_count > 255) {
b0982c0cf54d EF select logic: detect invalid record count > 255
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
252 fprintf(stderr,
b0982c0cf54d EF select logic: detect invalid record count > 255
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
253 "error: EF record count exceeds protocol limit\n");
b0982c0cf54d EF select logic: detect invalid record count > 255
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
254 return(-1);
b0982c0cf54d EF select logic: detect invalid record count > 255
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
255 }
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
256 break;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
257 default:
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
258 fprintf(stderr, "error: unknown EF structure code %02X\n",
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
259 curfile_structure);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
260 return(-1);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
261 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
262 return(0);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
263 }