]>
Commit | Line | Data |
---|---|---|
6fc6879b JM |
1 | /* |
2 | * hostapd - IEEE 802.11i-2004 / WPA Authenticator | |
e6965d4e | 3 | * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi> |
6fc6879b JM |
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 | ||
6226e38d | 15 | #include "utils/includes.h" |
6fc6879b | 16 | |
6226e38d JM |
17 | #include "utils/common.h" |
18 | #include "utils/eloop.h" | |
19 | #include "utils/state_machine.h" | |
81f4f619 | 20 | #include "common/ieee802_11_defs.h" |
03da66bd JM |
21 | #include "crypto/aes_wrap.h" |
22 | #include "crypto/crypto.h" | |
23 | #include "crypto/sha1.h" | |
24 | #include "crypto/sha256.h" | |
3642c431 | 25 | #include "crypto/random.h" |
281c950b | 26 | #include "eapol_auth/eapol_auth_sm.h" |
6226e38d | 27 | #include "ap_config.h" |
6fc6879b | 28 | #include "ieee802_11.h" |
6226e38d JM |
29 | #include "wpa_auth.h" |
30 | #include "pmksa_cache_auth.h" | |
6fc6879b JM |
31 | #include "wpa_auth_i.h" |
32 | #include "wpa_auth_ie.h" | |
33 | ||
34 | #define STATE_MACHINE_DATA struct wpa_state_machine | |
35 | #define STATE_MACHINE_DEBUG_PREFIX "WPA" | |
36 | #define STATE_MACHINE_ADDR sm->addr | |
37 | ||
38 | ||
39 | static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx); | |
e4a6ea1d | 40 | static int wpa_sm_step(struct wpa_state_machine *sm); |
6fc6879b JM |
41 | static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len); |
42 | static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx); | |
43 | static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth, | |
44 | struct wpa_group *group); | |
581a8cde | 45 | static void wpa_request_new_ptk(struct wpa_state_machine *sm); |
e6965d4e JM |
46 | static int wpa_gtk_update(struct wpa_authenticator *wpa_auth, |
47 | struct wpa_group *group); | |
1bdb7ab3 JM |
48 | static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth, |
49 | struct wpa_group *group); | |
6fc6879b | 50 | |
bae61562 JM |
51 | static const u32 dot11RSNAConfigGroupUpdateCount = 4; |
52 | static const u32 dot11RSNAConfigPairwiseUpdateCount = 4; | |
53 | static const u32 eapol_key_timeout_first = 100; /* ms */ | |
54 | static const u32 eapol_key_timeout_subseq = 1000; /* ms */ | |
6fc6879b JM |
55 | |
56 | /* TODO: make these configurable */ | |
57 | static const int dot11RSNAConfigPMKLifetime = 43200; | |
58 | static const int dot11RSNAConfigPMKReauthThreshold = 70; | |
59 | static const int dot11RSNAConfigSATimeout = 60; | |
60 | ||
61 | ||
62 | static inline void wpa_auth_mic_failure_report( | |
63 | struct wpa_authenticator *wpa_auth, const u8 *addr) | |
64 | { | |
65 | if (wpa_auth->cb.mic_failure_report) | |
66 | wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr); | |
67 | } | |
68 | ||
69 | ||
70 | static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth, | |
71 | const u8 *addr, wpa_eapol_variable var, | |
72 | int value) | |
73 | { | |
74 | if (wpa_auth->cb.set_eapol) | |
75 | wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value); | |
76 | } | |
77 | ||
78 | ||
79 | static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth, | |
80 | const u8 *addr, wpa_eapol_variable var) | |
81 | { | |
82 | if (wpa_auth->cb.get_eapol == NULL) | |
83 | return -1; | |
84 | return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var); | |
85 | } | |
86 | ||
87 | ||
88 | static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth, | |
89 | const u8 *addr, const u8 *prev_psk) | |
90 | { | |
91 | if (wpa_auth->cb.get_psk == NULL) | |
92 | return NULL; | |
93 | return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, prev_psk); | |
94 | } | |
95 | ||
96 | ||
97 | static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth, | |
98 | const u8 *addr, u8 *msk, size_t *len) | |
99 | { | |
100 | if (wpa_auth->cb.get_msk == NULL) | |
101 | return -1; | |
102 | return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len); | |
103 | } | |
104 | ||
105 | ||
106 | static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth, | |
107 | int vlan_id, | |
71934751 | 108 | enum wpa_alg alg, const u8 *addr, int idx, |
6fc6879b JM |
109 | u8 *key, size_t key_len) |
110 | { | |
111 | if (wpa_auth->cb.set_key == NULL) | |
112 | return -1; | |
113 | return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx, | |
114 | key, key_len); | |
115 | } | |
116 | ||
117 | ||
118 | static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth, | |
119 | const u8 *addr, int idx, u8 *seq) | |
120 | { | |
121 | if (wpa_auth->cb.get_seqnum == NULL) | |
122 | return -1; | |
123 | return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq); | |
124 | } | |
125 | ||
126 | ||
6fc6879b JM |
127 | static inline int |
128 | wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr, | |
129 | const u8 *data, size_t data_len, int encrypt) | |
130 | { | |
131 | if (wpa_auth->cb.send_eapol == NULL) | |
132 | return -1; | |
133 | return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len, | |
134 | encrypt); | |
135 | } | |
136 | ||
137 | ||
138 | int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth, | |
139 | int (*cb)(struct wpa_state_machine *sm, void *ctx), | |
140 | void *cb_ctx) | |
141 | { | |
142 | if (wpa_auth->cb.for_each_sta == NULL) | |
143 | return 0; | |
144 | return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx); | |
145 | } | |
146 | ||
147 | ||
bf98f7f3 JM |
148 | int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth, |
149 | int (*cb)(struct wpa_authenticator *a, void *ctx), | |
150 | void *cb_ctx) | |
151 | { | |
152 | if (wpa_auth->cb.for_each_auth == NULL) | |
153 | return 0; | |
154 | return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx); | |
155 | } | |
156 | ||
157 | ||
6fc6879b JM |
158 | void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr, |
159 | logger_level level, const char *txt) | |
160 | { | |
161 | if (wpa_auth->cb.logger == NULL) | |
162 | return; | |
163 | wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt); | |
164 | } | |
165 | ||
166 | ||
167 | void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr, | |
168 | logger_level level, const char *fmt, ...) | |
169 | { | |
170 | char *format; | |
171 | int maxlen; | |
172 | va_list ap; | |
173 | ||
174 | if (wpa_auth->cb.logger == NULL) | |
175 | return; | |
176 | ||
177 | maxlen = os_strlen(fmt) + 100; | |
178 | format = os_malloc(maxlen); | |
179 | if (!format) | |
180 | return; | |
181 | ||
182 | va_start(ap, fmt); | |
183 | vsnprintf(format, maxlen, fmt, ap); | |
184 | va_end(ap); | |
185 | ||
186 | wpa_auth_logger(wpa_auth, addr, level, format); | |
187 | ||
188 | os_free(format); | |
189 | } | |
190 | ||
191 | ||
192 | static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth, | |
193 | const u8 *addr) | |
194 | { | |
195 | if (wpa_auth->cb.disconnect == NULL) | |
196 | return; | |
197 | wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr, | |
198 | WLAN_REASON_PREV_AUTH_NOT_VALID); | |
199 | } | |
200 | ||
201 | ||
202 | static int wpa_use_aes_cmac(struct wpa_state_machine *sm) | |
203 | { | |
56586197 | 204 | int ret = 0; |
6fc6879b | 205 | #ifdef CONFIG_IEEE80211R |
56586197 JM |
206 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) |
207 | ret = 1; | |
6fc6879b | 208 | #endif /* CONFIG_IEEE80211R */ |
56586197 JM |
209 | #ifdef CONFIG_IEEE80211W |
210 | if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt)) | |
211 | ret = 1; | |
212 | #endif /* CONFIG_IEEE80211W */ | |
213 | return ret; | |
6fc6879b JM |
214 | } |
215 | ||
216 | ||
217 | static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx) | |
218 | { | |
219 | struct wpa_authenticator *wpa_auth = eloop_ctx; | |
220 | ||
3642c431 | 221 | if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) { |
6fc6879b JM |
222 | wpa_printf(MSG_ERROR, "Failed to get random data for WPA " |
223 | "initialization."); | |
224 | } else { | |
225 | wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd"); | |
5cb9d5c3 JM |
226 | wpa_hexdump_key(MSG_DEBUG, "GMK", |
227 | wpa_auth->group->GMK, WPA_GMK_LEN); | |
6fc6879b JM |
228 | } |
229 | ||
230 | if (wpa_auth->conf.wpa_gmk_rekey) { | |
231 | eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0, | |
232 | wpa_rekey_gmk, wpa_auth, NULL); | |
233 | } | |
234 | } | |
235 | ||
236 | ||
237 | static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx) | |
238 | { | |
239 | struct wpa_authenticator *wpa_auth = eloop_ctx; | |
240 | struct wpa_group *group; | |
241 | ||
242 | wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK"); | |
243 | for (group = wpa_auth->group; group; group = group->next) { | |
244 | group->GTKReKey = TRUE; | |
245 | do { | |
246 | group->changed = FALSE; | |
247 | wpa_group_sm_step(wpa_auth, group); | |
248 | } while (group->changed); | |
249 | } | |
250 | ||
251 | if (wpa_auth->conf.wpa_group_rekey) { | |
252 | eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, | |
253 | 0, wpa_rekey_gtk, wpa_auth, NULL); | |
254 | } | |
255 | } | |
256 | ||
257 | ||
581a8cde JM |
258 | static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx) |
259 | { | |
260 | struct wpa_authenticator *wpa_auth = eloop_ctx; | |
261 | struct wpa_state_machine *sm = timeout_ctx; | |
262 | ||
263 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK"); | |
264 | wpa_request_new_ptk(sm); | |
265 | wpa_sm_step(sm); | |
266 | } | |
267 | ||
268 | ||
6fc6879b JM |
269 | static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx) |
270 | { | |
271 | if (sm->pmksa == ctx) | |
272 | sm->pmksa = NULL; | |
273 | return 0; | |
274 | } | |
275 | ||
276 | ||
277 | static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry, | |
278 | void *ctx) | |
279 | { | |
280 | struct wpa_authenticator *wpa_auth = ctx; | |
281 | wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry); | |
282 | } | |
283 | ||
284 | ||
e6965d4e | 285 | static void wpa_group_set_key_len(struct wpa_group *group, int cipher) |
6fc6879b | 286 | { |
e6965d4e | 287 | switch (cipher) { |
6fc6879b JM |
288 | case WPA_CIPHER_CCMP: |
289 | group->GTK_len = 16; | |
290 | break; | |
291 | case WPA_CIPHER_TKIP: | |
292 | group->GTK_len = 32; | |
293 | break; | |
294 | case WPA_CIPHER_WEP104: | |
295 | group->GTK_len = 13; | |
296 | break; | |
297 | case WPA_CIPHER_WEP40: | |
298 | group->GTK_len = 5; | |
299 | break; | |
300 | } | |
e6965d4e JM |
301 | } |
302 | ||
303 | ||
1bdb7ab3 JM |
304 | static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth, |
305 | struct wpa_group *group) | |
306 | { | |
307 | u8 buf[ETH_ALEN + 8 + sizeof(group)]; | |
308 | u8 rkey[32]; | |
309 | ||
3642c431 | 310 | if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0) |
1bdb7ab3 JM |
311 | return -1; |
312 | wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN); | |
313 | ||
314 | /* | |
315 | * Counter = PRF-256(Random number, "Init Counter", | |
316 | * Local MAC Address || Time) | |
317 | */ | |
318 | os_memcpy(buf, wpa_auth->addr, ETH_ALEN); | |
319 | wpa_get_ntp_timestamp(buf + ETH_ALEN); | |
320 | os_memcpy(buf + ETH_ALEN + 8, &group, sizeof(group)); | |
3642c431 | 321 | if (random_get_bytes(rkey, sizeof(rkey)) < 0) |
1bdb7ab3 JM |
322 | return -1; |
323 | ||
324 | if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf), | |
325 | group->Counter, WPA_NONCE_LEN) < 0) | |
326 | return -1; | |
327 | wpa_hexdump_key(MSG_DEBUG, "Key Counter", | |
328 | group->Counter, WPA_NONCE_LEN); | |
329 | ||
330 | return 0; | |
331 | } | |
332 | ||
333 | ||
e6965d4e | 334 | static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth, |
bdffdc5d | 335 | int vlan_id, int delay_init) |
e6965d4e JM |
336 | { |
337 | struct wpa_group *group; | |
e6965d4e JM |
338 | |
339 | group = os_zalloc(sizeof(struct wpa_group)); | |
340 | if (group == NULL) | |
341 | return NULL; | |
342 | ||
343 | group->GTKAuthenticator = TRUE; | |
344 | group->vlan_id = vlan_id; | |
345 | ||
346 | wpa_group_set_key_len(group, wpa_auth->conf.wpa_group); | |
6fc6879b | 347 | |
08704cd8 JM |
348 | if (random_pool_ready() != 1) { |
349 | wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool " | |
350 | "for secure operations - update keys later when " | |
351 | "the first station connects"); | |
352 | } | |
353 | ||
1bdb7ab3 JM |
354 | /* |
355 | * Set initial GMK/Counter value here. The actual values that will be | |
356 | * used in negotiations will be set once the first station tries to | |
357 | * connect. This allows more time for collecting additional randomness | |
358 | * on embedded devices. | |
6fc6879b | 359 | */ |
1bdb7ab3 | 360 | if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) { |
6fc6879b JM |
361 | wpa_printf(MSG_ERROR, "Failed to get random data for WPA " |
362 | "initialization."); | |
363 | os_free(group); | |
364 | return NULL; | |
365 | } | |
6fc6879b JM |
366 | |
367 | group->GInit = TRUE; | |
bdffdc5d JM |
368 | if (delay_init) { |
369 | wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start " | |
370 | "until Beacon frames have been configured"); | |
371 | /* Initialization is completed in wpa_init_keys(). */ | |
372 | } else { | |
373 | wpa_group_sm_step(wpa_auth, group); | |
374 | group->GInit = FALSE; | |
375 | wpa_group_sm_step(wpa_auth, group); | |
376 | } | |
6fc6879b JM |
377 | |
378 | return group; | |
379 | } | |
380 | ||
381 | ||
382 | /** | |
383 | * wpa_init - Initialize WPA authenticator | |
384 | * @addr: Authenticator address | |
385 | * @conf: Configuration for WPA authenticator | |
a17df5fb | 386 | * @cb: Callback functions for WPA authenticator |
6fc6879b JM |
387 | * Returns: Pointer to WPA authenticator data or %NULL on failure |
388 | */ | |
389 | struct wpa_authenticator * wpa_init(const u8 *addr, | |
390 | struct wpa_auth_config *conf, | |
391 | struct wpa_auth_callbacks *cb) | |
392 | { | |
393 | struct wpa_authenticator *wpa_auth; | |
394 | ||
395 | wpa_auth = os_zalloc(sizeof(struct wpa_authenticator)); | |
396 | if (wpa_auth == NULL) | |
397 | return NULL; | |
398 | os_memcpy(wpa_auth->addr, addr, ETH_ALEN); | |
399 | os_memcpy(&wpa_auth->conf, conf, sizeof(*conf)); | |
400 | os_memcpy(&wpa_auth->cb, cb, sizeof(*cb)); | |
401 | ||
402 | if (wpa_auth_gen_wpa_ie(wpa_auth)) { | |
403 | wpa_printf(MSG_ERROR, "Could not generate WPA IE."); | |
404 | os_free(wpa_auth); | |
405 | return NULL; | |
406 | } | |
407 | ||
bdffdc5d | 408 | wpa_auth->group = wpa_group_init(wpa_auth, 0, 1); |
6fc6879b JM |
409 | if (wpa_auth->group == NULL) { |
410 | os_free(wpa_auth->wpa_ie); | |
411 | os_free(wpa_auth); | |
412 | return NULL; | |
413 | } | |
414 | ||
4bb081f1 JM |
415 | wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb, |
416 | wpa_auth); | |
6fc6879b JM |
417 | if (wpa_auth->pmksa == NULL) { |
418 | wpa_printf(MSG_ERROR, "PMKSA cache initialization failed."); | |
419 | os_free(wpa_auth->wpa_ie); | |
420 | os_free(wpa_auth); | |
421 | return NULL; | |
422 | } | |
423 | ||
424 | #ifdef CONFIG_IEEE80211R | |
425 | wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init(); | |
426 | if (wpa_auth->ft_pmk_cache == NULL) { | |
427 | wpa_printf(MSG_ERROR, "FT PMK cache initialization failed."); | |
428 | os_free(wpa_auth->wpa_ie); | |
4bb081f1 | 429 | pmksa_cache_auth_deinit(wpa_auth->pmksa); |
6fc6879b JM |
430 | os_free(wpa_auth); |
431 | return NULL; | |
432 | } | |
433 | #endif /* CONFIG_IEEE80211R */ | |
434 | ||
435 | if (wpa_auth->conf.wpa_gmk_rekey) { | |
436 | eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0, | |
437 | wpa_rekey_gmk, wpa_auth, NULL); | |
438 | } | |
439 | ||
440 | if (wpa_auth->conf.wpa_group_rekey) { | |
441 | eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0, | |
442 | wpa_rekey_gtk, wpa_auth, NULL); | |
443 | } | |
444 | ||
445 | return wpa_auth; | |
446 | } | |
447 | ||
448 | ||
bdffdc5d JM |
449 | int wpa_init_keys(struct wpa_authenticator *wpa_auth) |
450 | { | |
451 | struct wpa_group *group = wpa_auth->group; | |
452 | ||
453 | wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial " | |
454 | "keys"); | |
455 | wpa_group_sm_step(wpa_auth, group); | |
456 | group->GInit = FALSE; | |
457 | wpa_group_sm_step(wpa_auth, group); | |
458 | return 0; | |
459 | } | |
460 | ||
461 | ||
6fc6879b JM |
462 | /** |
463 | * wpa_deinit - Deinitialize WPA authenticator | |
464 | * @wpa_auth: Pointer to WPA authenticator data from wpa_init() | |
465 | */ | |
466 | void wpa_deinit(struct wpa_authenticator *wpa_auth) | |
467 | { | |
468 | struct wpa_group *group, *prev; | |
469 | ||
470 | eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL); | |
471 | eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL); | |
472 | ||
473 | #ifdef CONFIG_PEERKEY | |
474 | while (wpa_auth->stsl_negotiations) | |
475 | wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations); | |
476 | #endif /* CONFIG_PEERKEY */ | |
477 | ||
4bb081f1 | 478 | pmksa_cache_auth_deinit(wpa_auth->pmksa); |
6fc6879b JM |
479 | |
480 | #ifdef CONFIG_IEEE80211R | |
481 | wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache); | |
482 | wpa_auth->ft_pmk_cache = NULL; | |
483 | #endif /* CONFIG_IEEE80211R */ | |
484 | ||
485 | os_free(wpa_auth->wpa_ie); | |
486 | ||
487 | group = wpa_auth->group; | |
488 | while (group) { | |
489 | prev = group; | |
490 | group = group->next; | |
491 | os_free(prev); | |
492 | } | |
493 | ||
494 | os_free(wpa_auth); | |
495 | } | |
496 | ||
497 | ||
498 | /** | |
499 | * wpa_reconfig - Update WPA authenticator configuration | |
500 | * @wpa_auth: Pointer to WPA authenticator data from wpa_init() | |
501 | * @conf: Configuration for WPA authenticator | |
502 | */ | |
503 | int wpa_reconfig(struct wpa_authenticator *wpa_auth, | |
504 | struct wpa_auth_config *conf) | |
505 | { | |
e6965d4e | 506 | struct wpa_group *group; |
6fc6879b JM |
507 | if (wpa_auth == NULL) |
508 | return 0; | |
509 | ||
510 | os_memcpy(&wpa_auth->conf, conf, sizeof(*conf)); | |
ad08c363 JM |
511 | if (wpa_auth_gen_wpa_ie(wpa_auth)) { |
512 | wpa_printf(MSG_ERROR, "Could not generate WPA IE."); | |
513 | return -1; | |
514 | } | |
515 | ||
e6965d4e JM |
516 | /* |
517 | * Reinitialize GTK to make sure it is suitable for the new | |
518 | * configuration. | |
519 | */ | |
520 | group = wpa_auth->group; | |
521 | wpa_group_set_key_len(group, wpa_auth->conf.wpa_group); | |
522 | group->GInit = TRUE; | |
523 | wpa_group_sm_step(wpa_auth, group); | |
524 | group->GInit = FALSE; | |
525 | wpa_group_sm_step(wpa_auth, group); | |
526 | ||
6fc6879b JM |
527 | return 0; |
528 | } | |
529 | ||
530 | ||
531 | struct wpa_state_machine * | |
532 | wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr) | |
533 | { | |
534 | struct wpa_state_machine *sm; | |
535 | ||
536 | sm = os_zalloc(sizeof(struct wpa_state_machine)); | |
537 | if (sm == NULL) | |
538 | return NULL; | |
539 | os_memcpy(sm->addr, addr, ETH_ALEN); | |
540 | ||
541 | sm->wpa_auth = wpa_auth; | |
542 | sm->group = wpa_auth->group; | |
543 | ||
544 | return sm; | |
545 | } | |
546 | ||
547 | ||
6f9b5d16 JM |
548 | int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth, |
549 | struct wpa_state_machine *sm) | |
6fc6879b JM |
550 | { |
551 | if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL) | |
6f9b5d16 | 552 | return -1; |
6fc6879b JM |
553 | |
554 | #ifdef CONFIG_IEEE80211R | |
555 | if (sm->ft_completed) { | |
556 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
557 | "FT authentication already completed - do not " | |
558 | "start 4-way handshake"); | |
6f9b5d16 | 559 | return 0; |
6fc6879b JM |
560 | } |
561 | #endif /* CONFIG_IEEE80211R */ | |
562 | ||
563 | if (sm->started) { | |
22a299ee | 564 | os_memset(&sm->key_replay, 0, sizeof(sm->key_replay)); |
6fc6879b | 565 | sm->ReAuthenticationRequest = TRUE; |
6f9b5d16 | 566 | return wpa_sm_step(sm); |
6fc6879b JM |
567 | } |
568 | ||
569 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
570 | "start authentication"); | |
571 | sm->started = 1; | |
572 | ||
573 | sm->Init = TRUE; | |
e4a6ea1d | 574 | if (wpa_sm_step(sm) == 1) |
6f9b5d16 | 575 | return 1; /* should not really happen */ |
6fc6879b JM |
576 | sm->Init = FALSE; |
577 | sm->AuthenticationRequest = TRUE; | |
6f9b5d16 | 578 | return wpa_sm_step(sm); |
6fc6879b JM |
579 | } |
580 | ||
581 | ||
a8d05fca JM |
582 | void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm) |
583 | { | |
584 | /* WPA/RSN was not used - clear WPA state. This is needed if the STA | |
585 | * reassociates back to the same AP while the previous entry for the | |
586 | * STA has not yet been removed. */ | |
587 | if (sm == NULL) | |
588 | return; | |
589 | ||
590 | sm->wpa_key_mgmt = 0; | |
591 | } | |
592 | ||
593 | ||
6fc6879b JM |
594 | static void wpa_free_sta_sm(struct wpa_state_machine *sm) |
595 | { | |
2ade8ef2 JM |
596 | if (sm->GUpdateStationKeys) { |
597 | sm->group->GKeyDoneStations--; | |
598 | sm->GUpdateStationKeys = FALSE; | |
599 | } | |
0f857f43 JM |
600 | #ifdef CONFIG_IEEE80211R |
601 | os_free(sm->assoc_resp_ftie); | |
602 | #endif /* CONFIG_IEEE80211R */ | |
6fc6879b JM |
603 | os_free(sm->last_rx_eapol_key); |
604 | os_free(sm->wpa_ie); | |
605 | os_free(sm); | |
606 | } | |
607 | ||
608 | ||
609 | void wpa_auth_sta_deinit(struct wpa_state_machine *sm) | |
610 | { | |
611 | if (sm == NULL) | |
612 | return; | |
613 | ||
614 | if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) { | |
615 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
616 | "strict rekeying - force GTK rekey since STA " | |
617 | "is leaving"); | |
618 | eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL); | |
619 | eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth, | |
620 | NULL); | |
621 | } | |
622 | ||
623 | eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm); | |
e4bf4db9 | 624 | sm->pending_1_of_4_timeout = 0; |
6fc6879b | 625 | eloop_cancel_timeout(wpa_sm_call_step, sm, NULL); |
581a8cde | 626 | eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm); |
6fc6879b JM |
627 | if (sm->in_step_loop) { |
628 | /* Must not free state machine while wpa_sm_step() is running. | |
629 | * Freeing will be completed in the end of wpa_sm_step(). */ | |
630 | wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state " | |
631 | "machine deinit for " MACSTR, MAC2STR(sm->addr)); | |
632 | sm->pending_deinit = 1; | |
633 | } else | |
634 | wpa_free_sta_sm(sm); | |
635 | } | |
636 | ||
637 | ||
638 | static void wpa_request_new_ptk(struct wpa_state_machine *sm) | |
639 | { | |
640 | if (sm == NULL) | |
641 | return; | |
642 | ||
643 | sm->PTKRequest = TRUE; | |
644 | sm->PTK_valid = 0; | |
645 | } | |
646 | ||
647 | ||
22a299ee JM |
648 | static int wpa_replay_counter_valid(struct wpa_state_machine *sm, |
649 | const u8 *replay_counter) | |
650 | { | |
651 | int i; | |
652 | for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) { | |
653 | if (!sm->key_replay[i].valid) | |
654 | break; | |
655 | if (os_memcmp(replay_counter, sm->key_replay[i].counter, | |
656 | WPA_REPLAY_COUNTER_LEN) == 0) | |
657 | return 1; | |
658 | } | |
659 | return 0; | |
660 | } | |
661 | ||
662 | ||
0f857f43 JM |
663 | #ifdef CONFIG_IEEE80211R |
664 | static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth, | |
665 | struct wpa_state_machine *sm, | |
666 | struct wpa_eapol_ie_parse *kde) | |
667 | { | |
668 | struct wpa_ie_data ie; | |
669 | struct rsn_mdie *mdie; | |
670 | ||
671 | if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 || | |
672 | ie.num_pmkid != 1 || ie.pmkid == NULL) { | |
673 | wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in " | |
674 | "FT 4-way handshake message 2/4"); | |
675 | return -1; | |
676 | } | |
677 | ||
678 | os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN); | |
679 | wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant", | |
680 | sm->sup_pmk_r1_name, PMKID_LEN); | |
681 | ||
682 | if (!kde->mdie || !kde->ftie) { | |
683 | wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake " | |
684 | "message 2/4", kde->mdie ? "FTIE" : "MDIE"); | |
685 | return -1; | |
686 | } | |
687 | ||
688 | mdie = (struct rsn_mdie *) (kde->mdie + 2); | |
689 | if (kde->mdie[1] < sizeof(struct rsn_mdie) || | |
690 | os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain, | |
691 | MOBILITY_DOMAIN_ID_LEN) != 0) { | |
692 | wpa_printf(MSG_DEBUG, "FT: MDIE mismatch"); | |
693 | return -1; | |
694 | } | |
695 | ||
696 | if (sm->assoc_resp_ftie && | |
697 | (kde->ftie[1] != sm->assoc_resp_ftie[1] || | |
698 | os_memcmp(kde->ftie, sm->assoc_resp_ftie, | |
699 | 2 + sm->assoc_resp_ftie[1]) != 0)) { | |
700 | wpa_printf(MSG_DEBUG, "FT: FTIE mismatch"); | |
701 | wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4", | |
702 | kde->ftie, kde->ftie_len); | |
703 | wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp", | |
704 | sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]); | |
705 | return -1; | |
706 | } | |
707 | ||
708 | return 0; | |
709 | } | |
710 | #endif /* CONFIG_IEEE80211R */ | |
711 | ||
712 | ||
ec027805 JM |
713 | static void wpa_receive_error_report(struct wpa_authenticator *wpa_auth, |
714 | struct wpa_state_machine *sm, int group) | |
715 | { | |
716 | /* Supplicant reported a Michael MIC error */ | |
717 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, | |
718 | "received EAPOL-Key Error Request " | |
719 | "(STA detected Michael MIC failure (group=%d))", | |
720 | group); | |
721 | wpa_auth_mic_failure_report(wpa_auth, sm->addr); | |
722 | sm->dot11RSNAStatsTKIPRemoteMICFailures++; | |
723 | wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++; | |
724 | ||
725 | /* | |
726 | * Error report is not a request for a new key handshake, but since | |
727 | * Authenticator may do it, let's change the keys now anyway. | |
728 | */ | |
729 | wpa_request_new_ptk(sm); | |
730 | } | |
731 | ||
732 | ||
6fc6879b JM |
733 | void wpa_receive(struct wpa_authenticator *wpa_auth, |
734 | struct wpa_state_machine *sm, | |
735 | u8 *data, size_t data_len) | |
736 | { | |
737 | struct ieee802_1x_hdr *hdr; | |
738 | struct wpa_eapol_key *key; | |
739 | u16 key_info, key_data_length; | |
740 | enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST, | |
741 | SMK_M1, SMK_M3, SMK_ERROR } msg; | |
742 | char *msgtxt; | |
743 | struct wpa_eapol_ie_parse kde; | |
26e23750 | 744 | int ft; |
1566ec46 JM |
745 | const u8 *eapol_key_ie; |
746 | size_t eapol_key_ie_len; | |
6fc6879b JM |
747 | |
748 | if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL) | |
749 | return; | |
750 | ||
751 | if (data_len < sizeof(*hdr) + sizeof(*key)) | |
752 | return; | |
753 | ||
754 | hdr = (struct ieee802_1x_hdr *) data; | |
755 | key = (struct wpa_eapol_key *) (hdr + 1); | |
756 | key_info = WPA_GET_BE16(key->key_info); | |
757 | key_data_length = WPA_GET_BE16(key->key_data_length); | |
758 | if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) { | |
759 | wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - " | |
760 | "key_data overflow (%d > %lu)", | |
761 | key_data_length, | |
762 | (unsigned long) (data_len - sizeof(*hdr) - | |
763 | sizeof(*key))); | |
764 | return; | |
765 | } | |
766 | ||
f8e96eb6 JM |
767 | if (sm->wpa == WPA_VERSION_WPA2) { |
768 | if (key->type != EAPOL_KEY_TYPE_RSN) { | |
769 | wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with " | |
770 | "unexpected type %d in RSN mode", | |
771 | key->type); | |
772 | return; | |
773 | } | |
774 | } else { | |
775 | if (key->type != EAPOL_KEY_TYPE_WPA) { | |
776 | wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with " | |
777 | "unexpected type %d in WPA mode", | |
778 | key->type); | |
779 | return; | |
780 | } | |
781 | } | |
782 | ||
bc8318ac JM |
783 | wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce, |
784 | WPA_NONCE_LEN); | |
785 | wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter", | |
786 | key->replay_counter, WPA_REPLAY_COUNTER_LEN); | |
787 | ||
6fc6879b JM |
788 | /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys |
789 | * are set */ | |
790 | ||
791 | if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) == | |
792 | (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) { | |
793 | if (key_info & WPA_KEY_INFO_ERROR) { | |
794 | msg = SMK_ERROR; | |
795 | msgtxt = "SMK Error"; | |
796 | } else { | |
797 | msg = SMK_M1; | |
798 | msgtxt = "SMK M1"; | |
799 | } | |
800 | } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) { | |
801 | msg = SMK_M3; | |
802 | msgtxt = "SMK M3"; | |
803 | } else if (key_info & WPA_KEY_INFO_REQUEST) { | |
804 | msg = REQUEST; | |
805 | msgtxt = "Request"; | |
806 | } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) { | |
807 | msg = GROUP_2; | |
808 | msgtxt = "2/2 Group"; | |
809 | } else if (key_data_length == 0) { | |
810 | msg = PAIRWISE_4; | |
811 | msgtxt = "4/4 Pairwise"; | |
812 | } else { | |
813 | msg = PAIRWISE_2; | |
814 | msgtxt = "2/4 Pairwise"; | |
815 | } | |
816 | ||
817 | /* TODO: key_info type validation for PeerKey */ | |
818 | if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 || | |
819 | msg == GROUP_2) { | |
820 | u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK; | |
821 | if (sm->pairwise == WPA_CIPHER_CCMP) { | |
822 | if (wpa_use_aes_cmac(sm) && | |
823 | ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) { | |
824 | wpa_auth_logger(wpa_auth, sm->addr, | |
825 | LOGGER_WARNING, | |
826 | "advertised support for " | |
827 | "AES-128-CMAC, but did not " | |
828 | "use it"); | |
829 | return; | |
830 | } | |
831 | ||
832 | if (!wpa_use_aes_cmac(sm) && | |
833 | ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) { | |
834 | wpa_auth_logger(wpa_auth, sm->addr, | |
835 | LOGGER_WARNING, | |
836 | "did not use HMAC-SHA1-AES " | |
837 | "with CCMP"); | |
838 | return; | |
839 | } | |
840 | } | |
841 | } | |
842 | ||
843 | if (key_info & WPA_KEY_INFO_REQUEST) { | |
844 | if (sm->req_replay_counter_used && | |
845 | os_memcmp(key->replay_counter, sm->req_replay_counter, | |
846 | WPA_REPLAY_COUNTER_LEN) <= 0) { | |
847 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING, | |
848 | "received EAPOL-Key request with " | |
849 | "replayed counter"); | |
850 | return; | |
851 | } | |
852 | } | |
853 | ||
854 | if (!(key_info & WPA_KEY_INFO_REQUEST) && | |
22a299ee JM |
855 | !wpa_replay_counter_valid(sm, key->replay_counter)) { |
856 | int i; | |
6fc6879b JM |
857 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, |
858 | "received EAPOL-Key %s with unexpected " | |
859 | "replay counter", msgtxt); | |
22a299ee JM |
860 | for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) { |
861 | if (!sm->key_replay[i].valid) | |
862 | break; | |
863 | wpa_hexdump(MSG_DEBUG, "pending replay counter", | |
864 | sm->key_replay[i].counter, | |
865 | WPA_REPLAY_COUNTER_LEN); | |
866 | } | |
6fc6879b JM |
867 | wpa_hexdump(MSG_DEBUG, "received replay counter", |
868 | key->replay_counter, WPA_REPLAY_COUNTER_LEN); | |
869 | return; | |
870 | } | |
871 | ||
872 | switch (msg) { | |
873 | case PAIRWISE_2: | |
874 | if (sm->wpa_ptk_state != WPA_PTK_PTKSTART && | |
875 | sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING) { | |
876 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, | |
877 | "received EAPOL-Key msg 2/4 in " | |
878 | "invalid state (%d) - dropped", | |
879 | sm->wpa_ptk_state); | |
880 | return; | |
881 | } | |
bbb921da | 882 | random_add_randomness(key->key_nonce, WPA_NONCE_LEN); |
08704cd8 JM |
883 | if (sm->group->reject_4way_hs_for_entropy) { |
884 | /* | |
885 | * The system did not have enough entropy to generate | |
886 | * strong random numbers. Reject the first 4-way | |
887 | * handshake(s) and collect some entropy based on the | |
888 | * information from it. Once enough entropy is | |
889 | * available, the next atempt will trigger GMK/Key | |
890 | * Counter update and the station will be allowed to | |
891 | * continue. | |
892 | */ | |
893 | wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to " | |
894 | "collect more entropy for random number " | |
895 | "generation"); | |
896 | sm->group->reject_4way_hs_for_entropy = FALSE; | |
897 | random_mark_pool_ready(); | |
898 | sm->group->first_sta_seen = FALSE; | |
899 | wpa_sta_disconnect(wpa_auth, sm->addr); | |
900 | return; | |
901 | } | |
1566ec46 JM |
902 | if (wpa_parse_kde_ies((u8 *) (key + 1), key_data_length, |
903 | &kde) < 0) { | |
904 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, | |
905 | "received EAPOL-Key msg 2/4 with " | |
906 | "invalid Key Data contents"); | |
907 | return; | |
908 | } | |
909 | if (kde.rsn_ie) { | |
910 | eapol_key_ie = kde.rsn_ie; | |
911 | eapol_key_ie_len = kde.rsn_ie_len; | |
912 | } else { | |
913 | eapol_key_ie = kde.wpa_ie; | |
914 | eapol_key_ie_len = kde.wpa_ie_len; | |
915 | } | |
26e23750 JM |
916 | ft = sm->wpa == WPA_VERSION_WPA2 && |
917 | wpa_key_mgmt_ft(sm->wpa_key_mgmt); | |
6fc6879b | 918 | if (sm->wpa_ie == NULL || |
26e23750 JM |
919 | wpa_compare_rsn_ie(ft, |
920 | sm->wpa_ie, sm->wpa_ie_len, | |
1566ec46 | 921 | eapol_key_ie, eapol_key_ie_len)) { |
6fc6879b JM |
922 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
923 | "WPA IE from (Re)AssocReq did not " | |
924 | "match with msg 2/4"); | |
925 | if (sm->wpa_ie) { | |
926 | wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq", | |
927 | sm->wpa_ie, sm->wpa_ie_len); | |
928 | } | |
929 | wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4", | |
1566ec46 | 930 | eapol_key_ie, eapol_key_ie_len); |
6fc6879b JM |
931 | /* MLME-DEAUTHENTICATE.request */ |
932 | wpa_sta_disconnect(wpa_auth, sm->addr); | |
933 | return; | |
934 | } | |
26e23750 | 935 | #ifdef CONFIG_IEEE80211R |
0f857f43 JM |
936 | if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) { |
937 | wpa_sta_disconnect(wpa_auth, sm->addr); | |
938 | return; | |
26e23750 JM |
939 | } |
940 | #endif /* CONFIG_IEEE80211R */ | |
6fc6879b JM |
941 | break; |
942 | case PAIRWISE_4: | |
943 | if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING || | |
944 | !sm->PTK_valid) { | |
945 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, | |
946 | "received EAPOL-Key msg 4/4 in " | |
947 | "invalid state (%d) - dropped", | |
948 | sm->wpa_ptk_state); | |
949 | return; | |
950 | } | |
951 | break; | |
952 | case GROUP_2: | |
953 | if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING | |
954 | || !sm->PTK_valid) { | |
955 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, | |
956 | "received EAPOL-Key msg 2/2 in " | |
957 | "invalid state (%d) - dropped", | |
958 | sm->wpa_ptk_group_state); | |
959 | return; | |
960 | } | |
961 | break; | |
962 | #ifdef CONFIG_PEERKEY | |
963 | case SMK_M1: | |
964 | case SMK_M3: | |
965 | case SMK_ERROR: | |
966 | if (!wpa_auth->conf.peerkey) { | |
967 | wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but " | |
968 | "PeerKey use disabled - ignoring message"); | |
969 | return; | |
970 | } | |
971 | if (!sm->PTK_valid) { | |
972 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
973 | "received EAPOL-Key msg SMK in " | |
974 | "invalid state - dropped"); | |
975 | return; | |
976 | } | |
977 | break; | |
978 | #else /* CONFIG_PEERKEY */ | |
979 | case SMK_M1: | |
980 | case SMK_M3: | |
981 | case SMK_ERROR: | |
982 | return; /* STSL disabled - ignore SMK messages */ | |
983 | #endif /* CONFIG_PEERKEY */ | |
984 | case REQUEST: | |
985 | break; | |
986 | } | |
987 | ||
988 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
989 | "received EAPOL-Key frame (%s)", msgtxt); | |
990 | ||
991 | if (key_info & WPA_KEY_INFO_ACK) { | |
992 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
993 | "received invalid EAPOL-Key: Key Ack set"); | |
994 | return; | |
995 | } | |
996 | ||
997 | if (!(key_info & WPA_KEY_INFO_MIC)) { | |
998 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
999 | "received invalid EAPOL-Key: Key MIC not set"); | |
1000 | return; | |
1001 | } | |
1002 | ||
1003 | sm->MICVerified = FALSE; | |
1004 | if (sm->PTK_valid) { | |
1005 | if (wpa_verify_key_mic(&sm->PTK, data, data_len)) { | |
1006 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
1007 | "received EAPOL-Key with invalid MIC"); | |
1008 | return; | |
1009 | } | |
1010 | sm->MICVerified = TRUE; | |
1011 | eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm); | |
e4bf4db9 | 1012 | sm->pending_1_of_4_timeout = 0; |
6fc6879b JM |
1013 | } |
1014 | ||
1015 | if (key_info & WPA_KEY_INFO_REQUEST) { | |
1016 | if (sm->MICVerified) { | |
1017 | sm->req_replay_counter_used = 1; | |
1018 | os_memcpy(sm->req_replay_counter, key->replay_counter, | |
1019 | WPA_REPLAY_COUNTER_LEN); | |
1020 | } else { | |
1021 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
1022 | "received EAPOL-Key request with " | |
1023 | "invalid MIC"); | |
1024 | return; | |
1025 | } | |
1026 | ||
1027 | /* | |
1028 | * TODO: should decrypt key data field if encryption was used; | |
1029 | * even though MAC address KDE is not normally encrypted, | |
1030 | * supplicant is allowed to encrypt it. | |
1031 | */ | |
1032 | if (msg == SMK_ERROR) { | |
1033 | #ifdef CONFIG_PEERKEY | |
1034 | wpa_smk_error(wpa_auth, sm, key); | |
1035 | #endif /* CONFIG_PEERKEY */ | |
1036 | return; | |
1037 | } else if (key_info & WPA_KEY_INFO_ERROR) { | |
ec027805 JM |
1038 | wpa_receive_error_report( |
1039 | wpa_auth, sm, | |
1040 | !(key_info & WPA_KEY_INFO_KEY_TYPE)); | |
6fc6879b JM |
1041 | } else if (key_info & WPA_KEY_INFO_KEY_TYPE) { |
1042 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
1043 | "received EAPOL-Key Request for new " | |
1044 | "4-Way Handshake"); | |
1045 | wpa_request_new_ptk(sm); | |
1046 | #ifdef CONFIG_PEERKEY | |
1047 | } else if (msg == SMK_M1) { | |
1048 | wpa_smk_m1(wpa_auth, sm, key); | |
1049 | #endif /* CONFIG_PEERKEY */ | |
1050 | } else if (key_data_length > 0 && | |
1051 | wpa_parse_kde_ies((const u8 *) (key + 1), | |
1052 | key_data_length, &kde) == 0 && | |
1053 | kde.mac_addr) { | |
1054 | } else { | |
1055 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
1056 | "received EAPOL-Key Request for GTK " | |
1057 | "rekeying"); | |
1058 | /* FIX: why was this triggering PTK rekeying for the | |
1059 | * STA that requested Group Key rekeying?? */ | |
1060 | /* wpa_request_new_ptk(sta->wpa_sm); */ | |
1061 | eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL); | |
1062 | wpa_rekey_gtk(wpa_auth, NULL); | |
1063 | } | |
1064 | } else { | |
22a299ee JM |
1065 | /* Do not allow the same key replay counter to be reused. This |
1066 | * does also invalidate all other pending replay counters if | |
1067 | * retransmissions were used, i.e., we will only process one of | |
1068 | * the pending replies and ignore rest if more than one is | |
1069 | * received. */ | |
1070 | sm->key_replay[0].valid = FALSE; | |
6fc6879b JM |
1071 | } |
1072 | ||
1073 | #ifdef CONFIG_PEERKEY | |
1074 | if (msg == SMK_M3) { | |
1075 | wpa_smk_m3(wpa_auth, sm, key); | |
1076 | return; | |
1077 | } | |
1078 | #endif /* CONFIG_PEERKEY */ | |
1079 | ||
1080 | os_free(sm->last_rx_eapol_key); | |
1081 | sm->last_rx_eapol_key = os_malloc(data_len); | |
1082 | if (sm->last_rx_eapol_key == NULL) | |
1083 | return; | |
1084 | os_memcpy(sm->last_rx_eapol_key, data, data_len); | |
1085 | sm->last_rx_eapol_key_len = data_len; | |
1086 | ||
1087 | sm->EAPOLKeyReceived = TRUE; | |
1088 | sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE); | |
1089 | sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST); | |
1090 | os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN); | |
1091 | wpa_sm_step(sm); | |
1092 | } | |
1093 | ||
1094 | ||
3c7302c2 JM |
1095 | static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr, |
1096 | const u8 *gnonce, u8 *gtk, size_t gtk_len) | |
6fc6879b | 1097 | { |
5cb9d5c3 JM |
1098 | u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16]; |
1099 | u8 *pos; | |
3c7302c2 | 1100 | int ret = 0; |
6fc6879b | 1101 | |
5cb9d5c3 JM |
1102 | /* GTK = PRF-X(GMK, "Group key expansion", |
1103 | * AA || GNonce || Time || random data) | |
1104 | * The example described in the IEEE 802.11 standard uses only AA and | |
1105 | * GNonce as inputs here. Add some more entropy since this derivation | |
1106 | * is done only at the Authenticator and as such, does not need to be | |
1107 | * exactly same. | |
1108 | */ | |
6fc6879b JM |
1109 | os_memcpy(data, addr, ETH_ALEN); |
1110 | os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN); | |
5cb9d5c3 JM |
1111 | pos = data + ETH_ALEN + WPA_NONCE_LEN; |
1112 | wpa_get_ntp_timestamp(pos); | |
1113 | pos += 8; | |
3642c431 | 1114 | if (random_get_bytes(pos, 16) < 0) |
3c7302c2 | 1115 | ret = -1; |
6fc6879b | 1116 | |
56586197 | 1117 | #ifdef CONFIG_IEEE80211W |
5cb9d5c3 | 1118 | sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len); |
56586197 | 1119 | #else /* CONFIG_IEEE80211W */ |
3c7302c2 JM |
1120 | if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len) |
1121 | < 0) | |
1122 | ret = -1; | |
56586197 | 1123 | #endif /* CONFIG_IEEE80211W */ |
3c7302c2 JM |
1124 | |
1125 | return ret; | |
6fc6879b JM |
1126 | } |
1127 | ||
1128 | ||
1129 | static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx) | |
1130 | { | |
1131 | struct wpa_authenticator *wpa_auth = eloop_ctx; | |
1132 | struct wpa_state_machine *sm = timeout_ctx; | |
1133 | ||
e4bf4db9 | 1134 | sm->pending_1_of_4_timeout = 0; |
6fc6879b JM |
1135 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout"); |
1136 | sm->TimeoutEvt = TRUE; | |
1137 | wpa_sm_step(sm); | |
1138 | } | |
1139 | ||
1140 | ||
1141 | void __wpa_send_eapol(struct wpa_authenticator *wpa_auth, | |
1142 | struct wpa_state_machine *sm, int key_info, | |
1143 | const u8 *key_rsc, const u8 *nonce, | |
1144 | const u8 *kde, size_t kde_len, | |
1145 | int keyidx, int encr, int force_version) | |
1146 | { | |
1147 | struct ieee802_1x_hdr *hdr; | |
1148 | struct wpa_eapol_key *key; | |
1149 | size_t len; | |
1150 | int alg; | |
1151 | int key_data_len, pad_len = 0; | |
1152 | u8 *buf, *pos; | |
1153 | int version, pairwise; | |
22a299ee | 1154 | int i; |
6fc6879b JM |
1155 | |
1156 | len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key); | |
1157 | ||
1158 | if (force_version) | |
1159 | version = force_version; | |
1160 | else if (wpa_use_aes_cmac(sm)) | |
1161 | version = WPA_KEY_INFO_TYPE_AES_128_CMAC; | |
1162 | else if (sm->pairwise == WPA_CIPHER_CCMP) | |
1163 | version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES; | |
1164 | else | |
1165 | version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4; | |
1166 | ||
1167 | pairwise = key_info & WPA_KEY_INFO_KEY_TYPE; | |
1168 | ||
1169 | wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d " | |
1170 | "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d " | |
1171 | "encr=%d)", | |
1172 | version, | |
1173 | (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0, | |
1174 | (key_info & WPA_KEY_INFO_MIC) ? 1 : 0, | |
1175 | (key_info & WPA_KEY_INFO_ACK) ? 1 : 0, | |
1176 | (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0, | |
1177 | pairwise, (unsigned long) kde_len, keyidx, encr); | |
1178 | ||
1179 | key_data_len = kde_len; | |
1180 | ||
1181 | if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES || | |
1182 | version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) { | |
1183 | pad_len = key_data_len % 8; | |
1184 | if (pad_len) | |
1185 | pad_len = 8 - pad_len; | |
1186 | key_data_len += pad_len + 8; | |
1187 | } | |
1188 | ||
1189 | len += key_data_len; | |
1190 | ||
1191 | hdr = os_zalloc(len); | |
1192 | if (hdr == NULL) | |
1193 | return; | |
1194 | hdr->version = wpa_auth->conf.eapol_version; | |
1195 | hdr->type = IEEE802_1X_TYPE_EAPOL_KEY; | |
1196 | hdr->length = host_to_be16(len - sizeof(*hdr)); | |
1197 | key = (struct wpa_eapol_key *) (hdr + 1); | |
1198 | ||
1199 | key->type = sm->wpa == WPA_VERSION_WPA2 ? | |
1200 | EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA; | |
1201 | key_info |= version; | |
1202 | if (encr && sm->wpa == WPA_VERSION_WPA2) | |
1203 | key_info |= WPA_KEY_INFO_ENCR_KEY_DATA; | |
1204 | if (sm->wpa != WPA_VERSION_WPA2) | |
1205 | key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT; | |
1206 | WPA_PUT_BE16(key->key_info, key_info); | |
1207 | ||
1208 | alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group; | |
1209 | switch (alg) { | |
1210 | case WPA_CIPHER_CCMP: | |
1211 | WPA_PUT_BE16(key->key_length, 16); | |
1212 | break; | |
1213 | case WPA_CIPHER_TKIP: | |
1214 | WPA_PUT_BE16(key->key_length, 32); | |
1215 | break; | |
1216 | case WPA_CIPHER_WEP40: | |
1217 | WPA_PUT_BE16(key->key_length, 5); | |
1218 | break; | |
1219 | case WPA_CIPHER_WEP104: | |
1220 | WPA_PUT_BE16(key->key_length, 13); | |
1221 | break; | |
1222 | } | |
1223 | if (key_info & WPA_KEY_INFO_SMK_MESSAGE) | |
1224 | WPA_PUT_BE16(key->key_length, 0); | |
1225 | ||
1226 | /* FIX: STSL: what to use as key_replay_counter? */ | |
22a299ee JM |
1227 | for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) { |
1228 | sm->key_replay[i].valid = sm->key_replay[i - 1].valid; | |
1229 | os_memcpy(sm->key_replay[i].counter, | |
1230 | sm->key_replay[i - 1].counter, | |
1231 | WPA_REPLAY_COUNTER_LEN); | |
1232 | } | |
1233 | inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN); | |
1234 | os_memcpy(key->replay_counter, sm->key_replay[0].counter, | |
6fc6879b | 1235 | WPA_REPLAY_COUNTER_LEN); |
22a299ee | 1236 | sm->key_replay[0].valid = TRUE; |
6fc6879b JM |
1237 | |
1238 | if (nonce) | |
1239 | os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN); | |
1240 | ||
1241 | if (key_rsc) | |
1242 | os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN); | |
1243 | ||
1244 | if (kde && !encr) { | |
1245 | os_memcpy(key + 1, kde, kde_len); | |
1246 | WPA_PUT_BE16(key->key_data_length, kde_len); | |
1247 | } else if (encr && kde) { | |
1248 | buf = os_zalloc(key_data_len); | |
1249 | if (buf == NULL) { | |
1250 | os_free(hdr); | |
1251 | return; | |
1252 | } | |
1253 | pos = buf; | |
1254 | os_memcpy(pos, kde, kde_len); | |
1255 | pos += kde_len; | |
1256 | ||
1257 | if (pad_len) | |
1258 | *pos++ = 0xdd; | |
1259 | ||
1260 | wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data", | |
1261 | buf, key_data_len); | |
1262 | if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES || | |
1263 | version == WPA_KEY_INFO_TYPE_AES_128_CMAC) { | |
1264 | if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf, | |
1265 | (u8 *) (key + 1))) { | |
1266 | os_free(hdr); | |
1267 | os_free(buf); | |
1268 | return; | |
1269 | } | |
1270 | WPA_PUT_BE16(key->key_data_length, key_data_len); | |
1271 | } else { | |
1272 | u8 ek[32]; | |
1273 | os_memcpy(key->key_iv, | |
1274 | sm->group->Counter + WPA_NONCE_LEN - 16, 16); | |
1275 | inc_byte_array(sm->group->Counter, WPA_NONCE_LEN); | |
1276 | os_memcpy(ek, key->key_iv, 16); | |
1277 | os_memcpy(ek + 16, sm->PTK.kek, 16); | |
1278 | os_memcpy(key + 1, buf, key_data_len); | |
1279 | rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len); | |
1280 | WPA_PUT_BE16(key->key_data_length, key_data_len); | |
1281 | } | |
1282 | os_free(buf); | |
1283 | } | |
1284 | ||
1285 | if (key_info & WPA_KEY_INFO_MIC) { | |
1286 | if (!sm->PTK_valid) { | |
1287 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
1288 | "PTK not valid when sending EAPOL-Key " | |
1289 | "frame"); | |
1290 | os_free(hdr); | |
1291 | return; | |
1292 | } | |
1293 | wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len, | |
1294 | key->key_mic); | |
1295 | } | |
1296 | ||
1297 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx, | |
1298 | 1); | |
1299 | wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len, | |
1300 | sm->pairwise_set); | |
1301 | os_free(hdr); | |
1302 | } | |
1303 | ||
1304 | ||
1305 | static void wpa_send_eapol(struct wpa_authenticator *wpa_auth, | |
1306 | struct wpa_state_machine *sm, int key_info, | |
1307 | const u8 *key_rsc, const u8 *nonce, | |
1308 | const u8 *kde, size_t kde_len, | |
1309 | int keyidx, int encr) | |
1310 | { | |
1311 | int timeout_ms; | |
1312 | int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE; | |
bae61562 | 1313 | int ctr; |
6fc6879b JM |
1314 | |
1315 | if (sm == NULL) | |
1316 | return; | |
1317 | ||
1318 | __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len, | |
1319 | keyidx, encr, 0); | |
1320 | ||
bae61562 | 1321 | ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr; |
e4bf4db9 | 1322 | if (ctr == 1 && wpa_auth->conf.tx_status) |
bae61562 JM |
1323 | timeout_ms = eapol_key_timeout_first; |
1324 | else | |
1325 | timeout_ms = eapol_key_timeout_subseq; | |
e4bf4db9 JM |
1326 | if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC)) |
1327 | sm->pending_1_of_4_timeout = 1; | |
1328 | wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry " | |
1329 | "counter %d)", timeout_ms, ctr); | |
6fc6879b JM |
1330 | eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000, |
1331 | wpa_send_eapol_timeout, wpa_auth, sm); | |
1332 | } | |
1333 | ||
1334 | ||
1335 | static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len) | |
1336 | { | |
1337 | struct ieee802_1x_hdr *hdr; | |
1338 | struct wpa_eapol_key *key; | |
1339 | u16 key_info; | |
1340 | int ret = 0; | |
1341 | u8 mic[16]; | |
1342 | ||
1343 | if (data_len < sizeof(*hdr) + sizeof(*key)) | |
1344 | return -1; | |
1345 | ||
1346 | hdr = (struct ieee802_1x_hdr *) data; | |
1347 | key = (struct wpa_eapol_key *) (hdr + 1); | |
1348 | key_info = WPA_GET_BE16(key->key_info); | |
1349 | os_memcpy(mic, key->key_mic, 16); | |
1350 | os_memset(key->key_mic, 0, 16); | |
1351 | if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK, | |
1352 | data, data_len, key->key_mic) || | |
1353 | os_memcmp(mic, key->key_mic, 16) != 0) | |
1354 | ret = -1; | |
1355 | os_memcpy(key->key_mic, mic, 16); | |
1356 | return ret; | |
1357 | } | |
1358 | ||
1359 | ||
1360 | void wpa_remove_ptk(struct wpa_state_machine *sm) | |
1361 | { | |
1362 | sm->PTK_valid = FALSE; | |
1363 | os_memset(&sm->PTK, 0, sizeof(sm->PTK)); | |
26083221 | 1364 | wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL, 0); |
6fc6879b | 1365 | sm->pairwise_set = FALSE; |
581a8cde | 1366 | eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm); |
6fc6879b JM |
1367 | } |
1368 | ||
1369 | ||
6f9b5d16 | 1370 | int wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event) |
6fc6879b | 1371 | { |
5d22a1d5 JM |
1372 | int remove_ptk = 1; |
1373 | ||
6fc6879b | 1374 | if (sm == NULL) |
6f9b5d16 | 1375 | return -1; |
6fc6879b JM |
1376 | |
1377 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
1378 | "event %d notification", event); | |
1379 | ||
1380 | switch (event) { | |
1381 | case WPA_AUTH: | |
1382 | case WPA_ASSOC: | |
1383 | break; | |
1384 | case WPA_DEAUTH: | |
1385 | case WPA_DISASSOC: | |
1386 | sm->DeauthenticationRequest = TRUE; | |
1387 | break; | |
1388 | case WPA_REAUTH: | |
1389 | case WPA_REAUTH_EAPOL: | |
3ab72b62 JM |
1390 | if (!sm->started) { |
1391 | /* | |
1392 | * When using WPS, we may end up here if the STA | |
1393 | * manages to re-associate without the previous STA | |
1394 | * entry getting removed. Consequently, we need to make | |
1395 | * sure that the WPA state machines gets initialized | |
1396 | * properly at this point. | |
1397 | */ | |
1398 | wpa_printf(MSG_DEBUG, "WPA state machine had not been " | |
1399 | "started - initialize now"); | |
1400 | sm->started = 1; | |
1401 | sm->Init = TRUE; | |
1402 | if (wpa_sm_step(sm) == 1) | |
1403 | return 1; /* should not really happen */ | |
1404 | sm->Init = FALSE; | |
1405 | sm->AuthenticationRequest = TRUE; | |
1406 | break; | |
1407 | } | |
9663596f JM |
1408 | if (sm->GUpdateStationKeys) { |
1409 | /* | |
1410 | * Reauthentication cancels the pending group key | |
1411 | * update for this STA. | |
1412 | */ | |
1413 | sm->group->GKeyDoneStations--; | |
1414 | sm->GUpdateStationKeys = FALSE; | |
1415 | sm->PtkGroupInit = TRUE; | |
1416 | } | |
6fc6879b JM |
1417 | sm->ReAuthenticationRequest = TRUE; |
1418 | break; | |
1419 | case WPA_ASSOC_FT: | |
1420 | #ifdef CONFIG_IEEE80211R | |
81a658d7 JM |
1421 | wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration " |
1422 | "after association"); | |
1423 | wpa_ft_install_ptk(sm); | |
1424 | ||
6fc6879b JM |
1425 | /* Using FT protocol, not WPA auth state machine */ |
1426 | sm->ft_completed = 1; | |
6f9b5d16 | 1427 | return 0; |
6fc6879b JM |
1428 | #else /* CONFIG_IEEE80211R */ |
1429 | break; | |
1430 | #endif /* CONFIG_IEEE80211R */ | |
1431 | } | |
1432 | ||
1433 | #ifdef CONFIG_IEEE80211R | |
1434 | sm->ft_completed = 0; | |
1435 | #endif /* CONFIG_IEEE80211R */ | |
1436 | ||
5d22a1d5 JM |
1437 | #ifdef CONFIG_IEEE80211W |
1438 | if (sm->mgmt_frame_prot && event == WPA_AUTH) | |
1439 | remove_ptk = 0; | |
1440 | #endif /* CONFIG_IEEE80211W */ | |
6fc6879b | 1441 | |
5d22a1d5 JM |
1442 | if (remove_ptk) { |
1443 | sm->PTK_valid = FALSE; | |
1444 | os_memset(&sm->PTK, 0, sizeof(sm->PTK)); | |
1445 | ||
1446 | if (event != WPA_REAUTH_EAPOL) | |
1447 | wpa_remove_ptk(sm); | |
1448 | } | |
6fc6879b | 1449 | |
6f9b5d16 | 1450 | return wpa_sm_step(sm); |
6fc6879b JM |
1451 | } |
1452 | ||
1453 | ||
71934751 | 1454 | static enum wpa_alg wpa_alg_enum(int alg) |
6fc6879b JM |
1455 | { |
1456 | switch (alg) { | |
1457 | case WPA_CIPHER_CCMP: | |
89d39d9d | 1458 | return WPA_ALG_CCMP; |
6fc6879b | 1459 | case WPA_CIPHER_TKIP: |
89d39d9d | 1460 | return WPA_ALG_TKIP; |
6fc6879b JM |
1461 | case WPA_CIPHER_WEP104: |
1462 | case WPA_CIPHER_WEP40: | |
89d39d9d | 1463 | return WPA_ALG_WEP; |
6fc6879b | 1464 | default: |
89d39d9d | 1465 | return WPA_ALG_NONE; |
6fc6879b JM |
1466 | } |
1467 | } | |
1468 | ||
1469 | ||
1470 | SM_STATE(WPA_PTK, INITIALIZE) | |
1471 | { | |
1472 | SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk); | |
1473 | if (sm->Init) { | |
1474 | /* Init flag is not cleared here, so avoid busy | |
1475 | * loop by claiming nothing changed. */ | |
1476 | sm->changed = FALSE; | |
1477 | } | |
1478 | ||
1479 | sm->keycount = 0; | |
1480 | if (sm->GUpdateStationKeys) | |
1481 | sm->group->GKeyDoneStations--; | |
1482 | sm->GUpdateStationKeys = FALSE; | |
1483 | if (sm->wpa == WPA_VERSION_WPA) | |
1484 | sm->PInitAKeys = FALSE; | |
1485 | if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and | |
1486 | * Local AA > Remote AA)) */) { | |
1487 | sm->Pair = TRUE; | |
1488 | } | |
1489 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0); | |
1490 | wpa_remove_ptk(sm); | |
1491 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0); | |
1492 | sm->TimeoutCtr = 0; | |
56586197 | 1493 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) { |
6fc6879b JM |
1494 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, |
1495 | WPA_EAPOL_authorized, 0); | |
1496 | } | |
1497 | } | |
1498 | ||
1499 | ||
1500 | SM_STATE(WPA_PTK, DISCONNECT) | |
1501 | { | |
1502 | SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk); | |
1503 | sm->Disconnect = FALSE; | |
1504 | wpa_sta_disconnect(sm->wpa_auth, sm->addr); | |
1505 | } | |
1506 | ||
1507 | ||
1508 | SM_STATE(WPA_PTK, DISCONNECTED) | |
1509 | { | |
1510 | SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk); | |
1511 | sm->DeauthenticationRequest = FALSE; | |
1512 | } | |
1513 | ||
1514 | ||
1515 | SM_STATE(WPA_PTK, AUTHENTICATION) | |
1516 | { | |
1517 | SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk); | |
1518 | os_memset(&sm->PTK, 0, sizeof(sm->PTK)); | |
1519 | sm->PTK_valid = FALSE; | |
1520 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto, | |
1521 | 1); | |
1522 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1); | |
1523 | sm->AuthenticationRequest = FALSE; | |
1524 | } | |
1525 | ||
1526 | ||
1bdb7ab3 JM |
1527 | static void wpa_group_first_station(struct wpa_authenticator *wpa_auth, |
1528 | struct wpa_group *group) | |
1529 | { | |
1530 | /* | |
1531 | * System has run bit further than at the time hostapd was started | |
1532 | * potentially very early during boot up. This provides better chances | |
1533 | * of collecting more randomness on embedded systems. Re-initialize the | |
1534 | * GMK and Counter here to improve their strength if there was not | |
1535 | * enough entropy available immediately after system startup. | |
1536 | */ | |
1537 | wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first " | |
1538 | "station"); | |
08704cd8 JM |
1539 | if (random_pool_ready() != 1) { |
1540 | wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool " | |
1541 | "to proceed - reject first 4-way handshake"); | |
1542 | group->reject_4way_hs_for_entropy = TRUE; | |
1543 | } | |
1bdb7ab3 JM |
1544 | wpa_group_init_gmk_and_counter(wpa_auth, group); |
1545 | wpa_gtk_update(wpa_auth, group); | |
1546 | wpa_group_config_group_keys(wpa_auth, group); | |
1547 | } | |
1548 | ||
1549 | ||
6fc6879b JM |
1550 | SM_STATE(WPA_PTK, AUTHENTICATION2) |
1551 | { | |
1552 | SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk); | |
1bdb7ab3 JM |
1553 | |
1554 | if (!sm->group->first_sta_seen) { | |
1555 | wpa_group_first_station(sm->wpa_auth, sm->group); | |
1556 | sm->group->first_sta_seen = TRUE; | |
1557 | } | |
1558 | ||
6fc6879b | 1559 | os_memcpy(sm->ANonce, sm->group->Counter, WPA_NONCE_LEN); |
bc8318ac JM |
1560 | wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce, |
1561 | WPA_NONCE_LEN); | |
6fc6879b JM |
1562 | inc_byte_array(sm->group->Counter, WPA_NONCE_LEN); |
1563 | sm->ReAuthenticationRequest = FALSE; | |
1564 | /* IEEE 802.11i does not clear TimeoutCtr here, but this is more | |
1565 | * logical place than INITIALIZE since AUTHENTICATION2 can be | |
1566 | * re-entered on ReAuthenticationRequest without going through | |
1567 | * INITIALIZE. */ | |
1568 | sm->TimeoutCtr = 0; | |
1569 | } | |
1570 | ||
1571 | ||
1572 | SM_STATE(WPA_PTK, INITPMK) | |
1573 | { | |
1574 | u8 msk[2 * PMK_LEN]; | |
1575 | size_t len = 2 * PMK_LEN; | |
1576 | ||
1577 | SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk); | |
1578 | #ifdef CONFIG_IEEE80211R | |
1579 | sm->xxkey_len = 0; | |
1580 | #endif /* CONFIG_IEEE80211R */ | |
1581 | if (sm->pmksa) { | |
1582 | wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache"); | |
1583 | os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN); | |
1584 | } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) { | |
1585 | wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine " | |
1586 | "(len=%lu)", (unsigned long) len); | |
1587 | os_memcpy(sm->PMK, msk, PMK_LEN); | |
1588 | #ifdef CONFIG_IEEE80211R | |
1589 | if (len >= 2 * PMK_LEN) { | |
1590 | os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN); | |
1591 | sm->xxkey_len = PMK_LEN; | |
1592 | } | |
1593 | #endif /* CONFIG_IEEE80211R */ | |
1594 | } else { | |
1595 | wpa_printf(MSG_DEBUG, "WPA: Could not get PMK"); | |
1596 | } | |
1597 | ||
1598 | sm->req_replay_counter_used = 0; | |
1599 | /* IEEE 802.11i does not set keyRun to FALSE, but not doing this | |
1600 | * will break reauthentication since EAPOL state machines may not be | |
1601 | * get into AUTHENTICATING state that clears keyRun before WPA state | |
1602 | * machine enters AUTHENTICATION2 state and goes immediately to INITPMK | |
1603 | * state and takes PMK from the previously used AAA Key. This will | |
1604 | * eventually fail in 4-Way Handshake because Supplicant uses PMK | |
1605 | * derived from the new AAA Key. Setting keyRun = FALSE here seems to | |
1606 | * be good workaround for this issue. */ | |
1607 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0); | |
1608 | } | |
1609 | ||
1610 | ||
1611 | SM_STATE(WPA_PTK, INITPSK) | |
1612 | { | |
1613 | const u8 *psk; | |
1614 | SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk); | |
1615 | psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL); | |
1616 | if (psk) { | |
1617 | os_memcpy(sm->PMK, psk, PMK_LEN); | |
1618 | #ifdef CONFIG_IEEE80211R | |
1619 | os_memcpy(sm->xxkey, psk, PMK_LEN); | |
1620 | sm->xxkey_len = PMK_LEN; | |
1621 | #endif /* CONFIG_IEEE80211R */ | |
1622 | } | |
1623 | sm->req_replay_counter_used = 0; | |
1624 | } | |
1625 | ||
1626 | ||
1627 | SM_STATE(WPA_PTK, PTKSTART) | |
1628 | { | |
1629 | u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL; | |
1630 | size_t pmkid_len = 0; | |
1631 | ||
1632 | SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk); | |
1633 | sm->PTKRequest = FALSE; | |
1634 | sm->TimeoutEvt = FALSE; | |
bae61562 JM |
1635 | |
1636 | sm->TimeoutCtr++; | |
1637 | if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) { | |
1638 | /* No point in sending the EAPOL-Key - we will disconnect | |
1639 | * immediately following this. */ | |
1640 | return; | |
1641 | } | |
1642 | ||
6fc6879b JM |
1643 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
1644 | "sending 1/4 msg of 4-Way Handshake"); | |
1645 | /* | |
1646 | * TODO: Could add PMKID even with WPA2-PSK, but only if there is only | |
1647 | * one possible PSK for this STA. | |
1648 | */ | |
1649 | if (sm->wpa == WPA_VERSION_WPA2 && | |
56586197 | 1650 | wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) { |
6fc6879b JM |
1651 | pmkid = buf; |
1652 | pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN; | |
1653 | pmkid[0] = WLAN_EID_VENDOR_SPECIFIC; | |
1654 | pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN; | |
1655 | RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID); | |
1656 | if (sm->pmksa) | |
1657 | os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN], | |
1658 | sm->pmksa->pmkid, PMKID_LEN); | |
1659 | else { | |
1660 | /* | |
1661 | * Calculate PMKID since no PMKSA cache entry was | |
1662 | * available with pre-calculated PMKID. | |
1663 | */ | |
1664 | rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr, | |
56586197 JM |
1665 | sm->addr, &pmkid[2 + RSN_SELECTOR_LEN], |
1666 | wpa_key_mgmt_sha256(sm->wpa_key_mgmt)); | |
6fc6879b JM |
1667 | } |
1668 | } | |
1669 | wpa_send_eapol(sm->wpa_auth, sm, | |
1670 | WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL, | |
1671 | sm->ANonce, pmkid, pmkid_len, 0, 0); | |
6fc6879b JM |
1672 | } |
1673 | ||
1674 | ||
1675 | static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk, | |
1676 | struct wpa_ptk *ptk) | |
1677 | { | |
c0a61908 | 1678 | size_t ptk_len = sm->pairwise == WPA_CIPHER_CCMP ? 48 : 64; |
6fc6879b | 1679 | #ifdef CONFIG_IEEE80211R |
56586197 | 1680 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) |
c0a61908 | 1681 | return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len); |
6fc6879b JM |
1682 | #endif /* CONFIG_IEEE80211R */ |
1683 | ||
1684 | wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion", | |
1685 | sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce, | |
c0a61908 | 1686 | (u8 *) ptk, ptk_len, |
56586197 | 1687 | wpa_key_mgmt_sha256(sm->wpa_key_mgmt)); |
6fc6879b JM |
1688 | |
1689 | return 0; | |
1690 | } | |
1691 | ||
1692 | ||
1693 | SM_STATE(WPA_PTK, PTKCALCNEGOTIATING) | |
1694 | { | |
1695 | struct wpa_ptk PTK; | |
1696 | int ok = 0; | |
1697 | const u8 *pmk = NULL; | |
1698 | ||
1699 | SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk); | |
1700 | sm->EAPOLKeyReceived = FALSE; | |
1701 | ||
1702 | /* WPA with IEEE 802.1X: use the derived PMK from EAP | |
1703 | * WPA-PSK: iterate through possible PSKs and select the one matching | |
1704 | * the packet */ | |
1705 | for (;;) { | |
56586197 | 1706 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) { |
6fc6879b JM |
1707 | pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, pmk); |
1708 | if (pmk == NULL) | |
1709 | break; | |
1710 | } else | |
1711 | pmk = sm->PMK; | |
1712 | ||
1713 | wpa_derive_ptk(sm, pmk, &PTK); | |
1714 | ||
1715 | if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key, | |
1716 | sm->last_rx_eapol_key_len) == 0) { | |
1717 | ok = 1; | |
1718 | break; | |
1719 | } | |
1720 | ||
56586197 | 1721 | if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) |
6fc6879b JM |
1722 | break; |
1723 | } | |
1724 | ||
1725 | if (!ok) { | |
1726 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
1727 | "invalid MIC in msg 2/4 of 4-Way Handshake"); | |
1728 | return; | |
1729 | } | |
1730 | ||
26e23750 JM |
1731 | #ifdef CONFIG_IEEE80211R |
1732 | if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { | |
1733 | /* | |
1734 | * Verify that PMKR1Name from EAPOL-Key message 2/4 matches | |
1735 | * with the value we derived. | |
1736 | */ | |
1737 | if (os_memcmp(sm->sup_pmk_r1_name, sm->pmk_r1_name, | |
1738 | WPA_PMK_NAME_LEN) != 0) { | |
1739 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
1740 | "PMKR1Name mismatch in FT 4-way " | |
1741 | "handshake"); | |
1742 | wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from " | |
1743 | "Supplicant", | |
1744 | sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN); | |
1745 | wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name", | |
1746 | sm->pmk_r1_name, WPA_PMK_NAME_LEN); | |
1747 | return; | |
1748 | } | |
1749 | } | |
1750 | #endif /* CONFIG_IEEE80211R */ | |
1751 | ||
e4bf4db9 | 1752 | sm->pending_1_of_4_timeout = 0; |
6fc6879b JM |
1753 | eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm); |
1754 | ||
56586197 | 1755 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) { |
6fc6879b JM |
1756 | /* PSK may have changed from the previous choice, so update |
1757 | * state machine data based on whatever PSK was selected here. | |
1758 | */ | |
1759 | os_memcpy(sm->PMK, pmk, PMK_LEN); | |
1760 | } | |
1761 | ||
1762 | sm->MICVerified = TRUE; | |
1763 | ||
1764 | os_memcpy(&sm->PTK, &PTK, sizeof(PTK)); | |
1765 | sm->PTK_valid = TRUE; | |
1766 | } | |
1767 | ||
1768 | ||
1769 | SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2) | |
1770 | { | |
1771 | SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk); | |
1772 | sm->TimeoutCtr = 0; | |
1773 | } | |
1774 | ||
1775 | ||
1776 | #ifdef CONFIG_IEEE80211W | |
1777 | ||
1778 | static int ieee80211w_kde_len(struct wpa_state_machine *sm) | |
1779 | { | |
1780 | if (sm->mgmt_frame_prot) { | |
1781 | return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde); | |
1782 | } | |
1783 | ||
1784 | return 0; | |
1785 | } | |
1786 | ||
1787 | ||
1788 | static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos) | |
1789 | { | |
1790 | struct wpa_igtk_kde igtk; | |
1791 | struct wpa_group *gsm = sm->group; | |
1792 | ||
1793 | if (!sm->mgmt_frame_prot) | |
1794 | return pos; | |
1795 | ||
1796 | igtk.keyid[0] = gsm->GN_igtk; | |
1797 | igtk.keyid[1] = 0; | |
7b1080da JM |
1798 | if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE || |
1799 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, igtk.pn) < 0) | |
6fc6879b JM |
1800 | os_memset(igtk.pn, 0, sizeof(igtk.pn)); |
1801 | os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN); | |
1802 | pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK, | |
1803 | (const u8 *) &igtk, sizeof(igtk), NULL, 0); | |
1804 | ||
1805 | return pos; | |
1806 | } | |
1807 | ||
1808 | #else /* CONFIG_IEEE80211W */ | |
1809 | ||
1810 | static int ieee80211w_kde_len(struct wpa_state_machine *sm) | |
1811 | { | |
1812 | return 0; | |
1813 | } | |
1814 | ||
1815 | ||
1816 | static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos) | |
1817 | { | |
1818 | return pos; | |
1819 | } | |
1820 | ||
1821 | #endif /* CONFIG_IEEE80211W */ | |
1822 | ||
1823 | ||
1824 | SM_STATE(WPA_PTK, PTKINITNEGOTIATING) | |
1825 | { | |
1826 | u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos; | |
1827 | size_t gtk_len, kde_len; | |
1828 | struct wpa_group *gsm = sm->group; | |
1829 | u8 *wpa_ie; | |
1830 | int wpa_ie_len, secure, keyidx, encr = 0; | |
1831 | ||
1832 | SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk); | |
1833 | sm->TimeoutEvt = FALSE; | |
bae61562 JM |
1834 | |
1835 | sm->TimeoutCtr++; | |
1836 | if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) { | |
1837 | /* No point in sending the EAPOL-Key - we will disconnect | |
1838 | * immediately following this. */ | |
1839 | return; | |
1840 | } | |
1841 | ||
86dfabb8 JM |
1842 | /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE], |
1843 | GTK[GN], IGTK, [FTIE], [TIE * 2]) | |
6fc6879b JM |
1844 | */ |
1845 | os_memset(rsc, 0, WPA_KEY_RSC_LEN); | |
1846 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc); | |
86dfabb8 | 1847 | /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */ |
6fc6879b JM |
1848 | wpa_ie = sm->wpa_auth->wpa_ie; |
1849 | wpa_ie_len = sm->wpa_auth->wpa_ie_len; | |
1850 | if (sm->wpa == WPA_VERSION_WPA && | |
1851 | (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) && | |
1852 | wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) { | |
1853 | /* WPA-only STA, remove RSN IE */ | |
1854 | wpa_ie = wpa_ie + wpa_ie[1] + 2; | |
1855 | wpa_ie_len = wpa_ie[1] + 2; | |
1856 | } | |
1857 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
1858 | "sending 3/4 msg of 4-Way Handshake"); | |
1859 | if (sm->wpa == WPA_VERSION_WPA2) { | |
1860 | /* WPA2 send GTK in the 4-way handshake */ | |
1861 | secure = 1; | |
1862 | gtk = gsm->GTK[gsm->GN - 1]; | |
1863 | gtk_len = gsm->GTK_len; | |
1864 | keyidx = gsm->GN; | |
1865 | _rsc = rsc; | |
1866 | encr = 1; | |
1867 | } else { | |
1868 | /* WPA does not include GTK in msg 3/4 */ | |
1869 | secure = 0; | |
1870 | gtk = NULL; | |
1871 | gtk_len = 0; | |
1872 | keyidx = 0; | |
1873 | _rsc = NULL; | |
1874 | } | |
1875 | ||
1876 | kde_len = wpa_ie_len + ieee80211w_kde_len(sm); | |
1877 | if (gtk) | |
1878 | kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len; | |
26e23750 | 1879 | #ifdef CONFIG_IEEE80211R |
86dfabb8 JM |
1880 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
1881 | kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */ | |
1882 | kde_len += 300; /* FTIE + 2 * TIE */ | |
1883 | } | |
26e23750 | 1884 | #endif /* CONFIG_IEEE80211R */ |
6fc6879b JM |
1885 | kde = os_malloc(kde_len); |
1886 | if (kde == NULL) | |
1887 | return; | |
1888 | ||
1889 | pos = kde; | |
1890 | os_memcpy(pos, wpa_ie, wpa_ie_len); | |
1891 | pos += wpa_ie_len; | |
26e23750 JM |
1892 | #ifdef CONFIG_IEEE80211R |
1893 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { | |
1894 | int res = wpa_insert_pmkid(kde, pos - kde, sm->pmk_r1_name); | |
1895 | if (res < 0) { | |
1896 | wpa_printf(MSG_ERROR, "FT: Failed to insert " | |
1897 | "PMKR1Name into RSN IE in EAPOL-Key data"); | |
1898 | os_free(kde); | |
1899 | return; | |
1900 | } | |
1901 | pos += res; | |
1902 | } | |
1903 | #endif /* CONFIG_IEEE80211R */ | |
6fc6879b JM |
1904 | if (gtk) { |
1905 | u8 hdr[2]; | |
1906 | hdr[0] = keyidx & 0x03; | |
1907 | hdr[1] = 0; | |
1908 | pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2, | |
1909 | gtk, gtk_len); | |
1910 | } | |
1911 | pos = ieee80211w_kde_add(sm, pos); | |
1912 | ||
86dfabb8 JM |
1913 | #ifdef CONFIG_IEEE80211R |
1914 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { | |
1915 | int res; | |
1916 | struct wpa_auth_config *conf; | |
1917 | ||
1918 | conf = &sm->wpa_auth->conf; | |
1919 | res = wpa_write_ftie(conf, conf->r0_key_holder, | |
1920 | conf->r0_key_holder_len, | |
1921 | NULL, NULL, pos, kde + kde_len - pos, | |
1922 | NULL, 0); | |
1923 | if (res < 0) { | |
1924 | wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE " | |
1925 | "into EAPOL-Key Key Data"); | |
1926 | os_free(kde); | |
1927 | return; | |
1928 | } | |
1929 | pos += res; | |
1930 | ||
1931 | /* TIE[ReassociationDeadline] (TU) */ | |
1932 | *pos++ = WLAN_EID_TIMEOUT_INTERVAL; | |
1933 | *pos++ = 5; | |
1934 | *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE; | |
1935 | WPA_PUT_LE32(pos, conf->reassociation_deadline); | |
1936 | pos += 4; | |
1937 | ||
1938 | /* TIE[KeyLifetime] (seconds) */ | |
1939 | *pos++ = WLAN_EID_TIMEOUT_INTERVAL; | |
1940 | *pos++ = 5; | |
1941 | *pos++ = WLAN_TIMEOUT_KEY_LIFETIME; | |
1942 | WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60); | |
1943 | pos += 4; | |
1944 | } | |
1945 | #endif /* CONFIG_IEEE80211R */ | |
1946 | ||
6fc6879b JM |
1947 | wpa_send_eapol(sm->wpa_auth, sm, |
1948 | (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC | | |
1949 | WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL | | |
1950 | WPA_KEY_INFO_KEY_TYPE, | |
1951 | _rsc, sm->ANonce, kde, pos - kde, keyidx, encr); | |
1952 | os_free(kde); | |
6fc6879b JM |
1953 | } |
1954 | ||
1955 | ||
1956 | SM_STATE(WPA_PTK, PTKINITDONE) | |
1957 | { | |
1958 | SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk); | |
1959 | sm->EAPOLKeyReceived = FALSE; | |
1960 | if (sm->Pair) { | |
71934751 | 1961 | enum wpa_alg alg; |
6fc6879b JM |
1962 | int klen; |
1963 | if (sm->pairwise == WPA_CIPHER_TKIP) { | |
89d39d9d | 1964 | alg = WPA_ALG_TKIP; |
6fc6879b JM |
1965 | klen = 32; |
1966 | } else { | |
89d39d9d | 1967 | alg = WPA_ALG_CCMP; |
6fc6879b JM |
1968 | klen = 16; |
1969 | } | |
1970 | if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0, | |
1971 | sm->PTK.tk1, klen)) { | |
1972 | wpa_sta_disconnect(sm->wpa_auth, sm->addr); | |
1973 | return; | |
1974 | } | |
1975 | /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */ | |
1976 | sm->pairwise_set = TRUE; | |
1977 | ||
581a8cde JM |
1978 | if (sm->wpa_auth->conf.wpa_ptk_rekey) { |
1979 | eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm); | |
1980 | eloop_register_timeout(sm->wpa_auth->conf. | |
1981 | wpa_ptk_rekey, 0, wpa_rekey_ptk, | |
1982 | sm->wpa_auth, sm); | |
1983 | } | |
1984 | ||
56586197 | 1985 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) { |
6fc6879b JM |
1986 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, |
1987 | WPA_EAPOL_authorized, 1); | |
1988 | } | |
1989 | } | |
1990 | ||
1991 | if (0 /* IBSS == TRUE */) { | |
1992 | sm->keycount++; | |
1993 | if (sm->keycount == 2) { | |
1994 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, | |
1995 | WPA_EAPOL_portValid, 1); | |
1996 | } | |
1997 | } else { | |
1998 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, | |
1999 | 1); | |
2000 | } | |
2001 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0); | |
2002 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1); | |
2003 | if (sm->wpa == WPA_VERSION_WPA) | |
2004 | sm->PInitAKeys = TRUE; | |
2005 | else | |
2006 | sm->has_GTK = TRUE; | |
2007 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO, | |
2008 | "pairwise key handshake completed (%s)", | |
2009 | sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN"); | |
2010 | ||
2011 | #ifdef CONFIG_IEEE80211R | |
2012 | wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr); | |
2013 | #endif /* CONFIG_IEEE80211R */ | |
2014 | } | |
2015 | ||
2016 | ||
2017 | SM_STEP(WPA_PTK) | |
2018 | { | |
2019 | struct wpa_authenticator *wpa_auth = sm->wpa_auth; | |
2020 | ||
2021 | if (sm->Init) | |
2022 | SM_ENTER(WPA_PTK, INITIALIZE); | |
2023 | else if (sm->Disconnect | |
1aae01fc BG |
2024 | /* || FIX: dot11RSNAConfigSALifetime timeout */) { |
2025 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
2026 | "WPA_PTK: sm->Disconnect"); | |
6fc6879b | 2027 | SM_ENTER(WPA_PTK, DISCONNECT); |
1aae01fc | 2028 | } |
6fc6879b JM |
2029 | else if (sm->DeauthenticationRequest) |
2030 | SM_ENTER(WPA_PTK, DISCONNECTED); | |
2031 | else if (sm->AuthenticationRequest) | |
2032 | SM_ENTER(WPA_PTK, AUTHENTICATION); | |
2033 | else if (sm->ReAuthenticationRequest) | |
2034 | SM_ENTER(WPA_PTK, AUTHENTICATION2); | |
2035 | else if (sm->PTKRequest) | |
2036 | SM_ENTER(WPA_PTK, PTKSTART); | |
2037 | else switch (sm->wpa_ptk_state) { | |
2038 | case WPA_PTK_INITIALIZE: | |
2039 | break; | |
2040 | case WPA_PTK_DISCONNECT: | |
2041 | SM_ENTER(WPA_PTK, DISCONNECTED); | |
2042 | break; | |
2043 | case WPA_PTK_DISCONNECTED: | |
2044 | SM_ENTER(WPA_PTK, INITIALIZE); | |
2045 | break; | |
2046 | case WPA_PTK_AUTHENTICATION: | |
2047 | SM_ENTER(WPA_PTK, AUTHENTICATION2); | |
2048 | break; | |
2049 | case WPA_PTK_AUTHENTICATION2: | |
56586197 | 2050 | if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) && |
6fc6879b JM |
2051 | wpa_auth_get_eapol(sm->wpa_auth, sm->addr, |
2052 | WPA_EAPOL_keyRun) > 0) | |
2053 | SM_ENTER(WPA_PTK, INITPMK); | |
56586197 | 2054 | else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) |
6fc6879b JM |
2055 | /* FIX: && 802.1X::keyRun */) |
2056 | SM_ENTER(WPA_PTK, INITPSK); | |
2057 | break; | |
2058 | case WPA_PTK_INITPMK: | |
2059 | if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr, | |
2060 | WPA_EAPOL_keyAvailable) > 0) | |
2061 | SM_ENTER(WPA_PTK, PTKSTART); | |
2062 | else { | |
2063 | wpa_auth->dot11RSNA4WayHandshakeFailures++; | |
1aae01fc BG |
2064 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO, |
2065 | "INITPMK - keyAvailable = false"); | |
6fc6879b JM |
2066 | SM_ENTER(WPA_PTK, DISCONNECT); |
2067 | } | |
2068 | break; | |
2069 | case WPA_PTK_INITPSK: | |
2070 | if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL)) | |
2071 | SM_ENTER(WPA_PTK, PTKSTART); | |
2072 | else { | |
2073 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO, | |
2074 | "no PSK configured for the STA"); | |
2075 | wpa_auth->dot11RSNA4WayHandshakeFailures++; | |
2076 | SM_ENTER(WPA_PTK, DISCONNECT); | |
2077 | } | |
2078 | break; | |
2079 | case WPA_PTK_PTKSTART: | |
2080 | if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && | |
2081 | sm->EAPOLKeyPairwise) | |
2082 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING); | |
2083 | else if (sm->TimeoutCtr > | |
2084 | (int) dot11RSNAConfigPairwiseUpdateCount) { | |
2085 | wpa_auth->dot11RSNA4WayHandshakeFailures++; | |
1aae01fc BG |
2086 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
2087 | "PTKSTART: Retry limit %d reached", | |
2088 | dot11RSNAConfigPairwiseUpdateCount); | |
6fc6879b JM |
2089 | SM_ENTER(WPA_PTK, DISCONNECT); |
2090 | } else if (sm->TimeoutEvt) | |
2091 | SM_ENTER(WPA_PTK, PTKSTART); | |
2092 | break; | |
2093 | case WPA_PTK_PTKCALCNEGOTIATING: | |
2094 | if (sm->MICVerified) | |
2095 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2); | |
2096 | else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && | |
2097 | sm->EAPOLKeyPairwise) | |
2098 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING); | |
2099 | else if (sm->TimeoutEvt) | |
2100 | SM_ENTER(WPA_PTK, PTKSTART); | |
2101 | break; | |
2102 | case WPA_PTK_PTKCALCNEGOTIATING2: | |
2103 | SM_ENTER(WPA_PTK, PTKINITNEGOTIATING); | |
2104 | break; | |
2105 | case WPA_PTK_PTKINITNEGOTIATING: | |
2106 | if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && | |
2107 | sm->EAPOLKeyPairwise && sm->MICVerified) | |
2108 | SM_ENTER(WPA_PTK, PTKINITDONE); | |
2109 | else if (sm->TimeoutCtr > | |
2110 | (int) dot11RSNAConfigPairwiseUpdateCount) { | |
2111 | wpa_auth->dot11RSNA4WayHandshakeFailures++; | |
1aae01fc BG |
2112 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
2113 | "PTKINITNEGOTIATING: Retry limit %d " | |
2114 | "reached", | |
2115 | dot11RSNAConfigPairwiseUpdateCount); | |
6fc6879b JM |
2116 | SM_ENTER(WPA_PTK, DISCONNECT); |
2117 | } else if (sm->TimeoutEvt) | |
2118 | SM_ENTER(WPA_PTK, PTKINITNEGOTIATING); | |
2119 | break; | |
2120 | case WPA_PTK_PTKINITDONE: | |
2121 | break; | |
2122 | } | |
2123 | } | |
2124 | ||
2125 | ||
2126 | SM_STATE(WPA_PTK_GROUP, IDLE) | |
2127 | { | |
2128 | SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group); | |
2129 | if (sm->Init) { | |
2130 | /* Init flag is not cleared here, so avoid busy | |
2131 | * loop by claiming nothing changed. */ | |
2132 | sm->changed = FALSE; | |
2133 | } | |
2134 | sm->GTimeoutCtr = 0; | |
2135 | } | |
2136 | ||
2137 | ||
2138 | SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING) | |
2139 | { | |
2140 | u8 rsc[WPA_KEY_RSC_LEN]; | |
2141 | struct wpa_group *gsm = sm->group; | |
2142 | u8 *kde, *pos, hdr[2]; | |
2143 | size_t kde_len; | |
2144 | ||
2145 | SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group); | |
bae61562 JM |
2146 | |
2147 | sm->GTimeoutCtr++; | |
2148 | if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) { | |
2149 | /* No point in sending the EAPOL-Key - we will disconnect | |
2150 | * immediately following this. */ | |
2151 | return; | |
2152 | } | |
2153 | ||
6fc6879b JM |
2154 | if (sm->wpa == WPA_VERSION_WPA) |
2155 | sm->PInitAKeys = FALSE; | |
2156 | sm->TimeoutEvt = FALSE; | |
2157 | /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */ | |
2158 | os_memset(rsc, 0, WPA_KEY_RSC_LEN); | |
2159 | if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE) | |
2160 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc); | |
2161 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
2162 | "sending 1/2 msg of Group Key Handshake"); | |
2163 | ||
2164 | if (sm->wpa == WPA_VERSION_WPA2) { | |
2165 | kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len + | |
2166 | ieee80211w_kde_len(sm); | |
2167 | kde = os_malloc(kde_len); | |
2168 | if (kde == NULL) | |
2169 | return; | |
2170 | ||
2171 | pos = kde; | |
2172 | hdr[0] = gsm->GN & 0x03; | |
2173 | hdr[1] = 0; | |
2174 | pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2, | |
2175 | gsm->GTK[gsm->GN - 1], gsm->GTK_len); | |
2176 | pos = ieee80211w_kde_add(sm, pos); | |
2177 | } else { | |
2178 | kde = gsm->GTK[gsm->GN - 1]; | |
2179 | pos = kde + gsm->GTK_len; | |
2180 | } | |
2181 | ||
2182 | wpa_send_eapol(sm->wpa_auth, sm, | |
2183 | WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC | | |
2184 | WPA_KEY_INFO_ACK | | |
2185 | (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0), | |
2186 | rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1); | |
2187 | if (sm->wpa == WPA_VERSION_WPA2) | |
2188 | os_free(kde); | |
6fc6879b JM |
2189 | } |
2190 | ||
2191 | ||
2192 | SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED) | |
2193 | { | |
2194 | SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group); | |
2195 | sm->EAPOLKeyReceived = FALSE; | |
2196 | if (sm->GUpdateStationKeys) | |
2197 | sm->group->GKeyDoneStations--; | |
2198 | sm->GUpdateStationKeys = FALSE; | |
2199 | sm->GTimeoutCtr = 0; | |
2200 | /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */ | |
2201 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO, | |
2202 | "group key handshake completed (%s)", | |
2203 | sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN"); | |
2204 | sm->has_GTK = TRUE; | |
2205 | } | |
2206 | ||
2207 | ||
2208 | SM_STATE(WPA_PTK_GROUP, KEYERROR) | |
2209 | { | |
2210 | SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group); | |
2211 | if (sm->GUpdateStationKeys) | |
2212 | sm->group->GKeyDoneStations--; | |
2213 | sm->GUpdateStationKeys = FALSE; | |
2214 | sm->Disconnect = TRUE; | |
2215 | } | |
2216 | ||
2217 | ||
2218 | SM_STEP(WPA_PTK_GROUP) | |
2219 | { | |
9663596f | 2220 | if (sm->Init || sm->PtkGroupInit) { |
6fc6879b | 2221 | SM_ENTER(WPA_PTK_GROUP, IDLE); |
9663596f JM |
2222 | sm->PtkGroupInit = FALSE; |
2223 | } else switch (sm->wpa_ptk_group_state) { | |
6fc6879b JM |
2224 | case WPA_PTK_GROUP_IDLE: |
2225 | if (sm->GUpdateStationKeys || | |
2226 | (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys)) | |
2227 | SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING); | |
2228 | break; | |
2229 | case WPA_PTK_GROUP_REKEYNEGOTIATING: | |
2230 | if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && | |
2231 | !sm->EAPOLKeyPairwise && sm->MICVerified) | |
2232 | SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED); | |
2233 | else if (sm->GTimeoutCtr > | |
2234 | (int) dot11RSNAConfigGroupUpdateCount) | |
2235 | SM_ENTER(WPA_PTK_GROUP, KEYERROR); | |
2236 | else if (sm->TimeoutEvt) | |
2237 | SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING); | |
2238 | break; | |
2239 | case WPA_PTK_GROUP_KEYERROR: | |
2240 | SM_ENTER(WPA_PTK_GROUP, IDLE); | |
2241 | break; | |
2242 | case WPA_PTK_GROUP_REKEYESTABLISHED: | |
2243 | SM_ENTER(WPA_PTK_GROUP, IDLE); | |
2244 | break; | |
2245 | } | |
2246 | } | |
2247 | ||
2248 | ||
2249 | static int wpa_gtk_update(struct wpa_authenticator *wpa_auth, | |
2250 | struct wpa_group *group) | |
2251 | { | |
2252 | int ret = 0; | |
2253 | ||
6fc6879b JM |
2254 | os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN); |
2255 | inc_byte_array(group->Counter, WPA_NONCE_LEN); | |
3c7302c2 JM |
2256 | if (wpa_gmk_to_gtk(group->GMK, "Group key expansion", |
2257 | wpa_auth->addr, group->GNonce, | |
2258 | group->GTK[group->GN - 1], group->GTK_len) < 0) | |
2259 | ret = -1; | |
5cb9d5c3 JM |
2260 | wpa_hexdump_key(MSG_DEBUG, "GTK", |
2261 | group->GTK[group->GN - 1], group->GTK_len); | |
6fc6879b JM |
2262 | |
2263 | #ifdef CONFIG_IEEE80211W | |
70f8cc8e | 2264 | if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) { |
5cb9d5c3 JM |
2265 | os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN); |
2266 | inc_byte_array(group->Counter, WPA_NONCE_LEN); | |
3c7302c2 JM |
2267 | if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion", |
2268 | wpa_auth->addr, group->GNonce, | |
2269 | group->IGTK[group->GN_igtk - 4], | |
2270 | WPA_IGTK_LEN) < 0) | |
2271 | ret = -1; | |
6fc6879b JM |
2272 | wpa_hexdump_key(MSG_DEBUG, "IGTK", |
2273 | group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN); | |
2274 | } | |
2275 | #endif /* CONFIG_IEEE80211W */ | |
2276 | ||
2277 | return ret; | |
2278 | } | |
2279 | ||
2280 | ||
2281 | static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth, | |
2282 | struct wpa_group *group) | |
2283 | { | |
2284 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state " | |
2285 | "GTK_INIT (VLAN-ID %d)", group->vlan_id); | |
2286 | group->changed = FALSE; /* GInit is not cleared here; avoid loop */ | |
2287 | group->wpa_group_state = WPA_GROUP_GTK_INIT; | |
2288 | ||
2289 | /* GTK[0..N] = 0 */ | |
2290 | os_memset(group->GTK, 0, sizeof(group->GTK)); | |
2291 | group->GN = 1; | |
2292 | group->GM = 2; | |
2293 | #ifdef CONFIG_IEEE80211W | |
2294 | group->GN_igtk = 4; | |
2295 | group->GM_igtk = 5; | |
2296 | #endif /* CONFIG_IEEE80211W */ | |
2297 | /* GTK[GN] = CalcGTK() */ | |
2298 | wpa_gtk_update(wpa_auth, group); | |
2299 | } | |
2300 | ||
2301 | ||
2302 | static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx) | |
2303 | { | |
2304 | if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) { | |
2305 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
2306 | "Not in PTKINITDONE; skip Group Key update"); | |
3c183894 | 2307 | sm->GUpdateStationKeys = FALSE; |
6fc6879b JM |
2308 | return 0; |
2309 | } | |
9663596f JM |
2310 | if (sm->GUpdateStationKeys) { |
2311 | /* | |
3c183894 JM |
2312 | * This should not really happen, so add a debug log entry. |
2313 | * Since we clear the GKeyDoneStations before the loop, the | |
2314 | * station needs to be counted here anyway. | |
9663596f JM |
2315 | */ |
2316 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
3c183894 JM |
2317 | "GUpdateStationKeys was already set when " |
2318 | "marking station for GTK rekeying"); | |
9663596f | 2319 | } |
3c183894 JM |
2320 | |
2321 | sm->group->GKeyDoneStations++; | |
2322 | sm->GUpdateStationKeys = TRUE; | |
2323 | ||
6fc6879b JM |
2324 | wpa_sm_step(sm); |
2325 | return 0; | |
2326 | } | |
2327 | ||
2328 | ||
2329 | static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth, | |
2330 | struct wpa_group *group) | |
2331 | { | |
2332 | int tmp; | |
2333 | ||
2334 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state " | |
2335 | "SETKEYS (VLAN-ID %d)", group->vlan_id); | |
2336 | group->changed = TRUE; | |
2337 | group->wpa_group_state = WPA_GROUP_SETKEYS; | |
2338 | group->GTKReKey = FALSE; | |
2339 | tmp = group->GM; | |
2340 | group->GM = group->GN; | |
2341 | group->GN = tmp; | |
2342 | #ifdef CONFIG_IEEE80211W | |
2343 | tmp = group->GM_igtk; | |
2344 | group->GM_igtk = group->GN_igtk; | |
2345 | group->GN_igtk = tmp; | |
2346 | #endif /* CONFIG_IEEE80211W */ | |
2347 | /* "GKeyDoneStations = GNoStations" is done in more robust way by | |
2348 | * counting the STAs that are marked with GUpdateStationKeys instead of | |
2349 | * including all STAs that could be in not-yet-completed state. */ | |
2350 | wpa_gtk_update(wpa_auth, group); | |
2351 | ||
3c183894 JM |
2352 | if (group->GKeyDoneStations) { |
2353 | wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected " | |
2354 | "GKeyDoneStations=%d when starting new GTK rekey", | |
2355 | group->GKeyDoneStations); | |
2356 | group->GKeyDoneStations = 0; | |
2357 | } | |
6fc6879b JM |
2358 | wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, NULL); |
2359 | wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d", | |
2360 | group->GKeyDoneStations); | |
2361 | } | |
2362 | ||
2363 | ||
1bdb7ab3 JM |
2364 | static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth, |
2365 | struct wpa_group *group) | |
6fc6879b | 2366 | { |
3c7302c2 JM |
2367 | int ret = 0; |
2368 | ||
3c7302c2 JM |
2369 | if (wpa_auth_set_key(wpa_auth, group->vlan_id, |
2370 | wpa_alg_enum(wpa_auth->conf.wpa_group), | |
0382097e JM |
2371 | broadcast_ether_addr, group->GN, |
2372 | group->GTK[group->GN - 1], group->GTK_len) < 0) | |
3c7302c2 | 2373 | ret = -1; |
6fc6879b JM |
2374 | |
2375 | #ifdef CONFIG_IEEE80211W | |
3c7302c2 JM |
2376 | if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION && |
2377 | wpa_auth_set_key(wpa_auth, group->vlan_id, WPA_ALG_IGTK, | |
0382097e | 2378 | broadcast_ether_addr, group->GN_igtk, |
3c7302c2 JM |
2379 | group->IGTK[group->GN_igtk - 4], |
2380 | WPA_IGTK_LEN) < 0) | |
2381 | ret = -1; | |
6fc6879b | 2382 | #endif /* CONFIG_IEEE80211W */ |
3c7302c2 JM |
2383 | |
2384 | return ret; | |
6fc6879b JM |
2385 | } |
2386 | ||
2387 | ||
1bdb7ab3 JM |
2388 | static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth, |
2389 | struct wpa_group *group) | |
2390 | { | |
2391 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state " | |
2392 | "SETKEYSDONE (VLAN-ID %d)", group->vlan_id); | |
2393 | group->changed = TRUE; | |
2394 | group->wpa_group_state = WPA_GROUP_SETKEYSDONE; | |
2395 | ||
2396 | if (wpa_group_config_group_keys(wpa_auth, group) < 0) | |
2397 | return -1; | |
2398 | ||
2399 | return 0; | |
2400 | } | |
2401 | ||
2402 | ||
6fc6879b JM |
2403 | static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth, |
2404 | struct wpa_group *group) | |
2405 | { | |
2406 | if (group->GInit) { | |
2407 | wpa_group_gtk_init(wpa_auth, group); | |
2408 | } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT && | |
2409 | group->GTKAuthenticator) { | |
2410 | wpa_group_setkeysdone(wpa_auth, group); | |
2411 | } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE && | |
2412 | group->GTKReKey) { | |
2413 | wpa_group_setkeys(wpa_auth, group); | |
2414 | } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) { | |
2415 | if (group->GKeyDoneStations == 0) | |
2416 | wpa_group_setkeysdone(wpa_auth, group); | |
2417 | else if (group->GTKReKey) | |
2418 | wpa_group_setkeys(wpa_auth, group); | |
2419 | } | |
2420 | } | |
2421 | ||
2422 | ||
e4a6ea1d | 2423 | static int wpa_sm_step(struct wpa_state_machine *sm) |
6fc6879b JM |
2424 | { |
2425 | if (sm == NULL) | |
e4a6ea1d | 2426 | return 0; |
6fc6879b JM |
2427 | |
2428 | if (sm->in_step_loop) { | |
2429 | /* This should not happen, but if it does, make sure we do not | |
2430 | * end up freeing the state machine too early by exiting the | |
2431 | * recursive call. */ | |
2432 | wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively"); | |
e4a6ea1d | 2433 | return 0; |
6fc6879b JM |
2434 | } |
2435 | ||
2436 | sm->in_step_loop = 1; | |
2437 | do { | |
2438 | if (sm->pending_deinit) | |
2439 | break; | |
2440 | ||
2441 | sm->changed = FALSE; | |
2442 | sm->wpa_auth->group->changed = FALSE; | |
2443 | ||
2444 | SM_STEP_RUN(WPA_PTK); | |
2445 | if (sm->pending_deinit) | |
2446 | break; | |
2447 | SM_STEP_RUN(WPA_PTK_GROUP); | |
2448 | if (sm->pending_deinit) | |
2449 | break; | |
2450 | wpa_group_sm_step(sm->wpa_auth, sm->group); | |
2451 | } while (sm->changed || sm->wpa_auth->group->changed); | |
2452 | sm->in_step_loop = 0; | |
2453 | ||
2454 | if (sm->pending_deinit) { | |
2455 | wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state " | |
2456 | "machine deinit for " MACSTR, MAC2STR(sm->addr)); | |
2457 | wpa_free_sta_sm(sm); | |
e4a6ea1d | 2458 | return 1; |
6fc6879b | 2459 | } |
e4a6ea1d | 2460 | return 0; |
6fc6879b JM |
2461 | } |
2462 | ||
2463 | ||
2464 | static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx) | |
2465 | { | |
2466 | struct wpa_state_machine *sm = eloop_ctx; | |
2467 | wpa_sm_step(sm); | |
2468 | } | |
2469 | ||
2470 | ||
2471 | void wpa_auth_sm_notify(struct wpa_state_machine *sm) | |
2472 | { | |
2473 | if (sm == NULL) | |
2474 | return; | |
2475 | eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL); | |
2476 | } | |
2477 | ||
2478 | ||
2479 | void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth) | |
2480 | { | |
2481 | int tmp, i; | |
2482 | struct wpa_group *group; | |
2483 | ||
2484 | if (wpa_auth == NULL) | |
2485 | return; | |
2486 | ||
2487 | group = wpa_auth->group; | |
2488 | ||
2489 | for (i = 0; i < 2; i++) { | |
2490 | tmp = group->GM; | |
2491 | group->GM = group->GN; | |
2492 | group->GN = tmp; | |
2493 | #ifdef CONFIG_IEEE80211W | |
2494 | tmp = group->GM_igtk; | |
2495 | group->GM_igtk = group->GN_igtk; | |
2496 | group->GN_igtk = tmp; | |
2497 | #endif /* CONFIG_IEEE80211W */ | |
2498 | wpa_gtk_update(wpa_auth, group); | |
2499 | } | |
2500 | } | |
2501 | ||
2502 | ||
2503 | static const char * wpa_bool_txt(int bool) | |
2504 | { | |
2505 | return bool ? "TRUE" : "FALSE"; | |
2506 | } | |
2507 | ||
2508 | ||
2509 | static int wpa_cipher_bits(int cipher) | |
2510 | { | |
2511 | switch (cipher) { | |
2512 | case WPA_CIPHER_CCMP: | |
2513 | return 128; | |
2514 | case WPA_CIPHER_TKIP: | |
2515 | return 256; | |
2516 | case WPA_CIPHER_WEP104: | |
2517 | return 104; | |
2518 | case WPA_CIPHER_WEP40: | |
2519 | return 40; | |
2520 | default: | |
2521 | return 0; | |
2522 | } | |
2523 | } | |
2524 | ||
2525 | ||
2526 | #define RSN_SUITE "%02x-%02x-%02x-%d" | |
2527 | #define RSN_SUITE_ARG(s) \ | |
2528 | ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff | |
2529 | ||
2530 | int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen) | |
2531 | { | |
2532 | int len = 0, ret; | |
2533 | char pmkid_txt[PMKID_LEN * 2 + 1]; | |
8ce58ceb IH |
2534 | #ifdef CONFIG_RSN_PREAUTH |
2535 | const int preauth = 1; | |
2536 | #else /* CONFIG_RSN_PREAUTH */ | |
2537 | const int preauth = 0; | |
2538 | #endif /* CONFIG_RSN_PREAUTH */ | |
6fc6879b JM |
2539 | |
2540 | if (wpa_auth == NULL) | |
2541 | return len; | |
2542 | ||
2543 | ret = os_snprintf(buf + len, buflen - len, | |
2544 | "dot11RSNAOptionImplemented=TRUE\n" | |
8ce58ceb | 2545 | "dot11RSNAPreauthenticationImplemented=%s\n" |
6fc6879b JM |
2546 | "dot11RSNAEnabled=%s\n" |
2547 | "dot11RSNAPreauthenticationEnabled=%s\n", | |
8ce58ceb | 2548 | wpa_bool_txt(preauth), |
6fc6879b JM |
2549 | wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN), |
2550 | wpa_bool_txt(wpa_auth->conf.rsn_preauth)); | |
2551 | if (ret < 0 || (size_t) ret >= buflen - len) | |
2552 | return len; | |
2553 | len += ret; | |
2554 | ||
2555 | wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt), | |
2556 | wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN); | |
2557 | ||
2558 | ret = os_snprintf( | |
2559 | buf + len, buflen - len, | |
2560 | "dot11RSNAConfigVersion=%u\n" | |
2561 | "dot11RSNAConfigPairwiseKeysSupported=9999\n" | |
2562 | /* FIX: dot11RSNAConfigGroupCipher */ | |
2563 | /* FIX: dot11RSNAConfigGroupRekeyMethod */ | |
2564 | /* FIX: dot11RSNAConfigGroupRekeyTime */ | |
2565 | /* FIX: dot11RSNAConfigGroupRekeyPackets */ | |
2566 | "dot11RSNAConfigGroupRekeyStrict=%u\n" | |
2567 | "dot11RSNAConfigGroupUpdateCount=%u\n" | |
2568 | "dot11RSNAConfigPairwiseUpdateCount=%u\n" | |
2569 | "dot11RSNAConfigGroupCipherSize=%u\n" | |
2570 | "dot11RSNAConfigPMKLifetime=%u\n" | |
2571 | "dot11RSNAConfigPMKReauthThreshold=%u\n" | |
2572 | "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n" | |
2573 | "dot11RSNAConfigSATimeout=%u\n" | |
2574 | "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n" | |
2575 | "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n" | |
2576 | "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n" | |
2577 | "dot11RSNAPMKIDUsed=%s\n" | |
2578 | "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n" | |
2579 | "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n" | |
2580 | "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n" | |
2581 | "dot11RSNATKIPCounterMeasuresInvoked=%u\n" | |
2582 | "dot11RSNA4WayHandshakeFailures=%u\n" | |
2583 | "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n", | |
2584 | RSN_VERSION, | |
2585 | !!wpa_auth->conf.wpa_strict_rekey, | |
2586 | dot11RSNAConfigGroupUpdateCount, | |
2587 | dot11RSNAConfigPairwiseUpdateCount, | |
2588 | wpa_cipher_bits(wpa_auth->conf.wpa_group), | |
2589 | dot11RSNAConfigPMKLifetime, | |
2590 | dot11RSNAConfigPMKReauthThreshold, | |
2591 | dot11RSNAConfigSATimeout, | |
2592 | RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected), | |
2593 | RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected), | |
2594 | RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected), | |
2595 | pmkid_txt, | |
2596 | RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested), | |
2597 | RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested), | |
2598 | RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested), | |
2599 | wpa_auth->dot11RSNATKIPCounterMeasuresInvoked, | |
2600 | wpa_auth->dot11RSNA4WayHandshakeFailures); | |
2601 | if (ret < 0 || (size_t) ret >= buflen - len) | |
2602 | return len; | |
2603 | len += ret; | |
2604 | ||
2605 | /* TODO: dot11RSNAConfigPairwiseCiphersTable */ | |
2606 | /* TODO: dot11RSNAConfigAuthenticationSuitesTable */ | |
2607 | ||
2608 | /* Private MIB */ | |
2609 | ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n", | |
2610 | wpa_auth->group->wpa_group_state); | |
2611 | if (ret < 0 || (size_t) ret >= buflen - len) | |
2612 | return len; | |
2613 | len += ret; | |
2614 | ||
2615 | return len; | |
2616 | } | |
2617 | ||
2618 | ||
2619 | int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen) | |
2620 | { | |
2621 | int len = 0, ret; | |
2622 | u32 pairwise = 0; | |
2623 | ||
2624 | if (sm == NULL) | |
2625 | return 0; | |
2626 | ||
2627 | /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */ | |
2628 | ||
2629 | /* dot11RSNAStatsEntry */ | |
2630 | ||
2631 | if (sm->wpa == WPA_VERSION_WPA) { | |
2632 | if (sm->pairwise == WPA_CIPHER_CCMP) | |
2633 | pairwise = WPA_CIPHER_SUITE_CCMP; | |
2634 | else if (sm->pairwise == WPA_CIPHER_TKIP) | |
2635 | pairwise = WPA_CIPHER_SUITE_TKIP; | |
2636 | else if (sm->pairwise == WPA_CIPHER_WEP104) | |
2637 | pairwise = WPA_CIPHER_SUITE_WEP104; | |
2638 | else if (sm->pairwise == WPA_CIPHER_WEP40) | |
2639 | pairwise = WPA_CIPHER_SUITE_WEP40; | |
2640 | else if (sm->pairwise == WPA_CIPHER_NONE) | |
2641 | pairwise = WPA_CIPHER_SUITE_NONE; | |
2642 | } else if (sm->wpa == WPA_VERSION_WPA2) { | |
2643 | if (sm->pairwise == WPA_CIPHER_CCMP) | |
2644 | pairwise = RSN_CIPHER_SUITE_CCMP; | |
2645 | else if (sm->pairwise == WPA_CIPHER_TKIP) | |
2646 | pairwise = RSN_CIPHER_SUITE_TKIP; | |
2647 | else if (sm->pairwise == WPA_CIPHER_WEP104) | |
2648 | pairwise = RSN_CIPHER_SUITE_WEP104; | |
2649 | else if (sm->pairwise == WPA_CIPHER_WEP40) | |
2650 | pairwise = RSN_CIPHER_SUITE_WEP40; | |
2651 | else if (sm->pairwise == WPA_CIPHER_NONE) | |
2652 | pairwise = RSN_CIPHER_SUITE_NONE; | |
2653 | } else | |
2654 | return 0; | |
2655 | ||
2656 | ret = os_snprintf( | |
2657 | buf + len, buflen - len, | |
2658 | /* TODO: dot11RSNAStatsIndex */ | |
2659 | "dot11RSNAStatsSTAAddress=" MACSTR "\n" | |
2660 | "dot11RSNAStatsVersion=1\n" | |
2661 | "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n" | |
2662 | /* TODO: dot11RSNAStatsTKIPICVErrors */ | |
2663 | "dot11RSNAStatsTKIPLocalMICFailures=%u\n" | |
d9040cdb | 2664 | "dot11RSNAStatsTKIPRemoteMICFailures=%u\n" |
6fc6879b JM |
2665 | /* TODO: dot11RSNAStatsCCMPReplays */ |
2666 | /* TODO: dot11RSNAStatsCCMPDecryptErrors */ | |
2667 | /* TODO: dot11RSNAStatsTKIPReplays */, | |
2668 | MAC2STR(sm->addr), | |
2669 | RSN_SUITE_ARG(pairwise), | |
2670 | sm->dot11RSNAStatsTKIPLocalMICFailures, | |
2671 | sm->dot11RSNAStatsTKIPRemoteMICFailures); | |
2672 | if (ret < 0 || (size_t) ret >= buflen - len) | |
2673 | return len; | |
2674 | len += ret; | |
2675 | ||
2676 | /* Private MIB */ | |
2677 | ret = os_snprintf(buf + len, buflen - len, | |
2678 | "hostapdWPAPTKState=%d\n" | |
2679 | "hostapdWPAPTKGroupState=%d\n", | |
2680 | sm->wpa_ptk_state, | |
2681 | sm->wpa_ptk_group_state); | |
2682 | if (ret < 0 || (size_t) ret >= buflen - len) | |
2683 | return len; | |
2684 | len += ret; | |
2685 | ||
2686 | return len; | |
2687 | } | |
2688 | ||
2689 | ||
2690 | void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth) | |
2691 | { | |
2692 | if (wpa_auth) | |
2693 | wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++; | |
2694 | } | |
2695 | ||
2696 | ||
2697 | int wpa_auth_pairwise_set(struct wpa_state_machine *sm) | |
2698 | { | |
2699 | return sm && sm->pairwise_set; | |
2700 | } | |
2701 | ||
2702 | ||
ff36ff00 JM |
2703 | int wpa_auth_get_pairwise(struct wpa_state_machine *sm) |
2704 | { | |
2705 | return sm->pairwise; | |
2706 | } | |
2707 | ||
2708 | ||
6fc6879b JM |
2709 | int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm) |
2710 | { | |
2711 | if (sm == NULL) | |
2712 | return -1; | |
2713 | return sm->wpa_key_mgmt; | |
2714 | } | |
2715 | ||
2716 | ||
2717 | int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm) | |
2718 | { | |
2719 | if (sm == NULL) | |
2720 | return 0; | |
2721 | return sm->wpa; | |
2722 | } | |
2723 | ||
2724 | ||
2725 | int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm, | |
2726 | struct rsn_pmksa_cache_entry *entry) | |
2727 | { | |
2728 | if (sm == NULL || sm->pmksa != entry) | |
2729 | return -1; | |
2730 | sm->pmksa = NULL; | |
2731 | return 0; | |
2732 | } | |
2733 | ||
2734 | ||
2735 | struct rsn_pmksa_cache_entry * | |
2736 | wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm) | |
2737 | { | |
2738 | return sm ? sm->pmksa : NULL; | |
2739 | } | |
2740 | ||
2741 | ||
2742 | void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm) | |
2743 | { | |
2744 | if (sm) | |
2745 | sm->dot11RSNAStatsTKIPLocalMICFailures++; | |
2746 | } | |
2747 | ||
2748 | ||
2749 | const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len) | |
2750 | { | |
2751 | if (wpa_auth == NULL) | |
2752 | return NULL; | |
2753 | *len = wpa_auth->wpa_ie_len; | |
2754 | return wpa_auth->wpa_ie; | |
2755 | } | |
2756 | ||
2757 | ||
2758 | int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk, | |
2759 | int session_timeout, struct eapol_state_machine *eapol) | |
2760 | { | |
cb465555 JM |
2761 | if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 || |
2762 | sm->wpa_auth->conf.disable_pmksa_caching) | |
6fc6879b JM |
2763 | return -1; |
2764 | ||
4bb081f1 JM |
2765 | if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN, |
2766 | sm->wpa_auth->addr, sm->addr, session_timeout, | |
2767 | eapol, sm->wpa_key_mgmt)) | |
6fc6879b JM |
2768 | return 0; |
2769 | ||
2770 | return -1; | |
2771 | } | |
2772 | ||
2773 | ||
2774 | int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth, | |
2775 | const u8 *pmk, size_t len, const u8 *sta_addr, | |
2776 | int session_timeout, | |
2777 | struct eapol_state_machine *eapol) | |
2778 | { | |
2779 | if (wpa_auth == NULL) | |
2780 | return -1; | |
2781 | ||
4bb081f1 JM |
2782 | if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr, |
2783 | sta_addr, session_timeout, eapol, | |
2784 | WPA_KEY_MGMT_IEEE8021X)) | |
6fc6879b JM |
2785 | return 0; |
2786 | ||
2787 | return -1; | |
2788 | } | |
2789 | ||
2790 | ||
2791 | static struct wpa_group * | |
2792 | wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id) | |
2793 | { | |
2794 | struct wpa_group *group; | |
2795 | ||
2796 | if (wpa_auth == NULL || wpa_auth->group == NULL) | |
2797 | return NULL; | |
2798 | ||
2799 | wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d", | |
2800 | vlan_id); | |
bdffdc5d | 2801 | group = wpa_group_init(wpa_auth, vlan_id, 0); |
6fc6879b JM |
2802 | if (group == NULL) |
2803 | return NULL; | |
2804 | ||
2805 | group->next = wpa_auth->group->next; | |
2806 | wpa_auth->group->next = group; | |
2807 | ||
2808 | return group; | |
2809 | } | |
2810 | ||
2811 | ||
2812 | int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id) | |
2813 | { | |
2814 | struct wpa_group *group; | |
2815 | ||
2816 | if (sm == NULL || sm->wpa_auth == NULL) | |
2817 | return 0; | |
2818 | ||
2819 | group = sm->wpa_auth->group; | |
2820 | while (group) { | |
2821 | if (group->vlan_id == vlan_id) | |
2822 | break; | |
2823 | group = group->next; | |
2824 | } | |
2825 | ||
2826 | if (group == NULL) { | |
2827 | group = wpa_auth_add_group(sm->wpa_auth, vlan_id); | |
2828 | if (group == NULL) | |
2829 | return -1; | |
2830 | } | |
2831 | ||
2832 | if (sm->group == group) | |
2833 | return 0; | |
2834 | ||
2835 | wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state " | |
2836 | "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id); | |
2837 | ||
2838 | sm->group = group; | |
2839 | return 0; | |
2840 | } | |
e4bf4db9 JM |
2841 | |
2842 | ||
2843 | void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth, | |
2844 | struct wpa_state_machine *sm, int ack) | |
2845 | { | |
2846 | if (wpa_auth == NULL || sm == NULL) | |
2847 | return; | |
2848 | wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR | |
2849 | " ack=%d", MAC2STR(sm->addr), ack); | |
2850 | if (sm->pending_1_of_4_timeout && ack) { | |
2851 | /* | |
2852 | * Some deployed supplicant implementations update their SNonce | |
2853 | * for each EAPOL-Key 2/4 message even within the same 4-way | |
2854 | * handshake and then fail to use the first SNonce when | |
2855 | * deriving the PTK. This results in unsuccessful 4-way | |
2856 | * handshake whenever the relatively short initial timeout is | |
2857 | * reached and EAPOL-Key 1/4 is retransmitted. Try to work | |
2858 | * around this by increasing the timeout now that we know that | |
2859 | * the station has received the frame. | |
2860 | */ | |
2861 | int timeout_ms = eapol_key_timeout_subseq; | |
2862 | wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 " | |
2863 | "timeout by %u ms because of acknowledged frame", | |
2864 | timeout_ms); | |
2865 | eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm); | |
2866 | eloop_register_timeout(timeout_ms / 1000, | |
2867 | (timeout_ms % 1000) * 1000, | |
2868 | wpa_send_eapol_timeout, wpa_auth, sm); | |
2869 | } | |
2870 | } |