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 <rfid/rfid.h>
22 #include <rfid/rfid_reader.h>
23 #include <rfid/rfid_layer2.h>
24 #include <rfid/rfid_protocol.h>
25 #include <rfid/rfid_reader_cm5121.h>
26 #include <rfid/rfid_protocol_mifare_classic.h>
28 static struct rfid_reader_handle *rh;
29 static struct rfid_layer2_handle *l2h;
30 static struct rfid_protocol_handle *ph;
34 unsigned char buf[0x3f];
37 printf("initializing librfid\n");
40 printf("opening reader handle\n");
41 rh = rfid_reader_open(NULL, RFID_READER_CM5121);
43 fprintf(stderr, "error, no cm5121 handle\n");
49 printf("opening layer2 handle\n");
50 l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443A);
51 //l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443B);
53 fprintf(stderr, "error during iso14443a_init\n");
57 //rc632_register_dump(rh->ah, buf);
59 printf("running layer2 anticol\n");
60 rc = rfid_layer2_open(l2h);
62 fprintf(stderr, "error during layer2_open\n");
69 static int l3(int protocol)
71 printf("running layer3 (ats)\n");
72 ph = rfid_protocol_init(l2h, protocol);
74 fprintf(stderr, "error during protocol_init\n");
77 if (rfid_protocol_open(ph) < 0) {
78 fprintf(stderr, "error during protocol_open\n");
82 printf("we now have layer3 up and running\n");
87 static int select_mf(void)
89 unsigned char cmd[] = { 0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x00 };
90 unsigned char ret[256];
91 unsigned int rlen = sizeof(ret);
95 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
99 printf("%s\n", rfid_hexdump(ret, rlen));
105 static int iso7816_get_challenge(unsigned char len)
107 unsigned char cmd[] = { 0x00, 0x84, 0x00, 0x00, 0x08 };
108 unsigned char ret[256];
109 unsigned int rlen = sizeof(ret);
115 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
119 printf("%d: [%s]\n", rlen, rfid_hexdump(ret, rlen));
125 iso7816_select_application(void)
127 char cmd[] = { 0x00, 0xa4, 0x04, 0x0c, 0x07,
128 0xa0, 0x00, 0x00, 0x02, 0x47, 0x10, 0x01 };
130 unsigned int rlen = sizeof(resp);
134 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
138 /* FIXME: parse response */
139 printf("%s\n", rfid_hexdump(resp, rlen));
145 iso7816_select_ef(u_int16_t fid)
147 unsigned char cmd[7] = { 0x00, 0xa4, 0x02, 0x0c, 0x02, 0x00, 0x00 };
148 unsigned char resp[7];
149 unsigned int rlen = sizeof(resp);
153 cmd[5] = (fid >> 8) & 0xff;
156 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
160 /* FIXME: parse response */
161 printf("%s\n", rfid_hexdump(resp, rlen));
167 iso7816_read_binary(unsigned char *buf, unsigned int *len)
169 unsigned char cmd[] = { 0x00, 0xb0, 0x00, 0x00, 0x00 };
170 unsigned char resp[256];
171 unsigned int rlen = sizeof(resp);
175 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
179 /* FIXME: parse response, determine whether we need additional reads */
181 /* FIXME: copy 'len' number of response bytes to 'buf' */
185 /* wrapper function around SELECT EF and READ BINARY */
187 iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len)
191 rv = iso7816_select_ef(fid);
195 return iso7816_read_binary(buf, len);
198 /* mifare ultralight helpers */
200 mifare_ulight_write(struct rfid_protocol_handle *ph)
202 unsigned char buf[4] = { 0xa1, 0xa2, 0xa3, 0xa4 };
204 return rfid_protocol_write(ph, 10, buf, 4);
208 mifare_ulight_blank(struct rfid_protocol_handle *ph)
210 unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };
213 for (i = 4; i <= MIFARE_UL_PAGE_MAX; i++) {
214 ret = rfid_protocol_write(ph, i, buf, 4);
222 mifare_ulight_read(struct rfid_protocol_handle *ph)
224 unsigned char buf[20];
225 unsigned int len = sizeof(buf);
229 for (i = 0; i <= MIFARE_UL_PAGE_MAX; i++) {
230 ret = rfid_protocol_read(ph, i, buf, &len);
234 printf("Page 0x%x: %s\n", i, rfid_hexdump(buf, 4));
239 /* mifare classic helpers */
241 mifare_classic_read(struct rfid_protocol_handle *ph)
243 unsigned char buf[20];
244 unsigned int len = sizeof(buf);
248 for (i = 0; i <= MIFARE_CL_PAGE_MAX; i++) {
249 ret = rfid_protocol_read(ph, i, buf, &len);
253 printf("Page 0x%x: %s\n", i, rfid_hexdump(buf, 4));
259 int main(int argc, char **argv)
272 if ( !strcmp (*argv, "--list")) {
274 p = ccid_get_reader_list ();
281 else if ( !strcmp (*argv, "--debug")) {
282 ccid_set_debug_level (ccid_set_debug_level (-1) + 1);
293 //protocol = RFID_PROTOCOL_MIFARE_UL;
294 //protocol = RFID_PROTOCOL_MIFARE_CLASSIC;
295 protocol = RFID_PROTOCOL_TCL;
297 if (l3(protocol) < 0)
301 case RFID_PROTOCOL_TCL:
302 /* we've established T=CL at this point */
305 iso7816_select_application();
306 iso7816_select_ef(0x011e);
307 iso7816_select_ef(0x0101);
309 for (i = 0; i < 4; i++)
310 iso7816_get_challenge(0x60);
313 case RFID_PROTOCOL_MIFARE_UL:
314 mifare_ulight_read(ph);
316 mifare_ulight_blank(ph);
317 mifare_ulight_write(ph);
318 mifare_ulight_read(ph);
321 case RFID_PROTOCOL_MIFARE_CLASSIC:
322 rc = mfcl_set_key(ph, MIFARE_CLASSIC_KEYB_DEFAULT);
324 printf("key format error\n");
327 rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1B, 10);
329 printf("mifare auth error\n");
332 printf("mifare authe succeeded!\n");
333 mifare_classic_read(ph);
337 rfid_reader_close(rh);