Partial fix for test_wpa build
[libeap.git] / wpa_supplicant / tests / test_wpa.c
1 /*
2  * Test program for combined WPA authenticator/supplicant
3  * Copyright (c) 2006-2007, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "eloop.h"
19 #include "ieee802_11_defs.h"
20 #include "../config.h"
21 #include "wpa.h"
22 #include "wpa_ie.h"
23 #include "../hostapd/wpa.h"
24
25
26 extern int wpa_debug_level;
27 extern int wpa_debug_show_keys;
28
29
30 struct wpa {
31         u8 auth_addr[ETH_ALEN];
32         u8 supp_addr[ETH_ALEN];
33         u8 psk[PMK_LEN];
34
35         /* from authenticator */
36         u8 auth_eapol_dst[ETH_ALEN];
37         u8 *auth_eapol;
38         size_t auth_eapol_len;
39
40         /* from supplicant */
41         u8 *supp_eapol;
42         size_t supp_eapol_len;
43
44         struct wpa_sm *supp;
45         struct wpa_authenticator *auth_group;
46         struct wpa_state_machine *auth;
47
48         struct wpa_ssid ssid;
49         u8 supp_ie[80];
50         size_t supp_ie_len;
51 };
52
53
54 static int supp_get_bssid(void *ctx, u8 *bssid)
55 {
56         struct wpa *wpa = ctx;
57         wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
58         os_memcpy(bssid, wpa->auth_addr, ETH_ALEN);
59         return 0;
60 }
61
62
63 static void supp_set_state(void *ctx, wpa_states state)
64 {
65         wpa_printf(MSG_DEBUG, "SUPP: %s(state=%d)", __func__, state);
66 }
67
68
69 static void auth_eapol_rx(void *eloop_data, void *user_ctx)
70 {
71         struct wpa *wpa = eloop_data;
72
73         wpa_printf(MSG_DEBUG, "AUTH: RX EAPOL frame");
74         wpa_receive(wpa->auth_group, wpa->auth, wpa->supp_eapol,
75                     wpa->supp_eapol_len);
76 }
77
78
79 static int supp_ether_send(void *ctx, const u8 *dest, u16 proto, const u8 *buf,
80                            size_t len)
81 {
82         struct wpa *wpa = ctx;
83
84         wpa_printf(MSG_DEBUG, "SUPP: %s(dest=" MACSTR " proto=0x%04x "
85                    "len=%lu)",
86                    __func__, MAC2STR(dest), proto, (unsigned long) len);
87
88         os_free(wpa->supp_eapol);
89         wpa->supp_eapol = os_malloc(len);
90         if (wpa->supp_eapol == NULL)
91                 return -1;
92         os_memcpy(wpa->supp_eapol, buf, len);
93         wpa->supp_eapol_len = len;
94         eloop_register_timeout(0, 0, auth_eapol_rx, wpa, NULL);
95
96         return 0;
97 }
98
99
100 static u8 * supp_alloc_eapol(void *ctx, u8 type, const void *data,
101                              u16 data_len, size_t *msg_len, void **data_pos)
102 {
103         struct ieee802_1x_hdr *hdr;
104
105         wpa_printf(MSG_DEBUG, "SUPP: %s(type=%d data_len=%d)",
106                    __func__, type, data_len);
107
108         *msg_len = sizeof(*hdr) + data_len;
109         hdr = os_malloc(*msg_len);
110         if (hdr == NULL)
111                 return NULL;
112
113         hdr->version = 2;
114         hdr->type = type;
115         hdr->length = host_to_be16(data_len);
116
117         if (data)
118                 os_memcpy(hdr + 1, data, data_len);
119         else
120                 os_memset(hdr + 1, 0, data_len);
121
122         if (data_pos)
123                 *data_pos = hdr + 1;
124
125         return (u8 *) hdr;
126 }
127
128
129 static int supp_get_beacon_ie(void *ctx)
130 {
131         struct wpa *wpa = ctx;
132         const u8 *ie;
133         size_t ielen;
134
135         wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
136
137         ie = wpa_auth_get_wpa_ie(wpa->auth_group, &ielen);
138         if (ie == NULL || ielen < 1)
139                 return -1;
140         if (ie[0] == WLAN_EID_RSN)
141                 return wpa_sm_set_ap_rsn_ie(wpa->supp, ie, 2 + ie[1]);
142         return wpa_sm_set_ap_wpa_ie(wpa->supp, ie, 2 + ie[1]);
143 }
144
145
146 static int supp_set_key(void *ctx, wpa_alg alg,
147                         const u8 *addr, int key_idx, int set_tx,
148                         const u8 *seq, size_t seq_len,
149                         const u8 *key, size_t key_len)
150 {
151         wpa_printf(MSG_DEBUG, "SUPP: %s(alg=%d addr=" MACSTR " key_idx=%d "
152                    "set_tx=%d)",
153                    __func__, alg, MAC2STR(addr), key_idx, set_tx);
154         wpa_hexdump(MSG_DEBUG, "SUPP: set_key - seq", seq, seq_len);
155         wpa_hexdump(MSG_DEBUG, "SUPP: set_key - key", key, key_len);
156         return 0;
157 }
158
159
160 static int supp_mlme_setprotection(void *ctx, const u8 *addr,
161                                    int protection_type, int key_type)
162 {
163         wpa_printf(MSG_DEBUG, "SUPP: %s(addr=" MACSTR " protection_type=%d "
164                    "key_type=%d)",
165                    __func__, MAC2STR(addr), protection_type, key_type);
166         return 0;
167 }
168
169
170 static void supp_cancel_auth_timeout(void *ctx)
171 {
172         wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
173 }
174
175
176 static int supp_init(struct wpa *wpa)
177 {
178         struct wpa_sm_ctx *ctx = os_zalloc(sizeof(*ctx));
179         if (ctx == NULL)
180                 return -1;
181
182         ctx->ctx = wpa;
183         ctx->set_state = supp_set_state;
184         ctx->get_bssid = supp_get_bssid;
185         ctx->ether_send = supp_ether_send;
186         ctx->get_beacon_ie = supp_get_beacon_ie;
187         ctx->alloc_eapol = supp_alloc_eapol;
188         ctx->set_key = supp_set_key;
189         ctx->mlme_setprotection = supp_mlme_setprotection;
190         ctx->cancel_auth_timeout = supp_cancel_auth_timeout;
191         wpa->supp = wpa_sm_init(ctx);
192         if (wpa->supp == NULL) {
193                 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_init() failed");
194                 return -1;
195         }
196
197         wpa_sm_set_own_addr(wpa->supp, wpa->supp_addr);
198         wpa_sm_set_param(wpa->supp, WPA_PARAM_RSN_ENABLED, 1);
199         wpa_sm_set_param(wpa->supp, WPA_PARAM_PROTO, WPA_PROTO_RSN);
200         wpa_sm_set_param(wpa->supp, WPA_PARAM_PAIRWISE, WPA_CIPHER_CCMP);
201         wpa_sm_set_param(wpa->supp, WPA_PARAM_GROUP, WPA_CIPHER_CCMP);
202         wpa_sm_set_param(wpa->supp, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
203         wpa_sm_set_pmk(wpa->supp, wpa->psk, PMK_LEN);
204
205         wpa->supp_ie_len = sizeof(wpa->supp_ie);
206         if (wpa_sm_set_assoc_wpa_ie_default(wpa->supp, wpa->supp_ie,
207                                             &wpa->supp_ie_len) < 0) {
208                 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
209                            " failed");
210                 return -1;
211         }
212
213         wpa_sm_notify_assoc(wpa->supp, wpa->auth_addr);
214
215         return 0;
216 }
217
218
219 static void auth_logger(void *ctx, const u8 *addr, logger_level level,
220                         const char *txt)
221 {
222         if (addr)
223                 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
224                            MAC2STR(addr), txt);
225         else
226                 wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
227 }
228
229
230 static void supp_eapol_rx(void *eloop_data, void *user_ctx)
231 {
232         struct wpa *wpa = eloop_data;
233
234         wpa_printf(MSG_DEBUG, "SUPP: RX EAPOL frame");
235         wpa_sm_rx_eapol(wpa->supp, wpa->auth_addr, wpa->auth_eapol,
236                         wpa->auth_eapol_len);
237 }
238
239
240 static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
241                            size_t data_len, int encrypt)
242 {
243         struct wpa *wpa = ctx;
244
245         wpa_printf(MSG_DEBUG, "AUTH: %s(addr=" MACSTR " data_len=%lu "
246                    "encrypt=%d)",
247                    __func__, MAC2STR(addr), (unsigned long) data_len, encrypt);
248
249         os_free(wpa->auth_eapol);
250         wpa->auth_eapol = os_malloc(data_len);
251         if (wpa->auth_eapol == NULL)
252                 return -1;
253         os_memcpy(wpa->auth_eapol_dst, addr, ETH_ALEN);
254         os_memcpy(wpa->auth_eapol, data, data_len);
255         wpa->auth_eapol_len = data_len;
256         eloop_register_timeout(0, 0, supp_eapol_rx, wpa, NULL);
257
258         return 0;
259 }
260
261
262 static const u8 * auth_get_psk(void *ctx, const u8 *addr, const u8 *prev_psk)
263 {
264         struct wpa *wpa = ctx;
265         wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
266                    __func__, MAC2STR(addr), prev_psk);
267         if (prev_psk)
268                 return NULL;
269         return wpa->psk;
270 }
271
272
273 static int auth_init_group(struct wpa *wpa)
274 {
275         struct wpa_auth_config conf;
276         struct wpa_auth_callbacks cb;
277
278         wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
279
280         os_memset(&conf, 0, sizeof(conf));
281         conf.wpa = 2;
282         conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
283         conf.wpa_pairwise = WPA_CIPHER_CCMP;
284         conf.rsn_pairwise = WPA_CIPHER_CCMP;
285         conf.wpa_group = WPA_CIPHER_CCMP;
286         conf.eapol_version = 2;
287
288         os_memset(&cb, 0, sizeof(cb));
289         cb.ctx = wpa;
290         cb.logger = auth_logger;
291         cb.send_eapol = auth_send_eapol;
292         cb.get_psk = auth_get_psk;
293
294         wpa->auth_group = wpa_init(wpa->auth_addr, &conf, &cb);
295         if (wpa->auth_group == NULL) {
296                 wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
297                 return -1;
298         }
299
300         return 0;
301 }
302
303
304 static int auth_init(struct wpa *wpa)
305 {
306         wpa->auth = wpa_auth_sta_init(wpa->auth_group, wpa->supp_addr);
307         if (wpa->auth == NULL) {
308                 wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
309                 return -1;
310         }
311
312         if (wpa_validate_wpa_ie(wpa->auth_group, wpa->auth, wpa->supp_ie,
313                                 wpa->supp_ie_len, NULL, 0) != WPA_IE_OK) {
314                 wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
315                 return -1;
316         }
317
318         wpa_auth_sm_event(wpa->auth, WPA_ASSOC);
319
320         wpa_auth_sta_associated(wpa->auth_group, wpa->auth);
321
322         return 0;
323 }
324
325
326 static void deinit(struct wpa *wpa)
327 {
328         wpa_auth_sta_deinit(wpa->auth);
329         wpa_sm_deinit(wpa->supp);
330         wpa_deinit(wpa->auth_group);
331         os_free(wpa->auth_eapol);
332         wpa->auth_eapol = NULL;
333         os_free(wpa->supp_eapol);
334         wpa->supp_eapol = NULL;
335 }
336
337
338 int main(int argc, char *argv[])
339 {
340         struct wpa wpa;
341
342         if (os_program_init())
343                 return -1;
344
345         os_memset(&wpa, 0, sizeof(wpa));
346         os_memset(wpa.auth_addr, 0x12, ETH_ALEN);
347         os_memset(wpa.supp_addr, 0x32, ETH_ALEN);
348         os_memset(wpa.psk, 0x44, PMK_LEN);
349
350         wpa_debug_level = 0;
351         wpa_debug_show_keys = 1;
352
353         if (eloop_init(&wpa)) {
354                 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
355                 return -1;
356         }
357
358         if (auth_init_group(&wpa) < 0)
359                 return -1;
360
361         if (supp_init(&wpa) < 0)
362                 return -1;
363
364         if (auth_init(&wpa) < 0)
365                 return -1;
366
367         wpa_printf(MSG_DEBUG, "Starting eloop");
368         eloop_run();
369         wpa_printf(MSG_DEBUG, "eloop done");
370
371         deinit(&wpa);
372
373         eloop_destroy();
374
375         os_program_deinit();
376
377         return 0;
378 }