3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2
5 * as published by the Free Software Foundation
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <openct/openct.h>
23 #include <rfid/rfid.h>
24 #include <rfid/rfid_reader.h>
25 #include <rfid/rfid_layer2.h>
26 #include <rfid/rfid_protocol.h>
27 #include <rfid/rfid_reader_cm5121.h>
28 #include <rfid/rfid_protocol_mifare_classic.h>
32 static ct_lock_handle lock;
34 static struct rfid_reader_handle *rh;
35 static struct rfid_layer2_handle *l2h;
36 static struct rfid_protocol_handle *ph;
39 /* this is the sole function required by rfid_reader_cm5121.c */
41 PC_to_RDR_Escape(void *handle,
42 const unsigned char *tx_buf, unsigned int tx_len,
43 unsigned char *rx_buf, unsigned int *rx_len)
45 ct_handle *h = (ct_handle *) handle;
48 rc = ct_card_transact(h, 1, tx_buf, tx_len, rx_buf, *rx_len);
61 unsigned char buf[0x3f];
62 unsigned char atr[64];
65 h = ct_reader_connect(0);
69 printf("acquiring card lock\n");
70 rc = ct_card_lock(h, slot, IFD_LOCK_EXCLUSIVE, &lock);
72 fprintf(stderr, "error, no card lock\n");
76 rc = ct_card_reset(h, slot, atr, sizeof(atr));
78 fprintf(stderr, "error, can't reset virtual card\n");
82 printf("initializing librfid\n");
85 printf("opening reader handle\n");
86 rh = rfid_reader_open(h, RFID_READER_CM5121);
88 fprintf(stderr, "error, no cm5121 handle\n");
94 printf("opening layer2 handle\n");
95 l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443A);
96 //l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443B);
98 fprintf(stderr, "error during iso14443a_init\n");
102 //rc632_register_dump(rh->ah, buf);
104 printf("running layer2 anticol\n");
105 rc = rfid_layer2_open(l2h);
107 fprintf(stderr, "error during layer2_open\n");
114 static int l3(int protocol)
116 printf("running layer3 (ats)\n");
117 ph = rfid_protocol_init(l2h, protocol);
119 fprintf(stderr, "error during protocol_init\n");
122 if (rfid_protocol_open(ph) < 0) {
123 fprintf(stderr, "error during protocol_open\n");
127 printf("we now have layer3 up and running\n");
132 static int select_mf(void)
134 unsigned char cmd[] = { 0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x00 };
135 unsigned char ret[256];
136 unsigned int rlen = sizeof(ret);
140 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
144 printf("%s\n", rfid_hexdump(ret, rlen));
150 static int get_challenge(unsigned char len)
152 unsigned char cmd[] = { 0x00, 0x84, 0x00, 0x00, 0x08 };
153 unsigned char ret[256];
154 unsigned int rlen = sizeof(ret);
160 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
164 //printf("%s\n", rfid_hexdump(ret, rlen));
170 iso7816_select_application(void)
172 char cmd[] = { 0x00, 0xa4, 0x04, 0x0c, 0x07,
173 0x0a, 0x00, 0x00, 0x02, 0x47, 0x10, 0x01 };
175 unsigned int rlen = sizeof(resp);
179 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
183 /* FIXME: parse response */
188 iso7816_select_ef(u_int16_t fid)
190 unsigned char cmd[7] = { 0x00, 0xa4, 0x02, 0x0c, 0x02, 0x00, 0x00 };
191 unsigned char resp[7];
192 unsigned int rlen = sizeof(resp);
196 cmd[5] = (fid >> 8) & 0xff;
199 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
203 /* FIXME: parse response */
209 iso7816_read_binary(unsigned char *buf, unsigned int *len)
211 unsigned char cmd[] = { 0x00, 0xb0, 0x00, 0x00, 0x00 };
212 unsigned char resp[256];
213 unsigned int rlen = sizeof(resp);
217 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
221 /* FIXME: parse response, determine whether we need additional reads */
223 /* FIXME: copy 'len' number of response bytes to 'buf' */
227 /* wrapper function around SELECT EF and READ BINARY */
229 iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len)
233 rv = iso7816_select_ef(fid);
237 return iso7816_read_binary(buf, len);
241 mifare_ulight_write(struct rfid_protocol_handle *ph)
243 unsigned char buf[4] = { 0xa1, 0xa2, 0xa3, 0xa4 };
245 return rfid_protocol_write(ph, 20, buf, 4);
249 mifare_ulight_blank(struct rfid_protocol_handle *ph)
251 unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };
254 for (i = 4; i <= MIFARE_UL_PAGE_MAX; i++) {
255 ret = rfid_protocol_write(ph, i, buf, 4);
263 mifare_ulight_read(struct rfid_protocol_handle *ph)
265 unsigned char buf[20];
266 unsigned int len = sizeof(buf);
270 for (i = 0; i <= MIFARE_UL_PAGE_MAX; i++) {
271 ret = rfid_protocol_read(ph, i, buf, &len);
275 printf("Page 0x%x: %s\n", i, rfid_hexdump(buf, 4));
280 int main(int argc, char **argv)
289 protocol = RFID_PROTOCOL_MIFARE_UL;
290 protocol = RFID_PROTOCOL_MIFARE_CLASSIC;
291 // protocol = RFID_PROTOCOL_TCL;
293 if (l3(protocol) < 0)
297 case RFID_PROTOCOL_TCL:
298 /* we've established T=CL at this point */
301 rc632_register_dump(rh->ah, buf);
304 iso7816_select_application();
305 iso7816_select_ef(0x011e);
306 iso7816_select_ef(0x0101);
308 for (i = 0; i < 4; i++)
312 case RFID_PROTOCOL_MIFARE_UL:
313 mifare_ulight_read(ph);
315 //mifare_ulight_blank(ph);
316 mifare_ulight_write(ph);
317 mifare_ulight_read(ph);
320 case RFID_PROTOCOL_MIFARE_CLASSIC:
321 mfcl_set_key(ph, MIFARE_CLASSIC_KEY_DEFAULT);
322 rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, 0);
324 printf("mifare auth error\n");
327 printf("mifare authe succeeded!\n");
331 rfid_reader_close(rh);