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