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