1 /*
2 * IEEE 802.11 RSN / WPA Authenticator
3 * Copyright (c) 2004-2019, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/state_machine.h"
14 #include "utils/bitfield.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/ocv.h"
17 #include "common/dpp.h"
18 #include "common/wpa_ctrl.h"
19 #include "crypto/aes.h"
20 #include "crypto/aes_wrap.h"
21 #include "crypto/aes_siv.h"
22 #include "crypto/crypto.h"
23 #include "crypto/sha1.h"
24 #include "crypto/sha256.h"
25 #include "crypto/sha384.h"
26 #include "crypto/random.h"
27 #include "eapol_auth/eapol_auth_sm.h"
28 #include "drivers/driver.h"
29 #include "ap_config.h"
30 #include "ieee802_11.h"
31 #include "wpa_auth.h"
32 #include "pmksa_cache_auth.h"
33 #include "wpa_auth_i.h"
34 #include "wpa_auth_ie.h"
35 #ifdef CONFIG_VENDOR_EXT
36 #include "vendor_ext.h"
37 #endif
38
39 #if defined(CONFIG_OPEN_HARMONY_PATCH) && defined(OPEN_HARMONY_MIRACAST_SINK_OPT)
40 #include "hm_miracast_sink.h"
41 #endif
42
43 #define STATE_MACHINE_DATA struct wpa_state_machine
44 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
45 #define STATE_MACHINE_ADDR sm->addr
46
47
48 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
49 static int wpa_sm_step(struct wpa_state_machine *sm);
50 static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK,
51 u8 *data, size_t data_len);
52 #ifdef CONFIG_FILS
53 static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
54 u8 *buf, size_t buf_len, u16 *_key_data_len);
55 static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm,
56 const struct wpabuf *hlp);
57 #endif /* CONFIG_FILS */
58 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
59 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
60 struct wpa_group *group);
61 static void wpa_request_new_ptk(struct wpa_state_machine *sm);
62 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
63 struct wpa_group *group);
64 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
65 struct wpa_group *group);
66 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
67 const u8 *pmk, unsigned int pmk_len,
68 struct wpa_ptk *ptk, int force_sha256);
69 static void wpa_group_free(struct wpa_authenticator *wpa_auth,
70 struct wpa_group *group);
71 static void wpa_group_get(struct wpa_authenticator *wpa_auth,
72 struct wpa_group *group);
73 static void wpa_group_put(struct wpa_authenticator *wpa_auth,
74 struct wpa_group *group);
75 static int ieee80211w_kde_len(struct wpa_state_machine *sm);
76 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos);
77
78 #ifdef OPEN_HARMONY_MIRACAST_SINK_OPT
79 /* hostapd send and recv packet more then 100ms */
80 static const u32 eapol_key_timeout_first = 1000; /* ms */
81 #else
82 static const u32 eapol_key_timeout_first = 100; /* ms */
83 #endif
84
85 static const u32 eapol_key_timeout_subseq = 1000; /* ms */
86 static const u32 eapol_key_timeout_first_group = 500; /* ms */
87 static const u32 eapol_key_timeout_no_retrans = 4000; /* ms */
88
89 /* TODO: make these configurable */
90 static const int dot11RSNAConfigPMKLifetime = 43200;
91 static const int dot11RSNAConfigPMKReauthThreshold = 70;
92 static const int dot11RSNAConfigSATimeout = 60;
93
94
wpa_auth_mic_failure_report( struct wpa_authenticator *wpa_auth, const u8 *addr)95 static inline int wpa_auth_mic_failure_report(
96 struct wpa_authenticator *wpa_auth, const u8 *addr)
97 {
98 if (wpa_auth->cb->mic_failure_report)
99 return wpa_auth->cb->mic_failure_report(wpa_auth->cb_ctx, addr);
100 return 0;
101 }
102
103
wpa_auth_psk_failure_report( struct wpa_authenticator *wpa_auth, const u8 *addr)104 static inline void wpa_auth_psk_failure_report(
105 struct wpa_authenticator *wpa_auth, const u8 *addr)
106 {
107 if (wpa_auth->cb->psk_failure_report)
108 wpa_auth->cb->psk_failure_report(wpa_auth->cb_ctx, addr);
109 }
110
111
wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr, wpa_eapol_variable var, int value)112 static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
113 const u8 *addr, wpa_eapol_variable var,
114 int value)
115 {
116 if (wpa_auth->cb->set_eapol)
117 wpa_auth->cb->set_eapol(wpa_auth->cb_ctx, addr, var, value);
118 }
119
120
wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr, wpa_eapol_variable var)121 static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
122 const u8 *addr, wpa_eapol_variable var)
123 {
124 if (!wpa_auth->cb->get_eapol)
125 return -1;
126 return wpa_auth->cb->get_eapol(wpa_auth->cb_ctx, addr, var);
127 }
128
129
wpa_auth_get_psk(struct wpa_authenticator *wpa_auth, const u8 *addr, const u8 *p2p_dev_addr, const u8 *prev_psk, size_t *psk_len, int *vlan_id)130 static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
131 const u8 *addr,
132 const u8 *p2p_dev_addr,
133 const u8 *prev_psk, size_t *psk_len,
134 int *vlan_id)
135 {
136 if (!wpa_auth->cb->get_psk)
137 return NULL;
138 return wpa_auth->cb->get_psk(wpa_auth->cb_ctx, addr, p2p_dev_addr,
139 prev_psk, psk_len, vlan_id);
140 }
141
142
wpa_auth_get_msk(struct wpa_authenticator *wpa_auth, const u8 *addr, u8 *msk, size_t *len)143 static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
144 const u8 *addr, u8 *msk, size_t *len)
145 {
146 if (!wpa_auth->cb->get_msk)
147 return -1;
148 return wpa_auth->cb->get_msk(wpa_auth->cb_ctx, addr, msk, len);
149 }
150
151
wpa_auth_set_key(struct wpa_authenticator *wpa_auth, int vlan_id, enum wpa_alg alg, const u8 *addr, int idx, u8 *key, size_t key_len, enum key_flag key_flag)152 int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
153 int vlan_id,
154 enum wpa_alg alg, const u8 *addr, int idx,
155 u8 *key, size_t key_len,
156 enum key_flag key_flag)
157 {
158 if (wpa_auth == NULL)
159 return -1;
160
161 if (wpa_auth->cb == NULL)
162 return -1;
163
164 if (!wpa_auth->cb->set_key)
165 return -1;
166 return wpa_auth->cb->set_key(wpa_auth->cb_ctx, vlan_id, alg, addr, idx,
167 key, key_len, key_flag);
168 }
169
170
wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth, const u8 *addr, int idx, u8 *seq)171 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
172 const u8 *addr, int idx, u8 *seq)
173 {
174 int res;
175
176 if (!wpa_auth->cb->get_seqnum)
177 return -1;
178 res = wpa_auth->cb->get_seqnum(wpa_auth->cb_ctx, addr, idx, seq);
179 #ifdef CONFIG_TESTING_OPTIONS
180 if (!addr && idx < 4 && wpa_auth->conf.gtk_rsc_override_set) {
181 wpa_printf(MSG_DEBUG,
182 "TESTING: Override GTK RSC %016llx --> %016llx",
183 (long long unsigned) WPA_GET_LE64(seq),
184 (long long unsigned)
185 WPA_GET_LE64(wpa_auth->conf.gtk_rsc_override));
186 os_memcpy(seq, wpa_auth->conf.gtk_rsc_override,
187 WPA_KEY_RSC_LEN);
188 }
189 if (!addr && idx >= 4 && idx <= 5 &&
190 wpa_auth->conf.igtk_rsc_override_set) {
191 wpa_printf(MSG_DEBUG,
192 "TESTING: Override IGTK RSC %016llx --> %016llx",
193 (long long unsigned) WPA_GET_LE64(seq),
194 (long long unsigned)
195 WPA_GET_LE64(wpa_auth->conf.igtk_rsc_override));
196 os_memcpy(seq, wpa_auth->conf.igtk_rsc_override,
197 WPA_KEY_RSC_LEN);
198 }
199 #endif /* CONFIG_TESTING_OPTIONS */
200 return res;
201 }
202
203
204 static inline int
wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr, const u8 *data, size_t data_len, int encrypt)205 wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
206 const u8 *data, size_t data_len, int encrypt)
207 {
208 if (!wpa_auth->cb->send_eapol)
209 return -1;
210 return wpa_auth->cb->send_eapol(wpa_auth->cb_ctx, addr, data, data_len,
211 encrypt);
212 }
213
214
215 #ifdef CONFIG_MESH
wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth, const u8 *addr)216 static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth,
217 const u8 *addr)
218 {
219 if (!wpa_auth->cb->start_ampe)
220 return -1;
221 return wpa_auth->cb->start_ampe(wpa_auth->cb_ctx, addr);
222 }
223 #endif /* CONFIG_MESH */
224
225
wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth, int (*cb)(struct wpa_state_machine *sm, void *ctx), void *cb_ctx)226 int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
227 int (*cb)(struct wpa_state_machine *sm, void *ctx),
228 void *cb_ctx)
229 {
230 if (!wpa_auth->cb->for_each_sta)
231 return 0;
232 return wpa_auth->cb->for_each_sta(wpa_auth->cb_ctx, cb, cb_ctx);
233 }
234
235
wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth, int (*cb)(struct wpa_authenticator *a, void *ctx), void *cb_ctx)236 int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
237 int (*cb)(struct wpa_authenticator *a, void *ctx),
238 void *cb_ctx)
239 {
240 if (!wpa_auth->cb->for_each_auth)
241 return 0;
242 return wpa_auth->cb->for_each_auth(wpa_auth->cb_ctx, cb, cb_ctx);
243 }
244
245
wpa_auth_store_ptksa(struct wpa_authenticator *wpa_auth, const u8 *addr, int cipher, u32 life_time, const struct wpa_ptk *ptk)246 void wpa_auth_store_ptksa(struct wpa_authenticator *wpa_auth,
247 const u8 *addr, int cipher,
248 u32 life_time, const struct wpa_ptk *ptk)
249 {
250 if (wpa_auth->cb->store_ptksa)
251 wpa_auth->cb->store_ptksa(wpa_auth->cb_ctx, addr, cipher,
252 life_time, ptk);
253 }
254
255
wpa_auth_remove_ptksa(struct wpa_authenticator *wpa_auth, const u8 *addr, int cipher)256 void wpa_auth_remove_ptksa(struct wpa_authenticator *wpa_auth,
257 const u8 *addr, int cipher)
258 {
259 if (wpa_auth->cb->clear_ptksa)
260 wpa_auth->cb->clear_ptksa(wpa_auth->cb_ctx, addr, cipher);
261 }
262
wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr, logger_level level, const char *txt)263 void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
264 logger_level level, const char *txt)
265 {
266 if (!wpa_auth->cb->logger)
267 return;
268 wpa_auth->cb->logger(wpa_auth->cb_ctx, addr, level, txt);
269 }
270
271
wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr, logger_level level, const char *fmt, ...)272 void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
273 logger_level level, const char *fmt, ...)
274 {
275 char *format;
276 int maxlen;
277 va_list ap;
278
279 if (!wpa_auth->cb->logger)
280 return;
281
282 maxlen = os_strlen(fmt) + 100;
283 format = os_malloc(maxlen);
284 if (!format)
285 return;
286
287 va_start(ap, fmt);
288 vsnprintf(format, maxlen, fmt, ap);
289 va_end(ap);
290
291 wpa_auth_logger(wpa_auth, addr, level, format);
292
293 os_free(format);
294 }
295
296
wpa_sta_disconnect(struct wpa_authenticator *wpa_auth, const u8 *addr, u16 reason)297 static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
298 const u8 *addr, u16 reason)
299 {
300 if (!wpa_auth->cb->disconnect)
301 return;
302 wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR_SEC " (reason %u)",
303 MAC2STR_SEC(addr), reason);
304 wpa_auth->cb->disconnect(wpa_auth->cb_ctx, addr, reason);
305 }
306
307
308 #ifdef CONFIG_OCV
wpa_channel_info(struct wpa_authenticator *wpa_auth, struct wpa_channel_info *ci)309 static int wpa_channel_info(struct wpa_authenticator *wpa_auth,
310 struct wpa_channel_info *ci)
311 {
312 if (!wpa_auth->cb->channel_info)
313 return -1;
314 return wpa_auth->cb->channel_info(wpa_auth->cb_ctx, ci);
315 }
316 #endif /* CONFIG_OCV */
317
318
wpa_auth_update_vlan(struct wpa_authenticator *wpa_auth, const u8 *addr, int vlan_id)319 static int wpa_auth_update_vlan(struct wpa_authenticator *wpa_auth,
320 const u8 *addr, int vlan_id)
321 {
322 if (!wpa_auth->cb->update_vlan)
323 return -1;
324 return wpa_auth->cb->update_vlan(wpa_auth->cb_ctx, addr, vlan_id);
325 }
326
327
wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)328 static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
329 {
330 struct wpa_authenticator *wpa_auth = eloop_ctx;
331
332 if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
333 wpa_printf(MSG_ERROR,
334 "Failed to get random data for WPA initialization.");
335 } else {
336 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
337 wpa_hexdump_key(MSG_DEBUG, "GMK",
338 wpa_auth->group->GMK, WPA_GMK_LEN);
339 }
340
341 if (wpa_auth->conf.wpa_gmk_rekey) {
342 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
343 wpa_rekey_gmk, wpa_auth, NULL);
344 }
345 }
346
347
wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)348 static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
349 {
350 struct wpa_authenticator *wpa_auth = eloop_ctx;
351 struct wpa_group *group, *next;
352
353 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
354 group = wpa_auth->group;
355 while (group) {
356 wpa_group_get(wpa_auth, group);
357
358 group->GTKReKey = true;
359 do {
360 group->changed = false;
361 wpa_group_sm_step(wpa_auth, group);
362 } while (group->changed);
363
364 next = group->next;
365 wpa_group_put(wpa_auth, group);
366 group = next;
367 }
368
369 if (wpa_auth->conf.wpa_group_rekey) {
370 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
371 0, wpa_rekey_gtk, wpa_auth, NULL);
372 }
373 }
374
375
wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)376 static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
377 {
378 struct wpa_authenticator *wpa_auth = eloop_ctx;
379 struct wpa_state_machine *sm = timeout_ctx;
380
381 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
382 wpa_request_new_ptk(sm);
383 wpa_sm_step(sm);
384 }
385
386
wpa_auth_set_ptk_rekey_timer(struct wpa_state_machine *sm)387 void wpa_auth_set_ptk_rekey_timer(struct wpa_state_machine *sm)
388 {
389 if (sm && sm->wpa_auth->conf.wpa_ptk_rekey) {
390 wpa_printf(MSG_DEBUG, "WPA: Start PTK rekeying timer for "
391 MACSTR_SEC " (%d seconds)", MAC2STR_SEC(sm->addr),
392 sm->wpa_auth->conf.wpa_ptk_rekey);
393 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
394 eloop_register_timeout(sm->wpa_auth->conf.wpa_ptk_rekey, 0,
395 wpa_rekey_ptk, sm->wpa_auth, sm);
396 }
397 }
398
399
wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)400 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
401 {
402 if (sm->pmksa == ctx)
403 sm->pmksa = NULL;
404 return 0;
405 }
406
407
wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry, void *ctx)408 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
409 void *ctx)
410 {
411 struct wpa_authenticator *wpa_auth = ctx;
412 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
413 }
414
415
wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth, struct wpa_group *group)416 static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
417 struct wpa_group *group)
418 {
419 u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
420 u8 rkey[32];
421 unsigned long ptr;
422
423 if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
424 return -1;
425 wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
426
427 /*
428 * Counter = PRF-256(Random number, "Init Counter",
429 * Local MAC Address || Time)
430 */
431 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
432 wpa_get_ntp_timestamp(buf + ETH_ALEN);
433 ptr = (unsigned long) group;
434 os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
435 #ifdef TEST_FUZZ
436 os_memset(buf + ETH_ALEN, 0xab, 8);
437 os_memset(buf + ETH_ALEN + 8, 0xcd, sizeof(ptr));
438 #endif /* TEST_FUZZ */
439 if (random_get_bytes(rkey, sizeof(rkey)) < 0)
440 return -1;
441
442 if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
443 group->Counter, WPA_NONCE_LEN) < 0)
444 return -1;
445 wpa_hexdump_key(MSG_DEBUG, "Key Counter",
446 group->Counter, WPA_NONCE_LEN);
447
448 return 0;
449 }
450
451
wpa_group_init(struct wpa_authenticator *wpa_auth, int vlan_id, int delay_init)452 static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
453 int vlan_id, int delay_init)
454 {
455 struct wpa_group *group;
456
457 group = os_zalloc(sizeof(struct wpa_group));
458 if (!group)
459 return NULL;
460
461 group->GTKAuthenticator = true;
462 group->vlan_id = vlan_id;
463 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
464
465 if (random_pool_ready() != 1) {
466 wpa_printf(MSG_INFO,
467 "WPA: Not enough entropy in random pool for secure operations - update keys later when the first station connects");
468 }
469
470 /*
471 * Set initial GMK/Counter value here. The actual values that will be
472 * used in negotiations will be set once the first station tries to
473 * connect. This allows more time for collecting additional randomness
474 * on embedded devices.
475 */
476 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
477 wpa_printf(MSG_ERROR,
478 "Failed to get random data for WPA initialization.");
479 os_free(group);
480 return NULL;
481 }
482
483 group->GInit = true;
484 if (delay_init) {
485 wpa_printf(MSG_DEBUG,
486 "WPA: Delay group state machine start until Beacon frames have been configured");
487 /* Initialization is completed in wpa_init_keys(). */
488 } else {
489 wpa_group_sm_step(wpa_auth, group);
490 group->GInit = false;
491 wpa_group_sm_step(wpa_auth, group);
492 }
493
494 return group;
495 }
496
497
498 /**
499 * wpa_init - Initialize WPA authenticator
500 * @addr: Authenticator address
501 * @conf: Configuration for WPA authenticator
502 * @cb: Callback functions for WPA authenticator
503 * Returns: Pointer to WPA authenticator data or %NULL on failure
504 */
wpa_init(const u8 *addr, struct wpa_auth_config *conf, const struct wpa_auth_callbacks *cb, void *cb_ctx)505 struct wpa_authenticator * wpa_init(const u8 *addr,
506 struct wpa_auth_config *conf,
507 const struct wpa_auth_callbacks *cb,
508 void *cb_ctx)
509 {
510 struct wpa_authenticator *wpa_auth;
511
512 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
513 if (!wpa_auth)
514 return NULL;
515 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
516 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
517 wpa_auth->cb = cb;
518 wpa_auth->cb_ctx = cb_ctx;
519
520 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
521 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
522 os_free(wpa_auth);
523 return NULL;
524 }
525
526 wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
527 if (!wpa_auth->group) {
528 os_free(wpa_auth->wpa_ie);
529 os_free(wpa_auth);
530 return NULL;
531 }
532
533 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
534 wpa_auth);
535 if (!wpa_auth->pmksa) {
536 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
537 os_free(wpa_auth->group);
538 os_free(wpa_auth->wpa_ie);
539 os_free(wpa_auth);
540 return NULL;
541 }
542
543 #ifdef CONFIG_IEEE80211R_AP
544 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
545 if (!wpa_auth->ft_pmk_cache) {
546 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
547 os_free(wpa_auth->group);
548 os_free(wpa_auth->wpa_ie);
549 pmksa_cache_auth_deinit(wpa_auth->pmksa);
550 os_free(wpa_auth);
551 return NULL;
552 }
553 #endif /* CONFIG_IEEE80211R_AP */
554
555 if (wpa_auth->conf.wpa_gmk_rekey) {
556 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
557 wpa_rekey_gmk, wpa_auth, NULL);
558 }
559
560 if (wpa_auth->conf.wpa_group_rekey) {
561 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
562 wpa_rekey_gtk, wpa_auth, NULL);
563 }
564
565 #ifdef CONFIG_P2P
566 if (WPA_GET_BE32(conf->ip_addr_start)) {
567 int count = WPA_GET_BE32(conf->ip_addr_end) -
568 WPA_GET_BE32(conf->ip_addr_start) + 1;
569 if (count > 1000)
570 count = 1000;
571 if (count > 0)
572 wpa_auth->ip_pool = bitfield_alloc(count);
573 }
574 #endif /* CONFIG_P2P */
575
576 return wpa_auth;
577 }
578
579
wpa_init_keys(struct wpa_authenticator *wpa_auth)580 int wpa_init_keys(struct wpa_authenticator *wpa_auth)
581 {
582 struct wpa_group *group = wpa_auth->group;
583
584 wpa_printf(MSG_DEBUG,
585 "WPA: Start group state machine to set initial keys");
586 wpa_group_sm_step(wpa_auth, group);
587 group->GInit = false;
588 wpa_group_sm_step(wpa_auth, group);
589 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
590 return -1;
591 return 0;
592 }
593
594
595 /**
596 * wpa_deinit - Deinitialize WPA authenticator
597 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
598 */
wpa_deinit(struct wpa_authenticator *wpa_auth)599 void wpa_deinit(struct wpa_authenticator *wpa_auth)
600 {
601 struct wpa_group *group, *prev;
602
603 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
604 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
605
606 pmksa_cache_auth_deinit(wpa_auth->pmksa);
607
608 #ifdef CONFIG_IEEE80211R_AP
609 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
610 wpa_auth->ft_pmk_cache = NULL;
611 wpa_ft_deinit(wpa_auth);
612 #endif /* CONFIG_IEEE80211R_AP */
613
614 #ifdef CONFIG_P2P
615 bitfield_free(wpa_auth->ip_pool);
616 #endif /* CONFIG_P2P */
617
618
619 os_free(wpa_auth->wpa_ie);
620
621 group = wpa_auth->group;
622 while (group) {
623 prev = group;
624 group = group->next;
625 os_free(prev);
626 }
627
628 os_free(wpa_auth);
629 }
630
631
632 /**
633 * wpa_reconfig - Update WPA authenticator configuration
634 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
635 * @conf: Configuration for WPA authenticator
636 */
wpa_reconfig(struct wpa_authenticator *wpa_auth, struct wpa_auth_config *conf)637 int wpa_reconfig(struct wpa_authenticator *wpa_auth,
638 struct wpa_auth_config *conf)
639 {
640 struct wpa_group *group;
641
642 if (!wpa_auth)
643 return 0;
644
645 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
646 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
647 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
648 return -1;
649 }
650
651 /*
652 * Reinitialize GTK to make sure it is suitable for the new
653 * configuration.
654 */
655 group = wpa_auth->group;
656 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
657 group->GInit = true;
658 wpa_group_sm_step(wpa_auth, group);
659 group->GInit = false;
660 wpa_group_sm_step(wpa_auth, group);
661
662 return 0;
663 }
664
665
666 struct wpa_state_machine *
wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr, const u8 *p2p_dev_addr)667 wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
668 const u8 *p2p_dev_addr)
669 {
670 struct wpa_state_machine *sm;
671
672 if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
673 return NULL;
674
675 sm = os_zalloc(sizeof(struct wpa_state_machine));
676 if (!sm)
677 return NULL;
678 os_memcpy(sm->addr, addr, ETH_ALEN);
679 if (p2p_dev_addr)
680 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
681
682 sm->wpa_auth = wpa_auth;
683 sm->group = wpa_auth->group;
684 wpa_group_get(sm->wpa_auth, sm->group);
685
686 return sm;
687 }
688
689
wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth, struct wpa_state_machine *sm)690 int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
691 struct wpa_state_machine *sm)
692 {
693 if (!wpa_auth || !wpa_auth->conf.wpa || !sm)
694 return -1;
695
696 #ifdef CONFIG_IEEE80211R_AP
697 if (sm->ft_completed) {
698 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
699 "FT authentication already completed - do not start 4-way handshake");
700 /* Go to PTKINITDONE state to allow GTK rekeying */
701 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
702 sm->Pair = true;
703 return 0;
704 }
705 #endif /* CONFIG_IEEE80211R_AP */
706
707 #ifdef CONFIG_FILS
708 if (sm->fils_completed) {
709 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
710 "FILS authentication already completed - do not start 4-way handshake");
711 /* Go to PTKINITDONE state to allow GTK rekeying */
712 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
713 sm->Pair = true;
714 return 0;
715 }
716 #endif /* CONFIG_FILS */
717
718 #ifdef CONFIG_VENDOR_EXT
719 if (wpa_vendor_ext_wpa_auth_set_sm(wpa_auth, sm))
720 return 0;
721 #endif
722 if (sm->started) {
723 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
724 sm->ReAuthenticationRequest = true;
725 return wpa_sm_step(sm);
726 }
727
728 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
729 "start authentication");
730 sm->started = 1;
731
732 sm->Init = true;
733 if (wpa_sm_step(sm) == 1)
734 return 1; /* should not really happen */
735 sm->Init = false;
736 sm->AuthenticationRequest = true;
737 return wpa_sm_step(sm);
738 }
739
740
wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)741 void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
742 {
743 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
744 * reassociates back to the same AP while the previous entry for the
745 * STA has not yet been removed. */
746 if (!sm)
747 return;
748
749 sm->wpa_key_mgmt = 0;
750 }
751
752
wpa_free_sta_sm(struct wpa_state_machine *sm)753 static void wpa_free_sta_sm(struct wpa_state_machine *sm)
754 {
755 #ifdef CONFIG_P2P
756 if (WPA_GET_BE32(sm->ip_addr)) {
757 u32 start;
758 wpa_printf(MSG_DEBUG,
759 "P2P: Free assigned IP address %u.%u.%u.%u from "
760 MACSTR_SEC,
761 sm->ip_addr[0], sm->ip_addr[1],
762 sm->ip_addr[2], sm->ip_addr[3],
763 MAC2STR_SEC(sm->addr));
764 start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
765 bitfield_clear(sm->wpa_auth->ip_pool,
766 WPA_GET_BE32(sm->ip_addr) - start);
767 }
768 #endif /* CONFIG_P2P */
769 if (sm->GUpdateStationKeys) {
770 sm->group->GKeyDoneStations--;
771 sm->GUpdateStationKeys = false;
772 }
773 #ifdef CONFIG_IEEE80211R_AP
774 os_free(sm->assoc_resp_ftie);
775 wpabuf_free(sm->ft_pending_req_ies);
776 #endif /* CONFIG_IEEE80211R_AP */
777 os_free(sm->last_rx_eapol_key);
778 os_free(sm->wpa_ie);
779 os_free(sm->rsnxe);
780 wpa_group_put(sm->wpa_auth, sm->group);
781 #ifdef CONFIG_DPP2
782 wpabuf_clear_free(sm->dpp_z);
783 #endif /* CONFIG_DPP2 */
784 bin_clear_free(sm, sizeof(*sm));
785 }
786
787
wpa_auth_sta_deinit(struct wpa_state_machine *sm)788 void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
789 {
790 struct wpa_authenticator *wpa_auth;
791
792 if (!sm)
793 return;
794
795 wpa_auth = sm->wpa_auth;
796 if (wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
797 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
798 "strict rekeying - force GTK rekey since STA is leaving");
799 if (eloop_deplete_timeout(0, 500000, wpa_rekey_gtk,
800 wpa_auth, NULL) == -1)
801 eloop_register_timeout(0, 500000, wpa_rekey_gtk,
802 wpa_auth, NULL);
803 }
804
805 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
806 sm->pending_1_of_4_timeout = 0;
807 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
808 eloop_cancel_timeout(wpa_rekey_ptk, wpa_auth, sm);
809 #ifdef CONFIG_IEEE80211R_AP
810 wpa_ft_sta_deinit(sm);
811 #endif /* CONFIG_IEEE80211R_AP */
812 if (sm->in_step_loop) {
813 /* Must not free state machine while wpa_sm_step() is running.
814 * Freeing will be completed in the end of wpa_sm_step(). */
815 wpa_printf(MSG_DEBUG,
816 "WPA: Registering pending STA state machine deinit for "
817 MACSTR_SEC, MAC2STR_SEC(sm->addr));
818 sm->pending_deinit = 1;
819 } else
820 wpa_free_sta_sm(sm);
821 }
822
823
wpa_request_new_ptk(struct wpa_state_machine *sm)824 static void wpa_request_new_ptk(struct wpa_state_machine *sm)
825 {
826 if (!sm)
827 return;
828
829 if (!sm->use_ext_key_id && sm->wpa_auth->conf.wpa_deny_ptk0_rekey) {
830 wpa_printf(MSG_INFO,
831 "WPA: PTK0 rekey not allowed, disconnect " MACSTR_SEC,
832 MAC2STR_SEC(sm->addr));
833 sm->Disconnect = true;
834 /* Try to encourage the STA to reconnect */
835 sm->disconnect_reason =
836 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
837 } else {
838 if (sm->use_ext_key_id)
839 sm->keyidx_active ^= 1; /* flip Key ID */
840 sm->PTKRequest = true;
841 sm->PTK_valid = 0;
842 }
843 }
844
845
wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr, const u8 *replay_counter)846 static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
847 const u8 *replay_counter)
848 {
849 int i;
850 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
851 if (!ctr[i].valid)
852 break;
853 if (os_memcmp(replay_counter, ctr[i].counter,
854 WPA_REPLAY_COUNTER_LEN) == 0)
855 return 1;
856 }
857 return 0;
858 }
859
860
wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr, const u8 *replay_counter)861 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
862 const u8 *replay_counter)
863 {
864 int i;
865 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
866 if (ctr[i].valid &&
867 (!replay_counter ||
868 os_memcmp(replay_counter, ctr[i].counter,
869 WPA_REPLAY_COUNTER_LEN) == 0))
870 ctr[i].valid = false;
871 }
872 }
873
874
875 #ifdef CONFIG_IEEE80211R_AP
ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth, struct wpa_state_machine *sm, struct wpa_eapol_ie_parse *kde)876 static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
877 struct wpa_state_machine *sm,
878 struct wpa_eapol_ie_parse *kde)
879 {
880 struct wpa_ie_data ie;
881 struct rsn_mdie *mdie;
882
883 if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
884 ie.num_pmkid != 1 || !ie.pmkid) {
885 wpa_printf(MSG_DEBUG,
886 "FT: No PMKR1Name in FT 4-way handshake message 2/4");
887 return -1;
888 }
889
890 os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
891 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
892 sm->sup_pmk_r1_name, PMKID_LEN);
893
894 if (!kde->mdie || !kde->ftie) {
895 wpa_printf(MSG_DEBUG,
896 "FT: No %s in FT 4-way handshake message 2/4",
897 kde->mdie ? "FTIE" : "MDIE");
898 return -1;
899 }
900
901 mdie = (struct rsn_mdie *) (kde->mdie + 2);
902 if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
903 os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
904 MOBILITY_DOMAIN_ID_LEN) != 0) {
905 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
906 return -1;
907 }
908
909 if (sm->assoc_resp_ftie &&
910 (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
911 os_memcmp(kde->ftie, sm->assoc_resp_ftie,
912 2 + sm->assoc_resp_ftie[1]) != 0)) {
913 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
914 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
915 kde->ftie, kde->ftie_len);
916 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
917 sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
918 return -1;
919 }
920
921 return 0;
922 }
923 #endif /* CONFIG_IEEE80211R_AP */
924
925
wpa_receive_error_report(struct wpa_authenticator *wpa_auth, struct wpa_state_machine *sm, int group)926 static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
927 struct wpa_state_machine *sm, int group)
928 {
929 /* Supplicant reported a Michael MIC error */
930 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
931 "received EAPOL-Key Error Request (STA detected Michael MIC failure (group=%d))",
932 group);
933
934 if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
935 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
936 "ignore Michael MIC failure report since group cipher is not TKIP");
937 } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
938 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
939 "ignore Michael MIC failure report since pairwise cipher is not TKIP");
940 } else {
941 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
942 return 1; /* STA entry was removed */
943 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
944 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
945 }
946
947 /*
948 * Error report is not a request for a new key handshake, but since
949 * Authenticator may do it, let's change the keys now anyway.
950 */
951 wpa_request_new_ptk(sm);
952 return 0;
953 }
954
955
wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data, size_t data_len)956 static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
957 size_t data_len)
958 {
959 struct wpa_ptk PTK;
960 int ok = 0;
961 const u8 *pmk = NULL;
962 size_t pmk_len;
963 int vlan_id = 0;
964
965 os_memset(&PTK, 0, sizeof(PTK));
966 for (;;) {
967 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
968 !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
969 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
970 sm->p2p_dev_addr, pmk, &pmk_len,
971 &vlan_id);
972 if (!pmk)
973 break;
974 #ifdef CONFIG_IEEE80211R_AP
975 if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
976 os_memcpy(sm->xxkey, pmk, pmk_len);
977 sm->xxkey_len = pmk_len;
978 }
979 #endif /* CONFIG_IEEE80211R_AP */
980 } else {
981 pmk = sm->PMK;
982 pmk_len = sm->pmk_len;
983 }
984
985 if (wpa_derive_ptk(sm, sm->alt_SNonce, pmk, pmk_len, &PTK, 0) <
986 0)
987 break;
988
989 if (wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK,
990 data, data_len) == 0) {
991 if (sm->PMK != pmk) {
992 os_memcpy(sm->PMK, pmk, pmk_len);
993 sm->pmk_len = pmk_len;
994 }
995 ok = 1;
996 break;
997 }
998
999 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
1000 wpa_key_mgmt_sae(sm->wpa_key_mgmt))
1001 break;
1002 }
1003
1004 if (!ok) {
1005 wpa_printf(MSG_DEBUG,
1006 "WPA: Earlier SNonce did not result in matching MIC");
1007 return -1;
1008 }
1009
1010 wpa_printf(MSG_DEBUG,
1011 "WPA: Earlier SNonce resulted in matching MIC");
1012 sm->alt_snonce_valid = 0;
1013
1014 if (vlan_id && wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
1015 wpa_auth_update_vlan(sm->wpa_auth, sm->addr, vlan_id) < 0)
1016 return -1;
1017
1018 os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
1019 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
1020 forced_memzero(&PTK, sizeof(PTK));
1021 sm->PTK_valid = true;
1022
1023 return 0;
1024 }
1025
1026
wpa_auth_gtk_rekey_in_process(struct wpa_authenticator *wpa_auth)1027 static bool wpa_auth_gtk_rekey_in_process(struct wpa_authenticator *wpa_auth)
1028 {
1029 struct wpa_group *group;
1030
1031 for (group = wpa_auth->group; group; group = group->next) {
1032 if (group->GKeyDoneStations)
1033 return true;
1034 }
1035 return false;
1036 }
1037
1038
wpa_receive(struct wpa_authenticator *wpa_auth, struct wpa_state_machine *sm, u8 *data, size_t data_len)1039 void wpa_receive(struct wpa_authenticator *wpa_auth,
1040 struct wpa_state_machine *sm,
1041 u8 *data, size_t data_len)
1042 {
1043 struct ieee802_1x_hdr *hdr;
1044 struct wpa_eapol_key *key;
1045 u16 key_info, key_data_length;
1046 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST } msg;
1047 char *msgtxt;
1048 struct wpa_eapol_ie_parse kde;
1049 const u8 *key_data;
1050 size_t keyhdrlen, mic_len;
1051 u8 *mic;
1052
1053 if (!wpa_auth || !wpa_auth->conf.wpa || !sm)
1054 return;
1055 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL data", data, data_len);
1056
1057 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
1058 keyhdrlen = sizeof(*key) + mic_len + 2;
1059
1060 if (data_len < sizeof(*hdr) + keyhdrlen) {
1061 wpa_printf(MSG_DEBUG, "WPA: Ignore too short EAPOL-Key frame");
1062 return;
1063 }
1064
1065 hdr = (struct ieee802_1x_hdr *) data;
1066 key = (struct wpa_eapol_key *) (hdr + 1);
1067 mic = (u8 *) (key + 1);
1068 key_info = WPA_GET_BE16(key->key_info);
1069 key_data = mic + mic_len + 2;
1070 key_data_length = WPA_GET_BE16(mic + mic_len);
1071 wpa_printf(MSG_INFO, "WPA: Received EAPOL-Key from " MACSTR_SEC
1072 " key_info=0x%x type=%u mic_len=%zu key_data_length=%u",
1073 MAC2STR_SEC(sm->addr), key_info, key->type,
1074 mic_len, key_data_length);
1075 wpa_hexdump(MSG_MSGDUMP,
1076 "WPA: EAPOL-Key header (ending before Key MIC)",
1077 key, sizeof(*key));
1078 wpa_hexdump(MSG_MSGDUMP, "WPA: EAPOL-Key Key MIC",
1079 mic, mic_len);
1080 if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) {
1081 wpa_printf(MSG_INFO,
1082 "WPA: Invalid EAPOL-Key frame - key_data overflow (%d > %zu)",
1083 key_data_length,
1084 data_len - sizeof(*hdr) - keyhdrlen);
1085 return;
1086 }
1087
1088 if (sm->wpa == WPA_VERSION_WPA2) {
1089 if (key->type == EAPOL_KEY_TYPE_WPA) {
1090 /*
1091 * Some deployed station implementations seem to send
1092 * msg 4/4 with incorrect type value in WPA2 mode.
1093 */
1094 wpa_printf(MSG_DEBUG,
1095 "Workaround: Allow EAPOL-Key with unexpected WPA type in RSN mode");
1096 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
1097 wpa_printf(MSG_DEBUG,
1098 "Ignore EAPOL-Key with unexpected type %d in RSN mode",
1099 key->type);
1100 return;
1101 }
1102 } else {
1103 if (key->type != EAPOL_KEY_TYPE_WPA) {
1104 wpa_printf(MSG_DEBUG,
1105 "Ignore EAPOL-Key with unexpected type %d in WPA mode",
1106 key->type);
1107 return;
1108 }
1109 }
1110
1111 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
1112 WPA_NONCE_LEN);
1113 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
1114 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1115
1116 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
1117 * are set */
1118
1119 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1120 wpa_printf(MSG_DEBUG, "WPA: Ignore SMK message");
1121 return;
1122 }
1123
1124 if (key_info & WPA_KEY_INFO_REQUEST) {
1125 msg = REQUEST;
1126 msgtxt = "Request";
1127 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1128 msg = GROUP_2;
1129 msgtxt = "2/2 Group";
1130 } else if (key_data_length == 0 ||
1131 (mic_len == 0 && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) &&
1132 key_data_length == AES_BLOCK_SIZE)) {
1133 msg = PAIRWISE_4;
1134 msgtxt = "4/4 Pairwise";
1135 } else {
1136 msg = PAIRWISE_2;
1137 msgtxt = "2/4 Pairwise";
1138 }
1139
1140 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
1141 msg == GROUP_2) {
1142 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1143 if (sm->pairwise == WPA_CIPHER_CCMP ||
1144 sm->pairwise == WPA_CIPHER_GCMP) {
1145 if (wpa_use_cmac(sm->wpa_key_mgmt) &&
1146 !wpa_use_akm_defined(sm->wpa_key_mgmt) &&
1147 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1148 wpa_auth_logger(wpa_auth, sm->addr,
1149 LOGGER_WARNING,
1150 "advertised support for AES-128-CMAC, but did not use it");
1151 return;
1152 }
1153
1154 if (!wpa_use_cmac(sm->wpa_key_mgmt) &&
1155 !wpa_use_akm_defined(sm->wpa_key_mgmt) &&
1156 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1157 wpa_auth_logger(wpa_auth, sm->addr,
1158 LOGGER_WARNING,
1159 "did not use HMAC-SHA1-AES with CCMP/GCMP");
1160 return;
1161 }
1162 }
1163
1164 if (wpa_use_akm_defined(sm->wpa_key_mgmt) &&
1165 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1166 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1167 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1168 return;
1169 }
1170 }
1171
1172 if (key_info & WPA_KEY_INFO_REQUEST) {
1173 if (sm->req_replay_counter_used &&
1174 os_memcmp(key->replay_counter, sm->req_replay_counter,
1175 WPA_REPLAY_COUNTER_LEN) <= 0) {
1176 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1177 "received EAPOL-Key request with replayed counter");
1178 return;
1179 }
1180 }
1181
1182 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
1183 !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
1184 int i;
1185
1186 if (msg == PAIRWISE_2 &&
1187 wpa_replay_counter_valid(sm->prev_key_replay,
1188 key->replay_counter) &&
1189 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1190 os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
1191 {
1192 /*
1193 * Some supplicant implementations (e.g., Windows XP
1194 * WZC) update SNonce for each EAPOL-Key 2/4. This
1195 * breaks the workaround on accepting any of the
1196 * pending requests, so allow the SNonce to be updated
1197 * even if we have already sent out EAPOL-Key 3/4.
1198 */
1199 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1200 "Process SNonce update from STA based on retransmitted EAPOL-Key 1/4");
1201 sm->update_snonce = 1;
1202 os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
1203 sm->alt_snonce_valid = true;
1204 os_memcpy(sm->alt_replay_counter,
1205 sm->key_replay[0].counter,
1206 WPA_REPLAY_COUNTER_LEN);
1207 goto continue_processing;
1208 }
1209
1210 if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
1211 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1212 os_memcmp(key->replay_counter, sm->alt_replay_counter,
1213 WPA_REPLAY_COUNTER_LEN) == 0) {
1214 /*
1215 * Supplicant may still be using the old SNonce since
1216 * there was two EAPOL-Key 2/4 messages and they had
1217 * different SNonce values.
1218 */
1219 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1220 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
1221 goto continue_processing;
1222 }
1223
1224 if (msg == PAIRWISE_2 &&
1225 wpa_replay_counter_valid(sm->prev_key_replay,
1226 key->replay_counter) &&
1227 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
1228 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1229 "ignore retransmitted EAPOL-Key %s - SNonce did not change",
1230 msgtxt);
1231 } else {
1232 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1233 "received EAPOL-Key %s with unexpected replay counter",
1234 msgtxt);
1235 }
1236 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
1237 if (!sm->key_replay[i].valid)
1238 break;
1239 wpa_hexdump(MSG_DEBUG, "pending replay counter",
1240 sm->key_replay[i].counter,
1241 WPA_REPLAY_COUNTER_LEN);
1242 }
1243 wpa_hexdump(MSG_DEBUG, "received replay counter",
1244 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1245 return;
1246 }
1247
1248 continue_processing:
1249 #ifdef CONFIG_FILS
1250 if (sm->wpa == WPA_VERSION_WPA2 && mic_len == 0 &&
1251 !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1252 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1253 "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
1254 return;
1255 }
1256 #endif /* CONFIG_FILS */
1257
1258 switch (msg) {
1259 case PAIRWISE_2:
1260 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
1261 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
1262 (!sm->update_snonce ||
1263 sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
1264 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1265 "received EAPOL-Key msg 2/4 in invalid state (%d) - dropped",
1266 sm->wpa_ptk_state);
1267 return;
1268 }
1269 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
1270 if (sm->group->reject_4way_hs_for_entropy) {
1271 /*
1272 * The system did not have enough entropy to generate
1273 * strong random numbers. Reject the first 4-way
1274 * handshake(s) and collect some entropy based on the
1275 * information from it. Once enough entropy is
1276 * available, the next atempt will trigger GMK/Key
1277 * Counter update and the station will be allowed to
1278 * continue.
1279 */
1280 wpa_printf(MSG_DEBUG,
1281 "WPA: Reject 4-way handshake to collect more entropy for random number generation");
1282 random_mark_pool_ready();
1283 wpa_sta_disconnect(wpa_auth, sm->addr,
1284 WLAN_REASON_PREV_AUTH_NOT_VALID);
1285 return;
1286 }
1287 break;
1288 case PAIRWISE_4:
1289 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1290 !sm->PTK_valid) {
1291 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1292 "received EAPOL-Key msg 4/4 in invalid state (%d) - dropped",
1293 sm->wpa_ptk_state);
1294 return;
1295 }
1296 break;
1297 case GROUP_2:
1298 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1299 || !sm->PTK_valid) {
1300 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1301 "received EAPOL-Key msg 2/2 in invalid state (%d) - dropped",
1302 sm->wpa_ptk_group_state);
1303 return;
1304 }
1305 break;
1306 case REQUEST:
1307 break;
1308 }
1309
1310 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1311 "received EAPOL-Key frame (%s)", msgtxt);
1312
1313 if (key_info & WPA_KEY_INFO_ACK) {
1314 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1315 "received invalid EAPOL-Key: Key Ack set");
1316 return;
1317 }
1318
1319 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1320 !(key_info & WPA_KEY_INFO_MIC)) {
1321 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1322 "received invalid EAPOL-Key: Key MIC not set");
1323 return;
1324 }
1325
1326 #ifdef CONFIG_FILS
1327 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1328 (key_info & WPA_KEY_INFO_MIC)) {
1329 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1330 "received invalid EAPOL-Key: Key MIC set");
1331 return;
1332 }
1333 #endif /* CONFIG_FILS */
1334
1335 sm->MICVerified = false;
1336 if (sm->PTK_valid && !sm->update_snonce) {
1337 if (mic_len &&
1338 wpa_verify_key_mic(sm->wpa_key_mgmt, sm->pmk_len, &sm->PTK,
1339 data, data_len) &&
1340 (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
1341 wpa_try_alt_snonce(sm, data, data_len))) {
1342 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1343 "received EAPOL-Key with invalid MIC");
1344 #ifdef TEST_FUZZ
1345 wpa_printf(MSG_INFO,
1346 "TEST: Ignore Key MIC failure for fuzz testing");
1347 goto continue_fuzz;
1348 #endif /* TEST_FUZZ */
1349 return;
1350 }
1351 #ifdef CONFIG_FILS
1352 if (!mic_len &&
1353 wpa_aead_decrypt(sm, &sm->PTK, data, data_len,
1354 &key_data_length) < 0) {
1355 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1356 "received EAPOL-Key with invalid MIC");
1357 #ifdef TEST_FUZZ
1358 wpa_printf(MSG_INFO,
1359 "TEST: Ignore Key MIC failure for fuzz testing");
1360 goto continue_fuzz;
1361 #endif /* TEST_FUZZ */
1362 return;
1363 }
1364 #endif /* CONFIG_FILS */
1365 #ifdef TEST_FUZZ
1366 continue_fuzz:
1367 #endif /* TEST_FUZZ */
1368 sm->MICVerified = true;
1369 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1370 sm->pending_1_of_4_timeout = 0;
1371 }
1372
1373 if (key_info & WPA_KEY_INFO_REQUEST) {
1374 if (sm->MICVerified) {
1375 sm->req_replay_counter_used = 1;
1376 os_memcpy(sm->req_replay_counter, key->replay_counter,
1377 WPA_REPLAY_COUNTER_LEN);
1378 } else {
1379 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1380 "received EAPOL-Key request with invalid MIC");
1381 return;
1382 }
1383
1384 /*
1385 * TODO: should decrypt key data field if encryption was used;
1386 * even though MAC address KDE is not normally encrypted,
1387 * supplicant is allowed to encrypt it.
1388 */
1389 if (key_info & WPA_KEY_INFO_ERROR) {
1390 if (wpa_receive_error_report(
1391 wpa_auth, sm,
1392 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1393 return; /* STA entry was removed */
1394 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1395 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1396 "received EAPOL-Key Request for new 4-Way Handshake");
1397 wpa_request_new_ptk(sm);
1398 } else if (key_data_length > 0 &&
1399 wpa_parse_kde_ies(key_data, key_data_length,
1400 &kde) == 0 &&
1401 kde.mac_addr) {
1402 } else {
1403 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1404 "received EAPOL-Key Request for GTK rekeying");
1405 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1406 if (wpa_auth_gtk_rekey_in_process(wpa_auth))
1407 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG,
1408 "skip new GTK rekey - already in process");
1409 else
1410 wpa_rekey_gtk(wpa_auth, NULL);
1411 }
1412 } else {
1413 /* Do not allow the same key replay counter to be reused. */
1414 wpa_replay_counter_mark_invalid(sm->key_replay,
1415 key->replay_counter);
1416
1417 if (msg == PAIRWISE_2) {
1418 /*
1419 * Maintain a copy of the pending EAPOL-Key frames in
1420 * case the EAPOL-Key frame was retransmitted. This is
1421 * needed to allow EAPOL-Key msg 2/4 reply to another
1422 * pending msg 1/4 to update the SNonce to work around
1423 * unexpected supplicant behavior.
1424 */
1425 os_memcpy(sm->prev_key_replay, sm->key_replay,
1426 sizeof(sm->key_replay));
1427 } else {
1428 os_memset(sm->prev_key_replay, 0,
1429 sizeof(sm->prev_key_replay));
1430 }
1431
1432 /*
1433 * Make sure old valid counters are not accepted anymore and
1434 * do not get copied again.
1435 */
1436 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
1437 }
1438
1439 os_free(sm->last_rx_eapol_key);
1440 sm->last_rx_eapol_key = os_memdup(data, data_len);
1441 if (!sm->last_rx_eapol_key)
1442 return;
1443 sm->last_rx_eapol_key_len = data_len;
1444
1445 sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
1446 sm->EAPOLKeyReceived = true;
1447 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1448 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1449 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1450 wpa_sm_step(sm);
1451 }
1452
1453
wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr, const u8 *gnonce, u8 *gtk, size_t gtk_len)1454 static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1455 const u8 *gnonce, u8 *gtk, size_t gtk_len)
1456 {
1457 u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + WPA_GTK_MAX_LEN];
1458 u8 *pos;
1459 int ret = 0;
1460
1461 /* GTK = PRF-X(GMK, "Group key expansion",
1462 * AA || GNonce || Time || random data)
1463 * The example described in the IEEE 802.11 standard uses only AA and
1464 * GNonce as inputs here. Add some more entropy since this derivation
1465 * is done only at the Authenticator and as such, does not need to be
1466 * exactly same.
1467 */
1468 os_memset(data, 0, sizeof(data));
1469 os_memcpy(data, addr, ETH_ALEN);
1470 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1471 pos = data + ETH_ALEN + WPA_NONCE_LEN;
1472 wpa_get_ntp_timestamp(pos);
1473 #ifdef TEST_FUZZ
1474 os_memset(pos, 0xef, 8);
1475 #endif /* TEST_FUZZ */
1476 pos += 8;
1477 if (random_get_bytes(pos, gtk_len) < 0)
1478 ret = -1;
1479
1480 #ifdef CONFIG_SHA384
1481 if (sha384_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1482 gtk, gtk_len) < 0)
1483 ret = -1;
1484 #else /* CONFIG_SHA384 */
1485 #ifdef CONFIG_SHA256
1486 if (sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1487 gtk, gtk_len) < 0)
1488 ret = -1;
1489 #else /* CONFIG_SHA256 */
1490 if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1491 gtk, gtk_len) < 0)
1492 ret = -1;
1493 #endif /* CONFIG_SHA256 */
1494 #endif /* CONFIG_SHA384 */
1495
1496 forced_memzero(data, sizeof(data));
1497
1498 return ret;
1499 }
1500
1501
wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)1502 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1503 {
1504 struct wpa_authenticator *wpa_auth = eloop_ctx;
1505 struct wpa_state_machine *sm = timeout_ctx;
1506
1507 sm->pending_1_of_4_timeout = 0;
1508 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
1509 sm->TimeoutEvt = true;
1510 wpa_sm_step(sm);
1511 }
1512
1513
__wpa_send_eapol(struct wpa_authenticator *wpa_auth, struct wpa_state_machine *sm, int key_info, const u8 *key_rsc, const u8 *nonce, const u8 *kde, size_t kde_len, int keyidx, int encr, int force_version)1514 void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1515 struct wpa_state_machine *sm, int key_info,
1516 const u8 *key_rsc, const u8 *nonce,
1517 const u8 *kde, size_t kde_len,
1518 int keyidx, int encr, int force_version)
1519 {
1520 struct wpa_auth_config *conf = &wpa_auth->conf;
1521 struct ieee802_1x_hdr *hdr;
1522 struct wpa_eapol_key *key;
1523 size_t len, mic_len, keyhdrlen;
1524 int alg;
1525 int key_data_len, pad_len = 0;
1526 u8 *buf, *pos;
1527 int version, pairwise;
1528 int i;
1529 u8 *key_mic, *key_data;
1530
1531 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
1532 keyhdrlen = sizeof(*key) + mic_len + 2;
1533
1534 len = sizeof(struct ieee802_1x_hdr) + keyhdrlen;
1535
1536 if (force_version)
1537 version = force_version;
1538 else if (wpa_use_akm_defined(sm->wpa_key_mgmt))
1539 version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
1540 else if (wpa_use_cmac(sm->wpa_key_mgmt))
1541 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
1542 else if (sm->pairwise != WPA_CIPHER_TKIP)
1543 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1544 else
1545 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1546
1547 pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1548
1549 wpa_printf(MSG_DEBUG,
1550 "WPA: Send EAPOL(version=%d secure=%d mic=%d ack=%d install=%d pairwise=%d kde_len=%zu keyidx=%d encr=%d)",
1551 version,
1552 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1553 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1554 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1555 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1556 pairwise, kde_len, keyidx, encr);
1557
1558 key_data_len = kde_len;
1559
1560 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1561 wpa_use_aes_key_wrap(sm->wpa_key_mgmt) ||
1562 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1563 pad_len = key_data_len % 8;
1564 if (pad_len)
1565 pad_len = 8 - pad_len;
1566 key_data_len += pad_len + 8;
1567 }
1568
1569 len += key_data_len;
1570 if (!mic_len && encr)
1571 len += AES_BLOCK_SIZE;
1572
1573 hdr = os_zalloc(len);
1574 if (!hdr)
1575 return;
1576 hdr->version = conf->eapol_version;
1577 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1578 hdr->length = host_to_be16(len - sizeof(*hdr));
1579 key = (struct wpa_eapol_key *) (hdr + 1);
1580 key_mic = (u8 *) (key + 1);
1581 key_data = ((u8 *) (hdr + 1)) + keyhdrlen;
1582
1583 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1584 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1585 key_info |= version;
1586 if (encr && sm->wpa == WPA_VERSION_WPA2)
1587 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1588 if (sm->wpa != WPA_VERSION_WPA2)
1589 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1590 WPA_PUT_BE16(key->key_info, key_info);
1591
1592 alg = pairwise ? sm->pairwise : conf->wpa_group;
1593 if (sm->wpa == WPA_VERSION_WPA2 && !pairwise)
1594 WPA_PUT_BE16(key->key_length, 0);
1595 else
1596 WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
1597
1598 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1599 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1600 os_memcpy(sm->key_replay[i].counter,
1601 sm->key_replay[i - 1].counter,
1602 WPA_REPLAY_COUNTER_LEN);
1603 }
1604 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1605 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1606 WPA_REPLAY_COUNTER_LEN);
1607 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
1608 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1609 sm->key_replay[0].valid = true;
1610
1611 if (nonce)
1612 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1613
1614 if (key_rsc)
1615 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1616
1617 if (kde && !encr) {
1618 os_memcpy(key_data, kde, kde_len);
1619 WPA_PUT_BE16(key_mic + mic_len, kde_len);
1620 #ifdef CONFIG_FILS
1621 } else if (!mic_len && kde) {
1622 const u8 *aad[1];
1623 size_t aad_len[1];
1624
1625 WPA_PUT_BE16(key_mic, AES_BLOCK_SIZE + kde_len);
1626 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1627 kde, kde_len);
1628
1629 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK",
1630 sm->PTK.kek, sm->PTK.kek_len);
1631 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1632 * to Key Data (exclusive). */
1633 aad[0] = (u8 *) hdr;
1634 aad_len[0] = key_mic + 2 - (u8 *) hdr;
1635 if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len, kde, kde_len,
1636 1, aad, aad_len, key_mic + 2) < 0) {
1637 wpa_printf(MSG_DEBUG, "WPA: AES-SIV encryption failed");
1638 return;
1639 }
1640
1641 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
1642 key_mic + 2, AES_BLOCK_SIZE + kde_len);
1643 #endif /* CONFIG_FILS */
1644 } else if (encr && kde) {
1645 buf = os_zalloc(key_data_len);
1646 if (!buf) {
1647 os_free(hdr);
1648 return;
1649 }
1650 pos = buf;
1651 os_memcpy(pos, kde, kde_len);
1652 pos += kde_len;
1653
1654 if (pad_len)
1655 *pos++ = 0xdd;
1656
1657 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1658 buf, key_data_len);
1659 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1660 wpa_use_aes_key_wrap(sm->wpa_key_mgmt) ||
1661 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1662 wpa_printf(MSG_DEBUG,
1663 "WPA: Encrypt Key Data using AES-WRAP (KEK length %zu)",
1664 sm->PTK.kek_len);
1665 if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len,
1666 (key_data_len - 8) / 8, buf, key_data)) {
1667 os_free(hdr);
1668 os_free(buf);
1669 return;
1670 }
1671 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
1672 #ifndef CONFIG_NO_RC4
1673 } else if (sm->PTK.kek_len == 16) {
1674 u8 ek[32];
1675
1676 wpa_printf(MSG_DEBUG,
1677 "WPA: Encrypt Key Data using RC4");
1678 os_memcpy(key->key_iv,
1679 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1680 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1681 os_memcpy(ek, key->key_iv, 16);
1682 os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len);
1683 os_memcpy(key_data, buf, key_data_len);
1684 rc4_skip(ek, 32, 256, key_data, key_data_len);
1685 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
1686 #endif /* CONFIG_NO_RC4 */
1687 } else {
1688 os_free(hdr);
1689 os_free(buf);
1690 return;
1691 }
1692 os_free(buf);
1693 }
1694
1695 if (key_info & WPA_KEY_INFO_MIC) {
1696 if (!sm->PTK_valid || !mic_len) {
1697 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1698 "PTK not valid when sending EAPOL-Key frame");
1699 os_free(hdr);
1700 return;
1701 }
1702
1703 if (wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len,
1704 sm->wpa_key_mgmt, version,
1705 (u8 *) hdr, len, key_mic) < 0) {
1706 os_free(hdr);
1707 return;
1708 }
1709 #ifdef CONFIG_TESTING_OPTIONS
1710 if (!pairwise &&
1711 conf->corrupt_gtk_rekey_mic_probability > 0.0 &&
1712 drand48() < conf->corrupt_gtk_rekey_mic_probability) {
1713 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1714 "Corrupting group EAPOL-Key Key MIC");
1715 key_mic[0]++;
1716 }
1717 #endif /* CONFIG_TESTING_OPTIONS */
1718 }
1719
1720 wpa_auth_set_eapol(wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx, 1);
1721 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1722 sm->pairwise_set);
1723 os_free(hdr);
1724 }
1725
1726
wpa_send_eapol(struct wpa_authenticator *wpa_auth, struct wpa_state_machine *sm, int key_info, const u8 *key_rsc, const u8 *nonce, const u8 *kde, size_t kde_len, int keyidx, int encr)1727 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1728 struct wpa_state_machine *sm, int key_info,
1729 const u8 *key_rsc, const u8 *nonce,
1730 const u8 *kde, size_t kde_len,
1731 int keyidx, int encr)
1732 {
1733 int timeout_ms;
1734 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1735 u32 ctr;
1736
1737 if (!sm)
1738 return;
1739
1740 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1741 keyidx, encr, 0);
1742
1743 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1744 if (ctr == 1 && wpa_auth->conf.tx_status)
1745 #if defined(CONFIG_OPEN_HARMONY_PATCH) && defined(OPEN_HARMONY_MIRACAST_SINK_OPT)
1746 timeout_ms = pairwise ? HM_EAPOL_KEY_TIMEOUT_FIRST :
1747 eapol_key_timeout_first_group;
1748 #else
1749 timeout_ms = pairwise ? eapol_key_timeout_first :
1750 eapol_key_timeout_first_group;
1751 #endif
1752 else
1753 timeout_ms = eapol_key_timeout_subseq;
1754 if (wpa_auth->conf.wpa_disable_eapol_key_retries &&
1755 (!pairwise || (key_info & WPA_KEY_INFO_MIC)))
1756 timeout_ms = eapol_key_timeout_no_retrans;
1757 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1758 sm->pending_1_of_4_timeout = 1;
1759 #ifdef TEST_FUZZ
1760 timeout_ms = 1;
1761 #endif /* TEST_FUZZ */
1762 wpa_printf(MSG_DEBUG,
1763 "WPA: Use EAPOL-Key timeout of %u ms (retry counter %u)",
1764 timeout_ms, ctr);
1765 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1766 wpa_send_eapol_timeout, wpa_auth, sm);
1767 }
1768
1769
wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK, u8 *data, size_t data_len)1770 static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK,
1771 u8 *data, size_t data_len)
1772 {
1773 struct ieee802_1x_hdr *hdr;
1774 struct wpa_eapol_key *key;
1775 u16 key_info;
1776 int ret = 0;
1777 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN], *mic_pos;
1778 size_t mic_len = wpa_mic_len(akmp, pmk_len);
1779
1780 if (data_len < sizeof(*hdr) + sizeof(*key))
1781 return -1;
1782
1783 hdr = (struct ieee802_1x_hdr *) data;
1784 key = (struct wpa_eapol_key *) (hdr + 1);
1785 mic_pos = (u8 *) (key + 1);
1786 key_info = WPA_GET_BE16(key->key_info);
1787 os_memcpy(mic, mic_pos, mic_len);
1788 os_memset(mic_pos, 0, mic_len);
1789 if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp,
1790 key_info & WPA_KEY_INFO_TYPE_MASK,
1791 data, data_len, mic_pos) ||
1792 os_memcmp_const(mic, mic_pos, mic_len) != 0)
1793 ret = -1;
1794 os_memcpy(mic_pos, mic, mic_len);
1795 return ret;
1796 }
1797
1798
wpa_remove_ptk(struct wpa_state_machine *sm)1799 void wpa_remove_ptk(struct wpa_state_machine *sm)
1800 {
1801 if (sm == NULL)
1802 return;
1803
1804 sm->PTK_valid = false;
1805 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1806
1807 wpa_auth_remove_ptksa(sm->wpa_auth, sm->addr, sm->pairwise);
1808
1809 if (wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL,
1810 0, KEY_FLAG_PAIRWISE))
1811 wpa_printf(MSG_DEBUG,
1812 "RSN: PTK removal from the driver failed");
1813 if (sm->use_ext_key_id &&
1814 wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 1, NULL,
1815 0, KEY_FLAG_PAIRWISE))
1816 wpa_printf(MSG_DEBUG,
1817 "RSN: PTK Key ID 1 removal from the driver failed");
1818 sm->pairwise_set = false;
1819 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1820 }
1821
1822
wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)1823 int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)
1824 {
1825 int remove_ptk = 1;
1826
1827 if (!sm)
1828 return -1;
1829
1830 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1831 "event %d notification", event);
1832
1833 switch (event) {
1834 case WPA_AUTH:
1835 #ifdef CONFIG_MESH
1836 /* PTKs are derived through AMPE */
1837 if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
1838 /* not mesh */
1839 break;
1840 }
1841 return 0;
1842 #endif /* CONFIG_MESH */
1843 case WPA_ASSOC:
1844 break;
1845 case WPA_DEAUTH:
1846 case WPA_DISASSOC:
1847 sm->DeauthenticationRequest = true;
1848 #ifdef CONFIG_IEEE80211R_AP
1849 os_memset(sm->PMK, 0, sizeof(sm->PMK));
1850 sm->pmk_len = 0;
1851 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
1852 sm->xxkey_len = 0;
1853 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
1854 sm->pmk_r1_len = 0;
1855 #endif /* CONFIG_IEEE80211R_AP */
1856 break;
1857 case WPA_REAUTH:
1858 case WPA_REAUTH_EAPOL:
1859 if (!sm->started) {
1860 /*
1861 * When using WPS, we may end up here if the STA
1862 * manages to re-associate without the previous STA
1863 * entry getting removed. Consequently, we need to make
1864 * sure that the WPA state machines gets initialized
1865 * properly at this point.
1866 */
1867 wpa_printf(MSG_DEBUG,
1868 "WPA state machine had not been started - initialize now");
1869 sm->started = 1;
1870 sm->Init = true;
1871 if (wpa_sm_step(sm) == 1)
1872 return 1; /* should not really happen */
1873 sm->Init = false;
1874 sm->AuthenticationRequest = true;
1875 break;
1876 }
1877
1878 if (!sm->use_ext_key_id &&
1879 sm->wpa_auth->conf.wpa_deny_ptk0_rekey) {
1880 wpa_printf(MSG_INFO,
1881 "WPA: PTK0 rekey not allowed, disconnect "
1882 MACSTR_SEC, MAC2STR_SEC(sm->addr));
1883 sm->Disconnect = true;
1884 /* Try to encourage the STA to reconnect */
1885 sm->disconnect_reason =
1886 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1887 break;
1888 }
1889
1890 if (sm->use_ext_key_id)
1891 sm->keyidx_active ^= 1; /* flip Key ID */
1892
1893 if (sm->GUpdateStationKeys) {
1894 /*
1895 * Reauthentication cancels the pending group key
1896 * update for this STA.
1897 */
1898 sm->group->GKeyDoneStations--;
1899 sm->GUpdateStationKeys = false;
1900 sm->PtkGroupInit = true;
1901 }
1902 sm->ReAuthenticationRequest = true;
1903 break;
1904 case WPA_ASSOC_FT:
1905 #ifdef CONFIG_IEEE80211R_AP
1906 wpa_printf(MSG_DEBUG,
1907 "FT: Retry PTK configuration after association");
1908 wpa_ft_install_ptk(sm, 1);
1909
1910 /* Using FT protocol, not WPA auth state machine */
1911 sm->ft_completed = 1;
1912 wpa_auth_set_ptk_rekey_timer(sm);
1913 return 0;
1914 #else /* CONFIG_IEEE80211R_AP */
1915 break;
1916 #endif /* CONFIG_IEEE80211R_AP */
1917 case WPA_ASSOC_FILS:
1918 #ifdef CONFIG_FILS
1919 wpa_printf(MSG_DEBUG,
1920 "FILS: TK configuration after association");
1921 fils_set_tk(sm);
1922 sm->fils_completed = 1;
1923 return 0;
1924 #else /* CONFIG_FILS */
1925 break;
1926 #endif /* CONFIG_FILS */
1927 case WPA_DRV_STA_REMOVED:
1928 sm->tk_already_set = false;
1929 return 0;
1930 }
1931
1932 #ifdef CONFIG_IEEE80211R_AP
1933 sm->ft_completed = 0;
1934 #endif /* CONFIG_IEEE80211R_AP */
1935
1936 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1937 remove_ptk = 0;
1938 #ifdef CONFIG_FILS
1939 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1940 (event == WPA_AUTH || event == WPA_ASSOC))
1941 remove_ptk = 0;
1942 #endif /* CONFIG_FILS */
1943
1944 if (remove_ptk) {
1945 sm->PTK_valid = false;
1946 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1947
1948 if (event != WPA_REAUTH_EAPOL)
1949 wpa_remove_ptk(sm);
1950 }
1951
1952 if (sm->in_step_loop) {
1953 /*
1954 * wpa_sm_step() is already running - avoid recursive call to
1955 * it by making the existing loop process the new update.
1956 */
1957 sm->changed = true;
1958 return 0;
1959 }
1960 return wpa_sm_step(sm);
1961 }
1962
1963
SM_STATEnull1964 SM_STATE(WPA_PTK, INITIALIZE)
1965 {
1966 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1967 if (sm->Init) {
1968 /* Init flag is not cleared here, so avoid busy
1969 * loop by claiming nothing changed. */
1970 sm->changed = false;
1971 }
1972
1973 sm->keycount = 0;
1974 if (sm->GUpdateStationKeys)
1975 sm->group->GKeyDoneStations--;
1976 sm->GUpdateStationKeys = false;
1977 if (sm->wpa == WPA_VERSION_WPA)
1978 sm->PInitAKeys = false;
1979 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1980 * Local AA > Remote AA)) */) {
1981 sm->Pair = true;
1982 }
1983 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1984 wpa_remove_ptk(sm);
1985 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1986 sm->TimeoutCtr = 0;
1987 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
1988 sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP ||
1989 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
1990 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1991 WPA_EAPOL_authorized, 0);
1992 }
1993 }
1994
1995
SM_STATEnull1996 SM_STATE(WPA_PTK, DISCONNECT)
1997 {
1998 u16 reason = sm->disconnect_reason;
1999
2000 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
2001 sm->Disconnect = false;
2002 sm->disconnect_reason = 0;
2003 if (!reason)
2004 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
2005 wpa_sta_disconnect(sm->wpa_auth, sm->addr, reason);
2006 }
2007
2008
SM_STATEnull2009 SM_STATE(WPA_PTK, DISCONNECTED)
2010 {
2011 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
2012 sm->DeauthenticationRequest = false;
2013 }
2014
2015
SM_STATEnull2016 SM_STATE(WPA_PTK, AUTHENTICATION)
2017 {
2018 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
2019 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
2020 sm->PTK_valid = false;
2021 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
2022 1);
2023 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
2024 sm->AuthenticationRequest = false;
2025 }
2026
2027
wpa_group_ensure_init(struct wpa_authenticator *wpa_auth, struct wpa_group *group)2028 static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
2029 struct wpa_group *group)
2030 {
2031 if (group->first_sta_seen)
2032 return;
2033 /*
2034 * System has run bit further than at the time hostapd was started
2035 * potentially very early during boot up. This provides better chances
2036 * of collecting more randomness on embedded systems. Re-initialize the
2037 * GMK and Counter here to improve their strength if there was not
2038 * enough entropy available immediately after system startup.
2039 */
2040 wpa_printf(MSG_DEBUG,
2041 "WPA: Re-initialize GMK/Counter on first station");
2042 if (random_pool_ready() != 1) {
2043 wpa_printf(MSG_INFO,
2044 "WPA: Not enough entropy in random pool to proceed - reject first 4-way handshake");
2045 group->reject_4way_hs_for_entropy = true;
2046 } else {
2047 group->first_sta_seen = true;
2048 group->reject_4way_hs_for_entropy = false;
2049 }
2050
2051 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 ||
2052 wpa_gtk_update(wpa_auth, group) < 0 ||
2053 wpa_group_config_group_keys(wpa_auth, group) < 0) {
2054 wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed");
2055 group->first_sta_seen = false;
2056 group->reject_4way_hs_for_entropy = true;
2057 }
2058 }
2059
2060
SM_STATEnull2061 SM_STATE(WPA_PTK, AUTHENTICATION2)
2062 {
2063 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
2064
2065 wpa_group_ensure_init(sm->wpa_auth, sm->group);
2066 sm->ReAuthenticationRequest = false;
2067
2068 /*
2069 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
2070 * ambiguous. The Authenticator state machine uses a counter that is
2071 * incremented by one for each 4-way handshake. However, the security
2072 * analysis of 4-way handshake points out that unpredictable nonces
2073 * help in preventing precomputation attacks. Instead of the state
2074 * machine definition, use an unpredictable nonce value here to provide
2075 * stronger protection against potential precomputation attacks.
2076 */
2077 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
2078 wpa_printf(MSG_ERROR,
2079 "WPA: Failed to get random data for ANonce.");
2080 sm->Disconnect = true;
2081 return;
2082 }
2083 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
2084 WPA_NONCE_LEN);
2085 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
2086 * logical place than INITIALIZE since AUTHENTICATION2 can be
2087 * re-entered on ReAuthenticationRequest without going through
2088 * INITIALIZE. */
2089 sm->TimeoutCtr = 0;
2090 }
2091
2092
wpa_auth_sm_ptk_update(struct wpa_state_machine *sm)2093 static int wpa_auth_sm_ptk_update(struct wpa_state_machine *sm)
2094 {
2095 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
2096 wpa_printf(MSG_ERROR,
2097 "WPA: Failed to get random data for ANonce");
2098 sm->Disconnect = true;
2099 return -1;
2100 }
2101 wpa_hexdump(MSG_DEBUG, "WPA: Assign new ANonce", sm->ANonce,
2102 WPA_NONCE_LEN);
2103 sm->TimeoutCtr = 0;
2104 return 0;
2105 }
2106
2107
SM_STATEnull2108 SM_STATE(WPA_PTK, INITPMK)
2109 {
2110 u8 msk[2 * PMK_LEN];
2111 size_t len = 2 * PMK_LEN;
2112
2113 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
2114 #ifdef CONFIG_IEEE80211R_AP
2115 sm->xxkey_len = 0;
2116 #endif /* CONFIG_IEEE80211R_AP */
2117 if (sm->pmksa) {
2118 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
2119 os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
2120 sm->pmk_len = sm->pmksa->pmk_len;
2121 #ifdef CONFIG_DPP
2122 } else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP) {
2123 wpa_printf(MSG_DEBUG,
2124 "DPP: No PMKSA cache entry for STA - reject connection");
2125 sm->Disconnect = true;
2126 sm->disconnect_reason = WLAN_REASON_INVALID_PMKID;
2127 return;
2128 #endif /* CONFIG_DPP */
2129 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
2130 unsigned int pmk_len;
2131
2132 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt))
2133 pmk_len = PMK_LEN_SUITE_B_192;
2134 else
2135 pmk_len = PMK_LEN;
2136 wpa_printf(MSG_DEBUG,
2137 "WPA: PMK from EAPOL state machine (MSK len=%zu PMK len=%u)",
2138 len, pmk_len);
2139 if (len < pmk_len) {
2140 wpa_printf(MSG_DEBUG,
2141 "WPA: MSK not long enough (%zu) to create PMK (%u)",
2142 len, pmk_len);
2143 sm->Disconnect = true;
2144 return;
2145 }
2146 os_memcpy(sm->PMK, msk, pmk_len);
2147 sm->pmk_len = pmk_len;
2148 #ifdef CONFIG_IEEE80211R_AP
2149 if (len >= 2 * PMK_LEN) {
2150 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
2151 os_memcpy(sm->xxkey, msk, SHA384_MAC_LEN);
2152 sm->xxkey_len = SHA384_MAC_LEN;
2153 } else {
2154 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
2155 sm->xxkey_len = PMK_LEN;
2156 }
2157 }
2158 #endif /* CONFIG_IEEE80211R_AP */
2159 } else {
2160 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p",
2161 sm->wpa_auth->cb->get_msk);
2162 sm->Disconnect = true;
2163 return;
2164 }
2165 forced_memzero(msk, sizeof(msk));
2166
2167 sm->req_replay_counter_used = 0;
2168 /* IEEE 802.11i does not set keyRun to false, but not doing this
2169 * will break reauthentication since EAPOL state machines may not be
2170 * get into AUTHENTICATING state that clears keyRun before WPA state
2171 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
2172 * state and takes PMK from the previously used AAA Key. This will
2173 * eventually fail in 4-Way Handshake because Supplicant uses PMK
2174 * derived from the new AAA Key. Setting keyRun = false here seems to
2175 * be good workaround for this issue. */
2176 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, false);
2177 }
2178
2179
SM_STATEnull2180 SM_STATE(WPA_PTK, INITPSK)
2181 {
2182 const u8 *psk;
2183 size_t psk_len;
2184
2185 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
2186 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL,
2187 &psk_len, NULL);
2188 if (psk) {
2189 os_memcpy(sm->PMK, psk, psk_len);
2190 sm->pmk_len = psk_len;
2191 #ifdef CONFIG_IEEE80211R_AP
2192 os_memcpy(sm->xxkey, psk, PMK_LEN);
2193 sm->xxkey_len = PMK_LEN;
2194 #endif /* CONFIG_IEEE80211R_AP */
2195 }
2196 #ifdef CONFIG_SAE
2197 if (wpa_auth_uses_sae(sm) && sm->pmksa) {
2198 wpa_printf(MSG_DEBUG, "SAE: PMK from PMKSA cache");
2199 os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
2200 sm->pmk_len = sm->pmksa->pmk_len;
2201 #ifdef CONFIG_IEEE80211R_AP
2202 os_memcpy(sm->xxkey, sm->pmksa->pmk, sm->pmksa->pmk_len);
2203 sm->xxkey_len = sm->pmksa->pmk_len;
2204 #endif /* CONFIG_IEEE80211R_AP */
2205 }
2206 #endif /* CONFIG_SAE */
2207 sm->req_replay_counter_used = 0;
2208 }
2209
2210
SM_STATEnull2211 SM_STATE(WPA_PTK, PTKSTART)
2212 {
2213 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
2214 size_t pmkid_len = 0;
2215
2216 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
2217 sm->PTKRequest = false;
2218 sm->TimeoutEvt = false;
2219 sm->alt_snonce_valid = false;
2220
2221 sm->TimeoutCtr++;
2222 if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
2223 /* No point in sending the EAPOL-Key - we will disconnect
2224 * immediately following this. */
2225 return;
2226 }
2227
2228 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2229 "sending 1/4 msg of 4-Way Handshake");
2230 /*
2231 * For infrastructure BSS cases, it is better for the AP not to include
2232 * the PMKID KDE in EAPOL-Key msg 1/4 since it could be used to initiate
2233 * offline search for the passphrase/PSK without having to be able to
2234 * capture a 4-way handshake from a STA that has access to the network.
2235 *
2236 * For IBSS cases, addition of PMKID KDE could be considered even with
2237 * WPA2-PSK cases that use multiple PSKs, but only if there is a single
2238 * possible PSK for this STA. However, this should not be done unless
2239 * there is support for using that information on the supplicant side.
2240 * The concern about exposing PMKID unnecessarily in infrastructure BSS
2241 * cases would also apply here, but at least in the IBSS case, this
2242 * would cover a potential real use case.
2243 */
2244 if (sm->wpa == WPA_VERSION_WPA2 &&
2245 (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) ||
2246 (sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE && sm->pmksa) ||
2247 wpa_key_mgmt_sae(sm->wpa_key_mgmt)) &&
2248 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
2249 pmkid = buf;
2250 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
2251 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
2252 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
2253 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
2254 if (sm->pmksa) {
2255 wpa_hexdump(MSG_DEBUG,
2256 "RSN: Message 1/4 PMKID from PMKSA entry",
2257 sm->pmksa->pmkid, PMKID_LEN);
2258 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2259 sm->pmksa->pmkid, PMKID_LEN);
2260 } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
2261 /* No KCK available to derive PMKID */
2262 wpa_printf(MSG_DEBUG,
2263 "RSN: No KCK available to derive PMKID for message 1/4");
2264 pmkid = NULL;
2265 #ifdef CONFIG_FILS
2266 } else if (wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2267 if (sm->pmkid_set) {
2268 wpa_hexdump(MSG_DEBUG,
2269 "RSN: Message 1/4 PMKID from FILS/ERP",
2270 sm->pmkid, PMKID_LEN);
2271 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2272 sm->pmkid, PMKID_LEN);
2273 } else {
2274 /* No PMKID available */
2275 wpa_printf(MSG_DEBUG,
2276 "RSN: No FILS/ERP PMKID available for message 1/4");
2277 pmkid = NULL;
2278 }
2279 #endif /* CONFIG_FILS */
2280 #ifdef CONFIG_IEEE80211R_AP
2281 } else if (wpa_key_mgmt_ft(sm->wpa_key_mgmt) &&
2282 sm->ft_completed) {
2283 wpa_printf(MSG_DEBUG,
2284 "FT: No PMKID in message 1/4 when using FT protocol");
2285 pmkid = NULL;
2286 #endif /* CONFIG_IEEE80211R_AP */
2287 #ifdef CONFIG_SAE
2288 } else if (wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
2289 if (sm->pmkid_set) {
2290 wpa_hexdump(MSG_DEBUG,
2291 "RSN: Message 1/4 PMKID from SAE",
2292 sm->pmkid, PMKID_LEN);
2293 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2294 sm->pmkid, PMKID_LEN);
2295 } else {
2296 /* No PMKID available */
2297 wpa_printf(MSG_DEBUG,
2298 "RSN: No SAE PMKID available for message 1/4");
2299 pmkid = NULL;
2300 }
2301 #endif /* CONFIG_SAE */
2302 } else {
2303 /*
2304 * Calculate PMKID since no PMKSA cache entry was
2305 * available with pre-calculated PMKID.
2306 */
2307 rsn_pmkid(sm->PMK, sm->pmk_len, sm->wpa_auth->addr,
2308 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
2309 sm->wpa_key_mgmt);
2310 wpa_hexdump(MSG_DEBUG,
2311 "RSN: Message 1/4 PMKID derived from PMK",
2312 &pmkid[2 + RSN_SELECTOR_LEN], PMKID_LEN);
2313 }
2314 }
2315 if (!pmkid)
2316 pmkid_len = 0;
2317 wpa_send_eapol(sm->wpa_auth, sm,
2318 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
2319 sm->ANonce, pmkid, pmkid_len, 0, 0);
2320 }
2321
2322
wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce, const u8 *pmk, unsigned int pmk_len, struct wpa_ptk *ptk, int force_sha256)2323 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
2324 const u8 *pmk, unsigned int pmk_len,
2325 struct wpa_ptk *ptk, int force_sha256)
2326 {
2327 const u8 *z = NULL;
2328 size_t z_len = 0, kdk_len;
2329 int akmp;
2330
2331 if (sm->wpa_auth->conf.force_kdk_derivation ||
2332 (sm->wpa_auth->conf.secure_ltf &&
2333 ieee802_11_rsnx_capab(sm->rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
2334 kdk_len = WPA_KDK_MAX_LEN;
2335 else
2336 kdk_len = 0;
2337
2338 #ifdef CONFIG_IEEE80211R_AP
2339 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2340 if (sm->ft_completed) {
2341 u8 ptk_name[WPA_PMK_NAME_LEN];
2342
2343 return wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->pmk_r1_len,
2344 sm->SNonce, sm->ANonce,
2345 sm->addr, sm->wpa_auth->addr,
2346 sm->pmk_r1_name,
2347 ptk, ptk_name,
2348 sm->wpa_key_mgmt,
2349 sm->pairwise,
2350 kdk_len);
2351 }
2352 return wpa_auth_derive_ptk_ft(sm, ptk);
2353 }
2354 #endif /* CONFIG_IEEE80211R_AP */
2355
2356 #ifdef CONFIG_DPP2
2357 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
2358 z = wpabuf_head(sm->dpp_z);
2359 z_len = wpabuf_len(sm->dpp_z);
2360 }
2361 #endif /* CONFIG_DPP2 */
2362
2363 akmp = sm->wpa_key_mgmt;
2364 if (force_sha256)
2365 akmp |= WPA_KEY_MGMT_PSK_SHA256;
2366 return wpa_pmk_to_ptk(pmk, pmk_len, "Pairwise key expansion",
2367 sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce,
2368 ptk, akmp, sm->pairwise, z, z_len, kdk_len);
2369 }
2370
2371
2372 #ifdef CONFIG_FILS
2373
fils_auth_pmk_to_ptk(struct wpa_state_machine *sm, const u8 *pmk, size_t pmk_len, const u8 *snonce, const u8 *anonce, const u8 *dhss, size_t dhss_len, struct wpabuf *g_sta, struct wpabuf *g_ap)2374 int fils_auth_pmk_to_ptk(struct wpa_state_machine *sm, const u8 *pmk,
2375 size_t pmk_len, const u8 *snonce, const u8 *anonce,
2376 const u8 *dhss, size_t dhss_len,
2377 struct wpabuf *g_sta, struct wpabuf *g_ap)
2378 {
2379 u8 ick[FILS_ICK_MAX_LEN];
2380 size_t ick_len;
2381 int res;
2382 u8 fils_ft[FILS_FT_MAX_LEN];
2383 size_t fils_ft_len = 0, kdk_len;
2384
2385 if (sm->wpa_auth->conf.force_kdk_derivation ||
2386 (sm->wpa_auth->conf.secure_ltf &&
2387 ieee802_11_rsnx_capab(sm->rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
2388 kdk_len = WPA_KDK_MAX_LEN;
2389 else
2390 kdk_len = 0;
2391
2392 res = fils_pmk_to_ptk(pmk, pmk_len, sm->addr, sm->wpa_auth->addr,
2393 snonce, anonce, dhss, dhss_len,
2394 &sm->PTK, ick, &ick_len,
2395 sm->wpa_key_mgmt, sm->pairwise,
2396 fils_ft, &fils_ft_len, kdk_len);
2397 if (res < 0)
2398 return res;
2399 sm->PTK_valid = true;
2400 sm->tk_already_set = false;
2401
2402 #ifdef CONFIG_IEEE80211R_AP
2403 if (fils_ft_len) {
2404 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2405 struct wpa_auth_config *conf = &wpa_auth->conf;
2406 u8 pmk_r0[PMK_LEN_MAX], pmk_r0_name[WPA_PMK_NAME_LEN];
2407 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
2408
2409 if (wpa_derive_pmk_r0(fils_ft, fils_ft_len,
2410 conf->ssid, conf->ssid_len,
2411 conf->mobility_domain,
2412 conf->r0_key_holder,
2413 conf->r0_key_holder_len,
2414 sm->addr, pmk_r0, pmk_r0_name,
2415 use_sha384) < 0)
2416 return -1;
2417
2418 wpa_ft_store_pmk_fils(sm, pmk_r0, pmk_r0_name);
2419 forced_memzero(fils_ft, sizeof(fils_ft));
2420
2421 res = wpa_derive_pmk_r1_name(pmk_r0_name, conf->r1_key_holder,
2422 sm->addr, sm->pmk_r1_name,
2423 use_sha384);
2424 forced_memzero(pmk_r0, PMK_LEN_MAX);
2425 if (res < 0)
2426 return -1;
2427 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", sm->pmk_r1_name,
2428 WPA_PMK_NAME_LEN);
2429 sm->pmk_r1_name_valid = 1;
2430 }
2431 #endif /* CONFIG_IEEE80211R_AP */
2432
2433 res = fils_key_auth_sk(ick, ick_len, snonce, anonce,
2434 sm->addr, sm->wpa_auth->addr,
2435 g_sta ? wpabuf_head(g_sta) : NULL,
2436 g_sta ? wpabuf_len(g_sta) : 0,
2437 g_ap ? wpabuf_head(g_ap) : NULL,
2438 g_ap ? wpabuf_len(g_ap) : 0,
2439 sm->wpa_key_mgmt, sm->fils_key_auth_sta,
2440 sm->fils_key_auth_ap,
2441 &sm->fils_key_auth_len);
2442 forced_memzero(ick, sizeof(ick));
2443
2444 /* Store nonces for (Re)Association Request/Response frame processing */
2445 os_memcpy(sm->SNonce, snonce, FILS_NONCE_LEN);
2446 os_memcpy(sm->ANonce, anonce, FILS_NONCE_LEN);
2447
2448 return res;
2449 }
2450
2451
wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk, u8 *buf, size_t buf_len, u16 *_key_data_len)2452 static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
2453 u8 *buf, size_t buf_len, u16 *_key_data_len)
2454 {
2455 struct ieee802_1x_hdr *hdr;
2456 struct wpa_eapol_key *key;
2457 u8 *pos;
2458 u16 key_data_len;
2459 u8 *tmp;
2460 const u8 *aad[1];
2461 size_t aad_len[1];
2462
2463 hdr = (struct ieee802_1x_hdr *) buf;
2464 key = (struct wpa_eapol_key *) (hdr + 1);
2465 pos = (u8 *) (key + 1);
2466 key_data_len = WPA_GET_BE16(pos);
2467 if (key_data_len < AES_BLOCK_SIZE ||
2468 key_data_len > buf_len - sizeof(*hdr) - sizeof(*key) - 2) {
2469 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2470 "No room for AES-SIV data in the frame");
2471 return -1;
2472 }
2473 pos += 2; /* Pointing at the Encrypted Key Data field */
2474
2475 tmp = os_malloc(key_data_len);
2476 if (!tmp)
2477 return -1;
2478
2479 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2480 * to Key Data (exclusive). */
2481 aad[0] = buf;
2482 aad_len[0] = pos - buf;
2483 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, key_data_len,
2484 1, aad, aad_len, tmp) < 0) {
2485 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2486 "Invalid AES-SIV data in the frame");
2487 bin_clear_free(tmp, key_data_len);
2488 return -1;
2489 }
2490
2491 /* AEAD decryption and validation completed successfully */
2492 key_data_len -= AES_BLOCK_SIZE;
2493 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2494 tmp, key_data_len);
2495
2496 /* Replace Key Data field with the decrypted version */
2497 os_memcpy(pos, tmp, key_data_len);
2498 pos -= 2; /* Key Data Length field */
2499 WPA_PUT_BE16(pos, key_data_len);
2500 bin_clear_free(tmp, key_data_len);
2501 if (_key_data_len)
2502 *_key_data_len = key_data_len;
2503 return 0;
2504 }
2505
2506
wpa_fils_validate_fils_session(struct wpa_state_machine *sm, const u8 *ies, size_t ies_len, const u8 *fils_session)2507 const u8 * wpa_fils_validate_fils_session(struct wpa_state_machine *sm,
2508 const u8 *ies, size_t ies_len,
2509 const u8 *fils_session)
2510 {
2511 const u8 *ie, *end;
2512 const u8 *session = NULL;
2513
2514 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2515 wpa_printf(MSG_DEBUG,
2516 "FILS: Not a FILS AKM - reject association");
2517 return NULL;
2518 }
2519
2520 /* Verify Session element */
2521 ie = ies;
2522 end = ((const u8 *) ie) + ies_len;
2523 while (ie + 1 < end) {
2524 if (ie + 2 + ie[1] > end)
2525 break;
2526 if (ie[0] == WLAN_EID_EXTENSION &&
2527 ie[1] >= 1 + FILS_SESSION_LEN &&
2528 ie[2] == WLAN_EID_EXT_FILS_SESSION) {
2529 session = ie;
2530 break;
2531 }
2532 ie += 2 + ie[1];
2533 }
2534
2535 if (!session) {
2536 wpa_printf(MSG_DEBUG,
2537 "FILS: %s: Could not find FILS Session element in Assoc Req - reject",
2538 __func__);
2539 return NULL;
2540 }
2541
2542 if (!fils_session) {
2543 wpa_printf(MSG_DEBUG,
2544 "FILS: %s: Could not find FILS Session element in STA entry - reject",
2545 __func__);
2546 return NULL;
2547 }
2548
2549 if (os_memcmp(fils_session, session + 3, FILS_SESSION_LEN) != 0) {
2550 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
2551 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
2552 fils_session, FILS_SESSION_LEN);
2553 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
2554 session + 3, FILS_SESSION_LEN);
2555 return NULL;
2556 }
2557 return session;
2558 }
2559
2560
wpa_fils_validate_key_confirm(struct wpa_state_machine *sm, const u8 *ies, size_t ies_len)2561 int wpa_fils_validate_key_confirm(struct wpa_state_machine *sm, const u8 *ies,
2562 size_t ies_len)
2563 {
2564 struct ieee802_11_elems elems;
2565
2566 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
2567 wpa_printf(MSG_DEBUG,
2568 "FILS: Failed to parse decrypted elements");
2569 return -1;
2570 }
2571
2572 if (!elems.fils_session) {
2573 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
2574 return -1;
2575 }
2576
2577 if (!elems.fils_key_confirm) {
2578 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
2579 return -1;
2580 }
2581
2582 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
2583 wpa_printf(MSG_DEBUG,
2584 "FILS: Unexpected Key-Auth length %d (expected %zu)",
2585 elems.fils_key_confirm_len,
2586 sm->fils_key_auth_len);
2587 return -1;
2588 }
2589
2590 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_sta,
2591 sm->fils_key_auth_len) != 0) {
2592 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
2593 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
2594 elems.fils_key_confirm, elems.fils_key_confirm_len);
2595 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
2596 sm->fils_key_auth_sta, sm->fils_key_auth_len);
2597 return -1;
2598 }
2599
2600 return 0;
2601 }
2602
2603
fils_decrypt_assoc(struct wpa_state_machine *sm, const u8 *fils_session, const struct ieee80211_mgmt *mgmt, size_t frame_len, u8 *pos, size_t left)2604 int fils_decrypt_assoc(struct wpa_state_machine *sm, const u8 *fils_session,
2605 const struct ieee80211_mgmt *mgmt, size_t frame_len,
2606 u8 *pos, size_t left)
2607 {
2608 u16 fc, stype;
2609 const u8 *end, *ie_start, *ie, *session, *crypt;
2610 const u8 *aad[5];
2611 size_t aad_len[5];
2612
2613 if (!sm || !sm->PTK_valid) {
2614 wpa_printf(MSG_DEBUG,
2615 "FILS: No KEK to decrypt Assocication Request frame");
2616 return -1;
2617 }
2618
2619 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2620 wpa_printf(MSG_DEBUG,
2621 "FILS: Not a FILS AKM - reject association");
2622 return -1;
2623 }
2624
2625 end = ((const u8 *) mgmt) + frame_len;
2626 fc = le_to_host16(mgmt->frame_control);
2627 stype = WLAN_FC_GET_STYPE(fc);
2628 if (stype == WLAN_FC_STYPE_REASSOC_REQ)
2629 ie_start = mgmt->u.reassoc_req.variable;
2630 else
2631 ie_start = mgmt->u.assoc_req.variable;
2632 ie = ie_start;
2633
2634 /*
2635 * Find FILS Session element which is the last unencrypted element in
2636 * the frame.
2637 */
2638 session = wpa_fils_validate_fils_session(sm, ie, end - ie,
2639 fils_session);
2640 if (!session) {
2641 wpa_printf(MSG_DEBUG, "FILS: Session validation failed");
2642 return -1;
2643 }
2644
2645 crypt = session + 2 + session[1];
2646
2647 if (end - crypt < AES_BLOCK_SIZE) {
2648 wpa_printf(MSG_DEBUG,
2649 "FILS: Too short frame to include AES-SIV data");
2650 return -1;
2651 }
2652
2653 /* AES-SIV AAD vectors */
2654
2655 /* The STA's MAC address */
2656 aad[0] = mgmt->sa;
2657 aad_len[0] = ETH_ALEN;
2658 /* The AP's BSSID */
2659 aad[1] = mgmt->da;
2660 aad_len[1] = ETH_ALEN;
2661 /* The STA's nonce */
2662 aad[2] = sm->SNonce;
2663 aad_len[2] = FILS_NONCE_LEN;
2664 /* The AP's nonce */
2665 aad[3] = sm->ANonce;
2666 aad_len[3] = FILS_NONCE_LEN;
2667 /*
2668 * The (Re)Association Request frame from the Capability Information
2669 * field to the FILS Session element (both inclusive).
2670 */
2671 aad[4] = (const u8 *) &mgmt->u.assoc_req.capab_info;
2672 aad_len[4] = crypt - aad[4];
2673
2674 if (aes_siv_decrypt(sm->PTK.kek, sm->PTK.kek_len, crypt, end - crypt,
2675 5, aad, aad_len, pos + (crypt - ie_start)) < 0) {
2676 wpa_printf(MSG_DEBUG,
2677 "FILS: Invalid AES-SIV data in the frame");
2678 return -1;
2679 }
2680 wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Request elements",
2681 pos, left - AES_BLOCK_SIZE);
2682
2683 if (wpa_fils_validate_key_confirm(sm, pos, left - AES_BLOCK_SIZE) < 0) {
2684 wpa_printf(MSG_DEBUG, "FILS: Key Confirm validation failed");
2685 return -1;
2686 }
2687
2688 return left - AES_BLOCK_SIZE;
2689 }
2690
2691
fils_encrypt_assoc(struct wpa_state_machine *sm, u8 *buf, size_t current_len, size_t max_len, const struct wpabuf *hlp)2692 int fils_encrypt_assoc(struct wpa_state_machine *sm, u8 *buf,
2693 size_t current_len, size_t max_len,
2694 const struct wpabuf *hlp)
2695 {
2696 u8 *end = buf + max_len;
2697 u8 *pos = buf + current_len;
2698 struct ieee80211_mgmt *mgmt;
2699 struct wpabuf *plain;
2700 const u8 *aad[5];
2701 size_t aad_len[5];
2702
2703 if (!sm || !sm->PTK_valid)
2704 return -1;
2705
2706 wpa_hexdump(MSG_DEBUG,
2707 "FILS: Association Response frame before FILS processing",
2708 buf, current_len);
2709
2710 mgmt = (struct ieee80211_mgmt *) buf;
2711
2712 /* AES-SIV AAD vectors */
2713
2714 /* The AP's BSSID */
2715 aad[0] = mgmt->sa;
2716 aad_len[0] = ETH_ALEN;
2717 /* The STA's MAC address */
2718 aad[1] = mgmt->da;
2719 aad_len[1] = ETH_ALEN;
2720 /* The AP's nonce */
2721 aad[2] = sm->ANonce;
2722 aad_len[2] = FILS_NONCE_LEN;
2723 /* The STA's nonce */
2724 aad[3] = sm->SNonce;
2725 aad_len[3] = FILS_NONCE_LEN;
2726 /*
2727 * The (Re)Association Response frame from the Capability Information
2728 * field (the same offset in both Association and Reassociation
2729 * Response frames) to the FILS Session element (both inclusive).
2730 */
2731 aad[4] = (const u8 *) &mgmt->u.assoc_resp.capab_info;
2732 aad_len[4] = pos - aad[4];
2733
2734 /* The following elements will be encrypted with AES-SIV */
2735 plain = fils_prepare_plainbuf(sm, hlp);
2736 if (!plain) {
2737 wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
2738 return -1;
2739 }
2740
2741 if (pos + wpabuf_len(plain) + AES_BLOCK_SIZE > end) {
2742 wpa_printf(MSG_DEBUG,
2743 "FILS: Not enough room for FILS elements");
2744 wpabuf_clear_free(plain);
2745 return -1;
2746 }
2747
2748 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: Association Response plaintext",
2749 plain);
2750
2751 if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len,
2752 wpabuf_head(plain), wpabuf_len(plain),
2753 5, aad, aad_len, pos) < 0) {
2754 wpabuf_clear_free(plain);
2755 return -1;
2756 }
2757
2758 wpa_hexdump(MSG_DEBUG,
2759 "FILS: Encrypted Association Response elements",
2760 pos, AES_BLOCK_SIZE + wpabuf_len(plain));
2761 current_len += wpabuf_len(plain) + AES_BLOCK_SIZE;
2762 wpabuf_clear_free(plain);
2763
2764 sm->fils_completed = 1;
2765
2766 return current_len;
2767 }
2768
2769
fils_prepare_plainbuf(struct wpa_state_machine *sm, const struct wpabuf *hlp)2770 static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm,
2771 const struct wpabuf *hlp)
2772 {
2773 struct wpabuf *plain;
2774 u8 *len, *tmp, *tmp2;
2775 u8 hdr[2];
2776 u8 *gtk, stub_gtk[32];
2777 size_t gtk_len;
2778 struct wpa_group *gsm;
2779 size_t plain_len;
2780 struct wpa_auth_config *conf = &sm->wpa_auth->conf;
2781
2782 plain_len = 1000 + ieee80211w_kde_len(sm);
2783 if (conf->transition_disable)
2784 plain_len += 2 + RSN_SELECTOR_LEN + 1;
2785 plain = wpabuf_alloc(plain_len);
2786 if (!plain)
2787 return NULL;
2788
2789 /* TODO: FILS Public Key */
2790
2791 /* FILS Key Confirmation */
2792 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2793 wpabuf_put_u8(plain, 1 + sm->fils_key_auth_len); /* Length */
2794 /* Element ID Extension */
2795 wpabuf_put_u8(plain, WLAN_EID_EXT_FILS_KEY_CONFIRM);
2796 wpabuf_put_data(plain, sm->fils_key_auth_ap, sm->fils_key_auth_len);
2797
2798 /* FILS HLP Container */
2799 if (hlp)
2800 wpabuf_put_buf(plain, hlp);
2801
2802 /* TODO: FILS IP Address Assignment */
2803
2804 /* Key Delivery */
2805 gsm = sm->group;
2806 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2807 len = wpabuf_put(plain, 1);
2808 wpabuf_put_u8(plain, WLAN_EID_EXT_KEY_DELIVERY);
2809 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN,
2810 wpabuf_put(plain, WPA_KEY_RSC_LEN));
2811 /* GTK KDE */
2812 gtk = gsm->GTK[gsm->GN - 1];
2813 gtk_len = gsm->GTK_len;
2814 if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
2815 /*
2816 * Provide unique random GTK to each STA to prevent use
2817 * of GTK in the BSS.
2818 */
2819 if (random_get_bytes(stub_gtk, gtk_len) < 0) {
2820 wpabuf_clear_free(plain);
2821 return NULL;
2822 }
2823 gtk = stub_gtk;
2824 }
2825 hdr[0] = gsm->GN & 0x03;
2826 hdr[1] = 0;
2827 tmp = wpabuf_put(plain, 0);
2828 tmp2 = wpa_add_kde(tmp, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2829 gtk, gtk_len);
2830 wpabuf_put(plain, tmp2 - tmp);
2831
2832 /* IGTK KDE and BIGTK KDE */
2833 tmp = wpabuf_put(plain, 0);
2834 tmp2 = ieee80211w_kde_add(sm, tmp);
2835 wpabuf_put(plain, tmp2 - tmp);
2836
2837 if (conf->transition_disable) {
2838 tmp = wpabuf_put(plain, 0);
2839 tmp2 = wpa_add_kde(tmp, WFA_KEY_DATA_TRANSITION_DISABLE,
2840 &conf->transition_disable, 1, NULL, 0);
2841 wpabuf_put(plain, tmp2 - tmp);
2842 }
2843
2844 *len = (u8 *) wpabuf_put(plain, 0) - len - 1;
2845
2846 #ifdef CONFIG_OCV
2847 if (wpa_auth_uses_ocv(sm)) {
2848 struct wpa_channel_info ci;
2849 u8 *pos;
2850
2851 if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
2852 wpa_printf(MSG_WARNING,
2853 "FILS: Failed to get channel info for OCI element");
2854 wpabuf_clear_free(plain);
2855 return NULL;
2856 }
2857 #ifdef CONFIG_TESTING_OPTIONS
2858 if (conf->oci_freq_override_fils_assoc) {
2859 wpa_printf(MSG_INFO,
2860 "TEST: Override OCI frequency %d -> %u MHz",
2861 ci.frequency,
2862 conf->oci_freq_override_fils_assoc);
2863 ci.frequency = conf->oci_freq_override_fils_assoc;
2864 }
2865 #endif /* CONFIG_TESTING_OPTIONS */
2866
2867 pos = wpabuf_put(plain, OCV_OCI_EXTENDED_LEN);
2868 if (ocv_insert_extended_oci(&ci, pos) < 0) {
2869 wpabuf_clear_free(plain);
2870 return NULL;
2871 }
2872 }
2873 #endif /* CONFIG_OCV */
2874
2875 return plain;
2876 }
2877
2878
fils_set_tk(struct wpa_state_machine *sm)2879 int fils_set_tk(struct wpa_state_machine *sm)
2880 {
2881 enum wpa_alg alg;
2882 int klen;
2883
2884 if (!sm || !sm->PTK_valid) {
2885 wpa_printf(MSG_DEBUG, "FILS: No valid PTK available to set TK");
2886 return -1;
2887 }
2888 if (sm->tk_already_set) {
2889 wpa_printf(MSG_DEBUG, "FILS: TK already set to the driver");
2890 return -1;
2891 }
2892
2893 alg = wpa_cipher_to_alg(sm->pairwise);
2894 klen = wpa_cipher_key_len(sm->pairwise);
2895
2896 wpa_printf(MSG_DEBUG, "FILS: Configure TK to the driver");
2897 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2898 sm->PTK.tk, klen, KEY_FLAG_PAIRWISE_RX_TX)) {
2899 wpa_printf(MSG_DEBUG, "FILS: Failed to set TK to the driver");
2900 return -1;
2901 }
2902 sm->tk_already_set = true;
2903
2904 wpa_auth_store_ptksa(sm->wpa_auth, sm->addr, sm->pairwise,
2905 dot11RSNAConfigPMKLifetime, &sm->PTK);
2906
2907 return 0;
2908 }
2909
2910
hostapd_eid_assoc_fils_session(struct wpa_state_machine *sm, u8 *buf, const u8 *fils_session, struct wpabuf *hlp)2911 u8 * hostapd_eid_assoc_fils_session(struct wpa_state_machine *sm, u8 *buf,
2912 const u8 *fils_session, struct wpabuf *hlp)
2913 {
2914 struct wpabuf *plain;
2915 u8 *pos = buf;
2916
2917 /* FILS Session */
2918 *pos++ = WLAN_EID_EXTENSION; /* Element ID */
2919 *pos++ = 1 + FILS_SESSION_LEN; /* Length */
2920 *pos++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */
2921 os_memcpy(pos, fils_session, FILS_SESSION_LEN);
2922 pos += FILS_SESSION_LEN;
2923
2924 plain = fils_prepare_plainbuf(sm, hlp);
2925 if (!plain) {
2926 wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
2927 return NULL;
2928 }
2929
2930 os_memcpy(pos, wpabuf_head(plain), wpabuf_len(plain));
2931 pos += wpabuf_len(plain);
2932
2933 wpa_printf(MSG_DEBUG, "%s: plain buf_len: %zu", __func__,
2934 wpabuf_len(plain));
2935 wpabuf_clear_free(plain);
2936 sm->fils_completed = 1;
2937 return pos;
2938 }
2939
2940 #endif /* CONFIG_FILS */
2941
2942
2943 #ifdef CONFIG_OCV
get_sta_tx_parameters(struct wpa_state_machine *sm, int ap_max_chanwidth, int ap_seg1_idx, int *bandwidth, int *seg1_idx)2944 int get_sta_tx_parameters(struct wpa_state_machine *sm, int ap_max_chanwidth,
2945 int ap_seg1_idx, int *bandwidth, int *seg1_idx)
2946 {
2947 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2948
2949 if (!wpa_auth->cb->get_sta_tx_params)
2950 return -1;
2951 return wpa_auth->cb->get_sta_tx_params(wpa_auth->cb_ctx, sm->addr,
2952 ap_max_chanwidth, ap_seg1_idx,
2953 bandwidth, seg1_idx);
2954 }
2955 #endif /* CONFIG_OCV */
2956
2957
SM_STATEnull2958 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
2959 {
2960 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2961 struct wpa_ptk PTK;
2962 int ok = 0, psk_found = 0;
2963 const u8 *pmk = NULL;
2964 size_t pmk_len;
2965 int ft;
2966 const u8 *eapol_key_ie, *key_data, *mic;
2967 u16 key_data_length;
2968 size_t mic_len, eapol_key_ie_len;
2969 struct ieee802_1x_hdr *hdr;
2970 struct wpa_eapol_key *key;
2971 struct wpa_eapol_ie_parse kde;
2972 int vlan_id = 0;
2973 int owe_ptk_workaround = !!wpa_auth->conf.owe_ptk_workaround;
2974
2975 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
2976 sm->EAPOLKeyReceived = false;
2977 sm->update_snonce = false;
2978 os_memset(&PTK, 0, sizeof(PTK));
2979
2980 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
2981
2982 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2983 * WPA-PSK: iterate through possible PSKs and select the one matching
2984 * the packet */
2985 for (;;) {
2986 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
2987 !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
2988 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
2989 sm->p2p_dev_addr, pmk, &pmk_len,
2990 &vlan_id);
2991 if (!pmk)
2992 break;
2993 psk_found = 1;
2994 #ifdef CONFIG_IEEE80211R_AP
2995 if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
2996 os_memcpy(sm->xxkey, pmk, pmk_len);
2997 sm->xxkey_len = pmk_len;
2998 }
2999 #endif /* CONFIG_IEEE80211R_AP */
3000 } else {
3001 pmk = sm->PMK;
3002 pmk_len = sm->pmk_len;
3003 }
3004
3005 if ((!pmk || !pmk_len) && sm->pmksa) {
3006 wpa_printf(MSG_DEBUG, "WPA: Use PMK from PMKSA cache");
3007 pmk = sm->pmksa->pmk;
3008 pmk_len = sm->pmksa->pmk_len;
3009 }
3010
3011 if (wpa_derive_ptk(sm, sm->SNonce, pmk, pmk_len, &PTK,
3012 owe_ptk_workaround == 2) < 0)
3013 break;
3014
3015 if (mic_len &&
3016 wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK,
3017 sm->last_rx_eapol_key,
3018 sm->last_rx_eapol_key_len) == 0) {
3019 if (sm->PMK != pmk) {
3020 os_memcpy(sm->PMK, pmk, pmk_len);
3021 sm->pmk_len = pmk_len;
3022 }
3023 ok = 1;
3024 break;
3025 }
3026
3027 #ifdef CONFIG_FILS
3028 if (!mic_len &&
3029 wpa_aead_decrypt(sm, &PTK, sm->last_rx_eapol_key,
3030 sm->last_rx_eapol_key_len, NULL) == 0) {
3031 ok = 1;
3032 break;
3033 }
3034 #endif /* CONFIG_FILS */
3035
3036 #ifdef CONFIG_OWE
3037 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE && pmk_len > 32 &&
3038 owe_ptk_workaround == 1) {
3039 wpa_printf(MSG_DEBUG,
3040 "OWE: Try PTK derivation workaround with SHA256");
3041 owe_ptk_workaround = 2;
3042 continue;
3043 }
3044 #endif /* CONFIG_OWE */
3045
3046 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
3047 wpa_key_mgmt_sae(sm->wpa_key_mgmt))
3048 break;
3049 }
3050
3051 if (!ok) {
3052 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3053 "invalid MIC in msg 2/4 of 4-Way Handshake");
3054 if (psk_found)
3055 wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr);
3056 return;
3057 }
3058
3059 /*
3060 * Note: last_rx_eapol_key length fields have already been validated in
3061 * wpa_receive().
3062 */
3063 hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
3064 key = (struct wpa_eapol_key *) (hdr + 1);
3065 mic = (u8 *) (key + 1);
3066 key_data = mic + mic_len + 2;
3067 key_data_length = WPA_GET_BE16(mic + mic_len);
3068 if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
3069 sizeof(*key) - mic_len - 2)
3070 return;
3071
3072 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
3073 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
3074 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
3075 return;
3076 }
3077 if (kde.rsn_ie) {
3078 eapol_key_ie = kde.rsn_ie;
3079 eapol_key_ie_len = kde.rsn_ie_len;
3080 } else if (kde.osen) {
3081 eapol_key_ie = kde.osen;
3082 eapol_key_ie_len = kde.osen_len;
3083 } else {
3084 eapol_key_ie = kde.wpa_ie;
3085 eapol_key_ie_len = kde.wpa_ie_len;
3086 }
3087 ft = sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt);
3088 if (!sm->wpa_ie ||
3089 wpa_compare_rsn_ie(ft, sm->wpa_ie, sm->wpa_ie_len,
3090 eapol_key_ie, eapol_key_ie_len)) {
3091 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
3092 "WPA IE from (Re)AssocReq did not match with msg 2/4");
3093 if (sm->wpa_ie) {
3094 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
3095 sm->wpa_ie, sm->wpa_ie_len);
3096 }
3097 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
3098 eapol_key_ie, eapol_key_ie_len);
3099 /* MLME-DEAUTHENTICATE.request */
3100 wpa_sta_disconnect(wpa_auth, sm->addr,
3101 WLAN_REASON_PREV_AUTH_NOT_VALID);
3102 return;
3103 }
3104 if ((!sm->rsnxe && kde.rsnxe) ||
3105 (sm->rsnxe && !kde.rsnxe) ||
3106 (sm->rsnxe && kde.rsnxe &&
3107 (sm->rsnxe_len != kde.rsnxe_len ||
3108 os_memcmp(sm->rsnxe, kde.rsnxe, sm->rsnxe_len) != 0))) {
3109 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
3110 "RSNXE from (Re)AssocReq did not match the one in EAPOL-Key msg 2/4");
3111 wpa_hexdump(MSG_DEBUG, "RSNXE in AssocReq",
3112 sm->rsnxe, sm->rsnxe_len);
3113 wpa_hexdump(MSG_DEBUG, "RSNXE in EAPOL-Key msg 2/4",
3114 kde.rsnxe, kde.rsnxe_len);
3115 /* MLME-DEAUTHENTICATE.request */
3116 wpa_sta_disconnect(wpa_auth, sm->addr,
3117 WLAN_REASON_PREV_AUTH_NOT_VALID);
3118 return;
3119 }
3120 #ifdef CONFIG_OCV
3121 if (wpa_auth_uses_ocv(sm)) {
3122 struct wpa_channel_info ci;
3123 int tx_chanwidth;
3124 int tx_seg1_idx;
3125 enum oci_verify_result res;
3126
3127 if (wpa_channel_info(wpa_auth, &ci) != 0) {
3128 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
3129 "Failed to get channel info to validate received OCI in EAPOL-Key 2/4");
3130 return;
3131 }
3132
3133 if (get_sta_tx_parameters(sm,
3134 channel_width_to_int(ci.chanwidth),
3135 ci.seg1_idx, &tx_chanwidth,
3136 &tx_seg1_idx) < 0)
3137 return;
3138
3139 res = ocv_verify_tx_params(kde.oci, kde.oci_len, &ci,
3140 tx_chanwidth, tx_seg1_idx);
3141 if (wpa_auth_uses_ocv(sm) == 2 && res == OCI_NOT_FOUND) {
3142 /* Work around misbehaving STAs */
3143 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
3144 "Disable OCV with a STA that does not send OCI");
3145 wpa_auth_set_ocv(sm, 0);
3146 } else if (res != OCI_SUCCESS) {
3147 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
3148 "OCV failed: %s", ocv_errorstr);
3149 if (wpa_auth->conf.msg_ctx)
3150 wpa_msg(wpa_auth->conf.msg_ctx, MSG_INFO,
3151 OCV_FAILURE "addr=" MACSTR
3152 " frame=eapol-key-m2 error=%s",
3153 MAC2STR(sm->addr), ocv_errorstr);
3154 return;
3155 }
3156 }
3157 #endif /* CONFIG_OCV */
3158 #ifdef CONFIG_IEEE80211R_AP
3159 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
3160 wpa_sta_disconnect(wpa_auth, sm->addr,
3161 WLAN_REASON_PREV_AUTH_NOT_VALID);
3162 return;
3163 }
3164 #endif /* CONFIG_IEEE80211R_AP */
3165 #ifdef CONFIG_P2P
3166 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
3167 wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
3168 int idx;
3169 wpa_printf(MSG_DEBUG,
3170 "P2P: IP address requested in EAPOL-Key exchange");
3171 idx = bitfield_get_first_zero(wpa_auth->ip_pool);
3172 if (idx >= 0) {
3173 u32 start = WPA_GET_BE32(wpa_auth->conf.ip_addr_start);
3174 bitfield_set(wpa_auth->ip_pool, idx);
3175 WPA_PUT_BE32(sm->ip_addr, start + idx);
3176 wpa_printf(MSG_DEBUG,
3177 "P2P: Assigned IP address %u.%u.%u.%u to "
3178 MACSTR_SEC, sm->ip_addr[0], sm->ip_addr[1],
3179 sm->ip_addr[2], sm->ip_addr[3],
3180 MAC2STR_SEC(sm->addr));
3181 }
3182 }
3183 #endif /* CONFIG_P2P */
3184
3185 #ifdef CONFIG_DPP2
3186 if (DPP_VERSION > 1 && kde.dpp_kde) {
3187 wpa_printf(MSG_DEBUG,
3188 "DPP: peer Protocol Version %u Flags 0x%x",
3189 kde.dpp_kde[0], kde.dpp_kde[1]);
3190 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP &&
3191 wpa_auth->conf.dpp_pfs != 2 &&
3192 (kde.dpp_kde[1] & DPP_KDE_PFS_ALLOWED) &&
3193 !sm->dpp_z) {
3194 wpa_printf(MSG_INFO,
3195 "DPP: Peer indicated it supports PFS and local configuration allows this, but PFS was not negotiated for the association");
3196 wpa_sta_disconnect(wpa_auth, sm->addr,
3197 WLAN_REASON_PREV_AUTH_NOT_VALID);
3198 return;
3199 }
3200 }
3201 #endif /* CONFIG_DPP2 */
3202
3203 #ifdef CONFIG_IEEE80211R_AP
3204 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3205 /*
3206 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
3207 * with the value we derived.
3208 */
3209 if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
3210 WPA_PMK_NAME_LEN) != 0) {
3211 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3212 "PMKR1Name mismatch in FT 4-way handshake");
3213 wpa_hexdump(MSG_DEBUG,
3214 "FT: PMKR1Name from Supplicant",
3215 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
3216 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
3217 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
3218 return;
3219 }
3220 }
3221 #endif /* CONFIG_IEEE80211R_AP */
3222
3223 if (vlan_id && wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
3224 wpa_auth_update_vlan(wpa_auth, sm->addr, vlan_id) < 0) {
3225 wpa_sta_disconnect(wpa_auth, sm->addr,
3226 WLAN_REASON_PREV_AUTH_NOT_VALID);
3227 return;
3228 }
3229
3230 sm->pending_1_of_4_timeout = 0;
3231 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
3232
3233 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) && sm->PMK != pmk) {
3234 /* PSK may have changed from the previous choice, so update
3235 * state machine data based on whatever PSK was selected here.
3236 */
3237 os_memcpy(sm->PMK, pmk, PMK_LEN);
3238 sm->pmk_len = PMK_LEN;
3239 }
3240
3241 sm->MICVerified = true;
3242
3243 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
3244 forced_memzero(&PTK, sizeof(PTK));
3245 sm->PTK_valid = true;
3246 }
3247
3248
SM_STATEnull3249 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
3250 {
3251 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
3252 sm->TimeoutCtr = 0;
3253 }
3254
3255
ieee80211w_kde_len(struct wpa_state_machine *sm)3256 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
3257 {
3258 size_t len = 0;
3259
3260 if (sm->mgmt_frame_prot) {
3261 len += 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN;
3262 len += wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
3263 }
3264 if (sm->mgmt_frame_prot && sm->wpa_auth->conf.beacon_prot) {
3265 len += 2 + RSN_SELECTOR_LEN + WPA_BIGTK_KDE_PREFIX_LEN;
3266 len += wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
3267 }
3268
3269 return len;
3270 }
3271
3272
ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)3273 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
3274 {
3275 struct wpa_igtk_kde igtk;
3276 struct wpa_bigtk_kde bigtk;
3277 struct wpa_group *gsm = sm->group;
3278 u8 rsc[WPA_KEY_RSC_LEN];
3279 struct wpa_auth_config *conf = &sm->wpa_auth->conf;
3280 size_t len = wpa_cipher_key_len(conf->group_mgmt_cipher);
3281
3282 if (!sm->mgmt_frame_prot)
3283 return pos;
3284
3285 igtk.keyid[0] = gsm->GN_igtk;
3286 igtk.keyid[1] = 0;
3287 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
3288 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
3289 os_memset(igtk.pn, 0, sizeof(igtk.pn));
3290 else
3291 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
3292 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
3293 if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
3294 /*
3295 * Provide unique random IGTK to each STA to prevent use of
3296 * IGTK in the BSS.
3297 */
3298 if (random_get_bytes(igtk.igtk, len) < 0)
3299 return pos;
3300 }
3301 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
3302 (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
3303 NULL, 0);
3304
3305 if (!conf->beacon_prot)
3306 return pos;
3307
3308 bigtk.keyid[0] = gsm->GN_bigtk;
3309 bigtk.keyid[1] = 0;
3310 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
3311 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_bigtk, rsc) < 0)
3312 os_memset(bigtk.pn, 0, sizeof(bigtk.pn));
3313 else
3314 os_memcpy(bigtk.pn, rsc, sizeof(bigtk.pn));
3315 os_memcpy(bigtk.bigtk, gsm->BIGTK[gsm->GN_bigtk - 6], len);
3316 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
3317 /*
3318 * Provide unique random BIGTK to each OSEN STA to prevent use
3319 * of BIGTK in the BSS.
3320 */
3321 if (random_get_bytes(bigtk.bigtk, len) < 0)
3322 return pos;
3323 }
3324 pos = wpa_add_kde(pos, RSN_KEY_DATA_BIGTK,
3325 (const u8 *) &bigtk, WPA_BIGTK_KDE_PREFIX_LEN + len,
3326 NULL, 0);
3327
3328 return pos;
3329 }
3330
3331
ocv_oci_len(struct wpa_state_machine *sm)3332 static int ocv_oci_len(struct wpa_state_machine *sm)
3333 {
3334 #ifdef CONFIG_OCV
3335 if (wpa_auth_uses_ocv(sm))
3336 return OCV_OCI_KDE_LEN;
3337 #endif /* CONFIG_OCV */
3338 return 0;
3339 }
3340
3341
ocv_oci_add(struct wpa_state_machine *sm, u8 **argpos, unsigned int freq)3342 static int ocv_oci_add(struct wpa_state_machine *sm, u8 **argpos,
3343 unsigned int freq)
3344 {
3345 #ifdef CONFIG_OCV
3346 struct wpa_channel_info ci;
3347
3348 if (!wpa_auth_uses_ocv(sm))
3349 return 0;
3350
3351 if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
3352 wpa_printf(MSG_WARNING,
3353 "Failed to get channel info for OCI element");
3354 return -1;
3355 }
3356 #ifdef CONFIG_TESTING_OPTIONS
3357 if (freq) {
3358 wpa_printf(MSG_INFO,
3359 "TEST: Override OCI KDE frequency %d -> %u MHz",
3360 ci.frequency, freq);
3361 ci.frequency = freq;
3362 }
3363 #endif /* CONFIG_TESTING_OPTIONS */
3364
3365 return ocv_insert_oci_kde(&ci, argpos);
3366 #else /* CONFIG_OCV */
3367 return 0;
3368 #endif /* CONFIG_OCV */
3369 }
3370
3371
3372 #ifdef CONFIG_TESTING_OPTIONS
replace_ie(const char *name, const u8 *old_buf, size_t *len, u8 eid, const u8 *ie, size_t ie_len)3373 static u8 * replace_ie(const char *name, const u8 *old_buf, size_t *len, u8 eid,
3374 const u8 *ie, size_t ie_len)
3375 {
3376 const u8 *elem;
3377 u8 *buf;
3378
3379 wpa_printf(MSG_DEBUG, "TESTING: %s EAPOL override", name);
3380 wpa_hexdump(MSG_DEBUG, "TESTING: wpa_ie before override",
3381 old_buf, *len);
3382 buf = os_malloc(*len + ie_len);
3383 if (!buf)
3384 return NULL;
3385 os_memcpy(buf, old_buf, *len);
3386 elem = get_ie(buf, *len, eid);
3387 if (elem) {
3388 u8 elem_len = 2 + elem[1];
3389
3390 os_memmove((void *) elem, elem + elem_len,
3391 *len - (elem - buf) - elem_len);
3392 *len -= elem_len;
3393 }
3394 os_memcpy(buf + *len, ie, ie_len);
3395 *len += ie_len;
3396 wpa_hexdump(MSG_DEBUG, "TESTING: wpa_ie after EAPOL override",
3397 buf, *len);
3398
3399 return buf;
3400 }
3401 #endif /* CONFIG_TESTING_OPTIONS */
3402
3403
SM_STATEnull3404 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
3405 {
3406 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde = NULL, *pos, stub_gtk[32];
3407 size_t gtk_len, kde_len, wpa_ie_len;
3408 struct wpa_group *gsm = sm->group;
3409 u8 *wpa_ie;
3410 int secure, gtkidx, encr = 0;
3411 u8 *wpa_ie_buf = NULL, *wpa_ie_buf2 = NULL;
3412 u8 hdr[2];
3413 struct wpa_auth_config *conf = &sm->wpa_auth->conf;
3414
3415 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
3416 sm->TimeoutEvt = false;
3417
3418 sm->TimeoutCtr++;
3419 if (conf->wpa_disable_eapol_key_retries && sm->TimeoutCtr > 1) {
3420 /* Do not allow retransmission of EAPOL-Key msg 3/4 */
3421 return;
3422 }
3423 if (sm->TimeoutCtr > conf->wpa_pairwise_update_count) {
3424 /* No point in sending the EAPOL-Key - we will disconnect
3425 * immediately following this. */
3426 return;
3427 }
3428
3429 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
3430 GTK[GN], IGTK, [BIGTK], [FTIE], [TIE * 2])
3431 */
3432 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
3433 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
3434 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
3435 wpa_ie = sm->wpa_auth->wpa_ie;
3436 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
3437 if (sm->wpa == WPA_VERSION_WPA && (conf->wpa & WPA_PROTO_RSN) &&
3438 wpa_ie_len > wpa_ie[1] + 2U && wpa_ie[0] == WLAN_EID_RSN) {
3439 /* WPA-only STA, remove RSN IE and possible MDIE */
3440 wpa_ie = wpa_ie + wpa_ie[1] + 2;
3441 if (wpa_ie[0] == WLAN_EID_RSNX)
3442 wpa_ie = wpa_ie + wpa_ie[1] + 2;
3443 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
3444 wpa_ie = wpa_ie + wpa_ie[1] + 2;
3445 wpa_ie_len = wpa_ie[1] + 2;
3446 }
3447 #ifdef CONFIG_TESTING_OPTIONS
3448 if (conf->rsne_override_eapol_set) {
3449 wpa_ie_buf2 = replace_ie(
3450 "RSNE", wpa_ie, &wpa_ie_len, WLAN_EID_RSN,
3451 conf->rsne_override_eapol,
3452 conf->rsne_override_eapol_len);
3453 if (!wpa_ie_buf2)
3454 goto done;
3455 wpa_ie = wpa_ie_buf2;
3456 }
3457 if (conf->rsnxe_override_eapol_set) {
3458 wpa_ie_buf = replace_ie(
3459 "RSNXE", wpa_ie, &wpa_ie_len, WLAN_EID_RSNX,
3460 conf->rsnxe_override_eapol,
3461 conf->rsnxe_override_eapol_len);
3462 if (!wpa_ie_buf)
3463 goto done;
3464 wpa_ie = wpa_ie_buf;
3465 }
3466 #endif /* CONFIG_TESTING_OPTIONS */
3467 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3468 "sending 3/4 msg of 4-Way Handshake");
3469 if (sm->wpa == WPA_VERSION_WPA2) {
3470 if (sm->use_ext_key_id && sm->TimeoutCtr == 1 &&
3471 wpa_auth_set_key(sm->wpa_auth, 0,
3472 wpa_cipher_to_alg(sm->pairwise),
3473 sm->addr,
3474 sm->keyidx_active, sm->PTK.tk,
3475 wpa_cipher_key_len(sm->pairwise),
3476 KEY_FLAG_PAIRWISE_RX)) {
3477 wpa_sta_disconnect(sm->wpa_auth, sm->addr,
3478 WLAN_REASON_PREV_AUTH_NOT_VALID);
3479 return;
3480 }
3481
3482 /* WPA2 send GTK in the 4-way handshake */
3483 secure = 1;
3484 gtk = gsm->GTK[gsm->GN - 1];
3485 gtk_len = gsm->GTK_len;
3486 if (conf->disable_gtk ||
3487 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
3488 /*
3489 * Provide unique random GTK to each STA to prevent use
3490 * of GTK in the BSS.
3491 */
3492 if (random_get_bytes(stub_gtk, gtk_len) < 0)
3493 goto done;
3494 gtk = stub_gtk;
3495 }
3496 gtkidx = gsm->GN;
3497 _rsc = rsc;
3498 encr = 1;
3499 } else {
3500 /* WPA does not include GTK in msg 3/4 */
3501 secure = 0;
3502 gtk = NULL;
3503 gtk_len = 0;
3504 _rsc = NULL;
3505 if (sm->rx_eapol_key_secure) {
3506 /*
3507 * It looks like Windows 7 supplicant tries to use
3508 * Secure bit in msg 2/4 after having reported Michael
3509 * MIC failure and it then rejects the 4-way handshake
3510 * if msg 3/4 does not set Secure bit. Work around this
3511 * by setting the Secure bit here even in the case of
3512 * WPA if the supplicant used it first.
3513 */
3514 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3515 "STA used Secure bit in WPA msg 2/4 - set Secure for 3/4 as workaround");
3516 secure = 1;
3517 }
3518 }
3519
3520 kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
3521
3522 if (sm->use_ext_key_id)
3523 kde_len += 2 + RSN_SELECTOR_LEN + 2;
3524
3525 if (gtk)
3526 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
3527 #ifdef CONFIG_IEEE80211R_AP
3528 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3529 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
3530 kde_len += 300; /* FTIE + 2 * TIE */
3531 }
3532 #endif /* CONFIG_IEEE80211R_AP */
3533 #ifdef CONFIG_P2P
3534 if (WPA_GET_BE32(sm->ip_addr) > 0)
3535 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
3536 #endif /* CONFIG_P2P */
3537
3538 if (conf->transition_disable)
3539 kde_len += 2 + RSN_SELECTOR_LEN + 1;
3540
3541 #ifdef CONFIG_DPP2
3542 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP)
3543 kde_len += 2 + RSN_SELECTOR_LEN + 2;
3544 #endif /* CONFIG_DPP2 */
3545
3546 kde = os_malloc(kde_len);
3547 if (!kde)
3548 goto done;
3549
3550 pos = kde;
3551 os_memcpy(pos, wpa_ie, wpa_ie_len);
3552 pos += wpa_ie_len;
3553 #ifdef CONFIG_IEEE80211R_AP
3554 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3555 int res;
3556 size_t elen;
3557
3558 elen = pos - kde;
3559 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
3560 if (res < 0) {
3561 wpa_printf(MSG_ERROR,
3562 "FT: Failed to insert PMKR1Name into RSN IE in EAPOL-Key data");
3563 goto done;
3564 }
3565 pos -= wpa_ie_len;
3566 pos += elen;
3567 }
3568 #endif /* CONFIG_IEEE80211R_AP */
3569 hdr[1] = 0;
3570
3571 if (sm->use_ext_key_id) {
3572 hdr[0] = sm->keyidx_active & 0x01;
3573 pos = wpa_add_kde(pos, RSN_KEY_DATA_KEYID, hdr, 2, NULL, 0);
3574 }
3575
3576 if (gtk) {
3577 hdr[0] = gtkidx & 0x03;
3578 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
3579 gtk, gtk_len);
3580 }
3581 pos = ieee80211w_kde_add(sm, pos);
3582 if (ocv_oci_add(sm, &pos, conf->oci_freq_override_eapol_m3) < 0)
3583 goto done;
3584
3585 #ifdef CONFIG_IEEE80211R_AP
3586 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3587 int res;
3588
3589 if (sm->assoc_resp_ftie &&
3590 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
3591 os_memcpy(pos, sm->assoc_resp_ftie,
3592 2 + sm->assoc_resp_ftie[1]);
3593 res = 2 + sm->assoc_resp_ftie[1];
3594 } else {
3595 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
3596
3597 res = wpa_write_ftie(conf, use_sha384,
3598 conf->r0_key_holder,
3599 conf->r0_key_holder_len,
3600 NULL, NULL, pos,
3601 kde + kde_len - pos,
3602 NULL, 0, 0);
3603 }
3604 if (res < 0) {
3605 wpa_printf(MSG_ERROR,
3606 "FT: Failed to insert FTIE into EAPOL-Key Key Data");
3607 goto done;
3608 }
3609 pos += res;
3610
3611 /* TIE[ReassociationDeadline] (TU) */
3612 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
3613 *pos++ = 5;
3614 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
3615 WPA_PUT_LE32(pos, conf->reassociation_deadline);
3616 pos += 4;
3617
3618 /* TIE[KeyLifetime] (seconds) */
3619 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
3620 *pos++ = 5;
3621 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
3622 WPA_PUT_LE32(pos, conf->r0_key_lifetime);
3623 pos += 4;
3624 }
3625 #endif /* CONFIG_IEEE80211R_AP */
3626 #ifdef CONFIG_P2P
3627 if (WPA_GET_BE32(sm->ip_addr) > 0) {
3628 u8 addr[3 * 4];
3629 os_memcpy(addr, sm->ip_addr, 4);
3630 os_memcpy(addr + 4, conf->ip_addr_mask, 4);
3631 os_memcpy(addr + 8, conf->ip_addr_go, 4);
3632 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
3633 addr, sizeof(addr), NULL, 0);
3634 }
3635 #endif /* CONFIG_P2P */
3636
3637 if (conf->transition_disable)
3638 pos = wpa_add_kde(pos, WFA_KEY_DATA_TRANSITION_DISABLE,
3639 &conf->transition_disable, 1, NULL, 0);
3640
3641 #ifdef CONFIG_DPP2
3642 if (DPP_VERSION > 1 && sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP) {
3643 u8 payload[2];
3644
3645 payload[0] = DPP_VERSION; /* Protocol Version */
3646 payload[1] = 0; /* Flags */
3647 if (conf->dpp_pfs == 0)
3648 payload[1] |= DPP_KDE_PFS_ALLOWED;
3649 else if (conf->dpp_pfs == 1)
3650 payload[1] |= DPP_KDE_PFS_ALLOWED |
3651 DPP_KDE_PFS_REQUIRED;
3652 pos = wpa_add_kde(pos, WFA_KEY_DATA_DPP,
3653 payload, sizeof(payload), NULL, 0);
3654 }
3655 #endif /* CONFIG_DPP2 */
3656
3657 wpa_send_eapol(sm->wpa_auth, sm,
3658 (secure ? WPA_KEY_INFO_SECURE : 0) |
3659 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
3660 WPA_KEY_INFO_MIC : 0) |
3661 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
3662 WPA_KEY_INFO_KEY_TYPE,
3663 _rsc, sm->ANonce, kde, pos - kde, 0, encr);
3664 done:
3665 os_free(kde);
3666 os_free(wpa_ie_buf);
3667 os_free(wpa_ie_buf2);
3668 }
3669
3670
SM_STATEnull3671 SM_STATE(WPA_PTK, PTKINITDONE)
3672 {
3673 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
3674 sm->EAPOLKeyReceived = false;
3675 if (sm->Pair) {
3676 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
3677 int klen = wpa_cipher_key_len(sm->pairwise);
3678 int res;
3679
3680 if (sm->use_ext_key_id)
3681 res = wpa_auth_set_key(sm->wpa_auth, 0, 0, sm->addr,
3682 sm->keyidx_active, NULL, 0,
3683 KEY_FLAG_PAIRWISE_RX_TX_MODIFY);
3684 else
3685 res = wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr,
3686 0, sm->PTK.tk, klen,
3687 KEY_FLAG_PAIRWISE_RX_TX);
3688 if (res) {
3689 wpa_sta_disconnect(sm->wpa_auth, sm->addr,
3690 WLAN_REASON_PREV_AUTH_NOT_VALID);
3691 return;
3692 }
3693 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
3694 sm->pairwise_set = true;
3695
3696 wpa_auth_set_ptk_rekey_timer(sm);
3697 wpa_auth_store_ptksa(sm->wpa_auth, sm->addr, sm->pairwise,
3698 dot11RSNAConfigPMKLifetime, &sm->PTK);
3699
3700 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
3701 sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP ||
3702 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
3703 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
3704 WPA_EAPOL_authorized, 1);
3705 }
3706 }
3707
3708 if (0 /* IBSS == TRUE */) {
3709 sm->keycount++;
3710 if (sm->keycount == 2) {
3711 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
3712 WPA_EAPOL_portValid, 1);
3713 }
3714 } else {
3715 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
3716 1);
3717 }
3718 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable,
3719 false);
3720 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, true);
3721 if (sm->wpa == WPA_VERSION_WPA)
3722 sm->PInitAKeys = true;
3723 else
3724 sm->has_GTK = true;
3725 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3726 "pairwise key handshake completed (%s)",
3727 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
3728 wpa_msg(sm->wpa_auth->conf.msg_ctx, MSG_INFO, "EAPOL-4WAY-HS-COMPLETED "
3729 MACSTR, MAC2STR(sm->addr));
3730
3731 #ifdef CONFIG_IEEE80211R_AP
3732 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
3733 #endif /* CONFIG_IEEE80211R_AP */
3734 }
3735
3736
SM_STEPnull3737 SM_STEP(WPA_PTK)
3738 {
3739 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3740 struct wpa_auth_config *conf = &wpa_auth->conf;
3741
3742 if (sm->Init)
3743 SM_ENTER(WPA_PTK, INITIALIZE);
3744 else if (sm->Disconnect
3745 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
3746 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
3747 "WPA_PTK: sm->Disconnect");
3748 SM_ENTER(WPA_PTK, DISCONNECT);
3749 }
3750 else if (sm->DeauthenticationRequest)
3751 SM_ENTER(WPA_PTK, DISCONNECTED);
3752 else if (sm->AuthenticationRequest)
3753 SM_ENTER(WPA_PTK, AUTHENTICATION);
3754 else if (sm->ReAuthenticationRequest)
3755 SM_ENTER(WPA_PTK, AUTHENTICATION2);
3756 else if (sm->PTKRequest) {
3757 if (wpa_auth_sm_ptk_update(sm) < 0)
3758 SM_ENTER(WPA_PTK, DISCONNECTED);
3759 else
3760 SM_ENTER(WPA_PTK, PTKSTART);
3761 } else switch (sm->wpa_ptk_state) {
3762 case WPA_PTK_INITIALIZE:
3763 break;
3764 case WPA_PTK_DISCONNECT:
3765 SM_ENTER(WPA_PTK, DISCONNECTED);
3766 break;
3767 case WPA_PTK_DISCONNECTED:
3768 SM_ENTER(WPA_PTK, INITIALIZE);
3769 break;
3770 case WPA_PTK_AUTHENTICATION:
3771 SM_ENTER(WPA_PTK, AUTHENTICATION2);
3772 break;
3773 case WPA_PTK_AUTHENTICATION2:
3774 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
3775 wpa_auth_get_eapol(wpa_auth, sm->addr,
3776 WPA_EAPOL_keyRun))
3777 SM_ENTER(WPA_PTK, INITPMK);
3778 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
3779 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE
3780 /* FIX: && 802.1X::keyRun */)
3781 SM_ENTER(WPA_PTK, INITPSK);
3782 else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP)
3783 SM_ENTER(WPA_PTK, INITPMK);
3784 break;
3785 case WPA_PTK_INITPMK:
3786 if (wpa_auth_get_eapol(wpa_auth, sm->addr,
3787 WPA_EAPOL_keyAvailable)) {
3788 SM_ENTER(WPA_PTK, PTKSTART);
3789 #ifdef CONFIG_DPP
3790 } else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && sm->pmksa) {
3791 SM_ENTER(WPA_PTK, PTKSTART);
3792 #endif /* CONFIG_DPP */
3793 } else {
3794 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3795 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
3796 "INITPMK - keyAvailable = false");
3797 SM_ENTER(WPA_PTK, DISCONNECT);
3798 }
3799 break;
3800 case WPA_PTK_INITPSK:
3801 if (wpa_auth_get_psk(wpa_auth, sm->addr, sm->p2p_dev_addr,
3802 NULL, NULL, NULL)) {
3803 SM_ENTER(WPA_PTK, PTKSTART);
3804 #ifdef CONFIG_SAE
3805 } else if (wpa_auth_uses_sae(sm) && sm->pmksa) {
3806 SM_ENTER(WPA_PTK, PTKSTART);
3807 #endif /* CONFIG_SAE */
3808 } else {
3809 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
3810 "no PSK configured for the STA");
3811 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3812 SM_ENTER(WPA_PTK, DISCONNECT);
3813 }
3814 break;
3815 case WPA_PTK_PTKSTART:
3816 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3817 sm->EAPOLKeyPairwise)
3818 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3819 else if (sm->TimeoutCtr > conf->wpa_pairwise_update_count) {
3820 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3821 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
3822 "PTKSTART: Retry limit %u reached",
3823 conf->wpa_pairwise_update_count);
3824 sm->disconnect_reason =
3825 WLAN_REASON_4WAY_HANDSHAKE_TIMEOUT;
3826 SM_ENTER(WPA_PTK, DISCONNECT);
3827 } else if (sm->TimeoutEvt)
3828 SM_ENTER(WPA_PTK, PTKSTART);
3829 break;
3830 case WPA_PTK_PTKCALCNEGOTIATING:
3831 if (sm->MICVerified)
3832 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
3833 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3834 sm->EAPOLKeyPairwise)
3835 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3836 else if (sm->TimeoutEvt)
3837 SM_ENTER(WPA_PTK, PTKSTART);
3838 break;
3839 case WPA_PTK_PTKCALCNEGOTIATING2:
3840 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3841 break;
3842 case WPA_PTK_PTKINITNEGOTIATING:
3843 if (sm->update_snonce)
3844 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3845 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3846 sm->EAPOLKeyPairwise && sm->MICVerified)
3847 SM_ENTER(WPA_PTK, PTKINITDONE);
3848 else if (sm->TimeoutCtr >
3849 conf->wpa_pairwise_update_count ||
3850 (conf->wpa_disable_eapol_key_retries &&
3851 sm->TimeoutCtr > 1)) {
3852 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3853 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
3854 "PTKINITNEGOTIATING: Retry limit %u reached",
3855 conf->wpa_pairwise_update_count);
3856 sm->disconnect_reason =
3857 WLAN_REASON_4WAY_HANDSHAKE_TIMEOUT;
3858 SM_ENTER(WPA_PTK, DISCONNECT);
3859 } else if (sm->TimeoutEvt)
3860 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3861 break;
3862 case WPA_PTK_PTKINITDONE:
3863 break;
3864 }
3865 }
3866
3867
SM_STATEnull3868 SM_STATE(WPA_PTK_GROUP, IDLE)
3869 {
3870 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
3871 if (sm->Init) {
3872 /* Init flag is not cleared here, so avoid busy
3873 * loop by claiming nothing changed. */
3874 sm->changed = false;
3875 }
3876 sm->GTimeoutCtr = 0;
3877 }
3878
3879
SM_STATEnull3880 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
3881 {
3882 u8 rsc[WPA_KEY_RSC_LEN];
3883 struct wpa_group *gsm = sm->group;
3884 const u8 *kde;
3885 u8 *kde_buf = NULL, *pos, hdr[2];
3886 size_t kde_len;
3887 u8 *gtk, stub_gtk[32];
3888 struct wpa_auth_config *conf = &sm->wpa_auth->conf;
3889
3890 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
3891
3892 sm->GTimeoutCtr++;
3893 if (conf->wpa_disable_eapol_key_retries && sm->GTimeoutCtr > 1) {
3894 /* Do not allow retransmission of EAPOL-Key group msg 1/2 */
3895 return;
3896 }
3897 if (sm->GTimeoutCtr > conf->wpa_group_update_count) {
3898 /* No point in sending the EAPOL-Key - we will disconnect
3899 * immediately following this. */
3900 return;
3901 }
3902
3903 if (sm->wpa == WPA_VERSION_WPA)
3904 sm->PInitAKeys = false;
3905 sm->TimeoutEvt = false;
3906 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
3907 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
3908 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
3909 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
3910 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3911 "sending 1/2 msg of Group Key Handshake");
3912
3913 gtk = gsm->GTK[gsm->GN - 1];
3914 if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
3915 /*
3916 * Provide unique random GTK to each STA to prevent use
3917 * of GTK in the BSS.
3918 */
3919 if (random_get_bytes(stub_gtk, gsm->GTK_len) < 0)
3920 return;
3921 gtk = stub_gtk;
3922 }
3923 if (sm->wpa == WPA_VERSION_WPA2) {
3924 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
3925 ieee80211w_kde_len(sm) + ocv_oci_len(sm);
3926 kde_buf = os_malloc(kde_len);
3927 if (!kde_buf)
3928 return;
3929
3930 kde = pos = kde_buf;
3931 hdr[0] = gsm->GN & 0x03;
3932 hdr[1] = 0;
3933 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
3934 gtk, gsm->GTK_len);
3935 pos = ieee80211w_kde_add(sm, pos);
3936 if (ocv_oci_add(sm, &pos,
3937 conf->oci_freq_override_eapol_g1) < 0) {
3938 os_free(kde_buf);
3939 return;
3940 }
3941 kde_len = pos - kde;
3942 } else {
3943 kde = gtk;
3944 kde_len = gsm->GTK_len;
3945 }
3946
3947 wpa_send_eapol(sm->wpa_auth, sm,
3948 WPA_KEY_INFO_SECURE |
3949 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
3950 WPA_KEY_INFO_MIC : 0) |
3951 WPA_KEY_INFO_ACK |
3952 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
3953 rsc, NULL, kde, kde_len, gsm->GN, 1);
3954
3955 os_free(kde_buf);
3956 }
3957
3958
SM_STATEnull3959 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
3960 {
3961 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3962 #ifdef CONFIG_OCV
3963 const u8 *key_data, *mic;
3964 struct ieee802_1x_hdr *hdr;
3965 struct wpa_eapol_key *key;
3966 struct wpa_eapol_ie_parse kde;
3967 size_t mic_len;
3968 u16 key_data_length;
3969 #endif /* CONFIG_OCV */
3970
3971 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
3972 sm->EAPOLKeyReceived = false;
3973
3974 #ifdef CONFIG_OCV
3975 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
3976
3977 /*
3978 * Note: last_rx_eapol_key length fields have already been validated in
3979 * wpa_receive().
3980 */
3981 hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
3982 key = (struct wpa_eapol_key *) (hdr + 1);
3983 mic = (u8 *) (key + 1);
3984 key_data = mic + mic_len + 2;
3985 key_data_length = WPA_GET_BE16(mic + mic_len);
3986 if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
3987 sizeof(*key) - mic_len - 2)
3988 return;
3989
3990 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
3991 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
3992 "received EAPOL-Key group msg 2/2 with invalid Key Data contents");
3993 return;
3994 }
3995
3996 if (wpa_auth_uses_ocv(sm)) {
3997 struct wpa_channel_info ci;
3998 int tx_chanwidth;
3999 int tx_seg1_idx;
4000
4001 if (wpa_channel_info(wpa_auth, &ci) != 0) {
4002 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
4003 "Failed to get channel info to validate received OCI in EAPOL-Key group 2/2");
4004 return;
4005 }
4006
4007 if (get_sta_tx_parameters(sm,
4008 channel_width_to_int(ci.chanwidth),
4009 ci.seg1_idx, &tx_chanwidth,
4010 &tx_seg1_idx) < 0)
4011 return;
4012
4013 if (ocv_verify_tx_params(kde.oci, kde.oci_len, &ci,
4014 tx_chanwidth, tx_seg1_idx) !=
4015 OCI_SUCCESS) {
4016 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
4017 "OCV failed: %s", ocv_errorstr);
4018 if (wpa_auth->conf.msg_ctx)
4019 wpa_msg(wpa_auth->conf.msg_ctx, MSG_INFO,
4020 OCV_FAILURE "addr=" MACSTR
4021 " frame=eapol-key-g2 error=%s",
4022 MAC2STR(sm->addr), ocv_errorstr);
4023 return;
4024 }
4025 }
4026 #endif /* CONFIG_OCV */
4027
4028 if (sm->GUpdateStationKeys)
4029 sm->group->GKeyDoneStations--;
4030 sm->GUpdateStationKeys = false;
4031 sm->GTimeoutCtr = 0;
4032 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
4033 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
4034 "group key handshake completed (%s)",
4035 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
4036 sm->has_GTK = true;
4037 }
4038
4039
SM_STATEnull4040 SM_STATE(WPA_PTK_GROUP, KEYERROR)
4041 {
4042 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
4043 if (sm->GUpdateStationKeys)
4044 sm->group->GKeyDoneStations--;
4045 sm->GUpdateStationKeys = false;
4046 sm->Disconnect = true;
4047 sm->disconnect_reason = WLAN_REASON_GROUP_KEY_UPDATE_TIMEOUT;
4048 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
4049 "group key handshake failed (%s) after %u tries",
4050 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN",
4051 sm->wpa_auth->conf.wpa_group_update_count);
4052 }
4053
4054
SM_STEPnull4055 SM_STEP(WPA_PTK_GROUP)
4056 {
4057 if (sm->Init || sm->PtkGroupInit) {
4058 SM_ENTER(WPA_PTK_GROUP, IDLE);
4059 sm->PtkGroupInit = false;
4060 } else switch (sm->wpa_ptk_group_state) {
4061 case WPA_PTK_GROUP_IDLE:
4062 if (sm->GUpdateStationKeys ||
4063 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
4064 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
4065 break;
4066 case WPA_PTK_GROUP_REKEYNEGOTIATING:
4067 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
4068 !sm->EAPOLKeyPairwise && sm->MICVerified)
4069 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
4070 else if (sm->GTimeoutCtr >
4071 sm->wpa_auth->conf.wpa_group_update_count ||
4072 (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
4073 sm->GTimeoutCtr > 1))
4074 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
4075 else if (sm->TimeoutEvt)
4076 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
4077 break;
4078 case WPA_PTK_GROUP_KEYERROR:
4079 SM_ENTER(WPA_PTK_GROUP, IDLE);
4080 break;
4081 case WPA_PTK_GROUP_REKEYESTABLISHED:
4082 SM_ENTER(WPA_PTK_GROUP, IDLE);
4083 break;
4084 }
4085 }
4086
4087
wpa_gtk_update(struct wpa_authenticator *wpa_auth, struct wpa_group *group)4088 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
4089 struct wpa_group *group)
4090 {
4091 struct wpa_auth_config *conf = &wpa_auth->conf;
4092 int ret = 0;
4093 size_t len;
4094
4095 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
4096 inc_byte_array(group->Counter, WPA_NONCE_LEN);
4097 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
4098 wpa_auth->addr, group->GNonce,
4099 group->GTK[group->GN - 1], group->GTK_len) < 0)
4100 ret = -1;
4101 wpa_hexdump_key(MSG_DEBUG, "GTK",
4102 group->GTK[group->GN - 1], group->GTK_len);
4103
4104 if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
4105 len = wpa_cipher_key_len(conf->group_mgmt_cipher);
4106 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
4107 inc_byte_array(group->Counter, WPA_NONCE_LEN);
4108 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
4109 wpa_auth->addr, group->GNonce,
4110 group->IGTK[group->GN_igtk - 4], len) < 0)
4111 ret = -1;
4112 wpa_hexdump_key(MSG_DEBUG, "IGTK",
4113 group->IGTK[group->GN_igtk - 4], len);
4114 }
4115
4116 if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION &&
4117 conf->beacon_prot) {
4118 len = wpa_cipher_key_len(conf->group_mgmt_cipher);
4119 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
4120 inc_byte_array(group->Counter, WPA_NONCE_LEN);
4121 if (wpa_gmk_to_gtk(group->GMK, "BIGTK key expansion",
4122 wpa_auth->addr, group->GNonce,
4123 group->BIGTK[group->GN_bigtk - 6], len) < 0)
4124 ret = -1;
4125 wpa_hexdump_key(MSG_DEBUG, "BIGTK",
4126 group->BIGTK[group->GN_bigtk - 6], len);
4127 }
4128
4129 return ret;
4130 }
4131
4132
wpa_group_gtk_init(struct wpa_authenticator *wpa_auth, struct wpa_group *group)4133 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
4134 struct wpa_group *group)
4135 {
4136 wpa_printf(MSG_DEBUG,
4137 "WPA: group state machine entering state GTK_INIT (VLAN-ID %d)",
4138 group->vlan_id);
4139 group->changed = false; /* GInit is not cleared here; avoid loop */
4140 group->wpa_group_state = WPA_GROUP_GTK_INIT;
4141
4142 /* GTK[0..N] = 0 */
4143 os_memset(group->GTK, 0, sizeof(group->GTK));
4144 group->GN = 1;
4145 group->GM = 2;
4146 group->GN_igtk = 4;
4147 group->GM_igtk = 5;
4148 group->GN_bigtk = 6;
4149 group->GM_bigtk = 7;
4150 /* GTK[GN] = CalcGTK() */
4151 wpa_gtk_update(wpa_auth, group);
4152 }
4153
4154
wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)4155 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
4156 {
4157 if (ctx != NULL && ctx != sm->group)
4158 return 0;
4159
4160 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
4161 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
4162 "Not in PTKINITDONE; skip Group Key update");
4163 sm->GUpdateStationKeys = false;
4164 return 0;
4165 }
4166 if (sm->GUpdateStationKeys) {
4167 /*
4168 * This should not really happen, so add a debug log entry.
4169 * Since we clear the GKeyDoneStations before the loop, the
4170 * station needs to be counted here anyway.
4171 */
4172 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
4173 "GUpdateStationKeys was already set when marking station for GTK rekeying");
4174 }
4175
4176 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
4177 if (sm->is_wnmsleep)
4178 return 0;
4179
4180 sm->group->GKeyDoneStations++;
4181 sm->GUpdateStationKeys = true;
4182
4183 wpa_sm_step(sm);
4184 return 0;
4185 }
4186
4187
4188 #ifdef CONFIG_WNM_AP
4189 /* update GTK when exiting WNM-Sleep Mode */
wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)4190 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
4191 {
4192 if (!sm || sm->is_wnmsleep)
4193 return;
4194
4195 wpa_group_update_sta(sm, NULL);
4196 }
4197
4198
wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)4199 void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
4200 {
4201 if (sm)
4202 sm->is_wnmsleep = !!flag;
4203 }
4204
4205
wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)4206 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
4207 {
4208 struct wpa_auth_config *conf = &sm->wpa_auth->conf;
4209 struct wpa_group *gsm = sm->group;
4210 u8 *start = pos;
4211
4212 /*
4213 * GTK subelement:
4214 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
4215 * Key[5..32]
4216 */
4217 *pos++ = WNM_SLEEP_SUBELEM_GTK;
4218 *pos++ = 11 + gsm->GTK_len;
4219 /* Key ID in B0-B1 of Key Info */
4220 WPA_PUT_LE16(pos, gsm->GN & 0x03);
4221 pos += 2;
4222 *pos++ = gsm->GTK_len;
4223 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
4224 return 0;
4225 pos += 8;
4226 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
4227 if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
4228 /*
4229 * Provide unique random GTK to each STA to prevent use
4230 * of GTK in the BSS.
4231 */
4232 if (random_get_bytes(pos, gsm->GTK_len) < 0)
4233 return 0;
4234 }
4235 pos += gsm->GTK_len;
4236
4237 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
4238 gsm->GN);
4239 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
4240 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
4241
4242 return pos - start;
4243 }
4244
4245
wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)4246 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
4247 {
4248 struct wpa_auth_config *conf = &sm->wpa_auth->conf;
4249 struct wpa_group *gsm = sm->group;
4250 u8 *start = pos;
4251 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
4252
4253 /*
4254 * IGTK subelement:
4255 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
4256 */
4257 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
4258 *pos++ = 2 + 6 + len;
4259 WPA_PUT_LE16(pos, gsm->GN_igtk);
4260 pos += 2;
4261 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
4262 return 0;
4263 pos += 6;
4264
4265 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
4266 if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
4267 /*
4268 * Provide unique random IGTK to each STA to prevent use
4269 * of IGTK in the BSS.
4270 */
4271 if (random_get_bytes(pos, len) < 0)
4272 return 0;
4273 }
4274 pos += len;
4275
4276 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
4277 gsm->GN_igtk);
4278 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
4279 gsm->IGTK[gsm->GN_igtk - 4], len);
4280
4281 return pos - start;
4282 }
4283
4284
wpa_wnmsleep_bigtk_subelem(struct wpa_state_machine *sm, u8 *pos)4285 int wpa_wnmsleep_bigtk_subelem(struct wpa_state_machine *sm, u8 *pos)
4286 {
4287 struct wpa_group *gsm = sm->group;
4288 u8 *start = pos;
4289 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
4290
4291 /*
4292 * BIGTK subelement:
4293 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
4294 */
4295 *pos++ = WNM_SLEEP_SUBELEM_BIGTK;
4296 *pos++ = 2 + 6 + len;
4297 WPA_PUT_LE16(pos, gsm->GN_bigtk);
4298 pos += 2;
4299 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_bigtk, pos) != 0)
4300 return 0;
4301 pos += 6;
4302
4303 os_memcpy(pos, gsm->BIGTK[gsm->GN_bigtk - 6], len);
4304 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
4305 /*
4306 * Provide unique random BIGTK to each STA to prevent use
4307 * of BIGTK in the BSS.
4308 */
4309 if (random_get_bytes(pos, len) < 0)
4310 return 0;
4311 }
4312 pos += len;
4313
4314 wpa_printf(MSG_DEBUG, "WNM: BIGTK Key ID %u in WNM-Sleep Mode exit",
4315 gsm->GN_bigtk);
4316 wpa_hexdump_key(MSG_DEBUG, "WNM: BIGTK in WNM-Sleep Mode exit",
4317 gsm->BIGTK[gsm->GN_bigtk - 6], len);
4318
4319 return pos - start;
4320 }
4321
4322 #endif /* CONFIG_WNM_AP */
4323
4324
wpa_group_setkeys(struct wpa_authenticator *wpa_auth, struct wpa_group *group)4325 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
4326 struct wpa_group *group)
4327 {
4328 int tmp;
4329
4330 wpa_printf(MSG_EXCESSIVE,
4331 "WPA: group state machine entering state SETKEYS (VLAN-ID %d)",
4332 group->vlan_id);
4333 group->changed = true;
4334 group->wpa_group_state = WPA_GROUP_SETKEYS;
4335 group->GTKReKey = false;
4336 tmp = group->GM;
4337 group->GM = group->GN;
4338 group->GN = tmp;
4339 tmp = group->GM_igtk;
4340 group->GM_igtk = group->GN_igtk;
4341 group->GN_igtk = tmp;
4342 tmp = group->GM_bigtk;
4343 group->GM_bigtk = group->GN_bigtk;
4344 group->GN_bigtk = tmp;
4345 /* "GKeyDoneStations = GNoStations" is done in more robust way by
4346 * counting the STAs that are marked with GUpdateStationKeys instead of
4347 * including all STAs that could be in not-yet-completed state. */
4348 wpa_gtk_update(wpa_auth, group);
4349
4350 if (group->GKeyDoneStations) {
4351 wpa_printf(MSG_DEBUG,
4352 "wpa_group_setkeys: Unexpected GKeyDoneStations=%d when starting new GTK rekey",
4353 group->GKeyDoneStations);
4354 group->GKeyDoneStations = 0;
4355 }
4356 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
4357 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
4358 group->GKeyDoneStations);
4359 }
4360
4361
wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth, struct wpa_group *group)4362 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
4363 struct wpa_group *group)
4364 {
4365 struct wpa_auth_config *conf = &wpa_auth->conf;
4366 int ret = 0;
4367
4368 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
4369 wpa_cipher_to_alg(conf->wpa_group),
4370 broadcast_ether_addr, group->GN,
4371 group->GTK[group->GN - 1], group->GTK_len,
4372 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
4373 ret = -1;
4374
4375 if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
4376 enum wpa_alg alg;
4377 size_t len;
4378
4379 alg = wpa_cipher_to_alg(conf->group_mgmt_cipher);
4380 len = wpa_cipher_key_len(conf->group_mgmt_cipher);
4381
4382 if (ret == 0 &&
4383 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
4384 broadcast_ether_addr, group->GN_igtk,
4385 group->IGTK[group->GN_igtk - 4], len,
4386 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
4387 ret = -1;
4388
4389 if (ret == 0 && conf->beacon_prot &&
4390 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
4391 broadcast_ether_addr, group->GN_bigtk,
4392 group->BIGTK[group->GN_bigtk - 6], len,
4393 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
4394 ret = -1;
4395 }
4396
4397 return ret;
4398 }
4399
4400
wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)4401 static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
4402 {
4403 if (sm->group == ctx) {
4404 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR_SEC
4405 " for disconnection due to fatal failure",
4406 MAC2STR_SEC(sm->addr));
4407 sm->Disconnect = true;
4408 }
4409
4410 return 0;
4411 }
4412
4413
wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth, struct wpa_group *group)4414 static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
4415 struct wpa_group *group)
4416 {
4417 wpa_printf(MSG_DEBUG,
4418 "WPA: group state machine entering state FATAL_FAILURE");
4419 group->changed = true;
4420 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
4421 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
4422 }
4423
4424
wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth, struct wpa_group *group)4425 static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
4426 struct wpa_group *group)
4427 {
4428 wpa_printf(MSG_DEBUG,
4429 "WPA: group state machine entering state SETKEYSDONE (VLAN-ID %d)",
4430 group->vlan_id);
4431 group->changed = true;
4432 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
4433
4434 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
4435 wpa_group_fatal_failure(wpa_auth, group);
4436 return -1;
4437 }
4438
4439 return 0;
4440 }
4441
4442
wpa_group_sm_step(struct wpa_authenticator *wpa_auth, struct wpa_group *group)4443 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
4444 struct wpa_group *group)
4445 {
4446 if (group->GInit) {
4447 wpa_group_gtk_init(wpa_auth, group);
4448 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
4449 /* Do not allow group operations */
4450 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
4451 group->GTKAuthenticator) {
4452 wpa_group_setkeysdone(wpa_auth, group);
4453 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
4454 group->GTKReKey) {
4455 wpa_group_setkeys(wpa_auth, group);
4456 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
4457 if (group->GKeyDoneStations == 0)
4458 wpa_group_setkeysdone(wpa_auth, group);
4459 else if (group->GTKReKey)
4460 wpa_group_setkeys(wpa_auth, group);
4461 }
4462 }
4463
4464
wpa_sm_step(struct wpa_state_machine *sm)4465 static int wpa_sm_step(struct wpa_state_machine *sm)
4466 {
4467 if (!sm)
4468 return 0;
4469
4470 if (sm->in_step_loop) {
4471 /* This should not happen, but if it does, make sure we do not
4472 * end up freeing the state machine too early by exiting the
4473 * recursive call. */
4474 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
4475 return 0;
4476 }
4477
4478 sm->in_step_loop = 1;
4479 do {
4480 if (sm->pending_deinit)
4481 break;
4482
4483 sm->changed = false;
4484 sm->wpa_auth->group->changed = false;
4485
4486 SM_STEP_RUN(WPA_PTK);
4487 if (sm->pending_deinit)
4488 break;
4489 SM_STEP_RUN(WPA_PTK_GROUP);
4490 if (sm->pending_deinit)
4491 break;
4492 wpa_group_sm_step(sm->wpa_auth, sm->group);
4493 } while (sm->changed || sm->wpa_auth->group->changed);
4494 sm->in_step_loop = 0;
4495
4496 if (sm->pending_deinit) {
4497 wpa_printf(MSG_DEBUG,
4498 "WPA: Completing pending STA state machine deinit for "
4499 MACSTR_SEC, MAC2STR_SEC(sm->addr));
4500 wpa_free_sta_sm(sm);
4501 return 1;
4502 }
4503 return 0;
4504 }
4505
4506
wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)4507 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
4508 {
4509 struct wpa_state_machine *sm = eloop_ctx;
4510 wpa_sm_step(sm);
4511 }
4512
4513
wpa_auth_sm_notify(struct wpa_state_machine *sm)4514 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
4515 {
4516 if (!sm)
4517 return;
4518 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
4519 }
4520
4521
wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)4522 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
4523 {
4524 int tmp, i;
4525 struct wpa_group *group;
4526
4527 if (!wpa_auth)
4528 return;
4529
4530 group = wpa_auth->group;
4531
4532 for (i = 0; i < 2; i++) {
4533 tmp = group->GM;
4534 group->GM = group->GN;
4535 group->GN = tmp;
4536 tmp = group->GM_igtk;
4537 group->GM_igtk = group->GN_igtk;
4538 group->GN_igtk = tmp;
4539 tmp = group->GM_bigtk;
4540 group->GM_bigtk = group->GN_bigtk;
4541 group->GN_bigtk = tmp;
4542 wpa_gtk_update(wpa_auth, group);
4543 wpa_group_config_group_keys(wpa_auth, group);
4544 }
4545 }
4546
4547
wpa_bool_txt(int val)4548 static const char * wpa_bool_txt(int val)
4549 {
4550 return val ? "TRUE" : "FALSE";
4551 }
4552
4553
4554 #define RSN_SUITE "%02x-%02x-%02x-%d"
4555 #define RSN_SUITE_ARG(s) \
4556 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
4557
wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)4558 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
4559 {
4560 struct wpa_auth_config *conf;
4561 int len = 0, ret;
4562 char pmkid_txt[PMKID_LEN * 2 + 1];
4563 #ifdef CONFIG_RSN_PREAUTH
4564 const int preauth = 1;
4565 #else /* CONFIG_RSN_PREAUTH */
4566 const int preauth = 0;
4567 #endif /* CONFIG_RSN_PREAUTH */
4568
4569 if (!wpa_auth)
4570 return len;
4571 conf = &wpa_auth->conf;
4572
4573 ret = os_snprintf(buf + len, buflen - len,
4574 "dot11RSNAOptionImplemented=TRUE\n"
4575 "dot11RSNAPreauthenticationImplemented=%s\n"
4576 "dot11RSNAEnabled=%s\n"
4577 "dot11RSNAPreauthenticationEnabled=%s\n",
4578 wpa_bool_txt(preauth),
4579 wpa_bool_txt(conf->wpa & WPA_PROTO_RSN),
4580 wpa_bool_txt(conf->rsn_preauth));
4581 if (os_snprintf_error(buflen - len, ret))
4582 return len;
4583 len += ret;
4584
4585 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
4586 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
4587
4588 ret = os_snprintf(
4589 buf + len, buflen - len,
4590 "dot11RSNAConfigVersion=%u\n"
4591 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
4592 /* FIX: dot11RSNAConfigGroupCipher */
4593 /* FIX: dot11RSNAConfigGroupRekeyMethod */
4594 /* FIX: dot11RSNAConfigGroupRekeyTime */
4595 /* FIX: dot11RSNAConfigGroupRekeyPackets */
4596 "dot11RSNAConfigGroupRekeyStrict=%u\n"
4597 "dot11RSNAConfigGroupUpdateCount=%u\n"
4598 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
4599 "dot11RSNAConfigGroupCipherSize=%u\n"
4600 "dot11RSNAConfigPMKLifetime=%u\n"
4601 "dot11RSNAConfigPMKReauthThreshold=%u\n"
4602 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
4603 "dot11RSNAConfigSATimeout=%u\n"
4604 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
4605 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
4606 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
4607 "dot11RSNAPMKIDUsed=%s\n"
4608 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
4609 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
4610 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
4611 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
4612 "dot11RSNA4WayHandshakeFailures=%u\n"
4613 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
4614 RSN_VERSION,
4615 !!conf->wpa_strict_rekey,
4616 conf->wpa_group_update_count,
4617 conf->wpa_pairwise_update_count,
4618 wpa_cipher_key_len(conf->wpa_group) * 8,
4619 dot11RSNAConfigPMKLifetime,
4620 dot11RSNAConfigPMKReauthThreshold,
4621 dot11RSNAConfigSATimeout,
4622 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
4623 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
4624 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
4625 pmkid_txt,
4626 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
4627 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
4628 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
4629 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
4630 wpa_auth->dot11RSNA4WayHandshakeFailures);
4631 if (os_snprintf_error(buflen - len, ret))
4632 return len;
4633 len += ret;
4634
4635 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
4636 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
4637
4638 /* Private MIB */
4639 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
4640 wpa_auth->group->wpa_group_state);
4641 if (os_snprintf_error(buflen - len, ret))
4642 return len;
4643 len += ret;
4644
4645 return len;
4646 }
4647
4648
wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)4649 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
4650 {
4651 int len = 0, ret;
4652 u32 pairwise = 0;
4653
4654 if (!sm)
4655 return 0;
4656
4657 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
4658
4659 /* dot11RSNAStatsEntry */
4660
4661 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
4662 WPA_PROTO_RSN : WPA_PROTO_WPA,
4663 sm->pairwise);
4664 if (pairwise == 0)
4665 return 0;
4666
4667 ret = os_snprintf(
4668 buf + len, buflen - len,
4669 /* TODO: dot11RSNAStatsIndex */
4670 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
4671 "dot11RSNAStatsVersion=1\n"
4672 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
4673 /* TODO: dot11RSNAStatsTKIPICVErrors */
4674 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
4675 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
4676 /* TODO: dot11RSNAStatsCCMPReplays */
4677 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
4678 /* TODO: dot11RSNAStatsTKIPReplays */,
4679 MAC2STR(sm->addr),
4680 RSN_SUITE_ARG(pairwise),
4681 sm->dot11RSNAStatsTKIPLocalMICFailures,
4682 sm->dot11RSNAStatsTKIPRemoteMICFailures);
4683 if (os_snprintf_error(buflen - len, ret))
4684 return len;
4685 len += ret;
4686
4687 /* Private MIB */
4688 ret = os_snprintf(buf + len, buflen - len,
4689 "wpa=%d\n"
4690 "AKMSuiteSelector=" RSN_SUITE "\n"
4691 "hostapdWPAPTKState=%d\n"
4692 "hostapdWPAPTKGroupState=%d\n",
4693 sm->wpa,
4694 RSN_SUITE_ARG(wpa_akm_to_suite(sm->wpa_key_mgmt)),
4695 sm->wpa_ptk_state,
4696 sm->wpa_ptk_group_state);
4697 if (os_snprintf_error(buflen - len, ret))
4698 return len;
4699 len += ret;
4700
4701 return len;
4702 }
4703
4704
wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)4705 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
4706 {
4707 if (wpa_auth)
4708 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
4709 }
4710
4711
wpa_auth_pairwise_set(struct wpa_state_machine *sm)4712 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
4713 {
4714 return sm && sm->pairwise_set;
4715 }
4716
4717
wpa_auth_get_pairwise(struct wpa_state_machine *sm)4718 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
4719 {
4720 return sm->pairwise;
4721 }
4722
4723
wpa_auth_get_pmk(struct wpa_state_machine *sm, int *len)4724 const u8 * wpa_auth_get_pmk(struct wpa_state_machine *sm, int *len)
4725 {
4726 if (!sm)
4727 return NULL;
4728 *len = sm->pmk_len;
4729 return sm->PMK;
4730 }
4731
4732
wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)4733 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
4734 {
4735 if (!sm)
4736 return -1;
4737 return sm->wpa_key_mgmt;
4738 }
4739
4740
wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)4741 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
4742 {
4743 if (!sm)
4744 return 0;
4745 return sm->wpa;
4746 }
4747
4748
wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm)4749 int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm)
4750 {
4751 if (!sm || !wpa_key_mgmt_ft(sm->wpa_key_mgmt))
4752 return 0;
4753 return sm->tk_already_set;
4754 }
4755
4756
wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine *sm)4757 int wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine *sm)
4758 {
4759 if (!sm || !wpa_key_mgmt_fils(sm->wpa_key_mgmt))
4760 return 0;
4761 return sm->tk_already_set;
4762 }
4763
4764
wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm, struct rsn_pmksa_cache_entry *entry)4765 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
4766 struct rsn_pmksa_cache_entry *entry)
4767 {
4768 if (!sm || sm->pmksa != entry)
4769 return -1;
4770 sm->pmksa = NULL;
4771 return 0;
4772 }
4773
4774
4775 struct rsn_pmksa_cache_entry *
wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)4776 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
4777 {
4778 return sm ? sm->pmksa : NULL;
4779 }
4780
4781
wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)4782 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
4783 {
4784 if (sm)
4785 sm->dot11RSNAStatsTKIPLocalMICFailures++;
4786 }
4787
4788
wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)4789 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
4790 {
4791 if (!wpa_auth)
4792 return NULL;
4793 *len = wpa_auth->wpa_ie_len;
4794 return wpa_auth->wpa_ie;
4795 }
4796
4797
wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk, unsigned int pmk_len, int session_timeout, struct eapol_state_machine *eapol)4798 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
4799 unsigned int pmk_len,
4800 int session_timeout, struct eapol_state_machine *eapol)
4801 {
4802 if (!sm || sm->wpa != WPA_VERSION_WPA2 ||
4803 sm->wpa_auth->conf.disable_pmksa_caching)
4804 return -1;
4805
4806 #ifdef CONFIG_IEEE80211R_AP
4807 if (pmk_len >= 2 * PMK_LEN && wpa_key_mgmt_ft(sm->wpa_key_mgmt) &&
4808 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
4809 !wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
4810 /* Cache MPMK/XXKey instead of initial part from MSK */
4811 pmk = pmk + PMK_LEN;
4812 pmk_len = PMK_LEN;
4813 } else
4814 #endif /* CONFIG_IEEE80211R_AP */
4815 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
4816 if (pmk_len > PMK_LEN_SUITE_B_192)
4817 pmk_len = PMK_LEN_SUITE_B_192;
4818 } else if (pmk_len > PMK_LEN) {
4819 pmk_len = PMK_LEN;
4820 }
4821
4822 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK", pmk, pmk_len);
4823 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, pmk_len, NULL,
4824 sm->PTK.kck, sm->PTK.kck_len,
4825 sm->wpa_auth->addr, sm->addr, session_timeout,
4826 eapol, sm->wpa_key_mgmt))
4827 return 0;
4828
4829 return -1;
4830 }
4831
4832
wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth, const u8 *pmk, size_t len, const u8 *sta_addr, int session_timeout, struct eapol_state_machine *eapol)4833 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
4834 const u8 *pmk, size_t len, const u8 *sta_addr,
4835 int session_timeout,
4836 struct eapol_state_machine *eapol)
4837 {
4838 if (!wpa_auth)
4839 return -1;
4840
4841 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK from preauth", pmk, len);
4842 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, NULL,
4843 NULL, 0,
4844 wpa_auth->addr,
4845 sta_addr, session_timeout, eapol,
4846 WPA_KEY_MGMT_IEEE8021X))
4847 return 0;
4848
4849 return -1;
4850 }
4851
4852
wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr, const u8 *pmk, size_t pmk_len, const u8 *pmkid, int akmp)4853 int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
4854 const u8 *pmk, size_t pmk_len, const u8 *pmkid,
4855 int akmp)
4856 {
4857 if (wpa_auth->conf.disable_pmksa_caching)
4858 return -1;
4859
4860 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK from SAE", pmk, pmk_len);
4861 if (!akmp)
4862 akmp = WPA_KEY_MGMT_SAE;
4863 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, pmk_len, pmkid,
4864 NULL, 0, wpa_auth->addr, addr, 0, NULL, akmp))
4865 return 0;
4866
4867 return -1;
4868 }
4869
4870
wpa_auth_add_sae_pmkid(struct wpa_state_machine *sm, const u8 *pmkid)4871 void wpa_auth_add_sae_pmkid(struct wpa_state_machine *sm, const u8 *pmkid)
4872 {
4873 os_memcpy(sm->pmkid, pmkid, PMKID_LEN);
4874 sm->pmkid_set = 1;
4875 }
4876
4877
wpa_auth_pmksa_add2(struct wpa_authenticator *wpa_auth, const u8 *addr, const u8 *pmk, size_t pmk_len, const u8 *pmkid, int session_timeout, int akmp)4878 int wpa_auth_pmksa_add2(struct wpa_authenticator *wpa_auth, const u8 *addr,
4879 const u8 *pmk, size_t pmk_len, const u8 *pmkid,
4880 int session_timeout, int akmp)
4881 {
4882 if (!wpa_auth || wpa_auth->conf.disable_pmksa_caching)
4883 return -1;
4884
4885 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK (2)", pmk, PMK_LEN);
4886 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, pmk_len, pmkid,
4887 NULL, 0, wpa_auth->addr, addr, session_timeout,
4888 NULL, akmp))
4889 return 0;
4890
4891 return -1;
4892 }
4893
4894
wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth, const u8 *sta_addr)4895 void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
4896 const u8 *sta_addr)
4897 {
4898 struct rsn_pmksa_cache_entry *pmksa;
4899
4900 if (!wpa_auth || !wpa_auth->pmksa)
4901 return;
4902 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
4903 if (pmksa) {
4904 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
4905 MACSTR_SEC " based on request", MAC2STR_SEC(sta_addr));
4906 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
4907 }
4908 }
4909
4910
wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf, size_t len)4911 int wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf,
4912 size_t len)
4913 {
4914 if (!wpa_auth || !wpa_auth->pmksa)
4915 return 0;
4916 return pmksa_cache_auth_list(wpa_auth->pmksa, buf, len);
4917 }
4918
4919
wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth)4920 void wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth)
4921 {
4922 if (wpa_auth && wpa_auth->pmksa)
4923 pmksa_cache_auth_flush(wpa_auth->pmksa);
4924 }
4925
4926
4927 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
4928 #ifdef CONFIG_MESH
4929
wpa_auth_pmksa_list_mesh(struct wpa_authenticator *wpa_auth, const u8 *addr, char *buf, size_t len)4930 int wpa_auth_pmksa_list_mesh(struct wpa_authenticator *wpa_auth, const u8 *addr,
4931 char *buf, size_t len)
4932 {
4933 if (!wpa_auth || !wpa_auth->pmksa)
4934 return 0;
4935
4936 return pmksa_cache_auth_list_mesh(wpa_auth->pmksa, addr, buf, len);
4937 }
4938
4939
4940 struct rsn_pmksa_cache_entry *
wpa_auth_pmksa_create_entry(const u8 *aa, const u8 *spa, const u8 *pmk, size_t pmk_len, int akmp, const u8 *pmkid, int expiration)4941 wpa_auth_pmksa_create_entry(const u8 *aa, const u8 *spa, const u8 *pmk,
4942 size_t pmk_len, int akmp,
4943 const u8 *pmkid, int expiration)
4944 {
4945 struct rsn_pmksa_cache_entry *entry;
4946 struct os_reltime now;
4947
4948 entry = pmksa_cache_auth_create_entry(pmk, pmk_len, pmkid, NULL, 0, aa,
4949 spa, 0, NULL, akmp);
4950 if (!entry)
4951 return NULL;
4952
4953 os_get_reltime(&now);
4954 entry->expiration = now.sec + expiration;
4955 return entry;
4956 }
4957
4958
wpa_auth_pmksa_add_entry(struct wpa_authenticator *wpa_auth, struct rsn_pmksa_cache_entry *entry)4959 int wpa_auth_pmksa_add_entry(struct wpa_authenticator *wpa_auth,
4960 struct rsn_pmksa_cache_entry *entry)
4961 {
4962 int ret;
4963
4964 if (!wpa_auth || !wpa_auth->pmksa)
4965 return -1;
4966
4967 ret = pmksa_cache_auth_add_entry(wpa_auth->pmksa, entry);
4968 if (ret < 0)
4969 wpa_printf(MSG_DEBUG,
4970 "RSN: Failed to store external PMKSA cache for "
4971 MACSTR_SEC, MAC2STR_SEC(entry->spa));
4972
4973 return ret;
4974 }
4975
4976 #endif /* CONFIG_MESH */
4977 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
4978
4979
4980 struct rsn_pmksa_cache_entry *
wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr, const u8 *pmkid)4981 wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
4982 const u8 *pmkid)
4983 {
4984 if (!wpa_auth || !wpa_auth->pmksa)
4985 return NULL;
4986 return pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, pmkid);
4987 }
4988
4989
wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa, struct wpa_state_machine *sm, struct wpa_authenticator *wpa_auth, u8 *pmkid, u8 *pmk)4990 void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa,
4991 struct wpa_state_machine *sm,
4992 struct wpa_authenticator *wpa_auth,
4993 u8 *pmkid, u8 *pmk)
4994 {
4995 if (!sm)
4996 return;
4997
4998 sm->pmksa = pmksa;
4999 os_memcpy(pmk, pmksa->pmk, PMK_LEN);
5000 os_memcpy(pmkid, pmksa->pmkid, PMKID_LEN);
5001 os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmksa->pmkid, PMKID_LEN);
5002 }
5003
5004
5005 /*
5006 * Remove and free the group from wpa_authenticator. This is triggered by a
5007 * callback to make sure nobody is currently iterating the group list while it
5008 * gets modified.
5009 */
wpa_group_free(struct wpa_authenticator *wpa_auth, struct wpa_group *group)5010 static void wpa_group_free(struct wpa_authenticator *wpa_auth,
5011 struct wpa_group *group)
5012 {
5013 struct wpa_group *prev = wpa_auth->group;
5014
5015 wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d",
5016 group->vlan_id);
5017
5018 while (prev) {
5019 if (prev->next == group) {
5020 /* This never frees the special first group as needed */
5021 prev->next = group->next;
5022 os_free(group);
5023 break;
5024 }
5025 prev = prev->next;
5026 }
5027
5028 }
5029
5030
5031 /* Increase the reference counter for group */
wpa_group_get(struct wpa_authenticator *wpa_auth, struct wpa_group *group)5032 static void wpa_group_get(struct wpa_authenticator *wpa_auth,
5033 struct wpa_group *group)
5034 {
5035 /* Skip the special first group */
5036 if (wpa_auth->group == group)
5037 return;
5038
5039 group->references++;
5040 }
5041
5042
5043 /* Decrease the reference counter and maybe free the group */
wpa_group_put(struct wpa_authenticator *wpa_auth, struct wpa_group *group)5044 static void wpa_group_put(struct wpa_authenticator *wpa_auth,
5045 struct wpa_group *group)
5046 {
5047 /* Skip the special first group */
5048 if (wpa_auth->group == group)
5049 return;
5050
5051 group->references--;
5052 if (group->references)
5053 return;
5054 wpa_group_free(wpa_auth, group);
5055 }
5056
5057
5058 /*
5059 * Add a group that has its references counter set to zero. Caller needs to
5060 * call wpa_group_get() on the return value to mark the entry in use.
5061 */
5062 static struct wpa_group *
wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)5063 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
5064 {
5065 struct wpa_group *group;
5066
5067 if (!wpa_auth || !wpa_auth->group)
5068 return NULL;
5069
5070 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
5071 vlan_id);
5072 group = wpa_group_init(wpa_auth, vlan_id, 0);
5073 if (!group)
5074 return NULL;
5075
5076 group->next = wpa_auth->group->next;
5077 wpa_auth->group->next = group;
5078
5079 return group;
5080 }
5081
5082
5083 /*
5084 * Enforce that the group state machine for the VLAN is running, increase
5085 * reference counter as interface is up. References might have been increased
5086 * even if a negative value is returned.
5087 * Returns: -1 on error (group missing, group already failed); otherwise, 0
5088 */
wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id)5089 int wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id)
5090 {
5091 struct wpa_group *group;
5092
5093 if (!wpa_auth)
5094 return 0;
5095
5096 group = wpa_auth->group;
5097 while (group) {
5098 if (group->vlan_id == vlan_id)
5099 break;
5100 group = group->next;
5101 }
5102
5103 if (!group) {
5104 group = wpa_auth_add_group(wpa_auth, vlan_id);
5105 if (!group)
5106 return -1;
5107 }
5108
5109 wpa_printf(MSG_DEBUG,
5110 "WPA: Ensure group state machine running for VLAN ID %d",
5111 vlan_id);
5112
5113 wpa_group_get(wpa_auth, group);
5114 group->num_setup_iface++;
5115
5116 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
5117 return -1;
5118
5119 return 0;
5120 }
5121
5122
5123 /*
5124 * Decrease reference counter, expected to be zero afterwards.
5125 * returns: -1 on error (group not found, group in fail state)
5126 * -2 if wpa_group is still referenced
5127 * 0 else
5128 */
wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id)5129 int wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id)
5130 {
5131 struct wpa_group *group;
5132 int ret = 0;
5133
5134 if (!wpa_auth)
5135 return 0;
5136
5137 group = wpa_auth->group;
5138 while (group) {
5139 if (group->vlan_id == vlan_id)
5140 break;
5141 group = group->next;
5142 }
5143
5144 if (!group)
5145 return -1;
5146
5147 wpa_printf(MSG_DEBUG,
5148 "WPA: Try stopping group state machine for VLAN ID %d",
5149 vlan_id);
5150
5151 if (group->num_setup_iface <= 0) {
5152 wpa_printf(MSG_ERROR,
5153 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
5154 vlan_id);
5155 return -1;
5156 }
5157 group->num_setup_iface--;
5158
5159 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
5160 ret = -1;
5161
5162 if (group->references > 1) {
5163 wpa_printf(MSG_DEBUG,
5164 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
5165 vlan_id);
5166 ret = -2;
5167 }
5168
5169 wpa_group_put(wpa_auth, group);
5170
5171 return ret;
5172 }
5173
5174
wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)5175 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
5176 {
5177 struct wpa_group *group;
5178
5179 if (!sm || !sm->wpa_auth)
5180 return 0;
5181
5182 group = sm->wpa_auth->group;
5183 while (group) {
5184 if (group->vlan_id == vlan_id)
5185 break;
5186 group = group->next;
5187 }
5188
5189 if (!group) {
5190 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
5191 if (!group)
5192 return -1;
5193 }
5194
5195 if (sm->group == group)
5196 return 0;
5197
5198 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
5199 return -1;
5200
5201 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR_SEC
5202 " to use group state machine for VLAN ID %d",
5203 MAC2STR_SEC(sm->addr), vlan_id);
5204
5205 wpa_group_get(sm->wpa_auth, group);
5206 wpa_group_put(sm->wpa_auth, sm->group);
5207 sm->group = group;
5208
5209 return 0;
5210 }
5211
5212
wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth, struct wpa_state_machine *sm, int ack)5213 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
5214 struct wpa_state_machine *sm, int ack)
5215 {
5216 if (!wpa_auth || !sm)
5217 return;
5218 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR_SEC
5219 " ack=%d", MAC2STR_SEC(sm->addr), ack);
5220 if (sm->pending_1_of_4_timeout && ack) {
5221 /*
5222 * Some deployed supplicant implementations update their SNonce
5223 * for each EAPOL-Key 2/4 message even within the same 4-way
5224 * handshake and then fail to use the first SNonce when
5225 * deriving the PTK. This results in unsuccessful 4-way
5226 * handshake whenever the relatively short initial timeout is
5227 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
5228 * around this by increasing the timeout now that we know that
5229 * the station has received the frame.
5230 */
5231 int timeout_ms = eapol_key_timeout_subseq;
5232 wpa_printf(MSG_DEBUG,
5233 "WPA: Increase initial EAPOL-Key 1/4 timeout by %u ms because of acknowledged frame",
5234 timeout_ms);
5235 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
5236 eloop_register_timeout(timeout_ms / 1000,
5237 (timeout_ms % 1000) * 1000,
5238 wpa_send_eapol_timeout, wpa_auth, sm);
5239 }
5240
5241 #ifdef CONFIG_TESTING_OPTIONS
5242 if (sm->eapol_status_cb) {
5243 sm->eapol_status_cb(sm->eapol_status_cb_ctx1,
5244 sm->eapol_status_cb_ctx2);
5245 sm->eapol_status_cb = NULL;
5246 }
5247 #endif /* CONFIG_TESTING_OPTIONS */
5248 }
5249
5250
wpa_auth_uses_sae(struct wpa_state_machine *sm)5251 int wpa_auth_uses_sae(struct wpa_state_machine *sm)
5252 {
5253 if (!sm)
5254 return 0;
5255 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
5256 }
5257
5258
wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)5259 int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
5260 {
5261 if (!sm)
5262 return 0;
5263 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
5264 }
5265
5266
5267 #ifdef CONFIG_P2P
wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)5268 int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
5269 {
5270 if (!sm || WPA_GET_BE32(sm->ip_addr) == 0)
5271 return -1;
5272 os_memcpy(addr, sm->ip_addr, 4);
5273 return 0;
5274 }
5275 #endif /* CONFIG_P2P */
5276
5277
wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth, struct radius_das_attrs *attr)5278 int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
5279 struct radius_das_attrs *attr)
5280 {
5281 return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
5282 }
5283
5284
wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)5285 void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)
5286 {
5287 struct wpa_group *group;
5288
5289 if (!wpa_auth)
5290 return;
5291 for (group = wpa_auth->group; group; group = group->next)
5292 wpa_group_config_group_keys(wpa_auth, group);
5293 }
5294
5295
5296 #ifdef CONFIG_FILS
5297
5298 struct wpa_auth_fils_iter_data {
5299 struct wpa_authenticator *auth;
5300 const u8 *cache_id;
5301 struct rsn_pmksa_cache_entry *pmksa;
5302 const u8 *spa;
5303 const u8 *pmkid;
5304 };
5305
5306
wpa_auth_fils_iter(struct wpa_authenticator *a, void *ctx)5307 static int wpa_auth_fils_iter(struct wpa_authenticator *a, void *ctx)
5308 {
5309 struct wpa_auth_fils_iter_data *data = ctx;
5310
5311 if (a == data->auth || !a->conf.fils_cache_id_set ||
5312 os_memcmp(a->conf.fils_cache_id, data->cache_id,
5313 FILS_CACHE_ID_LEN) != 0)
5314 return 0;
5315 data->pmksa = pmksa_cache_auth_get(a->pmksa, data->spa, data->pmkid);
5316 return data->pmksa != NULL;
5317 }
5318
5319
5320 struct rsn_pmksa_cache_entry *
wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator *wpa_auth, const u8 *sta_addr, const u8 *pmkid)5321 wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator *wpa_auth,
5322 const u8 *sta_addr, const u8 *pmkid)
5323 {
5324 struct wpa_auth_fils_iter_data idata;
5325
5326 if (!wpa_auth->conf.fils_cache_id_set)
5327 return NULL;
5328 idata.auth = wpa_auth;
5329 idata.cache_id = wpa_auth->conf.fils_cache_id;
5330 idata.pmksa = NULL;
5331 idata.spa = sta_addr;
5332 idata.pmkid = pmkid;
5333 wpa_auth_for_each_auth(wpa_auth, wpa_auth_fils_iter, &idata);
5334 return idata.pmksa;
5335 }
5336
5337
5338 #ifdef CONFIG_IEEE80211R_AP
wpa_auth_write_fte(struct wpa_authenticator *wpa_auth, int use_sha384, u8 *buf, size_t len)5339 int wpa_auth_write_fte(struct wpa_authenticator *wpa_auth, int use_sha384,
5340 u8 *buf, size_t len)
5341 {
5342 struct wpa_auth_config *conf = &wpa_auth->conf;
5343
5344 return wpa_write_ftie(conf, use_sha384, conf->r0_key_holder,
5345 conf->r0_key_holder_len,
5346 NULL, NULL, buf, len, NULL, 0, 0);
5347 }
5348 #endif /* CONFIG_IEEE80211R_AP */
5349
5350
wpa_auth_get_fils_aead_params(struct wpa_state_machine *sm, u8 *fils_anonce, u8 *fils_snonce, u8 *fils_kek, size_t *fils_kek_len)5351 void wpa_auth_get_fils_aead_params(struct wpa_state_machine *sm,
5352 u8 *fils_anonce, u8 *fils_snonce,
5353 u8 *fils_kek, size_t *fils_kek_len)
5354 {
5355 os_memcpy(fils_anonce, sm->ANonce, WPA_NONCE_LEN);
5356 os_memcpy(fils_snonce, sm->SNonce, WPA_NONCE_LEN);
5357 os_memcpy(fils_kek, sm->PTK.kek, WPA_KEK_MAX_LEN);
5358 *fils_kek_len = sm->PTK.kek_len;
5359 }
5360
5361
wpa_auth_add_fils_pmk_pmkid(struct wpa_state_machine *sm, const u8 *pmk, size_t pmk_len, const u8 *pmkid)5362 void wpa_auth_add_fils_pmk_pmkid(struct wpa_state_machine *sm, const u8 *pmk,
5363 size_t pmk_len, const u8 *pmkid)
5364 {
5365 os_memcpy(sm->PMK, pmk, pmk_len);
5366 sm->pmk_len = pmk_len;
5367 os_memcpy(sm->pmkid, pmkid, PMKID_LEN);
5368 sm->pmkid_set = 1;
5369 }
5370
5371 #endif /* CONFIG_FILS */
5372
5373
wpa_auth_set_auth_alg(struct wpa_state_machine *sm, u16 auth_alg)5374 void wpa_auth_set_auth_alg(struct wpa_state_machine *sm, u16 auth_alg)
5375 {
5376 if (sm)
5377 sm->auth_alg = auth_alg;
5378 }
5379
5380
5381 #ifdef CONFIG_DPP2
wpa_auth_set_dpp_z(struct wpa_state_machine *sm, const struct wpabuf *z)5382 void wpa_auth_set_dpp_z(struct wpa_state_machine *sm, const struct wpabuf *z)
5383 {
5384 if (sm) {
5385 wpabuf_clear_free(sm->dpp_z);
5386 sm->dpp_z = z ? wpabuf_dup(z) : NULL;
5387 }
5388 }
5389 #endif /* CONFIG_DPP2 */
5390
5391
wpa_auth_set_transition_disable(struct wpa_authenticator *wpa_auth, u8 val)5392 void wpa_auth_set_transition_disable(struct wpa_authenticator *wpa_auth,
5393 u8 val)
5394 {
5395 if (wpa_auth)
5396 wpa_auth->conf.transition_disable = val;
5397 }
5398
5399
5400 #ifdef CONFIG_TESTING_OPTIONS
5401
wpa_auth_resend_m1(struct wpa_state_machine *sm, int change_anonce, void (*cb)(void *ctx1, void *ctx2), void *ctx1, void *ctx2)5402 int wpa_auth_resend_m1(struct wpa_state_machine *sm, int change_anonce,
5403 void (*cb)(void *ctx1, void *ctx2),
5404 void *ctx1, void *ctx2)
5405 {
5406 const u8 *anonce = sm->ANonce;
5407 u8 anonce_buf[WPA_NONCE_LEN];
5408
5409 if (change_anonce) {
5410 if (random_get_bytes(anonce_buf, WPA_NONCE_LEN))
5411 return -1;
5412 anonce = anonce_buf;
5413 }
5414
5415 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
5416 "sending 1/4 msg of 4-Way Handshake (TESTING)");
5417 wpa_send_eapol(sm->wpa_auth, sm,
5418 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
5419 anonce, NULL, 0, 0, 0);
5420 return 0;
5421 }
5422
5423
wpa_auth_resend_m3(struct wpa_state_machine *sm, void (*cb)(void *ctx1, void *ctx2), void *ctx1, void *ctx2)5424 int wpa_auth_resend_m3(struct wpa_state_machine *sm,
5425 void (*cb)(void *ctx1, void *ctx2),
5426 void *ctx1, void *ctx2)
5427 {
5428 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
5429 u8 *opos;
5430 size_t gtk_len, kde_len;
5431 struct wpa_auth_config *conf = &sm->wpa_auth->conf;
5432 struct wpa_group *gsm = sm->group;
5433 u8 *wpa_ie;
5434 int wpa_ie_len, secure, gtkidx, encr = 0;
5435 u8 hdr[2];
5436
5437 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
5438 GTK[GN], IGTK, [BIGTK], [FTIE], [TIE * 2])
5439 */
5440
5441 /* Use 0 RSC */
5442 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
5443 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
5444 wpa_ie = sm->wpa_auth->wpa_ie;
5445 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
5446 if (sm->wpa == WPA_VERSION_WPA &&
5447 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
5448 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
5449 /* WPA-only STA, remove RSN IE and possible MDIE */
5450 wpa_ie = wpa_ie + wpa_ie[1] + 2;
5451 if (wpa_ie[0] == WLAN_EID_RSNX)
5452 wpa_ie = wpa_ie + wpa_ie[1] + 2;
5453 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
5454 wpa_ie = wpa_ie + wpa_ie[1] + 2;
5455 wpa_ie_len = wpa_ie[1] + 2;
5456 }
5457 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
5458 "sending 3/4 msg of 4-Way Handshake (TESTING)");
5459 if (sm->wpa == WPA_VERSION_WPA2) {
5460 /* WPA2 send GTK in the 4-way handshake */
5461 secure = 1;
5462 gtk = gsm->GTK[gsm->GN - 1];
5463 gtk_len = gsm->GTK_len;
5464 gtkidx = gsm->GN;
5465 _rsc = rsc;
5466 encr = 1;
5467 } else {
5468 /* WPA does not include GTK in msg 3/4 */
5469 secure = 0;
5470 gtk = NULL;
5471 gtk_len = 0;
5472 _rsc = NULL;
5473 if (sm->rx_eapol_key_secure) {
5474 /*
5475 * It looks like Windows 7 supplicant tries to use
5476 * Secure bit in msg 2/4 after having reported Michael
5477 * MIC failure and it then rejects the 4-way handshake
5478 * if msg 3/4 does not set Secure bit. Work around this
5479 * by setting the Secure bit here even in the case of
5480 * WPA if the supplicant used it first.
5481 */
5482 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
5483 "STA used Secure bit in WPA msg 2/4 - set Secure for 3/4 as workaround");
5484 secure = 1;
5485 }
5486 }
5487
5488 kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
5489
5490 if (sm->use_ext_key_id)
5491 kde_len += 2 + RSN_SELECTOR_LEN + 2;
5492
5493 if (gtk)
5494 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
5495 #ifdef CONFIG_IEEE80211R_AP
5496 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
5497 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
5498 kde_len += 300; /* FTIE + 2 * TIE */
5499 }
5500 #endif /* CONFIG_IEEE80211R_AP */
5501 kde = os_malloc(kde_len);
5502 if (!kde)
5503 return -1;
5504
5505 pos = kde;
5506 os_memcpy(pos, wpa_ie, wpa_ie_len);
5507 pos += wpa_ie_len;
5508 #ifdef CONFIG_IEEE80211R_AP
5509 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
5510 int res;
5511 size_t elen;
5512
5513 elen = pos - kde;
5514 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
5515 if (res < 0) {
5516 wpa_printf(MSG_ERROR,
5517 "FT: Failed to insert PMKR1Name into RSN IE in EAPOL-Key data");
5518 os_free(kde);
5519 return -1;
5520 }
5521 pos -= wpa_ie_len;
5522 pos += elen;
5523 }
5524 #endif /* CONFIG_IEEE80211R_AP */
5525 hdr[1] = 0;
5526
5527 if (sm->use_ext_key_id) {
5528 hdr[0] = sm->keyidx_active & 0x01;
5529 pos = wpa_add_kde(pos, RSN_KEY_DATA_KEYID, hdr, 2, NULL, 0);
5530 }
5531
5532 if (gtk) {
5533 hdr[0] = gtkidx & 0x03;
5534 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
5535 gtk, gtk_len);
5536 }
5537 opos = pos;
5538 pos = ieee80211w_kde_add(sm, pos);
5539 if (pos - opos >= 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
5540 /* skip KDE header and keyid */
5541 opos += 2 + RSN_SELECTOR_LEN + 2;
5542 os_memset(opos, 0, 6); /* clear PN */
5543 }
5544 if (ocv_oci_add(sm, &pos, conf->oci_freq_override_eapol_m3) < 0) {
5545 os_free(kde);
5546 return -1;
5547 }
5548
5549 #ifdef CONFIG_IEEE80211R_AP
5550 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
5551 int res;
5552
5553 if (sm->assoc_resp_ftie &&
5554 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
5555 os_memcpy(pos, sm->assoc_resp_ftie,
5556 2 + sm->assoc_resp_ftie[1]);
5557 res = 2 + sm->assoc_resp_ftie[1];
5558 } else {
5559 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
5560
5561 res = wpa_write_ftie(conf, use_sha384,
5562 conf->r0_key_holder,
5563 conf->r0_key_holder_len,
5564 NULL, NULL, pos,
5565 kde + kde_len - pos,
5566 NULL, 0, 0);
5567 }
5568 if (res < 0) {
5569 wpa_printf(MSG_ERROR,
5570 "FT: Failed to insert FTIE into EAPOL-Key Key Data");
5571 os_free(kde);
5572 return -1;
5573 }
5574 pos += res;
5575
5576 /* TIE[ReassociationDeadline] (TU) */
5577 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
5578 *pos++ = 5;
5579 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
5580 WPA_PUT_LE32(pos, conf->reassociation_deadline);
5581 pos += 4;
5582
5583 /* TIE[KeyLifetime] (seconds) */
5584 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
5585 *pos++ = 5;
5586 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
5587 WPA_PUT_LE32(pos, conf->r0_key_lifetime);
5588 pos += 4;
5589 }
5590 #endif /* CONFIG_IEEE80211R_AP */
5591
5592 wpa_send_eapol(sm->wpa_auth, sm,
5593 (secure ? WPA_KEY_INFO_SECURE : 0) |
5594 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
5595 WPA_KEY_INFO_MIC : 0) |
5596 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
5597 WPA_KEY_INFO_KEY_TYPE,
5598 _rsc, sm->ANonce, kde, pos - kde, 0, encr);
5599 os_free(kde);
5600 return 0;
5601 }
5602
5603
wpa_auth_resend_group_m1(struct wpa_state_machine *sm, void (*cb)(void *ctx1, void *ctx2), void *ctx1, void *ctx2)5604 int wpa_auth_resend_group_m1(struct wpa_state_machine *sm,
5605 void (*cb)(void *ctx1, void *ctx2),
5606 void *ctx1, void *ctx2)
5607 {
5608 u8 rsc[WPA_KEY_RSC_LEN];
5609 struct wpa_auth_config *conf = &sm->wpa_auth->conf;
5610 struct wpa_group *gsm = sm->group;
5611 const u8 *kde;
5612 u8 *kde_buf = NULL, *pos, hdr[2];
5613 u8 *opos;
5614 size_t kde_len;
5615 u8 *gtk;
5616
5617 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
5618 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
5619 /* Use 0 RSC */
5620 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
5621 "sending 1/2 msg of Group Key Handshake (TESTING)");
5622
5623 gtk = gsm->GTK[gsm->GN - 1];
5624 if (sm->wpa == WPA_VERSION_WPA2) {
5625 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
5626 ieee80211w_kde_len(sm) + ocv_oci_len(sm);
5627 kde_buf = os_malloc(kde_len);
5628 if (!kde_buf)
5629 return -1;
5630
5631 kde = pos = kde_buf;
5632 hdr[0] = gsm->GN & 0x03;
5633 hdr[1] = 0;
5634 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
5635 gtk, gsm->GTK_len);
5636 opos = pos;
5637 pos = ieee80211w_kde_add(sm, pos);
5638 if (pos - opos >=
5639 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
5640 /* skip KDE header and keyid */
5641 opos += 2 + RSN_SELECTOR_LEN + 2;
5642 os_memset(opos, 0, 6); /* clear PN */
5643 }
5644 if (ocv_oci_add(sm, &pos,
5645 conf->oci_freq_override_eapol_g1) < 0) {
5646 os_free(kde_buf);
5647 return -1;
5648 }
5649 kde_len = pos - kde;
5650 } else {
5651 kde = gtk;
5652 kde_len = gsm->GTK_len;
5653 }
5654
5655 sm->eapol_status_cb = cb;
5656 sm->eapol_status_cb_ctx1 = ctx1;
5657 sm->eapol_status_cb_ctx2 = ctx2;
5658
5659 wpa_send_eapol(sm->wpa_auth, sm,
5660 WPA_KEY_INFO_SECURE |
5661 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
5662 WPA_KEY_INFO_MIC : 0) |
5663 WPA_KEY_INFO_ACK |
5664 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
5665 rsc, NULL, kde, kde_len, gsm->GN, 1);
5666
5667 os_free(kde_buf);
5668 return 0;
5669 }
5670
5671
wpa_auth_rekey_gtk(struct wpa_authenticator *wpa_auth)5672 int wpa_auth_rekey_gtk(struct wpa_authenticator *wpa_auth)
5673 {
5674 if (!wpa_auth)
5675 return -1;
5676 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
5677 return eloop_register_timeout(0, 0, wpa_rekey_gtk, wpa_auth, NULL);
5678 }
5679
5680
wpa_auth_rekey_ptk(struct wpa_authenticator *wpa_auth, struct wpa_state_machine *sm)5681 int wpa_auth_rekey_ptk(struct wpa_authenticator *wpa_auth,
5682 struct wpa_state_machine *sm)
5683 {
5684 if (!wpa_auth || !sm)
5685 return -1;
5686 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
5687 wpa_request_new_ptk(sm);
5688 wpa_sm_step(sm);
5689 return 0;
5690 }
5691
5692
wpa_auth_set_ft_rsnxe_used(struct wpa_authenticator *wpa_auth, int val)5693 void wpa_auth_set_ft_rsnxe_used(struct wpa_authenticator *wpa_auth, int val)
5694 {
5695 if (wpa_auth)
5696 wpa_auth->conf.ft_rsnxe_used = val;
5697 }
5698
5699
wpa_auth_set_ocv_override_freq(struct wpa_authenticator *wpa_auth, enum wpa_auth_ocv_override_frame frame, unsigned int freq)5700 void wpa_auth_set_ocv_override_freq(struct wpa_authenticator *wpa_auth,
5701 enum wpa_auth_ocv_override_frame frame,
5702 unsigned int freq)
5703 {
5704 if (!wpa_auth)
5705 return;
5706 switch (frame) {
5707 case WPA_AUTH_OCV_OVERRIDE_EAPOL_M3:
5708 wpa_auth->conf.oci_freq_override_eapol_m3 = freq;
5709 break;
5710 case WPA_AUTH_OCV_OVERRIDE_EAPOL_G1:
5711 wpa_auth->conf.oci_freq_override_eapol_g1 = freq;
5712 break;
5713 case WPA_AUTH_OCV_OVERRIDE_FT_ASSOC:
5714 wpa_auth->conf.oci_freq_override_ft_assoc = freq;
5715 break;
5716 case WPA_AUTH_OCV_OVERRIDE_FILS_ASSOC:
5717 wpa_auth->conf.oci_freq_override_fils_assoc = freq;
5718 break;
5719 }
5720 }
5721
5722 #endif /* CONFIG_TESTING_OPTIONS */
5723