annotate uicc/select.c @ 64:8cd4771bdd79

doc/User-oriented-commands: document started
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 14 Feb 2021 19:01:04 +0000
parents 1b1468869ccf
children 58406ead2497
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
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
167 parse_and_display_select_response()
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
168 {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
169 unsigned offset, totlen, reclen, n;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
170 u_char *dp, *endp;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
171 int rc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
172
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
173 rc = select_resp_header_check(&offset, &totlen);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
174 if (rc < 0)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
175 return(rc);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
176 dp = sim_resp_data + offset;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
177 endp = sim_resp_data + offset + totlen;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
178 while (dp < endp) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
179 if (endp - dp < 2) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
180 trunc_error: fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
181 "error: truncated TLV record in SELECT response\n");
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
182 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
183 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
184 if ((dp[0] & 0x1F) == 0x1F) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
185 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
186 "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
187 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
188 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
189 if (dp[1] & 0x80) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
190 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
191 "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
192 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
193 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
194 reclen = dp[1] + 2;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
195 if (endp - dp < reclen)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
196 goto trunc_error;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
197 if (dp[0] == 0x82)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
198 check_for_record_struct(dp);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
199 for (n = 0; n < reclen; n++) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
200 if (n)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
201 putchar(' ');
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
202 printf("%02X", *dp++);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
203 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
204 putchar('\n');
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
205 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
206 return(0);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
207 }
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 cmd_select(argc, argv)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
210 char **argv;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
211 {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
212 int file_id, rc;
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 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
215 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
216 file_id = strtoul(argv[1], 0, 16);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
217 else
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
218 file_id = find_symbolic_file_name(argv[1]);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
219 if (file_id < 0) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
220 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
221 "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
222 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
223 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
224 rc = select_op(file_id);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
225 if (rc < 0)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
226 return(rc);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
227 return parse_and_display_select_response();
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
228 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
229
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
230 cmd_select_aid(argc, argv)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
231 char **argv;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
232 {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
233 u_char aid[16];
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
234 unsigned aid_len;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
235 int rc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
236
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
237 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
238 if (rc < 0)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
239 return(rc);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
240 aid_len = rc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
241 rc = select_aid_op(aid, aid_len);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
242 if (rc < 0)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
243 return(rc);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
244 return parse_and_display_select_response();
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
245 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
246
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
247 cmd_select_usim()
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 int rc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
250
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
251 rc = select_aid_op(std_aid_usim, 7);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
252 if (rc < 0)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
253 return(rc);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
254 return parse_and_display_select_response();
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
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
257 cmd_select_isim()
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
258 {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
259 int rc;
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 rc = select_aid_op(std_aid_isim, 7);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
262 if (rc < 0)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
263 return(rc);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
264 return parse_and_display_select_response();
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
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
267 u_char *
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
268 extract_select_resp_tag(sought_tag)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
269 unsigned sought_tag;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
270 {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
271 unsigned offset, totlen, reclen;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
272 u_char *dp, *endp;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
273 int rc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
274
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
275 rc = select_resp_header_check(&offset, &totlen);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
276 if (rc < 0)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
277 return(0);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
278 dp = sim_resp_data + offset;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
279 endp = sim_resp_data + offset + totlen;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
280 while (dp < endp) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
281 if (endp - dp < 2) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
282 trunc_error: fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
283 "error: truncated TLV record in SELECT response\n");
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 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
286 if ((dp[0] & 0x1F) == 0x1F) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
287 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
288 "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
289 return(0);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
290 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
291 if (dp[1] & 0x80) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
292 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
293 "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
294 return(0);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
295 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
296 reclen = dp[1] + 2;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
297 if (endp - dp < reclen)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
298 goto trunc_error;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
299 if (dp[0] == sought_tag)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
300 return(dp);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
301 dp += reclen;
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 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
304 sought_tag);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
305 return(0);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
306 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
307
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
308 select_resp_get_transparent(lenp)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
309 unsigned *lenp;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
310 {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
311 u_char *tlv;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
312
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
313 tlv = extract_select_resp_tag(0x82);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
314 if (!tlv)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
315 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
316 if (tlv[1] != 2) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
317 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
318 return(-1);
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 if (tlv[2] & 0x80)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
321 goto bad_file_desc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
322 if ((tlv[2] & 0x38) == 0x38)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
323 goto bad_file_desc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
324 if ((tlv[2] & 0x07) != 0x01)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
325 goto bad_file_desc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
326 tlv = extract_select_resp_tag(0x80);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
327 if (!tlv)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
328 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
329 if (tlv[1] != 2) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
330 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
331 "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
332 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
333 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
334 if (lenp)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
335 *lenp = (tlv[2] << 8) | tlv[3];
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
336 return(0);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
337 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
338
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
339 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
340 unsigned *rec_len_ret, *rec_count_ret;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
341 {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
342 u_char *tlv;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
343 unsigned reclen;
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 tlv = extract_select_resp_tag(0x82);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
346 if (!tlv)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
347 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
348 if (tlv[1] != 5) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
349 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
350 return(-1);
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 if (tlv[2] & 0x80)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
353 goto bad_file_desc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
354 if ((tlv[2] & 0x38) == 0x38)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
355 goto bad_file_desc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
356 if ((tlv[2] & 0x07) != 0x02)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
357 goto bad_file_desc;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
358 reclen = (tlv[4] << 8) | tlv[5];
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
359 if (reclen < 1 || reclen > 255) {
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
360 fprintf(stderr,
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
361 "error: SELECT response gives invalid record length\n");
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
362 return(-1);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
363 }
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
364 if (rec_len_ret)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
365 *rec_len_ret = reclen;
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
366 if (rec_count_ret)
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
367 *rec_count_ret = tlv[6];
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
368 return(0);
1b1468869ccf new trimmed fc-uicc-tool is here
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
369 }