annotate uicc/select.c @ 138:58406ead2497

fc-uicc-tool: support output redirection for select commands
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 23 Feb 2021 02:34:46 +0000
parents 1b1468869ccf
children 77832c9f2001
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 #include <sys/types.h>
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 #include <ctype.h>
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 #include <string.h>
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 #include <strings.h>
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <stdio.h>
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdlib.h>
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include "simresp.h"
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 u_char std_aid_usim[7] = {0xA0, 0x00, 0x00, 0x00, 0x87, 0x10, 0x02};
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 u_char std_aid_isim[7] = {0xA0, 0x00, 0x00, 0x00, 0x87, 0x10, 0x04};
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 unsigned last_sel_file_record_len;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 select_op(file_id)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 unsigned file_id;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 u_char cmd[7];
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 int rc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 unsigned expect_resp_len;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 last_sel_file_record_len = 0;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 /* SELECT command APDU */
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 cmd[0] = 0x00;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 cmd[1] = 0xA4;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 cmd[2] = 0x00;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 cmd[3] = 0x04;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 cmd[4] = 2;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 cmd[5] = file_id >> 8;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 cmd[6] = file_id;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 rc = apdu_exchange(cmd, 7);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 if (rc < 0)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 return(rc);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 if ((sim_resp_sw & 0xFF00) != 0x6100) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 "error or unexpected SW response to SELECT of 0x%04X: %04X\n",
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 file_id, sim_resp_sw);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 expect_resp_len = sim_resp_sw & 0xFF;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 /* GET RESPONSE follow-up */
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 cmd[1] = 0xC0;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 cmd[2] = 0;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 cmd[3] = 0;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 cmd[4] = expect_resp_len;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 rc = apdu_exchange(cmd, 5);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 if (rc < 0)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 return(rc);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 if (sim_resp_sw != 0x9000) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 "bad SW resp to GET RESPONSE after SELECT: %04X\n",
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 sim_resp_sw);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 if (sim_resp_data_len != expect_resp_len) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 "error: GET RESPONSE after SELECT returned %u bytes, expected %u\n",
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 sim_resp_data_len, expect_resp_len);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 return(0);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 select_aid_op(aid, aid_len)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 u_char *aid;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 unsigned aid_len;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 u_char cmd[21];
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 int rc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 unsigned expect_resp_len;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 last_sel_file_record_len = 0;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 /* SELECT command APDU */
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 cmd[0] = 0x00;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 cmd[1] = 0xA4;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 cmd[2] = 0x04;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 cmd[3] = 0x04;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 cmd[4] = aid_len;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 bcopy(aid, cmd + 5, aid_len);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 rc = apdu_exchange(cmd, aid_len + 5);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 if (rc < 0)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 return(rc);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 if ((sim_resp_sw & 0xFF00) != 0x6100) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 "error or unexpected SW response to SELECT by AID: %04X\n",
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 sim_resp_sw);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 expect_resp_len = sim_resp_sw & 0xFF;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 /* GET RESPONSE follow-up */
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 cmd[1] = 0xC0;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 cmd[2] = 0;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 cmd[3] = 0;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 cmd[4] = expect_resp_len;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 rc = apdu_exchange(cmd, 5);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 if (rc < 0)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 return(rc);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 if (sim_resp_sw != 0x9000) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 "bad SW resp to GET RESPONSE after SELECT: %04X\n",
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 sim_resp_sw);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 if (sim_resp_data_len != expect_resp_len) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 "error: GET RESPONSE after SELECT returned %u bytes, expected %u\n",
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 sim_resp_data_len, expect_resp_len);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 return(0);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 select_resp_header_check(ret_offset, ret_length)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 unsigned *ret_offset, *ret_length;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 unsigned offset, len;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 if (sim_resp_data_len < 2) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 tooshort: fprintf(stderr, "error: SELECT response is too short\n");
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 if (sim_resp_data[0] != 0x62) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 fprintf(stderr, "error: SELECT response first byte != 0x62\n");
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 len = sim_resp_data[1];
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 if (len <= 0x7F) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 offset = 2;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 return_check: if (offset + len > sim_resp_data_len)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 goto tooshort;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 if (ret_offset)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 *ret_offset = offset;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 if (ret_length)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 *ret_length = len;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 return(0);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 if (len != 0x81) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 fprintf(stderr, "SELECT response: first length byte is bad\n");
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 if (sim_resp_data_len < 3)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141 goto tooshort;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 len = sim_resp_data[2];
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 offset = 3;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 goto return_check;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147 static void
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 check_for_record_struct(tlv)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
149 u_char *tlv;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
150 {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
151 unsigned reclen;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
152
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
153 if (tlv[1] != 5)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
154 return;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
155 if (tlv[2] & 0x80)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
156 return;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
157 if ((tlv[2] & 0x38) == 0x38)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
158 return;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
159 if ((tlv[2] & 0x03) != 0x02)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
160 return;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
161 reclen = (tlv[4] << 8) | tlv[5];
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
162 if (reclen < 1 || reclen > 255)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
163 return;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
164 last_sel_file_record_len = reclen;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
165 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
166
138
58406ead2497 fc-uicc-tool: support output redirection for select commands
Mychaela Falconia <falcon@freecalypso.org>
parents: 22
diff changeset
167 parse_and_display_select_response(outf)
58406ead2497 fc-uicc-tool: support output redirection for select commands
Mychaela Falconia <falcon@freecalypso.org>
parents: 22
diff changeset
168 FILE *outf;
22
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
169 {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
170 unsigned offset, totlen, reclen, n;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
171 u_char *dp, *endp;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
172 int rc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
173
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
174 rc = select_resp_header_check(&offset, &totlen);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
175 if (rc < 0)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
176 return(rc);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
177 dp = sim_resp_data + offset;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
178 endp = sim_resp_data + offset + totlen;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
179 while (dp < endp) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
180 if (endp - dp < 2) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
181 trunc_error: fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
182 "error: truncated TLV record in SELECT response\n");
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
183 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
184 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
185 if ((dp[0] & 0x1F) == 0x1F) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
186 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
187 "error: extended tag not supported in SELECT response\n");
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
188 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
189 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
190 if (dp[1] & 0x80) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
191 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
192 "error: extended length not supported in SELECT response\n");
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
193 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
194 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
195 reclen = dp[1] + 2;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
196 if (endp - dp < reclen)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
197 goto trunc_error;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
198 if (dp[0] == 0x82)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
199 check_for_record_struct(dp);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
200 for (n = 0; n < reclen; n++) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
201 if (n)
138
58406ead2497 fc-uicc-tool: support output redirection for select commands
Mychaela Falconia <falcon@freecalypso.org>
parents: 22
diff changeset
202 putc(' ', outf);
58406ead2497 fc-uicc-tool: support output redirection for select commands
Mychaela Falconia <falcon@freecalypso.org>
parents: 22
diff changeset
203 fprintf(outf, "%02X", *dp++);
22
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
204 }
138
58406ead2497 fc-uicc-tool: support output redirection for select commands
Mychaela Falconia <falcon@freecalypso.org>
parents: 22
diff changeset
205 putc('\n', outf);
22
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
206 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
207 return(0);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
208 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
209
138
58406ead2497 fc-uicc-tool: support output redirection for select commands
Mychaela Falconia <falcon@freecalypso.org>
parents: 22
diff changeset
210 cmd_select(argc, argv, outf)
22
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
211 char **argv;
138
58406ead2497 fc-uicc-tool: support output redirection for select commands
Mychaela Falconia <falcon@freecalypso.org>
parents: 22
diff changeset
212 FILE *outf;
22
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
213 {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
214 int file_id, rc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
215
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
216 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) &&
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
217 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4])
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
218 file_id = strtoul(argv[1], 0, 16);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
219 else
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
220 file_id = find_symbolic_file_name(argv[1]);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
221 if (file_id < 0) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
222 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
223 "error: file ID argument is not a hex value or a recognized symbolic name\n");
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
224 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
225 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
226 rc = select_op(file_id);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
227 if (rc < 0)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
228 return(rc);
138
58406ead2497 fc-uicc-tool: support output redirection for select commands
Mychaela Falconia <falcon@freecalypso.org>
parents: 22
diff changeset
229 return parse_and_display_select_response(outf);
22
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
230 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
231
138
58406ead2497 fc-uicc-tool: support output redirection for select commands
Mychaela Falconia <falcon@freecalypso.org>
parents: 22
diff changeset
232 cmd_select_aid(argc, argv, outf)
22
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
233 char **argv;
138
58406ead2497 fc-uicc-tool: support output redirection for select commands
Mychaela Falconia <falcon@freecalypso.org>
parents: 22
diff changeset
234 FILE *outf;
22
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
235 {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
236 u_char aid[16];
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
237 unsigned aid_len;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
238 int rc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
239
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
240 rc = decode_hex_data_from_string(argv[1], aid, 1, 16);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
241 if (rc < 0)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
242 return(rc);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
243 aid_len = rc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
244 rc = select_aid_op(aid, aid_len);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
245 if (rc < 0)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
246 return(rc);
138
58406ead2497 fc-uicc-tool: support output redirection for select commands
Mychaela Falconia <falcon@freecalypso.org>
parents: 22
diff changeset
247 return parse_and_display_select_response(outf);
22
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
248 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
249
138
58406ead2497 fc-uicc-tool: support output redirection for select commands
Mychaela Falconia <falcon@freecalypso.org>
parents: 22
diff changeset
250 cmd_select_usim(argc, argv, outf)
58406ead2497 fc-uicc-tool: support output redirection for select commands
Mychaela Falconia <falcon@freecalypso.org>
parents: 22
diff changeset
251 char **argv;
58406ead2497 fc-uicc-tool: support output redirection for select commands
Mychaela Falconia <falcon@freecalypso.org>
parents: 22
diff changeset
252 FILE *outf;
22
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
253 {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
254 int rc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
255
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
256 rc = select_aid_op(std_aid_usim, 7);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
257 if (rc < 0)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
258 return(rc);
138
58406ead2497 fc-uicc-tool: support output redirection for select commands
Mychaela Falconia <falcon@freecalypso.org>
parents: 22
diff changeset
259 return parse_and_display_select_response(outf);
22
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
260 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
261
138
58406ead2497 fc-uicc-tool: support output redirection for select commands
Mychaela Falconia <falcon@freecalypso.org>
parents: 22
diff changeset
262 cmd_select_isim(argc, argv, outf)
58406ead2497 fc-uicc-tool: support output redirection for select commands
Mychaela Falconia <falcon@freecalypso.org>
parents: 22
diff changeset
263 char **argv;
58406ead2497 fc-uicc-tool: support output redirection for select commands
Mychaela Falconia <falcon@freecalypso.org>
parents: 22
diff changeset
264 FILE *outf;
22
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
265 {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
266 int rc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
267
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
268 rc = select_aid_op(std_aid_isim, 7);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
269 if (rc < 0)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
270 return(rc);
138
58406ead2497 fc-uicc-tool: support output redirection for select commands
Mychaela Falconia <falcon@freecalypso.org>
parents: 22
diff changeset
271 return parse_and_display_select_response(outf);
22
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
272 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
273
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
274 u_char *
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
275 extract_select_resp_tag(sought_tag)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
276 unsigned sought_tag;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
277 {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
278 unsigned offset, totlen, reclen;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
279 u_char *dp, *endp;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
280 int rc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
281
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
282 rc = select_resp_header_check(&offset, &totlen);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
283 if (rc < 0)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
284 return(0);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
285 dp = sim_resp_data + offset;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
286 endp = sim_resp_data + offset + totlen;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
287 while (dp < endp) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
288 if (endp - dp < 2) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
289 trunc_error: fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
290 "error: truncated TLV record in SELECT response\n");
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
291 return(0);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
292 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
293 if ((dp[0] & 0x1F) == 0x1F) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
294 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
295 "error: extended tag not supported in SELECT response\n");
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
296 return(0);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
297 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
298 if (dp[1] & 0x80) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
299 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
300 "error: extended length not supported in SELECT response\n");
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
301 return(0);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
302 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
303 reclen = dp[1] + 2;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
304 if (endp - dp < reclen)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
305 goto trunc_error;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
306 if (dp[0] == sought_tag)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
307 return(dp);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
308 dp += reclen;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
309 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
310 fprintf(stderr, "error: tag 0x%02X not found in SELECT response\n",
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
311 sought_tag);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
312 return(0);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
313 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
314
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
315 select_resp_get_transparent(lenp)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
316 unsigned *lenp;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
317 {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
318 u_char *tlv;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
319
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
320 tlv = extract_select_resp_tag(0x82);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
321 if (!tlv)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
322 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
323 if (tlv[1] != 2) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
324 bad_file_desc: fprintf(stderr, "error: file type is not transparent EF\n");
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
325 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
326 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
327 if (tlv[2] & 0x80)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
328 goto bad_file_desc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
329 if ((tlv[2] & 0x38) == 0x38)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
330 goto bad_file_desc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
331 if ((tlv[2] & 0x07) != 0x01)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
332 goto bad_file_desc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
333 tlv = extract_select_resp_tag(0x80);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
334 if (!tlv)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
335 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
336 if (tlv[1] != 2) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
337 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
338 "error: file size TLV element has wrong length\n");
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
339 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
340 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
341 if (lenp)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
342 *lenp = (tlv[2] << 8) | tlv[3];
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
343 return(0);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
344 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
345
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
346 select_resp_get_linear_fixed(rec_len_ret, rec_count_ret)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
347 unsigned *rec_len_ret, *rec_count_ret;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
348 {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
349 u_char *tlv;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
350 unsigned reclen;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
351
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
352 tlv = extract_select_resp_tag(0x82);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
353 if (!tlv)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
354 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
355 if (tlv[1] != 5) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
356 bad_file_desc: fprintf(stderr, "error: file type is not linear fixed EF\n");
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
357 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
358 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
359 if (tlv[2] & 0x80)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
360 goto bad_file_desc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
361 if ((tlv[2] & 0x38) == 0x38)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
362 goto bad_file_desc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
363 if ((tlv[2] & 0x07) != 0x02)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
364 goto bad_file_desc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
365 reclen = (tlv[4] << 8) | tlv[5];
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
366 if (reclen < 1 || reclen > 255) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
367 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
368 "error: SELECT response gives invalid record length\n");
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
369 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
370 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
371 if (rec_len_ret)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
372 *rec_len_ret = reclen;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
373 if (rec_count_ret)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
374 *rec_count_ret = tlv[6];
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
375 return(0);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
376 }