- add mifare classic support
[librfid] / openct-escape.c
1
2 /*
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
6  *
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.
11  *
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
15  */
16
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <openct/openct.h>
22
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>
29
30 static int slot = 1;
31 static ct_handle *h;
32 static ct_lock_handle lock;
33
34 static struct rfid_reader_handle *rh;
35 static struct rfid_layer2_handle *l2h;
36 static struct rfid_protocol_handle *ph;
37
38
39 /* this is the sole function required by rfid_reader_cm5121.c */
40 int 
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)
44 {
45         ct_handle *h = (ct_handle *) handle;
46         int rc;
47
48         rc = ct_card_transact(h, 1, tx_buf, tx_len, rx_buf, *rx_len);
49         if (rc >= 0) {
50                 *rx_len = rc;
51                 return 0;
52         }
53
54         return rc;
55 }
56
57
58
59 static int init()
60 {
61         unsigned char buf[0x3f];
62         unsigned char atr[64];
63         int rc;
64
65         h = ct_reader_connect(0);
66         if (!h)
67                 return -1;
68
69         printf("acquiring card lock\n");
70         rc = ct_card_lock(h, slot, IFD_LOCK_EXCLUSIVE, &lock);
71         if (rc < 0) {
72                 fprintf(stderr, "error, no card lock\n");
73                 return -1;
74         }
75
76         rc = ct_card_reset(h, slot, atr, sizeof(atr));
77         if (rc < 0) {
78                 fprintf(stderr, "error, can't reset virtual card\n");
79                 return -1;
80         }
81
82         printf("initializing librfid\n");
83         rfid_init();
84
85         printf("opening reader handle\n");
86         rh = rfid_reader_open(h, RFID_READER_CM5121);
87         if (!rh) {
88                 fprintf(stderr, "error, no cm5121 handle\n");
89                 return -1;
90         }
91
92         sleep(2);
93
94         printf("opening layer2 handle\n");
95         l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443A);
96         //l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443B);
97         if (!l2h) {
98                 fprintf(stderr, "error during iso14443a_init\n");
99                 return -1;
100         }
101
102         //rc632_register_dump(rh->ah, buf);
103
104         printf("running layer2 anticol\n");
105         rc = rfid_layer2_open(l2h);
106         if (rc < 0) {
107                 fprintf(stderr, "error during layer2_open\n");
108                 return rc;
109         }
110
111         return 0;
112 }
113
114 static int l3(int protocol)
115 {
116         printf("running layer3 (ats)\n");
117         ph = rfid_protocol_init(l2h, protocol);
118         if (!ph) {
119                 fprintf(stderr, "error during protocol_init\n");
120                 return -1;
121         }
122         if (rfid_protocol_open(ph) < 0) {
123                 fprintf(stderr, "error during protocol_open\n");
124                 return -1;
125         }
126
127         printf("we now have layer3 up and running\n");
128
129         return 0;
130 }
131
132 static int select_mf(void)
133 {
134         unsigned char cmd[] = { 0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x00 };
135         unsigned char ret[256];
136         unsigned int rlen = sizeof(ret);
137
138         int rv;
139
140         rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
141         if (rv < 0)
142                 return rv;
143
144         printf("%s\n", rfid_hexdump(ret, rlen));
145
146         return 0;
147 }
148
149
150 static int get_challenge(unsigned char len)
151 {
152         unsigned char cmd[] = { 0x00, 0x84, 0x00, 0x00, 0x08 };
153         unsigned char ret[256];
154         unsigned int rlen = sizeof(ret);
155
156         cmd[4] = len;
157
158         int rv;
159
160         rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
161         if (rv < 0)
162                 return rv;
163
164         //printf("%s\n", rfid_hexdump(ret, rlen));
165
166         return 0;
167 }
168
169 int
170 iso7816_select_application(void)
171 {
172         char cmd[] = { 0x00, 0xa4, 0x04, 0x0c, 0x07,
173                        0x0a, 0x00, 0x00, 0x02, 0x47, 0x10, 0x01 };
174         char resp[7];
175         unsigned int rlen = sizeof(resp);
176
177         int rv;
178
179         rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
180         if (rv < 0)
181                 return rv;
182
183         /* FIXME: parse response */
184         return 0;
185 }
186
187 int
188 iso7816_select_ef(u_int16_t fid)
189 {
190         unsigned char cmd[7] = { 0x00, 0xa4, 0x02, 0x0c, 0x02, 0x00, 0x00 };
191         unsigned char resp[7];
192         unsigned int rlen = sizeof(resp);
193
194         int rv;
195
196         cmd[5] = (fid >> 8) & 0xff;
197         cmd[6] = fid & 0xff;
198
199         rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
200         if (rv < 0)
201                 return rv;
202
203         /* FIXME: parse response */
204
205         return 0;
206 }
207
208 int
209 iso7816_read_binary(unsigned char *buf, unsigned int *len)
210 {
211         unsigned char cmd[] = { 0x00, 0xb0, 0x00, 0x00, 0x00 };
212         unsigned char resp[256];
213         unsigned int rlen = sizeof(resp);
214         
215         int rv;
216
217         rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
218         if (rv < 0)
219                 return rv;
220
221         /* FIXME: parse response, determine whether we need additional reads */
222
223         /* FIXME: copy 'len' number of response bytes to 'buf' */
224         return 0;
225 }
226
227 /* wrapper function around SELECT EF and READ BINARY */
228 int
229 iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len)
230 {
231         int rv;
232
233         rv = iso7816_select_ef(fid);
234         if (rv < 0)
235                 return rv;
236
237         return iso7816_read_binary(buf, len);
238 }
239
240 int
241 mifare_ulight_write(struct rfid_protocol_handle *ph)
242 {
243         unsigned char buf[4] = { 0xa1, 0xa2, 0xa3, 0xa4 };
244
245         return rfid_protocol_write(ph, 20, buf, 4);
246 }
247
248 int
249 mifare_ulight_blank(struct rfid_protocol_handle *ph)
250 {
251         unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };
252         int i, ret;
253
254         for (i = 4; i <= MIFARE_UL_PAGE_MAX; i++) {
255                 ret = rfid_protocol_write(ph, i, buf, 4);
256                 if (ret < 0)
257                         return ret;
258         }
259         return 0;
260 }
261
262 int
263 mifare_ulight_read(struct rfid_protocol_handle *ph)
264 {
265         unsigned char buf[20];
266         unsigned int len = sizeof(buf);
267         int ret;
268         int i;
269
270         for (i = 0; i <= MIFARE_UL_PAGE_MAX; i++) {
271                 ret = rfid_protocol_read(ph, i, buf, &len);
272                 if (ret < 0)
273                         return ret;
274
275                 printf("Page 0x%x: %s\n", i, rfid_hexdump(buf, 4));
276         }
277         return 0;
278 }
279
280 int main(int argc, char **argv)
281 {
282         int rc;
283         char buf[0x40];
284         int i, protocol;
285
286         if (init() < 0)
287                 exit(1);
288
289         protocol = RFID_PROTOCOL_MIFARE_UL;
290         protocol = RFID_PROTOCOL_MIFARE_CLASSIC;
291 //      protocol = RFID_PROTOCOL_TCL;
292
293         if (l3(protocol) < 0)
294                 exit(1);
295
296         switch (protocol) {
297         case RFID_PROTOCOL_TCL:
298                 /* we've established T=CL at this point */
299                 select_mf();
300
301                 rc632_register_dump(rh->ah, buf);
302                 select_mf();
303
304                 iso7816_select_application();
305                 iso7816_select_ef(0x011e);
306                 iso7816_select_ef(0x0101);
307 #if 0
308                 for (i = 0; i < 4; i++)
309                         get_challenge(0x60);
310 #endif
311                 break;
312         case RFID_PROTOCOL_MIFARE_UL:
313                 mifare_ulight_read(ph);
314 #if 0
315                 //mifare_ulight_blank(ph);
316                 mifare_ulight_write(ph);
317                 mifare_ulight_read(ph);
318 #endif
319                 break;
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);
323                 if (rc < 0) {
324                         printf("mifare auth error\n");
325                         exit(1);
326                 } else 
327                         printf("mifare authe succeeded!\n");
328                 break;
329         }
330
331         rfid_reader_close(rh);
332         
333         exit(0);
334 }