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
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.
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
24 #include <librfid/rfid.h>
25 #include <librfid/rfid_reader.h>
26 #include <librfid/rfid_layer2.h>
27 #include <librfid/rfid_protocol.h>
29 #include <librfid/rfid_protocol_mifare_classic.h>
30 #include <librfid/rfid_protocol_mifare_ul.h>
32 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
35 hexdump(const void *data, unsigned int len)
37 static char string[1024];
38 unsigned char *d = (unsigned char *) data;
42 left = sizeof(string);
43 for (i = 0; len--; i += 3) {
44 if (i >= sizeof(string) -4)
46 snprintf(string+i, 4, " %02x", *d++);
51 static struct rfid_reader_handle *rh;
52 static struct rfid_layer2_handle *l2h;
53 static struct rfid_protocol_handle *ph;
57 unsigned char buf[0x3f];
60 printf("initializing librfid\n");
63 printf("opening reader handle\n");
64 rh = rfid_reader_open(NULL, RFID_READER_CM5121);
66 fprintf(stderr, "error, no cm5121 handle\n");
70 printf("opening layer2 handle\n");
71 l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443A);
72 //l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443B);
74 fprintf(stderr, "error during iso14443a_init\n");
78 //rc632_register_dump(rh->ah, buf);
80 printf("running layer2 anticol\n");
81 rc = rfid_layer2_open(l2h);
83 fprintf(stderr, "error during layer2_open\n");
90 static int l3(int protocol)
92 printf("running layer3 (ats)\n");
93 ph = rfid_protocol_init(l2h, protocol);
95 fprintf(stderr, "error during protocol_init\n");
98 if (rfid_protocol_open(ph) < 0) {
99 fprintf(stderr, "error during protocol_open\n");
103 printf("we now have layer3 up and running\n");
108 static int select_mf(void)
110 unsigned char cmd[] = { 0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x00 };
111 unsigned char ret[256];
112 unsigned int rlen = sizeof(ret);
116 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
120 printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
126 static int iso7816_get_challenge(unsigned char len)
128 unsigned char cmd[] = { 0x00, 0x84, 0x00, 0x00, 0x08 };
129 unsigned char ret[256];
130 unsigned int rlen = sizeof(ret);
136 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
140 printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
146 iso7816_select_application(void)
148 unsigned char cmd[] = { 0x00, 0xa4, 0x04, 0x0c, 0x07,
149 0xa0, 0x00, 0x00, 0x02, 0x47, 0x10, 0x01 };
150 unsigned char resp[7];
151 unsigned int rlen = sizeof(resp);
155 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
159 /* FIXME: parse response */
160 printf("%s\n", hexdump(resp, rlen));
166 iso7816_select_ef(u_int16_t fid)
168 unsigned char cmd[7] = { 0x00, 0xa4, 0x02, 0x0c, 0x02, 0x00, 0x00 };
169 unsigned char resp[7];
170 unsigned int rlen = sizeof(resp);
174 cmd[5] = (fid >> 8) & 0xff;
177 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
181 /* FIXME: parse response */
182 printf("%s\n", hexdump(resp, rlen));
188 iso7816_read_binary(unsigned char *buf, unsigned int *len)
190 unsigned char cmd[] = { 0x00, 0xb0, 0x00, 0x00, 0x00 };
191 unsigned char resp[256];
192 unsigned int rlen = sizeof(resp);
196 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
200 printf("%s\n", hexdump(resp, rlen));
202 /* FIXME: parse response, determine whether we need additional reads */
204 /* FIXME: copy 'len' number of response bytes to 'buf' */
208 /* wrapper function around SELECT EF and READ BINARY */
210 iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len)
214 rv = iso7816_select_ef(fid);
218 return iso7816_read_binary(buf, len);
221 /* mifare ultralight helpers */
223 mifare_ulight_write(struct rfid_protocol_handle *ph)
225 unsigned char buf[4] = { 0xa1, 0xa2, 0xa3, 0xa4 };
227 return rfid_protocol_write(ph, 10, buf, 4);
231 mifare_ulight_blank(struct rfid_protocol_handle *ph)
233 unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };
236 for (i = 4; i <= MIFARE_UL_PAGE_MAX; i++) {
237 ret = rfid_protocol_write(ph, i, buf, 4);
245 mifare_ulight_read(struct rfid_protocol_handle *ph)
247 unsigned char buf[20];
248 unsigned int len = sizeof(buf);
252 for (i = 0; i <= MIFARE_UL_PAGE_MAX; i++) {
253 ret = rfid_protocol_read(ph, i, buf, &len);
257 printf("Page 0x%x: %s\n", i, hexdump(buf, 4));
262 /* mifare classic helpers */
264 mifare_classic_read_sector(struct rfid_protocol_handle *ph, int sector)
266 unsigned char buf[20];
267 unsigned int len = sizeof(buf);
271 /* FIXME: make this work for sectors > 31 */
272 printf("reading sector %u\n", sector);
274 for (block = sector*4; block < sector*4+4; block++) {
275 printf("reading block %u\n", block);
276 ret = rfid_protocol_read(ph, block, buf, &len);
280 printf("Page 0x%x: %s\n", block, hexdump(buf, len));
285 static char *proto_names[] = {
286 [RFID_PROTOCOL_TCL] = "tcl",
287 [RFID_PROTOCOL_MIFARE_UL] = "mifare-ultralight",
288 [RFID_PROTOCOL_MIFARE_CLASSIC] = "mifare-classic",
291 static int proto_by_name(const char *name)
295 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
296 if (proto_names[i] == NULL)
298 if (!strcasecmp(name, proto_names[i]))
304 static void help(void)
306 printf(" -p --protocol {tcl,mifare-ultralight,mifare-classic}\n");
309 static struct option opts[] = {
310 { "help", 0, 0, 'h' },
311 { "protocol", 1, 0, 'p' },
315 int main(int argc, char **argv)
319 int i, protocol = -1;
321 printf("librfid_tool - (C) 2006 by Harald Welte\n"
322 "This program is Free Software and has ABSOLUTELY NO WARRANTY\n\n");
325 int c, option_index = 0;
326 c = getopt_long(argc, argv, "hp:", opts, &option_index);
332 protocol = proto_by_name(optarg);
334 fprintf(stderr, "unknown protocol `%s'\n", optarg);
346 fprintf(stderr, "you have to specify --protocol\n");
353 if (l3(protocol) < 0)
360 case RFID_PROTOCOL_TCL:
361 printf("Protocol T=CL\n");
362 /* we've established T=CL at this point */
363 printf("selecting Master File\n");
366 printf("Getting random challenge, length 255\n");
367 iso7816_get_challenge(0xff);
369 printf("selecting Passport application\n");
370 iso7816_select_application();
372 printf("selecting EF 0x1e\n");
373 iso7816_select_ef(0x011e);
375 printf("selecting EF 0x01\n");
376 iso7816_select_ef(0x0101);
379 printf("reading EF1\n");
381 printf("reading ef\n");
382 iso7816_read_binary(buf, &len);
385 for (i = 0; i < 4; i++)
386 iso7816_get_challenge(0xff);
389 case RFID_PROTOCOL_MIFARE_UL:
390 printf("Protocol Mifare Ultralight\n");
391 mifare_ulight_read(ph);
393 mifare_ulight_blank(ph);
394 mifare_ulight_write(ph);
395 mifare_ulight_read(ph);
398 case RFID_PROTOCOL_MIFARE_CLASSIC:
399 printf("Protocol Mifare Classic\n");
402 for (sector = 1; sector < 31; sector++) {
403 rc = mfcl_set_key(ph, MIFARE_CL_KEYA_DEFAULT_INFINEON);
405 printf("key format error\n");
408 rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, sector);
410 printf("mifare auth error\n");
413 printf("mifare authe succeeded!\n");
415 mifare_classic_read_sector(ph, sector);
420 printf("unknown protocol\n");
425 rfid_reader_close(rh);