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
25 #include <librfid/rfid.h>
26 #include <librfid/rfid_reader.h>
27 #include <librfid/rfid_layer2.h>
28 #include <librfid/rfid_protocol.h>
30 #include <librfid/rfid_protocol_mifare_classic.h>
31 #include <librfid/rfid_protocol_mifare_ul.h>
33 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
36 hexdump(const void *data, unsigned int len)
38 static char string[1024];
39 unsigned char *d = (unsigned char *) data;
43 left = sizeof(string);
44 for (i = 0; len--; i += 3) {
45 if (i >= sizeof(string) -4)
47 snprintf(string+i, 4, " %02x", *d++);
52 static struct rfid_reader_handle *rh;
53 static struct rfid_layer2_handle *l2h;
54 static struct rfid_protocol_handle *ph;
58 unsigned char buf[0x3f];
61 printf("initializing librfid\n");
64 printf("opening reader handle\n");
65 rh = rfid_reader_open(NULL, RFID_READER_CM5121);
67 fprintf(stderr, "No Omnikey Cardman 5121 found\n");
68 rh = rfid_reader_open(NULL, RFID_READER_OPENPCD);
70 fprintf(stderr, "No OpenPCD found either\n");
75 printf("opening layer2 handle\n");
76 l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443A);
77 //l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443B);
79 fprintf(stderr, "error during iso14443a_init\n");
83 //rc632_register_dump(rh->ah, buf);
85 printf("running layer2 anticol\n");
86 rc = rfid_layer2_open(l2h);
88 fprintf(stderr, "error during layer2_open\n");
95 static int l3(int protocol)
97 printf("running layer3 (ats)\n");
98 ph = rfid_protocol_init(l2h, protocol);
100 fprintf(stderr, "error during protocol_init\n");
103 if (rfid_protocol_open(ph) < 0) {
104 fprintf(stderr, "error during protocol_open\n");
108 printf("we now have layer3 up and running\n");
113 static int select_mf(void)
115 unsigned char cmd[] = { 0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x00 };
116 unsigned char ret[256];
117 unsigned int rlen = sizeof(ret);
121 rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
125 printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
131 static int iso7816_get_challenge(unsigned char len)
133 unsigned char cmd[] = { 0x00, 0x84, 0x00, 0x00, 0x08 };
134 unsigned char ret[256];
135 unsigned int rlen = sizeof(ret);
141 rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
145 printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
151 iso7816_select_application(void)
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);
160 rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
164 /* FIXME: parse response */
165 printf("%s\n", hexdump(resp, rlen));
171 iso7816_select_ef(u_int16_t fid)
173 unsigned char cmd[7] = { 0x00, 0xa4, 0x02, 0x0c, 0x02, 0x00, 0x00 };
174 unsigned char resp[7];
175 unsigned int rlen = sizeof(resp);
179 cmd[5] = (fid >> 8) & 0xff;
182 rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
186 /* FIXME: parse response */
187 printf("%s\n", hexdump(resp, rlen));
193 iso7816_read_binary(unsigned char *buf, unsigned int *len)
195 unsigned char cmd[] = { 0x00, 0xb0, 0x00, 0x00, 0x00 };
196 unsigned char resp[256];
197 unsigned int rlen = sizeof(resp);
201 rv = rfid_protocol_transceive(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
205 printf("%s\n", hexdump(resp, rlen));
207 /* FIXME: parse response, determine whether we need additional reads */
209 /* FIXME: copy 'len' number of response bytes to 'buf' */
213 /* wrapper function around SELECT EF and READ BINARY */
215 iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len)
219 rv = iso7816_select_ef(fid);
223 return iso7816_read_binary(buf, len);
226 /* mifare ultralight helpers */
228 mifare_ulight_write(struct rfid_protocol_handle *ph)
230 unsigned char buf[4] = { 0xa1, 0xa2, 0xa3, 0xa4 };
232 return rfid_protocol_write(ph, 10, buf, 4);
236 mifare_ulight_blank(struct rfid_protocol_handle *ph)
238 unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };
241 for (i = 4; i <= MIFARE_UL_PAGE_MAX; i++) {
242 ret = rfid_protocol_write(ph, i, buf, 4);
250 mifare_ulight_read(struct rfid_protocol_handle *ph)
252 unsigned char buf[20];
253 unsigned int len = sizeof(buf);
257 for (i = 0; i <= MIFARE_UL_PAGE_MAX; i++) {
258 ret = rfid_protocol_read(ph, i, buf, &len);
262 printf("Page 0x%x: %s\n", i, hexdump(buf, 4));
267 /* mifare classic helpers */
269 mifare_classic_read_sector(struct rfid_protocol_handle *ph, int sector)
271 unsigned char buf[20];
272 unsigned int len = sizeof(buf);
276 /* FIXME: make this work for sectors > 31 */
277 printf("reading sector %u\n", sector);
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");
287 printf("Page 0x%x: %s\n", block, hexdump(buf, len));
292 static char *proto_names[] = {
293 [RFID_PROTOCOL_TCL] = "tcl",
294 [RFID_PROTOCOL_MIFARE_UL] = "mifare-ultralight",
295 [RFID_PROTOCOL_MIFARE_CLASSIC] = "mifare-classic",
298 static int proto_by_name(const char *name)
302 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
303 if (proto_names[i] == NULL)
305 if (!strcasecmp(name, proto_names[i]))
311 static void help(void)
313 printf(" -p --protocol {tcl,mifare-ultralight,mifare-classic}\n");
316 static struct option opts[] = {
317 { "help", 0, 0, 'h' },
318 { "protocol", 1, 0, 'p' },
322 int main(int argc, char **argv)
326 int i, protocol = -1;
328 printf("librfid_tool - (C) 2006 by Harald Welte\n"
329 "This program is Free Software and has ABSOLUTELY NO WARRANTY\n\n");
332 int c, option_index = 0;
333 c = getopt_long(argc, argv, "hp:", opts, &option_index);
339 protocol = proto_by_name(optarg);
341 fprintf(stderr, "unknown protocol `%s'\n", optarg);
353 fprintf(stderr, "you have to specify --protocol\n");
360 if (l3(protocol) < 0)
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");
373 printf("error selecting MF\n");
377 printf("Getting random challenge, length 255\n");
378 rc = iso7816_get_challenge(0xff);
380 printf("error getting random challenge\n");
384 printf("selecting Passport application\n");
385 rc = iso7816_select_application();
387 printf("error selecting passport application\n");
391 printf("selecting EF 0x1e\n");
392 rc = iso7816_select_ef(0x011e);
394 printf("error selecting EF 0x1e\n");
398 printf("selecting EF 0x01\n");
399 rc = iso7816_select_ef(0x0101);
401 printf("error selecting EF 0x01\n");
406 printf("reading EF1\n");
408 printf("reading ef\n");
409 rc = iso7816_read_binary(buf, &len);
411 printf("error reading EF\n");
416 for (i = 0; i < 4; i++)
417 iso7816_get_challenge(0xff);
420 case RFID_PROTOCOL_MIFARE_UL:
421 printf("Protocol Mifare Ultralight\n");
422 mifare_ulight_read(ph);
424 mifare_ulight_blank(ph);
425 mifare_ulight_write(ph);
426 mifare_ulight_read(ph);
429 case RFID_PROTOCOL_MIFARE_CLASSIC:
430 printf("Protocol Mifare Classic\n");
433 for (sector = 1; sector < 31; sector++) {
434 rc = mfcl_set_key(ph, MIFARE_CL_KEYA_DEFAULT_INFINEON);
436 printf("key format error\n");
439 rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, sector*4);
441 printf("mifare auth error\n");
444 printf("mifare authe succeeded!\n");
446 mifare_classic_read_sector(ph, sector);
451 printf("unknown protocol\n");
456 rfid_reader_close(rh);