include not-installed headers in release tarballs
[librfid] / utils / mifare-tool.c
1 /* mifare-tool - a small command-line tool for librfid mifare testing
2  *
3  * (C) 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <libgen.h>
25
26 #define _GNU_SOURCE
27 #include <getopt.h>
28
29 #include <librfid/rfid.h>
30 #include <librfid/rfid_scan.h>
31 #include <librfid/rfid_reader.h>
32 #include <librfid/rfid_layer2.h>
33 #include <librfid/rfid_protocol.h>
34
35 #include <librfid/rfid_protocol_mifare_classic.h>
36 #include <librfid/rfid_protocol_mifare_ul.h>
37
38 #include <librfid/rfid_access_mifare_classic.h>
39
40 #include "librfid-tool.h"
41
42 static char *program_name;
43
44 static void help(void)
45 {
46         printf( " -h    --help          Print this help message\n"
47                 " -r    --read          Read a mifare sector\n"
48                 " -l    --loop-read     Loop reading a mifare sector\n"
49                 " -w    --write         Write a mifare sector\n"
50                 " -k    --key           Specify mifare access key (in hex tuples)\n");
51 }
52
53 static struct option mifare_opts[] = {
54         { "key", 1, 0, 'k' },
55         { "read", 1, 0, 'r' },
56         { "loop-read", 1, 0, 'l' },
57         { "write", 1 ,0, 'w' },
58         { "help", 0, 0, 'h' },
59         { 0, 0, 0, 0 }
60 };
61
62 static int mifare_cl_auth(unsigned char *key, int page)
63 {
64         int rc;
65
66         rc = mfcl_set_key(ph, key);
67         if (rc < 0) {
68                 fprintf(stderr, "key format error\n");
69                 return rc;
70         }
71         rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, page);
72         if (rc < 0) {
73                 fprintf(stderr, "mifare auth error\n");
74                 return rc;
75         } else 
76                 printf("mifare auth succeeded!\n");
77         
78         return 0;
79 }
80
81 static void mifare_l3(void)
82 {
83         while (l2_init(RFID_LAYER2_ISO14443A) < 0) ;
84
85         printf("ISO14443-3A anticollision succeeded\n");
86
87         while (l3_init(RFID_PROTOCOL_MIFARE_CLASSIC) < 0) ;
88
89         printf("Mifare card available\n");
90 }
91
92 int main(int argc, char **argv)
93 {
94         int len, rc, c, option_index = 0;
95         unsigned int page;
96         char key[MIFARE_CL_KEY_LEN];
97         char buf[MIFARE_CL_PAGE_SIZE];
98         program_name = basename(argv[0]);
99
100         memcpy(key, MIFARE_CL_KEYA_DEFAULT_INFINEON,
101                sizeof(MIFARE_CL_KEYA_DEFAULT_INFINEON));
102
103         printf("%s - (C) 2006 by Harald Welte\n"
104                "This program is Free Software and has "
105                "ABSOLUTELY NO WARRANTY\n\n", program_name);
106
107         printf("initializing librfid\n");
108         rfid_init();
109
110         if (reader_init() < 0) {
111                 fprintf(stderr, "error opening reader\n");
112                 exit(1);
113         }
114
115         while (1) {
116                 c = getopt_long(argc, argv, "k:r:l:w:", mifare_opts,
117                                 &option_index);
118                 if (c == -1)
119                         break;
120
121                 switch (c) {
122                 case 'k':
123                         hexread(key, optarg, strlen(optarg));
124                         printf("key: %s\n", hexdump(key, MIFARE_CL_KEY_LEN));
125                         break;
126                 case 'r':
127                         page = atoi(optarg);
128                         printf("read(key='%s',page=%u):",
129                                 hexdump(key, MIFARE_CL_KEY_LEN), page);
130                         len = MIFARE_CL_PAGE_SIZE;
131                         mifare_l3();
132                         if (mifare_cl_auth(key, page) < 0)
133                                 exit(1);
134                         rc = rfid_protocol_read(ph, page, buf, &len);
135                         if (rc < 0) {
136                                 printf("\n");
137                                 fprintf(stderr, "error during read\n");
138                                 break;
139                         }
140                         printf("%s\n", hexdump(buf, len));
141
142                         if (page & 0x3 == 0x3) {
143                                 struct mfcl_access_sect s;
144                                 struct mfcl_access_exp_sect es;
145                                 int b;
146                                 u_int8_t recreated[4];
147                                 mfcl_parse_access(&s, buf+6);
148                                 printf("access b0:%u b1:%u b2:%u b3:%u\n",
149                                         s.block[0], s.block[1],
150                                         s.block[2], s.block[3]);
151                                 mfcl_access_to_exp(&es, &s);
152                                 for (b = 0; b < 3; b++)
153                                         printf("%u: %s\n", b, mfcl_access_exp_stringify(&es.block[b]));
154                                 printf("3: %s\n", mfcl_access_exp_acc_stringify(&es.acc));
155 #if 0
156                                 mfcl_compile_access(recreated, &s);
157                                 printf("recreated; %s\n", hexdump(recreated,4));
158 #endif
159                         }
160                         break;
161                 case 'l':
162                         page = atoi(optarg);
163                         printf("read_loop(key='%s',page=%u):\n",
164                                 hexdump(key, MIFARE_CL_KEY_LEN), page);
165                         while (1) {
166                                 mifare_l3();
167                                 if (mifare_cl_auth(key, page) < 0)
168                                         continue;
169                                 rc = rfid_protocol_read(ph, page, buf, &len);
170                                 if (rc < 0) {
171                                         printf("\n");
172                                         fprintf(stderr, "error during read\n");
173                                         continue;
174                                 }
175                                 printf("%s\n", hexdump(buf, len));
176                         }
177                         break;
178                 case 'w':
179                         page = atoi(optarg);
180                         len = strlen(argv[optind]);
181                         len = hexread(buf, argv[optind], len);
182                         printf("write(key='%s',page=%u):",
183                                 hexdump(key, MIFARE_CL_KEY_LEN), page);
184                         printf(" '%s'(%u):", hexdump(buf, len), len);
185                         mifare_l3();
186                         if (mifare_cl_auth(key, page) < 0)
187                                 exit(1);
188                         rc = rfid_protocol_write(ph, page, buf, len); 
189                         if (rc < 0) {
190                                 printf("\n");
191                                 fprintf(stderr, "error during write\n");
192                                 break;
193                         }
194                         printf("success\n");
195                         break;
196                 case 'h':
197                 default:
198                         help();
199                 }
200         }
201
202 #if 0
203         rfid_protocol_close(ph);
204         rfid_protocol_fini(ph);
205
206         rfid_layer2_close(l2h);
207         rfid_layer2_fini(l2h);
208 #endif
209         rfid_reader_close(rh);
210         exit(0);
211 }
212