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
21 #include <librfid/rfid.h>
22 #include <librfid/rfid_reader.h>
23 #include <librfid/rfid_layer2.h>
24 #include <librfid/rfid_protocol.h>
26 #include <librfid/rfid_protocol_mifare_classic.h>
27 #include <librfid/rfid_protocol_mifare_ul.h>
30 hexdump(const void *data, unsigned int len)
32 static char string[1024];
33 unsigned char *d = (unsigned char *) data;
37 left = sizeof(string);
38 for (i = 0; len--; i += 3) {
39 if (i >= sizeof(string) -4)
41 snprintf(string+i, 4, " %02x", *d++);
46 static struct rfid_reader_handle *rh;
47 static struct rfid_layer2_handle *l2h;
48 static struct rfid_protocol_handle *ph;
52 unsigned char buf[0x3f];
55 printf("initializing librfid\n");
58 printf("opening reader handle\n");
59 rh = rfid_reader_open(NULL, RFID_READER_CM5121);
61 fprintf(stderr, "error, no cm5121 handle\n");
65 printf("opening layer2 handle\n");
66 l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443A);
67 //l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443B);
69 fprintf(stderr, "error during iso14443a_init\n");
73 //rc632_register_dump(rh->ah, buf);
75 printf("running layer2 anticol\n");
76 rc = rfid_layer2_open(l2h);
78 fprintf(stderr, "error during layer2_open\n");
85 static int l3(int protocol)
87 printf("running layer3 (ats)\n");
88 ph = rfid_protocol_init(l2h, protocol);
90 fprintf(stderr, "error during protocol_init\n");
93 if (rfid_protocol_open(ph) < 0) {
94 fprintf(stderr, "error during protocol_open\n");
98 printf("we now have layer3 up and running\n");
103 static int select_mf(void)
105 unsigned char cmd[] = { 0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x00 };
106 unsigned char ret[256];
107 unsigned int rlen = sizeof(ret);
111 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
115 printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
121 static int iso7816_get_challenge(unsigned char len)
123 unsigned char cmd[] = { 0x00, 0x84, 0x00, 0x00, 0x08 };
124 unsigned char ret[256];
125 unsigned int rlen = sizeof(ret);
131 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
135 printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
141 iso7816_select_application(void)
143 unsigned char cmd[] = { 0x00, 0xa4, 0x04, 0x0c, 0x07,
144 0xa0, 0x00, 0x00, 0x02, 0x47, 0x10, 0x01 };
145 unsigned char resp[7];
146 unsigned int rlen = sizeof(resp);
150 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
154 /* FIXME: parse response */
155 printf("%s\n", hexdump(resp, rlen));
161 iso7816_select_ef(u_int16_t fid)
163 unsigned char cmd[7] = { 0x00, 0xa4, 0x02, 0x0c, 0x02, 0x00, 0x00 };
164 unsigned char resp[7];
165 unsigned int rlen = sizeof(resp);
169 cmd[5] = (fid >> 8) & 0xff;
172 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
176 /* FIXME: parse response */
177 printf("%s\n", hexdump(resp, rlen));
183 iso7816_read_binary(unsigned char *buf, unsigned int *len)
185 unsigned char cmd[] = { 0x00, 0xb0, 0x00, 0x00, 0x00 };
186 unsigned char resp[256];
187 unsigned int rlen = sizeof(resp);
191 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
195 printf("%s\n", hexdump(resp, rlen));
197 /* FIXME: parse response, determine whether we need additional reads */
199 /* FIXME: copy 'len' number of response bytes to 'buf' */
203 /* wrapper function around SELECT EF and READ BINARY */
205 iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len)
209 rv = iso7816_select_ef(fid);
213 return iso7816_read_binary(buf, len);
216 /* mifare ultralight helpers */
218 mifare_ulight_write(struct rfid_protocol_handle *ph)
220 unsigned char buf[4] = { 0xa1, 0xa2, 0xa3, 0xa4 };
222 return rfid_protocol_write(ph, 10, buf, 4);
226 mifare_ulight_blank(struct rfid_protocol_handle *ph)
228 unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };
231 for (i = 4; i <= MIFARE_UL_PAGE_MAX; i++) {
232 ret = rfid_protocol_write(ph, i, buf, 4);
240 mifare_ulight_read(struct rfid_protocol_handle *ph)
242 unsigned char buf[20];
243 unsigned int len = sizeof(buf);
247 for (i = 0; i <= MIFARE_UL_PAGE_MAX; i++) {
248 ret = rfid_protocol_read(ph, i, buf, &len);
252 printf("Page 0x%x: %s\n", i, hexdump(buf, 4));
257 /* mifare classic helpers */
259 mifare_classic_read_sector(struct rfid_protocol_handle *ph, int sector)
261 unsigned char buf[20];
262 unsigned int len = sizeof(buf);
266 /* FIXME: make this work for sectors > 31 */
267 printf("reading sector %u\n", sector);
269 for (block = sector*4; block < sector*4+4; block++) {
270 printf("reading block %u\n", block);
271 ret = rfid_protocol_read(ph, block, buf, &len);
275 printf("Page 0x%x: %s\n", block, hexdump(buf, len));
281 int main(int argc, char **argv)
294 if ( !strcmp (*argv, "--list")) {
296 p = ccid_get_reader_list ();
303 else if ( !strcmp (*argv, "--debug")) {
304 ccid_set_debug_level (ccid_set_debug_level (-1) + 1);
317 //protocol = RFID_PROTOCOL_MIFARE_UL;
318 //protocol = RFID_PROTOCOL_MIFARE_CLASSIC;
319 protocol = RFID_PROTOCOL_TCL;
321 if (l3(protocol) < 0)
328 case RFID_PROTOCOL_TCL:
329 /* we've established T=CL at this point */
332 iso7816_select_application();
333 iso7816_select_ef(0x011e);
334 iso7816_select_ef(0x0101);
338 printf("reading ef\n");
339 iso7816_read_binary(buf, &len);
342 for (i = 0; i < 4; i++)
343 iso7816_get_challenge(0xff);
346 case RFID_PROTOCOL_MIFARE_UL:
347 mifare_ulight_read(ph);
349 mifare_ulight_blank(ph);
350 mifare_ulight_write(ph);
351 mifare_ulight_read(ph);
354 case RFID_PROTOCOL_MIFARE_CLASSIC:
357 for (sector = 1; sector < 31; sector++) {
358 rc = mfcl_set_key(ph, MIFARE_CL_KEYA_DEFAULT_INFINEON);
360 printf("key format error\n");
363 rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, sector);
365 printf("mifare auth error\n");
368 printf("mifare authe succeeded!\n");
370 mifare_classic_read_sector(ph, sector);
376 rfid_reader_close(rh);