Add the OpenPCD USB device ID to the udev rule file (Uwe Hermann)
[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, 'c' },
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 static void inc_key(char* key, int len)
98 {
99         int i;
100
101         if (len <= 0)
102                 return;
103         i = len - 1;
104         if (key[i] < 0xff)
105                 key[i]++;
106         else
107                 key[i] = 0;
108 }
109
110 int main(int argc, char **argv)
111 {
112         int len, rc, c, option_index = 0;
113         unsigned int page,uid,uid_len;
114         char key[MIFARE_CL_KEY_LEN];
115         char buf[MIFARE_CL_PAGE_SIZE];
116
117 #ifdef  __MINGW32__
118         program_name = argv[0];
119 #else
120         program_name = basename(argv[0]);
121 #endif/*__MINGW32__*/
122
123         memcpy(key, MIFARE_CL_KEYA_DEFAULT_INFINEON, MIFARE_CL_KEY_LEN);
124
125         printf("%s - (C) 2006 by Harald Welte\n"
126                "This program is Free Software and has "
127                "ABSOLUTELY NO WARRANTY\n\n", program_name);
128
129         printf("initializing librfid\n");
130         rfid_init();
131
132         if (reader_init() < 0) {
133                 fprintf(stderr, "error opening reader\n");
134                 exit(1);
135         }
136
137         while (1) {
138                 c = getopt_long(argc, argv, "k:r:l:w:c:", mifare_opts,
139                                 &option_index);
140                 if (c == -1)
141                         break;
142
143                 switch (c) {
144                         int i;
145                 case 'c':
146                         page = atoi(optarg);
147                         printf("key: %s\n", hexdump(key, MIFARE_CL_KEY_LEN));
148                         len = MIFARE_CL_PAGE_SIZE;
149                         mifare_l3();
150                         for (i = 0; i <= 0xff; i++) {
151                                 key[MIFARE_CL_KEY_LEN-1]=i;
152                                 if (mifare_cl_auth(key, page) >= 0)
153                                         printf("KEY: %s\n",hexdump(key, MIFARE_CL_KEY_LEN));
154                         }
155
156                         break;
157                 case 'k':
158                         hexread(key, optarg, strlen(optarg));
159                         printf("key: %s\n", hexdump(key, MIFARE_CL_KEY_LEN));
160                         break;
161                 case 'r':
162                         page = atoi(optarg);
163                         printf("read(key='%s',page=%u):",
164                                 hexdump(key, MIFARE_CL_KEY_LEN), page);
165                         len = MIFARE_CL_PAGE_SIZE;
166                         mifare_l3();
167                         if (mifare_cl_auth(key, page) < 0)
168                                 exit(1);
169
170                         uid_len=sizeof(uid);
171                         uid=0;
172                         if(rfid_layer2_getopt(l2h,RFID_OPT_LAYER2_UID,&uid,&uid_len)>=0)
173                             printf("UID=%08X (len=%u)\n",uid,uid_len);
174                                 
175                         len=MIFARE_CL_PAGE_SIZE;                                                                                                                                                                                                
176                         rc = rfid_protocol_read(ph, page, buf, &len);
177                         if (rc < 0) {
178                                 printf("\n");
179                                 fprintf(stderr, "error during read\n");
180                                 break;
181                         }
182                         printf("len=%u data=%s\n", len, hexdump(buf, len));
183
184                         if (page & 0x3 == 0x3) {
185                                 struct mfcl_access_sect s;
186                                 struct mfcl_access_exp_sect es;
187                                 int b;
188                                 u_int8_t recreated[4];
189                                 mfcl_parse_access(&s, buf+6);
190                                 printf("access b0:%u b1:%u b2:%u b3:%u\n",
191                                         s.block[0], s.block[1],
192                                         s.block[2], s.block[3]);
193                                 mfcl_access_to_exp(&es, &s);
194                                 for (b = 0; b < 3; b++)
195                                         printf("%u: %s\n", b, mfcl_access_exp_stringify(&es.block[b]));
196                                 printf("3: %s\n", mfcl_access_exp_acc_stringify(&es.acc));
197 #if 0
198                                 mfcl_compile_access(recreated, &s);
199                                 printf("recreated; %s\n", hexdump(recreated,4));
200 #endif
201                         }
202                         break;
203                 case 'l':
204                         page = atoi(optarg);
205                         printf("read_loop(key='%s',page=%u):\n",
206                                 hexdump(key, MIFARE_CL_KEY_LEN), page);
207                         while (1) {
208                                 mifare_l3();
209                                 if (mifare_cl_auth(key, page) < 0)
210                                         continue;
211
212                                 uid_len=sizeof(uid);
213                                 uid=0;
214                                 if(rfid_layer2_getopt(l2h,RFID_OPT_LAYER2_UID,&uid,&uid_len)>=0)
215                                     printf("UID=%08X (len=%u)\n",uid,uid_len);
216
217                                 len=MIFARE_CL_PAGE_SIZE;                                                                                                                                                                                                
218                                 rc = rfid_protocol_read(ph, page, buf, &len);
219                                 if (rc < 0) {
220                                         printf("\n");
221                                         fprintf(stderr, "error during read\n");
222                                         continue;
223                                 }
224                                 printf("%s\n", hexdump(buf, len));
225                         }
226                         break;
227                 case 'w':
228                         page = atoi(optarg);
229                         len = strlen(argv[optind]);
230                         len = hexread(buf, argv[optind], len);
231                         printf("write(key='%s',page=%u):",
232                                 hexdump(key, MIFARE_CL_KEY_LEN), page);
233                         printf(" '%s'(%u):", hexdump(buf, len), len);
234                         mifare_l3();
235                         if (mifare_cl_auth(key, page) < 0)
236                                 exit(1);
237                         rc = rfid_protocol_write(ph, page, buf, len); 
238                         if (rc < 0) {
239                                 printf("\n");
240                                 fprintf(stderr, "error during write\n");
241                                 break;
242                         }
243                         printf("success\n");
244                         break;
245                 case 'h':
246                 default:
247                         help();
248                 }
249         }
250
251 #if 0
252         rfid_protocol_close(ph);
253         rfid_protocol_fini(ph);
254
255         rfid_layer2_close(l2h);
256         rfid_layer2_fini(l2h);
257 #endif
258         rfid_reader_close(rh);
259         exit(0);
260 }
261