rename openct-escape into librfid-tool
[librfid] / utils / librfid-tool.c
1 /*                                                 -*- linux-c -*-
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
5  *
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.
10  *
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
14  */
15
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include <librfid/rfid.h>
22 #include <librfid/rfid_reader.h>
23 #include <librfid/rfid_layer2.h>
24 #include <librfid/rfid_protocol.h>
25
26 #include <librfid/rfid_protocol_mifare_classic.h>
27 #include <librfid/rfid_protocol_mifare_ul.h>
28
29 static const char *
30 hexdump(const void *data, unsigned int len)
31 {
32         static char string[1024];
33         unsigned char *d = (unsigned char *) data;
34         unsigned int i, left;
35
36         string[0] = '\0';
37         left = sizeof(string);
38         for (i = 0; len--; i += 3) {
39                 if (i >= sizeof(string) -4)
40                         break;
41                 snprintf(string+i, 4, " %02x", *d++);
42         }
43         return string;
44 }
45
46 static struct rfid_reader_handle *rh;
47 static struct rfid_layer2_handle *l2h;
48 static struct rfid_protocol_handle *ph;
49
50 static int init()
51 {
52         unsigned char buf[0x3f];
53         int rc;
54
55         printf("initializing librfid\n");
56         rfid_init();
57
58         printf("opening reader handle\n");
59         rh = rfid_reader_open(NULL, RFID_READER_CM5121);
60         if (!rh) {
61                 fprintf(stderr, "error, no cm5121 handle\n");
62                 return -1;
63         }
64
65         printf("opening layer2 handle\n");
66         l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443A);
67         //l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443B);
68         if (!l2h) {
69                 fprintf(stderr, "error during iso14443a_init\n");
70                 return -1;
71         }
72
73         //rc632_register_dump(rh->ah, buf);
74
75         printf("running layer2 anticol\n");
76         rc = rfid_layer2_open(l2h);
77         if (rc < 0) {
78                 fprintf(stderr, "error during layer2_open\n");
79                 return rc;
80         }
81
82         return 0;
83 }
84
85 static int l3(int protocol)
86 {
87         printf("running layer3 (ats)\n");
88         ph = rfid_protocol_init(l2h, protocol);
89         if (!ph) {
90                 fprintf(stderr, "error during protocol_init\n");
91                 return -1;
92         }
93         if (rfid_protocol_open(ph) < 0) {
94                 fprintf(stderr, "error during protocol_open\n");
95                 return -1;
96         }
97
98         printf("we now have layer3 up and running\n");
99
100         return 0;
101 }
102
103 static int select_mf(void)
104 {
105         unsigned char cmd[] = { 0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x00 };
106         unsigned char ret[256];
107         unsigned int rlen = sizeof(ret);
108
109         int rv;
110
111         rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
112         if (rv < 0)
113                 return rv;
114
115         printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
116
117         return 0;
118 }
119
120
121 static int iso7816_get_challenge(unsigned char len)
122 {
123         unsigned char cmd[] = { 0x00, 0x84, 0x00, 0x00, 0x08 };
124         unsigned char ret[256];
125         unsigned int rlen = sizeof(ret);
126
127         cmd[4] = len;
128
129         int rv;
130
131         rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
132         if (rv < 0)
133                 return rv;
134
135         printf("%d: [%s]\n", rlen, hexdump(ret, rlen));
136
137         return 0;
138 }
139
140 int
141 iso7816_select_application(void)
142 {
143         unsigned char cmd[] = { 0x00, 0xa4, 0x04, 0x0c, 0x07,
144                        0xa0, 0x00, 0x00, 0x02, 0x47, 0x10, 0x01 };
145         unsigned char resp[7];
146         unsigned int rlen = sizeof(resp);
147
148         int rv;
149
150         rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
151         if (rv < 0)
152                 return rv;
153
154         /* FIXME: parse response */
155         printf("%s\n", hexdump(resp, rlen));
156
157         return 0;
158 }
159
160 int
161 iso7816_select_ef(u_int16_t fid)
162 {
163         unsigned char cmd[7] = { 0x00, 0xa4, 0x02, 0x0c, 0x02, 0x00, 0x00 };
164         unsigned char resp[7];
165         unsigned int rlen = sizeof(resp);
166
167         int rv;
168
169         cmd[5] = (fid >> 8) & 0xff;
170         cmd[6] = fid & 0xff;
171
172         rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
173         if (rv < 0)
174                 return rv;
175
176         /* FIXME: parse response */
177         printf("%s\n", hexdump(resp, rlen));
178
179         return 0;
180 }
181
182 int
183 iso7816_read_binary(unsigned char *buf, unsigned int *len)
184 {
185         unsigned char cmd[] = { 0x00, 0xb0, 0x00, 0x00, 0x00 };
186         unsigned char resp[256];
187         unsigned int rlen = sizeof(resp);
188         
189         int rv;
190
191         rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), resp, &rlen, 0, 0);
192         if (rv < 0)
193                 return rv;
194
195         printf("%s\n", hexdump(resp, rlen));
196
197         /* FIXME: parse response, determine whether we need additional reads */
198
199         /* FIXME: copy 'len' number of response bytes to 'buf' */
200         return 0;
201 }
202
203 /* wrapper function around SELECT EF and READ BINARY */
204 int
205 iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len)
206 {
207         int rv;
208
209         rv = iso7816_select_ef(fid);
210         if (rv < 0)
211                 return rv;
212
213         return iso7816_read_binary(buf, len);
214 }
215
216 /* mifare ultralight helpers */
217 int
218 mifare_ulight_write(struct rfid_protocol_handle *ph)
219 {
220         unsigned char buf[4] = { 0xa1, 0xa2, 0xa3, 0xa4 };
221
222         return rfid_protocol_write(ph, 10, buf, 4);
223 }
224
225 int
226 mifare_ulight_blank(struct rfid_protocol_handle *ph)
227 {
228         unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };
229         int i, ret;
230
231         for (i = 4; i <= MIFARE_UL_PAGE_MAX; i++) {
232                 ret = rfid_protocol_write(ph, i, buf, 4);
233                 if (ret < 0)
234                         return ret;
235         }
236         return 0;
237 }
238
239 static int
240 mifare_ulight_read(struct rfid_protocol_handle *ph)
241 {
242         unsigned char buf[20];
243         unsigned int len = sizeof(buf);
244         int ret;
245         int i;
246
247         for (i = 0; i <= MIFARE_UL_PAGE_MAX; i++) {
248                 ret = rfid_protocol_read(ph, i, buf, &len);
249                 if (ret < 0)
250                         return ret;
251
252                 printf("Page 0x%x: %s\n", i, hexdump(buf, 4));
253         }
254         return 0;
255 }
256
257 /* mifare classic helpers */
258 static int
259 mifare_classic_read_sector(struct rfid_protocol_handle *ph, int sector)
260 {
261         unsigned char buf[20];
262         unsigned int len = sizeof(buf);
263         int ret;
264         int block;
265
266         /* FIXME: make this work for sectors > 31 */
267         printf("reading sector %u\n", sector);
268
269         for (block = sector*4; block < sector*4+4; block++) {
270                 printf("reading block %u\n", block);
271                 ret = rfid_protocol_read(ph, block, buf, &len);
272                 if (ret < 0)
273                         return ret;
274
275                 printf("Page 0x%x: %s\n", block, hexdump(buf, len));
276         }
277         return 0;
278 }
279
280
281 int main(int argc, char **argv)
282 {
283         int rc;
284         char buf[0x40];
285         int i, protocol;
286
287 #if 0
288         if (argc) {
289                 argc--;
290                 argv++;
291         }
292         
293         while (argc) {
294                 if ( !strcmp (*argv, "--list")) {
295                         char *p;
296                         p = ccid_get_reader_list ();
297                         if (!p)
298                                 return 1;
299                         fputs (p, stderr);
300                         free (p);
301                         return 0;
302                 }
303                 else if ( !strcmp (*argv, "--debug")) {
304                         ccid_set_debug_level (ccid_set_debug_level (-1) + 1);
305                         argc--; argv++;
306                 }
307                 else
308                         break;
309         }
310 #endif
311
312         if (init() < 0)
313                 exit(1);
314
315         //while(1) { }
316
317         //protocol = RFID_PROTOCOL_MIFARE_UL;
318         //protocol = RFID_PROTOCOL_MIFARE_CLASSIC;
319         protocol = RFID_PROTOCOL_TCL;
320
321         if (l3(protocol) < 0)
322                 exit(1);
323
324         switch (protocol) {
325                 char buf[32000];
326                 int len = 200;
327
328         case RFID_PROTOCOL_TCL:
329                 /* we've established T=CL at this point */
330                 select_mf();
331
332                 iso7816_select_application();
333                 iso7816_select_ef(0x011e);
334                 iso7816_select_ef(0x0101);
335
336                 while (1) {
337                         len = 200;
338                         printf("reading ef\n");
339                         iso7816_read_binary(buf, &len);
340                 }
341 #if 0
342                 for (i = 0; i < 4; i++)
343                         iso7816_get_challenge(0xff);
344 #endif
345                 break;
346         case RFID_PROTOCOL_MIFARE_UL:
347                 mifare_ulight_read(ph);
348 #if 0
349                 mifare_ulight_blank(ph);
350                 mifare_ulight_write(ph);
351                 mifare_ulight_read(ph);
352 #endif
353                 break;
354         case RFID_PROTOCOL_MIFARE_CLASSIC:
355                 {
356                         int sector;
357                         for (sector = 1; sector < 31; sector++) {
358                                 rc = mfcl_set_key(ph, MIFARE_CL_KEYA_DEFAULT_INFINEON);
359                                 if (rc < 0) {
360                                         printf("key format error\n");
361                                         exit(1);
362                                 }
363                                 rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, sector);
364                                 if (rc < 0) {
365                                         printf("mifare auth error\n");
366                                         exit(1);
367                                 } else 
368                                         printf("mifare authe succeeded!\n");
369
370                                 mifare_classic_read_sector(ph, sector);
371                         }
372                 }
373                 break;
374         }
375
376         rfid_reader_close(rh);
377         
378         exit(0);
379 }