Make librfid-tool print out the ATS in case of success with a T=CL card
[librfid] / utils / librfid-tool.c
1 /* librfid-tool - a small command-line tool for librfid testing
2  *
3  * (C) 2005-2008 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_layer2_iso14443a.h>
39 #include <librfid/rfid_layer2_iso15693.h>
40
41 #include <librfid/rfid_protocol_mifare_classic.h>
42 #include <librfid/rfid_protocol_mifare_ul.h>
43 #include <librfid/rfid_protocol_tagit.h>
44 #include <librfid/rfid_protocol_icode.h>
45 #include <librfid/rfid_protocol_tcl.h>
46
47 #include "librfid-tool.h"
48
49
50 static int select_mf(void)
51 {
52         unsigned char cmd[] = { 0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x00 };
53         unsigned char ret[256];
54         unsigned int rlen = sizeof(ret);
55
56         int rv;
57
58         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
59         if (rv < 0)
60                 return rv;
61
62         printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
63
64         return 0;
65 }
66
67
68 static int iso7816_get_challenge(unsigned char len)
69 {
70         unsigned char cmd[] = { 0x00, 0x84, 0x00, 0x00, 0x08 };
71         unsigned char ret[256];
72         unsigned int rlen = sizeof(ret);
73
74         cmd[4] = len;
75
76         int rv;
77
78         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
79         if (rv < 0)
80                 return rv;
81
82         printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
83
84         return 0;
85 }
86
87 int
88 iso7816_select_application(void)
89 {
90         unsigned char cmd[] = { 0x00, 0xa4, 0x04, 0x0c, 0x07,
91                        0xa0, 0x00, 0x00, 0x02, 0x47, 0x10, 0x01 };
92         unsigned char resp[7];
93         unsigned int rlen = sizeof(resp);
94
95         int rv;
96
97         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
98         if (rv < 0)
99                 return rv;
100
101         /* FIXME: parse response */
102         printf("%s\n", hexdump(resp, rlen));
103
104         return 0;
105 }
106
107 int
108 iso7816_select_ef(u_int16_t fid)
109 {
110         unsigned char cmd[7] = { 0x00, 0xa4, 0x02, 0x0c, 0x02, 0x00, 0x00 };
111         unsigned char resp[7];
112         unsigned int rlen = sizeof(resp);
113
114         int rv;
115
116         cmd[5] = (fid >> 8) & 0xff;
117         cmd[6] = fid & 0xff;
118
119         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
120         if (rv < 0)
121                 return rv;
122
123         /* FIXME: parse response */
124         printf("%s\n", hexdump(resp, rlen));
125
126         return 0;
127 }
128
129 int
130 iso7816_read_binary(unsigned char *buf, unsigned int *len)
131 {
132         unsigned char cmd[] = { 0x00, 0xb0, 0x00, 0x00, 0x00 };
133         unsigned char resp[256];
134         unsigned int rlen = sizeof(resp);
135         
136         int rv;
137
138         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
139         if (rv < 0)
140                 return rv;
141
142         printf("%s\n", hexdump(resp, rlen));
143
144         /* FIXME: parse response, determine whether we need additional reads */
145
146         /* FIXME: copy 'len' number of response bytes to 'buf' */
147         return 0;
148 }
149
150 /* wrapper function around SELECT EF and READ BINARY */
151 int
152 iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len)
153 {
154         int rv;
155
156         rv = iso7816_select_ef(fid);
157         if (rv < 0)
158                 return rv;
159
160         return iso7816_read_binary(buf, len);
161 }
162
163 /* mifare ultralight helpers */
164 int
165 mifare_ulight_write(struct rfid_protocol_handle *ph)
166 {
167         unsigned char buf[4] = { 0xa1, 0xa2, 0xa3, 0xa4 };
168
169         return rfid_protocol_write(ph, 10, buf, 4);
170 }
171
172 int
173 mifare_ulight_blank(struct rfid_protocol_handle *ph)
174 {
175         unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };
176         int i, ret;
177
178         for (i = 4; i <= MIFARE_UL_PAGE_MAX; i++) {
179                 ret = rfid_protocol_write(ph, i, buf, 4);
180                 if (ret < 0)
181                         return ret;
182         }
183         return 0;
184 }
185
186 static int
187 mifare_ulight_read(struct rfid_protocol_handle *ph)
188 {
189         unsigned char buf[20];
190         unsigned int len = sizeof(buf);
191         int ret;
192         int i;
193
194         for (i = 0; i <= MIFARE_UL_PAGE_MAX; i++) {
195                 ret = rfid_protocol_read(ph, i, buf, &len);
196                 if (ret < 0)
197                         return ret;
198
199                 printf("Page 0x%x: %s\n", i, hexdump(buf, 4));
200         }
201         return 0;
202 }
203
204 /* mifare classic helpers */
205 static int
206 mifare_classic_read_sector(struct rfid_protocol_handle *ph, int sector)
207 {
208         unsigned char buf[20];
209         unsigned int len = sizeof(buf);
210         int ret;
211         int block, blocks_per_sector, first_block;
212
213         printf("Reading sector %u\n", sector);
214
215         first_block = mfcl_sector2block(sector);
216         blocks_per_sector = mfcl_sector_blocks(sector);
217
218         if (first_block < 0 || blocks_per_sector < 0)
219                 return -EINVAL;
220
221         for (block = first_block; block < first_block + blocks_per_sector;
222              block++) {
223                 printf("Reading block %u: ", block);
224                 ret = rfid_protocol_read(ph, block, buf, &len);
225                 if (ret == -ETIMEDOUT)
226                         fprintf(stderr, "TIMEOUT\n");
227                 if (ret < 0) {
228                         printf("Error %d reading\n", ret);
229                         return ret;
230                 }
231
232                 printf("Page 0x%x: %s\n", block, hexdump(buf, len));
233         }
234         return 0;
235 }
236
237 static int
238 mifare_classic_dump(struct rfid_protocol_handle *ph)
239 {
240         unsigned int size;
241         unsigned int size_len = sizeof(size);
242         int sector, num_sectors;
243
244         if (rfid_protocol_getopt(ph, RFID_OPT_PROTO_SIZE, 
245                                  &size, &size_len) == 0) {
246                 printf("Size: %u bytes\n", size);
247         } else {
248                 printf("Size: unknown ?!?\n");
249                 return -EINVAL;
250         }
251
252         switch (size) {
253         case 320:
254                 num_sectors = 5;
255                 break;
256         case 1024:
257                 num_sectors = 16;
258                 break;
259         case 4096:
260                 num_sectors = 40;
261                 break;
262         default:
263                 return -EINVAL;
264         }
265
266         for (sector = 0; sector < num_sectors; sector++) {
267                 int rc;
268
269                 printf("Authenticating sector %u: ", sector);
270                 fflush(stdout);
271
272                 rc = mfcl_set_key(ph, MIFARE_CL_KEYA_DEFAULT_INFINEON);
273                 if (rc < 0) {
274                         printf("key format error\n");
275                         exit(1);
276                 }
277
278                 rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A,
279                                mfcl_sector2block(sector));
280                 if (rc < 0) {
281                         printf("mifare auth error\n");
282                         exit(1);
283                 } else 
284                         printf("mifare auth succeeded!\n");
285
286                 mifare_classic_read_sector(ph, sector);
287         }
288 }
289
290 static char *proto_names[] = {
291         [RFID_PROTOCOL_TCL] = "tcl",
292         [RFID_PROTOCOL_MIFARE_UL] = "mifare-ultralight",
293         [RFID_PROTOCOL_MIFARE_CLASSIC] = "mifare-classic",
294         [RFID_PROTOCOL_ICODE_SLI] = "icode",
295         [RFID_PROTOCOL_TAGIT] = "tagit",
296 };
297
298 static int proto_by_name(const char *name)
299 {
300         int i;
301
302         for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
303                 if (proto_names[i] == NULL)
304                         continue;
305                 if (!strcasecmp(name, proto_names[i]))
306                         return i;
307         }
308         return -1;
309 }
310
311 static char *l2_names[] = {
312         [RFID_LAYER2_ISO14443A] = "iso14443a",
313         [RFID_LAYER2_ISO14443B] = "iso14443b",
314         [RFID_LAYER2_ISO15693] = "iso15693",
315         [RFID_LAYER2_ICODE1] = "icode1",
316 };
317
318 static int l2_by_name(const char *name)
319 {
320         int i;
321
322         for (i = 0; i < ARRAY_SIZE(l2_names); i++) {
323                 if (l2_names[i] == NULL)
324                         continue;
325                 if (!strcasecmp(name, l2_names[i]))
326                         return i;
327         }
328         return -1;
329 }
330
331 static int do_scan(int first)
332 {
333         int rc;
334         unsigned int size;
335         unsigned int size_len = sizeof(size);
336         char *data;
337         unsigned int data_len;
338
339         if (first) {
340                 unsigned int opt;
341                 unsigned int optlen = sizeof(opt);
342
343                 /* turn off RF */
344                 opt = 1;
345                 rfid_reader_setopt(rh, RFID_OPT_RDR_RF_KILL, &opt, optlen);
346
347                 usleep(10*1000);
348
349                 /* turn on RF */
350                 opt = 0;
351                 rfid_reader_setopt(rh, RFID_OPT_RDR_RF_KILL, &opt, optlen);
352         }
353         printf("scanning for RFID token...\n");
354         rc = rfid_scan(rh, &l2h, &ph);
355         if (rc >= 2) {
356                 unsigned char uid_buf[16];
357                 unsigned int uid_len = sizeof(uid_buf);
358                 rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_UID, &uid_buf,
359                                    &uid_len);
360                 printf("Layer 2 success (%s): %s\n", rfid_layer2_name(l2h),
361                         hexdump(uid_buf, uid_len));
362         }
363         if (rc >= 3) {
364                 printf("Protocol success (%s)\n", rfid_protocol_name(ph));
365
366                 if (rfid_protocol_getopt(ph, RFID_OPT_PROTO_SIZE, 
367                                          &size, &size_len) == 0)
368                         printf("Size: %u bytes\n", size);
369                 size_len = sizeof(size);
370                 size = 0;
371                 if (rfid_protocol_getopt(ph, RFID_OPT_P_TCL_ATS_LEN,
372                                          &size, &size_len) == 0) {
373                         data_len = size + 1;
374                         data = malloc(data_len);
375                         if (data) {
376                                 if (rfid_protocol_getopt(ph, RFID_OPT_P_TCL_ATS,
377                                                          data, &data_len) == 0) {
378                                         printf("Got ATS of %u bytes: %s\n", size,
379                                                hexdump(data, data_len));
380                                 }
381                         }
382                 }
383         }
384
385         return rc;
386 }
387
388 static void do_endless_scan()
389 {
390         int rc;
391         int first = 1;
392
393         while (1) {
394                 if (first)
395                         putc('\n', stdout);
396                 printf("==> doing %s scan\n", first ? "first" : "successive");
397                 rc = do_scan(first);
398                 if (rc >= 3) {
399                         printf("closing proto\n");
400                         rfid_protocol_close(ph);
401                 }
402                 if (rc >= 2) {
403                         printf("closing layer2\n");
404                         rfid_layer2_close(l2h);
405                         first = 0;
406                 } else
407                         first = 1;
408         }
409 }
410
411 static void do_regdump(void)
412 {
413         u_int8_t buffer[0xff];
414         int i;
415
416         printf("dumping rc632 regs...\n");
417
418         rc632_register_dump(rh->ah, buffer);
419
420         printf("\n     ");
421         for (i=0; i<=0x0f; i++)
422                 printf(" 0x_%01X",i);
423         printf("\n-----------------------------------------------------------------------------------\n");
424
425         for (i=0; i <= 0x3f; i++) {
426                 if ((i % 0x10) == 0)
427                         printf("0x%01X_:",i/0x10);
428                 printf(" 0x%02X", buffer[i]);
429                 if ((i% 0x10) == 0x0f)
430                         printf("\n");
431         }
432
433         /* print regdump as c-style array*/
434         printf("u_int8_t rc632_regs[] = {");
435         for (i = 0; i <= 0x3f; i++) {
436                 if (((i+1) % 0x08) == 1) {
437                         if (i > 7)
438                                 printf("//%2d..%2d",i-8,i-1);
439                         printf("\n\t");
440                 }
441                 printf(" 0x%02X, ",buffer[i]);
442         }
443         printf("//%2d..%2d\n\t 0 };\n",i-8,i-1);
444
445 }
446
447 static void do_enum(int layer2)
448 {
449         int rc;
450         //unsigned int size;
451         //unsigned int size_len = sizeof(size);
452         unsigned char uid_buf[16];
453         unsigned int uid_len;
454
455         printf("scanning for RFID token on layer %s...\n", l2_names[layer2]);
456
457         if (rh->reader->l2_supported & (1 << layer2)) {
458                 l2h = rfid_layer2_init(rh, layer2);
459                 rc = rfid_layer2_open(l2h);
460         } else {
461                 printf("error during layer2_open\n");
462                 return ;
463         }
464
465         while (rc>=0) {
466                 if (l2h) {
467                         uid_len = sizeof(uid_buf);
468                         rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_UID, &uid_buf, &uid_len);
469                         printf("Layer 2 success (%s)[%d]: %s\n", rfid_layer2_name(l2h), uid_len, hexdump(uid_buf, uid_len));
470                 }
471
472         /*
473                 ph = rfid_protocol_scan(l2h);
474                 if (ph) {
475                         printf("Protocol success (%s)\n", rfid_protocol_name(ph));
476
477                         if (rfid_protocol_getopt(ph, RFID_OPT_PROTO_SIZE,
478                                              &size, &size_len) == 0)
479                         printf("Size: %u bytes\n", size);
480                 } else
481                         printf("##############\n");
482                 */
483
484                 if (rc >= 0) {
485                         rfid_layer2_close(l2h);
486                 }
487                 rc = rfid_layer2_open(l2h);
488         }
489 }
490
491 #define OPTION_OFFSET 256
492
493 static struct option original_opts[] = {
494         { "help", 0, 0, 'h' },
495         { "layer2", 1, 0, 'l' },
496         { "protocol", 1, 0, 'p' },
497         { "scan", 0, 0, 's' },
498         { "scan-loop", 0, 0, 'S' },
499         { "dump", 0, 0, 'd' },
500         { "enum", 0, 0, 'e' },
501         {0, 0, 0, 0}
502 };
503
504 /* module / option merging code */
505 static struct option *opts = original_opts;
506 static unsigned int global_option_offset = 0;
507
508 static char *program_name;
509 static char *program_version = LIBRFID_TOOL_VERSION;
510
511 static void free_opts(int reset_offset)
512 {
513         if (opts != original_opts) {
514                 free(opts);
515                 opts = original_opts;
516                 if (reset_offset)
517                         global_option_offset = 0;
518         }
519 }
520
521 static struct option *
522 merge_options(struct option *oldopts, const struct option *newopts,
523               unsigned int *option_offset)
524 {
525         unsigned int num_old, num_new, i;
526         struct option *merge;
527
528         for (num_old = 0; oldopts[num_old].name; num_old++);
529         for (num_new = 0; oldopts[num_new].name; num_new++);
530
531         global_option_offset += OPTION_OFFSET;
532         *option_offset = global_option_offset;
533
534         merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
535         memcpy(merge, oldopts, num_old * sizeof(struct option));
536         free_opts(0); /* Release previous options merged if any */
537         for (i = 0; i < num_new; i++) {
538                 merge[num_old + i] = newopts[i];
539                 merge[num_old + i].val += *option_offset;
540         }
541         memset(merge + num_old + num_new, 0, sizeof(struct option));
542
543         return merge;
544 }
545
546 struct rfidtool_module *find_module(const char *name)
547 {
548         return NULL;
549 }
550
551 void register_module(struct rfidtool_module *me)
552 {
553         struct rfidtool_module *old;
554
555         if (strcmp(me->version, program_version) != 0) {
556                 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
557                         program_name, me->name, me->version, program_version);
558                 exit(1);
559         }
560
561         old = find_module(me->name);
562         if (old) {
563                 fprintf(stderr, "%s: target `%s' already registered.\n",
564                         program_name, me->name);
565                 exit(1);
566         }
567 }
568
569 static void help(void)
570 {
571         printf( " -s    --scan          scan until first RFID tag is found\n"
572                 " -S    --scan-loop     endless scanning loop\n" 
573                 " -p    --protocol      {tcl,mifare-ultralight,mifare-classic,tagit}\n"
574                 " -l    --layer2        {iso14443a,iso14443b,iso15693}\n"
575                 " -d    --dump          dump rc632 registers\n"
576                 " -e    --enum          enumerate all tag's in field (iso14443a)\n"
577                 " -h    --help\n");
578 }
579
580 int main(int argc, char **argv)
581 {
582         int rc;
583         char buf[0x100];
584         int i, len, protocol = -1, layer2 = -1;
585
586 #ifdef  __MINGW32__
587         program_name = argv[0];
588 #else /*__MINGW32__*/
589         program_name = basename(argv[0]);
590 #endif/*__MINGW32__*/
591         
592         printf("%s - (C) 2005-2008 by Harald Welte\n"
593                "This program is Free Software and has "
594                "ABSOLUTELY NO WARRANTY\n\n", program_name);
595
596         printf("initializing librfid\n");
597         rfid_init();
598
599         while (1) {
600                 int c, option_index = 0;
601                 c = getopt_long(argc, argv, "hp:l:sSde", opts, &option_index);
602                 if (c == -1)
603                         break;
604
605                 switch (c) {
606                 case 'e':
607                         if (reader_init() < 0)
608                                 exit(1);
609                         layer2 = RFID_LAYER2_ISO14443A;
610                         do_enum(layer2);
611                         exit(0);
612                         break;
613                 case 'd':
614                         if (reader_init() < 0)
615                                 exit(1);
616                         do_regdump();
617                         break;
618                 case 's':
619                         if (reader_init() < 0)
620                                 exit(1);
621                         do_scan(0);
622                         exit(0);
623                         break;
624                 case 'S':
625                         if (reader_init() < 0)
626                                 exit(1);
627                         do_endless_scan();
628                         exit(0);
629                         break;
630                 case 'p':
631                         protocol = proto_by_name(optarg);
632                         if (protocol < 0) {
633                                 fprintf(stderr, "unknown protocol `%s'\n", 
634                                         optarg);
635                                 exit(2);
636                         }
637                         break;
638                 case 'l':
639                         layer2 = l2_by_name(optarg);
640                         if (layer2 < 0) {
641                                 fprintf(stderr, "unknown layer2 `%s'\n",
642                                         optarg);
643                                 exit(2);
644                         }
645                         break;
646                 case 'h':
647                         help();
648                         exit(0);
649                         break;
650                 }
651         }
652
653         switch (protocol) {
654         case RFID_PROTOCOL_MIFARE_UL:
655         case RFID_PROTOCOL_MIFARE_CLASSIC:
656                 layer2 = RFID_LAYER2_ISO14443A;
657                 break;
658         case -1:
659                 fprintf(stderr, "you have to specify --protocol\n");
660                 exit(2);
661         }
662
663         if (layer2 < 0) {
664                 fprintf(stderr, "you have to specify --layer2\n");
665                 exit(2);
666         }
667         
668         if (reader_init() < 0)
669                 exit(1);
670
671
672         if (l2_init(layer2) < 0) {
673                 rfid_reader_close(rh);
674                 exit(1);
675         }
676
677         if (l3_init(protocol) < 0) {
678                 rfid_reader_close(rh);
679                 exit(1);
680         }
681
682         switch (protocol) {
683
684         case RFID_PROTOCOL_TCL:
685                 printf("Protocol T=CL\n");
686                 /* we've established T=CL at this point */
687                 printf("selecting Master File\n");
688                 rc = select_mf();
689                 if (rc < 0) {
690                         printf("error selecting MF\n");
691                         break;
692                 }
693
694                 printf("Getting random challenge, length 255\n");
695                 rc = iso7816_get_challenge(0xff);
696                 if (rc < 0) {
697                         printf("error getting random challenge\n");
698                         break;
699                 }
700
701                 printf("selecting Passport application\n");
702                 rc = iso7816_select_application();
703                 if (rc < 0) {
704                         printf("error selecting passport application\n");
705                         break;
706                 }
707
708                 printf("selecting EF 0x1e\n");
709                 rc = iso7816_select_ef(0x011e);
710                 if (rc < 0) {
711                         printf("error selecting EF 0x1e\n");
712                         break;
713                 }
714
715                 printf("selecting EF 0x01\n");
716                 rc = iso7816_select_ef(0x0101);
717                 if (rc < 0) {
718                         printf("error selecting EF 0x01\n");
719                         break;
720                 }
721
722                 while (1) {
723                         printf("reading EF1\n");
724                         len = sizeof(buf);
725                         printf("reading ef\n");
726                         rc = iso7816_read_binary(buf, &len);
727                         if (rc < 0) {
728                                 printf("error reading EF\n");
729                                 break;
730                         }
731                 }
732 #if 0
733                 for (i = 0; i < 4; i++)
734                         iso7816_get_challenge(0xff);
735 #endif
736                 break;
737         case RFID_PROTOCOL_MIFARE_UL:
738                 printf("Protocol Mifare Ultralight\n");
739                 mifare_ulight_read(ph);
740 #if 0
741                 mifare_ulight_blank(ph);
742                 mifare_ulight_write(ph);
743                 mifare_ulight_read(ph);
744 #endif
745                 break;
746         case RFID_PROTOCOL_MIFARE_CLASSIC:
747                 printf("Protocol Mifare Classic\n");
748                 mifare_classic_dump(ph);
749                 break;
750         default:
751                 printf("unknown protocol %u\n", protocol);
752                 exit(1);
753                 break;
754         }
755
756         rfid_reader_close(rh);
757         
758         exit(0);
759 }