blob: 8e2b48e2d06177105f197ad418469bfb6223748f [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002 * IEEE 802.11 RSN / WPA Authenticator
Roshan Pius3a1667e2018-07-03 15:17:14 -07003 * Copyright (c) 2004-2018, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "utils/state_machine.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080014#include "utils/bitfield.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "common/ieee802_11_defs.h"
Hai Shalom39bc25d2019-02-06 16:32:13 -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 Shalom39bc25d2019-02-06 16:32:13 -080026#include "drivers/driver.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "ap_config.h"
28#include "ieee802_11.h"
29#include "wpa_auth.h"
30#include "pmksa_cache_auth.h"
31#include "wpa_auth_i.h"
32#include "wpa_auth_ie.h"
33
34#define STATE_MACHINE_DATA struct wpa_state_machine
35#define STATE_MACHINE_DEBUG_PREFIX "WPA"
36#define STATE_MACHINE_ADDR sm->addr
37
38
39static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
40static int wpa_sm_step(struct wpa_state_machine *sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070041static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK,
42 u8 *data, size_t data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080043#ifdef CONFIG_FILS
44static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
45 u8 *buf, size_t buf_len, u16 *_key_data_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070046static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm,
47 const struct wpabuf *hlp);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080048#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
50static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
51 struct wpa_group *group);
52static void wpa_request_new_ptk(struct wpa_state_machine *sm);
53static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
54 struct wpa_group *group);
55static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
56 struct wpa_group *group);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080057static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080058 const u8 *pmk, unsigned int pmk_len,
59 struct wpa_ptk *ptk);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070060static void wpa_group_free(struct wpa_authenticator *wpa_auth,
61 struct wpa_group *group);
62static void wpa_group_get(struct wpa_authenticator *wpa_auth,
63 struct wpa_group *group);
64static void wpa_group_put(struct wpa_authenticator *wpa_auth,
65 struct wpa_group *group);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080066static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070067
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070068static const u32 eapol_key_timeout_first = 100; /* ms */
69static const u32 eapol_key_timeout_subseq = 1000; /* ms */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080070static const u32 eapol_key_timeout_first_group = 500; /* ms */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070071static const u32 eapol_key_timeout_no_retrans = 4000; /* ms */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072
73/* TODO: make these configurable */
74static const int dot11RSNAConfigPMKLifetime = 43200;
75static const int dot11RSNAConfigPMKReauthThreshold = 70;
76static const int dot11RSNAConfigSATimeout = 60;
77
78
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080079static inline int wpa_auth_mic_failure_report(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070080 struct wpa_authenticator *wpa_auth, const u8 *addr)
81{
Paul Stewart092955c2017-02-06 09:13:09 -080082 if (wpa_auth->cb->mic_failure_report)
83 return wpa_auth->cb->mic_failure_report(wpa_auth->cb_ctx, addr);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080084 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070085}
86
87
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070088static inline void wpa_auth_psk_failure_report(
89 struct wpa_authenticator *wpa_auth, const u8 *addr)
90{
Paul Stewart092955c2017-02-06 09:13:09 -080091 if (wpa_auth->cb->psk_failure_report)
92 wpa_auth->cb->psk_failure_report(wpa_auth->cb_ctx, addr);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070093}
94
95
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070096static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
97 const u8 *addr, wpa_eapol_variable var,
98 int value)
99{
Paul Stewart092955c2017-02-06 09:13:09 -0800100 if (wpa_auth->cb->set_eapol)
101 wpa_auth->cb->set_eapol(wpa_auth->cb_ctx, addr, var, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102}
103
104
105static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
106 const u8 *addr, wpa_eapol_variable var)
107{
Paul Stewart092955c2017-02-06 09:13:09 -0800108 if (wpa_auth->cb->get_eapol == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800110 return wpa_auth->cb->get_eapol(wpa_auth->cb_ctx, addr, var);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111}
112
113
114static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700115 const u8 *addr,
116 const u8 *p2p_dev_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700117 const u8 *prev_psk, size_t *psk_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700118{
Paul Stewart092955c2017-02-06 09:13:09 -0800119 if (wpa_auth->cb->get_psk == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700120 return NULL;
Paul Stewart092955c2017-02-06 09:13:09 -0800121 return wpa_auth->cb->get_psk(wpa_auth->cb_ctx, addr, p2p_dev_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700122 prev_psk, psk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700123}
124
125
126static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
127 const u8 *addr, u8 *msk, size_t *len)
128{
Paul Stewart092955c2017-02-06 09:13:09 -0800129 if (wpa_auth->cb->get_msk == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800131 return wpa_auth->cb->get_msk(wpa_auth->cb_ctx, addr, msk, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700132}
133
134
135static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
136 int vlan_id,
137 enum wpa_alg alg, const u8 *addr, int idx,
138 u8 *key, size_t key_len)
139{
Paul Stewart092955c2017-02-06 09:13:09 -0800140 if (wpa_auth->cb->set_key == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800142 return wpa_auth->cb->set_key(wpa_auth->cb_ctx, vlan_id, alg, addr, idx,
143 key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700144}
145
146
147static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
148 const u8 *addr, int idx, u8 *seq)
149{
Paul Stewart092955c2017-02-06 09:13:09 -0800150 if (wpa_auth->cb->get_seqnum == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700151 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800152 return wpa_auth->cb->get_seqnum(wpa_auth->cb_ctx, addr, idx, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153}
154
155
156static inline int
157wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
158 const u8 *data, size_t data_len, int encrypt)
159{
Paul Stewart092955c2017-02-06 09:13:09 -0800160 if (wpa_auth->cb->send_eapol == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800162 return wpa_auth->cb->send_eapol(wpa_auth->cb_ctx, addr, data, data_len,
163 encrypt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700164}
165
166
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800167#ifdef CONFIG_MESH
168static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth,
169 const u8 *addr)
170{
Paul Stewart092955c2017-02-06 09:13:09 -0800171 if (wpa_auth->cb->start_ampe == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800172 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800173 return wpa_auth->cb->start_ampe(wpa_auth->cb_ctx, addr);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800174}
175#endif /* CONFIG_MESH */
176
177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700178int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
179 int (*cb)(struct wpa_state_machine *sm, void *ctx),
180 void *cb_ctx)
181{
Paul Stewart092955c2017-02-06 09:13:09 -0800182 if (wpa_auth->cb->for_each_sta == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183 return 0;
Paul Stewart092955c2017-02-06 09:13:09 -0800184 return wpa_auth->cb->for_each_sta(wpa_auth->cb_ctx, cb, cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700185}
186
187
188int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
189 int (*cb)(struct wpa_authenticator *a, void *ctx),
190 void *cb_ctx)
191{
Paul Stewart092955c2017-02-06 09:13:09 -0800192 if (wpa_auth->cb->for_each_auth == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700193 return 0;
Paul Stewart092955c2017-02-06 09:13:09 -0800194 return wpa_auth->cb->for_each_auth(wpa_auth->cb_ctx, cb, cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700195}
196
197
198void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
199 logger_level level, const char *txt)
200{
Paul Stewart092955c2017-02-06 09:13:09 -0800201 if (wpa_auth->cb->logger == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202 return;
Paul Stewart092955c2017-02-06 09:13:09 -0800203 wpa_auth->cb->logger(wpa_auth->cb_ctx, addr, level, txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204}
205
206
207void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
208 logger_level level, const char *fmt, ...)
209{
210 char *format;
211 int maxlen;
212 va_list ap;
213
Paul Stewart092955c2017-02-06 09:13:09 -0800214 if (wpa_auth->cb->logger == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 return;
216
217 maxlen = os_strlen(fmt) + 100;
218 format = os_malloc(maxlen);
219 if (!format)
220 return;
221
222 va_start(ap, fmt);
223 vsnprintf(format, maxlen, fmt, ap);
224 va_end(ap);
225
226 wpa_auth_logger(wpa_auth, addr, level, format);
227
228 os_free(format);
229}
230
231
232static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700233 const u8 *addr, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234{
Paul Stewart092955c2017-02-06 09:13:09 -0800235 if (wpa_auth->cb->disconnect == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236 return;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700237 wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR " (reason %u)",
238 MAC2STR(addr), reason);
239 wpa_auth->cb->disconnect(wpa_auth->cb_ctx, addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240}
241
242
Hai Shalom39bc25d2019-02-06 16:32:13 -0800243#ifdef CONFIG_OCV
244static int wpa_channel_info(struct wpa_authenticator *wpa_auth,
245 struct wpa_channel_info *ci)
246{
247 if (!wpa_auth->cb->channel_info)
248 return -1;
249 return wpa_auth->cb->channel_info(wpa_auth->cb_ctx, ci);
250}
251#endif /* CONFIG_OCV */
252
253
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
255{
256 struct wpa_authenticator *wpa_auth = eloop_ctx;
257
258 if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
259 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
260 "initialization.");
261 } else {
262 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
263 wpa_hexdump_key(MSG_DEBUG, "GMK",
264 wpa_auth->group->GMK, WPA_GMK_LEN);
265 }
266
267 if (wpa_auth->conf.wpa_gmk_rekey) {
268 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
269 wpa_rekey_gmk, wpa_auth, NULL);
270 }
271}
272
273
274static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
275{
276 struct wpa_authenticator *wpa_auth = eloop_ctx;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700277 struct wpa_group *group, *next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700278
279 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700280 group = wpa_auth->group;
281 while (group) {
282 wpa_group_get(wpa_auth, group);
283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 group->GTKReKey = TRUE;
285 do {
286 group->changed = FALSE;
287 wpa_group_sm_step(wpa_auth, group);
288 } while (group->changed);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700289
290 next = group->next;
291 wpa_group_put(wpa_auth, group);
292 group = next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 }
294
295 if (wpa_auth->conf.wpa_group_rekey) {
296 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
297 0, wpa_rekey_gtk, wpa_auth, NULL);
298 }
299}
300
301
302static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
303{
304 struct wpa_authenticator *wpa_auth = eloop_ctx;
305 struct wpa_state_machine *sm = timeout_ctx;
306
307 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
308 wpa_request_new_ptk(sm);
309 wpa_sm_step(sm);
310}
311
312
313static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
314{
315 if (sm->pmksa == ctx)
316 sm->pmksa = NULL;
317 return 0;
318}
319
320
321static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
322 void *ctx)
323{
324 struct wpa_authenticator *wpa_auth = ctx;
325 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
326}
327
328
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
330 struct wpa_group *group)
331{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800332 u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333 u8 rkey[32];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800334 unsigned long ptr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335
336 if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
337 return -1;
338 wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
339
340 /*
341 * Counter = PRF-256(Random number, "Init Counter",
342 * Local MAC Address || Time)
343 */
344 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
345 wpa_get_ntp_timestamp(buf + ETH_ALEN);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800346 ptr = (unsigned long) group;
347 os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348 if (random_get_bytes(rkey, sizeof(rkey)) < 0)
349 return -1;
350
351 if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
352 group->Counter, WPA_NONCE_LEN) < 0)
353 return -1;
354 wpa_hexdump_key(MSG_DEBUG, "Key Counter",
355 group->Counter, WPA_NONCE_LEN);
356
357 return 0;
358}
359
360
361static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800362 int vlan_id, int delay_init)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363{
364 struct wpa_group *group;
365
366 group = os_zalloc(sizeof(struct wpa_group));
367 if (group == NULL)
368 return NULL;
369
370 group->GTKAuthenticator = TRUE;
371 group->vlan_id = vlan_id;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700372 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700373
374 if (random_pool_ready() != 1) {
375 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
376 "for secure operations - update keys later when "
377 "the first station connects");
378 }
379
380 /*
381 * Set initial GMK/Counter value here. The actual values that will be
382 * used in negotiations will be set once the first station tries to
383 * connect. This allows more time for collecting additional randomness
384 * on embedded devices.
385 */
386 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
387 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
388 "initialization.");
389 os_free(group);
390 return NULL;
391 }
392
393 group->GInit = TRUE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800394 if (delay_init) {
395 wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
396 "until Beacon frames have been configured");
397 /* Initialization is completed in wpa_init_keys(). */
398 } else {
399 wpa_group_sm_step(wpa_auth, group);
400 group->GInit = FALSE;
401 wpa_group_sm_step(wpa_auth, group);
402 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700403
404 return group;
405}
406
407
408/**
409 * wpa_init - Initialize WPA authenticator
410 * @addr: Authenticator address
411 * @conf: Configuration for WPA authenticator
412 * @cb: Callback functions for WPA authenticator
413 * Returns: Pointer to WPA authenticator data or %NULL on failure
414 */
415struct wpa_authenticator * wpa_init(const u8 *addr,
416 struct wpa_auth_config *conf,
Paul Stewart092955c2017-02-06 09:13:09 -0800417 const struct wpa_auth_callbacks *cb,
418 void *cb_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700419{
420 struct wpa_authenticator *wpa_auth;
421
422 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
423 if (wpa_auth == NULL)
424 return NULL;
425 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
426 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
Paul Stewart092955c2017-02-06 09:13:09 -0800427 wpa_auth->cb = cb;
428 wpa_auth->cb_ctx = cb_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429
430 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
431 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
432 os_free(wpa_auth);
433 return NULL;
434 }
435
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800436 wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437 if (wpa_auth->group == NULL) {
438 os_free(wpa_auth->wpa_ie);
439 os_free(wpa_auth);
440 return NULL;
441 }
442
443 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
444 wpa_auth);
445 if (wpa_auth->pmksa == NULL) {
446 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800447 os_free(wpa_auth->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700448 os_free(wpa_auth->wpa_ie);
449 os_free(wpa_auth);
450 return NULL;
451 }
452
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800453#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700454 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
455 if (wpa_auth->ft_pmk_cache == NULL) {
456 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800457 os_free(wpa_auth->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458 os_free(wpa_auth->wpa_ie);
459 pmksa_cache_auth_deinit(wpa_auth->pmksa);
460 os_free(wpa_auth);
461 return NULL;
462 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800463#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464
465 if (wpa_auth->conf.wpa_gmk_rekey) {
466 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
467 wpa_rekey_gmk, wpa_auth, NULL);
468 }
469
470 if (wpa_auth->conf.wpa_group_rekey) {
471 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
472 wpa_rekey_gtk, wpa_auth, NULL);
473 }
474
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800475#ifdef CONFIG_P2P
476 if (WPA_GET_BE32(conf->ip_addr_start)) {
477 int count = WPA_GET_BE32(conf->ip_addr_end) -
478 WPA_GET_BE32(conf->ip_addr_start) + 1;
479 if (count > 1000)
480 count = 1000;
481 if (count > 0)
482 wpa_auth->ip_pool = bitfield_alloc(count);
483 }
484#endif /* CONFIG_P2P */
485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700486 return wpa_auth;
487}
488
489
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800490int wpa_init_keys(struct wpa_authenticator *wpa_auth)
491{
492 struct wpa_group *group = wpa_auth->group;
493
494 wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
495 "keys");
496 wpa_group_sm_step(wpa_auth, group);
497 group->GInit = FALSE;
498 wpa_group_sm_step(wpa_auth, group);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800499 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
500 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800501 return 0;
502}
503
504
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700505/**
506 * wpa_deinit - Deinitialize WPA authenticator
507 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
508 */
509void wpa_deinit(struct wpa_authenticator *wpa_auth)
510{
511 struct wpa_group *group, *prev;
512
513 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
514 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
515
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700516 pmksa_cache_auth_deinit(wpa_auth->pmksa);
517
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800518#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700519 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
520 wpa_auth->ft_pmk_cache = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700521 wpa_ft_deinit(wpa_auth);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800522#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800524#ifdef CONFIG_P2P
525 bitfield_free(wpa_auth->ip_pool);
526#endif /* CONFIG_P2P */
527
528
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700529 os_free(wpa_auth->wpa_ie);
530
531 group = wpa_auth->group;
532 while (group) {
533 prev = group;
534 group = group->next;
535 os_free(prev);
536 }
537
538 os_free(wpa_auth);
539}
540
541
542/**
543 * wpa_reconfig - Update WPA authenticator configuration
544 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
545 * @conf: Configuration for WPA authenticator
546 */
547int wpa_reconfig(struct wpa_authenticator *wpa_auth,
548 struct wpa_auth_config *conf)
549{
550 struct wpa_group *group;
551 if (wpa_auth == NULL)
552 return 0;
553
554 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
555 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
556 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
557 return -1;
558 }
559
560 /*
561 * Reinitialize GTK to make sure it is suitable for the new
562 * configuration.
563 */
564 group = wpa_auth->group;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700565 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700566 group->GInit = TRUE;
567 wpa_group_sm_step(wpa_auth, group);
568 group->GInit = FALSE;
569 wpa_group_sm_step(wpa_auth, group);
570
571 return 0;
572}
573
574
575struct wpa_state_machine *
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700576wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
577 const u8 *p2p_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700578{
579 struct wpa_state_machine *sm;
580
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800581 if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
582 return NULL;
583
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584 sm = os_zalloc(sizeof(struct wpa_state_machine));
585 if (sm == NULL)
586 return NULL;
587 os_memcpy(sm->addr, addr, ETH_ALEN);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700588 if (p2p_dev_addr)
589 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590
591 sm->wpa_auth = wpa_auth;
592 sm->group = wpa_auth->group;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700593 wpa_group_get(sm->wpa_auth, sm->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594
595 return sm;
596}
597
598
599int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
600 struct wpa_state_machine *sm)
601{
602 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
603 return -1;
604
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800605#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606 if (sm->ft_completed) {
607 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
608 "FT authentication already completed - do not "
609 "start 4-way handshake");
Dmitry Shmidt71757432014-06-02 13:50:35 -0700610 /* Go to PTKINITDONE state to allow GTK rekeying */
611 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800612 sm->Pair = TRUE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700613 return 0;
614 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800615#endif /* CONFIG_IEEE80211R_AP */
616
617#ifdef CONFIG_FILS
618 if (sm->fils_completed) {
619 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
620 "FILS authentication already completed - do not start 4-way handshake");
621 /* Go to PTKINITDONE state to allow GTK rekeying */
622 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800623 sm->Pair = TRUE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800624 return 0;
625 }
626#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700627
628 if (sm->started) {
629 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
630 sm->ReAuthenticationRequest = TRUE;
631 return wpa_sm_step(sm);
632 }
633
634 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
635 "start authentication");
636 sm->started = 1;
637
638 sm->Init = TRUE;
639 if (wpa_sm_step(sm) == 1)
640 return 1; /* should not really happen */
641 sm->Init = FALSE;
642 sm->AuthenticationRequest = TRUE;
643 return wpa_sm_step(sm);
644}
645
646
647void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
648{
649 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
650 * reassociates back to the same AP while the previous entry for the
651 * STA has not yet been removed. */
652 if (sm == NULL)
653 return;
654
655 sm->wpa_key_mgmt = 0;
656}
657
658
659static void wpa_free_sta_sm(struct wpa_state_machine *sm)
660{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800661#ifdef CONFIG_P2P
662 if (WPA_GET_BE32(sm->ip_addr)) {
663 u32 start;
664 wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
665 "address %u.%u.%u.%u from " MACSTR,
666 sm->ip_addr[0], sm->ip_addr[1],
667 sm->ip_addr[2], sm->ip_addr[3],
668 MAC2STR(sm->addr));
669 start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
670 bitfield_clear(sm->wpa_auth->ip_pool,
671 WPA_GET_BE32(sm->ip_addr) - start);
672 }
673#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700674 if (sm->GUpdateStationKeys) {
675 sm->group->GKeyDoneStations--;
676 sm->GUpdateStationKeys = FALSE;
677 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800678#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700679 os_free(sm->assoc_resp_ftie);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700680 wpabuf_free(sm->ft_pending_req_ies);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800681#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700682 os_free(sm->last_rx_eapol_key);
683 os_free(sm->wpa_ie);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700684 wpa_group_put(sm->wpa_auth, sm->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685 os_free(sm);
686}
687
688
689void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
690{
691 if (sm == NULL)
692 return;
693
694 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
695 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
696 "strict rekeying - force GTK rekey since STA "
697 "is leaving");
Roshan Pius3a1667e2018-07-03 15:17:14 -0700698 if (eloop_deplete_timeout(0, 500000, wpa_rekey_gtk,
699 sm->wpa_auth, NULL) == -1)
700 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
701 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700702 }
703
704 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
705 sm->pending_1_of_4_timeout = 0;
706 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
707 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700708#ifdef CONFIG_IEEE80211R_AP
709 wpa_ft_sta_deinit(sm);
710#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711 if (sm->in_step_loop) {
712 /* Must not free state machine while wpa_sm_step() is running.
713 * Freeing will be completed in the end of wpa_sm_step(). */
714 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
715 "machine deinit for " MACSTR, MAC2STR(sm->addr));
716 sm->pending_deinit = 1;
717 } else
718 wpa_free_sta_sm(sm);
719}
720
721
722static void wpa_request_new_ptk(struct wpa_state_machine *sm)
723{
724 if (sm == NULL)
725 return;
726
727 sm->PTKRequest = TRUE;
728 sm->PTK_valid = 0;
729}
730
731
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800732static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733 const u8 *replay_counter)
734{
735 int i;
736 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800737 if (!ctr[i].valid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800739 if (os_memcmp(replay_counter, ctr[i].counter,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700740 WPA_REPLAY_COUNTER_LEN) == 0)
741 return 1;
742 }
743 return 0;
744}
745
746
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800747static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
748 const u8 *replay_counter)
749{
750 int i;
751 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
752 if (ctr[i].valid &&
753 (replay_counter == NULL ||
754 os_memcmp(replay_counter, ctr[i].counter,
755 WPA_REPLAY_COUNTER_LEN) == 0))
756 ctr[i].valid = FALSE;
757 }
758}
759
760
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800761#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
763 struct wpa_state_machine *sm,
764 struct wpa_eapol_ie_parse *kde)
765{
766 struct wpa_ie_data ie;
767 struct rsn_mdie *mdie;
768
769 if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
770 ie.num_pmkid != 1 || ie.pmkid == NULL) {
771 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
772 "FT 4-way handshake message 2/4");
773 return -1;
774 }
775
776 os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
777 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
778 sm->sup_pmk_r1_name, PMKID_LEN);
779
780 if (!kde->mdie || !kde->ftie) {
781 wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
782 "message 2/4", kde->mdie ? "FTIE" : "MDIE");
783 return -1;
784 }
785
786 mdie = (struct rsn_mdie *) (kde->mdie + 2);
787 if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
788 os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
789 MOBILITY_DOMAIN_ID_LEN) != 0) {
790 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
791 return -1;
792 }
793
794 if (sm->assoc_resp_ftie &&
795 (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
796 os_memcmp(kde->ftie, sm->assoc_resp_ftie,
797 2 + sm->assoc_resp_ftie[1]) != 0)) {
798 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
799 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
800 kde->ftie, kde->ftie_len);
801 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
802 sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
803 return -1;
804 }
805
806 return 0;
807}
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800808#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700809
810
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800811static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
812 struct wpa_state_machine *sm, int group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800813{
814 /* Supplicant reported a Michael MIC error */
815 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
816 "received EAPOL-Key Error Request "
817 "(STA detected Michael MIC failure (group=%d))",
818 group);
819
820 if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
821 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
822 "ignore Michael MIC failure report since "
823 "group cipher is not TKIP");
824 } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
825 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
826 "ignore Michael MIC failure report since "
827 "pairwise cipher is not TKIP");
828 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800829 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
830 return 1; /* STA entry was removed */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800831 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
832 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
833 }
834
835 /*
836 * Error report is not a request for a new key handshake, but since
837 * Authenticator may do it, let's change the keys now anyway.
838 */
839 wpa_request_new_ptk(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800840 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800841}
842
843
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800844static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
845 size_t data_len)
846{
847 struct wpa_ptk PTK;
848 int ok = 0;
849 const u8 *pmk = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700850 size_t pmk_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800851
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800852 os_memset(&PTK, 0, sizeof(PTK));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800853 for (;;) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700854 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
855 !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800856 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700857 sm->p2p_dev_addr, pmk, &pmk_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800858 if (pmk == NULL)
859 break;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700860#ifdef CONFIG_IEEE80211R_AP
861 if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
862 os_memcpy(sm->xxkey, pmk, pmk_len);
863 sm->xxkey_len = pmk_len;
864 }
865#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800866 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800867 pmk = sm->PMK;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800868 pmk_len = sm->pmk_len;
869 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800870
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800871 if (wpa_derive_ptk(sm, sm->alt_SNonce, pmk, pmk_len, &PTK) < 0)
872 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800873
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700874 if (wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK,
875 data, data_len) == 0) {
Hai Shalom39bc25d2019-02-06 16:32:13 -0800876 os_memcpy(sm->PMK, pmk, pmk_len);
877 sm->pmk_len = pmk_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800878 ok = 1;
879 break;
880 }
881
Roshan Pius3a1667e2018-07-03 15:17:14 -0700882 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
883 wpa_key_mgmt_sae(sm->wpa_key_mgmt))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800884 break;
885 }
886
887 if (!ok) {
888 wpa_printf(MSG_DEBUG,
889 "WPA: Earlier SNonce did not result in matching MIC");
890 return -1;
891 }
892
893 wpa_printf(MSG_DEBUG,
894 "WPA: Earlier SNonce resulted in matching MIC");
895 sm->alt_snonce_valid = 0;
896 os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
897 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
898 sm->PTK_valid = TRUE;
899
900 return 0;
901}
902
903
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904void wpa_receive(struct wpa_authenticator *wpa_auth,
905 struct wpa_state_machine *sm,
906 u8 *data, size_t data_len)
907{
908 struct ieee802_1x_hdr *hdr;
909 struct wpa_eapol_key *key;
910 u16 key_info, key_data_length;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700911 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST } msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 char *msgtxt;
913 struct wpa_eapol_ie_parse kde;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800914 const u8 *key_data;
915 size_t keyhdrlen, mic_len;
916 u8 *mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917
918 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
919 return;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800920 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL data", data, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700922 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800923 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800924
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800925 if (data_len < sizeof(*hdr) + keyhdrlen) {
926 wpa_printf(MSG_DEBUG, "WPA: Ignore too short EAPOL-Key frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700927 return;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800928 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929
930 hdr = (struct ieee802_1x_hdr *) data;
931 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800932 mic = (u8 *) (key + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700933 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800934 key_data = mic + mic_len + 2;
935 key_data_length = WPA_GET_BE16(mic + mic_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800936 wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800937 " key_info=0x%x type=%u mic_len=%u key_data_length=%u",
938 MAC2STR(sm->addr), key_info, key->type,
939 (unsigned int) mic_len, key_data_length);
940 wpa_hexdump(MSG_MSGDUMP,
941 "WPA: EAPOL-Key header (ending before Key MIC)",
942 key, sizeof(*key));
943 wpa_hexdump(MSG_MSGDUMP, "WPA: EAPOL-Key Key MIC",
944 mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800945 if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700946 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
947 "key_data overflow (%d > %lu)",
948 key_data_length,
949 (unsigned long) (data_len - sizeof(*hdr) -
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800950 keyhdrlen));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951 return;
952 }
953
954 if (sm->wpa == WPA_VERSION_WPA2) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800955 if (key->type == EAPOL_KEY_TYPE_WPA) {
956 /*
957 * Some deployed station implementations seem to send
958 * msg 4/4 with incorrect type value in WPA2 mode.
959 */
960 wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
961 "with unexpected WPA type in RSN mode");
962 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
964 "unexpected type %d in RSN mode",
965 key->type);
966 return;
967 }
968 } else {
969 if (key->type != EAPOL_KEY_TYPE_WPA) {
970 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
971 "unexpected type %d in WPA mode",
972 key->type);
973 return;
974 }
975 }
976
977 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
978 WPA_NONCE_LEN);
979 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
980 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
981
982 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
983 * are set */
984
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700985 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
986 wpa_printf(MSG_DEBUG, "WPA: Ignore SMK message");
987 return;
988 }
989
990 if (key_info & WPA_KEY_INFO_REQUEST) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700991 msg = REQUEST;
992 msgtxt = "Request";
993 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
994 msg = GROUP_2;
995 msgtxt = "2/2 Group";
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800996 } else if (key_data_length == 0 ||
997 (mic_len == 0 && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) &&
998 key_data_length == AES_BLOCK_SIZE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700999 msg = PAIRWISE_4;
1000 msgtxt = "4/4 Pairwise";
1001 } else {
1002 msg = PAIRWISE_2;
1003 msgtxt = "2/4 Pairwise";
1004 }
1005
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
1007 msg == GROUP_2) {
1008 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001009 if (sm->pairwise == WPA_CIPHER_CCMP ||
1010 sm->pairwise == WPA_CIPHER_GCMP) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001011 if (wpa_use_cmac(sm->wpa_key_mgmt) &&
1012 !wpa_use_akm_defined(sm->wpa_key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1014 wpa_auth_logger(wpa_auth, sm->addr,
1015 LOGGER_WARNING,
1016 "advertised support for "
1017 "AES-128-CMAC, but did not "
1018 "use it");
1019 return;
1020 }
1021
Roshan Pius3a1667e2018-07-03 15:17:14 -07001022 if (!wpa_use_cmac(sm->wpa_key_mgmt) &&
1023 !wpa_use_akm_defined(sm->wpa_key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1025 wpa_auth_logger(wpa_auth, sm->addr,
1026 LOGGER_WARNING,
1027 "did not use HMAC-SHA1-AES "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001028 "with CCMP/GCMP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001029 return;
1030 }
1031 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001032
Roshan Pius3a1667e2018-07-03 15:17:14 -07001033 if (wpa_use_akm_defined(sm->wpa_key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001034 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1035 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1036 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1037 return;
1038 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039 }
1040
1041 if (key_info & WPA_KEY_INFO_REQUEST) {
1042 if (sm->req_replay_counter_used &&
1043 os_memcmp(key->replay_counter, sm->req_replay_counter,
1044 WPA_REPLAY_COUNTER_LEN) <= 0) {
1045 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1046 "received EAPOL-Key request with "
1047 "replayed counter");
1048 return;
1049 }
1050 }
1051
1052 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001053 !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054 int i;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001055
1056 if (msg == PAIRWISE_2 &&
1057 wpa_replay_counter_valid(sm->prev_key_replay,
1058 key->replay_counter) &&
1059 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1060 os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
1061 {
1062 /*
1063 * Some supplicant implementations (e.g., Windows XP
1064 * WZC) update SNonce for each EAPOL-Key 2/4. This
1065 * breaks the workaround on accepting any of the
1066 * pending requests, so allow the SNonce to be updated
1067 * even if we have already sent out EAPOL-Key 3/4.
1068 */
1069 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1070 "Process SNonce update from STA "
1071 "based on retransmitted EAPOL-Key "
1072 "1/4");
1073 sm->update_snonce = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001074 os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
1075 sm->alt_snonce_valid = TRUE;
1076 os_memcpy(sm->alt_replay_counter,
1077 sm->key_replay[0].counter,
1078 WPA_REPLAY_COUNTER_LEN);
1079 goto continue_processing;
1080 }
1081
1082 if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
1083 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1084 os_memcmp(key->replay_counter, sm->alt_replay_counter,
1085 WPA_REPLAY_COUNTER_LEN) == 0) {
1086 /*
1087 * Supplicant may still be using the old SNonce since
1088 * there was two EAPOL-Key 2/4 messages and they had
1089 * different SNonce values.
1090 */
1091 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1092 "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 -08001093 goto continue_processing;
1094 }
1095
1096 if (msg == PAIRWISE_2 &&
1097 wpa_replay_counter_valid(sm->prev_key_replay,
1098 key->replay_counter) &&
1099 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
1100 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1101 "ignore retransmitted EAPOL-Key %s - "
1102 "SNonce did not change", msgtxt);
1103 } else {
1104 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1105 "received EAPOL-Key %s with "
1106 "unexpected replay counter", msgtxt);
1107 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
1109 if (!sm->key_replay[i].valid)
1110 break;
1111 wpa_hexdump(MSG_DEBUG, "pending replay counter",
1112 sm->key_replay[i].counter,
1113 WPA_REPLAY_COUNTER_LEN);
1114 }
1115 wpa_hexdump(MSG_DEBUG, "received replay counter",
1116 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1117 return;
1118 }
1119
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001120continue_processing:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001121#ifdef CONFIG_FILS
1122 if (sm->wpa == WPA_VERSION_WPA2 && mic_len == 0 &&
1123 !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1124 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1125 "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
1126 return;
1127 }
1128#endif /* CONFIG_FILS */
1129
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001130 switch (msg) {
1131 case PAIRWISE_2:
1132 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001133 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
1134 (!sm->update_snonce ||
1135 sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1137 "received EAPOL-Key msg 2/4 in "
1138 "invalid state (%d) - dropped",
1139 sm->wpa_ptk_state);
1140 return;
1141 }
1142 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
1143 if (sm->group->reject_4way_hs_for_entropy) {
1144 /*
1145 * The system did not have enough entropy to generate
1146 * strong random numbers. Reject the first 4-way
1147 * handshake(s) and collect some entropy based on the
1148 * information from it. Once enough entropy is
1149 * available, the next atempt will trigger GMK/Key
1150 * Counter update and the station will be allowed to
1151 * continue.
1152 */
1153 wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
1154 "collect more entropy for random number "
1155 "generation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001156 random_mark_pool_ready();
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001157 wpa_sta_disconnect(wpa_auth, sm->addr,
1158 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159 return;
1160 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161 break;
1162 case PAIRWISE_4:
1163 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1164 !sm->PTK_valid) {
1165 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1166 "received EAPOL-Key msg 4/4 in "
1167 "invalid state (%d) - dropped",
1168 sm->wpa_ptk_state);
1169 return;
1170 }
1171 break;
1172 case GROUP_2:
1173 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1174 || !sm->PTK_valid) {
1175 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1176 "received EAPOL-Key msg 2/2 in "
1177 "invalid state (%d) - dropped",
1178 sm->wpa_ptk_group_state);
1179 return;
1180 }
1181 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182 case REQUEST:
1183 break;
1184 }
1185
1186 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1187 "received EAPOL-Key frame (%s)", msgtxt);
1188
1189 if (key_info & WPA_KEY_INFO_ACK) {
1190 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1191 "received invalid EAPOL-Key: Key Ack set");
1192 return;
1193 }
1194
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001195 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1196 !(key_info & WPA_KEY_INFO_MIC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1198 "received invalid EAPOL-Key: Key MIC not set");
1199 return;
1200 }
1201
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001202#ifdef CONFIG_FILS
1203 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1204 (key_info & WPA_KEY_INFO_MIC)) {
1205 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1206 "received invalid EAPOL-Key: Key MIC set");
1207 return;
1208 }
1209#endif /* CONFIG_FILS */
1210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001211 sm->MICVerified = FALSE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001212 if (sm->PTK_valid && !sm->update_snonce) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001213 if (mic_len &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001214 wpa_verify_key_mic(sm->wpa_key_mgmt, sm->pmk_len, &sm->PTK,
1215 data, data_len) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001216 (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
1217 wpa_try_alt_snonce(sm, data, data_len))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1219 "received EAPOL-Key with invalid MIC");
1220 return;
1221 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001222#ifdef CONFIG_FILS
1223 if (!mic_len &&
1224 wpa_aead_decrypt(sm, &sm->PTK, data, data_len,
1225 &key_data_length) < 0) {
1226 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1227 "received EAPOL-Key with invalid MIC");
1228 return;
1229 }
1230#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001231 sm->MICVerified = TRUE;
1232 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1233 sm->pending_1_of_4_timeout = 0;
1234 }
1235
1236 if (key_info & WPA_KEY_INFO_REQUEST) {
1237 if (sm->MICVerified) {
1238 sm->req_replay_counter_used = 1;
1239 os_memcpy(sm->req_replay_counter, key->replay_counter,
1240 WPA_REPLAY_COUNTER_LEN);
1241 } else {
1242 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1243 "received EAPOL-Key request with "
1244 "invalid MIC");
1245 return;
1246 }
1247
1248 /*
1249 * TODO: should decrypt key data field if encryption was used;
1250 * even though MAC address KDE is not normally encrypted,
1251 * supplicant is allowed to encrypt it.
1252 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001253 if (key_info & WPA_KEY_INFO_ERROR) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001254 if (wpa_receive_error_report(
1255 wpa_auth, sm,
1256 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1257 return; /* STA entry was removed */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1259 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1260 "received EAPOL-Key Request for new "
1261 "4-Way Handshake");
1262 wpa_request_new_ptk(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001263 } else if (key_data_length > 0 &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001264 wpa_parse_kde_ies(key_data, key_data_length,
1265 &kde) == 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266 kde.mac_addr) {
1267 } else {
1268 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1269 "received EAPOL-Key Request for GTK "
1270 "rekeying");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001271 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1272 wpa_rekey_gtk(wpa_auth, NULL);
1273 }
1274 } else {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001275 /* Do not allow the same key replay counter to be reused. */
1276 wpa_replay_counter_mark_invalid(sm->key_replay,
1277 key->replay_counter);
1278
1279 if (msg == PAIRWISE_2) {
1280 /*
1281 * Maintain a copy of the pending EAPOL-Key frames in
1282 * case the EAPOL-Key frame was retransmitted. This is
1283 * needed to allow EAPOL-Key msg 2/4 reply to another
1284 * pending msg 1/4 to update the SNonce to work around
1285 * unexpected supplicant behavior.
1286 */
1287 os_memcpy(sm->prev_key_replay, sm->key_replay,
1288 sizeof(sm->key_replay));
1289 } else {
1290 os_memset(sm->prev_key_replay, 0,
1291 sizeof(sm->prev_key_replay));
1292 }
1293
1294 /*
1295 * Make sure old valid counters are not accepted anymore and
1296 * do not get copied again.
1297 */
1298 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001299 }
1300
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001301 os_free(sm->last_rx_eapol_key);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001302 sm->last_rx_eapol_key = os_memdup(data, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303 if (sm->last_rx_eapol_key == NULL)
1304 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305 sm->last_rx_eapol_key_len = data_len;
1306
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001307 sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001308 sm->EAPOLKeyReceived = TRUE;
1309 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1310 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1311 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1312 wpa_sm_step(sm);
1313}
1314
1315
1316static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1317 const u8 *gnonce, u8 *gtk, size_t gtk_len)
1318{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001319 u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + WPA_GTK_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320 u8 *pos;
1321 int ret = 0;
1322
1323 /* GTK = PRF-X(GMK, "Group key expansion",
1324 * AA || GNonce || Time || random data)
1325 * The example described in the IEEE 802.11 standard uses only AA and
1326 * GNonce as inputs here. Add some more entropy since this derivation
1327 * is done only at the Authenticator and as such, does not need to be
1328 * exactly same.
1329 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001330 os_memset(data, 0, sizeof(data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331 os_memcpy(data, addr, ETH_ALEN);
1332 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1333 pos = data + ETH_ALEN + WPA_NONCE_LEN;
1334 wpa_get_ntp_timestamp(pos);
1335 pos += 8;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001336 if (random_get_bytes(pos, gtk_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337 ret = -1;
1338
Roshan Pius3a1667e2018-07-03 15:17:14 -07001339#ifdef CONFIG_SHA384
1340 if (sha384_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1341 gtk, gtk_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001342 ret = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001343#else /* CONFIG_SHA384 */
1344#ifdef CONFIG_SHA256
1345 if (sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1346 gtk, gtk_len) < 0)
1347 ret = -1;
1348#else /* CONFIG_SHA256 */
1349 if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1350 gtk, gtk_len) < 0)
1351 ret = -1;
1352#endif /* CONFIG_SHA256 */
1353#endif /* CONFIG_SHA384 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001354
1355 return ret;
1356}
1357
1358
1359static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1360{
1361 struct wpa_authenticator *wpa_auth = eloop_ctx;
1362 struct wpa_state_machine *sm = timeout_ctx;
1363
1364 sm->pending_1_of_4_timeout = 0;
1365 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
1366 sm->TimeoutEvt = TRUE;
1367 wpa_sm_step(sm);
1368}
1369
1370
1371void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1372 struct wpa_state_machine *sm, int key_info,
1373 const u8 *key_rsc, const u8 *nonce,
1374 const u8 *kde, size_t kde_len,
1375 int keyidx, int encr, int force_version)
1376{
1377 struct ieee802_1x_hdr *hdr;
1378 struct wpa_eapol_key *key;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001379 size_t len, mic_len, keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001380 int alg;
1381 int key_data_len, pad_len = 0;
1382 u8 *buf, *pos;
1383 int version, pairwise;
1384 int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001385 u8 *key_mic, *key_data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001387 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001388 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001389
1390 len = sizeof(struct ieee802_1x_hdr) + keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391
1392 if (force_version)
1393 version = force_version;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001394 else if (wpa_use_akm_defined(sm->wpa_key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001395 version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001396 else if (wpa_use_cmac(sm->wpa_key_mgmt))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001398 else if (sm->pairwise != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001399 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1400 else
1401 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1402
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001403 pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001404
1405 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1406 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1407 "encr=%d)",
1408 version,
1409 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1410 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1411 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1412 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1413 pairwise, (unsigned long) kde_len, keyidx, encr);
1414
1415 key_data_len = kde_len;
1416
1417 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07001418 wpa_use_aes_key_wrap(sm->wpa_key_mgmt) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001419 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1420 pad_len = key_data_len % 8;
1421 if (pad_len)
1422 pad_len = 8 - pad_len;
1423 key_data_len += pad_len + 8;
1424 }
1425
1426 len += key_data_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001427 if (!mic_len && encr)
1428 len += AES_BLOCK_SIZE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001429
1430 hdr = os_zalloc(len);
1431 if (hdr == NULL)
1432 return;
1433 hdr->version = wpa_auth->conf.eapol_version;
1434 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1435 hdr->length = host_to_be16(len - sizeof(*hdr));
1436 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001437 key_mic = (u8 *) (key + 1);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001438 key_data = ((u8 *) (hdr + 1)) + keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001439
1440 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1441 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1442 key_info |= version;
1443 if (encr && sm->wpa == WPA_VERSION_WPA2)
1444 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1445 if (sm->wpa != WPA_VERSION_WPA2)
1446 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1447 WPA_PUT_BE16(key->key_info, key_info);
1448
1449 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001450 if (sm->wpa == WPA_VERSION_WPA2 && !pairwise)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001451 WPA_PUT_BE16(key->key_length, 0);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001452 else
1453 WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001454
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001455 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1456 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1457 os_memcpy(sm->key_replay[i].counter,
1458 sm->key_replay[i - 1].counter,
1459 WPA_REPLAY_COUNTER_LEN);
1460 }
1461 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1462 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1463 WPA_REPLAY_COUNTER_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001464 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
1465 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001466 sm->key_replay[0].valid = TRUE;
1467
1468 if (nonce)
1469 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1470
1471 if (key_rsc)
1472 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1473
1474 if (kde && !encr) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001475 os_memcpy(key_data, kde, kde_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001476 WPA_PUT_BE16(key_mic + mic_len, kde_len);
1477#ifdef CONFIG_FILS
Roshan Pius3a1667e2018-07-03 15:17:14 -07001478 } else if (!mic_len && kde) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001479 const u8 *aad[1];
1480 size_t aad_len[1];
1481
1482 WPA_PUT_BE16(key_mic, AES_BLOCK_SIZE + kde_len);
1483 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1484 kde, kde_len);
1485
1486 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK",
1487 sm->PTK.kek, sm->PTK.kek_len);
1488 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1489 * to Key Data (exclusive). */
1490 aad[0] = (u8 *) hdr;
1491 aad_len[0] = key_mic + 2 - (u8 *) hdr;
1492 if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len, kde, kde_len,
1493 1, aad, aad_len, key_mic + 2) < 0) {
1494 wpa_printf(MSG_DEBUG, "WPA: AES-SIV encryption failed");
1495 return;
1496 }
1497
1498 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
1499 key_mic + 2, AES_BLOCK_SIZE + kde_len);
1500#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001501 } else if (encr && kde) {
1502 buf = os_zalloc(key_data_len);
1503 if (buf == NULL) {
1504 os_free(hdr);
1505 return;
1506 }
1507 pos = buf;
1508 os_memcpy(pos, kde, kde_len);
1509 pos += kde_len;
1510
1511 if (pad_len)
1512 *pos++ = 0xdd;
1513
1514 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1515 buf, key_data_len);
1516 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07001517 wpa_use_aes_key_wrap(sm->wpa_key_mgmt) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001518 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001519 wpa_printf(MSG_DEBUG,
1520 "WPA: Encrypt Key Data using AES-WRAP (KEK length %u)",
1521 (unsigned int) sm->PTK.kek_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001522 if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len,
1523 (key_data_len - 8) / 8, buf, key_data)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001524 os_free(hdr);
1525 os_free(buf);
1526 return;
1527 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001528 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001529#ifndef CONFIG_NO_RC4
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001530 } else if (sm->PTK.kek_len == 16) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001531 u8 ek[32];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001532
1533 wpa_printf(MSG_DEBUG,
1534 "WPA: Encrypt Key Data using RC4");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001535 os_memcpy(key->key_iv,
1536 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1537 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1538 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001539 os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len);
1540 os_memcpy(key_data, buf, key_data_len);
1541 rc4_skip(ek, 32, 256, key_data, key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001542 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001543#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001544 } else {
1545 os_free(hdr);
1546 os_free(buf);
1547 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001548 }
1549 os_free(buf);
1550 }
1551
1552 if (key_info & WPA_KEY_INFO_MIC) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001553 if (!sm->PTK_valid || !mic_len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001554 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1555 "PTK not valid when sending EAPOL-Key "
1556 "frame");
1557 os_free(hdr);
1558 return;
1559 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001560
Roshan Pius3a1667e2018-07-03 15:17:14 -07001561 if (wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len,
1562 sm->wpa_key_mgmt, version,
1563 (u8 *) hdr, len, key_mic) < 0) {
1564 os_free(hdr);
1565 return;
1566 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001567#ifdef CONFIG_TESTING_OPTIONS
1568 if (!pairwise &&
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001569 wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001570 drand48() <
1571 wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
1572 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1573 "Corrupting group EAPOL-Key Key MIC");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001574 key_mic[0]++;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001575 }
1576#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001577 }
1578
1579 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1580 1);
1581 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1582 sm->pairwise_set);
1583 os_free(hdr);
1584}
1585
1586
1587static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1588 struct wpa_state_machine *sm, int key_info,
1589 const u8 *key_rsc, const u8 *nonce,
1590 const u8 *kde, size_t kde_len,
1591 int keyidx, int encr)
1592{
1593 int timeout_ms;
1594 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001595 u32 ctr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001596
1597 if (sm == NULL)
1598 return;
1599
1600 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1601 keyidx, encr, 0);
1602
1603 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1604 if (ctr == 1 && wpa_auth->conf.tx_status)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001605 timeout_ms = pairwise ? eapol_key_timeout_first :
1606 eapol_key_timeout_first_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001607 else
1608 timeout_ms = eapol_key_timeout_subseq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001609 if (wpa_auth->conf.wpa_disable_eapol_key_retries &&
1610 (!pairwise || (key_info & WPA_KEY_INFO_MIC)))
1611 timeout_ms = eapol_key_timeout_no_retrans;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001612 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1613 sm->pending_1_of_4_timeout = 1;
1614 wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001615 "counter %u)", timeout_ms, ctr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001616 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1617 wpa_send_eapol_timeout, wpa_auth, sm);
1618}
1619
1620
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001621static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK,
1622 u8 *data, size_t data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001623{
1624 struct ieee802_1x_hdr *hdr;
1625 struct wpa_eapol_key *key;
1626 u16 key_info;
1627 int ret = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001628 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN], *mic_pos;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001629 size_t mic_len = wpa_mic_len(akmp, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001630
1631 if (data_len < sizeof(*hdr) + sizeof(*key))
1632 return -1;
1633
1634 hdr = (struct ieee802_1x_hdr *) data;
1635 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001636 mic_pos = (u8 *) (key + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001637 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001638 os_memcpy(mic, mic_pos, mic_len);
1639 os_memset(mic_pos, 0, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001640 if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp,
1641 key_info & WPA_KEY_INFO_TYPE_MASK,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001642 data, data_len, mic_pos) ||
1643 os_memcmp_const(mic, mic_pos, mic_len) != 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001644 ret = -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001645 os_memcpy(mic_pos, mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001646 return ret;
1647}
1648
1649
1650void wpa_remove_ptk(struct wpa_state_machine *sm)
1651{
1652 sm->PTK_valid = FALSE;
1653 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001654 if (wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL,
1655 0))
1656 wpa_printf(MSG_DEBUG,
1657 "RSN: PTK removal from the driver failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001658 sm->pairwise_set = FALSE;
1659 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1660}
1661
1662
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001663int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001664{
1665 int remove_ptk = 1;
1666
1667 if (sm == NULL)
1668 return -1;
1669
1670 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1671 "event %d notification", event);
1672
1673 switch (event) {
1674 case WPA_AUTH:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001675#ifdef CONFIG_MESH
1676 /* PTKs are derived through AMPE */
1677 if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
1678 /* not mesh */
1679 break;
1680 }
1681 return 0;
1682#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683 case WPA_ASSOC:
1684 break;
1685 case WPA_DEAUTH:
1686 case WPA_DISASSOC:
1687 sm->DeauthenticationRequest = TRUE;
1688 break;
1689 case WPA_REAUTH:
1690 case WPA_REAUTH_EAPOL:
1691 if (!sm->started) {
1692 /*
1693 * When using WPS, we may end up here if the STA
1694 * manages to re-associate without the previous STA
1695 * entry getting removed. Consequently, we need to make
1696 * sure that the WPA state machines gets initialized
1697 * properly at this point.
1698 */
1699 wpa_printf(MSG_DEBUG, "WPA state machine had not been "
1700 "started - initialize now");
1701 sm->started = 1;
1702 sm->Init = TRUE;
1703 if (wpa_sm_step(sm) == 1)
1704 return 1; /* should not really happen */
1705 sm->Init = FALSE;
1706 sm->AuthenticationRequest = TRUE;
1707 break;
1708 }
1709 if (sm->GUpdateStationKeys) {
1710 /*
1711 * Reauthentication cancels the pending group key
1712 * update for this STA.
1713 */
1714 sm->group->GKeyDoneStations--;
1715 sm->GUpdateStationKeys = FALSE;
1716 sm->PtkGroupInit = TRUE;
1717 }
1718 sm->ReAuthenticationRequest = TRUE;
1719 break;
1720 case WPA_ASSOC_FT:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001721#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001722 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1723 "after association");
1724 wpa_ft_install_ptk(sm);
1725
1726 /* Using FT protocol, not WPA auth state machine */
1727 sm->ft_completed = 1;
1728 return 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001729#else /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001730 break;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001731#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001732 case WPA_ASSOC_FILS:
1733#ifdef CONFIG_FILS
1734 wpa_printf(MSG_DEBUG,
1735 "FILS: TK configuration after association");
1736 fils_set_tk(sm);
1737 sm->fils_completed = 1;
1738 return 0;
1739#else /* CONFIG_FILS */
1740 break;
1741#endif /* CONFIG_FILS */
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02001742 case WPA_DRV_STA_REMOVED:
1743 sm->tk_already_set = FALSE;
1744 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001745 }
1746
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001747#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748 sm->ft_completed = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001749#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001750
1751#ifdef CONFIG_IEEE80211W
1752 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1753 remove_ptk = 0;
1754#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001755#ifdef CONFIG_FILS
1756 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1757 (event == WPA_AUTH || event == WPA_ASSOC))
1758 remove_ptk = 0;
1759#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001760
1761 if (remove_ptk) {
1762 sm->PTK_valid = FALSE;
1763 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1764
1765 if (event != WPA_REAUTH_EAPOL)
1766 wpa_remove_ptk(sm);
1767 }
1768
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001769 if (sm->in_step_loop) {
1770 /*
1771 * wpa_sm_step() is already running - avoid recursive call to
1772 * it by making the existing loop process the new update.
1773 */
1774 sm->changed = TRUE;
1775 return 0;
1776 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001777 return wpa_sm_step(sm);
1778}
1779
1780
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001781SM_STATE(WPA_PTK, INITIALIZE)
1782{
1783 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1784 if (sm->Init) {
1785 /* Init flag is not cleared here, so avoid busy
1786 * loop by claiming nothing changed. */
1787 sm->changed = FALSE;
1788 }
1789
1790 sm->keycount = 0;
1791 if (sm->GUpdateStationKeys)
1792 sm->group->GKeyDoneStations--;
1793 sm->GUpdateStationKeys = FALSE;
1794 if (sm->wpa == WPA_VERSION_WPA)
1795 sm->PInitAKeys = FALSE;
1796 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1797 * Local AA > Remote AA)) */) {
1798 sm->Pair = TRUE;
1799 }
1800 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1801 wpa_remove_ptk(sm);
1802 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1803 sm->TimeoutCtr = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001804 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
1805 sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP ||
1806 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001807 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1808 WPA_EAPOL_authorized, 0);
1809 }
1810}
1811
1812
1813SM_STATE(WPA_PTK, DISCONNECT)
1814{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001815 u16 reason = sm->disconnect_reason;
1816
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001817 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1818 sm->Disconnect = FALSE;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001819 sm->disconnect_reason = 0;
1820 if (!reason)
1821 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1822 wpa_sta_disconnect(sm->wpa_auth, sm->addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823}
1824
1825
1826SM_STATE(WPA_PTK, DISCONNECTED)
1827{
1828 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1829 sm->DeauthenticationRequest = FALSE;
1830}
1831
1832
1833SM_STATE(WPA_PTK, AUTHENTICATION)
1834{
1835 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1836 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1837 sm->PTK_valid = FALSE;
1838 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1839 1);
1840 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1841 sm->AuthenticationRequest = FALSE;
1842}
1843
1844
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001845static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
1846 struct wpa_group *group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001847{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001848 if (group->first_sta_seen)
1849 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001850 /*
1851 * System has run bit further than at the time hostapd was started
1852 * potentially very early during boot up. This provides better chances
1853 * of collecting more randomness on embedded systems. Re-initialize the
1854 * GMK and Counter here to improve their strength if there was not
1855 * enough entropy available immediately after system startup.
1856 */
1857 wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
1858 "station");
1859 if (random_pool_ready() != 1) {
1860 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
1861 "to proceed - reject first 4-way handshake");
1862 group->reject_4way_hs_for_entropy = TRUE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001863 } else {
1864 group->first_sta_seen = TRUE;
1865 group->reject_4way_hs_for_entropy = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001866 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001867
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001868 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 ||
1869 wpa_gtk_update(wpa_auth, group) < 0 ||
1870 wpa_group_config_group_keys(wpa_auth, group) < 0) {
1871 wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed");
1872 group->first_sta_seen = FALSE;
1873 group->reject_4way_hs_for_entropy = TRUE;
1874 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001875}
1876
1877
1878SM_STATE(WPA_PTK, AUTHENTICATION2)
1879{
1880 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1881
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001882 wpa_group_ensure_init(sm->wpa_auth, sm->group);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001883 sm->ReAuthenticationRequest = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001884
Dmitry Shmidt04949592012-07-19 12:16:46 -07001885 /*
1886 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1887 * ambiguous. The Authenticator state machine uses a counter that is
1888 * incremented by one for each 4-way handshake. However, the security
1889 * analysis of 4-way handshake points out that unpredictable nonces
1890 * help in preventing precomputation attacks. Instead of the state
1891 * machine definition, use an unpredictable nonce value here to provide
1892 * stronger protection against potential precomputation attacks.
1893 */
1894 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1895 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
1896 "ANonce.");
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001897 sm->Disconnect = TRUE;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001898 return;
1899 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001900 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
1901 WPA_NONCE_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001902 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1903 * logical place than INITIALIZE since AUTHENTICATION2 can be
1904 * re-entered on ReAuthenticationRequest without going through
1905 * INITIALIZE. */
1906 sm->TimeoutCtr = 0;
1907}
1908
1909
Jouni Malinen1420a892017-10-01 12:32:57 +03001910static int wpa_auth_sm_ptk_update(struct wpa_state_machine *sm)
1911{
1912 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1913 wpa_printf(MSG_ERROR,
1914 "WPA: Failed to get random data for ANonce");
1915 sm->Disconnect = TRUE;
1916 return -1;
1917 }
1918 wpa_hexdump(MSG_DEBUG, "WPA: Assign new ANonce", sm->ANonce,
1919 WPA_NONCE_LEN);
1920 sm->TimeoutCtr = 0;
1921 return 0;
1922}
1923
1924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001925SM_STATE(WPA_PTK, INITPMK)
1926{
1927 u8 msk[2 * PMK_LEN];
1928 size_t len = 2 * PMK_LEN;
1929
1930 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001931#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001932 sm->xxkey_len = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001933#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934 if (sm->pmksa) {
1935 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001936 os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
1937 sm->pmk_len = sm->pmksa->pmk_len;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001938#ifdef CONFIG_DPP
1939 } else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP) {
1940 wpa_printf(MSG_DEBUG,
1941 "DPP: No PMKSA cache entry for STA - reject connection");
1942 sm->Disconnect = TRUE;
1943 sm->disconnect_reason = WLAN_REASON_INVALID_PMKID;
1944 return;
1945#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001946 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001947 unsigned int pmk_len;
1948
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001949 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001950 pmk_len = PMK_LEN_SUITE_B_192;
1951 else
1952 pmk_len = PMK_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001953 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001954 "(MSK len=%lu PMK len=%u)", (unsigned long) len,
1955 pmk_len);
1956 if (len < pmk_len) {
1957 wpa_printf(MSG_DEBUG,
1958 "WPA: MSK not long enough (%u) to create PMK (%u)",
1959 (unsigned int) len, (unsigned int) pmk_len);
1960 sm->Disconnect = TRUE;
1961 return;
1962 }
1963 os_memcpy(sm->PMK, msk, pmk_len);
1964 sm->pmk_len = pmk_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001965#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001966 if (len >= 2 * PMK_LEN) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001967 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
1968 os_memcpy(sm->xxkey, msk, SHA384_MAC_LEN);
1969 sm->xxkey_len = SHA384_MAC_LEN;
1970 } else {
1971 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1972 sm->xxkey_len = PMK_LEN;
1973 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001974 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001975#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001976 } else {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001977 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p",
Paul Stewart092955c2017-02-06 09:13:09 -08001978 sm->wpa_auth->cb->get_msk);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001979 sm->Disconnect = TRUE;
1980 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001981 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001982 os_memset(msk, 0, sizeof(msk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001983
1984 sm->req_replay_counter_used = 0;
1985 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1986 * will break reauthentication since EAPOL state machines may not be
1987 * get into AUTHENTICATING state that clears keyRun before WPA state
1988 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1989 * state and takes PMK from the previously used AAA Key. This will
1990 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1991 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1992 * be good workaround for this issue. */
1993 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1994}
1995
1996
1997SM_STATE(WPA_PTK, INITPSK)
1998{
1999 const u8 *psk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002000 size_t psk_len;
2001
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002003 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL,
2004 &psk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002005 if (psk) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002006 os_memcpy(sm->PMK, psk, psk_len);
2007 sm->pmk_len = psk_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002008#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002009 os_memcpy(sm->xxkey, psk, PMK_LEN);
2010 sm->xxkey_len = PMK_LEN;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002011#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002012 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002013#ifdef CONFIG_SAE
2014 if (wpa_auth_uses_sae(sm) && sm->pmksa) {
2015 wpa_printf(MSG_DEBUG, "SAE: PMK from PMKSA cache");
2016 os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
2017 sm->pmk_len = sm->pmksa->pmk_len;
2018 }
2019#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020 sm->req_replay_counter_used = 0;
2021}
2022
2023
2024SM_STATE(WPA_PTK, PTKSTART)
2025{
2026 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
2027 size_t pmkid_len = 0;
2028
2029 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
2030 sm->PTKRequest = FALSE;
2031 sm->TimeoutEvt = FALSE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002032 sm->alt_snonce_valid = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033
2034 sm->TimeoutCtr++;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002035 if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002036 /* No point in sending the EAPOL-Key - we will disconnect
2037 * immediately following this. */
2038 return;
2039 }
2040
2041 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2042 "sending 1/4 msg of 4-Way Handshake");
2043 /*
Hai Shalomce48b4a2018-09-05 11:41:35 -07002044 * For infrastructure BSS cases, it is better for the AP not to include
2045 * the PMKID KDE in EAPOL-Key msg 1/4 since it could be used to initiate
2046 * offline search for the passphrase/PSK without having to be able to
2047 * capture a 4-way handshake from a STA that has access to the network.
2048 *
2049 * For IBSS cases, addition of PMKID KDE could be considered even with
2050 * WPA2-PSK cases that use multiple PSKs, but only if there is a single
2051 * possible PSK for this STA. However, this should not be done unless
2052 * there is support for using that information on the supplicant side.
2053 * The concern about exposing PMKID unnecessarily in infrastructure BSS
2054 * cases would also apply here, but at least in the IBSS case, this
2055 * would cover a potential real use case.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002056 */
2057 if (sm->wpa == WPA_VERSION_WPA2 &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002058 (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) ||
2059 (sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE && sm->pmksa) ||
2060 wpa_key_mgmt_sae(sm->wpa_key_mgmt)) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002061 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002062 pmkid = buf;
2063 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
2064 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
2065 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
2066 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002067 if (sm->pmksa) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002068 wpa_hexdump(MSG_DEBUG,
2069 "RSN: Message 1/4 PMKID from PMKSA entry",
2070 sm->pmksa->pmkid, PMKID_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002071 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2072 sm->pmksa->pmkid, PMKID_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002073 } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
2074 /* No KCK available to derive PMKID */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002075 wpa_printf(MSG_DEBUG,
2076 "RSN: No KCK available to derive PMKID for message 1/4");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002077 pmkid = NULL;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002078#ifdef CONFIG_SAE
2079 } else if (wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
2080 if (sm->pmkid_set) {
2081 wpa_hexdump(MSG_DEBUG,
2082 "RSN: Message 1/4 PMKID from SAE",
2083 sm->pmkid, PMKID_LEN);
2084 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2085 sm->pmkid, PMKID_LEN);
2086 } else {
2087 /* No PMKID available */
2088 wpa_printf(MSG_DEBUG,
2089 "RSN: No SAE PMKID available for message 1/4");
2090 pmkid = NULL;
2091 }
2092#endif /* CONFIG_SAE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002093 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094 /*
2095 * Calculate PMKID since no PMKSA cache entry was
2096 * available with pre-calculated PMKID.
2097 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002098 rsn_pmkid(sm->PMK, sm->pmk_len, sm->wpa_auth->addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002100 sm->wpa_key_mgmt);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002101 wpa_hexdump(MSG_DEBUG,
2102 "RSN: Message 1/4 PMKID derived from PMK",
2103 &pmkid[2 + RSN_SELECTOR_LEN], PMKID_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104 }
2105 }
2106 wpa_send_eapol(sm->wpa_auth, sm,
2107 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
2108 sm->ANonce, pmkid, pmkid_len, 0, 0);
2109}
2110
2111
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002112static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002113 const u8 *pmk, unsigned int pmk_len,
2114 struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002115{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002116#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002117 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002118 return wpa_auth_derive_ptk_ft(sm, pmk, ptk);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002119#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002120
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002121 return wpa_pmk_to_ptk(pmk, pmk_len, "Pairwise key expansion",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002122 sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce,
2123 ptk, sm->wpa_key_mgmt, sm->pairwise);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124}
2125
2126
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002127#ifdef CONFIG_FILS
2128
2129int fils_auth_pmk_to_ptk(struct wpa_state_machine *sm, const u8 *pmk,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002130 size_t pmk_len, const u8 *snonce, const u8 *anonce,
2131 const u8 *dhss, size_t dhss_len,
2132 struct wpabuf *g_sta, struct wpabuf *g_ap)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002133{
2134 u8 ick[FILS_ICK_MAX_LEN];
2135 size_t ick_len;
2136 int res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002137 u8 fils_ft[FILS_FT_MAX_LEN];
2138 size_t fils_ft_len = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002139
2140 res = fils_pmk_to_ptk(pmk, pmk_len, sm->addr, sm->wpa_auth->addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002141 snonce, anonce, dhss, dhss_len,
2142 &sm->PTK, ick, &ick_len,
2143 sm->wpa_key_mgmt, sm->pairwise,
2144 fils_ft, &fils_ft_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002145 if (res < 0)
2146 return res;
2147 sm->PTK_valid = TRUE;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002148 sm->tk_already_set = FALSE;
2149
2150#ifdef CONFIG_IEEE80211R_AP
2151 if (fils_ft_len) {
2152 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2153 struct wpa_auth_config *conf = &wpa_auth->conf;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002154 u8 pmk_r0[PMK_LEN_MAX], pmk_r0_name[WPA_PMK_NAME_LEN];
2155 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
2156 size_t pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002157
2158 if (wpa_derive_pmk_r0(fils_ft, fils_ft_len,
2159 conf->ssid, conf->ssid_len,
2160 conf->mobility_domain,
2161 conf->r0_key_holder,
2162 conf->r0_key_holder_len,
Roshan Pius3a1667e2018-07-03 15:17:14 -07002163 sm->addr, pmk_r0, pmk_r0_name,
2164 use_sha384) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002165 return -1;
2166
Roshan Pius3a1667e2018-07-03 15:17:14 -07002167 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0",
2168 pmk_r0, pmk_r0_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002169 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name",
2170 pmk_r0_name, WPA_PMK_NAME_LEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002171 wpa_ft_store_pmk_fils(sm, pmk_r0, pmk_r0_name);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002172 os_memset(fils_ft, 0, sizeof(fils_ft));
2173 }
2174#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002175
2176 res = fils_key_auth_sk(ick, ick_len, snonce, anonce,
2177 sm->addr, sm->wpa_auth->addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002178 g_sta ? wpabuf_head(g_sta) : NULL,
2179 g_sta ? wpabuf_len(g_sta) : 0,
2180 g_ap ? wpabuf_head(g_ap) : NULL,
2181 g_ap ? wpabuf_len(g_ap) : 0,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002182 sm->wpa_key_mgmt, sm->fils_key_auth_sta,
2183 sm->fils_key_auth_ap,
2184 &sm->fils_key_auth_len);
2185 os_memset(ick, 0, sizeof(ick));
2186
2187 /* Store nonces for (Re)Association Request/Response frame processing */
2188 os_memcpy(sm->SNonce, snonce, FILS_NONCE_LEN);
2189 os_memcpy(sm->ANonce, anonce, FILS_NONCE_LEN);
2190
2191 return res;
2192}
2193
2194
2195static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
2196 u8 *buf, size_t buf_len, u16 *_key_data_len)
2197{
2198 struct ieee802_1x_hdr *hdr;
2199 struct wpa_eapol_key *key;
2200 u8 *pos;
2201 u16 key_data_len;
2202 u8 *tmp;
2203 const u8 *aad[1];
2204 size_t aad_len[1];
2205
2206 hdr = (struct ieee802_1x_hdr *) buf;
2207 key = (struct wpa_eapol_key *) (hdr + 1);
2208 pos = (u8 *) (key + 1);
2209 key_data_len = WPA_GET_BE16(pos);
2210 if (key_data_len < AES_BLOCK_SIZE ||
2211 key_data_len > buf_len - sizeof(*hdr) - sizeof(*key) - 2) {
2212 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2213 "No room for AES-SIV data in the frame");
2214 return -1;
2215 }
2216 pos += 2; /* Pointing at the Encrypted Key Data field */
2217
2218 tmp = os_malloc(key_data_len);
2219 if (!tmp)
2220 return -1;
2221
2222 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2223 * to Key Data (exclusive). */
2224 aad[0] = buf;
2225 aad_len[0] = pos - buf;
2226 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, key_data_len,
2227 1, aad, aad_len, tmp) < 0) {
2228 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2229 "Invalid AES-SIV data in the frame");
2230 bin_clear_free(tmp, key_data_len);
2231 return -1;
2232 }
2233
2234 /* AEAD decryption and validation completed successfully */
2235 key_data_len -= AES_BLOCK_SIZE;
2236 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2237 tmp, key_data_len);
2238
2239 /* Replace Key Data field with the decrypted version */
2240 os_memcpy(pos, tmp, key_data_len);
2241 pos -= 2; /* Key Data Length field */
2242 WPA_PUT_BE16(pos, key_data_len);
2243 bin_clear_free(tmp, key_data_len);
2244 if (_key_data_len)
2245 *_key_data_len = key_data_len;
2246 return 0;
2247}
2248
2249
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002250const u8 * wpa_fils_validate_fils_session(struct wpa_state_machine *sm,
2251 const u8 *ies, size_t ies_len,
2252 const u8 *fils_session)
2253{
2254 const u8 *ie, *end;
2255 const u8 *session = NULL;
2256
2257 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2258 wpa_printf(MSG_DEBUG,
2259 "FILS: Not a FILS AKM - reject association");
2260 return NULL;
2261 }
2262
2263 /* Verify Session element */
2264 ie = ies;
2265 end = ((const u8 *) ie) + ies_len;
2266 while (ie + 1 < end) {
2267 if (ie + 2 + ie[1] > end)
2268 break;
2269 if (ie[0] == WLAN_EID_EXTENSION &&
2270 ie[1] >= 1 + FILS_SESSION_LEN &&
2271 ie[2] == WLAN_EID_EXT_FILS_SESSION) {
2272 session = ie;
2273 break;
2274 }
2275 ie += 2 + ie[1];
2276 }
2277
2278 if (!session) {
2279 wpa_printf(MSG_DEBUG,
2280 "FILS: %s: Could not find FILS Session element in Assoc Req - reject",
2281 __func__);
2282 return NULL;
2283 }
2284
2285 if (!fils_session) {
2286 wpa_printf(MSG_DEBUG,
2287 "FILS: %s: Could not find FILS Session element in STA entry - reject",
2288 __func__);
2289 return NULL;
2290 }
2291
2292 if (os_memcmp(fils_session, session + 3, FILS_SESSION_LEN) != 0) {
2293 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
2294 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
2295 fils_session, FILS_SESSION_LEN);
2296 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
2297 session + 3, FILS_SESSION_LEN);
2298 return NULL;
2299 }
2300 return session;
2301}
2302
2303
2304int wpa_fils_validate_key_confirm(struct wpa_state_machine *sm, const u8 *ies,
2305 size_t ies_len)
2306{
2307 struct ieee802_11_elems elems;
2308
2309 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
2310 wpa_printf(MSG_DEBUG,
2311 "FILS: Failed to parse decrypted elements");
2312 return -1;
2313 }
2314
2315 if (!elems.fils_session) {
2316 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
2317 return -1;
2318 }
2319
2320 if (!elems.fils_key_confirm) {
2321 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
2322 return -1;
2323 }
2324
2325 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
2326 wpa_printf(MSG_DEBUG,
2327 "FILS: Unexpected Key-Auth length %d (expected %d)",
2328 elems.fils_key_confirm_len,
2329 (int) sm->fils_key_auth_len);
2330 return -1;
2331 }
2332
2333 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_sta,
2334 sm->fils_key_auth_len) != 0) {
2335 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
2336 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
2337 elems.fils_key_confirm, elems.fils_key_confirm_len);
2338 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
2339 sm->fils_key_auth_sta, sm->fils_key_auth_len);
2340 return -1;
2341 }
2342
2343 return 0;
2344}
2345
2346
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002347int fils_decrypt_assoc(struct wpa_state_machine *sm, const u8 *fils_session,
2348 const struct ieee80211_mgmt *mgmt, size_t frame_len,
2349 u8 *pos, size_t left)
2350{
2351 u16 fc, stype;
2352 const u8 *end, *ie_start, *ie, *session, *crypt;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002353 const u8 *aad[5];
2354 size_t aad_len[5];
2355
2356 if (!sm || !sm->PTK_valid) {
2357 wpa_printf(MSG_DEBUG,
2358 "FILS: No KEK to decrypt Assocication Request frame");
2359 return -1;
2360 }
2361
2362 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2363 wpa_printf(MSG_DEBUG,
2364 "FILS: Not a FILS AKM - reject association");
2365 return -1;
2366 }
2367
2368 end = ((const u8 *) mgmt) + frame_len;
2369 fc = le_to_host16(mgmt->frame_control);
2370 stype = WLAN_FC_GET_STYPE(fc);
2371 if (stype == WLAN_FC_STYPE_REASSOC_REQ)
2372 ie_start = mgmt->u.reassoc_req.variable;
2373 else
2374 ie_start = mgmt->u.assoc_req.variable;
2375 ie = ie_start;
2376
2377 /*
2378 * Find FILS Session element which is the last unencrypted element in
2379 * the frame.
2380 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002381 session = wpa_fils_validate_fils_session(sm, ie, end - ie,
2382 fils_session);
2383 if (!session) {
2384 wpa_printf(MSG_DEBUG, "FILS: Session validation failed");
2385 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002386 }
2387
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002388 crypt = session + 2 + session[1];
2389
2390 if (end - crypt < AES_BLOCK_SIZE) {
2391 wpa_printf(MSG_DEBUG,
2392 "FILS: Too short frame to include AES-SIV data");
2393 return -1;
2394 }
2395
2396 /* AES-SIV AAD vectors */
2397
2398 /* The STA's MAC address */
2399 aad[0] = mgmt->sa;
2400 aad_len[0] = ETH_ALEN;
2401 /* The AP's BSSID */
2402 aad[1] = mgmt->da;
2403 aad_len[1] = ETH_ALEN;
2404 /* The STA's nonce */
2405 aad[2] = sm->SNonce;
2406 aad_len[2] = FILS_NONCE_LEN;
2407 /* The AP's nonce */
2408 aad[3] = sm->ANonce;
2409 aad_len[3] = FILS_NONCE_LEN;
2410 /*
2411 * The (Re)Association Request frame from the Capability Information
2412 * field to the FILS Session element (both inclusive).
2413 */
2414 aad[4] = (const u8 *) &mgmt->u.assoc_req.capab_info;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002415 aad_len[4] = crypt - aad[4];
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002416
2417 if (aes_siv_decrypt(sm->PTK.kek, sm->PTK.kek_len, crypt, end - crypt,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002418 5, aad, aad_len, pos + (crypt - ie_start)) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002419 wpa_printf(MSG_DEBUG,
2420 "FILS: Invalid AES-SIV data in the frame");
2421 return -1;
2422 }
2423 wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Request elements",
2424 pos, left - AES_BLOCK_SIZE);
2425
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002426 if (wpa_fils_validate_key_confirm(sm, pos, left - AES_BLOCK_SIZE) < 0) {
2427 wpa_printf(MSG_DEBUG, "FILS: Key Confirm validation failed");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002428 return -1;
2429 }
2430
2431 return left - AES_BLOCK_SIZE;
2432}
2433
2434
2435int fils_encrypt_assoc(struct wpa_state_machine *sm, u8 *buf,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002436 size_t current_len, size_t max_len,
2437 const struct wpabuf *hlp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002438{
2439 u8 *end = buf + max_len;
2440 u8 *pos = buf + current_len;
2441 struct ieee80211_mgmt *mgmt;
2442 struct wpabuf *plain;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002443 const u8 *aad[5];
2444 size_t aad_len[5];
2445
2446 if (!sm || !sm->PTK_valid)
2447 return -1;
2448
2449 wpa_hexdump(MSG_DEBUG,
2450 "FILS: Association Response frame before FILS processing",
2451 buf, current_len);
2452
2453 mgmt = (struct ieee80211_mgmt *) buf;
2454
2455 /* AES-SIV AAD vectors */
2456
2457 /* The AP's BSSID */
2458 aad[0] = mgmt->sa;
2459 aad_len[0] = ETH_ALEN;
2460 /* The STA's MAC address */
2461 aad[1] = mgmt->da;
2462 aad_len[1] = ETH_ALEN;
2463 /* The AP's nonce */
2464 aad[2] = sm->ANonce;
2465 aad_len[2] = FILS_NONCE_LEN;
2466 /* The STA's nonce */
2467 aad[3] = sm->SNonce;
2468 aad_len[3] = FILS_NONCE_LEN;
2469 /*
2470 * The (Re)Association Response frame from the Capability Information
2471 * field (the same offset in both Association and Reassociation
2472 * Response frames) to the FILS Session element (both inclusive).
2473 */
2474 aad[4] = (const u8 *) &mgmt->u.assoc_resp.capab_info;
2475 aad_len[4] = pos - aad[4];
2476
2477 /* The following elements will be encrypted with AES-SIV */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002478 plain = fils_prepare_plainbuf(sm, hlp);
2479 if (!plain) {
2480 wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
2481 return -1;
2482 }
2483
2484 if (pos + wpabuf_len(plain) + AES_BLOCK_SIZE > end) {
2485 wpa_printf(MSG_DEBUG,
2486 "FILS: Not enough room for FILS elements");
2487 wpabuf_free(plain);
2488 return -1;
2489 }
2490
2491 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: Association Response plaintext",
2492 plain);
2493
2494 if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len,
2495 wpabuf_head(plain), wpabuf_len(plain),
2496 5, aad, aad_len, pos) < 0) {
2497 wpabuf_free(plain);
2498 return -1;
2499 }
2500
2501 wpa_hexdump(MSG_DEBUG,
2502 "FILS: Encrypted Association Response elements",
2503 pos, AES_BLOCK_SIZE + wpabuf_len(plain));
2504 current_len += wpabuf_len(plain) + AES_BLOCK_SIZE;
2505 wpabuf_free(plain);
2506
2507 sm->fils_completed = 1;
2508
2509 return current_len;
2510}
2511
2512
2513static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm,
2514 const struct wpabuf *hlp)
2515{
2516 struct wpabuf *plain;
2517 u8 *len, *tmp, *tmp2;
2518 u8 hdr[2];
2519 u8 *gtk, dummy_gtk[32];
2520 size_t gtk_len;
2521 struct wpa_group *gsm;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002522
2523 plain = wpabuf_alloc(1000);
2524 if (!plain)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002525 return NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002526
2527 /* TODO: FILS Public Key */
2528
2529 /* FILS Key Confirmation */
2530 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2531 wpabuf_put_u8(plain, 1 + sm->fils_key_auth_len); /* Length */
2532 /* Element ID Extension */
2533 wpabuf_put_u8(plain, WLAN_EID_EXT_FILS_KEY_CONFIRM);
2534 wpabuf_put_data(plain, sm->fils_key_auth_ap, sm->fils_key_auth_len);
2535
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002536 /* FILS HLP Container */
2537 if (hlp)
2538 wpabuf_put_buf(plain, hlp);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002539
2540 /* TODO: FILS IP Address Assignment */
2541
2542 /* Key Delivery */
2543 gsm = sm->group;
2544 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2545 len = wpabuf_put(plain, 1);
2546 wpabuf_put_u8(plain, WLAN_EID_EXT_KEY_DELIVERY);
2547 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN,
2548 wpabuf_put(plain, WPA_KEY_RSC_LEN));
2549 /* GTK KDE */
2550 gtk = gsm->GTK[gsm->GN - 1];
2551 gtk_len = gsm->GTK_len;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002552 if (sm->wpa_auth->conf.disable_gtk ||
2553 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002554 /*
2555 * Provide unique random GTK to each STA to prevent use
2556 * of GTK in the BSS.
2557 */
2558 if (random_get_bytes(dummy_gtk, gtk_len) < 0) {
2559 wpabuf_free(plain);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002560 return NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002561 }
2562 gtk = dummy_gtk;
2563 }
2564 hdr[0] = gsm->GN & 0x03;
2565 hdr[1] = 0;
2566 tmp = wpabuf_put(plain, 0);
2567 tmp2 = wpa_add_kde(tmp, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2568 gtk, gtk_len);
2569 wpabuf_put(plain, tmp2 - tmp);
2570
2571 /* IGTK KDE */
2572 tmp = wpabuf_put(plain, 0);
2573 tmp2 = ieee80211w_kde_add(sm, tmp);
2574 wpabuf_put(plain, tmp2 - tmp);
2575
2576 *len = (u8 *) wpabuf_put(plain, 0) - len - 1;
Hai Shalom39bc25d2019-02-06 16:32:13 -08002577
2578#ifdef CONFIG_OCV
2579 if (wpa_auth_uses_ocv(sm)) {
2580 struct wpa_channel_info ci;
2581 u8 *pos;
2582
2583 if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
2584 wpa_printf(MSG_WARNING,
2585 "FILS: Failed to get channel info for OCI element");
2586 wpabuf_free(plain);
2587 return NULL;
2588 }
2589
2590 pos = wpabuf_put(plain, OCV_OCI_EXTENDED_LEN);
2591 if (ocv_insert_extended_oci(&ci, pos) < 0) {
2592 wpabuf_free(plain);
2593 return NULL;
2594 }
2595 }
2596#endif /* CONFIG_OCV */
2597
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002598 return plain;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002599}
2600
2601
2602int fils_set_tk(struct wpa_state_machine *sm)
2603{
2604 enum wpa_alg alg;
2605 int klen;
2606
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002607 if (!sm || !sm->PTK_valid) {
2608 wpa_printf(MSG_DEBUG, "FILS: No valid PTK available to set TK");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002609 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002610 }
2611 if (sm->tk_already_set) {
2612 wpa_printf(MSG_DEBUG, "FILS: TK already set to the driver");
2613 return -1;
2614 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002615
2616 alg = wpa_cipher_to_alg(sm->pairwise);
2617 klen = wpa_cipher_key_len(sm->pairwise);
2618
2619 wpa_printf(MSG_DEBUG, "FILS: Configure TK to the driver");
2620 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2621 sm->PTK.tk, klen)) {
2622 wpa_printf(MSG_DEBUG, "FILS: Failed to set TK to the driver");
2623 return -1;
2624 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002625 sm->tk_already_set = TRUE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002626
2627 return 0;
2628}
2629
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002630
2631u8 * hostapd_eid_assoc_fils_session(struct wpa_state_machine *sm, u8 *buf,
2632 const u8 *fils_session, struct wpabuf *hlp)
2633{
2634 struct wpabuf *plain;
2635 u8 *pos = buf;
2636
2637 /* FILS Session */
2638 *pos++ = WLAN_EID_EXTENSION; /* Element ID */
2639 *pos++ = 1 + FILS_SESSION_LEN; /* Length */
2640 *pos++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */
2641 os_memcpy(pos, fils_session, FILS_SESSION_LEN);
2642 pos += FILS_SESSION_LEN;
2643
2644 plain = fils_prepare_plainbuf(sm, hlp);
2645 if (!plain) {
2646 wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
2647 return NULL;
2648 }
2649
2650 os_memcpy(pos, wpabuf_head(plain), wpabuf_len(plain));
2651 pos += wpabuf_len(plain);
2652
2653 wpa_printf(MSG_DEBUG, "%s: plain buf_len: %u", __func__,
2654 (unsigned int) wpabuf_len(plain));
2655 wpabuf_free(plain);
2656 sm->fils_completed = 1;
2657 return pos;
2658}
2659
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002660#endif /* CONFIG_FILS */
2661
2662
Hai Shalom39bc25d2019-02-06 16:32:13 -08002663#ifdef CONFIG_OCV
2664int get_sta_tx_parameters(struct wpa_state_machine *sm, int ap_max_chanwidth,
2665 int ap_seg1_idx, int *bandwidth, int *seg1_idx)
2666{
2667 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2668
2669 if (!wpa_auth->cb->get_sta_tx_params)
2670 return -1;
2671 return wpa_auth->cb->get_sta_tx_params(wpa_auth->cb_ctx, sm->addr,
2672 ap_max_chanwidth, ap_seg1_idx,
2673 bandwidth, seg1_idx);
2674}
2675#endif /* CONFIG_OCV */
2676
2677
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002678SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
2679{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002680 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002681 struct wpa_ptk PTK;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002682 int ok = 0, psk_found = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002683 const u8 *pmk = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002684 size_t pmk_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002685 int ft;
2686 const u8 *eapol_key_ie, *key_data, *mic;
2687 u16 key_data_length;
2688 size_t mic_len, eapol_key_ie_len;
2689 struct ieee802_1x_hdr *hdr;
2690 struct wpa_eapol_key *key;
2691 struct wpa_eapol_ie_parse kde;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002692
2693 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
2694 sm->EAPOLKeyReceived = FALSE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002695 sm->update_snonce = FALSE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002696 os_memset(&PTK, 0, sizeof(PTK));
2697
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002698 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002699
2700 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2701 * WPA-PSK: iterate through possible PSKs and select the one matching
2702 * the packet */
2703 for (;;) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002704 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
2705 !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002706 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002707 sm->p2p_dev_addr, pmk, &pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002708 if (pmk == NULL)
2709 break;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002710 psk_found = 1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002711#ifdef CONFIG_IEEE80211R_AP
2712 if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
2713 os_memcpy(sm->xxkey, pmk, pmk_len);
2714 sm->xxkey_len = pmk_len;
2715 }
2716#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002717 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002718 pmk = sm->PMK;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002719 pmk_len = sm->pmk_len;
2720 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002721
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002722 if (wpa_derive_ptk(sm, sm->SNonce, pmk, pmk_len, &PTK) < 0)
2723 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002724
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002725 if (mic_len &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002726 wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002727 sm->last_rx_eapol_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002728 sm->last_rx_eapol_key_len) == 0) {
Hai Shalom39bc25d2019-02-06 16:32:13 -08002729 os_memcpy(sm->PMK, pmk, pmk_len);
2730 sm->pmk_len = pmk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002731 ok = 1;
2732 break;
2733 }
2734
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002735#ifdef CONFIG_FILS
2736 if (!mic_len &&
2737 wpa_aead_decrypt(sm, &PTK, sm->last_rx_eapol_key,
2738 sm->last_rx_eapol_key_len, NULL) == 0) {
2739 ok = 1;
2740 break;
2741 }
2742#endif /* CONFIG_FILS */
2743
Roshan Pius3a1667e2018-07-03 15:17:14 -07002744 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
2745 wpa_key_mgmt_sae(sm->wpa_key_mgmt))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002746 break;
2747 }
2748
2749 if (!ok) {
2750 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2751 "invalid MIC in msg 2/4 of 4-Way Handshake");
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002752 if (psk_found)
2753 wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002754 return;
2755 }
2756
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002757 /*
2758 * Note: last_rx_eapol_key length fields have already been validated in
2759 * wpa_receive().
2760 */
2761 hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
2762 key = (struct wpa_eapol_key *) (hdr + 1);
2763 mic = (u8 *) (key + 1);
2764 key_data = mic + mic_len + 2;
2765 key_data_length = WPA_GET_BE16(mic + mic_len);
2766 if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
2767 sizeof(*key) - mic_len - 2)
2768 return;
2769
2770 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
2771 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
2772 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
2773 return;
2774 }
2775 if (kde.rsn_ie) {
2776 eapol_key_ie = kde.rsn_ie;
2777 eapol_key_ie_len = kde.rsn_ie_len;
2778 } else if (kde.osen) {
2779 eapol_key_ie = kde.osen;
2780 eapol_key_ie_len = kde.osen_len;
2781 } else {
2782 eapol_key_ie = kde.wpa_ie;
2783 eapol_key_ie_len = kde.wpa_ie_len;
2784 }
2785 ft = sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt);
2786 if (sm->wpa_ie == NULL ||
2787 wpa_compare_rsn_ie(ft, sm->wpa_ie, sm->wpa_ie_len,
2788 eapol_key_ie, eapol_key_ie_len)) {
2789 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2790 "WPA IE from (Re)AssocReq did not match with msg 2/4");
2791 if (sm->wpa_ie) {
2792 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
2793 sm->wpa_ie, sm->wpa_ie_len);
2794 }
2795 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
2796 eapol_key_ie, eapol_key_ie_len);
2797 /* MLME-DEAUTHENTICATE.request */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002798 wpa_sta_disconnect(wpa_auth, sm->addr,
2799 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002800 return;
2801 }
Hai Shalom39bc25d2019-02-06 16:32:13 -08002802#ifdef CONFIG_OCV
2803 if (wpa_auth_uses_ocv(sm)) {
2804 struct wpa_channel_info ci;
2805 int tx_chanwidth;
2806 int tx_seg1_idx;
2807
2808 if (wpa_channel_info(wpa_auth, &ci) != 0) {
2809 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2810 "Failed to get channel info to validate received OCI in EAPOL-Key 2/4");
2811 return;
2812 }
2813
2814 if (get_sta_tx_parameters(sm,
2815 channel_width_to_int(ci.chanwidth),
2816 ci.seg1_idx, &tx_chanwidth,
2817 &tx_seg1_idx) < 0)
2818 return;
2819
2820 if (ocv_verify_tx_params(kde.oci, kde.oci_len, &ci,
2821 tx_chanwidth, tx_seg1_idx) != 0) {
2822 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2823 ocv_errorstr);
2824 return;
2825 }
2826 }
2827#endif /* CONFIG_OCV */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002828#ifdef CONFIG_IEEE80211R_AP
2829 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002830 wpa_sta_disconnect(wpa_auth, sm->addr,
2831 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002832 return;
2833 }
2834#endif /* CONFIG_IEEE80211R_AP */
2835#ifdef CONFIG_P2P
2836 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
2837 wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
2838 int idx;
2839 wpa_printf(MSG_DEBUG,
2840 "P2P: IP address requested in EAPOL-Key exchange");
2841 idx = bitfield_get_first_zero(wpa_auth->ip_pool);
2842 if (idx >= 0) {
2843 u32 start = WPA_GET_BE32(wpa_auth->conf.ip_addr_start);
2844 bitfield_set(wpa_auth->ip_pool, idx);
2845 WPA_PUT_BE32(sm->ip_addr, start + idx);
2846 wpa_printf(MSG_DEBUG,
2847 "P2P: Assigned IP address %u.%u.%u.%u to "
2848 MACSTR, sm->ip_addr[0], sm->ip_addr[1],
2849 sm->ip_addr[2], sm->ip_addr[3],
2850 MAC2STR(sm->addr));
2851 }
2852 }
2853#endif /* CONFIG_P2P */
2854
2855#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2857 /*
2858 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
2859 * with the value we derived.
2860 */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002861 if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
2862 WPA_PMK_NAME_LEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2864 "PMKR1Name mismatch in FT 4-way "
2865 "handshake");
2866 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
2867 "Supplicant",
2868 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
2869 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
2870 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2871 return;
2872 }
2873 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002874#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002875
2876 sm->pending_1_of_4_timeout = 0;
2877 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
2878
2879 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2880 /* PSK may have changed from the previous choice, so update
2881 * state machine data based on whatever PSK was selected here.
2882 */
2883 os_memcpy(sm->PMK, pmk, PMK_LEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002884 sm->pmk_len = PMK_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885 }
2886
2887 sm->MICVerified = TRUE;
2888
2889 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
2890 sm->PTK_valid = TRUE;
2891}
2892
2893
2894SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
2895{
2896 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
2897 sm->TimeoutCtr = 0;
2898}
2899
2900
2901#ifdef CONFIG_IEEE80211W
2902
2903static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2904{
2905 if (sm->mgmt_frame_prot) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002906 size_t len;
2907 len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2908 return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002909 }
2910
2911 return 0;
2912}
2913
2914
2915static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2916{
2917 struct wpa_igtk_kde igtk;
2918 struct wpa_group *gsm = sm->group;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002919 u8 rsc[WPA_KEY_RSC_LEN];
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002920 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002921
2922 if (!sm->mgmt_frame_prot)
2923 return pos;
2924
2925 igtk.keyid[0] = gsm->GN_igtk;
2926 igtk.keyid[1] = 0;
2927 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002928 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002929 os_memset(igtk.pn, 0, sizeof(igtk.pn));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002930 else
2931 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002932 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002933 if (sm->wpa_auth->conf.disable_gtk ||
2934 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002935 /*
2936 * Provide unique random IGTK to each STA to prevent use of
2937 * IGTK in the BSS.
2938 */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002939 if (random_get_bytes(igtk.igtk, len) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002940 return pos;
2941 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002942 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002943 (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
2944 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002945
2946 return pos;
2947}
2948
2949#else /* CONFIG_IEEE80211W */
2950
2951static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2952{
2953 return 0;
2954}
2955
2956
2957static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2958{
2959 return pos;
2960}
2961
2962#endif /* CONFIG_IEEE80211W */
2963
2964
Hai Shalom39bc25d2019-02-06 16:32:13 -08002965static int ocv_oci_len(struct wpa_state_machine *sm)
2966{
2967#ifdef CONFIG_OCV
2968 if (wpa_auth_uses_ocv(sm))
2969 return OCV_OCI_KDE_LEN;
2970#endif /* CONFIG_OCV */
2971 return 0;
2972}
2973
2974static int ocv_oci_add(struct wpa_state_machine *sm, u8 **argpos)
2975{
2976#ifdef CONFIG_OCV
2977 struct wpa_channel_info ci;
2978
2979 if (!wpa_auth_uses_ocv(sm))
2980 return 0;
2981
2982 if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
2983 wpa_printf(MSG_WARNING,
2984 "Failed to get channel info for OCI element");
2985 return -1;
2986 }
2987
2988 return ocv_insert_oci_kde(&ci, argpos);
2989#else /* CONFIG_OCV */
2990 return 0;
2991#endif /* CONFIG_OCV */
2992}
2993
2994
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002995SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
2996{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002997 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002998 size_t gtk_len, kde_len;
2999 struct wpa_group *gsm = sm->group;
3000 u8 *wpa_ie;
3001 int wpa_ie_len, secure, keyidx, encr = 0;
3002
3003 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
3004 sm->TimeoutEvt = FALSE;
3005
3006 sm->TimeoutCtr++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003007 if (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3008 sm->TimeoutCtr > 1) {
3009 /* Do not allow retransmission of EAPOL-Key msg 3/4 */
3010 return;
3011 }
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003012 if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003013 /* No point in sending the EAPOL-Key - we will disconnect
3014 * immediately following this. */
3015 return;
3016 }
3017
3018 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
3019 GTK[GN], IGTK, [FTIE], [TIE * 2])
3020 */
3021 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
3022 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
3023 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
3024 wpa_ie = sm->wpa_auth->wpa_ie;
3025 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
3026 if (sm->wpa == WPA_VERSION_WPA &&
3027 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
3028 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003029 /* WPA-only STA, remove RSN IE and possible MDIE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003030 wpa_ie = wpa_ie + wpa_ie[1] + 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003031 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
3032 wpa_ie = wpa_ie + wpa_ie[1] + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003033 wpa_ie_len = wpa_ie[1] + 2;
3034 }
3035 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3036 "sending 3/4 msg of 4-Way Handshake");
3037 if (sm->wpa == WPA_VERSION_WPA2) {
3038 /* WPA2 send GTK in the 4-way handshake */
3039 secure = 1;
3040 gtk = gsm->GTK[gsm->GN - 1];
3041 gtk_len = gsm->GTK_len;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003042 if (sm->wpa_auth->conf.disable_gtk ||
3043 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003044 /*
3045 * Provide unique random GTK to each STA to prevent use
3046 * of GTK in the BSS.
3047 */
3048 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
3049 return;
3050 gtk = dummy_gtk;
3051 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003052 keyidx = gsm->GN;
3053 _rsc = rsc;
3054 encr = 1;
3055 } else {
3056 /* WPA does not include GTK in msg 3/4 */
3057 secure = 0;
3058 gtk = NULL;
3059 gtk_len = 0;
3060 keyidx = 0;
3061 _rsc = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003062 if (sm->rx_eapol_key_secure) {
3063 /*
3064 * It looks like Windows 7 supplicant tries to use
3065 * Secure bit in msg 2/4 after having reported Michael
3066 * MIC failure and it then rejects the 4-way handshake
3067 * if msg 3/4 does not set Secure bit. Work around this
3068 * by setting the Secure bit here even in the case of
3069 * WPA if the supplicant used it first.
3070 */
3071 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3072 "STA used Secure bit in WPA msg 2/4 - "
3073 "set Secure for 3/4 as workaround");
3074 secure = 1;
3075 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003076 }
3077
Hai Shalom39bc25d2019-02-06 16:32:13 -08003078 kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003079 if (gtk)
3080 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003081#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003082 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3083 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
3084 kde_len += 300; /* FTIE + 2 * TIE */
3085 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003086#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003087#ifdef CONFIG_P2P
3088 if (WPA_GET_BE32(sm->ip_addr) > 0)
3089 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
3090#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003091 kde = os_malloc(kde_len);
3092 if (kde == NULL)
3093 return;
3094
3095 pos = kde;
3096 os_memcpy(pos, wpa_ie, wpa_ie_len);
3097 pos += wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003098#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003099 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003100 int res;
3101 size_t elen;
3102
3103 elen = pos - kde;
3104 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003105 if (res < 0) {
3106 wpa_printf(MSG_ERROR, "FT: Failed to insert "
3107 "PMKR1Name into RSN IE in EAPOL-Key data");
3108 os_free(kde);
3109 return;
3110 }
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003111 pos -= wpa_ie_len;
3112 pos += elen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003113 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003114#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003115 if (gtk) {
3116 u8 hdr[2];
3117 hdr[0] = keyidx & 0x03;
3118 hdr[1] = 0;
3119 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
3120 gtk, gtk_len);
3121 }
3122 pos = ieee80211w_kde_add(sm, pos);
Hai Shalom39bc25d2019-02-06 16:32:13 -08003123 if (ocv_oci_add(sm, &pos) < 0) {
3124 os_free(kde);
3125 return;
3126 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003127
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003128#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003129 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3130 int res;
3131 struct wpa_auth_config *conf;
3132
3133 conf = &sm->wpa_auth->conf;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003134 if (sm->assoc_resp_ftie &&
3135 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
3136 os_memcpy(pos, sm->assoc_resp_ftie,
3137 2 + sm->assoc_resp_ftie[1]);
3138 res = 2 + sm->assoc_resp_ftie[1];
3139 } else {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003140 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
3141
3142 res = wpa_write_ftie(conf, use_sha384,
3143 conf->r0_key_holder,
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003144 conf->r0_key_holder_len,
3145 NULL, NULL, pos,
3146 kde + kde_len - pos,
3147 NULL, 0);
3148 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003149 if (res < 0) {
3150 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
3151 "into EAPOL-Key Key Data");
3152 os_free(kde);
3153 return;
3154 }
3155 pos += res;
3156
3157 /* TIE[ReassociationDeadline] (TU) */
3158 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
3159 *pos++ = 5;
3160 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
3161 WPA_PUT_LE32(pos, conf->reassociation_deadline);
3162 pos += 4;
3163
3164 /* TIE[KeyLifetime] (seconds) */
3165 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
3166 *pos++ = 5;
3167 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003168 WPA_PUT_LE32(pos, conf->r0_key_lifetime);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003169 pos += 4;
3170 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003171#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003172#ifdef CONFIG_P2P
3173 if (WPA_GET_BE32(sm->ip_addr) > 0) {
3174 u8 addr[3 * 4];
3175 os_memcpy(addr, sm->ip_addr, 4);
3176 os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
3177 os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
3178 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
3179 addr, sizeof(addr), NULL, 0);
3180 }
3181#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003182
3183 wpa_send_eapol(sm->wpa_auth, sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003184 (secure ? WPA_KEY_INFO_SECURE : 0) |
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003185 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
3186 WPA_KEY_INFO_MIC : 0) |
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003187 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
3188 WPA_KEY_INFO_KEY_TYPE,
3189 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
3190 os_free(kde);
3191}
3192
3193
3194SM_STATE(WPA_PTK, PTKINITDONE)
3195{
3196 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
3197 sm->EAPOLKeyReceived = FALSE;
3198 if (sm->Pair) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003199 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
3200 int klen = wpa_cipher_key_len(sm->pairwise);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003201 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003202 sm->PTK.tk, klen)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003203 wpa_sta_disconnect(sm->wpa_auth, sm->addr,
3204 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003205 return;
3206 }
3207 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
3208 sm->pairwise_set = TRUE;
3209
3210 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
3211 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
3212 eloop_register_timeout(sm->wpa_auth->conf.
3213 wpa_ptk_rekey, 0, wpa_rekey_ptk,
3214 sm->wpa_auth, sm);
3215 }
3216
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003217 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
3218 sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP ||
3219 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
3221 WPA_EAPOL_authorized, 1);
3222 }
3223 }
3224
3225 if (0 /* IBSS == TRUE */) {
3226 sm->keycount++;
3227 if (sm->keycount == 2) {
3228 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
3229 WPA_EAPOL_portValid, 1);
3230 }
3231 } else {
3232 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
3233 1);
3234 }
3235 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
3236 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
3237 if (sm->wpa == WPA_VERSION_WPA)
3238 sm->PInitAKeys = TRUE;
3239 else
3240 sm->has_GTK = TRUE;
3241 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3242 "pairwise key handshake completed (%s)",
3243 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
3244
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003245#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003246 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003247#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003248}
3249
3250
3251SM_STEP(WPA_PTK)
3252{
3253 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3254
3255 if (sm->Init)
3256 SM_ENTER(WPA_PTK, INITIALIZE);
3257 else if (sm->Disconnect
3258 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
3259 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
3260 "WPA_PTK: sm->Disconnect");
3261 SM_ENTER(WPA_PTK, DISCONNECT);
3262 }
3263 else if (sm->DeauthenticationRequest)
3264 SM_ENTER(WPA_PTK, DISCONNECTED);
3265 else if (sm->AuthenticationRequest)
3266 SM_ENTER(WPA_PTK, AUTHENTICATION);
3267 else if (sm->ReAuthenticationRequest)
3268 SM_ENTER(WPA_PTK, AUTHENTICATION2);
Jouni Malinen1420a892017-10-01 12:32:57 +03003269 else if (sm->PTKRequest) {
3270 if (wpa_auth_sm_ptk_update(sm) < 0)
3271 SM_ENTER(WPA_PTK, DISCONNECTED);
3272 else
3273 SM_ENTER(WPA_PTK, PTKSTART);
3274 } else switch (sm->wpa_ptk_state) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275 case WPA_PTK_INITIALIZE:
3276 break;
3277 case WPA_PTK_DISCONNECT:
3278 SM_ENTER(WPA_PTK, DISCONNECTED);
3279 break;
3280 case WPA_PTK_DISCONNECTED:
3281 SM_ENTER(WPA_PTK, INITIALIZE);
3282 break;
3283 case WPA_PTK_AUTHENTICATION:
3284 SM_ENTER(WPA_PTK, AUTHENTICATION2);
3285 break;
3286 case WPA_PTK_AUTHENTICATION2:
3287 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
3288 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
3289 WPA_EAPOL_keyRun) > 0)
3290 SM_ENTER(WPA_PTK, INITPMK);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003291 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
3292 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003293 /* FIX: && 802.1X::keyRun */)
3294 SM_ENTER(WPA_PTK, INITPSK);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003295 else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP)
3296 SM_ENTER(WPA_PTK, INITPMK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003297 break;
3298 case WPA_PTK_INITPMK:
3299 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003300 WPA_EAPOL_keyAvailable) > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003301 SM_ENTER(WPA_PTK, PTKSTART);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003302#ifdef CONFIG_DPP
3303 } else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && sm->pmksa) {
3304 SM_ENTER(WPA_PTK, PTKSTART);
3305#endif /* CONFIG_DPP */
3306 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3308 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3309 "INITPMK - keyAvailable = false");
3310 SM_ENTER(WPA_PTK, DISCONNECT);
3311 }
3312 break;
3313 case WPA_PTK_INITPSK:
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003314 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003315 NULL, NULL)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003316 SM_ENTER(WPA_PTK, PTKSTART);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003317#ifdef CONFIG_SAE
3318 } else if (wpa_auth_uses_sae(sm) && sm->pmksa) {
3319 SM_ENTER(WPA_PTK, PTKSTART);
3320#endif /* CONFIG_SAE */
3321 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003322 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3323 "no PSK configured for the STA");
3324 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3325 SM_ENTER(WPA_PTK, DISCONNECT);
3326 }
3327 break;
3328 case WPA_PTK_PTKSTART:
3329 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3330 sm->EAPOLKeyPairwise)
3331 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3332 else if (sm->TimeoutCtr >
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003333 sm->wpa_auth->conf.wpa_pairwise_update_count) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003334 wpa_auth->dot11RSNA4WayHandshakeFailures++;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003335 wpa_auth_vlogger(
3336 sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3337 "PTKSTART: Retry limit %u reached",
3338 sm->wpa_auth->conf.wpa_pairwise_update_count);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003339 SM_ENTER(WPA_PTK, DISCONNECT);
3340 } else if (sm->TimeoutEvt)
3341 SM_ENTER(WPA_PTK, PTKSTART);
3342 break;
3343 case WPA_PTK_PTKCALCNEGOTIATING:
3344 if (sm->MICVerified)
3345 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
3346 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3347 sm->EAPOLKeyPairwise)
3348 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3349 else if (sm->TimeoutEvt)
3350 SM_ENTER(WPA_PTK, PTKSTART);
3351 break;
3352 case WPA_PTK_PTKCALCNEGOTIATING2:
3353 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3354 break;
3355 case WPA_PTK_PTKINITNEGOTIATING:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003356 if (sm->update_snonce)
3357 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3358 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3359 sm->EAPOLKeyPairwise && sm->MICVerified)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003360 SM_ENTER(WPA_PTK, PTKINITDONE);
3361 else if (sm->TimeoutCtr >
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003362 sm->wpa_auth->conf.wpa_pairwise_update_count ||
3363 (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3364 sm->TimeoutCtr > 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003365 wpa_auth->dot11RSNA4WayHandshakeFailures++;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003366 wpa_auth_vlogger(
3367 sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3368 "PTKINITNEGOTIATING: Retry limit %u reached",
3369 sm->wpa_auth->conf.wpa_pairwise_update_count);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003370 SM_ENTER(WPA_PTK, DISCONNECT);
3371 } else if (sm->TimeoutEvt)
3372 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3373 break;
3374 case WPA_PTK_PTKINITDONE:
3375 break;
3376 }
3377}
3378
3379
3380SM_STATE(WPA_PTK_GROUP, IDLE)
3381{
3382 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
3383 if (sm->Init) {
3384 /* Init flag is not cleared here, so avoid busy
3385 * loop by claiming nothing changed. */
3386 sm->changed = FALSE;
3387 }
3388 sm->GTimeoutCtr = 0;
3389}
3390
3391
3392SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
3393{
3394 u8 rsc[WPA_KEY_RSC_LEN];
3395 struct wpa_group *gsm = sm->group;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003396 const u8 *kde;
3397 u8 *kde_buf = NULL, *pos, hdr[2];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003398 size_t kde_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003399 u8 *gtk, dummy_gtk[32];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003400
3401 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
3402
3403 sm->GTimeoutCtr++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003404 if (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3405 sm->GTimeoutCtr > 1) {
3406 /* Do not allow retransmission of EAPOL-Key group msg 1/2 */
3407 return;
3408 }
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003409 if (sm->GTimeoutCtr > sm->wpa_auth->conf.wpa_group_update_count) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003410 /* No point in sending the EAPOL-Key - we will disconnect
3411 * immediately following this. */
3412 return;
3413 }
3414
3415 if (sm->wpa == WPA_VERSION_WPA)
3416 sm->PInitAKeys = FALSE;
3417 sm->TimeoutEvt = FALSE;
3418 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
3419 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
3420 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
3421 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
3422 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3423 "sending 1/2 msg of Group Key Handshake");
3424
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003425 gtk = gsm->GTK[gsm->GN - 1];
Roshan Pius3a1667e2018-07-03 15:17:14 -07003426 if (sm->wpa_auth->conf.disable_gtk ||
3427 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003428 /*
3429 * Provide unique random GTK to each STA to prevent use
3430 * of GTK in the BSS.
3431 */
3432 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
3433 return;
3434 gtk = dummy_gtk;
3435 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003436 if (sm->wpa == WPA_VERSION_WPA2) {
3437 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
Hai Shalom39bc25d2019-02-06 16:32:13 -08003438 ieee80211w_kde_len(sm) + ocv_oci_len(sm);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003439 kde_buf = os_malloc(kde_len);
3440 if (kde_buf == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003441 return;
3442
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003443 kde = pos = kde_buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003444 hdr[0] = gsm->GN & 0x03;
3445 hdr[1] = 0;
3446 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003447 gtk, gsm->GTK_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003448 pos = ieee80211w_kde_add(sm, pos);
Hai Shalom39bc25d2019-02-06 16:32:13 -08003449 if (ocv_oci_add(sm, &pos) < 0) {
3450 os_free(kde_buf);
3451 return;
3452 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003453 kde_len = pos - kde;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003454 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003455 kde = gtk;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003456 kde_len = gsm->GTK_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003457 }
3458
3459 wpa_send_eapol(sm->wpa_auth, sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003460 WPA_KEY_INFO_SECURE |
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003461 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
3462 WPA_KEY_INFO_MIC : 0) |
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003463 WPA_KEY_INFO_ACK |
3464 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003465 rsc, NULL, kde, kde_len, gsm->GN, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003466
3467 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003468}
3469
3470
3471SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
3472{
Hai Shalom39bc25d2019-02-06 16:32:13 -08003473#ifdef CONFIG_OCV
3474 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3475 const u8 *key_data, *mic;
3476 struct ieee802_1x_hdr *hdr;
3477 struct wpa_eapol_key *key;
3478 struct wpa_eapol_ie_parse kde;
3479 size_t mic_len;
3480 u16 key_data_length;
3481#endif /* CONFIG_OCV */
3482
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003483 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
3484 sm->EAPOLKeyReceived = FALSE;
Hai Shalom39bc25d2019-02-06 16:32:13 -08003485
3486#ifdef CONFIG_OCV
3487 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
3488
3489 /*
3490 * Note: last_rx_eapol_key length fields have already been validated in
3491 * wpa_receive().
3492 */
3493 hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
3494 key = (struct wpa_eapol_key *) (hdr + 1);
3495 mic = (u8 *) (key + 1);
3496 key_data = mic + mic_len + 2;
3497 key_data_length = WPA_GET_BE16(mic + mic_len);
3498 if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
3499 sizeof(*key) - mic_len - 2)
3500 return;
3501
3502 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
3503 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
3504 "received EAPOL-Key group msg 2/2 with invalid Key Data contents");
3505 return;
3506 }
3507
3508 if (wpa_auth_uses_ocv(sm)) {
3509 struct wpa_channel_info ci;
3510 int tx_chanwidth;
3511 int tx_seg1_idx;
3512
3513 if (wpa_channel_info(wpa_auth, &ci) != 0) {
3514 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
3515 "Failed to get channel info to validate received OCI in EAPOL-Key group 1/2");
3516 return;
3517 }
3518
3519 if (get_sta_tx_parameters(sm,
3520 channel_width_to_int(ci.chanwidth),
3521 ci.seg1_idx, &tx_chanwidth,
3522 &tx_seg1_idx) < 0)
3523 return;
3524
3525 if (ocv_verify_tx_params(kde.oci, kde.oci_len, &ci,
3526 tx_chanwidth, tx_seg1_idx) != 0) {
3527 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
3528 ocv_errorstr);
3529 return;
3530 }
3531 }
3532#endif /* CONFIG_OCV */
3533
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003534 if (sm->GUpdateStationKeys)
3535 sm->group->GKeyDoneStations--;
3536 sm->GUpdateStationKeys = FALSE;
3537 sm->GTimeoutCtr = 0;
3538 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
3539 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3540 "group key handshake completed (%s)",
3541 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
3542 sm->has_GTK = TRUE;
3543}
3544
3545
3546SM_STATE(WPA_PTK_GROUP, KEYERROR)
3547{
3548 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
3549 if (sm->GUpdateStationKeys)
3550 sm->group->GKeyDoneStations--;
3551 sm->GUpdateStationKeys = FALSE;
3552 sm->Disconnect = TRUE;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003553 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3554 "group key handshake failed (%s) after %u tries",
3555 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN",
3556 sm->wpa_auth->conf.wpa_group_update_count);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003557}
3558
3559
3560SM_STEP(WPA_PTK_GROUP)
3561{
3562 if (sm->Init || sm->PtkGroupInit) {
3563 SM_ENTER(WPA_PTK_GROUP, IDLE);
3564 sm->PtkGroupInit = FALSE;
3565 } else switch (sm->wpa_ptk_group_state) {
3566 case WPA_PTK_GROUP_IDLE:
3567 if (sm->GUpdateStationKeys ||
3568 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
3569 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3570 break;
3571 case WPA_PTK_GROUP_REKEYNEGOTIATING:
3572 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3573 !sm->EAPOLKeyPairwise && sm->MICVerified)
3574 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
3575 else if (sm->GTimeoutCtr >
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003576 sm->wpa_auth->conf.wpa_group_update_count ||
3577 (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3578 sm->GTimeoutCtr > 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003579 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
3580 else if (sm->TimeoutEvt)
3581 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3582 break;
3583 case WPA_PTK_GROUP_KEYERROR:
3584 SM_ENTER(WPA_PTK_GROUP, IDLE);
3585 break;
3586 case WPA_PTK_GROUP_REKEYESTABLISHED:
3587 SM_ENTER(WPA_PTK_GROUP, IDLE);
3588 break;
3589 }
3590}
3591
3592
3593static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
3594 struct wpa_group *group)
3595{
3596 int ret = 0;
3597
3598 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3599 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3600 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
3601 wpa_auth->addr, group->GNonce,
3602 group->GTK[group->GN - 1], group->GTK_len) < 0)
3603 ret = -1;
3604 wpa_hexdump_key(MSG_DEBUG, "GTK",
3605 group->GTK[group->GN - 1], group->GTK_len);
3606
3607#ifdef CONFIG_IEEE80211W
3608 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003609 size_t len;
3610 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003611 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3612 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3613 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
3614 wpa_auth->addr, group->GNonce,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003615 group->IGTK[group->GN_igtk - 4], len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003616 ret = -1;
3617 wpa_hexdump_key(MSG_DEBUG, "IGTK",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003618 group->IGTK[group->GN_igtk - 4], len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003619 }
3620#endif /* CONFIG_IEEE80211W */
3621
3622 return ret;
3623}
3624
3625
3626static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
3627 struct wpa_group *group)
3628{
3629 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3630 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
3631 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
3632 group->wpa_group_state = WPA_GROUP_GTK_INIT;
3633
3634 /* GTK[0..N] = 0 */
3635 os_memset(group->GTK, 0, sizeof(group->GTK));
3636 group->GN = 1;
3637 group->GM = 2;
3638#ifdef CONFIG_IEEE80211W
3639 group->GN_igtk = 4;
3640 group->GM_igtk = 5;
3641#endif /* CONFIG_IEEE80211W */
3642 /* GTK[GN] = CalcGTK() */
3643 wpa_gtk_update(wpa_auth, group);
3644}
3645
3646
3647static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
3648{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003649 if (ctx != NULL && ctx != sm->group)
3650 return 0;
3651
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003652 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
3653 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3654 "Not in PTKINITDONE; skip Group Key update");
3655 sm->GUpdateStationKeys = FALSE;
3656 return 0;
3657 }
3658 if (sm->GUpdateStationKeys) {
3659 /*
3660 * This should not really happen, so add a debug log entry.
3661 * Since we clear the GKeyDoneStations before the loop, the
3662 * station needs to be counted here anyway.
3663 */
3664 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3665 "GUpdateStationKeys was already set when "
3666 "marking station for GTK rekeying");
3667 }
3668
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003669 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003670 if (sm->is_wnmsleep)
3671 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003673 sm->group->GKeyDoneStations++;
3674 sm->GUpdateStationKeys = TRUE;
3675
3676 wpa_sm_step(sm);
3677 return 0;
3678}
3679
3680
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003681#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003682/* update GTK when exiting WNM-Sleep Mode */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003683void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
3684{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003685 if (sm == NULL || sm->is_wnmsleep)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003686 return;
3687
3688 wpa_group_update_sta(sm, NULL);
3689}
3690
3691
3692void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
3693{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003694 if (sm)
3695 sm->is_wnmsleep = !!flag;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003696}
3697
3698
3699int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3700{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003701 struct wpa_group *gsm = sm->group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003702 u8 *start = pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003703
3704 /*
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003705 * GTK subelement:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003706 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003707 * Key[5..32]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003708 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003709 *pos++ = WNM_SLEEP_SUBELEM_GTK;
3710 *pos++ = 11 + gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003711 /* Key ID in B0-B1 of Key Info */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003712 WPA_PUT_LE16(pos, gsm->GN & 0x03);
3713 pos += 2;
3714 *pos++ = gsm->GTK_len;
3715 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003716 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003717 pos += 8;
3718 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
3719 pos += gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003720
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003721 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
3722 gsm->GN);
3723 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003724 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003725
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003726 return pos - start;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003727}
3728
3729
3730#ifdef CONFIG_IEEE80211W
3731int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3732{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003733 struct wpa_group *gsm = sm->group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003734 u8 *start = pos;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003735 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003736
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003737 /*
3738 * IGTK subelement:
3739 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
3740 */
3741 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003742 *pos++ = 2 + 6 + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003743 WPA_PUT_LE16(pos, gsm->GN_igtk);
3744 pos += 2;
3745 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003746 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003747 pos += 6;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003748
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003749 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
3750 pos += len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003751
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003752 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
3753 gsm->GN_igtk);
3754 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003755 gsm->IGTK[gsm->GN_igtk - 4], len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003756
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003757 return pos - start;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003758}
3759#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003760#endif /* CONFIG_WNM_AP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003761
3762
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003763static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
3764 struct wpa_group *group)
3765{
3766 int tmp;
3767
3768 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3769 "SETKEYS (VLAN-ID %d)", group->vlan_id);
3770 group->changed = TRUE;
3771 group->wpa_group_state = WPA_GROUP_SETKEYS;
3772 group->GTKReKey = FALSE;
3773 tmp = group->GM;
3774 group->GM = group->GN;
3775 group->GN = tmp;
3776#ifdef CONFIG_IEEE80211W
3777 tmp = group->GM_igtk;
3778 group->GM_igtk = group->GN_igtk;
3779 group->GN_igtk = tmp;
3780#endif /* CONFIG_IEEE80211W */
3781 /* "GKeyDoneStations = GNoStations" is done in more robust way by
3782 * counting the STAs that are marked with GUpdateStationKeys instead of
3783 * including all STAs that could be in not-yet-completed state. */
3784 wpa_gtk_update(wpa_auth, group);
3785
3786 if (group->GKeyDoneStations) {
3787 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
3788 "GKeyDoneStations=%d when starting new GTK rekey",
3789 group->GKeyDoneStations);
3790 group->GKeyDoneStations = 0;
3791 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003792 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003793 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
3794 group->GKeyDoneStations);
3795}
3796
3797
3798static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
3799 struct wpa_group *group)
3800{
3801 int ret = 0;
3802
3803 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003804 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003805 broadcast_ether_addr, group->GN,
3806 group->GTK[group->GN - 1], group->GTK_len) < 0)
3807 ret = -1;
3808
3809#ifdef CONFIG_IEEE80211W
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003810 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
3811 enum wpa_alg alg;
3812 size_t len;
3813
3814 alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
3815 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
3816
3817 if (ret == 0 &&
3818 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
3819 broadcast_ether_addr, group->GN_igtk,
3820 group->IGTK[group->GN_igtk - 4], len) < 0)
3821 ret = -1;
3822 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003823#endif /* CONFIG_IEEE80211W */
3824
3825 return ret;
3826}
3827
3828
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003829static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
3830{
3831 if (sm->group == ctx) {
3832 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
3833 " for discconnection due to fatal failure",
3834 MAC2STR(sm->addr));
3835 sm->Disconnect = TRUE;
3836 }
3837
3838 return 0;
3839}
3840
3841
3842static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
3843 struct wpa_group *group)
3844{
3845 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
3846 group->changed = TRUE;
3847 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
3848 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
3849}
3850
3851
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003852static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
3853 struct wpa_group *group)
3854{
3855 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3856 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
3857 group->changed = TRUE;
3858 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
3859
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003860 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
3861 wpa_group_fatal_failure(wpa_auth, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003862 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003863 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003864
3865 return 0;
3866}
3867
3868
3869static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
3870 struct wpa_group *group)
3871{
3872 if (group->GInit) {
3873 wpa_group_gtk_init(wpa_auth, group);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003874 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
3875 /* Do not allow group operations */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003876 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
3877 group->GTKAuthenticator) {
3878 wpa_group_setkeysdone(wpa_auth, group);
3879 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
3880 group->GTKReKey) {
3881 wpa_group_setkeys(wpa_auth, group);
3882 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
3883 if (group->GKeyDoneStations == 0)
3884 wpa_group_setkeysdone(wpa_auth, group);
3885 else if (group->GTKReKey)
3886 wpa_group_setkeys(wpa_auth, group);
3887 }
3888}
3889
3890
3891static int wpa_sm_step(struct wpa_state_machine *sm)
3892{
3893 if (sm == NULL)
3894 return 0;
3895
3896 if (sm->in_step_loop) {
3897 /* This should not happen, but if it does, make sure we do not
3898 * end up freeing the state machine too early by exiting the
3899 * recursive call. */
3900 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
3901 return 0;
3902 }
3903
3904 sm->in_step_loop = 1;
3905 do {
3906 if (sm->pending_deinit)
3907 break;
3908
3909 sm->changed = FALSE;
3910 sm->wpa_auth->group->changed = FALSE;
3911
3912 SM_STEP_RUN(WPA_PTK);
3913 if (sm->pending_deinit)
3914 break;
3915 SM_STEP_RUN(WPA_PTK_GROUP);
3916 if (sm->pending_deinit)
3917 break;
3918 wpa_group_sm_step(sm->wpa_auth, sm->group);
3919 } while (sm->changed || sm->wpa_auth->group->changed);
3920 sm->in_step_loop = 0;
3921
3922 if (sm->pending_deinit) {
3923 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
3924 "machine deinit for " MACSTR, MAC2STR(sm->addr));
3925 wpa_free_sta_sm(sm);
3926 return 1;
3927 }
3928 return 0;
3929}
3930
3931
3932static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
3933{
3934 struct wpa_state_machine *sm = eloop_ctx;
3935 wpa_sm_step(sm);
3936}
3937
3938
3939void wpa_auth_sm_notify(struct wpa_state_machine *sm)
3940{
3941 if (sm == NULL)
3942 return;
3943 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
3944}
3945
3946
3947void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
3948{
3949 int tmp, i;
3950 struct wpa_group *group;
3951
3952 if (wpa_auth == NULL)
3953 return;
3954
3955 group = wpa_auth->group;
3956
3957 for (i = 0; i < 2; i++) {
3958 tmp = group->GM;
3959 group->GM = group->GN;
3960 group->GN = tmp;
3961#ifdef CONFIG_IEEE80211W
3962 tmp = group->GM_igtk;
3963 group->GM_igtk = group->GN_igtk;
3964 group->GN_igtk = tmp;
3965#endif /* CONFIG_IEEE80211W */
3966 wpa_gtk_update(wpa_auth, group);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003967 wpa_group_config_group_keys(wpa_auth, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003968 }
3969}
3970
3971
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003972static const char * wpa_bool_txt(int val)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003973{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003974 return val ? "TRUE" : "FALSE";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003975}
3976
3977
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003978#define RSN_SUITE "%02x-%02x-%02x-%d"
3979#define RSN_SUITE_ARG(s) \
3980((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
3981
3982int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
3983{
3984 int len = 0, ret;
3985 char pmkid_txt[PMKID_LEN * 2 + 1];
3986#ifdef CONFIG_RSN_PREAUTH
3987 const int preauth = 1;
3988#else /* CONFIG_RSN_PREAUTH */
3989 const int preauth = 0;
3990#endif /* CONFIG_RSN_PREAUTH */
3991
3992 if (wpa_auth == NULL)
3993 return len;
3994
3995 ret = os_snprintf(buf + len, buflen - len,
3996 "dot11RSNAOptionImplemented=TRUE\n"
3997 "dot11RSNAPreauthenticationImplemented=%s\n"
3998 "dot11RSNAEnabled=%s\n"
3999 "dot11RSNAPreauthenticationEnabled=%s\n",
4000 wpa_bool_txt(preauth),
4001 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
4002 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004003 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004004 return len;
4005 len += ret;
4006
4007 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
4008 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
4009
4010 ret = os_snprintf(
4011 buf + len, buflen - len,
4012 "dot11RSNAConfigVersion=%u\n"
4013 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
4014 /* FIX: dot11RSNAConfigGroupCipher */
4015 /* FIX: dot11RSNAConfigGroupRekeyMethod */
4016 /* FIX: dot11RSNAConfigGroupRekeyTime */
4017 /* FIX: dot11RSNAConfigGroupRekeyPackets */
4018 "dot11RSNAConfigGroupRekeyStrict=%u\n"
4019 "dot11RSNAConfigGroupUpdateCount=%u\n"
4020 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
4021 "dot11RSNAConfigGroupCipherSize=%u\n"
4022 "dot11RSNAConfigPMKLifetime=%u\n"
4023 "dot11RSNAConfigPMKReauthThreshold=%u\n"
4024 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
4025 "dot11RSNAConfigSATimeout=%u\n"
4026 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
4027 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
4028 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
4029 "dot11RSNAPMKIDUsed=%s\n"
4030 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
4031 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
4032 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
4033 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
4034 "dot11RSNA4WayHandshakeFailures=%u\n"
4035 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
4036 RSN_VERSION,
4037 !!wpa_auth->conf.wpa_strict_rekey,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004038 wpa_auth->conf.wpa_group_update_count,
4039 wpa_auth->conf.wpa_pairwise_update_count,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004040 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004041 dot11RSNAConfigPMKLifetime,
4042 dot11RSNAConfigPMKReauthThreshold,
4043 dot11RSNAConfigSATimeout,
4044 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
4045 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
4046 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
4047 pmkid_txt,
4048 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
4049 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
4050 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
4051 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
4052 wpa_auth->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004053 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004054 return len;
4055 len += ret;
4056
4057 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
4058 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
4059
4060 /* Private MIB */
4061 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
4062 wpa_auth->group->wpa_group_state);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004063 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004064 return len;
4065 len += ret;
4066
4067 return len;
4068}
4069
4070
4071int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
4072{
4073 int len = 0, ret;
4074 u32 pairwise = 0;
4075
4076 if (sm == NULL)
4077 return 0;
4078
4079 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
4080
4081 /* dot11RSNAStatsEntry */
4082
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004083 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
4084 WPA_PROTO_RSN : WPA_PROTO_WPA,
4085 sm->pairwise);
4086 if (pairwise == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004087 return 0;
4088
4089 ret = os_snprintf(
4090 buf + len, buflen - len,
4091 /* TODO: dot11RSNAStatsIndex */
4092 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
4093 "dot11RSNAStatsVersion=1\n"
4094 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
4095 /* TODO: dot11RSNAStatsTKIPICVErrors */
4096 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
4097 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
4098 /* TODO: dot11RSNAStatsCCMPReplays */
4099 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
4100 /* TODO: dot11RSNAStatsTKIPReplays */,
4101 MAC2STR(sm->addr),
4102 RSN_SUITE_ARG(pairwise),
4103 sm->dot11RSNAStatsTKIPLocalMICFailures,
4104 sm->dot11RSNAStatsTKIPRemoteMICFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004105 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004106 return len;
4107 len += ret;
4108
4109 /* Private MIB */
4110 ret = os_snprintf(buf + len, buflen - len,
4111 "hostapdWPAPTKState=%d\n"
4112 "hostapdWPAPTKGroupState=%d\n",
4113 sm->wpa_ptk_state,
4114 sm->wpa_ptk_group_state);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004115 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004116 return len;
4117 len += ret;
4118
4119 return len;
4120}
4121
4122
4123void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
4124{
4125 if (wpa_auth)
4126 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
4127}
4128
4129
4130int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
4131{
4132 return sm && sm->pairwise_set;
4133}
4134
4135
4136int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
4137{
4138 return sm->pairwise;
4139}
4140
4141
Hai Shalom39bc25d2019-02-06 16:32:13 -08004142const u8 * wpa_auth_get_pmk(struct wpa_state_machine *sm, int *len)
4143{
4144 if (!sm)
4145 return NULL;
4146 *len = sm->pmk_len;
4147 return sm->PMK;
4148}
4149
4150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004151int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
4152{
4153 if (sm == NULL)
4154 return -1;
4155 return sm->wpa_key_mgmt;
4156}
4157
4158
4159int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
4160{
4161 if (sm == NULL)
4162 return 0;
4163 return sm->wpa;
4164}
4165
4166
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02004167int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm)
4168{
4169 if (!sm || !wpa_key_mgmt_ft(sm->wpa_key_mgmt))
4170 return 0;
4171 return sm->tk_already_set;
4172}
4173
4174
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004175int wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine *sm)
4176{
4177 if (!sm || !wpa_key_mgmt_fils(sm->wpa_key_mgmt))
4178 return 0;
4179 return sm->tk_already_set;
4180}
4181
4182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004183int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
4184 struct rsn_pmksa_cache_entry *entry)
4185{
4186 if (sm == NULL || sm->pmksa != entry)
4187 return -1;
4188 sm->pmksa = NULL;
4189 return 0;
4190}
4191
4192
4193struct rsn_pmksa_cache_entry *
4194wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
4195{
4196 return sm ? sm->pmksa : NULL;
4197}
4198
4199
4200void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
4201{
4202 if (sm)
4203 sm->dot11RSNAStatsTKIPLocalMICFailures++;
4204}
4205
4206
4207const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
4208{
4209 if (wpa_auth == NULL)
4210 return NULL;
4211 *len = wpa_auth->wpa_ie_len;
4212 return wpa_auth->wpa_ie;
4213}
4214
4215
4216int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004217 unsigned int pmk_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004218 int session_timeout, struct eapol_state_machine *eapol)
4219{
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07004220 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
4221 sm->wpa_auth->conf.disable_pmksa_caching)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004222 return -1;
4223
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004224 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004225 if (pmk_len > PMK_LEN_SUITE_B_192)
4226 pmk_len = PMK_LEN_SUITE_B_192;
4227 } else if (pmk_len > PMK_LEN) {
4228 pmk_len = PMK_LEN;
4229 }
4230
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004231 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004232 sm->PTK.kck, sm->PTK.kck_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004233 sm->wpa_auth->addr, sm->addr, session_timeout,
4234 eapol, sm->wpa_key_mgmt))
4235 return 0;
4236
4237 return -1;
4238}
4239
4240
4241int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
4242 const u8 *pmk, size_t len, const u8 *sta_addr,
4243 int session_timeout,
4244 struct eapol_state_machine *eapol)
4245{
4246 if (wpa_auth == NULL)
4247 return -1;
4248
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004249 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, NULL,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004250 NULL, 0,
4251 wpa_auth->addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004252 sta_addr, session_timeout, eapol,
4253 WPA_KEY_MGMT_IEEE8021X))
4254 return 0;
4255
4256 return -1;
4257}
4258
4259
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004260int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004261 const u8 *pmk, const u8 *pmkid)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004262{
4263 if (wpa_auth->conf.disable_pmksa_caching)
4264 return -1;
4265
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004266 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN, pmkid,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004267 NULL, 0,
4268 wpa_auth->addr, addr, 0, NULL,
4269 WPA_KEY_MGMT_SAE))
4270 return 0;
4271
4272 return -1;
4273}
4274
4275
Roshan Pius3a1667e2018-07-03 15:17:14 -07004276void wpa_auth_add_sae_pmkid(struct wpa_state_machine *sm, const u8 *pmkid)
4277{
4278 os_memcpy(sm->pmkid, pmkid, PMKID_LEN);
4279 sm->pmkid_set = 1;
4280}
4281
4282
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004283int wpa_auth_pmksa_add2(struct wpa_authenticator *wpa_auth, const u8 *addr,
4284 const u8 *pmk, size_t pmk_len, const u8 *pmkid,
4285 int session_timeout, int akmp)
4286{
4287 if (wpa_auth->conf.disable_pmksa_caching)
4288 return -1;
4289
4290 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, pmk_len, pmkid,
4291 NULL, 0, wpa_auth->addr, addr, session_timeout,
4292 NULL, akmp))
4293 return 0;
4294
4295 return -1;
4296}
4297
4298
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004299void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
4300 const u8 *sta_addr)
4301{
4302 struct rsn_pmksa_cache_entry *pmksa;
4303
4304 if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
4305 return;
4306 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
4307 if (pmksa) {
4308 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
4309 MACSTR " based on request", MAC2STR(sta_addr));
4310 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
4311 }
4312}
4313
4314
Dmitry Shmidte4663042016-04-04 10:07:49 -07004315int wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf,
4316 size_t len)
4317{
4318 if (!wpa_auth || !wpa_auth->pmksa)
4319 return 0;
4320 return pmksa_cache_auth_list(wpa_auth->pmksa, buf, len);
4321}
4322
4323
4324void wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth)
4325{
4326 if (wpa_auth && wpa_auth->pmksa)
4327 pmksa_cache_auth_flush(wpa_auth->pmksa);
4328}
4329
4330
Paul Stewart092955c2017-02-06 09:13:09 -08004331#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
4332#ifdef CONFIG_MESH
4333
4334int wpa_auth_pmksa_list_mesh(struct wpa_authenticator *wpa_auth, const u8 *addr,
4335 char *buf, size_t len)
4336{
4337 if (!wpa_auth || !wpa_auth->pmksa)
4338 return 0;
4339
4340 return pmksa_cache_auth_list_mesh(wpa_auth->pmksa, addr, buf, len);
4341}
4342
4343
4344struct rsn_pmksa_cache_entry *
4345wpa_auth_pmksa_create_entry(const u8 *aa, const u8 *spa, const u8 *pmk,
4346 const u8 *pmkid, int expiration)
4347{
4348 struct rsn_pmksa_cache_entry *entry;
4349 struct os_reltime now;
4350
4351 entry = pmksa_cache_auth_create_entry(pmk, PMK_LEN, pmkid, NULL, 0, aa,
4352 spa, 0, NULL, WPA_KEY_MGMT_SAE);
4353 if (!entry)
4354 return NULL;
4355
4356 os_get_reltime(&now);
4357 entry->expiration = now.sec + expiration;
4358 return entry;
4359}
4360
4361
4362int wpa_auth_pmksa_add_entry(struct wpa_authenticator *wpa_auth,
4363 struct rsn_pmksa_cache_entry *entry)
4364{
4365 int ret;
4366
4367 if (!wpa_auth || !wpa_auth->pmksa)
4368 return -1;
4369
4370 ret = pmksa_cache_auth_add_entry(wpa_auth->pmksa, entry);
4371 if (ret < 0)
4372 wpa_printf(MSG_DEBUG,
4373 "RSN: Failed to store external PMKSA cache for "
4374 MACSTR, MAC2STR(entry->spa));
4375
4376 return ret;
4377}
4378
4379#endif /* CONFIG_MESH */
4380#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
4381
4382
Dmitry Shmidte4663042016-04-04 10:07:49 -07004383struct rsn_pmksa_cache_entry *
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004384wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
4385 const u8 *pmkid)
Dmitry Shmidte4663042016-04-04 10:07:49 -07004386{
4387 if (!wpa_auth || !wpa_auth->pmksa)
4388 return NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004389 return pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, pmkid);
Dmitry Shmidte4663042016-04-04 10:07:49 -07004390}
4391
4392
4393void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa,
4394 struct wpa_state_machine *sm,
4395 struct wpa_authenticator *wpa_auth,
4396 u8 *pmkid, u8 *pmk)
4397{
4398 if (!sm)
4399 return;
4400
4401 sm->pmksa = pmksa;
4402 os_memcpy(pmk, pmksa->pmk, PMK_LEN);
4403 os_memcpy(pmkid, pmksa->pmkid, PMKID_LEN);
4404 os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmksa->pmkid, PMKID_LEN);
4405}
4406
4407
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004408/*
4409 * Remove and free the group from wpa_authenticator. This is triggered by a
4410 * callback to make sure nobody is currently iterating the group list while it
4411 * gets modified.
4412 */
4413static void wpa_group_free(struct wpa_authenticator *wpa_auth,
4414 struct wpa_group *group)
4415{
4416 struct wpa_group *prev = wpa_auth->group;
4417
4418 wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d",
4419 group->vlan_id);
4420
4421 while (prev) {
4422 if (prev->next == group) {
4423 /* This never frees the special first group as needed */
4424 prev->next = group->next;
4425 os_free(group);
4426 break;
4427 }
4428 prev = prev->next;
4429 }
4430
4431}
4432
4433
4434/* Increase the reference counter for group */
4435static void wpa_group_get(struct wpa_authenticator *wpa_auth,
4436 struct wpa_group *group)
4437{
4438 /* Skip the special first group */
4439 if (wpa_auth->group == group)
4440 return;
4441
4442 group->references++;
4443}
4444
4445
4446/* Decrease the reference counter and maybe free the group */
4447static void wpa_group_put(struct wpa_authenticator *wpa_auth,
4448 struct wpa_group *group)
4449{
4450 /* Skip the special first group */
4451 if (wpa_auth->group == group)
4452 return;
4453
4454 group->references--;
4455 if (group->references)
4456 return;
4457 wpa_group_free(wpa_auth, group);
4458}
4459
4460
4461/*
4462 * Add a group that has its references counter set to zero. Caller needs to
4463 * call wpa_group_get() on the return value to mark the entry in use.
4464 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004465static struct wpa_group *
4466wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4467{
4468 struct wpa_group *group;
4469
4470 if (wpa_auth == NULL || wpa_auth->group == NULL)
4471 return NULL;
4472
4473 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
4474 vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004475 group = wpa_group_init(wpa_auth, vlan_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004476 if (group == NULL)
4477 return NULL;
4478
4479 group->next = wpa_auth->group->next;
4480 wpa_auth->group->next = group;
4481
4482 return group;
4483}
4484
4485
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004486/*
4487 * Enforce that the group state machine for the VLAN is running, increase
4488 * reference counter as interface is up. References might have been increased
4489 * even if a negative value is returned.
4490 * Returns: -1 on error (group missing, group already failed); otherwise, 0
4491 */
4492int wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4493{
4494 struct wpa_group *group;
4495
4496 if (wpa_auth == NULL)
4497 return 0;
4498
4499 group = wpa_auth->group;
4500 while (group) {
4501 if (group->vlan_id == vlan_id)
4502 break;
4503 group = group->next;
4504 }
4505
4506 if (group == NULL) {
4507 group = wpa_auth_add_group(wpa_auth, vlan_id);
4508 if (group == NULL)
4509 return -1;
4510 }
4511
4512 wpa_printf(MSG_DEBUG,
4513 "WPA: Ensure group state machine running for VLAN ID %d",
4514 vlan_id);
4515
4516 wpa_group_get(wpa_auth, group);
4517 group->num_setup_iface++;
4518
4519 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4520 return -1;
4521
4522 return 0;
4523}
4524
4525
4526/*
4527 * Decrease reference counter, expected to be zero afterwards.
4528 * returns: -1 on error (group not found, group in fail state)
4529 * -2 if wpa_group is still referenced
4530 * 0 else
4531 */
4532int wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4533{
4534 struct wpa_group *group;
4535 int ret = 0;
4536
4537 if (wpa_auth == NULL)
4538 return 0;
4539
4540 group = wpa_auth->group;
4541 while (group) {
4542 if (group->vlan_id == vlan_id)
4543 break;
4544 group = group->next;
4545 }
4546
4547 if (group == NULL)
4548 return -1;
4549
4550 wpa_printf(MSG_DEBUG,
4551 "WPA: Try stopping group state machine for VLAN ID %d",
4552 vlan_id);
4553
4554 if (group->num_setup_iface <= 0) {
4555 wpa_printf(MSG_ERROR,
4556 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
4557 vlan_id);
4558 return -1;
4559 }
4560 group->num_setup_iface--;
4561
4562 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4563 ret = -1;
4564
4565 if (group->references > 1) {
4566 wpa_printf(MSG_DEBUG,
4567 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
4568 vlan_id);
4569 ret = -2;
4570 }
4571
4572 wpa_group_put(wpa_auth, group);
4573
4574 return ret;
4575}
4576
4577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004578int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
4579{
4580 struct wpa_group *group;
4581
4582 if (sm == NULL || sm->wpa_auth == NULL)
4583 return 0;
4584
4585 group = sm->wpa_auth->group;
4586 while (group) {
4587 if (group->vlan_id == vlan_id)
4588 break;
4589 group = group->next;
4590 }
4591
4592 if (group == NULL) {
4593 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
4594 if (group == NULL)
4595 return -1;
4596 }
4597
4598 if (sm->group == group)
4599 return 0;
4600
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004601 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4602 return -1;
4603
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004604 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
4605 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
4606
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004607 wpa_group_get(sm->wpa_auth, group);
4608 wpa_group_put(sm->wpa_auth, sm->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004609 sm->group = group;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004611 return 0;
4612}
4613
4614
4615void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
4616 struct wpa_state_machine *sm, int ack)
4617{
4618 if (wpa_auth == NULL || sm == NULL)
4619 return;
4620 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
4621 " ack=%d", MAC2STR(sm->addr), ack);
4622 if (sm->pending_1_of_4_timeout && ack) {
4623 /*
4624 * Some deployed supplicant implementations update their SNonce
4625 * for each EAPOL-Key 2/4 message even within the same 4-way
4626 * handshake and then fail to use the first SNonce when
4627 * deriving the PTK. This results in unsuccessful 4-way
4628 * handshake whenever the relatively short initial timeout is
4629 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
4630 * around this by increasing the timeout now that we know that
4631 * the station has received the frame.
4632 */
4633 int timeout_ms = eapol_key_timeout_subseq;
4634 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
4635 "timeout by %u ms because of acknowledged frame",
4636 timeout_ms);
4637 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
4638 eloop_register_timeout(timeout_ms / 1000,
4639 (timeout_ms % 1000) * 1000,
4640 wpa_send_eapol_timeout, wpa_auth, sm);
4641 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004642
4643#ifdef CONFIG_TESTING_OPTIONS
4644 if (sm->eapol_status_cb) {
4645 sm->eapol_status_cb(sm->eapol_status_cb_ctx1,
4646 sm->eapol_status_cb_ctx2);
4647 sm->eapol_status_cb = NULL;
4648 }
4649#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004650}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004651
4652
4653int wpa_auth_uses_sae(struct wpa_state_machine *sm)
4654{
4655 if (sm == NULL)
4656 return 0;
4657 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
4658}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004659
4660
4661int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
4662{
4663 if (sm == NULL)
4664 return 0;
4665 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
4666}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004667
4668
4669#ifdef CONFIG_P2P
4670int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
4671{
4672 if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
4673 return -1;
4674 os_memcpy(addr, sm->ip_addr, 4);
4675 return 0;
4676}
4677#endif /* CONFIG_P2P */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004678
4679
4680int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
4681 struct radius_das_attrs *attr)
4682{
4683 return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
4684}
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004685
4686
4687void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)
4688{
4689 struct wpa_group *group;
4690
4691 if (!wpa_auth)
4692 return;
4693 for (group = wpa_auth->group; group; group = group->next)
4694 wpa_group_config_group_keys(wpa_auth, group);
4695}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004696
4697
4698#ifdef CONFIG_FILS
4699
4700struct wpa_auth_fils_iter_data {
4701 struct wpa_authenticator *auth;
4702 const u8 *cache_id;
4703 struct rsn_pmksa_cache_entry *pmksa;
4704 const u8 *spa;
4705 const u8 *pmkid;
4706};
4707
4708
4709static int wpa_auth_fils_iter(struct wpa_authenticator *a, void *ctx)
4710{
4711 struct wpa_auth_fils_iter_data *data = ctx;
4712
4713 if (a == data->auth || !a->conf.fils_cache_id_set ||
4714 os_memcmp(a->conf.fils_cache_id, data->cache_id,
4715 FILS_CACHE_ID_LEN) != 0)
4716 return 0;
4717 data->pmksa = pmksa_cache_auth_get(a->pmksa, data->spa, data->pmkid);
4718 return data->pmksa != NULL;
4719}
4720
4721
4722struct rsn_pmksa_cache_entry *
4723wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator *wpa_auth,
4724 const u8 *sta_addr, const u8 *pmkid)
4725{
4726 struct wpa_auth_fils_iter_data idata;
4727
4728 if (!wpa_auth->conf.fils_cache_id_set)
4729 return NULL;
4730 idata.auth = wpa_auth;
4731 idata.cache_id = wpa_auth->conf.fils_cache_id;
4732 idata.pmksa = NULL;
4733 idata.spa = sta_addr;
4734 idata.pmkid = pmkid;
4735 wpa_auth_for_each_auth(wpa_auth, wpa_auth_fils_iter, &idata);
4736 return idata.pmksa;
4737}
4738
4739
4740#ifdef CONFIG_IEEE80211R_AP
Roshan Pius3a1667e2018-07-03 15:17:14 -07004741int wpa_auth_write_fte(struct wpa_authenticator *wpa_auth, int use_sha384,
4742 u8 *buf, size_t len)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004743{
4744 struct wpa_auth_config *conf = &wpa_auth->conf;
4745
Roshan Pius3a1667e2018-07-03 15:17:14 -07004746 return wpa_write_ftie(conf, use_sha384, conf->r0_key_holder,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004747 conf->r0_key_holder_len,
4748 NULL, NULL, buf, len, NULL, 0);
4749}
4750#endif /* CONFIG_IEEE80211R_AP */
4751
4752
4753void wpa_auth_get_fils_aead_params(struct wpa_state_machine *sm,
4754 u8 *fils_anonce, u8 *fils_snonce,
4755 u8 *fils_kek, size_t *fils_kek_len)
4756{
4757 os_memcpy(fils_anonce, sm->ANonce, WPA_NONCE_LEN);
4758 os_memcpy(fils_snonce, sm->SNonce, WPA_NONCE_LEN);
4759 os_memcpy(fils_kek, sm->PTK.kek, WPA_KEK_MAX_LEN);
4760 *fils_kek_len = sm->PTK.kek_len;
4761}
4762
4763#endif /* CONFIG_FILS */
4764
4765
Roshan Pius3a1667e2018-07-03 15:17:14 -07004766#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004767
4768int wpa_auth_resend_m1(struct wpa_state_machine *sm, int change_anonce,
4769 void (*cb)(void *ctx1, void *ctx2),
4770 void *ctx1, void *ctx2)
4771{
4772 const u8 *anonce = sm->ANonce;
4773 u8 anonce_buf[WPA_NONCE_LEN];
4774
4775 if (change_anonce) {
4776 if (random_get_bytes(anonce_buf, WPA_NONCE_LEN))
4777 return -1;
4778 anonce = anonce_buf;
4779 }
4780
4781 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
4782 "sending 1/4 msg of 4-Way Handshake (TESTING)");
4783 wpa_send_eapol(sm->wpa_auth, sm,
4784 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
4785 anonce, NULL, 0, 0, 0);
4786 return 0;
4787}
4788
4789
4790int wpa_auth_resend_m3(struct wpa_state_machine *sm,
4791 void (*cb)(void *ctx1, void *ctx2),
4792 void *ctx1, void *ctx2)
4793{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004794 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
4795#ifdef CONFIG_IEEE80211W
4796 u8 *opos;
4797#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004798 size_t gtk_len, kde_len;
4799 struct wpa_group *gsm = sm->group;
4800 u8 *wpa_ie;
4801 int wpa_ie_len, secure, keyidx, encr = 0;
4802
4803 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
4804 GTK[GN], IGTK, [FTIE], [TIE * 2])
4805 */
4806
4807 /* Use 0 RSC */
4808 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
4809 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
4810 wpa_ie = sm->wpa_auth->wpa_ie;
4811 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
4812 if (sm->wpa == WPA_VERSION_WPA &&
4813 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
4814 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
4815 /* WPA-only STA, remove RSN IE and possible MDIE */
4816 wpa_ie = wpa_ie + wpa_ie[1] + 2;
4817 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
4818 wpa_ie = wpa_ie + wpa_ie[1] + 2;
4819 wpa_ie_len = wpa_ie[1] + 2;
4820 }
4821 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
4822 "sending 3/4 msg of 4-Way Handshake (TESTING)");
4823 if (sm->wpa == WPA_VERSION_WPA2) {
4824 /* WPA2 send GTK in the 4-way handshake */
4825 secure = 1;
4826 gtk = gsm->GTK[gsm->GN - 1];
4827 gtk_len = gsm->GTK_len;
4828 keyidx = gsm->GN;
4829 _rsc = rsc;
4830 encr = 1;
4831 } else {
4832 /* WPA does not include GTK in msg 3/4 */
4833 secure = 0;
4834 gtk = NULL;
4835 gtk_len = 0;
4836 keyidx = 0;
4837 _rsc = NULL;
4838 if (sm->rx_eapol_key_secure) {
4839 /*
4840 * It looks like Windows 7 supplicant tries to use
4841 * Secure bit in msg 2/4 after having reported Michael
4842 * MIC failure and it then rejects the 4-way handshake
4843 * if msg 3/4 does not set Secure bit. Work around this
4844 * by setting the Secure bit here even in the case of
4845 * WPA if the supplicant used it first.
4846 */
4847 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
4848 "STA used Secure bit in WPA msg 2/4 - "
4849 "set Secure for 3/4 as workaround");
4850 secure = 1;
4851 }
4852 }
4853
Hai Shalom39bc25d2019-02-06 16:32:13 -08004854 kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004855 if (gtk)
4856 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
4857#ifdef CONFIG_IEEE80211R_AP
4858 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
4859 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
4860 kde_len += 300; /* FTIE + 2 * TIE */
4861 }
4862#endif /* CONFIG_IEEE80211R_AP */
4863 kde = os_malloc(kde_len);
4864 if (kde == NULL)
4865 return -1;
4866
4867 pos = kde;
4868 os_memcpy(pos, wpa_ie, wpa_ie_len);
4869 pos += wpa_ie_len;
4870#ifdef CONFIG_IEEE80211R_AP
4871 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
4872 int res;
4873 size_t elen;
4874
4875 elen = pos - kde;
4876 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
4877 if (res < 0) {
4878 wpa_printf(MSG_ERROR, "FT: Failed to insert "
4879 "PMKR1Name into RSN IE in EAPOL-Key data");
4880 os_free(kde);
4881 return -1;
4882 }
4883 pos -= wpa_ie_len;
4884 pos += elen;
4885 }
4886#endif /* CONFIG_IEEE80211R_AP */
4887 if (gtk) {
4888 u8 hdr[2];
4889 hdr[0] = keyidx & 0x03;
4890 hdr[1] = 0;
4891 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
4892 gtk, gtk_len);
4893 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07004894#ifdef CONFIG_IEEE80211W
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004895 opos = pos;
4896 pos = ieee80211w_kde_add(sm, pos);
Roshan Pius3a1667e2018-07-03 15:17:14 -07004897 if (pos - opos >= 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
4898 /* skip KDE header and keyid */
4899 opos += 2 + RSN_SELECTOR_LEN + 2;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004900 os_memset(opos, 0, 6); /* clear PN */
4901 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07004902#endif /* CONFIG_IEEE80211W */
Hai Shalom39bc25d2019-02-06 16:32:13 -08004903 if (ocv_oci_add(sm, &pos) < 0) {
4904 os_free(kde);
4905 return -1;
4906 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004907
4908#ifdef CONFIG_IEEE80211R_AP
4909 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
4910 int res;
4911 struct wpa_auth_config *conf;
4912
4913 conf = &sm->wpa_auth->conf;
4914 if (sm->assoc_resp_ftie &&
4915 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
4916 os_memcpy(pos, sm->assoc_resp_ftie,
4917 2 + sm->assoc_resp_ftie[1]);
4918 res = 2 + sm->assoc_resp_ftie[1];
4919 } else {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004920 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
4921
4922 res = wpa_write_ftie(conf, use_sha384,
4923 conf->r0_key_holder,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004924 conf->r0_key_holder_len,
4925 NULL, NULL, pos,
4926 kde + kde_len - pos,
4927 NULL, 0);
4928 }
4929 if (res < 0) {
4930 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
4931 "into EAPOL-Key Key Data");
4932 os_free(kde);
4933 return -1;
4934 }
4935 pos += res;
4936
4937 /* TIE[ReassociationDeadline] (TU) */
4938 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
4939 *pos++ = 5;
4940 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
4941 WPA_PUT_LE32(pos, conf->reassociation_deadline);
4942 pos += 4;
4943
4944 /* TIE[KeyLifetime] (seconds) */
4945 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
4946 *pos++ = 5;
4947 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004948 WPA_PUT_LE32(pos, conf->r0_key_lifetime);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004949 pos += 4;
4950 }
4951#endif /* CONFIG_IEEE80211R_AP */
4952
4953 wpa_send_eapol(sm->wpa_auth, sm,
4954 (secure ? WPA_KEY_INFO_SECURE : 0) |
4955 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
4956 WPA_KEY_INFO_MIC : 0) |
4957 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
4958 WPA_KEY_INFO_KEY_TYPE,
4959 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
4960 os_free(kde);
4961 return 0;
4962}
4963
4964
4965int wpa_auth_resend_group_m1(struct wpa_state_machine *sm,
4966 void (*cb)(void *ctx1, void *ctx2),
4967 void *ctx1, void *ctx2)
4968{
4969 u8 rsc[WPA_KEY_RSC_LEN];
4970 struct wpa_group *gsm = sm->group;
4971 const u8 *kde;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004972 u8 *kde_buf = NULL, *pos, hdr[2];
4973#ifdef CONFIG_IEEE80211W
4974 u8 *opos;
4975#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004976 size_t kde_len;
4977 u8 *gtk;
4978
4979 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
4980 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
4981 /* Use 0 RSC */
4982 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
4983 "sending 1/2 msg of Group Key Handshake (TESTING)");
4984
4985 gtk = gsm->GTK[gsm->GN - 1];
4986 if (sm->wpa == WPA_VERSION_WPA2) {
4987 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
Hai Shalom39bc25d2019-02-06 16:32:13 -08004988 ieee80211w_kde_len(sm) + ocv_oci_len(sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004989 kde_buf = os_malloc(kde_len);
4990 if (kde_buf == NULL)
4991 return -1;
4992
4993 kde = pos = kde_buf;
4994 hdr[0] = gsm->GN & 0x03;
4995 hdr[1] = 0;
4996 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
4997 gtk, gsm->GTK_len);
Roshan Pius3a1667e2018-07-03 15:17:14 -07004998#ifdef CONFIG_IEEE80211W
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004999 opos = pos;
5000 pos = ieee80211w_kde_add(sm, pos);
Roshan Pius3a1667e2018-07-03 15:17:14 -07005001 if (pos - opos >=
5002 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
5003 /* skip KDE header and keyid */
5004 opos += 2 + RSN_SELECTOR_LEN + 2;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005005 os_memset(opos, 0, 6); /* clear PN */
5006 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07005007#endif /* CONFIG_IEEE80211W */
Hai Shalom39bc25d2019-02-06 16:32:13 -08005008 if (ocv_oci_add(sm, &pos) < 0) {
5009 os_free(kde_buf);
5010 return -1;
5011 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005012 kde_len = pos - kde;
5013 } else {
5014 kde = gtk;
5015 kde_len = gsm->GTK_len;
5016 }
5017
5018 sm->eapol_status_cb = cb;
5019 sm->eapol_status_cb_ctx1 = ctx1;
5020 sm->eapol_status_cb_ctx2 = ctx2;
5021
5022 wpa_send_eapol(sm->wpa_auth, sm,
5023 WPA_KEY_INFO_SECURE |
5024 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
5025 WPA_KEY_INFO_MIC : 0) |
5026 WPA_KEY_INFO_ACK |
5027 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
5028 rsc, NULL, kde, kde_len, gsm->GN, 1);
5029
5030 os_free(kde_buf);
5031 return 0;
5032}
5033
Roshan Pius3a1667e2018-07-03 15:17:14 -07005034
5035int wpa_auth_rekey_gtk(struct wpa_authenticator *wpa_auth)
5036{
5037 if (!wpa_auth)
5038 return -1;
5039 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
5040 return eloop_register_timeout(0, 0, wpa_rekey_gtk, wpa_auth, NULL);
5041}
5042
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005043#endif /* CONFIG_TESTING_OPTIONS */