annotate uicc/select.c @ 135:51d6aaa43a7b

fc-uicc-tool: telecom-sum command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 04 Feb 2021 02:55:31 +0000
parents 69628bcfec17
children baf5bd698764
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
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 select_op(file_id)
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 unsigned file_id;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 {
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 u_char cmd[7];
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 int rc;
131
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
16 unsigned expect_resp_len;
130
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 /* SELECT command APDU */
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 cmd[0] = 0x00;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 cmd[1] = 0xA4;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 cmd[2] = 0x00;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 cmd[3] = 0x04;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 cmd[4] = 2;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 cmd[5] = file_id >> 8;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 cmd[6] = file_id;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 rc = apdu_exchange(cmd, 7);
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 if (rc < 0)
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 return(rc);
131
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
29 if ((sim_resp_sw & 0xFF00) != 0x6100) {
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
30 fprintf(stderr,
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
31 "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
32 file_id, sim_resp_sw);
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
33 return(-1);
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
34 }
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
35 expect_resp_len = sim_resp_sw & 0xFF;
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
36 /* GET RESPONSE follow-up */
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
37 cmd[1] = 0xC0;
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
38 cmd[2] = 0;
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
39 cmd[3] = 0;
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
40 cmd[4] = expect_resp_len;
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
41 rc = apdu_exchange(cmd, 5);
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
42 if (rc < 0)
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
43 return(rc);
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
44 if (sim_resp_sw != 0x9000) {
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
45 fprintf(stderr,
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
46 "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
47 sim_resp_sw);
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
48 return(-1);
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
49 }
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
50 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
51 fprintf(stderr,
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
52 "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
53 sim_resp_data_len, expect_resp_len);
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
54 return(-1);
d0929cb42e1c fc-uicc-tool: GET RESPONSE after SELECT
Mychaela Falconia <falcon@freecalypso.org>
parents: 130
diff changeset
55 }
130
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 return(0);
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 }
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58
132
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
59 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
60 unsigned *ret_offset, *ret_length;
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
61 {
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
62 unsigned offset, len;
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
63
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
64 if (sim_resp_data_len < 2) {
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
65 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
66 return(-1);
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
67 }
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
68 if (sim_resp_data[0] != 0x62) {
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
69 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
70 return(-1);
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
71 }
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
72 len = sim_resp_data[1];
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
73 if (len <= 0x7F) {
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
74 offset = 2;
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
75 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
76 goto tooshort;
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
77 if (ret_offset)
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
78 *ret_offset = offset;
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
79 if (ret_length)
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
80 *ret_length = len;
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
81 return(0);
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
82 }
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
83 if (len != 0x81) {
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
84 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
85 return(-1);
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
86 }
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
87 if (sim_resp_data_len < 3)
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
88 goto tooshort;
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
89 len = sim_resp_data[2];
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
90 offset = 3;
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
91 goto return_check;
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
92 }
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
93
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
94 parse_and_display_select_response()
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
95 {
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
96 unsigned offset, totlen, reclen, n;
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
97 u_char *dp, *endp;
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
98 int rc;
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
99
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
100 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
101 if (rc < 0)
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
102 return(rc);
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
103 dp = sim_resp_data + offset;
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
104 endp = sim_resp_data + offset + totlen;
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
105 while (dp < endp) {
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
106 if (endp - dp < 2) {
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
107 trunc_error: fprintf(stderr,
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
108 "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
109 return(-1);
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
110 }
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
111 if ((dp[0] & 0x1F) == 0x1F) {
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
112 fprintf(stderr,
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
113 "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
114 return(-1);
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
115 }
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
116 if (dp[1] & 0x80) {
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
117 fprintf(stderr,
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
118 "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
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 reclen = dp[1] + 2;
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
122 if (endp - dp < reclen)
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
123 goto trunc_error;
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
124 for (n = 0; n < reclen; n++) {
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
125 if (n)
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
126 putchar(' ');
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
127 printf("%02X", *dp++);
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
128 }
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
129 putchar('\n');
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
130 }
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
131 return(0);
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
132 }
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
133
130
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 cmd_select(argc, argv)
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 char **argv;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 {
132
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
137 int file_id, rc;
130
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) &&
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 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
141 file_id = strtoul(argv[1], 0, 16);
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 else
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 file_id = find_symbolic_file_name(argv[1]);
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 if (file_id < 0) {
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 fprintf(stderr,
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146 "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
147 return(-1);
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 }
132
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
149 rc = select_op(file_id);
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
150 if (rc < 0)
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
151 return(rc);
5af88fa11b54 fc-uicc-tool: SELECT response parsing implemented (basic)
Mychaela Falconia <falcon@freecalypso.org>
parents: 131
diff changeset
152 return parse_and_display_select_response();
130
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
153 }
134
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
154
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
155 u_char *
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
156 extract_select_resp_tag(sought_tag)
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
157 unsigned sought_tag;
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
158 {
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
159 unsigned offset, totlen, reclen;
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
160 u_char *dp, *endp;
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
161 int rc;
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
162
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
163 rc = select_resp_header_check(&offset, &totlen);
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
164 if (rc < 0)
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
165 return(0);
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
166 dp = sim_resp_data + offset;
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
167 endp = sim_resp_data + offset + totlen;
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
168 while (dp < endp) {
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
169 if (endp - dp < 2) {
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
170 trunc_error: fprintf(stderr,
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
171 "error: truncated TLV record in SELECT response\n");
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
172 return(0);
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
173 }
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
174 if ((dp[0] & 0x1F) == 0x1F) {
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
175 fprintf(stderr,
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
176 "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
177 return(0);
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
178 }
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
179 if (dp[1] & 0x80) {
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
180 fprintf(stderr,
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
181 "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
182 return(0);
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
183 }
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
184 reclen = dp[1] + 2;
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
185 if (endp - dp < reclen)
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
186 goto trunc_error;
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
187 if (dp[0] == sought_tag)
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
188 return(dp);
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
189 dp += reclen;
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
190 }
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
191 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
192 sought_tag);
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
193 return(0);
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
194 }
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
195
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
196 select_resp_get_transparent(lenp)
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
197 unsigned *lenp;
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
198 {
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
199 u_char *tlv;
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
200
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
201 tlv = extract_select_resp_tag(0x82);
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
202 if (!tlv)
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
203 return(-1);
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
204 if (tlv[1] != 2) {
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
205 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
206 return(-1);
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
207 }
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
208 if (tlv[2] & 0x80)
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
209 goto bad_file_desc;
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
210 if ((tlv[2] & 0x38) == 0x38)
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
211 goto bad_file_desc;
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
212 if ((tlv[2] & 0x07) != 0x01)
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
213 goto bad_file_desc;
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
214 tlv = extract_select_resp_tag(0x80);
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
215 if (!tlv)
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
216 return(-1);
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
217 if (tlv[1] != 2) {
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
218 fprintf(stderr,
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
219 "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
220 return(-1);
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
221 }
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
222 if (lenp)
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
223 *lenp = (tlv[2] << 8) | tlv[3];
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
224 return(0);
69628bcfec17 fc-uicc-tool: iccid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 132
diff changeset
225 }
135
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
226
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
227 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
228 unsigned *rec_len_ret, *rec_count_ret;
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
229 {
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
230 u_char *tlv;
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
231 unsigned reclen;
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
232
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
233 tlv = extract_select_resp_tag(0x82);
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
234 if (!tlv)
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
235 return(-1);
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
236 if (tlv[1] != 5) {
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
237 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
238 return(-1);
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
239 }
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
240 if (tlv[2] & 0x80)
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
241 goto bad_file_desc;
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
242 if ((tlv[2] & 0x38) == 0x38)
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
243 goto bad_file_desc;
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
244 if ((tlv[2] & 0x07) != 0x02)
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
245 goto bad_file_desc;
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
246 reclen = (tlv[4] << 8) | tlv[5];
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
247 if (reclen < 1 || reclen > 255) {
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
248 fprintf(stderr,
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
249 "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
250 return(-1);
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
251 }
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
252 if (rec_len_ret)
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
253 *rec_len_ret = reclen;
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
254 if (rec_count_ret)
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
255 *rec_count_ret = tlv[6];
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
256 return(0);
51d6aaa43a7b fc-uicc-tool: telecom-sum command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 134
diff changeset
257 }