]>
Commit | Line | Data |
---|---|---|
6fc6879b JM |
1 | /* |
2 | * hostapd - IEEE 802.11i-2004 / WPA Authenticator | |
3 | * Copyright (c) 2004-2008, 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 | #ifndef CONFIG_NATIVE_WINDOWS | |
18 | ||
19 | #include "common.h" | |
20 | #include "config.h" | |
21 | #include "eapol_sm.h" | |
22 | #include "wpa.h" | |
23 | #include "sha1.h" | |
56586197 | 24 | #include "sha256.h" |
6fc6879b JM |
25 | #include "rc4.h" |
26 | #include "aes_wrap.h" | |
27 | #include "crypto.h" | |
28 | #include "eloop.h" | |
29 | #include "ieee802_11.h" | |
30 | #include "pmksa_cache.h" | |
31 | #include "state_machine.h" | |
32 | #include "wpa_auth_i.h" | |
33 | #include "wpa_auth_ie.h" | |
34 | ||
35 | #define STATE_MACHINE_DATA struct wpa_state_machine | |
36 | #define STATE_MACHINE_DEBUG_PREFIX "WPA" | |
37 | #define STATE_MACHINE_ADDR sm->addr | |
38 | ||
39 | ||
40 | static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx); | |
41 | static void wpa_sm_step(struct wpa_state_machine *sm); | |
42 | static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len); | |
43 | static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx); | |
44 | static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth, | |
45 | struct wpa_group *group); | |
46 | ||
47 | /* Default timeouts are 100 ms, but this seems to be a bit too fast for most | |
48 | * WPA Supplicants, so use a bit longer timeout. */ | |
49 | static const u32 dot11RSNAConfigGroupUpdateTimeOut = 1000; /* ms */ | |
50 | static const u32 dot11RSNAConfigGroupUpdateCount = 3; | |
51 | static const u32 dot11RSNAConfigPairwiseUpdateTimeOut = 1000; /* ms */ | |
52 | static const u32 dot11RSNAConfigPairwiseUpdateCount = 3; | |
53 | ||
54 | /* TODO: make these configurable */ | |
55 | static const int dot11RSNAConfigPMKLifetime = 43200; | |
56 | static const int dot11RSNAConfigPMKReauthThreshold = 70; | |
57 | static const int dot11RSNAConfigSATimeout = 60; | |
58 | ||
59 | ||
60 | static inline void wpa_auth_mic_failure_report( | |
61 | struct wpa_authenticator *wpa_auth, const u8 *addr) | |
62 | { | |
63 | if (wpa_auth->cb.mic_failure_report) | |
64 | wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr); | |
65 | } | |
66 | ||
67 | ||
68 | static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth, | |
69 | const u8 *addr, wpa_eapol_variable var, | |
70 | int value) | |
71 | { | |
72 | if (wpa_auth->cb.set_eapol) | |
73 | wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value); | |
74 | } | |
75 | ||
76 | ||
77 | static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth, | |
78 | const u8 *addr, wpa_eapol_variable var) | |
79 | { | |
80 | if (wpa_auth->cb.get_eapol == NULL) | |
81 | return -1; | |
82 | return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var); | |
83 | } | |
84 | ||
85 | ||
86 | static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth, | |
87 | const u8 *addr, const u8 *prev_psk) | |
88 | { | |
89 | if (wpa_auth->cb.get_psk == NULL) | |
90 | return NULL; | |
91 | return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, prev_psk); | |
92 | } | |
93 | ||
94 | ||
95 | static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth, | |
96 | const u8 *addr, u8 *msk, size_t *len) | |
97 | { | |
98 | if (wpa_auth->cb.get_msk == NULL) | |
99 | return -1; | |
100 | return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len); | |
101 | } | |
102 | ||
103 | ||
104 | static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth, | |
105 | int vlan_id, | |
106 | const char *alg, const u8 *addr, int idx, | |
107 | u8 *key, size_t key_len) | |
108 | { | |
109 | if (wpa_auth->cb.set_key == NULL) | |
110 | return -1; | |
111 | return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx, | |
112 | key, key_len); | |
113 | } | |
114 | ||
115 | ||
116 | static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth, | |
117 | const u8 *addr, int idx, u8 *seq) | |
118 | { | |
119 | if (wpa_auth->cb.get_seqnum == NULL) | |
120 | return -1; | |
121 | return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq); | |
122 | } | |
123 | ||
124 | ||
125 | static inline int wpa_auth_get_seqnum_igtk(struct wpa_authenticator *wpa_auth, | |
126 | const u8 *addr, int idx, u8 *seq) | |
127 | { | |
128 | if (wpa_auth->cb.get_seqnum_igtk == NULL) | |
129 | return -1; | |
130 | return wpa_auth->cb.get_seqnum_igtk(wpa_auth->cb.ctx, addr, idx, seq); | |
131 | } | |
132 | ||
133 | ||
134 | static inline int | |
135 | wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr, | |
136 | const u8 *data, size_t data_len, int encrypt) | |
137 | { | |
138 | if (wpa_auth->cb.send_eapol == NULL) | |
139 | return -1; | |
140 | return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len, | |
141 | encrypt); | |
142 | } | |
143 | ||
144 | ||
145 | int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth, | |
146 | int (*cb)(struct wpa_state_machine *sm, void *ctx), | |
147 | void *cb_ctx) | |
148 | { | |
149 | if (wpa_auth->cb.for_each_sta == NULL) | |
150 | return 0; | |
151 | return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx); | |
152 | } | |
153 | ||
154 | ||
bf98f7f3 JM |
155 | int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth, |
156 | int (*cb)(struct wpa_authenticator *a, void *ctx), | |
157 | void *cb_ctx) | |
158 | { | |
159 | if (wpa_auth->cb.for_each_auth == NULL) | |
160 | return 0; | |
161 | return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx); | |
162 | } | |
163 | ||
164 | ||
6fc6879b JM |
165 | void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr, |
166 | logger_level level, const char *txt) | |
167 | { | |
168 | if (wpa_auth->cb.logger == NULL) | |
169 | return; | |
170 | wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt); | |
171 | } | |
172 | ||
173 | ||
174 | void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr, | |
175 | logger_level level, const char *fmt, ...) | |
176 | { | |
177 | char *format; | |
178 | int maxlen; | |
179 | va_list ap; | |
180 | ||
181 | if (wpa_auth->cb.logger == NULL) | |
182 | return; | |
183 | ||
184 | maxlen = os_strlen(fmt) + 100; | |
185 | format = os_malloc(maxlen); | |
186 | if (!format) | |
187 | return; | |
188 | ||
189 | va_start(ap, fmt); | |
190 | vsnprintf(format, maxlen, fmt, ap); | |
191 | va_end(ap); | |
192 | ||
193 | wpa_auth_logger(wpa_auth, addr, level, format); | |
194 | ||
195 | os_free(format); | |
196 | } | |
197 | ||
198 | ||
199 | static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth, | |
200 | const u8 *addr) | |
201 | { | |
202 | if (wpa_auth->cb.disconnect == NULL) | |
203 | return; | |
204 | wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr, | |
205 | WLAN_REASON_PREV_AUTH_NOT_VALID); | |
206 | } | |
207 | ||
208 | ||
209 | static int wpa_use_aes_cmac(struct wpa_state_machine *sm) | |
210 | { | |
56586197 | 211 | int ret = 0; |
6fc6879b | 212 | #ifdef CONFIG_IEEE80211R |
56586197 JM |
213 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) |
214 | ret = 1; | |
6fc6879b | 215 | #endif /* CONFIG_IEEE80211R */ |
56586197 JM |
216 | #ifdef CONFIG_IEEE80211W |
217 | if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt)) | |
218 | ret = 1; | |
219 | #endif /* CONFIG_IEEE80211W */ | |
220 | return ret; | |
6fc6879b JM |
221 | } |
222 | ||
223 | ||
224 | static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx) | |
225 | { | |
226 | struct wpa_authenticator *wpa_auth = eloop_ctx; | |
227 | ||
228 | if (os_get_random(wpa_auth->group->GMK, WPA_GMK_LEN)) { | |
229 | wpa_printf(MSG_ERROR, "Failed to get random data for WPA " | |
230 | "initialization."); | |
231 | } else { | |
232 | wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd"); | |
233 | } | |
234 | ||
235 | if (wpa_auth->conf.wpa_gmk_rekey) { | |
236 | eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0, | |
237 | wpa_rekey_gmk, wpa_auth, NULL); | |
238 | } | |
239 | } | |
240 | ||
241 | ||
242 | static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx) | |
243 | { | |
244 | struct wpa_authenticator *wpa_auth = eloop_ctx; | |
245 | struct wpa_group *group; | |
246 | ||
247 | wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK"); | |
248 | for (group = wpa_auth->group; group; group = group->next) { | |
249 | group->GTKReKey = TRUE; | |
250 | do { | |
251 | group->changed = FALSE; | |
252 | wpa_group_sm_step(wpa_auth, group); | |
253 | } while (group->changed); | |
254 | } | |
255 | ||
256 | if (wpa_auth->conf.wpa_group_rekey) { | |
257 | eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, | |
258 | 0, wpa_rekey_gtk, wpa_auth, NULL); | |
259 | } | |
260 | } | |
261 | ||
262 | ||
263 | static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx) | |
264 | { | |
265 | if (sm->pmksa == ctx) | |
266 | sm->pmksa = NULL; | |
267 | return 0; | |
268 | } | |
269 | ||
270 | ||
271 | static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry, | |
272 | void *ctx) | |
273 | { | |
274 | struct wpa_authenticator *wpa_auth = ctx; | |
275 | wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry); | |
276 | } | |
277 | ||
278 | ||
279 | static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth, | |
280 | int vlan_id) | |
281 | { | |
282 | struct wpa_group *group; | |
283 | u8 buf[ETH_ALEN + 8 + sizeof(group)]; | |
284 | u8 rkey[32]; | |
285 | ||
286 | group = os_zalloc(sizeof(struct wpa_group)); | |
287 | if (group == NULL) | |
288 | return NULL; | |
289 | ||
290 | group->GTKAuthenticator = TRUE; | |
291 | group->vlan_id = vlan_id; | |
292 | ||
293 | switch (wpa_auth->conf.wpa_group) { | |
294 | case WPA_CIPHER_CCMP: | |
295 | group->GTK_len = 16; | |
296 | break; | |
297 | case WPA_CIPHER_TKIP: | |
298 | group->GTK_len = 32; | |
299 | break; | |
300 | case WPA_CIPHER_WEP104: | |
301 | group->GTK_len = 13; | |
302 | break; | |
303 | case WPA_CIPHER_WEP40: | |
304 | group->GTK_len = 5; | |
305 | break; | |
306 | } | |
307 | ||
308 | /* Counter = PRF-256(Random number, "Init Counter", | |
309 | * Local MAC Address || Time) | |
310 | */ | |
311 | os_memcpy(buf, wpa_auth->addr, ETH_ALEN); | |
312 | wpa_get_ntp_timestamp(buf + ETH_ALEN); | |
313 | os_memcpy(buf + ETH_ALEN + 8, &group, sizeof(group)); | |
314 | if (os_get_random(rkey, sizeof(rkey)) || | |
315 | os_get_random(group->GMK, WPA_GMK_LEN)) { | |
316 | wpa_printf(MSG_ERROR, "Failed to get random data for WPA " | |
317 | "initialization."); | |
318 | os_free(group); | |
319 | return NULL; | |
320 | } | |
321 | ||
322 | sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf), | |
323 | group->Counter, WPA_NONCE_LEN); | |
324 | ||
325 | group->GInit = TRUE; | |
326 | wpa_group_sm_step(wpa_auth, group); | |
327 | group->GInit = FALSE; | |
328 | wpa_group_sm_step(wpa_auth, group); | |
329 | ||
330 | return group; | |
331 | } | |
332 | ||
333 | ||
334 | /** | |
335 | * wpa_init - Initialize WPA authenticator | |
336 | * @addr: Authenticator address | |
337 | * @conf: Configuration for WPA authenticator | |
338 | * Returns: Pointer to WPA authenticator data or %NULL on failure | |
339 | */ | |
340 | struct wpa_authenticator * wpa_init(const u8 *addr, | |
341 | struct wpa_auth_config *conf, | |
342 | struct wpa_auth_callbacks *cb) | |
343 | { | |
344 | struct wpa_authenticator *wpa_auth; | |
345 | ||
346 | wpa_auth = os_zalloc(sizeof(struct wpa_authenticator)); | |
347 | if (wpa_auth == NULL) | |
348 | return NULL; | |
349 | os_memcpy(wpa_auth->addr, addr, ETH_ALEN); | |
350 | os_memcpy(&wpa_auth->conf, conf, sizeof(*conf)); | |
351 | os_memcpy(&wpa_auth->cb, cb, sizeof(*cb)); | |
352 | ||
353 | if (wpa_auth_gen_wpa_ie(wpa_auth)) { | |
354 | wpa_printf(MSG_ERROR, "Could not generate WPA IE."); | |
355 | os_free(wpa_auth); | |
356 | return NULL; | |
357 | } | |
358 | ||
359 | wpa_auth->group = wpa_group_init(wpa_auth, 0); | |
360 | if (wpa_auth->group == NULL) { | |
361 | os_free(wpa_auth->wpa_ie); | |
362 | os_free(wpa_auth); | |
363 | return NULL; | |
364 | } | |
365 | ||
366 | wpa_auth->pmksa = pmksa_cache_init(wpa_auth_pmksa_free_cb, wpa_auth); | |
367 | if (wpa_auth->pmksa == NULL) { | |
368 | wpa_printf(MSG_ERROR, "PMKSA cache initialization failed."); | |
369 | os_free(wpa_auth->wpa_ie); | |
370 | os_free(wpa_auth); | |
371 | return NULL; | |
372 | } | |
373 | ||
374 | #ifdef CONFIG_IEEE80211R | |
375 | wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init(); | |
376 | if (wpa_auth->ft_pmk_cache == NULL) { | |
377 | wpa_printf(MSG_ERROR, "FT PMK cache initialization failed."); | |
378 | os_free(wpa_auth->wpa_ie); | |
379 | pmksa_cache_deinit(wpa_auth->pmksa); | |
380 | os_free(wpa_auth); | |
381 | return NULL; | |
382 | } | |
383 | #endif /* CONFIG_IEEE80211R */ | |
384 | ||
385 | if (wpa_auth->conf.wpa_gmk_rekey) { | |
386 | eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0, | |
387 | wpa_rekey_gmk, wpa_auth, NULL); | |
388 | } | |
389 | ||
390 | if (wpa_auth->conf.wpa_group_rekey) { | |
391 | eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0, | |
392 | wpa_rekey_gtk, wpa_auth, NULL); | |
393 | } | |
394 | ||
395 | return wpa_auth; | |
396 | } | |
397 | ||
398 | ||
399 | /** | |
400 | * wpa_deinit - Deinitialize WPA authenticator | |
401 | * @wpa_auth: Pointer to WPA authenticator data from wpa_init() | |
402 | */ | |
403 | void wpa_deinit(struct wpa_authenticator *wpa_auth) | |
404 | { | |
405 | struct wpa_group *group, *prev; | |
406 | ||
407 | eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL); | |
408 | eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL); | |
409 | ||
410 | #ifdef CONFIG_PEERKEY | |
411 | while (wpa_auth->stsl_negotiations) | |
412 | wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations); | |
413 | #endif /* CONFIG_PEERKEY */ | |
414 | ||
415 | pmksa_cache_deinit(wpa_auth->pmksa); | |
416 | ||
417 | #ifdef CONFIG_IEEE80211R | |
418 | wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache); | |
419 | wpa_auth->ft_pmk_cache = NULL; | |
420 | #endif /* CONFIG_IEEE80211R */ | |
421 | ||
422 | os_free(wpa_auth->wpa_ie); | |
423 | ||
424 | group = wpa_auth->group; | |
425 | while (group) { | |
426 | prev = group; | |
427 | group = group->next; | |
428 | os_free(prev); | |
429 | } | |
430 | ||
431 | os_free(wpa_auth); | |
432 | } | |
433 | ||
434 | ||
435 | /** | |
436 | * wpa_reconfig - Update WPA authenticator configuration | |
437 | * @wpa_auth: Pointer to WPA authenticator data from wpa_init() | |
438 | * @conf: Configuration for WPA authenticator | |
439 | */ | |
440 | int wpa_reconfig(struct wpa_authenticator *wpa_auth, | |
441 | struct wpa_auth_config *conf) | |
442 | { | |
443 | if (wpa_auth == NULL) | |
444 | return 0; | |
445 | ||
446 | os_memcpy(&wpa_auth->conf, conf, sizeof(*conf)); | |
447 | /* | |
448 | * TODO: | |
449 | * Disassociate stations if configuration changed | |
450 | * Update WPA/RSN IE | |
451 | */ | |
452 | return 0; | |
453 | } | |
454 | ||
455 | ||
456 | struct wpa_state_machine * | |
457 | wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr) | |
458 | { | |
459 | struct wpa_state_machine *sm; | |
460 | ||
461 | sm = os_zalloc(sizeof(struct wpa_state_machine)); | |
462 | if (sm == NULL) | |
463 | return NULL; | |
464 | os_memcpy(sm->addr, addr, ETH_ALEN); | |
465 | ||
466 | sm->wpa_auth = wpa_auth; | |
467 | sm->group = wpa_auth->group; | |
468 | ||
469 | return sm; | |
470 | } | |
471 | ||
472 | ||
473 | void wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth, | |
474 | struct wpa_state_machine *sm) | |
475 | { | |
476 | if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL) | |
477 | return; | |
478 | ||
479 | #ifdef CONFIG_IEEE80211R | |
480 | if (sm->ft_completed) { | |
481 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
482 | "FT authentication already completed - do not " | |
483 | "start 4-way handshake"); | |
484 | return; | |
485 | } | |
486 | #endif /* CONFIG_IEEE80211R */ | |
487 | ||
488 | if (sm->started) { | |
489 | os_memset(sm->key_replay_counter, 0, WPA_REPLAY_COUNTER_LEN); | |
490 | sm->ReAuthenticationRequest = TRUE; | |
491 | wpa_sm_step(sm); | |
492 | return; | |
493 | } | |
494 | ||
495 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
496 | "start authentication"); | |
497 | sm->started = 1; | |
498 | ||
499 | sm->Init = TRUE; | |
500 | wpa_sm_step(sm); | |
501 | sm->Init = FALSE; | |
502 | sm->AuthenticationRequest = TRUE; | |
503 | wpa_sm_step(sm); | |
504 | } | |
505 | ||
506 | ||
507 | static void wpa_free_sta_sm(struct wpa_state_machine *sm) | |
508 | { | |
509 | os_free(sm->last_rx_eapol_key); | |
510 | os_free(sm->wpa_ie); | |
511 | os_free(sm); | |
512 | } | |
513 | ||
514 | ||
515 | void wpa_auth_sta_deinit(struct wpa_state_machine *sm) | |
516 | { | |
517 | if (sm == NULL) | |
518 | return; | |
519 | ||
520 | if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) { | |
521 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
522 | "strict rekeying - force GTK rekey since STA " | |
523 | "is leaving"); | |
524 | eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL); | |
525 | eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth, | |
526 | NULL); | |
527 | } | |
528 | ||
529 | eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm); | |
530 | eloop_cancel_timeout(wpa_sm_call_step, sm, NULL); | |
531 | if (sm->in_step_loop) { | |
532 | /* Must not free state machine while wpa_sm_step() is running. | |
533 | * Freeing will be completed in the end of wpa_sm_step(). */ | |
534 | wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state " | |
535 | "machine deinit for " MACSTR, MAC2STR(sm->addr)); | |
536 | sm->pending_deinit = 1; | |
537 | } else | |
538 | wpa_free_sta_sm(sm); | |
539 | } | |
540 | ||
541 | ||
542 | static void wpa_request_new_ptk(struct wpa_state_machine *sm) | |
543 | { | |
544 | if (sm == NULL) | |
545 | return; | |
546 | ||
547 | sm->PTKRequest = TRUE; | |
548 | sm->PTK_valid = 0; | |
549 | } | |
550 | ||
551 | ||
552 | void wpa_receive(struct wpa_authenticator *wpa_auth, | |
553 | struct wpa_state_machine *sm, | |
554 | u8 *data, size_t data_len) | |
555 | { | |
556 | struct ieee802_1x_hdr *hdr; | |
557 | struct wpa_eapol_key *key; | |
558 | u16 key_info, key_data_length; | |
559 | enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST, | |
560 | SMK_M1, SMK_M3, SMK_ERROR } msg; | |
561 | char *msgtxt; | |
562 | struct wpa_eapol_ie_parse kde; | |
563 | ||
564 | if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL) | |
565 | return; | |
566 | ||
567 | if (data_len < sizeof(*hdr) + sizeof(*key)) | |
568 | return; | |
569 | ||
570 | hdr = (struct ieee802_1x_hdr *) data; | |
571 | key = (struct wpa_eapol_key *) (hdr + 1); | |
572 | key_info = WPA_GET_BE16(key->key_info); | |
573 | key_data_length = WPA_GET_BE16(key->key_data_length); | |
574 | if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) { | |
575 | wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - " | |
576 | "key_data overflow (%d > %lu)", | |
577 | key_data_length, | |
578 | (unsigned long) (data_len - sizeof(*hdr) - | |
579 | sizeof(*key))); | |
580 | return; | |
581 | } | |
582 | ||
583 | /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys | |
584 | * are set */ | |
585 | ||
586 | if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) == | |
587 | (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) { | |
588 | if (key_info & WPA_KEY_INFO_ERROR) { | |
589 | msg = SMK_ERROR; | |
590 | msgtxt = "SMK Error"; | |
591 | } else { | |
592 | msg = SMK_M1; | |
593 | msgtxt = "SMK M1"; | |
594 | } | |
595 | } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) { | |
596 | msg = SMK_M3; | |
597 | msgtxt = "SMK M3"; | |
598 | } else if (key_info & WPA_KEY_INFO_REQUEST) { | |
599 | msg = REQUEST; | |
600 | msgtxt = "Request"; | |
601 | } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) { | |
602 | msg = GROUP_2; | |
603 | msgtxt = "2/2 Group"; | |
604 | } else if (key_data_length == 0) { | |
605 | msg = PAIRWISE_4; | |
606 | msgtxt = "4/4 Pairwise"; | |
607 | } else { | |
608 | msg = PAIRWISE_2; | |
609 | msgtxt = "2/4 Pairwise"; | |
610 | } | |
611 | ||
612 | /* TODO: key_info type validation for PeerKey */ | |
613 | if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 || | |
614 | msg == GROUP_2) { | |
615 | u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK; | |
616 | if (sm->pairwise == WPA_CIPHER_CCMP) { | |
617 | if (wpa_use_aes_cmac(sm) && | |
618 | ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) { | |
619 | wpa_auth_logger(wpa_auth, sm->addr, | |
620 | LOGGER_WARNING, | |
621 | "advertised support for " | |
622 | "AES-128-CMAC, but did not " | |
623 | "use it"); | |
624 | return; | |
625 | } | |
626 | ||
627 | if (!wpa_use_aes_cmac(sm) && | |
628 | ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) { | |
629 | wpa_auth_logger(wpa_auth, sm->addr, | |
630 | LOGGER_WARNING, | |
631 | "did not use HMAC-SHA1-AES " | |
632 | "with CCMP"); | |
633 | return; | |
634 | } | |
635 | } | |
636 | } | |
637 | ||
638 | if (key_info & WPA_KEY_INFO_REQUEST) { | |
639 | if (sm->req_replay_counter_used && | |
640 | os_memcmp(key->replay_counter, sm->req_replay_counter, | |
641 | WPA_REPLAY_COUNTER_LEN) <= 0) { | |
642 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING, | |
643 | "received EAPOL-Key request with " | |
644 | "replayed counter"); | |
645 | return; | |
646 | } | |
647 | } | |
648 | ||
649 | if (!(key_info & WPA_KEY_INFO_REQUEST) && | |
650 | (!sm->key_replay_counter_valid || | |
651 | os_memcmp(key->replay_counter, sm->key_replay_counter, | |
652 | WPA_REPLAY_COUNTER_LEN) != 0)) { | |
653 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, | |
654 | "received EAPOL-Key %s with unexpected " | |
655 | "replay counter", msgtxt); | |
656 | wpa_hexdump(MSG_DEBUG, "expected replay counter", | |
657 | sm->key_replay_counter, WPA_REPLAY_COUNTER_LEN); | |
658 | wpa_hexdump(MSG_DEBUG, "received replay counter", | |
659 | key->replay_counter, WPA_REPLAY_COUNTER_LEN); | |
660 | return; | |
661 | } | |
662 | ||
663 | switch (msg) { | |
664 | case PAIRWISE_2: | |
665 | if (sm->wpa_ptk_state != WPA_PTK_PTKSTART && | |
666 | sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING) { | |
667 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, | |
668 | "received EAPOL-Key msg 2/4 in " | |
669 | "invalid state (%d) - dropped", | |
670 | sm->wpa_ptk_state); | |
671 | return; | |
672 | } | |
673 | if (sm->wpa_ie == NULL || | |
674 | sm->wpa_ie_len != key_data_length || | |
675 | os_memcmp(sm->wpa_ie, key + 1, key_data_length) != 0) { | |
676 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
677 | "WPA IE from (Re)AssocReq did not " | |
678 | "match with msg 2/4"); | |
679 | if (sm->wpa_ie) { | |
680 | wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq", | |
681 | sm->wpa_ie, sm->wpa_ie_len); | |
682 | } | |
683 | wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4", | |
684 | (u8 *) (key + 1), key_data_length); | |
685 | /* MLME-DEAUTHENTICATE.request */ | |
686 | wpa_sta_disconnect(wpa_auth, sm->addr); | |
687 | return; | |
688 | } | |
689 | break; | |
690 | case PAIRWISE_4: | |
691 | if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING || | |
692 | !sm->PTK_valid) { | |
693 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, | |
694 | "received EAPOL-Key msg 4/4 in " | |
695 | "invalid state (%d) - dropped", | |
696 | sm->wpa_ptk_state); | |
697 | return; | |
698 | } | |
699 | break; | |
700 | case GROUP_2: | |
701 | if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING | |
702 | || !sm->PTK_valid) { | |
703 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, | |
704 | "received EAPOL-Key msg 2/2 in " | |
705 | "invalid state (%d) - dropped", | |
706 | sm->wpa_ptk_group_state); | |
707 | return; | |
708 | } | |
709 | break; | |
710 | #ifdef CONFIG_PEERKEY | |
711 | case SMK_M1: | |
712 | case SMK_M3: | |
713 | case SMK_ERROR: | |
714 | if (!wpa_auth->conf.peerkey) { | |
715 | wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but " | |
716 | "PeerKey use disabled - ignoring message"); | |
717 | return; | |
718 | } | |
719 | if (!sm->PTK_valid) { | |
720 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
721 | "received EAPOL-Key msg SMK in " | |
722 | "invalid state - dropped"); | |
723 | return; | |
724 | } | |
725 | break; | |
726 | #else /* CONFIG_PEERKEY */ | |
727 | case SMK_M1: | |
728 | case SMK_M3: | |
729 | case SMK_ERROR: | |
730 | return; /* STSL disabled - ignore SMK messages */ | |
731 | #endif /* CONFIG_PEERKEY */ | |
732 | case REQUEST: | |
733 | break; | |
734 | } | |
735 | ||
736 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
737 | "received EAPOL-Key frame (%s)", msgtxt); | |
738 | ||
739 | if (key_info & WPA_KEY_INFO_ACK) { | |
740 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
741 | "received invalid EAPOL-Key: Key Ack set"); | |
742 | return; | |
743 | } | |
744 | ||
745 | if (!(key_info & WPA_KEY_INFO_MIC)) { | |
746 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
747 | "received invalid EAPOL-Key: Key MIC not set"); | |
748 | return; | |
749 | } | |
750 | ||
751 | sm->MICVerified = FALSE; | |
752 | if (sm->PTK_valid) { | |
753 | if (wpa_verify_key_mic(&sm->PTK, data, data_len)) { | |
754 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
755 | "received EAPOL-Key with invalid MIC"); | |
756 | return; | |
757 | } | |
758 | sm->MICVerified = TRUE; | |
759 | eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm); | |
760 | } | |
761 | ||
762 | if (key_info & WPA_KEY_INFO_REQUEST) { | |
763 | if (sm->MICVerified) { | |
764 | sm->req_replay_counter_used = 1; | |
765 | os_memcpy(sm->req_replay_counter, key->replay_counter, | |
766 | WPA_REPLAY_COUNTER_LEN); | |
767 | } else { | |
768 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
769 | "received EAPOL-Key request with " | |
770 | "invalid MIC"); | |
771 | return; | |
772 | } | |
773 | ||
774 | /* | |
775 | * TODO: should decrypt key data field if encryption was used; | |
776 | * even though MAC address KDE is not normally encrypted, | |
777 | * supplicant is allowed to encrypt it. | |
778 | */ | |
779 | if (msg == SMK_ERROR) { | |
780 | #ifdef CONFIG_PEERKEY | |
781 | wpa_smk_error(wpa_auth, sm, key); | |
782 | #endif /* CONFIG_PEERKEY */ | |
783 | return; | |
784 | } else if (key_info & WPA_KEY_INFO_ERROR) { | |
785 | /* Supplicant reported a Michael MIC error */ | |
786 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
787 | "received EAPOL-Key Error Request " | |
788 | "(STA detected Michael MIC failure)"); | |
789 | wpa_auth_mic_failure_report(wpa_auth, sm->addr); | |
790 | sm->dot11RSNAStatsTKIPRemoteMICFailures++; | |
791 | wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++; | |
792 | /* Error report is not a request for a new key | |
793 | * handshake, but since Authenticator may do it, let's | |
794 | * change the keys now anyway. */ | |
795 | wpa_request_new_ptk(sm); | |
796 | } else if (key_info & WPA_KEY_INFO_KEY_TYPE) { | |
797 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
798 | "received EAPOL-Key Request for new " | |
799 | "4-Way Handshake"); | |
800 | wpa_request_new_ptk(sm); | |
801 | #ifdef CONFIG_PEERKEY | |
802 | } else if (msg == SMK_M1) { | |
803 | wpa_smk_m1(wpa_auth, sm, key); | |
804 | #endif /* CONFIG_PEERKEY */ | |
805 | } else if (key_data_length > 0 && | |
806 | wpa_parse_kde_ies((const u8 *) (key + 1), | |
807 | key_data_length, &kde) == 0 && | |
808 | kde.mac_addr) { | |
809 | } else { | |
810 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
811 | "received EAPOL-Key Request for GTK " | |
812 | "rekeying"); | |
813 | /* FIX: why was this triggering PTK rekeying for the | |
814 | * STA that requested Group Key rekeying?? */ | |
815 | /* wpa_request_new_ptk(sta->wpa_sm); */ | |
816 | eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL); | |
817 | wpa_rekey_gtk(wpa_auth, NULL); | |
818 | } | |
819 | } else { | |
820 | /* Do not allow the same key replay counter to be reused. */ | |
821 | sm->key_replay_counter_valid = FALSE; | |
822 | } | |
823 | ||
824 | #ifdef CONFIG_PEERKEY | |
825 | if (msg == SMK_M3) { | |
826 | wpa_smk_m3(wpa_auth, sm, key); | |
827 | return; | |
828 | } | |
829 | #endif /* CONFIG_PEERKEY */ | |
830 | ||
831 | os_free(sm->last_rx_eapol_key); | |
832 | sm->last_rx_eapol_key = os_malloc(data_len); | |
833 | if (sm->last_rx_eapol_key == NULL) | |
834 | return; | |
835 | os_memcpy(sm->last_rx_eapol_key, data, data_len); | |
836 | sm->last_rx_eapol_key_len = data_len; | |
837 | ||
838 | sm->EAPOLKeyReceived = TRUE; | |
839 | sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE); | |
840 | sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST); | |
841 | os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN); | |
842 | wpa_sm_step(sm); | |
843 | } | |
844 | ||
845 | ||
846 | static void wpa_gmk_to_gtk(const u8 *gmk, const u8 *addr, const u8 *gnonce, | |
847 | u8 *gtk, size_t gtk_len) | |
848 | { | |
849 | u8 data[ETH_ALEN + WPA_NONCE_LEN]; | |
850 | ||
851 | /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */ | |
852 | os_memcpy(data, addr, ETH_ALEN); | |
853 | os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN); | |
854 | ||
56586197 JM |
855 | #ifdef CONFIG_IEEE80211W |
856 | sha256_prf(gmk, WPA_GMK_LEN, "Group key expansion", | |
857 | data, sizeof(data), gtk, gtk_len); | |
858 | #else /* CONFIG_IEEE80211W */ | |
6fc6879b JM |
859 | sha1_prf(gmk, WPA_GMK_LEN, "Group key expansion", |
860 | data, sizeof(data), gtk, gtk_len); | |
56586197 | 861 | #endif /* CONFIG_IEEE80211W */ |
6fc6879b JM |
862 | |
863 | wpa_hexdump_key(MSG_DEBUG, "GMK", gmk, WPA_GMK_LEN); | |
864 | wpa_hexdump_key(MSG_DEBUG, "GTK", gtk, gtk_len); | |
865 | } | |
866 | ||
867 | ||
868 | static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx) | |
869 | { | |
870 | struct wpa_authenticator *wpa_auth = eloop_ctx; | |
871 | struct wpa_state_machine *sm = timeout_ctx; | |
872 | ||
873 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout"); | |
874 | sm->TimeoutEvt = TRUE; | |
875 | wpa_sm_step(sm); | |
876 | } | |
877 | ||
878 | ||
879 | void __wpa_send_eapol(struct wpa_authenticator *wpa_auth, | |
880 | struct wpa_state_machine *sm, int key_info, | |
881 | const u8 *key_rsc, const u8 *nonce, | |
882 | const u8 *kde, size_t kde_len, | |
883 | int keyidx, int encr, int force_version) | |
884 | { | |
885 | struct ieee802_1x_hdr *hdr; | |
886 | struct wpa_eapol_key *key; | |
887 | size_t len; | |
888 | int alg; | |
889 | int key_data_len, pad_len = 0; | |
890 | u8 *buf, *pos; | |
891 | int version, pairwise; | |
892 | ||
893 | len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key); | |
894 | ||
895 | if (force_version) | |
896 | version = force_version; | |
897 | else if (wpa_use_aes_cmac(sm)) | |
898 | version = WPA_KEY_INFO_TYPE_AES_128_CMAC; | |
899 | else if (sm->pairwise == WPA_CIPHER_CCMP) | |
900 | version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES; | |
901 | else | |
902 | version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4; | |
903 | ||
904 | pairwise = key_info & WPA_KEY_INFO_KEY_TYPE; | |
905 | ||
906 | wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d " | |
907 | "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d " | |
908 | "encr=%d)", | |
909 | version, | |
910 | (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0, | |
911 | (key_info & WPA_KEY_INFO_MIC) ? 1 : 0, | |
912 | (key_info & WPA_KEY_INFO_ACK) ? 1 : 0, | |
913 | (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0, | |
914 | pairwise, (unsigned long) kde_len, keyidx, encr); | |
915 | ||
916 | key_data_len = kde_len; | |
917 | ||
918 | if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES || | |
919 | version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) { | |
920 | pad_len = key_data_len % 8; | |
921 | if (pad_len) | |
922 | pad_len = 8 - pad_len; | |
923 | key_data_len += pad_len + 8; | |
924 | } | |
925 | ||
926 | len += key_data_len; | |
927 | ||
928 | hdr = os_zalloc(len); | |
929 | if (hdr == NULL) | |
930 | return; | |
931 | hdr->version = wpa_auth->conf.eapol_version; | |
932 | hdr->type = IEEE802_1X_TYPE_EAPOL_KEY; | |
933 | hdr->length = host_to_be16(len - sizeof(*hdr)); | |
934 | key = (struct wpa_eapol_key *) (hdr + 1); | |
935 | ||
936 | key->type = sm->wpa == WPA_VERSION_WPA2 ? | |
937 | EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA; | |
938 | key_info |= version; | |
939 | if (encr && sm->wpa == WPA_VERSION_WPA2) | |
940 | key_info |= WPA_KEY_INFO_ENCR_KEY_DATA; | |
941 | if (sm->wpa != WPA_VERSION_WPA2) | |
942 | key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT; | |
943 | WPA_PUT_BE16(key->key_info, key_info); | |
944 | ||
945 | alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group; | |
946 | switch (alg) { | |
947 | case WPA_CIPHER_CCMP: | |
948 | WPA_PUT_BE16(key->key_length, 16); | |
949 | break; | |
950 | case WPA_CIPHER_TKIP: | |
951 | WPA_PUT_BE16(key->key_length, 32); | |
952 | break; | |
953 | case WPA_CIPHER_WEP40: | |
954 | WPA_PUT_BE16(key->key_length, 5); | |
955 | break; | |
956 | case WPA_CIPHER_WEP104: | |
957 | WPA_PUT_BE16(key->key_length, 13); | |
958 | break; | |
959 | } | |
960 | if (key_info & WPA_KEY_INFO_SMK_MESSAGE) | |
961 | WPA_PUT_BE16(key->key_length, 0); | |
962 | ||
963 | /* FIX: STSL: what to use as key_replay_counter? */ | |
964 | inc_byte_array(sm->key_replay_counter, WPA_REPLAY_COUNTER_LEN); | |
965 | os_memcpy(key->replay_counter, sm->key_replay_counter, | |
966 | WPA_REPLAY_COUNTER_LEN); | |
967 | sm->key_replay_counter_valid = TRUE; | |
968 | ||
969 | if (nonce) | |
970 | os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN); | |
971 | ||
972 | if (key_rsc) | |
973 | os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN); | |
974 | ||
975 | if (kde && !encr) { | |
976 | os_memcpy(key + 1, kde, kde_len); | |
977 | WPA_PUT_BE16(key->key_data_length, kde_len); | |
978 | } else if (encr && kde) { | |
979 | buf = os_zalloc(key_data_len); | |
980 | if (buf == NULL) { | |
981 | os_free(hdr); | |
982 | return; | |
983 | } | |
984 | pos = buf; | |
985 | os_memcpy(pos, kde, kde_len); | |
986 | pos += kde_len; | |
987 | ||
988 | if (pad_len) | |
989 | *pos++ = 0xdd; | |
990 | ||
991 | wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data", | |
992 | buf, key_data_len); | |
993 | if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES || | |
994 | version == WPA_KEY_INFO_TYPE_AES_128_CMAC) { | |
995 | if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf, | |
996 | (u8 *) (key + 1))) { | |
997 | os_free(hdr); | |
998 | os_free(buf); | |
999 | return; | |
1000 | } | |
1001 | WPA_PUT_BE16(key->key_data_length, key_data_len); | |
1002 | } else { | |
1003 | u8 ek[32]; | |
1004 | os_memcpy(key->key_iv, | |
1005 | sm->group->Counter + WPA_NONCE_LEN - 16, 16); | |
1006 | inc_byte_array(sm->group->Counter, WPA_NONCE_LEN); | |
1007 | os_memcpy(ek, key->key_iv, 16); | |
1008 | os_memcpy(ek + 16, sm->PTK.kek, 16); | |
1009 | os_memcpy(key + 1, buf, key_data_len); | |
1010 | rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len); | |
1011 | WPA_PUT_BE16(key->key_data_length, key_data_len); | |
1012 | } | |
1013 | os_free(buf); | |
1014 | } | |
1015 | ||
1016 | if (key_info & WPA_KEY_INFO_MIC) { | |
1017 | if (!sm->PTK_valid) { | |
1018 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
1019 | "PTK not valid when sending EAPOL-Key " | |
1020 | "frame"); | |
1021 | os_free(hdr); | |
1022 | return; | |
1023 | } | |
1024 | wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len, | |
1025 | key->key_mic); | |
1026 | } | |
1027 | ||
1028 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx, | |
1029 | 1); | |
1030 | wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len, | |
1031 | sm->pairwise_set); | |
1032 | os_free(hdr); | |
1033 | } | |
1034 | ||
1035 | ||
1036 | static void wpa_send_eapol(struct wpa_authenticator *wpa_auth, | |
1037 | struct wpa_state_machine *sm, int key_info, | |
1038 | const u8 *key_rsc, const u8 *nonce, | |
1039 | const u8 *kde, size_t kde_len, | |
1040 | int keyidx, int encr) | |
1041 | { | |
1042 | int timeout_ms; | |
1043 | int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE; | |
1044 | ||
1045 | if (sm == NULL) | |
1046 | return; | |
1047 | ||
1048 | __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len, | |
1049 | keyidx, encr, 0); | |
1050 | ||
1051 | timeout_ms = pairwise ? dot11RSNAConfigPairwiseUpdateTimeOut : | |
1052 | dot11RSNAConfigGroupUpdateTimeOut; | |
1053 | eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000, | |
1054 | wpa_send_eapol_timeout, wpa_auth, sm); | |
1055 | } | |
1056 | ||
1057 | ||
1058 | static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len) | |
1059 | { | |
1060 | struct ieee802_1x_hdr *hdr; | |
1061 | struct wpa_eapol_key *key; | |
1062 | u16 key_info; | |
1063 | int ret = 0; | |
1064 | u8 mic[16]; | |
1065 | ||
1066 | if (data_len < sizeof(*hdr) + sizeof(*key)) | |
1067 | return -1; | |
1068 | ||
1069 | hdr = (struct ieee802_1x_hdr *) data; | |
1070 | key = (struct wpa_eapol_key *) (hdr + 1); | |
1071 | key_info = WPA_GET_BE16(key->key_info); | |
1072 | os_memcpy(mic, key->key_mic, 16); | |
1073 | os_memset(key->key_mic, 0, 16); | |
1074 | if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK, | |
1075 | data, data_len, key->key_mic) || | |
1076 | os_memcmp(mic, key->key_mic, 16) != 0) | |
1077 | ret = -1; | |
1078 | os_memcpy(key->key_mic, mic, 16); | |
1079 | return ret; | |
1080 | } | |
1081 | ||
1082 | ||
1083 | void wpa_remove_ptk(struct wpa_state_machine *sm) | |
1084 | { | |
1085 | sm->PTK_valid = FALSE; | |
1086 | os_memset(&sm->PTK, 0, sizeof(sm->PTK)); | |
1087 | wpa_auth_set_key(sm->wpa_auth, 0, "none", sm->addr, 0, (u8 *) "", 0); | |
1088 | sm->pairwise_set = FALSE; | |
1089 | } | |
1090 | ||
1091 | ||
1092 | void wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event) | |
1093 | { | |
5d22a1d5 JM |
1094 | int remove_ptk = 1; |
1095 | ||
6fc6879b JM |
1096 | if (sm == NULL) |
1097 | return; | |
1098 | ||
1099 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
1100 | "event %d notification", event); | |
1101 | ||
1102 | switch (event) { | |
1103 | case WPA_AUTH: | |
1104 | case WPA_ASSOC: | |
1105 | break; | |
1106 | case WPA_DEAUTH: | |
1107 | case WPA_DISASSOC: | |
1108 | sm->DeauthenticationRequest = TRUE; | |
1109 | break; | |
1110 | case WPA_REAUTH: | |
1111 | case WPA_REAUTH_EAPOL: | |
9663596f JM |
1112 | if (sm->GUpdateStationKeys) { |
1113 | /* | |
1114 | * Reauthentication cancels the pending group key | |
1115 | * update for this STA. | |
1116 | */ | |
1117 | sm->group->GKeyDoneStations--; | |
1118 | sm->GUpdateStationKeys = FALSE; | |
1119 | sm->PtkGroupInit = TRUE; | |
1120 | } | |
6fc6879b JM |
1121 | sm->ReAuthenticationRequest = TRUE; |
1122 | break; | |
1123 | case WPA_ASSOC_FT: | |
1124 | #ifdef CONFIG_IEEE80211R | |
1125 | /* Using FT protocol, not WPA auth state machine */ | |
1126 | sm->ft_completed = 1; | |
1127 | return; | |
1128 | #else /* CONFIG_IEEE80211R */ | |
1129 | break; | |
1130 | #endif /* CONFIG_IEEE80211R */ | |
1131 | } | |
1132 | ||
1133 | #ifdef CONFIG_IEEE80211R | |
1134 | sm->ft_completed = 0; | |
1135 | #endif /* CONFIG_IEEE80211R */ | |
1136 | ||
5d22a1d5 JM |
1137 | #ifdef CONFIG_IEEE80211W |
1138 | if (sm->mgmt_frame_prot && event == WPA_AUTH) | |
1139 | remove_ptk = 0; | |
1140 | #endif /* CONFIG_IEEE80211W */ | |
6fc6879b | 1141 | |
5d22a1d5 JM |
1142 | if (remove_ptk) { |
1143 | sm->PTK_valid = FALSE; | |
1144 | os_memset(&sm->PTK, 0, sizeof(sm->PTK)); | |
1145 | ||
1146 | if (event != WPA_REAUTH_EAPOL) | |
1147 | wpa_remove_ptk(sm); | |
1148 | } | |
6fc6879b JM |
1149 | |
1150 | wpa_sm_step(sm); | |
1151 | } | |
1152 | ||
1153 | ||
1154 | static const char * wpa_alg_txt(int alg) | |
1155 | { | |
1156 | switch (alg) { | |
1157 | case WPA_CIPHER_CCMP: | |
1158 | return "CCMP"; | |
1159 | case WPA_CIPHER_TKIP: | |
1160 | return "TKIP"; | |
1161 | case WPA_CIPHER_WEP104: | |
1162 | case WPA_CIPHER_WEP40: | |
1163 | return "WEP"; | |
1164 | default: | |
1165 | return ""; | |
1166 | } | |
1167 | } | |
1168 | ||
1169 | ||
1170 | SM_STATE(WPA_PTK, INITIALIZE) | |
1171 | { | |
1172 | SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk); | |
1173 | if (sm->Init) { | |
1174 | /* Init flag is not cleared here, so avoid busy | |
1175 | * loop by claiming nothing changed. */ | |
1176 | sm->changed = FALSE; | |
1177 | } | |
1178 | ||
1179 | sm->keycount = 0; | |
1180 | if (sm->GUpdateStationKeys) | |
1181 | sm->group->GKeyDoneStations--; | |
1182 | sm->GUpdateStationKeys = FALSE; | |
1183 | if (sm->wpa == WPA_VERSION_WPA) | |
1184 | sm->PInitAKeys = FALSE; | |
1185 | if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and | |
1186 | * Local AA > Remote AA)) */) { | |
1187 | sm->Pair = TRUE; | |
1188 | } | |
1189 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0); | |
1190 | wpa_remove_ptk(sm); | |
1191 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0); | |
1192 | sm->TimeoutCtr = 0; | |
56586197 | 1193 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) { |
6fc6879b JM |
1194 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, |
1195 | WPA_EAPOL_authorized, 0); | |
1196 | } | |
1197 | } | |
1198 | ||
1199 | ||
1200 | SM_STATE(WPA_PTK, DISCONNECT) | |
1201 | { | |
1202 | SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk); | |
1203 | sm->Disconnect = FALSE; | |
1204 | wpa_sta_disconnect(sm->wpa_auth, sm->addr); | |
1205 | } | |
1206 | ||
1207 | ||
1208 | SM_STATE(WPA_PTK, DISCONNECTED) | |
1209 | { | |
1210 | SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk); | |
1211 | sm->DeauthenticationRequest = FALSE; | |
1212 | } | |
1213 | ||
1214 | ||
1215 | SM_STATE(WPA_PTK, AUTHENTICATION) | |
1216 | { | |
1217 | SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk); | |
1218 | os_memset(&sm->PTK, 0, sizeof(sm->PTK)); | |
1219 | sm->PTK_valid = FALSE; | |
1220 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto, | |
1221 | 1); | |
1222 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1); | |
1223 | sm->AuthenticationRequest = FALSE; | |
1224 | } | |
1225 | ||
1226 | ||
1227 | SM_STATE(WPA_PTK, AUTHENTICATION2) | |
1228 | { | |
1229 | SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk); | |
1230 | os_memcpy(sm->ANonce, sm->group->Counter, WPA_NONCE_LEN); | |
1231 | inc_byte_array(sm->group->Counter, WPA_NONCE_LEN); | |
1232 | sm->ReAuthenticationRequest = FALSE; | |
1233 | /* IEEE 802.11i does not clear TimeoutCtr here, but this is more | |
1234 | * logical place than INITIALIZE since AUTHENTICATION2 can be | |
1235 | * re-entered on ReAuthenticationRequest without going through | |
1236 | * INITIALIZE. */ | |
1237 | sm->TimeoutCtr = 0; | |
1238 | } | |
1239 | ||
1240 | ||
1241 | SM_STATE(WPA_PTK, INITPMK) | |
1242 | { | |
1243 | u8 msk[2 * PMK_LEN]; | |
1244 | size_t len = 2 * PMK_LEN; | |
1245 | ||
1246 | SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk); | |
1247 | #ifdef CONFIG_IEEE80211R | |
1248 | sm->xxkey_len = 0; | |
1249 | #endif /* CONFIG_IEEE80211R */ | |
1250 | if (sm->pmksa) { | |
1251 | wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache"); | |
1252 | os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN); | |
1253 | } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) { | |
1254 | wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine " | |
1255 | "(len=%lu)", (unsigned long) len); | |
1256 | os_memcpy(sm->PMK, msk, PMK_LEN); | |
1257 | #ifdef CONFIG_IEEE80211R | |
1258 | if (len >= 2 * PMK_LEN) { | |
1259 | os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN); | |
1260 | sm->xxkey_len = PMK_LEN; | |
1261 | } | |
1262 | #endif /* CONFIG_IEEE80211R */ | |
1263 | } else { | |
1264 | wpa_printf(MSG_DEBUG, "WPA: Could not get PMK"); | |
1265 | } | |
1266 | ||
1267 | sm->req_replay_counter_used = 0; | |
1268 | /* IEEE 802.11i does not set keyRun to FALSE, but not doing this | |
1269 | * will break reauthentication since EAPOL state machines may not be | |
1270 | * get into AUTHENTICATING state that clears keyRun before WPA state | |
1271 | * machine enters AUTHENTICATION2 state and goes immediately to INITPMK | |
1272 | * state and takes PMK from the previously used AAA Key. This will | |
1273 | * eventually fail in 4-Way Handshake because Supplicant uses PMK | |
1274 | * derived from the new AAA Key. Setting keyRun = FALSE here seems to | |
1275 | * be good workaround for this issue. */ | |
1276 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0); | |
1277 | } | |
1278 | ||
1279 | ||
1280 | SM_STATE(WPA_PTK, INITPSK) | |
1281 | { | |
1282 | const u8 *psk; | |
1283 | SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk); | |
1284 | psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL); | |
1285 | if (psk) { | |
1286 | os_memcpy(sm->PMK, psk, PMK_LEN); | |
1287 | #ifdef CONFIG_IEEE80211R | |
1288 | os_memcpy(sm->xxkey, psk, PMK_LEN); | |
1289 | sm->xxkey_len = PMK_LEN; | |
1290 | #endif /* CONFIG_IEEE80211R */ | |
1291 | } | |
1292 | sm->req_replay_counter_used = 0; | |
1293 | } | |
1294 | ||
1295 | ||
1296 | SM_STATE(WPA_PTK, PTKSTART) | |
1297 | { | |
1298 | u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL; | |
1299 | size_t pmkid_len = 0; | |
1300 | ||
1301 | SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk); | |
1302 | sm->PTKRequest = FALSE; | |
1303 | sm->TimeoutEvt = FALSE; | |
1304 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
1305 | "sending 1/4 msg of 4-Way Handshake"); | |
1306 | /* | |
1307 | * TODO: Could add PMKID even with WPA2-PSK, but only if there is only | |
1308 | * one possible PSK for this STA. | |
1309 | */ | |
1310 | if (sm->wpa == WPA_VERSION_WPA2 && | |
56586197 | 1311 | wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) { |
6fc6879b JM |
1312 | pmkid = buf; |
1313 | pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN; | |
1314 | pmkid[0] = WLAN_EID_VENDOR_SPECIFIC; | |
1315 | pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN; | |
1316 | RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID); | |
1317 | if (sm->pmksa) | |
1318 | os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN], | |
1319 | sm->pmksa->pmkid, PMKID_LEN); | |
1320 | else { | |
1321 | /* | |
1322 | * Calculate PMKID since no PMKSA cache entry was | |
1323 | * available with pre-calculated PMKID. | |
1324 | */ | |
1325 | rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr, | |
56586197 JM |
1326 | sm->addr, &pmkid[2 + RSN_SELECTOR_LEN], |
1327 | wpa_key_mgmt_sha256(sm->wpa_key_mgmt)); | |
6fc6879b JM |
1328 | } |
1329 | } | |
1330 | wpa_send_eapol(sm->wpa_auth, sm, | |
1331 | WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL, | |
1332 | sm->ANonce, pmkid, pmkid_len, 0, 0); | |
1333 | sm->TimeoutCtr++; | |
1334 | } | |
1335 | ||
1336 | ||
1337 | static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk, | |
1338 | struct wpa_ptk *ptk) | |
1339 | { | |
1340 | #ifdef CONFIG_IEEE80211R | |
56586197 | 1341 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) |
6fc6879b JM |
1342 | return wpa_auth_derive_ptk_ft(sm, pmk, ptk); |
1343 | #endif /* CONFIG_IEEE80211R */ | |
1344 | ||
1345 | wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion", | |
1346 | sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce, | |
56586197 JM |
1347 | (u8 *) ptk, sizeof(*ptk), |
1348 | wpa_key_mgmt_sha256(sm->wpa_key_mgmt)); | |
6fc6879b JM |
1349 | |
1350 | return 0; | |
1351 | } | |
1352 | ||
1353 | ||
1354 | SM_STATE(WPA_PTK, PTKCALCNEGOTIATING) | |
1355 | { | |
1356 | struct wpa_ptk PTK; | |
1357 | int ok = 0; | |
1358 | const u8 *pmk = NULL; | |
1359 | ||
1360 | SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk); | |
1361 | sm->EAPOLKeyReceived = FALSE; | |
1362 | ||
1363 | /* WPA with IEEE 802.1X: use the derived PMK from EAP | |
1364 | * WPA-PSK: iterate through possible PSKs and select the one matching | |
1365 | * the packet */ | |
1366 | for (;;) { | |
56586197 | 1367 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) { |
6fc6879b JM |
1368 | pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, pmk); |
1369 | if (pmk == NULL) | |
1370 | break; | |
1371 | } else | |
1372 | pmk = sm->PMK; | |
1373 | ||
1374 | wpa_derive_ptk(sm, pmk, &PTK); | |
1375 | ||
1376 | if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key, | |
1377 | sm->last_rx_eapol_key_len) == 0) { | |
1378 | ok = 1; | |
1379 | break; | |
1380 | } | |
1381 | ||
56586197 | 1382 | if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) |
6fc6879b JM |
1383 | break; |
1384 | } | |
1385 | ||
1386 | if (!ok) { | |
1387 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
1388 | "invalid MIC in msg 2/4 of 4-Way Handshake"); | |
1389 | return; | |
1390 | } | |
1391 | ||
1392 | eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm); | |
1393 | ||
56586197 | 1394 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) { |
6fc6879b JM |
1395 | /* PSK may have changed from the previous choice, so update |
1396 | * state machine data based on whatever PSK was selected here. | |
1397 | */ | |
1398 | os_memcpy(sm->PMK, pmk, PMK_LEN); | |
1399 | } | |
1400 | ||
1401 | sm->MICVerified = TRUE; | |
1402 | ||
1403 | os_memcpy(&sm->PTK, &PTK, sizeof(PTK)); | |
1404 | sm->PTK_valid = TRUE; | |
1405 | } | |
1406 | ||
1407 | ||
1408 | SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2) | |
1409 | { | |
1410 | SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk); | |
1411 | sm->TimeoutCtr = 0; | |
1412 | } | |
1413 | ||
1414 | ||
1415 | #ifdef CONFIG_IEEE80211W | |
1416 | ||
1417 | static int ieee80211w_kde_len(struct wpa_state_machine *sm) | |
1418 | { | |
1419 | if (sm->mgmt_frame_prot) { | |
1420 | return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde); | |
1421 | } | |
1422 | ||
1423 | return 0; | |
1424 | } | |
1425 | ||
1426 | ||
1427 | static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos) | |
1428 | { | |
1429 | struct wpa_igtk_kde igtk; | |
1430 | struct wpa_group *gsm = sm->group; | |
1431 | ||
1432 | if (!sm->mgmt_frame_prot) | |
1433 | return pos; | |
1434 | ||
1435 | igtk.keyid[0] = gsm->GN_igtk; | |
1436 | igtk.keyid[1] = 0; | |
1437 | if (wpa_auth_get_seqnum_igtk(sm->wpa_auth, NULL, gsm->GN_igtk, igtk.pn) | |
1438 | < 0) | |
1439 | os_memset(igtk.pn, 0, sizeof(igtk.pn)); | |
1440 | os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN); | |
1441 | pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK, | |
1442 | (const u8 *) &igtk, sizeof(igtk), NULL, 0); | |
1443 | ||
1444 | return pos; | |
1445 | } | |
1446 | ||
1447 | #else /* CONFIG_IEEE80211W */ | |
1448 | ||
1449 | static int ieee80211w_kde_len(struct wpa_state_machine *sm) | |
1450 | { | |
1451 | return 0; | |
1452 | } | |
1453 | ||
1454 | ||
1455 | static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos) | |
1456 | { | |
1457 | return pos; | |
1458 | } | |
1459 | ||
1460 | #endif /* CONFIG_IEEE80211W */ | |
1461 | ||
1462 | ||
1463 | SM_STATE(WPA_PTK, PTKINITNEGOTIATING) | |
1464 | { | |
1465 | u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos; | |
1466 | size_t gtk_len, kde_len; | |
1467 | struct wpa_group *gsm = sm->group; | |
1468 | u8 *wpa_ie; | |
1469 | int wpa_ie_len, secure, keyidx, encr = 0; | |
1470 | ||
1471 | SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk); | |
1472 | sm->TimeoutEvt = FALSE; | |
1473 | /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN]) | |
1474 | */ | |
1475 | os_memset(rsc, 0, WPA_KEY_RSC_LEN); | |
1476 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc); | |
1477 | wpa_ie = sm->wpa_auth->wpa_ie; | |
1478 | wpa_ie_len = sm->wpa_auth->wpa_ie_len; | |
1479 | if (sm->wpa == WPA_VERSION_WPA && | |
1480 | (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) && | |
1481 | wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) { | |
1482 | /* WPA-only STA, remove RSN IE */ | |
1483 | wpa_ie = wpa_ie + wpa_ie[1] + 2; | |
1484 | wpa_ie_len = wpa_ie[1] + 2; | |
1485 | } | |
1486 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
1487 | "sending 3/4 msg of 4-Way Handshake"); | |
1488 | if (sm->wpa == WPA_VERSION_WPA2) { | |
1489 | /* WPA2 send GTK in the 4-way handshake */ | |
1490 | secure = 1; | |
1491 | gtk = gsm->GTK[gsm->GN - 1]; | |
1492 | gtk_len = gsm->GTK_len; | |
1493 | keyidx = gsm->GN; | |
1494 | _rsc = rsc; | |
1495 | encr = 1; | |
1496 | } else { | |
1497 | /* WPA does not include GTK in msg 3/4 */ | |
1498 | secure = 0; | |
1499 | gtk = NULL; | |
1500 | gtk_len = 0; | |
1501 | keyidx = 0; | |
1502 | _rsc = NULL; | |
1503 | } | |
1504 | ||
1505 | kde_len = wpa_ie_len + ieee80211w_kde_len(sm); | |
1506 | if (gtk) | |
1507 | kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len; | |
1508 | kde = os_malloc(kde_len); | |
1509 | if (kde == NULL) | |
1510 | return; | |
1511 | ||
1512 | pos = kde; | |
1513 | os_memcpy(pos, wpa_ie, wpa_ie_len); | |
1514 | pos += wpa_ie_len; | |
1515 | if (gtk) { | |
1516 | u8 hdr[2]; | |
1517 | hdr[0] = keyidx & 0x03; | |
1518 | hdr[1] = 0; | |
1519 | pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2, | |
1520 | gtk, gtk_len); | |
1521 | } | |
1522 | pos = ieee80211w_kde_add(sm, pos); | |
1523 | ||
1524 | wpa_send_eapol(sm->wpa_auth, sm, | |
1525 | (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC | | |
1526 | WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL | | |
1527 | WPA_KEY_INFO_KEY_TYPE, | |
1528 | _rsc, sm->ANonce, kde, pos - kde, keyidx, encr); | |
1529 | os_free(kde); | |
1530 | sm->TimeoutCtr++; | |
1531 | } | |
1532 | ||
1533 | ||
1534 | SM_STATE(WPA_PTK, PTKINITDONE) | |
1535 | { | |
1536 | SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk); | |
1537 | sm->EAPOLKeyReceived = FALSE; | |
1538 | if (sm->Pair) { | |
1539 | char *alg; | |
1540 | int klen; | |
1541 | if (sm->pairwise == WPA_CIPHER_TKIP) { | |
1542 | alg = "TKIP"; | |
1543 | klen = 32; | |
1544 | } else { | |
1545 | alg = "CCMP"; | |
1546 | klen = 16; | |
1547 | } | |
1548 | if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0, | |
1549 | sm->PTK.tk1, klen)) { | |
1550 | wpa_sta_disconnect(sm->wpa_auth, sm->addr); | |
1551 | return; | |
1552 | } | |
1553 | /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */ | |
1554 | sm->pairwise_set = TRUE; | |
1555 | ||
56586197 | 1556 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) { |
6fc6879b JM |
1557 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, |
1558 | WPA_EAPOL_authorized, 1); | |
1559 | } | |
1560 | } | |
1561 | ||
1562 | if (0 /* IBSS == TRUE */) { | |
1563 | sm->keycount++; | |
1564 | if (sm->keycount == 2) { | |
1565 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, | |
1566 | WPA_EAPOL_portValid, 1); | |
1567 | } | |
1568 | } else { | |
1569 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, | |
1570 | 1); | |
1571 | } | |
1572 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0); | |
1573 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1); | |
1574 | if (sm->wpa == WPA_VERSION_WPA) | |
1575 | sm->PInitAKeys = TRUE; | |
1576 | else | |
1577 | sm->has_GTK = TRUE; | |
1578 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO, | |
1579 | "pairwise key handshake completed (%s)", | |
1580 | sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN"); | |
1581 | ||
1582 | #ifdef CONFIG_IEEE80211R | |
1583 | wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr); | |
1584 | #endif /* CONFIG_IEEE80211R */ | |
1585 | } | |
1586 | ||
1587 | ||
1588 | SM_STEP(WPA_PTK) | |
1589 | { | |
1590 | struct wpa_authenticator *wpa_auth = sm->wpa_auth; | |
1591 | ||
1592 | if (sm->Init) | |
1593 | SM_ENTER(WPA_PTK, INITIALIZE); | |
1594 | else if (sm->Disconnect | |
1595 | /* || FIX: dot11RSNAConfigSALifetime timeout */) | |
1596 | SM_ENTER(WPA_PTK, DISCONNECT); | |
1597 | else if (sm->DeauthenticationRequest) | |
1598 | SM_ENTER(WPA_PTK, DISCONNECTED); | |
1599 | else if (sm->AuthenticationRequest) | |
1600 | SM_ENTER(WPA_PTK, AUTHENTICATION); | |
1601 | else if (sm->ReAuthenticationRequest) | |
1602 | SM_ENTER(WPA_PTK, AUTHENTICATION2); | |
1603 | else if (sm->PTKRequest) | |
1604 | SM_ENTER(WPA_PTK, PTKSTART); | |
1605 | else switch (sm->wpa_ptk_state) { | |
1606 | case WPA_PTK_INITIALIZE: | |
1607 | break; | |
1608 | case WPA_PTK_DISCONNECT: | |
1609 | SM_ENTER(WPA_PTK, DISCONNECTED); | |
1610 | break; | |
1611 | case WPA_PTK_DISCONNECTED: | |
1612 | SM_ENTER(WPA_PTK, INITIALIZE); | |
1613 | break; | |
1614 | case WPA_PTK_AUTHENTICATION: | |
1615 | SM_ENTER(WPA_PTK, AUTHENTICATION2); | |
1616 | break; | |
1617 | case WPA_PTK_AUTHENTICATION2: | |
56586197 | 1618 | if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) && |
6fc6879b JM |
1619 | wpa_auth_get_eapol(sm->wpa_auth, sm->addr, |
1620 | WPA_EAPOL_keyRun) > 0) | |
1621 | SM_ENTER(WPA_PTK, INITPMK); | |
56586197 | 1622 | else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) |
6fc6879b JM |
1623 | /* FIX: && 802.1X::keyRun */) |
1624 | SM_ENTER(WPA_PTK, INITPSK); | |
1625 | break; | |
1626 | case WPA_PTK_INITPMK: | |
1627 | if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr, | |
1628 | WPA_EAPOL_keyAvailable) > 0) | |
1629 | SM_ENTER(WPA_PTK, PTKSTART); | |
1630 | else { | |
1631 | wpa_auth->dot11RSNA4WayHandshakeFailures++; | |
1632 | SM_ENTER(WPA_PTK, DISCONNECT); | |
1633 | } | |
1634 | break; | |
1635 | case WPA_PTK_INITPSK: | |
1636 | if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL)) | |
1637 | SM_ENTER(WPA_PTK, PTKSTART); | |
1638 | else { | |
1639 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO, | |
1640 | "no PSK configured for the STA"); | |
1641 | wpa_auth->dot11RSNA4WayHandshakeFailures++; | |
1642 | SM_ENTER(WPA_PTK, DISCONNECT); | |
1643 | } | |
1644 | break; | |
1645 | case WPA_PTK_PTKSTART: | |
1646 | if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && | |
1647 | sm->EAPOLKeyPairwise) | |
1648 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING); | |
1649 | else if (sm->TimeoutCtr > | |
1650 | (int) dot11RSNAConfigPairwiseUpdateCount) { | |
1651 | wpa_auth->dot11RSNA4WayHandshakeFailures++; | |
1652 | SM_ENTER(WPA_PTK, DISCONNECT); | |
1653 | } else if (sm->TimeoutEvt) | |
1654 | SM_ENTER(WPA_PTK, PTKSTART); | |
1655 | break; | |
1656 | case WPA_PTK_PTKCALCNEGOTIATING: | |
1657 | if (sm->MICVerified) | |
1658 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2); | |
1659 | else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && | |
1660 | sm->EAPOLKeyPairwise) | |
1661 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING); | |
1662 | else if (sm->TimeoutEvt) | |
1663 | SM_ENTER(WPA_PTK, PTKSTART); | |
1664 | break; | |
1665 | case WPA_PTK_PTKCALCNEGOTIATING2: | |
1666 | SM_ENTER(WPA_PTK, PTKINITNEGOTIATING); | |
1667 | break; | |
1668 | case WPA_PTK_PTKINITNEGOTIATING: | |
1669 | if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && | |
1670 | sm->EAPOLKeyPairwise && sm->MICVerified) | |
1671 | SM_ENTER(WPA_PTK, PTKINITDONE); | |
1672 | else if (sm->TimeoutCtr > | |
1673 | (int) dot11RSNAConfigPairwiseUpdateCount) { | |
1674 | wpa_auth->dot11RSNA4WayHandshakeFailures++; | |
1675 | SM_ENTER(WPA_PTK, DISCONNECT); | |
1676 | } else if (sm->TimeoutEvt) | |
1677 | SM_ENTER(WPA_PTK, PTKINITNEGOTIATING); | |
1678 | break; | |
1679 | case WPA_PTK_PTKINITDONE: | |
1680 | break; | |
1681 | } | |
1682 | } | |
1683 | ||
1684 | ||
1685 | SM_STATE(WPA_PTK_GROUP, IDLE) | |
1686 | { | |
1687 | SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group); | |
1688 | if (sm->Init) { | |
1689 | /* Init flag is not cleared here, so avoid busy | |
1690 | * loop by claiming nothing changed. */ | |
1691 | sm->changed = FALSE; | |
1692 | } | |
1693 | sm->GTimeoutCtr = 0; | |
1694 | } | |
1695 | ||
1696 | ||
1697 | SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING) | |
1698 | { | |
1699 | u8 rsc[WPA_KEY_RSC_LEN]; | |
1700 | struct wpa_group *gsm = sm->group; | |
1701 | u8 *kde, *pos, hdr[2]; | |
1702 | size_t kde_len; | |
1703 | ||
1704 | SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group); | |
1705 | if (sm->wpa == WPA_VERSION_WPA) | |
1706 | sm->PInitAKeys = FALSE; | |
1707 | sm->TimeoutEvt = FALSE; | |
1708 | /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */ | |
1709 | os_memset(rsc, 0, WPA_KEY_RSC_LEN); | |
1710 | if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE) | |
1711 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc); | |
1712 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
1713 | "sending 1/2 msg of Group Key Handshake"); | |
1714 | ||
1715 | if (sm->wpa == WPA_VERSION_WPA2) { | |
1716 | kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len + | |
1717 | ieee80211w_kde_len(sm); | |
1718 | kde = os_malloc(kde_len); | |
1719 | if (kde == NULL) | |
1720 | return; | |
1721 | ||
1722 | pos = kde; | |
1723 | hdr[0] = gsm->GN & 0x03; | |
1724 | hdr[1] = 0; | |
1725 | pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2, | |
1726 | gsm->GTK[gsm->GN - 1], gsm->GTK_len); | |
1727 | pos = ieee80211w_kde_add(sm, pos); | |
1728 | } else { | |
1729 | kde = gsm->GTK[gsm->GN - 1]; | |
1730 | pos = kde + gsm->GTK_len; | |
1731 | } | |
1732 | ||
1733 | wpa_send_eapol(sm->wpa_auth, sm, | |
1734 | WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC | | |
1735 | WPA_KEY_INFO_ACK | | |
1736 | (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0), | |
1737 | rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1); | |
1738 | if (sm->wpa == WPA_VERSION_WPA2) | |
1739 | os_free(kde); | |
1740 | sm->GTimeoutCtr++; | |
1741 | } | |
1742 | ||
1743 | ||
1744 | SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED) | |
1745 | { | |
1746 | SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group); | |
1747 | sm->EAPOLKeyReceived = FALSE; | |
1748 | if (sm->GUpdateStationKeys) | |
1749 | sm->group->GKeyDoneStations--; | |
1750 | sm->GUpdateStationKeys = FALSE; | |
1751 | sm->GTimeoutCtr = 0; | |
1752 | /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */ | |
1753 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO, | |
1754 | "group key handshake completed (%s)", | |
1755 | sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN"); | |
1756 | sm->has_GTK = TRUE; | |
1757 | } | |
1758 | ||
1759 | ||
1760 | SM_STATE(WPA_PTK_GROUP, KEYERROR) | |
1761 | { | |
1762 | SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group); | |
1763 | if (sm->GUpdateStationKeys) | |
1764 | sm->group->GKeyDoneStations--; | |
1765 | sm->GUpdateStationKeys = FALSE; | |
1766 | sm->Disconnect = TRUE; | |
1767 | } | |
1768 | ||
1769 | ||
1770 | SM_STEP(WPA_PTK_GROUP) | |
1771 | { | |
9663596f | 1772 | if (sm->Init || sm->PtkGroupInit) { |
6fc6879b | 1773 | SM_ENTER(WPA_PTK_GROUP, IDLE); |
9663596f JM |
1774 | sm->PtkGroupInit = FALSE; |
1775 | } else switch (sm->wpa_ptk_group_state) { | |
6fc6879b JM |
1776 | case WPA_PTK_GROUP_IDLE: |
1777 | if (sm->GUpdateStationKeys || | |
1778 | (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys)) | |
1779 | SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING); | |
1780 | break; | |
1781 | case WPA_PTK_GROUP_REKEYNEGOTIATING: | |
1782 | if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && | |
1783 | !sm->EAPOLKeyPairwise && sm->MICVerified) | |
1784 | SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED); | |
1785 | else if (sm->GTimeoutCtr > | |
1786 | (int) dot11RSNAConfigGroupUpdateCount) | |
1787 | SM_ENTER(WPA_PTK_GROUP, KEYERROR); | |
1788 | else if (sm->TimeoutEvt) | |
1789 | SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING); | |
1790 | break; | |
1791 | case WPA_PTK_GROUP_KEYERROR: | |
1792 | SM_ENTER(WPA_PTK_GROUP, IDLE); | |
1793 | break; | |
1794 | case WPA_PTK_GROUP_REKEYESTABLISHED: | |
1795 | SM_ENTER(WPA_PTK_GROUP, IDLE); | |
1796 | break; | |
1797 | } | |
1798 | } | |
1799 | ||
1800 | ||
1801 | static int wpa_gtk_update(struct wpa_authenticator *wpa_auth, | |
1802 | struct wpa_group *group) | |
1803 | { | |
1804 | int ret = 0; | |
1805 | ||
1806 | /* FIX: is this the correct way of getting GNonce? */ | |
1807 | os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN); | |
1808 | inc_byte_array(group->Counter, WPA_NONCE_LEN); | |
1809 | wpa_gmk_to_gtk(group->GMK, wpa_auth->addr, group->GNonce, | |
1810 | group->GTK[group->GN - 1], group->GTK_len); | |
1811 | ||
1812 | #ifdef CONFIG_IEEE80211W | |
1813 | if (wpa_auth->conf.ieee80211w != WPA_NO_IEEE80211W) { | |
1814 | if (os_get_random(group->IGTK[group->GN_igtk - 4], | |
1815 | WPA_IGTK_LEN) < 0) { | |
1816 | wpa_printf(MSG_INFO, "RSN: Failed to get new random " | |
1817 | "IGTK"); | |
1818 | ret = -1; | |
1819 | } | |
1820 | wpa_hexdump_key(MSG_DEBUG, "IGTK", | |
1821 | group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN); | |
1822 | } | |
1823 | #endif /* CONFIG_IEEE80211W */ | |
1824 | ||
1825 | return ret; | |
1826 | } | |
1827 | ||
1828 | ||
1829 | static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth, | |
1830 | struct wpa_group *group) | |
1831 | { | |
1832 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state " | |
1833 | "GTK_INIT (VLAN-ID %d)", group->vlan_id); | |
1834 | group->changed = FALSE; /* GInit is not cleared here; avoid loop */ | |
1835 | group->wpa_group_state = WPA_GROUP_GTK_INIT; | |
1836 | ||
1837 | /* GTK[0..N] = 0 */ | |
1838 | os_memset(group->GTK, 0, sizeof(group->GTK)); | |
1839 | group->GN = 1; | |
1840 | group->GM = 2; | |
1841 | #ifdef CONFIG_IEEE80211W | |
1842 | group->GN_igtk = 4; | |
1843 | group->GM_igtk = 5; | |
1844 | #endif /* CONFIG_IEEE80211W */ | |
1845 | /* GTK[GN] = CalcGTK() */ | |
1846 | wpa_gtk_update(wpa_auth, group); | |
1847 | } | |
1848 | ||
1849 | ||
1850 | static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx) | |
1851 | { | |
1852 | if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) { | |
1853 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
1854 | "Not in PTKINITDONE; skip Group Key update"); | |
1855 | return 0; | |
1856 | } | |
9663596f JM |
1857 | if (sm->GUpdateStationKeys) { |
1858 | /* | |
1859 | * This should not really happen, but just in case, make sure | |
1860 | * we do not count the same STA twice in GKeyDoneStations. | |
1861 | */ | |
1862 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
1863 | "GUpdateStationKeys already set - do not " | |
1864 | "increment GKeyDoneStations"); | |
1865 | } else { | |
1866 | sm->group->GKeyDoneStations++; | |
1867 | sm->GUpdateStationKeys = TRUE; | |
1868 | } | |
6fc6879b JM |
1869 | wpa_sm_step(sm); |
1870 | return 0; | |
1871 | } | |
1872 | ||
1873 | ||
1874 | static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth, | |
1875 | struct wpa_group *group) | |
1876 | { | |
1877 | int tmp; | |
1878 | ||
1879 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state " | |
1880 | "SETKEYS (VLAN-ID %d)", group->vlan_id); | |
1881 | group->changed = TRUE; | |
1882 | group->wpa_group_state = WPA_GROUP_SETKEYS; | |
1883 | group->GTKReKey = FALSE; | |
1884 | tmp = group->GM; | |
1885 | group->GM = group->GN; | |
1886 | group->GN = tmp; | |
1887 | #ifdef CONFIG_IEEE80211W | |
1888 | tmp = group->GM_igtk; | |
1889 | group->GM_igtk = group->GN_igtk; | |
1890 | group->GN_igtk = tmp; | |
1891 | #endif /* CONFIG_IEEE80211W */ | |
1892 | /* "GKeyDoneStations = GNoStations" is done in more robust way by | |
1893 | * counting the STAs that are marked with GUpdateStationKeys instead of | |
1894 | * including all STAs that could be in not-yet-completed state. */ | |
1895 | wpa_gtk_update(wpa_auth, group); | |
1896 | ||
1897 | wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, NULL); | |
1898 | wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d", | |
1899 | group->GKeyDoneStations); | |
1900 | } | |
1901 | ||
1902 | ||
1903 | static void wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth, | |
1904 | struct wpa_group *group) | |
1905 | { | |
1906 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state " | |
1907 | "SETKEYSDONE (VLAN-ID %d)", group->vlan_id); | |
1908 | group->changed = TRUE; | |
1909 | group->wpa_group_state = WPA_GROUP_SETKEYSDONE; | |
1910 | wpa_auth_set_key(wpa_auth, group->vlan_id, | |
1911 | wpa_alg_txt(wpa_auth->conf.wpa_group), | |
1912 | NULL, group->GN, group->GTK[group->GN - 1], | |
1913 | group->GTK_len); | |
1914 | ||
1915 | #ifdef CONFIG_IEEE80211W | |
1916 | if (wpa_auth->conf.ieee80211w != WPA_NO_IEEE80211W) { | |
1917 | wpa_auth_set_key(wpa_auth, group->vlan_id, "IGTK", | |
1918 | NULL, group->GN_igtk, | |
1919 | group->IGTK[group->GN_igtk - 4], | |
1920 | WPA_IGTK_LEN); | |
1921 | } | |
1922 | #endif /* CONFIG_IEEE80211W */ | |
1923 | } | |
1924 | ||
1925 | ||
1926 | static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth, | |
1927 | struct wpa_group *group) | |
1928 | { | |
1929 | if (group->GInit) { | |
1930 | wpa_group_gtk_init(wpa_auth, group); | |
1931 | } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT && | |
1932 | group->GTKAuthenticator) { | |
1933 | wpa_group_setkeysdone(wpa_auth, group); | |
1934 | } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE && | |
1935 | group->GTKReKey) { | |
1936 | wpa_group_setkeys(wpa_auth, group); | |
1937 | } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) { | |
1938 | if (group->GKeyDoneStations == 0) | |
1939 | wpa_group_setkeysdone(wpa_auth, group); | |
1940 | else if (group->GTKReKey) | |
1941 | wpa_group_setkeys(wpa_auth, group); | |
1942 | } | |
1943 | } | |
1944 | ||
1945 | ||
1946 | static void wpa_sm_step(struct wpa_state_machine *sm) | |
1947 | { | |
1948 | if (sm == NULL) | |
1949 | return; | |
1950 | ||
1951 | if (sm->in_step_loop) { | |
1952 | /* This should not happen, but if it does, make sure we do not | |
1953 | * end up freeing the state machine too early by exiting the | |
1954 | * recursive call. */ | |
1955 | wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively"); | |
1956 | return; | |
1957 | } | |
1958 | ||
1959 | sm->in_step_loop = 1; | |
1960 | do { | |
1961 | if (sm->pending_deinit) | |
1962 | break; | |
1963 | ||
1964 | sm->changed = FALSE; | |
1965 | sm->wpa_auth->group->changed = FALSE; | |
1966 | ||
1967 | SM_STEP_RUN(WPA_PTK); | |
1968 | if (sm->pending_deinit) | |
1969 | break; | |
1970 | SM_STEP_RUN(WPA_PTK_GROUP); | |
1971 | if (sm->pending_deinit) | |
1972 | break; | |
1973 | wpa_group_sm_step(sm->wpa_auth, sm->group); | |
1974 | } while (sm->changed || sm->wpa_auth->group->changed); | |
1975 | sm->in_step_loop = 0; | |
1976 | ||
1977 | if (sm->pending_deinit) { | |
1978 | wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state " | |
1979 | "machine deinit for " MACSTR, MAC2STR(sm->addr)); | |
1980 | wpa_free_sta_sm(sm); | |
1981 | } | |
1982 | } | |
1983 | ||
1984 | ||
1985 | static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx) | |
1986 | { | |
1987 | struct wpa_state_machine *sm = eloop_ctx; | |
1988 | wpa_sm_step(sm); | |
1989 | } | |
1990 | ||
1991 | ||
1992 | void wpa_auth_sm_notify(struct wpa_state_machine *sm) | |
1993 | { | |
1994 | if (sm == NULL) | |
1995 | return; | |
1996 | eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL); | |
1997 | } | |
1998 | ||
1999 | ||
2000 | void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth) | |
2001 | { | |
2002 | int tmp, i; | |
2003 | struct wpa_group *group; | |
2004 | ||
2005 | if (wpa_auth == NULL) | |
2006 | return; | |
2007 | ||
2008 | group = wpa_auth->group; | |
2009 | ||
2010 | for (i = 0; i < 2; i++) { | |
2011 | tmp = group->GM; | |
2012 | group->GM = group->GN; | |
2013 | group->GN = tmp; | |
2014 | #ifdef CONFIG_IEEE80211W | |
2015 | tmp = group->GM_igtk; | |
2016 | group->GM_igtk = group->GN_igtk; | |
2017 | group->GN_igtk = tmp; | |
2018 | #endif /* CONFIG_IEEE80211W */ | |
2019 | wpa_gtk_update(wpa_auth, group); | |
2020 | } | |
2021 | } | |
2022 | ||
2023 | ||
2024 | static const char * wpa_bool_txt(int bool) | |
2025 | { | |
2026 | return bool ? "TRUE" : "FALSE"; | |
2027 | } | |
2028 | ||
2029 | ||
2030 | static int wpa_cipher_bits(int cipher) | |
2031 | { | |
2032 | switch (cipher) { | |
2033 | case WPA_CIPHER_CCMP: | |
2034 | return 128; | |
2035 | case WPA_CIPHER_TKIP: | |
2036 | return 256; | |
2037 | case WPA_CIPHER_WEP104: | |
2038 | return 104; | |
2039 | case WPA_CIPHER_WEP40: | |
2040 | return 40; | |
2041 | default: | |
2042 | return 0; | |
2043 | } | |
2044 | } | |
2045 | ||
2046 | ||
2047 | #define RSN_SUITE "%02x-%02x-%02x-%d" | |
2048 | #define RSN_SUITE_ARG(s) \ | |
2049 | ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff | |
2050 | ||
2051 | int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen) | |
2052 | { | |
2053 | int len = 0, ret; | |
2054 | char pmkid_txt[PMKID_LEN * 2 + 1]; | |
2055 | ||
2056 | if (wpa_auth == NULL) | |
2057 | return len; | |
2058 | ||
2059 | ret = os_snprintf(buf + len, buflen - len, | |
2060 | "dot11RSNAOptionImplemented=TRUE\n" | |
2061 | #ifdef CONFIG_RSN_PREAUTH | |
2062 | "dot11RSNAPreauthenticationImplemented=TRUE\n" | |
2063 | #else /* CONFIG_RSN_PREAUTH */ | |
2064 | "dot11RSNAPreauthenticationImplemented=FALSE\n" | |
2065 | #endif /* CONFIG_RSN_PREAUTH */ | |
2066 | "dot11RSNAEnabled=%s\n" | |
2067 | "dot11RSNAPreauthenticationEnabled=%s\n", | |
2068 | wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN), | |
2069 | wpa_bool_txt(wpa_auth->conf.rsn_preauth)); | |
2070 | if (ret < 0 || (size_t) ret >= buflen - len) | |
2071 | return len; | |
2072 | len += ret; | |
2073 | ||
2074 | wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt), | |
2075 | wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN); | |
2076 | ||
2077 | ret = os_snprintf( | |
2078 | buf + len, buflen - len, | |
2079 | "dot11RSNAConfigVersion=%u\n" | |
2080 | "dot11RSNAConfigPairwiseKeysSupported=9999\n" | |
2081 | /* FIX: dot11RSNAConfigGroupCipher */ | |
2082 | /* FIX: dot11RSNAConfigGroupRekeyMethod */ | |
2083 | /* FIX: dot11RSNAConfigGroupRekeyTime */ | |
2084 | /* FIX: dot11RSNAConfigGroupRekeyPackets */ | |
2085 | "dot11RSNAConfigGroupRekeyStrict=%u\n" | |
2086 | "dot11RSNAConfigGroupUpdateCount=%u\n" | |
2087 | "dot11RSNAConfigPairwiseUpdateCount=%u\n" | |
2088 | "dot11RSNAConfigGroupCipherSize=%u\n" | |
2089 | "dot11RSNAConfigPMKLifetime=%u\n" | |
2090 | "dot11RSNAConfigPMKReauthThreshold=%u\n" | |
2091 | "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n" | |
2092 | "dot11RSNAConfigSATimeout=%u\n" | |
2093 | "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n" | |
2094 | "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n" | |
2095 | "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n" | |
2096 | "dot11RSNAPMKIDUsed=%s\n" | |
2097 | "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n" | |
2098 | "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n" | |
2099 | "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n" | |
2100 | "dot11RSNATKIPCounterMeasuresInvoked=%u\n" | |
2101 | "dot11RSNA4WayHandshakeFailures=%u\n" | |
2102 | "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n", | |
2103 | RSN_VERSION, | |
2104 | !!wpa_auth->conf.wpa_strict_rekey, | |
2105 | dot11RSNAConfigGroupUpdateCount, | |
2106 | dot11RSNAConfigPairwiseUpdateCount, | |
2107 | wpa_cipher_bits(wpa_auth->conf.wpa_group), | |
2108 | dot11RSNAConfigPMKLifetime, | |
2109 | dot11RSNAConfigPMKReauthThreshold, | |
2110 | dot11RSNAConfigSATimeout, | |
2111 | RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected), | |
2112 | RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected), | |
2113 | RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected), | |
2114 | pmkid_txt, | |
2115 | RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested), | |
2116 | RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested), | |
2117 | RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested), | |
2118 | wpa_auth->dot11RSNATKIPCounterMeasuresInvoked, | |
2119 | wpa_auth->dot11RSNA4WayHandshakeFailures); | |
2120 | if (ret < 0 || (size_t) ret >= buflen - len) | |
2121 | return len; | |
2122 | len += ret; | |
2123 | ||
2124 | /* TODO: dot11RSNAConfigPairwiseCiphersTable */ | |
2125 | /* TODO: dot11RSNAConfigAuthenticationSuitesTable */ | |
2126 | ||
2127 | /* Private MIB */ | |
2128 | ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n", | |
2129 | wpa_auth->group->wpa_group_state); | |
2130 | if (ret < 0 || (size_t) ret >= buflen - len) | |
2131 | return len; | |
2132 | len += ret; | |
2133 | ||
2134 | return len; | |
2135 | } | |
2136 | ||
2137 | ||
2138 | int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen) | |
2139 | { | |
2140 | int len = 0, ret; | |
2141 | u32 pairwise = 0; | |
2142 | ||
2143 | if (sm == NULL) | |
2144 | return 0; | |
2145 | ||
2146 | /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */ | |
2147 | ||
2148 | /* dot11RSNAStatsEntry */ | |
2149 | ||
2150 | if (sm->wpa == WPA_VERSION_WPA) { | |
2151 | if (sm->pairwise == WPA_CIPHER_CCMP) | |
2152 | pairwise = WPA_CIPHER_SUITE_CCMP; | |
2153 | else if (sm->pairwise == WPA_CIPHER_TKIP) | |
2154 | pairwise = WPA_CIPHER_SUITE_TKIP; | |
2155 | else if (sm->pairwise == WPA_CIPHER_WEP104) | |
2156 | pairwise = WPA_CIPHER_SUITE_WEP104; | |
2157 | else if (sm->pairwise == WPA_CIPHER_WEP40) | |
2158 | pairwise = WPA_CIPHER_SUITE_WEP40; | |
2159 | else if (sm->pairwise == WPA_CIPHER_NONE) | |
2160 | pairwise = WPA_CIPHER_SUITE_NONE; | |
2161 | } else if (sm->wpa == WPA_VERSION_WPA2) { | |
2162 | if (sm->pairwise == WPA_CIPHER_CCMP) | |
2163 | pairwise = RSN_CIPHER_SUITE_CCMP; | |
2164 | else if (sm->pairwise == WPA_CIPHER_TKIP) | |
2165 | pairwise = RSN_CIPHER_SUITE_TKIP; | |
2166 | else if (sm->pairwise == WPA_CIPHER_WEP104) | |
2167 | pairwise = RSN_CIPHER_SUITE_WEP104; | |
2168 | else if (sm->pairwise == WPA_CIPHER_WEP40) | |
2169 | pairwise = RSN_CIPHER_SUITE_WEP40; | |
2170 | else if (sm->pairwise == WPA_CIPHER_NONE) | |
2171 | pairwise = RSN_CIPHER_SUITE_NONE; | |
2172 | } else | |
2173 | return 0; | |
2174 | ||
2175 | ret = os_snprintf( | |
2176 | buf + len, buflen - len, | |
2177 | /* TODO: dot11RSNAStatsIndex */ | |
2178 | "dot11RSNAStatsSTAAddress=" MACSTR "\n" | |
2179 | "dot11RSNAStatsVersion=1\n" | |
2180 | "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n" | |
2181 | /* TODO: dot11RSNAStatsTKIPICVErrors */ | |
2182 | "dot11RSNAStatsTKIPLocalMICFailures=%u\n" | |
2183 | "dot11RSNAStatsTKIPRemoveMICFailures=%u\n" | |
2184 | /* TODO: dot11RSNAStatsCCMPReplays */ | |
2185 | /* TODO: dot11RSNAStatsCCMPDecryptErrors */ | |
2186 | /* TODO: dot11RSNAStatsTKIPReplays */, | |
2187 | MAC2STR(sm->addr), | |
2188 | RSN_SUITE_ARG(pairwise), | |
2189 | sm->dot11RSNAStatsTKIPLocalMICFailures, | |
2190 | sm->dot11RSNAStatsTKIPRemoteMICFailures); | |
2191 | if (ret < 0 || (size_t) ret >= buflen - len) | |
2192 | return len; | |
2193 | len += ret; | |
2194 | ||
2195 | /* Private MIB */ | |
2196 | ret = os_snprintf(buf + len, buflen - len, | |
2197 | "hostapdWPAPTKState=%d\n" | |
2198 | "hostapdWPAPTKGroupState=%d\n", | |
2199 | sm->wpa_ptk_state, | |
2200 | sm->wpa_ptk_group_state); | |
2201 | if (ret < 0 || (size_t) ret >= buflen - len) | |
2202 | return len; | |
2203 | len += ret; | |
2204 | ||
2205 | return len; | |
2206 | } | |
2207 | ||
2208 | ||
2209 | void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth) | |
2210 | { | |
2211 | if (wpa_auth) | |
2212 | wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++; | |
2213 | } | |
2214 | ||
2215 | ||
2216 | int wpa_auth_pairwise_set(struct wpa_state_machine *sm) | |
2217 | { | |
2218 | return sm && sm->pairwise_set; | |
2219 | } | |
2220 | ||
2221 | ||
2222 | int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm) | |
2223 | { | |
2224 | if (sm == NULL) | |
2225 | return -1; | |
2226 | return sm->wpa_key_mgmt; | |
2227 | } | |
2228 | ||
2229 | ||
2230 | int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm) | |
2231 | { | |
2232 | if (sm == NULL) | |
2233 | return 0; | |
2234 | return sm->wpa; | |
2235 | } | |
2236 | ||
2237 | ||
2238 | int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm, | |
2239 | struct rsn_pmksa_cache_entry *entry) | |
2240 | { | |
2241 | if (sm == NULL || sm->pmksa != entry) | |
2242 | return -1; | |
2243 | sm->pmksa = NULL; | |
2244 | return 0; | |
2245 | } | |
2246 | ||
2247 | ||
2248 | struct rsn_pmksa_cache_entry * | |
2249 | wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm) | |
2250 | { | |
2251 | return sm ? sm->pmksa : NULL; | |
2252 | } | |
2253 | ||
2254 | ||
2255 | void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm) | |
2256 | { | |
2257 | if (sm) | |
2258 | sm->dot11RSNAStatsTKIPLocalMICFailures++; | |
2259 | } | |
2260 | ||
2261 | ||
2262 | const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len) | |
2263 | { | |
2264 | if (wpa_auth == NULL) | |
2265 | return NULL; | |
2266 | *len = wpa_auth->wpa_ie_len; | |
2267 | return wpa_auth->wpa_ie; | |
2268 | } | |
2269 | ||
2270 | ||
2271 | int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk, | |
2272 | int session_timeout, struct eapol_state_machine *eapol) | |
2273 | { | |
2274 | if (sm == NULL || sm->wpa != WPA_VERSION_WPA2) | |
2275 | return -1; | |
2276 | ||
2277 | if (pmksa_cache_add(sm->wpa_auth->pmksa, pmk, PMK_LEN, | |
2278 | sm->wpa_auth->addr, sm->addr, session_timeout, | |
56586197 | 2279 | eapol, sm->wpa_key_mgmt)) |
6fc6879b JM |
2280 | return 0; |
2281 | ||
2282 | return -1; | |
2283 | } | |
2284 | ||
2285 | ||
2286 | int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth, | |
2287 | const u8 *pmk, size_t len, const u8 *sta_addr, | |
2288 | int session_timeout, | |
2289 | struct eapol_state_machine *eapol) | |
2290 | { | |
2291 | if (wpa_auth == NULL) | |
2292 | return -1; | |
2293 | ||
2294 | if (pmksa_cache_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr, | |
56586197 JM |
2295 | sta_addr, session_timeout, eapol, |
2296 | WPA_KEY_MGMT_IEEE8021X)) | |
6fc6879b JM |
2297 | return 0; |
2298 | ||
2299 | return -1; | |
2300 | } | |
2301 | ||
2302 | ||
2303 | static struct wpa_group * | |
2304 | wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id) | |
2305 | { | |
2306 | struct wpa_group *group; | |
2307 | ||
2308 | if (wpa_auth == NULL || wpa_auth->group == NULL) | |
2309 | return NULL; | |
2310 | ||
2311 | wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d", | |
2312 | vlan_id); | |
2313 | group = wpa_group_init(wpa_auth, vlan_id); | |
2314 | if (group == NULL) | |
2315 | return NULL; | |
2316 | ||
2317 | group->next = wpa_auth->group->next; | |
2318 | wpa_auth->group->next = group; | |
2319 | ||
2320 | return group; | |
2321 | } | |
2322 | ||
2323 | ||
2324 | int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id) | |
2325 | { | |
2326 | struct wpa_group *group; | |
2327 | ||
2328 | if (sm == NULL || sm->wpa_auth == NULL) | |
2329 | return 0; | |
2330 | ||
2331 | group = sm->wpa_auth->group; | |
2332 | while (group) { | |
2333 | if (group->vlan_id == vlan_id) | |
2334 | break; | |
2335 | group = group->next; | |
2336 | } | |
2337 | ||
2338 | if (group == NULL) { | |
2339 | group = wpa_auth_add_group(sm->wpa_auth, vlan_id); | |
2340 | if (group == NULL) | |
2341 | return -1; | |
2342 | } | |
2343 | ||
2344 | if (sm->group == group) | |
2345 | return 0; | |
2346 | ||
2347 | wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state " | |
2348 | "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id); | |
2349 | ||
2350 | sm->group = group; | |
2351 | return 0; | |
2352 | } | |
2353 | ||
2354 | #endif /* CONFIG_NATIVE_WINDOWS */ |