support lower and upper case hex chars
[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., 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/rfid_access_mifare_classic.h>
42
43 #include "librfid-tool.h"
44
45 static char *program_name;
46
47 static void help(void)
48 {
49         printf( " -h    --help          Print this help message\n"
50                 " -r    --read          Read a mifare sector\n"
51                 " -l    --loop-read     Loop reading a mifare sector\n"
52                 " -w    --write         Write a mifare sector\n"
53                 " -k    --key           Specify mifare access key (in hex tuples)\n"
54                 " -b    --brute-force n Brute Force read sector n\n");
55 }
56
57 static struct option mifare_opts[] = {
58         { "key", 1, 0, 'k' },
59         { "read", 1, 0, 'r' },
60         { "loop-read", 1, 0, 'l' },
61         { "write", 1 ,0, 'w' },
62         { "help", 0, 0, 'h' },
63         { "brute-force", 1, 0, 'b' },
64         { 0, 0, 0, 0 }
65 };
66
67 static int mifare_cl_auth(unsigned char *key, int page)
68 {
69         int rc;
70
71         rc = mfcl_set_key(ph, key);
72         if (rc < 0) {
73                 fprintf(stderr, "key format error\n");
74                 return rc;
75         }
76         rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, page);
77         if (rc < 0) {
78                 fprintf(stderr, "mifare auth error\n");
79                 return rc;
80         } else 
81                 printf("mifare auth succeeded!\n");
82         
83         return 0;
84 }
85
86 static void mifare_l3(void)
87 {
88         while (l2_init(RFID_LAYER2_ISO14443A) < 0) ;
89
90         printf("ISO14443-3A anticollision succeeded\n");
91
92         while (l3_init(RFID_PROTOCOL_MIFARE_CLASSIC) < 0) ;
93
94         printf("Mifare card available\n");
95 }
96
97 int main(int argc, char **argv)
98 {
99         int len, rc, c, option_index = 0;
100         unsigned int page,uid,uid_len;
101         char key[MIFARE_CL_KEY_LEN];
102         char buf[MIFARE_CL_PAGE_SIZE];
103
104 #ifdef  __MINGW32__
105         program_name = argv[0];
106 #else
107         program_name = basename(argv[0]);
108 #endif/*__MINGW32__*/
109
110         memcpy(key, MIFARE_CL_KEYA_DEFAULT_INFINEON, MIFARE_CL_KEY_LEN);
111
112         printf("%s - (C) 2006 by Harald Welte\n"
113                "This program is Free Software and has "
114                "ABSOLUTELY NO WARRANTY\n\n", program_name);
115
116         printf("initializing librfid\n");
117         rfid_init();
118
119         if (reader_init() < 0) {
120                 fprintf(stderr, "error opening reader\n");
121                 exit(1);
122         }
123
124         while (1) {
125                 c = getopt_long(argc, argv, "k:r:l:w:b:h", mifare_opts,
126                                 &option_index);
127                 if (c == -1)
128                         break;
129
130                 switch (c) {
131                         int i;
132                 case 'b':
133                         page = atoi(optarg);
134                         printf("key: %s\n", hexdump(key, MIFARE_CL_KEY_LEN));
135                         len = MIFARE_CL_PAGE_SIZE;
136                         mifare_l3();
137                         for (i = 0; i <= 0xff; i++) {
138                                 key[MIFARE_CL_KEY_LEN-1]=i;
139                                 if (mifare_cl_auth(key, page) >= 0)
140                                         printf("KEY: %s\n",hexdump(key, MIFARE_CL_KEY_LEN));
141                         }
142
143                         break;
144                 case 'k':
145                         hexread(key, optarg, strlen(optarg));
146                         printf("key: %s\n", hexdump(key, MIFARE_CL_KEY_LEN));
147                         break;
148                 case 'r':
149                         page = atoi(optarg);
150                         printf("read(key='%s',page=%u):",
151                                 hexdump(key, MIFARE_CL_KEY_LEN), page);
152                         len = MIFARE_CL_PAGE_SIZE;
153                         mifare_l3();
154                         if (mifare_cl_auth(key, page) < 0)
155                                 exit(1);
156
157                         uid_len=sizeof(uid);
158                         uid=0;
159                         if(rfid_layer2_getopt(l2h,RFID_OPT_LAYER2_UID,&uid,&uid_len)>=0)
160                             printf("UID=%08X (len=%u)\n",uid,uid_len);
161                                 
162                         len=MIFARE_CL_PAGE_SIZE;                                                                                                                                                                                                
163                         rc = rfid_protocol_read(ph, page, buf, &len);
164                         if (rc < 0) {
165                                 printf("\n");
166                                 fprintf(stderr, "error during read\n");
167                                 break;
168                         }
169                         printf("len=%u data=%s\n", len, hexdump(buf, len));
170
171                         if (page & 0x3 == 0x3) {
172                                 struct mfcl_access_sect s;
173                                 struct mfcl_access_exp_sect es;
174                                 int b;
175                                 u_int8_t recreated[4];
176                                 mfcl_parse_access(&s, buf+6);
177                                 printf("access b0:%u b1:%u b2:%u b3:%u\n",
178                                         s.block[0], s.block[1],
179                                         s.block[2], s.block[3]);
180                                 mfcl_access_to_exp(&es, &s);
181                                 for (b = 0; b < 3; b++)
182                                         printf("%u: %s\n", b, mfcl_access_exp_stringify(&es.block[b]));
183                                 printf("3: %s\n", mfcl_access_exp_acc_stringify(&es.acc));
184 #if 0
185                                 mfcl_compile_access(recreated, &s);
186                                 printf("recreated; %s\n", hexdump(recreated,4));
187 #endif
188                         }
189                         break;
190                 case 'l':
191                         page = atoi(optarg);
192                         printf("read_loop(key='%s',page=%u):\n",
193                                 hexdump(key, MIFARE_CL_KEY_LEN), page);
194                         while (1) {
195                                 mifare_l3();
196                                 if (mifare_cl_auth(key, page) < 0)
197                                         continue;
198
199                                 uid_len=sizeof(uid);
200                                 uid=0;
201                                 if(rfid_layer2_getopt(l2h,RFID_OPT_LAYER2_UID,&uid,&uid_len)>=0)
202                                     printf("UID=%08X (len=%u)\n",uid,uid_len);
203
204                                 len=MIFARE_CL_PAGE_SIZE;                                                                                                                                                                                                
205                                 rc = rfid_protocol_read(ph, page, buf, &len);
206                                 if (rc < 0) {
207                                         printf("\n");
208                                         fprintf(stderr, "error during read\n");
209                                         continue;
210                                 }
211                                 printf("%s\n", hexdump(buf, len));
212                         }
213                         break;
214                 case 'w':
215                         page = atoi(optarg);
216                         len = strlen(argv[optind]);
217                         len = hexread(buf, argv[optind], len);
218                         printf("write(key='%s',page=%u):",
219                                 hexdump(key, MIFARE_CL_KEY_LEN), page);
220                         printf(" '%s'(%u):", hexdump(buf, len), len);
221                         mifare_l3();
222                         if (mifare_cl_auth(key, page) < 0)
223                                 exit(1);
224                         rc = rfid_protocol_write(ph, page, buf, len); 
225                         if (rc < 0) {
226                                 printf("\n");
227                                 fprintf(stderr, "error during write\n");
228                                 break;
229                         }
230                         printf("success\n");
231                         break;
232                 case 'h':
233                 default:
234                         help();
235                 }
236         }
237
238 #if 0
239         rfid_protocol_close(ph);
240         rfid_protocol_fini(ph);
241
242         rfid_layer2_close(l2h);
243         rfid_layer2_fini(l2h);
244 #endif
245         rfid_reader_close(rh);
246         exit(0);
247 }
248