blob: 5588294a7f4d5eeb88689366077980c5814243ba [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);
Neels Hofmeyr95fdbc12018-07-26 17:14:40 +020077 /* Do not copy the terminating \0 */
78 memcpy(pindst, pin, strlen(pin));
Harald Welted54c2ee2012-01-17 18:25:50 +010079
80 return osim_transceive_apdu(st, msg);
81}
82
83/* 11.1.5 */
84static struct msgb *read_record_nr(struct osim_chan_hdl *st, uint8_t rec_nr, uint16_t rec_size)
85{
86 struct msgb *msg;
87
88 msg = osim_new_apdumsg(0x00, 0xB2, rec_nr, 0x04, 0, rec_size);
89
90 osim_transceive_apdu(st, msg);
91
92 return msg;
93}
94
Harald Welted54c2ee2012-01-17 18:25:50 +010095/* 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
Harald Welted54c2ee2012-01-17 18:25:50 +0100110static int dump_fcp_template(struct tlv_parsed *tp)
111{
112 int i;
113
114 for (i = 0; i < ARRAY_SIZE(tp->lv); i++) {
115 if (TLVP_PRESENT(tp, i))
116 printf("Tag 0x%02x (%s): %s\n", i,
117 get_value_string(ts102221_fcp_vals, i),
118 osmo_hexdump(TLVP_VAL(tp, i), TLVP_LEN(tp, i)));
119 }
120
121 return 0;
122}
123
124static int dump_fcp_template_msg(struct msgb *msg)
125{
126 struct tlv_parsed tp;
127 int rc;
128
Harald Weltea5c92552012-09-10 21:05:42 +0200129 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 +0100130 if (rc < 0)
131 return rc;
132
133 return dump_fcp_template(&tp);
134}
135
136struct osim_fcp_fd_decoded {
137 enum osim_file_type type;
138 enum osim_ef_type ef_type;
139 uint16_t rec_len;
140 uint8_t num_rec;
141};
142
143static const enum osim_file_type iso2ftype[8] = {
144 [0] = TYPE_EF,
145 [1] = TYPE_EF_INT,
146 [7] = TYPE_DF,
147};
148
149static const enum osim_ef_type iso2eftype[8] = {
150 [1] = EF_TYPE_TRANSP,
151 [2] = EF_TYPE_RECORD_FIXED,
152 [6] = EF_TYPE_RECORD_CYCLIC,
153};
154
155static int osim_fcp_fd_decode(struct osim_fcp_fd_decoded *ofd, const uint8_t *fcp, int fcp_len)
156{
157 memset(ofd, 0, sizeof(*ofd));
158
159 if (fcp_len != 2 && fcp_len != 5)
160 return -EINVAL;
161
162 ofd->type = iso2ftype[(fcp[0] >> 3) & 7];
163 if (ofd->type != TYPE_DF)
164 ofd->ef_type = iso2eftype[fcp[0] & 7];
165
166 if (fcp[1] != 0x21)
167 return -EINVAL;
168
169 if (fcp_len >= 5) {
170 ofd->rec_len = ntohs(*(uint16_t *)(fcp+2));
171 ofd->num_rec = fcp[4];
172 }
173
174 return 0;
175}
176
177extern struct osim_card_profile *osim_cprof_usim(void *ctx);
178
179static struct msgb *try_select_adf_usim(struct osim_chan_hdl *st)
180{
181 struct tlv_parsed tp;
182 struct osim_fcp_fd_decoded ofd;
183 struct msgb *msg, *msg2;
184 uint8_t *cur;
185 int rc, i;
186
187 msg = select_file(st, 0x2f00);
Harald Weltea5c92552012-09-10 21:05:42 +0200188 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 +0100189 if (rc < 0)
190 return NULL;
191
192 dump_fcp_template(&tp);
193
194 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
195 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 5) {
196 msgb_free(msg);
197 return NULL;
198 }
199
200 rc = osim_fcp_fd_decode(&ofd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
201 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
202 if (rc < 0) {
203 msgb_free(msg);
204 return NULL;
205 }
206
207 if (ofd.type != TYPE_EF || ofd.ef_type != EF_TYPE_RECORD_FIXED) {
208 msgb_free(msg);
209 return NULL;
210 }
211
212 msgb_free(msg);
213
214 printf("ofd rec_len = %u, num_rec = %u\n", ofd.rec_len, ofd.num_rec);
215
216 for (i = 0; i < ofd.num_rec; i++) {
217 msg = read_record_nr(st, i+1, ofd.rec_len);
218 if (!msg)
219 return NULL;
220
221 cur = msgb_apdu_de(msg);
222 if (msgb_apdu_le(msg) < 5) {
223 msgb_free(msg);
224 return NULL;
225 }
226
227 if (cur[0] != 0x61 || cur[1] < 0x03 || cur[1] > 0x7f ||
228 cur[2] != 0x4F || cur[3] < 0x01 || cur[3] > 0x10) {
229 msgb_free(msg);
230 return NULL;
231 }
232
233 /* FIXME: actually check if it is an AID that we support, or
234 * iterate until we find one that we support */
235
236 msg2 = select_adf(st, cur+4, cur[3]);
237
238 /* attach the USIM profile, FIXME: do this based on AID match */
239 st->card->prof = osim_cprof_usim(st->card);
Harald Welte5ffb5032016-03-11 09:40:56 +0700240 st->cwd = osim_file_desc_find_name(st->card->prof->mf, "ADF.USIM");
Harald Welted54c2ee2012-01-17 18:25:50 +0100241
242 msgb_free(msg);
243
244 return msg2;
245 }
246
247 return NULL;
248}
249
250static int dump_file(struct osim_chan_hdl *chan, uint16_t fid)
251{
252 struct tlv_parsed tp;
253 struct osim_fcp_fd_decoded ffdd;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200254 struct msgb *msg, *rmsg;
255 int rc, i, offset;
Harald Welted54c2ee2012-01-17 18:25:50 +0100256
257 msg = select_file(chan, fid);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200258 if (!msg) {
259 printf("Unable to select file\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100260 return -EIO;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200261 }
Harald Welte76749602012-09-19 20:55:54 +0200262 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Weltea0ba4d92012-09-10 10:43:15 +0200263 if (msgb_apdu_sw(msg) != 0x9000) {
264 printf("status 0x%04x selecting file\n", msgb_apdu_sw(msg));
Harald Welted54c2ee2012-01-17 18:25:50 +0100265 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200266 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100267
Harald Weltea5c92552012-09-10 21:05:42 +0200268 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 +0200269 if (rc < 0) {
270 printf("Unable to parse FCP\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100271 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200272 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100273
274 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
Harald Weltea0ba4d92012-09-10 10:43:15 +0200275 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 2) {
276 printf("No file descriptor present ?!?\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100277 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200278 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100279
280 rc = osim_fcp_fd_decode(&ffdd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
281 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
Harald Weltea0ba4d92012-09-10 10:43:15 +0200282 if (rc < 0) {
283 printf("Unable to decode File Descriptor\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100284 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200285 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100286
Harald Weltea0ba4d92012-09-10 10:43:15 +0200287 if (ffdd.type != TYPE_EF) {
288 printf("File Type != EF\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100289 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200290 }
291
292 printf("EF type: %u\n", ffdd.ef_type);
Harald Welted54c2ee2012-01-17 18:25:50 +0100293
294 switch (ffdd.ef_type) {
295 case EF_TYPE_RECORD_FIXED:
296 for (i = 0; i < ffdd.num_rec; i++) {
Harald Weltea0ba4d92012-09-10 10:43:15 +0200297 rmsg = read_record_nr(chan, i+1, ffdd.rec_len);
Harald Welte95336312016-11-26 09:54:40 +0100298 if (!rmsg)
Harald Weltea0ba4d92012-09-10 10:43:15 +0200299 return -EIO;
Harald Welte76749602012-09-19 20:55:54 +0200300 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Welted54c2ee2012-01-17 18:25:50 +0100301 printf("Rec %03u: %s\n", i+1,
302 osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg)));
303 }
304 break;
305 case EF_TYPE_TRANSP:
Harald Weltea0ba4d92012-09-10 10:43:15 +0200306 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_SIZE))
307 goto out;
308 i = ntohs(*(uint16_t *)TLVP_VAL(&tp, UICC_FCP_T_FILE_SIZE));
309 printf("File size: %d bytes\n", i);
310
311 for (offset = 0; offset < i-1; ) {
312 uint16_t remain_len = i - offset;
313 uint16_t read_len = OSMO_MIN(remain_len, 256);
314 rmsg = read_binary(chan, offset, read_len);
Harald Welted6ec9842014-10-27 20:43:06 +0100315 if (!rmsg)
Harald Weltea0ba4d92012-09-10 10:43:15 +0200316 return -EIO;
317 offset += read_len;
318 printf("Content: %s\n",
319 osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg)));
320 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100321 break;
322 default:
323 goto out;
324 }
325
326out:
327 msgb_free(msg);
328 return -EINVAL;
329}
330
331int main(int argc, char **argv)
332{
333 struct osim_reader_hdl *reader;
334 struct osim_card_hdl *card;
335 struct osim_chan_hdl *chan;
336 struct msgb *msg;
Harald Welted54c2ee2012-01-17 18:25:50 +0100337
Harald Welte55790aa2014-10-26 18:46:50 +0100338 reader = osim_reader_open(OSIM_READER_DRV_PCSC, 0, "", NULL);
Harald Welted54c2ee2012-01-17 18:25:50 +0100339 if (!reader)
340 exit(1);
Harald Welte55790aa2014-10-26 18:46:50 +0100341 card = osim_card_open(reader, OSIM_PROTO_T0);
Harald Welted54c2ee2012-01-17 18:25:50 +0100342 if (!card)
343 exit(2);
344 chan = llist_entry(card->channels.next, struct osim_chan_hdl, list);
345 if (!chan)
346 exit(3);
347
348 msg = try_select_adf_usim(chan);
349 if (!msg || msgb_apdu_sw(msg) != 0x9000)
350 exit(4);
351 dump_fcp_template_msg(msg);
352 msgb_free(msg);
353
354 msg = select_file(chan, 0x6fc5);
355 dump_fcp_template_msg(msg);
Harald Welte76749602012-09-19 20:55:54 +0200356 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Welted54c2ee2012-01-17 18:25:50 +0100357 msgb_free(msg);
358
359 verify_pin(chan, 1, "1653");
360
361 msg = select_file(chan, 0x6f06);
362 dump_fcp_template_msg(msg);
363 msgb_free(msg);
364
Harald Welted54c2ee2012-01-17 18:25:50 +0100365 {
366 struct osim_file_desc *ofd;
367 llist_for_each_entry(ofd, &chan->cwd->child_list, list) {
368 struct msgb *m;
369 printf("\n\n================ %s (%s) ==================\n",
370 ofd->short_name, ofd->long_name);
371
372 m = select_file(chan, ofd->fid);
373 dump_fcp_template_msg(m);
374 msgb_free(m);
375 dump_file(chan, ofd->fid);
376 }
377 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100378
379 exit(0);
380}