1 /*
2 * IEEE 802.11 RSN / WPA Authenticator
3 * Copyright (c) 2004-2022, 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/sha512.h"
27 #include "crypto/random.h"
28 #include "eapol_auth/eapol_auth_sm.h"
29 #include "drivers/driver.h"
30 #include "ap_config.h"
31 #include "ieee802_11.h"
32 #include "sta_info.h"
33 #include "wpa_auth.h"
34 #include "pmksa_cache_auth.h"
35 #include "wpa_auth_i.h"
36 #include "wpa_auth_ie.h"
37
38 #define STATE_MACHINE_DATA struct wpa_state_machine
39 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
40 #define STATE_MACHINE_ADDR wpa_auth_get_spa(sm)
41
42
43 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
44 static int wpa_sm_step(struct wpa_state_machine *sm);
45 static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK,
46 u8 *data, size_t data_len);
47 #ifdef CONFIG_FILS
48 static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
49 u8 *buf, size_t buf_len, u16 *_key_data_len);
50 static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm,
51 const struct wpabuf *hlp);
52 #endif /* CONFIG_FILS */
53 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
54 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
55 struct wpa_group *group);
56 static void wpa_request_new_ptk(struct wpa_state_machine *sm);
57 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
58 struct wpa_group *group);
59 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
60 struct wpa_group *group);
61 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
62 const u8 *pmk, unsigned int pmk_len,
63 struct wpa_ptk *ptk, int force_sha256,
64 u8 *pmk_r0, u8 *pmk_r1, u8 *pmk_r0_name,
65 size_t *key_len, bool no_kdk);
66 static void wpa_group_free(struct wpa_authenticator *wpa_auth,
67 struct wpa_group *group);
68 static void wpa_group_get(struct wpa_authenticator *wpa_auth,
69 struct wpa_group *group);
70 static void wpa_group_put(struct wpa_authenticator *wpa_auth,
71 struct wpa_group *group);
72 static int ieee80211w_kde_len(struct wpa_state_machine *sm);
73 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos);
74 static void wpa_group_update_gtk(struct wpa_authenticator *wpa_auth,
75 struct wpa_group *group);
76
77
78 static const u32 eapol_key_timeout_first = 100; /* ms */
79 static const u32 eapol_key_timeout_subseq = 1000; /* ms */
80 static const u32 eapol_key_timeout_first_group = 500; /* ms */
81 static const u32 eapol_key_timeout_no_retrans = 4000; /* ms */
82
83 /* TODO: make these configurable */
84 static const int dot11RSNAConfigPMKLifetime = 43200;
85 static const int dot11RSNAConfigPMKReauthThreshold = 70;
86 static const int dot11RSNAConfigSATimeout = 60;
87
88
wpa_auth_get_aa(const struct wpa_state_machine * sm)89 static const u8 * wpa_auth_get_aa(const struct wpa_state_machine *sm)
90 {
91 #ifdef CONFIG_IEEE80211BE
92 if (sm->mld_assoc_link_id >= 0)
93 return sm->wpa_auth->mld_addr;
94 #endif /* CONFIG_IEEE80211BE */
95 return sm->wpa_auth->addr;
96 }
97
98
wpa_auth_get_spa(const struct wpa_state_machine * sm)99 static const u8 * wpa_auth_get_spa(const struct wpa_state_machine *sm)
100 {
101 #ifdef CONFIG_IEEE80211BE
102 if (sm->mld_assoc_link_id >= 0)
103 return sm->peer_mld_addr;
104 #endif /* CONFIG_IEEE80211BE */
105 return sm->addr;
106 }
107
108
wpa_gkeydone_sta(struct wpa_state_machine * sm)109 static void wpa_gkeydone_sta(struct wpa_state_machine *sm)
110 {
111 #ifdef CONFIG_IEEE80211BE
112 int link_id;
113 #endif /* CONFIG_IEEE80211BE */
114
115 if (!sm->wpa_auth)
116 return;
117
118 sm->wpa_auth->group->GKeyDoneStations--;
119 sm->GUpdateStationKeys = false;
120
121 #ifdef CONFIG_IEEE80211BE
122 for_each_sm_auth(sm, link_id)
123 sm->mld_links[link_id].wpa_auth->group->GKeyDoneStations--;
124 #endif /* CONFIG_IEEE80211BE */
125 }
126
127
128 #ifdef CONFIG_IEEE80211BE
129
wpa_release_link_auth_ref(struct wpa_state_machine * sm,int release_link_id)130 void wpa_release_link_auth_ref(struct wpa_state_machine *sm,
131 int release_link_id)
132 {
133 int link_id;
134
135 if (!sm || release_link_id >= MAX_NUM_MLD_LINKS)
136 return;
137
138 for_each_sm_auth(sm, link_id) {
139 if (link_id == release_link_id) {
140 wpa_group_put(sm->mld_links[link_id].wpa_auth,
141 sm->mld_links[link_id].wpa_auth->group);
142 sm->mld_links[link_id].wpa_auth = NULL;
143 }
144 }
145 }
146
147
148 struct wpa_get_link_auth_ctx {
149 const u8 *addr;
150 const u8 *mld_addr;
151 int link_id;
152 struct wpa_authenticator *wpa_auth;
153 };
154
wpa_get_link_sta_auth(struct wpa_authenticator * wpa_auth,void * data)155 static int wpa_get_link_sta_auth(struct wpa_authenticator *wpa_auth, void *data)
156 {
157 struct wpa_get_link_auth_ctx *ctx = data;
158
159 if (!wpa_auth->is_ml)
160 return 0;
161
162 if (ctx->mld_addr &&
163 !ether_addr_equal(wpa_auth->mld_addr, ctx->mld_addr))
164 return 0;
165
166 if ((ctx->addr && ether_addr_equal(wpa_auth->addr, ctx->addr)) ||
167 (ctx->link_id > -1 && wpa_auth->is_ml &&
168 wpa_auth->link_id == ctx->link_id)) {
169 ctx->wpa_auth = wpa_auth;
170 return 1;
171
172 }
173 return 0;
174 }
175
176
177 static struct wpa_authenticator *
wpa_get_link_auth(struct wpa_authenticator * wpa_auth,int link_id)178 wpa_get_link_auth(struct wpa_authenticator *wpa_auth, int link_id)
179 {
180 struct wpa_get_link_auth_ctx ctx;
181
182 ctx.addr = NULL;
183 ctx.mld_addr = wpa_auth->mld_addr;
184 ctx.link_id = link_id;
185 ctx.wpa_auth = NULL;
186 wpa_auth_for_each_auth(wpa_auth, wpa_get_link_sta_auth, &ctx);
187 return ctx.wpa_auth;
188 }
189
190
wpa_get_primary_auth_cb(struct wpa_authenticator * wpa_auth,void * data)191 static int wpa_get_primary_auth_cb(struct wpa_authenticator *wpa_auth,
192 void *data)
193 {
194 struct wpa_get_link_auth_ctx *ctx = data;
195
196 if (!wpa_auth->is_ml ||
197 !ether_addr_equal(wpa_auth->mld_addr, ctx->addr) ||
198 !wpa_auth->primary_auth)
199 return 0;
200
201 ctx->wpa_auth = wpa_auth;
202 return 1;
203 }
204
205 #endif /* CONFIG_IEEE80211BE */
206
207
208 static struct wpa_authenticator *
wpa_get_primary_auth(struct wpa_authenticator * wpa_auth)209 wpa_get_primary_auth(struct wpa_authenticator *wpa_auth)
210 {
211 #ifdef CONFIG_IEEE80211BE
212 struct wpa_get_link_auth_ctx ctx;
213
214 if (!wpa_auth || !wpa_auth->is_ml || wpa_auth->primary_auth)
215 return wpa_auth;
216
217 ctx.addr = wpa_auth->mld_addr;
218 ctx.wpa_auth = NULL;
219 wpa_auth_for_each_auth(wpa_auth, wpa_get_primary_auth_cb, &ctx);
220
221 return ctx.wpa_auth;
222 #else /* CONFIG_IEEE80211BE */
223 return wpa_auth;
224 #endif /* CONFIG_IEEE80211BE */
225 }
226
227
wpa_auth_mic_failure_report(struct wpa_authenticator * wpa_auth,const u8 * addr)228 static inline int wpa_auth_mic_failure_report(
229 struct wpa_authenticator *wpa_auth, const u8 *addr)
230 {
231 if (wpa_auth->cb->mic_failure_report)
232 return wpa_auth->cb->mic_failure_report(wpa_auth->cb_ctx, addr);
233 return 0;
234 }
235
236
wpa_auth_psk_failure_report(struct wpa_authenticator * wpa_auth,const u8 * addr)237 static inline void wpa_auth_psk_failure_report(
238 struct wpa_authenticator *wpa_auth, const u8 *addr)
239 {
240 if (wpa_auth->cb->psk_failure_report)
241 wpa_auth->cb->psk_failure_report(wpa_auth->cb_ctx, addr);
242 }
243
244
wpa_auth_set_eapol(struct wpa_authenticator * wpa_auth,const u8 * addr,wpa_eapol_variable var,int value)245 static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
246 const u8 *addr, wpa_eapol_variable var,
247 int value)
248 {
249 if (wpa_auth->cb->set_eapol)
250 wpa_auth->cb->set_eapol(wpa_auth->cb_ctx, addr, var, value);
251 }
252
253
wpa_auth_get_eapol(struct wpa_authenticator * wpa_auth,const u8 * addr,wpa_eapol_variable var)254 static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
255 const u8 *addr, wpa_eapol_variable var)
256 {
257 if (!wpa_auth->cb->get_eapol)
258 return -1;
259 return wpa_auth->cb->get_eapol(wpa_auth->cb_ctx, addr, var);
260 }
261
262
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)263 static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
264 const u8 *addr,
265 const u8 *p2p_dev_addr,
266 const u8 *prev_psk, size_t *psk_len,
267 int *vlan_id)
268 {
269 if (!wpa_auth->cb->get_psk)
270 return NULL;
271 return wpa_auth->cb->get_psk(wpa_auth->cb_ctx, addr, p2p_dev_addr,
272 prev_psk, psk_len, vlan_id);
273 }
274
275
wpa_auth_get_msk(struct wpa_authenticator * wpa_auth,const u8 * addr,u8 * msk,size_t * len)276 static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
277 const u8 *addr, u8 *msk, size_t *len)
278 {
279 if (!wpa_auth->cb->get_msk)
280 return -1;
281 return wpa_auth->cb->get_msk(wpa_auth->cb_ctx, addr, msk, len);
282 }
283
284
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)285 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
286 int vlan_id,
287 enum wpa_alg alg, const u8 *addr, int idx,
288 u8 *key, size_t key_len,
289 enum key_flag key_flag)
290 {
291 if (!wpa_auth->cb->set_key)
292 return -1;
293 return wpa_auth->cb->set_key(wpa_auth->cb_ctx, vlan_id, alg, addr, idx,
294 key, key_len, key_flag);
295 }
296
297
298 #ifdef CONFIG_PASN
wpa_auth_set_ltf_keyseed(struct wpa_authenticator * wpa_auth,const u8 * peer_addr,const u8 * ltf_keyseed,size_t ltf_keyseed_len)299 static inline int wpa_auth_set_ltf_keyseed(struct wpa_authenticator *wpa_auth,
300 const u8 *peer_addr,
301 const u8 *ltf_keyseed,
302 size_t ltf_keyseed_len)
303 {
304 if (!wpa_auth->cb->set_ltf_keyseed)
305 return -1;
306 return wpa_auth->cb->set_ltf_keyseed(wpa_auth->cb_ctx, peer_addr,
307 ltf_keyseed, ltf_keyseed_len);
308 }
309 #endif /* CONFIG_PASN */
310
311
wpa_auth_get_seqnum(struct wpa_authenticator * wpa_auth,const u8 * addr,int idx,u8 * seq)312 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
313 const u8 *addr, int idx, u8 *seq)
314 {
315 int res;
316
317 if (!wpa_auth->cb->get_seqnum)
318 return -1;
319 #ifdef CONFIG_TESTING_OPTIONS
320 os_memset(seq, 0, WPA_KEY_RSC_LEN);
321 #endif /* CONFIG_TESTING_OPTIONS */
322 res = wpa_auth->cb->get_seqnum(wpa_auth->cb_ctx, addr, idx, seq);
323 #ifdef CONFIG_TESTING_OPTIONS
324 if (!addr && idx < 4 && wpa_auth->conf.gtk_rsc_override_set) {
325 wpa_printf(MSG_DEBUG,
326 "TESTING: Override GTK RSC %016llx --> %016llx",
327 (long long unsigned) WPA_GET_LE64(seq),
328 (long long unsigned)
329 WPA_GET_LE64(wpa_auth->conf.gtk_rsc_override));
330 os_memcpy(seq, wpa_auth->conf.gtk_rsc_override,
331 WPA_KEY_RSC_LEN);
332 }
333 if (!addr && idx >= 4 && idx <= 5 &&
334 wpa_auth->conf.igtk_rsc_override_set) {
335 wpa_printf(MSG_DEBUG,
336 "TESTING: Override IGTK RSC %016llx --> %016llx",
337 (long long unsigned) WPA_GET_LE64(seq),
338 (long long unsigned)
339 WPA_GET_LE64(wpa_auth->conf.igtk_rsc_override));
340 os_memcpy(seq, wpa_auth->conf.igtk_rsc_override,
341 WPA_KEY_RSC_LEN);
342 }
343 #endif /* CONFIG_TESTING_OPTIONS */
344 return res;
345 }
346
347
348 static inline int
wpa_auth_send_eapol(struct wpa_authenticator * wpa_auth,const u8 * addr,const u8 * data,size_t data_len,int encrypt)349 wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
350 const u8 *data, size_t data_len, int encrypt)
351 {
352 if (!wpa_auth->cb->send_eapol)
353 return -1;
354 return wpa_auth->cb->send_eapol(wpa_auth->cb_ctx, addr, data, data_len,
355 encrypt);
356 }
357
358
359 #ifdef CONFIG_MESH
wpa_auth_start_ampe(struct wpa_authenticator * wpa_auth,const u8 * addr)360 static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth,
361 const u8 *addr)
362 {
363 if (!wpa_auth->cb->start_ampe)
364 return -1;
365 return wpa_auth->cb->start_ampe(wpa_auth->cb_ctx, addr);
366 }
367 #endif /* CONFIG_MESH */
368
369
wpa_auth_for_each_sta(struct wpa_authenticator * wpa_auth,int (* cb)(struct wpa_state_machine * sm,void * ctx),void * cb_ctx)370 int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
371 int (*cb)(struct wpa_state_machine *sm, void *ctx),
372 void *cb_ctx)
373 {
374 if (!wpa_auth->cb->for_each_sta)
375 return 0;
376 return wpa_auth->cb->for_each_sta(wpa_auth->cb_ctx, cb, cb_ctx);
377 }
378
379
wpa_auth_for_each_auth(struct wpa_authenticator * wpa_auth,int (* cb)(struct wpa_authenticator * a,void * ctx),void * cb_ctx)380 int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
381 int (*cb)(struct wpa_authenticator *a, void *ctx),
382 void *cb_ctx)
383 {
384 if (!wpa_auth->cb->for_each_auth)
385 return 0;
386 return wpa_auth->cb->for_each_auth(wpa_auth->cb_ctx, cb, cb_ctx);
387 }
388
389
wpa_auth_store_ptksa(struct wpa_authenticator * wpa_auth,const u8 * addr,int cipher,u32 life_time,const struct wpa_ptk * ptk)390 void wpa_auth_store_ptksa(struct wpa_authenticator *wpa_auth,
391 const u8 *addr, int cipher,
392 u32 life_time, const struct wpa_ptk *ptk)
393 {
394 if (wpa_auth->cb->store_ptksa)
395 wpa_auth->cb->store_ptksa(wpa_auth->cb_ctx, addr, cipher,
396 life_time, ptk);
397 }
398
399
wpa_auth_remove_ptksa(struct wpa_authenticator * wpa_auth,const u8 * addr,int cipher)400 static void wpa_auth_remove_ptksa(struct wpa_authenticator *wpa_auth,
401 const u8 *addr, int cipher)
402 {
403 if (wpa_auth->cb->clear_ptksa)
404 wpa_auth->cb->clear_ptksa(wpa_auth->cb_ctx, addr, cipher);
405 }
406
407
wpa_auth_logger(struct wpa_authenticator * wpa_auth,const u8 * addr,logger_level level,const char * txt)408 void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
409 logger_level level, const char *txt)
410 {
411 if (!wpa_auth->cb->logger)
412 return;
413 wpa_auth->cb->logger(wpa_auth->cb_ctx, addr, level, txt);
414 }
415
416
wpa_auth_vlogger(struct wpa_authenticator * wpa_auth,const u8 * addr,logger_level level,const char * fmt,...)417 void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
418 logger_level level, const char *fmt, ...)
419 {
420 char *format;
421 int maxlen;
422 va_list ap;
423
424 if (!wpa_auth->cb->logger)
425 return;
426
427 maxlen = os_strlen(fmt) + 100;
428 format = os_malloc(maxlen);
429 if (!format)
430 return;
431
432 va_start(ap, fmt);
433 vsnprintf(format, maxlen, fmt, ap);
434 va_end(ap);
435
436 wpa_auth_logger(wpa_auth, addr, level, format);
437
438 os_free(format);
439 }
440
441
wpa_sta_disconnect(struct wpa_authenticator * wpa_auth,const u8 * addr,u16 reason)442 static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
443 const u8 *addr, u16 reason)
444 {
445 if (!wpa_auth->cb->disconnect)
446 return;
447 wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR " (reason %u)",
448 MAC2STR(addr), reason);
449 wpa_auth->cb->disconnect(wpa_auth->cb_ctx, addr, reason);
450 }
451
452
453 #ifdef CONFIG_OCV
wpa_channel_info(struct wpa_authenticator * wpa_auth,struct wpa_channel_info * ci)454 static int wpa_channel_info(struct wpa_authenticator *wpa_auth,
455 struct wpa_channel_info *ci)
456 {
457 if (!wpa_auth->cb->channel_info)
458 return -1;
459 return wpa_auth->cb->channel_info(wpa_auth->cb_ctx, ci);
460 }
461 #endif /* CONFIG_OCV */
462
463
wpa_auth_update_vlan(struct wpa_authenticator * wpa_auth,const u8 * addr,int vlan_id)464 static int wpa_auth_update_vlan(struct wpa_authenticator *wpa_auth,
465 const u8 *addr, int vlan_id)
466 {
467 if (!wpa_auth->cb->update_vlan)
468 return -1;
469 return wpa_auth->cb->update_vlan(wpa_auth->cb_ctx, addr, vlan_id);
470 }
471
472
wpa_rekey_gmk(void * eloop_ctx,void * timeout_ctx)473 static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
474 {
475 struct wpa_authenticator *wpa_auth = eloop_ctx;
476
477 if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
478 wpa_printf(MSG_ERROR,
479 "Failed to get random data for WPA initialization.");
480 } else {
481 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
482 wpa_hexdump_key(MSG_DEBUG, "GMK",
483 wpa_auth->group->GMK, WPA_GMK_LEN);
484 }
485
486 if (wpa_auth->conf.wpa_gmk_rekey) {
487 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
488 wpa_rekey_gmk, wpa_auth, NULL);
489 }
490 }
491
492
wpa_rekey_all_groups(struct wpa_authenticator * wpa_auth)493 static void wpa_rekey_all_groups(struct wpa_authenticator *wpa_auth)
494 {
495 struct wpa_group *group, *next;
496
497 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
498 group = wpa_auth->group;
499 while (group) {
500 wpa_printf(MSG_DEBUG, "GTK rekey start for authenticator ("
501 MACSTR "), group vlan %d",
502 MAC2STR(wpa_auth->addr), group->vlan_id);
503 wpa_group_get(wpa_auth, group);
504
505 group->GTKReKey = true;
506 do {
507 group->changed = false;
508 wpa_group_sm_step(wpa_auth, group);
509 } while (group->changed);
510
511 next = group->next;
512 wpa_group_put(wpa_auth, group);
513 group = next;
514 }
515 }
516
517
518 #ifdef CONFIG_IEEE80211BE
519
wpa_update_all_gtks(struct wpa_authenticator * wpa_auth)520 static void wpa_update_all_gtks(struct wpa_authenticator *wpa_auth)
521 {
522 struct wpa_group *group, *next;
523
524 group = wpa_auth->group;
525 while (group) {
526 wpa_group_get(wpa_auth, group);
527
528 wpa_group_update_gtk(wpa_auth, group);
529 next = group->next;
530 wpa_group_put(wpa_auth, group);
531 group = next;
532 }
533 }
534
535
wpa_update_all_gtks_cb(struct wpa_authenticator * wpa_auth,void * ctx)536 static int wpa_update_all_gtks_cb(struct wpa_authenticator *wpa_auth, void *ctx)
537 {
538 const u8 *mld_addr = ctx;
539
540 if (!ether_addr_equal(wpa_auth->mld_addr, mld_addr))
541 return 0;
542
543 wpa_update_all_gtks(wpa_auth);
544 return 0;
545 }
546
547
wpa_rekey_all_groups_cb(struct wpa_authenticator * wpa_auth,void * ctx)548 static int wpa_rekey_all_groups_cb(struct wpa_authenticator *wpa_auth,
549 void *ctx)
550 {
551 const u8 *mld_addr = ctx;
552
553 if (!ether_addr_equal(wpa_auth->mld_addr, mld_addr))
554 return 0;
555
556 wpa_rekey_all_groups(wpa_auth);
557 return 0;
558 }
559
560 #endif /* CONFIG_IEEE80211BE */
561
562
wpa_rekey_gtk(void * eloop_ctx,void * timeout_ctx)563 static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
564 {
565 struct wpa_authenticator *wpa_auth = eloop_ctx;
566
567 #ifdef CONFIG_IEEE80211BE
568 if (wpa_auth->is_ml) {
569 /* Non-primary ML authenticator eloop timer for group rekey is
570 * never started and shouldn't fire. Check and warn just in
571 * case. */
572 if (!wpa_auth->primary_auth) {
573 wpa_printf(MSG_DEBUG,
574 "RSN: Cannot start GTK rekey on non-primary ML authenticator");
575 return;
576 }
577
578 /* Generate all the new group keys */
579 wpa_auth_for_each_auth(wpa_auth, wpa_update_all_gtks_cb,
580 wpa_auth->mld_addr);
581
582 /* Send all the generated group keys to the respective stations
583 * with group key handshake. */
584 wpa_auth_for_each_auth(wpa_auth, wpa_rekey_all_groups_cb,
585 wpa_auth->mld_addr);
586 } else {
587 wpa_rekey_all_groups(wpa_auth);
588 }
589 #else /* CONFIG_IEEE80211BE */
590 wpa_rekey_all_groups(wpa_auth);
591 #endif /* CONFIG_IEEE80211BE */
592
593 if (wpa_auth->conf.wpa_group_rekey) {
594 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
595 0, wpa_rekey_gtk, wpa_auth, NULL);
596 }
597 }
598
599
wpa_rekey_ptk(void * eloop_ctx,void * timeout_ctx)600 static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
601 {
602 struct wpa_authenticator *wpa_auth = eloop_ctx;
603 struct wpa_state_machine *sm = timeout_ctx;
604
605 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
606 "rekeying PTK");
607 wpa_request_new_ptk(sm);
608 wpa_sm_step(sm);
609 }
610
611
wpa_auth_set_ptk_rekey_timer(struct wpa_state_machine * sm)612 void wpa_auth_set_ptk_rekey_timer(struct wpa_state_machine *sm)
613 {
614 if (sm && sm->wpa_auth->conf.wpa_ptk_rekey) {
615 wpa_printf(MSG_DEBUG, "WPA: Start PTK rekeying timer for "
616 MACSTR " (%d seconds)",
617 MAC2STR(wpa_auth_get_spa(sm)),
618 sm->wpa_auth->conf.wpa_ptk_rekey);
619 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
620 eloop_register_timeout(sm->wpa_auth->conf.wpa_ptk_rekey, 0,
621 wpa_rekey_ptk, sm->wpa_auth, sm);
622 }
623 }
624
625
wpa_auth_pmksa_clear_cb(struct wpa_state_machine * sm,void * ctx)626 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
627 {
628 if (sm->pmksa == ctx)
629 sm->pmksa = NULL;
630 return 0;
631 }
632
633
wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry * entry,void * ctx)634 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
635 void *ctx)
636 {
637 struct wpa_authenticator *wpa_auth = ctx;
638 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
639 }
640
641
wpa_group_init_gmk_and_counter(struct wpa_authenticator * wpa_auth,struct wpa_group * group)642 static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
643 struct wpa_group *group)
644 {
645 u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
646 u8 rkey[32];
647 unsigned long ptr;
648
649 if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
650 return -1;
651 wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
652
653 /*
654 * Counter = PRF-256(Random number, "Init Counter",
655 * Local MAC Address || Time)
656 */
657 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
658 wpa_get_ntp_timestamp(buf + ETH_ALEN);
659 ptr = (unsigned long) group;
660 os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
661 #ifdef TEST_FUZZ
662 os_memset(buf + ETH_ALEN, 0xab, 8);
663 os_memset(buf + ETH_ALEN + 8, 0xcd, sizeof(ptr));
664 #endif /* TEST_FUZZ */
665 if (random_get_bytes(rkey, sizeof(rkey)) < 0)
666 return -1;
667
668 if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
669 group->Counter, WPA_NONCE_LEN) < 0)
670 return -1;
671 wpa_hexdump_key(MSG_DEBUG, "Key Counter",
672 group->Counter, WPA_NONCE_LEN);
673
674 return 0;
675 }
676
677
wpa_group_init(struct wpa_authenticator * wpa_auth,int vlan_id,int delay_init)678 static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
679 int vlan_id, int delay_init)
680 {
681 struct wpa_group *group;
682
683 group = os_zalloc(sizeof(struct wpa_group));
684 if (!group)
685 return NULL;
686
687 group->GTKAuthenticator = true;
688 group->vlan_id = vlan_id;
689 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
690
691 if (random_pool_ready() != 1) {
692 wpa_printf(MSG_INFO,
693 "WPA: Not enough entropy in random pool for secure operations - update keys later when the first station connects");
694 }
695
696 /*
697 * Set initial GMK/Counter value here. The actual values that will be
698 * used in negotiations will be set once the first station tries to
699 * connect. This allows more time for collecting additional randomness
700 * on embedded devices.
701 */
702 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
703 wpa_printf(MSG_ERROR,
704 "Failed to get random data for WPA initialization.");
705 os_free(group);
706 return NULL;
707 }
708
709 group->GInit = true;
710 if (delay_init) {
711 wpa_printf(MSG_DEBUG,
712 "WPA: Delay group state machine start until Beacon frames have been configured");
713 /* Initialization is completed in wpa_init_keys(). */
714 } else {
715 wpa_group_sm_step(wpa_auth, group);
716 group->GInit = false;
717 wpa_group_sm_step(wpa_auth, group);
718 }
719
720 return group;
721 }
722
723
724 /**
725 * wpa_init - Initialize WPA authenticator
726 * @addr: Authenticator address
727 * @conf: Configuration for WPA authenticator
728 * @cb: Callback functions for WPA authenticator
729 * Returns: Pointer to WPA authenticator data or %NULL on failure
730 */
wpa_init(const u8 * addr,struct wpa_auth_config * conf,const struct wpa_auth_callbacks * cb,void * cb_ctx)731 struct wpa_authenticator * wpa_init(const u8 *addr,
732 struct wpa_auth_config *conf,
733 const struct wpa_auth_callbacks *cb,
734 void *cb_ctx)
735 {
736 struct wpa_authenticator *wpa_auth;
737
738 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
739 if (!wpa_auth)
740 return NULL;
741
742 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
743 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
744
745 #ifdef CONFIG_IEEE80211BE
746 if (conf->mld_addr) {
747 wpa_auth->is_ml = true;
748 wpa_auth->link_id = conf->link_id;
749 wpa_auth->primary_auth = !conf->first_link_auth;
750 os_memcpy(wpa_auth->mld_addr, conf->mld_addr, ETH_ALEN);
751 }
752 #endif /* CONFIG_IEEE80211BE */
753
754 wpa_auth->cb = cb;
755 wpa_auth->cb_ctx = cb_ctx;
756
757 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
758 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
759 os_free(wpa_auth);
760 return NULL;
761 }
762
763 wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
764 if (!wpa_auth->group) {
765 os_free(wpa_auth->wpa_ie);
766 os_free(wpa_auth);
767 return NULL;
768 }
769
770 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
771 wpa_auth);
772 if (!wpa_auth->pmksa) {
773 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
774 os_free(wpa_auth->group);
775 os_free(wpa_auth->wpa_ie);
776 os_free(wpa_auth);
777 return NULL;
778 }
779
780 #ifdef CONFIG_IEEE80211R_AP
781 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
782 if (!wpa_auth->ft_pmk_cache) {
783 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
784 os_free(wpa_auth->group);
785 os_free(wpa_auth->wpa_ie);
786 pmksa_cache_auth_deinit(wpa_auth->pmksa);
787 os_free(wpa_auth);
788 return NULL;
789 }
790 #endif /* CONFIG_IEEE80211R_AP */
791
792 if (wpa_auth->conf.wpa_gmk_rekey) {
793 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
794 wpa_rekey_gmk, wpa_auth, NULL);
795 }
796
797 #ifdef CONFIG_IEEE80211BE
798 /* For AP MLD, run group rekey timer only on one link (first) and
799 * whenever it fires do rekey on all associated ML links in one shot.
800 */
801 if ((!wpa_auth->is_ml || !conf->first_link_auth) &&
802 wpa_auth->conf.wpa_group_rekey) {
803 #else /* CONFIG_IEEE80211BE */
804 if (wpa_auth->conf.wpa_group_rekey) {
805 #endif /* CONFIG_IEEE80211BE */
806 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
807 wpa_rekey_gtk, wpa_auth, NULL);
808 }
809
810 #ifdef CONFIG_P2P
811 if (WPA_GET_BE32(conf->ip_addr_start)) {
812 int count = WPA_GET_BE32(conf->ip_addr_end) -
813 WPA_GET_BE32(conf->ip_addr_start) + 1;
814 if (count > 1000)
815 count = 1000;
816 if (count > 0)
817 wpa_auth->ip_pool = bitfield_alloc(count);
818 }
819 #endif /* CONFIG_P2P */
820
821 if (conf->tx_bss_auth && conf->beacon_prot) {
822 conf->tx_bss_auth->non_tx_beacon_prot = true;
823 if (!conf->tx_bss_auth->conf.beacon_prot)
824 conf->tx_bss_auth->conf.beacon_prot = true;
825 if (!conf->tx_bss_auth->conf.group_mgmt_cipher)
826 conf->tx_bss_auth->conf.group_mgmt_cipher =
827 conf->group_mgmt_cipher;
828 }
829
830 return wpa_auth;
831 }
832
833
834 int wpa_init_keys(struct wpa_authenticator *wpa_auth)
835 {
836 struct wpa_group *group = wpa_auth->group;
837
838 wpa_printf(MSG_DEBUG,
839 "WPA: Start group state machine to set initial keys");
840 wpa_group_sm_step(wpa_auth, group);
841 group->GInit = false;
842 wpa_group_sm_step(wpa_auth, group);
843 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
844 return -1;
845 return 0;
846 }
847
848
849 static void wpa_auth_free_conf(struct wpa_auth_config *conf)
850 {
851 #ifdef CONFIG_TESTING_OPTIONS
852 wpabuf_free(conf->eapol_m1_elements);
853 conf->eapol_m1_elements = NULL;
854 wpabuf_free(conf->eapol_m3_elements);
855 conf->eapol_m3_elements = NULL;
856 #endif /* CONFIG_TESTING_OPTIONS */
857 }
858
859
860 /**
861 * wpa_deinit - Deinitialize WPA authenticator
862 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
863 */
864 void wpa_deinit(struct wpa_authenticator *wpa_auth)
865 {
866 struct wpa_group *group, *prev;
867
868 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
869
870 /* TODO: Assign ML primary authenticator to next link authenticator and
871 * start rekey timer. */
872 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
873
874 pmksa_cache_auth_deinit(wpa_auth->pmksa);
875
876 #ifdef CONFIG_IEEE80211R_AP
877 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
878 wpa_auth->ft_pmk_cache = NULL;
879 wpa_ft_deinit(wpa_auth);
880 #endif /* CONFIG_IEEE80211R_AP */
881
882 #ifdef CONFIG_P2P
883 bitfield_free(wpa_auth->ip_pool);
884 #endif /* CONFIG_P2P */
885
886
887 os_free(wpa_auth->wpa_ie);
888
889 group = wpa_auth->group;
890 while (group) {
891 prev = group;
892 group = group->next;
893 bin_clear_free(prev, sizeof(*prev));
894 }
895
896 wpa_auth_free_conf(&wpa_auth->conf);
897 os_free(wpa_auth);
898 }
899
900
901 /**
902 * wpa_reconfig - Update WPA authenticator configuration
903 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
904 * @conf: Configuration for WPA authenticator
905 */
906 int wpa_reconfig(struct wpa_authenticator *wpa_auth,
907 struct wpa_auth_config *conf)
908 {
909 struct wpa_group *group;
910
911 if (!wpa_auth)
912 return 0;
913
914 wpa_auth_free_conf(&wpa_auth->conf);
915 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
916 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
917 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
918 return -1;
919 }
920
921 /*
922 * Reinitialize GTK to make sure it is suitable for the new
923 * configuration.
924 */
925 group = wpa_auth->group;
926 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
927 group->GInit = true;
928 wpa_group_sm_step(wpa_auth, group);
929 group->GInit = false;
930 wpa_group_sm_step(wpa_auth, group);
931
932 return 0;
933 }
934
935
936 struct wpa_state_machine *
937 wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
938 const u8 *p2p_dev_addr)
939 {
940 struct wpa_state_machine *sm;
941
942 if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
943 return NULL;
944
945 sm = os_zalloc(sizeof(struct wpa_state_machine));
946 if (!sm)
947 return NULL;
948 os_memcpy(sm->addr, addr, ETH_ALEN);
949 if (p2p_dev_addr)
950 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
951
952 sm->wpa_auth = wpa_auth;
953 sm->group = wpa_auth->group;
954 wpa_group_get(sm->wpa_auth, sm->group);
955 #ifdef CONFIG_IEEE80211BE
956 sm->mld_assoc_link_id = -1;
957 #endif /* CONFIG_IEEE80211BE */
958
959 return sm;
960 }
961
962
963 int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
964 struct wpa_state_machine *sm)
965 {
966 if (!wpa_auth || !wpa_auth->conf.wpa || !sm)
967 return -1;
968
969 #ifdef CONFIG_IEEE80211R_AP
970 if (sm->ft_completed) {
971 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
972 "FT authentication already completed - do not start 4-way handshake");
973 /* Go to PTKINITDONE state to allow GTK rekeying */
974 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
975 sm->Pair = true;
976 return 0;
977 }
978 #endif /* CONFIG_IEEE80211R_AP */
979
980 #ifdef CONFIG_FILS
981 if (sm->fils_completed) {
982 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
983 "FILS authentication already completed - do not start 4-way handshake");
984 /* Go to PTKINITDONE state to allow GTK rekeying */
985 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
986 sm->Pair = true;
987 return 0;
988 }
989 #endif /* CONFIG_FILS */
990
991 if (sm->started) {
992 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
993 sm->ReAuthenticationRequest = true;
994 return wpa_sm_step(sm);
995 }
996
997 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
998 "start authentication");
999 sm->started = 1;
1000
1001 sm->Init = true;
1002 if (wpa_sm_step(sm) == 1)
1003 return 1; /* should not really happen */
1004 sm->Init = false;
1005 sm->AuthenticationRequest = true;
1006 return wpa_sm_step(sm);
1007 }
1008
1009
1010 void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
1011 {
1012 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
1013 * reassociates back to the same AP while the previous entry for the
1014 * STA has not yet been removed. */
1015 if (!sm)
1016 return;
1017
1018 sm->wpa_key_mgmt = 0;
1019 }
1020
1021
1022 static void wpa_free_sta_sm(struct wpa_state_machine *sm)
1023 {
1024 #ifdef CONFIG_IEEE80211BE
1025 int link_id;
1026 #endif /* CONFIG_IEEE80211BE */
1027
1028 #ifdef CONFIG_P2P
1029 if (WPA_GET_BE32(sm->ip_addr)) {
1030 wpa_printf(MSG_DEBUG,
1031 "P2P: Free assigned IP address %u.%u.%u.%u from "
1032 MACSTR " (bit %u)",
1033 sm->ip_addr[0], sm->ip_addr[1],
1034 sm->ip_addr[2], sm->ip_addr[3],
1035 MAC2STR(wpa_auth_get_spa(sm)),
1036 sm->ip_addr_bit);
1037 bitfield_clear(sm->wpa_auth->ip_pool, sm->ip_addr_bit);
1038 }
1039 #endif /* CONFIG_P2P */
1040 if (sm->GUpdateStationKeys)
1041 wpa_gkeydone_sta(sm);
1042 #ifdef CONFIG_IEEE80211R_AP
1043 os_free(sm->assoc_resp_ftie);
1044 wpabuf_free(sm->ft_pending_req_ies);
1045 #endif /* CONFIG_IEEE80211R_AP */
1046 os_free(sm->last_rx_eapol_key);
1047 os_free(sm->wpa_ie);
1048 os_free(sm->rsnxe);
1049 os_free(sm->rsn_selection);
1050 #ifdef CONFIG_IEEE80211BE
1051 for_each_sm_auth(sm, link_id) {
1052 wpa_group_put(sm->mld_links[link_id].wpa_auth,
1053 sm->mld_links[link_id].wpa_auth->group);
1054 sm->mld_links[link_id].wpa_auth = NULL;
1055 }
1056 #endif /* CONFIG_IEEE80211BE */
1057 wpa_group_put(sm->wpa_auth, sm->group);
1058 #ifdef CONFIG_DPP2
1059 wpabuf_clear_free(sm->dpp_z);
1060 #endif /* CONFIG_DPP2 */
1061 bin_clear_free(sm, sizeof(*sm));
1062 }
1063
1064
1065 void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
1066 {
1067 struct wpa_authenticator *wpa_auth;
1068
1069 if (!sm)
1070 return;
1071
1072 wpa_auth = sm->wpa_auth;
1073 if (wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
1074 struct wpa_authenticator *primary_auth = wpa_auth;
1075
1076 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
1077 "strict rekeying - force GTK rekey since STA is leaving");
1078
1079 #ifdef CONFIG_IEEE80211BE
1080 if (wpa_auth->is_ml && !wpa_auth->primary_auth)
1081 primary_auth = wpa_get_primary_auth(wpa_auth);
1082 #endif /* CONFIG_IEEE80211BE */
1083
1084 if (eloop_deplete_timeout(0, 500000, wpa_rekey_gtk,
1085 primary_auth, NULL) == -1)
1086 eloop_register_timeout(0, 500000, wpa_rekey_gtk,
1087 primary_auth, NULL);
1088 }
1089
1090 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1091 sm->pending_1_of_4_timeout = 0;
1092 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
1093 eloop_cancel_timeout(wpa_rekey_ptk, wpa_auth, sm);
1094 #ifdef CONFIG_IEEE80211R_AP
1095 wpa_ft_sta_deinit(sm);
1096 #endif /* CONFIG_IEEE80211R_AP */
1097 if (sm->in_step_loop) {
1098 /* Must not free state machine while wpa_sm_step() is running.
1099 * Freeing will be completed in the end of wpa_sm_step(). */
1100 wpa_printf(MSG_DEBUG,
1101 "WPA: Registering pending STA state machine deinit for "
1102 MACSTR, MAC2STR(wpa_auth_get_spa(sm)));
1103 sm->pending_deinit = 1;
1104 } else
1105 wpa_free_sta_sm(sm);
1106 }
1107
1108
1109 static void wpa_request_new_ptk(struct wpa_state_machine *sm)
1110 {
1111 if (!sm)
1112 return;
1113
1114 if (!sm->use_ext_key_id && sm->wpa_auth->conf.wpa_deny_ptk0_rekey) {
1115 wpa_printf(MSG_INFO,
1116 "WPA: PTK0 rekey not allowed, disconnect " MACSTR,
1117 MAC2STR(wpa_auth_get_spa(sm)));
1118 sm->Disconnect = true;
1119 /* Try to encourage the STA to reconnect */
1120 sm->disconnect_reason =
1121 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1122 } else {
1123 if (sm->use_ext_key_id)
1124 sm->keyidx_active ^= 1; /* flip Key ID */
1125 sm->PTKRequest = true;
1126 sm->PTK_valid = 0;
1127 }
1128 }
1129
1130
1131 static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
1132 const u8 *replay_counter)
1133 {
1134 int i;
1135 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
1136 if (!ctr[i].valid)
1137 break;
1138 if (os_memcmp(replay_counter, ctr[i].counter,
1139 WPA_REPLAY_COUNTER_LEN) == 0)
1140 return 1;
1141 }
1142 return 0;
1143 }
1144
1145
1146 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
1147 const u8 *replay_counter)
1148 {
1149 int i;
1150 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
1151 if (ctr[i].valid &&
1152 (!replay_counter ||
1153 os_memcmp(replay_counter, ctr[i].counter,
1154 WPA_REPLAY_COUNTER_LEN) == 0))
1155 ctr[i].valid = false;
1156 }
1157 }
1158
1159
1160 #ifdef CONFIG_IEEE80211R_AP
1161 static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
1162 struct wpa_state_machine *sm,
1163 struct wpa_eapol_ie_parse *kde)
1164 {
1165 struct wpa_ie_data ie, assoc_ie;
1166 struct rsn_mdie *mdie;
1167 unsigned int i, j;
1168 bool found = false;
1169
1170 /* Verify that PMKR1Name from EAPOL-Key message 2/4 matches the value
1171 * we derived. */
1172
1173 if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
1174 ie.num_pmkid < 1 || !ie.pmkid) {
1175 wpa_printf(MSG_DEBUG,
1176 "FT: No PMKR1Name in FT 4-way handshake message 2/4");
1177 return -1;
1178 }
1179
1180 if (wpa_parse_wpa_ie_rsn(sm->wpa_ie, sm->wpa_ie_len, &assoc_ie) < 0) {
1181 wpa_printf(MSG_DEBUG,
1182 "FT: Could not parse (Re)Association Request frame RSNE");
1183 os_memset(&assoc_ie, 0, sizeof(assoc_ie));
1184 /* Continue to allow PMKR1Name matching to be done to cover the
1185 * case where it is the only listed PMKID. */
1186 }
1187
1188 for (i = 0; i < ie.num_pmkid; i++) {
1189 const u8 *pmkid = ie.pmkid + i * PMKID_LEN;
1190
1191 if (os_memcmp_const(pmkid, sm->pmk_r1_name,
1192 WPA_PMK_NAME_LEN) == 0) {
1193 wpa_printf(MSG_DEBUG,
1194 "FT: RSNE[PMKID[%u]] from supplicant matches PMKR1Name",
1195 i);
1196 found = true;
1197 } else {
1198 for (j = 0; j < assoc_ie.num_pmkid; j++) {
1199 if (os_memcmp(pmkid,
1200 assoc_ie.pmkid + j * PMKID_LEN,
1201 PMKID_LEN) == 0)
1202 break;
1203 }
1204
1205 if (j == assoc_ie.num_pmkid) {
1206 wpa_printf(MSG_DEBUG,
1207 "FT: RSNE[PMKID[%u]] from supplicant is neither PMKR1Name nor included in AssocReq",
1208 i);
1209 found = false;
1210 break;
1211 }
1212 wpa_printf(MSG_DEBUG,
1213 "FT: RSNE[PMKID[%u]] from supplicant is not PMKR1Name, but matches a PMKID in AssocReq",
1214 i);
1215 }
1216 }
1217
1218 if (!found) {
1219 wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm),
1220 LOGGER_DEBUG,
1221 "PMKR1Name mismatch in FT 4-way handshake");
1222 wpa_hexdump(MSG_DEBUG,
1223 "FT: PMKIDs/PMKR1Name from Supplicant",
1224 ie.pmkid, ie.num_pmkid * PMKID_LEN);
1225 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1226 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1227 return -1;
1228 }
1229
1230 if (!kde->mdie || !kde->ftie) {
1231 wpa_printf(MSG_DEBUG,
1232 "FT: No %s in FT 4-way handshake message 2/4",
1233 kde->mdie ? "FTIE" : "MDIE");
1234 return -1;
1235 }
1236
1237 mdie = (struct rsn_mdie *) (kde->mdie + 2);
1238 if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
1239 os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
1240 MOBILITY_DOMAIN_ID_LEN) != 0) {
1241 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
1242 return -1;
1243 }
1244
1245 if (sm->assoc_resp_ftie &&
1246 (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
1247 os_memcmp(kde->ftie, sm->assoc_resp_ftie,
1248 2 + sm->assoc_resp_ftie[1]) != 0)) {
1249 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
1250 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
1251 kde->ftie, kde->ftie_len);
1252 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
1253 sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
1254 return -1;
1255 }
1256
1257 return 0;
1258 }
1259 #endif /* CONFIG_IEEE80211R_AP */
1260
1261
1262 static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
1263 struct wpa_state_machine *sm, int group)
1264 {
1265 /* Supplicant reported a Michael MIC error */
1266 wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
1267 "received EAPOL-Key Error Request (STA detected Michael MIC failure (group=%d))",
1268 group);
1269
1270 if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
1271 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
1272 "ignore Michael MIC failure report since group cipher is not TKIP");
1273 } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
1274 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
1275 "ignore Michael MIC failure report since pairwise cipher is not TKIP");
1276 } else {
1277 if (wpa_auth_mic_failure_report(wpa_auth,
1278 wpa_auth_get_spa(sm)) > 0)
1279 return 1; /* STA entry was removed */
1280 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
1281 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
1282 }
1283
1284 /*
1285 * Error report is not a request for a new key handshake, but since
1286 * Authenticator may do it, let's change the keys now anyway.
1287 */
1288 wpa_request_new_ptk(sm);
1289 return 0;
1290 }
1291
1292
1293 static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
1294 size_t data_len)
1295 {
1296 struct wpa_ptk PTK;
1297 int ok = 0;
1298 const u8 *pmk = NULL;
1299 size_t pmk_len;
1300 int vlan_id = 0;
1301 u8 pmk_r0[PMK_LEN_MAX], pmk_r0_name[WPA_PMK_NAME_LEN];
1302 u8 pmk_r1[PMK_LEN_MAX];
1303 size_t key_len;
1304 int ret = -1;
1305
1306 os_memset(&PTK, 0, sizeof(PTK));
1307 for (;;) {
1308 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
1309 !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
1310 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
1311 sm->p2p_dev_addr, pmk, &pmk_len,
1312 &vlan_id);
1313 if (!pmk)
1314 break;
1315 #ifdef CONFIG_IEEE80211R_AP
1316 if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
1317 os_memcpy(sm->xxkey, pmk, pmk_len);
1318 sm->xxkey_len = pmk_len;
1319 }
1320 #endif /* CONFIG_IEEE80211R_AP */
1321 } else {
1322 pmk = sm->PMK;
1323 pmk_len = sm->pmk_len;
1324 }
1325
1326 if (wpa_derive_ptk(sm, sm->alt_SNonce, pmk, pmk_len, &PTK, 0,
1327 pmk_r0, pmk_r1, pmk_r0_name, &key_len,
1328 false) < 0)
1329 break;
1330
1331 if (wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK,
1332 data, data_len) == 0) {
1333 if (sm->PMK != pmk) {
1334 os_memcpy(sm->PMK, pmk, pmk_len);
1335 sm->pmk_len = pmk_len;
1336 }
1337 ok = 1;
1338 break;
1339 }
1340
1341 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
1342 wpa_key_mgmt_sae(sm->wpa_key_mgmt))
1343 break;
1344 }
1345
1346 if (!ok) {
1347 wpa_printf(MSG_DEBUG,
1348 "WPA: Earlier SNonce did not result in matching MIC");
1349 goto fail;
1350 }
1351
1352 wpa_printf(MSG_DEBUG,
1353 "WPA: Earlier SNonce resulted in matching MIC");
1354 sm->alt_snonce_valid = 0;
1355
1356 if (vlan_id && wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
1357 wpa_auth_update_vlan(sm->wpa_auth, sm->addr, vlan_id) < 0)
1358 goto fail;
1359
1360 #ifdef CONFIG_IEEE80211R_AP
1361 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt) && !sm->ft_completed) {
1362 wpa_printf(MSG_DEBUG, "FT: Store PMK-R0/PMK-R1");
1363 wpa_auth_ft_store_keys(sm, pmk_r0, pmk_r1, pmk_r0_name,
1364 key_len);
1365 }
1366 #endif /* CONFIG_IEEE80211R_AP */
1367
1368 os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
1369 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
1370 forced_memzero(&PTK, sizeof(PTK));
1371 sm->PTK_valid = true;
1372
1373 ret = 0;
1374 fail:
1375 forced_memzero(pmk_r0, sizeof(pmk_r0));
1376 forced_memzero(pmk_r1, sizeof(pmk_r1));
1377 return ret;
1378 }
1379
1380
1381 static bool wpa_auth_gtk_rekey_in_process(struct wpa_authenticator *wpa_auth)
1382 {
1383 struct wpa_group *group;
1384
1385 for (group = wpa_auth->group; group; group = group->next) {
1386 if (group->GKeyDoneStations)
1387 return true;
1388 }
1389 return false;
1390 }
1391
1392
1393 enum eapol_key_msg { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST };
1394
1395 static bool wpa_auth_valid_key_desc_ver(struct wpa_authenticator *wpa_auth,
1396 struct wpa_state_machine *sm, u16 ver)
1397 {
1398 if (ver > WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1399 wpa_printf(MSG_INFO, "RSN: " MACSTR
1400 " used undefined Key Descriptor Version %d",
1401 MAC2STR(wpa_auth_get_spa(sm)), ver);
1402 return false;
1403 }
1404
1405 if (!wpa_use_akm_defined(sm->wpa_key_mgmt) &&
1406 wpa_use_cmac(sm->wpa_key_mgmt) &&
1407 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1408 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1409 LOGGER_WARNING,
1410 "advertised support for AES-128-CMAC, but did not use it");
1411 return false;
1412 }
1413
1414 if (sm->pairwise != WPA_CIPHER_TKIP &&
1415 !wpa_use_akm_defined(sm->wpa_key_mgmt) &&
1416 !wpa_use_cmac(sm->wpa_key_mgmt) &&
1417 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1418 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1419 LOGGER_WARNING,
1420 "did not use HMAC-SHA1-AES with CCMP/GCMP");
1421 return false;
1422 }
1423
1424 if (wpa_use_akm_defined(sm->wpa_key_mgmt) &&
1425 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1426 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1427 LOGGER_WARNING,
1428 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1429 return false;
1430 }
1431
1432 return true;
1433 }
1434
1435
1436 static bool wpa_auth_valid_request_counter(struct wpa_authenticator *wpa_auth,
1437 struct wpa_state_machine *sm,
1438 const u8 *replay_counter)
1439 {
1440
1441 if (sm->req_replay_counter_used &&
1442 os_memcmp(replay_counter, sm->req_replay_counter,
1443 WPA_REPLAY_COUNTER_LEN) <= 0) {
1444 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1445 LOGGER_WARNING,
1446 "received EAPOL-Key request with replayed counter");
1447 return false;
1448 }
1449
1450 return true;
1451 }
1452
1453
1454 static bool wpa_auth_valid_counter(struct wpa_authenticator *wpa_auth,
1455 struct wpa_state_machine *sm,
1456 const struct wpa_eapol_key *key,
1457 enum eapol_key_msg msg,
1458 const char *msgtxt)
1459 {
1460 int i;
1461
1462 if (msg == REQUEST)
1463 return wpa_auth_valid_request_counter(wpa_auth, sm,
1464 key->replay_counter);
1465
1466 if (wpa_replay_counter_valid(sm->key_replay, key->replay_counter))
1467 return true;
1468
1469 if (msg == PAIRWISE_2 &&
1470 wpa_replay_counter_valid(sm->prev_key_replay,
1471 key->replay_counter) &&
1472 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1473 os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1474 /*
1475 * Some supplicant implementations (e.g., Windows XP
1476 * WZC) update SNonce for each EAPOL-Key 2/4. This
1477 * breaks the workaround on accepting any of the
1478 * pending requests, so allow the SNonce to be updated
1479 * even if we have already sent out EAPOL-Key 3/4.
1480 */
1481 wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
1482 LOGGER_DEBUG,
1483 "Process SNonce update from STA based on retransmitted EAPOL-Key 1/4");
1484 sm->update_snonce = 1;
1485 os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
1486 sm->alt_snonce_valid = true;
1487 os_memcpy(sm->alt_replay_counter,
1488 sm->key_replay[0].counter,
1489 WPA_REPLAY_COUNTER_LEN);
1490 return true;
1491 }
1492
1493 if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
1494 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1495 os_memcmp(key->replay_counter, sm->alt_replay_counter,
1496 WPA_REPLAY_COUNTER_LEN) == 0) {
1497 /*
1498 * Supplicant may still be using the old SNonce since
1499 * there was two EAPOL-Key 2/4 messages and they had
1500 * different SNonce values.
1501 */
1502 wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
1503 LOGGER_DEBUG,
1504 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
1505 return true;
1506 }
1507
1508 if (msg == PAIRWISE_2 &&
1509 wpa_replay_counter_valid(sm->prev_key_replay,
1510 key->replay_counter) &&
1511 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
1512 wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
1513 LOGGER_DEBUG,
1514 "ignore retransmitted EAPOL-Key %s - SNonce did not change",
1515 msgtxt);
1516 } else {
1517 wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
1518 LOGGER_DEBUG,
1519 "received EAPOL-Key %s with unexpected replay counter",
1520 msgtxt);
1521 }
1522 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
1523 if (!sm->key_replay[i].valid)
1524 break;
1525 wpa_hexdump(MSG_DEBUG, "pending replay counter",
1526 sm->key_replay[i].counter,
1527 WPA_REPLAY_COUNTER_LEN);
1528 }
1529 wpa_hexdump(MSG_DEBUG, "received replay counter",
1530 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1531 return false;
1532 }
1533
1534
1535 void wpa_receive(struct wpa_authenticator *wpa_auth,
1536 struct wpa_state_machine *sm,
1537 u8 *data, size_t data_len)
1538 {
1539 struct ieee802_1x_hdr *hdr;
1540 struct wpa_eapol_key *key;
1541 u16 key_info, ver, key_data_length;
1542 enum eapol_key_msg msg;
1543 const char *msgtxt;
1544 const u8 *key_data;
1545 size_t keyhdrlen, mic_len;
1546 u8 *mic;
1547 u8 *key_data_buf = NULL;
1548 size_t key_data_buf_len = 0;
1549
1550 if (!wpa_auth || !wpa_auth->conf.wpa || !sm)
1551 return;
1552
1553 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL data", data, data_len);
1554
1555 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
1556 keyhdrlen = sizeof(*key) + mic_len + 2;
1557
1558 if (data_len < sizeof(*hdr) + keyhdrlen) {
1559 wpa_printf(MSG_DEBUG, "WPA: Ignore too short EAPOL-Key frame");
1560 return;
1561 }
1562
1563 hdr = (struct ieee802_1x_hdr *) data;
1564 key = (struct wpa_eapol_key *) (hdr + 1);
1565 mic = (u8 *) (key + 1);
1566 key_info = WPA_GET_BE16(key->key_info);
1567 key_data = mic + mic_len + 2;
1568 key_data_length = WPA_GET_BE16(mic + mic_len);
1569 wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
1570 " key_info=0x%x type=%u mic_len=%zu key_data_length=%u",
1571 MAC2STR(wpa_auth_get_spa(sm)), key_info, key->type,
1572 mic_len, key_data_length);
1573 wpa_hexdump(MSG_MSGDUMP,
1574 "WPA: EAPOL-Key header (ending before Key MIC)",
1575 key, sizeof(*key));
1576 wpa_hexdump(MSG_MSGDUMP, "WPA: EAPOL-Key Key MIC",
1577 mic, mic_len);
1578 if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) {
1579 wpa_printf(MSG_INFO,
1580 "WPA: Invalid EAPOL-Key frame - key_data overflow (%d > %zu)",
1581 key_data_length,
1582 data_len - sizeof(*hdr) - keyhdrlen);
1583 return;
1584 }
1585
1586 if (sm->wpa == WPA_VERSION_WPA2) {
1587 if (key->type == EAPOL_KEY_TYPE_WPA) {
1588 /*
1589 * Some deployed station implementations seem to send
1590 * msg 4/4 with incorrect type value in WPA2 mode.
1591 */
1592 wpa_printf(MSG_DEBUG,
1593 "Workaround: Allow EAPOL-Key with unexpected WPA type in RSN mode");
1594 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
1595 wpa_printf(MSG_DEBUG,
1596 "Ignore EAPOL-Key with unexpected type %d in RSN mode",
1597 key->type);
1598 return;
1599 }
1600 } else {
1601 if (key->type != EAPOL_KEY_TYPE_WPA) {
1602 wpa_printf(MSG_DEBUG,
1603 "Ignore EAPOL-Key with unexpected type %d in WPA mode",
1604 key->type);
1605 return;
1606 }
1607 }
1608
1609 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
1610 WPA_NONCE_LEN);
1611 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
1612 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1613
1614 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
1615 * are set */
1616
1617 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1618 wpa_printf(MSG_DEBUG, "WPA: Ignore SMK message");
1619 return;
1620 }
1621
1622 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1623 if (!wpa_auth_valid_key_desc_ver(wpa_auth, sm, ver))
1624 goto out;
1625 if (mic_len > 0 && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) &&
1626 sm->PTK_valid &&
1627 (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1628 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
1629 wpa_use_aes_key_wrap(sm->wpa_key_mgmt)) &&
1630 key_data_length >= 8 && key_data_length % 8 == 0) {
1631 key_data_length -= 8; /* AES-WRAP adds 8 bytes */
1632 key_data_buf = os_malloc(key_data_length);
1633 if (!key_data_buf)
1634 goto out;
1635 key_data_buf_len = key_data_length;
1636 if (aes_unwrap(sm->PTK.kek, sm->PTK.kek_len,
1637 key_data_length / 8, key_data, key_data_buf)) {
1638 wpa_printf(MSG_INFO,
1639 "RSN: AES unwrap failed - could not decrypt EAPOL-Key key data");
1640 goto out;
1641 }
1642 key_data = key_data_buf;
1643 wpa_hexdump_key(MSG_DEBUG, "RSN: Decrypted EAPOL-Key Key Data",
1644 key_data, key_data_length);
1645 }
1646
1647 if (key_info & WPA_KEY_INFO_REQUEST) {
1648 msg = REQUEST;
1649 msgtxt = "Request";
1650 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1651 msg = GROUP_2;
1652 msgtxt = "2/2 Group";
1653 } else if (key_data_length == 0 ||
1654 (sm->wpa == WPA_VERSION_WPA2 &&
1655 (!(key_info & WPA_KEY_INFO_ENCR_KEY_DATA) ||
1656 key_data_buf) &&
1657 (key_info & WPA_KEY_INFO_SECURE) &&
1658 !get_ie(key_data, key_data_length, WLAN_EID_RSN)) ||
1659 (mic_len == 0 && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) &&
1660 key_data_length == AES_BLOCK_SIZE)) {
1661 msg = PAIRWISE_4;
1662 msgtxt = "4/4 Pairwise";
1663 } else {
1664 msg = PAIRWISE_2;
1665 msgtxt = "2/4 Pairwise";
1666 }
1667
1668 if (!wpa_auth_valid_counter(wpa_auth, sm, key, msg, msgtxt))
1669 goto out;
1670
1671 #ifdef CONFIG_FILS
1672 if (sm->wpa == WPA_VERSION_WPA2 && mic_len == 0 &&
1673 !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1674 wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
1675 "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
1676 goto out;
1677 }
1678 #endif /* CONFIG_FILS */
1679
1680 switch (msg) {
1681 case PAIRWISE_2:
1682 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
1683 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
1684 (!sm->update_snonce ||
1685 sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
1686 wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
1687 LOGGER_INFO,
1688 "received EAPOL-Key msg 2/4 in invalid state (%d) - dropped",
1689 sm->wpa_ptk_state);
1690 goto out;
1691 }
1692 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
1693 if (sm->group->reject_4way_hs_for_entropy) {
1694 /*
1695 * The system did not have enough entropy to generate
1696 * strong random numbers. Reject the first 4-way
1697 * handshake(s) and collect some entropy based on the
1698 * information from it. Once enough entropy is
1699 * available, the next atempt will trigger GMK/Key
1700 * Counter update and the station will be allowed to
1701 * continue.
1702 */
1703 wpa_printf(MSG_DEBUG,
1704 "WPA: Reject 4-way handshake to collect more entropy for random number generation");
1705 random_mark_pool_ready();
1706 wpa_sta_disconnect(wpa_auth, sm->addr,
1707 WLAN_REASON_PREV_AUTH_NOT_VALID);
1708 goto out;
1709 }
1710 break;
1711 case PAIRWISE_4:
1712 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1713 !sm->PTK_valid) {
1714 wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
1715 LOGGER_INFO,
1716 "received EAPOL-Key msg 4/4 in invalid state (%d) - dropped",
1717 sm->wpa_ptk_state);
1718 goto out;
1719 }
1720 break;
1721 case GROUP_2:
1722 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1723 || !sm->PTK_valid) {
1724 wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
1725 LOGGER_INFO,
1726 "received EAPOL-Key msg 2/2 in invalid state (%d) - dropped",
1727 sm->wpa_ptk_group_state);
1728 goto out;
1729 }
1730 break;
1731 case REQUEST:
1732 if (sm->wpa_ptk_state == WPA_PTK_PTKSTART ||
1733 sm->wpa_ptk_state == WPA_PTK_PTKCALCNEGOTIATING ||
1734 sm->wpa_ptk_state == WPA_PTK_PTKCALCNEGOTIATING2 ||
1735 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
1736 wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
1737 LOGGER_INFO,
1738 "received EAPOL-Key Request in invalid state (%d) - dropped",
1739 sm->wpa_ptk_state);
1740 goto out;
1741 }
1742 break;
1743 }
1744
1745 wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
1746 "received EAPOL-Key frame (%s)", msgtxt);
1747
1748 if (key_info & WPA_KEY_INFO_ACK) {
1749 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
1750 "received invalid EAPOL-Key: Key Ack set");
1751 goto out;
1752 }
1753
1754 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1755 !(key_info & WPA_KEY_INFO_MIC)) {
1756 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
1757 "received invalid EAPOL-Key: Key MIC not set");
1758 goto out;
1759 }
1760
1761 #ifdef CONFIG_FILS
1762 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1763 (key_info & WPA_KEY_INFO_MIC)) {
1764 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
1765 "received invalid EAPOL-Key: Key MIC set");
1766 goto out;
1767 }
1768 #endif /* CONFIG_FILS */
1769
1770 sm->MICVerified = false;
1771 if (sm->PTK_valid && !sm->update_snonce) {
1772 if (mic_len &&
1773 wpa_verify_key_mic(sm->wpa_key_mgmt, sm->pmk_len, &sm->PTK,
1774 data, data_len) &&
1775 (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
1776 wpa_try_alt_snonce(sm, data, data_len))) {
1777 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1778 LOGGER_INFO,
1779 "received EAPOL-Key with invalid MIC");
1780 #ifdef TEST_FUZZ
1781 wpa_printf(MSG_INFO,
1782 "TEST: Ignore Key MIC failure for fuzz testing");
1783 goto continue_fuzz;
1784 #endif /* TEST_FUZZ */
1785 goto out;
1786 }
1787 #ifdef CONFIG_FILS
1788 if (!mic_len &&
1789 wpa_aead_decrypt(sm, &sm->PTK, data, data_len,
1790 &key_data_length) < 0) {
1791 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1792 LOGGER_INFO,
1793 "received EAPOL-Key with invalid MIC");
1794 #ifdef TEST_FUZZ
1795 wpa_printf(MSG_INFO,
1796 "TEST: Ignore Key MIC failure for fuzz testing");
1797 goto continue_fuzz;
1798 #endif /* TEST_FUZZ */
1799 goto out;
1800 }
1801 #endif /* CONFIG_FILS */
1802 #ifdef TEST_FUZZ
1803 continue_fuzz:
1804 #endif /* TEST_FUZZ */
1805 sm->MICVerified = true;
1806 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1807 sm->pending_1_of_4_timeout = 0;
1808 }
1809
1810 if (key_info & WPA_KEY_INFO_REQUEST) {
1811 if (!(key_info & WPA_KEY_INFO_SECURE)) {
1812 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1813 LOGGER_INFO,
1814 "received EAPOL-Key request without Secure=1");
1815 goto out;
1816 }
1817 if (sm->MICVerified) {
1818 sm->req_replay_counter_used = 1;
1819 os_memcpy(sm->req_replay_counter, key->replay_counter,
1820 WPA_REPLAY_COUNTER_LEN);
1821 } else {
1822 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1823 LOGGER_INFO,
1824 "received EAPOL-Key request with invalid MIC");
1825 goto out;
1826 }
1827
1828 if (key_info & WPA_KEY_INFO_ERROR) {
1829 if (wpa_receive_error_report(
1830 wpa_auth, sm,
1831 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1832 goto out; /* STA entry was removed */
1833 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1834 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1835 LOGGER_INFO,
1836 "received EAPOL-Key Request for new 4-Way Handshake");
1837 wpa_request_new_ptk(sm);
1838 } else {
1839 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1840 LOGGER_INFO,
1841 "received EAPOL-Key Request for GTK rekeying");
1842
1843 eloop_cancel_timeout(wpa_rekey_gtk,
1844 wpa_get_primary_auth(wpa_auth),
1845 NULL);
1846 if (wpa_auth_gtk_rekey_in_process(wpa_auth))
1847 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG,
1848 "skip new GTK rekey - already in process");
1849 else
1850 wpa_rekey_gtk(wpa_get_primary_auth(wpa_auth),
1851 NULL);
1852 }
1853 } else {
1854 /* Do not allow the same key replay counter to be reused. */
1855 wpa_replay_counter_mark_invalid(sm->key_replay,
1856 key->replay_counter);
1857
1858 if (msg == PAIRWISE_2) {
1859 /*
1860 * Maintain a copy of the pending EAPOL-Key frames in
1861 * case the EAPOL-Key frame was retransmitted. This is
1862 * needed to allow EAPOL-Key msg 2/4 reply to another
1863 * pending msg 1/4 to update the SNonce to work around
1864 * unexpected supplicant behavior.
1865 */
1866 os_memcpy(sm->prev_key_replay, sm->key_replay,
1867 sizeof(sm->key_replay));
1868 } else {
1869 os_memset(sm->prev_key_replay, 0,
1870 sizeof(sm->prev_key_replay));
1871 }
1872
1873 /*
1874 * Make sure old valid counters are not accepted anymore and
1875 * do not get copied again.
1876 */
1877 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
1878 }
1879
1880 os_free(sm->last_rx_eapol_key);
1881 sm->last_rx_eapol_key = os_memdup(data, data_len);
1882 if (!sm->last_rx_eapol_key)
1883 goto out;
1884 sm->last_rx_eapol_key_len = data_len;
1885
1886 sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
1887 sm->EAPOLKeyReceived = true;
1888 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1889 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1890 if (msg == PAIRWISE_2)
1891 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1892 wpa_sm_step(sm);
1893
1894 out:
1895 bin_clear_free(key_data_buf, key_data_buf_len);
1896 }
1897
1898
1899 static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1900 const u8 *gnonce, u8 *gtk, size_t gtk_len)
1901 {
1902 u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + WPA_GTK_MAX_LEN];
1903 u8 *pos;
1904 int ret = 0;
1905
1906 /* GTK = PRF-X(GMK, "Group key expansion",
1907 * AA || GNonce || Time || random data)
1908 * The example described in the IEEE 802.11 standard uses only AA and
1909 * GNonce as inputs here. Add some more entropy since this derivation
1910 * is done only at the Authenticator and as such, does not need to be
1911 * exactly same.
1912 */
1913 os_memset(data, 0, sizeof(data));
1914 os_memcpy(data, addr, ETH_ALEN);
1915 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1916 pos = data + ETH_ALEN + WPA_NONCE_LEN;
1917 wpa_get_ntp_timestamp(pos);
1918 #ifdef TEST_FUZZ
1919 os_memset(pos, 0xef, 8);
1920 #endif /* TEST_FUZZ */
1921 pos += 8;
1922 if (random_get_bytes(pos, gtk_len) < 0)
1923 ret = -1;
1924
1925 #ifdef CONFIG_SHA384
1926 if (sha384_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1927 gtk, gtk_len) < 0)
1928 ret = -1;
1929 #else /* CONFIG_SHA384 */
1930 #ifdef CONFIG_SHA256
1931 if (sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1932 gtk, gtk_len) < 0)
1933 ret = -1;
1934 #else /* CONFIG_SHA256 */
1935 if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1936 gtk, gtk_len) < 0)
1937 ret = -1;
1938 #endif /* CONFIG_SHA256 */
1939 #endif /* CONFIG_SHA384 */
1940
1941 forced_memzero(data, sizeof(data));
1942
1943 return ret;
1944 }
1945
1946
1947 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1948 {
1949 struct wpa_authenticator *wpa_auth = eloop_ctx;
1950 struct wpa_state_machine *sm = timeout_ctx;
1951
1952 if (sm->waiting_radius_psk) {
1953 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1954 "Ignore EAPOL-Key timeout while waiting for RADIUS PSK");
1955 return;
1956 }
1957
1958 sm->pending_1_of_4_timeout = 0;
1959 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
1960 "EAPOL-Key timeout");
1961 sm->TimeoutEvt = true;
1962 wpa_sm_step(sm);
1963 }
1964
1965
1966 void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1967 struct wpa_state_machine *sm, int key_info,
1968 const u8 *key_rsc, const u8 *nonce,
1969 const u8 *kde, size_t kde_len,
1970 int keyidx, int encr, int force_version)
1971 {
1972 struct wpa_auth_config *conf = &wpa_auth->conf;
1973 struct ieee802_1x_hdr *hdr;
1974 struct wpa_eapol_key *key;
1975 size_t len, mic_len, keyhdrlen;
1976 int alg;
1977 int key_data_len, pad_len = 0;
1978 u8 *buf, *pos;
1979 int version, pairwise;
1980 int i;
1981 u8 *key_mic, *key_data;
1982
1983 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
1984 keyhdrlen = sizeof(*key) + mic_len + 2;
1985
1986 len = sizeof(struct ieee802_1x_hdr) + keyhdrlen;
1987
1988 if (force_version)
1989 version = force_version;
1990 else if (wpa_use_akm_defined(sm->wpa_key_mgmt))
1991 version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
1992 else if (wpa_use_cmac(sm->wpa_key_mgmt))
1993 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
1994 else if (sm->pairwise != WPA_CIPHER_TKIP)
1995 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1996 else
1997 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1998
1999 pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
2000
2001 wpa_printf(MSG_DEBUG,
2002 "WPA: Send EAPOL(version=%d secure=%d mic=%d ack=%d install=%d pairwise=%d kde_len=%zu keyidx=%d encr=%d)",
2003 version,
2004 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
2005 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
2006 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
2007 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
2008 pairwise, kde_len, keyidx, encr);
2009
2010 key_data_len = kde_len;
2011
2012 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
2013 wpa_use_aes_key_wrap(sm->wpa_key_mgmt) ||
2014 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
2015 pad_len = key_data_len % 8;
2016 if (pad_len)
2017 pad_len = 8 - pad_len;
2018 key_data_len += pad_len + 8;
2019 }
2020
2021 len += key_data_len;
2022 if (!mic_len && encr)
2023 len += AES_BLOCK_SIZE;
2024
2025 hdr = os_zalloc(len);
2026 if (!hdr)
2027 return;
2028 hdr->version = conf->eapol_version;
2029 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
2030 hdr->length = host_to_be16(len - sizeof(*hdr));
2031 key = (struct wpa_eapol_key *) (hdr + 1);
2032 key_mic = (u8 *) (key + 1);
2033 key_data = ((u8 *) (hdr + 1)) + keyhdrlen;
2034
2035 key->type = sm->wpa == WPA_VERSION_WPA2 ?
2036 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
2037 key_info |= version;
2038 if (encr && sm->wpa == WPA_VERSION_WPA2)
2039 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
2040 if (sm->wpa != WPA_VERSION_WPA2)
2041 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
2042 WPA_PUT_BE16(key->key_info, key_info);
2043
2044 alg = pairwise ? sm->pairwise : conf->wpa_group;
2045 if (sm->wpa == WPA_VERSION_WPA2 && !pairwise)
2046 WPA_PUT_BE16(key->key_length, 0);
2047 else
2048 WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
2049
2050 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
2051 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
2052 os_memcpy(sm->key_replay[i].counter,
2053 sm->key_replay[i - 1].counter,
2054 WPA_REPLAY_COUNTER_LEN);
2055 }
2056 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
2057 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
2058 WPA_REPLAY_COUNTER_LEN);
2059 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
2060 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
2061 sm->key_replay[0].valid = true;
2062
2063 if (nonce)
2064 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
2065
2066 if (key_rsc)
2067 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
2068
2069 if (kde && !encr) {
2070 os_memcpy(key_data, kde, kde_len);
2071 WPA_PUT_BE16(key_mic + mic_len, kde_len);
2072 #ifdef CONFIG_FILS
2073 } else if (!mic_len && kde) {
2074 const u8 *aad[1];
2075 size_t aad_len[1];
2076
2077 WPA_PUT_BE16(key_mic, AES_BLOCK_SIZE + kde_len);
2078 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
2079 kde, kde_len);
2080
2081 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK",
2082 sm->PTK.kek, sm->PTK.kek_len);
2083 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2084 * to Key Data (exclusive). */
2085 aad[0] = (u8 *) hdr;
2086 aad_len[0] = key_mic + 2 - (u8 *) hdr;
2087 if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len, kde, kde_len,
2088 1, aad, aad_len, key_mic + 2) < 0) {
2089 wpa_printf(MSG_DEBUG, "WPA: AES-SIV encryption failed");
2090 return;
2091 }
2092
2093 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
2094 key_mic + 2, AES_BLOCK_SIZE + kde_len);
2095 #endif /* CONFIG_FILS */
2096 } else if (encr && kde) {
2097 buf = os_zalloc(key_data_len);
2098 if (!buf) {
2099 os_free(hdr);
2100 return;
2101 }
2102 pos = buf;
2103 os_memcpy(pos, kde, kde_len);
2104 pos += kde_len;
2105
2106 if (pad_len)
2107 *pos++ = 0xdd;
2108
2109 wpa_hexdump_key(MSG_DEBUG,
2110 "Plaintext EAPOL-Key Key Data (+ padding)",
2111 buf, key_data_len);
2112 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
2113 wpa_use_aes_key_wrap(sm->wpa_key_mgmt) ||
2114 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
2115 wpa_hexdump_key(MSG_DEBUG, "RSN: AES-WRAP using KEK",
2116 sm->PTK.kek, sm->PTK.kek_len);
2117 if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len,
2118 (key_data_len - 8) / 8, buf, key_data)) {
2119 os_free(hdr);
2120 bin_clear_free(buf, key_data_len);
2121 return;
2122 }
2123 wpa_hexdump(MSG_DEBUG,
2124 "RSN: Encrypted Key Data from AES-WRAP",
2125 key_data, key_data_len);
2126 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
2127 #if !defined(CONFIG_NO_RC4) && !defined(CONFIG_FIPS)
2128 } else if (sm->PTK.kek_len == 16) {
2129 u8 ek[32];
2130
2131 wpa_printf(MSG_DEBUG,
2132 "WPA: Encrypt Key Data using RC4");
2133 os_memcpy(key->key_iv,
2134 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
2135 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
2136 os_memcpy(ek, key->key_iv, 16);
2137 os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len);
2138 os_memcpy(key_data, buf, key_data_len);
2139 rc4_skip(ek, 32, 256, key_data, key_data_len);
2140 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
2141 #endif /* !(CONFIG_NO_RC4 || CONFIG_FIPS) */
2142 } else {
2143 os_free(hdr);
2144 bin_clear_free(buf, key_data_len);
2145 return;
2146 }
2147 bin_clear_free(buf, key_data_len);
2148 }
2149
2150 if (key_info & WPA_KEY_INFO_MIC) {
2151 if (!sm->PTK_valid || !mic_len) {
2152 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
2153 LOGGER_DEBUG,
2154 "PTK not valid when sending EAPOL-Key frame");
2155 os_free(hdr);
2156 return;
2157 }
2158
2159 if (wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len,
2160 sm->wpa_key_mgmt, version,
2161 (u8 *) hdr, len, key_mic) < 0) {
2162 os_free(hdr);
2163 return;
2164 }
2165 #ifdef CONFIG_TESTING_OPTIONS
2166 if (!pairwise &&
2167 conf->corrupt_gtk_rekey_mic_probability > 0.0 &&
2168 drand48() < conf->corrupt_gtk_rekey_mic_probability) {
2169 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
2170 LOGGER_INFO,
2171 "Corrupting group EAPOL-Key Key MIC");
2172 key_mic[0]++;
2173 }
2174 #endif /* CONFIG_TESTING_OPTIONS */
2175 }
2176
2177 wpa_auth_set_eapol(wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx, 1);
2178 wpa_hexdump(MSG_DEBUG, "Send EAPOL-Key msg", hdr, len);
2179 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
2180 sm->pairwise_set);
2181 os_free(hdr);
2182 }
2183
2184
2185 static int wpa_auth_get_sta_count(struct wpa_authenticator *wpa_auth)
2186 {
2187 if (!wpa_auth->cb->get_sta_count)
2188 return -1;
2189
2190 return wpa_auth->cb->get_sta_count(wpa_auth->cb_ctx);
2191 }
2192
2193
2194 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
2195 struct wpa_state_machine *sm, int key_info,
2196 const u8 *key_rsc, const u8 *nonce,
2197 const u8 *kde, size_t kde_len,
2198 int keyidx, int encr)
2199 {
2200 int timeout_ms;
2201 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
2202 u32 ctr;
2203
2204 if (!sm)
2205 return;
2206
2207 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
2208
2209 #ifdef CONFIG_TESTING_OPTIONS
2210 /* When delay_eapol_tx is true, delay the EAPOL-Key transmission by
2211 * sending it only on the last attempt after all timeouts for the prior
2212 * skipped attemps. */
2213 if (wpa_auth->conf.delay_eapol_tx &&
2214 ctr != wpa_auth->conf.wpa_pairwise_update_count) {
2215 wpa_msg(sm->wpa_auth->conf.msg_ctx, MSG_INFO,
2216 "DELAY-EAPOL-TX-%d", ctr);
2217 goto skip_tx;
2218 }
2219 #endif /* CONFIG_TESTING_OPTIONS */
2220 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
2221 keyidx, encr, 0);
2222 #ifdef CONFIG_TESTING_OPTIONS
2223 skip_tx:
2224 #endif /* CONFIG_TESTING_OPTIONS */
2225
2226 if (ctr == 1 && wpa_auth->conf.tx_status) {
2227 if (pairwise)
2228 timeout_ms = eapol_key_timeout_first;
2229 else if (wpa_auth_get_sta_count(wpa_auth) > 100)
2230 timeout_ms = eapol_key_timeout_first_group * 2;
2231 else
2232 timeout_ms = eapol_key_timeout_first_group;
2233 } else {
2234 timeout_ms = eapol_key_timeout_subseq;
2235 }
2236 if (wpa_auth->conf.wpa_disable_eapol_key_retries &&
2237 (!pairwise || (key_info & WPA_KEY_INFO_MIC)))
2238 timeout_ms = eapol_key_timeout_no_retrans;
2239 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
2240 sm->pending_1_of_4_timeout = 1;
2241 #ifdef TEST_FUZZ
2242 timeout_ms = 1;
2243 #endif /* TEST_FUZZ */
2244 wpa_printf(MSG_DEBUG,
2245 "WPA: Use EAPOL-Key timeout of %u ms (retry counter %u)",
2246 timeout_ms, ctr);
2247 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
2248 wpa_send_eapol_timeout, wpa_auth, sm);
2249 }
2250
2251
2252 static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK,
2253 u8 *data, size_t data_len)
2254 {
2255 struct ieee802_1x_hdr *hdr;
2256 struct wpa_eapol_key *key;
2257 u16 key_info;
2258 int ret = 0;
2259 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN], *mic_pos;
2260 size_t mic_len = wpa_mic_len(akmp, pmk_len);
2261
2262 if (data_len < sizeof(*hdr) + sizeof(*key))
2263 return -1;
2264
2265 hdr = (struct ieee802_1x_hdr *) data;
2266 key = (struct wpa_eapol_key *) (hdr + 1);
2267 mic_pos = (u8 *) (key + 1);
2268 key_info = WPA_GET_BE16(key->key_info);
2269 os_memcpy(mic, mic_pos, mic_len);
2270 os_memset(mic_pos, 0, mic_len);
2271 if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp,
2272 key_info & WPA_KEY_INFO_TYPE_MASK,
2273 data, data_len, mic_pos) ||
2274 os_memcmp_const(mic, mic_pos, mic_len) != 0)
2275 ret = -1;
2276 os_memcpy(mic_pos, mic, mic_len);
2277 return ret;
2278 }
2279
2280
2281 void wpa_remove_ptk(struct wpa_state_machine *sm)
2282 {
2283 sm->PTK_valid = false;
2284 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
2285
2286 wpa_auth_remove_ptksa(sm->wpa_auth, sm->addr, sm->pairwise);
2287
2288 if (wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL,
2289 0, KEY_FLAG_PAIRWISE))
2290 wpa_printf(MSG_DEBUG,
2291 "RSN: PTK removal from the driver failed");
2292 if (sm->use_ext_key_id &&
2293 wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 1, NULL,
2294 0, KEY_FLAG_PAIRWISE))
2295 wpa_printf(MSG_DEBUG,
2296 "RSN: PTK Key ID 1 removal from the driver failed");
2297 sm->pairwise_set = false;
2298 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
2299 }
2300
2301
2302 int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)
2303 {
2304 int remove_ptk = 1;
2305
2306 if (!sm)
2307 return -1;
2308
2309 wpa_auth_vlogger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
2310 "event %d notification", event);
2311
2312 switch (event) {
2313 case WPA_AUTH:
2314 #ifdef CONFIG_MESH
2315 /* PTKs are derived through AMPE */
2316 if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
2317 /* not mesh */
2318 break;
2319 }
2320 return 0;
2321 #endif /* CONFIG_MESH */
2322 case WPA_ASSOC:
2323 break;
2324 case WPA_DEAUTH:
2325 case WPA_DISASSOC:
2326 sm->DeauthenticationRequest = true;
2327 os_memset(sm->PMK, 0, sizeof(sm->PMK));
2328 sm->pmk_len = 0;
2329 #ifdef CONFIG_IEEE80211R_AP
2330 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
2331 sm->xxkey_len = 0;
2332 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
2333 sm->pmk_r1_len = 0;
2334 #endif /* CONFIG_IEEE80211R_AP */
2335 break;
2336 case WPA_REAUTH:
2337 case WPA_REAUTH_EAPOL:
2338 if (!sm->started) {
2339 /*
2340 * When using WPS, we may end up here if the STA
2341 * manages to re-associate without the previous STA
2342 * entry getting removed. Consequently, we need to make
2343 * sure that the WPA state machines gets initialized
2344 * properly at this point.
2345 */
2346 wpa_printf(MSG_DEBUG,
2347 "WPA state machine had not been started - initialize now");
2348 sm->started = 1;
2349 sm->Init = true;
2350 if (wpa_sm_step(sm) == 1)
2351 return 1; /* should not really happen */
2352 sm->Init = false;
2353 sm->AuthenticationRequest = true;
2354 break;
2355 }
2356
2357 if (sm->ptkstart_without_success > 3) {
2358 wpa_printf(MSG_INFO,
2359 "WPA: Multiple EAP reauth attempts without 4-way handshake completion, disconnect "
2360 MACSTR, MAC2STR(sm->addr));
2361 sm->Disconnect = true;
2362 break;
2363 }
2364
2365 if (!sm->use_ext_key_id &&
2366 sm->wpa_auth->conf.wpa_deny_ptk0_rekey) {
2367 wpa_printf(MSG_INFO,
2368 "WPA: PTK0 rekey not allowed, disconnect "
2369 MACSTR, MAC2STR(wpa_auth_get_spa(sm)));
2370 sm->Disconnect = true;
2371 /* Try to encourage the STA to reconnect */
2372 sm->disconnect_reason =
2373 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
2374 break;
2375 }
2376
2377 if (sm->use_ext_key_id)
2378 sm->keyidx_active ^= 1; /* flip Key ID */
2379
2380 if (sm->GUpdateStationKeys) {
2381 /*
2382 * Reauthentication cancels the pending group key
2383 * update for this STA.
2384 */
2385 wpa_gkeydone_sta(sm);
2386 sm->PtkGroupInit = true;
2387 }
2388 sm->ReAuthenticationRequest = true;
2389 break;
2390 case WPA_ASSOC_FT:
2391 #ifdef CONFIG_IEEE80211R_AP
2392 wpa_printf(MSG_DEBUG,
2393 "FT: Retry PTK configuration after association");
2394 wpa_ft_install_ptk(sm, 1);
2395
2396 /* Using FT protocol, not WPA auth state machine */
2397 sm->ft_completed = 1;
2398 wpa_auth_set_ptk_rekey_timer(sm);
2399 return 0;
2400 #else /* CONFIG_IEEE80211R_AP */
2401 break;
2402 #endif /* CONFIG_IEEE80211R_AP */
2403 case WPA_ASSOC_FILS:
2404 #ifdef CONFIG_FILS
2405 wpa_printf(MSG_DEBUG,
2406 "FILS: TK configuration after association");
2407 fils_set_tk(sm);
2408 sm->fils_completed = 1;
2409 return 0;
2410 #else /* CONFIG_FILS */
2411 break;
2412 #endif /* CONFIG_FILS */
2413 case WPA_DRV_STA_REMOVED:
2414 sm->tk_already_set = false;
2415 return 0;
2416 }
2417
2418 #ifdef CONFIG_IEEE80211R_AP
2419 sm->ft_completed = 0;
2420 #endif /* CONFIG_IEEE80211R_AP */
2421
2422 if (sm->mgmt_frame_prot && event == WPA_AUTH)
2423 remove_ptk = 0;
2424 #ifdef CONFIG_FILS
2425 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
2426 (event == WPA_AUTH || event == WPA_ASSOC))
2427 remove_ptk = 0;
2428 #endif /* CONFIG_FILS */
2429
2430 if (remove_ptk) {
2431 sm->PTK_valid = false;
2432 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
2433
2434 if (event != WPA_REAUTH_EAPOL)
2435 wpa_remove_ptk(sm);
2436 }
2437
2438 if (sm->in_step_loop) {
2439 /*
2440 * wpa_sm_step() is already running - avoid recursive call to
2441 * it by making the existing loop process the new update.
2442 */
2443 sm->changed = true;
2444 return 0;
2445 }
2446 return wpa_sm_step(sm);
2447 }
2448
2449
2450 SM_STATE(WPA_PTK, INITIALIZE)
2451 {
2452 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
2453 if (sm->Init) {
2454 /* Init flag is not cleared here, so avoid busy
2455 * loop by claiming nothing changed. */
2456 sm->changed = false;
2457 }
2458
2459 sm->keycount = 0;
2460 if (sm->GUpdateStationKeys)
2461 wpa_gkeydone_sta(sm);
2462 if (sm->wpa == WPA_VERSION_WPA)
2463 sm->PInitAKeys = false;
2464 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
2465 * Local AA > Remote AA)) */) {
2466 sm->Pair = true;
2467 }
2468 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
2469 wpa_remove_ptk(sm);
2470 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
2471 sm->TimeoutCtr = 0;
2472 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
2473 sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP ||
2474 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
2475 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2476 WPA_EAPOL_authorized, 0);
2477 }
2478 }
2479
2480
2481 SM_STATE(WPA_PTK, DISCONNECT)
2482 {
2483 u16 reason = sm->disconnect_reason;
2484
2485 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
2486 sm->Disconnect = false;
2487 sm->disconnect_reason = 0;
2488 if (!reason)
2489 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
2490 wpa_sta_disconnect(sm->wpa_auth, sm->addr, reason);
2491 }
2492
2493
2494 SM_STATE(WPA_PTK, DISCONNECTED)
2495 {
2496 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
2497 sm->DeauthenticationRequest = false;
2498 }
2499
2500
2501 SM_STATE(WPA_PTK, AUTHENTICATION)
2502 {
2503 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
2504 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
2505 sm->PTK_valid = false;
2506 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
2507 1);
2508 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
2509 sm->AuthenticationRequest = false;
2510 }
2511
2512
2513 static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
2514 struct wpa_group *group)
2515 {
2516 if (group->first_sta_seen)
2517 return;
2518 /*
2519 * System has run bit further than at the time hostapd was started
2520 * potentially very early during boot up. This provides better chances
2521 * of collecting more randomness on embedded systems. Re-initialize the
2522 * GMK and Counter here to improve their strength if there was not
2523 * enough entropy available immediately after system startup.
2524 */
2525 wpa_printf(MSG_DEBUG,
2526 "WPA: Re-initialize GMK/Counter on first station");
2527 if (random_pool_ready() != 1) {
2528 wpa_printf(MSG_INFO,
2529 "WPA: Not enough entropy in random pool to proceed - reject first 4-way handshake");
2530 group->reject_4way_hs_for_entropy = true;
2531 } else {
2532 group->first_sta_seen = true;
2533 group->reject_4way_hs_for_entropy = false;
2534 }
2535
2536 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 ||
2537 wpa_gtk_update(wpa_auth, group) < 0 ||
2538 wpa_group_config_group_keys(wpa_auth, group) < 0) {
2539 wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed");
2540 group->first_sta_seen = false;
2541 group->reject_4way_hs_for_entropy = true;
2542 }
2543 }
2544
2545
2546 SM_STATE(WPA_PTK, AUTHENTICATION2)
2547 {
2548 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
2549
2550 wpa_group_ensure_init(sm->wpa_auth, sm->group);
2551 sm->ReAuthenticationRequest = false;
2552
2553 /*
2554 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
2555 * ambiguous. The Authenticator state machine uses a counter that is
2556 * incremented by one for each 4-way handshake. However, the security
2557 * analysis of 4-way handshake points out that unpredictable nonces
2558 * help in preventing precomputation attacks. Instead of the state
2559 * machine definition, use an unpredictable nonce value here to provide
2560 * stronger protection against potential precomputation attacks.
2561 */
2562 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
2563 wpa_printf(MSG_ERROR,
2564 "WPA: Failed to get random data for ANonce.");
2565 sm->Disconnect = true;
2566 return;
2567 }
2568 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
2569 WPA_NONCE_LEN);
2570 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
2571 * logical place than INITIALIZE since AUTHENTICATION2 can be
2572 * re-entered on ReAuthenticationRequest without going through
2573 * INITIALIZE. */
2574 sm->TimeoutCtr = 0;
2575 }
2576
2577
2578 static int wpa_auth_sm_ptk_update(struct wpa_state_machine *sm)
2579 {
2580 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
2581 wpa_printf(MSG_ERROR,
2582 "WPA: Failed to get random data for ANonce");
2583 sm->Disconnect = true;
2584 return -1;
2585 }
2586 wpa_hexdump(MSG_DEBUG, "WPA: Assign new ANonce", sm->ANonce,
2587 WPA_NONCE_LEN);
2588 sm->TimeoutCtr = 0;
2589 return 0;
2590 }
2591
2592
2593 SM_STATE(WPA_PTK, INITPMK)
2594 {
2595 u8 msk[2 * PMK_LEN];
2596 size_t len = 2 * PMK_LEN;
2597
2598 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
2599 #ifdef CONFIG_IEEE80211R_AP
2600 sm->xxkey_len = 0;
2601 #endif /* CONFIG_IEEE80211R_AP */
2602 if (sm->pmksa) {
2603 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
2604 os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
2605 sm->pmk_len = sm->pmksa->pmk_len;
2606 #ifdef CONFIG_DPP
2607 } else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP) {
2608 wpa_printf(MSG_DEBUG,
2609 "DPP: No PMKSA cache entry for STA - reject connection");
2610 sm->Disconnect = true;
2611 sm->disconnect_reason = WLAN_REASON_INVALID_PMKID;
2612 return;
2613 #endif /* CONFIG_DPP */
2614 } else if (wpa_auth_get_msk(sm->wpa_auth, wpa_auth_get_spa(sm),
2615 msk, &len) == 0) {
2616 unsigned int pmk_len;
2617
2618 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt))
2619 pmk_len = PMK_LEN_SUITE_B_192;
2620 else
2621 pmk_len = PMK_LEN;
2622 wpa_printf(MSG_DEBUG,
2623 "WPA: PMK from EAPOL state machine (MSK len=%zu PMK len=%u)",
2624 len, pmk_len);
2625 if (len < pmk_len) {
2626 wpa_printf(MSG_DEBUG,
2627 "WPA: MSK not long enough (%zu) to create PMK (%u)",
2628 len, pmk_len);
2629 sm->Disconnect = true;
2630 return;
2631 }
2632 os_memcpy(sm->PMK, msk, pmk_len);
2633 sm->pmk_len = pmk_len;
2634 #ifdef CONFIG_IEEE80211R_AP
2635 if (len >= 2 * PMK_LEN) {
2636 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
2637 os_memcpy(sm->xxkey, msk, SHA384_MAC_LEN);
2638 sm->xxkey_len = SHA384_MAC_LEN;
2639 } else {
2640 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
2641 sm->xxkey_len = PMK_LEN;
2642 }
2643 }
2644 #endif /* CONFIG_IEEE80211R_AP */
2645 } else {
2646 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p",
2647 sm->wpa_auth->cb->get_msk);
2648 sm->Disconnect = true;
2649 return;
2650 }
2651 forced_memzero(msk, sizeof(msk));
2652
2653 sm->req_replay_counter_used = 0;
2654 /* IEEE 802.11i does not set keyRun to false, but not doing this
2655 * will break reauthentication since EAPOL state machines may not be
2656 * get into AUTHENTICATING state that clears keyRun before WPA state
2657 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
2658 * state and takes PMK from the previously used AAA Key. This will
2659 * eventually fail in 4-Way Handshake because Supplicant uses PMK
2660 * derived from the new AAA Key. Setting keyRun = false here seems to
2661 * be good workaround for this issue. */
2662 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, false);
2663 }
2664
2665
2666 SM_STATE(WPA_PTK, INITPSK)
2667 {
2668 const u8 *psk;
2669 size_t psk_len;
2670
2671 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
2672 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL,
2673 &psk_len, NULL);
2674 if (psk) {
2675 os_memcpy(sm->PMK, psk, psk_len);
2676 sm->pmk_len = psk_len;
2677 #ifdef CONFIG_IEEE80211R_AP
2678 sm->xxkey_len = PMK_LEN;
2679 #ifdef CONFIG_SAE
2680 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
2681 (psk_len == SHA512_MAC_LEN || psk_len == SHA384_MAC_LEN ||
2682 psk_len == SHA256_MAC_LEN))
2683 sm->xxkey_len = psk_len;
2684 #endif /* CONFIG_SAE */
2685 os_memcpy(sm->xxkey, psk, sm->xxkey_len);
2686 #endif /* CONFIG_IEEE80211R_AP */
2687 }
2688 #ifdef CONFIG_SAE
2689 if (wpa_auth_uses_sae(sm) && sm->pmksa) {
2690 wpa_printf(MSG_DEBUG, "SAE: PMK from PMKSA cache (len=%zu)",
2691 sm->pmksa->pmk_len);
2692 os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
2693 sm->pmk_len = sm->pmksa->pmk_len;
2694 #ifdef CONFIG_IEEE80211R_AP
2695 os_memcpy(sm->xxkey, sm->pmksa->pmk, sm->pmksa->pmk_len);
2696 sm->xxkey_len = sm->pmksa->pmk_len;
2697 #endif /* CONFIG_IEEE80211R_AP */
2698 }
2699 #endif /* CONFIG_SAE */
2700 sm->req_replay_counter_used = 0;
2701 }
2702
2703
2704 SM_STATE(WPA_PTK, PTKSTART)
2705 {
2706 u8 *buf;
2707 size_t buf_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
2708 u8 *pmkid = NULL;
2709 size_t kde_len = 0;
2710 u16 key_info;
2711 #ifdef CONFIG_TESTING_OPTIONS
2712 struct wpa_auth_config *conf = &sm->wpa_auth->conf;
2713 #endif /* CONFIG_TESTING_OPTIONS */
2714
2715 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
2716 sm->PTKRequest = false;
2717 sm->TimeoutEvt = false;
2718 sm->alt_snonce_valid = false;
2719 sm->ptkstart_without_success++;
2720
2721 sm->TimeoutCtr++;
2722 if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
2723 /* No point in sending the EAPOL-Key - we will disconnect
2724 * immediately following this. */
2725 return;
2726 }
2727
2728 #ifdef CONFIG_IEEE80211BE
2729 if (sm->mld_assoc_link_id >= 0)
2730 buf_len += 2 + RSN_SELECTOR_LEN + ETH_ALEN;
2731 #endif /* CONFIG_IEEE80211BE */
2732 #ifdef CONFIG_TESTING_OPTIONS
2733 if (conf->eapol_m1_elements)
2734 buf_len += wpabuf_len(conf->eapol_m1_elements);
2735 #endif /* CONFIG_TESTING_OPTIONS */
2736
2737 buf = os_zalloc(buf_len);
2738 if (!buf)
2739 return;
2740
2741 wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
2742 "sending 1/4 msg of 4-Way Handshake");
2743 /*
2744 * For infrastructure BSS cases, it is better for the AP not to include
2745 * the PMKID KDE in EAPOL-Key msg 1/4 since it could be used to initiate
2746 * offline search for the passphrase/PSK without having to be able to
2747 * capture a 4-way handshake from a STA that has access to the network.
2748 *
2749 * For IBSS cases, addition of PMKID KDE could be considered even with
2750 * WPA2-PSK cases that use multiple PSKs, but only if there is a single
2751 * possible PSK for this STA. However, this should not be done unless
2752 * there is support for using that information on the supplicant side.
2753 * The concern about exposing PMKID unnecessarily in infrastructure BSS
2754 * cases would also apply here, but at least in the IBSS case, this
2755 * would cover a potential real use case.
2756 */
2757 if (sm->wpa == WPA_VERSION_WPA2 &&
2758 (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) ||
2759 (sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE && sm->pmksa) ||
2760 wpa_key_mgmt_sae(sm->wpa_key_mgmt)) &&
2761 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
2762 pmkid = buf;
2763 kde_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
2764 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
2765 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
2766 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
2767 if (sm->pmksa) {
2768 wpa_hexdump(MSG_DEBUG,
2769 "RSN: Message 1/4 PMKID from PMKSA entry",
2770 sm->pmksa->pmkid, PMKID_LEN);
2771 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2772 sm->pmksa->pmkid, PMKID_LEN);
2773 } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
2774 /* No KCK available to derive PMKID */
2775 wpa_printf(MSG_DEBUG,
2776 "RSN: No KCK available to derive PMKID for message 1/4");
2777 pmkid = NULL;
2778 #ifdef CONFIG_FILS
2779 } else if (wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2780 if (sm->pmkid_set) {
2781 wpa_hexdump(MSG_DEBUG,
2782 "RSN: Message 1/4 PMKID from FILS/ERP",
2783 sm->pmkid, PMKID_LEN);
2784 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2785 sm->pmkid, PMKID_LEN);
2786 } else {
2787 /* No PMKID available */
2788 wpa_printf(MSG_DEBUG,
2789 "RSN: No FILS/ERP PMKID available for message 1/4");
2790 pmkid = NULL;
2791 }
2792 #endif /* CONFIG_FILS */
2793 #ifdef CONFIG_IEEE80211R_AP
2794 } else if (wpa_key_mgmt_ft(sm->wpa_key_mgmt) &&
2795 sm->ft_completed) {
2796 wpa_printf(MSG_DEBUG,
2797 "FT: No PMKID in message 1/4 when using FT protocol");
2798 pmkid = NULL;
2799 #endif /* CONFIG_IEEE80211R_AP */
2800 #ifdef CONFIG_SAE
2801 } else if (wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
2802 if (sm->pmkid_set) {
2803 wpa_hexdump(MSG_DEBUG,
2804 "RSN: Message 1/4 PMKID from SAE",
2805 sm->pmkid, PMKID_LEN);
2806 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2807 sm->pmkid, PMKID_LEN);
2808 } else {
2809 /* No PMKID available */
2810 wpa_printf(MSG_DEBUG,
2811 "RSN: No SAE PMKID available for message 1/4");
2812 pmkid = NULL;
2813 }
2814 #endif /* CONFIG_SAE */
2815 } else {
2816 /*
2817 * Calculate PMKID since no PMKSA cache entry was
2818 * available with pre-calculated PMKID.
2819 */
2820 rsn_pmkid(sm->PMK, sm->pmk_len,
2821 wpa_auth_get_aa(sm),
2822 wpa_auth_get_spa(sm),
2823 &pmkid[2 + RSN_SELECTOR_LEN],
2824 sm->wpa_key_mgmt);
2825 wpa_hexdump(MSG_DEBUG,
2826 "RSN: Message 1/4 PMKID derived from PMK",
2827 &pmkid[2 + RSN_SELECTOR_LEN], PMKID_LEN);
2828 }
2829 }
2830 if (!pmkid)
2831 kde_len = 0;
2832
2833 #ifdef CONFIG_IEEE80211BE
2834 if (sm->mld_assoc_link_id >= 0) {
2835 wpa_printf(MSG_DEBUG,
2836 "RSN: MLD: Add MAC Address KDE: kde_len=%zu",
2837 kde_len);
2838 wpa_add_kde(buf + kde_len, RSN_KEY_DATA_MAC_ADDR,
2839 sm->wpa_auth->mld_addr, ETH_ALEN, NULL, 0);
2840 kde_len += 2 + RSN_SELECTOR_LEN + ETH_ALEN;
2841 }
2842 #endif /* CONFIG_IEEE80211BE */
2843
2844 #ifdef CONFIG_TESTING_OPTIONS
2845 if (conf->eapol_m1_elements) {
2846 os_memcpy(buf + kde_len, wpabuf_head(conf->eapol_m1_elements),
2847 wpabuf_len(conf->eapol_m1_elements));
2848 kde_len += wpabuf_len(conf->eapol_m1_elements);
2849 }
2850 #endif /* CONFIG_TESTING_OPTIONS */
2851
2852 key_info = WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE;
2853 if (sm->pairwise_set && sm->wpa != WPA_VERSION_WPA)
2854 key_info |= WPA_KEY_INFO_SECURE;
2855 wpa_send_eapol(sm->wpa_auth, sm, key_info, NULL,
2856 sm->ANonce, kde_len ? buf : NULL, kde_len, 0, 0);
2857 os_free(buf);
2858 }
2859
2860
2861 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
2862 const u8 *pmk, unsigned int pmk_len,
2863 struct wpa_ptk *ptk, int force_sha256,
2864 u8 *pmk_r0, u8 *pmk_r1, u8 *pmk_r0_name,
2865 size_t *key_len, bool no_kdk)
2866 {
2867 const u8 *z = NULL;
2868 size_t z_len = 0, kdk_len;
2869 int akmp;
2870 int ret;
2871
2872 if (sm->wpa_auth->conf.force_kdk_derivation ||
2873 (!no_kdk && sm->wpa_auth->conf.secure_ltf &&
2874 ieee802_11_rsnx_capab(sm->rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
2875 kdk_len = WPA_KDK_MAX_LEN;
2876 else
2877 kdk_len = 0;
2878
2879 #ifdef CONFIG_IEEE80211R_AP
2880 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2881 if (sm->ft_completed) {
2882 u8 ptk_name[WPA_PMK_NAME_LEN];
2883
2884 ret = wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->pmk_r1_len,
2885 sm->SNonce, sm->ANonce,
2886 wpa_auth_get_spa(sm),
2887 wpa_auth_get_aa(sm),
2888 sm->pmk_r1_name, ptk,
2889 ptk_name, sm->wpa_key_mgmt,
2890 sm->pairwise, kdk_len);
2891 } else {
2892 ret = wpa_auth_derive_ptk_ft(sm, ptk, pmk_r0, pmk_r1,
2893 pmk_r0_name, key_len,
2894 kdk_len);
2895 }
2896 if (ret) {
2897 wpa_printf(MSG_ERROR, "FT: PTK derivation failed");
2898 return ret;
2899 }
2900
2901 #ifdef CONFIG_PASN
2902 if (!no_kdk && sm->wpa_auth->conf.secure_ltf &&
2903 ieee802_11_rsnx_capab(sm->rsnxe,
2904 WLAN_RSNX_CAPAB_SECURE_LTF)) {
2905 ret = wpa_ltf_keyseed(ptk, sm->wpa_key_mgmt,
2906 sm->pairwise);
2907 if (ret) {
2908 wpa_printf(MSG_ERROR,
2909 "FT: LTF keyseed derivation failed");
2910 }
2911 }
2912 #endif /* CONFIG_PASN */
2913 return ret;
2914 }
2915 #endif /* CONFIG_IEEE80211R_AP */
2916
2917 #ifdef CONFIG_DPP2
2918 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
2919 z = wpabuf_head(sm->dpp_z);
2920 z_len = wpabuf_len(sm->dpp_z);
2921 }
2922 #endif /* CONFIG_DPP2 */
2923
2924 akmp = sm->wpa_key_mgmt;
2925 if (force_sha256)
2926 akmp |= WPA_KEY_MGMT_PSK_SHA256;
2927 ret = wpa_pmk_to_ptk(pmk, pmk_len, "Pairwise key expansion",
2928 wpa_auth_get_aa(sm), wpa_auth_get_spa(sm),
2929 sm->ANonce, snonce, ptk, akmp,
2930 sm->pairwise, z, z_len, kdk_len);
2931 if (ret) {
2932 wpa_printf(MSG_DEBUG,
2933 "WPA: PTK derivation failed");
2934 return ret;
2935 }
2936
2937 #ifdef CONFIG_PASN
2938 if (!no_kdk && sm->wpa_auth->conf.secure_ltf &&
2939 ieee802_11_rsnx_capab(sm->rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)) {
2940 ret = wpa_ltf_keyseed(ptk, sm->wpa_key_mgmt, sm->pairwise);
2941 if (ret) {
2942 wpa_printf(MSG_DEBUG,
2943 "WPA: LTF keyseed derivation failed");
2944 }
2945 }
2946 #endif /* CONFIG_PASN */
2947 return ret;
2948 }
2949
2950
2951 #ifdef CONFIG_FILS
2952
2953 int fils_auth_pmk_to_ptk(struct wpa_state_machine *sm, const u8 *pmk,
2954 size_t pmk_len, const u8 *snonce, const u8 *anonce,
2955 const u8 *dhss, size_t dhss_len,
2956 struct wpabuf *g_sta, struct wpabuf *g_ap)
2957 {
2958 u8 ick[FILS_ICK_MAX_LEN];
2959 size_t ick_len;
2960 int res;
2961 u8 fils_ft[FILS_FT_MAX_LEN];
2962 size_t fils_ft_len = 0, kdk_len;
2963
2964 if (sm->wpa_auth->conf.force_kdk_derivation ||
2965 (sm->wpa_auth->conf.secure_ltf &&
2966 ieee802_11_rsnx_capab(sm->rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
2967 kdk_len = WPA_KDK_MAX_LEN;
2968 else
2969 kdk_len = 0;
2970
2971 res = fils_pmk_to_ptk(pmk, pmk_len, wpa_auth_get_spa(sm),
2972 wpa_auth_get_aa(sm),
2973 snonce, anonce, dhss, dhss_len,
2974 &sm->PTK, ick, &ick_len,
2975 sm->wpa_key_mgmt, sm->pairwise,
2976 fils_ft, &fils_ft_len, kdk_len);
2977 if (res < 0)
2978 return res;
2979
2980 #ifdef CONFIG_PASN
2981 if (sm->wpa_auth->conf.secure_ltf &&
2982 ieee802_11_rsnx_capab(sm->rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)) {
2983 res = wpa_ltf_keyseed(&sm->PTK, sm->wpa_key_mgmt, sm->pairwise);
2984 if (res) {
2985 wpa_printf(MSG_ERROR,
2986 "FILS: LTF keyseed derivation failed");
2987 return res;
2988 }
2989 }
2990 #endif /* CONFIG_PASN */
2991
2992 sm->PTK_valid = true;
2993 sm->tk_already_set = false;
2994
2995 #ifdef CONFIG_IEEE80211R_AP
2996 if (fils_ft_len) {
2997 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2998 struct wpa_auth_config *conf = &wpa_auth->conf;
2999 u8 pmk_r0[PMK_LEN_MAX], pmk_r0_name[WPA_PMK_NAME_LEN];
3000
3001 if (wpa_derive_pmk_r0(fils_ft, fils_ft_len,
3002 conf->ssid, conf->ssid_len,
3003 conf->mobility_domain,
3004 conf->r0_key_holder,
3005 conf->r0_key_holder_len,
3006 wpa_auth_get_spa(sm), pmk_r0, pmk_r0_name,
3007 sm->wpa_key_mgmt) < 0)
3008 return -1;
3009
3010 wpa_ft_store_pmk_fils(sm, pmk_r0, pmk_r0_name);
3011 forced_memzero(fils_ft, sizeof(fils_ft));
3012
3013 res = wpa_derive_pmk_r1_name(pmk_r0_name, conf->r1_key_holder,
3014 wpa_auth_get_spa(sm),
3015 sm->pmk_r1_name,
3016 fils_ft_len);
3017 forced_memzero(pmk_r0, PMK_LEN_MAX);
3018 if (res < 0)
3019 return -1;
3020 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", sm->pmk_r1_name,
3021 WPA_PMK_NAME_LEN);
3022 sm->pmk_r1_name_valid = 1;
3023 }
3024 #endif /* CONFIG_IEEE80211R_AP */
3025
3026 res = fils_key_auth_sk(ick, ick_len, snonce, anonce,
3027 wpa_auth_get_spa(sm),
3028 wpa_auth_get_aa(sm),
3029 g_sta ? wpabuf_head(g_sta) : NULL,
3030 g_sta ? wpabuf_len(g_sta) : 0,
3031 g_ap ? wpabuf_head(g_ap) : NULL,
3032 g_ap ? wpabuf_len(g_ap) : 0,
3033 sm->wpa_key_mgmt, sm->fils_key_auth_sta,
3034 sm->fils_key_auth_ap,
3035 &sm->fils_key_auth_len);
3036 forced_memzero(ick, sizeof(ick));
3037
3038 /* Store nonces for (Re)Association Request/Response frame processing */
3039 os_memcpy(sm->SNonce, snonce, FILS_NONCE_LEN);
3040 os_memcpy(sm->ANonce, anonce, FILS_NONCE_LEN);
3041
3042 return res;
3043 }
3044
3045
3046 static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
3047 u8 *buf, size_t buf_len, u16 *_key_data_len)
3048 {
3049 struct ieee802_1x_hdr *hdr;
3050 struct wpa_eapol_key *key;
3051 u8 *pos;
3052 u16 key_data_len;
3053 u8 *tmp;
3054 const u8 *aad[1];
3055 size_t aad_len[1];
3056
3057 hdr = (struct ieee802_1x_hdr *) buf;
3058 key = (struct wpa_eapol_key *) (hdr + 1);
3059 pos = (u8 *) (key + 1);
3060 key_data_len = WPA_GET_BE16(pos);
3061 if (key_data_len < AES_BLOCK_SIZE ||
3062 key_data_len > buf_len - sizeof(*hdr) - sizeof(*key) - 2) {
3063 wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
3064 "No room for AES-SIV data in the frame");
3065 return -1;
3066 }
3067 pos += 2; /* Pointing at the Encrypted Key Data field */
3068
3069 tmp = os_malloc(key_data_len);
3070 if (!tmp)
3071 return -1;
3072
3073 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
3074 * to Key Data (exclusive). */
3075 aad[0] = buf;
3076 aad_len[0] = pos - buf;
3077 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, key_data_len,
3078 1, aad, aad_len, tmp) < 0) {
3079 wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
3080 "Invalid AES-SIV data in the frame");
3081 bin_clear_free(tmp, key_data_len);
3082 return -1;
3083 }
3084
3085 /* AEAD decryption and validation completed successfully */
3086 key_data_len -= AES_BLOCK_SIZE;
3087 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
3088 tmp, key_data_len);
3089
3090 /* Replace Key Data field with the decrypted version */
3091 os_memcpy(pos, tmp, key_data_len);
3092 pos -= 2; /* Key Data Length field */
3093 WPA_PUT_BE16(pos, key_data_len);
3094 bin_clear_free(tmp, key_data_len);
3095 if (_key_data_len)
3096 *_key_data_len = key_data_len;
3097 return 0;
3098 }
3099
3100
3101 const u8 * wpa_fils_validate_fils_session(struct wpa_state_machine *sm,
3102 const u8 *ies, size_t ies_len,
3103 const u8 *fils_session)
3104 {
3105 const u8 *ie, *end;
3106 const u8 *session = NULL;
3107
3108 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
3109 wpa_printf(MSG_DEBUG,
3110 "FILS: Not a FILS AKM - reject association");
3111 return NULL;
3112 }
3113
3114 /* Verify Session element */
3115 ie = ies;
3116 end = ((const u8 *) ie) + ies_len;
3117 while (ie + 1 < end) {
3118 if (ie + 2 + ie[1] > end)
3119 break;
3120 if (ie[0] == WLAN_EID_EXTENSION &&
3121 ie[1] >= 1 + FILS_SESSION_LEN &&
3122 ie[2] == WLAN_EID_EXT_FILS_SESSION) {
3123 session = ie;
3124 break;
3125 }
3126 ie += 2 + ie[1];
3127 }
3128
3129 if (!session) {
3130 wpa_printf(MSG_DEBUG,
3131 "FILS: %s: Could not find FILS Session element in Assoc Req - reject",
3132 __func__);
3133 return NULL;
3134 }
3135
3136 if (!fils_session) {
3137 wpa_printf(MSG_DEBUG,
3138 "FILS: %s: Could not find FILS Session element in STA entry - reject",
3139 __func__);
3140 return NULL;
3141 }
3142
3143 if (os_memcmp(fils_session, session + 3, FILS_SESSION_LEN) != 0) {
3144 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
3145 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
3146 fils_session, FILS_SESSION_LEN);
3147 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
3148 session + 3, FILS_SESSION_LEN);
3149 return NULL;
3150 }
3151 return session;
3152 }
3153
3154
3155 int wpa_fils_validate_key_confirm(struct wpa_state_machine *sm, const u8 *ies,
3156 size_t ies_len)
3157 {
3158 struct ieee802_11_elems elems;
3159
3160 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
3161 wpa_printf(MSG_DEBUG,
3162 "FILS: Failed to parse decrypted elements");
3163 return -1;
3164 }
3165
3166 if (!elems.fils_session) {
3167 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
3168 return -1;
3169 }
3170
3171 if (!elems.fils_key_confirm) {
3172 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
3173 return -1;
3174 }
3175
3176 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
3177 wpa_printf(MSG_DEBUG,
3178 "FILS: Unexpected Key-Auth length %d (expected %zu)",
3179 elems.fils_key_confirm_len,
3180 sm->fils_key_auth_len);
3181 return -1;
3182 }
3183
3184 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_sta,
3185 sm->fils_key_auth_len) != 0) {
3186 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
3187 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
3188 elems.fils_key_confirm, elems.fils_key_confirm_len);
3189 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
3190 sm->fils_key_auth_sta, sm->fils_key_auth_len);
3191 return -1;
3192 }
3193
3194 return 0;
3195 }
3196
3197
3198 int fils_decrypt_assoc(struct wpa_state_machine *sm, const u8 *fils_session,
3199 const struct ieee80211_mgmt *mgmt, size_t frame_len,
3200 u8 *pos, size_t left)
3201 {
3202 u16 fc, stype;
3203 const u8 *end, *ie_start, *ie, *session, *crypt;
3204 const u8 *aad[5];
3205 size_t aad_len[5];
3206
3207 if (!sm || !sm->PTK_valid) {
3208 wpa_printf(MSG_DEBUG,
3209 "FILS: No KEK to decrypt Assocication Request frame");
3210 return -1;
3211 }
3212
3213 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
3214 wpa_printf(MSG_DEBUG,
3215 "FILS: Not a FILS AKM - reject association");
3216 return -1;
3217 }
3218
3219 end = ((const u8 *) mgmt) + frame_len;
3220 fc = le_to_host16(mgmt->frame_control);
3221 stype = WLAN_FC_GET_STYPE(fc);
3222 if (stype == WLAN_FC_STYPE_REASSOC_REQ)
3223 ie_start = mgmt->u.reassoc_req.variable;
3224 else
3225 ie_start = mgmt->u.assoc_req.variable;
3226 ie = ie_start;
3227
3228 /*
3229 * Find FILS Session element which is the last unencrypted element in
3230 * the frame.
3231 */
3232 session = wpa_fils_validate_fils_session(sm, ie, end - ie,
3233 fils_session);
3234 if (!session) {
3235 wpa_printf(MSG_DEBUG, "FILS: Session validation failed");
3236 return -1;
3237 }
3238
3239 crypt = session + 2 + session[1];
3240
3241 if (end - crypt < AES_BLOCK_SIZE) {
3242 wpa_printf(MSG_DEBUG,
3243 "FILS: Too short frame to include AES-SIV data");
3244 return -1;
3245 }
3246
3247 /* AES-SIV AAD vectors */
3248
3249 /* The STA's MAC address */
3250 aad[0] = mgmt->sa;
3251 aad_len[0] = ETH_ALEN;
3252 /* The AP's BSSID */
3253 aad[1] = mgmt->da;
3254 aad_len[1] = ETH_ALEN;
3255 /* The STA's nonce */
3256 aad[2] = sm->SNonce;
3257 aad_len[2] = FILS_NONCE_LEN;
3258 /* The AP's nonce */
3259 aad[3] = sm->ANonce;
3260 aad_len[3] = FILS_NONCE_LEN;
3261 /*
3262 * The (Re)Association Request frame from the Capability Information
3263 * field to the FILS Session element (both inclusive).
3264 */
3265 aad[4] = (const u8 *) &mgmt->u.assoc_req.capab_info;
3266 aad_len[4] = crypt - aad[4];
3267
3268 if (aes_siv_decrypt(sm->PTK.kek, sm->PTK.kek_len, crypt, end - crypt,
3269 5, aad, aad_len, pos + (crypt - ie_start)) < 0) {
3270 wpa_printf(MSG_DEBUG,
3271 "FILS: Invalid AES-SIV data in the frame");
3272 return -1;
3273 }
3274 wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Request elements",
3275 pos, left - AES_BLOCK_SIZE);
3276
3277 if (wpa_fils_validate_key_confirm(sm, pos, left - AES_BLOCK_SIZE) < 0) {
3278 wpa_printf(MSG_DEBUG, "FILS: Key Confirm validation failed");
3279 return -1;
3280 }
3281
3282 return left - AES_BLOCK_SIZE;
3283 }
3284
3285
3286 int fils_encrypt_assoc(struct wpa_state_machine *sm, u8 *buf,
3287 size_t current_len, size_t max_len,
3288 const struct wpabuf *hlp)
3289 {
3290 u8 *end = buf + max_len;
3291 u8 *pos = buf + current_len;
3292 struct ieee80211_mgmt *mgmt;
3293 struct wpabuf *plain;
3294 const u8 *aad[5];
3295 size_t aad_len[5];
3296
3297 if (!sm || !sm->PTK_valid)
3298 return -1;
3299
3300 wpa_hexdump(MSG_DEBUG,
3301 "FILS: Association Response frame before FILS processing",
3302 buf, current_len);
3303
3304 mgmt = (struct ieee80211_mgmt *) buf;
3305
3306 /* AES-SIV AAD vectors */
3307
3308 /* The AP's BSSID */
3309 aad[0] = mgmt->sa;
3310 aad_len[0] = ETH_ALEN;
3311 /* The STA's MAC address */
3312 aad[1] = mgmt->da;
3313 aad_len[1] = ETH_ALEN;
3314 /* The AP's nonce */
3315 aad[2] = sm->ANonce;
3316 aad_len[2] = FILS_NONCE_LEN;
3317 /* The STA's nonce */
3318 aad[3] = sm->SNonce;
3319 aad_len[3] = FILS_NONCE_LEN;
3320 /*
3321 * The (Re)Association Response frame from the Capability Information
3322 * field (the same offset in both Association and Reassociation
3323 * Response frames) to the FILS Session element (both inclusive).
3324 */
3325 aad[4] = (const u8 *) &mgmt->u.assoc_resp.capab_info;
3326 aad_len[4] = pos - aad[4];
3327
3328 /* The following elements will be encrypted with AES-SIV */
3329 plain = fils_prepare_plainbuf(sm, hlp);
3330 if (!plain) {
3331 wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
3332 return -1;
3333 }
3334
3335 if (pos + wpabuf_len(plain) + AES_BLOCK_SIZE > end) {
3336 wpa_printf(MSG_DEBUG,
3337 "FILS: Not enough room for FILS elements");
3338 wpabuf_clear_free(plain);
3339 return -1;
3340 }
3341
3342 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: Association Response plaintext",
3343 plain);
3344
3345 if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len,
3346 wpabuf_head(plain), wpabuf_len(plain),
3347 5, aad, aad_len, pos) < 0) {
3348 wpabuf_clear_free(plain);
3349 return -1;
3350 }
3351
3352 wpa_hexdump(MSG_DEBUG,
3353 "FILS: Encrypted Association Response elements",
3354 pos, AES_BLOCK_SIZE + wpabuf_len(plain));
3355 current_len += wpabuf_len(plain) + AES_BLOCK_SIZE;
3356 wpabuf_clear_free(plain);
3357
3358 sm->fils_completed = 1;
3359
3360 return current_len;
3361 }
3362
3363
3364 static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm,
3365 const struct wpabuf *hlp)
3366 {
3367 struct wpabuf *plain;
3368 u8 *len, *tmp, *tmp2;
3369 u8 hdr[2];
3370 u8 *gtk, stub_gtk[32];
3371 size_t gtk_len;
3372 struct wpa_group *gsm;
3373 size_t plain_len;
3374 struct wpa_auth_config *conf = &sm->wpa_auth->conf;
3375
3376 plain_len = 1000 + ieee80211w_kde_len(sm);
3377 if (conf->transition_disable)
3378 plain_len += 2 + RSN_SELECTOR_LEN + 1;
3379 plain = wpabuf_alloc(plain_len);
3380 if (!plain)
3381 return NULL;
3382
3383 /* TODO: FILS Public Key */
3384
3385 /* FILS Key Confirmation */
3386 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
3387 wpabuf_put_u8(plain, 1 + sm->fils_key_auth_len); /* Length */
3388 /* Element ID Extension */
3389 wpabuf_put_u8(plain, WLAN_EID_EXT_FILS_KEY_CONFIRM);
3390 wpabuf_put_data(plain, sm->fils_key_auth_ap, sm->fils_key_auth_len);
3391
3392 /* FILS HLP Container */
3393 if (hlp)
3394 wpabuf_put_buf(plain, hlp);
3395
3396 /* TODO: FILS IP Address Assignment */
3397
3398 /* Key Delivery */
3399 gsm = sm->group;
3400 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
3401 len = wpabuf_put(plain, 1);
3402 wpabuf_put_u8(plain, WLAN_EID_EXT_KEY_DELIVERY);
3403 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN,
3404 wpabuf_put(plain, WPA_KEY_RSC_LEN));
3405 /* GTK KDE */
3406 gtk = gsm->GTK[gsm->GN - 1];
3407 gtk_len = gsm->GTK_len;
3408 if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
3409 /*
3410 * Provide unique random GTK to each STA to prevent use
3411 * of GTK in the BSS.
3412 */
3413 if (random_get_bytes(stub_gtk, gtk_len) < 0) {
3414 wpabuf_clear_free(plain);
3415 return NULL;
3416 }
3417 gtk = stub_gtk;
3418 }
3419 hdr[0] = gsm->GN & 0x03;
3420 hdr[1] = 0;
3421 tmp = wpabuf_put(plain, 0);
3422 tmp2 = wpa_add_kde(tmp, RSN_KEY_DATA_GROUPKEY, hdr, 2,
3423 gtk, gtk_len);
3424 wpabuf_put(plain, tmp2 - tmp);
3425
3426 /* IGTK KDE and BIGTK KDE */
3427 tmp = wpabuf_put(plain, 0);
3428 tmp2 = ieee80211w_kde_add(sm, tmp);
3429 wpabuf_put(plain, tmp2 - tmp);
3430
3431 if (conf->transition_disable) {
3432 tmp = wpabuf_put(plain, 0);
3433 tmp2 = wpa_add_kde(tmp, WFA_KEY_DATA_TRANSITION_DISABLE,
3434 &conf->transition_disable, 1, NULL, 0);
3435 wpabuf_put(plain, tmp2 - tmp);
3436 }
3437
3438 *len = (u8 *) wpabuf_put(plain, 0) - len - 1;
3439
3440 #ifdef CONFIG_OCV
3441 if (wpa_auth_uses_ocv(sm)) {
3442 struct wpa_channel_info ci;
3443 u8 *pos;
3444
3445 if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
3446 wpa_printf(MSG_WARNING,
3447 "FILS: Failed to get channel info for OCI element");
3448 wpabuf_clear_free(plain);
3449 return NULL;
3450 }
3451 #ifdef CONFIG_TESTING_OPTIONS
3452 if (conf->oci_freq_override_fils_assoc) {
3453 wpa_printf(MSG_INFO,
3454 "TEST: Override OCI frequency %d -> %u MHz",
3455 ci.frequency,
3456 conf->oci_freq_override_fils_assoc);
3457 ci.frequency = conf->oci_freq_override_fils_assoc;
3458 }
3459 #endif /* CONFIG_TESTING_OPTIONS */
3460
3461 pos = wpabuf_put(plain, OCV_OCI_EXTENDED_LEN);
3462 if (ocv_insert_extended_oci(&ci, pos) < 0) {
3463 wpabuf_clear_free(plain);
3464 return NULL;
3465 }
3466 }
3467 #endif /* CONFIG_OCV */
3468
3469 return plain;
3470 }
3471
3472
3473 int fils_set_tk(struct wpa_state_machine *sm)
3474 {
3475 enum wpa_alg alg;
3476 int klen;
3477
3478 if (!sm || !sm->PTK_valid) {
3479 wpa_printf(MSG_DEBUG, "FILS: No valid PTK available to set TK");
3480 return -1;
3481 }
3482 if (sm->tk_already_set) {
3483 wpa_printf(MSG_DEBUG, "FILS: TK already set to the driver");
3484 return -1;
3485 }
3486
3487 alg = wpa_cipher_to_alg(sm->pairwise);
3488 klen = wpa_cipher_key_len(sm->pairwise);
3489
3490 wpa_printf(MSG_DEBUG, "FILS: Configure TK to the driver");
3491 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
3492 sm->PTK.tk, klen, KEY_FLAG_PAIRWISE_RX_TX)) {
3493 wpa_printf(MSG_DEBUG, "FILS: Failed to set TK to the driver");
3494 return -1;
3495 }
3496
3497 #ifdef CONFIG_PASN
3498 if (sm->wpa_auth->conf.secure_ltf &&
3499 ieee802_11_rsnx_capab(sm->rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF) &&
3500 wpa_auth_set_ltf_keyseed(sm->wpa_auth, sm->addr,
3501 sm->PTK.ltf_keyseed,
3502 sm->PTK.ltf_keyseed_len)) {
3503 wpa_printf(MSG_ERROR,
3504 "FILS: Failed to set LTF keyseed to driver");
3505 return -1;
3506 }
3507 #endif /* CONFIG_PASN */
3508
3509 sm->pairwise_set = true;
3510 sm->tk_already_set = true;
3511
3512 wpa_auth_store_ptksa(sm->wpa_auth, sm->addr, sm->pairwise,
3513 dot11RSNAConfigPMKLifetime, &sm->PTK);
3514
3515 return 0;
3516 }
3517
3518
3519 u8 * hostapd_eid_assoc_fils_session(struct wpa_state_machine *sm, u8 *buf,
3520 const u8 *fils_session, struct wpabuf *hlp)
3521 {
3522 struct wpabuf *plain;
3523 u8 *pos = buf;
3524
3525 /* FILS Session */
3526 *pos++ = WLAN_EID_EXTENSION; /* Element ID */
3527 *pos++ = 1 + FILS_SESSION_LEN; /* Length */
3528 *pos++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */
3529 os_memcpy(pos, fils_session, FILS_SESSION_LEN);
3530 pos += FILS_SESSION_LEN;
3531
3532 plain = fils_prepare_plainbuf(sm, hlp);
3533 if (!plain) {
3534 wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
3535 return NULL;
3536 }
3537
3538 os_memcpy(pos, wpabuf_head(plain), wpabuf_len(plain));
3539 pos += wpabuf_len(plain);
3540
3541 wpa_printf(MSG_DEBUG, "%s: plain buf_len: %zu", __func__,
3542 wpabuf_len(plain));
3543 wpabuf_clear_free(plain);
3544 sm->fils_completed = 1;
3545 return pos;
3546 }
3547
3548 #endif /* CONFIG_FILS */
3549
3550
3551 #ifdef CONFIG_OCV
3552 int get_sta_tx_parameters(struct wpa_state_machine *sm, int ap_max_chanwidth,
3553 int ap_seg1_idx, int *bandwidth, int *seg1_idx)
3554 {
3555 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3556
3557 if (!wpa_auth->cb->get_sta_tx_params)
3558 return -1;
3559 return wpa_auth->cb->get_sta_tx_params(wpa_auth->cb_ctx, sm->addr,
3560 ap_max_chanwidth, ap_seg1_idx,
3561 bandwidth, seg1_idx);
3562 }
3563 #endif /* CONFIG_OCV */
3564
3565
3566 static int wpa_auth_validate_ml_kdes_m2(struct wpa_state_machine *sm,
3567 struct wpa_eapol_ie_parse *kde)
3568 {
3569 #ifdef CONFIG_IEEE80211BE
3570 int i;
3571 unsigned int n_links = 0;
3572
3573 if (sm->mld_assoc_link_id < 0)
3574 return 0;
3575
3576 /* MLD MAC address must be the same */
3577 if (!kde->mac_addr ||
3578 !ether_addr_equal(kde->mac_addr, sm->peer_mld_addr)) {
3579 wpa_printf(MSG_DEBUG, "RSN: MLD: Invalid MLD address");
3580 return -1;
3581 }
3582
3583 /* Find matching link ID and the MAC address for each link */
3584 for_each_link(kde->valid_mlo_links, i) {
3585 /*
3586 * Each entry should contain the link information and the MAC
3587 * address.
3588 */
3589 if (kde->mlo_link_len[i] != 1 + ETH_ALEN) {
3590 wpa_printf(MSG_DEBUG,
3591 "RSN: MLD: Invalid MLO Link (ID %u) KDE len=%zu",
3592 i, kde->mlo_link_len[i]);
3593 return -1;
3594 }
3595
3596 if (!sm->mld_links[i].valid || i == sm->mld_assoc_link_id) {
3597 wpa_printf(MSG_DEBUG,
3598 "RSN: MLD: Invalid link ID=%u", i);
3599 return -1;
3600 }
3601
3602 if (!ether_addr_equal(sm->mld_links[i].peer_addr,
3603 kde->mlo_link[i] + 1)) {
3604 wpa_printf(MSG_DEBUG,
3605 "RSN: MLD: invalid MAC address=" MACSTR
3606 " expected " MACSTR " (link ID %u)",
3607 MAC2STR(kde->mlo_link[i] + 1),
3608 MAC2STR(sm->mld_links[i].peer_addr), i);
3609 return -1;
3610 }
3611
3612 n_links++;
3613 }
3614
3615 /* Must have the same number of MLO links (excluding the local one) */
3616 if (n_links != sm->n_mld_affiliated_links) {
3617 wpa_printf(MSG_DEBUG,
3618 "RSN: MLD: Expecting %u MLD links in msg 2, but got %u",
3619 sm->n_mld_affiliated_links, n_links);
3620 return -1;
3621 }
3622 #endif /* CONFIG_IEEE80211BE */
3623
3624 return 0;
3625 }
3626
3627
3628 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
3629 {
3630 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3631 struct wpa_ptk PTK;
3632 int ok = 0, psk_found = 0;
3633 const u8 *pmk = NULL;
3634 size_t pmk_len;
3635 int ft;
3636 const u8 *eapol_key_ie, *key_data, *mic;
3637 u16 key_info, ver, key_data_length;
3638 size_t mic_len, eapol_key_ie_len;
3639 struct ieee802_1x_hdr *hdr;
3640 struct wpa_eapol_key *key;
3641 struct wpa_eapol_ie_parse kde;
3642 int vlan_id = 0;
3643 int owe_ptk_workaround = !!wpa_auth->conf.owe_ptk_workaround;
3644 u8 pmk_r0[PMK_LEN_MAX], pmk_r0_name[WPA_PMK_NAME_LEN];
3645 u8 pmk_r1[PMK_LEN_MAX];
3646 size_t key_len;
3647 u8 *key_data_buf = NULL;
3648 size_t key_data_buf_len = 0;
3649 bool derive_kdk, no_kdk = false;
3650
3651 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
3652 sm->EAPOLKeyReceived = false;
3653 sm->update_snonce = false;
3654 os_memset(&PTK, 0, sizeof(PTK));
3655
3656 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
3657
3658 derive_kdk = sm->wpa_auth->conf.secure_ltf &&
3659 ieee802_11_rsnx_capab(sm->rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF);
3660
3661 /* WPA with IEEE 802.1X: use the derived PMK from EAP
3662 * WPA-PSK: iterate through possible PSKs and select the one matching
3663 * the packet */
3664 for (;;) {
3665 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
3666 !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
3667 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
3668 sm->p2p_dev_addr, pmk, &pmk_len,
3669 &vlan_id);
3670 if (!pmk)
3671 break;
3672 psk_found = 1;
3673 #ifdef CONFIG_IEEE80211R_AP
3674 if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
3675 os_memcpy(sm->xxkey, pmk, pmk_len);
3676 sm->xxkey_len = pmk_len;
3677 }
3678 #endif /* CONFIG_IEEE80211R_AP */
3679 } else {
3680 pmk = sm->PMK;
3681 pmk_len = sm->pmk_len;
3682 }
3683
3684 if ((!pmk || !pmk_len) && sm->pmksa) {
3685 wpa_printf(MSG_DEBUG, "WPA: Use PMK from PMKSA cache");
3686 pmk = sm->pmksa->pmk;
3687 pmk_len = sm->pmksa->pmk_len;
3688 }
3689
3690 no_kdk = false;
3691 try_without_kdk:
3692 if (wpa_derive_ptk(sm, sm->SNonce, pmk, pmk_len, &PTK,
3693 owe_ptk_workaround == 2, pmk_r0, pmk_r1,
3694 pmk_r0_name, &key_len, no_kdk) < 0)
3695 break;
3696
3697 if (mic_len &&
3698 wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK,
3699 sm->last_rx_eapol_key,
3700 sm->last_rx_eapol_key_len) == 0) {
3701 if (sm->PMK != pmk) {
3702 os_memcpy(sm->PMK, pmk, pmk_len);
3703 sm->pmk_len = pmk_len;
3704 }
3705 ok = 1;
3706 break;
3707 }
3708
3709 #ifdef CONFIG_FILS
3710 if (!mic_len &&
3711 wpa_aead_decrypt(sm, &PTK, sm->last_rx_eapol_key,
3712 sm->last_rx_eapol_key_len, NULL) == 0) {
3713 ok = 1;
3714 break;
3715 }
3716 #endif /* CONFIG_FILS */
3717
3718 #ifdef CONFIG_OWE
3719 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE && pmk_len > 32 &&
3720 owe_ptk_workaround == 1) {
3721 wpa_printf(MSG_DEBUG,
3722 "OWE: Try PTK derivation workaround with SHA256");
3723 owe_ptk_workaround = 2;
3724 continue;
3725 }
3726 #endif /* CONFIG_OWE */
3727
3728 /* Some deployed STAs that advertise SecureLTF support in the
3729 * RSNXE in (Re)Association Request frames, do not derive KDK
3730 * during PTK generation. Try to work around this by checking if
3731 * a PTK derived without KDK would result in a matching MIC. */
3732 if (!sm->wpa_auth->conf.force_kdk_derivation &&
3733 derive_kdk && !no_kdk) {
3734 wpa_printf(MSG_DEBUG,
3735 "Try new PTK derivation without KDK as a workaround");
3736 no_kdk = true;
3737 goto try_without_kdk;
3738 }
3739
3740 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
3741 wpa_key_mgmt_sae(sm->wpa_key_mgmt))
3742 break;
3743 }
3744
3745 if (no_kdk && ok) {
3746 /* The workaround worked, so allow the 4-way handshake to be
3747 * completed with the PTK that was derived without the KDK. */
3748 wpa_printf(MSG_DEBUG,
3749 "PTK without KDK worked - misbehaving STA "
3750 MACSTR, MAC2STR(sm->addr));
3751 }
3752
3753 if (!ok && wpa_key_mgmt_wpa_psk_no_sae(sm->wpa_key_mgmt) &&
3754 wpa_auth->conf.radius_psk && wpa_auth->cb->request_radius_psk &&
3755 !sm->waiting_radius_psk) {
3756 wpa_printf(MSG_DEBUG, "No PSK available - ask RADIUS server");
3757 wpa_auth->cb->request_radius_psk(wpa_auth->cb_ctx, sm->addr,
3758 sm->wpa_key_mgmt,
3759 sm->ANonce,
3760 sm->last_rx_eapol_key,
3761 sm->last_rx_eapol_key_len);
3762 sm->waiting_radius_psk = 1;
3763 goto out;
3764 }
3765
3766 if (!ok) {
3767 wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm),
3768 LOGGER_DEBUG,
3769 "invalid MIC in msg 2/4 of 4-Way Handshake");
3770 if (psk_found)
3771 wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr);
3772 goto out;
3773 }
3774
3775 /*
3776 * Note: last_rx_eapol_key length fields have already been validated in
3777 * wpa_receive().
3778 */
3779 hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
3780 key = (struct wpa_eapol_key *) (hdr + 1);
3781 mic = (u8 *) (key + 1);
3782 key_info = WPA_GET_BE16(key->key_info);
3783 key_data = mic + mic_len + 2;
3784 key_data_length = WPA_GET_BE16(mic + mic_len);
3785 if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
3786 sizeof(*key) - mic_len - 2)
3787 goto out;
3788
3789 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
3790 if (mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
3791 if (ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
3792 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
3793 !wpa_use_aes_key_wrap(sm->wpa_key_mgmt)) {
3794 wpa_printf(MSG_INFO,
3795 "Unsupported EAPOL-Key Key Data field encryption");
3796 goto out;
3797 }
3798
3799 if (key_data_length < 8 || key_data_length % 8) {
3800 wpa_printf(MSG_INFO,
3801 "RSN: Unsupported AES-WRAP len %u",
3802 key_data_length);
3803 goto out;
3804 }
3805 key_data_length -= 8; /* AES-WRAP adds 8 bytes */
3806 key_data_buf = os_malloc(key_data_length);
3807 if (!key_data_buf)
3808 goto out;
3809 key_data_buf_len = key_data_length;
3810 if (aes_unwrap(PTK.kek, PTK.kek_len, key_data_length / 8,
3811 key_data, key_data_buf)) {
3812 bin_clear_free(key_data_buf, key_data_buf_len);
3813 wpa_printf(MSG_INFO,
3814 "RSN: AES unwrap failed - could not decrypt EAPOL-Key key data");
3815 goto out;
3816 }
3817 key_data = key_data_buf;
3818 wpa_hexdump_key(MSG_DEBUG, "RSN: Decrypted EAPOL-Key Key Data",
3819 key_data, key_data_length);
3820 }
3821
3822 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
3823 wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
3824 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
3825 goto out;
3826 }
3827 if (kde.rsn_ie) {
3828 eapol_key_ie = kde.rsn_ie;
3829 eapol_key_ie_len = kde.rsn_ie_len;
3830 } else if (kde.osen) {
3831 eapol_key_ie = kde.osen;
3832 eapol_key_ie_len = kde.osen_len;
3833 } else {
3834 eapol_key_ie = kde.wpa_ie;
3835 eapol_key_ie_len = kde.wpa_ie_len;
3836 }
3837 ft = sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt);
3838 if (!sm->wpa_ie ||
3839 wpa_compare_rsn_ie(ft, sm->wpa_ie, sm->wpa_ie_len,
3840 eapol_key_ie, eapol_key_ie_len)) {
3841 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
3842 "WPA IE from (Re)AssocReq did not match with msg 2/4");
3843 if (sm->wpa_ie) {
3844 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
3845 sm->wpa_ie, sm->wpa_ie_len);
3846 }
3847 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
3848 eapol_key_ie, eapol_key_ie_len);
3849 /* MLME-DEAUTHENTICATE.request */
3850 wpa_sta_disconnect(wpa_auth, sm->addr,
3851 WLAN_REASON_PREV_AUTH_NOT_VALID);
3852 goto out;
3853 }
3854 if ((!sm->rsnxe && kde.rsnxe) ||
3855 (sm->rsnxe && !kde.rsnxe) ||
3856 (sm->rsnxe && kde.rsnxe &&
3857 (sm->rsnxe_len != kde.rsnxe_len ||
3858 os_memcmp(sm->rsnxe, kde.rsnxe, sm->rsnxe_len) != 0))) {
3859 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
3860 "RSNXE from (Re)AssocReq did not match the one in EAPOL-Key msg 2/4");
3861 wpa_hexdump(MSG_DEBUG, "RSNXE in AssocReq",
3862 sm->rsnxe, sm->rsnxe_len);
3863 wpa_hexdump(MSG_DEBUG, "RSNXE in EAPOL-Key msg 2/4",
3864 kde.rsnxe, kde.rsnxe_len);
3865 /* MLME-DEAUTHENTICATE.request */
3866 wpa_sta_disconnect(wpa_auth, sm->addr,
3867 WLAN_REASON_PREV_AUTH_NOT_VALID);
3868 goto out;
3869 }
3870 #ifdef CONFIG_OCV
3871 if (wpa_auth_uses_ocv(sm)) {
3872 struct wpa_channel_info ci;
3873 int tx_chanwidth;
3874 int tx_seg1_idx;
3875 enum oci_verify_result res;
3876
3877 if (wpa_channel_info(wpa_auth, &ci) != 0) {
3878 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
3879 LOGGER_INFO,
3880 "Failed to get channel info to validate received OCI in EAPOL-Key 2/4");
3881 goto out;
3882 }
3883
3884 if (get_sta_tx_parameters(sm,
3885 channel_width_to_int(ci.chanwidth),
3886 ci.seg1_idx, &tx_chanwidth,
3887 &tx_seg1_idx) < 0)
3888 goto out;
3889
3890 res = ocv_verify_tx_params(kde.oci, kde.oci_len, &ci,
3891 tx_chanwidth, tx_seg1_idx);
3892 if (wpa_auth_uses_ocv(sm) == 2 && res == OCI_NOT_FOUND) {
3893 /* Work around misbehaving STAs */
3894 wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
3895 LOGGER_INFO,
3896 "Disable OCV with a STA that does not send OCI");
3897 wpa_auth_set_ocv(sm, 0);
3898 } else if (res != OCI_SUCCESS) {
3899 wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
3900 LOGGER_INFO,
3901 "OCV failed: %s", ocv_errorstr);
3902 if (wpa_auth->conf.msg_ctx)
3903 wpa_msg(wpa_auth->conf.msg_ctx, MSG_INFO,
3904 OCV_FAILURE "addr=" MACSTR
3905 " frame=eapol-key-m2 error=%s",
3906 MAC2STR(wpa_auth_get_spa(sm)),
3907 ocv_errorstr);
3908 goto out;
3909 }
3910 }
3911 #endif /* CONFIG_OCV */
3912 #ifdef CONFIG_IEEE80211R_AP
3913 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
3914 wpa_sta_disconnect(wpa_auth, sm->addr,
3915 WLAN_REASON_PREV_AUTH_NOT_VALID);
3916 goto out;
3917 }
3918 #endif /* CONFIG_IEEE80211R_AP */
3919
3920 /* Verify RSN Selection element for RSN overriding */
3921 if ((wpa_auth->conf.rsn_override_key_mgmt ||
3922 wpa_auth->conf.rsn_override_key_mgmt_2) &&
3923 ((rsn_is_snonce_cookie(sm->SNonce) && !kde.rsn_selection) ||
3924 (!rsn_is_snonce_cookie(sm->SNonce) && kde.rsn_selection) ||
3925 (sm->rsn_selection && !kde.rsn_selection) ||
3926 (!sm->rsn_selection && kde.rsn_selection) ||
3927 (sm->rsn_selection && kde.rsn_selection &&
3928 (sm->rsn_selection_len != kde.rsn_selection_len ||
3929 os_memcmp(sm->rsn_selection, kde.rsn_selection,
3930 sm->rsn_selection_len) != 0)))) {
3931 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
3932 "RSN Selection element from (Re)AssocReq did not match the one in EAPOL-Key msg 2/4");
3933 wpa_printf(MSG_DEBUG,
3934 "SNonce cookie for RSN overriding %sused",
3935 rsn_is_snonce_cookie(sm->SNonce) ? "" : "not ");
3936 wpa_hexdump(MSG_DEBUG, "RSN Selection in AssocReq",
3937 sm->rsn_selection, sm->rsn_selection_len);
3938 wpa_hexdump(MSG_DEBUG, "RSN Selection in EAPOL-Key msg 2/4",
3939 kde.rsn_selection, kde.rsn_selection_len);
3940 /* MLME-DEAUTHENTICATE.request */
3941 wpa_sta_disconnect(wpa_auth, sm->addr,
3942 WLAN_REASON_PREV_AUTH_NOT_VALID);
3943 goto out;
3944
3945 }
3946
3947 #ifdef CONFIG_P2P
3948 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
3949 wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
3950 int idx;
3951 wpa_printf(MSG_DEBUG,
3952 "P2P: IP address requested in EAPOL-Key exchange");
3953 idx = bitfield_get_first_zero(wpa_auth->ip_pool);
3954 if (idx >= 0) {
3955 u32 start = WPA_GET_BE32(wpa_auth->conf.ip_addr_start);
3956 bitfield_set(wpa_auth->ip_pool, idx);
3957 sm->ip_addr_bit = idx;
3958 WPA_PUT_BE32(sm->ip_addr, start + idx);
3959 wpa_printf(MSG_DEBUG,
3960 "P2P: Assigned IP address %u.%u.%u.%u to "
3961 MACSTR " (bit %u)",
3962 sm->ip_addr[0], sm->ip_addr[1],
3963 sm->ip_addr[2], sm->ip_addr[3],
3964 MAC2STR(wpa_auth_get_spa(sm)),
3965 sm->ip_addr_bit);
3966 }
3967 }
3968 #endif /* CONFIG_P2P */
3969
3970 #ifdef CONFIG_DPP2
3971 if (DPP_VERSION > 1 && kde.dpp_kde) {
3972 wpa_printf(MSG_DEBUG,
3973 "DPP: peer Protocol Version %u Flags 0x%x",
3974 kde.dpp_kde[0], kde.dpp_kde[1]);
3975 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP &&
3976 wpa_auth->conf.dpp_pfs != 2 &&
3977 (kde.dpp_kde[1] & DPP_KDE_PFS_ALLOWED) &&
3978 !sm->dpp_z) {
3979 wpa_printf(MSG_INFO,
3980 "DPP: Peer indicated it supports PFS and local configuration allows this, but PFS was not negotiated for the association");
3981 wpa_sta_disconnect(wpa_auth, sm->addr,
3982 WLAN_REASON_PREV_AUTH_NOT_VALID);
3983 goto out;
3984 }
3985 }
3986 #endif /* CONFIG_DPP2 */
3987
3988 if (wpa_auth_validate_ml_kdes_m2(sm, &kde) < 0) {
3989 wpa_sta_disconnect(wpa_auth, sm->addr,
3990 WLAN_REASON_PREV_AUTH_NOT_VALID);
3991 return;
3992 }
3993
3994 if (vlan_id && wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
3995 wpa_auth_update_vlan(wpa_auth, sm->addr, vlan_id) < 0) {
3996 wpa_sta_disconnect(wpa_auth, sm->addr,
3997 WLAN_REASON_PREV_AUTH_NOT_VALID);
3998 goto out;
3999 }
4000
4001 sm->pending_1_of_4_timeout = 0;
4002 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
4003
4004 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) && sm->PMK != pmk) {
4005 /* PSK may have changed from the previous choice, so update
4006 * state machine data based on whatever PSK was selected here.
4007 */
4008 os_memcpy(sm->PMK, pmk, PMK_LEN);
4009 sm->pmk_len = PMK_LEN;
4010 }
4011
4012 sm->MICVerified = true;
4013
4014 #ifdef CONFIG_IEEE80211R_AP
4015 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt) && !sm->ft_completed) {
4016 wpa_printf(MSG_DEBUG, "FT: Store PMK-R0/PMK-R1");
4017 wpa_auth_ft_store_keys(sm, pmk_r0, pmk_r1, pmk_r0_name,
4018 key_len);
4019 }
4020 #endif /* CONFIG_IEEE80211R_AP */
4021
4022 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
4023 forced_memzero(&PTK, sizeof(PTK));
4024 sm->PTK_valid = true;
4025 out:
4026 forced_memzero(pmk_r0, sizeof(pmk_r0));
4027 forced_memzero(pmk_r1, sizeof(pmk_r1));
4028 bin_clear_free(key_data_buf, key_data_buf_len);
4029 }
4030
4031
4032 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
4033 {
4034 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
4035 sm->TimeoutCtr = 0;
4036 }
4037
4038
4039 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
4040 {
4041 size_t len = 0;
4042 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
4043
4044 if (sm->mgmt_frame_prot) {
4045 len += 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN;
4046 len += wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
4047 }
4048
4049 if (wpa_auth->conf.tx_bss_auth)
4050 wpa_auth = wpa_auth->conf.tx_bss_auth;
4051 if (sm->mgmt_frame_prot && sm->wpa_auth->conf.beacon_prot) {
4052 len += 2 + RSN_SELECTOR_LEN + WPA_BIGTK_KDE_PREFIX_LEN;
4053 len += wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
4054 }
4055
4056 return len;
4057 }
4058
4059
4060 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
4061 {
4062 struct wpa_igtk_kde igtk;
4063 struct wpa_bigtk_kde bigtk;
4064 struct wpa_group *gsm = sm->group;
4065 u8 rsc[WPA_KEY_RSC_LEN];
4066 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
4067 struct wpa_auth_config *conf = &wpa_auth->conf;
4068 size_t len = wpa_cipher_key_len(conf->group_mgmt_cipher);
4069
4070 if (!sm->mgmt_frame_prot)
4071 return pos;
4072
4073 #ifdef CONFIG_IEEE80211BE
4074 if (sm->mld_assoc_link_id >= 0)
4075 return pos; /* Use per-link MLO KDEs instead */
4076 #endif /* CONFIG_IEEE80211BE */
4077
4078 igtk.keyid[0] = gsm->GN_igtk;
4079 igtk.keyid[1] = 0;
4080 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
4081 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
4082 os_memset(igtk.pn, 0, sizeof(igtk.pn));
4083 else
4084 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
4085 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
4086 if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
4087 /*
4088 * Provide unique random IGTK to each STA to prevent use of
4089 * IGTK in the BSS.
4090 */
4091 if (random_get_bytes(igtk.igtk, len) < 0)
4092 return pos;
4093 }
4094 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
4095 (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
4096 NULL, 0);
4097 forced_memzero(&igtk, sizeof(igtk));
4098
4099 if (wpa_auth->conf.tx_bss_auth) {
4100 wpa_auth = wpa_auth->conf.tx_bss_auth;
4101 conf = &wpa_auth->conf;
4102 len = wpa_cipher_key_len(conf->group_mgmt_cipher);
4103 gsm = wpa_auth->group;
4104 }
4105
4106 if (!sm->wpa_auth->conf.beacon_prot)
4107 return pos;
4108
4109 bigtk.keyid[0] = gsm->GN_bigtk;
4110 bigtk.keyid[1] = 0;
4111 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
4112 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_bigtk, rsc) < 0)
4113 os_memset(bigtk.pn, 0, sizeof(bigtk.pn));
4114 else
4115 os_memcpy(bigtk.pn, rsc, sizeof(bigtk.pn));
4116 os_memcpy(bigtk.bigtk, gsm->BIGTK[gsm->GN_bigtk - 6], len);
4117 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
4118 /*
4119 * Provide unique random BIGTK to each OSEN STA to prevent use
4120 * of BIGTK in the BSS.
4121 */
4122 if (random_get_bytes(bigtk.bigtk, len) < 0)
4123 return pos;
4124 }
4125 pos = wpa_add_kde(pos, RSN_KEY_DATA_BIGTK,
4126 (const u8 *) &bigtk, WPA_BIGTK_KDE_PREFIX_LEN + len,
4127 NULL, 0);
4128 forced_memzero(&bigtk, sizeof(bigtk));
4129
4130 return pos;
4131 }
4132
4133
4134 static int ocv_oci_len(struct wpa_state_machine *sm)
4135 {
4136 #ifdef CONFIG_OCV
4137 if (wpa_auth_uses_ocv(sm))
4138 return OCV_OCI_KDE_LEN;
4139 #endif /* CONFIG_OCV */
4140 return 0;
4141 }
4142
4143
4144 static int ocv_oci_add(struct wpa_state_machine *sm, u8 **argpos,
4145 unsigned int freq)
4146 {
4147 #ifdef CONFIG_OCV
4148 struct wpa_channel_info ci;
4149
4150 if (!wpa_auth_uses_ocv(sm))
4151 return 0;
4152
4153 if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
4154 wpa_printf(MSG_WARNING,
4155 "Failed to get channel info for OCI element");
4156 return -1;
4157 }
4158 #ifdef CONFIG_TESTING_OPTIONS
4159 if (freq) {
4160 wpa_printf(MSG_INFO,
4161 "TEST: Override OCI KDE frequency %d -> %u MHz",
4162 ci.frequency, freq);
4163 ci.frequency = freq;
4164 }
4165 #endif /* CONFIG_TESTING_OPTIONS */
4166
4167 return ocv_insert_oci_kde(&ci, argpos);
4168 #else /* CONFIG_OCV */
4169 return 0;
4170 #endif /* CONFIG_OCV */
4171 }
4172
4173
4174 #ifdef CONFIG_TESTING_OPTIONS
4175 static u8 * replace_ie(const char *name, const u8 *old_buf, size_t *len, u8 eid,
4176 const u8 *ie, size_t ie_len)
4177 {
4178 const u8 *elem;
4179 u8 *buf;
4180
4181 wpa_printf(MSG_DEBUG, "TESTING: %s EAPOL override", name);
4182 wpa_hexdump(MSG_DEBUG, "TESTING: wpa_ie before override",
4183 old_buf, *len);
4184 buf = os_malloc(*len + ie_len);
4185 if (!buf)
4186 return NULL;
4187 os_memcpy(buf, old_buf, *len);
4188 elem = get_ie(buf, *len, eid);
4189 if (elem) {
4190 u8 elem_len = 2 + elem[1];
4191
4192 os_memmove((void *) elem, elem + elem_len,
4193 *len - (elem - buf) - elem_len);
4194 *len -= elem_len;
4195 }
4196 os_memcpy(buf + *len, ie, ie_len);
4197 *len += ie_len;
4198 wpa_hexdump(MSG_DEBUG, "TESTING: wpa_ie after EAPOL override",
4199 buf, *len);
4200
4201 return buf;
4202 }
4203 #endif /* CONFIG_TESTING_OPTIONS */
4204
4205
4206 #ifdef CONFIG_IEEE80211BE
4207
4208 void wpa_auth_ml_get_key_info(struct wpa_authenticator *a,
4209 struct wpa_auth_ml_link_key_info *info,
4210 bool mgmt_frame_prot, bool beacon_prot)
4211 {
4212 struct wpa_group *gsm = a->group;
4213 u8 rsc[WPA_KEY_RSC_LEN];
4214
4215 wpa_printf(MSG_DEBUG,
4216 "MLD: Get group key info: link_id=%u, IGTK=%u, BIGTK=%u",
4217 info->link_id, mgmt_frame_prot, beacon_prot);
4218
4219 info->gtkidx = gsm->GN & 0x03;
4220 info->gtk = gsm->GTK[gsm->GN - 1];
4221 info->gtk_len = gsm->GTK_len;
4222
4223 if (wpa_auth_get_seqnum(a, NULL, gsm->GN, rsc) < 0)
4224 os_memset(info->pn, 0, sizeof(info->pn));
4225 else
4226 os_memcpy(info->pn, rsc, sizeof(info->pn));
4227
4228 if (!mgmt_frame_prot)
4229 return;
4230
4231 info->igtkidx = gsm->GN_igtk;
4232 info->igtk = gsm->IGTK[gsm->GN_igtk - 4];
4233 info->igtk_len = wpa_cipher_key_len(a->conf.group_mgmt_cipher);
4234
4235 if (wpa_auth_get_seqnum(a, NULL, gsm->GN_igtk, rsc) < 0)
4236 os_memset(info->ipn, 0, sizeof(info->ipn));
4237 else
4238 os_memcpy(info->ipn, rsc, sizeof(info->ipn));
4239
4240 if (!beacon_prot)
4241 return;
4242
4243 if (a->conf.tx_bss_auth) {
4244 a = a->conf.tx_bss_auth;
4245 gsm = a->group;
4246 }
4247
4248 info->bigtkidx = gsm->GN_bigtk;
4249 info->bigtk = gsm->BIGTK[gsm->GN_bigtk - 6];
4250
4251 if (wpa_auth_get_seqnum(a, NULL, gsm->GN_bigtk, rsc) < 0)
4252 os_memset(info->bipn, 0, sizeof(info->bipn));
4253 else
4254 os_memcpy(info->bipn, rsc, sizeof(info->bipn));
4255 }
4256
4257
4258 static void wpa_auth_get_ml_key_info(struct wpa_authenticator *wpa_auth,
4259 struct wpa_auth_ml_key_info *info)
4260 {
4261 if (!wpa_auth->cb->get_ml_key_info)
4262 return;
4263
4264 wpa_auth->cb->get_ml_key_info(wpa_auth->cb_ctx, info);
4265 }
4266
4267
4268 static size_t wpa_auth_ml_group_kdes_len(struct wpa_state_machine *sm)
4269 {
4270 struct wpa_authenticator *wpa_auth;
4271 size_t kde_len = 0;
4272 int link_id;
4273
4274 if (sm->mld_assoc_link_id < 0)
4275 return 0;
4276
4277 for (link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
4278 if (!sm->mld_links[link_id].valid)
4279 continue;
4280
4281 wpa_auth = sm->mld_links[link_id].wpa_auth;
4282 if (!wpa_auth || !wpa_auth->group)
4283 continue;
4284
4285 /* MLO GTK KDE
4286 * Header + Key ID + Tx + LinkID + PN + GTK */
4287 kde_len += KDE_HDR_LEN + 1 + RSN_PN_LEN;
4288 kde_len += wpa_auth->group->GTK_len;
4289
4290 if (!sm->mgmt_frame_prot)
4291 continue;
4292
4293 if (wpa_auth->conf.tx_bss_auth)
4294 wpa_auth = wpa_auth->conf.tx_bss_auth;
4295
4296 /* MLO IGTK KDE
4297 * Header + Key ID + IPN + LinkID + IGTK */
4298 kde_len += KDE_HDR_LEN + WPA_IGTK_KDE_PREFIX_LEN + 1;
4299 kde_len += wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
4300
4301 if (!wpa_auth->conf.beacon_prot)
4302 continue;
4303
4304 /* MLO BIGTK KDE
4305 * Header + Key ID + BIPN + LinkID + BIGTK */
4306 kde_len += KDE_HDR_LEN + WPA_BIGTK_KDE_PREFIX_LEN + 1;
4307 kde_len += wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
4308 }
4309
4310 wpa_printf(MSG_DEBUG, "MLO Group KDEs len = %zu", kde_len);
4311
4312 return kde_len;
4313 }
4314
4315
4316 static u8 * wpa_auth_ml_group_kdes(struct wpa_state_machine *sm, u8 *pos)
4317 {
4318 struct wpa_auth_ml_key_info ml_key_info;
4319 unsigned int i, link_id;
4320 u8 *start = pos;
4321
4322 /* First fetch the key information from all the authenticators */
4323 os_memset(&ml_key_info, 0, sizeof(ml_key_info));
4324 ml_key_info.n_mld_links = sm->n_mld_affiliated_links + 1;
4325
4326 /*
4327 * Assume that management frame protection and beacon protection are the
4328 * same on all links.
4329 */
4330 ml_key_info.mgmt_frame_prot = sm->mgmt_frame_prot;
4331 ml_key_info.beacon_prot = sm->wpa_auth->conf.beacon_prot;
4332
4333 for (i = 0, link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
4334 if (!sm->mld_links[link_id].valid)
4335 continue;
4336
4337 ml_key_info.links[i++].link_id = link_id;
4338 }
4339
4340 wpa_auth_get_ml_key_info(sm->wpa_auth, &ml_key_info);
4341
4342 /* Add MLO GTK KDEs */
4343 for (i = 0, link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
4344 if (!sm->mld_links[link_id].valid ||
4345 !ml_key_info.links[i].gtk_len)
4346 continue;
4347
4348 wpa_printf(MSG_DEBUG, "RSN: MLO GTK: link=%u", link_id);
4349 wpa_hexdump_key(MSG_DEBUG, "RSN: MLO GTK",
4350 ml_key_info.links[i].gtk,
4351 ml_key_info.links[i].gtk_len);
4352
4353 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
4354 *pos++ = RSN_SELECTOR_LEN + 1 + 6 +
4355 ml_key_info.links[i].gtk_len;
4356
4357 RSN_SELECTOR_PUT(pos, RSN_KEY_DATA_MLO_GTK);
4358 pos += RSN_SELECTOR_LEN;
4359
4360 *pos++ = (ml_key_info.links[i].gtkidx & 0x3) | (link_id << 4);
4361
4362 os_memcpy(pos, ml_key_info.links[i].pn, 6);
4363 pos += 6;
4364
4365 os_memcpy(pos, ml_key_info.links[i].gtk,
4366 ml_key_info.links[i].gtk_len);
4367 pos += ml_key_info.links[i].gtk_len;
4368
4369 i++;
4370 }
4371
4372 if (!sm->mgmt_frame_prot) {
4373 wpa_printf(MSG_DEBUG, "RSN: MLO Group KDE len = %ld",
4374 pos - start);
4375 return pos;
4376 }
4377
4378 /* Add MLO IGTK KDEs */
4379 for (i = 0, link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
4380 if (!sm->mld_links[link_id].valid ||
4381 !ml_key_info.links[i].igtk_len)
4382 continue;
4383
4384 wpa_printf(MSG_DEBUG, "RSN: MLO IGTK: link=%u", link_id);
4385 wpa_hexdump_key(MSG_DEBUG, "RSN: MLO IGTK",
4386 ml_key_info.links[i].igtk,
4387 ml_key_info.links[i].igtk_len);
4388
4389 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
4390 *pos++ = RSN_SELECTOR_LEN + 2 + 1 +
4391 sizeof(ml_key_info.links[i].ipn) +
4392 ml_key_info.links[i].igtk_len;
4393
4394 RSN_SELECTOR_PUT(pos, RSN_KEY_DATA_MLO_IGTK);
4395 pos += RSN_SELECTOR_LEN;
4396
4397 /* Add the Key ID */
4398 *pos++ = ml_key_info.links[i].igtkidx;
4399 *pos++ = 0;
4400
4401 /* Add the IPN */
4402 os_memcpy(pos, ml_key_info.links[i].ipn,
4403 sizeof(ml_key_info.links[i].ipn));
4404 pos += sizeof(ml_key_info.links[i].ipn);
4405
4406 *pos++ = ml_key_info.links[i].link_id << 4;
4407
4408 os_memcpy(pos, ml_key_info.links[i].igtk,
4409 ml_key_info.links[i].igtk_len);
4410 pos += ml_key_info.links[i].igtk_len;
4411
4412 i++;
4413 }
4414
4415 if (!sm->wpa_auth->conf.beacon_prot) {
4416 wpa_printf(MSG_DEBUG, "RSN: MLO Group KDE len = %ld",
4417 pos - start);
4418 return pos;
4419 }
4420
4421 /* Add MLO BIGTK KDEs */
4422 for (i = 0, link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
4423 if (!sm->mld_links[link_id].valid ||
4424 !ml_key_info.links[i].bigtk ||
4425 !ml_key_info.links[i].igtk_len)
4426 continue;
4427
4428 wpa_printf(MSG_DEBUG, "RSN: MLO BIGTK: link=%u", link_id);
4429 wpa_hexdump_key(MSG_DEBUG, "RSN: MLO BIGTK",
4430 ml_key_info.links[i].bigtk,
4431 ml_key_info.links[i].igtk_len);
4432
4433 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
4434 *pos++ = RSN_SELECTOR_LEN + 2 + 1 +
4435 sizeof(ml_key_info.links[i].bipn) +
4436 ml_key_info.links[i].igtk_len;
4437
4438 RSN_SELECTOR_PUT(pos, RSN_KEY_DATA_MLO_BIGTK);
4439 pos += RSN_SELECTOR_LEN;
4440
4441 /* Add the Key ID */
4442 *pos++ = ml_key_info.links[i].bigtkidx;
4443 *pos++ = 0;
4444
4445 /* Add the BIPN */
4446 os_memcpy(pos, ml_key_info.links[i].bipn,
4447 sizeof(ml_key_info.links[i].bipn));
4448 pos += sizeof(ml_key_info.links[i].bipn);
4449
4450 *pos++ = ml_key_info.links[i].link_id << 4;
4451
4452 os_memcpy(pos, ml_key_info.links[i].bigtk,
4453 ml_key_info.links[i].igtk_len);
4454 pos += ml_key_info.links[i].igtk_len;
4455
4456 i++;
4457 }
4458
4459 wpa_printf(MSG_DEBUG, "RSN: MLO Group KDE len = %ld", pos - start);
4460 return pos;
4461 }
4462
4463 #endif /* CONFIG_IEEE80211BE */
4464
4465
4466 static size_t wpa_auth_ml_kdes_len(struct wpa_state_machine *sm)
4467 {
4468 size_t kde_len = 0;
4469
4470 #ifdef CONFIG_IEEE80211BE
4471 unsigned int link_id;
4472
4473 if (sm->mld_assoc_link_id < 0)
4474 return 0;
4475
4476 /* For the MAC Address KDE */
4477 kde_len = 2 + RSN_SELECTOR_LEN + ETH_ALEN;
4478
4479 /* MLO Link KDE and RSN Override Link KDE for each link */
4480 for (link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
4481 struct wpa_authenticator *wpa_auth;
4482 const u8 *ie;
4483
4484 wpa_auth = wpa_get_link_auth(sm->wpa_auth, link_id);
4485 if (!wpa_auth)
4486 continue;
4487
4488 /* MLO Link KDE */
4489 kde_len += 2 + RSN_SELECTOR_LEN + 1 + ETH_ALEN;
4490
4491 ie = get_ie(wpa_auth->wpa_ie, wpa_auth->wpa_ie_len,
4492 WLAN_EID_RSN);
4493 if (ie)
4494 kde_len += 2 + ie[1];
4495
4496 ie = get_ie(wpa_auth->wpa_ie, wpa_auth->wpa_ie_len,
4497 WLAN_EID_RSNX);
4498 if (ie)
4499 kde_len += 2 + ie[1];
4500
4501 if (!rsn_is_snonce_cookie(sm->SNonce))
4502 continue;
4503
4504 /* RSN Override Link KDE */
4505 kde_len += 2 + RSN_SELECTOR_LEN + 1;
4506
4507 ie = get_vendor_ie(wpa_auth->wpa_ie, wpa_auth->wpa_ie_len,
4508 RSNE_OVERRIDE_IE_VENDOR_TYPE);
4509 if (ie)
4510 kde_len += 2 + ie[1];
4511
4512 ie = get_vendor_ie(wpa_auth->wpa_ie, wpa_auth->wpa_ie_len,
4513 RSNE_OVERRIDE_2_IE_VENDOR_TYPE);
4514 if (ie)
4515 kde_len += 2 + ie[1];
4516
4517 ie = get_vendor_ie(wpa_auth->wpa_ie, wpa_auth->wpa_ie_len,
4518 RSNXE_OVERRIDE_IE_VENDOR_TYPE);
4519 if (ie)
4520 kde_len += 2 + ie[1];
4521 }
4522
4523 kde_len += wpa_auth_ml_group_kdes_len(sm);
4524 #endif /* CONFIG_IEEE80211BE */
4525
4526 return kde_len;
4527 }
4528
4529
4530 static u8 * wpa_auth_ml_kdes(struct wpa_state_machine *sm, u8 *pos)
4531 {
4532 #ifdef CONFIG_IEEE80211BE
4533 u8 link_id;
4534 u8 *start = pos;
4535
4536 if (sm->mld_assoc_link_id < 0)
4537 return pos;
4538
4539 wpa_printf(MSG_DEBUG, "RSN: MLD: Adding MAC Address KDE");
4540 pos = wpa_add_kde(pos, RSN_KEY_DATA_MAC_ADDR,
4541 sm->wpa_auth->mld_addr, ETH_ALEN, NULL, 0);
4542
4543 for (link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
4544 struct wpa_authenticator *wpa_auth;
4545 const u8 *rsne, *rsnxe, *rsnoe, *rsno2e, *rsnxoe;
4546 size_t rsne_len, rsnxe_len, rsnoe_len, rsno2e_len, rsnxoe_len;
4547 size_t kde_len;
4548
4549 wpa_auth = wpa_get_link_auth(sm->wpa_auth, link_id);
4550 if (!wpa_auth)
4551 continue;
4552
4553 rsne = get_ie(wpa_auth->wpa_ie, wpa_auth->wpa_ie_len,
4554 WLAN_EID_RSN);
4555 rsne_len = rsne ? 2 + rsne[1] : 0;
4556
4557 rsnxe = get_ie(wpa_auth->wpa_ie, wpa_auth->wpa_ie_len,
4558 WLAN_EID_RSNX);
4559 rsnxe_len = rsnxe ? 2 + rsnxe[1] : 0;
4560
4561 wpa_printf(MSG_DEBUG,
4562 "RSN: MLO Link: link=%u, len=%zu", link_id,
4563 RSN_SELECTOR_LEN + 1 + ETH_ALEN +
4564 rsne_len + rsnxe_len);
4565
4566 /* MLO Link KDE */
4567 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
4568 *pos++ = RSN_SELECTOR_LEN + 1 + ETH_ALEN +
4569 rsne_len + rsnxe_len;
4570
4571 RSN_SELECTOR_PUT(pos, RSN_KEY_DATA_MLO_LINK);
4572 pos += RSN_SELECTOR_LEN;
4573
4574 /* Add the Link Information */
4575 *pos = link_id;
4576 if (rsne_len)
4577 *pos |= RSN_MLO_LINK_KDE_LI_RSNE_INFO;
4578 if (rsnxe_len)
4579 *pos |= RSN_MLO_LINK_KDE_LI_RSNXE_INFO;
4580
4581 pos++;
4582 os_memcpy(pos, wpa_auth->addr, ETH_ALEN);
4583 pos += ETH_ALEN;
4584
4585 if (rsne_len) {
4586 os_memcpy(pos, rsne, rsne_len);
4587 pos += rsne_len;
4588 }
4589
4590 if (rsnxe_len) {
4591 os_memcpy(pos, rsnxe, rsnxe_len);
4592 pos += rsnxe_len;
4593 }
4594
4595 if (!rsn_is_snonce_cookie(sm->SNonce))
4596 continue;
4597
4598 rsnoe = get_vendor_ie(wpa_auth->wpa_ie, wpa_auth->wpa_ie_len,
4599 RSNE_OVERRIDE_IE_VENDOR_TYPE);
4600 rsnoe_len = rsnoe ? 2 + rsnoe[1] : 0;
4601
4602 rsno2e = get_vendor_ie(wpa_auth->wpa_ie, wpa_auth->wpa_ie_len,
4603 RSNE_OVERRIDE_2_IE_VENDOR_TYPE);
4604 rsno2e_len = rsno2e ? 2 + rsno2e[1] : 0;
4605
4606 rsnxoe = get_vendor_ie(wpa_auth->wpa_ie, wpa_auth->wpa_ie_len,
4607 RSNXE_OVERRIDE_IE_VENDOR_TYPE);
4608 rsnxoe_len = rsnxoe ? 2 + rsnxoe[1] : 0;
4609
4610 wpa_printf(MSG_DEBUG,
4611 "RSN: RSN Override Link KDE: link=%u, len=%zu",
4612 link_id, RSN_SELECTOR_LEN + rsnoe_len + rsno2e_len +
4613 rsnxoe_len);
4614
4615 /* RSN Override Link KDE */
4616 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
4617 kde_len = RSN_SELECTOR_LEN + 1 + rsnoe_len + rsno2e_len +
4618 rsnxoe_len;
4619 if (kde_len > 255) {
4620 wpa_printf(MSG_ERROR,
4621 "RSN: RSNOE/RSNO2E/RSNXOE too long (KDE length %zu) to fit in RSN Override Link KDE for link %u",
4622 kde_len, link_id);
4623 return NULL;
4624 }
4625 *pos++ = kde_len;
4626
4627 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_RSN_OVERRIDE_LINK);
4628 pos += RSN_SELECTOR_LEN;
4629
4630 *pos++ = link_id;
4631
4632 if (rsnoe_len) {
4633 os_memcpy(pos, rsnoe, rsnoe_len);
4634 pos += rsnoe_len;
4635 }
4636
4637 if (rsno2e_len) {
4638 os_memcpy(pos, rsno2e, rsno2e_len);
4639 pos += rsno2e_len;
4640 }
4641
4642 if (rsnxoe_len) {
4643 os_memcpy(pos, rsnxoe, rsnxoe_len);
4644 pos += rsnxoe_len;
4645 }
4646 }
4647
4648 wpa_printf(MSG_DEBUG,
4649 "RSN: MLO Link KDEs and RSN Override Link KDEs len = %ld",
4650 pos - start);
4651 pos = wpa_auth_ml_group_kdes(sm, pos);
4652 #endif /* CONFIG_IEEE80211BE */
4653
4654 return pos;
4655 }
4656
4657
4658 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
4659 {
4660 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde = NULL, *pos, stub_gtk[32];
4661 size_t gtk_len, kde_len = 0, wpa_ie_len;
4662 struct wpa_group *gsm = sm->group;
4663 u8 *wpa_ie;
4664 int secure, gtkidx, encr = 0;
4665 u8 *wpa_ie_buf = NULL, *wpa_ie_buf2 = NULL, *wpa_ie_buf3 = NULL;
4666 u8 hdr[2];
4667 struct wpa_auth_config *conf = &sm->wpa_auth->conf;
4668 #ifdef CONFIG_IEEE80211BE
4669 bool is_mld = sm->mld_assoc_link_id >= 0;
4670 #else /* CONFIG_IEEE80211BE */
4671 bool is_mld = false;
4672 #endif /* CONFIG_IEEE80211BE */
4673
4674 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
4675 sm->TimeoutEvt = false;
4676
4677 sm->TimeoutCtr++;
4678 if (conf->wpa_disable_eapol_key_retries && sm->TimeoutCtr > 1) {
4679 /* Do not allow retransmission of EAPOL-Key msg 3/4 */
4680 return;
4681 }
4682 if (sm->TimeoutCtr > conf->wpa_pairwise_update_count) {
4683 /* No point in sending the EAPOL-Key - we will disconnect
4684 * immediately following this. */
4685 return;
4686 }
4687
4688 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
4689 GTK[GN], IGTK, [BIGTK], [FTIE], [TIE * 2])
4690 */
4691 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
4692 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
4693 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
4694 wpa_ie = sm->wpa_auth->wpa_ie;
4695 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
4696 if (sm->wpa == WPA_VERSION_WPA && (conf->wpa & WPA_PROTO_RSN) &&
4697 wpa_ie_len > wpa_ie[1] + 2U && wpa_ie[0] == WLAN_EID_RSN) {
4698 /* WPA-only STA, remove RSN IE and possible MDIE */
4699 wpa_ie = wpa_ie + wpa_ie[1] + 2;
4700 if (wpa_ie[0] == WLAN_EID_RSNX)
4701 wpa_ie = wpa_ie + wpa_ie[1] + 2;
4702 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
4703 wpa_ie = wpa_ie + wpa_ie[1] + 2;
4704 wpa_ie_len = wpa_ie[1] + 2;
4705 }
4706 if ((conf->rsn_override_key_mgmt || conf->rsn_override_key_mgmt_2) &&
4707 !rsn_is_snonce_cookie(sm->SNonce)) {
4708 u8 *ie;
4709 size_t ie_len;
4710 u32 ids[] = {
4711 RSNE_OVERRIDE_IE_VENDOR_TYPE,
4712 RSNE_OVERRIDE_2_IE_VENDOR_TYPE,
4713 RSNXE_OVERRIDE_IE_VENDOR_TYPE,
4714 0
4715 };
4716 int i;
4717
4718 wpa_printf(MSG_DEBUG,
4719 "RSN: Remove RSNE/RSNXE override elements");
4720 wpa_hexdump(MSG_DEBUG, "EAPOL-Key msg 3/4 IEs before edits",
4721 wpa_ie, wpa_ie_len);
4722 wpa_ie_buf3 = os_memdup(wpa_ie, wpa_ie_len);
4723 if (!wpa_ie_buf3)
4724 goto done;
4725 wpa_ie = wpa_ie_buf3;
4726
4727 for (i = 0; ids[i]; i++) {
4728 ie = (u8 *) get_vendor_ie(wpa_ie, wpa_ie_len, ids[i]);
4729 if (ie) {
4730 ie_len = 2 + ie[1];
4731 os_memmove(ie, ie + ie_len,
4732 wpa_ie_len - (ie + ie_len - wpa_ie));
4733 wpa_ie_len -= ie_len;
4734 }
4735 }
4736 wpa_hexdump(MSG_DEBUG, "EAPOL-Key msg 3/4 IEs after edits",
4737 wpa_ie, wpa_ie_len);
4738 }
4739 #ifdef CONFIG_TESTING_OPTIONS
4740 if (conf->rsne_override_eapol_set) {
4741 wpa_ie_buf2 = replace_ie(
4742 "RSNE", wpa_ie, &wpa_ie_len, WLAN_EID_RSN,
4743 conf->rsne_override_eapol,
4744 conf->rsne_override_eapol_len);
4745 if (!wpa_ie_buf2)
4746 goto done;
4747 wpa_ie = wpa_ie_buf2;
4748 }
4749 if (conf->rsnxe_override_eapol_set) {
4750 wpa_ie_buf = replace_ie(
4751 "RSNXE", wpa_ie, &wpa_ie_len, WLAN_EID_RSNX,
4752 conf->rsnxe_override_eapol,
4753 conf->rsnxe_override_eapol_len);
4754 if (!wpa_ie_buf)
4755 goto done;
4756 wpa_ie = wpa_ie_buf;
4757 }
4758 #endif /* CONFIG_TESTING_OPTIONS */
4759 wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
4760 "sending 3/4 msg of 4-Way Handshake");
4761 if (sm->wpa == WPA_VERSION_WPA2) {
4762 if (sm->use_ext_key_id && sm->TimeoutCtr == 1 &&
4763 wpa_auth_set_key(sm->wpa_auth, 0,
4764 wpa_cipher_to_alg(sm->pairwise),
4765 sm->addr,
4766 sm->keyidx_active, sm->PTK.tk,
4767 wpa_cipher_key_len(sm->pairwise),
4768 KEY_FLAG_PAIRWISE_RX)) {
4769 wpa_sta_disconnect(sm->wpa_auth, sm->addr,
4770 WLAN_REASON_PREV_AUTH_NOT_VALID);
4771 return;
4772 }
4773
4774 #ifdef CONFIG_PASN
4775 if (sm->wpa_auth->conf.secure_ltf &&
4776 ieee802_11_rsnx_capab(sm->rsnxe,
4777 WLAN_RSNX_CAPAB_SECURE_LTF) &&
4778 wpa_auth_set_ltf_keyseed(sm->wpa_auth, sm->addr,
4779 sm->PTK.ltf_keyseed,
4780 sm->PTK.ltf_keyseed_len)) {
4781 wpa_printf(MSG_ERROR,
4782 "WPA: Failed to set LTF keyseed to driver");
4783 wpa_sta_disconnect(sm->wpa_auth, sm->addr,
4784 WLAN_REASON_PREV_AUTH_NOT_VALID);
4785 return;
4786 }
4787 #endif /* CONFIG_PASN */
4788
4789 /* WPA2 send GTK in the 4-way handshake */
4790 secure = 1;
4791 gtk = gsm->GTK[gsm->GN - 1];
4792 gtk_len = gsm->GTK_len;
4793 if (conf->disable_gtk ||
4794 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
4795 /*
4796 * Provide unique random GTK to each STA to prevent use
4797 * of GTK in the BSS.
4798 */
4799 if (random_get_bytes(stub_gtk, gtk_len) < 0)
4800 goto done;
4801 gtk = stub_gtk;
4802 }
4803 gtkidx = gsm->GN;
4804 _rsc = rsc;
4805 encr = 1;
4806 } else {
4807 /* WPA does not include GTK in msg 3/4 */
4808 secure = 0;
4809 gtk = NULL;
4810 gtk_len = 0;
4811 gtkidx = 0;
4812 _rsc = NULL;
4813 if (sm->rx_eapol_key_secure) {
4814 /*
4815 * It looks like Windows 7 supplicant tries to use
4816 * Secure bit in msg 2/4 after having reported Michael
4817 * MIC failure and it then rejects the 4-way handshake
4818 * if msg 3/4 does not set Secure bit. Work around this
4819 * by setting the Secure bit here even in the case of
4820 * WPA if the supplicant used it first.
4821 */
4822 wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm),
4823 LOGGER_DEBUG,
4824 "STA used Secure bit in WPA msg 2/4 - set Secure for 3/4 as workaround");
4825 secure = 1;
4826 }
4827 }
4828
4829 kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
4830
4831 if (sm->use_ext_key_id)
4832 kde_len += 2 + RSN_SELECTOR_LEN + 2;
4833
4834 if (gtk)
4835 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
4836 #ifdef CONFIG_IEEE80211R_AP
4837 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
4838 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
4839 kde_len += 300; /* FTIE + 2 * TIE */
4840 }
4841 #endif /* CONFIG_IEEE80211R_AP */
4842 #ifdef CONFIG_P2P
4843 if (WPA_GET_BE32(sm->ip_addr) > 0)
4844 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
4845 #endif /* CONFIG_P2P */
4846
4847 if (conf->transition_disable)
4848 kde_len += 2 + RSN_SELECTOR_LEN + 1;
4849
4850 #ifdef CONFIG_DPP2
4851 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP)
4852 kde_len += 2 + RSN_SELECTOR_LEN + 2;
4853 #endif /* CONFIG_DPP2 */
4854
4855 kde_len += wpa_auth_ml_kdes_len(sm);
4856
4857 if (sm->ssid_protection)
4858 kde_len += 2 + conf->ssid_len;
4859
4860 #ifdef CONFIG_TESTING_OPTIONS
4861 if (conf->eapol_m3_elements)
4862 kde_len += wpabuf_len(conf->eapol_m3_elements);
4863 #endif /* CONFIG_TESTING_OPTIONS */
4864
4865 kde = os_malloc(kde_len);
4866 if (!kde)
4867 goto done;
4868
4869 pos = kde;
4870 if (!is_mld) {
4871 os_memcpy(pos, wpa_ie, wpa_ie_len);
4872 pos += wpa_ie_len;
4873 }
4874 #ifdef CONFIG_IEEE80211R_AP
4875 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
4876 int res;
4877 size_t elen;
4878
4879 elen = pos - kde;
4880 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name, true);
4881 if (res < 0) {
4882 wpa_printf(MSG_ERROR,
4883 "FT: Failed to insert PMKR1Name into RSN IE in EAPOL-Key data");
4884 goto done;
4885 }
4886 pos -= wpa_ie_len;
4887 pos += elen;
4888 }
4889 #endif /* CONFIG_IEEE80211R_AP */
4890 hdr[1] = 0;
4891
4892 if (sm->use_ext_key_id) {
4893 hdr[0] = sm->keyidx_active & 0x01;
4894 pos = wpa_add_kde(pos, RSN_KEY_DATA_KEYID, hdr, 2, NULL, 0);
4895 }
4896
4897 if (gtk && !is_mld) {
4898 hdr[0] = gtkidx & 0x03;
4899 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
4900 gtk, gtk_len);
4901 }
4902 pos = ieee80211w_kde_add(sm, pos);
4903 if (ocv_oci_add(sm, &pos, conf->oci_freq_override_eapol_m3) < 0)
4904 goto done;
4905
4906 #ifdef CONFIG_IEEE80211R_AP
4907 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
4908 int res;
4909
4910 if (sm->assoc_resp_ftie &&
4911 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
4912 os_memcpy(pos, sm->assoc_resp_ftie,
4913 2 + sm->assoc_resp_ftie[1]);
4914 res = 2 + sm->assoc_resp_ftie[1];
4915 } else {
4916 res = wpa_write_ftie(conf, sm->wpa_key_mgmt,
4917 sm->xxkey_len,
4918 conf->r0_key_holder,
4919 conf->r0_key_holder_len,
4920 NULL, NULL, pos,
4921 kde + kde_len - pos,
4922 NULL, 0, 0);
4923 }
4924 if (res < 0) {
4925 wpa_printf(MSG_ERROR,
4926 "FT: Failed to insert FTIE into EAPOL-Key Key Data");
4927 goto done;
4928 }
4929 pos += res;
4930
4931 /* TIE[ReassociationDeadline] (TU) */
4932 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
4933 *pos++ = 5;
4934 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
4935 WPA_PUT_LE32(pos, conf->reassociation_deadline);
4936 pos += 4;
4937
4938 /* TIE[KeyLifetime] (seconds) */
4939 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
4940 *pos++ = 5;
4941 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
4942 WPA_PUT_LE32(pos, conf->r0_key_lifetime);
4943 pos += 4;
4944 }
4945 #endif /* CONFIG_IEEE80211R_AP */
4946 #ifdef CONFIG_P2P
4947 if (WPA_GET_BE32(sm->ip_addr) > 0) {
4948 u8 addr[3 * 4];
4949 os_memcpy(addr, sm->ip_addr, 4);
4950 os_memcpy(addr + 4, conf->ip_addr_mask, 4);
4951 os_memcpy(addr + 8, conf->ip_addr_go, 4);
4952 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
4953 addr, sizeof(addr), NULL, 0);
4954 }
4955 #endif /* CONFIG_P2P */
4956
4957 if (conf->transition_disable)
4958 pos = wpa_add_kde(pos, WFA_KEY_DATA_TRANSITION_DISABLE,
4959 &conf->transition_disable, 1, NULL, 0);
4960
4961 #ifdef CONFIG_DPP2
4962 if (DPP_VERSION > 1 && sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP) {
4963 u8 payload[2];
4964
4965 payload[0] = DPP_VERSION; /* Protocol Version */
4966 payload[1] = 0; /* Flags */
4967 if (conf->dpp_pfs == 0)
4968 payload[1] |= DPP_KDE_PFS_ALLOWED;
4969 else if (conf->dpp_pfs == 1)
4970 payload[1] |= DPP_KDE_PFS_ALLOWED |
4971 DPP_KDE_PFS_REQUIRED;
4972 pos = wpa_add_kde(pos, WFA_KEY_DATA_DPP,
4973 payload, sizeof(payload), NULL, 0);
4974 }
4975 #endif /* CONFIG_DPP2 */
4976
4977 pos = wpa_auth_ml_kdes(sm, pos);
4978 if (!pos) {
4979 wpa_printf(MSG_ERROR, "RSN: Failed to add MLO KDEs");
4980 goto done;
4981 }
4982
4983 if (sm->ssid_protection) {
4984 *pos++ = WLAN_EID_SSID;
4985 *pos++ = conf->ssid_len;
4986 os_memcpy(pos, conf->ssid, conf->ssid_len);
4987 pos += conf->ssid_len;
4988 }
4989
4990 #ifdef CONFIG_TESTING_OPTIONS
4991 if (conf->eapol_m3_elements) {
4992 os_memcpy(pos, wpabuf_head(conf->eapol_m3_elements),
4993 wpabuf_len(conf->eapol_m3_elements));
4994 pos += wpabuf_len(conf->eapol_m3_elements);
4995 }
4996
4997 if (conf->eapol_m3_no_encrypt)
4998 encr = 0;
4999 #endif /* CONFIG_TESTING_OPTIONS */
5000
5001 wpa_send_eapol(sm->wpa_auth, sm,
5002 (secure ? WPA_KEY_INFO_SECURE : 0) |
5003 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
5004 WPA_KEY_INFO_MIC : 0) |
5005 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
5006 WPA_KEY_INFO_KEY_TYPE,
5007 _rsc, sm->ANonce, kde, pos - kde, 0, encr);
5008 done:
5009 bin_clear_free(kde, kde_len);
5010 os_free(wpa_ie_buf);
5011 os_free(wpa_ie_buf2);
5012 os_free(wpa_ie_buf3);
5013 }
5014
5015
5016 static int wpa_auth_validate_ml_kdes_m4(struct wpa_state_machine *sm)
5017 {
5018 #ifdef CONFIG_IEEE80211BE
5019 const struct ieee802_1x_hdr *hdr;
5020 const struct wpa_eapol_key *key;
5021 struct wpa_eapol_ie_parse kde;
5022 const u8 *key_data, *mic;
5023 u16 key_data_length;
5024 size_t mic_len;
5025
5026 if (sm->mld_assoc_link_id < 0)
5027 return 0;
5028
5029 /*
5030 * Note: last_rx_eapol_key length fields have already been validated in
5031 * wpa_receive().
5032 */
5033 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
5034
5035 hdr = (const struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
5036 key = (const struct wpa_eapol_key *) (hdr + 1);
5037 mic = (const u8 *) (key + 1);
5038 key_data = mic + mic_len + 2;
5039 key_data_length = WPA_GET_BE16(mic + mic_len);
5040 if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
5041 sizeof(*key) - mic_len - 2)
5042 return -1;
5043
5044 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
5045 wpa_auth_vlogger(sm->wpa_auth, wpa_auth_get_spa(sm),
5046 LOGGER_INFO,
5047 "received EAPOL-Key msg 4/4 with invalid Key Data contents");
5048 return -1;
5049 }
5050
5051 /* MLD MAC address must be the same */
5052 if (!kde.mac_addr ||
5053 !ether_addr_equal(kde.mac_addr, sm->peer_mld_addr)) {
5054 wpa_printf(MSG_DEBUG,
5055 "MLD: Mismatching or missing MLD address in EAPOL-Key msg 4/4");
5056 return -1;
5057 }
5058
5059 wpa_printf(MSG_DEBUG, "MLD: MLD address in EAPOL-Key msg 4/4: " MACSTR,
5060 MAC2STR(kde.mac_addr));
5061 #endif /* CONFIG_IEEE80211BE */
5062
5063 return 0;
5064 }
5065
5066
5067 SM_STATE(WPA_PTK, PTKINITDONE)
5068 {
5069 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
5070 sm->EAPOLKeyReceived = false;
5071
5072 if (wpa_auth_validate_ml_kdes_m4(sm) < 0) {
5073 wpa_sta_disconnect(sm->wpa_auth, sm->addr,
5074 WLAN_REASON_PREV_AUTH_NOT_VALID);
5075 return;
5076 }
5077
5078 if (sm->Pair) {
5079 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
5080 int klen = wpa_cipher_key_len(sm->pairwise);
5081 int res;
5082
5083 if (sm->use_ext_key_id)
5084 res = wpa_auth_set_key(sm->wpa_auth, 0, 0, sm->addr,
5085 sm->keyidx_active, NULL, 0,
5086 KEY_FLAG_PAIRWISE_RX_TX_MODIFY);
5087 else
5088 res = wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr,
5089 0, sm->PTK.tk, klen,
5090 KEY_FLAG_PAIRWISE_RX_TX);
5091 if (res) {
5092 wpa_sta_disconnect(sm->wpa_auth, sm->addr,
5093 WLAN_REASON_PREV_AUTH_NOT_VALID);
5094 return;
5095 }
5096
5097 #ifdef CONFIG_PASN
5098 if (sm->wpa_auth->conf.secure_ltf &&
5099 ieee802_11_rsnx_capab(sm->rsnxe,
5100 WLAN_RSNX_CAPAB_SECURE_LTF) &&
5101 wpa_auth_set_ltf_keyseed(sm->wpa_auth, sm->addr,
5102 sm->PTK.ltf_keyseed,
5103 sm->PTK.ltf_keyseed_len)) {
5104 wpa_printf(MSG_ERROR,
5105 "WPA: Failed to set LTF keyseed to driver");
5106 wpa_sta_disconnect(sm->wpa_auth, sm->addr,
5107 WLAN_REASON_PREV_AUTH_NOT_VALID);
5108 return;
5109 }
5110 #endif /* CONFIG_PASN */
5111
5112 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
5113 sm->pairwise_set = true;
5114
5115 wpa_auth_set_ptk_rekey_timer(sm);
5116 wpa_auth_store_ptksa(sm->wpa_auth, sm->addr, sm->pairwise,
5117 dot11RSNAConfigPMKLifetime, &sm->PTK);
5118
5119 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
5120 sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP ||
5121 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
5122 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
5123 WPA_EAPOL_authorized, 1);
5124 }
5125 }
5126
5127 if (0 /* IBSS == TRUE */) {
5128 sm->keycount++;
5129 if (sm->keycount == 2) {
5130 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
5131 WPA_EAPOL_portValid, 1);
5132 }
5133 } else {
5134 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
5135 1);
5136 }
5137 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable,
5138 false);
5139 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, true);
5140 if (sm->wpa == WPA_VERSION_WPA)
5141 sm->PInitAKeys = true;
5142 else
5143 sm->has_GTK = true;
5144 wpa_auth_vlogger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
5145 "pairwise key handshake completed (%s)",
5146 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
5147 wpa_msg(sm->wpa_auth->conf.msg_ctx, MSG_INFO, "EAPOL-4WAY-HS-COMPLETED "
5148 MACSTR, MAC2STR(sm->addr));
5149
5150 #ifdef CONFIG_IEEE80211R_AP
5151 wpa_ft_push_pmk_r1(sm->wpa_auth, wpa_auth_get_spa(sm));
5152 #endif /* CONFIG_IEEE80211R_AP */
5153
5154 sm->ptkstart_without_success = 0;
5155 }
5156
5157
5158 SM_STEP(WPA_PTK)
5159 {
5160 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
5161 struct wpa_auth_config *conf = &wpa_auth->conf;
5162
5163 if (sm->Init)
5164 SM_ENTER(WPA_PTK, INITIALIZE);
5165 else if (sm->Disconnect
5166 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
5167 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
5168 "WPA_PTK: sm->Disconnect");
5169 SM_ENTER(WPA_PTK, DISCONNECT);
5170 }
5171 else if (sm->DeauthenticationRequest)
5172 SM_ENTER(WPA_PTK, DISCONNECTED);
5173 else if (sm->AuthenticationRequest)
5174 SM_ENTER(WPA_PTK, AUTHENTICATION);
5175 else if (sm->ReAuthenticationRequest)
5176 SM_ENTER(WPA_PTK, AUTHENTICATION2);
5177 else if (sm->PTKRequest) {
5178 if (wpa_auth_sm_ptk_update(sm) < 0)
5179 SM_ENTER(WPA_PTK, DISCONNECTED);
5180 else
5181 SM_ENTER(WPA_PTK, PTKSTART);
5182 } else switch (sm->wpa_ptk_state) {
5183 case WPA_PTK_INITIALIZE:
5184 break;
5185 case WPA_PTK_DISCONNECT:
5186 SM_ENTER(WPA_PTK, DISCONNECTED);
5187 break;
5188 case WPA_PTK_DISCONNECTED:
5189 SM_ENTER(WPA_PTK, INITIALIZE);
5190 break;
5191 case WPA_PTK_AUTHENTICATION:
5192 SM_ENTER(WPA_PTK, AUTHENTICATION2);
5193 break;
5194 case WPA_PTK_AUTHENTICATION2:
5195 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
5196 wpa_auth_get_eapol(wpa_auth, sm->addr,
5197 WPA_EAPOL_keyRun))
5198 SM_ENTER(WPA_PTK, INITPMK);
5199 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
5200 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE
5201 /* FIX: && 802.1X::keyRun */)
5202 SM_ENTER(WPA_PTK, INITPSK);
5203 else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP)
5204 SM_ENTER(WPA_PTK, INITPMK);
5205 break;
5206 case WPA_PTK_INITPMK:
5207 if (wpa_auth_get_eapol(wpa_auth, sm->addr,
5208 WPA_EAPOL_keyAvailable)) {
5209 SM_ENTER(WPA_PTK, PTKSTART);
5210 #ifdef CONFIG_DPP
5211 } else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && sm->pmksa) {
5212 SM_ENTER(WPA_PTK, PTKSTART);
5213 #endif /* CONFIG_DPP */
5214 } else {
5215 wpa_auth->dot11RSNA4WayHandshakeFailures++;
5216 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
5217 LOGGER_INFO,
5218 "INITPMK - keyAvailable = false");
5219 SM_ENTER(WPA_PTK, DISCONNECT);
5220 }
5221 break;
5222 case WPA_PTK_INITPSK:
5223 if (wpa_auth_get_psk(wpa_auth, sm->addr, sm->p2p_dev_addr,
5224 NULL, NULL, NULL)) {
5225 SM_ENTER(WPA_PTK, PTKSTART);
5226 #ifdef CONFIG_SAE
5227 } else if (wpa_auth_uses_sae(sm) && sm->pmksa) {
5228 SM_ENTER(WPA_PTK, PTKSTART);
5229 #endif /* CONFIG_SAE */
5230 } else if (wpa_key_mgmt_wpa_psk_no_sae(sm->wpa_key_mgmt) &&
5231 wpa_auth->conf.radius_psk) {
5232 wpa_printf(MSG_DEBUG,
5233 "INITPSK: No PSK yet available for STA - use RADIUS later");
5234 SM_ENTER(WPA_PTK, PTKSTART);
5235 } else {
5236 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
5237 LOGGER_INFO,
5238 "no PSK configured for the STA");
5239 wpa_auth->dot11RSNA4WayHandshakeFailures++;
5240 SM_ENTER(WPA_PTK, DISCONNECT);
5241 }
5242 break;
5243 case WPA_PTK_PTKSTART:
5244 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
5245 sm->EAPOLKeyPairwise)
5246 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
5247 else if (sm->TimeoutCtr > conf->wpa_pairwise_update_count) {
5248 wpa_auth->dot11RSNA4WayHandshakeFailures++;
5249 wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
5250 LOGGER_DEBUG,
5251 "PTKSTART: Retry limit %u reached",
5252 conf->wpa_pairwise_update_count);
5253 sm->disconnect_reason =
5254 WLAN_REASON_4WAY_HANDSHAKE_TIMEOUT;
5255 SM_ENTER(WPA_PTK, DISCONNECT);
5256 } else if (sm->TimeoutEvt)
5257 SM_ENTER(WPA_PTK, PTKSTART);
5258 break;
5259 case WPA_PTK_PTKCALCNEGOTIATING:
5260 if (sm->MICVerified)
5261 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
5262 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
5263 sm->EAPOLKeyPairwise)
5264 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
5265 else if (sm->TimeoutEvt)
5266 SM_ENTER(WPA_PTK, PTKSTART);
5267 break;
5268 case WPA_PTK_PTKCALCNEGOTIATING2:
5269 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
5270 break;
5271 case WPA_PTK_PTKINITNEGOTIATING:
5272 if (sm->update_snonce)
5273 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
5274 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
5275 sm->EAPOLKeyPairwise && sm->MICVerified)
5276 SM_ENTER(WPA_PTK, PTKINITDONE);
5277 else if (sm->TimeoutCtr >
5278 conf->wpa_pairwise_update_count ||
5279 (conf->wpa_disable_eapol_key_retries &&
5280 sm->TimeoutCtr > 1)) {
5281 wpa_auth->dot11RSNA4WayHandshakeFailures++;
5282 wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
5283 LOGGER_DEBUG,
5284 "PTKINITNEGOTIATING: Retry limit %u reached",
5285 conf->wpa_pairwise_update_count);
5286 sm->disconnect_reason =
5287 WLAN_REASON_4WAY_HANDSHAKE_TIMEOUT;
5288 SM_ENTER(WPA_PTK, DISCONNECT);
5289 } else if (sm->TimeoutEvt)
5290 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
5291 break;
5292 case WPA_PTK_PTKINITDONE:
5293 break;
5294 }
5295 }
5296
5297
5298 SM_STATE(WPA_PTK_GROUP, IDLE)
5299 {
5300 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
5301 if (sm->Init) {
5302 /* Init flag is not cleared here, so avoid busy
5303 * loop by claiming nothing changed. */
5304 sm->changed = false;
5305 }
5306 sm->GTimeoutCtr = 0;
5307 }
5308
5309
5310 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
5311 {
5312 u8 rsc[WPA_KEY_RSC_LEN];
5313 struct wpa_group *gsm = sm->group;
5314 const u8 *kde = NULL;
5315 u8 *kde_buf = NULL, *pos, hdr[2];
5316 size_t kde_len = 0;
5317 u8 *gtk, stub_gtk[32];
5318 struct wpa_auth_config *conf = &sm->wpa_auth->conf;
5319 bool is_mld = false;
5320
5321 #ifdef CONFIG_IEEE80211BE
5322 is_mld = sm->mld_assoc_link_id >= 0;
5323 #endif /* CONFIG_IEEE80211BE */
5324
5325 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
5326
5327 sm->GTimeoutCtr++;
5328 if (conf->wpa_disable_eapol_key_retries && sm->GTimeoutCtr > 1) {
5329 /* Do not allow retransmission of EAPOL-Key group msg 1/2 */
5330 return;
5331 }
5332 if (sm->GTimeoutCtr > conf->wpa_group_update_count) {
5333 /* No point in sending the EAPOL-Key - we will disconnect
5334 * immediately following this. */
5335 return;
5336 }
5337
5338 if (sm->wpa == WPA_VERSION_WPA)
5339 sm->PInitAKeys = false;
5340 sm->TimeoutEvt = false;
5341 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
5342 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
5343 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
5344 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
5345 wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
5346 "sending 1/2 msg of Group Key Handshake");
5347
5348 gtk = gsm->GTK[gsm->GN - 1];
5349 if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
5350 /*
5351 * Provide unique random GTK to each STA to prevent use
5352 * of GTK in the BSS.
5353 */
5354 if (random_get_bytes(stub_gtk, gsm->GTK_len) < 0)
5355 return;
5356 gtk = stub_gtk;
5357 }
5358
5359 if (sm->wpa == WPA_VERSION_WPA2 && !is_mld) {
5360 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
5361 ieee80211w_kde_len(sm) + ocv_oci_len(sm);
5362 kde_buf = os_malloc(kde_len);
5363 if (!kde_buf)
5364 return;
5365
5366 kde = pos = kde_buf;
5367 hdr[0] = gsm->GN & 0x03;
5368 hdr[1] = 0;
5369 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
5370 gtk, gsm->GTK_len);
5371 pos = ieee80211w_kde_add(sm, pos);
5372 if (ocv_oci_add(sm, &pos,
5373 conf->oci_freq_override_eapol_g1) < 0) {
5374 os_free(kde_buf);
5375 return;
5376 }
5377 kde_len = pos - kde;
5378 #ifdef CONFIG_IEEE80211BE
5379 } else if (sm->wpa == WPA_VERSION_WPA2 && is_mld) {
5380 kde_len = wpa_auth_ml_group_kdes_len(sm);
5381 if (kde_len) {
5382 kde_buf = os_malloc(kde_len);
5383 if (!kde_buf)
5384 return;
5385
5386 kde = pos = kde_buf;
5387 pos = wpa_auth_ml_group_kdes(sm, pos);
5388 kde_len = pos - kde_buf;
5389 }
5390 #endif /* CONFIG_IEEE80211BE */
5391 } else {
5392 kde = gtk;
5393 kde_len = gsm->GTK_len;
5394 }
5395
5396 wpa_send_eapol(sm->wpa_auth, sm,
5397 WPA_KEY_INFO_SECURE |
5398 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
5399 WPA_KEY_INFO_MIC : 0) |
5400 WPA_KEY_INFO_ACK |
5401 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
5402 rsc, NULL, kde, kde_len, gsm->GN, 1);
5403
5404 bin_clear_free(kde_buf, kde_len);
5405 }
5406
5407
5408 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
5409 {
5410 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
5411 #ifdef CONFIG_OCV
5412 const u8 *key_data, *mic;
5413 struct ieee802_1x_hdr *hdr;
5414 struct wpa_eapol_key *key;
5415 struct wpa_eapol_ie_parse kde;
5416 size_t mic_len;
5417 u16 key_data_length;
5418 #endif /* CONFIG_OCV */
5419
5420 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
5421 sm->EAPOLKeyReceived = false;
5422
5423 #ifdef CONFIG_OCV
5424 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
5425
5426 /*
5427 * Note: last_rx_eapol_key length fields have already been validated in
5428 * wpa_receive().
5429 */
5430 hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
5431 key = (struct wpa_eapol_key *) (hdr + 1);
5432 mic = (u8 *) (key + 1);
5433 key_data = mic + mic_len + 2;
5434 key_data_length = WPA_GET_BE16(mic + mic_len);
5435 if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
5436 sizeof(*key) - mic_len - 2)
5437 return;
5438
5439 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
5440 wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
5441 "received EAPOL-Key group msg 2/2 with invalid Key Data contents");
5442 return;
5443 }
5444
5445 if (wpa_auth_uses_ocv(sm)) {
5446 struct wpa_channel_info ci;
5447 int tx_chanwidth;
5448 int tx_seg1_idx;
5449
5450 if (wpa_channel_info(wpa_auth, &ci) != 0) {
5451 wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
5452 LOGGER_INFO,
5453 "Failed to get channel info to validate received OCI in EAPOL-Key group 2/2");
5454 return;
5455 }
5456
5457 if (get_sta_tx_parameters(sm,
5458 channel_width_to_int(ci.chanwidth),
5459 ci.seg1_idx, &tx_chanwidth,
5460 &tx_seg1_idx) < 0)
5461 return;
5462
5463 if (ocv_verify_tx_params(kde.oci, kde.oci_len, &ci,
5464 tx_chanwidth, tx_seg1_idx) !=
5465 OCI_SUCCESS) {
5466 wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
5467 LOGGER_INFO,
5468 "OCV failed: %s", ocv_errorstr);
5469 if (wpa_auth->conf.msg_ctx)
5470 wpa_msg(wpa_auth->conf.msg_ctx, MSG_INFO,
5471 OCV_FAILURE "addr=" MACSTR
5472 " frame=eapol-key-g2 error=%s",
5473 MAC2STR(wpa_auth_get_spa(sm)),
5474 ocv_errorstr);
5475 return;
5476 }
5477 }
5478 #endif /* CONFIG_OCV */
5479
5480 if (sm->GUpdateStationKeys)
5481 wpa_gkeydone_sta(sm);
5482 sm->GTimeoutCtr = 0;
5483 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
5484 wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
5485 "group key handshake completed (%s)",
5486 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
5487 sm->has_GTK = true;
5488 }
5489
5490
5491 SM_STATE(WPA_PTK_GROUP, KEYERROR)
5492 {
5493 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
5494 if (sm->GUpdateStationKeys)
5495 wpa_gkeydone_sta(sm);
5496 if (sm->wpa_auth->conf.no_disconnect_on_group_keyerror &&
5497 sm->wpa == WPA_VERSION_WPA2) {
5498 wpa_auth_vlogger(sm->wpa_auth, wpa_auth_get_spa(sm),
5499 LOGGER_DEBUG,
5500 "group key handshake failed after %u tries - allow STA to remain connected",
5501 sm->wpa_auth->conf.wpa_group_update_count);
5502 return;
5503 }
5504 sm->Disconnect = true;
5505 sm->disconnect_reason = WLAN_REASON_GROUP_KEY_UPDATE_TIMEOUT;
5506 wpa_auth_vlogger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
5507 "group key handshake failed (%s) after %u tries",
5508 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN",
5509 sm->wpa_auth->conf.wpa_group_update_count);
5510 }
5511
5512
5513 SM_STEP(WPA_PTK_GROUP)
5514 {
5515 if (sm->Init || sm->PtkGroupInit) {
5516 SM_ENTER(WPA_PTK_GROUP, IDLE);
5517 sm->PtkGroupInit = false;
5518 } else switch (sm->wpa_ptk_group_state) {
5519 case WPA_PTK_GROUP_IDLE:
5520 if (sm->GUpdateStationKeys ||
5521 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
5522 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
5523 break;
5524 case WPA_PTK_GROUP_REKEYNEGOTIATING:
5525 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
5526 !sm->EAPOLKeyPairwise && sm->MICVerified)
5527 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
5528 else if (sm->GTimeoutCtr >
5529 sm->wpa_auth->conf.wpa_group_update_count ||
5530 (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
5531 sm->GTimeoutCtr > 1))
5532 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
5533 else if (sm->TimeoutEvt)
5534 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
5535 break;
5536 case WPA_PTK_GROUP_KEYERROR:
5537 SM_ENTER(WPA_PTK_GROUP, IDLE);
5538 break;
5539 case WPA_PTK_GROUP_REKEYESTABLISHED:
5540 SM_ENTER(WPA_PTK_GROUP, IDLE);
5541 break;
5542 }
5543 }
5544
5545
5546 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
5547 struct wpa_group *group)
5548 {
5549 struct wpa_auth_config *conf = &wpa_auth->conf;
5550 int ret = 0;
5551 size_t len;
5552
5553 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
5554 inc_byte_array(group->Counter, WPA_NONCE_LEN);
5555 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
5556 wpa_auth->addr, group->GNonce,
5557 group->GTK[group->GN - 1], group->GTK_len) < 0)
5558 ret = -1;
5559 wpa_hexdump_key(MSG_DEBUG, "GTK",
5560 group->GTK[group->GN - 1], group->GTK_len);
5561
5562 if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
5563 len = wpa_cipher_key_len(conf->group_mgmt_cipher);
5564 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
5565 inc_byte_array(group->Counter, WPA_NONCE_LEN);
5566 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
5567 wpa_auth->addr, group->GNonce,
5568 group->IGTK[group->GN_igtk - 4], len) < 0)
5569 ret = -1;
5570 wpa_hexdump_key(MSG_DEBUG, "IGTK",
5571 group->IGTK[group->GN_igtk - 4], len);
5572 }
5573
5574 if (!wpa_auth->non_tx_beacon_prot &&
5575 conf->ieee80211w == NO_MGMT_FRAME_PROTECTION)
5576 return ret;
5577 if (!conf->beacon_prot)
5578 return ret;
5579
5580 if (wpa_auth->conf.tx_bss_auth) {
5581 group = wpa_auth->conf.tx_bss_auth->group;
5582 if (group->bigtk_set)
5583 return ret;
5584 wpa_printf(MSG_DEBUG, "Set up BIGTK for TX BSS");
5585 }
5586
5587 len = wpa_cipher_key_len(conf->group_mgmt_cipher);
5588 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
5589 inc_byte_array(group->Counter, WPA_NONCE_LEN);
5590 if (wpa_gmk_to_gtk(group->GMK, "BIGTK key expansion",
5591 wpa_auth->addr, group->GNonce,
5592 group->BIGTK[group->GN_bigtk - 6], len) < 0)
5593 return -1;
5594 group->bigtk_set = true;
5595 wpa_hexdump_key(MSG_DEBUG, "BIGTK",
5596 group->BIGTK[group->GN_bigtk - 6], len);
5597
5598 return ret;
5599 }
5600
5601
5602 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
5603 struct wpa_group *group)
5604 {
5605 wpa_printf(MSG_DEBUG,
5606 "WPA: group state machine entering state GTK_INIT (VLAN-ID %d)",
5607 group->vlan_id);
5608 group->changed = false; /* GInit is not cleared here; avoid loop */
5609 group->wpa_group_state = WPA_GROUP_GTK_INIT;
5610
5611 /* GTK[0..N] = 0 */
5612 os_memset(group->GTK, 0, sizeof(group->GTK));
5613 group->GN = 1;
5614 group->GM = 2;
5615 group->GN_igtk = 4;
5616 group->GM_igtk = 5;
5617 group->GN_bigtk = 6;
5618 group->GM_bigtk = 7;
5619 /* GTK[GN] = CalcGTK() */
5620 wpa_gtk_update(wpa_auth, group);
5621 }
5622
5623
5624 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
5625 {
5626 if (ctx != NULL && ctx != sm->group)
5627 return 0;
5628
5629 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
5630 wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm),
5631 LOGGER_DEBUG,
5632 "Not in PTKINITDONE; skip Group Key update");
5633 sm->GUpdateStationKeys = false;
5634 return 0;
5635 }
5636 if (sm->GUpdateStationKeys) {
5637 /*
5638 * This should not really happen, so add a debug log entry.
5639 * Since we clear the GKeyDoneStations before the loop, the
5640 * station needs to be counted here anyway.
5641 */
5642 wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm),
5643 LOGGER_DEBUG,
5644 "GUpdateStationKeys was already set when marking station for GTK rekeying");
5645 }
5646
5647 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
5648 if (sm->is_wnmsleep)
5649 return 0;
5650
5651 sm->group->GKeyDoneStations++;
5652 sm->GUpdateStationKeys = true;
5653
5654 wpa_sm_step(sm);
5655 return 0;
5656 }
5657
5658
5659 #ifdef CONFIG_WNM_AP
5660 /* update GTK when exiting WNM-Sleep Mode */
5661 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
5662 {
5663 if (!sm || sm->is_wnmsleep)
5664 return;
5665
5666 wpa_group_update_sta(sm, NULL);
5667 }
5668
5669
5670 void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
5671 {
5672 if (sm)
5673 sm->is_wnmsleep = !!flag;
5674 }
5675
5676
5677 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
5678 {
5679 struct wpa_auth_config *conf = &sm->wpa_auth->conf;
5680 struct wpa_group *gsm = sm->group;
5681 u8 *start = pos;
5682
5683 /*
5684 * GTK subelement:
5685 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
5686 * Key[5..32]
5687 */
5688 *pos++ = WNM_SLEEP_SUBELEM_GTK;
5689 *pos++ = 11 + gsm->GTK_len;
5690 /* Key ID in B0-B1 of Key Info */
5691 WPA_PUT_LE16(pos, gsm->GN & 0x03);
5692 pos += 2;
5693 *pos++ = gsm->GTK_len;
5694 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
5695 return 0;
5696 pos += 8;
5697 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
5698 if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
5699 /*
5700 * Provide unique random GTK to each STA to prevent use
5701 * of GTK in the BSS.
5702 */
5703 if (random_get_bytes(pos, gsm->GTK_len) < 0)
5704 return 0;
5705 }
5706 pos += gsm->GTK_len;
5707
5708 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
5709 gsm->GN);
5710 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
5711 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
5712
5713 return pos - start;
5714 }
5715
5716
5717 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
5718 {
5719 struct wpa_auth_config *conf = &sm->wpa_auth->conf;
5720 struct wpa_group *gsm = sm->group;
5721 u8 *start = pos;
5722 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
5723
5724 /*
5725 * IGTK subelement:
5726 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
5727 */
5728 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
5729 *pos++ = 2 + 6 + len;
5730 WPA_PUT_LE16(pos, gsm->GN_igtk);
5731 pos += 2;
5732 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
5733 return 0;
5734 pos += 6;
5735
5736 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
5737 if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
5738 /*
5739 * Provide unique random IGTK to each STA to prevent use
5740 * of IGTK in the BSS.
5741 */
5742 if (random_get_bytes(pos, len) < 0)
5743 return 0;
5744 }
5745 pos += len;
5746
5747 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
5748 gsm->GN_igtk);
5749 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
5750 gsm->IGTK[gsm->GN_igtk - 4], len);
5751
5752 return pos - start;
5753 }
5754
5755
5756 int wpa_wnmsleep_bigtk_subelem(struct wpa_state_machine *sm, u8 *pos)
5757 {
5758 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
5759 struct wpa_group *gsm = wpa_auth->group;
5760 u8 *start = pos;
5761 size_t len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
5762
5763 /*
5764 * BIGTK subelement:
5765 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
5766 */
5767 *pos++ = WNM_SLEEP_SUBELEM_BIGTK;
5768 *pos++ = 2 + 6 + len;
5769 WPA_PUT_LE16(pos, gsm->GN_bigtk);
5770 pos += 2;
5771 if (wpa_auth_get_seqnum(wpa_auth, NULL, gsm->GN_bigtk, pos) != 0)
5772 return 0;
5773 pos += 6;
5774
5775 os_memcpy(pos, gsm->BIGTK[gsm->GN_bigtk - 6], len);
5776 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
5777 /*
5778 * Provide unique random BIGTK to each STA to prevent use
5779 * of BIGTK in the BSS.
5780 */
5781 if (random_get_bytes(pos, len) < 0)
5782 return 0;
5783 }
5784 pos += len;
5785
5786 wpa_printf(MSG_DEBUG, "WNM: BIGTK Key ID %u in WNM-Sleep Mode exit",
5787 gsm->GN_bigtk);
5788 wpa_hexdump_key(MSG_DEBUG, "WNM: BIGTK in WNM-Sleep Mode exit",
5789 gsm->BIGTK[gsm->GN_bigtk - 6], len);
5790
5791 return pos - start;
5792 }
5793
5794 #endif /* CONFIG_WNM_AP */
5795
5796
5797 static void wpa_group_update_gtk(struct wpa_authenticator *wpa_auth,
5798 struct wpa_group *group)
5799 {
5800 int tmp;
5801
5802 tmp = group->GM;
5803 group->GM = group->GN;
5804 group->GN = tmp;
5805 tmp = group->GM_igtk;
5806 group->GM_igtk = group->GN_igtk;
5807 group->GN_igtk = tmp;
5808 tmp = group->GM_bigtk;
5809 group->GM_bigtk = group->GN_bigtk;
5810 group->GN_bigtk = tmp;
5811 /* "GKeyDoneStations = GNoStations" is done in more robust way by
5812 * counting the STAs that are marked with GUpdateStationKeys instead of
5813 * including all STAs that could be in not-yet-completed state. */
5814 wpa_gtk_update(wpa_auth, group);
5815 }
5816
5817
5818 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
5819 struct wpa_group *group)
5820 {
5821 wpa_printf(MSG_DEBUG,
5822 "WPA: group state machine entering state SETKEYS (VLAN-ID %d)",
5823 group->vlan_id);
5824 group->changed = true;
5825 group->wpa_group_state = WPA_GROUP_SETKEYS;
5826 group->GTKReKey = false;
5827
5828 #ifdef CONFIG_IEEE80211BE
5829 if (wpa_auth->is_ml)
5830 goto skip_update;
5831 #endif /* CONFIG_IEEE80211BE */
5832
5833 wpa_group_update_gtk(wpa_auth, group);
5834
5835 if (group->GKeyDoneStations) {
5836 wpa_printf(MSG_DEBUG,
5837 "wpa_group_setkeys: Unexpected GKeyDoneStations=%d when starting new GTK rekey",
5838 group->GKeyDoneStations);
5839 group->GKeyDoneStations = 0;
5840 }
5841
5842 #ifdef CONFIG_IEEE80211BE
5843 skip_update:
5844 #endif /* CONFIG_IEEE80211BE */
5845 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
5846 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
5847 group->GKeyDoneStations);
5848 }
5849
5850
5851 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
5852 struct wpa_group *group)
5853 {
5854 struct wpa_auth_config *conf = &wpa_auth->conf;
5855 int ret = 0;
5856
5857 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
5858 wpa_cipher_to_alg(conf->wpa_group),
5859 broadcast_ether_addr, group->GN,
5860 group->GTK[group->GN - 1], group->GTK_len,
5861 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
5862 ret = -1;
5863
5864 if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
5865 enum wpa_alg alg;
5866 size_t len;
5867
5868 alg = wpa_cipher_to_alg(conf->group_mgmt_cipher);
5869 len = wpa_cipher_key_len(conf->group_mgmt_cipher);
5870
5871 if (ret == 0 &&
5872 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
5873 broadcast_ether_addr, group->GN_igtk,
5874 group->IGTK[group->GN_igtk - 4], len,
5875 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
5876 ret = -1;
5877
5878 if (ret || !conf->beacon_prot)
5879 return ret;
5880 if (wpa_auth->conf.tx_bss_auth) {
5881 wpa_auth = wpa_auth->conf.tx_bss_auth;
5882 group = wpa_auth->group;
5883 if (!group->bigtk_set || group->bigtk_configured)
5884 return ret;
5885 }
5886 if (wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
5887 broadcast_ether_addr, group->GN_bigtk,
5888 group->BIGTK[group->GN_bigtk - 6], len,
5889 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
5890 ret = -1;
5891 else
5892 group->bigtk_configured = true;
5893 }
5894
5895 return ret;
5896 }
5897
5898
5899 static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
5900 {
5901 if (sm->group == ctx) {
5902 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
5903 " for disconnection due to fatal failure",
5904 MAC2STR(wpa_auth_get_spa(sm)));
5905 sm->Disconnect = true;
5906 }
5907
5908 return 0;
5909 }
5910
5911
5912 static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
5913 struct wpa_group *group)
5914 {
5915 wpa_printf(MSG_DEBUG,
5916 "WPA: group state machine entering state FATAL_FAILURE");
5917 group->changed = true;
5918 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
5919 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
5920 }
5921
5922
5923 static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
5924 struct wpa_group *group)
5925 {
5926 wpa_printf(MSG_DEBUG,
5927 "WPA: group state machine entering state SETKEYSDONE (VLAN-ID %d)",
5928 group->vlan_id);
5929 group->changed = true;
5930 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
5931
5932 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
5933 wpa_group_fatal_failure(wpa_auth, group);
5934 return -1;
5935 }
5936
5937 return 0;
5938 }
5939
5940
5941 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
5942 struct wpa_group *group)
5943 {
5944 if (group->GInit) {
5945 wpa_group_gtk_init(wpa_auth, group);
5946 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
5947 /* Do not allow group operations */
5948 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
5949 group->GTKAuthenticator) {
5950 wpa_group_setkeysdone(wpa_auth, group);
5951 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
5952 group->GTKReKey) {
5953 wpa_group_setkeys(wpa_auth, group);
5954 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
5955 if (group->GKeyDoneStations == 0)
5956 wpa_group_setkeysdone(wpa_auth, group);
5957 else if (group->GTKReKey)
5958 wpa_group_setkeys(wpa_auth, group);
5959 }
5960 }
5961
5962
5963 static void wpa_clear_changed(struct wpa_state_machine *sm)
5964 {
5965 #ifdef CONFIG_IEEE80211BE
5966 int link_id;
5967 #endif /* CONFIG_IEEE80211BE */
5968
5969 sm->changed = false;
5970 sm->wpa_auth->group->changed = false;
5971
5972 #ifdef CONFIG_IEEE80211BE
5973 for_each_sm_auth(sm, link_id)
5974 sm->mld_links[link_id].wpa_auth->group->changed = false;
5975 #endif /* CONFIG_IEEE80211BE */
5976 }
5977
5978
5979 static void wpa_group_sm_step_links(struct wpa_state_machine *sm)
5980 {
5981 #ifdef CONFIG_IEEE80211BE
5982 int link_id;
5983 #endif /* CONFIG_IEEE80211BE */
5984
5985 if (!sm || !sm->wpa_auth)
5986 return;
5987 wpa_group_sm_step(sm->wpa_auth, sm->wpa_auth->group);
5988
5989 #ifdef CONFIG_IEEE80211BE
5990 for_each_sm_auth(sm, link_id) {
5991 wpa_group_sm_step(sm->mld_links[link_id].wpa_auth,
5992 sm->mld_links[link_id].wpa_auth->group);
5993 }
5994 #endif /* CONFIG_IEEE80211BE */
5995 }
5996
5997
5998 static bool wpa_group_sm_changed(struct wpa_state_machine *sm)
5999 {
6000 #ifdef CONFIG_IEEE80211BE
6001 int link_id;
6002 #endif /* CONFIG_IEEE80211BE */
6003 bool changed;
6004
6005 if (!sm || !sm->wpa_auth)
6006 return false;
6007 changed = sm->wpa_auth->group->changed;
6008
6009 #ifdef CONFIG_IEEE80211BE
6010 for_each_sm_auth(sm, link_id)
6011 changed |= sm->mld_links[link_id].wpa_auth->group->changed;
6012 #endif /* CONFIG_IEEE80211BE */
6013
6014 return changed;
6015 }
6016
6017
6018 static int wpa_sm_step(struct wpa_state_machine *sm)
6019 {
6020 if (!sm)
6021 return 0;
6022
6023 if (sm->in_step_loop) {
6024 /* This should not happen, but if it does, make sure we do not
6025 * end up freeing the state machine too early by exiting the
6026 * recursive call. */
6027 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
6028 return 0;
6029 }
6030
6031 sm->in_step_loop = 1;
6032 do {
6033 if (sm->pending_deinit)
6034 break;
6035
6036 wpa_clear_changed(sm);
6037
6038 SM_STEP_RUN(WPA_PTK);
6039 if (sm->pending_deinit)
6040 break;
6041 SM_STEP_RUN(WPA_PTK_GROUP);
6042 if (sm->pending_deinit)
6043 break;
6044 wpa_group_sm_step_links(sm);
6045 } while (sm->changed || wpa_group_sm_changed(sm));
6046 sm->in_step_loop = 0;
6047
6048 if (sm->pending_deinit) {
6049 wpa_printf(MSG_DEBUG,
6050 "WPA: Completing pending STA state machine deinit for "
6051 MACSTR, MAC2STR(wpa_auth_get_spa(sm)));
6052 wpa_free_sta_sm(sm);
6053 return 1;
6054 }
6055 return 0;
6056 }
6057
6058
6059 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
6060 {
6061 struct wpa_state_machine *sm = eloop_ctx;
6062 wpa_sm_step(sm);
6063 }
6064
6065
6066 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
6067 {
6068 if (!sm)
6069 return;
6070 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
6071 }
6072
6073
6074 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
6075 {
6076 int tmp, i;
6077 struct wpa_group *group;
6078
6079 if (!wpa_auth)
6080 return;
6081
6082 group = wpa_auth->group;
6083
6084 for (i = 0; i < 2; i++) {
6085 tmp = group->GM;
6086 group->GM = group->GN;
6087 group->GN = tmp;
6088 tmp = group->GM_igtk;
6089 group->GM_igtk = group->GN_igtk;
6090 group->GN_igtk = tmp;
6091 if (!wpa_auth->conf.tx_bss_auth) {
6092 tmp = group->GM_bigtk;
6093 group->GM_bigtk = group->GN_bigtk;
6094 group->GN_bigtk = tmp;
6095 }
6096 wpa_gtk_update(wpa_auth, group);
6097 wpa_group_config_group_keys(wpa_auth, group);
6098 }
6099 }
6100
6101
6102 static const char * wpa_bool_txt(int val)
6103 {
6104 return val ? "TRUE" : "FALSE";
6105 }
6106
6107
6108 #define RSN_SUITE "%02x-%02x-%02x-%d"
6109 #define RSN_SUITE_ARG(s) \
6110 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
6111
6112 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
6113 {
6114 struct wpa_auth_config *conf;
6115 int len = 0, ret;
6116 char pmkid_txt[PMKID_LEN * 2 + 1];
6117 #ifdef CONFIG_RSN_PREAUTH
6118 const int preauth = 1;
6119 #else /* CONFIG_RSN_PREAUTH */
6120 const int preauth = 0;
6121 #endif /* CONFIG_RSN_PREAUTH */
6122
6123 if (!wpa_auth)
6124 return len;
6125 conf = &wpa_auth->conf;
6126
6127 ret = os_snprintf(buf + len, buflen - len,
6128 "dot11RSNAOptionImplemented=TRUE\n"
6129 "dot11RSNAPreauthenticationImplemented=%s\n"
6130 "dot11RSNAEnabled=%s\n"
6131 "dot11RSNAPreauthenticationEnabled=%s\n",
6132 wpa_bool_txt(preauth),
6133 wpa_bool_txt(conf->wpa & WPA_PROTO_RSN),
6134 wpa_bool_txt(conf->rsn_preauth));
6135 if (os_snprintf_error(buflen - len, ret))
6136 return len;
6137 len += ret;
6138
6139 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
6140 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
6141
6142 ret = os_snprintf(
6143 buf + len, buflen - len,
6144 "dot11RSNAConfigVersion=%u\n"
6145 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
6146 /* FIX: dot11RSNAConfigGroupCipher */
6147 /* FIX: dot11RSNAConfigGroupRekeyMethod */
6148 /* FIX: dot11RSNAConfigGroupRekeyTime */
6149 /* FIX: dot11RSNAConfigGroupRekeyPackets */
6150 "dot11RSNAConfigGroupRekeyStrict=%u\n"
6151 "dot11RSNAConfigGroupUpdateCount=%u\n"
6152 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
6153 "dot11RSNAConfigGroupCipherSize=%u\n"
6154 "dot11RSNAConfigPMKLifetime=%u\n"
6155 "dot11RSNAConfigPMKReauthThreshold=%u\n"
6156 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
6157 "dot11RSNAConfigSATimeout=%u\n"
6158 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
6159 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
6160 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
6161 "dot11RSNAPMKIDUsed=%s\n"
6162 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
6163 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
6164 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
6165 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
6166 "dot11RSNA4WayHandshakeFailures=%u\n"
6167 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
6168 RSN_VERSION,
6169 !!conf->wpa_strict_rekey,
6170 conf->wpa_group_update_count,
6171 conf->wpa_pairwise_update_count,
6172 wpa_cipher_key_len(conf->wpa_group) * 8,
6173 dot11RSNAConfigPMKLifetime,
6174 dot11RSNAConfigPMKReauthThreshold,
6175 dot11RSNAConfigSATimeout,
6176 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
6177 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
6178 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
6179 pmkid_txt,
6180 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
6181 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
6182 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
6183 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
6184 wpa_auth->dot11RSNA4WayHandshakeFailures);
6185 if (os_snprintf_error(buflen - len, ret))
6186 return len;
6187 len += ret;
6188
6189 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
6190 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
6191
6192 /* Private MIB */
6193 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
6194 wpa_auth->group->wpa_group_state);
6195 if (os_snprintf_error(buflen - len, ret))
6196 return len;
6197 len += ret;
6198
6199 return len;
6200 }
6201
6202
6203 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
6204 {
6205 int len = 0, ret;
6206 u32 pairwise = 0;
6207
6208 if (!sm)
6209 return 0;
6210
6211 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
6212
6213 /* dot11RSNAStatsEntry */
6214
6215 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
6216 WPA_PROTO_RSN : WPA_PROTO_WPA,
6217 sm->pairwise);
6218 if (pairwise == 0)
6219 return 0;
6220
6221 ret = os_snprintf(
6222 buf + len, buflen - len,
6223 /* TODO: dot11RSNAStatsIndex */
6224 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
6225 "dot11RSNAStatsVersion=1\n"
6226 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
6227 /* TODO: dot11RSNAStatsTKIPICVErrors */
6228 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
6229 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
6230 /* TODO: dot11RSNAStatsCCMPReplays */
6231 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
6232 /* TODO: dot11RSNAStatsTKIPReplays */,
6233 MAC2STR(sm->addr),
6234 RSN_SUITE_ARG(pairwise),
6235 sm->dot11RSNAStatsTKIPLocalMICFailures,
6236 sm->dot11RSNAStatsTKIPRemoteMICFailures);
6237 if (os_snprintf_error(buflen - len, ret))
6238 return len;
6239 len += ret;
6240
6241 /* Private MIB */
6242 ret = os_snprintf(buf + len, buflen - len,
6243 "wpa=%d\n"
6244 "AKMSuiteSelector=" RSN_SUITE "\n"
6245 "hostapdWPAPTKState=%d\n"
6246 "hostapdWPAPTKGroupState=%d\n"
6247 "hostapdMFPR=%d\n",
6248 sm->wpa,
6249 RSN_SUITE_ARG(wpa_akm_to_suite(sm->wpa_key_mgmt)),
6250 sm->wpa_ptk_state,
6251 sm->wpa_ptk_group_state,
6252 sm->mfpr);
6253 if (os_snprintf_error(buflen - len, ret))
6254 return len;
6255 len += ret;
6256
6257 return len;
6258 }
6259
6260
6261 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
6262 {
6263 if (wpa_auth)
6264 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
6265 }
6266
6267
6268 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
6269 {
6270 return sm && sm->pairwise_set;
6271 }
6272
6273
6274 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
6275 {
6276 return sm->pairwise;
6277 }
6278
6279
6280 const u8 * wpa_auth_get_pmk(struct wpa_state_machine *sm, int *len)
6281 {
6282 if (!sm)
6283 return NULL;
6284 *len = sm->pmk_len;
6285 return sm->PMK;
6286 }
6287
6288
6289 const u8 * wpa_auth_get_dpp_pkhash(struct wpa_state_machine *sm)
6290 {
6291 if (!sm || !sm->pmksa)
6292 return NULL;
6293 return sm->pmksa->dpp_pkhash;
6294 }
6295
6296
6297 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
6298 {
6299 if (!sm)
6300 return -1;
6301 return sm->wpa_key_mgmt;
6302 }
6303
6304
6305 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
6306 {
6307 if (!sm)
6308 return 0;
6309 return sm->wpa;
6310 }
6311
6312
6313 int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm)
6314 {
6315 if (!sm || !wpa_key_mgmt_ft(sm->wpa_key_mgmt))
6316 return 0;
6317 return sm->tk_already_set;
6318 }
6319
6320
6321 int wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine *sm)
6322 {
6323 if (!sm || !wpa_key_mgmt_fils(sm->wpa_key_mgmt))
6324 return 0;
6325 return sm->tk_already_set;
6326 }
6327
6328
6329 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
6330 struct rsn_pmksa_cache_entry *entry)
6331 {
6332 if (!sm || sm->pmksa != entry)
6333 return -1;
6334 sm->pmksa = NULL;
6335 return 0;
6336 }
6337
6338
6339 struct rsn_pmksa_cache_entry *
6340 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
6341 {
6342 return sm ? sm->pmksa : NULL;
6343 }
6344
6345
6346 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
6347 {
6348 if (sm)
6349 sm->dot11RSNAStatsTKIPLocalMICFailures++;
6350 }
6351
6352
6353 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
6354 {
6355 if (!wpa_auth)
6356 return NULL;
6357 *len = wpa_auth->wpa_ie_len;
6358 return wpa_auth->wpa_ie;
6359 }
6360
6361
6362 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
6363 unsigned int pmk_len,
6364 int session_timeout, struct eapol_state_machine *eapol)
6365 {
6366 if (!sm || sm->wpa != WPA_VERSION_WPA2 ||
6367 sm->wpa_auth->conf.disable_pmksa_caching)
6368 return -1;
6369
6370 #ifdef CONFIG_IEEE80211R_AP
6371 if (pmk_len >= 2 * PMK_LEN && wpa_key_mgmt_ft(sm->wpa_key_mgmt) &&
6372 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
6373 !wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
6374 /* Cache MPMK/XXKey instead of initial part from MSK */
6375 pmk = pmk + PMK_LEN;
6376 pmk_len = PMK_LEN;
6377 } else
6378 #endif /* CONFIG_IEEE80211R_AP */
6379 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
6380 if (pmk_len > PMK_LEN_SUITE_B_192)
6381 pmk_len = PMK_LEN_SUITE_B_192;
6382 } else if (pmk_len > PMK_LEN) {
6383 pmk_len = PMK_LEN;
6384 }
6385
6386 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK", pmk, pmk_len);
6387 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, pmk_len, NULL,
6388 sm->PTK.kck, sm->PTK.kck_len,
6389 wpa_auth_get_aa(sm),
6390 wpa_auth_get_spa(sm), session_timeout,
6391 eapol, sm->wpa_key_mgmt))
6392 return 0;
6393
6394 return -1;
6395 }
6396
6397
6398 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
6399 const u8 *pmk, size_t len, const u8 *sta_addr,
6400 int session_timeout,
6401 struct eapol_state_machine *eapol)
6402 {
6403 if (!wpa_auth)
6404 return -1;
6405
6406 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK from preauth", pmk, len);
6407 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, NULL,
6408 NULL, 0,
6409 wpa_auth->addr,
6410 sta_addr, session_timeout, eapol,
6411 WPA_KEY_MGMT_IEEE8021X))
6412 return 0;
6413
6414 return -1;
6415 }
6416
6417
6418 int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
6419 const u8 *pmk, size_t pmk_len, const u8 *pmkid,
6420 int akmp)
6421 {
6422 if (wpa_auth->conf.disable_pmksa_caching)
6423 return -1;
6424
6425 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK from SAE", pmk, pmk_len);
6426 if (!akmp)
6427 akmp = WPA_KEY_MGMT_SAE;
6428 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, pmk_len, pmkid,
6429 NULL, 0, wpa_auth->addr, addr, 0, NULL, akmp))
6430 return 0;
6431
6432 return -1;
6433 }
6434
6435
6436 void wpa_auth_add_sae_pmkid(struct wpa_state_machine *sm, const u8 *pmkid)
6437 {
6438 os_memcpy(sm->pmkid, pmkid, PMKID_LEN);
6439 sm->pmkid_set = 1;
6440 }
6441
6442
6443 int wpa_auth_pmksa_add2(struct wpa_authenticator *wpa_auth, const u8 *addr,
6444 const u8 *pmk, size_t pmk_len, const u8 *pmkid,
6445 int session_timeout, int akmp, const u8 *dpp_pkhash)
6446 {
6447 struct rsn_pmksa_cache_entry *entry;
6448
6449 if (!wpa_auth || wpa_auth->conf.disable_pmksa_caching)
6450 return -1;
6451
6452 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK (3)", pmk, PMK_LEN);
6453 entry = pmksa_cache_auth_add(wpa_auth->pmksa, pmk, pmk_len, pmkid,
6454 NULL, 0, wpa_auth->addr, addr, session_timeout,
6455 NULL, akmp);
6456 if (!entry)
6457 return -1;
6458
6459 if (dpp_pkhash)
6460 entry->dpp_pkhash = os_memdup(dpp_pkhash, SHA256_MAC_LEN);
6461
6462 return 0;
6463 }
6464
6465
6466 void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
6467 const u8 *sta_addr)
6468 {
6469 struct rsn_pmksa_cache_entry *pmksa;
6470
6471 if (!wpa_auth || !wpa_auth->pmksa)
6472 return;
6473 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
6474 if (pmksa) {
6475 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
6476 MACSTR " based on request", MAC2STR(sta_addr));
6477 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
6478 }
6479 }
6480
6481
6482 int wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf,
6483 size_t len)
6484 {
6485 if (!wpa_auth || !wpa_auth->pmksa)
6486 return 0;
6487 return pmksa_cache_auth_list(wpa_auth->pmksa, buf, len);
6488 }
6489
6490
6491 void wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth)
6492 {
6493 if (wpa_auth && wpa_auth->pmksa)
6494 pmksa_cache_auth_flush(wpa_auth->pmksa);
6495 }
6496
6497
6498 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
6499 #ifdef CONFIG_MESH
6500
6501 int wpa_auth_pmksa_list_mesh(struct wpa_authenticator *wpa_auth, const u8 *addr,
6502 char *buf, size_t len)
6503 {
6504 if (!wpa_auth || !wpa_auth->pmksa)
6505 return 0;
6506
6507 return pmksa_cache_auth_list_mesh(wpa_auth->pmksa, addr, buf, len);
6508 }
6509
6510
6511 struct rsn_pmksa_cache_entry *
6512 wpa_auth_pmksa_create_entry(const u8 *aa, const u8 *spa, const u8 *pmk,
6513 size_t pmk_len, int akmp,
6514 const u8 *pmkid, int expiration)
6515 {
6516 struct rsn_pmksa_cache_entry *entry;
6517 struct os_reltime now;
6518
6519 entry = pmksa_cache_auth_create_entry(pmk, pmk_len, pmkid, NULL, 0, aa,
6520 spa, 0, NULL, akmp);
6521 if (!entry)
6522 return NULL;
6523
6524 os_get_reltime(&now);
6525 entry->expiration = now.sec + expiration;
6526 return entry;
6527 }
6528
6529
6530 int wpa_auth_pmksa_add_entry(struct wpa_authenticator *wpa_auth,
6531 struct rsn_pmksa_cache_entry *entry)
6532 {
6533 int ret;
6534
6535 if (!wpa_auth || !wpa_auth->pmksa)
6536 return -1;
6537
6538 ret = pmksa_cache_auth_add_entry(wpa_auth->pmksa, entry);
6539 if (ret < 0)
6540 wpa_printf(MSG_DEBUG,
6541 "RSN: Failed to store external PMKSA cache for "
6542 MACSTR, MAC2STR(entry->spa));
6543
6544 return ret;
6545 }
6546
6547 #endif /* CONFIG_MESH */
6548 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
6549
6550
6551 struct rsn_pmksa_cache *
6552 wpa_auth_get_pmksa_cache(struct wpa_authenticator *wpa_auth)
6553 {
6554 if (!wpa_auth || !wpa_auth->pmksa)
6555 return NULL;
6556 return wpa_auth->pmksa;
6557 }
6558
6559
6560 struct rsn_pmksa_cache_entry *
6561 wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
6562 const u8 *pmkid)
6563 {
6564 if (!wpa_auth || !wpa_auth->pmksa)
6565 return NULL;
6566 return pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, pmkid);
6567 }
6568
6569
6570 void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa,
6571 struct wpa_state_machine *sm,
6572 struct wpa_authenticator *wpa_auth,
6573 u8 *pmkid, u8 *pmk, size_t *pmk_len)
6574 {
6575 if (!sm)
6576 return;
6577
6578 sm->pmksa = pmksa;
6579 os_memcpy(pmk, pmksa->pmk, pmksa->pmk_len);
6580 *pmk_len = pmksa->pmk_len;
6581 os_memcpy(pmkid, pmksa->pmkid, PMKID_LEN);
6582 os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmksa->pmkid, PMKID_LEN);
6583 }
6584
6585
6586 /*
6587 * Remove and free the group from wpa_authenticator. This is triggered by a
6588 * callback to make sure nobody is currently iterating the group list while it
6589 * gets modified.
6590 */
6591 static void wpa_group_free(struct wpa_authenticator *wpa_auth,
6592 struct wpa_group *group)
6593 {
6594 struct wpa_group *prev = wpa_auth->group;
6595
6596 wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d",
6597 group->vlan_id);
6598
6599 while (prev) {
6600 if (prev->next == group) {
6601 /* This never frees the special first group as needed */
6602 prev->next = group->next;
6603 os_free(group);
6604 break;
6605 }
6606 prev = prev->next;
6607 }
6608
6609 }
6610
6611
6612 /* Increase the reference counter for group */
6613 static void wpa_group_get(struct wpa_authenticator *wpa_auth,
6614 struct wpa_group *group)
6615 {
6616 /* Skip the special first group */
6617 if (wpa_auth->group == group)
6618 return;
6619
6620 group->references++;
6621 }
6622
6623
6624 /* Decrease the reference counter and maybe free the group */
6625 static void wpa_group_put(struct wpa_authenticator *wpa_auth,
6626 struct wpa_group *group)
6627 {
6628 /* Skip the special first group */
6629 if (wpa_auth->group == group)
6630 return;
6631
6632 group->references--;
6633 if (group->references)
6634 return;
6635 wpa_group_free(wpa_auth, group);
6636 }
6637
6638
6639 /*
6640 * Add a group that has its references counter set to zero. Caller needs to
6641 * call wpa_group_get() on the return value to mark the entry in use.
6642 */
6643 static struct wpa_group *
6644 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
6645 {
6646 struct wpa_group *group;
6647
6648 if (!wpa_auth || !wpa_auth->group)
6649 return NULL;
6650
6651 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
6652 vlan_id);
6653 group = wpa_group_init(wpa_auth, vlan_id, 0);
6654 if (!group)
6655 return NULL;
6656
6657 group->next = wpa_auth->group->next;
6658 wpa_auth->group->next = group;
6659
6660 return group;
6661 }
6662
6663
6664 /*
6665 * Enforce that the group state machine for the VLAN is running, increase
6666 * reference counter as interface is up. References might have been increased
6667 * even if a negative value is returned.
6668 * Returns: -1 on error (group missing, group already failed); otherwise, 0
6669 */
6670 int wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id)
6671 {
6672 struct wpa_group *group;
6673
6674 if (!wpa_auth)
6675 return 0;
6676
6677 group = wpa_auth->group;
6678 while (group) {
6679 if (group->vlan_id == vlan_id)
6680 break;
6681 group = group->next;
6682 }
6683
6684 if (!group) {
6685 group = wpa_auth_add_group(wpa_auth, vlan_id);
6686 if (!group)
6687 return -1;
6688 }
6689
6690 wpa_printf(MSG_DEBUG,
6691 "WPA: Ensure group state machine running for VLAN ID %d",
6692 vlan_id);
6693
6694 wpa_group_get(wpa_auth, group);
6695 group->num_setup_iface++;
6696
6697 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
6698 return -1;
6699
6700 return 0;
6701 }
6702
6703
6704 /*
6705 * Decrease reference counter, expected to be zero afterwards.
6706 * returns: -1 on error (group not found, group in fail state)
6707 * -2 if wpa_group is still referenced
6708 * 0 else
6709 */
6710 int wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id)
6711 {
6712 struct wpa_group *group;
6713 int ret = 0;
6714
6715 if (!wpa_auth)
6716 return 0;
6717
6718 group = wpa_auth->group;
6719 while (group) {
6720 if (group->vlan_id == vlan_id)
6721 break;
6722 group = group->next;
6723 }
6724
6725 if (!group)
6726 return -1;
6727
6728 wpa_printf(MSG_DEBUG,
6729 "WPA: Try stopping group state machine for VLAN ID %d",
6730 vlan_id);
6731
6732 if (group->num_setup_iface <= 0) {
6733 wpa_printf(MSG_ERROR,
6734 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
6735 vlan_id);
6736 return -1;
6737 }
6738 group->num_setup_iface--;
6739
6740 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
6741 ret = -1;
6742
6743 if (group->references > 1) {
6744 wpa_printf(MSG_DEBUG,
6745 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
6746 vlan_id);
6747 ret = -2;
6748 }
6749
6750 wpa_group_put(wpa_auth, group);
6751
6752 return ret;
6753 }
6754
6755
6756 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
6757 {
6758 struct wpa_group *group;
6759
6760 if (!sm || !sm->wpa_auth)
6761 return 0;
6762
6763 group = sm->wpa_auth->group;
6764 while (group) {
6765 if (group->vlan_id == vlan_id)
6766 break;
6767 group = group->next;
6768 }
6769
6770 if (!group) {
6771 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
6772 if (!group)
6773 return -1;
6774 }
6775
6776 if (sm->group == group)
6777 return 0;
6778
6779 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
6780 return -1;
6781
6782 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR
6783 " to use group state machine for VLAN ID %d",
6784 MAC2STR(wpa_auth_get_spa(sm)), vlan_id);
6785
6786 wpa_group_get(sm->wpa_auth, group);
6787 wpa_group_put(sm->wpa_auth, sm->group);
6788 sm->group = group;
6789
6790 return 0;
6791 }
6792
6793
6794 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
6795 struct wpa_state_machine *sm, int ack)
6796 {
6797 if (!wpa_auth || !sm)
6798 return;
6799 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
6800 " ack=%d", MAC2STR(wpa_auth_get_spa(sm)), ack);
6801 if (sm->pending_1_of_4_timeout && ack) {
6802 /*
6803 * Some deployed supplicant implementations update their SNonce
6804 * for each EAPOL-Key 2/4 message even within the same 4-way
6805 * handshake and then fail to use the first SNonce when
6806 * deriving the PTK. This results in unsuccessful 4-way
6807 * handshake whenever the relatively short initial timeout is
6808 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
6809 * around this by increasing the timeout now that we know that
6810 * the station has received the frame.
6811 */
6812 int timeout_ms = eapol_key_timeout_subseq;
6813 wpa_printf(MSG_DEBUG,
6814 "WPA: Increase initial EAPOL-Key 1/4 timeout by %u ms because of acknowledged frame",
6815 timeout_ms);
6816 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
6817 eloop_register_timeout(timeout_ms / 1000,
6818 (timeout_ms % 1000) * 1000,
6819 wpa_send_eapol_timeout, wpa_auth, sm);
6820 }
6821
6822 #ifdef CONFIG_TESTING_OPTIONS
6823 if (sm->eapol_status_cb) {
6824 sm->eapol_status_cb(sm->eapol_status_cb_ctx1,
6825 sm->eapol_status_cb_ctx2);
6826 sm->eapol_status_cb = NULL;
6827 }
6828 #endif /* CONFIG_TESTING_OPTIONS */
6829 }
6830
6831
6832 int wpa_auth_uses_sae(struct wpa_state_machine *sm)
6833 {
6834 if (!sm)
6835 return 0;
6836 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
6837 }
6838
6839
6840 int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
6841 {
6842 if (!sm)
6843 return 0;
6844 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE ||
6845 sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY;
6846 }
6847
6848
6849 #ifdef CONFIG_P2P
6850 int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
6851 {
6852 if (!sm || WPA_GET_BE32(sm->ip_addr) == 0)
6853 return -1;
6854 os_memcpy(addr, sm->ip_addr, 4);
6855 return 0;
6856 }
6857 #endif /* CONFIG_P2P */
6858
6859
6860 int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
6861 struct radius_das_attrs *attr)
6862 {
6863 return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
6864 }
6865
6866
6867 void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)
6868 {
6869 struct wpa_group *group;
6870
6871 if (!wpa_auth)
6872 return;
6873 for (group = wpa_auth->group; group; group = group->next)
6874 wpa_group_config_group_keys(wpa_auth, group);
6875 }
6876
6877
6878 #ifdef CONFIG_FILS
6879
6880 struct wpa_auth_fils_iter_data {
6881 struct wpa_authenticator *auth;
6882 const u8 *cache_id;
6883 struct rsn_pmksa_cache_entry *pmksa;
6884 const u8 *spa;
6885 const u8 *pmkid;
6886 };
6887
6888
6889 static int wpa_auth_fils_iter(struct wpa_authenticator *a, void *ctx)
6890 {
6891 struct wpa_auth_fils_iter_data *data = ctx;
6892
6893 if (a == data->auth || !a->conf.fils_cache_id_set ||
6894 os_memcmp(a->conf.fils_cache_id, data->cache_id,
6895 FILS_CACHE_ID_LEN) != 0)
6896 return 0;
6897 data->pmksa = pmksa_cache_auth_get(a->pmksa, data->spa, data->pmkid);
6898 return data->pmksa != NULL;
6899 }
6900
6901
6902 struct rsn_pmksa_cache_entry *
6903 wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator *wpa_auth,
6904 const u8 *sta_addr, const u8 *pmkid)
6905 {
6906 struct wpa_auth_fils_iter_data idata;
6907
6908 if (!wpa_auth->conf.fils_cache_id_set)
6909 return NULL;
6910 idata.auth = wpa_auth;
6911 idata.cache_id = wpa_auth->conf.fils_cache_id;
6912 idata.pmksa = NULL;
6913 idata.spa = sta_addr;
6914 idata.pmkid = pmkid;
6915 wpa_auth_for_each_auth(wpa_auth, wpa_auth_fils_iter, &idata);
6916 return idata.pmksa;
6917 }
6918
6919
6920 #ifdef CONFIG_IEEE80211R_AP
6921 int wpa_auth_write_fte(struct wpa_authenticator *wpa_auth,
6922 struct wpa_state_machine *sm,
6923 u8 *buf, size_t len)
6924 {
6925 struct wpa_auth_config *conf = &wpa_auth->conf;
6926
6927 return wpa_write_ftie(conf, sm->wpa_key_mgmt, sm->xxkey_len,
6928 conf->r0_key_holder, conf->r0_key_holder_len,
6929 NULL, NULL, buf, len, NULL, 0, 0);
6930 }
6931 #endif /* CONFIG_IEEE80211R_AP */
6932
6933
6934 void wpa_auth_get_fils_aead_params(struct wpa_state_machine *sm,
6935 u8 *fils_anonce, u8 *fils_snonce,
6936 u8 *fils_kek, size_t *fils_kek_len)
6937 {
6938 os_memcpy(fils_anonce, sm->ANonce, WPA_NONCE_LEN);
6939 os_memcpy(fils_snonce, sm->SNonce, WPA_NONCE_LEN);
6940 os_memcpy(fils_kek, sm->PTK.kek, WPA_KEK_MAX_LEN);
6941 *fils_kek_len = sm->PTK.kek_len;
6942 }
6943
6944
6945 void wpa_auth_add_fils_pmk_pmkid(struct wpa_state_machine *sm, const u8 *pmk,
6946 size_t pmk_len, const u8 *pmkid)
6947 {
6948 os_memcpy(sm->PMK, pmk, pmk_len);
6949 sm->pmk_len = pmk_len;
6950 os_memcpy(sm->pmkid, pmkid, PMKID_LEN);
6951 sm->pmkid_set = 1;
6952 }
6953
6954 #endif /* CONFIG_FILS */
6955
6956
6957 void wpa_auth_set_auth_alg(struct wpa_state_machine *sm, u16 auth_alg)
6958 {
6959 if (sm)
6960 sm->auth_alg = auth_alg;
6961 }
6962
6963
6964 void wpa_auth_set_rsn_selection(struct wpa_state_machine *sm, const u8 *ie,
6965 size_t len)
6966 {
6967 if (!sm)
6968 return;
6969 os_free(sm->rsn_selection);
6970 sm->rsn_selection = NULL;
6971 sm->rsn_selection_len = 0;
6972 sm->rsn_override = false;
6973 sm->rsn_override_2 = false;
6974 if (ie) {
6975 if (len >= 1) {
6976 if (ie[0] == RSN_SELECTION_RSNE_OVERRIDE)
6977 sm->rsn_override = true;
6978 else if (ie[0] == RSN_SELECTION_RSNE_OVERRIDE_2)
6979 sm->rsn_override_2 = true;
6980 }
6981 sm->rsn_selection = os_memdup(ie, len);
6982 if (sm->rsn_selection)
6983 sm->rsn_selection_len = len;
6984 }
6985 }
6986
6987
6988 #ifdef CONFIG_DPP2
6989 void wpa_auth_set_dpp_z(struct wpa_state_machine *sm, const struct wpabuf *z)
6990 {
6991 if (sm) {
6992 wpabuf_clear_free(sm->dpp_z);
6993 sm->dpp_z = z ? wpabuf_dup(z) : NULL;
6994 }
6995 }
6996 #endif /* CONFIG_DPP2 */
6997
6998
6999 void wpa_auth_set_ssid_protection(struct wpa_state_machine *sm, bool val)
7000 {
7001 if (sm)
7002 sm->ssid_protection = val;
7003 }
7004
7005
7006 void wpa_auth_set_transition_disable(struct wpa_authenticator *wpa_auth,
7007 u8 val)
7008 {
7009 if (wpa_auth)
7010 wpa_auth->conf.transition_disable = val;
7011 }
7012
7013
7014 #ifdef CONFIG_TESTING_OPTIONS
7015
7016 int wpa_auth_resend_m1(struct wpa_state_machine *sm, int change_anonce,
7017 void (*cb)(void *ctx1, void *ctx2),
7018 void *ctx1, void *ctx2)
7019 {
7020 const u8 *anonce = sm->ANonce;
7021 u8 anonce_buf[WPA_NONCE_LEN];
7022
7023 if (change_anonce) {
7024 if (random_get_bytes(anonce_buf, WPA_NONCE_LEN))
7025 return -1;
7026 anonce = anonce_buf;
7027 }
7028
7029 wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
7030 "sending 1/4 msg of 4-Way Handshake (TESTING)");
7031 wpa_send_eapol(sm->wpa_auth, sm,
7032 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
7033 anonce, NULL, 0, 0, 0);
7034 return 0;
7035 }
7036
7037
7038 int wpa_auth_resend_m3(struct wpa_state_machine *sm,
7039 void (*cb)(void *ctx1, void *ctx2),
7040 void *ctx1, void *ctx2)
7041 {
7042 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
7043 u8 *opos;
7044 size_t gtk_len, kde_len;
7045 struct wpa_auth_config *conf = &sm->wpa_auth->conf;
7046 struct wpa_group *gsm = sm->group;
7047 u8 *wpa_ie;
7048 int wpa_ie_len, secure, gtkidx, encr = 0;
7049 u8 hdr[2];
7050
7051 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
7052 GTK[GN], IGTK, [BIGTK], [FTIE], [TIE * 2])
7053 */
7054
7055 /* Use 0 RSC */
7056 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
7057 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
7058 wpa_ie = sm->wpa_auth->wpa_ie;
7059 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
7060 if (sm->wpa == WPA_VERSION_WPA &&
7061 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
7062 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
7063 /* WPA-only STA, remove RSN IE and possible MDIE */
7064 wpa_ie = wpa_ie + wpa_ie[1] + 2;
7065 if (wpa_ie[0] == WLAN_EID_RSNX)
7066 wpa_ie = wpa_ie + wpa_ie[1] + 2;
7067 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
7068 wpa_ie = wpa_ie + wpa_ie[1] + 2;
7069 wpa_ie_len = wpa_ie[1] + 2;
7070 }
7071 wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
7072 "sending 3/4 msg of 4-Way Handshake (TESTING)");
7073 if (sm->wpa == WPA_VERSION_WPA2) {
7074 /* WPA2 send GTK in the 4-way handshake */
7075 secure = 1;
7076 gtk = gsm->GTK[gsm->GN - 1];
7077 gtk_len = gsm->GTK_len;
7078 gtkidx = gsm->GN;
7079 _rsc = rsc;
7080 encr = 1;
7081 } else {
7082 /* WPA does not include GTK in msg 3/4 */
7083 secure = 0;
7084 gtk = NULL;
7085 gtk_len = 0;
7086 _rsc = NULL;
7087 if (sm->rx_eapol_key_secure) {
7088 /*
7089 * It looks like Windows 7 supplicant tries to use
7090 * Secure bit in msg 2/4 after having reported Michael
7091 * MIC failure and it then rejects the 4-way handshake
7092 * if msg 3/4 does not set Secure bit. Work around this
7093 * by setting the Secure bit here even in the case of
7094 * WPA if the supplicant used it first.
7095 */
7096 wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm),
7097 LOGGER_DEBUG,
7098 "STA used Secure bit in WPA msg 2/4 - set Secure for 3/4 as workaround");
7099 secure = 1;
7100 }
7101 }
7102
7103 kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
7104
7105 if (sm->use_ext_key_id)
7106 kde_len += 2 + RSN_SELECTOR_LEN + 2;
7107
7108 if (gtk)
7109 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
7110 #ifdef CONFIG_IEEE80211R_AP
7111 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
7112 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
7113 kde_len += 300; /* FTIE + 2 * TIE */
7114 }
7115 #endif /* CONFIG_IEEE80211R_AP */
7116 kde = os_malloc(kde_len);
7117 if (!kde)
7118 return -1;
7119
7120 pos = kde;
7121 os_memcpy(pos, wpa_ie, wpa_ie_len);
7122 pos += wpa_ie_len;
7123 #ifdef CONFIG_IEEE80211R_AP
7124 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
7125 int res;
7126 size_t elen;
7127
7128 elen = pos - kde;
7129 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name, true);
7130 if (res < 0) {
7131 wpa_printf(MSG_ERROR,
7132 "FT: Failed to insert PMKR1Name into RSN IE in EAPOL-Key data");
7133 os_free(kde);
7134 return -1;
7135 }
7136 pos -= wpa_ie_len;
7137 pos += elen;
7138 }
7139 #endif /* CONFIG_IEEE80211R_AP */
7140 hdr[1] = 0;
7141
7142 if (sm->use_ext_key_id) {
7143 hdr[0] = sm->keyidx_active & 0x01;
7144 pos = wpa_add_kde(pos, RSN_KEY_DATA_KEYID, hdr, 2, NULL, 0);
7145 }
7146
7147 if (gtk) {
7148 hdr[0] = gtkidx & 0x03;
7149 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
7150 gtk, gtk_len);
7151 }
7152 opos = pos;
7153 pos = ieee80211w_kde_add(sm, pos);
7154 if (pos - opos >= 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
7155 /* skip KDE header and keyid */
7156 opos += 2 + RSN_SELECTOR_LEN + 2;
7157 os_memset(opos, 0, 6); /* clear PN */
7158 }
7159 if (ocv_oci_add(sm, &pos, conf->oci_freq_override_eapol_m3) < 0) {
7160 os_free(kde);
7161 return -1;
7162 }
7163
7164 #ifdef CONFIG_IEEE80211R_AP
7165 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
7166 int res;
7167
7168 if (sm->assoc_resp_ftie &&
7169 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
7170 os_memcpy(pos, sm->assoc_resp_ftie,
7171 2 + sm->assoc_resp_ftie[1]);
7172 res = 2 + sm->assoc_resp_ftie[1];
7173 } else {
7174 res = wpa_write_ftie(conf, sm->wpa_key_mgmt,
7175 sm->xxkey_len,
7176 conf->r0_key_holder,
7177 conf->r0_key_holder_len,
7178 NULL, NULL, pos,
7179 kde + kde_len - pos,
7180 NULL, 0, 0);
7181 }
7182 if (res < 0) {
7183 wpa_printf(MSG_ERROR,
7184 "FT: Failed to insert FTIE into EAPOL-Key Key Data");
7185 os_free(kde);
7186 return -1;
7187 }
7188 pos += res;
7189
7190 /* TIE[ReassociationDeadline] (TU) */
7191 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
7192 *pos++ = 5;
7193 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
7194 WPA_PUT_LE32(pos, conf->reassociation_deadline);
7195 pos += 4;
7196
7197 /* TIE[KeyLifetime] (seconds) */
7198 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
7199 *pos++ = 5;
7200 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
7201 WPA_PUT_LE32(pos, conf->r0_key_lifetime);
7202 pos += 4;
7203 }
7204 #endif /* CONFIG_IEEE80211R_AP */
7205
7206 wpa_send_eapol(sm->wpa_auth, sm,
7207 (secure ? WPA_KEY_INFO_SECURE : 0) |
7208 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
7209 WPA_KEY_INFO_MIC : 0) |
7210 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
7211 WPA_KEY_INFO_KEY_TYPE,
7212 _rsc, sm->ANonce, kde, pos - kde, 0, encr);
7213 bin_clear_free(kde, kde_len);
7214 return 0;
7215 }
7216
7217
7218 int wpa_auth_resend_group_m1(struct wpa_state_machine *sm,
7219 void (*cb)(void *ctx1, void *ctx2),
7220 void *ctx1, void *ctx2)
7221 {
7222 u8 rsc[WPA_KEY_RSC_LEN];
7223 struct wpa_auth_config *conf = &sm->wpa_auth->conf;
7224 struct wpa_group *gsm = sm->group;
7225 const u8 *kde;
7226 u8 *kde_buf = NULL, *pos, hdr[2];
7227 u8 *opos;
7228 size_t kde_len;
7229 u8 *gtk;
7230
7231 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
7232 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
7233 /* Use 0 RSC */
7234 wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
7235 "sending 1/2 msg of Group Key Handshake (TESTING)");
7236
7237 gtk = gsm->GTK[gsm->GN - 1];
7238 if (sm->wpa == WPA_VERSION_WPA2) {
7239 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
7240 ieee80211w_kde_len(sm) + ocv_oci_len(sm);
7241 kde_buf = os_malloc(kde_len);
7242 if (!kde_buf)
7243 return -1;
7244
7245 kde = pos = kde_buf;
7246 hdr[0] = gsm->GN & 0x03;
7247 hdr[1] = 0;
7248 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
7249 gtk, gsm->GTK_len);
7250 opos = pos;
7251 pos = ieee80211w_kde_add(sm, pos);
7252 if (pos - opos >=
7253 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
7254 /* skip KDE header and keyid */
7255 opos += 2 + RSN_SELECTOR_LEN + 2;
7256 os_memset(opos, 0, 6); /* clear PN */
7257 }
7258 if (ocv_oci_add(sm, &pos,
7259 conf->oci_freq_override_eapol_g1) < 0) {
7260 os_free(kde_buf);
7261 return -1;
7262 }
7263 kde_len = pos - kde;
7264 } else {
7265 kde = gtk;
7266 kde_len = gsm->GTK_len;
7267 }
7268
7269 sm->eapol_status_cb = cb;
7270 sm->eapol_status_cb_ctx1 = ctx1;
7271 sm->eapol_status_cb_ctx2 = ctx2;
7272
7273 wpa_send_eapol(sm->wpa_auth, sm,
7274 WPA_KEY_INFO_SECURE |
7275 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
7276 WPA_KEY_INFO_MIC : 0) |
7277 WPA_KEY_INFO_ACK |
7278 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
7279 rsc, NULL, kde, kde_len, gsm->GN, 1);
7280
7281 bin_clear_free(kde_buf, kde_len);
7282 return 0;
7283 }
7284
7285
7286 int wpa_auth_rekey_gtk(struct wpa_authenticator *wpa_auth)
7287 {
7288 if (!wpa_auth)
7289 return -1;
7290 eloop_cancel_timeout(wpa_rekey_gtk,
7291 wpa_get_primary_auth(wpa_auth), NULL);
7292 return eloop_register_timeout(0, 0, wpa_rekey_gtk,
7293 wpa_get_primary_auth(wpa_auth), NULL);
7294 }
7295
7296
7297 int wpa_auth_rekey_ptk(struct wpa_authenticator *wpa_auth,
7298 struct wpa_state_machine *sm)
7299 {
7300 if (!wpa_auth || !sm)
7301 return -1;
7302 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
7303 wpa_request_new_ptk(sm);
7304 wpa_sm_step(sm);
7305 return 0;
7306 }
7307
7308
7309 void wpa_auth_set_ft_rsnxe_used(struct wpa_authenticator *wpa_auth, int val)
7310 {
7311 if (wpa_auth)
7312 wpa_auth->conf.ft_rsnxe_used = val;
7313 }
7314
7315
7316 void wpa_auth_set_ocv_override_freq(struct wpa_authenticator *wpa_auth,
7317 enum wpa_auth_ocv_override_frame frame,
7318 unsigned int freq)
7319 {
7320 if (!wpa_auth)
7321 return;
7322 switch (frame) {
7323 case WPA_AUTH_OCV_OVERRIDE_EAPOL_M3:
7324 wpa_auth->conf.oci_freq_override_eapol_m3 = freq;
7325 break;
7326 case WPA_AUTH_OCV_OVERRIDE_EAPOL_G1:
7327 wpa_auth->conf.oci_freq_override_eapol_g1 = freq;
7328 break;
7329 case WPA_AUTH_OCV_OVERRIDE_FT_ASSOC:
7330 wpa_auth->conf.oci_freq_override_ft_assoc = freq;
7331 break;
7332 case WPA_AUTH_OCV_OVERRIDE_FILS_ASSOC:
7333 wpa_auth->conf.oci_freq_override_fils_assoc = freq;
7334 break;
7335 }
7336 }
7337
7338 #endif /* CONFIG_TESTING_OPTIONS */
7339
7340
7341 void wpa_auth_sta_radius_psk_resp(struct wpa_state_machine *sm, bool success)
7342 {
7343 if (!sm->waiting_radius_psk) {
7344 wpa_printf(MSG_DEBUG,
7345 "Ignore RADIUS PSK response for " MACSTR
7346 " that did not wait one",
7347 MAC2STR(sm->addr));
7348 return;
7349 }
7350
7351 wpa_printf(MSG_DEBUG, "RADIUS PSK response for " MACSTR " (%s)",
7352 MAC2STR(sm->addr), success ? "success" : "fail");
7353 sm->waiting_radius_psk = 0;
7354
7355 if (success) {
7356 /* Try to process the EAPOL-Key msg 2/4 again */
7357 sm->EAPOLKeyReceived = true;
7358 } else {
7359 sm->Disconnect = true;
7360 }
7361
7362 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
7363 }
7364
7365
7366 void wpa_auth_set_ml_info(struct wpa_state_machine *sm,
7367 u8 mld_assoc_link_id, struct mld_info *info)
7368 {
7369 #ifdef CONFIG_IEEE80211BE
7370 unsigned int link_id;
7371
7372 if (!info)
7373 return;
7374
7375 os_memset(sm->mld_links, 0, sizeof(sm->mld_links));
7376 sm->n_mld_affiliated_links = 0;
7377
7378 wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
7379 "MLD: Initialization");
7380
7381 os_memcpy(sm->peer_mld_addr, info->common_info.mld_addr, ETH_ALEN);
7382
7383 sm->mld_assoc_link_id = mld_assoc_link_id;
7384
7385 for (link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
7386 struct mld_link_info *link = &info->links[link_id];
7387 struct mld_link *sm_link = &sm->mld_links[link_id];
7388 struct wpa_get_link_auth_ctx ctx;
7389
7390 sm_link->valid = link->valid;
7391 if (!link->valid)
7392 continue;
7393
7394 os_memcpy(sm_link->peer_addr, link->peer_addr, ETH_ALEN);
7395
7396 wpa_printf(MSG_DEBUG,
7397 "WPA_AUTH: MLD: id=%u, peer=" MACSTR,
7398 link_id,
7399 MAC2STR(sm_link->peer_addr));
7400
7401 if (link_id != mld_assoc_link_id) {
7402 sm->n_mld_affiliated_links++;
7403 ctx.addr = link->local_addr;
7404 ctx.mld_addr = NULL;
7405 ctx.link_id = -1;
7406 ctx.wpa_auth = NULL;
7407 wpa_auth_for_each_auth(sm->wpa_auth,
7408 wpa_get_link_sta_auth, &ctx);
7409 if (ctx.wpa_auth) {
7410 sm_link->wpa_auth = ctx.wpa_auth;
7411 wpa_group_get(sm_link->wpa_auth,
7412 sm_link->wpa_auth->group);
7413 }
7414 } else {
7415 sm_link->wpa_auth = sm->wpa_auth;
7416 }
7417
7418 if (!sm_link->wpa_auth)
7419 wpa_printf(MSG_ERROR,
7420 "Unable to find authenticator object for ML STA "
7421 MACSTR " on link id %d",
7422 MAC2STR(sm->wpa_auth->mld_addr),
7423 link_id);
7424 }
7425 #endif /* CONFIG_IEEE80211BE */
7426 }
7427