dab7db343bba501b5f66ed600028886cb73e6e3b
[librfid] / utils / librfid-tool.c
1 /* librfid-tool - a small command-line tool for librfid testing
2  *
3  * (C) 2005-2006 by Harald Welte <laforge@gnumonks.org>
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 version 2 
7  *  as published by the Free Software Foundation
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24
25 #ifndef __MINGW32__
26 #include <libgen.h>
27 #endif
28
29 #define _GNU_SOURCE
30 #include <getopt.h>
31
32 #include <librfid/rfid.h>
33 #include <librfid/rfid_scan.h>
34 #include <librfid/rfid_reader.h>
35 #include <librfid/rfid_layer2.h>
36 #include <librfid/rfid_protocol.h>
37
38 #include <librfid/rfid_protocol_mifare_classic.h>
39 #include <librfid/rfid_protocol_mifare_ul.h>
40
41 #include "librfid-tool.h"
42
43
44 static int select_mf(void)
45 {
46         unsigned char cmd[] = { 0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x00 };
47         unsigned char ret[256];
48         unsigned int rlen = sizeof(ret);
49
50         int rv;
51
52         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
53         if (rv < 0)
54                 return rv;
55
56         printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
57
58         return 0;
59 }
60
61
62 static int iso7816_get_challenge(unsigned char len)
63 {
64         unsigned char cmd[] = { 0x00, 0x84, 0x00, 0x00, 0x08 };
65         unsigned char ret[256];
66         unsigned int rlen = sizeof(ret);
67
68         cmd[4] = len;
69
70         int rv;
71
72         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
73         if (rv < 0)
74                 return rv;
75
76         printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
77
78         return 0;
79 }
80
81 int
82 iso7816_select_application(void)
83 {
84         unsigned char cmd[] = { 0x00, 0xa4, 0x04, 0x0c, 0x07,
85                        0xa0, 0x00, 0x00, 0x02, 0x47, 0x10, 0x01 };
86         unsigned char resp[7];
87         unsigned int rlen = sizeof(resp);
88
89         int rv;
90
91         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
92         if (rv < 0)
93                 return rv;
94
95         /* FIXME: parse response */
96         printf("%s\n", hexdump(resp, rlen));
97
98         return 0;
99 }
100
101 int
102 iso7816_select_ef(u_int16_t fid)
103 {
104         unsigned char cmd[7] = { 0x00, 0xa4, 0x02, 0x0c, 0x02, 0x00, 0x00 };
105         unsigned char resp[7];
106         unsigned int rlen = sizeof(resp);
107
108         int rv;
109
110         cmd[5] = (fid >> 8) & 0xff;
111         cmd[6] = fid & 0xff;
112
113         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
114         if (rv < 0)
115                 return rv;
116
117         /* FIXME: parse response */
118         printf("%s\n", hexdump(resp, rlen));
119
120         return 0;
121 }
122
123 int
124 iso7816_read_binary(unsigned char *buf, unsigned int *len)
125 {
126         unsigned char cmd[] = { 0x00, 0xb0, 0x00, 0x00, 0x00 };
127         unsigned char resp[256];
128         unsigned int rlen = sizeof(resp);
129         
130         int rv;
131
132         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
133         if (rv < 0)
134                 return rv;
135
136         printf("%s\n", hexdump(resp, rlen));
137
138         /* FIXME: parse response, determine whether we need additional reads */
139
140         /* FIXME: copy 'len' number of response bytes to 'buf' */
141         return 0;
142 }
143
144 /* wrapper function around SELECT EF and READ BINARY */
145 int
146 iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len)
147 {
148         int rv;
149
150         rv = iso7816_select_ef(fid);
151         if (rv < 0)
152                 return rv;
153
154         return iso7816_read_binary(buf, len);
155 }
156
157 /* mifare ultralight helpers */
158 int
159 mifare_ulight_write(struct rfid_protocol_handle *ph)
160 {
161         unsigned char buf[4] = { 0xa1, 0xa2, 0xa3, 0xa4 };
162
163         return rfid_protocol_write(ph, 10, buf, 4);
164 }
165
166 int
167 mifare_ulight_blank(struct rfid_protocol_handle *ph)
168 {
169         unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };
170         int i, ret;
171
172         for (i = 4; i <= MIFARE_UL_PAGE_MAX; i++) {
173                 ret = rfid_protocol_write(ph, i, buf, 4);
174                 if (ret < 0)
175                         return ret;
176         }
177         return 0;
178 }
179
180 static int
181 mifare_ulight_read(struct rfid_protocol_handle *ph)
182 {
183         unsigned char buf[20];
184         unsigned int len = sizeof(buf);
185         int ret;
186         int i;
187
188         for (i = 0; i <= MIFARE_UL_PAGE_MAX; i++) {
189                 ret = rfid_protocol_read(ph, i, buf, &len);
190                 if (ret < 0)
191                         return ret;
192
193                 printf("Page 0x%x: %s\n", i, hexdump(buf, 4));
194         }
195         return 0;
196 }
197
198 /* mifare classic helpers */
199 static int
200 mifare_classic_read_sector(struct rfid_protocol_handle *ph, int sector)
201 {
202         unsigned char buf[20];
203         unsigned int len = sizeof(buf);
204         int ret;
205         int block;
206
207         /* FIXME: make this work for sectors > 31 */
208         printf("Reading sector %u\n", sector);
209
210         for (block = sector*4; block < sector*4+4; block++) {
211                 printf("Reading block %u: ", block);
212                 ret = rfid_protocol_read(ph, block, buf, &len);
213                 if(ret == -ETIMEDOUT)
214                         fprintf(stderr, "TIMEOUT\n");
215                 if (ret < 0) {
216                         printf("Error %d reading\n", ret);
217                         return ret;
218                 }
219
220                 printf("Page 0x%x: %s\n", block, hexdump(buf, len));
221         }
222         return 0;
223 }
224
225 static char *proto_names[] = {
226         [RFID_PROTOCOL_TCL] = "tcl",
227         [RFID_PROTOCOL_MIFARE_UL] = "mifare-ultralight",
228         [RFID_PROTOCOL_MIFARE_CLASSIC] = "mifare-classic",
229 };
230
231 static int proto_by_name(const char *name)
232 {
233         int i;
234
235         for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
236                 if (proto_names[i] == NULL)
237                         continue;
238                 if (!strcasecmp(name, proto_names[i]))
239                         return i;
240         }
241         return -1;
242 }
243
244 static char *l2_names[] = {
245         [RFID_LAYER2_ISO14443A] = "iso14443a",
246         [RFID_LAYER2_ISO14443B] = "iso14443b",
247         [RFID_LAYER2_ISO15693] = "iso15693",
248 };
249
250 static int l2_by_name(const char *name)
251 {
252         int i;
253
254         for (i = 0; i < ARRAY_SIZE(l2_names); i++) {
255                 if (l2_names[i] == NULL)
256                         continue;
257                 if (!strcasecmp(name, l2_names[i]))
258                         return i;
259         }
260         return -1;
261 }
262
263 static int do_scan(int first)
264 {
265         int rc;
266         unsigned int size;
267         unsigned int size_len = sizeof(size);
268
269         if (first) {
270                 rh->reader->rf_power(rh, 0);
271                 usleep(10*1000);
272                 rh->reader->rf_power(rh, 1);
273         }
274         printf("scanning for RFID token...\n");
275         rc = rfid_scan(rh, &l2h, &ph);
276         if (rc >= 2) {
277                 unsigned char uid_buf[16];
278                 unsigned int uid_len = sizeof(uid_buf);
279                 rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_UID, &uid_buf,
280                                    &uid_len);
281                 printf("Layer 2 success (%s): %s\n", rfid_layer2_name(l2h),
282                         hexdump(uid_buf, uid_len));
283         }
284         if (rc >= 3) {
285                 printf("Protocol success (%s)\n", rfid_protocol_name(ph));
286
287                 if (rfid_protocol_getopt(ph, RFID_OPT_PROTO_SIZE, 
288                                          &size, &size_len) == 0)
289                         printf("Size: %u bytes\n", size);
290         }
291
292         return rc;
293 }
294
295 static void do_endless_scan()
296 {
297         int rc;
298         int first = 1;
299
300         while (1) {
301                 if (first)
302                         putc('\n', stdout);
303                 printf("==> doing %s scan\n", first ? "first" : "successive");
304                 rc = do_scan(first);
305                 if (rc >= 3) {
306                         printf("closing proto\n");
307                         rfid_protocol_close(ph);
308                 }
309                 if (rc >= 2) {
310                         printf("closing layer2\n");
311                         rfid_layer2_close(l2h);
312                         first = 0;
313                 } else
314                         first = 1;
315         }
316 }
317
318 #define OPTION_OFFSET 256
319
320 static struct option original_opts[] = {
321         { "help", 0, 0, 'h' },
322         { "layer2", 1, 0, 'l' },
323         { "protocol", 1, 0, 'p' },
324         { "scan", 0, 0, 's' },
325         { "scan-loop", 0, 0, 'S' },
326         {0, 0, 0, 0}
327 };
328
329 /* module / option merging code */
330 static struct option *opts = original_opts;
331 static unsigned int global_option_offset = 0;
332
333 static char *program_name;
334 static char *program_version = LIBRFID_TOOL_VERSION;
335
336 static void free_opts(int reset_offset)
337 {
338         if (opts != original_opts) {
339                 free(opts);
340                 opts = original_opts;
341                 if (reset_offset)
342                         global_option_offset = 0;
343         }
344 }
345
346 static struct option *
347 merge_options(struct option *oldopts, const struct option *newopts,
348               unsigned int *option_offset)
349 {
350         unsigned int num_old, num_new, i;
351         struct option *merge;
352
353         for (num_old = 0; oldopts[num_old].name; num_old++);
354         for (num_new = 0; oldopts[num_new].name; num_new++);
355
356         global_option_offset += OPTION_OFFSET;
357         *option_offset = global_option_offset;
358
359         merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
360         memcpy(merge, oldopts, num_old * sizeof(struct option));
361         free_opts(0); /* Release previous options merged if any */
362         for (i = 0; i < num_new; i++) {
363                 merge[num_old + i] = newopts[i];
364                 merge[num_old + i].val += *option_offset;
365         }
366         memset(merge + num_old + num_new, 0, sizeof(struct option));
367
368         return merge;
369 }
370
371 struct rfidtool_module *find_module(const char *name)
372 {
373         return NULL;
374 }
375
376 void register_module(struct rfidtool_module *me)
377 {
378         struct rfidtool_module *old;
379
380         if (strcmp(me->version, program_version) != 0) {
381                 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
382                         program_name, me->name, me->version, program_version);
383                 exit(1);
384         }
385
386         old = find_module(me->name);
387         if (old) {
388                 fprintf(stderr, "%s: target `%s' already registered.\n",
389                         program_name, me->name);
390                 exit(1);
391         }
392 }
393
394 static void help(void)
395 {
396         printf( " -s    --scan          scan until first RFID tag is found\n"
397                 " -S    --scan-loop     endless scanning loop\n" 
398                 " -p    --protocol      {tcl,mifare-ultralight,mifare-classic}\n"
399                 " -l    --layer2        {iso14443a,iso14443b,iso15693}\n"
400                 " -h    --help\n");
401 }
402
403 int main(int argc, char **argv)
404 {
405         int rc;
406         char buf[0x100];
407         int i, len, protocol = -1, layer2 = -1;
408
409 #ifdef  __MINGW32__
410         program_name = argv[0];
411 #else /*__MINGW32__*/
412         program_name = basename(argv[0]);
413 #endif/*__MINGW32__*/
414         
415         printf("%s - (C) 2006 by Harald Welte\n"
416                "This program is Free Software and has "
417                "ABSOLUTELY NO WARRANTY\n\n", program_name);
418
419         printf("initializing librfid\n");
420         rfid_init();
421
422         while (1) {
423                 int c, option_index = 0;
424                 c = getopt_long(argc, argv, "hp:l:sS", opts, &option_index);
425                 if (c == -1)
426                         break;
427
428                 switch (c) {
429                 case 's':
430                         if (reader_init() < 0)
431                                 exit(1);
432                         do_scan(0);
433                         exit(0);
434                         break;
435                 case 'S':
436                         if (reader_init() < 0)
437                                 exit(1);
438                         do_endless_scan();
439                         exit(0);
440                         break;
441                 case 'p':
442                         protocol = proto_by_name(optarg);
443                         if (protocol < 0) {
444                                 fprintf(stderr, "unknown protocol `%s'\n", 
445                                         optarg);
446                                 exit(2);
447                         }
448                         break;
449                 case 'l':
450                         layer2 = l2_by_name(optarg);
451                         if (layer2 < 0) {
452                                 fprintf(stderr, "unknown layer2 `%s'\n",
453                                         optarg);
454                                 exit(2);
455                         }
456                         break;
457                 case 'h':
458                         help();
459                         exit(0);
460                         break;
461                 }
462         }
463
464         switch (protocol) {
465         case RFID_PROTOCOL_MIFARE_UL:
466         case RFID_PROTOCOL_MIFARE_CLASSIC:
467                 layer2 = RFID_LAYER2_ISO14443A;
468                 break;
469         case -1:
470                 fprintf(stderr, "you have to specify --protocol\n");
471                 exit(2);
472         }
473
474         if (layer2 < 0) {
475                 fprintf(stderr, "you have to specify --layer2\n");
476                 exit(2);
477         }
478         
479         if (reader_init() < 0)
480                 exit(1);
481
482         if (l2_init(layer2) < 0)
483                 exit(1);
484
485         if (l3_init(protocol) < 0)
486                 exit(1);
487
488         switch (protocol) {
489
490         case RFID_PROTOCOL_TCL:
491                 printf("Protocol T=CL\n");
492                 /* we've established T=CL at this point */
493                 printf("selecting Master File\n");
494                 rc = select_mf();
495                 if (rc < 0) {
496                         printf("error selecting MF\n");
497                         break;
498                 }
499
500                 printf("Getting random challenge, length 255\n");
501                 rc = iso7816_get_challenge(0xff);
502                 if (rc < 0) {
503                         printf("error getting random challenge\n");
504                         break;
505                 }
506
507                 printf("selecting Passport application\n");
508                 rc = iso7816_select_application();
509                 if (rc < 0) {
510                         printf("error selecting passport application\n");
511                         break;
512                 }
513
514                 printf("selecting EF 0x1e\n");
515                 rc = iso7816_select_ef(0x011e);
516                 if (rc < 0) {
517                         printf("error selecting EF 0x1e\n");
518                         break;
519                 }
520
521                 printf("selecting EF 0x01\n");
522                 rc = iso7816_select_ef(0x0101);
523                 if (rc < 0) {
524                         printf("error selecting EF 0x01\n");
525                         break;
526                 }
527
528                 while (1) {
529                         printf("reading EF1\n");
530                         len = sizeof(buf);
531                         printf("reading ef\n");
532                         rc = iso7816_read_binary(buf, &len);
533                         if (rc < 0) {
534                                 printf("error reading EF\n");
535                                 break;
536                         }
537                 }
538 #if 0
539                 for (i = 0; i < 4; i++)
540                         iso7816_get_challenge(0xff);
541 #endif
542                 break;
543         case RFID_PROTOCOL_MIFARE_UL:
544                 printf("Protocol Mifare Ultralight\n");
545                 mifare_ulight_read(ph);
546 #if 0
547                 mifare_ulight_blank(ph);
548                 mifare_ulight_write(ph);
549                 mifare_ulight_read(ph);
550 #endif
551                 break;
552         case RFID_PROTOCOL_MIFARE_CLASSIC:
553                 printf("Protocol Mifare Classic\n");
554                 {
555                         int sector;
556                         for (sector = 0; sector < 31; sector++) {
557                                 printf("Authenticating sector %u: ", sector);
558                                 fflush(stdout);
559                                 rc = mfcl_set_key(ph, MIFARE_CL_KEYA_DEFAULT_INFINEON);
560                                 if (rc < 0) {
561                                         printf("key format error\n");
562                                         exit(1);
563                                 }
564                                 rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, sector*4);
565                                 if (rc < 0) {
566                                         printf("mifare auth error\n");
567                                         exit(1);
568                                 } else 
569                                         printf("mifare auth succeeded!\n");
570
571                                 mifare_classic_read_sector(ph, sector);
572                         }
573                 }
574                 break;
575         default:
576                 printf("unknown protocol\n");
577                 exit(1);
578                 break;
579         }
580
581         rfid_reader_close(rh);
582         
583         exit(0);
584 }