19a53cbbeSchristos /*
29a53cbbeSchristos  * WPA Supplicant - Mesh RSN routines
39a53cbbeSchristos  * Copyright (c) 2013-2014, cozybit, Inc.  All rights reserved.
49a53cbbeSchristos  *
59a53cbbeSchristos  * This software may be distributed under the terms of the BSD license.
69a53cbbeSchristos  * See README for more details.
79a53cbbeSchristos  */
89a53cbbeSchristos 
99a53cbbeSchristos #include "utils/includes.h"
109a53cbbeSchristos 
119a53cbbeSchristos #include "utils/common.h"
129a53cbbeSchristos #include "utils/eloop.h"
139a53cbbeSchristos #include "crypto/sha256.h"
149a53cbbeSchristos #include "crypto/random.h"
159a53cbbeSchristos #include "crypto/aes.h"
169a53cbbeSchristos #include "crypto/aes_siv.h"
179a53cbbeSchristos #include "rsn_supp/wpa.h"
189a53cbbeSchristos #include "ap/hostapd.h"
199a53cbbeSchristos #include "ap/wpa_auth.h"
209a53cbbeSchristos #include "ap/sta_info.h"
219a53cbbeSchristos #include "ap/ieee802_11.h"
229a53cbbeSchristos #include "wpa_supplicant_i.h"
239a53cbbeSchristos #include "driver_i.h"
249a53cbbeSchristos #include "wpas_glue.h"
259a53cbbeSchristos #include "mesh_mpm.h"
269a53cbbeSchristos #include "mesh_rsn.h"
279a53cbbeSchristos 
289a53cbbeSchristos #define MESH_AUTH_TIMEOUT 10
299a53cbbeSchristos #define MESH_AUTH_RETRY 3
309a53cbbeSchristos 
mesh_auth_timer(void * eloop_ctx,void * user_data)319a53cbbeSchristos void mesh_auth_timer(void *eloop_ctx, void *user_data)
329a53cbbeSchristos {
339a53cbbeSchristos 	struct wpa_supplicant *wpa_s = eloop_ctx;
349a53cbbeSchristos 	struct sta_info *sta = user_data;
35928750b6Schristos 	struct hostapd_data *hapd;
369a53cbbeSchristos 
379a53cbbeSchristos 	if (sta->sae->state != SAE_ACCEPTED) {
389a53cbbeSchristos 		wpa_printf(MSG_DEBUG, "AUTH: Re-authenticate with " MACSTR
399a53cbbeSchristos 			   " (attempt %d) ",
409a53cbbeSchristos 			   MAC2STR(sta->addr), sta->sae_auth_retry);
419a53cbbeSchristos 		wpa_msg(wpa_s, MSG_INFO, MESH_SAE_AUTH_FAILURE "addr=" MACSTR,
429a53cbbeSchristos 			MAC2STR(sta->addr));
439a53cbbeSchristos 		if (sta->sae_auth_retry < MESH_AUTH_RETRY) {
449a53cbbeSchristos 			mesh_rsn_auth_sae_sta(wpa_s, sta);
459a53cbbeSchristos 		} else {
46928750b6Schristos 			hapd = wpa_s->ifmsh->bss[0];
47928750b6Schristos 
489a53cbbeSchristos 			if (sta->sae_auth_retry > MESH_AUTH_RETRY) {
49928750b6Schristos 				ap_free_sta(hapd, sta);
509a53cbbeSchristos 				return;
519a53cbbeSchristos 			}
529a53cbbeSchristos 
539a53cbbeSchristos 			/* block the STA if exceeded the number of attempts */
549a53cbbeSchristos 			wpa_mesh_set_plink_state(wpa_s, sta, PLINK_BLOCKED);
559a53cbbeSchristos 			sta->sae->state = SAE_NOTHING;
569a53cbbeSchristos 			wpa_msg(wpa_s, MSG_INFO, MESH_SAE_AUTH_BLOCKED "addr="
579a53cbbeSchristos 				MACSTR " duration=%d",
589a53cbbeSchristos 				MAC2STR(sta->addr),
59928750b6Schristos 				hapd->conf->ap_max_inactivity);
609a53cbbeSchristos 		}
619a53cbbeSchristos 		sta->sae_auth_retry++;
629a53cbbeSchristos 	}
639a53cbbeSchristos }
649a53cbbeSchristos 
659a53cbbeSchristos 
auth_logger(void * ctx,const u8 * addr,logger_level level,const char * txt)669a53cbbeSchristos static void auth_logger(void *ctx, const u8 *addr, logger_level level,
679a53cbbeSchristos 			const char *txt)
689a53cbbeSchristos {
699a53cbbeSchristos 	if (addr)
709a53cbbeSchristos 		wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
719a53cbbeSchristos 			   MAC2STR(addr), txt);
729a53cbbeSchristos 	else
739a53cbbeSchristos 		wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
749a53cbbeSchristos }
759a53cbbeSchristos 
769a53cbbeSchristos 
auth_get_psk(void * ctx,const u8 * addr,const u8 * p2p_dev_addr,const u8 * prev_psk,size_t * psk_len,int * vlan_id)779a53cbbeSchristos static const u8 *auth_get_psk(void *ctx, const u8 *addr,
78ebb5671cSchristos 			      const u8 *p2p_dev_addr, const u8 *prev_psk,
79*0d69f216Schristos 			      size_t *psk_len, int *vlan_id)
809a53cbbeSchristos {
819a53cbbeSchristos 	struct mesh_rsn *mesh_rsn = ctx;
829a53cbbeSchristos 	struct hostapd_data *hapd = mesh_rsn->wpa_s->ifmsh->bss[0];
839a53cbbeSchristos 	struct sta_info *sta = ap_get_sta(hapd, addr);
849a53cbbeSchristos 
85ebb5671cSchristos 	if (psk_len)
86ebb5671cSchristos 		*psk_len = PMK_LEN;
87*0d69f216Schristos 	if (vlan_id)
88*0d69f216Schristos 		*vlan_id = 0;
899a53cbbeSchristos 	wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
909a53cbbeSchristos 		   __func__, MAC2STR(addr), prev_psk);
919a53cbbeSchristos 
929a53cbbeSchristos 	if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
939a53cbbeSchristos 		if (!sta->sae || prev_psk)
949a53cbbeSchristos 			return NULL;
959a53cbbeSchristos 		return sta->sae->pmk;
969a53cbbeSchristos 	}
979a53cbbeSchristos 
989a53cbbeSchristos 	return NULL;
999a53cbbeSchristos }
1009a53cbbeSchristos 
1019a53cbbeSchristos 
auth_set_key(void * ctx,int vlan_id,enum wpa_alg alg,const u8 * addr,int idx,u8 * key,size_t key_len)1029a53cbbeSchristos static int auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
1039a53cbbeSchristos 			const u8 *addr, int idx, u8 *key, size_t key_len)
1049a53cbbeSchristos {
1059a53cbbeSchristos 	struct mesh_rsn *mesh_rsn = ctx;
1069a53cbbeSchristos 	u8 seq[6];
1079a53cbbeSchristos 
1089a53cbbeSchristos 	os_memset(seq, 0, sizeof(seq));
1099a53cbbeSchristos 
1109a53cbbeSchristos 	if (addr) {
1119a53cbbeSchristos 		wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d addr=" MACSTR
1129a53cbbeSchristos 			   " key_idx=%d)",
1139a53cbbeSchristos 			   __func__, alg, MAC2STR(addr), idx);
1149a53cbbeSchristos 	} else {
1159a53cbbeSchristos 		wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d key_idx=%d)",
1169a53cbbeSchristos 			   __func__, alg, idx);
1179a53cbbeSchristos 	}
1189a53cbbeSchristos 	wpa_hexdump_key(MSG_DEBUG, "AUTH: set_key - key", key, key_len);
1199a53cbbeSchristos 
1209a53cbbeSchristos 	return wpa_drv_set_key(mesh_rsn->wpa_s, alg, addr, idx,
1219a53cbbeSchristos 			       1, seq, 6, key, key_len);
1229a53cbbeSchristos }
1239a53cbbeSchristos 
1249a53cbbeSchristos 
auth_start_ampe(void * ctx,const u8 * addr)1259a53cbbeSchristos static int auth_start_ampe(void *ctx, const u8 *addr)
1269a53cbbeSchristos {
1279a53cbbeSchristos 	struct mesh_rsn *mesh_rsn = ctx;
1289a53cbbeSchristos 	struct hostapd_data *hapd;
1299a53cbbeSchristos 	struct sta_info *sta;
1309a53cbbeSchristos 
1319a53cbbeSchristos 	if (mesh_rsn->wpa_s->current_ssid->mode != WPAS_MODE_MESH)
1329a53cbbeSchristos 		return -1;
1339a53cbbeSchristos 
1349a53cbbeSchristos 	hapd = mesh_rsn->wpa_s->ifmsh->bss[0];
1359a53cbbeSchristos 	sta = ap_get_sta(hapd, addr);
1369a53cbbeSchristos 	if (sta)
1379a53cbbeSchristos 		eloop_cancel_timeout(mesh_auth_timer, mesh_rsn->wpa_s, sta);
1389a53cbbeSchristos 
1399a53cbbeSchristos 	mesh_mpm_auth_peer(mesh_rsn->wpa_s, addr);
1409a53cbbeSchristos 	return 0;
1419a53cbbeSchristos }
1429a53cbbeSchristos 
1439a53cbbeSchristos 
__mesh_rsn_auth_init(struct mesh_rsn * rsn,const u8 * addr,enum mfp_options ieee80211w,int ocv)144928750b6Schristos static int __mesh_rsn_auth_init(struct mesh_rsn *rsn, const u8 *addr,
145*0d69f216Schristos 				enum mfp_options ieee80211w, int ocv)
1469a53cbbeSchristos {
1479a53cbbeSchristos 	struct wpa_auth_config conf;
148ebb5671cSchristos 	static const struct wpa_auth_callbacks cb = {
149ebb5671cSchristos 		.logger = auth_logger,
150ebb5671cSchristos 		.get_psk = auth_get_psk,
151ebb5671cSchristos 		.set_key = auth_set_key,
152ebb5671cSchristos 		.start_ampe = auth_start_ampe,
153ebb5671cSchristos 	};
1549a53cbbeSchristos 	u8 seq[6] = {};
1559a53cbbeSchristos 
1569a53cbbeSchristos 	wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
1579a53cbbeSchristos 
1589a53cbbeSchristos 	os_memset(&conf, 0, sizeof(conf));
159928750b6Schristos 	conf.wpa = WPA_PROTO_RSN;
1609a53cbbeSchristos 	conf.wpa_key_mgmt = WPA_KEY_MGMT_SAE;
161928750b6Schristos 	conf.wpa_pairwise = rsn->pairwise_cipher;
162928750b6Schristos 	conf.rsn_pairwise = rsn->pairwise_cipher;
163928750b6Schristos 	conf.wpa_group = rsn->group_cipher;
1649a53cbbeSchristos 	conf.eapol_version = 0;
1659a53cbbeSchristos 	conf.wpa_group_rekey = -1;
166ebb5671cSchristos 	conf.wpa_group_update_count = 4;
167ebb5671cSchristos 	conf.wpa_pairwise_update_count = 4;
168928750b6Schristos #ifdef CONFIG_IEEE80211W
169928750b6Schristos 	conf.ieee80211w = ieee80211w;
170928750b6Schristos 	if (ieee80211w != NO_MGMT_FRAME_PROTECTION)
171928750b6Schristos 		conf.group_mgmt_cipher = rsn->mgmt_group_cipher;
172928750b6Schristos #endif /* CONFIG_IEEE80211W */
173*0d69f216Schristos #ifdef CONFIG_OCV
174*0d69f216Schristos 	conf.ocv = ocv;
175*0d69f216Schristos #endif /* CONFIG_OCV */
1769a53cbbeSchristos 
177ebb5671cSchristos 	rsn->auth = wpa_init(addr, &conf, &cb, rsn);
1789a53cbbeSchristos 	if (rsn->auth == NULL) {
1799a53cbbeSchristos 		wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
1809a53cbbeSchristos 		return -1;
1819a53cbbeSchristos 	}
1829a53cbbeSchristos 
1839a53cbbeSchristos 	/* TODO: support rekeying */
184928750b6Schristos 	rsn->mgtk_len = wpa_cipher_key_len(conf.wpa_group);
185928750b6Schristos 	if (random_get_bytes(rsn->mgtk, rsn->mgtk_len) < 0)
1869a53cbbeSchristos 		return -1;
187928750b6Schristos 	rsn->mgtk_key_id = 1;
188928750b6Schristos 
189928750b6Schristos #ifdef CONFIG_IEEE80211W
190928750b6Schristos 	if (ieee80211w != NO_MGMT_FRAME_PROTECTION) {
191928750b6Schristos 		rsn->igtk_len = wpa_cipher_key_len(conf.group_mgmt_cipher);
192928750b6Schristos 		if (random_get_bytes(rsn->igtk, rsn->igtk_len) < 0)
193928750b6Schristos 			return -1;
194928750b6Schristos 		rsn->igtk_key_id = 4;
1959a53cbbeSchristos 
1969a53cbbeSchristos 		/* group mgmt */
197928750b6Schristos 		wpa_hexdump_key(MSG_DEBUG, "mesh: Own TX IGTK",
198928750b6Schristos 				rsn->igtk, rsn->igtk_len);
199928750b6Schristos 		wpa_drv_set_key(rsn->wpa_s,
200928750b6Schristos 				wpa_cipher_to_alg(rsn->mgmt_group_cipher), NULL,
201928750b6Schristos 				rsn->igtk_key_id, 1,
202928750b6Schristos 				seq, sizeof(seq), rsn->igtk, rsn->igtk_len);
203928750b6Schristos 	}
204928750b6Schristos #endif /* CONFIG_IEEE80211W */
2059a53cbbeSchristos 
2069a53cbbeSchristos 	/* group privacy / data frames */
207928750b6Schristos 	wpa_hexdump_key(MSG_DEBUG, "mesh: Own TX MGTK",
208928750b6Schristos 			rsn->mgtk, rsn->mgtk_len);
209928750b6Schristos 	wpa_drv_set_key(rsn->wpa_s, wpa_cipher_to_alg(rsn->group_cipher), NULL,
210928750b6Schristos 			rsn->mgtk_key_id, 1, seq, sizeof(seq),
211928750b6Schristos 			rsn->mgtk, rsn->mgtk_len);
2129a53cbbeSchristos 
2139a53cbbeSchristos 	return 0;
2149a53cbbeSchristos }
2159a53cbbeSchristos 
2169a53cbbeSchristos 
mesh_rsn_deinit(struct mesh_rsn * rsn)2179a53cbbeSchristos static void mesh_rsn_deinit(struct mesh_rsn *rsn)
2189a53cbbeSchristos {
2199a53cbbeSchristos 	os_memset(rsn->mgtk, 0, sizeof(rsn->mgtk));
220928750b6Schristos 	rsn->mgtk_len = 0;
221928750b6Schristos 	os_memset(rsn->igtk, 0, sizeof(rsn->igtk));
222928750b6Schristos 	rsn->igtk_len = 0;
223928750b6Schristos 	if (rsn->auth)
2249a53cbbeSchristos 		wpa_deinit(rsn->auth);
2259a53cbbeSchristos }
2269a53cbbeSchristos 
2279a53cbbeSchristos 
mesh_rsn_auth_init(struct wpa_supplicant * wpa_s,struct mesh_conf * conf)2289a53cbbeSchristos struct mesh_rsn *mesh_rsn_auth_init(struct wpa_supplicant *wpa_s,
2299a53cbbeSchristos 				    struct mesh_conf *conf)
2309a53cbbeSchristos {
2319a53cbbeSchristos 	struct mesh_rsn *mesh_rsn;
2329a53cbbeSchristos 	struct hostapd_data *bss = wpa_s->ifmsh->bss[0];
2339a53cbbeSchristos 	const u8 *ie;
2349a53cbbeSchristos 	size_t ie_len;
235ebb5671cSchristos #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
236ebb5671cSchristos 	struct external_pmksa_cache *entry;
237ebb5671cSchristos #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
2389a53cbbeSchristos 
2399a53cbbeSchristos 	mesh_rsn = os_zalloc(sizeof(*mesh_rsn));
2409a53cbbeSchristos 	if (mesh_rsn == NULL)
2419a53cbbeSchristos 		return NULL;
2429a53cbbeSchristos 	mesh_rsn->wpa_s = wpa_s;
243928750b6Schristos 	mesh_rsn->pairwise_cipher = conf->pairwise_cipher;
244928750b6Schristos 	mesh_rsn->group_cipher = conf->group_cipher;
245928750b6Schristos 	mesh_rsn->mgmt_group_cipher = conf->mgmt_group_cipher;
2469a53cbbeSchristos 
247928750b6Schristos 	if (__mesh_rsn_auth_init(mesh_rsn, wpa_s->own_addr,
248*0d69f216Schristos 				 conf->ieee80211w, conf->ocv) < 0) {
2499a53cbbeSchristos 		mesh_rsn_deinit(mesh_rsn);
250928750b6Schristos 		os_free(mesh_rsn);
2519a53cbbeSchristos 		return NULL;
2529a53cbbeSchristos 	}
2539a53cbbeSchristos 
2549a53cbbeSchristos 	bss->wpa_auth = mesh_rsn->auth;
2559a53cbbeSchristos 
256ebb5671cSchristos #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
257ebb5671cSchristos 	while ((entry = dl_list_last(&wpa_s->mesh_external_pmksa_cache,
258ebb5671cSchristos 				     struct external_pmksa_cache,
259ebb5671cSchristos 				     list)) != NULL) {
260ebb5671cSchristos 		int ret;
261ebb5671cSchristos 
262ebb5671cSchristos 		ret = wpa_auth_pmksa_add_entry(bss->wpa_auth,
263ebb5671cSchristos 					       entry->pmksa_cache);
264ebb5671cSchristos 		dl_list_del(&entry->list);
265ebb5671cSchristos 		os_free(entry);
266ebb5671cSchristos 
267ebb5671cSchristos 		if (ret < 0)
268ebb5671cSchristos 			return NULL;
269ebb5671cSchristos 	}
270ebb5671cSchristos #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
271ebb5671cSchristos 
2729a53cbbeSchristos 	ie = wpa_auth_get_wpa_ie(mesh_rsn->auth, &ie_len);
273928750b6Schristos 	conf->rsn_ie = (u8 *) ie;
274928750b6Schristos 	conf->rsn_ie_len = ie_len;
2759a53cbbeSchristos 
2769a53cbbeSchristos 	wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
2779a53cbbeSchristos 
2789a53cbbeSchristos 	return mesh_rsn;
2799a53cbbeSchristos }
2809a53cbbeSchristos 
2819a53cbbeSchristos 
index_within_array(const int * array,int idx)2829a53cbbeSchristos static int index_within_array(const int *array, int idx)
2839a53cbbeSchristos {
2849a53cbbeSchristos 	int i;
2859a53cbbeSchristos 
2869a53cbbeSchristos 	for (i = 0; i < idx; i++) {
2879a53cbbeSchristos 		if (array[i] == -1)
2889a53cbbeSchristos 			return 0;
2899a53cbbeSchristos 	}
2909a53cbbeSchristos 
2919a53cbbeSchristos 	return 1;
2929a53cbbeSchristos }
2939a53cbbeSchristos 
2949a53cbbeSchristos 
mesh_rsn_sae_group(struct wpa_supplicant * wpa_s,struct sae_data * sae)2959a53cbbeSchristos static int mesh_rsn_sae_group(struct wpa_supplicant *wpa_s,
2969a53cbbeSchristos 			      struct sae_data *sae)
2979a53cbbeSchristos {
2989a53cbbeSchristos 	int *groups = wpa_s->ifmsh->bss[0]->conf->sae_groups;
2999a53cbbeSchristos 
3009a53cbbeSchristos 	/* Configuration may have changed, so validate current index */
3019a53cbbeSchristos 	if (!index_within_array(groups, wpa_s->mesh_rsn->sae_group_index))
3029a53cbbeSchristos 		return -1;
3039a53cbbeSchristos 
3049a53cbbeSchristos 	for (;;) {
3059a53cbbeSchristos 		int group = groups[wpa_s->mesh_rsn->sae_group_index];
3069a53cbbeSchristos 
3079a53cbbeSchristos 		if (group <= 0)
3089a53cbbeSchristos 			break;
3099a53cbbeSchristos 		if (sae_set_group(sae, group) == 0) {
3109a53cbbeSchristos 			wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected SAE group %d",
3119a53cbbeSchristos 				sae->group);
3129a53cbbeSchristos 			return 0;
3139a53cbbeSchristos 		}
3149a53cbbeSchristos 		wpa_s->mesh_rsn->sae_group_index++;
3159a53cbbeSchristos 	}
3169a53cbbeSchristos 
3179a53cbbeSchristos 	return -1;
3189a53cbbeSchristos }
3199a53cbbeSchristos 
3209a53cbbeSchristos 
mesh_rsn_build_sae_commit(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct sta_info * sta)3219a53cbbeSchristos static int mesh_rsn_build_sae_commit(struct wpa_supplicant *wpa_s,
3229a53cbbeSchristos 				     struct wpa_ssid *ssid,
3239a53cbbeSchristos 				     struct sta_info *sta)
3249a53cbbeSchristos {
325ebb5671cSchristos 	const char *password;
326ebb5671cSchristos 
327ebb5671cSchristos 	password = ssid->sae_password;
328ebb5671cSchristos 	if (!password)
329ebb5671cSchristos 		password = ssid->passphrase;
330ebb5671cSchristos 	if (!password) {
3319a53cbbeSchristos 		wpa_msg(wpa_s, MSG_DEBUG, "SAE: No password available");
3329a53cbbeSchristos 		return -1;
3339a53cbbeSchristos 	}
3349a53cbbeSchristos 
3359a53cbbeSchristos 	if (mesh_rsn_sae_group(wpa_s, sta->sae) < 0) {
3369a53cbbeSchristos 		wpa_msg(wpa_s, MSG_DEBUG, "SAE: Failed to select group");
3379a53cbbeSchristos 		return -1;
3389a53cbbeSchristos 	}
3399a53cbbeSchristos 
340ebb5671cSchristos 	if (sta->sae->tmp && !sta->sae->tmp->pw_id && ssid->sae_password_id) {
341ebb5671cSchristos 		sta->sae->tmp->pw_id = os_strdup(ssid->sae_password_id);
342ebb5671cSchristos 		if (!sta->sae->tmp->pw_id)
343ebb5671cSchristos 			return -1;
344ebb5671cSchristos 	}
3459a53cbbeSchristos 	return sae_prepare_commit(wpa_s->own_addr, sta->addr,
346ebb5671cSchristos 				  (u8 *) password, os_strlen(password),
347ebb5671cSchristos 				  ssid->sae_password_id,
348ebb5671cSchristos 				  sta->sae);
3499a53cbbeSchristos }
3509a53cbbeSchristos 
3519a53cbbeSchristos 
3529a53cbbeSchristos /* initiate new SAE authentication with sta */
mesh_rsn_auth_sae_sta(struct wpa_supplicant * wpa_s,struct sta_info * sta)3539a53cbbeSchristos int mesh_rsn_auth_sae_sta(struct wpa_supplicant *wpa_s,
3549a53cbbeSchristos 			  struct sta_info *sta)
3559a53cbbeSchristos {
3569a53cbbeSchristos 	struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
3579a53cbbeSchristos 	struct wpa_ssid *ssid = wpa_s->current_ssid;
358928750b6Schristos 	struct rsn_pmksa_cache_entry *pmksa;
3599a53cbbeSchristos 	unsigned int rnd;
3609a53cbbeSchristos 	int ret;
3619a53cbbeSchristos 
3629a53cbbeSchristos 	if (!ssid) {
3639a53cbbeSchristos 		wpa_msg(wpa_s, MSG_DEBUG,
3649a53cbbeSchristos 			"AUTH: No current_ssid known to initiate new SAE");
3659a53cbbeSchristos 		return -1;
3669a53cbbeSchristos 	}
3679a53cbbeSchristos 
3689a53cbbeSchristos 	if (!sta->sae) {
3699a53cbbeSchristos 		sta->sae = os_zalloc(sizeof(*sta->sae));
3709a53cbbeSchristos 		if (sta->sae == NULL)
3719a53cbbeSchristos 			return -1;
3729a53cbbeSchristos 	}
3739a53cbbeSchristos 
374ebb5671cSchristos 	pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr, NULL);
375928750b6Schristos 	if (pmksa) {
376928750b6Schristos 		if (!sta->wpa_sm)
377928750b6Schristos 			sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
378928750b6Schristos 							sta->addr, NULL);
379928750b6Schristos 		if (!sta->wpa_sm) {
380928750b6Schristos 			wpa_printf(MSG_ERROR,
381928750b6Schristos 				   "mesh: Failed to initialize RSN state machine");
382928750b6Schristos 			return -1;
383928750b6Schristos 		}
384928750b6Schristos 
385928750b6Schristos 		wpa_printf(MSG_DEBUG,
386928750b6Schristos 			   "AUTH: Mesh PMKSA cache entry found for " MACSTR
387928750b6Schristos 			   " - try to use PMKSA caching instead of new SAE authentication",
388928750b6Schristos 			   MAC2STR(sta->addr));
389928750b6Schristos 		wpa_auth_pmksa_set_to_sm(pmksa, sta->wpa_sm, hapd->wpa_auth,
390928750b6Schristos 					 sta->sae->pmkid, sta->sae->pmk);
391928750b6Schristos 		sae_accept_sta(hapd, sta);
392928750b6Schristos 		sta->mesh_sae_pmksa_caching = 1;
393928750b6Schristos 		return 0;
394928750b6Schristos 	}
395928750b6Schristos 	sta->mesh_sae_pmksa_caching = 0;
396928750b6Schristos 
3979a53cbbeSchristos 	if (mesh_rsn_build_sae_commit(wpa_s, ssid, sta))
3989a53cbbeSchristos 		return -1;
3999a53cbbeSchristos 
4009a53cbbeSchristos 	wpa_msg(wpa_s, MSG_DEBUG,
4019a53cbbeSchristos 		"AUTH: started authentication with SAE peer: " MACSTR,
4029a53cbbeSchristos 		MAC2STR(sta->addr));
4039a53cbbeSchristos 
4049a53cbbeSchristos 	ret = auth_sae_init_committed(hapd, sta);
4059a53cbbeSchristos 	if (ret)
4069a53cbbeSchristos 		return ret;
4079a53cbbeSchristos 
4089a53cbbeSchristos 	eloop_cancel_timeout(mesh_auth_timer, wpa_s, sta);
4099a53cbbeSchristos 	rnd = rand() % MESH_AUTH_TIMEOUT;
4109a53cbbeSchristos 	eloop_register_timeout(MESH_AUTH_TIMEOUT + rnd, 0, mesh_auth_timer,
4119a53cbbeSchristos 			       wpa_s, sta);
4129a53cbbeSchristos 	return 0;
4139a53cbbeSchristos }
4149a53cbbeSchristos 
4159a53cbbeSchristos 
mesh_rsn_get_pmkid(struct mesh_rsn * rsn,struct sta_info * sta,u8 * pmkid)4169a53cbbeSchristos void mesh_rsn_get_pmkid(struct mesh_rsn *rsn, struct sta_info *sta, u8 *pmkid)
4179a53cbbeSchristos {
418928750b6Schristos 	os_memcpy(pmkid, sta->sae->pmkid, SAE_PMKID_LEN);
4199a53cbbeSchristos }
4209a53cbbeSchristos 
4219a53cbbeSchristos 
4229a53cbbeSchristos static void
mesh_rsn_derive_aek(struct mesh_rsn * rsn,struct sta_info * sta)4239a53cbbeSchristos mesh_rsn_derive_aek(struct mesh_rsn *rsn, struct sta_info *sta)
4249a53cbbeSchristos {
4259a53cbbeSchristos 	u8 *myaddr = rsn->wpa_s->own_addr;
4269a53cbbeSchristos 	u8 *peer = sta->addr;
427928750b6Schristos 	u8 *addr1, *addr2;
428928750b6Schristos 	u8 context[RSN_SELECTOR_LEN + 2 * ETH_ALEN], *ptr = context;
4299a53cbbeSchristos 
430928750b6Schristos 	/*
431928750b6Schristos 	 * AEK = KDF-Hash-256(PMK, "AEK Derivation", Selected AKM Suite ||
432928750b6Schristos 	 *       min(localMAC, peerMAC) || max(localMAC, peerMAC))
433928750b6Schristos 	 */
434928750b6Schristos 	/* Selected AKM Suite: SAE */
435928750b6Schristos 	RSN_SELECTOR_PUT(ptr, RSN_AUTH_KEY_MGMT_SAE);
436928750b6Schristos 	ptr += RSN_SELECTOR_LEN;
4379a53cbbeSchristos 
4389a53cbbeSchristos 	if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
4399a53cbbeSchristos 		addr1 = myaddr;
4409a53cbbeSchristos 		addr2 = peer;
441928750b6Schristos 	} else {
442928750b6Schristos 		addr1 = peer;
443928750b6Schristos 		addr2 = myaddr;
4449a53cbbeSchristos 	}
445928750b6Schristos 	os_memcpy(ptr, addr1, ETH_ALEN);
446928750b6Schristos 	ptr += ETH_ALEN;
447928750b6Schristos 	os_memcpy(ptr, addr2, ETH_ALEN);
4489a53cbbeSchristos 
4499a53cbbeSchristos 	sha256_prf(sta->sae->pmk, sizeof(sta->sae->pmk), "AEK Derivation",
4509a53cbbeSchristos 		   context, sizeof(context), sta->aek, sizeof(sta->aek));
4519a53cbbeSchristos }
4529a53cbbeSchristos 
4539a53cbbeSchristos 
4549a53cbbeSchristos /* derive mesh temporal key from pmk */
mesh_rsn_derive_mtk(struct wpa_supplicant * wpa_s,struct sta_info * sta)4559a53cbbeSchristos int mesh_rsn_derive_mtk(struct wpa_supplicant *wpa_s, struct sta_info *sta)
4569a53cbbeSchristos {
4579a53cbbeSchristos 	u8 *ptr;
4589a53cbbeSchristos 	u8 *min, *max;
4599a53cbbeSchristos 	u8 *myaddr = wpa_s->own_addr;
4609a53cbbeSchristos 	u8 *peer = sta->addr;
461928750b6Schristos 	u8 context[2 * WPA_NONCE_LEN + 2 * 2 + RSN_SELECTOR_LEN + 2 * ETH_ALEN];
4629a53cbbeSchristos 
463928750b6Schristos 	/*
464928750b6Schristos 	 * MTK = KDF-Hash-Length(PMK, "Temporal Key Derivation", min(localNonce,
465928750b6Schristos 	 *  peerNonce) || max(localNonce, peerNonce) || min(localLinkID,
466928750b6Schristos 	 *  peerLinkID) || max(localLinkID, peerLinkID) || Selected AKM Suite ||
467928750b6Schristos 	 *  min(localMAC, peerMAC) || max(localMAC, peerMAC))
468928750b6Schristos 	 */
4699a53cbbeSchristos 	ptr = context;
470928750b6Schristos 	if (os_memcmp(sta->my_nonce, sta->peer_nonce, WPA_NONCE_LEN) < 0) {
4719a53cbbeSchristos 		min = sta->my_nonce;
4729a53cbbeSchristos 		max = sta->peer_nonce;
4739a53cbbeSchristos 	} else {
4749a53cbbeSchristos 		min = sta->peer_nonce;
4759a53cbbeSchristos 		max = sta->my_nonce;
4769a53cbbeSchristos 	}
477928750b6Schristos 	os_memcpy(ptr, min, WPA_NONCE_LEN);
478928750b6Schristos 	ptr += WPA_NONCE_LEN;
479928750b6Schristos 	os_memcpy(ptr, max, WPA_NONCE_LEN);
480928750b6Schristos 	ptr += WPA_NONCE_LEN;
4819a53cbbeSchristos 
4829a53cbbeSchristos 	if (sta->my_lid < sta->peer_lid) {
483928750b6Schristos 		WPA_PUT_LE16(ptr, sta->my_lid);
484928750b6Schristos 		ptr += 2;
485928750b6Schristos 		WPA_PUT_LE16(ptr, sta->peer_lid);
486928750b6Schristos 		ptr += 2;
4879a53cbbeSchristos 	} else {
488928750b6Schristos 		WPA_PUT_LE16(ptr, sta->peer_lid);
489928750b6Schristos 		ptr += 2;
490928750b6Schristos 		WPA_PUT_LE16(ptr, sta->my_lid);
491928750b6Schristos 		ptr += 2;
4929a53cbbeSchristos 	}
4939a53cbbeSchristos 
494928750b6Schristos 	/* Selected AKM Suite: SAE */
495928750b6Schristos 	RSN_SELECTOR_PUT(ptr, RSN_AUTH_KEY_MGMT_SAE);
496928750b6Schristos 	ptr += RSN_SELECTOR_LEN;
4979a53cbbeSchristos 
4989a53cbbeSchristos 	if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
4999a53cbbeSchristos 		min = myaddr;
5009a53cbbeSchristos 		max = peer;
5019a53cbbeSchristos 	} else {
5029a53cbbeSchristos 		min = peer;
5039a53cbbeSchristos 		max = myaddr;
5049a53cbbeSchristos 	}
5059a53cbbeSchristos 	os_memcpy(ptr, min, ETH_ALEN);
506928750b6Schristos 	ptr += ETH_ALEN;
507928750b6Schristos 	os_memcpy(ptr, max, ETH_ALEN);
5089a53cbbeSchristos 
509928750b6Schristos 	sta->mtk_len = wpa_cipher_key_len(wpa_s->mesh_rsn->pairwise_cipher);
510928750b6Schristos 	sha256_prf(sta->sae->pmk, SAE_PMK_LEN,
5119a53cbbeSchristos 		   "Temporal Key Derivation", context, sizeof(context),
512928750b6Schristos 		   sta->mtk, sta->mtk_len);
5139a53cbbeSchristos 	return 0;
5149a53cbbeSchristos }
5159a53cbbeSchristos 
5169a53cbbeSchristos 
mesh_rsn_init_ampe_sta(struct wpa_supplicant * wpa_s,struct sta_info * sta)5179a53cbbeSchristos void mesh_rsn_init_ampe_sta(struct wpa_supplicant *wpa_s, struct sta_info *sta)
5189a53cbbeSchristos {
519928750b6Schristos 	if (random_get_bytes(sta->my_nonce, WPA_NONCE_LEN) < 0) {
5209a53cbbeSchristos 		wpa_printf(MSG_INFO, "mesh: Failed to derive random nonce");
5219a53cbbeSchristos 		/* TODO: How to handle this more cleanly? */
5229a53cbbeSchristos 	}
523928750b6Schristos 	os_memset(sta->peer_nonce, 0, WPA_NONCE_LEN);
5249a53cbbeSchristos 	mesh_rsn_derive_aek(wpa_s->mesh_rsn, sta);
5259a53cbbeSchristos }
5269a53cbbeSchristos 
5279a53cbbeSchristos 
5289a53cbbeSchristos /* insert AMPE and encrypted MIC at @ie.
5299a53cbbeSchristos  * @mesh_rsn: mesh RSN context
5309a53cbbeSchristos  * @sta: STA we're sending to
5319a53cbbeSchristos  * @cat: pointer to category code in frame header.
5329a53cbbeSchristos  * @buf: wpabuf to add encrypted AMPE and MIC to.
5339a53cbbeSchristos  * */
mesh_rsn_protect_frame(struct mesh_rsn * rsn,struct sta_info * sta,const u8 * cat,struct wpabuf * buf)5349a53cbbeSchristos int mesh_rsn_protect_frame(struct mesh_rsn *rsn, struct sta_info *sta,
5359a53cbbeSchristos 			   const u8 *cat, struct wpabuf *buf)
5369a53cbbeSchristos {
5379a53cbbeSchristos 	struct ieee80211_ampe_ie *ampe;
5389a53cbbeSchristos 	u8 const *ie = wpabuf_head_u8(buf) + wpabuf_len(buf);
539928750b6Schristos 	u8 *ampe_ie, *pos, *mic_payload;
5409a53cbbeSchristos 	const u8 *aad[] = { rsn->wpa_s->own_addr, sta->addr, cat };
5419a53cbbeSchristos 	const size_t aad_len[] = { ETH_ALEN, ETH_ALEN, ie - cat };
5429a53cbbeSchristos 	int ret = 0;
543928750b6Schristos 	size_t len;
5449a53cbbeSchristos 
545928750b6Schristos 	len = sizeof(*ampe);
546928750b6Schristos 	if (cat[1] == PLINK_OPEN)
547928750b6Schristos 		len += rsn->mgtk_len + WPA_KEY_RSC_LEN + 4;
548928750b6Schristos #ifdef CONFIG_IEEE80211W
549928750b6Schristos 	if (cat[1] == PLINK_OPEN && rsn->igtk_len)
550928750b6Schristos 		len += 2 + 6 + rsn->igtk_len;
551928750b6Schristos #endif /* CONFIG_IEEE80211W */
552928750b6Schristos 
553928750b6Schristos 	if (2 + AES_BLOCK_SIZE + 2 + len > wpabuf_tailroom(buf)) {
5549a53cbbeSchristos 		wpa_printf(MSG_ERROR, "protect frame: buffer too small");
5559a53cbbeSchristos 		return -EINVAL;
5569a53cbbeSchristos 	}
5579a53cbbeSchristos 
558928750b6Schristos 	ampe_ie = os_zalloc(2 + len);
5599a53cbbeSchristos 	if (!ampe_ie) {
5609a53cbbeSchristos 		wpa_printf(MSG_ERROR, "protect frame: out of memory");
5619a53cbbeSchristos 		return -ENOMEM;
5629a53cbbeSchristos 	}
5639a53cbbeSchristos 
5649a53cbbeSchristos 	/*  IE: AMPE */
5659a53cbbeSchristos 	ampe_ie[0] = WLAN_EID_AMPE;
566928750b6Schristos 	ampe_ie[1] = len;
5679a53cbbeSchristos 	ampe = (struct ieee80211_ampe_ie *) (ampe_ie + 2);
5689a53cbbeSchristos 
5699a53cbbeSchristos 	RSN_SELECTOR_PUT(ampe->selected_pairwise_suite,
570928750b6Schristos 			 RSN_CIPHER_SUITE_CCMP);
571928750b6Schristos 	os_memcpy(ampe->local_nonce, sta->my_nonce, WPA_NONCE_LEN);
572928750b6Schristos 	os_memcpy(ampe->peer_nonce, sta->peer_nonce, WPA_NONCE_LEN);
573928750b6Schristos 
574928750b6Schristos 	pos = (u8 *) (ampe + 1);
575928750b6Schristos 	if (cat[1] != PLINK_OPEN)
576928750b6Schristos 		goto skip_keys;
577928750b6Schristos 
578928750b6Schristos 	/* TODO: Key Replay Counter[8] optionally for
579928750b6Schristos 	 * Mesh Group Key Inform/Acknowledge frames */
580928750b6Schristos 
5819a53cbbeSchristos 	/* TODO: static mgtk for now since we don't support rekeying! */
582928750b6Schristos 	/*
583928750b6Schristos 	 * GTKdata[variable]:
584928750b6Schristos 	 * MGTK[variable] || Key RSC[8] || GTKExpirationTime[4]
585928750b6Schristos 	 */
586928750b6Schristos 	os_memcpy(pos, rsn->mgtk, rsn->mgtk_len);
587928750b6Schristos 	pos += rsn->mgtk_len;
588928750b6Schristos 	wpa_drv_get_seqnum(rsn->wpa_s, NULL, rsn->mgtk_key_id, pos);
589928750b6Schristos 	pos += WPA_KEY_RSC_LEN;
590928750b6Schristos 	/* Use fixed GTKExpirationTime for now */
591928750b6Schristos 	WPA_PUT_LE32(pos, 0xffffffff);
592928750b6Schristos 	pos += 4;
593928750b6Schristos 
594928750b6Schristos #ifdef CONFIG_IEEE80211W
595928750b6Schristos 	/*
596928750b6Schristos 	 * IGTKdata[variable]:
597928750b6Schristos 	 * Key ID[2], IPN[6], IGTK[variable]
598928750b6Schristos 	 */
599928750b6Schristos 	if (rsn->igtk_len) {
600928750b6Schristos 		WPA_PUT_LE16(pos, rsn->igtk_key_id);
601928750b6Schristos 		pos += 2;
602928750b6Schristos 		wpa_drv_get_seqnum(rsn->wpa_s, NULL, rsn->igtk_key_id, pos);
603928750b6Schristos 		pos += 6;
604928750b6Schristos 		os_memcpy(pos, rsn->igtk, rsn->igtk_len);
605928750b6Schristos 	}
606928750b6Schristos #endif /* CONFIG_IEEE80211W */
607928750b6Schristos 
608928750b6Schristos skip_keys:
609928750b6Schristos 	wpa_hexdump_key(MSG_DEBUG, "mesh: Plaintext AMPE element",
610928750b6Schristos 			ampe_ie, 2 + len);
6119a53cbbeSchristos 
6129a53cbbeSchristos 	/* IE: MIC */
613928750b6Schristos 	wpabuf_put_u8(buf, WLAN_EID_MIC);
614928750b6Schristos 	wpabuf_put_u8(buf, AES_BLOCK_SIZE);
6159a53cbbeSchristos 	/* MIC field is output ciphertext */
6169a53cbbeSchristos 
6179a53cbbeSchristos 	/* encrypt after MIC */
618928750b6Schristos 	mic_payload = wpabuf_put(buf, 2 + len + AES_BLOCK_SIZE);
6199a53cbbeSchristos 
620ebb5671cSchristos 	if (aes_siv_encrypt(sta->aek, sizeof(sta->aek), ampe_ie, 2 + len, 3,
6219a53cbbeSchristos 			    aad, aad_len, mic_payload)) {
6229a53cbbeSchristos 		wpa_printf(MSG_ERROR, "protect frame: failed to encrypt");
6239a53cbbeSchristos 		ret = -ENOMEM;
6249a53cbbeSchristos 	}
6259a53cbbeSchristos 
6269a53cbbeSchristos 	os_free(ampe_ie);
6279a53cbbeSchristos 
6289a53cbbeSchristos 	return ret;
6299a53cbbeSchristos }
6309a53cbbeSchristos 
6319a53cbbeSchristos 
mesh_rsn_process_ampe(struct wpa_supplicant * wpa_s,struct sta_info * sta,struct ieee802_11_elems * elems,const u8 * cat,const u8 * chosen_pmk,const u8 * start,size_t elems_len)6329a53cbbeSchristos int mesh_rsn_process_ampe(struct wpa_supplicant *wpa_s, struct sta_info *sta,
6339a53cbbeSchristos 			  struct ieee802_11_elems *elems, const u8 *cat,
634928750b6Schristos 			  const u8 *chosen_pmk,
6359a53cbbeSchristos 			  const u8 *start, size_t elems_len)
6369a53cbbeSchristos {
6379a53cbbeSchristos 	int ret = 0;
6389a53cbbeSchristos 	struct ieee80211_ampe_ie *ampe;
639928750b6Schristos 	u8 null_nonce[WPA_NONCE_LEN] = {};
6409a53cbbeSchristos 	u8 ampe_eid;
6419a53cbbeSchristos 	u8 ampe_ie_len;
642928750b6Schristos 	u8 *ampe_buf, *crypt = NULL, *pos, *end;
6439a53cbbeSchristos 	size_t crypt_len;
6449a53cbbeSchristos 	const u8 *aad[] = { sta->addr, wpa_s->own_addr, cat };
6459a53cbbeSchristos 	const size_t aad_len[] = { ETH_ALEN, ETH_ALEN,
646*0d69f216Schristos 				   elems->mic ? (elems->mic - 2) - cat : 0 };
647928750b6Schristos 	size_t key_len;
648928750b6Schristos 
649928750b6Schristos 	if (!sta->sae) {
650928750b6Schristos 		struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
651928750b6Schristos 
652ebb5671cSchristos 		if (!wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr, NULL)) {
653928750b6Schristos 			wpa_printf(MSG_INFO,
654928750b6Schristos 				   "Mesh RSN: SAE is not prepared yet");
655928750b6Schristos 			return -1;
656928750b6Schristos 		}
657928750b6Schristos 		mesh_rsn_auth_sae_sta(wpa_s, sta);
658928750b6Schristos 	}
659928750b6Schristos 
660*0d69f216Schristos 	if (chosen_pmk &&
661*0d69f216Schristos 	    (!sta->sae ||
662*0d69f216Schristos 	     os_memcmp(chosen_pmk, sta->sae->pmkid, PMKID_LEN) != 0)) {
663928750b6Schristos 		wpa_msg(wpa_s, MSG_DEBUG,
664928750b6Schristos 			"Mesh RSN: Invalid PMKID (Chosen PMK did not match calculated PMKID)");
665928750b6Schristos 		return -1;
666928750b6Schristos 	}
6679a53cbbeSchristos 
6689a53cbbeSchristos 	if (!elems->mic || elems->mic_len < AES_BLOCK_SIZE) {
6699a53cbbeSchristos 		wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: missing mic ie");
6709a53cbbeSchristos 		return -1;
6719a53cbbeSchristos 	}
6729a53cbbeSchristos 
6739a53cbbeSchristos 	ampe_buf = (u8 *) elems->mic + elems->mic_len;
6749a53cbbeSchristos 	if ((int) elems_len < ampe_buf - start)
6759a53cbbeSchristos 		return -1;
6769a53cbbeSchristos 
6779a53cbbeSchristos 	crypt_len = elems_len - (elems->mic - start);
678928750b6Schristos 	if (crypt_len < 2 + AES_BLOCK_SIZE) {
6799a53cbbeSchristos 		wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: missing ampe ie");
6809a53cbbeSchristos 		return -1;
6819a53cbbeSchristos 	}
6829a53cbbeSchristos 
6839a53cbbeSchristos 	/* crypt is modified by siv_decrypt */
6849a53cbbeSchristos 	crypt = os_zalloc(crypt_len);
6859a53cbbeSchristos 	if (!crypt) {
6869a53cbbeSchristos 		wpa_printf(MSG_ERROR, "Mesh RSN: out of memory");
6879a53cbbeSchristos 		ret = -ENOMEM;
6889a53cbbeSchristos 		goto free;
6899a53cbbeSchristos 	}
6909a53cbbeSchristos 
6919a53cbbeSchristos 	os_memcpy(crypt, elems->mic, crypt_len);
6929a53cbbeSchristos 
693ebb5671cSchristos 	if (aes_siv_decrypt(sta->aek, sizeof(sta->aek), crypt, crypt_len, 3,
6949a53cbbeSchristos 			    aad, aad_len, ampe_buf)) {
6959a53cbbeSchristos 		wpa_printf(MSG_ERROR, "Mesh RSN: frame verification failed!");
696928750b6Schristos 		ret = -2;
6979a53cbbeSchristos 		goto free;
6989a53cbbeSchristos 	}
6999a53cbbeSchristos 
700928750b6Schristos 	crypt_len -= AES_BLOCK_SIZE;
701928750b6Schristos 	wpa_hexdump_key(MSG_DEBUG, "mesh: Decrypted AMPE element",
702928750b6Schristos 			ampe_buf, crypt_len);
703928750b6Schristos 
7049a53cbbeSchristos 	ampe_eid = *ampe_buf++;
7059a53cbbeSchristos 	ampe_ie_len = *ampe_buf++;
7069a53cbbeSchristos 
7079a53cbbeSchristos 	if (ampe_eid != WLAN_EID_AMPE ||
708928750b6Schristos 	    (size_t) 2 + ampe_ie_len > crypt_len ||
7099a53cbbeSchristos 	    ampe_ie_len < sizeof(struct ieee80211_ampe_ie)) {
7109a53cbbeSchristos 		wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid ampe ie");
7119a53cbbeSchristos 		ret = -1;
7129a53cbbeSchristos 		goto free;
7139a53cbbeSchristos 	}
7149a53cbbeSchristos 
7159a53cbbeSchristos 	ampe = (struct ieee80211_ampe_ie *) ampe_buf;
716928750b6Schristos 	pos = (u8 *) (ampe + 1);
717928750b6Schristos 	end = ampe_buf + ampe_ie_len;
718928750b6Schristos 	if (os_memcmp(ampe->peer_nonce, null_nonce, WPA_NONCE_LEN) != 0 &&
719928750b6Schristos 	    os_memcmp(ampe->peer_nonce, sta->my_nonce, WPA_NONCE_LEN) != 0) {
7209a53cbbeSchristos 		wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid peer nonce");
7219a53cbbeSchristos 		ret = -1;
7229a53cbbeSchristos 		goto free;
7239a53cbbeSchristos 	}
7249a53cbbeSchristos 	os_memcpy(sta->peer_nonce, ampe->local_nonce,
7259a53cbbeSchristos 		  sizeof(ampe->local_nonce));
7269a53cbbeSchristos 
727928750b6Schristos 	/* TODO: Key Replay Counter[8] in Mesh Group Key Inform/Acknowledge
728928750b6Schristos 	 * frames */
729928750b6Schristos 
730928750b6Schristos 	/*
731928750b6Schristos 	 * GTKdata shall not be included in Mesh Peering Confirm. While the
732928750b6Schristos 	 * standard does not state the same about IGTKdata, that same constraint
733928750b6Schristos 	 * needs to apply for it. It makes no sense to include the keys in Mesh
734928750b6Schristos 	 * Peering Close frames either, so while the standard does not seem to
735928750b6Schristos 	 * have a shall statement for these, they are described without
736928750b6Schristos 	 * mentioning GTKdata.
737928750b6Schristos 	 *
738928750b6Schristos 	 * An earlier implementation used to add GTKdata to both Mesh Peering
739928750b6Schristos 	 * Open and Mesh Peering Confirm frames, so ignore the possibly present
740928750b6Schristos 	 * GTKdata frame without rejecting the frame as a backwards
741928750b6Schristos 	 * compatibility mechanism.
742928750b6Schristos 	 */
743928750b6Schristos 	if (cat[1] != PLINK_OPEN) {
744928750b6Schristos 		if (end > pos) {
745928750b6Schristos 			wpa_hexdump_key(MSG_DEBUG,
746928750b6Schristos 					"mesh: Ignore unexpected GTKdata(etc.) fields in the end of AMPE element in Mesh Peering Confirm/Close",
747928750b6Schristos 					pos, end - pos);
748928750b6Schristos 		}
749928750b6Schristos 		goto free;
750928750b6Schristos 	}
751928750b6Schristos 
752928750b6Schristos 	/*
753928750b6Schristos 	 * GTKdata[variable]:
754928750b6Schristos 	 * MGTK[variable] || Key RSC[8] || GTKExpirationTime[4]
755928750b6Schristos 	 */
756928750b6Schristos 	sta->mgtk_key_id = 1; /* FIX: Where to get Key ID? */
757928750b6Schristos 	key_len = wpa_cipher_key_len(wpa_s->mesh_rsn->group_cipher);
758928750b6Schristos 	if ((int) key_len + WPA_KEY_RSC_LEN + 4 > end - pos) {
759928750b6Schristos 		wpa_dbg(wpa_s, MSG_DEBUG, "mesh: Truncated AMPE element");
760928750b6Schristos 		ret = -1;
761928750b6Schristos 		goto free;
762928750b6Schristos 	}
763928750b6Schristos 	sta->mgtk_len = key_len;
764928750b6Schristos 	os_memcpy(sta->mgtk, pos, sta->mgtk_len);
765928750b6Schristos 	wpa_hexdump_key(MSG_DEBUG, "mesh: GTKdata - MGTK",
766928750b6Schristos 			sta->mgtk, sta->mgtk_len);
767928750b6Schristos 	pos += sta->mgtk_len;
768928750b6Schristos 	wpa_hexdump(MSG_DEBUG, "mesh: GTKdata - MGTK - Key RSC",
769928750b6Schristos 		    pos, WPA_KEY_RSC_LEN);
770928750b6Schristos 	os_memcpy(sta->mgtk_rsc, pos, sizeof(sta->mgtk_rsc));
771928750b6Schristos 	pos += WPA_KEY_RSC_LEN;
772928750b6Schristos 	wpa_printf(MSG_DEBUG,
773928750b6Schristos 		   "mesh: GTKdata - MGTK - GTKExpirationTime: %u seconds",
774928750b6Schristos 		   WPA_GET_LE32(pos));
775928750b6Schristos 	pos += 4;
776928750b6Schristos 
777928750b6Schristos #ifdef CONFIG_IEEE80211W
778928750b6Schristos 	/*
779928750b6Schristos 	 * IGTKdata[variable]:
780928750b6Schristos 	 * Key ID[2], IPN[6], IGTK[variable]
781928750b6Schristos 	 */
782928750b6Schristos 	key_len = wpa_cipher_key_len(wpa_s->mesh_rsn->mgmt_group_cipher);
783928750b6Schristos 	if (end - pos >= (int) (2 + 6 + key_len)) {
784928750b6Schristos 		sta->igtk_key_id = WPA_GET_LE16(pos);
785928750b6Schristos 		wpa_printf(MSG_DEBUG, "mesh: IGTKdata - Key ID %u",
786928750b6Schristos 			   sta->igtk_key_id);
787928750b6Schristos 		pos += 2;
788928750b6Schristos 		os_memcpy(sta->igtk_rsc, pos, sizeof(sta->igtk_rsc));
789928750b6Schristos 		wpa_hexdump(MSG_DEBUG, "mesh: IGTKdata - IPN",
790928750b6Schristos 			    sta->igtk_rsc, sizeof(sta->igtk_rsc));
791928750b6Schristos 		pos += 6;
792928750b6Schristos 		os_memcpy(sta->igtk, pos, key_len);
793928750b6Schristos 		sta->igtk_len = key_len;
794928750b6Schristos 		wpa_hexdump_key(MSG_DEBUG, "mesh: IGTKdata - IGTK",
795928750b6Schristos 				sta->igtk, sta->igtk_len);
796928750b6Schristos 	}
797928750b6Schristos #endif /* CONFIG_IEEE80211W */
798928750b6Schristos 
7999a53cbbeSchristos free:
8009a53cbbeSchristos 	os_free(crypt);
8019a53cbbeSchristos 	return ret;
8029a53cbbeSchristos }
803