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