initial checkin of harald welte's original librfid project
[rfid/librfid.git] / utils / openct-escape.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         /* FIXME: parse response, determine whether we need additional reads */
196
197         /* FIXME: copy 'len' number of response bytes to 'buf' */
198         return 0;
199 }
200
201 /* wrapper function around SELECT EF and READ BINARY */
202 int
203 iso7816_read_ef(u_int16_t fid, unsigned char *buf, unsigned int *len)
204 {
205         int rv;
206
207         rv = iso7816_select_ef(fid);
208         if (rv < 0)
209                 return rv;
210
211         return iso7816_read_binary(buf, len);
212 }
213
214 /* mifare ultralight helpers */
215 int
216 mifare_ulight_write(struct rfid_protocol_handle *ph)
217 {
218         unsigned char buf[4] = { 0xa1, 0xa2, 0xa3, 0xa4 };
219
220         return rfid_protocol_write(ph, 10, buf, 4);
221 }
222
223 int
224 mifare_ulight_blank(struct rfid_protocol_handle *ph)
225 {
226         unsigned char buf[4] = { 0x00, 0x00, 0x00, 0x00 };
227         int i, ret;
228
229         for (i = 4; i <= MIFARE_UL_PAGE_MAX; i++) {
230                 ret = rfid_protocol_write(ph, i, buf, 4);
231                 if (ret < 0)
232                         return ret;
233         }
234         return 0;
235 }
236
237 static int
238 mifare_ulight_read(struct rfid_protocol_handle *ph)
239 {
240         unsigned char buf[20];
241         unsigned int len = sizeof(buf);
242         int ret;
243         int i;
244
245         for (i = 0; i <= MIFARE_UL_PAGE_MAX; i++) {
246                 ret = rfid_protocol_read(ph, i, buf, &len);
247                 if (ret < 0)
248                         return ret;
249
250                 printf("Page 0x%x: %s\n", i, hexdump(buf, 4));
251         }
252         return 0;
253 }
254
255 /* mifare classic helpers */
256 static int
257 mifare_classic_read(struct rfid_protocol_handle *ph)
258 {
259         unsigned char buf[20];
260         unsigned int len = sizeof(buf);
261         int ret;
262         int i;
263
264         for (i = 0; i <= MIFARE_CL_PAGE_MAX; i++) {
265                 ret = rfid_protocol_read(ph, i, buf, &len);
266                 if (ret < 0)
267                         return ret;
268
269                 printf("Page 0x%x: %s\n", i, hexdump(buf, len));
270         }
271         return 0;
272 }
273
274
275 int main(int argc, char **argv)
276 {
277         int rc;
278         char buf[0x40];
279         int i, protocol;
280
281 #if 0
282         if (argc) {
283                 argc--;
284                 argv++;
285         }
286         
287         while (argc) {
288                 if ( !strcmp (*argv, "--list")) {
289                         char *p;
290                         p = ccid_get_reader_list ();
291                         if (!p)
292                                 return 1;
293                         fputs (p, stderr);
294                         free (p);
295                         return 0;
296                 }
297                 else if ( !strcmp (*argv, "--debug")) {
298                         ccid_set_debug_level (ccid_set_debug_level (-1) + 1);
299                         argc--; argv++;
300                 }
301                 else
302                         break;
303         }
304 #endif
305
306         if (init() < 0)
307                 exit(1);
308
309         //protocol = RFID_PROTOCOL_MIFARE_UL;
310         //protocol = RFID_PROTOCOL_MIFARE_CLASSIC;
311         protocol = RFID_PROTOCOL_TCL;
312
313         if (l3(protocol) < 0)
314                 exit(1);
315
316         switch (protocol) {
317         case RFID_PROTOCOL_TCL:
318                 /* we've established T=CL at this point */
319                 select_mf();
320
321                 iso7816_select_application();
322                 iso7816_select_ef(0x011e);
323                 iso7816_select_ef(0x0101);
324 #if 1
325                 for (i = 0; i < 4; i++)
326                         iso7816_get_challenge(0x06);
327 #endif
328                 break;
329         case RFID_PROTOCOL_MIFARE_UL:
330                 mifare_ulight_read(ph);
331 #if 0
332                 mifare_ulight_blank(ph);
333                 mifare_ulight_write(ph);
334                 mifare_ulight_read(ph);
335 #endif
336                 break;
337         case RFID_PROTOCOL_MIFARE_CLASSIC:
338                 rc = mfcl_set_key(ph, MIFARE_CL_KEYA_DEFAULT_INFINEON);
339                 if (rc < 0) {
340                         printf("key format error\n");
341                         exit(1);
342                 }
343                 rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, 0);
344                 if (rc < 0) {
345                         printf("mifare auth error\n");
346                         exit(1);
347                 } else 
348                         printf("mifare authe succeeded!\n");
349                 mifare_classic_read(ph);
350                 break;
351         }
352
353         rfid_reader_close(rh);
354         
355         exit(0);
356 }