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>
31 static ct_lock_handle lock;
33 static struct rfid_reader_handle *rh;
34 static struct rfid_layer2_handle *l2h;
35 static struct rfid_protocol_handle *ph;
38 /* this is the sole function required by rfid_reader_cm5121.c */
40 PC_to_RDR_Escape(void *handle,
41 const unsigned char *tx_buf, unsigned int tx_len,
42 unsigned char *rx_buf, unsigned int *rx_len)
44 ct_handle *h = (ct_handle *) handle;
47 rc = ct_card_transact(h, 1, tx_buf, tx_len, rx_buf, *rx_len);
60 unsigned char buf[0x3f];
61 unsigned char atr[64];
64 h = ct_reader_connect(0);
68 printf("acquiring card lock\n");
69 rc = ct_card_lock(h, slot, IFD_LOCK_EXCLUSIVE, &lock);
71 fprintf(stderr, "error, no card lock\n");
75 rc = ct_card_reset(h, slot, atr, sizeof(atr));
77 fprintf(stderr, "error, can't reset virtual card\n");
81 printf("initializing librfid\n");
84 printf("opening reader handle\n");
85 rh = rfid_reader_open(h, RFID_READER_CM5121);
87 fprintf(stderr, "error, no cm5121 handle\n");
93 printf("opening layer2 handle\n");
94 l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443A);
95 //l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443B);
97 fprintf(stderr, "error during iso14443a_init\n");
101 //rc632_register_dump(rh->ah, buf);
103 printf("running layer2 anticol\n");
104 rc = rfid_layer2_open(l2h);
106 fprintf(stderr, "error during layer2_open\n");
113 static int l3(int protocol)
115 printf("running layer3 (ats)\n");
116 ph = rfid_protocol_init(l2h, protocol);
118 fprintf(stderr, "error during protocol_init\n");
121 if (rfid_protocol_open(ph) < 0) {
122 fprintf(stderr, "error during protocol_open\n");
126 printf("we now have layer3 up and running\n");
131 static int select_mf(void)
133 unsigned char cmd[] = { 0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x00 };
134 unsigned char ret[256];
135 unsigned int rlen = sizeof(ret);
139 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
143 printf("%s\n", rfid_hexdump(ret, rlen));
149 static int get_challenge(unsigned char len)
151 unsigned char cmd[] = { 0x00, 0x84, 0x00, 0x00, 0x08 };
152 unsigned char ret[256];
153 unsigned int rlen = sizeof(ret);
159 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
163 //printf("%s\n", rfid_hexdump(ret, rlen));
169 iso7816_select_application(void)
171 char cmd[] = { 0x00, 0xa4, 0x04, 0x0c, 0x07,
172 0x0a, 0x00, 0x00, 0x02, 0x47, 0x10, 0x01 };
174 unsigned int rlen = sizeof(resp);
178 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
182 /* FIXME: parse response */
187 iso7816_select_ef(u_int16_t fid)
189 unsigned char cmd[7] = { 0x00, 0xa4, 0x02, 0x0c, 0x02, 0x00, 0x00 };
190 unsigned char resp[7];
191 unsigned int rlen = sizeof(resp);
195 cmd[5] = (fid >> 8) & 0xff;
198 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
202 /* FIXME: parse response */
208 iso7816_read_binary(unsigned char *buf, unsigned int *len)
210 unsigned char cmd[] = { 0x00, 0xb0, 0x00, 0x00, 0x00 };
211 unsigned char resp[256];
212 unsigned int rlen = sizeof(resp);
216 rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
220 /* FIXME: parse response, determine whether we need additional reads */
222 /* FIXME: copy 'len' number of response bytes to 'buf' */
226 /* wrapper function around SELECT EF and READ BINARY */
228 iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len)
232 rv = iso7816_select_ef(fid);
236 return iso7816_read_binary(buf, len);
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 < 7; i++) {
248 ret = rfid_protocol_read(ph, i, buf, &len);
252 rfid_hexdump(buf, 4);
257 int main(int argc, char **argv)
266 protocol = RFID_PROTOCOL_MIFARE_UL;
267 protocol = RFID_PROTOCOL_TCL;
269 if (l3(protocol) < 0)
273 case RFID_PROTOCOL_TCL:
274 /* we've established T=CL at this point */
277 rc632_register_dump(rh->ah, buf);
280 iso7816_select_application();
281 iso7816_select_ef(0x011e);
282 iso7816_select_ef(0x0101);
284 for (i = 0; i < 4; i++)
288 case RFID_PROTOCOL_MIFARE_UL:
289 mifare_ulight_read(ph);
294 rfid_reader_close(rh);