blob: d5e932eb55dff0612af16105dc2f30aefc21b89b [file] [log] [blame]
Harald Weltead418632012-09-10 10:49:59 +02001/* libosmosim test application - currently simply dumps a USIM */
2/* (C) 2012 by Harald Welte <laforge@gnumonks.org>
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
Harald Welted54c2ee2012-01-17 18:25:50 +010021#include <stdio.h>
22#include <stdlib.h>
23#include <errno.h>
24#include <string.h>
Alexander Huemeraab4a242015-11-06 20:55:24 +010025#include <arpa/inet.h>
Harald Welted54c2ee2012-01-17 18:25:50 +010026
27#include <osmocom/core/msgb.h>
28#include <osmocom/core/talloc.h>
29#include <osmocom/sim/sim.h>
30#include <osmocom/gsm/tlv.h>
31
32
Harald Weltead418632012-09-10 10:49:59 +020033/* FIXME: this needs to be moved to card_fs_uicc.c */
Harald Welted54c2ee2012-01-17 18:25:50 +010034
35/* 11.1.1 */
36static struct msgb *_select_file(struct osim_chan_hdl *st, uint8_t p1, uint8_t p2,
37 const uint8_t *data, uint8_t data_len)
38{
Vadim Yanitskiy1cd99912017-05-15 21:37:16 +030039 struct msgb *msg;
Harald Weltef12d40f2017-02-08 15:46:53 +000040 uint8_t *dst;
Harald Welted54c2ee2012-01-17 18:25:50 +010041
42 msg = osim_new_apdumsg(0x00, 0xA4, p1, p2, data_len, 256);
43 dst = msgb_put(msg, data_len);
44 memcpy(dst, data, data_len);
45
46 osim_transceive_apdu(st, msg);
47
48 return msg;
49}
50
51/* 11.1.1 */
52static struct msgb *select_adf(struct osim_chan_hdl *st, const uint8_t *adf, uint8_t adf_len)
53{
Harald Welted54c2ee2012-01-17 18:25:50 +010054 return _select_file(st, 0x04, 0x04, adf,adf_len);
55}
56
57/* 11.1.1 */
58static struct msgb *select_file(struct osim_chan_hdl *st, uint16_t fid)
59{
60 uint16_t cfid = htons(fid);
61
62 return _select_file(st, 0x00, 0x04, (uint8_t *)&cfid, 2);
63}
64
65/* 11.1.9 */
Harald Weltef12d40f2017-02-08 15:46:53 +000066static int verify_pin(struct osim_chan_hdl *st, uint8_t pin_nr, char *pin)
Harald Welted54c2ee2012-01-17 18:25:50 +010067{
68 struct msgb *msg;
69 char *pindst;
Harald Welted54c2ee2012-01-17 18:25:50 +010070
71 if (strlen(pin) > 8)
72 return -EINVAL;
73
74 msg = osim_new_apdumsg(0x00, 0x20, 0x00, pin_nr, 8, 0);
Harald Weltef12d40f2017-02-08 15:46:53 +000075 pindst = (char *) msgb_put(msg, 8);
Harald Welted54c2ee2012-01-17 18:25:50 +010076 memset(pindst, 0xFF, 8);
77 strncpy(pindst, pin, strlen(pin));
78
79 return osim_transceive_apdu(st, msg);
80}
81
82/* 11.1.5 */
83static struct msgb *read_record_nr(struct osim_chan_hdl *st, uint8_t rec_nr, uint16_t rec_size)
84{
85 struct msgb *msg;
86
87 msg = osim_new_apdumsg(0x00, 0xB2, rec_nr, 0x04, 0, rec_size);
88
89 osim_transceive_apdu(st, msg);
90
91 return msg;
92}
93
94/* 11.1.6 */
95static struct msgb *update_record_nr(struct osim_chan_hdl *st, uint8_t rec_nr,
96 const uint8_t *data, uint16_t rec_size)
97{
98 struct msgb *msg;
99 uint8_t *cur;
100
101 msg = osim_new_apdumsg(0x00, 0xDC, rec_nr, 0x04, rec_size, 0);
102 cur = msgb_put(msg, rec_size);
103 memcpy(cur, data, rec_size);
104
105 osim_transceive_apdu(st, msg);
106
107 return msg;
108}
109
110/* 11.1.3 */
111static struct msgb *read_binary(struct osim_chan_hdl *st, uint16_t offset, uint16_t len)
112{
113 struct msgb *msg;
114
115 if (offset > 0x7fff || len > 256)
116 return NULL;
117
118 msg = osim_new_apdumsg(0x00, 0xB0, offset >> 8, offset & 0xff, 0, len & 0xff);
119
120 osim_transceive_apdu(st, msg);
121
122 return msg;
123}
124
125/* 11.1.4 */
126static struct msgb *update_binary(struct osim_chan_hdl *st, uint16_t offset,
127 const uint8_t *data, uint16_t len)
128{
129 struct msgb *msg;
130 uint8_t *cur;
131
132 if (offset > 0x7fff || len > 256)
133 return NULL;
134
135 msg = osim_new_apdumsg(0x00, 0xD6, offset >> 8, offset & 0xff, len & 0xff, 0);
136 cur = msgb_put(msg, len);
137 memcpy(cur, data, len);
138
139 osim_transceive_apdu(st, msg);
140
141 return msg;
142}
143
Harald Weltead418632012-09-10 10:49:59 +0200144
145
Harald Welted54c2ee2012-01-17 18:25:50 +0100146static int dump_fcp_template(struct tlv_parsed *tp)
147{
148 int i;
149
150 for (i = 0; i < ARRAY_SIZE(tp->lv); i++) {
151 if (TLVP_PRESENT(tp, i))
152 printf("Tag 0x%02x (%s): %s\n", i,
153 get_value_string(ts102221_fcp_vals, i),
154 osmo_hexdump(TLVP_VAL(tp, i), TLVP_LEN(tp, i)));
155 }
156
157 return 0;
158}
159
160static int dump_fcp_template_msg(struct msgb *msg)
161{
162 struct tlv_parsed tp;
163 int rc;
164
Harald Weltea5c92552012-09-10 21:05:42 +0200165 rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-2, 0, 0);
Harald Welted54c2ee2012-01-17 18:25:50 +0100166 if (rc < 0)
167 return rc;
168
169 return dump_fcp_template(&tp);
170}
171
172struct osim_fcp_fd_decoded {
173 enum osim_file_type type;
174 enum osim_ef_type ef_type;
175 uint16_t rec_len;
176 uint8_t num_rec;
177};
178
179static const enum osim_file_type iso2ftype[8] = {
180 [0] = TYPE_EF,
181 [1] = TYPE_EF_INT,
182 [7] = TYPE_DF,
183};
184
185static const enum osim_ef_type iso2eftype[8] = {
186 [1] = EF_TYPE_TRANSP,
187 [2] = EF_TYPE_RECORD_FIXED,
188 [6] = EF_TYPE_RECORD_CYCLIC,
189};
190
191static int osim_fcp_fd_decode(struct osim_fcp_fd_decoded *ofd, const uint8_t *fcp, int fcp_len)
192{
193 memset(ofd, 0, sizeof(*ofd));
194
195 if (fcp_len != 2 && fcp_len != 5)
196 return -EINVAL;
197
198 ofd->type = iso2ftype[(fcp[0] >> 3) & 7];
199 if (ofd->type != TYPE_DF)
200 ofd->ef_type = iso2eftype[fcp[0] & 7];
201
202 if (fcp[1] != 0x21)
203 return -EINVAL;
204
205 if (fcp_len >= 5) {
206 ofd->rec_len = ntohs(*(uint16_t *)(fcp+2));
207 ofd->num_rec = fcp[4];
208 }
209
210 return 0;
211}
212
213extern struct osim_card_profile *osim_cprof_usim(void *ctx);
214
215static struct msgb *try_select_adf_usim(struct osim_chan_hdl *st)
216{
217 struct tlv_parsed tp;
218 struct osim_fcp_fd_decoded ofd;
219 struct msgb *msg, *msg2;
220 uint8_t *cur;
221 int rc, i;
222
223 msg = select_file(st, 0x2f00);
Harald Weltea5c92552012-09-10 21:05:42 +0200224 rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-2, 0, 0);
Harald Welted54c2ee2012-01-17 18:25:50 +0100225 if (rc < 0)
226 return NULL;
227
228 dump_fcp_template(&tp);
229
230 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
231 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 5) {
232 msgb_free(msg);
233 return NULL;
234 }
235
236 rc = osim_fcp_fd_decode(&ofd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
237 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
238 if (rc < 0) {
239 msgb_free(msg);
240 return NULL;
241 }
242
243 if (ofd.type != TYPE_EF || ofd.ef_type != EF_TYPE_RECORD_FIXED) {
244 msgb_free(msg);
245 return NULL;
246 }
247
248 msgb_free(msg);
249
250 printf("ofd rec_len = %u, num_rec = %u\n", ofd.rec_len, ofd.num_rec);
251
252 for (i = 0; i < ofd.num_rec; i++) {
253 msg = read_record_nr(st, i+1, ofd.rec_len);
254 if (!msg)
255 return NULL;
256
257 cur = msgb_apdu_de(msg);
258 if (msgb_apdu_le(msg) < 5) {
259 msgb_free(msg);
260 return NULL;
261 }
262
263 if (cur[0] != 0x61 || cur[1] < 0x03 || cur[1] > 0x7f ||
264 cur[2] != 0x4F || cur[3] < 0x01 || cur[3] > 0x10) {
265 msgb_free(msg);
266 return NULL;
267 }
268
269 /* FIXME: actually check if it is an AID that we support, or
270 * iterate until we find one that we support */
271
272 msg2 = select_adf(st, cur+4, cur[3]);
273
274 /* attach the USIM profile, FIXME: do this based on AID match */
275 st->card->prof = osim_cprof_usim(st->card);
Harald Welte5ffb5032016-03-11 09:40:56 +0700276 st->cwd = osim_file_desc_find_name(st->card->prof->mf, "ADF.USIM");
Harald Welted54c2ee2012-01-17 18:25:50 +0100277
278 msgb_free(msg);
279
280 return msg2;
281 }
282
283 return NULL;
284}
285
286static int dump_file(struct osim_chan_hdl *chan, uint16_t fid)
287{
288 struct tlv_parsed tp;
289 struct osim_fcp_fd_decoded ffdd;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200290 struct msgb *msg, *rmsg;
291 int rc, i, offset;
Harald Welted54c2ee2012-01-17 18:25:50 +0100292
293 msg = select_file(chan, fid);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200294 if (!msg) {
295 printf("Unable to select file\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100296 return -EIO;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200297 }
Harald Welte76749602012-09-19 20:55:54 +0200298 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Weltea0ba4d92012-09-10 10:43:15 +0200299 if (msgb_apdu_sw(msg) != 0x9000) {
300 printf("status 0x%04x selecting file\n", msgb_apdu_sw(msg));
Harald Welted54c2ee2012-01-17 18:25:50 +0100301 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200302 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100303
Harald Weltea5c92552012-09-10 21:05:42 +0200304 rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-2, 0, 0);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200305 if (rc < 0) {
306 printf("Unable to parse FCP\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100307 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200308 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100309
310 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
Harald Weltea0ba4d92012-09-10 10:43:15 +0200311 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 2) {
312 printf("No file descriptor present ?!?\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100313 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200314 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100315
316 rc = osim_fcp_fd_decode(&ffdd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
317 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
Harald Weltea0ba4d92012-09-10 10:43:15 +0200318 if (rc < 0) {
319 printf("Unable to decode File Descriptor\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100320 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200321 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100322
Harald Weltea0ba4d92012-09-10 10:43:15 +0200323 if (ffdd.type != TYPE_EF) {
324 printf("File Type != EF\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100325 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200326 }
327
328 printf("EF type: %u\n", ffdd.ef_type);
Harald Welted54c2ee2012-01-17 18:25:50 +0100329
330 switch (ffdd.ef_type) {
331 case EF_TYPE_RECORD_FIXED:
332 for (i = 0; i < ffdd.num_rec; i++) {
Harald Weltea0ba4d92012-09-10 10:43:15 +0200333 rmsg = read_record_nr(chan, i+1, ffdd.rec_len);
Harald Welte95336312016-11-26 09:54:40 +0100334 if (!rmsg)
Harald Weltea0ba4d92012-09-10 10:43:15 +0200335 return -EIO;
Harald Welte76749602012-09-19 20:55:54 +0200336 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Welted54c2ee2012-01-17 18:25:50 +0100337 printf("Rec %03u: %s\n", i+1,
338 osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg)));
339 }
340 break;
341 case EF_TYPE_TRANSP:
Harald Weltea0ba4d92012-09-10 10:43:15 +0200342 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_SIZE))
343 goto out;
344 i = ntohs(*(uint16_t *)TLVP_VAL(&tp, UICC_FCP_T_FILE_SIZE));
345 printf("File size: %d bytes\n", i);
346
347 for (offset = 0; offset < i-1; ) {
348 uint16_t remain_len = i - offset;
349 uint16_t read_len = OSMO_MIN(remain_len, 256);
350 rmsg = read_binary(chan, offset, read_len);
Harald Welted6ec9842014-10-27 20:43:06 +0100351 if (!rmsg)
Harald Weltea0ba4d92012-09-10 10:43:15 +0200352 return -EIO;
353 offset += read_len;
354 printf("Content: %s\n",
355 osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg)));
356 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100357 break;
358 default:
359 goto out;
360 }
361
362out:
363 msgb_free(msg);
364 return -EINVAL;
365}
366
367int main(int argc, char **argv)
368{
369 struct osim_reader_hdl *reader;
370 struct osim_card_hdl *card;
371 struct osim_chan_hdl *chan;
372 struct msgb *msg;
Harald Welted54c2ee2012-01-17 18:25:50 +0100373
Harald Welte55790aa2014-10-26 18:46:50 +0100374 reader = osim_reader_open(OSIM_READER_DRV_PCSC, 0, "", NULL);
Harald Welted54c2ee2012-01-17 18:25:50 +0100375 if (!reader)
376 exit(1);
Harald Welte55790aa2014-10-26 18:46:50 +0100377 card = osim_card_open(reader, OSIM_PROTO_T0);
Harald Welted54c2ee2012-01-17 18:25:50 +0100378 if (!card)
379 exit(2);
380 chan = llist_entry(card->channels.next, struct osim_chan_hdl, list);
381 if (!chan)
382 exit(3);
383
384 msg = try_select_adf_usim(chan);
385 if (!msg || msgb_apdu_sw(msg) != 0x9000)
386 exit(4);
387 dump_fcp_template_msg(msg);
388 msgb_free(msg);
389
390 msg = select_file(chan, 0x6fc5);
391 dump_fcp_template_msg(msg);
Harald Welte76749602012-09-19 20:55:54 +0200392 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Welted54c2ee2012-01-17 18:25:50 +0100393 msgb_free(msg);
394
395 verify_pin(chan, 1, "1653");
396
397 msg = select_file(chan, 0x6f06);
398 dump_fcp_template_msg(msg);
399 msgb_free(msg);
400
Harald Welted54c2ee2012-01-17 18:25:50 +0100401 {
402 struct osim_file_desc *ofd;
403 llist_for_each_entry(ofd, &chan->cwd->child_list, list) {
404 struct msgb *m;
405 printf("\n\n================ %s (%s) ==================\n",
406 ofd->short_name, ofd->long_name);
407
408 m = select_file(chan, ofd->fid);
409 dump_fcp_template_msg(m);
410 msgb_free(m);
411 dump_file(chan, ofd->fid);
412 }
413 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100414
415 exit(0);
416}