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

Generated by: LCOV version 1.10