blob: 7b690d730d23100d21a43ca6895869150f815a98 [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
Hai Shalom021b0b52019-04-10 11:17:58 -07003 * Copyright (c) 2004-2019, 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,
Hai Shalom021b0b52019-04-10 11:17:58 -0700117 const u8 *prev_psk, size_t *psk_len,
118 int *vlan_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700119{
Paul Stewart092955c2017-02-06 09:13:09 -0800120 if (wpa_auth->cb->get_psk == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121 return NULL;
Paul Stewart092955c2017-02-06 09:13:09 -0800122 return wpa_auth->cb->get_psk(wpa_auth->cb_ctx, addr, p2p_dev_addr,
Hai Shalom021b0b52019-04-10 11:17:58 -0700123 prev_psk, psk_len, vlan_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700124}
125
126
127static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
128 const u8 *addr, u8 *msk, size_t *len)
129{
Paul Stewart092955c2017-02-06 09:13:09 -0800130 if (wpa_auth->cb->get_msk == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800132 return wpa_auth->cb->get_msk(wpa_auth->cb_ctx, addr, msk, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133}
134
135
136static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
137 int vlan_id,
138 enum wpa_alg alg, const u8 *addr, int idx,
139 u8 *key, size_t key_len)
140{
Paul Stewart092955c2017-02-06 09:13:09 -0800141 if (wpa_auth->cb->set_key == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700142 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800143 return wpa_auth->cb->set_key(wpa_auth->cb_ctx, vlan_id, alg, addr, idx,
144 key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700145}
146
147
148static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
149 const u8 *addr, int idx, u8 *seq)
150{
Paul Stewart092955c2017-02-06 09:13:09 -0800151 if (wpa_auth->cb->get_seqnum == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700152 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800153 return wpa_auth->cb->get_seqnum(wpa_auth->cb_ctx, addr, idx, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700154}
155
156
157static inline int
158wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
159 const u8 *data, size_t data_len, int encrypt)
160{
Paul Stewart092955c2017-02-06 09:13:09 -0800161 if (wpa_auth->cb->send_eapol == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700162 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800163 return wpa_auth->cb->send_eapol(wpa_auth->cb_ctx, addr, data, data_len,
164 encrypt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165}
166
167
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800168#ifdef CONFIG_MESH
169static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth,
170 const u8 *addr)
171{
Paul Stewart092955c2017-02-06 09:13:09 -0800172 if (wpa_auth->cb->start_ampe == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800173 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800174 return wpa_auth->cb->start_ampe(wpa_auth->cb_ctx, addr);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800175}
176#endif /* CONFIG_MESH */
177
178
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700179int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
180 int (*cb)(struct wpa_state_machine *sm, void *ctx),
181 void *cb_ctx)
182{
Paul Stewart092955c2017-02-06 09:13:09 -0800183 if (wpa_auth->cb->for_each_sta == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184 return 0;
Paul Stewart092955c2017-02-06 09:13:09 -0800185 return wpa_auth->cb->for_each_sta(wpa_auth->cb_ctx, cb, cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700186}
187
188
189int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
190 int (*cb)(struct wpa_authenticator *a, void *ctx),
191 void *cb_ctx)
192{
Paul Stewart092955c2017-02-06 09:13:09 -0800193 if (wpa_auth->cb->for_each_auth == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194 return 0;
Paul Stewart092955c2017-02-06 09:13:09 -0800195 return wpa_auth->cb->for_each_auth(wpa_auth->cb_ctx, cb, cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700196}
197
198
199void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
200 logger_level level, const char *txt)
201{
Paul Stewart092955c2017-02-06 09:13:09 -0800202 if (wpa_auth->cb->logger == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203 return;
Paul Stewart092955c2017-02-06 09:13:09 -0800204 wpa_auth->cb->logger(wpa_auth->cb_ctx, addr, level, txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205}
206
207
208void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
209 logger_level level, const char *fmt, ...)
210{
211 char *format;
212 int maxlen;
213 va_list ap;
214
Paul Stewart092955c2017-02-06 09:13:09 -0800215 if (wpa_auth->cb->logger == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700216 return;
217
218 maxlen = os_strlen(fmt) + 100;
219 format = os_malloc(maxlen);
220 if (!format)
221 return;
222
223 va_start(ap, fmt);
224 vsnprintf(format, maxlen, fmt, ap);
225 va_end(ap);
226
227 wpa_auth_logger(wpa_auth, addr, level, format);
228
229 os_free(format);
230}
231
232
233static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700234 const u8 *addr, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700235{
Paul Stewart092955c2017-02-06 09:13:09 -0800236 if (wpa_auth->cb->disconnect == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700237 return;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700238 wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR " (reason %u)",
239 MAC2STR(addr), reason);
240 wpa_auth->cb->disconnect(wpa_auth->cb_ctx, addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700241}
242
243
Hai Shalom74f70d42019-02-11 14:42:39 -0800244#ifdef CONFIG_OCV
245static int wpa_channel_info(struct wpa_authenticator *wpa_auth,
246 struct wpa_channel_info *ci)
247{
248 if (!wpa_auth->cb->channel_info)
249 return -1;
250 return wpa_auth->cb->channel_info(wpa_auth->cb_ctx, ci);
251}
252#endif /* CONFIG_OCV */
253
254
Hai Shalom021b0b52019-04-10 11:17:58 -0700255static int wpa_auth_update_vlan(struct wpa_authenticator *wpa_auth,
256 const u8 *addr, int vlan_id)
257{
258 if (!wpa_auth->cb->update_vlan)
259 return -1;
260 return wpa_auth->cb->update_vlan(wpa_auth->cb_ctx, addr, vlan_id);
261}
262
263
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700264static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
265{
266 struct wpa_authenticator *wpa_auth = eloop_ctx;
267
268 if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
269 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
270 "initialization.");
271 } else {
272 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
273 wpa_hexdump_key(MSG_DEBUG, "GMK",
274 wpa_auth->group->GMK, WPA_GMK_LEN);
275 }
276
277 if (wpa_auth->conf.wpa_gmk_rekey) {
278 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
279 wpa_rekey_gmk, wpa_auth, NULL);
280 }
281}
282
283
284static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
285{
286 struct wpa_authenticator *wpa_auth = eloop_ctx;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700287 struct wpa_group *group, *next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288
289 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700290 group = wpa_auth->group;
291 while (group) {
292 wpa_group_get(wpa_auth, group);
293
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700294 group->GTKReKey = TRUE;
295 do {
296 group->changed = FALSE;
297 wpa_group_sm_step(wpa_auth, group);
298 } while (group->changed);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700299
300 next = group->next;
301 wpa_group_put(wpa_auth, group);
302 group = next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303 }
304
305 if (wpa_auth->conf.wpa_group_rekey) {
306 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
307 0, wpa_rekey_gtk, wpa_auth, NULL);
308 }
309}
310
311
312static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
313{
314 struct wpa_authenticator *wpa_auth = eloop_ctx;
315 struct wpa_state_machine *sm = timeout_ctx;
316
317 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
318 wpa_request_new_ptk(sm);
319 wpa_sm_step(sm);
320}
321
322
Hai Shalom81f62d82019-07-22 12:10:00 -0700323void wpa_auth_set_ptk_rekey_timer(struct wpa_state_machine *sm)
324{
325 if (sm && sm->wpa_auth->conf.wpa_ptk_rekey) {
326 wpa_printf(MSG_DEBUG, "WPA: Start PTK rekeying timer for "
327 MACSTR " (%d seconds)", MAC2STR(sm->addr),
328 sm->wpa_auth->conf.wpa_ptk_rekey);
329 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
330 eloop_register_timeout(sm->wpa_auth->conf.wpa_ptk_rekey, 0,
331 wpa_rekey_ptk, sm->wpa_auth, sm);
332 }
333}
334
335
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
337{
338 if (sm->pmksa == ctx)
339 sm->pmksa = NULL;
340 return 0;
341}
342
343
344static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
345 void *ctx)
346{
347 struct wpa_authenticator *wpa_auth = ctx;
348 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
349}
350
351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
353 struct wpa_group *group)
354{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800355 u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356 u8 rkey[32];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800357 unsigned long ptr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358
359 if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
360 return -1;
361 wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
362
363 /*
364 * Counter = PRF-256(Random number, "Init Counter",
365 * Local MAC Address || Time)
366 */
367 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
368 wpa_get_ntp_timestamp(buf + ETH_ALEN);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800369 ptr = (unsigned long) group;
370 os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
Hai Shalom74f70d42019-02-11 14:42:39 -0800371#ifdef TEST_FUZZ
372 os_memset(buf + ETH_ALEN, 0xab, 8);
373 os_memset(buf + ETH_ALEN + 8, 0xcd, sizeof(ptr));
374#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 if (random_get_bytes(rkey, sizeof(rkey)) < 0)
376 return -1;
377
378 if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
379 group->Counter, WPA_NONCE_LEN) < 0)
380 return -1;
381 wpa_hexdump_key(MSG_DEBUG, "Key Counter",
382 group->Counter, WPA_NONCE_LEN);
383
384 return 0;
385}
386
387
388static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800389 int vlan_id, int delay_init)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390{
391 struct wpa_group *group;
392
393 group = os_zalloc(sizeof(struct wpa_group));
394 if (group == NULL)
395 return NULL;
396
397 group->GTKAuthenticator = TRUE;
398 group->vlan_id = vlan_id;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700399 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700400
401 if (random_pool_ready() != 1) {
402 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
403 "for secure operations - update keys later when "
404 "the first station connects");
405 }
406
407 /*
408 * Set initial GMK/Counter value here. The actual values that will be
409 * used in negotiations will be set once the first station tries to
410 * connect. This allows more time for collecting additional randomness
411 * on embedded devices.
412 */
413 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
414 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
415 "initialization.");
416 os_free(group);
417 return NULL;
418 }
419
420 group->GInit = TRUE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800421 if (delay_init) {
422 wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
423 "until Beacon frames have been configured");
424 /* Initialization is completed in wpa_init_keys(). */
425 } else {
426 wpa_group_sm_step(wpa_auth, group);
427 group->GInit = FALSE;
428 wpa_group_sm_step(wpa_auth, group);
429 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700430
431 return group;
432}
433
434
435/**
436 * wpa_init - Initialize WPA authenticator
437 * @addr: Authenticator address
438 * @conf: Configuration for WPA authenticator
439 * @cb: Callback functions for WPA authenticator
440 * Returns: Pointer to WPA authenticator data or %NULL on failure
441 */
442struct wpa_authenticator * wpa_init(const u8 *addr,
443 struct wpa_auth_config *conf,
Paul Stewart092955c2017-02-06 09:13:09 -0800444 const struct wpa_auth_callbacks *cb,
445 void *cb_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700446{
447 struct wpa_authenticator *wpa_auth;
448
449 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
450 if (wpa_auth == NULL)
451 return NULL;
452 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
453 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
Paul Stewart092955c2017-02-06 09:13:09 -0800454 wpa_auth->cb = cb;
455 wpa_auth->cb_ctx = cb_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700456
457 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
458 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
459 os_free(wpa_auth);
460 return NULL;
461 }
462
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800463 wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464 if (wpa_auth->group == NULL) {
465 os_free(wpa_auth->wpa_ie);
466 os_free(wpa_auth);
467 return NULL;
468 }
469
470 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
471 wpa_auth);
472 if (wpa_auth->pmksa == NULL) {
473 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800474 os_free(wpa_auth->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475 os_free(wpa_auth->wpa_ie);
476 os_free(wpa_auth);
477 return NULL;
478 }
479
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800480#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700481 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
482 if (wpa_auth->ft_pmk_cache == NULL) {
483 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800484 os_free(wpa_auth->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700485 os_free(wpa_auth->wpa_ie);
486 pmksa_cache_auth_deinit(wpa_auth->pmksa);
487 os_free(wpa_auth);
488 return NULL;
489 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800490#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700491
492 if (wpa_auth->conf.wpa_gmk_rekey) {
493 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
494 wpa_rekey_gmk, wpa_auth, NULL);
495 }
496
497 if (wpa_auth->conf.wpa_group_rekey) {
498 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
499 wpa_rekey_gtk, wpa_auth, NULL);
500 }
501
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800502#ifdef CONFIG_P2P
503 if (WPA_GET_BE32(conf->ip_addr_start)) {
504 int count = WPA_GET_BE32(conf->ip_addr_end) -
505 WPA_GET_BE32(conf->ip_addr_start) + 1;
506 if (count > 1000)
507 count = 1000;
508 if (count > 0)
509 wpa_auth->ip_pool = bitfield_alloc(count);
510 }
511#endif /* CONFIG_P2P */
512
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 return wpa_auth;
514}
515
516
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800517int wpa_init_keys(struct wpa_authenticator *wpa_auth)
518{
519 struct wpa_group *group = wpa_auth->group;
520
521 wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
522 "keys");
523 wpa_group_sm_step(wpa_auth, group);
524 group->GInit = FALSE;
525 wpa_group_sm_step(wpa_auth, group);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800526 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
527 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800528 return 0;
529}
530
531
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532/**
533 * wpa_deinit - Deinitialize WPA authenticator
534 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
535 */
536void wpa_deinit(struct wpa_authenticator *wpa_auth)
537{
538 struct wpa_group *group, *prev;
539
540 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
541 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
542
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700543 pmksa_cache_auth_deinit(wpa_auth->pmksa);
544
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800545#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
547 wpa_auth->ft_pmk_cache = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700548 wpa_ft_deinit(wpa_auth);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800549#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700550
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800551#ifdef CONFIG_P2P
552 bitfield_free(wpa_auth->ip_pool);
553#endif /* CONFIG_P2P */
554
555
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700556 os_free(wpa_auth->wpa_ie);
557
558 group = wpa_auth->group;
559 while (group) {
560 prev = group;
561 group = group->next;
562 os_free(prev);
563 }
564
565 os_free(wpa_auth);
566}
567
568
569/**
570 * wpa_reconfig - Update WPA authenticator configuration
571 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
572 * @conf: Configuration for WPA authenticator
573 */
574int wpa_reconfig(struct wpa_authenticator *wpa_auth,
575 struct wpa_auth_config *conf)
576{
577 struct wpa_group *group;
578 if (wpa_auth == NULL)
579 return 0;
580
581 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
582 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
583 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
584 return -1;
585 }
586
587 /*
588 * Reinitialize GTK to make sure it is suitable for the new
589 * configuration.
590 */
591 group = wpa_auth->group;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700592 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593 group->GInit = TRUE;
594 wpa_group_sm_step(wpa_auth, group);
595 group->GInit = FALSE;
596 wpa_group_sm_step(wpa_auth, group);
597
598 return 0;
599}
600
601
602struct wpa_state_machine *
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700603wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
604 const u8 *p2p_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700605{
606 struct wpa_state_machine *sm;
607
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800608 if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
609 return NULL;
610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700611 sm = os_zalloc(sizeof(struct wpa_state_machine));
612 if (sm == NULL)
613 return NULL;
614 os_memcpy(sm->addr, addr, ETH_ALEN);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700615 if (p2p_dev_addr)
616 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700617
618 sm->wpa_auth = wpa_auth;
619 sm->group = wpa_auth->group;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700620 wpa_group_get(sm->wpa_auth, sm->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700621
622 return sm;
623}
624
625
626int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
627 struct wpa_state_machine *sm)
628{
629 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
630 return -1;
631
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800632#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700633 if (sm->ft_completed) {
634 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
635 "FT authentication already completed - do not "
636 "start 4-way handshake");
Dmitry Shmidt71757432014-06-02 13:50:35 -0700637 /* Go to PTKINITDONE state to allow GTK rekeying */
638 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800639 sm->Pair = TRUE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700640 return 0;
641 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800642#endif /* CONFIG_IEEE80211R_AP */
643
644#ifdef CONFIG_FILS
645 if (sm->fils_completed) {
646 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
647 "FILS authentication already completed - do not start 4-way handshake");
648 /* Go to PTKINITDONE state to allow GTK rekeying */
649 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800650 sm->Pair = TRUE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800651 return 0;
652 }
653#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654
655 if (sm->started) {
656 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
657 sm->ReAuthenticationRequest = TRUE;
658 return wpa_sm_step(sm);
659 }
660
661 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
662 "start authentication");
663 sm->started = 1;
664
665 sm->Init = TRUE;
666 if (wpa_sm_step(sm) == 1)
667 return 1; /* should not really happen */
668 sm->Init = FALSE;
669 sm->AuthenticationRequest = TRUE;
670 return wpa_sm_step(sm);
671}
672
673
674void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
675{
676 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
677 * reassociates back to the same AP while the previous entry for the
678 * STA has not yet been removed. */
679 if (sm == NULL)
680 return;
681
682 sm->wpa_key_mgmt = 0;
683}
684
685
686static void wpa_free_sta_sm(struct wpa_state_machine *sm)
687{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800688#ifdef CONFIG_P2P
689 if (WPA_GET_BE32(sm->ip_addr)) {
690 u32 start;
691 wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
692 "address %u.%u.%u.%u from " MACSTR,
693 sm->ip_addr[0], sm->ip_addr[1],
694 sm->ip_addr[2], sm->ip_addr[3],
695 MAC2STR(sm->addr));
696 start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
697 bitfield_clear(sm->wpa_auth->ip_pool,
698 WPA_GET_BE32(sm->ip_addr) - start);
699 }
700#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700701 if (sm->GUpdateStationKeys) {
702 sm->group->GKeyDoneStations--;
703 sm->GUpdateStationKeys = FALSE;
704 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800705#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700706 os_free(sm->assoc_resp_ftie);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700707 wpabuf_free(sm->ft_pending_req_ies);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800708#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700709 os_free(sm->last_rx_eapol_key);
710 os_free(sm->wpa_ie);
Hai Shalomc3565922019-10-28 11:58:20 -0700711 os_free(sm->rsnxe);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700712 wpa_group_put(sm->wpa_auth, sm->group);
Hai Shalom021b0b52019-04-10 11:17:58 -0700713#ifdef CONFIG_DPP2
714 wpabuf_clear_free(sm->dpp_z);
715#endif /* CONFIG_DPP2 */
Hai Shalom1dc4d202019-04-29 16:22:27 -0700716 bin_clear_free(sm, sizeof(*sm));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700717}
718
719
720void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
721{
722 if (sm == NULL)
723 return;
724
725 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
726 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
727 "strict rekeying - force GTK rekey since STA "
728 "is leaving");
Roshan Pius3a1667e2018-07-03 15:17:14 -0700729 if (eloop_deplete_timeout(0, 500000, wpa_rekey_gtk,
730 sm->wpa_auth, NULL) == -1)
731 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
732 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733 }
734
735 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
736 sm->pending_1_of_4_timeout = 0;
737 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
738 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700739#ifdef CONFIG_IEEE80211R_AP
740 wpa_ft_sta_deinit(sm);
741#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742 if (sm->in_step_loop) {
743 /* Must not free state machine while wpa_sm_step() is running.
744 * Freeing will be completed in the end of wpa_sm_step(). */
745 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
746 "machine deinit for " MACSTR, MAC2STR(sm->addr));
747 sm->pending_deinit = 1;
748 } else
749 wpa_free_sta_sm(sm);
750}
751
752
753static void wpa_request_new_ptk(struct wpa_state_machine *sm)
754{
755 if (sm == NULL)
756 return;
757
758 sm->PTKRequest = TRUE;
759 sm->PTK_valid = 0;
760}
761
762
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800763static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700764 const u8 *replay_counter)
765{
766 int i;
767 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800768 if (!ctr[i].valid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700769 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800770 if (os_memcmp(replay_counter, ctr[i].counter,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771 WPA_REPLAY_COUNTER_LEN) == 0)
772 return 1;
773 }
774 return 0;
775}
776
777
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800778static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
779 const u8 *replay_counter)
780{
781 int i;
782 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
783 if (ctr[i].valid &&
784 (replay_counter == NULL ||
785 os_memcmp(replay_counter, ctr[i].counter,
786 WPA_REPLAY_COUNTER_LEN) == 0))
787 ctr[i].valid = FALSE;
788 }
789}
790
791
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800792#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700793static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
794 struct wpa_state_machine *sm,
795 struct wpa_eapol_ie_parse *kde)
796{
797 struct wpa_ie_data ie;
798 struct rsn_mdie *mdie;
799
800 if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
801 ie.num_pmkid != 1 || ie.pmkid == NULL) {
802 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
803 "FT 4-way handshake message 2/4");
804 return -1;
805 }
806
807 os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
808 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
809 sm->sup_pmk_r1_name, PMKID_LEN);
810
811 if (!kde->mdie || !kde->ftie) {
812 wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
813 "message 2/4", kde->mdie ? "FTIE" : "MDIE");
814 return -1;
815 }
816
817 mdie = (struct rsn_mdie *) (kde->mdie + 2);
818 if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
819 os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
820 MOBILITY_DOMAIN_ID_LEN) != 0) {
821 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
822 return -1;
823 }
824
825 if (sm->assoc_resp_ftie &&
826 (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
827 os_memcmp(kde->ftie, sm->assoc_resp_ftie,
828 2 + sm->assoc_resp_ftie[1]) != 0)) {
829 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
830 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
831 kde->ftie, kde->ftie_len);
832 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
833 sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
834 return -1;
835 }
836
837 return 0;
838}
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800839#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840
841
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800842static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
843 struct wpa_state_machine *sm, int group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800844{
845 /* Supplicant reported a Michael MIC error */
846 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
847 "received EAPOL-Key Error Request "
848 "(STA detected Michael MIC failure (group=%d))",
849 group);
850
851 if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
852 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
853 "ignore Michael MIC failure report since "
854 "group cipher is not TKIP");
855 } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
856 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
857 "ignore Michael MIC failure report since "
858 "pairwise cipher is not TKIP");
859 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800860 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
861 return 1; /* STA entry was removed */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800862 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
863 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
864 }
865
866 /*
867 * Error report is not a request for a new key handshake, but since
868 * Authenticator may do it, let's change the keys now anyway.
869 */
870 wpa_request_new_ptk(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800871 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800872}
873
874
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800875static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
876 size_t data_len)
877{
878 struct wpa_ptk PTK;
879 int ok = 0;
880 const u8 *pmk = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700881 size_t pmk_len;
Hai Shalom021b0b52019-04-10 11:17:58 -0700882 int vlan_id = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800883
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800884 os_memset(&PTK, 0, sizeof(PTK));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800885 for (;;) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -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 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
Hai Shalom021b0b52019-04-10 11:17:58 -0700889 sm->p2p_dev_addr, pmk, &pmk_len,
890 &vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800891 if (pmk == NULL)
892 break;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700893#ifdef CONFIG_IEEE80211R_AP
894 if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
895 os_memcpy(sm->xxkey, pmk, pmk_len);
896 sm->xxkey_len = pmk_len;
897 }
898#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800899 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800900 pmk = sm->PMK;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800901 pmk_len = sm->pmk_len;
902 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800903
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800904 if (wpa_derive_ptk(sm, sm->alt_SNonce, pmk, pmk_len, &PTK) < 0)
905 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800906
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700907 if (wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK,
908 data, data_len) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -0700909 if (sm->PMK != pmk) {
910 os_memcpy(sm->PMK, pmk, pmk_len);
911 sm->pmk_len = pmk_len;
912 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800913 ok = 1;
914 break;
915 }
916
Roshan Pius3a1667e2018-07-03 15:17:14 -0700917 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
918 wpa_key_mgmt_sae(sm->wpa_key_mgmt))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800919 break;
920 }
921
922 if (!ok) {
923 wpa_printf(MSG_DEBUG,
924 "WPA: Earlier SNonce did not result in matching MIC");
925 return -1;
926 }
927
928 wpa_printf(MSG_DEBUG,
929 "WPA: Earlier SNonce resulted in matching MIC");
930 sm->alt_snonce_valid = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -0700931
932 if (vlan_id && wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
933 wpa_auth_update_vlan(sm->wpa_auth, sm->addr, vlan_id) < 0)
934 return -1;
935
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800936 os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
937 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
Hai Shalom81f62d82019-07-22 12:10:00 -0700938 forced_memzero(&PTK, sizeof(PTK));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800939 sm->PTK_valid = TRUE;
940
941 return 0;
942}
943
944
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945void wpa_receive(struct wpa_authenticator *wpa_auth,
946 struct wpa_state_machine *sm,
947 u8 *data, size_t data_len)
948{
949 struct ieee802_1x_hdr *hdr;
950 struct wpa_eapol_key *key;
951 u16 key_info, key_data_length;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700952 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST } msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700953 char *msgtxt;
954 struct wpa_eapol_ie_parse kde;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800955 const u8 *key_data;
956 size_t keyhdrlen, mic_len;
957 u8 *mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700958
959 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
960 return;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800961 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL data", data, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700962
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700963 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800964 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800965
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800966 if (data_len < sizeof(*hdr) + keyhdrlen) {
967 wpa_printf(MSG_DEBUG, "WPA: Ignore too short EAPOL-Key frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700968 return;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800969 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970
971 hdr = (struct ieee802_1x_hdr *) data;
972 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800973 mic = (u8 *) (key + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700974 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800975 key_data = mic + mic_len + 2;
976 key_data_length = WPA_GET_BE16(mic + mic_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800977 wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800978 " key_info=0x%x type=%u mic_len=%u key_data_length=%u",
979 MAC2STR(sm->addr), key_info, key->type,
980 (unsigned int) mic_len, key_data_length);
981 wpa_hexdump(MSG_MSGDUMP,
982 "WPA: EAPOL-Key header (ending before Key MIC)",
983 key, sizeof(*key));
984 wpa_hexdump(MSG_MSGDUMP, "WPA: EAPOL-Key Key MIC",
985 mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800986 if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
988 "key_data overflow (%d > %lu)",
989 key_data_length,
990 (unsigned long) (data_len - sizeof(*hdr) -
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800991 keyhdrlen));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700992 return;
993 }
994
995 if (sm->wpa == WPA_VERSION_WPA2) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800996 if (key->type == EAPOL_KEY_TYPE_WPA) {
997 /*
998 * Some deployed station implementations seem to send
999 * msg 4/4 with incorrect type value in WPA2 mode.
1000 */
1001 wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
1002 "with unexpected WPA type in RSN mode");
1003 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001004 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
1005 "unexpected type %d in RSN mode",
1006 key->type);
1007 return;
1008 }
1009 } else {
1010 if (key->type != EAPOL_KEY_TYPE_WPA) {
1011 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
1012 "unexpected type %d in WPA mode",
1013 key->type);
1014 return;
1015 }
1016 }
1017
1018 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
1019 WPA_NONCE_LEN);
1020 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
1021 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1022
1023 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
1024 * are set */
1025
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001026 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1027 wpa_printf(MSG_DEBUG, "WPA: Ignore SMK message");
1028 return;
1029 }
1030
1031 if (key_info & WPA_KEY_INFO_REQUEST) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032 msg = REQUEST;
1033 msgtxt = "Request";
1034 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1035 msg = GROUP_2;
1036 msgtxt = "2/2 Group";
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001037 } else if (key_data_length == 0 ||
1038 (mic_len == 0 && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) &&
1039 key_data_length == AES_BLOCK_SIZE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001040 msg = PAIRWISE_4;
1041 msgtxt = "4/4 Pairwise";
1042 } else {
1043 msg = PAIRWISE_2;
1044 msgtxt = "2/4 Pairwise";
1045 }
1046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
1048 msg == GROUP_2) {
1049 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001050 if (sm->pairwise == WPA_CIPHER_CCMP ||
1051 sm->pairwise == WPA_CIPHER_GCMP) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001052 if (wpa_use_cmac(sm->wpa_key_mgmt) &&
1053 !wpa_use_akm_defined(sm->wpa_key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1055 wpa_auth_logger(wpa_auth, sm->addr,
1056 LOGGER_WARNING,
1057 "advertised support for "
1058 "AES-128-CMAC, but did not "
1059 "use it");
1060 return;
1061 }
1062
Roshan Pius3a1667e2018-07-03 15:17:14 -07001063 if (!wpa_use_cmac(sm->wpa_key_mgmt) &&
1064 !wpa_use_akm_defined(sm->wpa_key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1066 wpa_auth_logger(wpa_auth, sm->addr,
1067 LOGGER_WARNING,
1068 "did not use HMAC-SHA1-AES "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001069 "with CCMP/GCMP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070 return;
1071 }
1072 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001073
Roshan Pius3a1667e2018-07-03 15:17:14 -07001074 if (wpa_use_akm_defined(sm->wpa_key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001075 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1076 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1077 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1078 return;
1079 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080 }
1081
1082 if (key_info & WPA_KEY_INFO_REQUEST) {
1083 if (sm->req_replay_counter_used &&
1084 os_memcmp(key->replay_counter, sm->req_replay_counter,
1085 WPA_REPLAY_COUNTER_LEN) <= 0) {
1086 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1087 "received EAPOL-Key request with "
1088 "replayed counter");
1089 return;
1090 }
1091 }
1092
1093 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001094 !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095 int i;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001096
1097 if (msg == PAIRWISE_2 &&
1098 wpa_replay_counter_valid(sm->prev_key_replay,
1099 key->replay_counter) &&
1100 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1101 os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
1102 {
1103 /*
1104 * Some supplicant implementations (e.g., Windows XP
1105 * WZC) update SNonce for each EAPOL-Key 2/4. This
1106 * breaks the workaround on accepting any of the
1107 * pending requests, so allow the SNonce to be updated
1108 * even if we have already sent out EAPOL-Key 3/4.
1109 */
1110 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1111 "Process SNonce update from STA "
1112 "based on retransmitted EAPOL-Key "
1113 "1/4");
1114 sm->update_snonce = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001115 os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
1116 sm->alt_snonce_valid = TRUE;
1117 os_memcpy(sm->alt_replay_counter,
1118 sm->key_replay[0].counter,
1119 WPA_REPLAY_COUNTER_LEN);
1120 goto continue_processing;
1121 }
1122
1123 if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
1124 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1125 os_memcmp(key->replay_counter, sm->alt_replay_counter,
1126 WPA_REPLAY_COUNTER_LEN) == 0) {
1127 /*
1128 * Supplicant may still be using the old SNonce since
1129 * there was two EAPOL-Key 2/4 messages and they had
1130 * different SNonce values.
1131 */
1132 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1133 "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 -08001134 goto continue_processing;
1135 }
1136
1137 if (msg == PAIRWISE_2 &&
1138 wpa_replay_counter_valid(sm->prev_key_replay,
1139 key->replay_counter) &&
1140 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
1141 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1142 "ignore retransmitted EAPOL-Key %s - "
1143 "SNonce did not change", msgtxt);
1144 } else {
1145 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1146 "received EAPOL-Key %s with "
1147 "unexpected replay counter", msgtxt);
1148 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
1150 if (!sm->key_replay[i].valid)
1151 break;
1152 wpa_hexdump(MSG_DEBUG, "pending replay counter",
1153 sm->key_replay[i].counter,
1154 WPA_REPLAY_COUNTER_LEN);
1155 }
1156 wpa_hexdump(MSG_DEBUG, "received replay counter",
1157 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1158 return;
1159 }
1160
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001161continue_processing:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001162#ifdef CONFIG_FILS
1163 if (sm->wpa == WPA_VERSION_WPA2 && mic_len == 0 &&
1164 !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1165 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1166 "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
1167 return;
1168 }
1169#endif /* CONFIG_FILS */
1170
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171 switch (msg) {
1172 case PAIRWISE_2:
1173 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001174 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
1175 (!sm->update_snonce ||
1176 sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1178 "received EAPOL-Key msg 2/4 in "
1179 "invalid state (%d) - dropped",
1180 sm->wpa_ptk_state);
1181 return;
1182 }
1183 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
1184 if (sm->group->reject_4way_hs_for_entropy) {
1185 /*
1186 * The system did not have enough entropy to generate
1187 * strong random numbers. Reject the first 4-way
1188 * handshake(s) and collect some entropy based on the
1189 * information from it. Once enough entropy is
1190 * available, the next atempt will trigger GMK/Key
1191 * Counter update and the station will be allowed to
1192 * continue.
1193 */
1194 wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
1195 "collect more entropy for random number "
1196 "generation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197 random_mark_pool_ready();
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001198 wpa_sta_disconnect(wpa_auth, sm->addr,
1199 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200 return;
1201 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202 break;
1203 case PAIRWISE_4:
1204 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1205 !sm->PTK_valid) {
1206 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1207 "received EAPOL-Key msg 4/4 in "
1208 "invalid state (%d) - dropped",
1209 sm->wpa_ptk_state);
1210 return;
1211 }
1212 break;
1213 case GROUP_2:
1214 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1215 || !sm->PTK_valid) {
1216 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1217 "received EAPOL-Key msg 2/2 in "
1218 "invalid state (%d) - dropped",
1219 sm->wpa_ptk_group_state);
1220 return;
1221 }
1222 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223 case REQUEST:
1224 break;
1225 }
1226
1227 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1228 "received EAPOL-Key frame (%s)", msgtxt);
1229
1230 if (key_info & WPA_KEY_INFO_ACK) {
1231 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1232 "received invalid EAPOL-Key: Key Ack set");
1233 return;
1234 }
1235
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001236 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1237 !(key_info & WPA_KEY_INFO_MIC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001238 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1239 "received invalid EAPOL-Key: Key MIC not set");
1240 return;
1241 }
1242
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001243#ifdef CONFIG_FILS
1244 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1245 (key_info & WPA_KEY_INFO_MIC)) {
1246 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1247 "received invalid EAPOL-Key: Key MIC set");
1248 return;
1249 }
1250#endif /* CONFIG_FILS */
1251
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001252 sm->MICVerified = FALSE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001253 if (sm->PTK_valid && !sm->update_snonce) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001254 if (mic_len &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001255 wpa_verify_key_mic(sm->wpa_key_mgmt, sm->pmk_len, &sm->PTK,
1256 data, data_len) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001257 (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
1258 wpa_try_alt_snonce(sm, data, data_len))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1260 "received EAPOL-Key with invalid MIC");
Hai Shalom74f70d42019-02-11 14:42:39 -08001261#ifdef TEST_FUZZ
1262 wpa_printf(MSG_INFO,
1263 "TEST: Ignore Key MIC failure for fuzz testing");
1264 goto continue_fuzz;
1265#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266 return;
1267 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001268#ifdef CONFIG_FILS
1269 if (!mic_len &&
1270 wpa_aead_decrypt(sm, &sm->PTK, data, data_len,
1271 &key_data_length) < 0) {
1272 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1273 "received EAPOL-Key with invalid MIC");
Hai Shalom74f70d42019-02-11 14:42:39 -08001274#ifdef TEST_FUZZ
1275 wpa_printf(MSG_INFO,
1276 "TEST: Ignore Key MIC failure for fuzz testing");
1277 goto continue_fuzz;
1278#endif /* TEST_FUZZ */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001279 return;
1280 }
1281#endif /* CONFIG_FILS */
Hai Shalom74f70d42019-02-11 14:42:39 -08001282#ifdef TEST_FUZZ
1283 continue_fuzz:
1284#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001285 sm->MICVerified = TRUE;
1286 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1287 sm->pending_1_of_4_timeout = 0;
1288 }
1289
1290 if (key_info & WPA_KEY_INFO_REQUEST) {
1291 if (sm->MICVerified) {
1292 sm->req_replay_counter_used = 1;
1293 os_memcpy(sm->req_replay_counter, key->replay_counter,
1294 WPA_REPLAY_COUNTER_LEN);
1295 } else {
1296 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1297 "received EAPOL-Key request with "
1298 "invalid MIC");
1299 return;
1300 }
1301
1302 /*
1303 * TODO: should decrypt key data field if encryption was used;
1304 * even though MAC address KDE is not normally encrypted,
1305 * supplicant is allowed to encrypt it.
1306 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001307 if (key_info & WPA_KEY_INFO_ERROR) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001308 if (wpa_receive_error_report(
1309 wpa_auth, sm,
1310 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1311 return; /* STA entry was removed */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1313 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1314 "received EAPOL-Key Request for new "
1315 "4-Way Handshake");
1316 wpa_request_new_ptk(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001317 } else if (key_data_length > 0 &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001318 wpa_parse_kde_ies(key_data, key_data_length,
1319 &kde) == 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320 kde.mac_addr) {
1321 } else {
1322 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1323 "received EAPOL-Key Request for GTK "
1324 "rekeying");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001325 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1326 wpa_rekey_gtk(wpa_auth, NULL);
1327 }
1328 } else {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001329 /* Do not allow the same key replay counter to be reused. */
1330 wpa_replay_counter_mark_invalid(sm->key_replay,
1331 key->replay_counter);
1332
1333 if (msg == PAIRWISE_2) {
1334 /*
1335 * Maintain a copy of the pending EAPOL-Key frames in
1336 * case the EAPOL-Key frame was retransmitted. This is
1337 * needed to allow EAPOL-Key msg 2/4 reply to another
1338 * pending msg 1/4 to update the SNonce to work around
1339 * unexpected supplicant behavior.
1340 */
1341 os_memcpy(sm->prev_key_replay, sm->key_replay,
1342 sizeof(sm->key_replay));
1343 } else {
1344 os_memset(sm->prev_key_replay, 0,
1345 sizeof(sm->prev_key_replay));
1346 }
1347
1348 /*
1349 * Make sure old valid counters are not accepted anymore and
1350 * do not get copied again.
1351 */
1352 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353 }
1354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 os_free(sm->last_rx_eapol_key);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001356 sm->last_rx_eapol_key = os_memdup(data, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001357 if (sm->last_rx_eapol_key == NULL)
1358 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359 sm->last_rx_eapol_key_len = data_len;
1360
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001361 sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001362 sm->EAPOLKeyReceived = TRUE;
1363 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1364 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1365 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1366 wpa_sm_step(sm);
1367}
1368
1369
1370static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1371 const u8 *gnonce, u8 *gtk, size_t gtk_len)
1372{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001373 u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + WPA_GTK_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374 u8 *pos;
1375 int ret = 0;
1376
1377 /* GTK = PRF-X(GMK, "Group key expansion",
1378 * AA || GNonce || Time || random data)
1379 * The example described in the IEEE 802.11 standard uses only AA and
1380 * GNonce as inputs here. Add some more entropy since this derivation
1381 * is done only at the Authenticator and as such, does not need to be
1382 * exactly same.
1383 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001384 os_memset(data, 0, sizeof(data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385 os_memcpy(data, addr, ETH_ALEN);
1386 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1387 pos = data + ETH_ALEN + WPA_NONCE_LEN;
1388 wpa_get_ntp_timestamp(pos);
Hai Shalom74f70d42019-02-11 14:42:39 -08001389#ifdef TEST_FUZZ
1390 os_memset(pos, 0xef, 8);
1391#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392 pos += 8;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001393 if (random_get_bytes(pos, gtk_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 ret = -1;
1395
Roshan Pius3a1667e2018-07-03 15:17:14 -07001396#ifdef CONFIG_SHA384
1397 if (sha384_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1398 gtk, gtk_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001399 ret = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001400#else /* CONFIG_SHA384 */
1401#ifdef CONFIG_SHA256
1402 if (sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1403 gtk, gtk_len) < 0)
1404 ret = -1;
1405#else /* CONFIG_SHA256 */
1406 if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1407 gtk, gtk_len) < 0)
1408 ret = -1;
1409#endif /* CONFIG_SHA256 */
1410#endif /* CONFIG_SHA384 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411
Hai Shalom81f62d82019-07-22 12:10:00 -07001412 forced_memzero(data, sizeof(data));
1413
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001414 return ret;
1415}
1416
1417
1418static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1419{
1420 struct wpa_authenticator *wpa_auth = eloop_ctx;
1421 struct wpa_state_machine *sm = timeout_ctx;
1422
1423 sm->pending_1_of_4_timeout = 0;
1424 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
1425 sm->TimeoutEvt = TRUE;
1426 wpa_sm_step(sm);
1427}
1428
1429
1430void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1431 struct wpa_state_machine *sm, int key_info,
1432 const u8 *key_rsc, const u8 *nonce,
1433 const u8 *kde, size_t kde_len,
1434 int keyidx, int encr, int force_version)
1435{
1436 struct ieee802_1x_hdr *hdr;
1437 struct wpa_eapol_key *key;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001438 size_t len, mic_len, keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001439 int alg;
1440 int key_data_len, pad_len = 0;
1441 u8 *buf, *pos;
1442 int version, pairwise;
1443 int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001444 u8 *key_mic, *key_data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001446 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001447 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001448
1449 len = sizeof(struct ieee802_1x_hdr) + keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450
1451 if (force_version)
1452 version = force_version;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001453 else if (wpa_use_akm_defined(sm->wpa_key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001454 version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001455 else if (wpa_use_cmac(sm->wpa_key_mgmt))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001456 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001457 else if (sm->pairwise != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001458 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1459 else
1460 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1461
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001462 pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001463
1464 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1465 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1466 "encr=%d)",
1467 version,
1468 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1469 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1470 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1471 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1472 pairwise, (unsigned long) kde_len, keyidx, encr);
1473
1474 key_data_len = kde_len;
1475
1476 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07001477 wpa_use_aes_key_wrap(sm->wpa_key_mgmt) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1479 pad_len = key_data_len % 8;
1480 if (pad_len)
1481 pad_len = 8 - pad_len;
1482 key_data_len += pad_len + 8;
1483 }
1484
1485 len += key_data_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001486 if (!mic_len && encr)
1487 len += AES_BLOCK_SIZE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001488
1489 hdr = os_zalloc(len);
1490 if (hdr == NULL)
1491 return;
1492 hdr->version = wpa_auth->conf.eapol_version;
1493 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1494 hdr->length = host_to_be16(len - sizeof(*hdr));
1495 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001496 key_mic = (u8 *) (key + 1);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001497 key_data = ((u8 *) (hdr + 1)) + keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001498
1499 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1500 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1501 key_info |= version;
1502 if (encr && sm->wpa == WPA_VERSION_WPA2)
1503 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1504 if (sm->wpa != WPA_VERSION_WPA2)
1505 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1506 WPA_PUT_BE16(key->key_info, key_info);
1507
1508 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001509 if (sm->wpa == WPA_VERSION_WPA2 && !pairwise)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510 WPA_PUT_BE16(key->key_length, 0);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001511 else
1512 WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001513
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001514 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1515 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1516 os_memcpy(sm->key_replay[i].counter,
1517 sm->key_replay[i - 1].counter,
1518 WPA_REPLAY_COUNTER_LEN);
1519 }
1520 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1521 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1522 WPA_REPLAY_COUNTER_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001523 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
1524 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001525 sm->key_replay[0].valid = TRUE;
1526
1527 if (nonce)
1528 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1529
1530 if (key_rsc)
1531 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1532
1533 if (kde && !encr) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001534 os_memcpy(key_data, kde, kde_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001535 WPA_PUT_BE16(key_mic + mic_len, kde_len);
1536#ifdef CONFIG_FILS
Roshan Pius3a1667e2018-07-03 15:17:14 -07001537 } else if (!mic_len && kde) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001538 const u8 *aad[1];
1539 size_t aad_len[1];
1540
1541 WPA_PUT_BE16(key_mic, AES_BLOCK_SIZE + kde_len);
1542 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1543 kde, kde_len);
1544
1545 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK",
1546 sm->PTK.kek, sm->PTK.kek_len);
1547 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1548 * to Key Data (exclusive). */
1549 aad[0] = (u8 *) hdr;
1550 aad_len[0] = key_mic + 2 - (u8 *) hdr;
1551 if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len, kde, kde_len,
1552 1, aad, aad_len, key_mic + 2) < 0) {
1553 wpa_printf(MSG_DEBUG, "WPA: AES-SIV encryption failed");
1554 return;
1555 }
1556
1557 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
1558 key_mic + 2, AES_BLOCK_SIZE + kde_len);
1559#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001560 } else if (encr && kde) {
1561 buf = os_zalloc(key_data_len);
1562 if (buf == NULL) {
1563 os_free(hdr);
1564 return;
1565 }
1566 pos = buf;
1567 os_memcpy(pos, kde, kde_len);
1568 pos += kde_len;
1569
1570 if (pad_len)
1571 *pos++ = 0xdd;
1572
1573 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1574 buf, key_data_len);
1575 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07001576 wpa_use_aes_key_wrap(sm->wpa_key_mgmt) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001577 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001578 wpa_printf(MSG_DEBUG,
1579 "WPA: Encrypt Key Data using AES-WRAP (KEK length %u)",
1580 (unsigned int) sm->PTK.kek_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001581 if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len,
1582 (key_data_len - 8) / 8, buf, key_data)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001583 os_free(hdr);
1584 os_free(buf);
1585 return;
1586 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001587 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001588#ifndef CONFIG_NO_RC4
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001589 } else if (sm->PTK.kek_len == 16) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001590 u8 ek[32];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001591
1592 wpa_printf(MSG_DEBUG,
1593 "WPA: Encrypt Key Data using RC4");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001594 os_memcpy(key->key_iv,
1595 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1596 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1597 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001598 os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len);
1599 os_memcpy(key_data, buf, key_data_len);
1600 rc4_skip(ek, 32, 256, key_data, key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001601 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001602#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001603 } else {
1604 os_free(hdr);
1605 os_free(buf);
1606 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001607 }
1608 os_free(buf);
1609 }
1610
1611 if (key_info & WPA_KEY_INFO_MIC) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001612 if (!sm->PTK_valid || !mic_len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001613 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1614 "PTK not valid when sending EAPOL-Key "
1615 "frame");
1616 os_free(hdr);
1617 return;
1618 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001619
Roshan Pius3a1667e2018-07-03 15:17:14 -07001620 if (wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len,
1621 sm->wpa_key_mgmt, version,
1622 (u8 *) hdr, len, key_mic) < 0) {
1623 os_free(hdr);
1624 return;
1625 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001626#ifdef CONFIG_TESTING_OPTIONS
1627 if (!pairwise &&
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001628 wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001629 drand48() <
1630 wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
1631 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1632 "Corrupting group EAPOL-Key Key MIC");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001633 key_mic[0]++;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001634 }
1635#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636 }
1637
1638 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1639 1);
1640 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1641 sm->pairwise_set);
1642 os_free(hdr);
1643}
1644
1645
1646static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1647 struct wpa_state_machine *sm, int key_info,
1648 const u8 *key_rsc, const u8 *nonce,
1649 const u8 *kde, size_t kde_len,
1650 int keyidx, int encr)
1651{
1652 int timeout_ms;
1653 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001654 u32 ctr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655
1656 if (sm == NULL)
1657 return;
1658
1659 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1660 keyidx, encr, 0);
1661
1662 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1663 if (ctr == 1 && wpa_auth->conf.tx_status)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001664 timeout_ms = pairwise ? eapol_key_timeout_first :
1665 eapol_key_timeout_first_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666 else
1667 timeout_ms = eapol_key_timeout_subseq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001668 if (wpa_auth->conf.wpa_disable_eapol_key_retries &&
1669 (!pairwise || (key_info & WPA_KEY_INFO_MIC)))
1670 timeout_ms = eapol_key_timeout_no_retrans;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1672 sm->pending_1_of_4_timeout = 1;
Hai Shalom74f70d42019-02-11 14:42:39 -08001673#ifdef TEST_FUZZ
1674 timeout_ms = 1;
1675#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001676 wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001677 "counter %u)", timeout_ms, ctr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001678 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1679 wpa_send_eapol_timeout, wpa_auth, sm);
1680}
1681
1682
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001683static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK,
1684 u8 *data, size_t data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685{
1686 struct ieee802_1x_hdr *hdr;
1687 struct wpa_eapol_key *key;
1688 u16 key_info;
1689 int ret = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001690 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN], *mic_pos;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001691 size_t mic_len = wpa_mic_len(akmp, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001692
1693 if (data_len < sizeof(*hdr) + sizeof(*key))
1694 return -1;
1695
1696 hdr = (struct ieee802_1x_hdr *) data;
1697 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001698 mic_pos = (u8 *) (key + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001700 os_memcpy(mic, mic_pos, mic_len);
1701 os_memset(mic_pos, 0, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001702 if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp,
1703 key_info & WPA_KEY_INFO_TYPE_MASK,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001704 data, data_len, mic_pos) ||
1705 os_memcmp_const(mic, mic_pos, mic_len) != 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706 ret = -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001707 os_memcpy(mic_pos, mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001708 return ret;
1709}
1710
1711
1712void wpa_remove_ptk(struct wpa_state_machine *sm)
1713{
1714 sm->PTK_valid = FALSE;
1715 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001716 if (wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL,
1717 0))
1718 wpa_printf(MSG_DEBUG,
1719 "RSN: PTK removal from the driver failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720 sm->pairwise_set = FALSE;
1721 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1722}
1723
1724
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001725int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001726{
1727 int remove_ptk = 1;
1728
1729 if (sm == NULL)
1730 return -1;
1731
1732 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1733 "event %d notification", event);
1734
1735 switch (event) {
1736 case WPA_AUTH:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001737#ifdef CONFIG_MESH
1738 /* PTKs are derived through AMPE */
1739 if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
1740 /* not mesh */
1741 break;
1742 }
1743 return 0;
1744#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001745 case WPA_ASSOC:
1746 break;
1747 case WPA_DEAUTH:
1748 case WPA_DISASSOC:
1749 sm->DeauthenticationRequest = TRUE;
Hai Shalom1dc4d202019-04-29 16:22:27 -07001750#ifdef CONFIG_IEEE80211R_AP
1751 os_memset(sm->PMK, 0, sizeof(sm->PMK));
1752 sm->pmk_len = 0;
1753 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
1754 sm->xxkey_len = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07001755 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
1756 sm->pmk_r1_len = 0;
Hai Shalom1dc4d202019-04-29 16:22:27 -07001757#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001758 break;
1759 case WPA_REAUTH:
1760 case WPA_REAUTH_EAPOL:
1761 if (!sm->started) {
1762 /*
1763 * When using WPS, we may end up here if the STA
1764 * manages to re-associate without the previous STA
1765 * entry getting removed. Consequently, we need to make
1766 * sure that the WPA state machines gets initialized
1767 * properly at this point.
1768 */
1769 wpa_printf(MSG_DEBUG, "WPA state machine had not been "
1770 "started - initialize now");
1771 sm->started = 1;
1772 sm->Init = TRUE;
1773 if (wpa_sm_step(sm) == 1)
1774 return 1; /* should not really happen */
1775 sm->Init = FALSE;
1776 sm->AuthenticationRequest = TRUE;
1777 break;
1778 }
1779 if (sm->GUpdateStationKeys) {
1780 /*
1781 * Reauthentication cancels the pending group key
1782 * update for this STA.
1783 */
1784 sm->group->GKeyDoneStations--;
1785 sm->GUpdateStationKeys = FALSE;
1786 sm->PtkGroupInit = TRUE;
1787 }
1788 sm->ReAuthenticationRequest = TRUE;
1789 break;
1790 case WPA_ASSOC_FT:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001791#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001792 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1793 "after association");
1794 wpa_ft_install_ptk(sm);
1795
1796 /* Using FT protocol, not WPA auth state machine */
1797 sm->ft_completed = 1;
Hai Shalom81f62d82019-07-22 12:10:00 -07001798 wpa_auth_set_ptk_rekey_timer(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001799 return 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001800#else /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001801 break;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001802#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001803 case WPA_ASSOC_FILS:
1804#ifdef CONFIG_FILS
1805 wpa_printf(MSG_DEBUG,
1806 "FILS: TK configuration after association");
1807 fils_set_tk(sm);
1808 sm->fils_completed = 1;
1809 return 0;
1810#else /* CONFIG_FILS */
1811 break;
1812#endif /* CONFIG_FILS */
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02001813 case WPA_DRV_STA_REMOVED:
1814 sm->tk_already_set = FALSE;
1815 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001816 }
1817
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001818#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819 sm->ft_completed = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001820#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001822 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1823 remove_ptk = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001824#ifdef CONFIG_FILS
1825 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1826 (event == WPA_AUTH || event == WPA_ASSOC))
1827 remove_ptk = 0;
1828#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001829
1830 if (remove_ptk) {
1831 sm->PTK_valid = FALSE;
1832 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1833
1834 if (event != WPA_REAUTH_EAPOL)
1835 wpa_remove_ptk(sm);
1836 }
1837
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001838 if (sm->in_step_loop) {
1839 /*
1840 * wpa_sm_step() is already running - avoid recursive call to
1841 * it by making the existing loop process the new update.
1842 */
1843 sm->changed = TRUE;
1844 return 0;
1845 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846 return wpa_sm_step(sm);
1847}
1848
1849
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001850SM_STATE(WPA_PTK, INITIALIZE)
1851{
1852 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1853 if (sm->Init) {
1854 /* Init flag is not cleared here, so avoid busy
1855 * loop by claiming nothing changed. */
1856 sm->changed = FALSE;
1857 }
1858
1859 sm->keycount = 0;
1860 if (sm->GUpdateStationKeys)
1861 sm->group->GKeyDoneStations--;
1862 sm->GUpdateStationKeys = FALSE;
1863 if (sm->wpa == WPA_VERSION_WPA)
1864 sm->PInitAKeys = FALSE;
1865 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1866 * Local AA > Remote AA)) */) {
1867 sm->Pair = TRUE;
1868 }
1869 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1870 wpa_remove_ptk(sm);
1871 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1872 sm->TimeoutCtr = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001873 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
1874 sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP ||
1875 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001876 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1877 WPA_EAPOL_authorized, 0);
1878 }
1879}
1880
1881
1882SM_STATE(WPA_PTK, DISCONNECT)
1883{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001884 u16 reason = sm->disconnect_reason;
1885
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001886 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1887 sm->Disconnect = FALSE;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001888 sm->disconnect_reason = 0;
1889 if (!reason)
1890 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1891 wpa_sta_disconnect(sm->wpa_auth, sm->addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001892}
1893
1894
1895SM_STATE(WPA_PTK, DISCONNECTED)
1896{
1897 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1898 sm->DeauthenticationRequest = FALSE;
1899}
1900
1901
1902SM_STATE(WPA_PTK, AUTHENTICATION)
1903{
1904 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1905 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1906 sm->PTK_valid = FALSE;
1907 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1908 1);
1909 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1910 sm->AuthenticationRequest = FALSE;
1911}
1912
1913
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001914static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
1915 struct wpa_group *group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001917 if (group->first_sta_seen)
1918 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001919 /*
1920 * System has run bit further than at the time hostapd was started
1921 * potentially very early during boot up. This provides better chances
1922 * of collecting more randomness on embedded systems. Re-initialize the
1923 * GMK and Counter here to improve their strength if there was not
1924 * enough entropy available immediately after system startup.
1925 */
1926 wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
1927 "station");
1928 if (random_pool_ready() != 1) {
1929 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
1930 "to proceed - reject first 4-way handshake");
1931 group->reject_4way_hs_for_entropy = TRUE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001932 } else {
1933 group->first_sta_seen = TRUE;
1934 group->reject_4way_hs_for_entropy = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001935 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001936
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001937 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 ||
1938 wpa_gtk_update(wpa_auth, group) < 0 ||
1939 wpa_group_config_group_keys(wpa_auth, group) < 0) {
1940 wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed");
1941 group->first_sta_seen = FALSE;
1942 group->reject_4way_hs_for_entropy = TRUE;
1943 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001944}
1945
1946
1947SM_STATE(WPA_PTK, AUTHENTICATION2)
1948{
1949 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1950
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001951 wpa_group_ensure_init(sm->wpa_auth, sm->group);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001952 sm->ReAuthenticationRequest = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001953
Dmitry Shmidt04949592012-07-19 12:16:46 -07001954 /*
1955 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1956 * ambiguous. The Authenticator state machine uses a counter that is
1957 * incremented by one for each 4-way handshake. However, the security
1958 * analysis of 4-way handshake points out that unpredictable nonces
1959 * help in preventing precomputation attacks. Instead of the state
1960 * machine definition, use an unpredictable nonce value here to provide
1961 * stronger protection against potential precomputation attacks.
1962 */
1963 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1964 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
1965 "ANonce.");
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001966 sm->Disconnect = TRUE;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001967 return;
1968 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001969 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
1970 WPA_NONCE_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1972 * logical place than INITIALIZE since AUTHENTICATION2 can be
1973 * re-entered on ReAuthenticationRequest without going through
1974 * INITIALIZE. */
1975 sm->TimeoutCtr = 0;
1976}
1977
1978
Jouni Malinen1420a892017-10-01 12:32:57 +03001979static int wpa_auth_sm_ptk_update(struct wpa_state_machine *sm)
1980{
1981 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1982 wpa_printf(MSG_ERROR,
1983 "WPA: Failed to get random data for ANonce");
1984 sm->Disconnect = TRUE;
1985 return -1;
1986 }
1987 wpa_hexdump(MSG_DEBUG, "WPA: Assign new ANonce", sm->ANonce,
1988 WPA_NONCE_LEN);
1989 sm->TimeoutCtr = 0;
1990 return 0;
1991}
1992
1993
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001994SM_STATE(WPA_PTK, INITPMK)
1995{
1996 u8 msk[2 * PMK_LEN];
1997 size_t len = 2 * PMK_LEN;
1998
1999 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002000#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002001 sm->xxkey_len = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002002#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002003 if (sm->pmksa) {
2004 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002005 os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
2006 sm->pmk_len = sm->pmksa->pmk_len;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002007#ifdef CONFIG_DPP
2008 } else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP) {
2009 wpa_printf(MSG_DEBUG,
2010 "DPP: No PMKSA cache entry for STA - reject connection");
2011 sm->Disconnect = TRUE;
2012 sm->disconnect_reason = WLAN_REASON_INVALID_PMKID;
2013 return;
2014#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002015 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002016 unsigned int pmk_len;
2017
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002018 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002019 pmk_len = PMK_LEN_SUITE_B_192;
2020 else
2021 pmk_len = PMK_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002023 "(MSK len=%lu PMK len=%u)", (unsigned long) len,
2024 pmk_len);
2025 if (len < pmk_len) {
2026 wpa_printf(MSG_DEBUG,
2027 "WPA: MSK not long enough (%u) to create PMK (%u)",
2028 (unsigned int) len, (unsigned int) pmk_len);
2029 sm->Disconnect = TRUE;
2030 return;
2031 }
2032 os_memcpy(sm->PMK, msk, pmk_len);
2033 sm->pmk_len = pmk_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002034#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002035 if (len >= 2 * PMK_LEN) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002036 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
2037 os_memcpy(sm->xxkey, msk, SHA384_MAC_LEN);
2038 sm->xxkey_len = SHA384_MAC_LEN;
2039 } else {
2040 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
2041 sm->xxkey_len = PMK_LEN;
2042 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002044#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002045 } else {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002046 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p",
Paul Stewart092955c2017-02-06 09:13:09 -08002047 sm->wpa_auth->cb->get_msk);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002048 sm->Disconnect = TRUE;
2049 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002050 }
Hai Shalom81f62d82019-07-22 12:10:00 -07002051 forced_memzero(msk, sizeof(msk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002052
2053 sm->req_replay_counter_used = 0;
2054 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
2055 * will break reauthentication since EAPOL state machines may not be
2056 * get into AUTHENTICATING state that clears keyRun before WPA state
2057 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
2058 * state and takes PMK from the previously used AAA Key. This will
2059 * eventually fail in 4-Way Handshake because Supplicant uses PMK
2060 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
2061 * be good workaround for this issue. */
2062 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
2063}
2064
2065
2066SM_STATE(WPA_PTK, INITPSK)
2067{
2068 const u8 *psk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002069 size_t psk_len;
2070
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002071 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002072 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL,
Hai Shalom021b0b52019-04-10 11:17:58 -07002073 &psk_len, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002074 if (psk) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002075 os_memcpy(sm->PMK, psk, psk_len);
2076 sm->pmk_len = psk_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002077#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078 os_memcpy(sm->xxkey, psk, PMK_LEN);
2079 sm->xxkey_len = PMK_LEN;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002080#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002082#ifdef CONFIG_SAE
2083 if (wpa_auth_uses_sae(sm) && sm->pmksa) {
2084 wpa_printf(MSG_DEBUG, "SAE: PMK from PMKSA cache");
2085 os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
2086 sm->pmk_len = sm->pmksa->pmk_len;
Hai Shalom021b0b52019-04-10 11:17:58 -07002087#ifdef CONFIG_IEEE80211R_AP
2088 os_memcpy(sm->xxkey, sm->pmksa->pmk, sm->pmksa->pmk_len);
2089 sm->xxkey_len = sm->pmksa->pmk_len;
2090#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002091 }
2092#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002093 sm->req_replay_counter_used = 0;
2094}
2095
2096
2097SM_STATE(WPA_PTK, PTKSTART)
2098{
2099 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
2100 size_t pmkid_len = 0;
2101
2102 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
2103 sm->PTKRequest = FALSE;
2104 sm->TimeoutEvt = FALSE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002105 sm->alt_snonce_valid = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106
2107 sm->TimeoutCtr++;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002108 if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109 /* No point in sending the EAPOL-Key - we will disconnect
2110 * immediately following this. */
2111 return;
2112 }
2113
2114 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2115 "sending 1/4 msg of 4-Way Handshake");
2116 /*
Hai Shalomce48b4a2018-09-05 11:41:35 -07002117 * For infrastructure BSS cases, it is better for the AP not to include
2118 * the PMKID KDE in EAPOL-Key msg 1/4 since it could be used to initiate
2119 * offline search for the passphrase/PSK without having to be able to
2120 * capture a 4-way handshake from a STA that has access to the network.
2121 *
2122 * For IBSS cases, addition of PMKID KDE could be considered even with
2123 * WPA2-PSK cases that use multiple PSKs, but only if there is a single
2124 * possible PSK for this STA. However, this should not be done unless
2125 * there is support for using that information on the supplicant side.
2126 * The concern about exposing PMKID unnecessarily in infrastructure BSS
2127 * cases would also apply here, but at least in the IBSS case, this
2128 * would cover a potential real use case.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002129 */
2130 if (sm->wpa == WPA_VERSION_WPA2 &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002131 (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) ||
2132 (sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE && sm->pmksa) ||
2133 wpa_key_mgmt_sae(sm->wpa_key_mgmt)) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002134 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135 pmkid = buf;
2136 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
2137 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
2138 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
2139 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002140 if (sm->pmksa) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002141 wpa_hexdump(MSG_DEBUG,
2142 "RSN: Message 1/4 PMKID from PMKSA entry",
2143 sm->pmksa->pmkid, PMKID_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002144 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2145 sm->pmksa->pmkid, PMKID_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002146 } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
2147 /* No KCK available to derive PMKID */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002148 wpa_printf(MSG_DEBUG,
2149 "RSN: No KCK available to derive PMKID for message 1/4");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002150 pmkid = NULL;
Hai Shalom81f62d82019-07-22 12:10:00 -07002151#ifdef CONFIG_FILS
2152 } else if (wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2153 if (sm->pmkid_set) {
2154 wpa_hexdump(MSG_DEBUG,
2155 "RSN: Message 1/4 PMKID from FILS/ERP",
2156 sm->pmkid, PMKID_LEN);
2157 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2158 sm->pmkid, PMKID_LEN);
2159 } else {
2160 /* No PMKID available */
2161 wpa_printf(MSG_DEBUG,
2162 "RSN: No FILS/ERP PMKID available for message 1/4");
2163 pmkid = NULL;
2164 }
2165#endif /* CONFIG_FILS */
2166#ifdef CONFIG_IEEE80211R_AP
2167 } else if (wpa_key_mgmt_ft(sm->wpa_key_mgmt) &&
2168 sm->ft_completed) {
2169 wpa_printf(MSG_DEBUG,
2170 "FT: No PMKID in message 1/4 when using FT protocol");
2171 pmkid = NULL;
2172 pmkid_len = 0;
2173#endif /* CONFIG_IEEE80211R_AP */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002174#ifdef CONFIG_SAE
2175 } else if (wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
2176 if (sm->pmkid_set) {
2177 wpa_hexdump(MSG_DEBUG,
2178 "RSN: Message 1/4 PMKID from SAE",
2179 sm->pmkid, PMKID_LEN);
2180 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2181 sm->pmkid, PMKID_LEN);
2182 } else {
2183 /* No PMKID available */
2184 wpa_printf(MSG_DEBUG,
2185 "RSN: No SAE PMKID available for message 1/4");
2186 pmkid = NULL;
2187 }
2188#endif /* CONFIG_SAE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002189 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002190 /*
2191 * Calculate PMKID since no PMKSA cache entry was
2192 * available with pre-calculated PMKID.
2193 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002194 rsn_pmkid(sm->PMK, sm->pmk_len, sm->wpa_auth->addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002195 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002196 sm->wpa_key_mgmt);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002197 wpa_hexdump(MSG_DEBUG,
2198 "RSN: Message 1/4 PMKID derived from PMK",
2199 &pmkid[2 + RSN_SELECTOR_LEN], PMKID_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002200 }
2201 }
2202 wpa_send_eapol(sm->wpa_auth, sm,
2203 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
2204 sm->ANonce, pmkid, pmkid_len, 0, 0);
2205}
2206
2207
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002208static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002209 const u8 *pmk, unsigned int pmk_len,
2210 struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002211{
Hai Shalom021b0b52019-04-10 11:17:58 -07002212 const u8 *z = NULL;
2213 size_t z_len = 0;
2214
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002215#ifdef CONFIG_IEEE80211R_AP
Hai Shalom81f62d82019-07-22 12:10:00 -07002216 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2217 if (sm->ft_completed) {
2218 u8 ptk_name[WPA_PMK_NAME_LEN];
2219
2220 return wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->pmk_r1_len,
2221 sm->SNonce, sm->ANonce,
2222 sm->addr, sm->wpa_auth->addr,
2223 sm->pmk_r1_name,
2224 ptk, ptk_name,
2225 sm->wpa_key_mgmt,
2226 sm->pairwise);
2227 }
2228 return wpa_auth_derive_ptk_ft(sm, ptk);
2229 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002230#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002231
Hai Shalom021b0b52019-04-10 11:17:58 -07002232#ifdef CONFIG_DPP2
2233 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
2234 z = wpabuf_head(sm->dpp_z);
2235 z_len = wpabuf_len(sm->dpp_z);
2236 }
2237#endif /* CONFIG_DPP2 */
2238
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002239 return wpa_pmk_to_ptk(pmk, pmk_len, "Pairwise key expansion",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002240 sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce,
Hai Shalom021b0b52019-04-10 11:17:58 -07002241 ptk, sm->wpa_key_mgmt, sm->pairwise, z, z_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002242}
2243
2244
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002245#ifdef CONFIG_FILS
2246
2247int fils_auth_pmk_to_ptk(struct wpa_state_machine *sm, const u8 *pmk,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002248 size_t pmk_len, const u8 *snonce, const u8 *anonce,
2249 const u8 *dhss, size_t dhss_len,
2250 struct wpabuf *g_sta, struct wpabuf *g_ap)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002251{
2252 u8 ick[FILS_ICK_MAX_LEN];
2253 size_t ick_len;
2254 int res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002255 u8 fils_ft[FILS_FT_MAX_LEN];
2256 size_t fils_ft_len = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002257
2258 res = fils_pmk_to_ptk(pmk, pmk_len, sm->addr, sm->wpa_auth->addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002259 snonce, anonce, dhss, dhss_len,
2260 &sm->PTK, ick, &ick_len,
2261 sm->wpa_key_mgmt, sm->pairwise,
2262 fils_ft, &fils_ft_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002263 if (res < 0)
2264 return res;
2265 sm->PTK_valid = TRUE;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002266 sm->tk_already_set = FALSE;
2267
2268#ifdef CONFIG_IEEE80211R_AP
2269 if (fils_ft_len) {
2270 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2271 struct wpa_auth_config *conf = &wpa_auth->conf;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002272 u8 pmk_r0[PMK_LEN_MAX], pmk_r0_name[WPA_PMK_NAME_LEN];
2273 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
2274 size_t pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002275
2276 if (wpa_derive_pmk_r0(fils_ft, fils_ft_len,
2277 conf->ssid, conf->ssid_len,
2278 conf->mobility_domain,
2279 conf->r0_key_holder,
2280 conf->r0_key_holder_len,
Roshan Pius3a1667e2018-07-03 15:17:14 -07002281 sm->addr, pmk_r0, pmk_r0_name,
2282 use_sha384) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002283 return -1;
2284
Roshan Pius3a1667e2018-07-03 15:17:14 -07002285 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0",
2286 pmk_r0, pmk_r0_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002287 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name",
2288 pmk_r0_name, WPA_PMK_NAME_LEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002289 wpa_ft_store_pmk_fils(sm, pmk_r0, pmk_r0_name);
Hai Shalom81f62d82019-07-22 12:10:00 -07002290 forced_memzero(fils_ft, sizeof(fils_ft));
Hai Shalom021b0b52019-04-10 11:17:58 -07002291
2292 res = wpa_derive_pmk_r1_name(pmk_r0_name, conf->r1_key_holder,
2293 sm->addr, sm->pmk_r1_name,
2294 use_sha384);
Hai Shalom81f62d82019-07-22 12:10:00 -07002295 forced_memzero(pmk_r0, PMK_LEN_MAX);
Hai Shalom021b0b52019-04-10 11:17:58 -07002296 if (res < 0)
2297 return -1;
2298 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", sm->pmk_r1_name,
2299 WPA_PMK_NAME_LEN);
2300 sm->pmk_r1_name_valid = 1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002301 }
2302#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002303
2304 res = fils_key_auth_sk(ick, ick_len, snonce, anonce,
2305 sm->addr, sm->wpa_auth->addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002306 g_sta ? wpabuf_head(g_sta) : NULL,
2307 g_sta ? wpabuf_len(g_sta) : 0,
2308 g_ap ? wpabuf_head(g_ap) : NULL,
2309 g_ap ? wpabuf_len(g_ap) : 0,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002310 sm->wpa_key_mgmt, sm->fils_key_auth_sta,
2311 sm->fils_key_auth_ap,
2312 &sm->fils_key_auth_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07002313 forced_memzero(ick, sizeof(ick));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002314
2315 /* Store nonces for (Re)Association Request/Response frame processing */
2316 os_memcpy(sm->SNonce, snonce, FILS_NONCE_LEN);
2317 os_memcpy(sm->ANonce, anonce, FILS_NONCE_LEN);
2318
2319 return res;
2320}
2321
2322
2323static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
2324 u8 *buf, size_t buf_len, u16 *_key_data_len)
2325{
2326 struct ieee802_1x_hdr *hdr;
2327 struct wpa_eapol_key *key;
2328 u8 *pos;
2329 u16 key_data_len;
2330 u8 *tmp;
2331 const u8 *aad[1];
2332 size_t aad_len[1];
2333
2334 hdr = (struct ieee802_1x_hdr *) buf;
2335 key = (struct wpa_eapol_key *) (hdr + 1);
2336 pos = (u8 *) (key + 1);
2337 key_data_len = WPA_GET_BE16(pos);
2338 if (key_data_len < AES_BLOCK_SIZE ||
2339 key_data_len > buf_len - sizeof(*hdr) - sizeof(*key) - 2) {
2340 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2341 "No room for AES-SIV data in the frame");
2342 return -1;
2343 }
2344 pos += 2; /* Pointing at the Encrypted Key Data field */
2345
2346 tmp = os_malloc(key_data_len);
2347 if (!tmp)
2348 return -1;
2349
2350 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2351 * to Key Data (exclusive). */
2352 aad[0] = buf;
2353 aad_len[0] = pos - buf;
2354 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, key_data_len,
2355 1, aad, aad_len, tmp) < 0) {
2356 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2357 "Invalid AES-SIV data in the frame");
2358 bin_clear_free(tmp, key_data_len);
2359 return -1;
2360 }
2361
2362 /* AEAD decryption and validation completed successfully */
2363 key_data_len -= AES_BLOCK_SIZE;
2364 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2365 tmp, key_data_len);
2366
2367 /* Replace Key Data field with the decrypted version */
2368 os_memcpy(pos, tmp, key_data_len);
2369 pos -= 2; /* Key Data Length field */
2370 WPA_PUT_BE16(pos, key_data_len);
2371 bin_clear_free(tmp, key_data_len);
2372 if (_key_data_len)
2373 *_key_data_len = key_data_len;
2374 return 0;
2375}
2376
2377
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002378const u8 * wpa_fils_validate_fils_session(struct wpa_state_machine *sm,
2379 const u8 *ies, size_t ies_len,
2380 const u8 *fils_session)
2381{
2382 const u8 *ie, *end;
2383 const u8 *session = NULL;
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 NULL;
2389 }
2390
2391 /* Verify Session element */
2392 ie = ies;
2393 end = ((const u8 *) ie) + ies_len;
2394 while (ie + 1 < end) {
2395 if (ie + 2 + ie[1] > end)
2396 break;
2397 if (ie[0] == WLAN_EID_EXTENSION &&
2398 ie[1] >= 1 + FILS_SESSION_LEN &&
2399 ie[2] == WLAN_EID_EXT_FILS_SESSION) {
2400 session = ie;
2401 break;
2402 }
2403 ie += 2 + ie[1];
2404 }
2405
2406 if (!session) {
2407 wpa_printf(MSG_DEBUG,
2408 "FILS: %s: Could not find FILS Session element in Assoc Req - reject",
2409 __func__);
2410 return NULL;
2411 }
2412
2413 if (!fils_session) {
2414 wpa_printf(MSG_DEBUG,
2415 "FILS: %s: Could not find FILS Session element in STA entry - reject",
2416 __func__);
2417 return NULL;
2418 }
2419
2420 if (os_memcmp(fils_session, session + 3, FILS_SESSION_LEN) != 0) {
2421 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
2422 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
2423 fils_session, FILS_SESSION_LEN);
2424 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
2425 session + 3, FILS_SESSION_LEN);
2426 return NULL;
2427 }
2428 return session;
2429}
2430
2431
2432int wpa_fils_validate_key_confirm(struct wpa_state_machine *sm, const u8 *ies,
2433 size_t ies_len)
2434{
2435 struct ieee802_11_elems elems;
2436
2437 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
2438 wpa_printf(MSG_DEBUG,
2439 "FILS: Failed to parse decrypted elements");
2440 return -1;
2441 }
2442
2443 if (!elems.fils_session) {
2444 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
2445 return -1;
2446 }
2447
2448 if (!elems.fils_key_confirm) {
2449 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
2450 return -1;
2451 }
2452
2453 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
2454 wpa_printf(MSG_DEBUG,
2455 "FILS: Unexpected Key-Auth length %d (expected %d)",
2456 elems.fils_key_confirm_len,
2457 (int) sm->fils_key_auth_len);
2458 return -1;
2459 }
2460
2461 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_sta,
2462 sm->fils_key_auth_len) != 0) {
2463 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
2464 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
2465 elems.fils_key_confirm, elems.fils_key_confirm_len);
2466 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
2467 sm->fils_key_auth_sta, sm->fils_key_auth_len);
2468 return -1;
2469 }
2470
2471 return 0;
2472}
2473
2474
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002475int fils_decrypt_assoc(struct wpa_state_machine *sm, const u8 *fils_session,
2476 const struct ieee80211_mgmt *mgmt, size_t frame_len,
2477 u8 *pos, size_t left)
2478{
2479 u16 fc, stype;
2480 const u8 *end, *ie_start, *ie, *session, *crypt;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002481 const u8 *aad[5];
2482 size_t aad_len[5];
2483
2484 if (!sm || !sm->PTK_valid) {
2485 wpa_printf(MSG_DEBUG,
2486 "FILS: No KEK to decrypt Assocication Request frame");
2487 return -1;
2488 }
2489
2490 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2491 wpa_printf(MSG_DEBUG,
2492 "FILS: Not a FILS AKM - reject association");
2493 return -1;
2494 }
2495
2496 end = ((const u8 *) mgmt) + frame_len;
2497 fc = le_to_host16(mgmt->frame_control);
2498 stype = WLAN_FC_GET_STYPE(fc);
2499 if (stype == WLAN_FC_STYPE_REASSOC_REQ)
2500 ie_start = mgmt->u.reassoc_req.variable;
2501 else
2502 ie_start = mgmt->u.assoc_req.variable;
2503 ie = ie_start;
2504
2505 /*
2506 * Find FILS Session element which is the last unencrypted element in
2507 * the frame.
2508 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002509 session = wpa_fils_validate_fils_session(sm, ie, end - ie,
2510 fils_session);
2511 if (!session) {
2512 wpa_printf(MSG_DEBUG, "FILS: Session validation failed");
2513 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002514 }
2515
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002516 crypt = session + 2 + session[1];
2517
2518 if (end - crypt < AES_BLOCK_SIZE) {
2519 wpa_printf(MSG_DEBUG,
2520 "FILS: Too short frame to include AES-SIV data");
2521 return -1;
2522 }
2523
2524 /* AES-SIV AAD vectors */
2525
2526 /* The STA's MAC address */
2527 aad[0] = mgmt->sa;
2528 aad_len[0] = ETH_ALEN;
2529 /* The AP's BSSID */
2530 aad[1] = mgmt->da;
2531 aad_len[1] = ETH_ALEN;
2532 /* The STA's nonce */
2533 aad[2] = sm->SNonce;
2534 aad_len[2] = FILS_NONCE_LEN;
2535 /* The AP's nonce */
2536 aad[3] = sm->ANonce;
2537 aad_len[3] = FILS_NONCE_LEN;
2538 /*
2539 * The (Re)Association Request frame from the Capability Information
2540 * field to the FILS Session element (both inclusive).
2541 */
2542 aad[4] = (const u8 *) &mgmt->u.assoc_req.capab_info;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002543 aad_len[4] = crypt - aad[4];
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002544
2545 if (aes_siv_decrypt(sm->PTK.kek, sm->PTK.kek_len, crypt, end - crypt,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002546 5, aad, aad_len, pos + (crypt - ie_start)) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002547 wpa_printf(MSG_DEBUG,
2548 "FILS: Invalid AES-SIV data in the frame");
2549 return -1;
2550 }
2551 wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Request elements",
2552 pos, left - AES_BLOCK_SIZE);
2553
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002554 if (wpa_fils_validate_key_confirm(sm, pos, left - AES_BLOCK_SIZE) < 0) {
2555 wpa_printf(MSG_DEBUG, "FILS: Key Confirm validation failed");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002556 return -1;
2557 }
2558
2559 return left - AES_BLOCK_SIZE;
2560}
2561
2562
2563int fils_encrypt_assoc(struct wpa_state_machine *sm, u8 *buf,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002564 size_t current_len, size_t max_len,
2565 const struct wpabuf *hlp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002566{
2567 u8 *end = buf + max_len;
2568 u8 *pos = buf + current_len;
2569 struct ieee80211_mgmt *mgmt;
2570 struct wpabuf *plain;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002571 const u8 *aad[5];
2572 size_t aad_len[5];
2573
2574 if (!sm || !sm->PTK_valid)
2575 return -1;
2576
2577 wpa_hexdump(MSG_DEBUG,
2578 "FILS: Association Response frame before FILS processing",
2579 buf, current_len);
2580
2581 mgmt = (struct ieee80211_mgmt *) buf;
2582
2583 /* AES-SIV AAD vectors */
2584
2585 /* The AP's BSSID */
2586 aad[0] = mgmt->sa;
2587 aad_len[0] = ETH_ALEN;
2588 /* The STA's MAC address */
2589 aad[1] = mgmt->da;
2590 aad_len[1] = ETH_ALEN;
2591 /* The AP's nonce */
2592 aad[2] = sm->ANonce;
2593 aad_len[2] = FILS_NONCE_LEN;
2594 /* The STA's nonce */
2595 aad[3] = sm->SNonce;
2596 aad_len[3] = FILS_NONCE_LEN;
2597 /*
2598 * The (Re)Association Response frame from the Capability Information
2599 * field (the same offset in both Association and Reassociation
2600 * Response frames) to the FILS Session element (both inclusive).
2601 */
2602 aad[4] = (const u8 *) &mgmt->u.assoc_resp.capab_info;
2603 aad_len[4] = pos - aad[4];
2604
2605 /* The following elements will be encrypted with AES-SIV */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002606 plain = fils_prepare_plainbuf(sm, hlp);
2607 if (!plain) {
2608 wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
2609 return -1;
2610 }
2611
2612 if (pos + wpabuf_len(plain) + AES_BLOCK_SIZE > end) {
2613 wpa_printf(MSG_DEBUG,
2614 "FILS: Not enough room for FILS elements");
Hai Shalom81f62d82019-07-22 12:10:00 -07002615 wpabuf_clear_free(plain);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002616 return -1;
2617 }
2618
2619 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: Association Response plaintext",
2620 plain);
2621
2622 if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len,
2623 wpabuf_head(plain), wpabuf_len(plain),
2624 5, aad, aad_len, pos) < 0) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002625 wpabuf_clear_free(plain);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002626 return -1;
2627 }
2628
2629 wpa_hexdump(MSG_DEBUG,
2630 "FILS: Encrypted Association Response elements",
2631 pos, AES_BLOCK_SIZE + wpabuf_len(plain));
2632 current_len += wpabuf_len(plain) + AES_BLOCK_SIZE;
Hai Shalom81f62d82019-07-22 12:10:00 -07002633 wpabuf_clear_free(plain);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002634
2635 sm->fils_completed = 1;
2636
2637 return current_len;
2638}
2639
2640
2641static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm,
2642 const struct wpabuf *hlp)
2643{
2644 struct wpabuf *plain;
2645 u8 *len, *tmp, *tmp2;
2646 u8 hdr[2];
2647 u8 *gtk, dummy_gtk[32];
2648 size_t gtk_len;
2649 struct wpa_group *gsm;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002650
2651 plain = wpabuf_alloc(1000);
2652 if (!plain)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002653 return NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002654
2655 /* TODO: FILS Public Key */
2656
2657 /* FILS Key Confirmation */
2658 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2659 wpabuf_put_u8(plain, 1 + sm->fils_key_auth_len); /* Length */
2660 /* Element ID Extension */
2661 wpabuf_put_u8(plain, WLAN_EID_EXT_FILS_KEY_CONFIRM);
2662 wpabuf_put_data(plain, sm->fils_key_auth_ap, sm->fils_key_auth_len);
2663
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002664 /* FILS HLP Container */
2665 if (hlp)
2666 wpabuf_put_buf(plain, hlp);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002667
2668 /* TODO: FILS IP Address Assignment */
2669
2670 /* Key Delivery */
2671 gsm = sm->group;
2672 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2673 len = wpabuf_put(plain, 1);
2674 wpabuf_put_u8(plain, WLAN_EID_EXT_KEY_DELIVERY);
2675 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN,
2676 wpabuf_put(plain, WPA_KEY_RSC_LEN));
2677 /* GTK KDE */
2678 gtk = gsm->GTK[gsm->GN - 1];
2679 gtk_len = gsm->GTK_len;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002680 if (sm->wpa_auth->conf.disable_gtk ||
2681 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002682 /*
2683 * Provide unique random GTK to each STA to prevent use
2684 * of GTK in the BSS.
2685 */
2686 if (random_get_bytes(dummy_gtk, gtk_len) < 0) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002687 wpabuf_clear_free(plain);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002688 return NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002689 }
2690 gtk = dummy_gtk;
2691 }
2692 hdr[0] = gsm->GN & 0x03;
2693 hdr[1] = 0;
2694 tmp = wpabuf_put(plain, 0);
2695 tmp2 = wpa_add_kde(tmp, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2696 gtk, gtk_len);
2697 wpabuf_put(plain, tmp2 - tmp);
2698
2699 /* IGTK KDE */
2700 tmp = wpabuf_put(plain, 0);
2701 tmp2 = ieee80211w_kde_add(sm, tmp);
2702 wpabuf_put(plain, tmp2 - tmp);
2703
2704 *len = (u8 *) wpabuf_put(plain, 0) - len - 1;
Hai Shalom74f70d42019-02-11 14:42:39 -08002705
2706#ifdef CONFIG_OCV
2707 if (wpa_auth_uses_ocv(sm)) {
2708 struct wpa_channel_info ci;
2709 u8 *pos;
2710
2711 if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
2712 wpa_printf(MSG_WARNING,
2713 "FILS: Failed to get channel info for OCI element");
Hai Shalom81f62d82019-07-22 12:10:00 -07002714 wpabuf_clear_free(plain);
Hai Shalom74f70d42019-02-11 14:42:39 -08002715 return NULL;
2716 }
2717
2718 pos = wpabuf_put(plain, OCV_OCI_EXTENDED_LEN);
2719 if (ocv_insert_extended_oci(&ci, pos) < 0) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002720 wpabuf_clear_free(plain);
Hai Shalom74f70d42019-02-11 14:42:39 -08002721 return NULL;
2722 }
2723 }
2724#endif /* CONFIG_OCV */
2725
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002726 return plain;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002727}
2728
2729
2730int fils_set_tk(struct wpa_state_machine *sm)
2731{
2732 enum wpa_alg alg;
2733 int klen;
2734
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002735 if (!sm || !sm->PTK_valid) {
2736 wpa_printf(MSG_DEBUG, "FILS: No valid PTK available to set TK");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002737 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002738 }
2739 if (sm->tk_already_set) {
2740 wpa_printf(MSG_DEBUG, "FILS: TK already set to the driver");
2741 return -1;
2742 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002743
2744 alg = wpa_cipher_to_alg(sm->pairwise);
2745 klen = wpa_cipher_key_len(sm->pairwise);
2746
2747 wpa_printf(MSG_DEBUG, "FILS: Configure TK to the driver");
2748 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2749 sm->PTK.tk, klen)) {
2750 wpa_printf(MSG_DEBUG, "FILS: Failed to set TK to the driver");
2751 return -1;
2752 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002753 sm->tk_already_set = TRUE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002754
2755 return 0;
2756}
2757
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002758
2759u8 * hostapd_eid_assoc_fils_session(struct wpa_state_machine *sm, u8 *buf,
2760 const u8 *fils_session, struct wpabuf *hlp)
2761{
2762 struct wpabuf *plain;
2763 u8 *pos = buf;
2764
2765 /* FILS Session */
2766 *pos++ = WLAN_EID_EXTENSION; /* Element ID */
2767 *pos++ = 1 + FILS_SESSION_LEN; /* Length */
2768 *pos++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */
2769 os_memcpy(pos, fils_session, FILS_SESSION_LEN);
2770 pos += FILS_SESSION_LEN;
2771
2772 plain = fils_prepare_plainbuf(sm, hlp);
2773 if (!plain) {
2774 wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
2775 return NULL;
2776 }
2777
2778 os_memcpy(pos, wpabuf_head(plain), wpabuf_len(plain));
2779 pos += wpabuf_len(plain);
2780
2781 wpa_printf(MSG_DEBUG, "%s: plain buf_len: %u", __func__,
2782 (unsigned int) wpabuf_len(plain));
Hai Shalom81f62d82019-07-22 12:10:00 -07002783 wpabuf_clear_free(plain);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002784 sm->fils_completed = 1;
2785 return pos;
2786}
2787
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002788#endif /* CONFIG_FILS */
2789
2790
Hai Shalom74f70d42019-02-11 14:42:39 -08002791#ifdef CONFIG_OCV
2792int get_sta_tx_parameters(struct wpa_state_machine *sm, int ap_max_chanwidth,
2793 int ap_seg1_idx, int *bandwidth, int *seg1_idx)
2794{
2795 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2796
2797 if (!wpa_auth->cb->get_sta_tx_params)
2798 return -1;
2799 return wpa_auth->cb->get_sta_tx_params(wpa_auth->cb_ctx, sm->addr,
2800 ap_max_chanwidth, ap_seg1_idx,
2801 bandwidth, seg1_idx);
2802}
2803#endif /* CONFIG_OCV */
2804
2805
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
2807{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002808 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002809 struct wpa_ptk PTK;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002810 int ok = 0, psk_found = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002811 const u8 *pmk = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002812 size_t pmk_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002813 int ft;
2814 const u8 *eapol_key_ie, *key_data, *mic;
2815 u16 key_data_length;
2816 size_t mic_len, eapol_key_ie_len;
2817 struct ieee802_1x_hdr *hdr;
2818 struct wpa_eapol_key *key;
2819 struct wpa_eapol_ie_parse kde;
Hai Shalom021b0b52019-04-10 11:17:58 -07002820 int vlan_id = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002821
2822 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
2823 sm->EAPOLKeyReceived = FALSE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002824 sm->update_snonce = FALSE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002825 os_memset(&PTK, 0, sizeof(PTK));
2826
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002827 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002828
2829 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2830 * WPA-PSK: iterate through possible PSKs and select the one matching
2831 * the packet */
2832 for (;;) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002833 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
2834 !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002835 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
Hai Shalom021b0b52019-04-10 11:17:58 -07002836 sm->p2p_dev_addr, pmk, &pmk_len,
2837 &vlan_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002838 if (pmk == NULL)
2839 break;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002840 psk_found = 1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002841#ifdef CONFIG_IEEE80211R_AP
2842 if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
2843 os_memcpy(sm->xxkey, pmk, pmk_len);
2844 sm->xxkey_len = pmk_len;
2845 }
2846#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002847 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002848 pmk = sm->PMK;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002849 pmk_len = sm->pmk_len;
2850 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002851
Hai Shalom81f62d82019-07-22 12:10:00 -07002852 if ((!pmk || !pmk_len) && sm->pmksa) {
2853 wpa_printf(MSG_DEBUG, "WPA: Use PMK from PMKSA cache");
2854 pmk = sm->pmksa->pmk;
2855 pmk_len = sm->pmksa->pmk_len;
2856 }
2857
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002858 if (wpa_derive_ptk(sm, sm->SNonce, pmk, pmk_len, &PTK) < 0)
2859 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002860
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002861 if (mic_len &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002862 wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002863 sm->last_rx_eapol_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002864 sm->last_rx_eapol_key_len) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07002865 if (sm->PMK != pmk) {
2866 os_memcpy(sm->PMK, pmk, pmk_len);
2867 sm->pmk_len = pmk_len;
2868 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002869 ok = 1;
2870 break;
2871 }
2872
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002873#ifdef CONFIG_FILS
2874 if (!mic_len &&
2875 wpa_aead_decrypt(sm, &PTK, sm->last_rx_eapol_key,
2876 sm->last_rx_eapol_key_len, NULL) == 0) {
2877 ok = 1;
2878 break;
2879 }
2880#endif /* CONFIG_FILS */
2881
Roshan Pius3a1667e2018-07-03 15:17:14 -07002882 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
2883 wpa_key_mgmt_sae(sm->wpa_key_mgmt))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884 break;
2885 }
2886
2887 if (!ok) {
2888 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2889 "invalid MIC in msg 2/4 of 4-Way Handshake");
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002890 if (psk_found)
2891 wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002892 return;
2893 }
2894
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002895 /*
2896 * Note: last_rx_eapol_key length fields have already been validated in
2897 * wpa_receive().
2898 */
2899 hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
2900 key = (struct wpa_eapol_key *) (hdr + 1);
2901 mic = (u8 *) (key + 1);
2902 key_data = mic + mic_len + 2;
2903 key_data_length = WPA_GET_BE16(mic + mic_len);
2904 if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
2905 sizeof(*key) - mic_len - 2)
2906 return;
2907
2908 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
2909 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
2910 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
2911 return;
2912 }
2913 if (kde.rsn_ie) {
2914 eapol_key_ie = kde.rsn_ie;
2915 eapol_key_ie_len = kde.rsn_ie_len;
2916 } else if (kde.osen) {
2917 eapol_key_ie = kde.osen;
2918 eapol_key_ie_len = kde.osen_len;
2919 } else {
2920 eapol_key_ie = kde.wpa_ie;
2921 eapol_key_ie_len = kde.wpa_ie_len;
2922 }
2923 ft = sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt);
2924 if (sm->wpa_ie == NULL ||
2925 wpa_compare_rsn_ie(ft, sm->wpa_ie, sm->wpa_ie_len,
2926 eapol_key_ie, eapol_key_ie_len)) {
2927 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2928 "WPA IE from (Re)AssocReq did not match with msg 2/4");
2929 if (sm->wpa_ie) {
2930 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
2931 sm->wpa_ie, sm->wpa_ie_len);
2932 }
2933 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
2934 eapol_key_ie, eapol_key_ie_len);
2935 /* MLME-DEAUTHENTICATE.request */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002936 wpa_sta_disconnect(wpa_auth, sm->addr,
2937 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002938 return;
2939 }
Hai Shalomc3565922019-10-28 11:58:20 -07002940 if ((!sm->rsnxe && kde.rsnxe) ||
2941 (sm->rsnxe && !kde.rsnxe) ||
2942 (sm->rsnxe && kde.rsnxe &&
2943 (sm->rsnxe_len != kde.rsnxe_len ||
2944 os_memcmp(sm->rsnxe, kde.rsnxe, sm->rsnxe_len) != 0))) {
2945 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2946 "RSNXE from (Re)AssocReq did not match the one in EAPOL-Key msg 2/4");
2947 wpa_hexdump(MSG_DEBUG, "RSNXE in AssocReq",
2948 sm->rsnxe, sm->rsnxe_len);
2949 wpa_hexdump(MSG_DEBUG, "RSNXE in EAPOL-Key msg 2/4",
2950 kde.rsnxe, kde.rsnxe_len);
2951 /* MLME-DEAUTHENTICATE.request */
2952 wpa_sta_disconnect(wpa_auth, sm->addr,
2953 WLAN_REASON_PREV_AUTH_NOT_VALID);
2954 return;
2955 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002956#ifdef CONFIG_OCV
2957 if (wpa_auth_uses_ocv(sm)) {
2958 struct wpa_channel_info ci;
2959 int tx_chanwidth;
2960 int tx_seg1_idx;
2961
2962 if (wpa_channel_info(wpa_auth, &ci) != 0) {
2963 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2964 "Failed to get channel info to validate received OCI in EAPOL-Key 2/4");
2965 return;
2966 }
2967
2968 if (get_sta_tx_parameters(sm,
2969 channel_width_to_int(ci.chanwidth),
2970 ci.seg1_idx, &tx_chanwidth,
2971 &tx_seg1_idx) < 0)
2972 return;
2973
2974 if (ocv_verify_tx_params(kde.oci, kde.oci_len, &ci,
2975 tx_chanwidth, tx_seg1_idx) != 0) {
2976 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2977 ocv_errorstr);
2978 return;
2979 }
2980 }
2981#endif /* CONFIG_OCV */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002982#ifdef CONFIG_IEEE80211R_AP
2983 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002984 wpa_sta_disconnect(wpa_auth, sm->addr,
2985 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002986 return;
2987 }
2988#endif /* CONFIG_IEEE80211R_AP */
2989#ifdef CONFIG_P2P
2990 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
2991 wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
2992 int idx;
2993 wpa_printf(MSG_DEBUG,
2994 "P2P: IP address requested in EAPOL-Key exchange");
2995 idx = bitfield_get_first_zero(wpa_auth->ip_pool);
2996 if (idx >= 0) {
2997 u32 start = WPA_GET_BE32(wpa_auth->conf.ip_addr_start);
2998 bitfield_set(wpa_auth->ip_pool, idx);
2999 WPA_PUT_BE32(sm->ip_addr, start + idx);
3000 wpa_printf(MSG_DEBUG,
3001 "P2P: Assigned IP address %u.%u.%u.%u to "
3002 MACSTR, sm->ip_addr[0], sm->ip_addr[1],
3003 sm->ip_addr[2], sm->ip_addr[3],
3004 MAC2STR(sm->addr));
3005 }
3006 }
3007#endif /* CONFIG_P2P */
3008
3009#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003010 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3011 /*
3012 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
3013 * with the value we derived.
3014 */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003015 if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
3016 WPA_PMK_NAME_LEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003017 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3018 "PMKR1Name mismatch in FT 4-way "
3019 "handshake");
3020 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
3021 "Supplicant",
3022 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
3023 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
3024 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
3025 return;
3026 }
3027 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003028#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029
Hai Shalom021b0b52019-04-10 11:17:58 -07003030 if (vlan_id && wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
3031 wpa_auth_update_vlan(wpa_auth, sm->addr, vlan_id) < 0) {
3032 wpa_sta_disconnect(wpa_auth, sm->addr,
3033 WLAN_REASON_PREV_AUTH_NOT_VALID);
3034 return;
3035 }
3036
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003037 sm->pending_1_of_4_timeout = 0;
3038 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
3039
3040 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
3041 /* PSK may have changed from the previous choice, so update
3042 * state machine data based on whatever PSK was selected here.
3043 */
3044 os_memcpy(sm->PMK, pmk, PMK_LEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003045 sm->pmk_len = PMK_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046 }
3047
3048 sm->MICVerified = TRUE;
3049
3050 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
Hai Shalom81f62d82019-07-22 12:10:00 -07003051 forced_memzero(&PTK, sizeof(PTK));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003052 sm->PTK_valid = TRUE;
3053}
3054
3055
3056SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
3057{
3058 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
3059 sm->TimeoutCtr = 0;
3060}
3061
3062
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003063static int ieee80211w_kde_len(struct wpa_state_machine *sm)
3064{
3065 if (sm->mgmt_frame_prot) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003066 size_t len;
3067 len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
3068 return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003069 }
3070
3071 return 0;
3072}
3073
3074
3075static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
3076{
3077 struct wpa_igtk_kde igtk;
3078 struct wpa_group *gsm = sm->group;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003079 u8 rsc[WPA_KEY_RSC_LEN];
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003080 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003081
3082 if (!sm->mgmt_frame_prot)
3083 return pos;
3084
3085 igtk.keyid[0] = gsm->GN_igtk;
3086 igtk.keyid[1] = 0;
3087 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003088 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003089 os_memset(igtk.pn, 0, sizeof(igtk.pn));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003090 else
3091 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003092 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003093 if (sm->wpa_auth->conf.disable_gtk ||
3094 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003095 /*
3096 * Provide unique random IGTK to each STA to prevent use of
3097 * IGTK in the BSS.
3098 */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003099 if (random_get_bytes(igtk.igtk, len) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003100 return pos;
3101 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003102 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003103 (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
3104 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003105
3106 return pos;
3107}
3108
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003109
Hai Shalom74f70d42019-02-11 14:42:39 -08003110static int ocv_oci_len(struct wpa_state_machine *sm)
3111{
3112#ifdef CONFIG_OCV
3113 if (wpa_auth_uses_ocv(sm))
3114 return OCV_OCI_KDE_LEN;
3115#endif /* CONFIG_OCV */
3116 return 0;
3117}
3118
3119static int ocv_oci_add(struct wpa_state_machine *sm, u8 **argpos)
3120{
3121#ifdef CONFIG_OCV
3122 struct wpa_channel_info ci;
3123
3124 if (!wpa_auth_uses_ocv(sm))
3125 return 0;
3126
3127 if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
3128 wpa_printf(MSG_WARNING,
3129 "Failed to get channel info for OCI element");
3130 return -1;
3131 }
3132
3133 return ocv_insert_oci_kde(&ci, argpos);
3134#else /* CONFIG_OCV */
3135 return 0;
3136#endif /* CONFIG_OCV */
3137}
3138
3139
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003140SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
3141{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003142 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003143 size_t gtk_len, kde_len;
3144 struct wpa_group *gsm = sm->group;
3145 u8 *wpa_ie;
Hai Shalomc3565922019-10-28 11:58:20 -07003146 int wpa_ie_len, secure, gtkidx, encr = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003147
3148 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
3149 sm->TimeoutEvt = FALSE;
3150
3151 sm->TimeoutCtr++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003152 if (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3153 sm->TimeoutCtr > 1) {
3154 /* Do not allow retransmission of EAPOL-Key msg 3/4 */
3155 return;
3156 }
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003157 if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003158 /* No point in sending the EAPOL-Key - we will disconnect
3159 * immediately following this. */
3160 return;
3161 }
3162
3163 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
3164 GTK[GN], IGTK, [FTIE], [TIE * 2])
3165 */
3166 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
3167 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
3168 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
3169 wpa_ie = sm->wpa_auth->wpa_ie;
3170 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
3171 if (sm->wpa == WPA_VERSION_WPA &&
3172 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
3173 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003174 /* WPA-only STA, remove RSN IE and possible MDIE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003175 wpa_ie = wpa_ie + wpa_ie[1] + 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003176 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
3177 wpa_ie = wpa_ie + wpa_ie[1] + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003178 wpa_ie_len = wpa_ie[1] + 2;
3179 }
3180 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3181 "sending 3/4 msg of 4-Way Handshake");
3182 if (sm->wpa == WPA_VERSION_WPA2) {
3183 /* WPA2 send GTK in the 4-way handshake */
3184 secure = 1;
3185 gtk = gsm->GTK[gsm->GN - 1];
3186 gtk_len = gsm->GTK_len;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003187 if (sm->wpa_auth->conf.disable_gtk ||
3188 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003189 /*
3190 * Provide unique random GTK to each STA to prevent use
3191 * of GTK in the BSS.
3192 */
3193 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
3194 return;
3195 gtk = dummy_gtk;
3196 }
Hai Shalomc3565922019-10-28 11:58:20 -07003197 gtkidx = gsm->GN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003198 _rsc = rsc;
3199 encr = 1;
3200 } else {
3201 /* WPA does not include GTK in msg 3/4 */
3202 secure = 0;
3203 gtk = NULL;
3204 gtk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003205 _rsc = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003206 if (sm->rx_eapol_key_secure) {
3207 /*
3208 * It looks like Windows 7 supplicant tries to use
3209 * Secure bit in msg 2/4 after having reported Michael
3210 * MIC failure and it then rejects the 4-way handshake
3211 * if msg 3/4 does not set Secure bit. Work around this
3212 * by setting the Secure bit here even in the case of
3213 * WPA if the supplicant used it first.
3214 */
3215 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3216 "STA used Secure bit in WPA msg 2/4 - "
3217 "set Secure for 3/4 as workaround");
3218 secure = 1;
3219 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220 }
3221
Hai Shalom74f70d42019-02-11 14:42:39 -08003222 kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003223 if (gtk)
3224 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003225#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003226 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3227 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
3228 kde_len += 300; /* FTIE + 2 * TIE */
3229 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003230#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003231#ifdef CONFIG_P2P
3232 if (WPA_GET_BE32(sm->ip_addr) > 0)
3233 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
3234#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003235 kde = os_malloc(kde_len);
3236 if (kde == NULL)
3237 return;
3238
3239 pos = kde;
3240 os_memcpy(pos, wpa_ie, wpa_ie_len);
3241 pos += wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003242#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003243 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003244 int res;
3245 size_t elen;
3246
3247 elen = pos - kde;
3248 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003249 if (res < 0) {
3250 wpa_printf(MSG_ERROR, "FT: Failed to insert "
3251 "PMKR1Name into RSN IE in EAPOL-Key data");
3252 os_free(kde);
3253 return;
3254 }
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003255 pos -= wpa_ie_len;
3256 pos += elen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003257 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003258#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003259 if (gtk) {
3260 u8 hdr[2];
Hai Shalomc3565922019-10-28 11:58:20 -07003261 hdr[0] = gtkidx & 0x03;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003262 hdr[1] = 0;
3263 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
3264 gtk, gtk_len);
3265 }
3266 pos = ieee80211w_kde_add(sm, pos);
Hai Shalom74f70d42019-02-11 14:42:39 -08003267 if (ocv_oci_add(sm, &pos) < 0) {
3268 os_free(kde);
3269 return;
3270 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003271
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003272#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003273 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3274 int res;
3275 struct wpa_auth_config *conf;
3276
3277 conf = &sm->wpa_auth->conf;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003278 if (sm->assoc_resp_ftie &&
3279 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
3280 os_memcpy(pos, sm->assoc_resp_ftie,
3281 2 + sm->assoc_resp_ftie[1]);
3282 res = 2 + sm->assoc_resp_ftie[1];
3283 } else {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003284 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
3285
3286 res = wpa_write_ftie(conf, use_sha384,
3287 conf->r0_key_holder,
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003288 conf->r0_key_holder_len,
3289 NULL, NULL, pos,
3290 kde + kde_len - pos,
3291 NULL, 0);
3292 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003293 if (res < 0) {
3294 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
3295 "into EAPOL-Key Key Data");
3296 os_free(kde);
3297 return;
3298 }
3299 pos += res;
3300
3301 /* TIE[ReassociationDeadline] (TU) */
3302 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
3303 *pos++ = 5;
3304 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
3305 WPA_PUT_LE32(pos, conf->reassociation_deadline);
3306 pos += 4;
3307
3308 /* TIE[KeyLifetime] (seconds) */
3309 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
3310 *pos++ = 5;
3311 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003312 WPA_PUT_LE32(pos, conf->r0_key_lifetime);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003313 pos += 4;
3314 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003315#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003316#ifdef CONFIG_P2P
3317 if (WPA_GET_BE32(sm->ip_addr) > 0) {
3318 u8 addr[3 * 4];
3319 os_memcpy(addr, sm->ip_addr, 4);
3320 os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
3321 os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
3322 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
3323 addr, sizeof(addr), NULL, 0);
3324 }
3325#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003326
3327 wpa_send_eapol(sm->wpa_auth, sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003328 (secure ? WPA_KEY_INFO_SECURE : 0) |
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003329 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
3330 WPA_KEY_INFO_MIC : 0) |
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003331 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
3332 WPA_KEY_INFO_KEY_TYPE,
Hai Shalomc3565922019-10-28 11:58:20 -07003333 _rsc, sm->ANonce, kde, pos - kde, 0, encr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003334 os_free(kde);
3335}
3336
3337
3338SM_STATE(WPA_PTK, PTKINITDONE)
3339{
3340 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
3341 sm->EAPOLKeyReceived = FALSE;
3342 if (sm->Pair) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003343 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
3344 int klen = wpa_cipher_key_len(sm->pairwise);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003345 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003346 sm->PTK.tk, klen)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003347 wpa_sta_disconnect(sm->wpa_auth, sm->addr,
3348 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003349 return;
3350 }
3351 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
3352 sm->pairwise_set = TRUE;
3353
Hai Shalom81f62d82019-07-22 12:10:00 -07003354 wpa_auth_set_ptk_rekey_timer(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003355
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003356 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
3357 sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP ||
3358 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003359 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
3360 WPA_EAPOL_authorized, 1);
3361 }
3362 }
3363
3364 if (0 /* IBSS == TRUE */) {
3365 sm->keycount++;
3366 if (sm->keycount == 2) {
3367 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
3368 WPA_EAPOL_portValid, 1);
3369 }
3370 } else {
3371 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
3372 1);
3373 }
3374 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
3375 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
3376 if (sm->wpa == WPA_VERSION_WPA)
3377 sm->PInitAKeys = TRUE;
3378 else
3379 sm->has_GTK = TRUE;
3380 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3381 "pairwise key handshake completed (%s)",
3382 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
3383
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003384#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003385 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003386#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003387}
3388
3389
3390SM_STEP(WPA_PTK)
3391{
3392 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3393
3394 if (sm->Init)
3395 SM_ENTER(WPA_PTK, INITIALIZE);
3396 else if (sm->Disconnect
3397 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
3398 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
3399 "WPA_PTK: sm->Disconnect");
3400 SM_ENTER(WPA_PTK, DISCONNECT);
3401 }
3402 else if (sm->DeauthenticationRequest)
3403 SM_ENTER(WPA_PTK, DISCONNECTED);
3404 else if (sm->AuthenticationRequest)
3405 SM_ENTER(WPA_PTK, AUTHENTICATION);
3406 else if (sm->ReAuthenticationRequest)
3407 SM_ENTER(WPA_PTK, AUTHENTICATION2);
Jouni Malinen1420a892017-10-01 12:32:57 +03003408 else if (sm->PTKRequest) {
3409 if (wpa_auth_sm_ptk_update(sm) < 0)
3410 SM_ENTER(WPA_PTK, DISCONNECTED);
3411 else
3412 SM_ENTER(WPA_PTK, PTKSTART);
3413 } else switch (sm->wpa_ptk_state) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003414 case WPA_PTK_INITIALIZE:
3415 break;
3416 case WPA_PTK_DISCONNECT:
3417 SM_ENTER(WPA_PTK, DISCONNECTED);
3418 break;
3419 case WPA_PTK_DISCONNECTED:
3420 SM_ENTER(WPA_PTK, INITIALIZE);
3421 break;
3422 case WPA_PTK_AUTHENTICATION:
3423 SM_ENTER(WPA_PTK, AUTHENTICATION2);
3424 break;
3425 case WPA_PTK_AUTHENTICATION2:
3426 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
3427 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
3428 WPA_EAPOL_keyRun) > 0)
3429 SM_ENTER(WPA_PTK, INITPMK);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003430 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
3431 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003432 /* FIX: && 802.1X::keyRun */)
3433 SM_ENTER(WPA_PTK, INITPSK);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003434 else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP)
3435 SM_ENTER(WPA_PTK, INITPMK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003436 break;
3437 case WPA_PTK_INITPMK:
3438 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003439 WPA_EAPOL_keyAvailable) > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003440 SM_ENTER(WPA_PTK, PTKSTART);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003441#ifdef CONFIG_DPP
3442 } else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && sm->pmksa) {
3443 SM_ENTER(WPA_PTK, PTKSTART);
3444#endif /* CONFIG_DPP */
3445 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003446 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3447 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3448 "INITPMK - keyAvailable = false");
3449 SM_ENTER(WPA_PTK, DISCONNECT);
3450 }
3451 break;
3452 case WPA_PTK_INITPSK:
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003453 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
Hai Shalom021b0b52019-04-10 11:17:58 -07003454 NULL, NULL, NULL)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003455 SM_ENTER(WPA_PTK, PTKSTART);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003456#ifdef CONFIG_SAE
3457 } else if (wpa_auth_uses_sae(sm) && sm->pmksa) {
3458 SM_ENTER(WPA_PTK, PTKSTART);
3459#endif /* CONFIG_SAE */
3460 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003461 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3462 "no PSK configured for the STA");
3463 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3464 SM_ENTER(WPA_PTK, DISCONNECT);
3465 }
3466 break;
3467 case WPA_PTK_PTKSTART:
3468 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3469 sm->EAPOLKeyPairwise)
3470 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3471 else if (sm->TimeoutCtr >
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003472 sm->wpa_auth->conf.wpa_pairwise_update_count) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003473 wpa_auth->dot11RSNA4WayHandshakeFailures++;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003474 wpa_auth_vlogger(
3475 sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3476 "PTKSTART: Retry limit %u reached",
3477 sm->wpa_auth->conf.wpa_pairwise_update_count);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003478 SM_ENTER(WPA_PTK, DISCONNECT);
3479 } else if (sm->TimeoutEvt)
3480 SM_ENTER(WPA_PTK, PTKSTART);
3481 break;
3482 case WPA_PTK_PTKCALCNEGOTIATING:
3483 if (sm->MICVerified)
3484 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
3485 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3486 sm->EAPOLKeyPairwise)
3487 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3488 else if (sm->TimeoutEvt)
3489 SM_ENTER(WPA_PTK, PTKSTART);
3490 break;
3491 case WPA_PTK_PTKCALCNEGOTIATING2:
3492 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3493 break;
3494 case WPA_PTK_PTKINITNEGOTIATING:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003495 if (sm->update_snonce)
3496 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3497 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3498 sm->EAPOLKeyPairwise && sm->MICVerified)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003499 SM_ENTER(WPA_PTK, PTKINITDONE);
3500 else if (sm->TimeoutCtr >
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003501 sm->wpa_auth->conf.wpa_pairwise_update_count ||
3502 (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3503 sm->TimeoutCtr > 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003504 wpa_auth->dot11RSNA4WayHandshakeFailures++;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003505 wpa_auth_vlogger(
3506 sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3507 "PTKINITNEGOTIATING: Retry limit %u reached",
3508 sm->wpa_auth->conf.wpa_pairwise_update_count);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003509 SM_ENTER(WPA_PTK, DISCONNECT);
3510 } else if (sm->TimeoutEvt)
3511 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3512 break;
3513 case WPA_PTK_PTKINITDONE:
3514 break;
3515 }
3516}
3517
3518
3519SM_STATE(WPA_PTK_GROUP, IDLE)
3520{
3521 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
3522 if (sm->Init) {
3523 /* Init flag is not cleared here, so avoid busy
3524 * loop by claiming nothing changed. */
3525 sm->changed = FALSE;
3526 }
3527 sm->GTimeoutCtr = 0;
3528}
3529
3530
3531SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
3532{
3533 u8 rsc[WPA_KEY_RSC_LEN];
3534 struct wpa_group *gsm = sm->group;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003535 const u8 *kde;
3536 u8 *kde_buf = NULL, *pos, hdr[2];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003537 size_t kde_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003538 u8 *gtk, dummy_gtk[32];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003539
3540 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
3541
3542 sm->GTimeoutCtr++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003543 if (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3544 sm->GTimeoutCtr > 1) {
3545 /* Do not allow retransmission of EAPOL-Key group msg 1/2 */
3546 return;
3547 }
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003548 if (sm->GTimeoutCtr > sm->wpa_auth->conf.wpa_group_update_count) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003549 /* No point in sending the EAPOL-Key - we will disconnect
3550 * immediately following this. */
3551 return;
3552 }
3553
3554 if (sm->wpa == WPA_VERSION_WPA)
3555 sm->PInitAKeys = FALSE;
3556 sm->TimeoutEvt = FALSE;
3557 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
3558 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
3559 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
3560 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
3561 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3562 "sending 1/2 msg of Group Key Handshake");
3563
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003564 gtk = gsm->GTK[gsm->GN - 1];
Roshan Pius3a1667e2018-07-03 15:17:14 -07003565 if (sm->wpa_auth->conf.disable_gtk ||
3566 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003567 /*
3568 * Provide unique random GTK to each STA to prevent use
3569 * of GTK in the BSS.
3570 */
3571 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
3572 return;
3573 gtk = dummy_gtk;
3574 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003575 if (sm->wpa == WPA_VERSION_WPA2) {
3576 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
Hai Shalom74f70d42019-02-11 14:42:39 -08003577 ieee80211w_kde_len(sm) + ocv_oci_len(sm);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003578 kde_buf = os_malloc(kde_len);
3579 if (kde_buf == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003580 return;
3581
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003582 kde = pos = kde_buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003583 hdr[0] = gsm->GN & 0x03;
3584 hdr[1] = 0;
3585 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003586 gtk, gsm->GTK_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003587 pos = ieee80211w_kde_add(sm, pos);
Hai Shalom74f70d42019-02-11 14:42:39 -08003588 if (ocv_oci_add(sm, &pos) < 0) {
3589 os_free(kde_buf);
3590 return;
3591 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003592 kde_len = pos - kde;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003593 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003594 kde = gtk;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003595 kde_len = gsm->GTK_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003596 }
3597
3598 wpa_send_eapol(sm->wpa_auth, sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003599 WPA_KEY_INFO_SECURE |
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003600 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
3601 WPA_KEY_INFO_MIC : 0) |
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003602 WPA_KEY_INFO_ACK |
3603 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003604 rsc, NULL, kde, kde_len, gsm->GN, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003605
3606 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003607}
3608
3609
3610SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
3611{
Hai Shalom74f70d42019-02-11 14:42:39 -08003612#ifdef CONFIG_OCV
3613 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3614 const u8 *key_data, *mic;
3615 struct ieee802_1x_hdr *hdr;
3616 struct wpa_eapol_key *key;
3617 struct wpa_eapol_ie_parse kde;
3618 size_t mic_len;
3619 u16 key_data_length;
3620#endif /* CONFIG_OCV */
3621
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003622 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
3623 sm->EAPOLKeyReceived = FALSE;
Hai Shalom74f70d42019-02-11 14:42:39 -08003624
3625#ifdef CONFIG_OCV
3626 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
3627
3628 /*
3629 * Note: last_rx_eapol_key length fields have already been validated in
3630 * wpa_receive().
3631 */
3632 hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
3633 key = (struct wpa_eapol_key *) (hdr + 1);
3634 mic = (u8 *) (key + 1);
3635 key_data = mic + mic_len + 2;
3636 key_data_length = WPA_GET_BE16(mic + mic_len);
3637 if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
3638 sizeof(*key) - mic_len - 2)
3639 return;
3640
3641 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
3642 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
3643 "received EAPOL-Key group msg 2/2 with invalid Key Data contents");
3644 return;
3645 }
3646
3647 if (wpa_auth_uses_ocv(sm)) {
3648 struct wpa_channel_info ci;
3649 int tx_chanwidth;
3650 int tx_seg1_idx;
3651
3652 if (wpa_channel_info(wpa_auth, &ci) != 0) {
3653 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
3654 "Failed to get channel info to validate received OCI in EAPOL-Key group 1/2");
3655 return;
3656 }
3657
3658 if (get_sta_tx_parameters(sm,
3659 channel_width_to_int(ci.chanwidth),
3660 ci.seg1_idx, &tx_chanwidth,
3661 &tx_seg1_idx) < 0)
3662 return;
3663
3664 if (ocv_verify_tx_params(kde.oci, kde.oci_len, &ci,
3665 tx_chanwidth, tx_seg1_idx) != 0) {
3666 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
3667 ocv_errorstr);
3668 return;
3669 }
3670 }
3671#endif /* CONFIG_OCV */
3672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003673 if (sm->GUpdateStationKeys)
3674 sm->group->GKeyDoneStations--;
3675 sm->GUpdateStationKeys = FALSE;
3676 sm->GTimeoutCtr = 0;
3677 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
3678 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3679 "group key handshake completed (%s)",
3680 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
3681 sm->has_GTK = TRUE;
3682}
3683
3684
3685SM_STATE(WPA_PTK_GROUP, KEYERROR)
3686{
3687 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
3688 if (sm->GUpdateStationKeys)
3689 sm->group->GKeyDoneStations--;
3690 sm->GUpdateStationKeys = FALSE;
3691 sm->Disconnect = TRUE;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003692 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3693 "group key handshake failed (%s) after %u tries",
3694 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN",
3695 sm->wpa_auth->conf.wpa_group_update_count);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003696}
3697
3698
3699SM_STEP(WPA_PTK_GROUP)
3700{
3701 if (sm->Init || sm->PtkGroupInit) {
3702 SM_ENTER(WPA_PTK_GROUP, IDLE);
3703 sm->PtkGroupInit = FALSE;
3704 } else switch (sm->wpa_ptk_group_state) {
3705 case WPA_PTK_GROUP_IDLE:
3706 if (sm->GUpdateStationKeys ||
3707 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
3708 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3709 break;
3710 case WPA_PTK_GROUP_REKEYNEGOTIATING:
3711 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3712 !sm->EAPOLKeyPairwise && sm->MICVerified)
3713 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
3714 else if (sm->GTimeoutCtr >
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003715 sm->wpa_auth->conf.wpa_group_update_count ||
3716 (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3717 sm->GTimeoutCtr > 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003718 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
3719 else if (sm->TimeoutEvt)
3720 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3721 break;
3722 case WPA_PTK_GROUP_KEYERROR:
3723 SM_ENTER(WPA_PTK_GROUP, IDLE);
3724 break;
3725 case WPA_PTK_GROUP_REKEYESTABLISHED:
3726 SM_ENTER(WPA_PTK_GROUP, IDLE);
3727 break;
3728 }
3729}
3730
3731
3732static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
3733 struct wpa_group *group)
3734{
3735 int ret = 0;
3736
3737 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3738 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3739 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
3740 wpa_auth->addr, group->GNonce,
3741 group->GTK[group->GN - 1], group->GTK_len) < 0)
3742 ret = -1;
3743 wpa_hexdump_key(MSG_DEBUG, "GTK",
3744 group->GTK[group->GN - 1], group->GTK_len);
3745
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003746 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003747 size_t len;
3748 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003749 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3750 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3751 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
3752 wpa_auth->addr, group->GNonce,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003753 group->IGTK[group->GN_igtk - 4], len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003754 ret = -1;
3755 wpa_hexdump_key(MSG_DEBUG, "IGTK",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003756 group->IGTK[group->GN_igtk - 4], len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003757 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003758
3759 return ret;
3760}
3761
3762
3763static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
3764 struct wpa_group *group)
3765{
3766 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3767 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
3768 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
3769 group->wpa_group_state = WPA_GROUP_GTK_INIT;
3770
3771 /* GTK[0..N] = 0 */
3772 os_memset(group->GTK, 0, sizeof(group->GTK));
3773 group->GN = 1;
3774 group->GM = 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003775 group->GN_igtk = 4;
3776 group->GM_igtk = 5;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003777 /* GTK[GN] = CalcGTK() */
3778 wpa_gtk_update(wpa_auth, group);
3779}
3780
3781
3782static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
3783{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003784 if (ctx != NULL && ctx != sm->group)
3785 return 0;
3786
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003787 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
3788 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3789 "Not in PTKINITDONE; skip Group Key update");
3790 sm->GUpdateStationKeys = FALSE;
3791 return 0;
3792 }
3793 if (sm->GUpdateStationKeys) {
3794 /*
3795 * This should not really happen, so add a debug log entry.
3796 * Since we clear the GKeyDoneStations before the loop, the
3797 * station needs to be counted here anyway.
3798 */
3799 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3800 "GUpdateStationKeys was already set when "
3801 "marking station for GTK rekeying");
3802 }
3803
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003804 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003805 if (sm->is_wnmsleep)
3806 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003807
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003808 sm->group->GKeyDoneStations++;
3809 sm->GUpdateStationKeys = TRUE;
3810
3811 wpa_sm_step(sm);
3812 return 0;
3813}
3814
3815
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003816#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003817/* update GTK when exiting WNM-Sleep Mode */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003818void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
3819{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003820 if (sm == NULL || sm->is_wnmsleep)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003821 return;
3822
3823 wpa_group_update_sta(sm, NULL);
3824}
3825
3826
3827void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
3828{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003829 if (sm)
3830 sm->is_wnmsleep = !!flag;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003831}
3832
3833
3834int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3835{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003836 struct wpa_group *gsm = sm->group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003837 u8 *start = pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003838
3839 /*
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003840 * GTK subelement:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003841 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003842 * Key[5..32]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003843 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003844 *pos++ = WNM_SLEEP_SUBELEM_GTK;
3845 *pos++ = 11 + gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003846 /* Key ID in B0-B1 of Key Info */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003847 WPA_PUT_LE16(pos, gsm->GN & 0x03);
3848 pos += 2;
3849 *pos++ = gsm->GTK_len;
3850 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003851 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003852 pos += 8;
3853 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
3854 pos += gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003855
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003856 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
3857 gsm->GN);
3858 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003859 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003860
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003861 return pos - start;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003862}
3863
3864
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003865int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3866{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003867 struct wpa_group *gsm = sm->group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003868 u8 *start = pos;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003869 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003870
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003871 /*
3872 * IGTK subelement:
3873 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
3874 */
3875 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003876 *pos++ = 2 + 6 + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003877 WPA_PUT_LE16(pos, gsm->GN_igtk);
3878 pos += 2;
3879 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003880 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003881 pos += 6;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003882
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003883 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
3884 pos += len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003885
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003886 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
3887 gsm->GN_igtk);
3888 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003889 gsm->IGTK[gsm->GN_igtk - 4], len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003890
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003891 return pos - start;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003892}
Hai Shalomc3565922019-10-28 11:58:20 -07003893
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003894#endif /* CONFIG_WNM_AP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003895
3896
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003897static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
3898 struct wpa_group *group)
3899{
3900 int tmp;
3901
3902 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3903 "SETKEYS (VLAN-ID %d)", group->vlan_id);
3904 group->changed = TRUE;
3905 group->wpa_group_state = WPA_GROUP_SETKEYS;
3906 group->GTKReKey = FALSE;
3907 tmp = group->GM;
3908 group->GM = group->GN;
3909 group->GN = tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003910 tmp = group->GM_igtk;
3911 group->GM_igtk = group->GN_igtk;
3912 group->GN_igtk = tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003913 /* "GKeyDoneStations = GNoStations" is done in more robust way by
3914 * counting the STAs that are marked with GUpdateStationKeys instead of
3915 * including all STAs that could be in not-yet-completed state. */
3916 wpa_gtk_update(wpa_auth, group);
3917
3918 if (group->GKeyDoneStations) {
3919 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
3920 "GKeyDoneStations=%d when starting new GTK rekey",
3921 group->GKeyDoneStations);
3922 group->GKeyDoneStations = 0;
3923 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003924 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003925 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
3926 group->GKeyDoneStations);
3927}
3928
3929
3930static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
3931 struct wpa_group *group)
3932{
3933 int ret = 0;
3934
3935 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003936 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003937 broadcast_ether_addr, group->GN,
3938 group->GTK[group->GN - 1], group->GTK_len) < 0)
3939 ret = -1;
3940
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003941 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
3942 enum wpa_alg alg;
3943 size_t len;
3944
3945 alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
3946 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
3947
3948 if (ret == 0 &&
3949 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
3950 broadcast_ether_addr, group->GN_igtk,
3951 group->IGTK[group->GN_igtk - 4], len) < 0)
3952 ret = -1;
3953 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003954
3955 return ret;
3956}
3957
3958
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003959static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
3960{
3961 if (sm->group == ctx) {
3962 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
3963 " for discconnection due to fatal failure",
3964 MAC2STR(sm->addr));
3965 sm->Disconnect = TRUE;
3966 }
3967
3968 return 0;
3969}
3970
3971
3972static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
3973 struct wpa_group *group)
3974{
3975 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
3976 group->changed = TRUE;
3977 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
3978 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
3979}
3980
3981
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003982static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
3983 struct wpa_group *group)
3984{
3985 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3986 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
3987 group->changed = TRUE;
3988 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
3989
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003990 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
3991 wpa_group_fatal_failure(wpa_auth, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003992 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003993 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003994
3995 return 0;
3996}
3997
3998
3999static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
4000 struct wpa_group *group)
4001{
4002 if (group->GInit) {
4003 wpa_group_gtk_init(wpa_auth, group);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004004 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
4005 /* Do not allow group operations */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004006 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
4007 group->GTKAuthenticator) {
4008 wpa_group_setkeysdone(wpa_auth, group);
4009 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
4010 group->GTKReKey) {
4011 wpa_group_setkeys(wpa_auth, group);
4012 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
4013 if (group->GKeyDoneStations == 0)
4014 wpa_group_setkeysdone(wpa_auth, group);
4015 else if (group->GTKReKey)
4016 wpa_group_setkeys(wpa_auth, group);
4017 }
4018}
4019
4020
4021static int wpa_sm_step(struct wpa_state_machine *sm)
4022{
4023 if (sm == NULL)
4024 return 0;
4025
4026 if (sm->in_step_loop) {
4027 /* This should not happen, but if it does, make sure we do not
4028 * end up freeing the state machine too early by exiting the
4029 * recursive call. */
4030 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
4031 return 0;
4032 }
4033
4034 sm->in_step_loop = 1;
4035 do {
4036 if (sm->pending_deinit)
4037 break;
4038
4039 sm->changed = FALSE;
4040 sm->wpa_auth->group->changed = FALSE;
4041
4042 SM_STEP_RUN(WPA_PTK);
4043 if (sm->pending_deinit)
4044 break;
4045 SM_STEP_RUN(WPA_PTK_GROUP);
4046 if (sm->pending_deinit)
4047 break;
4048 wpa_group_sm_step(sm->wpa_auth, sm->group);
4049 } while (sm->changed || sm->wpa_auth->group->changed);
4050 sm->in_step_loop = 0;
4051
4052 if (sm->pending_deinit) {
4053 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
4054 "machine deinit for " MACSTR, MAC2STR(sm->addr));
4055 wpa_free_sta_sm(sm);
4056 return 1;
4057 }
4058 return 0;
4059}
4060
4061
4062static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
4063{
4064 struct wpa_state_machine *sm = eloop_ctx;
4065 wpa_sm_step(sm);
4066}
4067
4068
4069void wpa_auth_sm_notify(struct wpa_state_machine *sm)
4070{
4071 if (sm == NULL)
4072 return;
4073 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
4074}
4075
4076
4077void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
4078{
4079 int tmp, i;
4080 struct wpa_group *group;
4081
4082 if (wpa_auth == NULL)
4083 return;
4084
4085 group = wpa_auth->group;
4086
4087 for (i = 0; i < 2; i++) {
4088 tmp = group->GM;
4089 group->GM = group->GN;
4090 group->GN = tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004091 tmp = group->GM_igtk;
4092 group->GM_igtk = group->GN_igtk;
4093 group->GN_igtk = tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004094 wpa_gtk_update(wpa_auth, group);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004095 wpa_group_config_group_keys(wpa_auth, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004096 }
4097}
4098
4099
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004100static const char * wpa_bool_txt(int val)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004101{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004102 return val ? "TRUE" : "FALSE";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004103}
4104
4105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004106#define RSN_SUITE "%02x-%02x-%02x-%d"
4107#define RSN_SUITE_ARG(s) \
4108((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
4109
4110int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
4111{
4112 int len = 0, ret;
4113 char pmkid_txt[PMKID_LEN * 2 + 1];
4114#ifdef CONFIG_RSN_PREAUTH
4115 const int preauth = 1;
4116#else /* CONFIG_RSN_PREAUTH */
4117 const int preauth = 0;
4118#endif /* CONFIG_RSN_PREAUTH */
4119
4120 if (wpa_auth == NULL)
4121 return len;
4122
4123 ret = os_snprintf(buf + len, buflen - len,
4124 "dot11RSNAOptionImplemented=TRUE\n"
4125 "dot11RSNAPreauthenticationImplemented=%s\n"
4126 "dot11RSNAEnabled=%s\n"
4127 "dot11RSNAPreauthenticationEnabled=%s\n",
4128 wpa_bool_txt(preauth),
4129 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
4130 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004131 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004132 return len;
4133 len += ret;
4134
4135 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
4136 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
4137
4138 ret = os_snprintf(
4139 buf + len, buflen - len,
4140 "dot11RSNAConfigVersion=%u\n"
4141 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
4142 /* FIX: dot11RSNAConfigGroupCipher */
4143 /* FIX: dot11RSNAConfigGroupRekeyMethod */
4144 /* FIX: dot11RSNAConfigGroupRekeyTime */
4145 /* FIX: dot11RSNAConfigGroupRekeyPackets */
4146 "dot11RSNAConfigGroupRekeyStrict=%u\n"
4147 "dot11RSNAConfigGroupUpdateCount=%u\n"
4148 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
4149 "dot11RSNAConfigGroupCipherSize=%u\n"
4150 "dot11RSNAConfigPMKLifetime=%u\n"
4151 "dot11RSNAConfigPMKReauthThreshold=%u\n"
4152 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
4153 "dot11RSNAConfigSATimeout=%u\n"
4154 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
4155 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
4156 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
4157 "dot11RSNAPMKIDUsed=%s\n"
4158 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
4159 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
4160 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
4161 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
4162 "dot11RSNA4WayHandshakeFailures=%u\n"
4163 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
4164 RSN_VERSION,
4165 !!wpa_auth->conf.wpa_strict_rekey,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004166 wpa_auth->conf.wpa_group_update_count,
4167 wpa_auth->conf.wpa_pairwise_update_count,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004168 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004169 dot11RSNAConfigPMKLifetime,
4170 dot11RSNAConfigPMKReauthThreshold,
4171 dot11RSNAConfigSATimeout,
4172 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
4173 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
4174 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
4175 pmkid_txt,
4176 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
4177 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
4178 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
4179 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
4180 wpa_auth->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004181 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004182 return len;
4183 len += ret;
4184
4185 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
4186 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
4187
4188 /* Private MIB */
4189 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
4190 wpa_auth->group->wpa_group_state);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004191 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004192 return len;
4193 len += ret;
4194
4195 return len;
4196}
4197
4198
4199int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
4200{
4201 int len = 0, ret;
4202 u32 pairwise = 0;
4203
4204 if (sm == NULL)
4205 return 0;
4206
4207 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
4208
4209 /* dot11RSNAStatsEntry */
4210
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004211 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
4212 WPA_PROTO_RSN : WPA_PROTO_WPA,
4213 sm->pairwise);
4214 if (pairwise == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004215 return 0;
4216
4217 ret = os_snprintf(
4218 buf + len, buflen - len,
4219 /* TODO: dot11RSNAStatsIndex */
4220 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
4221 "dot11RSNAStatsVersion=1\n"
4222 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
4223 /* TODO: dot11RSNAStatsTKIPICVErrors */
4224 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
4225 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
4226 /* TODO: dot11RSNAStatsCCMPReplays */
4227 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
4228 /* TODO: dot11RSNAStatsTKIPReplays */,
4229 MAC2STR(sm->addr),
4230 RSN_SUITE_ARG(pairwise),
4231 sm->dot11RSNAStatsTKIPLocalMICFailures,
4232 sm->dot11RSNAStatsTKIPRemoteMICFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004233 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004234 return len;
4235 len += ret;
4236
4237 /* Private MIB */
4238 ret = os_snprintf(buf + len, buflen - len,
Hai Shalomc3565922019-10-28 11:58:20 -07004239 "wpa=%d\n"
4240 "AKMSuiteSelector=" RSN_SUITE "\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004241 "hostapdWPAPTKState=%d\n"
4242 "hostapdWPAPTKGroupState=%d\n",
Hai Shalomc3565922019-10-28 11:58:20 -07004243 sm->wpa,
4244 RSN_SUITE_ARG(wpa_akm_to_suite(sm->wpa_key_mgmt)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004245 sm->wpa_ptk_state,
4246 sm->wpa_ptk_group_state);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004247 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004248 return len;
4249 len += ret;
4250
4251 return len;
4252}
4253
4254
4255void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
4256{
4257 if (wpa_auth)
4258 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
4259}
4260
4261
4262int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
4263{
4264 return sm && sm->pairwise_set;
4265}
4266
4267
4268int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
4269{
4270 return sm->pairwise;
4271}
4272
4273
Hai Shalom74f70d42019-02-11 14:42:39 -08004274const u8 * wpa_auth_get_pmk(struct wpa_state_machine *sm, int *len)
4275{
4276 if (!sm)
4277 return NULL;
4278 *len = sm->pmk_len;
4279 return sm->PMK;
4280}
4281
4282
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004283int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
4284{
4285 if (sm == NULL)
4286 return -1;
4287 return sm->wpa_key_mgmt;
4288}
4289
4290
4291int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
4292{
4293 if (sm == NULL)
4294 return 0;
4295 return sm->wpa;
4296}
4297
4298
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02004299int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm)
4300{
4301 if (!sm || !wpa_key_mgmt_ft(sm->wpa_key_mgmt))
4302 return 0;
4303 return sm->tk_already_set;
4304}
4305
4306
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004307int wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine *sm)
4308{
4309 if (!sm || !wpa_key_mgmt_fils(sm->wpa_key_mgmt))
4310 return 0;
4311 return sm->tk_already_set;
4312}
4313
4314
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004315int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
4316 struct rsn_pmksa_cache_entry *entry)
4317{
4318 if (sm == NULL || sm->pmksa != entry)
4319 return -1;
4320 sm->pmksa = NULL;
4321 return 0;
4322}
4323
4324
4325struct rsn_pmksa_cache_entry *
4326wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
4327{
4328 return sm ? sm->pmksa : NULL;
4329}
4330
4331
4332void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
4333{
4334 if (sm)
4335 sm->dot11RSNAStatsTKIPLocalMICFailures++;
4336}
4337
4338
4339const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
4340{
4341 if (wpa_auth == NULL)
4342 return NULL;
4343 *len = wpa_auth->wpa_ie_len;
4344 return wpa_auth->wpa_ie;
4345}
4346
4347
4348int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004349 unsigned int pmk_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004350 int session_timeout, struct eapol_state_machine *eapol)
4351{
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07004352 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
4353 sm->wpa_auth->conf.disable_pmksa_caching)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004354 return -1;
4355
Hai Shalom81f62d82019-07-22 12:10:00 -07004356#ifdef CONFIG_IEEE80211R_AP
4357 if (pmk_len >= 2 * PMK_LEN && wpa_key_mgmt_ft(sm->wpa_key_mgmt) &&
4358 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
4359 !wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
4360 /* Cache MPMK/XXKey instead of initial part from MSK */
4361 pmk = pmk + PMK_LEN;
4362 pmk_len = PMK_LEN;
4363 } else
4364#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004365 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004366 if (pmk_len > PMK_LEN_SUITE_B_192)
4367 pmk_len = PMK_LEN_SUITE_B_192;
4368 } else if (pmk_len > PMK_LEN) {
4369 pmk_len = PMK_LEN;
4370 }
4371
Hai Shalom81f62d82019-07-22 12:10:00 -07004372 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK", pmk, pmk_len);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004373 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004374 sm->PTK.kck, sm->PTK.kck_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004375 sm->wpa_auth->addr, sm->addr, session_timeout,
4376 eapol, sm->wpa_key_mgmt))
4377 return 0;
4378
4379 return -1;
4380}
4381
4382
4383int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
4384 const u8 *pmk, size_t len, const u8 *sta_addr,
4385 int session_timeout,
4386 struct eapol_state_machine *eapol)
4387{
4388 if (wpa_auth == NULL)
4389 return -1;
4390
Hai Shalom81f62d82019-07-22 12:10:00 -07004391 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK from preauth", pmk, len);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004392 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, NULL,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004393 NULL, 0,
4394 wpa_auth->addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004395 sta_addr, session_timeout, eapol,
4396 WPA_KEY_MGMT_IEEE8021X))
4397 return 0;
4398
4399 return -1;
4400}
4401
4402
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004403int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004404 const u8 *pmk, const u8 *pmkid)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004405{
4406 if (wpa_auth->conf.disable_pmksa_caching)
4407 return -1;
4408
Hai Shalom81f62d82019-07-22 12:10:00 -07004409 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK from SAE", pmk, PMK_LEN);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004410 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN, pmkid,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004411 NULL, 0,
4412 wpa_auth->addr, addr, 0, NULL,
4413 WPA_KEY_MGMT_SAE))
4414 return 0;
4415
4416 return -1;
4417}
4418
4419
Roshan Pius3a1667e2018-07-03 15:17:14 -07004420void wpa_auth_add_sae_pmkid(struct wpa_state_machine *sm, const u8 *pmkid)
4421{
4422 os_memcpy(sm->pmkid, pmkid, PMKID_LEN);
4423 sm->pmkid_set = 1;
4424}
4425
4426
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004427int wpa_auth_pmksa_add2(struct wpa_authenticator *wpa_auth, const u8 *addr,
4428 const u8 *pmk, size_t pmk_len, const u8 *pmkid,
4429 int session_timeout, int akmp)
4430{
4431 if (wpa_auth->conf.disable_pmksa_caching)
4432 return -1;
4433
Hai Shalom81f62d82019-07-22 12:10:00 -07004434 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK (2)", pmk, PMK_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004435 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, pmk_len, pmkid,
4436 NULL, 0, wpa_auth->addr, addr, session_timeout,
4437 NULL, akmp))
4438 return 0;
4439
4440 return -1;
4441}
4442
4443
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004444void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
4445 const u8 *sta_addr)
4446{
4447 struct rsn_pmksa_cache_entry *pmksa;
4448
4449 if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
4450 return;
4451 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
4452 if (pmksa) {
4453 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
4454 MACSTR " based on request", MAC2STR(sta_addr));
4455 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
4456 }
4457}
4458
4459
Dmitry Shmidte4663042016-04-04 10:07:49 -07004460int wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf,
4461 size_t len)
4462{
4463 if (!wpa_auth || !wpa_auth->pmksa)
4464 return 0;
4465 return pmksa_cache_auth_list(wpa_auth->pmksa, buf, len);
4466}
4467
4468
4469void wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth)
4470{
4471 if (wpa_auth && wpa_auth->pmksa)
4472 pmksa_cache_auth_flush(wpa_auth->pmksa);
4473}
4474
4475
Paul Stewart092955c2017-02-06 09:13:09 -08004476#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
4477#ifdef CONFIG_MESH
4478
4479int wpa_auth_pmksa_list_mesh(struct wpa_authenticator *wpa_auth, const u8 *addr,
4480 char *buf, size_t len)
4481{
4482 if (!wpa_auth || !wpa_auth->pmksa)
4483 return 0;
4484
4485 return pmksa_cache_auth_list_mesh(wpa_auth->pmksa, addr, buf, len);
4486}
4487
4488
4489struct rsn_pmksa_cache_entry *
4490wpa_auth_pmksa_create_entry(const u8 *aa, const u8 *spa, const u8 *pmk,
4491 const u8 *pmkid, int expiration)
4492{
4493 struct rsn_pmksa_cache_entry *entry;
4494 struct os_reltime now;
4495
4496 entry = pmksa_cache_auth_create_entry(pmk, PMK_LEN, pmkid, NULL, 0, aa,
4497 spa, 0, NULL, WPA_KEY_MGMT_SAE);
4498 if (!entry)
4499 return NULL;
4500
4501 os_get_reltime(&now);
4502 entry->expiration = now.sec + expiration;
4503 return entry;
4504}
4505
4506
4507int wpa_auth_pmksa_add_entry(struct wpa_authenticator *wpa_auth,
4508 struct rsn_pmksa_cache_entry *entry)
4509{
4510 int ret;
4511
4512 if (!wpa_auth || !wpa_auth->pmksa)
4513 return -1;
4514
4515 ret = pmksa_cache_auth_add_entry(wpa_auth->pmksa, entry);
4516 if (ret < 0)
4517 wpa_printf(MSG_DEBUG,
4518 "RSN: Failed to store external PMKSA cache for "
4519 MACSTR, MAC2STR(entry->spa));
4520
4521 return ret;
4522}
4523
4524#endif /* CONFIG_MESH */
4525#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
4526
4527
Dmitry Shmidte4663042016-04-04 10:07:49 -07004528struct rsn_pmksa_cache_entry *
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004529wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
4530 const u8 *pmkid)
Dmitry Shmidte4663042016-04-04 10:07:49 -07004531{
4532 if (!wpa_auth || !wpa_auth->pmksa)
4533 return NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004534 return pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, pmkid);
Dmitry Shmidte4663042016-04-04 10:07:49 -07004535}
4536
4537
4538void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa,
4539 struct wpa_state_machine *sm,
4540 struct wpa_authenticator *wpa_auth,
4541 u8 *pmkid, u8 *pmk)
4542{
4543 if (!sm)
4544 return;
4545
4546 sm->pmksa = pmksa;
4547 os_memcpy(pmk, pmksa->pmk, PMK_LEN);
4548 os_memcpy(pmkid, pmksa->pmkid, PMKID_LEN);
4549 os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmksa->pmkid, PMKID_LEN);
4550}
4551
4552
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004553/*
4554 * Remove and free the group from wpa_authenticator. This is triggered by a
4555 * callback to make sure nobody is currently iterating the group list while it
4556 * gets modified.
4557 */
4558static void wpa_group_free(struct wpa_authenticator *wpa_auth,
4559 struct wpa_group *group)
4560{
4561 struct wpa_group *prev = wpa_auth->group;
4562
4563 wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d",
4564 group->vlan_id);
4565
4566 while (prev) {
4567 if (prev->next == group) {
4568 /* This never frees the special first group as needed */
4569 prev->next = group->next;
4570 os_free(group);
4571 break;
4572 }
4573 prev = prev->next;
4574 }
4575
4576}
4577
4578
4579/* Increase the reference counter for group */
4580static void wpa_group_get(struct wpa_authenticator *wpa_auth,
4581 struct wpa_group *group)
4582{
4583 /* Skip the special first group */
4584 if (wpa_auth->group == group)
4585 return;
4586
4587 group->references++;
4588}
4589
4590
4591/* Decrease the reference counter and maybe free the group */
4592static void wpa_group_put(struct wpa_authenticator *wpa_auth,
4593 struct wpa_group *group)
4594{
4595 /* Skip the special first group */
4596 if (wpa_auth->group == group)
4597 return;
4598
4599 group->references--;
4600 if (group->references)
4601 return;
4602 wpa_group_free(wpa_auth, group);
4603}
4604
4605
4606/*
4607 * Add a group that has its references counter set to zero. Caller needs to
4608 * call wpa_group_get() on the return value to mark the entry in use.
4609 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004610static struct wpa_group *
4611wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4612{
4613 struct wpa_group *group;
4614
4615 if (wpa_auth == NULL || wpa_auth->group == NULL)
4616 return NULL;
4617
4618 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
4619 vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004620 group = wpa_group_init(wpa_auth, vlan_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004621 if (group == NULL)
4622 return NULL;
4623
4624 group->next = wpa_auth->group->next;
4625 wpa_auth->group->next = group;
4626
4627 return group;
4628}
4629
4630
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004631/*
4632 * Enforce that the group state machine for the VLAN is running, increase
4633 * reference counter as interface is up. References might have been increased
4634 * even if a negative value is returned.
4635 * Returns: -1 on error (group missing, group already failed); otherwise, 0
4636 */
4637int wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4638{
4639 struct wpa_group *group;
4640
4641 if (wpa_auth == NULL)
4642 return 0;
4643
4644 group = wpa_auth->group;
4645 while (group) {
4646 if (group->vlan_id == vlan_id)
4647 break;
4648 group = group->next;
4649 }
4650
4651 if (group == NULL) {
4652 group = wpa_auth_add_group(wpa_auth, vlan_id);
4653 if (group == NULL)
4654 return -1;
4655 }
4656
4657 wpa_printf(MSG_DEBUG,
4658 "WPA: Ensure group state machine running for VLAN ID %d",
4659 vlan_id);
4660
4661 wpa_group_get(wpa_auth, group);
4662 group->num_setup_iface++;
4663
4664 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4665 return -1;
4666
4667 return 0;
4668}
4669
4670
4671/*
4672 * Decrease reference counter, expected to be zero afterwards.
4673 * returns: -1 on error (group not found, group in fail state)
4674 * -2 if wpa_group is still referenced
4675 * 0 else
4676 */
4677int wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4678{
4679 struct wpa_group *group;
4680 int ret = 0;
4681
4682 if (wpa_auth == NULL)
4683 return 0;
4684
4685 group = wpa_auth->group;
4686 while (group) {
4687 if (group->vlan_id == vlan_id)
4688 break;
4689 group = group->next;
4690 }
4691
4692 if (group == NULL)
4693 return -1;
4694
4695 wpa_printf(MSG_DEBUG,
4696 "WPA: Try stopping group state machine for VLAN ID %d",
4697 vlan_id);
4698
4699 if (group->num_setup_iface <= 0) {
4700 wpa_printf(MSG_ERROR,
4701 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
4702 vlan_id);
4703 return -1;
4704 }
4705 group->num_setup_iface--;
4706
4707 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4708 ret = -1;
4709
4710 if (group->references > 1) {
4711 wpa_printf(MSG_DEBUG,
4712 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
4713 vlan_id);
4714 ret = -2;
4715 }
4716
4717 wpa_group_put(wpa_auth, group);
4718
4719 return ret;
4720}
4721
4722
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004723int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
4724{
4725 struct wpa_group *group;
4726
4727 if (sm == NULL || sm->wpa_auth == NULL)
4728 return 0;
4729
4730 group = sm->wpa_auth->group;
4731 while (group) {
4732 if (group->vlan_id == vlan_id)
4733 break;
4734 group = group->next;
4735 }
4736
4737 if (group == NULL) {
4738 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
4739 if (group == NULL)
4740 return -1;
4741 }
4742
4743 if (sm->group == group)
4744 return 0;
4745
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004746 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4747 return -1;
4748
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004749 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
4750 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
4751
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004752 wpa_group_get(sm->wpa_auth, group);
4753 wpa_group_put(sm->wpa_auth, sm->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004754 sm->group = group;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004755
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004756 return 0;
4757}
4758
4759
4760void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
4761 struct wpa_state_machine *sm, int ack)
4762{
4763 if (wpa_auth == NULL || sm == NULL)
4764 return;
4765 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
4766 " ack=%d", MAC2STR(sm->addr), ack);
4767 if (sm->pending_1_of_4_timeout && ack) {
4768 /*
4769 * Some deployed supplicant implementations update their SNonce
4770 * for each EAPOL-Key 2/4 message even within the same 4-way
4771 * handshake and then fail to use the first SNonce when
4772 * deriving the PTK. This results in unsuccessful 4-way
4773 * handshake whenever the relatively short initial timeout is
4774 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
4775 * around this by increasing the timeout now that we know that
4776 * the station has received the frame.
4777 */
4778 int timeout_ms = eapol_key_timeout_subseq;
4779 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
4780 "timeout by %u ms because of acknowledged frame",
4781 timeout_ms);
4782 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
4783 eloop_register_timeout(timeout_ms / 1000,
4784 (timeout_ms % 1000) * 1000,
4785 wpa_send_eapol_timeout, wpa_auth, sm);
4786 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004787
4788#ifdef CONFIG_TESTING_OPTIONS
4789 if (sm->eapol_status_cb) {
4790 sm->eapol_status_cb(sm->eapol_status_cb_ctx1,
4791 sm->eapol_status_cb_ctx2);
4792 sm->eapol_status_cb = NULL;
4793 }
4794#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004795}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004796
4797
4798int wpa_auth_uses_sae(struct wpa_state_machine *sm)
4799{
4800 if (sm == NULL)
4801 return 0;
4802 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
4803}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004804
4805
4806int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
4807{
4808 if (sm == NULL)
4809 return 0;
4810 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
4811}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004812
4813
4814#ifdef CONFIG_P2P
4815int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
4816{
4817 if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
4818 return -1;
4819 os_memcpy(addr, sm->ip_addr, 4);
4820 return 0;
4821}
4822#endif /* CONFIG_P2P */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004823
4824
4825int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
4826 struct radius_das_attrs *attr)
4827{
4828 return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
4829}
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004830
4831
4832void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)
4833{
4834 struct wpa_group *group;
4835
4836 if (!wpa_auth)
4837 return;
4838 for (group = wpa_auth->group; group; group = group->next)
4839 wpa_group_config_group_keys(wpa_auth, group);
4840}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004841
4842
4843#ifdef CONFIG_FILS
4844
4845struct wpa_auth_fils_iter_data {
4846 struct wpa_authenticator *auth;
4847 const u8 *cache_id;
4848 struct rsn_pmksa_cache_entry *pmksa;
4849 const u8 *spa;
4850 const u8 *pmkid;
4851};
4852
4853
4854static int wpa_auth_fils_iter(struct wpa_authenticator *a, void *ctx)
4855{
4856 struct wpa_auth_fils_iter_data *data = ctx;
4857
4858 if (a == data->auth || !a->conf.fils_cache_id_set ||
4859 os_memcmp(a->conf.fils_cache_id, data->cache_id,
4860 FILS_CACHE_ID_LEN) != 0)
4861 return 0;
4862 data->pmksa = pmksa_cache_auth_get(a->pmksa, data->spa, data->pmkid);
4863 return data->pmksa != NULL;
4864}
4865
4866
4867struct rsn_pmksa_cache_entry *
4868wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator *wpa_auth,
4869 const u8 *sta_addr, const u8 *pmkid)
4870{
4871 struct wpa_auth_fils_iter_data idata;
4872
4873 if (!wpa_auth->conf.fils_cache_id_set)
4874 return NULL;
4875 idata.auth = wpa_auth;
4876 idata.cache_id = wpa_auth->conf.fils_cache_id;
4877 idata.pmksa = NULL;
4878 idata.spa = sta_addr;
4879 idata.pmkid = pmkid;
4880 wpa_auth_for_each_auth(wpa_auth, wpa_auth_fils_iter, &idata);
4881 return idata.pmksa;
4882}
4883
4884
4885#ifdef CONFIG_IEEE80211R_AP
Roshan Pius3a1667e2018-07-03 15:17:14 -07004886int wpa_auth_write_fte(struct wpa_authenticator *wpa_auth, int use_sha384,
4887 u8 *buf, size_t len)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004888{
4889 struct wpa_auth_config *conf = &wpa_auth->conf;
4890
Roshan Pius3a1667e2018-07-03 15:17:14 -07004891 return wpa_write_ftie(conf, use_sha384, conf->r0_key_holder,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004892 conf->r0_key_holder_len,
4893 NULL, NULL, buf, len, NULL, 0);
4894}
4895#endif /* CONFIG_IEEE80211R_AP */
4896
4897
4898void wpa_auth_get_fils_aead_params(struct wpa_state_machine *sm,
4899 u8 *fils_anonce, u8 *fils_snonce,
4900 u8 *fils_kek, size_t *fils_kek_len)
4901{
4902 os_memcpy(fils_anonce, sm->ANonce, WPA_NONCE_LEN);
4903 os_memcpy(fils_snonce, sm->SNonce, WPA_NONCE_LEN);
4904 os_memcpy(fils_kek, sm->PTK.kek, WPA_KEK_MAX_LEN);
4905 *fils_kek_len = sm->PTK.kek_len;
4906}
4907
Hai Shalom81f62d82019-07-22 12:10:00 -07004908
4909void wpa_auth_add_fils_pmk_pmkid(struct wpa_state_machine *sm, const u8 *pmk,
4910 size_t pmk_len, const u8 *pmkid)
4911{
4912 os_memcpy(sm->PMK, pmk, pmk_len);
4913 sm->pmk_len = pmk_len;
4914 os_memcpy(sm->pmkid, pmkid, PMKID_LEN);
4915 sm->pmkid_set = 1;
4916}
4917
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004918#endif /* CONFIG_FILS */
4919
4920
Hai Shalom021b0b52019-04-10 11:17:58 -07004921void wpa_auth_set_auth_alg(struct wpa_state_machine *sm, u16 auth_alg)
4922{
4923 if (sm)
4924 sm->auth_alg = auth_alg;
4925}
4926
4927
4928#ifdef CONFIG_DPP2
4929void wpa_auth_set_dpp_z(struct wpa_state_machine *sm, const struct wpabuf *z)
4930{
4931 if (sm) {
4932 wpabuf_clear_free(sm->dpp_z);
4933 sm->dpp_z = z ? wpabuf_dup(z) : NULL;
4934 }
4935}
4936#endif /* CONFIG_DPP2 */
4937
4938
Roshan Pius3a1667e2018-07-03 15:17:14 -07004939#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004940
4941int wpa_auth_resend_m1(struct wpa_state_machine *sm, int change_anonce,
4942 void (*cb)(void *ctx1, void *ctx2),
4943 void *ctx1, void *ctx2)
4944{
4945 const u8 *anonce = sm->ANonce;
4946 u8 anonce_buf[WPA_NONCE_LEN];
4947
4948 if (change_anonce) {
4949 if (random_get_bytes(anonce_buf, WPA_NONCE_LEN))
4950 return -1;
4951 anonce = anonce_buf;
4952 }
4953
4954 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
4955 "sending 1/4 msg of 4-Way Handshake (TESTING)");
4956 wpa_send_eapol(sm->wpa_auth, sm,
4957 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
4958 anonce, NULL, 0, 0, 0);
4959 return 0;
4960}
4961
4962
4963int wpa_auth_resend_m3(struct wpa_state_machine *sm,
4964 void (*cb)(void *ctx1, void *ctx2),
4965 void *ctx1, void *ctx2)
4966{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004967 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004968 u8 *opos;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004969 size_t gtk_len, kde_len;
4970 struct wpa_group *gsm = sm->group;
4971 u8 *wpa_ie;
Hai Shalomc3565922019-10-28 11:58:20 -07004972 int wpa_ie_len, secure, gtkidx, encr = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004973
4974 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
4975 GTK[GN], IGTK, [FTIE], [TIE * 2])
4976 */
4977
4978 /* Use 0 RSC */
4979 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
4980 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
4981 wpa_ie = sm->wpa_auth->wpa_ie;
4982 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
4983 if (sm->wpa == WPA_VERSION_WPA &&
4984 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
4985 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
4986 /* WPA-only STA, remove RSN IE and possible MDIE */
4987 wpa_ie = wpa_ie + wpa_ie[1] + 2;
4988 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
4989 wpa_ie = wpa_ie + wpa_ie[1] + 2;
4990 wpa_ie_len = wpa_ie[1] + 2;
4991 }
4992 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
4993 "sending 3/4 msg of 4-Way Handshake (TESTING)");
4994 if (sm->wpa == WPA_VERSION_WPA2) {
4995 /* WPA2 send GTK in the 4-way handshake */
4996 secure = 1;
4997 gtk = gsm->GTK[gsm->GN - 1];
4998 gtk_len = gsm->GTK_len;
Hai Shalomc3565922019-10-28 11:58:20 -07004999 gtkidx = gsm->GN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005000 _rsc = rsc;
5001 encr = 1;
5002 } else {
5003 /* WPA does not include GTK in msg 3/4 */
5004 secure = 0;
5005 gtk = NULL;
5006 gtk_len = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005007 _rsc = NULL;
5008 if (sm->rx_eapol_key_secure) {
5009 /*
5010 * It looks like Windows 7 supplicant tries to use
5011 * Secure bit in msg 2/4 after having reported Michael
5012 * MIC failure and it then rejects the 4-way handshake
5013 * if msg 3/4 does not set Secure bit. Work around this
5014 * by setting the Secure bit here even in the case of
5015 * WPA if the supplicant used it first.
5016 */
5017 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
5018 "STA used Secure bit in WPA msg 2/4 - "
5019 "set Secure for 3/4 as workaround");
5020 secure = 1;
5021 }
5022 }
5023
Hai Shalom74f70d42019-02-11 14:42:39 -08005024 kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005025 if (gtk)
5026 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
5027#ifdef CONFIG_IEEE80211R_AP
5028 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
5029 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
5030 kde_len += 300; /* FTIE + 2 * TIE */
5031 }
5032#endif /* CONFIG_IEEE80211R_AP */
5033 kde = os_malloc(kde_len);
5034 if (kde == NULL)
5035 return -1;
5036
5037 pos = kde;
5038 os_memcpy(pos, wpa_ie, wpa_ie_len);
5039 pos += wpa_ie_len;
5040#ifdef CONFIG_IEEE80211R_AP
5041 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
5042 int res;
5043 size_t elen;
5044
5045 elen = pos - kde;
5046 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
5047 if (res < 0) {
5048 wpa_printf(MSG_ERROR, "FT: Failed to insert "
5049 "PMKR1Name into RSN IE in EAPOL-Key data");
5050 os_free(kde);
5051 return -1;
5052 }
5053 pos -= wpa_ie_len;
5054 pos += elen;
5055 }
5056#endif /* CONFIG_IEEE80211R_AP */
5057 if (gtk) {
5058 u8 hdr[2];
Hai Shalomc3565922019-10-28 11:58:20 -07005059 hdr[0] = gtkidx & 0x03;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005060 hdr[1] = 0;
5061 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
5062 gtk, gtk_len);
5063 }
5064 opos = pos;
5065 pos = ieee80211w_kde_add(sm, pos);
Roshan Pius3a1667e2018-07-03 15:17:14 -07005066 if (pos - opos >= 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
5067 /* skip KDE header and keyid */
5068 opos += 2 + RSN_SELECTOR_LEN + 2;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005069 os_memset(opos, 0, 6); /* clear PN */
5070 }
Hai Shalom74f70d42019-02-11 14:42:39 -08005071 if (ocv_oci_add(sm, &pos) < 0) {
5072 os_free(kde);
5073 return -1;
5074 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005075
5076#ifdef CONFIG_IEEE80211R_AP
5077 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
5078 int res;
5079 struct wpa_auth_config *conf;
5080
5081 conf = &sm->wpa_auth->conf;
5082 if (sm->assoc_resp_ftie &&
5083 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
5084 os_memcpy(pos, sm->assoc_resp_ftie,
5085 2 + sm->assoc_resp_ftie[1]);
5086 res = 2 + sm->assoc_resp_ftie[1];
5087 } else {
Roshan Pius3a1667e2018-07-03 15:17:14 -07005088 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
5089
5090 res = wpa_write_ftie(conf, use_sha384,
5091 conf->r0_key_holder,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005092 conf->r0_key_holder_len,
5093 NULL, NULL, pos,
5094 kde + kde_len - pos,
5095 NULL, 0);
5096 }
5097 if (res < 0) {
5098 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
5099 "into EAPOL-Key Key Data");
5100 os_free(kde);
5101 return -1;
5102 }
5103 pos += res;
5104
5105 /* TIE[ReassociationDeadline] (TU) */
5106 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
5107 *pos++ = 5;
5108 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
5109 WPA_PUT_LE32(pos, conf->reassociation_deadline);
5110 pos += 4;
5111
5112 /* TIE[KeyLifetime] (seconds) */
5113 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
5114 *pos++ = 5;
5115 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005116 WPA_PUT_LE32(pos, conf->r0_key_lifetime);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005117 pos += 4;
5118 }
5119#endif /* CONFIG_IEEE80211R_AP */
5120
5121 wpa_send_eapol(sm->wpa_auth, sm,
5122 (secure ? WPA_KEY_INFO_SECURE : 0) |
5123 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
5124 WPA_KEY_INFO_MIC : 0) |
5125 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
5126 WPA_KEY_INFO_KEY_TYPE,
Hai Shalomc3565922019-10-28 11:58:20 -07005127 _rsc, sm->ANonce, kde, pos - kde, 0, encr);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005128 os_free(kde);
5129 return 0;
5130}
5131
5132
5133int wpa_auth_resend_group_m1(struct wpa_state_machine *sm,
5134 void (*cb)(void *ctx1, void *ctx2),
5135 void *ctx1, void *ctx2)
5136{
5137 u8 rsc[WPA_KEY_RSC_LEN];
5138 struct wpa_group *gsm = sm->group;
5139 const u8 *kde;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005140 u8 *kde_buf = NULL, *pos, hdr[2];
Roshan Pius3a1667e2018-07-03 15:17:14 -07005141 u8 *opos;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005142 size_t kde_len;
5143 u8 *gtk;
5144
5145 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
5146 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
5147 /* Use 0 RSC */
5148 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
5149 "sending 1/2 msg of Group Key Handshake (TESTING)");
5150
5151 gtk = gsm->GTK[gsm->GN - 1];
5152 if (sm->wpa == WPA_VERSION_WPA2) {
5153 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
Hai Shalom74f70d42019-02-11 14:42:39 -08005154 ieee80211w_kde_len(sm) + ocv_oci_len(sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005155 kde_buf = os_malloc(kde_len);
5156 if (kde_buf == NULL)
5157 return -1;
5158
5159 kde = pos = kde_buf;
5160 hdr[0] = gsm->GN & 0x03;
5161 hdr[1] = 0;
5162 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
5163 gtk, gsm->GTK_len);
5164 opos = pos;
5165 pos = ieee80211w_kde_add(sm, pos);
Roshan Pius3a1667e2018-07-03 15:17:14 -07005166 if (pos - opos >=
5167 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
5168 /* skip KDE header and keyid */
5169 opos += 2 + RSN_SELECTOR_LEN + 2;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005170 os_memset(opos, 0, 6); /* clear PN */
5171 }
Hai Shalom74f70d42019-02-11 14:42:39 -08005172 if (ocv_oci_add(sm, &pos) < 0) {
5173 os_free(kde_buf);
5174 return -1;
5175 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005176 kde_len = pos - kde;
5177 } else {
5178 kde = gtk;
5179 kde_len = gsm->GTK_len;
5180 }
5181
5182 sm->eapol_status_cb = cb;
5183 sm->eapol_status_cb_ctx1 = ctx1;
5184 sm->eapol_status_cb_ctx2 = ctx2;
5185
5186 wpa_send_eapol(sm->wpa_auth, sm,
5187 WPA_KEY_INFO_SECURE |
5188 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
5189 WPA_KEY_INFO_MIC : 0) |
5190 WPA_KEY_INFO_ACK |
5191 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
5192 rsc, NULL, kde, kde_len, gsm->GN, 1);
5193
5194 os_free(kde_buf);
5195 return 0;
5196}
5197
Roshan Pius3a1667e2018-07-03 15:17:14 -07005198
5199int wpa_auth_rekey_gtk(struct wpa_authenticator *wpa_auth)
5200{
5201 if (!wpa_auth)
5202 return -1;
5203 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
5204 return eloop_register_timeout(0, 0, wpa_rekey_gtk, wpa_auth, NULL);
5205}
5206
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005207#endif /* CONFIG_TESTING_OPTIONS */