1 /* librfid-tool - a small command-line tool for librfid testing
3 * (C) 2005-2006 by Harald Welte <laforge@gnumonks.org>
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
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.
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
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>
38 #include <librfid/rfid_protocol_mifare_classic.h>
39 #include <librfid/rfid_protocol_mifare_ul.h>
41 #include "librfid-tool.h"
44 static int select_mf(void)
46 unsigned char cmd[] = { 0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x00 };
47 unsigned char ret[256];
48 unsigned int rlen = sizeof(ret);
52 rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
56 printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
62 static int iso7816_get_challenge(unsigned char len)
64 unsigned char cmd[] = { 0x00, 0x84, 0x00, 0x00, 0x08 };
65 unsigned char ret[256];
66 unsigned int rlen = sizeof(ret);
72 rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
76 printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
82 iso7816_select_application(void)
84 unsigned char cmd[] = { 0x00, 0xa4, 0x04, 0x0c, 0x07,
85 0xa0, 0x00, 0x00, 0x02, 0x47, 0x10, 0x01 };
86 unsigned char resp[7];
87 unsigned int rlen = sizeof(resp);
91 rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
95 /* FIXME: parse response */
96 printf("%s\n", hexdump(resp, rlen));
102 iso7816_select_ef(u_int16_t fid)
104 unsigned char cmd[7] = { 0x00, 0xa4, 0x02, 0x0c, 0x02, 0x00, 0x00 };
105 unsigned char resp[7];
106 unsigned int rlen = sizeof(resp);
110 cmd[5] = (fid >> 8) & 0xff;
113 rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
117 /* FIXME: parse response */
118 printf("%s\n", hexdump(resp, rlen));
124 iso7816_read_binary(unsigned char *buf, unsigned int *len)
126 unsigned char cmd[] = { 0x00, 0xb0, 0x00, 0x00, 0x00 };
127 unsigned char resp[256];
128 unsigned int rlen = sizeof(resp);
132 rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
136 printf("%s\n", hexdump(resp, rlen));
138 /* FIXME: parse response, determine whether we need additional reads */
140 /* FIXME: copy 'len' number of response bytes to 'buf' */
144 /* wrapper function around SELECT EF and READ BINARY */
146 iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len)
150 rv = iso7816_select_ef(fid);
154 return iso7816_read_binary(buf, len);
157 /* mifare ultralight helpers */
159 mifare_ulight_write(struct rfid_protocol_handle *ph)
161 unsigned char buf[4] = { 0xa1, 0xa2, 0xa3, 0xa4 };
163 return rfid_protocol_write(ph, 10, buf, 4);
167 mifare_ulight_blank(struct rfid_protocol_handle *ph)
169 unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };
172 for (i = 4; i <= MIFARE_UL_PAGE_MAX; i++) {
173 ret = rfid_protocol_write(ph, i, buf, 4);
181 mifare_ulight_read(struct rfid_protocol_handle *ph)
183 unsigned char buf[20];
184 unsigned int len = sizeof(buf);
188 for (i = 0; i <= MIFARE_UL_PAGE_MAX; i++) {
189 ret = rfid_protocol_read(ph, i, buf, &len);
193 printf("Page 0x%x: %s\n", i, hexdump(buf, 4));
198 /* mifare classic helpers */
200 mifare_classic_read_sector(struct rfid_protocol_handle *ph, int sector)
202 unsigned char buf[20];
203 unsigned int len = sizeof(buf);
207 /* FIXME: make this work for sectors > 31 */
208 printf("Reading sector %u\n", sector);
210 for (block = sector*4; block < sector*4+4; block++) {
211 printf("Reading block %u: ", block);
212 ret = rfid_protocol_read(ph, block, buf, &len);
213 if(ret == -ETIMEDOUT)
214 fprintf(stderr, "TIMEOUT\n");
216 printf("Error %d reading\n", ret);
220 printf("Page 0x%x: %s\n", block, hexdump(buf, len));
225 static char *proto_names[] = {
226 [RFID_PROTOCOL_TCL] = "tcl",
227 [RFID_PROTOCOL_MIFARE_UL] = "mifare-ultralight",
228 [RFID_PROTOCOL_MIFARE_CLASSIC] = "mifare-classic",
231 static int proto_by_name(const char *name)
235 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
236 if (proto_names[i] == NULL)
238 if (!strcasecmp(name, proto_names[i]))
244 static char *l2_names[] = {
245 [RFID_LAYER2_ISO14443A] = "iso14443a",
246 [RFID_LAYER2_ISO14443B] = "iso14443b",
247 [RFID_LAYER2_ISO15693] = "iso15693",
250 static int l2_by_name(const char *name)
254 for (i = 0; i < ARRAY_SIZE(l2_names); i++) {
255 if (l2_names[i] == NULL)
257 if (!strcasecmp(name, l2_names[i]))
263 static void do_scan(void)
267 unsigned int size_len = sizeof(size);
269 printf("scanning for RFID token...\n");
270 rc = rfid_scan(rh, &l2h, &ph);
272 unsigned char uid_buf[16];
273 unsigned int uid_len = sizeof(uid_buf);
274 rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_UID, &uid_buf,
276 printf("Layer 2 success (%s): %s\n", rfid_layer2_name(l2h),
277 hexdump(uid_buf, uid_len));
280 printf("Protocol success (%s)\n", rfid_protocol_name(ph));
282 if (rfid_protocol_getopt(ph, RFID_OPT_PROTO_SIZE,
283 &size, &size_len) == 0)
284 printf("Size: %u bytes\n", size);
288 #define OPTION_OFFSET 256
290 static struct option original_opts[] = {
291 { "help", 0, 0, 'h' },
292 { "layer2", 1, 0, 'l' },
293 { "protocol", 1, 0, 'p' },
294 { "scan", 0, 0, 's' },
295 { "scan-loop", 0, 0, 'S' },
299 /* module / option merging code */
300 static struct option *opts = original_opts;
301 static unsigned int global_option_offset = 0;
303 static char *program_name;
304 static char *program_version = LIBRFID_TOOL_VERSION;
306 static void free_opts(int reset_offset)
308 if (opts != original_opts) {
310 opts = original_opts;
312 global_option_offset = 0;
316 static struct option *
317 merge_options(struct option *oldopts, const struct option *newopts,
318 unsigned int *option_offset)
320 unsigned int num_old, num_new, i;
321 struct option *merge;
323 for (num_old = 0; oldopts[num_old].name; num_old++);
324 for (num_new = 0; oldopts[num_new].name; num_new++);
326 global_option_offset += OPTION_OFFSET;
327 *option_offset = global_option_offset;
329 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
330 memcpy(merge, oldopts, num_old * sizeof(struct option));
331 free_opts(0); /* Release previous options merged if any */
332 for (i = 0; i < num_new; i++) {
333 merge[num_old + i] = newopts[i];
334 merge[num_old + i].val += *option_offset;
336 memset(merge + num_old + num_new, 0, sizeof(struct option));
341 struct rfidtool_module *find_module(const char *name)
346 void register_module(struct rfidtool_module *me)
348 struct rfidtool_module *old;
350 if (strcmp(me->version, program_version) != 0) {
351 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
352 program_name, me->name, me->version, program_version);
356 old = find_module(me->name);
358 fprintf(stderr, "%s: target `%s' already registered.\n",
359 program_name, me->name);
364 static void help(void)
366 printf( " -s --scan scan until first RFID tag is found\n"
367 " -S --scan-loop endless scanning loop\n"
368 " -p --protocol {tcl,mifare-ultralight,mifare-classic}\n"
369 " -l --layer2 {iso14443a,iso14443b,iso15693}\n"
373 int main(int argc, char **argv)
377 int i, len, protocol = -1, layer2 = -1;
380 program_name = argv[0];
381 #else /*__MINGW32__*/
382 program_name = basename(argv[0]);
383 #endif/*__MINGW32__*/
385 printf("%s - (C) 2006 by Harald Welte\n"
386 "This program is Free Software and has "
387 "ABSOLUTELY NO WARRANTY\n\n", program_name);
389 printf("initializing librfid\n");
393 int c, option_index = 0;
394 c = getopt_long(argc, argv, "hp:l:sS", opts, &option_index);
400 if (reader_init() < 0)
406 if (reader_init() < 0)
413 protocol = proto_by_name(optarg);
415 fprintf(stderr, "unknown protocol `%s'\n",
421 layer2 = l2_by_name(optarg);
423 fprintf(stderr, "unknown layer2 `%s'\n",
436 case RFID_PROTOCOL_MIFARE_UL:
437 case RFID_PROTOCOL_MIFARE_CLASSIC:
438 layer2 = RFID_LAYER2_ISO14443A;
441 fprintf(stderr, "you have to specify --protocol\n");
446 fprintf(stderr, "you have to specify --layer2\n");
450 if (reader_init() < 0)
453 if (l2_init(layer2) < 0)
456 if (l3_init(protocol) < 0)
461 case RFID_PROTOCOL_TCL:
462 printf("Protocol T=CL\n");
463 /* we've established T=CL at this point */
464 printf("selecting Master File\n");
467 printf("error selecting MF\n");
471 printf("Getting random challenge, length 255\n");
472 rc = iso7816_get_challenge(0xff);
474 printf("error getting random challenge\n");
478 printf("selecting Passport application\n");
479 rc = iso7816_select_application();
481 printf("error selecting passport application\n");
485 printf("selecting EF 0x1e\n");
486 rc = iso7816_select_ef(0x011e);
488 printf("error selecting EF 0x1e\n");
492 printf("selecting EF 0x01\n");
493 rc = iso7816_select_ef(0x0101);
495 printf("error selecting EF 0x01\n");
500 printf("reading EF1\n");
502 printf("reading ef\n");
503 rc = iso7816_read_binary(buf, &len);
505 printf("error reading EF\n");
510 for (i = 0; i < 4; i++)
511 iso7816_get_challenge(0xff);
514 case RFID_PROTOCOL_MIFARE_UL:
515 printf("Protocol Mifare Ultralight\n");
516 mifare_ulight_read(ph);
518 mifare_ulight_blank(ph);
519 mifare_ulight_write(ph);
520 mifare_ulight_read(ph);
523 case RFID_PROTOCOL_MIFARE_CLASSIC:
524 printf("Protocol Mifare Classic\n");
527 for (sector = 0; sector < 31; sector++) {
528 printf("Authenticating sector %u: ", sector);
530 rc = mfcl_set_key(ph, MIFARE_CL_KEYA_DEFAULT_INFINEON);
532 printf("key format error\n");
535 rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, sector*4);
537 printf("mifare auth error\n");
540 printf("mifare auth succeeded!\n");
542 mifare_classic_read_sector(ph, sector);
547 printf("unknown protocol\n");
552 rfid_reader_close(rh);