xref: /freebsd/contrib/wpa/wpa_supplicant/preauth_test.c (revision 3078531de10dcae44b253a35125c949ff4235284)
1 /*
2  * WPA Supplicant - test code for pre-authentication
3  * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  *
8  * IEEE 802.1X Supplicant test code (to be used in place of wpa_supplicant.c.
9  * Not used in production version.
10  */
11 
12 #include "includes.h"
13 #include <assert.h>
14 
15 #include "common.h"
16 #include "config.h"
17 #include "eapol_supp/eapol_supp_sm.h"
18 #include "eloop.h"
19 #include "rsn_supp/wpa.h"
20 #include "eap_peer/eap.h"
21 #include "wpa_supplicant_i.h"
22 #include "l2_packet/l2_packet.h"
23 #include "ctrl_iface.h"
24 #include "pcsc_funcs.h"
25 #include "rsn_supp/preauth.h"
26 #include "rsn_supp/pmksa_cache.h"
27 #include "drivers/driver.h"
28 
29 
30 const struct wpa_driver_ops *const wpa_drivers[] = { NULL };
31 
32 
33 struct preauth_test_data {
34 	int auth_timed_out;
35 };
36 
37 
38 static void _wpa_supplicant_deauthenticate(void *wpa_s, u16 reason_code)
39 {
40 	wpa_supplicant_deauthenticate(wpa_s, reason_code);
41 }
42 
43 
44 static void _wpa_supplicant_reconnect(void *wpa_s)
45 {
46 	wpa_supplicant_reconnect(wpa_s);
47 }
48 
49 
50 static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
51 			    const void *data, u16 data_len,
52 			    size_t *msg_len, void **data_pos)
53 {
54 	struct ieee802_1x_hdr *hdr;
55 
56 	*msg_len = sizeof(*hdr) + data_len;
57 	hdr = os_malloc(*msg_len);
58 	if (hdr == NULL)
59 		return NULL;
60 
61 	hdr->version = wpa_s->conf->eapol_version;
62 	hdr->type = type;
63 	hdr->length = htons(data_len);
64 
65 	if (data)
66 		os_memcpy(hdr + 1, data, data_len);
67 	else
68 		os_memset(hdr + 1, 0, data_len);
69 
70 	if (data_pos)
71 		*data_pos = hdr + 1;
72 
73 	return (u8 *) hdr;
74 }
75 
76 
77 static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
78 			     const void *data, u16 data_len,
79 			     size_t *msg_len, void **data_pos)
80 {
81 	return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
82 }
83 
84 
85 static void _wpa_supplicant_set_state(void *ctx, enum wpa_states state)
86 {
87 	struct wpa_supplicant *wpa_s = ctx;
88 	wpa_s->wpa_state = state;
89 }
90 
91 
92 static enum wpa_states _wpa_supplicant_get_state(void *ctx)
93 {
94 	struct wpa_supplicant *wpa_s = ctx;
95 	return wpa_s->wpa_state;
96 }
97 
98 
99 static int wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
100 			  const u8 *buf, size_t len)
101 {
102 	printf("%s - not implemented\n", __func__);
103 	return -1;
104 }
105 
106 
107 static void * wpa_supplicant_get_network_ctx(void *wpa_s)
108 {
109 	return wpa_supplicant_get_ssid(wpa_s);
110 }
111 
112 
113 static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
114 {
115 	wpa_supplicant_cancel_auth_timeout(wpa_s);
116 }
117 
118 
119 static int wpa_supplicant_get_beacon_ie(void *wpa_s)
120 {
121 	printf("%s - not implemented\n", __func__);
122 	return -1;
123 }
124 
125 
126 static int wpa_supplicant_get_bssid(void *wpa_s, u8 *bssid)
127 {
128 	printf("%s - not implemented\n", __func__);
129 	return -1;
130 }
131 
132 
133 static int wpa_supplicant_set_key(void *wpa_s, enum wpa_alg alg,
134 				  const u8 *addr, int key_idx, int set_tx,
135 				  const u8 *seq, size_t seq_len,
136 				  const u8 *key, size_t key_len,
137 				  enum key_flag key_flag)
138 {
139 	printf("%s - not implemented\n", __func__);
140 	return -1;
141 }
142 
143 
144 static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
145 					     int protection_type,
146 					     int key_type)
147 {
148 	printf("%s - not implemented\n", __func__);
149 	return -1;
150 }
151 
152 
153 static int wpa_supplicant_add_pmkid(void *wpa_s, void *network_ctx,
154 				    const u8 *bssid, const u8 *pmkid,
155 				    const u8 *fils_cache_id,
156 				    const u8 *pmk, size_t pmk_len,
157 				    u32 pmk_lifetime, u8 pmk_reauth_threshold,
158 				    int akmp)
159 {
160 	printf("%s - not implemented\n", __func__);
161 	return -1;
162 }
163 
164 
165 static int wpa_supplicant_remove_pmkid(void *wpa_s, void *network_ctx,
166 				       const u8 *bssid, const u8 *pmkid,
167 				       const u8 *fils_cache_id)
168 {
169 	printf("%s - not implemented\n", __func__);
170 	return -1;
171 }
172 
173 
174 static void wpa_supplicant_set_config_blob(void *ctx,
175 					   struct wpa_config_blob *blob)
176 {
177 	struct wpa_supplicant *wpa_s = ctx;
178 	wpa_config_set_blob(wpa_s->conf, blob);
179 }
180 
181 
182 static const struct wpa_config_blob *
183 wpa_supplicant_get_config_blob(void *ctx, const char *name)
184 {
185 	struct wpa_supplicant *wpa_s = ctx;
186 	return wpa_config_get_blob(wpa_s->conf, name);
187 }
188 
189 
190 static void test_eapol_clean(struct wpa_supplicant *wpa_s)
191 {
192 	rsn_preauth_deinit(wpa_s->wpa);
193 	pmksa_candidate_free(wpa_s->wpa);
194 	wpa_sm_deinit(wpa_s->wpa);
195 	scard_deinit(wpa_s->scard);
196 	wpa_supplicant_ctrl_iface_deinit(wpa_s, wpa_s->ctrl_iface);
197 	wpa_s->ctrl_iface = NULL;
198 	wpa_config_free(wpa_s->conf);
199 }
200 
201 
202 static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
203 {
204 	struct preauth_test_data *p = eloop_ctx;
205 	printf("EAPOL test timed out\n");
206 	p->auth_timed_out = 1;
207 	eloop_terminate();
208 }
209 
210 
211 static void eapol_test_poll(void *eloop_ctx, void *timeout_ctx)
212 {
213 	struct wpa_supplicant *wpa_s = eloop_ctx;
214 	if (!rsn_preauth_in_progress(wpa_s->wpa))
215 		eloop_terminate();
216 	else {
217 		eloop_register_timeout(0, 100000, eapol_test_poll, eloop_ctx,
218 				       timeout_ctx);
219 	}
220 }
221 
222 
223 static struct wpa_driver_ops stub_driver;
224 
225 
226 static void wpa_init_conf(struct wpa_supplicant *wpa_s, const char *ifname)
227 {
228 	struct l2_packet_data *l2;
229 	struct wpa_sm_ctx *ctx;
230 
231 	os_memset(&stub_driver, 0, sizeof(stub_driver));
232 	wpa_s->driver = &stub_driver;
233 
234 	ctx = os_zalloc(sizeof(*ctx));
235 	assert(ctx != NULL);
236 
237 	ctx->ctx = wpa_s;
238 	ctx->msg_ctx = wpa_s;
239 	ctx->set_state = _wpa_supplicant_set_state;
240 	ctx->get_state = _wpa_supplicant_get_state;
241 	ctx->deauthenticate = _wpa_supplicant_deauthenticate;
242 	ctx->set_key = wpa_supplicant_set_key;
243 	ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
244 	ctx->get_bssid = wpa_supplicant_get_bssid;
245 	ctx->ether_send = wpa_ether_send;
246 	ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
247 	ctx->alloc_eapol = _wpa_alloc_eapol;
248 	ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
249 	ctx->add_pmkid = wpa_supplicant_add_pmkid;
250 	ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
251 	ctx->set_config_blob = wpa_supplicant_set_config_blob;
252 	ctx->get_config_blob = wpa_supplicant_get_config_blob;
253 	ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
254 	ctx->reconnect = _wpa_supplicant_reconnect;
255 
256 	wpa_s->wpa = wpa_sm_init(ctx);
257 	assert(wpa_s->wpa != NULL);
258 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, WPA_PROTO_RSN);
259 
260 	os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
261 	wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname, NULL);
262 
263 	l2 = l2_packet_init(wpa_s->ifname, NULL, ETH_P_RSN_PREAUTH, NULL,
264 			    NULL, 0);
265 	assert(l2 != NULL);
266 	if (l2_packet_get_own_addr(l2, wpa_s->own_addr)) {
267 		wpa_printf(MSG_WARNING, "Failed to get own L2 address\n");
268 		exit(-1);
269 	}
270 	l2_packet_deinit(l2);
271 	wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
272 }
273 
274 
275 static void eapol_test_terminate(int sig, void *signal_ctx)
276 {
277 	struct wpa_supplicant *wpa_s = signal_ctx;
278 	wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
279 	eloop_terminate();
280 }
281 
282 
283 int main(int argc, char *argv[])
284 {
285 	struct wpa_supplicant wpa_s;
286 	int ret = 1;
287 	u8 bssid[ETH_ALEN];
288 	struct preauth_test_data preauth_test;
289 
290 	if (os_program_init())
291 		return -1;
292 
293 	os_memset(&preauth_test, 0, sizeof(preauth_test));
294 
295 	wpa_debug_level = 0;
296 	wpa_debug_show_keys = 1;
297 
298 	if (argc != 4) {
299 		printf("usage: preauth_test <conf> <target MAC address> "
300 		       "<ifname>\n");
301 		return -1;
302 	}
303 
304 	if (hwaddr_aton(argv[2], bssid)) {
305 		printf("Failed to parse target address '%s'.\n", argv[2]);
306 		return -1;
307 	}
308 
309 	if (eap_register_methods()) {
310 		wpa_printf(MSG_ERROR, "Failed to register EAP methods");
311 		return -1;
312 	}
313 
314 	if (eloop_init()) {
315 		wpa_printf(MSG_ERROR, "Failed to initialize event loop");
316 		return -1;
317 	}
318 
319 	os_memset(&wpa_s, 0, sizeof(wpa_s));
320 	wpa_s.conf = wpa_config_read(argv[1], NULL);
321 	if (wpa_s.conf == NULL) {
322 		printf("Failed to parse configuration file '%s'.\n", argv[1]);
323 		return -1;
324 	}
325 	if (wpa_s.conf->ssid == NULL) {
326 		printf("No networks defined.\n");
327 		return -1;
328 	}
329 
330 	wpa_init_conf(&wpa_s, argv[3]);
331 	wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
332 	if (wpa_s.ctrl_iface == NULL) {
333 		printf("Failed to initialize control interface '%s'.\n"
334 		       "You may have another preauth_test process already "
335 		       "running or the file was\n"
336 		       "left by an unclean termination of preauth_test in "
337 		       "which case you will need\n"
338 		       "to manually remove this file before starting "
339 		       "preauth_test again.\n",
340 		       wpa_s.conf->ctrl_interface);
341 		return -1;
342 	}
343 	if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
344 		return -1;
345 
346 	if (rsn_preauth_init(wpa_s.wpa, bssid, &wpa_s.conf->ssid->eap))
347 		return -1;
348 
349 	eloop_register_timeout(30, 0, eapol_test_timeout, &preauth_test, NULL);
350 	eloop_register_timeout(0, 100000, eapol_test_poll, &wpa_s, NULL);
351 	eloop_register_signal_terminate(eapol_test_terminate, &wpa_s);
352 	eloop_register_signal_reconfig(eapol_test_terminate, &wpa_s);
353 	eloop_run();
354 
355 	if (preauth_test.auth_timed_out)
356 		ret = -2;
357 	else {
358 		ret = pmksa_cache_set_current(wpa_s.wpa, NULL, bssid, NULL, 0,
359 					      NULL, 0) ? 0 : -3;
360 	}
361 
362 	test_eapol_clean(&wpa_s);
363 
364 	eap_peer_unregister_methods();
365 
366 	eloop_destroy();
367 
368 	os_program_deinit();
369 
370 	return ret;
371 }
372