annotate uicc/select.c @ 151:d515cfbb3f39

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