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