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>
29 static struct rfid_reader_handle *rh;
30 static struct rfid_layer2_handle *l2h;
31 static struct rfid_protocol_handle *ph;
35 unsigned char buf[0x3f];
38 printf("initializing librfid\n");
41 printf("opening reader handle\n");
42 rh = rfid_reader_open(NULL, RFID_READER_CM5121);
44 fprintf(stderr, "error, no cm5121 handle\n");
50 printf("opening layer2 handle\n");
51 l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443A);
52 //l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443B);
54 fprintf(stderr, "error during iso14443a_init\n");
58 //rc632_register_dump(rh->ah, buf);
60 printf("running layer2 anticol\n");
61 rc = rfid_layer2_open(l2h);
63 fprintf(stderr, "error during layer2_open\n");
70 static int l3(int protocol)
72 printf("running layer3 (ats)\n");
73 ph = rfid_protocol_init(l2h, protocol);
75 fprintf(stderr, "error during protocol_init\n");
78 if (rfid_protocol_open(ph) < 0) {
79 fprintf(stderr, "error during protocol_open\n");
83 printf("we now have layer3 up and running\n");
88 static int select_mf(void)
90 unsigned char cmd[] = { 0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x00 };
91 unsigned char ret[256];
92 unsigned int rlen = sizeof(ret);
96 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
100 printf("%s\n", rfid_hexdump(ret, rlen));
106 static int iso7816_get_challenge(unsigned char len)
108 unsigned char cmd[] = { 0x00, 0x84, 0x00, 0x00, 0x08 };
109 unsigned char ret[256];
110 unsigned int rlen = sizeof(ret);
116 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
120 printf("%d: [%s]\n", rlen, rfid_hexdump(ret, rlen));
126 iso7816_select_application(void)
128 unsigned char cmd[] = { 0x00, 0xa4, 0x04, 0x0c, 0x07,
129 0xa0, 0x00, 0x00, 0x02, 0x47, 0x10, 0x01 };
130 unsigned char resp[7];
131 unsigned int rlen = sizeof(resp);
135 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
139 /* FIXME: parse response */
140 printf("%s\n", rfid_hexdump(resp, rlen));
146 iso7816_select_ef(u_int16_t fid)
148 unsigned char cmd[7] = { 0x00, 0xa4, 0x02, 0x0c, 0x02, 0x00, 0x00 };
149 unsigned char resp[7];
150 unsigned int rlen = sizeof(resp);
154 cmd[5] = (fid >> 8) & 0xff;
157 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
161 /* FIXME: parse response */
162 printf("%s\n", rfid_hexdump(resp, rlen));
168 iso7816_read_binary(unsigned char *buf, unsigned int *len)
170 unsigned char cmd[] = { 0x00, 0xb0, 0x00, 0x00, 0x00 };
171 unsigned char resp[256];
172 unsigned int rlen = sizeof(resp);
176 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
180 /* FIXME: parse response, determine whether we need additional reads */
182 /* FIXME: copy 'len' number of response bytes to 'buf' */
186 /* wrapper function around SELECT EF and READ BINARY */
188 iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len)
192 rv = iso7816_select_ef(fid);
196 return iso7816_read_binary(buf, len);
199 /* mifare ultralight helpers */
201 mifare_ulight_write(struct rfid_protocol_handle *ph)
203 unsigned char buf[4] = { 0xa1, 0xa2, 0xa3, 0xa4 };
205 return rfid_protocol_write(ph, 10, buf, 4);
209 mifare_ulight_blank(struct rfid_protocol_handle *ph)
211 unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };
214 for (i = 4; i <= MIFARE_UL_PAGE_MAX; i++) {
215 ret = rfid_protocol_write(ph, i, buf, 4);
223 mifare_ulight_read(struct rfid_protocol_handle *ph)
225 unsigned char buf[20];
226 unsigned int len = sizeof(buf);
230 for (i = 0; i <= MIFARE_UL_PAGE_MAX; i++) {
231 ret = rfid_protocol_read(ph, i, buf, &len);
235 printf("Page 0x%x: %s\n", i, rfid_hexdump(buf, 4));
240 /* mifare classic helpers */
242 mifare_classic_read(struct rfid_protocol_handle *ph)
244 unsigned char buf[20];
245 unsigned int len = sizeof(buf);
249 for (i = 0; i <= MIFARE_CL_PAGE_MAX; i++) {
250 ret = rfid_protocol_read(ph, i, buf, &len);
254 printf("Page 0x%x: %s\n", i, rfid_hexdump(buf, len));
260 int main(int argc, char **argv)
273 if ( !strcmp (*argv, "--list")) {
275 p = ccid_get_reader_list ();
282 else if ( !strcmp (*argv, "--debug")) {
283 ccid_set_debug_level (ccid_set_debug_level (-1) + 1);
294 //protocol = RFID_PROTOCOL_MIFARE_UL;
295 //protocol = RFID_PROTOCOL_MIFARE_CLASSIC;
296 protocol = RFID_PROTOCOL_TCL;
298 if (l3(protocol) < 0)
302 case RFID_PROTOCOL_TCL:
303 /* we've established T=CL at this point */
306 iso7816_select_application();
307 iso7816_select_ef(0x011e);
308 iso7816_select_ef(0x0101);
310 for (i = 0; i < 4; i++)
311 iso7816_get_challenge(0x06);
314 case RFID_PROTOCOL_MIFARE_UL:
315 mifare_ulight_read(ph);
317 mifare_ulight_blank(ph);
318 mifare_ulight_write(ph);
319 mifare_ulight_read(ph);
322 case RFID_PROTOCOL_MIFARE_CLASSIC:
323 rc = mfcl_set_key(ph, MIFARE_CL_KEYA_DEFAULT_INFINEON);
325 printf("key format error\n");
328 rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, 0);
330 printf("mifare auth error\n");
333 printf("mifare authe succeeded!\n");
334 mifare_classic_read(ph);
338 rfid_reader_close(rh);