mesh_rsn.c 15 KB

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