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