LCOV - code coverage report
Current view: top level - wpa_supplicant - mesh_rsn.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1422976643 Lines: 211 282 74.8 %
Date: 2015-02-03 Functions: 16 18 88.9 %

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

Generated by: LCOV version 1.10