blob: d822bec4cb2ceb337f4211fe8c10281d120db511 [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{
39 struct msgb *msg, *resp;
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{
54 int sw;
55
56 return _select_file(st, 0x04, 0x04, adf,adf_len);
57}
58
59/* 11.1.1 */
60static struct msgb *select_file(struct osim_chan_hdl *st, uint16_t fid)
61{
62 uint16_t cfid = htons(fid);
63
64 return _select_file(st, 0x00, 0x04, (uint8_t *)&cfid, 2);
65}
66
67/* 11.1.9 */
Harald Weltef12d40f2017-02-08 15:46:53 +000068static int verify_pin(struct osim_chan_hdl *st, uint8_t pin_nr, char *pin)
Harald Welted54c2ee2012-01-17 18:25:50 +010069{
70 struct msgb *msg;
71 char *pindst;
72 int sw;
73
74 if (strlen(pin) > 8)
75 return -EINVAL;
76
77 msg = osim_new_apdumsg(0x00, 0x20, 0x00, pin_nr, 8, 0);
Harald Weltef12d40f2017-02-08 15:46:53 +000078 pindst = (char *) msgb_put(msg, 8);
Harald Welted54c2ee2012-01-17 18:25:50 +010079 memset(pindst, 0xFF, 8);
80 strncpy(pindst, pin, strlen(pin));
81
82 return osim_transceive_apdu(st, msg);
83}
84
85/* 11.1.5 */
86static struct msgb *read_record_nr(struct osim_chan_hdl *st, uint8_t rec_nr, uint16_t rec_size)
87{
88 struct msgb *msg;
89
90 msg = osim_new_apdumsg(0x00, 0xB2, rec_nr, 0x04, 0, rec_size);
91
92 osim_transceive_apdu(st, msg);
93
94 return msg;
95}
96
97/* 11.1.6 */
98static struct msgb *update_record_nr(struct osim_chan_hdl *st, uint8_t rec_nr,
99 const uint8_t *data, uint16_t rec_size)
100{
101 struct msgb *msg;
102 uint8_t *cur;
103
104 msg = osim_new_apdumsg(0x00, 0xDC, rec_nr, 0x04, rec_size, 0);
105 cur = msgb_put(msg, rec_size);
106 memcpy(cur, data, rec_size);
107
108 osim_transceive_apdu(st, msg);
109
110 return msg;
111}
112
113/* 11.1.3 */
114static struct msgb *read_binary(struct osim_chan_hdl *st, uint16_t offset, uint16_t len)
115{
116 struct msgb *msg;
117
118 if (offset > 0x7fff || len > 256)
119 return NULL;
120
121 msg = osim_new_apdumsg(0x00, 0xB0, offset >> 8, offset & 0xff, 0, len & 0xff);
122
123 osim_transceive_apdu(st, msg);
124
125 return msg;
126}
127
128/* 11.1.4 */
129static struct msgb *update_binary(struct osim_chan_hdl *st, uint16_t offset,
130 const uint8_t *data, uint16_t len)
131{
132 struct msgb *msg;
133 uint8_t *cur;
134
135 if (offset > 0x7fff || len > 256)
136 return NULL;
137
138 msg = osim_new_apdumsg(0x00, 0xD6, offset >> 8, offset & 0xff, len & 0xff, 0);
139 cur = msgb_put(msg, len);
140 memcpy(cur, data, len);
141
142 osim_transceive_apdu(st, msg);
143
144 return msg;
145}
146
Harald Weltead418632012-09-10 10:49:59 +0200147
148
Harald Welted54c2ee2012-01-17 18:25:50 +0100149static int dump_fcp_template(struct tlv_parsed *tp)
150{
151 int i;
152
153 for (i = 0; i < ARRAY_SIZE(tp->lv); i++) {
154 if (TLVP_PRESENT(tp, i))
155 printf("Tag 0x%02x (%s): %s\n", i,
156 get_value_string(ts102221_fcp_vals, i),
157 osmo_hexdump(TLVP_VAL(tp, i), TLVP_LEN(tp, i)));
158 }
159
160 return 0;
161}
162
163static int dump_fcp_template_msg(struct msgb *msg)
164{
165 struct tlv_parsed tp;
166 int rc;
167
Harald Weltea5c92552012-09-10 21:05:42 +0200168 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 +0100169 if (rc < 0)
170 return rc;
171
172 return dump_fcp_template(&tp);
173}
174
175struct osim_fcp_fd_decoded {
176 enum osim_file_type type;
177 enum osim_ef_type ef_type;
178 uint16_t rec_len;
179 uint8_t num_rec;
180};
181
182static const enum osim_file_type iso2ftype[8] = {
183 [0] = TYPE_EF,
184 [1] = TYPE_EF_INT,
185 [7] = TYPE_DF,
186};
187
188static const enum osim_ef_type iso2eftype[8] = {
189 [1] = EF_TYPE_TRANSP,
190 [2] = EF_TYPE_RECORD_FIXED,
191 [6] = EF_TYPE_RECORD_CYCLIC,
192};
193
194static int osim_fcp_fd_decode(struct osim_fcp_fd_decoded *ofd, const uint8_t *fcp, int fcp_len)
195{
196 memset(ofd, 0, sizeof(*ofd));
197
198 if (fcp_len != 2 && fcp_len != 5)
199 return -EINVAL;
200
201 ofd->type = iso2ftype[(fcp[0] >> 3) & 7];
202 if (ofd->type != TYPE_DF)
203 ofd->ef_type = iso2eftype[fcp[0] & 7];
204
205 if (fcp[1] != 0x21)
206 return -EINVAL;
207
208 if (fcp_len >= 5) {
209 ofd->rec_len = ntohs(*(uint16_t *)(fcp+2));
210 ofd->num_rec = fcp[4];
211 }
212
213 return 0;
214}
215
216extern struct osim_card_profile *osim_cprof_usim(void *ctx);
217
218static struct msgb *try_select_adf_usim(struct osim_chan_hdl *st)
219{
220 struct tlv_parsed tp;
221 struct osim_fcp_fd_decoded ofd;
222 struct msgb *msg, *msg2;
223 uint8_t *cur;
224 int rc, i;
225
226 msg = select_file(st, 0x2f00);
Harald Weltea5c92552012-09-10 21:05:42 +0200227 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 +0100228 if (rc < 0)
229 return NULL;
230
231 dump_fcp_template(&tp);
232
233 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
234 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 5) {
235 msgb_free(msg);
236 return NULL;
237 }
238
239 rc = osim_fcp_fd_decode(&ofd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
240 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
241 if (rc < 0) {
242 msgb_free(msg);
243 return NULL;
244 }
245
246 if (ofd.type != TYPE_EF || ofd.ef_type != EF_TYPE_RECORD_FIXED) {
247 msgb_free(msg);
248 return NULL;
249 }
250
251 msgb_free(msg);
252
253 printf("ofd rec_len = %u, num_rec = %u\n", ofd.rec_len, ofd.num_rec);
254
255 for (i = 0; i < ofd.num_rec; i++) {
256 msg = read_record_nr(st, i+1, ofd.rec_len);
257 if (!msg)
258 return NULL;
259
260 cur = msgb_apdu_de(msg);
261 if (msgb_apdu_le(msg) < 5) {
262 msgb_free(msg);
263 return NULL;
264 }
265
266 if (cur[0] != 0x61 || cur[1] < 0x03 || cur[1] > 0x7f ||
267 cur[2] != 0x4F || cur[3] < 0x01 || cur[3] > 0x10) {
268 msgb_free(msg);
269 return NULL;
270 }
271
272 /* FIXME: actually check if it is an AID that we support, or
273 * iterate until we find one that we support */
274
275 msg2 = select_adf(st, cur+4, cur[3]);
276
277 /* attach the USIM profile, FIXME: do this based on AID match */
278 st->card->prof = osim_cprof_usim(st->card);
Harald Welte5ffb5032016-03-11 09:40:56 +0700279 st->cwd = osim_file_desc_find_name(st->card->prof->mf, "ADF.USIM");
Harald Welted54c2ee2012-01-17 18:25:50 +0100280
281 msgb_free(msg);
282
283 return msg2;
284 }
285
286 return NULL;
287}
288
289static int dump_file(struct osim_chan_hdl *chan, uint16_t fid)
290{
291 struct tlv_parsed tp;
292 struct osim_fcp_fd_decoded ffdd;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200293 struct msgb *msg, *rmsg;
294 int rc, i, offset;
Harald Welted54c2ee2012-01-17 18:25:50 +0100295
296 msg = select_file(chan, fid);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200297 if (!msg) {
298 printf("Unable to select file\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100299 return -EIO;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200300 }
Harald Welte76749602012-09-19 20:55:54 +0200301 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Weltea0ba4d92012-09-10 10:43:15 +0200302 if (msgb_apdu_sw(msg) != 0x9000) {
303 printf("status 0x%04x selecting file\n", msgb_apdu_sw(msg));
Harald Welted54c2ee2012-01-17 18:25:50 +0100304 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200305 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100306
Harald Weltea5c92552012-09-10 21:05:42 +0200307 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 +0200308 if (rc < 0) {
309 printf("Unable to parse FCP\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100310 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200311 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100312
313 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
Harald Weltea0ba4d92012-09-10 10:43:15 +0200314 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 2) {
315 printf("No file descriptor present ?!?\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100316 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200317 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100318
319 rc = osim_fcp_fd_decode(&ffdd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
320 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
Harald Weltea0ba4d92012-09-10 10:43:15 +0200321 if (rc < 0) {
322 printf("Unable to decode File Descriptor\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100323 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200324 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100325
Harald Weltea0ba4d92012-09-10 10:43:15 +0200326 if (ffdd.type != TYPE_EF) {
327 printf("File Type != EF\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100328 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200329 }
330
331 printf("EF type: %u\n", ffdd.ef_type);
Harald Welted54c2ee2012-01-17 18:25:50 +0100332
333 switch (ffdd.ef_type) {
334 case EF_TYPE_RECORD_FIXED:
335 for (i = 0; i < ffdd.num_rec; i++) {
Harald Weltea0ba4d92012-09-10 10:43:15 +0200336 rmsg = read_record_nr(chan, i+1, ffdd.rec_len);
Harald Welte95336312016-11-26 09:54:40 +0100337 if (!rmsg)
Harald Weltea0ba4d92012-09-10 10:43:15 +0200338 return -EIO;
Harald Welte76749602012-09-19 20:55:54 +0200339 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Welted54c2ee2012-01-17 18:25:50 +0100340 printf("Rec %03u: %s\n", i+1,
341 osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg)));
342 }
343 break;
344 case EF_TYPE_TRANSP:
Harald Weltea0ba4d92012-09-10 10:43:15 +0200345 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_SIZE))
346 goto out;
347 i = ntohs(*(uint16_t *)TLVP_VAL(&tp, UICC_FCP_T_FILE_SIZE));
348 printf("File size: %d bytes\n", i);
349
350 for (offset = 0; offset < i-1; ) {
351 uint16_t remain_len = i - offset;
352 uint16_t read_len = OSMO_MIN(remain_len, 256);
353 rmsg = read_binary(chan, offset, read_len);
Harald Welted6ec9842014-10-27 20:43:06 +0100354 if (!rmsg)
Harald Weltea0ba4d92012-09-10 10:43:15 +0200355 return -EIO;
356 offset += read_len;
357 printf("Content: %s\n",
358 osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg)));
359 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100360 break;
361 default:
362 goto out;
363 }
364
365out:
366 msgb_free(msg);
367 return -EINVAL;
368}
369
370int main(int argc, char **argv)
371{
372 struct osim_reader_hdl *reader;
373 struct osim_card_hdl *card;
374 struct osim_chan_hdl *chan;
375 struct msgb *msg;
376 int rc;
377
Harald Welte55790aa2014-10-26 18:46:50 +0100378 reader = osim_reader_open(OSIM_READER_DRV_PCSC, 0, "", NULL);
Harald Welted54c2ee2012-01-17 18:25:50 +0100379 if (!reader)
380 exit(1);
Harald Welte55790aa2014-10-26 18:46:50 +0100381 card = osim_card_open(reader, OSIM_PROTO_T0);
Harald Welted54c2ee2012-01-17 18:25:50 +0100382 if (!card)
383 exit(2);
384 chan = llist_entry(card->channels.next, struct osim_chan_hdl, list);
385 if (!chan)
386 exit(3);
387
388 msg = try_select_adf_usim(chan);
389 if (!msg || msgb_apdu_sw(msg) != 0x9000)
390 exit(4);
391 dump_fcp_template_msg(msg);
392 msgb_free(msg);
393
394 msg = select_file(chan, 0x6fc5);
395 dump_fcp_template_msg(msg);
Harald Welte76749602012-09-19 20:55:54 +0200396 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Welted54c2ee2012-01-17 18:25:50 +0100397 msgb_free(msg);
398
399 verify_pin(chan, 1, "1653");
400
401 msg = select_file(chan, 0x6f06);
402 dump_fcp_template_msg(msg);
403 msgb_free(msg);
404
Harald Welted54c2ee2012-01-17 18:25:50 +0100405 {
406 struct osim_file_desc *ofd;
407 llist_for_each_entry(ofd, &chan->cwd->child_list, list) {
408 struct msgb *m;
409 printf("\n\n================ %s (%s) ==================\n",
410 ofd->short_name, ofd->long_name);
411
412 m = select_file(chan, ofd->fid);
413 dump_fcp_template_msg(m);
414 msgb_free(m);
415 dump_file(chan, ofd->fid);
416 }
417 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100418
419 exit(0);
420}