comparison uicc/select.c @ 22:1b1468869ccf

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