blob: ea241206d035d45b007ec33e826847a289207b6f [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
Harald Welted54c2ee2012-01-17 18:25:50 +010094/* 11.1.3 */
95static struct msgb *read_binary(struct osim_chan_hdl *st, uint16_t offset, uint16_t len)
96{
97 struct msgb *msg;
98
99 if (offset > 0x7fff || len > 256)
100 return NULL;
101
102 msg = osim_new_apdumsg(0x00, 0xB0, offset >> 8, offset & 0xff, 0, len & 0xff);
103
104 osim_transceive_apdu(st, msg);
105
106 return msg;
107}
108
Harald Welted54c2ee2012-01-17 18:25:50 +0100109static int dump_fcp_template(struct tlv_parsed *tp)
110{
111 int i;
112
113 for (i = 0; i < ARRAY_SIZE(tp->lv); i++) {
114 if (TLVP_PRESENT(tp, i))
115 printf("Tag 0x%02x (%s): %s\n", i,
116 get_value_string(ts102221_fcp_vals, i),
117 osmo_hexdump(TLVP_VAL(tp, i), TLVP_LEN(tp, i)));
118 }
119
120 return 0;
121}
122
123static int dump_fcp_template_msg(struct msgb *msg)
124{
125 struct tlv_parsed tp;
126 int rc;
127
Harald Weltea5c92552012-09-10 21:05:42 +0200128 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 +0100129 if (rc < 0)
130 return rc;
131
132 return dump_fcp_template(&tp);
133}
134
135struct osim_fcp_fd_decoded {
136 enum osim_file_type type;
137 enum osim_ef_type ef_type;
138 uint16_t rec_len;
139 uint8_t num_rec;
140};
141
142static const enum osim_file_type iso2ftype[8] = {
143 [0] = TYPE_EF,
144 [1] = TYPE_EF_INT,
145 [7] = TYPE_DF,
146};
147
148static const enum osim_ef_type iso2eftype[8] = {
149 [1] = EF_TYPE_TRANSP,
150 [2] = EF_TYPE_RECORD_FIXED,
151 [6] = EF_TYPE_RECORD_CYCLIC,
152};
153
154static int osim_fcp_fd_decode(struct osim_fcp_fd_decoded *ofd, const uint8_t *fcp, int fcp_len)
155{
156 memset(ofd, 0, sizeof(*ofd));
157
158 if (fcp_len != 2 && fcp_len != 5)
159 return -EINVAL;
160
161 ofd->type = iso2ftype[(fcp[0] >> 3) & 7];
162 if (ofd->type != TYPE_DF)
163 ofd->ef_type = iso2eftype[fcp[0] & 7];
164
165 if (fcp[1] != 0x21)
166 return -EINVAL;
167
168 if (fcp_len >= 5) {
169 ofd->rec_len = ntohs(*(uint16_t *)(fcp+2));
170 ofd->num_rec = fcp[4];
171 }
172
173 return 0;
174}
175
176extern struct osim_card_profile *osim_cprof_usim(void *ctx);
177
178static struct msgb *try_select_adf_usim(struct osim_chan_hdl *st)
179{
180 struct tlv_parsed tp;
181 struct osim_fcp_fd_decoded ofd;
182 struct msgb *msg, *msg2;
183 uint8_t *cur;
184 int rc, i;
185
186 msg = select_file(st, 0x2f00);
Harald Weltea5c92552012-09-10 21:05:42 +0200187 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 +0100188 if (rc < 0)
189 return NULL;
190
191 dump_fcp_template(&tp);
192
193 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
194 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 5) {
195 msgb_free(msg);
196 return NULL;
197 }
198
199 rc = osim_fcp_fd_decode(&ofd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
200 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
201 if (rc < 0) {
202 msgb_free(msg);
203 return NULL;
204 }
205
206 if (ofd.type != TYPE_EF || ofd.ef_type != EF_TYPE_RECORD_FIXED) {
207 msgb_free(msg);
208 return NULL;
209 }
210
211 msgb_free(msg);
212
213 printf("ofd rec_len = %u, num_rec = %u\n", ofd.rec_len, ofd.num_rec);
214
215 for (i = 0; i < ofd.num_rec; i++) {
216 msg = read_record_nr(st, i+1, ofd.rec_len);
217 if (!msg)
218 return NULL;
219
220 cur = msgb_apdu_de(msg);
221 if (msgb_apdu_le(msg) < 5) {
222 msgb_free(msg);
223 return NULL;
224 }
225
226 if (cur[0] != 0x61 || cur[1] < 0x03 || cur[1] > 0x7f ||
227 cur[2] != 0x4F || cur[3] < 0x01 || cur[3] > 0x10) {
228 msgb_free(msg);
229 return NULL;
230 }
231
232 /* FIXME: actually check if it is an AID that we support, or
233 * iterate until we find one that we support */
234
235 msg2 = select_adf(st, cur+4, cur[3]);
236
237 /* attach the USIM profile, FIXME: do this based on AID match */
238 st->card->prof = osim_cprof_usim(st->card);
Harald Welte5ffb5032016-03-11 09:40:56 +0700239 st->cwd = osim_file_desc_find_name(st->card->prof->mf, "ADF.USIM");
Harald Welted54c2ee2012-01-17 18:25:50 +0100240
241 msgb_free(msg);
242
243 return msg2;
244 }
245
246 return NULL;
247}
248
249static int dump_file(struct osim_chan_hdl *chan, uint16_t fid)
250{
251 struct tlv_parsed tp;
252 struct osim_fcp_fd_decoded ffdd;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200253 struct msgb *msg, *rmsg;
254 int rc, i, offset;
Harald Welted54c2ee2012-01-17 18:25:50 +0100255
256 msg = select_file(chan, fid);
Harald Weltea0ba4d92012-09-10 10:43:15 +0200257 if (!msg) {
258 printf("Unable to select file\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100259 return -EIO;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200260 }
Harald Welte76749602012-09-19 20:55:54 +0200261 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Weltea0ba4d92012-09-10 10:43:15 +0200262 if (msgb_apdu_sw(msg) != 0x9000) {
263 printf("status 0x%04x selecting file\n", msgb_apdu_sw(msg));
Harald Welted54c2ee2012-01-17 18:25:50 +0100264 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200265 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100266
Harald Weltea5c92552012-09-10 21:05:42 +0200267 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 +0200268 if (rc < 0) {
269 printf("Unable to parse FCP\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100270 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200271 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100272
273 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) ||
Harald Weltea0ba4d92012-09-10 10:43:15 +0200274 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 2) {
275 printf("No file descriptor present ?!?\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100276 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200277 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100278
279 rc = osim_fcp_fd_decode(&ffdd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC),
280 TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC));
Harald Weltea0ba4d92012-09-10 10:43:15 +0200281 if (rc < 0) {
282 printf("Unable to decode File Descriptor\n");
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
Harald Weltea0ba4d92012-09-10 10:43:15 +0200286 if (ffdd.type != TYPE_EF) {
287 printf("File Type != EF\n");
Harald Welted54c2ee2012-01-17 18:25:50 +0100288 goto out;
Harald Weltea0ba4d92012-09-10 10:43:15 +0200289 }
290
291 printf("EF type: %u\n", ffdd.ef_type);
Harald Welted54c2ee2012-01-17 18:25:50 +0100292
293 switch (ffdd.ef_type) {
294 case EF_TYPE_RECORD_FIXED:
295 for (i = 0; i < ffdd.num_rec; i++) {
Harald Weltea0ba4d92012-09-10 10:43:15 +0200296 rmsg = read_record_nr(chan, i+1, ffdd.rec_len);
Harald Welte95336312016-11-26 09:54:40 +0100297 if (!rmsg)
Harald Weltea0ba4d92012-09-10 10:43:15 +0200298 return -EIO;
Harald Welte76749602012-09-19 20:55:54 +0200299 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Welted54c2ee2012-01-17 18:25:50 +0100300 printf("Rec %03u: %s\n", i+1,
301 osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg)));
302 }
303 break;
304 case EF_TYPE_TRANSP:
Harald Weltea0ba4d92012-09-10 10:43:15 +0200305 if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_SIZE))
306 goto out;
307 i = ntohs(*(uint16_t *)TLVP_VAL(&tp, UICC_FCP_T_FILE_SIZE));
308 printf("File size: %d bytes\n", i);
309
310 for (offset = 0; offset < i-1; ) {
311 uint16_t remain_len = i - offset;
312 uint16_t read_len = OSMO_MIN(remain_len, 256);
313 rmsg = read_binary(chan, offset, read_len);
Harald Welted6ec9842014-10-27 20:43:06 +0100314 if (!rmsg)
Harald Weltea0ba4d92012-09-10 10:43:15 +0200315 return -EIO;
316 offset += read_len;
317 printf("Content: %s\n",
318 osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg)));
319 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100320 break;
321 default:
322 goto out;
323 }
324
325out:
326 msgb_free(msg);
327 return -EINVAL;
328}
329
330int main(int argc, char **argv)
331{
332 struct osim_reader_hdl *reader;
333 struct osim_card_hdl *card;
334 struct osim_chan_hdl *chan;
335 struct msgb *msg;
Harald Welted54c2ee2012-01-17 18:25:50 +0100336
Harald Welte55790aa2014-10-26 18:46:50 +0100337 reader = osim_reader_open(OSIM_READER_DRV_PCSC, 0, "", NULL);
Harald Welted54c2ee2012-01-17 18:25:50 +0100338 if (!reader)
339 exit(1);
Harald Welte55790aa2014-10-26 18:46:50 +0100340 card = osim_card_open(reader, OSIM_PROTO_T0);
Harald Welted54c2ee2012-01-17 18:25:50 +0100341 if (!card)
342 exit(2);
343 chan = llist_entry(card->channels.next, struct osim_chan_hdl, list);
344 if (!chan)
345 exit(3);
346
347 msg = try_select_adf_usim(chan);
348 if (!msg || msgb_apdu_sw(msg) != 0x9000)
349 exit(4);
350 dump_fcp_template_msg(msg);
351 msgb_free(msg);
352
353 msg = select_file(chan, 0x6fc5);
354 dump_fcp_template_msg(msg);
Harald Welte76749602012-09-19 20:55:54 +0200355 printf("SW: %s\n", osim_print_sw(chan->card, msgb_apdu_sw(msg)));
Harald Welted54c2ee2012-01-17 18:25:50 +0100356 msgb_free(msg);
357
358 verify_pin(chan, 1, "1653");
359
360 msg = select_file(chan, 0x6f06);
361 dump_fcp_template_msg(msg);
362 msgb_free(msg);
363
Harald Welted54c2ee2012-01-17 18:25:50 +0100364 {
365 struct osim_file_desc *ofd;
366 llist_for_each_entry(ofd, &chan->cwd->child_list, list) {
367 struct msgb *m;
368 printf("\n\n================ %s (%s) ==================\n",
369 ofd->short_name, ofd->long_name);
370
371 m = select_file(chan, ofd->fid);
372 dump_fcp_template_msg(m);
373 msgb_free(m);
374 dump_file(chan, ofd->fid);
375 }
376 }
Harald Welted54c2ee2012-01-17 18:25:50 +0100377
378 exit(0);
379}