0c21a4d707d72c149b3b2ab602a41c6d1394f667
[librfid] / utils / librfid-tool.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License version 2 
4  *  as published by the Free Software Foundation
5  *
6  *  This program is distributed in the hope that it will be useful,
7  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
8  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  *  GNU General Public License for more details.
10  *
11  *  You should have received a copy of the GNU General Public License
12  *  along with this program; if not, write to the Free Software
13  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14  */
15
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <errno.h>
21
22 #define _GNU_SOURCE
23 #include <getopt.h>
24
25 #include <librfid/rfid.h>
26 #include <librfid/rfid_reader.h>
27 #include <librfid/rfid_layer2.h>
28 #include <librfid/rfid_protocol.h>
29
30 #include <librfid/rfid_protocol_mifare_classic.h>
31 #include <librfid/rfid_protocol_mifare_ul.h>
32
33 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
34
35 static const char *
36 hexdump(const void *data, unsigned int len)
37 {
38         static char string[1024];
39         unsigned char *d = (unsigned char *) data;
40         unsigned int i, left;
41
42         string[0] = '\0';
43         left = sizeof(string);
44         for (i = 0; len--; i += 3) {
45                 if (i >= sizeof(string) -4)
46                         break;
47                 snprintf(string+i, 4, " %02x", *d++);
48         }
49         return string;
50 }
51
52 static struct rfid_reader_handle *rh;
53 static struct rfid_layer2_handle *l2h;
54 static struct rfid_protocol_handle *ph;
55
56 static int init()
57 {
58         unsigned char buf[0x3f];
59         int rc;
60
61         printf("initializing librfid\n");
62         rfid_init();
63
64         printf("opening reader handle\n");
65         rh = rfid_reader_open(NULL, RFID_READER_CM5121);
66         if (!rh) {
67                 fprintf(stderr, "No Omnikey Cardman 5121 found\n");
68                 rh = rfid_reader_open(NULL, RFID_READER_OPENPCD);
69                 if (!rh) {
70                         fprintf(stderr, "No OpenPCD found either\n");
71                         return -1;
72                 }
73         }
74
75         printf("opening layer2 handle\n");
76         l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443A);
77         //l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443B);
78         if (!l2h) {
79                 fprintf(stderr, "error during iso14443a_init\n");
80                 return -1;
81         }
82
83         //rc632_register_dump(rh->ah, buf);
84
85         printf("running layer2 anticol\n");
86         rc = rfid_layer2_open(l2h);
87         if (rc < 0) {
88                 fprintf(stderr, "error during layer2_open\n");
89                 return rc;
90         }
91
92         return 0;
93 }
94
95 static int l3(int protocol)
96 {
97         printf("running layer3 (ats)\n");
98         ph = rfid_protocol_init(l2h, protocol);
99         if (!ph) {
100                 fprintf(stderr, "error during protocol_init\n");
101                 return -1;
102         }
103         if (rfid_protocol_open(ph) < 0) {
104                 fprintf(stderr, "error during protocol_open\n");
105                 return -1;
106         }
107
108         printf("we now have layer3 up and running\n");
109
110         return 0;
111 }
112
113 static int select_mf(void)
114 {
115         unsigned char cmd[] = { 0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x00 };
116         unsigned char ret[256];
117         unsigned int rlen = sizeof(ret);
118
119         int rv;
120
121         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
122         if (rv < 0)
123                 return rv;
124
125         printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
126
127         return 0;
128 }
129
130
131 static int iso7816_get_challenge(unsigned char len)
132 {
133         unsigned char cmd[] = { 0x00, 0x84, 0x00, 0x00, 0x08 };
134         unsigned char ret[256];
135         unsigned int rlen = sizeof(ret);
136
137         cmd[4] = len;
138
139         int rv;
140
141         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
142         if (rv < 0)
143                 return rv;
144
145         printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
146
147         return 0;
148 }
149
150 int
151 iso7816_select_application(void)
152 {
153         unsigned char cmd[] = { 0x00, 0xa4, 0x04, 0x0c, 0x07,
154                        0xa0, 0x00, 0x00, 0x02, 0x47, 0x10, 0x01 };
155         unsigned char resp[7];
156         unsigned int rlen = sizeof(resp);
157
158         int rv;
159
160         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
161         if (rv < 0)
162                 return rv;
163
164         /* FIXME: parse response */
165         printf("%s\n", hexdump(resp, rlen));
166
167         return 0;
168 }
169
170 int
171 iso7816_select_ef(u_int16_t fid)
172 {
173         unsigned char cmd[7] = { 0x00, 0xa4, 0x02, 0x0c, 0x02, 0x00, 0x00 };
174         unsigned char resp[7];
175         unsigned int rlen = sizeof(resp);
176
177         int rv;
178
179         cmd[5] = (fid >> 8) & 0xff;
180         cmd[6] = fid & 0xff;
181
182         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
183         if (rv < 0)
184                 return rv;
185
186         /* FIXME: parse response */
187         printf("%s\n", hexdump(resp, rlen));
188
189         return 0;
190 }
191
192 int
193 iso7816_read_binary(unsigned char *buf, unsigned int *len)
194 {
195         unsigned char cmd[] = { 0x00, 0xb0, 0x00, 0x00, 0x00 };
196         unsigned char resp[256];
197         unsigned int rlen = sizeof(resp);
198         
199         int rv;
200
201         rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
202         if (rv < 0)
203                 return rv;
204
205         printf("%s\n", hexdump(resp, rlen));
206
207         /* FIXME: parse response, determine whether we need additional reads */
208
209         /* FIXME: copy 'len' number of response bytes to 'buf' */
210         return 0;
211 }
212
213 /* wrapper function around SELECT EF and READ BINARY */
214 int
215 iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len)
216 {
217         int rv;
218
219         rv = iso7816_select_ef(fid);
220         if (rv < 0)
221                 return rv;
222
223         return iso7816_read_binary(buf, len);
224 }
225
226 /* mifare ultralight helpers */
227 int
228 mifare_ulight_write(struct rfid_protocol_handle *ph)
229 {
230         unsigned char buf[4] = { 0xa1, 0xa2, 0xa3, 0xa4 };
231
232         return rfid_protocol_write(ph, 10, buf, 4);
233 }
234
235 int
236 mifare_ulight_blank(struct rfid_protocol_handle *ph)
237 {
238         unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };
239         int i, ret;
240
241         for (i = 4; i <= MIFARE_UL_PAGE_MAX; i++) {
242                 ret = rfid_protocol_write(ph, i, buf, 4);
243                 if (ret < 0)
244                         return ret;
245         }
246         return 0;
247 }
248
249 static int
250 mifare_ulight_read(struct rfid_protocol_handle *ph)
251 {
252         unsigned char buf[20];
253         unsigned int len = sizeof(buf);
254         int ret;
255         int i;
256
257         for (i = 0; i <= MIFARE_UL_PAGE_MAX; i++) {
258                 ret = rfid_protocol_read(ph, i, buf, &len);
259                 if (ret < 0)
260                         return ret;
261
262                 printf("Page 0x%x: %s\n", i, hexdump(buf, 4));
263         }
264         return 0;
265 }
266
267 /* mifare classic helpers */
268 static int
269 mifare_classic_read_sector(struct rfid_protocol_handle *ph, int sector)
270 {
271         unsigned char buf[20];
272         unsigned int len = sizeof(buf);
273         int ret;
274         int block;
275
276         /* FIXME: make this work for sectors > 31 */
277         printf("reading sector %u\n", sector);
278
279         for (block = sector*4; block < sector*4+4; block++) {
280                 printf("reading block %u\n", block);
281                 ret = rfid_protocol_read(ph, block, buf, &len);
282                 if(ret == -ETIMEDOUT)
283                         fprintf(stderr, "TIMEOUT\n");
284                 if (ret < 0)
285                         return ret;
286
287                 printf("Page 0x%x: %s\n", block, hexdump(buf, len));
288         }
289         return 0;
290 }
291
292 static char *proto_names[] = {
293         [RFID_PROTOCOL_TCL] = "tcl",
294         [RFID_PROTOCOL_MIFARE_UL] = "mifare-ultralight",
295         [RFID_PROTOCOL_MIFARE_CLASSIC] = "mifare-classic",
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 void help(void)
312 {
313         printf(" -p     --protocol {tcl,mifare-ultralight,mifare-classic}\n");
314 }
315
316 static struct option opts[] = {
317         { "help", 0, 0, 'h' },
318         { "protocol", 1, 0, 'p' },
319         {0, 0, 0, 0}
320 };
321
322 int main(int argc, char **argv)
323 {
324         int rc;
325         char buf[0x40];
326         int i, protocol = -1;
327         
328         printf("librfid_tool - (C) 2006 by Harald Welte\n"
329                "This program is Free Software and has ABSOLUTELY NO WARRANTY\n\n");
330
331         while (1) {
332                 int c, option_index = 0;
333                 c = getopt_long(argc, argv, "hp:", opts, &option_index);
334                 if (c == -1)
335                         break;
336
337                 switch (c) {
338                 case 'p':
339                         protocol = proto_by_name(optarg);
340                         if (protocol < 0) {
341                                 fprintf(stderr, "unknown protocol `%s'\n", optarg);
342                                 exit(2);
343                         }
344                         break;
345                 case 'h':
346                         help();
347                         exit(0);
348                         break;
349                 }
350         }
351
352         if (protocol < 0) {
353                 fprintf(stderr, "you have to specify --protocol\n");
354                 exit(2);
355         }
356
357         if (init() < 0)
358                 exit(1);
359
360         if (l3(protocol) < 0)
361                 exit(1);
362
363         switch (protocol) {
364                 char buf[32000];
365                 int len = 200;
366
367         case RFID_PROTOCOL_TCL:
368                 printf("Protocol T=CL\n");
369                 /* we've established T=CL at this point */
370                 printf("selecting Master File\n");
371                 rc = select_mf();
372                 if (rc < 0) {
373                         printf("error selecting MF\n");
374                         break;
375                 }
376
377                 printf("Getting random challenge, length 255\n");
378                 rc = iso7816_get_challenge(0xff);
379                 if (rc < 0) {
380                         printf("error getting random challenge\n");
381                         break;
382                 }
383
384                 printf("selecting Passport application\n");
385                 rc = iso7816_select_application();
386                 if (rc < 0) {
387                         printf("error selecting passport application\n");
388                         break;
389                 }
390
391                 printf("selecting EF 0x1e\n");
392                 rc = iso7816_select_ef(0x011e);
393                 if (rc < 0) {
394                         printf("error selecting EF 0x1e\n");
395                         break;
396                 }
397
398                 printf("selecting EF 0x01\n");
399                 rc = iso7816_select_ef(0x0101);
400                 if (rc < 0) {
401                         printf("error selecting EF 0x01\n");
402                         break;
403                 }
404
405                 while (1) {
406                         printf("reading EF1\n");
407                         len = 200;
408                         printf("reading ef\n");
409                         rc = iso7816_read_binary(buf, &len);
410                         if (rc < 0) {
411                                 printf("error reading EF\n");
412                                 break;
413                         }
414                 }
415 #if 0
416                 for (i = 0; i < 4; i++)
417                         iso7816_get_challenge(0xff);
418 #endif
419                 break;
420         case RFID_PROTOCOL_MIFARE_UL:
421                 printf("Protocol Mifare Ultralight\n");
422                 mifare_ulight_read(ph);
423 #if 0
424                 mifare_ulight_blank(ph);
425                 mifare_ulight_write(ph);
426                 mifare_ulight_read(ph);
427 #endif
428                 break;
429         case RFID_PROTOCOL_MIFARE_CLASSIC:
430                 printf("Protocol Mifare Classic\n");
431                 {
432                         int sector;
433                         for (sector = 1; sector < 31; sector++) {
434                                 rc = mfcl_set_key(ph, MIFARE_CL_KEYA_DEFAULT_INFINEON);
435                                 if (rc < 0) {
436                                         printf("key format error\n");
437                                         exit(1);
438                                 }
439                                 rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, sector*4);
440                                 if (rc < 0) {
441                                         printf("mifare auth error\n");
442                                         exit(1);
443                                 } else 
444                                         printf("mifare authe succeeded!\n");
445
446                                 mifare_classic_read_sector(ph, sector);
447                         }
448                 }
449                 break;
450         default:
451                 printf("unknown protocol\n");
452                 exit(1);
453                 break;
454         }
455
456         rfid_reader_close(rh);
457         
458         exit(0);
459 }