blob: 3021a8c9cad0a674b556468ec3298ebd223ad324 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002 * IEEE 802.11 RSN / WPA Authenticator
Roshan Pius3a1667e2018-07-03 15:17:14 -07003 * Copyright (c) 2004-2018, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "utils/state_machine.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080014#include "utils/bitfield.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "common/ieee802_11_defs.h"
Hai Shalom74f70d42019-02-11 14:42:39 -080016#include "common/ocv.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080017#include "crypto/aes.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "crypto/aes_wrap.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080019#include "crypto/aes_siv.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#include "crypto/crypto.h"
21#include "crypto/sha1.h"
22#include "crypto/sha256.h"
Roshan Pius3a1667e2018-07-03 15:17:14 -070023#include "crypto/sha384.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "crypto/random.h"
25#include "eapol_auth/eapol_auth_sm.h"
Hai Shalom74f70d42019-02-11 14:42:39 -080026#include "drivers/driver.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "ap_config.h"
28#include "ieee802_11.h"
29#include "wpa_auth.h"
30#include "pmksa_cache_auth.h"
31#include "wpa_auth_i.h"
32#include "wpa_auth_ie.h"
33
34#define STATE_MACHINE_DATA struct wpa_state_machine
35#define STATE_MACHINE_DEBUG_PREFIX "WPA"
36#define STATE_MACHINE_ADDR sm->addr
37
38
39static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
40static int wpa_sm_step(struct wpa_state_machine *sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070041static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK,
42 u8 *data, size_t data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080043#ifdef CONFIG_FILS
44static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
45 u8 *buf, size_t buf_len, u16 *_key_data_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070046static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm,
47 const struct wpabuf *hlp);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080048#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
50static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
51 struct wpa_group *group);
52static void wpa_request_new_ptk(struct wpa_state_machine *sm);
53static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
54 struct wpa_group *group);
55static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
56 struct wpa_group *group);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080057static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080058 const u8 *pmk, unsigned int pmk_len,
59 struct wpa_ptk *ptk);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070060static void wpa_group_free(struct wpa_authenticator *wpa_auth,
61 struct wpa_group *group);
62static void wpa_group_get(struct wpa_authenticator *wpa_auth,
63 struct wpa_group *group);
64static void wpa_group_put(struct wpa_authenticator *wpa_auth,
65 struct wpa_group *group);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080066static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070067
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070068static const u32 eapol_key_timeout_first = 100; /* ms */
69static const u32 eapol_key_timeout_subseq = 1000; /* ms */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080070static const u32 eapol_key_timeout_first_group = 500; /* ms */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070071static const u32 eapol_key_timeout_no_retrans = 4000; /* ms */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072
73/* TODO: make these configurable */
74static const int dot11RSNAConfigPMKLifetime = 43200;
75static const int dot11RSNAConfigPMKReauthThreshold = 70;
76static const int dot11RSNAConfigSATimeout = 60;
77
78
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080079static inline int wpa_auth_mic_failure_report(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070080 struct wpa_authenticator *wpa_auth, const u8 *addr)
81{
Paul Stewart092955c2017-02-06 09:13:09 -080082 if (wpa_auth->cb->mic_failure_report)
83 return wpa_auth->cb->mic_failure_report(wpa_auth->cb_ctx, addr);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080084 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070085}
86
87
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070088static inline void wpa_auth_psk_failure_report(
89 struct wpa_authenticator *wpa_auth, const u8 *addr)
90{
Paul Stewart092955c2017-02-06 09:13:09 -080091 if (wpa_auth->cb->psk_failure_report)
92 wpa_auth->cb->psk_failure_report(wpa_auth->cb_ctx, addr);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070093}
94
95
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070096static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
97 const u8 *addr, wpa_eapol_variable var,
98 int value)
99{
Paul Stewart092955c2017-02-06 09:13:09 -0800100 if (wpa_auth->cb->set_eapol)
101 wpa_auth->cb->set_eapol(wpa_auth->cb_ctx, addr, var, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102}
103
104
105static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
106 const u8 *addr, wpa_eapol_variable var)
107{
Paul Stewart092955c2017-02-06 09:13:09 -0800108 if (wpa_auth->cb->get_eapol == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800110 return wpa_auth->cb->get_eapol(wpa_auth->cb_ctx, addr, var);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111}
112
113
114static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700115 const u8 *addr,
116 const u8 *p2p_dev_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700117 const u8 *prev_psk, size_t *psk_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700118{
Paul Stewart092955c2017-02-06 09:13:09 -0800119 if (wpa_auth->cb->get_psk == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700120 return NULL;
Paul Stewart092955c2017-02-06 09:13:09 -0800121 return wpa_auth->cb->get_psk(wpa_auth->cb_ctx, addr, p2p_dev_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700122 prev_psk, psk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700123}
124
125
126static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
127 const u8 *addr, u8 *msk, size_t *len)
128{
Paul Stewart092955c2017-02-06 09:13:09 -0800129 if (wpa_auth->cb->get_msk == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800131 return wpa_auth->cb->get_msk(wpa_auth->cb_ctx, addr, msk, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700132}
133
134
135static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
136 int vlan_id,
137 enum wpa_alg alg, const u8 *addr, int idx,
138 u8 *key, size_t key_len)
139{
Paul Stewart092955c2017-02-06 09:13:09 -0800140 if (wpa_auth->cb->set_key == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800142 return wpa_auth->cb->set_key(wpa_auth->cb_ctx, vlan_id, alg, addr, idx,
143 key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700144}
145
146
147static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
148 const u8 *addr, int idx, u8 *seq)
149{
Paul Stewart092955c2017-02-06 09:13:09 -0800150 if (wpa_auth->cb->get_seqnum == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700151 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800152 return wpa_auth->cb->get_seqnum(wpa_auth->cb_ctx, addr, idx, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153}
154
155
156static inline int
157wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
158 const u8 *data, size_t data_len, int encrypt)
159{
Paul Stewart092955c2017-02-06 09:13:09 -0800160 if (wpa_auth->cb->send_eapol == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800162 return wpa_auth->cb->send_eapol(wpa_auth->cb_ctx, addr, data, data_len,
163 encrypt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700164}
165
166
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800167#ifdef CONFIG_MESH
168static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth,
169 const u8 *addr)
170{
Paul Stewart092955c2017-02-06 09:13:09 -0800171 if (wpa_auth->cb->start_ampe == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800172 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800173 return wpa_auth->cb->start_ampe(wpa_auth->cb_ctx, addr);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800174}
175#endif /* CONFIG_MESH */
176
177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700178int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
179 int (*cb)(struct wpa_state_machine *sm, void *ctx),
180 void *cb_ctx)
181{
Paul Stewart092955c2017-02-06 09:13:09 -0800182 if (wpa_auth->cb->for_each_sta == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183 return 0;
Paul Stewart092955c2017-02-06 09:13:09 -0800184 return wpa_auth->cb->for_each_sta(wpa_auth->cb_ctx, cb, cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700185}
186
187
188int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
189 int (*cb)(struct wpa_authenticator *a, void *ctx),
190 void *cb_ctx)
191{
Paul Stewart092955c2017-02-06 09:13:09 -0800192 if (wpa_auth->cb->for_each_auth == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700193 return 0;
Paul Stewart092955c2017-02-06 09:13:09 -0800194 return wpa_auth->cb->for_each_auth(wpa_auth->cb_ctx, cb, cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700195}
196
197
198void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
199 logger_level level, const char *txt)
200{
Paul Stewart092955c2017-02-06 09:13:09 -0800201 if (wpa_auth->cb->logger == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202 return;
Paul Stewart092955c2017-02-06 09:13:09 -0800203 wpa_auth->cb->logger(wpa_auth->cb_ctx, addr, level, txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204}
205
206
207void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
208 logger_level level, const char *fmt, ...)
209{
210 char *format;
211 int maxlen;
212 va_list ap;
213
Paul Stewart092955c2017-02-06 09:13:09 -0800214 if (wpa_auth->cb->logger == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 return;
216
217 maxlen = os_strlen(fmt) + 100;
218 format = os_malloc(maxlen);
219 if (!format)
220 return;
221
222 va_start(ap, fmt);
223 vsnprintf(format, maxlen, fmt, ap);
224 va_end(ap);
225
226 wpa_auth_logger(wpa_auth, addr, level, format);
227
228 os_free(format);
229}
230
231
232static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700233 const u8 *addr, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234{
Paul Stewart092955c2017-02-06 09:13:09 -0800235 if (wpa_auth->cb->disconnect == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236 return;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700237 wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR " (reason %u)",
238 MAC2STR(addr), reason);
239 wpa_auth->cb->disconnect(wpa_auth->cb_ctx, addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240}
241
242
Hai Shalom74f70d42019-02-11 14:42:39 -0800243#ifdef CONFIG_OCV
244static int wpa_channel_info(struct wpa_authenticator *wpa_auth,
245 struct wpa_channel_info *ci)
246{
247 if (!wpa_auth->cb->channel_info)
248 return -1;
249 return wpa_auth->cb->channel_info(wpa_auth->cb_ctx, ci);
250}
251#endif /* CONFIG_OCV */
252
253
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
255{
256 struct wpa_authenticator *wpa_auth = eloop_ctx;
257
258 if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
259 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
260 "initialization.");
261 } else {
262 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
263 wpa_hexdump_key(MSG_DEBUG, "GMK",
264 wpa_auth->group->GMK, WPA_GMK_LEN);
265 }
266
267 if (wpa_auth->conf.wpa_gmk_rekey) {
268 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
269 wpa_rekey_gmk, wpa_auth, NULL);
270 }
271}
272
273
274static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
275{
276 struct wpa_authenticator *wpa_auth = eloop_ctx;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700277 struct wpa_group *group, *next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700278
279 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700280 group = wpa_auth->group;
281 while (group) {
282 wpa_group_get(wpa_auth, group);
283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 group->GTKReKey = TRUE;
285 do {
286 group->changed = FALSE;
287 wpa_group_sm_step(wpa_auth, group);
288 } while (group->changed);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700289
290 next = group->next;
291 wpa_group_put(wpa_auth, group);
292 group = next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 }
294
295 if (wpa_auth->conf.wpa_group_rekey) {
296 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
297 0, wpa_rekey_gtk, wpa_auth, NULL);
298 }
299}
300
301
302static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
303{
304 struct wpa_authenticator *wpa_auth = eloop_ctx;
305 struct wpa_state_machine *sm = timeout_ctx;
306
307 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
308 wpa_request_new_ptk(sm);
309 wpa_sm_step(sm);
310}
311
312
313static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
314{
315 if (sm->pmksa == ctx)
316 sm->pmksa = NULL;
317 return 0;
318}
319
320
321static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
322 void *ctx)
323{
324 struct wpa_authenticator *wpa_auth = ctx;
325 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
326}
327
328
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
330 struct wpa_group *group)
331{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800332 u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333 u8 rkey[32];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800334 unsigned long ptr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335
336 if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
337 return -1;
338 wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
339
340 /*
341 * Counter = PRF-256(Random number, "Init Counter",
342 * Local MAC Address || Time)
343 */
344 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
345 wpa_get_ntp_timestamp(buf + ETH_ALEN);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800346 ptr = (unsigned long) group;
347 os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
Hai Shalom74f70d42019-02-11 14:42:39 -0800348#ifdef TEST_FUZZ
349 os_memset(buf + ETH_ALEN, 0xab, 8);
350 os_memset(buf + ETH_ALEN + 8, 0xcd, sizeof(ptr));
351#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352 if (random_get_bytes(rkey, sizeof(rkey)) < 0)
353 return -1;
354
355 if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
356 group->Counter, WPA_NONCE_LEN) < 0)
357 return -1;
358 wpa_hexdump_key(MSG_DEBUG, "Key Counter",
359 group->Counter, WPA_NONCE_LEN);
360
361 return 0;
362}
363
364
365static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800366 int vlan_id, int delay_init)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700367{
368 struct wpa_group *group;
369
370 group = os_zalloc(sizeof(struct wpa_group));
371 if (group == NULL)
372 return NULL;
373
374 group->GTKAuthenticator = TRUE;
375 group->vlan_id = vlan_id;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700376 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700377
378 if (random_pool_ready() != 1) {
379 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
380 "for secure operations - update keys later when "
381 "the first station connects");
382 }
383
384 /*
385 * Set initial GMK/Counter value here. The actual values that will be
386 * used in negotiations will be set once the first station tries to
387 * connect. This allows more time for collecting additional randomness
388 * on embedded devices.
389 */
390 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
391 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
392 "initialization.");
393 os_free(group);
394 return NULL;
395 }
396
397 group->GInit = TRUE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800398 if (delay_init) {
399 wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
400 "until Beacon frames have been configured");
401 /* Initialization is completed in wpa_init_keys(). */
402 } else {
403 wpa_group_sm_step(wpa_auth, group);
404 group->GInit = FALSE;
405 wpa_group_sm_step(wpa_auth, group);
406 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700407
408 return group;
409}
410
411
412/**
413 * wpa_init - Initialize WPA authenticator
414 * @addr: Authenticator address
415 * @conf: Configuration for WPA authenticator
416 * @cb: Callback functions for WPA authenticator
417 * Returns: Pointer to WPA authenticator data or %NULL on failure
418 */
419struct wpa_authenticator * wpa_init(const u8 *addr,
420 struct wpa_auth_config *conf,
Paul Stewart092955c2017-02-06 09:13:09 -0800421 const struct wpa_auth_callbacks *cb,
422 void *cb_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700423{
424 struct wpa_authenticator *wpa_auth;
425
426 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
427 if (wpa_auth == NULL)
428 return NULL;
429 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
430 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
Paul Stewart092955c2017-02-06 09:13:09 -0800431 wpa_auth->cb = cb;
432 wpa_auth->cb_ctx = cb_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700433
434 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
435 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
436 os_free(wpa_auth);
437 return NULL;
438 }
439
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800440 wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700441 if (wpa_auth->group == NULL) {
442 os_free(wpa_auth->wpa_ie);
443 os_free(wpa_auth);
444 return NULL;
445 }
446
447 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
448 wpa_auth);
449 if (wpa_auth->pmksa == NULL) {
450 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800451 os_free(wpa_auth->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452 os_free(wpa_auth->wpa_ie);
453 os_free(wpa_auth);
454 return NULL;
455 }
456
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800457#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
459 if (wpa_auth->ft_pmk_cache == NULL) {
460 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800461 os_free(wpa_auth->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 os_free(wpa_auth->wpa_ie);
463 pmksa_cache_auth_deinit(wpa_auth->pmksa);
464 os_free(wpa_auth);
465 return NULL;
466 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800467#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700468
469 if (wpa_auth->conf.wpa_gmk_rekey) {
470 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
471 wpa_rekey_gmk, wpa_auth, NULL);
472 }
473
474 if (wpa_auth->conf.wpa_group_rekey) {
475 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
476 wpa_rekey_gtk, wpa_auth, NULL);
477 }
478
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800479#ifdef CONFIG_P2P
480 if (WPA_GET_BE32(conf->ip_addr_start)) {
481 int count = WPA_GET_BE32(conf->ip_addr_end) -
482 WPA_GET_BE32(conf->ip_addr_start) + 1;
483 if (count > 1000)
484 count = 1000;
485 if (count > 0)
486 wpa_auth->ip_pool = bitfield_alloc(count);
487 }
488#endif /* CONFIG_P2P */
489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700490 return wpa_auth;
491}
492
493
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800494int wpa_init_keys(struct wpa_authenticator *wpa_auth)
495{
496 struct wpa_group *group = wpa_auth->group;
497
498 wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
499 "keys");
500 wpa_group_sm_step(wpa_auth, group);
501 group->GInit = FALSE;
502 wpa_group_sm_step(wpa_auth, group);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800503 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
504 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800505 return 0;
506}
507
508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700509/**
510 * wpa_deinit - Deinitialize WPA authenticator
511 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
512 */
513void wpa_deinit(struct wpa_authenticator *wpa_auth)
514{
515 struct wpa_group *group, *prev;
516
517 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
518 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
519
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700520 pmksa_cache_auth_deinit(wpa_auth->pmksa);
521
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800522#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
524 wpa_auth->ft_pmk_cache = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700525 wpa_ft_deinit(wpa_auth);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800526#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800528#ifdef CONFIG_P2P
529 bitfield_free(wpa_auth->ip_pool);
530#endif /* CONFIG_P2P */
531
532
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700533 os_free(wpa_auth->wpa_ie);
534
535 group = wpa_auth->group;
536 while (group) {
537 prev = group;
538 group = group->next;
539 os_free(prev);
540 }
541
542 os_free(wpa_auth);
543}
544
545
546/**
547 * wpa_reconfig - Update WPA authenticator configuration
548 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
549 * @conf: Configuration for WPA authenticator
550 */
551int wpa_reconfig(struct wpa_authenticator *wpa_auth,
552 struct wpa_auth_config *conf)
553{
554 struct wpa_group *group;
555 if (wpa_auth == NULL)
556 return 0;
557
558 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
559 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
560 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
561 return -1;
562 }
563
564 /*
565 * Reinitialize GTK to make sure it is suitable for the new
566 * configuration.
567 */
568 group = wpa_auth->group;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700569 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700570 group->GInit = TRUE;
571 wpa_group_sm_step(wpa_auth, group);
572 group->GInit = FALSE;
573 wpa_group_sm_step(wpa_auth, group);
574
575 return 0;
576}
577
578
579struct wpa_state_machine *
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700580wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
581 const u8 *p2p_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582{
583 struct wpa_state_machine *sm;
584
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800585 if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
586 return NULL;
587
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700588 sm = os_zalloc(sizeof(struct wpa_state_machine));
589 if (sm == NULL)
590 return NULL;
591 os_memcpy(sm->addr, addr, ETH_ALEN);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700592 if (p2p_dev_addr)
593 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594
595 sm->wpa_auth = wpa_auth;
596 sm->group = wpa_auth->group;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700597 wpa_group_get(sm->wpa_auth, sm->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700598
599 return sm;
600}
601
602
603int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
604 struct wpa_state_machine *sm)
605{
606 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
607 return -1;
608
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800609#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610 if (sm->ft_completed) {
611 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
612 "FT authentication already completed - do not "
613 "start 4-way handshake");
Dmitry Shmidt71757432014-06-02 13:50:35 -0700614 /* Go to PTKINITDONE state to allow GTK rekeying */
615 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800616 sm->Pair = TRUE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700617 return 0;
618 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800619#endif /* CONFIG_IEEE80211R_AP */
620
621#ifdef CONFIG_FILS
622 if (sm->fils_completed) {
623 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
624 "FILS authentication already completed - do not start 4-way handshake");
625 /* Go to PTKINITDONE state to allow GTK rekeying */
626 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800627 sm->Pair = TRUE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800628 return 0;
629 }
630#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700631
632 if (sm->started) {
633 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
634 sm->ReAuthenticationRequest = TRUE;
635 return wpa_sm_step(sm);
636 }
637
638 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
639 "start authentication");
640 sm->started = 1;
641
642 sm->Init = TRUE;
643 if (wpa_sm_step(sm) == 1)
644 return 1; /* should not really happen */
645 sm->Init = FALSE;
646 sm->AuthenticationRequest = TRUE;
647 return wpa_sm_step(sm);
648}
649
650
651void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
652{
653 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
654 * reassociates back to the same AP while the previous entry for the
655 * STA has not yet been removed. */
656 if (sm == NULL)
657 return;
658
659 sm->wpa_key_mgmt = 0;
660}
661
662
663static void wpa_free_sta_sm(struct wpa_state_machine *sm)
664{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800665#ifdef CONFIG_P2P
666 if (WPA_GET_BE32(sm->ip_addr)) {
667 u32 start;
668 wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
669 "address %u.%u.%u.%u from " MACSTR,
670 sm->ip_addr[0], sm->ip_addr[1],
671 sm->ip_addr[2], sm->ip_addr[3],
672 MAC2STR(sm->addr));
673 start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
674 bitfield_clear(sm->wpa_auth->ip_pool,
675 WPA_GET_BE32(sm->ip_addr) - start);
676 }
677#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678 if (sm->GUpdateStationKeys) {
679 sm->group->GKeyDoneStations--;
680 sm->GUpdateStationKeys = FALSE;
681 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800682#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700683 os_free(sm->assoc_resp_ftie);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700684 wpabuf_free(sm->ft_pending_req_ies);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800685#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700686 os_free(sm->last_rx_eapol_key);
687 os_free(sm->wpa_ie);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700688 wpa_group_put(sm->wpa_auth, sm->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700689 os_free(sm);
690}
691
692
693void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
694{
695 if (sm == NULL)
696 return;
697
698 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
699 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
700 "strict rekeying - force GTK rekey since STA "
701 "is leaving");
Roshan Pius3a1667e2018-07-03 15:17:14 -0700702 if (eloop_deplete_timeout(0, 500000, wpa_rekey_gtk,
703 sm->wpa_auth, NULL) == -1)
704 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
705 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700706 }
707
708 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
709 sm->pending_1_of_4_timeout = 0;
710 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
711 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700712#ifdef CONFIG_IEEE80211R_AP
713 wpa_ft_sta_deinit(sm);
714#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700715 if (sm->in_step_loop) {
716 /* Must not free state machine while wpa_sm_step() is running.
717 * Freeing will be completed in the end of wpa_sm_step(). */
718 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
719 "machine deinit for " MACSTR, MAC2STR(sm->addr));
720 sm->pending_deinit = 1;
721 } else
722 wpa_free_sta_sm(sm);
723}
724
725
726static void wpa_request_new_ptk(struct wpa_state_machine *sm)
727{
728 if (sm == NULL)
729 return;
730
731 sm->PTKRequest = TRUE;
732 sm->PTK_valid = 0;
733}
734
735
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800736static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700737 const u8 *replay_counter)
738{
739 int i;
740 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800741 if (!ctr[i].valid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800743 if (os_memcmp(replay_counter, ctr[i].counter,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744 WPA_REPLAY_COUNTER_LEN) == 0)
745 return 1;
746 }
747 return 0;
748}
749
750
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800751static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
752 const u8 *replay_counter)
753{
754 int i;
755 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
756 if (ctr[i].valid &&
757 (replay_counter == NULL ||
758 os_memcmp(replay_counter, ctr[i].counter,
759 WPA_REPLAY_COUNTER_LEN) == 0))
760 ctr[i].valid = FALSE;
761 }
762}
763
764
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800765#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700766static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
767 struct wpa_state_machine *sm,
768 struct wpa_eapol_ie_parse *kde)
769{
770 struct wpa_ie_data ie;
771 struct rsn_mdie *mdie;
772
773 if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
774 ie.num_pmkid != 1 || ie.pmkid == NULL) {
775 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
776 "FT 4-way handshake message 2/4");
777 return -1;
778 }
779
780 os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
781 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
782 sm->sup_pmk_r1_name, PMKID_LEN);
783
784 if (!kde->mdie || !kde->ftie) {
785 wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
786 "message 2/4", kde->mdie ? "FTIE" : "MDIE");
787 return -1;
788 }
789
790 mdie = (struct rsn_mdie *) (kde->mdie + 2);
791 if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
792 os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
793 MOBILITY_DOMAIN_ID_LEN) != 0) {
794 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
795 return -1;
796 }
797
798 if (sm->assoc_resp_ftie &&
799 (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
800 os_memcmp(kde->ftie, sm->assoc_resp_ftie,
801 2 + sm->assoc_resp_ftie[1]) != 0)) {
802 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
803 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
804 kde->ftie, kde->ftie_len);
805 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
806 sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
807 return -1;
808 }
809
810 return 0;
811}
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800812#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700813
814
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800815static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
816 struct wpa_state_machine *sm, int group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800817{
818 /* Supplicant reported a Michael MIC error */
819 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
820 "received EAPOL-Key Error Request "
821 "(STA detected Michael MIC failure (group=%d))",
822 group);
823
824 if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
825 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
826 "ignore Michael MIC failure report since "
827 "group cipher is not TKIP");
828 } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
829 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
830 "ignore Michael MIC failure report since "
831 "pairwise cipher is not TKIP");
832 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800833 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
834 return 1; /* STA entry was removed */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800835 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
836 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
837 }
838
839 /*
840 * Error report is not a request for a new key handshake, but since
841 * Authenticator may do it, let's change the keys now anyway.
842 */
843 wpa_request_new_ptk(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800844 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800845}
846
847
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800848static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
849 size_t data_len)
850{
851 struct wpa_ptk PTK;
852 int ok = 0;
853 const u8 *pmk = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700854 size_t pmk_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800855
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800856 os_memset(&PTK, 0, sizeof(PTK));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800857 for (;;) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700858 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
859 !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800860 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700861 sm->p2p_dev_addr, pmk, &pmk_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800862 if (pmk == NULL)
863 break;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700864#ifdef CONFIG_IEEE80211R_AP
865 if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
866 os_memcpy(sm->xxkey, pmk, pmk_len);
867 sm->xxkey_len = pmk_len;
868 }
869#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800870 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800871 pmk = sm->PMK;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800872 pmk_len = sm->pmk_len;
873 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800874
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800875 if (wpa_derive_ptk(sm, sm->alt_SNonce, pmk, pmk_len, &PTK) < 0)
876 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800877
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700878 if (wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK,
879 data, data_len) == 0) {
Hai Shalom74f70d42019-02-11 14:42:39 -0800880 os_memcpy(sm->PMK, pmk, pmk_len);
881 sm->pmk_len = pmk_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800882 ok = 1;
883 break;
884 }
885
Roshan Pius3a1667e2018-07-03 15:17:14 -0700886 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
887 wpa_key_mgmt_sae(sm->wpa_key_mgmt))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800888 break;
889 }
890
891 if (!ok) {
892 wpa_printf(MSG_DEBUG,
893 "WPA: Earlier SNonce did not result in matching MIC");
894 return -1;
895 }
896
897 wpa_printf(MSG_DEBUG,
898 "WPA: Earlier SNonce resulted in matching MIC");
899 sm->alt_snonce_valid = 0;
900 os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
901 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
902 sm->PTK_valid = TRUE;
903
904 return 0;
905}
906
907
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908void wpa_receive(struct wpa_authenticator *wpa_auth,
909 struct wpa_state_machine *sm,
910 u8 *data, size_t data_len)
911{
912 struct ieee802_1x_hdr *hdr;
913 struct wpa_eapol_key *key;
914 u16 key_info, key_data_length;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700915 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST } msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700916 char *msgtxt;
917 struct wpa_eapol_ie_parse kde;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800918 const u8 *key_data;
919 size_t keyhdrlen, mic_len;
920 u8 *mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921
922 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
923 return;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800924 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL data", data, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700926 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800927 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800928
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800929 if (data_len < sizeof(*hdr) + keyhdrlen) {
930 wpa_printf(MSG_DEBUG, "WPA: Ignore too short EAPOL-Key frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931 return;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800932 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700933
934 hdr = (struct ieee802_1x_hdr *) data;
935 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800936 mic = (u8 *) (key + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800938 key_data = mic + mic_len + 2;
939 key_data_length = WPA_GET_BE16(mic + mic_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800940 wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800941 " key_info=0x%x type=%u mic_len=%u key_data_length=%u",
942 MAC2STR(sm->addr), key_info, key->type,
943 (unsigned int) mic_len, key_data_length);
944 wpa_hexdump(MSG_MSGDUMP,
945 "WPA: EAPOL-Key header (ending before Key MIC)",
946 key, sizeof(*key));
947 wpa_hexdump(MSG_MSGDUMP, "WPA: EAPOL-Key Key MIC",
948 mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800949 if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700950 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
951 "key_data overflow (%d > %lu)",
952 key_data_length,
953 (unsigned long) (data_len - sizeof(*hdr) -
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800954 keyhdrlen));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700955 return;
956 }
957
958 if (sm->wpa == WPA_VERSION_WPA2) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800959 if (key->type == EAPOL_KEY_TYPE_WPA) {
960 /*
961 * Some deployed station implementations seem to send
962 * msg 4/4 with incorrect type value in WPA2 mode.
963 */
964 wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
965 "with unexpected WPA type in RSN mode");
966 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700967 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
968 "unexpected type %d in RSN mode",
969 key->type);
970 return;
971 }
972 } else {
973 if (key->type != EAPOL_KEY_TYPE_WPA) {
974 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
975 "unexpected type %d in WPA mode",
976 key->type);
977 return;
978 }
979 }
980
981 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
982 WPA_NONCE_LEN);
983 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
984 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
985
986 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
987 * are set */
988
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700989 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
990 wpa_printf(MSG_DEBUG, "WPA: Ignore SMK message");
991 return;
992 }
993
994 if (key_info & WPA_KEY_INFO_REQUEST) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995 msg = REQUEST;
996 msgtxt = "Request";
997 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
998 msg = GROUP_2;
999 msgtxt = "2/2 Group";
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001000 } else if (key_data_length == 0 ||
1001 (mic_len == 0 && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) &&
1002 key_data_length == AES_BLOCK_SIZE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001003 msg = PAIRWISE_4;
1004 msgtxt = "4/4 Pairwise";
1005 } else {
1006 msg = PAIRWISE_2;
1007 msgtxt = "2/4 Pairwise";
1008 }
1009
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001010 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
1011 msg == GROUP_2) {
1012 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001013 if (sm->pairwise == WPA_CIPHER_CCMP ||
1014 sm->pairwise == WPA_CIPHER_GCMP) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001015 if (wpa_use_cmac(sm->wpa_key_mgmt) &&
1016 !wpa_use_akm_defined(sm->wpa_key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001017 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1018 wpa_auth_logger(wpa_auth, sm->addr,
1019 LOGGER_WARNING,
1020 "advertised support for "
1021 "AES-128-CMAC, but did not "
1022 "use it");
1023 return;
1024 }
1025
Roshan Pius3a1667e2018-07-03 15:17:14 -07001026 if (!wpa_use_cmac(sm->wpa_key_mgmt) &&
1027 !wpa_use_akm_defined(sm->wpa_key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001028 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1029 wpa_auth_logger(wpa_auth, sm->addr,
1030 LOGGER_WARNING,
1031 "did not use HMAC-SHA1-AES "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001032 "with CCMP/GCMP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033 return;
1034 }
1035 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001036
Roshan Pius3a1667e2018-07-03 15:17:14 -07001037 if (wpa_use_akm_defined(sm->wpa_key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001038 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1039 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1040 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1041 return;
1042 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001043 }
1044
1045 if (key_info & WPA_KEY_INFO_REQUEST) {
1046 if (sm->req_replay_counter_used &&
1047 os_memcmp(key->replay_counter, sm->req_replay_counter,
1048 WPA_REPLAY_COUNTER_LEN) <= 0) {
1049 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1050 "received EAPOL-Key request with "
1051 "replayed counter");
1052 return;
1053 }
1054 }
1055
1056 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001057 !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001058 int i;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001059
1060 if (msg == PAIRWISE_2 &&
1061 wpa_replay_counter_valid(sm->prev_key_replay,
1062 key->replay_counter) &&
1063 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1064 os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
1065 {
1066 /*
1067 * Some supplicant implementations (e.g., Windows XP
1068 * WZC) update SNonce for each EAPOL-Key 2/4. This
1069 * breaks the workaround on accepting any of the
1070 * pending requests, so allow the SNonce to be updated
1071 * even if we have already sent out EAPOL-Key 3/4.
1072 */
1073 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1074 "Process SNonce update from STA "
1075 "based on retransmitted EAPOL-Key "
1076 "1/4");
1077 sm->update_snonce = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001078 os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
1079 sm->alt_snonce_valid = TRUE;
1080 os_memcpy(sm->alt_replay_counter,
1081 sm->key_replay[0].counter,
1082 WPA_REPLAY_COUNTER_LEN);
1083 goto continue_processing;
1084 }
1085
1086 if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
1087 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1088 os_memcmp(key->replay_counter, sm->alt_replay_counter,
1089 WPA_REPLAY_COUNTER_LEN) == 0) {
1090 /*
1091 * Supplicant may still be using the old SNonce since
1092 * there was two EAPOL-Key 2/4 messages and they had
1093 * different SNonce values.
1094 */
1095 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1096 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001097 goto continue_processing;
1098 }
1099
1100 if (msg == PAIRWISE_2 &&
1101 wpa_replay_counter_valid(sm->prev_key_replay,
1102 key->replay_counter) &&
1103 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
1104 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1105 "ignore retransmitted EAPOL-Key %s - "
1106 "SNonce did not change", msgtxt);
1107 } else {
1108 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1109 "received EAPOL-Key %s with "
1110 "unexpected replay counter", msgtxt);
1111 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
1113 if (!sm->key_replay[i].valid)
1114 break;
1115 wpa_hexdump(MSG_DEBUG, "pending replay counter",
1116 sm->key_replay[i].counter,
1117 WPA_REPLAY_COUNTER_LEN);
1118 }
1119 wpa_hexdump(MSG_DEBUG, "received replay counter",
1120 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1121 return;
1122 }
1123
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001124continue_processing:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001125#ifdef CONFIG_FILS
1126 if (sm->wpa == WPA_VERSION_WPA2 && mic_len == 0 &&
1127 !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1128 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1129 "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
1130 return;
1131 }
1132#endif /* CONFIG_FILS */
1133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 switch (msg) {
1135 case PAIRWISE_2:
1136 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001137 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
1138 (!sm->update_snonce ||
1139 sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1141 "received EAPOL-Key msg 2/4 in "
1142 "invalid state (%d) - dropped",
1143 sm->wpa_ptk_state);
1144 return;
1145 }
1146 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
1147 if (sm->group->reject_4way_hs_for_entropy) {
1148 /*
1149 * The system did not have enough entropy to generate
1150 * strong random numbers. Reject the first 4-way
1151 * handshake(s) and collect some entropy based on the
1152 * information from it. Once enough entropy is
1153 * available, the next atempt will trigger GMK/Key
1154 * Counter update and the station will be allowed to
1155 * continue.
1156 */
1157 wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
1158 "collect more entropy for random number "
1159 "generation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160 random_mark_pool_ready();
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001161 wpa_sta_disconnect(wpa_auth, sm->addr,
1162 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163 return;
1164 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165 break;
1166 case PAIRWISE_4:
1167 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1168 !sm->PTK_valid) {
1169 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1170 "received EAPOL-Key msg 4/4 in "
1171 "invalid state (%d) - dropped",
1172 sm->wpa_ptk_state);
1173 return;
1174 }
1175 break;
1176 case GROUP_2:
1177 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1178 || !sm->PTK_valid) {
1179 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1180 "received EAPOL-Key msg 2/2 in "
1181 "invalid state (%d) - dropped",
1182 sm->wpa_ptk_group_state);
1183 return;
1184 }
1185 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186 case REQUEST:
1187 break;
1188 }
1189
1190 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1191 "received EAPOL-Key frame (%s)", msgtxt);
1192
1193 if (key_info & WPA_KEY_INFO_ACK) {
1194 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1195 "received invalid EAPOL-Key: Key Ack set");
1196 return;
1197 }
1198
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001199 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1200 !(key_info & WPA_KEY_INFO_MIC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001201 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1202 "received invalid EAPOL-Key: Key MIC not set");
1203 return;
1204 }
1205
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001206#ifdef CONFIG_FILS
1207 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1208 (key_info & WPA_KEY_INFO_MIC)) {
1209 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1210 "received invalid EAPOL-Key: Key MIC set");
1211 return;
1212 }
1213#endif /* CONFIG_FILS */
1214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001215 sm->MICVerified = FALSE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001216 if (sm->PTK_valid && !sm->update_snonce) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001217 if (mic_len &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001218 wpa_verify_key_mic(sm->wpa_key_mgmt, sm->pmk_len, &sm->PTK,
1219 data, data_len) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001220 (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
1221 wpa_try_alt_snonce(sm, data, data_len))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1223 "received EAPOL-Key with invalid MIC");
Hai Shalom74f70d42019-02-11 14:42:39 -08001224#ifdef TEST_FUZZ
1225 wpa_printf(MSG_INFO,
1226 "TEST: Ignore Key MIC failure for fuzz testing");
1227 goto continue_fuzz;
1228#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001229 return;
1230 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001231#ifdef CONFIG_FILS
1232 if (!mic_len &&
1233 wpa_aead_decrypt(sm, &sm->PTK, data, data_len,
1234 &key_data_length) < 0) {
1235 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1236 "received EAPOL-Key with invalid MIC");
Hai Shalom74f70d42019-02-11 14:42:39 -08001237#ifdef TEST_FUZZ
1238 wpa_printf(MSG_INFO,
1239 "TEST: Ignore Key MIC failure for fuzz testing");
1240 goto continue_fuzz;
1241#endif /* TEST_FUZZ */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001242 return;
1243 }
1244#endif /* CONFIG_FILS */
Hai Shalom74f70d42019-02-11 14:42:39 -08001245#ifdef TEST_FUZZ
1246 continue_fuzz:
1247#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001248 sm->MICVerified = TRUE;
1249 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1250 sm->pending_1_of_4_timeout = 0;
1251 }
1252
1253 if (key_info & WPA_KEY_INFO_REQUEST) {
1254 if (sm->MICVerified) {
1255 sm->req_replay_counter_used = 1;
1256 os_memcpy(sm->req_replay_counter, key->replay_counter,
1257 WPA_REPLAY_COUNTER_LEN);
1258 } else {
1259 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1260 "received EAPOL-Key request with "
1261 "invalid MIC");
1262 return;
1263 }
1264
1265 /*
1266 * TODO: should decrypt key data field if encryption was used;
1267 * even though MAC address KDE is not normally encrypted,
1268 * supplicant is allowed to encrypt it.
1269 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001270 if (key_info & WPA_KEY_INFO_ERROR) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001271 if (wpa_receive_error_report(
1272 wpa_auth, sm,
1273 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1274 return; /* STA entry was removed */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001275 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1276 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1277 "received EAPOL-Key Request for new "
1278 "4-Way Handshake");
1279 wpa_request_new_ptk(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001280 } else if (key_data_length > 0 &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001281 wpa_parse_kde_ies(key_data, key_data_length,
1282 &kde) == 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001283 kde.mac_addr) {
1284 } else {
1285 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1286 "received EAPOL-Key Request for GTK "
1287 "rekeying");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001288 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1289 wpa_rekey_gtk(wpa_auth, NULL);
1290 }
1291 } else {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001292 /* Do not allow the same key replay counter to be reused. */
1293 wpa_replay_counter_mark_invalid(sm->key_replay,
1294 key->replay_counter);
1295
1296 if (msg == PAIRWISE_2) {
1297 /*
1298 * Maintain a copy of the pending EAPOL-Key frames in
1299 * case the EAPOL-Key frame was retransmitted. This is
1300 * needed to allow EAPOL-Key msg 2/4 reply to another
1301 * pending msg 1/4 to update the SNonce to work around
1302 * unexpected supplicant behavior.
1303 */
1304 os_memcpy(sm->prev_key_replay, sm->key_replay,
1305 sizeof(sm->key_replay));
1306 } else {
1307 os_memset(sm->prev_key_replay, 0,
1308 sizeof(sm->prev_key_replay));
1309 }
1310
1311 /*
1312 * Make sure old valid counters are not accepted anymore and
1313 * do not get copied again.
1314 */
1315 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316 }
1317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001318 os_free(sm->last_rx_eapol_key);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001319 sm->last_rx_eapol_key = os_memdup(data, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320 if (sm->last_rx_eapol_key == NULL)
1321 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001322 sm->last_rx_eapol_key_len = data_len;
1323
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001324 sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001325 sm->EAPOLKeyReceived = TRUE;
1326 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1327 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1328 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1329 wpa_sm_step(sm);
1330}
1331
1332
1333static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1334 const u8 *gnonce, u8 *gtk, size_t gtk_len)
1335{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001336 u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + WPA_GTK_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337 u8 *pos;
1338 int ret = 0;
1339
1340 /* GTK = PRF-X(GMK, "Group key expansion",
1341 * AA || GNonce || Time || random data)
1342 * The example described in the IEEE 802.11 standard uses only AA and
1343 * GNonce as inputs here. Add some more entropy since this derivation
1344 * is done only at the Authenticator and as such, does not need to be
1345 * exactly same.
1346 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001347 os_memset(data, 0, sizeof(data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001348 os_memcpy(data, addr, ETH_ALEN);
1349 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1350 pos = data + ETH_ALEN + WPA_NONCE_LEN;
1351 wpa_get_ntp_timestamp(pos);
Hai Shalom74f70d42019-02-11 14:42:39 -08001352#ifdef TEST_FUZZ
1353 os_memset(pos, 0xef, 8);
1354#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 pos += 8;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001356 if (random_get_bytes(pos, gtk_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001357 ret = -1;
1358
Roshan Pius3a1667e2018-07-03 15:17:14 -07001359#ifdef CONFIG_SHA384
1360 if (sha384_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1361 gtk, gtk_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001362 ret = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001363#else /* CONFIG_SHA384 */
1364#ifdef CONFIG_SHA256
1365 if (sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1366 gtk, gtk_len) < 0)
1367 ret = -1;
1368#else /* CONFIG_SHA256 */
1369 if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1370 gtk, gtk_len) < 0)
1371 ret = -1;
1372#endif /* CONFIG_SHA256 */
1373#endif /* CONFIG_SHA384 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374
1375 return ret;
1376}
1377
1378
1379static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1380{
1381 struct wpa_authenticator *wpa_auth = eloop_ctx;
1382 struct wpa_state_machine *sm = timeout_ctx;
1383
1384 sm->pending_1_of_4_timeout = 0;
1385 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
1386 sm->TimeoutEvt = TRUE;
1387 wpa_sm_step(sm);
1388}
1389
1390
1391void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1392 struct wpa_state_machine *sm, int key_info,
1393 const u8 *key_rsc, const u8 *nonce,
1394 const u8 *kde, size_t kde_len,
1395 int keyidx, int encr, int force_version)
1396{
1397 struct ieee802_1x_hdr *hdr;
1398 struct wpa_eapol_key *key;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001399 size_t len, mic_len, keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400 int alg;
1401 int key_data_len, pad_len = 0;
1402 u8 *buf, *pos;
1403 int version, pairwise;
1404 int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001405 u8 *key_mic, *key_data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001407 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001408 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001409
1410 len = sizeof(struct ieee802_1x_hdr) + keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411
1412 if (force_version)
1413 version = force_version;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001414 else if (wpa_use_akm_defined(sm->wpa_key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001415 version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001416 else if (wpa_use_cmac(sm->wpa_key_mgmt))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001417 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001418 else if (sm->pairwise != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001419 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1420 else
1421 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1422
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001423 pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001424
1425 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1426 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1427 "encr=%d)",
1428 version,
1429 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1430 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1431 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1432 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1433 pairwise, (unsigned long) kde_len, keyidx, encr);
1434
1435 key_data_len = kde_len;
1436
1437 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07001438 wpa_use_aes_key_wrap(sm->wpa_key_mgmt) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001439 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1440 pad_len = key_data_len % 8;
1441 if (pad_len)
1442 pad_len = 8 - pad_len;
1443 key_data_len += pad_len + 8;
1444 }
1445
1446 len += key_data_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001447 if (!mic_len && encr)
1448 len += AES_BLOCK_SIZE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001449
1450 hdr = os_zalloc(len);
1451 if (hdr == NULL)
1452 return;
1453 hdr->version = wpa_auth->conf.eapol_version;
1454 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1455 hdr->length = host_to_be16(len - sizeof(*hdr));
1456 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001457 key_mic = (u8 *) (key + 1);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001458 key_data = ((u8 *) (hdr + 1)) + keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001459
1460 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1461 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1462 key_info |= version;
1463 if (encr && sm->wpa == WPA_VERSION_WPA2)
1464 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1465 if (sm->wpa != WPA_VERSION_WPA2)
1466 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1467 WPA_PUT_BE16(key->key_info, key_info);
1468
1469 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001470 if (sm->wpa == WPA_VERSION_WPA2 && !pairwise)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001471 WPA_PUT_BE16(key->key_length, 0);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001472 else
1473 WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001474
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1476 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1477 os_memcpy(sm->key_replay[i].counter,
1478 sm->key_replay[i - 1].counter,
1479 WPA_REPLAY_COUNTER_LEN);
1480 }
1481 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1482 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1483 WPA_REPLAY_COUNTER_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001484 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
1485 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001486 sm->key_replay[0].valid = TRUE;
1487
1488 if (nonce)
1489 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1490
1491 if (key_rsc)
1492 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1493
1494 if (kde && !encr) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001495 os_memcpy(key_data, kde, kde_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001496 WPA_PUT_BE16(key_mic + mic_len, kde_len);
1497#ifdef CONFIG_FILS
Roshan Pius3a1667e2018-07-03 15:17:14 -07001498 } else if (!mic_len && kde) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001499 const u8 *aad[1];
1500 size_t aad_len[1];
1501
1502 WPA_PUT_BE16(key_mic, AES_BLOCK_SIZE + kde_len);
1503 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1504 kde, kde_len);
1505
1506 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK",
1507 sm->PTK.kek, sm->PTK.kek_len);
1508 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1509 * to Key Data (exclusive). */
1510 aad[0] = (u8 *) hdr;
1511 aad_len[0] = key_mic + 2 - (u8 *) hdr;
1512 if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len, kde, kde_len,
1513 1, aad, aad_len, key_mic + 2) < 0) {
1514 wpa_printf(MSG_DEBUG, "WPA: AES-SIV encryption failed");
1515 return;
1516 }
1517
1518 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
1519 key_mic + 2, AES_BLOCK_SIZE + kde_len);
1520#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001521 } else if (encr && kde) {
1522 buf = os_zalloc(key_data_len);
1523 if (buf == NULL) {
1524 os_free(hdr);
1525 return;
1526 }
1527 pos = buf;
1528 os_memcpy(pos, kde, kde_len);
1529 pos += kde_len;
1530
1531 if (pad_len)
1532 *pos++ = 0xdd;
1533
1534 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1535 buf, key_data_len);
1536 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07001537 wpa_use_aes_key_wrap(sm->wpa_key_mgmt) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001538 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001539 wpa_printf(MSG_DEBUG,
1540 "WPA: Encrypt Key Data using AES-WRAP (KEK length %u)",
1541 (unsigned int) sm->PTK.kek_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001542 if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len,
1543 (key_data_len - 8) / 8, buf, key_data)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001544 os_free(hdr);
1545 os_free(buf);
1546 return;
1547 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001548 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001549#ifndef CONFIG_NO_RC4
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001550 } else if (sm->PTK.kek_len == 16) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001551 u8 ek[32];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001552
1553 wpa_printf(MSG_DEBUG,
1554 "WPA: Encrypt Key Data using RC4");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001555 os_memcpy(key->key_iv,
1556 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1557 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1558 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001559 os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len);
1560 os_memcpy(key_data, buf, key_data_len);
1561 rc4_skip(ek, 32, 256, key_data, key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001562 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001563#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001564 } else {
1565 os_free(hdr);
1566 os_free(buf);
1567 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568 }
1569 os_free(buf);
1570 }
1571
1572 if (key_info & WPA_KEY_INFO_MIC) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001573 if (!sm->PTK_valid || !mic_len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001574 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1575 "PTK not valid when sending EAPOL-Key "
1576 "frame");
1577 os_free(hdr);
1578 return;
1579 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001580
Roshan Pius3a1667e2018-07-03 15:17:14 -07001581 if (wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len,
1582 sm->wpa_key_mgmt, version,
1583 (u8 *) hdr, len, key_mic) < 0) {
1584 os_free(hdr);
1585 return;
1586 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001587#ifdef CONFIG_TESTING_OPTIONS
1588 if (!pairwise &&
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001589 wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001590 drand48() <
1591 wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
1592 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1593 "Corrupting group EAPOL-Key Key MIC");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001594 key_mic[0]++;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001595 }
1596#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001597 }
1598
1599 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1600 1);
1601 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1602 sm->pairwise_set);
1603 os_free(hdr);
1604}
1605
1606
1607static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1608 struct wpa_state_machine *sm, int key_info,
1609 const u8 *key_rsc, const u8 *nonce,
1610 const u8 *kde, size_t kde_len,
1611 int keyidx, int encr)
1612{
1613 int timeout_ms;
1614 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001615 u32 ctr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001616
1617 if (sm == NULL)
1618 return;
1619
1620 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1621 keyidx, encr, 0);
1622
1623 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1624 if (ctr == 1 && wpa_auth->conf.tx_status)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001625 timeout_ms = pairwise ? eapol_key_timeout_first :
1626 eapol_key_timeout_first_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627 else
1628 timeout_ms = eapol_key_timeout_subseq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001629 if (wpa_auth->conf.wpa_disable_eapol_key_retries &&
1630 (!pairwise || (key_info & WPA_KEY_INFO_MIC)))
1631 timeout_ms = eapol_key_timeout_no_retrans;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001632 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1633 sm->pending_1_of_4_timeout = 1;
Hai Shalom74f70d42019-02-11 14:42:39 -08001634#ifdef TEST_FUZZ
1635 timeout_ms = 1;
1636#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001637 wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001638 "counter %u)", timeout_ms, ctr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001639 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1640 wpa_send_eapol_timeout, wpa_auth, sm);
1641}
1642
1643
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001644static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK,
1645 u8 *data, size_t data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001646{
1647 struct ieee802_1x_hdr *hdr;
1648 struct wpa_eapol_key *key;
1649 u16 key_info;
1650 int ret = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001651 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN], *mic_pos;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001652 size_t mic_len = wpa_mic_len(akmp, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001653
1654 if (data_len < sizeof(*hdr) + sizeof(*key))
1655 return -1;
1656
1657 hdr = (struct ieee802_1x_hdr *) data;
1658 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001659 mic_pos = (u8 *) (key + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001661 os_memcpy(mic, mic_pos, mic_len);
1662 os_memset(mic_pos, 0, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001663 if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp,
1664 key_info & WPA_KEY_INFO_TYPE_MASK,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001665 data, data_len, mic_pos) ||
1666 os_memcmp_const(mic, mic_pos, mic_len) != 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001667 ret = -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001668 os_memcpy(mic_pos, mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669 return ret;
1670}
1671
1672
1673void wpa_remove_ptk(struct wpa_state_machine *sm)
1674{
1675 sm->PTK_valid = FALSE;
1676 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001677 if (wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL,
1678 0))
1679 wpa_printf(MSG_DEBUG,
1680 "RSN: PTK removal from the driver failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681 sm->pairwise_set = FALSE;
1682 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1683}
1684
1685
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001686int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001687{
1688 int remove_ptk = 1;
1689
1690 if (sm == NULL)
1691 return -1;
1692
1693 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1694 "event %d notification", event);
1695
1696 switch (event) {
1697 case WPA_AUTH:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001698#ifdef CONFIG_MESH
1699 /* PTKs are derived through AMPE */
1700 if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
1701 /* not mesh */
1702 break;
1703 }
1704 return 0;
1705#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706 case WPA_ASSOC:
1707 break;
1708 case WPA_DEAUTH:
1709 case WPA_DISASSOC:
1710 sm->DeauthenticationRequest = TRUE;
1711 break;
1712 case WPA_REAUTH:
1713 case WPA_REAUTH_EAPOL:
1714 if (!sm->started) {
1715 /*
1716 * When using WPS, we may end up here if the STA
1717 * manages to re-associate without the previous STA
1718 * entry getting removed. Consequently, we need to make
1719 * sure that the WPA state machines gets initialized
1720 * properly at this point.
1721 */
1722 wpa_printf(MSG_DEBUG, "WPA state machine had not been "
1723 "started - initialize now");
1724 sm->started = 1;
1725 sm->Init = TRUE;
1726 if (wpa_sm_step(sm) == 1)
1727 return 1; /* should not really happen */
1728 sm->Init = FALSE;
1729 sm->AuthenticationRequest = TRUE;
1730 break;
1731 }
1732 if (sm->GUpdateStationKeys) {
1733 /*
1734 * Reauthentication cancels the pending group key
1735 * update for this STA.
1736 */
1737 sm->group->GKeyDoneStations--;
1738 sm->GUpdateStationKeys = FALSE;
1739 sm->PtkGroupInit = TRUE;
1740 }
1741 sm->ReAuthenticationRequest = TRUE;
1742 break;
1743 case WPA_ASSOC_FT:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001744#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001745 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1746 "after association");
1747 wpa_ft_install_ptk(sm);
1748
1749 /* Using FT protocol, not WPA auth state machine */
1750 sm->ft_completed = 1;
1751 return 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001752#else /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001753 break;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001754#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001755 case WPA_ASSOC_FILS:
1756#ifdef CONFIG_FILS
1757 wpa_printf(MSG_DEBUG,
1758 "FILS: TK configuration after association");
1759 fils_set_tk(sm);
1760 sm->fils_completed = 1;
1761 return 0;
1762#else /* CONFIG_FILS */
1763 break;
1764#endif /* CONFIG_FILS */
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02001765 case WPA_DRV_STA_REMOVED:
1766 sm->tk_already_set = FALSE;
1767 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768 }
1769
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001770#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001771 sm->ft_completed = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001772#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001773
1774#ifdef CONFIG_IEEE80211W
1775 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1776 remove_ptk = 0;
1777#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001778#ifdef CONFIG_FILS
1779 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1780 (event == WPA_AUTH || event == WPA_ASSOC))
1781 remove_ptk = 0;
1782#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001783
1784 if (remove_ptk) {
1785 sm->PTK_valid = FALSE;
1786 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1787
1788 if (event != WPA_REAUTH_EAPOL)
1789 wpa_remove_ptk(sm);
1790 }
1791
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001792 if (sm->in_step_loop) {
1793 /*
1794 * wpa_sm_step() is already running - avoid recursive call to
1795 * it by making the existing loop process the new update.
1796 */
1797 sm->changed = TRUE;
1798 return 0;
1799 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001800 return wpa_sm_step(sm);
1801}
1802
1803
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804SM_STATE(WPA_PTK, INITIALIZE)
1805{
1806 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1807 if (sm->Init) {
1808 /* Init flag is not cleared here, so avoid busy
1809 * loop by claiming nothing changed. */
1810 sm->changed = FALSE;
1811 }
1812
1813 sm->keycount = 0;
1814 if (sm->GUpdateStationKeys)
1815 sm->group->GKeyDoneStations--;
1816 sm->GUpdateStationKeys = FALSE;
1817 if (sm->wpa == WPA_VERSION_WPA)
1818 sm->PInitAKeys = FALSE;
1819 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1820 * Local AA > Remote AA)) */) {
1821 sm->Pair = TRUE;
1822 }
1823 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1824 wpa_remove_ptk(sm);
1825 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1826 sm->TimeoutCtr = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001827 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
1828 sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP ||
1829 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001830 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1831 WPA_EAPOL_authorized, 0);
1832 }
1833}
1834
1835
1836SM_STATE(WPA_PTK, DISCONNECT)
1837{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001838 u16 reason = sm->disconnect_reason;
1839
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1841 sm->Disconnect = FALSE;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001842 sm->disconnect_reason = 0;
1843 if (!reason)
1844 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1845 wpa_sta_disconnect(sm->wpa_auth, sm->addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846}
1847
1848
1849SM_STATE(WPA_PTK, DISCONNECTED)
1850{
1851 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1852 sm->DeauthenticationRequest = FALSE;
1853}
1854
1855
1856SM_STATE(WPA_PTK, AUTHENTICATION)
1857{
1858 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1859 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1860 sm->PTK_valid = FALSE;
1861 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1862 1);
1863 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1864 sm->AuthenticationRequest = FALSE;
1865}
1866
1867
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001868static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
1869 struct wpa_group *group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001871 if (group->first_sta_seen)
1872 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001873 /*
1874 * System has run bit further than at the time hostapd was started
1875 * potentially very early during boot up. This provides better chances
1876 * of collecting more randomness on embedded systems. Re-initialize the
1877 * GMK and Counter here to improve their strength if there was not
1878 * enough entropy available immediately after system startup.
1879 */
1880 wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
1881 "station");
1882 if (random_pool_ready() != 1) {
1883 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
1884 "to proceed - reject first 4-way handshake");
1885 group->reject_4way_hs_for_entropy = TRUE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001886 } else {
1887 group->first_sta_seen = TRUE;
1888 group->reject_4way_hs_for_entropy = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001889 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001890
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001891 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 ||
1892 wpa_gtk_update(wpa_auth, group) < 0 ||
1893 wpa_group_config_group_keys(wpa_auth, group) < 0) {
1894 wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed");
1895 group->first_sta_seen = FALSE;
1896 group->reject_4way_hs_for_entropy = TRUE;
1897 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001898}
1899
1900
1901SM_STATE(WPA_PTK, AUTHENTICATION2)
1902{
1903 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1904
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001905 wpa_group_ensure_init(sm->wpa_auth, sm->group);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001906 sm->ReAuthenticationRequest = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001907
Dmitry Shmidt04949592012-07-19 12:16:46 -07001908 /*
1909 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1910 * ambiguous. The Authenticator state machine uses a counter that is
1911 * incremented by one for each 4-way handshake. However, the security
1912 * analysis of 4-way handshake points out that unpredictable nonces
1913 * help in preventing precomputation attacks. Instead of the state
1914 * machine definition, use an unpredictable nonce value here to provide
1915 * stronger protection against potential precomputation attacks.
1916 */
1917 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1918 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
1919 "ANonce.");
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001920 sm->Disconnect = TRUE;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001921 return;
1922 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001923 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
1924 WPA_NONCE_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001925 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1926 * logical place than INITIALIZE since AUTHENTICATION2 can be
1927 * re-entered on ReAuthenticationRequest without going through
1928 * INITIALIZE. */
1929 sm->TimeoutCtr = 0;
1930}
1931
1932
Jouni Malinen1420a892017-10-01 12:32:57 +03001933static int wpa_auth_sm_ptk_update(struct wpa_state_machine *sm)
1934{
1935 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1936 wpa_printf(MSG_ERROR,
1937 "WPA: Failed to get random data for ANonce");
1938 sm->Disconnect = TRUE;
1939 return -1;
1940 }
1941 wpa_hexdump(MSG_DEBUG, "WPA: Assign new ANonce", sm->ANonce,
1942 WPA_NONCE_LEN);
1943 sm->TimeoutCtr = 0;
1944 return 0;
1945}
1946
1947
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001948SM_STATE(WPA_PTK, INITPMK)
1949{
1950 u8 msk[2 * PMK_LEN];
1951 size_t len = 2 * PMK_LEN;
1952
1953 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001954#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001955 sm->xxkey_len = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001956#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957 if (sm->pmksa) {
1958 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001959 os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
1960 sm->pmk_len = sm->pmksa->pmk_len;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001961#ifdef CONFIG_DPP
1962 } else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP) {
1963 wpa_printf(MSG_DEBUG,
1964 "DPP: No PMKSA cache entry for STA - reject connection");
1965 sm->Disconnect = TRUE;
1966 sm->disconnect_reason = WLAN_REASON_INVALID_PMKID;
1967 return;
1968#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001969 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001970 unsigned int pmk_len;
1971
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001972 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001973 pmk_len = PMK_LEN_SUITE_B_192;
1974 else
1975 pmk_len = PMK_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001976 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001977 "(MSK len=%lu PMK len=%u)", (unsigned long) len,
1978 pmk_len);
1979 if (len < pmk_len) {
1980 wpa_printf(MSG_DEBUG,
1981 "WPA: MSK not long enough (%u) to create PMK (%u)",
1982 (unsigned int) len, (unsigned int) pmk_len);
1983 sm->Disconnect = TRUE;
1984 return;
1985 }
1986 os_memcpy(sm->PMK, msk, pmk_len);
1987 sm->pmk_len = pmk_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001988#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001989 if (len >= 2 * PMK_LEN) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001990 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
1991 os_memcpy(sm->xxkey, msk, SHA384_MAC_LEN);
1992 sm->xxkey_len = SHA384_MAC_LEN;
1993 } else {
1994 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1995 sm->xxkey_len = PMK_LEN;
1996 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001997 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001998#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999 } else {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002000 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p",
Paul Stewart092955c2017-02-06 09:13:09 -08002001 sm->wpa_auth->cb->get_msk);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002002 sm->Disconnect = TRUE;
2003 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002004 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002005 os_memset(msk, 0, sizeof(msk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002006
2007 sm->req_replay_counter_used = 0;
2008 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
2009 * will break reauthentication since EAPOL state machines may not be
2010 * get into AUTHENTICATING state that clears keyRun before WPA state
2011 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
2012 * state and takes PMK from the previously used AAA Key. This will
2013 * eventually fail in 4-Way Handshake because Supplicant uses PMK
2014 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
2015 * be good workaround for this issue. */
2016 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
2017}
2018
2019
2020SM_STATE(WPA_PTK, INITPSK)
2021{
2022 const u8 *psk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002023 size_t psk_len;
2024
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002026 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL,
2027 &psk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002028 if (psk) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002029 os_memcpy(sm->PMK, psk, psk_len);
2030 sm->pmk_len = psk_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002031#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002032 os_memcpy(sm->xxkey, psk, PMK_LEN);
2033 sm->xxkey_len = PMK_LEN;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002034#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002035 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002036#ifdef CONFIG_SAE
2037 if (wpa_auth_uses_sae(sm) && sm->pmksa) {
2038 wpa_printf(MSG_DEBUG, "SAE: PMK from PMKSA cache");
2039 os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
2040 sm->pmk_len = sm->pmksa->pmk_len;
2041 }
2042#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043 sm->req_replay_counter_used = 0;
2044}
2045
2046
2047SM_STATE(WPA_PTK, PTKSTART)
2048{
2049 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
2050 size_t pmkid_len = 0;
2051
2052 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
2053 sm->PTKRequest = FALSE;
2054 sm->TimeoutEvt = FALSE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002055 sm->alt_snonce_valid = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002056
2057 sm->TimeoutCtr++;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002058 if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002059 /* No point in sending the EAPOL-Key - we will disconnect
2060 * immediately following this. */
2061 return;
2062 }
2063
2064 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2065 "sending 1/4 msg of 4-Way Handshake");
2066 /*
Hai Shalomce48b4a2018-09-05 11:41:35 -07002067 * For infrastructure BSS cases, it is better for the AP not to include
2068 * the PMKID KDE in EAPOL-Key msg 1/4 since it could be used to initiate
2069 * offline search for the passphrase/PSK without having to be able to
2070 * capture a 4-way handshake from a STA that has access to the network.
2071 *
2072 * For IBSS cases, addition of PMKID KDE could be considered even with
2073 * WPA2-PSK cases that use multiple PSKs, but only if there is a single
2074 * possible PSK for this STA. However, this should not be done unless
2075 * there is support for using that information on the supplicant side.
2076 * The concern about exposing PMKID unnecessarily in infrastructure BSS
2077 * cases would also apply here, but at least in the IBSS case, this
2078 * would cover a potential real use case.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002079 */
2080 if (sm->wpa == WPA_VERSION_WPA2 &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002081 (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) ||
2082 (sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE && sm->pmksa) ||
2083 wpa_key_mgmt_sae(sm->wpa_key_mgmt)) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002084 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002085 pmkid = buf;
2086 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
2087 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
2088 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
2089 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002090 if (sm->pmksa) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002091 wpa_hexdump(MSG_DEBUG,
2092 "RSN: Message 1/4 PMKID from PMKSA entry",
2093 sm->pmksa->pmkid, PMKID_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2095 sm->pmksa->pmkid, PMKID_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002096 } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
2097 /* No KCK available to derive PMKID */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002098 wpa_printf(MSG_DEBUG,
2099 "RSN: No KCK available to derive PMKID for message 1/4");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002100 pmkid = NULL;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002101#ifdef CONFIG_SAE
2102 } else if (wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
2103 if (sm->pmkid_set) {
2104 wpa_hexdump(MSG_DEBUG,
2105 "RSN: Message 1/4 PMKID from SAE",
2106 sm->pmkid, PMKID_LEN);
2107 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2108 sm->pmkid, PMKID_LEN);
2109 } else {
2110 /* No PMKID available */
2111 wpa_printf(MSG_DEBUG,
2112 "RSN: No SAE PMKID available for message 1/4");
2113 pmkid = NULL;
2114 }
2115#endif /* CONFIG_SAE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002116 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002117 /*
2118 * Calculate PMKID since no PMKSA cache entry was
2119 * available with pre-calculated PMKID.
2120 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002121 rsn_pmkid(sm->PMK, sm->pmk_len, sm->wpa_auth->addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002123 sm->wpa_key_mgmt);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002124 wpa_hexdump(MSG_DEBUG,
2125 "RSN: Message 1/4 PMKID derived from PMK",
2126 &pmkid[2 + RSN_SELECTOR_LEN], PMKID_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002127 }
2128 }
2129 wpa_send_eapol(sm->wpa_auth, sm,
2130 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
2131 sm->ANonce, pmkid, pmkid_len, 0, 0);
2132}
2133
2134
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002135static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002136 const u8 *pmk, unsigned int pmk_len,
2137 struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002139#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002140 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002141 return wpa_auth_derive_ptk_ft(sm, pmk, ptk);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002142#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002143
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002144 return wpa_pmk_to_ptk(pmk, pmk_len, "Pairwise key expansion",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002145 sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce,
2146 ptk, sm->wpa_key_mgmt, sm->pairwise);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002147}
2148
2149
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002150#ifdef CONFIG_FILS
2151
2152int fils_auth_pmk_to_ptk(struct wpa_state_machine *sm, const u8 *pmk,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002153 size_t pmk_len, const u8 *snonce, const u8 *anonce,
2154 const u8 *dhss, size_t dhss_len,
2155 struct wpabuf *g_sta, struct wpabuf *g_ap)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002156{
2157 u8 ick[FILS_ICK_MAX_LEN];
2158 size_t ick_len;
2159 int res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002160 u8 fils_ft[FILS_FT_MAX_LEN];
2161 size_t fils_ft_len = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002162
2163 res = fils_pmk_to_ptk(pmk, pmk_len, sm->addr, sm->wpa_auth->addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002164 snonce, anonce, dhss, dhss_len,
2165 &sm->PTK, ick, &ick_len,
2166 sm->wpa_key_mgmt, sm->pairwise,
2167 fils_ft, &fils_ft_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002168 if (res < 0)
2169 return res;
2170 sm->PTK_valid = TRUE;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002171 sm->tk_already_set = FALSE;
2172
2173#ifdef CONFIG_IEEE80211R_AP
2174 if (fils_ft_len) {
2175 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2176 struct wpa_auth_config *conf = &wpa_auth->conf;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002177 u8 pmk_r0[PMK_LEN_MAX], pmk_r0_name[WPA_PMK_NAME_LEN];
2178 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
2179 size_t pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002180
2181 if (wpa_derive_pmk_r0(fils_ft, fils_ft_len,
2182 conf->ssid, conf->ssid_len,
2183 conf->mobility_domain,
2184 conf->r0_key_holder,
2185 conf->r0_key_holder_len,
Roshan Pius3a1667e2018-07-03 15:17:14 -07002186 sm->addr, pmk_r0, pmk_r0_name,
2187 use_sha384) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002188 return -1;
2189
Roshan Pius3a1667e2018-07-03 15:17:14 -07002190 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0",
2191 pmk_r0, pmk_r0_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002192 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name",
2193 pmk_r0_name, WPA_PMK_NAME_LEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002194 wpa_ft_store_pmk_fils(sm, pmk_r0, pmk_r0_name);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002195 os_memset(fils_ft, 0, sizeof(fils_ft));
2196 }
2197#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002198
2199 res = fils_key_auth_sk(ick, ick_len, snonce, anonce,
2200 sm->addr, sm->wpa_auth->addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002201 g_sta ? wpabuf_head(g_sta) : NULL,
2202 g_sta ? wpabuf_len(g_sta) : 0,
2203 g_ap ? wpabuf_head(g_ap) : NULL,
2204 g_ap ? wpabuf_len(g_ap) : 0,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002205 sm->wpa_key_mgmt, sm->fils_key_auth_sta,
2206 sm->fils_key_auth_ap,
2207 &sm->fils_key_auth_len);
2208 os_memset(ick, 0, sizeof(ick));
2209
2210 /* Store nonces for (Re)Association Request/Response frame processing */
2211 os_memcpy(sm->SNonce, snonce, FILS_NONCE_LEN);
2212 os_memcpy(sm->ANonce, anonce, FILS_NONCE_LEN);
2213
2214 return res;
2215}
2216
2217
2218static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
2219 u8 *buf, size_t buf_len, u16 *_key_data_len)
2220{
2221 struct ieee802_1x_hdr *hdr;
2222 struct wpa_eapol_key *key;
2223 u8 *pos;
2224 u16 key_data_len;
2225 u8 *tmp;
2226 const u8 *aad[1];
2227 size_t aad_len[1];
2228
2229 hdr = (struct ieee802_1x_hdr *) buf;
2230 key = (struct wpa_eapol_key *) (hdr + 1);
2231 pos = (u8 *) (key + 1);
2232 key_data_len = WPA_GET_BE16(pos);
2233 if (key_data_len < AES_BLOCK_SIZE ||
2234 key_data_len > buf_len - sizeof(*hdr) - sizeof(*key) - 2) {
2235 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2236 "No room for AES-SIV data in the frame");
2237 return -1;
2238 }
2239 pos += 2; /* Pointing at the Encrypted Key Data field */
2240
2241 tmp = os_malloc(key_data_len);
2242 if (!tmp)
2243 return -1;
2244
2245 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2246 * to Key Data (exclusive). */
2247 aad[0] = buf;
2248 aad_len[0] = pos - buf;
2249 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, key_data_len,
2250 1, aad, aad_len, tmp) < 0) {
2251 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2252 "Invalid AES-SIV data in the frame");
2253 bin_clear_free(tmp, key_data_len);
2254 return -1;
2255 }
2256
2257 /* AEAD decryption and validation completed successfully */
2258 key_data_len -= AES_BLOCK_SIZE;
2259 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2260 tmp, key_data_len);
2261
2262 /* Replace Key Data field with the decrypted version */
2263 os_memcpy(pos, tmp, key_data_len);
2264 pos -= 2; /* Key Data Length field */
2265 WPA_PUT_BE16(pos, key_data_len);
2266 bin_clear_free(tmp, key_data_len);
2267 if (_key_data_len)
2268 *_key_data_len = key_data_len;
2269 return 0;
2270}
2271
2272
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002273const u8 * wpa_fils_validate_fils_session(struct wpa_state_machine *sm,
2274 const u8 *ies, size_t ies_len,
2275 const u8 *fils_session)
2276{
2277 const u8 *ie, *end;
2278 const u8 *session = NULL;
2279
2280 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2281 wpa_printf(MSG_DEBUG,
2282 "FILS: Not a FILS AKM - reject association");
2283 return NULL;
2284 }
2285
2286 /* Verify Session element */
2287 ie = ies;
2288 end = ((const u8 *) ie) + ies_len;
2289 while (ie + 1 < end) {
2290 if (ie + 2 + ie[1] > end)
2291 break;
2292 if (ie[0] == WLAN_EID_EXTENSION &&
2293 ie[1] >= 1 + FILS_SESSION_LEN &&
2294 ie[2] == WLAN_EID_EXT_FILS_SESSION) {
2295 session = ie;
2296 break;
2297 }
2298 ie += 2 + ie[1];
2299 }
2300
2301 if (!session) {
2302 wpa_printf(MSG_DEBUG,
2303 "FILS: %s: Could not find FILS Session element in Assoc Req - reject",
2304 __func__);
2305 return NULL;
2306 }
2307
2308 if (!fils_session) {
2309 wpa_printf(MSG_DEBUG,
2310 "FILS: %s: Could not find FILS Session element in STA entry - reject",
2311 __func__);
2312 return NULL;
2313 }
2314
2315 if (os_memcmp(fils_session, session + 3, FILS_SESSION_LEN) != 0) {
2316 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
2317 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
2318 fils_session, FILS_SESSION_LEN);
2319 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
2320 session + 3, FILS_SESSION_LEN);
2321 return NULL;
2322 }
2323 return session;
2324}
2325
2326
2327int wpa_fils_validate_key_confirm(struct wpa_state_machine *sm, const u8 *ies,
2328 size_t ies_len)
2329{
2330 struct ieee802_11_elems elems;
2331
2332 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
2333 wpa_printf(MSG_DEBUG,
2334 "FILS: Failed to parse decrypted elements");
2335 return -1;
2336 }
2337
2338 if (!elems.fils_session) {
2339 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
2340 return -1;
2341 }
2342
2343 if (!elems.fils_key_confirm) {
2344 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
2345 return -1;
2346 }
2347
2348 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
2349 wpa_printf(MSG_DEBUG,
2350 "FILS: Unexpected Key-Auth length %d (expected %d)",
2351 elems.fils_key_confirm_len,
2352 (int) sm->fils_key_auth_len);
2353 return -1;
2354 }
2355
2356 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_sta,
2357 sm->fils_key_auth_len) != 0) {
2358 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
2359 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
2360 elems.fils_key_confirm, elems.fils_key_confirm_len);
2361 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
2362 sm->fils_key_auth_sta, sm->fils_key_auth_len);
2363 return -1;
2364 }
2365
2366 return 0;
2367}
2368
2369
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002370int fils_decrypt_assoc(struct wpa_state_machine *sm, const u8 *fils_session,
2371 const struct ieee80211_mgmt *mgmt, size_t frame_len,
2372 u8 *pos, size_t left)
2373{
2374 u16 fc, stype;
2375 const u8 *end, *ie_start, *ie, *session, *crypt;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002376 const u8 *aad[5];
2377 size_t aad_len[5];
2378
2379 if (!sm || !sm->PTK_valid) {
2380 wpa_printf(MSG_DEBUG,
2381 "FILS: No KEK to decrypt Assocication Request frame");
2382 return -1;
2383 }
2384
2385 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2386 wpa_printf(MSG_DEBUG,
2387 "FILS: Not a FILS AKM - reject association");
2388 return -1;
2389 }
2390
2391 end = ((const u8 *) mgmt) + frame_len;
2392 fc = le_to_host16(mgmt->frame_control);
2393 stype = WLAN_FC_GET_STYPE(fc);
2394 if (stype == WLAN_FC_STYPE_REASSOC_REQ)
2395 ie_start = mgmt->u.reassoc_req.variable;
2396 else
2397 ie_start = mgmt->u.assoc_req.variable;
2398 ie = ie_start;
2399
2400 /*
2401 * Find FILS Session element which is the last unencrypted element in
2402 * the frame.
2403 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002404 session = wpa_fils_validate_fils_session(sm, ie, end - ie,
2405 fils_session);
2406 if (!session) {
2407 wpa_printf(MSG_DEBUG, "FILS: Session validation failed");
2408 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002409 }
2410
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002411 crypt = session + 2 + session[1];
2412
2413 if (end - crypt < AES_BLOCK_SIZE) {
2414 wpa_printf(MSG_DEBUG,
2415 "FILS: Too short frame to include AES-SIV data");
2416 return -1;
2417 }
2418
2419 /* AES-SIV AAD vectors */
2420
2421 /* The STA's MAC address */
2422 aad[0] = mgmt->sa;
2423 aad_len[0] = ETH_ALEN;
2424 /* The AP's BSSID */
2425 aad[1] = mgmt->da;
2426 aad_len[1] = ETH_ALEN;
2427 /* The STA's nonce */
2428 aad[2] = sm->SNonce;
2429 aad_len[2] = FILS_NONCE_LEN;
2430 /* The AP's nonce */
2431 aad[3] = sm->ANonce;
2432 aad_len[3] = FILS_NONCE_LEN;
2433 /*
2434 * The (Re)Association Request frame from the Capability Information
2435 * field to the FILS Session element (both inclusive).
2436 */
2437 aad[4] = (const u8 *) &mgmt->u.assoc_req.capab_info;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002438 aad_len[4] = crypt - aad[4];
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002439
2440 if (aes_siv_decrypt(sm->PTK.kek, sm->PTK.kek_len, crypt, end - crypt,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002441 5, aad, aad_len, pos + (crypt - ie_start)) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002442 wpa_printf(MSG_DEBUG,
2443 "FILS: Invalid AES-SIV data in the frame");
2444 return -1;
2445 }
2446 wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Request elements",
2447 pos, left - AES_BLOCK_SIZE);
2448
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002449 if (wpa_fils_validate_key_confirm(sm, pos, left - AES_BLOCK_SIZE) < 0) {
2450 wpa_printf(MSG_DEBUG, "FILS: Key Confirm validation failed");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002451 return -1;
2452 }
2453
2454 return left - AES_BLOCK_SIZE;
2455}
2456
2457
2458int fils_encrypt_assoc(struct wpa_state_machine *sm, u8 *buf,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002459 size_t current_len, size_t max_len,
2460 const struct wpabuf *hlp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002461{
2462 u8 *end = buf + max_len;
2463 u8 *pos = buf + current_len;
2464 struct ieee80211_mgmt *mgmt;
2465 struct wpabuf *plain;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002466 const u8 *aad[5];
2467 size_t aad_len[5];
2468
2469 if (!sm || !sm->PTK_valid)
2470 return -1;
2471
2472 wpa_hexdump(MSG_DEBUG,
2473 "FILS: Association Response frame before FILS processing",
2474 buf, current_len);
2475
2476 mgmt = (struct ieee80211_mgmt *) buf;
2477
2478 /* AES-SIV AAD vectors */
2479
2480 /* The AP's BSSID */
2481 aad[0] = mgmt->sa;
2482 aad_len[0] = ETH_ALEN;
2483 /* The STA's MAC address */
2484 aad[1] = mgmt->da;
2485 aad_len[1] = ETH_ALEN;
2486 /* The AP's nonce */
2487 aad[2] = sm->ANonce;
2488 aad_len[2] = FILS_NONCE_LEN;
2489 /* The STA's nonce */
2490 aad[3] = sm->SNonce;
2491 aad_len[3] = FILS_NONCE_LEN;
2492 /*
2493 * The (Re)Association Response frame from the Capability Information
2494 * field (the same offset in both Association and Reassociation
2495 * Response frames) to the FILS Session element (both inclusive).
2496 */
2497 aad[4] = (const u8 *) &mgmt->u.assoc_resp.capab_info;
2498 aad_len[4] = pos - aad[4];
2499
2500 /* The following elements will be encrypted with AES-SIV */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002501 plain = fils_prepare_plainbuf(sm, hlp);
2502 if (!plain) {
2503 wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
2504 return -1;
2505 }
2506
2507 if (pos + wpabuf_len(plain) + AES_BLOCK_SIZE > end) {
2508 wpa_printf(MSG_DEBUG,
2509 "FILS: Not enough room for FILS elements");
2510 wpabuf_free(plain);
2511 return -1;
2512 }
2513
2514 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: Association Response plaintext",
2515 plain);
2516
2517 if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len,
2518 wpabuf_head(plain), wpabuf_len(plain),
2519 5, aad, aad_len, pos) < 0) {
2520 wpabuf_free(plain);
2521 return -1;
2522 }
2523
2524 wpa_hexdump(MSG_DEBUG,
2525 "FILS: Encrypted Association Response elements",
2526 pos, AES_BLOCK_SIZE + wpabuf_len(plain));
2527 current_len += wpabuf_len(plain) + AES_BLOCK_SIZE;
2528 wpabuf_free(plain);
2529
2530 sm->fils_completed = 1;
2531
2532 return current_len;
2533}
2534
2535
2536static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm,
2537 const struct wpabuf *hlp)
2538{
2539 struct wpabuf *plain;
2540 u8 *len, *tmp, *tmp2;
2541 u8 hdr[2];
2542 u8 *gtk, dummy_gtk[32];
2543 size_t gtk_len;
2544 struct wpa_group *gsm;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002545
2546 plain = wpabuf_alloc(1000);
2547 if (!plain)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002548 return NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002549
2550 /* TODO: FILS Public Key */
2551
2552 /* FILS Key Confirmation */
2553 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2554 wpabuf_put_u8(plain, 1 + sm->fils_key_auth_len); /* Length */
2555 /* Element ID Extension */
2556 wpabuf_put_u8(plain, WLAN_EID_EXT_FILS_KEY_CONFIRM);
2557 wpabuf_put_data(plain, sm->fils_key_auth_ap, sm->fils_key_auth_len);
2558
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002559 /* FILS HLP Container */
2560 if (hlp)
2561 wpabuf_put_buf(plain, hlp);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002562
2563 /* TODO: FILS IP Address Assignment */
2564
2565 /* Key Delivery */
2566 gsm = sm->group;
2567 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2568 len = wpabuf_put(plain, 1);
2569 wpabuf_put_u8(plain, WLAN_EID_EXT_KEY_DELIVERY);
2570 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN,
2571 wpabuf_put(plain, WPA_KEY_RSC_LEN));
2572 /* GTK KDE */
2573 gtk = gsm->GTK[gsm->GN - 1];
2574 gtk_len = gsm->GTK_len;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002575 if (sm->wpa_auth->conf.disable_gtk ||
2576 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002577 /*
2578 * Provide unique random GTK to each STA to prevent use
2579 * of GTK in the BSS.
2580 */
2581 if (random_get_bytes(dummy_gtk, gtk_len) < 0) {
2582 wpabuf_free(plain);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002583 return NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002584 }
2585 gtk = dummy_gtk;
2586 }
2587 hdr[0] = gsm->GN & 0x03;
2588 hdr[1] = 0;
2589 tmp = wpabuf_put(plain, 0);
2590 tmp2 = wpa_add_kde(tmp, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2591 gtk, gtk_len);
2592 wpabuf_put(plain, tmp2 - tmp);
2593
2594 /* IGTK KDE */
2595 tmp = wpabuf_put(plain, 0);
2596 tmp2 = ieee80211w_kde_add(sm, tmp);
2597 wpabuf_put(plain, tmp2 - tmp);
2598
2599 *len = (u8 *) wpabuf_put(plain, 0) - len - 1;
Hai Shalom74f70d42019-02-11 14:42:39 -08002600
2601#ifdef CONFIG_OCV
2602 if (wpa_auth_uses_ocv(sm)) {
2603 struct wpa_channel_info ci;
2604 u8 *pos;
2605
2606 if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
2607 wpa_printf(MSG_WARNING,
2608 "FILS: Failed to get channel info for OCI element");
2609 wpabuf_free(plain);
2610 return NULL;
2611 }
2612
2613 pos = wpabuf_put(plain, OCV_OCI_EXTENDED_LEN);
2614 if (ocv_insert_extended_oci(&ci, pos) < 0) {
2615 wpabuf_free(plain);
2616 return NULL;
2617 }
2618 }
2619#endif /* CONFIG_OCV */
2620
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002621 return plain;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002622}
2623
2624
2625int fils_set_tk(struct wpa_state_machine *sm)
2626{
2627 enum wpa_alg alg;
2628 int klen;
2629
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002630 if (!sm || !sm->PTK_valid) {
2631 wpa_printf(MSG_DEBUG, "FILS: No valid PTK available to set TK");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002632 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002633 }
2634 if (sm->tk_already_set) {
2635 wpa_printf(MSG_DEBUG, "FILS: TK already set to the driver");
2636 return -1;
2637 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002638
2639 alg = wpa_cipher_to_alg(sm->pairwise);
2640 klen = wpa_cipher_key_len(sm->pairwise);
2641
2642 wpa_printf(MSG_DEBUG, "FILS: Configure TK to the driver");
2643 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2644 sm->PTK.tk, klen)) {
2645 wpa_printf(MSG_DEBUG, "FILS: Failed to set TK to the driver");
2646 return -1;
2647 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002648 sm->tk_already_set = TRUE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002649
2650 return 0;
2651}
2652
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002653
2654u8 * hostapd_eid_assoc_fils_session(struct wpa_state_machine *sm, u8 *buf,
2655 const u8 *fils_session, struct wpabuf *hlp)
2656{
2657 struct wpabuf *plain;
2658 u8 *pos = buf;
2659
2660 /* FILS Session */
2661 *pos++ = WLAN_EID_EXTENSION; /* Element ID */
2662 *pos++ = 1 + FILS_SESSION_LEN; /* Length */
2663 *pos++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */
2664 os_memcpy(pos, fils_session, FILS_SESSION_LEN);
2665 pos += FILS_SESSION_LEN;
2666
2667 plain = fils_prepare_plainbuf(sm, hlp);
2668 if (!plain) {
2669 wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
2670 return NULL;
2671 }
2672
2673 os_memcpy(pos, wpabuf_head(plain), wpabuf_len(plain));
2674 pos += wpabuf_len(plain);
2675
2676 wpa_printf(MSG_DEBUG, "%s: plain buf_len: %u", __func__,
2677 (unsigned int) wpabuf_len(plain));
2678 wpabuf_free(plain);
2679 sm->fils_completed = 1;
2680 return pos;
2681}
2682
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002683#endif /* CONFIG_FILS */
2684
2685
Hai Shalom74f70d42019-02-11 14:42:39 -08002686#ifdef CONFIG_OCV
2687int get_sta_tx_parameters(struct wpa_state_machine *sm, int ap_max_chanwidth,
2688 int ap_seg1_idx, int *bandwidth, int *seg1_idx)
2689{
2690 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2691
2692 if (!wpa_auth->cb->get_sta_tx_params)
2693 return -1;
2694 return wpa_auth->cb->get_sta_tx_params(wpa_auth->cb_ctx, sm->addr,
2695 ap_max_chanwidth, ap_seg1_idx,
2696 bandwidth, seg1_idx);
2697}
2698#endif /* CONFIG_OCV */
2699
2700
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002701SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
2702{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002703 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002704 struct wpa_ptk PTK;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002705 int ok = 0, psk_found = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002706 const u8 *pmk = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002707 size_t pmk_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002708 int ft;
2709 const u8 *eapol_key_ie, *key_data, *mic;
2710 u16 key_data_length;
2711 size_t mic_len, eapol_key_ie_len;
2712 struct ieee802_1x_hdr *hdr;
2713 struct wpa_eapol_key *key;
2714 struct wpa_eapol_ie_parse kde;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002715
2716 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
2717 sm->EAPOLKeyReceived = FALSE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002718 sm->update_snonce = FALSE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002719 os_memset(&PTK, 0, sizeof(PTK));
2720
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002721 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002722
2723 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2724 * WPA-PSK: iterate through possible PSKs and select the one matching
2725 * the packet */
2726 for (;;) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002727 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
2728 !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002729 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002730 sm->p2p_dev_addr, pmk, &pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002731 if (pmk == NULL)
2732 break;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002733 psk_found = 1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002734#ifdef CONFIG_IEEE80211R_AP
2735 if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
2736 os_memcpy(sm->xxkey, pmk, pmk_len);
2737 sm->xxkey_len = pmk_len;
2738 }
2739#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002740 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002741 pmk = sm->PMK;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002742 pmk_len = sm->pmk_len;
2743 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002744
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002745 if (wpa_derive_ptk(sm, sm->SNonce, pmk, pmk_len, &PTK) < 0)
2746 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002747
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002748 if (mic_len &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002749 wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002750 sm->last_rx_eapol_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002751 sm->last_rx_eapol_key_len) == 0) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002752 os_memcpy(sm->PMK, pmk, pmk_len);
2753 sm->pmk_len = pmk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002754 ok = 1;
2755 break;
2756 }
2757
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002758#ifdef CONFIG_FILS
2759 if (!mic_len &&
2760 wpa_aead_decrypt(sm, &PTK, sm->last_rx_eapol_key,
2761 sm->last_rx_eapol_key_len, NULL) == 0) {
2762 ok = 1;
2763 break;
2764 }
2765#endif /* CONFIG_FILS */
2766
Roshan Pius3a1667e2018-07-03 15:17:14 -07002767 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
2768 wpa_key_mgmt_sae(sm->wpa_key_mgmt))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002769 break;
2770 }
2771
2772 if (!ok) {
2773 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2774 "invalid MIC in msg 2/4 of 4-Way Handshake");
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002775 if (psk_found)
2776 wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777 return;
2778 }
2779
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002780 /*
2781 * Note: last_rx_eapol_key length fields have already been validated in
2782 * wpa_receive().
2783 */
2784 hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
2785 key = (struct wpa_eapol_key *) (hdr + 1);
2786 mic = (u8 *) (key + 1);
2787 key_data = mic + mic_len + 2;
2788 key_data_length = WPA_GET_BE16(mic + mic_len);
2789 if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
2790 sizeof(*key) - mic_len - 2)
2791 return;
2792
2793 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
2794 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
2795 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
2796 return;
2797 }
2798 if (kde.rsn_ie) {
2799 eapol_key_ie = kde.rsn_ie;
2800 eapol_key_ie_len = kde.rsn_ie_len;
2801 } else if (kde.osen) {
2802 eapol_key_ie = kde.osen;
2803 eapol_key_ie_len = kde.osen_len;
2804 } else {
2805 eapol_key_ie = kde.wpa_ie;
2806 eapol_key_ie_len = kde.wpa_ie_len;
2807 }
2808 ft = sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt);
2809 if (sm->wpa_ie == NULL ||
2810 wpa_compare_rsn_ie(ft, sm->wpa_ie, sm->wpa_ie_len,
2811 eapol_key_ie, eapol_key_ie_len)) {
2812 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2813 "WPA IE from (Re)AssocReq did not match with msg 2/4");
2814 if (sm->wpa_ie) {
2815 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
2816 sm->wpa_ie, sm->wpa_ie_len);
2817 }
2818 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
2819 eapol_key_ie, eapol_key_ie_len);
2820 /* MLME-DEAUTHENTICATE.request */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002821 wpa_sta_disconnect(wpa_auth, sm->addr,
2822 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002823 return;
2824 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002825#ifdef CONFIG_OCV
2826 if (wpa_auth_uses_ocv(sm)) {
2827 struct wpa_channel_info ci;
2828 int tx_chanwidth;
2829 int tx_seg1_idx;
2830
2831 if (wpa_channel_info(wpa_auth, &ci) != 0) {
2832 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2833 "Failed to get channel info to validate received OCI in EAPOL-Key 2/4");
2834 return;
2835 }
2836
2837 if (get_sta_tx_parameters(sm,
2838 channel_width_to_int(ci.chanwidth),
2839 ci.seg1_idx, &tx_chanwidth,
2840 &tx_seg1_idx) < 0)
2841 return;
2842
2843 if (ocv_verify_tx_params(kde.oci, kde.oci_len, &ci,
2844 tx_chanwidth, tx_seg1_idx) != 0) {
2845 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2846 ocv_errorstr);
2847 return;
2848 }
2849 }
2850#endif /* CONFIG_OCV */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002851#ifdef CONFIG_IEEE80211R_AP
2852 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002853 wpa_sta_disconnect(wpa_auth, sm->addr,
2854 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002855 return;
2856 }
2857#endif /* CONFIG_IEEE80211R_AP */
2858#ifdef CONFIG_P2P
2859 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
2860 wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
2861 int idx;
2862 wpa_printf(MSG_DEBUG,
2863 "P2P: IP address requested in EAPOL-Key exchange");
2864 idx = bitfield_get_first_zero(wpa_auth->ip_pool);
2865 if (idx >= 0) {
2866 u32 start = WPA_GET_BE32(wpa_auth->conf.ip_addr_start);
2867 bitfield_set(wpa_auth->ip_pool, idx);
2868 WPA_PUT_BE32(sm->ip_addr, start + idx);
2869 wpa_printf(MSG_DEBUG,
2870 "P2P: Assigned IP address %u.%u.%u.%u to "
2871 MACSTR, sm->ip_addr[0], sm->ip_addr[1],
2872 sm->ip_addr[2], sm->ip_addr[3],
2873 MAC2STR(sm->addr));
2874 }
2875 }
2876#endif /* CONFIG_P2P */
2877
2878#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002879 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2880 /*
2881 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
2882 * with the value we derived.
2883 */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002884 if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
2885 WPA_PMK_NAME_LEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002886 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2887 "PMKR1Name mismatch in FT 4-way "
2888 "handshake");
2889 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
2890 "Supplicant",
2891 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
2892 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
2893 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2894 return;
2895 }
2896 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002897#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002898
2899 sm->pending_1_of_4_timeout = 0;
2900 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
2901
2902 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2903 /* PSK may have changed from the previous choice, so update
2904 * state machine data based on whatever PSK was selected here.
2905 */
2906 os_memcpy(sm->PMK, pmk, PMK_LEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002907 sm->pmk_len = PMK_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002908 }
2909
2910 sm->MICVerified = TRUE;
2911
2912 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
2913 sm->PTK_valid = TRUE;
2914}
2915
2916
2917SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
2918{
2919 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
2920 sm->TimeoutCtr = 0;
2921}
2922
2923
2924#ifdef CONFIG_IEEE80211W
2925
2926static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2927{
2928 if (sm->mgmt_frame_prot) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002929 size_t len;
2930 len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2931 return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002932 }
2933
2934 return 0;
2935}
2936
2937
2938static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2939{
2940 struct wpa_igtk_kde igtk;
2941 struct wpa_group *gsm = sm->group;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002942 u8 rsc[WPA_KEY_RSC_LEN];
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002943 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002944
2945 if (!sm->mgmt_frame_prot)
2946 return pos;
2947
2948 igtk.keyid[0] = gsm->GN_igtk;
2949 igtk.keyid[1] = 0;
2950 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002951 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002952 os_memset(igtk.pn, 0, sizeof(igtk.pn));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002953 else
2954 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002955 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002956 if (sm->wpa_auth->conf.disable_gtk ||
2957 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002958 /*
2959 * Provide unique random IGTK to each STA to prevent use of
2960 * IGTK in the BSS.
2961 */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002962 if (random_get_bytes(igtk.igtk, len) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002963 return pos;
2964 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002965 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002966 (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
2967 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002968
2969 return pos;
2970}
2971
2972#else /* CONFIG_IEEE80211W */
2973
2974static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2975{
2976 return 0;
2977}
2978
2979
2980static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2981{
2982 return pos;
2983}
2984
2985#endif /* CONFIG_IEEE80211W */
2986
2987
Hai Shalom74f70d42019-02-11 14:42:39 -08002988static int ocv_oci_len(struct wpa_state_machine *sm)
2989{
2990#ifdef CONFIG_OCV
2991 if (wpa_auth_uses_ocv(sm))
2992 return OCV_OCI_KDE_LEN;
2993#endif /* CONFIG_OCV */
2994 return 0;
2995}
2996
2997static int ocv_oci_add(struct wpa_state_machine *sm, u8 **argpos)
2998{
2999#ifdef CONFIG_OCV
3000 struct wpa_channel_info ci;
3001
3002 if (!wpa_auth_uses_ocv(sm))
3003 return 0;
3004
3005 if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
3006 wpa_printf(MSG_WARNING,
3007 "Failed to get channel info for OCI element");
3008 return -1;
3009 }
3010
3011 return ocv_insert_oci_kde(&ci, argpos);
3012#else /* CONFIG_OCV */
3013 return 0;
3014#endif /* CONFIG_OCV */
3015}
3016
3017
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003018SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
3019{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003020 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003021 size_t gtk_len, kde_len;
3022 struct wpa_group *gsm = sm->group;
3023 u8 *wpa_ie;
3024 int wpa_ie_len, secure, keyidx, encr = 0;
3025
3026 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
3027 sm->TimeoutEvt = FALSE;
3028
3029 sm->TimeoutCtr++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003030 if (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3031 sm->TimeoutCtr > 1) {
3032 /* Do not allow retransmission of EAPOL-Key msg 3/4 */
3033 return;
3034 }
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003035 if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003036 /* No point in sending the EAPOL-Key - we will disconnect
3037 * immediately following this. */
3038 return;
3039 }
3040
3041 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
3042 GTK[GN], IGTK, [FTIE], [TIE * 2])
3043 */
3044 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
3045 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
3046 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
3047 wpa_ie = sm->wpa_auth->wpa_ie;
3048 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
3049 if (sm->wpa == WPA_VERSION_WPA &&
3050 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
3051 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003052 /* WPA-only STA, remove RSN IE and possible MDIE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003053 wpa_ie = wpa_ie + wpa_ie[1] + 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003054 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
3055 wpa_ie = wpa_ie + wpa_ie[1] + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003056 wpa_ie_len = wpa_ie[1] + 2;
3057 }
3058 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3059 "sending 3/4 msg of 4-Way Handshake");
3060 if (sm->wpa == WPA_VERSION_WPA2) {
3061 /* WPA2 send GTK in the 4-way handshake */
3062 secure = 1;
3063 gtk = gsm->GTK[gsm->GN - 1];
3064 gtk_len = gsm->GTK_len;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003065 if (sm->wpa_auth->conf.disable_gtk ||
3066 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003067 /*
3068 * Provide unique random GTK to each STA to prevent use
3069 * of GTK in the BSS.
3070 */
3071 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
3072 return;
3073 gtk = dummy_gtk;
3074 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003075 keyidx = gsm->GN;
3076 _rsc = rsc;
3077 encr = 1;
3078 } else {
3079 /* WPA does not include GTK in msg 3/4 */
3080 secure = 0;
3081 gtk = NULL;
3082 gtk_len = 0;
3083 keyidx = 0;
3084 _rsc = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003085 if (sm->rx_eapol_key_secure) {
3086 /*
3087 * It looks like Windows 7 supplicant tries to use
3088 * Secure bit in msg 2/4 after having reported Michael
3089 * MIC failure and it then rejects the 4-way handshake
3090 * if msg 3/4 does not set Secure bit. Work around this
3091 * by setting the Secure bit here even in the case of
3092 * WPA if the supplicant used it first.
3093 */
3094 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3095 "STA used Secure bit in WPA msg 2/4 - "
3096 "set Secure for 3/4 as workaround");
3097 secure = 1;
3098 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003099 }
3100
Hai Shalom74f70d42019-02-11 14:42:39 -08003101 kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003102 if (gtk)
3103 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003104#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003105 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3106 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
3107 kde_len += 300; /* FTIE + 2 * TIE */
3108 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003109#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003110#ifdef CONFIG_P2P
3111 if (WPA_GET_BE32(sm->ip_addr) > 0)
3112 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
3113#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003114 kde = os_malloc(kde_len);
3115 if (kde == NULL)
3116 return;
3117
3118 pos = kde;
3119 os_memcpy(pos, wpa_ie, wpa_ie_len);
3120 pos += wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003121#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003122 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003123 int res;
3124 size_t elen;
3125
3126 elen = pos - kde;
3127 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003128 if (res < 0) {
3129 wpa_printf(MSG_ERROR, "FT: Failed to insert "
3130 "PMKR1Name into RSN IE in EAPOL-Key data");
3131 os_free(kde);
3132 return;
3133 }
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003134 pos -= wpa_ie_len;
3135 pos += elen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003136 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003137#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003138 if (gtk) {
3139 u8 hdr[2];
3140 hdr[0] = keyidx & 0x03;
3141 hdr[1] = 0;
3142 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
3143 gtk, gtk_len);
3144 }
3145 pos = ieee80211w_kde_add(sm, pos);
Hai Shalom74f70d42019-02-11 14:42:39 -08003146 if (ocv_oci_add(sm, &pos) < 0) {
3147 os_free(kde);
3148 return;
3149 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003150
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003151#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003152 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3153 int res;
3154 struct wpa_auth_config *conf;
3155
3156 conf = &sm->wpa_auth->conf;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003157 if (sm->assoc_resp_ftie &&
3158 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
3159 os_memcpy(pos, sm->assoc_resp_ftie,
3160 2 + sm->assoc_resp_ftie[1]);
3161 res = 2 + sm->assoc_resp_ftie[1];
3162 } else {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003163 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
3164
3165 res = wpa_write_ftie(conf, use_sha384,
3166 conf->r0_key_holder,
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003167 conf->r0_key_holder_len,
3168 NULL, NULL, pos,
3169 kde + kde_len - pos,
3170 NULL, 0);
3171 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003172 if (res < 0) {
3173 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
3174 "into EAPOL-Key Key Data");
3175 os_free(kde);
3176 return;
3177 }
3178 pos += res;
3179
3180 /* TIE[ReassociationDeadline] (TU) */
3181 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
3182 *pos++ = 5;
3183 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
3184 WPA_PUT_LE32(pos, conf->reassociation_deadline);
3185 pos += 4;
3186
3187 /* TIE[KeyLifetime] (seconds) */
3188 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
3189 *pos++ = 5;
3190 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003191 WPA_PUT_LE32(pos, conf->r0_key_lifetime);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003192 pos += 4;
3193 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003194#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003195#ifdef CONFIG_P2P
3196 if (WPA_GET_BE32(sm->ip_addr) > 0) {
3197 u8 addr[3 * 4];
3198 os_memcpy(addr, sm->ip_addr, 4);
3199 os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
3200 os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
3201 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
3202 addr, sizeof(addr), NULL, 0);
3203 }
3204#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003205
3206 wpa_send_eapol(sm->wpa_auth, sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003207 (secure ? WPA_KEY_INFO_SECURE : 0) |
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003208 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
3209 WPA_KEY_INFO_MIC : 0) |
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003210 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
3211 WPA_KEY_INFO_KEY_TYPE,
3212 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
3213 os_free(kde);
3214}
3215
3216
3217SM_STATE(WPA_PTK, PTKINITDONE)
3218{
3219 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
3220 sm->EAPOLKeyReceived = FALSE;
3221 if (sm->Pair) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003222 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
3223 int klen = wpa_cipher_key_len(sm->pairwise);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003224 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003225 sm->PTK.tk, klen)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003226 wpa_sta_disconnect(sm->wpa_auth, sm->addr,
3227 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003228 return;
3229 }
3230 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
3231 sm->pairwise_set = TRUE;
3232
3233 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
3234 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
3235 eloop_register_timeout(sm->wpa_auth->conf.
3236 wpa_ptk_rekey, 0, wpa_rekey_ptk,
3237 sm->wpa_auth, sm);
3238 }
3239
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003240 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
3241 sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP ||
3242 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003243 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
3244 WPA_EAPOL_authorized, 1);
3245 }
3246 }
3247
3248 if (0 /* IBSS == TRUE */) {
3249 sm->keycount++;
3250 if (sm->keycount == 2) {
3251 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
3252 WPA_EAPOL_portValid, 1);
3253 }
3254 } else {
3255 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
3256 1);
3257 }
3258 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
3259 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
3260 if (sm->wpa == WPA_VERSION_WPA)
3261 sm->PInitAKeys = TRUE;
3262 else
3263 sm->has_GTK = TRUE;
3264 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3265 "pairwise key handshake completed (%s)",
3266 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
3267
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003268#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003269 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003270#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003271}
3272
3273
3274SM_STEP(WPA_PTK)
3275{
3276 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3277
3278 if (sm->Init)
3279 SM_ENTER(WPA_PTK, INITIALIZE);
3280 else if (sm->Disconnect
3281 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
3282 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
3283 "WPA_PTK: sm->Disconnect");
3284 SM_ENTER(WPA_PTK, DISCONNECT);
3285 }
3286 else if (sm->DeauthenticationRequest)
3287 SM_ENTER(WPA_PTK, DISCONNECTED);
3288 else if (sm->AuthenticationRequest)
3289 SM_ENTER(WPA_PTK, AUTHENTICATION);
3290 else if (sm->ReAuthenticationRequest)
3291 SM_ENTER(WPA_PTK, AUTHENTICATION2);
Jouni Malinen1420a892017-10-01 12:32:57 +03003292 else if (sm->PTKRequest) {
3293 if (wpa_auth_sm_ptk_update(sm) < 0)
3294 SM_ENTER(WPA_PTK, DISCONNECTED);
3295 else
3296 SM_ENTER(WPA_PTK, PTKSTART);
3297 } else switch (sm->wpa_ptk_state) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003298 case WPA_PTK_INITIALIZE:
3299 break;
3300 case WPA_PTK_DISCONNECT:
3301 SM_ENTER(WPA_PTK, DISCONNECTED);
3302 break;
3303 case WPA_PTK_DISCONNECTED:
3304 SM_ENTER(WPA_PTK, INITIALIZE);
3305 break;
3306 case WPA_PTK_AUTHENTICATION:
3307 SM_ENTER(WPA_PTK, AUTHENTICATION2);
3308 break;
3309 case WPA_PTK_AUTHENTICATION2:
3310 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
3311 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
3312 WPA_EAPOL_keyRun) > 0)
3313 SM_ENTER(WPA_PTK, INITPMK);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003314 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
3315 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003316 /* FIX: && 802.1X::keyRun */)
3317 SM_ENTER(WPA_PTK, INITPSK);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003318 else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP)
3319 SM_ENTER(WPA_PTK, INITPMK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003320 break;
3321 case WPA_PTK_INITPMK:
3322 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003323 WPA_EAPOL_keyAvailable) > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003324 SM_ENTER(WPA_PTK, PTKSTART);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003325#ifdef CONFIG_DPP
3326 } else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && sm->pmksa) {
3327 SM_ENTER(WPA_PTK, PTKSTART);
3328#endif /* CONFIG_DPP */
3329 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003330 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3331 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3332 "INITPMK - keyAvailable = false");
3333 SM_ENTER(WPA_PTK, DISCONNECT);
3334 }
3335 break;
3336 case WPA_PTK_INITPSK:
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003337 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003338 NULL, NULL)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003339 SM_ENTER(WPA_PTK, PTKSTART);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003340#ifdef CONFIG_SAE
3341 } else if (wpa_auth_uses_sae(sm) && sm->pmksa) {
3342 SM_ENTER(WPA_PTK, PTKSTART);
3343#endif /* CONFIG_SAE */
3344 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003345 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3346 "no PSK configured for the STA");
3347 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3348 SM_ENTER(WPA_PTK, DISCONNECT);
3349 }
3350 break;
3351 case WPA_PTK_PTKSTART:
3352 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3353 sm->EAPOLKeyPairwise)
3354 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3355 else if (sm->TimeoutCtr >
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003356 sm->wpa_auth->conf.wpa_pairwise_update_count) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003357 wpa_auth->dot11RSNA4WayHandshakeFailures++;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003358 wpa_auth_vlogger(
3359 sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3360 "PTKSTART: Retry limit %u reached",
3361 sm->wpa_auth->conf.wpa_pairwise_update_count);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003362 SM_ENTER(WPA_PTK, DISCONNECT);
3363 } else if (sm->TimeoutEvt)
3364 SM_ENTER(WPA_PTK, PTKSTART);
3365 break;
3366 case WPA_PTK_PTKCALCNEGOTIATING:
3367 if (sm->MICVerified)
3368 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
3369 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3370 sm->EAPOLKeyPairwise)
3371 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3372 else if (sm->TimeoutEvt)
3373 SM_ENTER(WPA_PTK, PTKSTART);
3374 break;
3375 case WPA_PTK_PTKCALCNEGOTIATING2:
3376 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3377 break;
3378 case WPA_PTK_PTKINITNEGOTIATING:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003379 if (sm->update_snonce)
3380 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3381 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3382 sm->EAPOLKeyPairwise && sm->MICVerified)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003383 SM_ENTER(WPA_PTK, PTKINITDONE);
3384 else if (sm->TimeoutCtr >
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003385 sm->wpa_auth->conf.wpa_pairwise_update_count ||
3386 (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3387 sm->TimeoutCtr > 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003388 wpa_auth->dot11RSNA4WayHandshakeFailures++;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003389 wpa_auth_vlogger(
3390 sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3391 "PTKINITNEGOTIATING: Retry limit %u reached",
3392 sm->wpa_auth->conf.wpa_pairwise_update_count);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003393 SM_ENTER(WPA_PTK, DISCONNECT);
3394 } else if (sm->TimeoutEvt)
3395 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3396 break;
3397 case WPA_PTK_PTKINITDONE:
3398 break;
3399 }
3400}
3401
3402
3403SM_STATE(WPA_PTK_GROUP, IDLE)
3404{
3405 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
3406 if (sm->Init) {
3407 /* Init flag is not cleared here, so avoid busy
3408 * loop by claiming nothing changed. */
3409 sm->changed = FALSE;
3410 }
3411 sm->GTimeoutCtr = 0;
3412}
3413
3414
3415SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
3416{
3417 u8 rsc[WPA_KEY_RSC_LEN];
3418 struct wpa_group *gsm = sm->group;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003419 const u8 *kde;
3420 u8 *kde_buf = NULL, *pos, hdr[2];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003421 size_t kde_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003422 u8 *gtk, dummy_gtk[32];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003423
3424 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
3425
3426 sm->GTimeoutCtr++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003427 if (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3428 sm->GTimeoutCtr > 1) {
3429 /* Do not allow retransmission of EAPOL-Key group msg 1/2 */
3430 return;
3431 }
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003432 if (sm->GTimeoutCtr > sm->wpa_auth->conf.wpa_group_update_count) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003433 /* No point in sending the EAPOL-Key - we will disconnect
3434 * immediately following this. */
3435 return;
3436 }
3437
3438 if (sm->wpa == WPA_VERSION_WPA)
3439 sm->PInitAKeys = FALSE;
3440 sm->TimeoutEvt = FALSE;
3441 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
3442 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
3443 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
3444 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
3445 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3446 "sending 1/2 msg of Group Key Handshake");
3447
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003448 gtk = gsm->GTK[gsm->GN - 1];
Roshan Pius3a1667e2018-07-03 15:17:14 -07003449 if (sm->wpa_auth->conf.disable_gtk ||
3450 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003451 /*
3452 * Provide unique random GTK to each STA to prevent use
3453 * of GTK in the BSS.
3454 */
3455 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
3456 return;
3457 gtk = dummy_gtk;
3458 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003459 if (sm->wpa == WPA_VERSION_WPA2) {
3460 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
Hai Shalom74f70d42019-02-11 14:42:39 -08003461 ieee80211w_kde_len(sm) + ocv_oci_len(sm);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003462 kde_buf = os_malloc(kde_len);
3463 if (kde_buf == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003464 return;
3465
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003466 kde = pos = kde_buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003467 hdr[0] = gsm->GN & 0x03;
3468 hdr[1] = 0;
3469 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003470 gtk, gsm->GTK_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003471 pos = ieee80211w_kde_add(sm, pos);
Hai Shalom74f70d42019-02-11 14:42:39 -08003472 if (ocv_oci_add(sm, &pos) < 0) {
3473 os_free(kde_buf);
3474 return;
3475 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003476 kde_len = pos - kde;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003477 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003478 kde = gtk;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003479 kde_len = gsm->GTK_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003480 }
3481
3482 wpa_send_eapol(sm->wpa_auth, sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003483 WPA_KEY_INFO_SECURE |
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003484 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
3485 WPA_KEY_INFO_MIC : 0) |
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003486 WPA_KEY_INFO_ACK |
3487 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003488 rsc, NULL, kde, kde_len, gsm->GN, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003489
3490 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003491}
3492
3493
3494SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
3495{
Hai Shalom74f70d42019-02-11 14:42:39 -08003496#ifdef CONFIG_OCV
3497 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3498 const u8 *key_data, *mic;
3499 struct ieee802_1x_hdr *hdr;
3500 struct wpa_eapol_key *key;
3501 struct wpa_eapol_ie_parse kde;
3502 size_t mic_len;
3503 u16 key_data_length;
3504#endif /* CONFIG_OCV */
3505
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003506 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
3507 sm->EAPOLKeyReceived = FALSE;
Hai Shalom74f70d42019-02-11 14:42:39 -08003508
3509#ifdef CONFIG_OCV
3510 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
3511
3512 /*
3513 * Note: last_rx_eapol_key length fields have already been validated in
3514 * wpa_receive().
3515 */
3516 hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
3517 key = (struct wpa_eapol_key *) (hdr + 1);
3518 mic = (u8 *) (key + 1);
3519 key_data = mic + mic_len + 2;
3520 key_data_length = WPA_GET_BE16(mic + mic_len);
3521 if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
3522 sizeof(*key) - mic_len - 2)
3523 return;
3524
3525 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
3526 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
3527 "received EAPOL-Key group msg 2/2 with invalid Key Data contents");
3528 return;
3529 }
3530
3531 if (wpa_auth_uses_ocv(sm)) {
3532 struct wpa_channel_info ci;
3533 int tx_chanwidth;
3534 int tx_seg1_idx;
3535
3536 if (wpa_channel_info(wpa_auth, &ci) != 0) {
3537 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
3538 "Failed to get channel info to validate received OCI in EAPOL-Key group 1/2");
3539 return;
3540 }
3541
3542 if (get_sta_tx_parameters(sm,
3543 channel_width_to_int(ci.chanwidth),
3544 ci.seg1_idx, &tx_chanwidth,
3545 &tx_seg1_idx) < 0)
3546 return;
3547
3548 if (ocv_verify_tx_params(kde.oci, kde.oci_len, &ci,
3549 tx_chanwidth, tx_seg1_idx) != 0) {
3550 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
3551 ocv_errorstr);
3552 return;
3553 }
3554 }
3555#endif /* CONFIG_OCV */
3556
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003557 if (sm->GUpdateStationKeys)
3558 sm->group->GKeyDoneStations--;
3559 sm->GUpdateStationKeys = FALSE;
3560 sm->GTimeoutCtr = 0;
3561 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
3562 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3563 "group key handshake completed (%s)",
3564 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
3565 sm->has_GTK = TRUE;
3566}
3567
3568
3569SM_STATE(WPA_PTK_GROUP, KEYERROR)
3570{
3571 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
3572 if (sm->GUpdateStationKeys)
3573 sm->group->GKeyDoneStations--;
3574 sm->GUpdateStationKeys = FALSE;
3575 sm->Disconnect = TRUE;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003576 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3577 "group key handshake failed (%s) after %u tries",
3578 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN",
3579 sm->wpa_auth->conf.wpa_group_update_count);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003580}
3581
3582
3583SM_STEP(WPA_PTK_GROUP)
3584{
3585 if (sm->Init || sm->PtkGroupInit) {
3586 SM_ENTER(WPA_PTK_GROUP, IDLE);
3587 sm->PtkGroupInit = FALSE;
3588 } else switch (sm->wpa_ptk_group_state) {
3589 case WPA_PTK_GROUP_IDLE:
3590 if (sm->GUpdateStationKeys ||
3591 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
3592 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3593 break;
3594 case WPA_PTK_GROUP_REKEYNEGOTIATING:
3595 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3596 !sm->EAPOLKeyPairwise && sm->MICVerified)
3597 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
3598 else if (sm->GTimeoutCtr >
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003599 sm->wpa_auth->conf.wpa_group_update_count ||
3600 (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3601 sm->GTimeoutCtr > 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003602 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
3603 else if (sm->TimeoutEvt)
3604 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3605 break;
3606 case WPA_PTK_GROUP_KEYERROR:
3607 SM_ENTER(WPA_PTK_GROUP, IDLE);
3608 break;
3609 case WPA_PTK_GROUP_REKEYESTABLISHED:
3610 SM_ENTER(WPA_PTK_GROUP, IDLE);
3611 break;
3612 }
3613}
3614
3615
3616static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
3617 struct wpa_group *group)
3618{
3619 int ret = 0;
3620
3621 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3622 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3623 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
3624 wpa_auth->addr, group->GNonce,
3625 group->GTK[group->GN - 1], group->GTK_len) < 0)
3626 ret = -1;
3627 wpa_hexdump_key(MSG_DEBUG, "GTK",
3628 group->GTK[group->GN - 1], group->GTK_len);
3629
3630#ifdef CONFIG_IEEE80211W
3631 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003632 size_t len;
3633 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003634 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3635 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3636 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
3637 wpa_auth->addr, group->GNonce,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003638 group->IGTK[group->GN_igtk - 4], len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003639 ret = -1;
3640 wpa_hexdump_key(MSG_DEBUG, "IGTK",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003641 group->IGTK[group->GN_igtk - 4], len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003642 }
3643#endif /* CONFIG_IEEE80211W */
3644
3645 return ret;
3646}
3647
3648
3649static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
3650 struct wpa_group *group)
3651{
3652 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3653 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
3654 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
3655 group->wpa_group_state = WPA_GROUP_GTK_INIT;
3656
3657 /* GTK[0..N] = 0 */
3658 os_memset(group->GTK, 0, sizeof(group->GTK));
3659 group->GN = 1;
3660 group->GM = 2;
3661#ifdef CONFIG_IEEE80211W
3662 group->GN_igtk = 4;
3663 group->GM_igtk = 5;
3664#endif /* CONFIG_IEEE80211W */
3665 /* GTK[GN] = CalcGTK() */
3666 wpa_gtk_update(wpa_auth, group);
3667}
3668
3669
3670static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
3671{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003672 if (ctx != NULL && ctx != sm->group)
3673 return 0;
3674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003675 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
3676 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3677 "Not in PTKINITDONE; skip Group Key update");
3678 sm->GUpdateStationKeys = FALSE;
3679 return 0;
3680 }
3681 if (sm->GUpdateStationKeys) {
3682 /*
3683 * This should not really happen, so add a debug log entry.
3684 * Since we clear the GKeyDoneStations before the loop, the
3685 * station needs to be counted here anyway.
3686 */
3687 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3688 "GUpdateStationKeys was already set when "
3689 "marking station for GTK rekeying");
3690 }
3691
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003692 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003693 if (sm->is_wnmsleep)
3694 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003695
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003696 sm->group->GKeyDoneStations++;
3697 sm->GUpdateStationKeys = TRUE;
3698
3699 wpa_sm_step(sm);
3700 return 0;
3701}
3702
3703
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003704#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003705/* update GTK when exiting WNM-Sleep Mode */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003706void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
3707{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003708 if (sm == NULL || sm->is_wnmsleep)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003709 return;
3710
3711 wpa_group_update_sta(sm, NULL);
3712}
3713
3714
3715void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
3716{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003717 if (sm)
3718 sm->is_wnmsleep = !!flag;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003719}
3720
3721
3722int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3723{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003724 struct wpa_group *gsm = sm->group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003725 u8 *start = pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003726
3727 /*
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003728 * GTK subelement:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003729 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003730 * Key[5..32]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003731 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003732 *pos++ = WNM_SLEEP_SUBELEM_GTK;
3733 *pos++ = 11 + gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003734 /* Key ID in B0-B1 of Key Info */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003735 WPA_PUT_LE16(pos, gsm->GN & 0x03);
3736 pos += 2;
3737 *pos++ = gsm->GTK_len;
3738 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003739 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003740 pos += 8;
3741 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
3742 pos += gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003743
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003744 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
3745 gsm->GN);
3746 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003747 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003748
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003749 return pos - start;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003750}
3751
3752
3753#ifdef CONFIG_IEEE80211W
3754int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3755{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003756 struct wpa_group *gsm = sm->group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003757 u8 *start = pos;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003758 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003759
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003760 /*
3761 * IGTK subelement:
3762 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
3763 */
3764 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003765 *pos++ = 2 + 6 + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003766 WPA_PUT_LE16(pos, gsm->GN_igtk);
3767 pos += 2;
3768 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003769 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003770 pos += 6;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003771
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003772 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
3773 pos += len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003774
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003775 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
3776 gsm->GN_igtk);
3777 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003778 gsm->IGTK[gsm->GN_igtk - 4], len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003779
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003780 return pos - start;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003781}
3782#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003783#endif /* CONFIG_WNM_AP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003784
3785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003786static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
3787 struct wpa_group *group)
3788{
3789 int tmp;
3790
3791 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3792 "SETKEYS (VLAN-ID %d)", group->vlan_id);
3793 group->changed = TRUE;
3794 group->wpa_group_state = WPA_GROUP_SETKEYS;
3795 group->GTKReKey = FALSE;
3796 tmp = group->GM;
3797 group->GM = group->GN;
3798 group->GN = tmp;
3799#ifdef CONFIG_IEEE80211W
3800 tmp = group->GM_igtk;
3801 group->GM_igtk = group->GN_igtk;
3802 group->GN_igtk = tmp;
3803#endif /* CONFIG_IEEE80211W */
3804 /* "GKeyDoneStations = GNoStations" is done in more robust way by
3805 * counting the STAs that are marked with GUpdateStationKeys instead of
3806 * including all STAs that could be in not-yet-completed state. */
3807 wpa_gtk_update(wpa_auth, group);
3808
3809 if (group->GKeyDoneStations) {
3810 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
3811 "GKeyDoneStations=%d when starting new GTK rekey",
3812 group->GKeyDoneStations);
3813 group->GKeyDoneStations = 0;
3814 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003815 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003816 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
3817 group->GKeyDoneStations);
3818}
3819
3820
3821static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
3822 struct wpa_group *group)
3823{
3824 int ret = 0;
3825
3826 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003827 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003828 broadcast_ether_addr, group->GN,
3829 group->GTK[group->GN - 1], group->GTK_len) < 0)
3830 ret = -1;
3831
3832#ifdef CONFIG_IEEE80211W
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003833 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
3834 enum wpa_alg alg;
3835 size_t len;
3836
3837 alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
3838 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
3839
3840 if (ret == 0 &&
3841 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
3842 broadcast_ether_addr, group->GN_igtk,
3843 group->IGTK[group->GN_igtk - 4], len) < 0)
3844 ret = -1;
3845 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003846#endif /* CONFIG_IEEE80211W */
3847
3848 return ret;
3849}
3850
3851
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003852static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
3853{
3854 if (sm->group == ctx) {
3855 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
3856 " for discconnection due to fatal failure",
3857 MAC2STR(sm->addr));
3858 sm->Disconnect = TRUE;
3859 }
3860
3861 return 0;
3862}
3863
3864
3865static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
3866 struct wpa_group *group)
3867{
3868 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
3869 group->changed = TRUE;
3870 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
3871 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
3872}
3873
3874
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003875static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
3876 struct wpa_group *group)
3877{
3878 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3879 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
3880 group->changed = TRUE;
3881 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
3882
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003883 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
3884 wpa_group_fatal_failure(wpa_auth, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003885 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003886 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003887
3888 return 0;
3889}
3890
3891
3892static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
3893 struct wpa_group *group)
3894{
3895 if (group->GInit) {
3896 wpa_group_gtk_init(wpa_auth, group);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003897 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
3898 /* Do not allow group operations */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003899 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
3900 group->GTKAuthenticator) {
3901 wpa_group_setkeysdone(wpa_auth, group);
3902 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
3903 group->GTKReKey) {
3904 wpa_group_setkeys(wpa_auth, group);
3905 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
3906 if (group->GKeyDoneStations == 0)
3907 wpa_group_setkeysdone(wpa_auth, group);
3908 else if (group->GTKReKey)
3909 wpa_group_setkeys(wpa_auth, group);
3910 }
3911}
3912
3913
3914static int wpa_sm_step(struct wpa_state_machine *sm)
3915{
3916 if (sm == NULL)
3917 return 0;
3918
3919 if (sm->in_step_loop) {
3920 /* This should not happen, but if it does, make sure we do not
3921 * end up freeing the state machine too early by exiting the
3922 * recursive call. */
3923 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
3924 return 0;
3925 }
3926
3927 sm->in_step_loop = 1;
3928 do {
3929 if (sm->pending_deinit)
3930 break;
3931
3932 sm->changed = FALSE;
3933 sm->wpa_auth->group->changed = FALSE;
3934
3935 SM_STEP_RUN(WPA_PTK);
3936 if (sm->pending_deinit)
3937 break;
3938 SM_STEP_RUN(WPA_PTK_GROUP);
3939 if (sm->pending_deinit)
3940 break;
3941 wpa_group_sm_step(sm->wpa_auth, sm->group);
3942 } while (sm->changed || sm->wpa_auth->group->changed);
3943 sm->in_step_loop = 0;
3944
3945 if (sm->pending_deinit) {
3946 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
3947 "machine deinit for " MACSTR, MAC2STR(sm->addr));
3948 wpa_free_sta_sm(sm);
3949 return 1;
3950 }
3951 return 0;
3952}
3953
3954
3955static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
3956{
3957 struct wpa_state_machine *sm = eloop_ctx;
3958 wpa_sm_step(sm);
3959}
3960
3961
3962void wpa_auth_sm_notify(struct wpa_state_machine *sm)
3963{
3964 if (sm == NULL)
3965 return;
3966 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
3967}
3968
3969
3970void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
3971{
3972 int tmp, i;
3973 struct wpa_group *group;
3974
3975 if (wpa_auth == NULL)
3976 return;
3977
3978 group = wpa_auth->group;
3979
3980 for (i = 0; i < 2; i++) {
3981 tmp = group->GM;
3982 group->GM = group->GN;
3983 group->GN = tmp;
3984#ifdef CONFIG_IEEE80211W
3985 tmp = group->GM_igtk;
3986 group->GM_igtk = group->GN_igtk;
3987 group->GN_igtk = tmp;
3988#endif /* CONFIG_IEEE80211W */
3989 wpa_gtk_update(wpa_auth, group);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003990 wpa_group_config_group_keys(wpa_auth, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003991 }
3992}
3993
3994
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003995static const char * wpa_bool_txt(int val)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003996{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003997 return val ? "TRUE" : "FALSE";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003998}
3999
4000
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004001#define RSN_SUITE "%02x-%02x-%02x-%d"
4002#define RSN_SUITE_ARG(s) \
4003((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
4004
4005int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
4006{
4007 int len = 0, ret;
4008 char pmkid_txt[PMKID_LEN * 2 + 1];
4009#ifdef CONFIG_RSN_PREAUTH
4010 const int preauth = 1;
4011#else /* CONFIG_RSN_PREAUTH */
4012 const int preauth = 0;
4013#endif /* CONFIG_RSN_PREAUTH */
4014
4015 if (wpa_auth == NULL)
4016 return len;
4017
4018 ret = os_snprintf(buf + len, buflen - len,
4019 "dot11RSNAOptionImplemented=TRUE\n"
4020 "dot11RSNAPreauthenticationImplemented=%s\n"
4021 "dot11RSNAEnabled=%s\n"
4022 "dot11RSNAPreauthenticationEnabled=%s\n",
4023 wpa_bool_txt(preauth),
4024 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
4025 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004026 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004027 return len;
4028 len += ret;
4029
4030 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
4031 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
4032
4033 ret = os_snprintf(
4034 buf + len, buflen - len,
4035 "dot11RSNAConfigVersion=%u\n"
4036 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
4037 /* FIX: dot11RSNAConfigGroupCipher */
4038 /* FIX: dot11RSNAConfigGroupRekeyMethod */
4039 /* FIX: dot11RSNAConfigGroupRekeyTime */
4040 /* FIX: dot11RSNAConfigGroupRekeyPackets */
4041 "dot11RSNAConfigGroupRekeyStrict=%u\n"
4042 "dot11RSNAConfigGroupUpdateCount=%u\n"
4043 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
4044 "dot11RSNAConfigGroupCipherSize=%u\n"
4045 "dot11RSNAConfigPMKLifetime=%u\n"
4046 "dot11RSNAConfigPMKReauthThreshold=%u\n"
4047 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
4048 "dot11RSNAConfigSATimeout=%u\n"
4049 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
4050 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
4051 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
4052 "dot11RSNAPMKIDUsed=%s\n"
4053 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
4054 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
4055 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
4056 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
4057 "dot11RSNA4WayHandshakeFailures=%u\n"
4058 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
4059 RSN_VERSION,
4060 !!wpa_auth->conf.wpa_strict_rekey,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004061 wpa_auth->conf.wpa_group_update_count,
4062 wpa_auth->conf.wpa_pairwise_update_count,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004063 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004064 dot11RSNAConfigPMKLifetime,
4065 dot11RSNAConfigPMKReauthThreshold,
4066 dot11RSNAConfigSATimeout,
4067 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
4068 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
4069 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
4070 pmkid_txt,
4071 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
4072 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
4073 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
4074 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
4075 wpa_auth->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004076 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004077 return len;
4078 len += ret;
4079
4080 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
4081 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
4082
4083 /* Private MIB */
4084 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
4085 wpa_auth->group->wpa_group_state);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004086 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004087 return len;
4088 len += ret;
4089
4090 return len;
4091}
4092
4093
4094int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
4095{
4096 int len = 0, ret;
4097 u32 pairwise = 0;
4098
4099 if (sm == NULL)
4100 return 0;
4101
4102 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
4103
4104 /* dot11RSNAStatsEntry */
4105
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004106 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
4107 WPA_PROTO_RSN : WPA_PROTO_WPA,
4108 sm->pairwise);
4109 if (pairwise == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004110 return 0;
4111
4112 ret = os_snprintf(
4113 buf + len, buflen - len,
4114 /* TODO: dot11RSNAStatsIndex */
4115 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
4116 "dot11RSNAStatsVersion=1\n"
4117 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
4118 /* TODO: dot11RSNAStatsTKIPICVErrors */
4119 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
4120 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
4121 /* TODO: dot11RSNAStatsCCMPReplays */
4122 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
4123 /* TODO: dot11RSNAStatsTKIPReplays */,
4124 MAC2STR(sm->addr),
4125 RSN_SUITE_ARG(pairwise),
4126 sm->dot11RSNAStatsTKIPLocalMICFailures,
4127 sm->dot11RSNAStatsTKIPRemoteMICFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004128 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004129 return len;
4130 len += ret;
4131
4132 /* Private MIB */
4133 ret = os_snprintf(buf + len, buflen - len,
4134 "hostapdWPAPTKState=%d\n"
4135 "hostapdWPAPTKGroupState=%d\n",
4136 sm->wpa_ptk_state,
4137 sm->wpa_ptk_group_state);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004138 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004139 return len;
4140 len += ret;
4141
4142 return len;
4143}
4144
4145
4146void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
4147{
4148 if (wpa_auth)
4149 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
4150}
4151
4152
4153int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
4154{
4155 return sm && sm->pairwise_set;
4156}
4157
4158
4159int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
4160{
4161 return sm->pairwise;
4162}
4163
4164
Hai Shalom74f70d42019-02-11 14:42:39 -08004165const u8 * wpa_auth_get_pmk(struct wpa_state_machine *sm, int *len)
4166{
4167 if (!sm)
4168 return NULL;
4169 *len = sm->pmk_len;
4170 return sm->PMK;
4171}
4172
4173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004174int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
4175{
4176 if (sm == NULL)
4177 return -1;
4178 return sm->wpa_key_mgmt;
4179}
4180
4181
4182int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
4183{
4184 if (sm == NULL)
4185 return 0;
4186 return sm->wpa;
4187}
4188
4189
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02004190int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm)
4191{
4192 if (!sm || !wpa_key_mgmt_ft(sm->wpa_key_mgmt))
4193 return 0;
4194 return sm->tk_already_set;
4195}
4196
4197
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004198int wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine *sm)
4199{
4200 if (!sm || !wpa_key_mgmt_fils(sm->wpa_key_mgmt))
4201 return 0;
4202 return sm->tk_already_set;
4203}
4204
4205
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004206int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
4207 struct rsn_pmksa_cache_entry *entry)
4208{
4209 if (sm == NULL || sm->pmksa != entry)
4210 return -1;
4211 sm->pmksa = NULL;
4212 return 0;
4213}
4214
4215
4216struct rsn_pmksa_cache_entry *
4217wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
4218{
4219 return sm ? sm->pmksa : NULL;
4220}
4221
4222
4223void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
4224{
4225 if (sm)
4226 sm->dot11RSNAStatsTKIPLocalMICFailures++;
4227}
4228
4229
4230const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
4231{
4232 if (wpa_auth == NULL)
4233 return NULL;
4234 *len = wpa_auth->wpa_ie_len;
4235 return wpa_auth->wpa_ie;
4236}
4237
4238
4239int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004240 unsigned int pmk_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004241 int session_timeout, struct eapol_state_machine *eapol)
4242{
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07004243 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
4244 sm->wpa_auth->conf.disable_pmksa_caching)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004245 return -1;
4246
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004247 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004248 if (pmk_len > PMK_LEN_SUITE_B_192)
4249 pmk_len = PMK_LEN_SUITE_B_192;
4250 } else if (pmk_len > PMK_LEN) {
4251 pmk_len = PMK_LEN;
4252 }
4253
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004254 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004255 sm->PTK.kck, sm->PTK.kck_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004256 sm->wpa_auth->addr, sm->addr, session_timeout,
4257 eapol, sm->wpa_key_mgmt))
4258 return 0;
4259
4260 return -1;
4261}
4262
4263
4264int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
4265 const u8 *pmk, size_t len, const u8 *sta_addr,
4266 int session_timeout,
4267 struct eapol_state_machine *eapol)
4268{
4269 if (wpa_auth == NULL)
4270 return -1;
4271
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004272 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, NULL,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004273 NULL, 0,
4274 wpa_auth->addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004275 sta_addr, session_timeout, eapol,
4276 WPA_KEY_MGMT_IEEE8021X))
4277 return 0;
4278
4279 return -1;
4280}
4281
4282
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004283int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004284 const u8 *pmk, const u8 *pmkid)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004285{
4286 if (wpa_auth->conf.disable_pmksa_caching)
4287 return -1;
4288
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004289 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN, pmkid,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004290 NULL, 0,
4291 wpa_auth->addr, addr, 0, NULL,
4292 WPA_KEY_MGMT_SAE))
4293 return 0;
4294
4295 return -1;
4296}
4297
4298
Roshan Pius3a1667e2018-07-03 15:17:14 -07004299void wpa_auth_add_sae_pmkid(struct wpa_state_machine *sm, const u8 *pmkid)
4300{
4301 os_memcpy(sm->pmkid, pmkid, PMKID_LEN);
4302 sm->pmkid_set = 1;
4303}
4304
4305
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004306int wpa_auth_pmksa_add2(struct wpa_authenticator *wpa_auth, const u8 *addr,
4307 const u8 *pmk, size_t pmk_len, const u8 *pmkid,
4308 int session_timeout, int akmp)
4309{
4310 if (wpa_auth->conf.disable_pmksa_caching)
4311 return -1;
4312
4313 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, pmk_len, pmkid,
4314 NULL, 0, wpa_auth->addr, addr, session_timeout,
4315 NULL, akmp))
4316 return 0;
4317
4318 return -1;
4319}
4320
4321
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004322void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
4323 const u8 *sta_addr)
4324{
4325 struct rsn_pmksa_cache_entry *pmksa;
4326
4327 if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
4328 return;
4329 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
4330 if (pmksa) {
4331 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
4332 MACSTR " based on request", MAC2STR(sta_addr));
4333 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
4334 }
4335}
4336
4337
Dmitry Shmidte4663042016-04-04 10:07:49 -07004338int wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf,
4339 size_t len)
4340{
4341 if (!wpa_auth || !wpa_auth->pmksa)
4342 return 0;
4343 return pmksa_cache_auth_list(wpa_auth->pmksa, buf, len);
4344}
4345
4346
4347void wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth)
4348{
4349 if (wpa_auth && wpa_auth->pmksa)
4350 pmksa_cache_auth_flush(wpa_auth->pmksa);
4351}
4352
4353
Paul Stewart092955c2017-02-06 09:13:09 -08004354#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
4355#ifdef CONFIG_MESH
4356
4357int wpa_auth_pmksa_list_mesh(struct wpa_authenticator *wpa_auth, const u8 *addr,
4358 char *buf, size_t len)
4359{
4360 if (!wpa_auth || !wpa_auth->pmksa)
4361 return 0;
4362
4363 return pmksa_cache_auth_list_mesh(wpa_auth->pmksa, addr, buf, len);
4364}
4365
4366
4367struct rsn_pmksa_cache_entry *
4368wpa_auth_pmksa_create_entry(const u8 *aa, const u8 *spa, const u8 *pmk,
4369 const u8 *pmkid, int expiration)
4370{
4371 struct rsn_pmksa_cache_entry *entry;
4372 struct os_reltime now;
4373
4374 entry = pmksa_cache_auth_create_entry(pmk, PMK_LEN, pmkid, NULL, 0, aa,
4375 spa, 0, NULL, WPA_KEY_MGMT_SAE);
4376 if (!entry)
4377 return NULL;
4378
4379 os_get_reltime(&now);
4380 entry->expiration = now.sec + expiration;
4381 return entry;
4382}
4383
4384
4385int wpa_auth_pmksa_add_entry(struct wpa_authenticator *wpa_auth,
4386 struct rsn_pmksa_cache_entry *entry)
4387{
4388 int ret;
4389
4390 if (!wpa_auth || !wpa_auth->pmksa)
4391 return -1;
4392
4393 ret = pmksa_cache_auth_add_entry(wpa_auth->pmksa, entry);
4394 if (ret < 0)
4395 wpa_printf(MSG_DEBUG,
4396 "RSN: Failed to store external PMKSA cache for "
4397 MACSTR, MAC2STR(entry->spa));
4398
4399 return ret;
4400}
4401
4402#endif /* CONFIG_MESH */
4403#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
4404
4405
Dmitry Shmidte4663042016-04-04 10:07:49 -07004406struct rsn_pmksa_cache_entry *
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004407wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
4408 const u8 *pmkid)
Dmitry Shmidte4663042016-04-04 10:07:49 -07004409{
4410 if (!wpa_auth || !wpa_auth->pmksa)
4411 return NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004412 return pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, pmkid);
Dmitry Shmidte4663042016-04-04 10:07:49 -07004413}
4414
4415
4416void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa,
4417 struct wpa_state_machine *sm,
4418 struct wpa_authenticator *wpa_auth,
4419 u8 *pmkid, u8 *pmk)
4420{
4421 if (!sm)
4422 return;
4423
4424 sm->pmksa = pmksa;
4425 os_memcpy(pmk, pmksa->pmk, PMK_LEN);
4426 os_memcpy(pmkid, pmksa->pmkid, PMKID_LEN);
4427 os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmksa->pmkid, PMKID_LEN);
4428}
4429
4430
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004431/*
4432 * Remove and free the group from wpa_authenticator. This is triggered by a
4433 * callback to make sure nobody is currently iterating the group list while it
4434 * gets modified.
4435 */
4436static void wpa_group_free(struct wpa_authenticator *wpa_auth,
4437 struct wpa_group *group)
4438{
4439 struct wpa_group *prev = wpa_auth->group;
4440
4441 wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d",
4442 group->vlan_id);
4443
4444 while (prev) {
4445 if (prev->next == group) {
4446 /* This never frees the special first group as needed */
4447 prev->next = group->next;
4448 os_free(group);
4449 break;
4450 }
4451 prev = prev->next;
4452 }
4453
4454}
4455
4456
4457/* Increase the reference counter for group */
4458static void wpa_group_get(struct wpa_authenticator *wpa_auth,
4459 struct wpa_group *group)
4460{
4461 /* Skip the special first group */
4462 if (wpa_auth->group == group)
4463 return;
4464
4465 group->references++;
4466}
4467
4468
4469/* Decrease the reference counter and maybe free the group */
4470static void wpa_group_put(struct wpa_authenticator *wpa_auth,
4471 struct wpa_group *group)
4472{
4473 /* Skip the special first group */
4474 if (wpa_auth->group == group)
4475 return;
4476
4477 group->references--;
4478 if (group->references)
4479 return;
4480 wpa_group_free(wpa_auth, group);
4481}
4482
4483
4484/*
4485 * Add a group that has its references counter set to zero. Caller needs to
4486 * call wpa_group_get() on the return value to mark the entry in use.
4487 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004488static struct wpa_group *
4489wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4490{
4491 struct wpa_group *group;
4492
4493 if (wpa_auth == NULL || wpa_auth->group == NULL)
4494 return NULL;
4495
4496 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
4497 vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004498 group = wpa_group_init(wpa_auth, vlan_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004499 if (group == NULL)
4500 return NULL;
4501
4502 group->next = wpa_auth->group->next;
4503 wpa_auth->group->next = group;
4504
4505 return group;
4506}
4507
4508
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004509/*
4510 * Enforce that the group state machine for the VLAN is running, increase
4511 * reference counter as interface is up. References might have been increased
4512 * even if a negative value is returned.
4513 * Returns: -1 on error (group missing, group already failed); otherwise, 0
4514 */
4515int wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4516{
4517 struct wpa_group *group;
4518
4519 if (wpa_auth == NULL)
4520 return 0;
4521
4522 group = wpa_auth->group;
4523 while (group) {
4524 if (group->vlan_id == vlan_id)
4525 break;
4526 group = group->next;
4527 }
4528
4529 if (group == NULL) {
4530 group = wpa_auth_add_group(wpa_auth, vlan_id);
4531 if (group == NULL)
4532 return -1;
4533 }
4534
4535 wpa_printf(MSG_DEBUG,
4536 "WPA: Ensure group state machine running for VLAN ID %d",
4537 vlan_id);
4538
4539 wpa_group_get(wpa_auth, group);
4540 group->num_setup_iface++;
4541
4542 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4543 return -1;
4544
4545 return 0;
4546}
4547
4548
4549/*
4550 * Decrease reference counter, expected to be zero afterwards.
4551 * returns: -1 on error (group not found, group in fail state)
4552 * -2 if wpa_group is still referenced
4553 * 0 else
4554 */
4555int wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4556{
4557 struct wpa_group *group;
4558 int ret = 0;
4559
4560 if (wpa_auth == NULL)
4561 return 0;
4562
4563 group = wpa_auth->group;
4564 while (group) {
4565 if (group->vlan_id == vlan_id)
4566 break;
4567 group = group->next;
4568 }
4569
4570 if (group == NULL)
4571 return -1;
4572
4573 wpa_printf(MSG_DEBUG,
4574 "WPA: Try stopping group state machine for VLAN ID %d",
4575 vlan_id);
4576
4577 if (group->num_setup_iface <= 0) {
4578 wpa_printf(MSG_ERROR,
4579 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
4580 vlan_id);
4581 return -1;
4582 }
4583 group->num_setup_iface--;
4584
4585 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4586 ret = -1;
4587
4588 if (group->references > 1) {
4589 wpa_printf(MSG_DEBUG,
4590 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
4591 vlan_id);
4592 ret = -2;
4593 }
4594
4595 wpa_group_put(wpa_auth, group);
4596
4597 return ret;
4598}
4599
4600
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004601int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
4602{
4603 struct wpa_group *group;
4604
4605 if (sm == NULL || sm->wpa_auth == NULL)
4606 return 0;
4607
4608 group = sm->wpa_auth->group;
4609 while (group) {
4610 if (group->vlan_id == vlan_id)
4611 break;
4612 group = group->next;
4613 }
4614
4615 if (group == NULL) {
4616 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
4617 if (group == NULL)
4618 return -1;
4619 }
4620
4621 if (sm->group == group)
4622 return 0;
4623
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004624 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4625 return -1;
4626
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004627 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
4628 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
4629
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004630 wpa_group_get(sm->wpa_auth, group);
4631 wpa_group_put(sm->wpa_auth, sm->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004632 sm->group = group;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004633
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004634 return 0;
4635}
4636
4637
4638void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
4639 struct wpa_state_machine *sm, int ack)
4640{
4641 if (wpa_auth == NULL || sm == NULL)
4642 return;
4643 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
4644 " ack=%d", MAC2STR(sm->addr), ack);
4645 if (sm->pending_1_of_4_timeout && ack) {
4646 /*
4647 * Some deployed supplicant implementations update their SNonce
4648 * for each EAPOL-Key 2/4 message even within the same 4-way
4649 * handshake and then fail to use the first SNonce when
4650 * deriving the PTK. This results in unsuccessful 4-way
4651 * handshake whenever the relatively short initial timeout is
4652 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
4653 * around this by increasing the timeout now that we know that
4654 * the station has received the frame.
4655 */
4656 int timeout_ms = eapol_key_timeout_subseq;
4657 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
4658 "timeout by %u ms because of acknowledged frame",
4659 timeout_ms);
4660 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
4661 eloop_register_timeout(timeout_ms / 1000,
4662 (timeout_ms % 1000) * 1000,
4663 wpa_send_eapol_timeout, wpa_auth, sm);
4664 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004665
4666#ifdef CONFIG_TESTING_OPTIONS
4667 if (sm->eapol_status_cb) {
4668 sm->eapol_status_cb(sm->eapol_status_cb_ctx1,
4669 sm->eapol_status_cb_ctx2);
4670 sm->eapol_status_cb = NULL;
4671 }
4672#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004673}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004674
4675
4676int wpa_auth_uses_sae(struct wpa_state_machine *sm)
4677{
4678 if (sm == NULL)
4679 return 0;
4680 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
4681}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004682
4683
4684int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
4685{
4686 if (sm == NULL)
4687 return 0;
4688 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
4689}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004690
4691
4692#ifdef CONFIG_P2P
4693int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
4694{
4695 if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
4696 return -1;
4697 os_memcpy(addr, sm->ip_addr, 4);
4698 return 0;
4699}
4700#endif /* CONFIG_P2P */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004701
4702
4703int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
4704 struct radius_das_attrs *attr)
4705{
4706 return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
4707}
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004708
4709
4710void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)
4711{
4712 struct wpa_group *group;
4713
4714 if (!wpa_auth)
4715 return;
4716 for (group = wpa_auth->group; group; group = group->next)
4717 wpa_group_config_group_keys(wpa_auth, group);
4718}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004719
4720
4721#ifdef CONFIG_FILS
4722
4723struct wpa_auth_fils_iter_data {
4724 struct wpa_authenticator *auth;
4725 const u8 *cache_id;
4726 struct rsn_pmksa_cache_entry *pmksa;
4727 const u8 *spa;
4728 const u8 *pmkid;
4729};
4730
4731
4732static int wpa_auth_fils_iter(struct wpa_authenticator *a, void *ctx)
4733{
4734 struct wpa_auth_fils_iter_data *data = ctx;
4735
4736 if (a == data->auth || !a->conf.fils_cache_id_set ||
4737 os_memcmp(a->conf.fils_cache_id, data->cache_id,
4738 FILS_CACHE_ID_LEN) != 0)
4739 return 0;
4740 data->pmksa = pmksa_cache_auth_get(a->pmksa, data->spa, data->pmkid);
4741 return data->pmksa != NULL;
4742}
4743
4744
4745struct rsn_pmksa_cache_entry *
4746wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator *wpa_auth,
4747 const u8 *sta_addr, const u8 *pmkid)
4748{
4749 struct wpa_auth_fils_iter_data idata;
4750
4751 if (!wpa_auth->conf.fils_cache_id_set)
4752 return NULL;
4753 idata.auth = wpa_auth;
4754 idata.cache_id = wpa_auth->conf.fils_cache_id;
4755 idata.pmksa = NULL;
4756 idata.spa = sta_addr;
4757 idata.pmkid = pmkid;
4758 wpa_auth_for_each_auth(wpa_auth, wpa_auth_fils_iter, &idata);
4759 return idata.pmksa;
4760}
4761
4762
4763#ifdef CONFIG_IEEE80211R_AP
Roshan Pius3a1667e2018-07-03 15:17:14 -07004764int wpa_auth_write_fte(struct wpa_authenticator *wpa_auth, int use_sha384,
4765 u8 *buf, size_t len)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004766{
4767 struct wpa_auth_config *conf = &wpa_auth->conf;
4768
Roshan Pius3a1667e2018-07-03 15:17:14 -07004769 return wpa_write_ftie(conf, use_sha384, conf->r0_key_holder,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004770 conf->r0_key_holder_len,
4771 NULL, NULL, buf, len, NULL, 0);
4772}
4773#endif /* CONFIG_IEEE80211R_AP */
4774
4775
4776void wpa_auth_get_fils_aead_params(struct wpa_state_machine *sm,
4777 u8 *fils_anonce, u8 *fils_snonce,
4778 u8 *fils_kek, size_t *fils_kek_len)
4779{
4780 os_memcpy(fils_anonce, sm->ANonce, WPA_NONCE_LEN);
4781 os_memcpy(fils_snonce, sm->SNonce, WPA_NONCE_LEN);
4782 os_memcpy(fils_kek, sm->PTK.kek, WPA_KEK_MAX_LEN);
4783 *fils_kek_len = sm->PTK.kek_len;
4784}
4785
4786#endif /* CONFIG_FILS */
4787
4788
Roshan Pius3a1667e2018-07-03 15:17:14 -07004789#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004790
4791int wpa_auth_resend_m1(struct wpa_state_machine *sm, int change_anonce,
4792 void (*cb)(void *ctx1, void *ctx2),
4793 void *ctx1, void *ctx2)
4794{
4795 const u8 *anonce = sm->ANonce;
4796 u8 anonce_buf[WPA_NONCE_LEN];
4797
4798 if (change_anonce) {
4799 if (random_get_bytes(anonce_buf, WPA_NONCE_LEN))
4800 return -1;
4801 anonce = anonce_buf;
4802 }
4803
4804 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
4805 "sending 1/4 msg of 4-Way Handshake (TESTING)");
4806 wpa_send_eapol(sm->wpa_auth, sm,
4807 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
4808 anonce, NULL, 0, 0, 0);
4809 return 0;
4810}
4811
4812
4813int wpa_auth_resend_m3(struct wpa_state_machine *sm,
4814 void (*cb)(void *ctx1, void *ctx2),
4815 void *ctx1, void *ctx2)
4816{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004817 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
4818#ifdef CONFIG_IEEE80211W
4819 u8 *opos;
4820#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004821 size_t gtk_len, kde_len;
4822 struct wpa_group *gsm = sm->group;
4823 u8 *wpa_ie;
4824 int wpa_ie_len, secure, keyidx, encr = 0;
4825
4826 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
4827 GTK[GN], IGTK, [FTIE], [TIE * 2])
4828 */
4829
4830 /* Use 0 RSC */
4831 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
4832 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
4833 wpa_ie = sm->wpa_auth->wpa_ie;
4834 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
4835 if (sm->wpa == WPA_VERSION_WPA &&
4836 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
4837 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
4838 /* WPA-only STA, remove RSN IE and possible MDIE */
4839 wpa_ie = wpa_ie + wpa_ie[1] + 2;
4840 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
4841 wpa_ie = wpa_ie + wpa_ie[1] + 2;
4842 wpa_ie_len = wpa_ie[1] + 2;
4843 }
4844 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
4845 "sending 3/4 msg of 4-Way Handshake (TESTING)");
4846 if (sm->wpa == WPA_VERSION_WPA2) {
4847 /* WPA2 send GTK in the 4-way handshake */
4848 secure = 1;
4849 gtk = gsm->GTK[gsm->GN - 1];
4850 gtk_len = gsm->GTK_len;
4851 keyidx = gsm->GN;
4852 _rsc = rsc;
4853 encr = 1;
4854 } else {
4855 /* WPA does not include GTK in msg 3/4 */
4856 secure = 0;
4857 gtk = NULL;
4858 gtk_len = 0;
4859 keyidx = 0;
4860 _rsc = NULL;
4861 if (sm->rx_eapol_key_secure) {
4862 /*
4863 * It looks like Windows 7 supplicant tries to use
4864 * Secure bit in msg 2/4 after having reported Michael
4865 * MIC failure and it then rejects the 4-way handshake
4866 * if msg 3/4 does not set Secure bit. Work around this
4867 * by setting the Secure bit here even in the case of
4868 * WPA if the supplicant used it first.
4869 */
4870 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
4871 "STA used Secure bit in WPA msg 2/4 - "
4872 "set Secure for 3/4 as workaround");
4873 secure = 1;
4874 }
4875 }
4876
Hai Shalom74f70d42019-02-11 14:42:39 -08004877 kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004878 if (gtk)
4879 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
4880#ifdef CONFIG_IEEE80211R_AP
4881 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
4882 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
4883 kde_len += 300; /* FTIE + 2 * TIE */
4884 }
4885#endif /* CONFIG_IEEE80211R_AP */
4886 kde = os_malloc(kde_len);
4887 if (kde == NULL)
4888 return -1;
4889
4890 pos = kde;
4891 os_memcpy(pos, wpa_ie, wpa_ie_len);
4892 pos += wpa_ie_len;
4893#ifdef CONFIG_IEEE80211R_AP
4894 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
4895 int res;
4896 size_t elen;
4897
4898 elen = pos - kde;
4899 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
4900 if (res < 0) {
4901 wpa_printf(MSG_ERROR, "FT: Failed to insert "
4902 "PMKR1Name into RSN IE in EAPOL-Key data");
4903 os_free(kde);
4904 return -1;
4905 }
4906 pos -= wpa_ie_len;
4907 pos += elen;
4908 }
4909#endif /* CONFIG_IEEE80211R_AP */
4910 if (gtk) {
4911 u8 hdr[2];
4912 hdr[0] = keyidx & 0x03;
4913 hdr[1] = 0;
4914 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
4915 gtk, gtk_len);
4916 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07004917#ifdef CONFIG_IEEE80211W
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004918 opos = pos;
4919 pos = ieee80211w_kde_add(sm, pos);
Roshan Pius3a1667e2018-07-03 15:17:14 -07004920 if (pos - opos >= 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
4921 /* skip KDE header and keyid */
4922 opos += 2 + RSN_SELECTOR_LEN + 2;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004923 os_memset(opos, 0, 6); /* clear PN */
4924 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07004925#endif /* CONFIG_IEEE80211W */
Hai Shalom74f70d42019-02-11 14:42:39 -08004926 if (ocv_oci_add(sm, &pos) < 0) {
4927 os_free(kde);
4928 return -1;
4929 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004930
4931#ifdef CONFIG_IEEE80211R_AP
4932 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
4933 int res;
4934 struct wpa_auth_config *conf;
4935
4936 conf = &sm->wpa_auth->conf;
4937 if (sm->assoc_resp_ftie &&
4938 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
4939 os_memcpy(pos, sm->assoc_resp_ftie,
4940 2 + sm->assoc_resp_ftie[1]);
4941 res = 2 + sm->assoc_resp_ftie[1];
4942 } else {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004943 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
4944
4945 res = wpa_write_ftie(conf, use_sha384,
4946 conf->r0_key_holder,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004947 conf->r0_key_holder_len,
4948 NULL, NULL, pos,
4949 kde + kde_len - pos,
4950 NULL, 0);
4951 }
4952 if (res < 0) {
4953 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
4954 "into EAPOL-Key Key Data");
4955 os_free(kde);
4956 return -1;
4957 }
4958 pos += res;
4959
4960 /* TIE[ReassociationDeadline] (TU) */
4961 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
4962 *pos++ = 5;
4963 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
4964 WPA_PUT_LE32(pos, conf->reassociation_deadline);
4965 pos += 4;
4966
4967 /* TIE[KeyLifetime] (seconds) */
4968 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
4969 *pos++ = 5;
4970 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004971 WPA_PUT_LE32(pos, conf->r0_key_lifetime);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004972 pos += 4;
4973 }
4974#endif /* CONFIG_IEEE80211R_AP */
4975
4976 wpa_send_eapol(sm->wpa_auth, sm,
4977 (secure ? WPA_KEY_INFO_SECURE : 0) |
4978 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
4979 WPA_KEY_INFO_MIC : 0) |
4980 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
4981 WPA_KEY_INFO_KEY_TYPE,
4982 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
4983 os_free(kde);
4984 return 0;
4985}
4986
4987
4988int wpa_auth_resend_group_m1(struct wpa_state_machine *sm,
4989 void (*cb)(void *ctx1, void *ctx2),
4990 void *ctx1, void *ctx2)
4991{
4992 u8 rsc[WPA_KEY_RSC_LEN];
4993 struct wpa_group *gsm = sm->group;
4994 const u8 *kde;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004995 u8 *kde_buf = NULL, *pos, hdr[2];
4996#ifdef CONFIG_IEEE80211W
4997 u8 *opos;
4998#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004999 size_t kde_len;
5000 u8 *gtk;
5001
5002 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
5003 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
5004 /* Use 0 RSC */
5005 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
5006 "sending 1/2 msg of Group Key Handshake (TESTING)");
5007
5008 gtk = gsm->GTK[gsm->GN - 1];
5009 if (sm->wpa == WPA_VERSION_WPA2) {
5010 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
Hai Shalom74f70d42019-02-11 14:42:39 -08005011 ieee80211w_kde_len(sm) + ocv_oci_len(sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005012 kde_buf = os_malloc(kde_len);
5013 if (kde_buf == NULL)
5014 return -1;
5015
5016 kde = pos = kde_buf;
5017 hdr[0] = gsm->GN & 0x03;
5018 hdr[1] = 0;
5019 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
5020 gtk, gsm->GTK_len);
Roshan Pius3a1667e2018-07-03 15:17:14 -07005021#ifdef CONFIG_IEEE80211W
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005022 opos = pos;
5023 pos = ieee80211w_kde_add(sm, pos);
Roshan Pius3a1667e2018-07-03 15:17:14 -07005024 if (pos - opos >=
5025 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
5026 /* skip KDE header and keyid */
5027 opos += 2 + RSN_SELECTOR_LEN + 2;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005028 os_memset(opos, 0, 6); /* clear PN */
5029 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07005030#endif /* CONFIG_IEEE80211W */
Hai Shalom74f70d42019-02-11 14:42:39 -08005031 if (ocv_oci_add(sm, &pos) < 0) {
5032 os_free(kde_buf);
5033 return -1;
5034 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005035 kde_len = pos - kde;
5036 } else {
5037 kde = gtk;
5038 kde_len = gsm->GTK_len;
5039 }
5040
5041 sm->eapol_status_cb = cb;
5042 sm->eapol_status_cb_ctx1 = ctx1;
5043 sm->eapol_status_cb_ctx2 = ctx2;
5044
5045 wpa_send_eapol(sm->wpa_auth, sm,
5046 WPA_KEY_INFO_SECURE |
5047 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
5048 WPA_KEY_INFO_MIC : 0) |
5049 WPA_KEY_INFO_ACK |
5050 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
5051 rsc, NULL, kde, kde_len, gsm->GN, 1);
5052
5053 os_free(kde_buf);
5054 return 0;
5055}
5056
Roshan Pius3a1667e2018-07-03 15:17:14 -07005057
5058int wpa_auth_rekey_gtk(struct wpa_authenticator *wpa_auth)
5059{
5060 if (!wpa_auth)
5061 return -1;
5062 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
5063 return eloop_register_timeout(0, 0, wpa_rekey_gtk, wpa_auth, NULL);
5064}
5065
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005066#endif /* CONFIG_TESTING_OPTIONS */