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