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 /* FIXME: parse response, determine whether we need additional reads */
197 /* FIXME: copy 'len' number of response bytes to 'buf' */
201 /* wrapper function around SELECT EF and READ BINARY */
203 iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len)
207 rv = iso7816_select_ef(fid);
211 return iso7816_read_binary(buf, len);
214 /* mifare ultralight helpers */
216 mifare_ulight_write(struct rfid_protocol_handle *ph)
218 unsigned char buf[4] = { 0xa1, 0xa2, 0xa3, 0xa4 };
220 return rfid_protocol_write(ph, 10, buf, 4);
224 mifare_ulight_blank(struct rfid_protocol_handle *ph)
226 unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };
229 for (i = 4; i <= MIFARE_UL_PAGE_MAX; i++) {
230 ret = rfid_protocol_write(ph, i, buf, 4);
238 mifare_ulight_read(struct rfid_protocol_handle *ph)
240 unsigned char buf[20];
241 unsigned int len = sizeof(buf);
245 for (i = 0; i <= MIFARE_UL_PAGE_MAX; i++) {
246 ret = rfid_protocol_read(ph, i, buf, &len);
250 printf("Page 0x%x: %s\n", i, hexdump(buf, 4));
255 /* mifare classic helpers */
257 mifare_classic_read(struct rfid_protocol_handle *ph)
259 unsigned char buf[20];
260 unsigned int len = sizeof(buf);
264 for (i = 0; i <= MIFARE_CL_PAGE_MAX; i++) {
265 ret = rfid_protocol_read(ph, i, buf, &len);
269 printf("Page 0x%x: %s\n", i, hexdump(buf, len));
275 int main(int argc, char **argv)
288 if ( !strcmp (*argv, "--list")) {
290 p = ccid_get_reader_list ();
297 else if ( !strcmp (*argv, "--debug")) {
298 ccid_set_debug_level (ccid_set_debug_level (-1) + 1);
309 //protocol = RFID_PROTOCOL_MIFARE_UL;
310 //protocol = RFID_PROTOCOL_MIFARE_CLASSIC;
311 protocol = RFID_PROTOCOL_TCL;
313 if (l3(protocol) < 0)
317 case RFID_PROTOCOL_TCL:
318 /* we've established T=CL at this point */
321 iso7816_select_application();
322 iso7816_select_ef(0x011e);
323 iso7816_select_ef(0x0101);
325 for (i = 0; i < 4; i++)
326 iso7816_get_challenge(0x06);
329 case RFID_PROTOCOL_MIFARE_UL:
330 mifare_ulight_read(ph);
332 mifare_ulight_blank(ph);
333 mifare_ulight_write(ph);
334 mifare_ulight_read(ph);
337 case RFID_PROTOCOL_MIFARE_CLASSIC:
338 rc = mfcl_set_key(ph, MIFARE_CL_KEYA_DEFAULT_INFINEON);
340 printf("key format error\n");
343 rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, 0);
345 printf("mifare auth error\n");
348 printf("mifare authe succeeded!\n");
349 mifare_classic_read(ph);
353 rfid_reader_close(rh);