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