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