mesh_rsn.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /*
  2. * WPA Supplicant - Mesh RSN routines
  3. * Copyright (c) 2013-2014, cozybit, Inc. All rights reserved.
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "crypto/sha256.h"
  12. #include "crypto/random.h"
  13. #include "crypto/aes.h"
  14. #include "crypto/aes_siv.h"
  15. #include "rsn_supp/wpa.h"
  16. #include "ap/hostapd.h"
  17. #include "ap/wpa_auth.h"
  18. #include "ap/sta_info.h"
  19. #include "wpa_supplicant_i.h"
  20. #include "driver_i.h"
  21. #include "wpas_glue.h"
  22. #include "mesh_mpm.h"
  23. #include "mesh_rsn.h"
  24. #define MESH_AUTH_TIMEOUT 10
  25. #define MESH_AUTH_RETRY 3
  26. void mesh_auth_timer(void *eloop_ctx, void *user_data)
  27. {
  28. struct wpa_supplicant *wpa_s = eloop_ctx;
  29. struct sta_info *sta = user_data;
  30. if (sta->sae->state != SAE_ACCEPTED) {
  31. wpa_printf(MSG_DEBUG, "AUTH: Re-authenticate with " MACSTR
  32. " (attempt %d) ",
  33. MAC2STR(sta->addr), sta->sae_auth_retry);
  34. if (sta->sae_auth_retry < MESH_AUTH_RETRY) {
  35. mesh_rsn_auth_sae_sta(wpa_s, sta);
  36. } else {
  37. /* block the STA if exceeded the number of attempts */
  38. sta->plink_state = PLINK_BLOCKED;
  39. sta->sae->state = SAE_NOTHING;
  40. }
  41. sta->sae_auth_retry++;
  42. }
  43. }
  44. static void auth_logger(void *ctx, const u8 *addr, logger_level level,
  45. const char *txt)
  46. {
  47. if (addr)
  48. wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
  49. MAC2STR(addr), txt);
  50. else
  51. wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
  52. }
  53. static const u8 *auth_get_psk(void *ctx, const u8 *addr,
  54. const u8 *p2p_dev_addr, const u8 *prev_psk)
  55. {
  56. struct mesh_rsn *mesh_rsn = ctx;
  57. struct hostapd_data *hapd = mesh_rsn->wpa_s->ifmsh->bss[0];
  58. struct sta_info *sta = ap_get_sta(hapd, addr);
  59. wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
  60. __func__, MAC2STR(addr), prev_psk);
  61. if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
  62. if (!sta->sae || prev_psk)
  63. return NULL;
  64. return sta->sae->pmk;
  65. }
  66. return NULL;
  67. }
  68. static int auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
  69. const u8 *addr, int idx, u8 *key, size_t key_len)
  70. {
  71. struct mesh_rsn *mesh_rsn = ctx;
  72. u8 seq[6];
  73. os_memset(seq, 0, sizeof(seq));
  74. if (addr) {
  75. wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d addr=" MACSTR
  76. " key_idx=%d)",
  77. __func__, alg, MAC2STR(addr), idx);
  78. } else {
  79. wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d key_idx=%d)",
  80. __func__, alg, idx);
  81. }
  82. wpa_hexdump_key(MSG_DEBUG, "AUTH: set_key - key", key, key_len);
  83. return wpa_drv_set_key(mesh_rsn->wpa_s, alg, addr, idx,
  84. 1, seq, 6, key, key_len);
  85. }
  86. static int auth_start_ampe(void *ctx, const u8 *addr)
  87. {
  88. struct mesh_rsn *mesh_rsn = ctx;
  89. struct hostapd_data *hapd;
  90. struct sta_info *sta;
  91. if (mesh_rsn->wpa_s->current_ssid->mode != WPAS_MODE_MESH)
  92. return -1;
  93. hapd = mesh_rsn->wpa_s->ifmsh->bss[0];
  94. sta = ap_get_sta(hapd, addr);
  95. if (sta)
  96. eloop_cancel_timeout(mesh_auth_timer, mesh_rsn->wpa_s, sta);
  97. mesh_mpm_auth_peer(mesh_rsn->wpa_s, addr);
  98. return 0;
  99. }
  100. static int __mesh_rsn_auth_init(struct mesh_rsn *rsn, const u8 *addr)
  101. {
  102. struct wpa_auth_config conf;
  103. struct wpa_auth_callbacks cb;
  104. u8 seq[6] = {};
  105. wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
  106. os_memset(&conf, 0, sizeof(conf));
  107. conf.wpa = 2;
  108. conf.wpa_key_mgmt = WPA_KEY_MGMT_SAE;
  109. conf.wpa_pairwise = WPA_CIPHER_CCMP;
  110. conf.rsn_pairwise = WPA_CIPHER_CCMP;
  111. conf.wpa_group = WPA_CIPHER_CCMP;
  112. conf.eapol_version = 0;
  113. conf.wpa_group_rekey = -1;
  114. os_memset(&cb, 0, sizeof(cb));
  115. cb.ctx = rsn;
  116. cb.logger = auth_logger;
  117. cb.get_psk = auth_get_psk;
  118. cb.set_key = auth_set_key;
  119. cb.start_ampe = auth_start_ampe;
  120. rsn->auth = wpa_init(addr, &conf, &cb);
  121. if (rsn->auth == NULL) {
  122. wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
  123. return -1;
  124. }
  125. /* TODO: support rekeying */
  126. if (random_get_bytes(rsn->mgtk, 16) < 0) {
  127. wpa_deinit(rsn->auth);
  128. return -1;
  129. }
  130. /* group mgmt */
  131. wpa_drv_set_key(rsn->wpa_s, WPA_ALG_IGTK, NULL, 4, 1,
  132. seq, sizeof(seq), rsn->mgtk, sizeof(rsn->mgtk));
  133. /* group privacy / data frames */
  134. wpa_drv_set_key(rsn->wpa_s, WPA_ALG_CCMP, NULL, 1, 1,
  135. seq, sizeof(seq), rsn->mgtk, sizeof(rsn->mgtk));
  136. return 0;
  137. }
  138. static void mesh_rsn_deinit(struct mesh_rsn *rsn)
  139. {
  140. os_memset(rsn->mgtk, 0, sizeof(rsn->mgtk));
  141. wpa_deinit(rsn->auth);
  142. }
  143. struct mesh_rsn *mesh_rsn_auth_init(struct wpa_supplicant *wpa_s,
  144. struct mesh_conf *conf)
  145. {
  146. struct mesh_rsn *mesh_rsn;
  147. struct hostapd_data *bss = wpa_s->ifmsh->bss[0];
  148. const u8 *ie;
  149. size_t ie_len;
  150. mesh_rsn = os_zalloc(sizeof(*mesh_rsn));
  151. if (mesh_rsn == NULL)
  152. return NULL;
  153. mesh_rsn->wpa_s = wpa_s;
  154. if (__mesh_rsn_auth_init(mesh_rsn, wpa_s->own_addr) < 0) {
  155. mesh_rsn_deinit(mesh_rsn);
  156. return NULL;
  157. }
  158. bss->wpa_auth = mesh_rsn->auth;
  159. ie = wpa_auth_get_wpa_ie(mesh_rsn->auth, &ie_len);
  160. conf->ies = (u8 *) ie;
  161. conf->ie_len = ie_len;
  162. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  163. return mesh_rsn;
  164. }
  165. static int index_within_array(const int *array, int idx)
  166. {
  167. int i;
  168. for (i = 0; i < idx; i++) {
  169. if (array[i] == -1)
  170. return 0;
  171. }
  172. return 1;
  173. }
  174. static int mesh_rsn_sae_group(struct wpa_supplicant *wpa_s,
  175. struct sae_data *sae)
  176. {
  177. int *groups = wpa_s->ifmsh->bss[0]->conf->sae_groups;
  178. /* Configuration may have changed, so validate current index */
  179. if (!index_within_array(groups, wpa_s->mesh_rsn->sae_group_index))
  180. return -1;
  181. for (;;) {
  182. int group = groups[wpa_s->mesh_rsn->sae_group_index];
  183. if (group <= 0)
  184. break;
  185. if (sae_set_group(sae, group) == 0) {
  186. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected SAE group %d",
  187. sae->group);
  188. return 0;
  189. }
  190. wpa_s->mesh_rsn->sae_group_index++;
  191. }
  192. return -1;
  193. }
  194. struct wpabuf *
  195. mesh_rsn_build_sae_commit(struct wpa_supplicant *wpa_s,
  196. struct wpa_ssid *ssid, struct sta_info *sta)
  197. {
  198. struct wpabuf *buf;
  199. int len;
  200. if (ssid->passphrase == NULL) {
  201. wpa_msg(wpa_s, MSG_DEBUG, "SAE: No password available");
  202. return NULL;
  203. }
  204. if (mesh_rsn_sae_group(wpa_s, sta->sae) < 0) {
  205. wpa_msg(wpa_s, MSG_DEBUG, "SAE: Failed to select group");
  206. return NULL;
  207. }
  208. if (sae_prepare_commit(wpa_s->own_addr, sta->addr,
  209. (u8 *) ssid->passphrase,
  210. os_strlen(ssid->passphrase), sta->sae) < 0) {
  211. wpa_msg(wpa_s, MSG_DEBUG, "SAE: Could not pick PWE");
  212. return NULL;
  213. }
  214. len = wpa_s->mesh_rsn->sae_token ?
  215. wpabuf_len(wpa_s->mesh_rsn->sae_token) : 0;
  216. buf = wpabuf_alloc(4 + SAE_COMMIT_MAX_LEN + len);
  217. if (buf == NULL)
  218. return NULL;
  219. sae_write_commit(sta->sae, buf, wpa_s->mesh_rsn->sae_token);
  220. return buf;
  221. }
  222. static void mesh_rsn_send_auth(struct wpa_supplicant *wpa_s,
  223. const u8 *dst, const u8 *src,
  224. u16 auth_transaction, u16 resp,
  225. struct wpabuf *data)
  226. {
  227. struct ieee80211_mgmt *auth;
  228. u8 *buf;
  229. size_t len, ielen = 0;
  230. if (data)
  231. ielen = wpabuf_len(data);
  232. len = IEEE80211_HDRLEN + sizeof(auth->u.auth) + ielen;
  233. buf = os_zalloc(len);
  234. if (buf == NULL)
  235. return;
  236. auth = (struct ieee80211_mgmt *) buf;
  237. auth->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  238. WLAN_FC_STYPE_AUTH);
  239. os_memcpy(auth->da, dst, ETH_ALEN);
  240. os_memcpy(auth->sa, src, ETH_ALEN);
  241. os_memcpy(auth->bssid, src, ETH_ALEN);
  242. auth->u.auth.auth_alg = host_to_le16(WLAN_AUTH_SAE);
  243. auth->u.auth.auth_transaction = host_to_le16(auth_transaction);
  244. auth->u.auth.status_code = host_to_le16(resp);
  245. if (data)
  246. os_memcpy(auth->u.auth.variable, wpabuf_head(data), ielen);
  247. wpa_msg(wpa_s, MSG_DEBUG, "authentication frame: STA=" MACSTR
  248. " auth_transaction=%d resp=%d (IE len=%lu)",
  249. MAC2STR(dst), auth_transaction, resp, (unsigned long) ielen);
  250. if (wpa_drv_send_mlme(wpa_s, buf, len, 0) < 0)
  251. perror("send_auth_reply: send");
  252. os_free(buf);
  253. }
  254. /* initiate new SAE authentication with sta */
  255. int mesh_rsn_auth_sae_sta(struct wpa_supplicant *wpa_s,
  256. struct sta_info *sta)
  257. {
  258. struct wpa_ssid *ssid = wpa_s->current_ssid;
  259. struct wpabuf *buf;
  260. unsigned int rnd;
  261. if (!ssid) {
  262. wpa_msg(wpa_s, MSG_DEBUG,
  263. "AUTH: No current_ssid known to initiate new SAE");
  264. return -1;
  265. }
  266. if (!sta->sae) {
  267. sta->sae = os_zalloc(sizeof(*sta->sae));
  268. if (sta->sae == NULL)
  269. return -1;
  270. }
  271. buf = mesh_rsn_build_sae_commit(wpa_s, ssid, sta);
  272. if (!buf)
  273. return -1;
  274. wpa_msg(wpa_s, MSG_DEBUG,
  275. "AUTH: started authentication with SAE peer: " MACSTR,
  276. MAC2STR(sta->addr));
  277. sta->sae->state = SAE_COMMITTED;
  278. wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
  279. mesh_rsn_send_auth(wpa_s, sta->addr, wpa_s->own_addr,
  280. 1, WLAN_STATUS_SUCCESS, buf);
  281. rnd = rand() % MESH_AUTH_TIMEOUT;
  282. eloop_register_timeout(MESH_AUTH_TIMEOUT + rnd, 0, mesh_auth_timer,
  283. wpa_s, sta);
  284. wpabuf_free(buf);
  285. return 0;
  286. }
  287. void mesh_rsn_get_pmkid(struct mesh_rsn *rsn, struct sta_info *sta, u8 *pmkid)
  288. {
  289. /* don't expect wpa auth to cache the pmkid for now */
  290. rsn_pmkid(sta->sae->pmk, PMK_LEN, rsn->wpa_s->own_addr,
  291. sta->addr, pmkid,
  292. wpa_key_mgmt_sha256(wpa_auth_sta_key_mgmt(sta->wpa_sm)));
  293. }
  294. static void
  295. mesh_rsn_derive_aek(struct mesh_rsn *rsn, struct sta_info *sta)
  296. {
  297. u8 *myaddr = rsn->wpa_s->own_addr;
  298. u8 *peer = sta->addr;
  299. u8 *addr1 = peer, *addr2 = myaddr;
  300. u8 context[AES_BLOCK_SIZE];
  301. /* SAE */
  302. RSN_SELECTOR_PUT(context, wpa_cipher_to_suite(0, WPA_CIPHER_GCMP));
  303. if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
  304. addr1 = myaddr;
  305. addr2 = peer;
  306. }
  307. os_memcpy(context + 4, addr1, ETH_ALEN);
  308. os_memcpy(context + 10, addr2, ETH_ALEN);
  309. sha256_prf(sta->sae->pmk, sizeof(sta->sae->pmk), "AEK Derivation",
  310. context, sizeof(context), sta->aek, sizeof(sta->aek));
  311. }
  312. /* derive mesh temporal key from pmk */
  313. int mesh_rsn_derive_mtk(struct wpa_supplicant *wpa_s, struct sta_info *sta)
  314. {
  315. u8 *ptr;
  316. u8 *min, *max;
  317. u16 min_lid, max_lid;
  318. size_t nonce_len = sizeof(sta->my_nonce);
  319. size_t lid_len = sizeof(sta->my_lid);
  320. u8 *myaddr = wpa_s->own_addr;
  321. u8 *peer = sta->addr;
  322. /* 2 nonces, 2 linkids, akm suite, 2 mac addrs */
  323. u8 context[64 + 4 + 4 + 12];
  324. ptr = context;
  325. if (os_memcmp(sta->my_nonce, sta->peer_nonce, nonce_len) < 0) {
  326. min = sta->my_nonce;
  327. max = sta->peer_nonce;
  328. } else {
  329. min = sta->peer_nonce;
  330. max = sta->my_nonce;
  331. }
  332. os_memcpy(ptr, min, nonce_len);
  333. os_memcpy(ptr + nonce_len, max, nonce_len);
  334. ptr += 2 * nonce_len;
  335. if (sta->my_lid < sta->peer_lid) {
  336. min_lid = host_to_le16(sta->my_lid);
  337. max_lid = host_to_le16(sta->peer_lid);
  338. } else {
  339. min_lid = host_to_le16(sta->peer_lid);
  340. max_lid = host_to_le16(sta->my_lid);
  341. }
  342. os_memcpy(ptr, &min_lid, lid_len);
  343. os_memcpy(ptr + lid_len, &max_lid, lid_len);
  344. ptr += 2 * lid_len;
  345. /* SAE */
  346. RSN_SELECTOR_PUT(ptr, wpa_cipher_to_suite(0, WPA_CIPHER_GCMP));
  347. ptr += 4;
  348. if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
  349. min = myaddr;
  350. max = peer;
  351. } else {
  352. min = peer;
  353. max = myaddr;
  354. }
  355. os_memcpy(ptr, min, ETH_ALEN);
  356. os_memcpy(ptr + ETH_ALEN, max, ETH_ALEN);
  357. sha256_prf(sta->sae->pmk, sizeof(sta->sae->pmk),
  358. "Temporal Key Derivation", context, sizeof(context),
  359. sta->mtk, sizeof(sta->mtk));
  360. return 0;
  361. }
  362. void mesh_rsn_init_ampe_sta(struct wpa_supplicant *wpa_s, struct sta_info *sta)
  363. {
  364. if (random_get_bytes(sta->my_nonce, 32) < 0) {
  365. wpa_printf(MSG_INFO, "mesh: Failed to derive random nonce");
  366. /* TODO: How to handle this more cleanly? */
  367. }
  368. os_memset(sta->peer_nonce, 0, 32);
  369. mesh_rsn_derive_aek(wpa_s->mesh_rsn, sta);
  370. }
  371. /* insert AMPE and encrypted MIC at @ie.
  372. * @mesh_rsn: mesh RSN context
  373. * @sta: STA we're sending to
  374. * @cat: pointer to category code in frame header.
  375. * @buf: wpabuf to add encrypted AMPE and MIC to.
  376. * */
  377. int mesh_rsn_protect_frame(struct mesh_rsn *rsn, struct sta_info *sta,
  378. const u8 *cat, struct wpabuf *buf)
  379. {
  380. struct ieee80211_ampe_ie *ampe;
  381. u8 const *ie = wpabuf_head_u8(buf) + wpabuf_len(buf);
  382. u8 *ampe_ie = NULL, *mic_ie = NULL, *mic_payload;
  383. const u8 *aad[] = { rsn->wpa_s->own_addr, sta->addr, cat };
  384. const size_t aad_len[] = { ETH_ALEN, ETH_ALEN, ie - cat };
  385. int ret = 0;
  386. if (AES_BLOCK_SIZE + 2 + sizeof(*ampe) + 2 > wpabuf_tailroom(buf)) {
  387. wpa_printf(MSG_ERROR, "protect frame: buffer too small");
  388. return -EINVAL;
  389. }
  390. ampe_ie = os_zalloc(2 + sizeof(*ampe));
  391. if (!ampe_ie) {
  392. wpa_printf(MSG_ERROR, "protect frame: out of memory");
  393. return -ENOMEM;
  394. }
  395. mic_ie = os_zalloc(2 + AES_BLOCK_SIZE);
  396. if (!mic_ie) {
  397. wpa_printf(MSG_ERROR, "protect frame: out of memory");
  398. ret = -ENOMEM;
  399. goto free;
  400. }
  401. /* IE: AMPE */
  402. ampe_ie[0] = WLAN_EID_AMPE;
  403. ampe_ie[1] = sizeof(*ampe);
  404. ampe = (struct ieee80211_ampe_ie *) (ampe_ie + 2);
  405. RSN_SELECTOR_PUT(ampe->selected_pairwise_suite,
  406. wpa_cipher_to_suite(WPA_PROTO_RSN, WPA_CIPHER_CCMP));
  407. os_memcpy(ampe->local_nonce, sta->my_nonce, 32);
  408. os_memcpy(ampe->peer_nonce, sta->peer_nonce, 32);
  409. /* incomplete: see 13.5.4 */
  410. /* TODO: static mgtk for now since we don't support rekeying! */
  411. os_memcpy(ampe->mgtk, rsn->mgtk, 16);
  412. /* TODO: Populate Key RSC */
  413. /* expire in 13 decades or so */
  414. os_memset(ampe->key_expiration, 0xff, 4);
  415. /* IE: MIC */
  416. mic_ie[0] = WLAN_EID_MIC;
  417. mic_ie[1] = AES_BLOCK_SIZE;
  418. wpabuf_put_data(buf, mic_ie, 2);
  419. /* MIC field is output ciphertext */
  420. /* encrypt after MIC */
  421. mic_payload = (u8 *) wpabuf_put(buf, 2 + sizeof(*ampe) +
  422. AES_BLOCK_SIZE);
  423. if (aes_siv_encrypt(sta->aek, ampe_ie, 2 + sizeof(*ampe), 3,
  424. aad, aad_len, mic_payload)) {
  425. wpa_printf(MSG_ERROR, "protect frame: failed to encrypt");
  426. ret = -ENOMEM;
  427. goto free;
  428. }
  429. free:
  430. os_free(ampe_ie);
  431. os_free(mic_ie);
  432. return ret;
  433. }
  434. int mesh_rsn_process_ampe(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  435. struct ieee802_11_elems *elems, const u8 *cat,
  436. const u8 *start, size_t elems_len)
  437. {
  438. int ret = 0;
  439. struct ieee80211_ampe_ie *ampe;
  440. u8 null_nonce[32] = {};
  441. u8 ampe_eid;
  442. u8 ampe_ie_len;
  443. u8 *ampe_buf, *crypt = NULL;
  444. size_t crypt_len;
  445. const u8 *aad[] = { sta->addr, wpa_s->own_addr, cat };
  446. const size_t aad_len[] = { ETH_ALEN, ETH_ALEN,
  447. (elems->mic - 2) - cat };
  448. if (!elems->mic || elems->mic_len < AES_BLOCK_SIZE) {
  449. wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: missing mic ie");
  450. return -1;
  451. }
  452. ampe_buf = (u8 *) elems->mic + elems->mic_len;
  453. if ((int) elems_len < ampe_buf - start)
  454. return -1;
  455. crypt_len = elems_len - (elems->mic - start);
  456. if (crypt_len < 2) {
  457. wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: missing ampe ie");
  458. return -1;
  459. }
  460. /* crypt is modified by siv_decrypt */
  461. crypt = os_zalloc(crypt_len);
  462. if (!crypt) {
  463. wpa_printf(MSG_ERROR, "Mesh RSN: out of memory");
  464. ret = -ENOMEM;
  465. goto free;
  466. }
  467. os_memcpy(crypt, elems->mic, crypt_len);
  468. if (aes_siv_decrypt(sta->aek, crypt, crypt_len, 3,
  469. aad, aad_len, ampe_buf)) {
  470. wpa_printf(MSG_ERROR, "Mesh RSN: frame verification failed!");
  471. ret = -1;
  472. goto free;
  473. }
  474. ampe_eid = *ampe_buf++;
  475. ampe_ie_len = *ampe_buf++;
  476. if (ampe_eid != WLAN_EID_AMPE ||
  477. ampe_ie_len < sizeof(struct ieee80211_ampe_ie)) {
  478. wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid ampe ie");
  479. ret = -1;
  480. goto free;
  481. }
  482. ampe = (struct ieee80211_ampe_ie *) ampe_buf;
  483. if (os_memcmp(ampe->peer_nonce, null_nonce, 32) != 0 &&
  484. os_memcmp(ampe->peer_nonce, sta->my_nonce, 32) != 0) {
  485. wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid peer nonce");
  486. ret = -1;
  487. goto free;
  488. }
  489. os_memcpy(sta->peer_nonce, ampe->local_nonce,
  490. sizeof(ampe->local_nonce));
  491. os_memcpy(sta->mgtk, ampe->mgtk, sizeof(ampe->mgtk));
  492. /* todo parse mgtk expiration */
  493. free:
  494. os_free(crypt);
  495. return ret;
  496. }