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