select_mf() and get_challenge() to test whether T=CL layer is working
[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
29 static int slot = 1;
30 static ct_handle *h;
31 static ct_lock_handle lock;
32
33 static struct rfid_reader_handle *rh;
34 static struct rfid_layer2_handle *l2h;
35 static struct rfid_protocol_handle *ph;
36
37
38 /* this is the sole function required by rfid_reader_cm5121.c */
39 int 
40 PC_to_RDR_Escape(void *handle, 
41                  const unsigned char *tx_buf, unsigned int tx_len,
42                  unsigned char *rx_buf, unsigned int *rx_len)
43 {
44         ct_handle *h = (ct_handle *) handle;
45         int rc;
46
47         rc = ct_card_transact(h, 1, tx_buf, tx_len, rx_buf, *rx_len);
48         if (rc >= 0) {
49                 *rx_len = rc;
50                 return 0;
51         }
52
53         return rc;
54 }
55
56
57
58 static int init()
59 {
60         unsigned char buf[0x3f];
61         unsigned char atr[64];
62         unsigned 
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         printf("opening layer2 handle\n");
93         l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443A);
94         //l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443B);
95         if (!l2h) {
96                 fprintf(stderr, "error during iso14443a_init\n");
97                 return -1;
98         }
99
100         //rc632_register_dump(rh->ah, buf);
101
102         printf("running layer2 anticol\n");
103         rc = rfid_layer2_open(l2h);
104         if (rc < 0) {
105                 fprintf(stderr, "error during layer2_open\n");
106                 return rc;
107         }
108
109         printf("running layer3 (ats)\n");
110         ph = rfid_protocol_init(l2h, RFID_PROTOCOL_TCL);
111         if (!ph) {
112                 fprintf(stderr, "error during protocol_init\n");
113                 return -1;
114         }
115         if (rfid_protocol_open(ph) < 0) {
116                 fprintf(stderr, "error during protocol_open\n");
117                 return -1;
118         }
119
120         printf("we now have T=CL up and running\n");
121
122         return 0;
123 }
124
125 static int select_mf(void)
126 {
127         unsigned char cmd[] = { 0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x00 };
128         unsigned char ret[256];
129         unsigned int rlen = sizeof(ret);
130
131         int rv;
132
133         rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
134         if (rv < 0)
135                 return rv;
136
137         //printf("%s\n", rfid_hexdump(ret, rlen));
138
139         return 0;
140 }
141
142
143 static int get_challenge(unsigned char len)
144 {
145         unsigned char cmd[] = { 0x00, 0x84, 0x00, 0x00, 0x08 };
146         unsigned char ret[256];
147         unsigned int rlen = sizeof(ret);
148
149         cmd[4] = len;
150
151         int rv;
152
153         rv = rfid_protocol_transcieve(ph, cmd, sizeof(cmd), ret, &rlen, 0, 0);
154         if (rv < 0)
155                 return rv;
156
157         //printf("%s\n", rfid_hexdump(ret, rlen));
158
159         return 0;
160 }
161
162 int main(int argc, char **argv)
163 {
164         int rc;
165         char buf[0x40];
166         int i;
167
168         if (init() < 0)
169                 exit(1);
170
171         /* we've established T=CL at this point */
172
173         select_mf();
174
175         for (i = 0; i < 4; i++)
176                 get_challenge(0x60);
177  
178         rfid_reader_close(rh);
179         
180         exit(0);
181 }