blob: 4cd8b05e7a0267ae60905bd86d6c126dc1b57247 [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;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200273 struct msgb *msg, *rmsg;
274 int rc, i, offset;
Harald Welted54c2ee2012-01-17 18:25:50 +0100275
276 msg = select_file(chan, fid);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200277 if (!msg) {
278 printf("Unable to select file\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100279 return -EIO;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200280 }
281 if (msgb_apdu_sw(msg) != 0x9000) {
282 printf("status 0x%04x selecting file\n", msgb_apdu_sw(msg));
Harald Welted54c2ee2012-01-17 18:25:50 +0100283 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200284 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100285
286 rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-4, 0, 0);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200287 if (rc < 0) {
288 printf("Unable to parse FCP\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100289 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200290 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100291
292 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
Harald Weltea0ba4d92012-09-10 10:43:15 +0200293 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 2) {
294 printf("No file descriptor present ?!?\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100295 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200296 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100297
298 rc = osim_fcp_fd_decode(&ffdd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
299 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
Harald Weltea0ba4d92012-09-10 10:43:15 +0200300 if (rc < 0) {
301 printf("Unable to decode File Descriptor\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100302 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200303 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100304
Harald Weltea0ba4d92012-09-10 10:43:15 +0200305 if (ffdd.type != TYPE_EF) {
306 printf("File Type != EF\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100307 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200308 }
309
310 printf("EF type: %u\n", ffdd.ef_type);
Harald Welted54c2ee2012-01-17 18:25:50 +0100311
312 switch (ffdd.ef_type) {
313 case EF_TYPE_RECORD_FIXED:
314 for (i = 0; i < ffdd.num_rec; i++) {
Harald Weltea0ba4d92012-09-10 10:43:15 +0200315 rmsg = read_record_nr(chan, i+1, ffdd.rec_len);
Harald Welted54c2ee2012-01-17 18:25:50 +0100316 if (!msg)
Harald Weltea0ba4d92012-09-10 10:43:15 +0200317 return -EIO;
Harald Welted54c2ee2012-01-17 18:25:50 +0100318 printf("Rec %03u: %s\n", i+1,
319 osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg)));
320 }
321 break;
322 case EF_TYPE_TRANSP:
Harald Weltea0ba4d92012-09-10 10:43:15 +0200323 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_SIZE))
324 goto out;
325 i = ntohs(*(uint16_t *)TLVP_VAL(&tp, UICC_FCP_T_FILE_SIZE));
326 printf("File size: %d bytes\n", i);
327
328 for (offset = 0; offset < i-1; ) {
329 uint16_t remain_len = i - offset;
330 uint16_t read_len = OSMO_MIN(remain_len, 256);
331 rmsg = read_binary(chan, offset, read_len);
332 if (!msg)
333 return -EIO;
334 offset += read_len;
335 printf("Content: %s\n",
336 osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg)));
337 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100338 break;
339 default:
340 goto out;
341 }
342
343out:
344 msgb_free(msg);
345 return -EINVAL;
346}
347
348int main(int argc, char **argv)
349{
350 struct osim_reader_hdl *reader;
351 struct osim_card_hdl *card;
352 struct osim_chan_hdl *chan;
353 struct msgb *msg;
354 int rc;
355
356 reader = osim_reader_open(0, NULL);
357 if (!reader)
358 exit(1);
359 card = osim_card_open(reader);
360 if (!card)
361 exit(2);
362 chan = llist_entry(card->channels.next, struct osim_chan_hdl, list);
363 if (!chan)
364 exit(3);
365
366 msg = try_select_adf_usim(chan);
367 if (!msg || msgb_apdu_sw(msg) != 0x9000)
368 exit(4);
369 dump_fcp_template_msg(msg);
370 msgb_free(msg);
371
372 msg = select_file(chan, 0x6fc5);
373 dump_fcp_template_msg(msg);
374 msgb_free(msg);
375
376 verify_pin(chan, 1, "1653");
377
378 msg = select_file(chan, 0x6f06);
379 dump_fcp_template_msg(msg);
380 msgb_free(msg);
381
382#if 1
383 {
384 struct osim_file_desc *ofd;
385 llist_for_each_entry(ofd, &chan->cwd->child_list, list) {
386 struct msgb *m;
387 printf("\n\n================ %s (%s) ==================\n",
388 ofd->short_name, ofd->long_name);
389
390 m = select_file(chan, ofd->fid);
391 dump_fcp_template_msg(m);
392 msgb_free(m);
393 dump_file(chan, ofd->fid);
394 }
395 }
396#endif
397
398 exit(0);
399}