blob: 5fbb03f25ceb88c4bb17ac13c377521f880a656d [file] [log] [blame]
Harald Welted54c2ee2012-01-17 18:25:50 +01001#include <stdio.h>
2#include <stdlib.h>
3#include <errno.h>
4#include <string.h>
5
6#include <osmocom/core/msgb.h>
7#include <osmocom/core/talloc.h>
8#include <osmocom/sim/sim.h>
9#include <osmocom/gsm/tlv.h>
10
11
12
13
14
15
16
17/* 11.1.1 */
18static struct msgb *_select_file(struct osim_chan_hdl *st, uint8_t p1, uint8_t p2,
19 const uint8_t *data, uint8_t data_len)
20{
21 struct msgb *msg, *resp;
22 char *dst;
23
24 msg = osim_new_apdumsg(0x00, 0xA4, p1, p2, data_len, 256);
25 dst = msgb_put(msg, data_len);
26 memcpy(dst, data, data_len);
27
28 osim_transceive_apdu(st, msg);
29
30 return msg;
31}
32
33/* 11.1.1 */
34static struct msgb *select_adf(struct osim_chan_hdl *st, const uint8_t *adf, uint8_t adf_len)
35{
36 int sw;
37
38 return _select_file(st, 0x04, 0x04, adf,adf_len);
39}
40
41/* 11.1.1 */
42static struct msgb *select_file(struct osim_chan_hdl *st, uint16_t fid)
43{
44 uint16_t cfid = htons(fid);
45
46 return _select_file(st, 0x00, 0x04, (uint8_t *)&cfid, 2);
47}
48
49/* 11.1.9 */
50static int verify_pin(struct osim_chan_hdl *st, uint8_t pin_nr, uint8_t *pin)
51{
52 struct msgb *msg;
53 char *pindst;
54 int sw;
55
56 if (strlen(pin) > 8)
57 return -EINVAL;
58
59 msg = osim_new_apdumsg(0x00, 0x20, 0x00, pin_nr, 8, 0);
60 pindst = msgb_put(msg, 8);
61 memset(pindst, 0xFF, 8);
62 strncpy(pindst, pin, strlen(pin));
63
64 return osim_transceive_apdu(st, msg);
65}
66
67/* 11.1.5 */
68static struct msgb *read_record_nr(struct osim_chan_hdl *st, uint8_t rec_nr, uint16_t rec_size)
69{
70 struct msgb *msg;
71
72 msg = osim_new_apdumsg(0x00, 0xB2, rec_nr, 0x04, 0, rec_size);
73
74 osim_transceive_apdu(st, msg);
75
76 return msg;
77}
78
79/* 11.1.6 */
80static struct msgb *update_record_nr(struct osim_chan_hdl *st, uint8_t rec_nr,
81 const uint8_t *data, uint16_t rec_size)
82{
83 struct msgb *msg;
84 uint8_t *cur;
85
86 msg = osim_new_apdumsg(0x00, 0xDC, rec_nr, 0x04, rec_size, 0);
87 cur = msgb_put(msg, rec_size);
88 memcpy(cur, data, rec_size);
89
90 osim_transceive_apdu(st, msg);
91
92 return msg;
93}
94
95/* 11.1.3 */
96static struct msgb *read_binary(struct osim_chan_hdl *st, uint16_t offset, uint16_t len)
97{
98 struct msgb *msg;
99
100 if (offset > 0x7fff || len > 256)
101 return NULL;
102
103 msg = osim_new_apdumsg(0x00, 0xB0, offset >> 8, offset & 0xff, 0, len & 0xff);
104
105 osim_transceive_apdu(st, msg);
106
107 return msg;
108}
109
110/* 11.1.4 */
111static struct msgb *update_binary(struct osim_chan_hdl *st, uint16_t offset,
112 const uint8_t *data, uint16_t len)
113{
114 struct msgb *msg;
115 uint8_t *cur;
116
117 if (offset > 0x7fff || len > 256)
118 return NULL;
119
120 msg = osim_new_apdumsg(0x00, 0xD6, offset >> 8, offset & 0xff, len & 0xff, 0);
121 cur = msgb_put(msg, len);
122 memcpy(cur, data, len);
123
124 osim_transceive_apdu(st, msg);
125
126 return msg;
127}
128
129static int dump_fcp_template(struct tlv_parsed *tp)
130{
131 int i;
132
133 for (i = 0; i < ARRAY_SIZE(tp->lv); i++) {
134 if (TLVP_PRESENT(tp, i))
135 printf("Tag 0x%02x (%s): %s\n", i,
136 get_value_string(ts102221_fcp_vals, i),
137 osmo_hexdump(TLVP_VAL(tp, i), TLVP_LEN(tp, i)));
138 }
139
140 return 0;
141}
142
143static int dump_fcp_template_msg(struct msgb *msg)
144{
145 struct tlv_parsed tp;
146 int rc;
147
148 rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-4, 0, 0);
149 if (rc < 0)
150 return rc;
151
152 return dump_fcp_template(&tp);
153}
154
155struct osim_fcp_fd_decoded {
156 enum osim_file_type type;
157 enum osim_ef_type ef_type;
158 uint16_t rec_len;
159 uint8_t num_rec;
160};
161
162static const enum osim_file_type iso2ftype[8] = {
163 [0] = TYPE_EF,
164 [1] = TYPE_EF_INT,
165 [7] = TYPE_DF,
166};
167
168static const enum osim_ef_type iso2eftype[8] = {
169 [1] = EF_TYPE_TRANSP,
170 [2] = EF_TYPE_RECORD_FIXED,
171 [6] = EF_TYPE_RECORD_CYCLIC,
172};
173
174static int osim_fcp_fd_decode(struct osim_fcp_fd_decoded *ofd, const uint8_t *fcp, int fcp_len)
175{
176 memset(ofd, 0, sizeof(*ofd));
177
178 if (fcp_len != 2 && fcp_len != 5)
179 return -EINVAL;
180
181 ofd->type = iso2ftype[(fcp[0] >> 3) & 7];
182 if (ofd->type != TYPE_DF)
183 ofd->ef_type = iso2eftype[fcp[0] & 7];
184
185 if (fcp[1] != 0x21)
186 return -EINVAL;
187
188 if (fcp_len >= 5) {
189 ofd->rec_len = ntohs(*(uint16_t *)(fcp+2));
190 ofd->num_rec = fcp[4];
191 }
192
193 return 0;
194}
195
196extern struct osim_card_profile *osim_cprof_usim(void *ctx);
197
198static struct msgb *try_select_adf_usim(struct osim_chan_hdl *st)
199{
200 struct tlv_parsed tp;
201 struct osim_fcp_fd_decoded ofd;
202 struct msgb *msg, *msg2;
203 uint8_t *cur;
204 int rc, i;
205
206 msg = select_file(st, 0x2f00);
207 rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-4, 0, 0);
208 if (rc < 0)
209 return NULL;
210
211 dump_fcp_template(&tp);
212
213 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
214 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 5) {
215 msgb_free(msg);
216 return NULL;
217 }
218
219 rc = osim_fcp_fd_decode(&ofd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
220 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
221 if (rc < 0) {
222 msgb_free(msg);
223 return NULL;
224 }
225
226 if (ofd.type != TYPE_EF || ofd.ef_type != EF_TYPE_RECORD_FIXED) {
227 msgb_free(msg);
228 return NULL;
229 }
230
231 msgb_free(msg);
232
233 printf("ofd rec_len = %u, num_rec = %u\n", ofd.rec_len, ofd.num_rec);
234
235 for (i = 0; i < ofd.num_rec; i++) {
236 msg = read_record_nr(st, i+1, ofd.rec_len);
237 if (!msg)
238 return NULL;
239
240 cur = msgb_apdu_de(msg);
241 if (msgb_apdu_le(msg) < 5) {
242 msgb_free(msg);
243 return NULL;
244 }
245
246 if (cur[0] != 0x61 || cur[1] < 0x03 || cur[1] > 0x7f ||
247 cur[2] != 0x4F || cur[3] < 0x01 || cur[3] > 0x10) {
248 msgb_free(msg);
249 return NULL;
250 }
251
252 /* FIXME: actually check if it is an AID that we support, or
253 * iterate until we find one that we support */
254
255 msg2 = select_adf(st, cur+4, cur[3]);
256
257 /* attach the USIM profile, FIXME: do this based on AID match */
258 st->card->prof = osim_cprof_usim(st->card);
259 st->cwd = osim_file_find_name(st->card->prof->mf, "ADF.USIM");
260
261 msgb_free(msg);
262
263 return msg2;
264 }
265
266 return NULL;
267}
268
269static int dump_file(struct osim_chan_hdl *chan, uint16_t fid)
270{
271 struct tlv_parsed tp;
272 struct osim_fcp_fd_decoded ffdd;
273 struct msgb *msg;
274 int rc, i;
275
276 msg = select_file(chan, fid);
277 if (!msg)
278 return -EIO;
279 if (msgb_apdu_sw(msg) != 0x9000)
280 goto out;
281
282 rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-4, 0, 0);
283 if (rc < 0)
284 goto out;
285
286 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
287 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 5)
288 goto out;
289
290 rc = osim_fcp_fd_decode(&ffdd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
291 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
292 if (rc < 0)
293 goto out;
294
295 if (ffdd.type != TYPE_EF)
296 goto out;
297
298 switch (ffdd.ef_type) {
299 case EF_TYPE_RECORD_FIXED:
300 for (i = 0; i < ffdd.num_rec; i++) {
301 struct msgb *rmsg = read_record_nr(chan, i+1, ffdd.rec_len);
302 if (!msg)
303 return NULL;
304 printf("Rec %03u: %s\n", i+1,
305 osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg)));
306 }
307 break;
308 case EF_TYPE_TRANSP:
309 break;
310 default:
311 goto out;
312 }
313
314out:
315 msgb_free(msg);
316 return -EINVAL;
317}
318
319int main(int argc, char **argv)
320{
321 struct osim_reader_hdl *reader;
322 struct osim_card_hdl *card;
323 struct osim_chan_hdl *chan;
324 struct msgb *msg;
325 int rc;
326
327 reader = osim_reader_open(0, NULL);
328 if (!reader)
329 exit(1);
330 card = osim_card_open(reader);
331 if (!card)
332 exit(2);
333 chan = llist_entry(card->channels.next, struct osim_chan_hdl, list);
334 if (!chan)
335 exit(3);
336
337 msg = try_select_adf_usim(chan);
338 if (!msg || msgb_apdu_sw(msg) != 0x9000)
339 exit(4);
340 dump_fcp_template_msg(msg);
341 msgb_free(msg);
342
343 msg = select_file(chan, 0x6fc5);
344 dump_fcp_template_msg(msg);
345 msgb_free(msg);
346
347 verify_pin(chan, 1, "1653");
348
349 msg = select_file(chan, 0x6f06);
350 dump_fcp_template_msg(msg);
351 msgb_free(msg);
352
353#if 1
354 {
355 struct osim_file_desc *ofd;
356 llist_for_each_entry(ofd, &chan->cwd->child_list, list) {
357 struct msgb *m;
358 printf("\n\n================ %s (%s) ==================\n",
359 ofd->short_name, ofd->long_name);
360
361 m = select_file(chan, ofd->fid);
362 dump_fcp_template_msg(m);
363 msgb_free(m);
364 dump_file(chan, ofd->fid);
365 }
366 }
367#endif
368
369 exit(0);
370}