mesh: Fix AEK derivation to use AKM suite selector
[mech_eap.git] / wpa_supplicant / mesh_rsn.c
index 4ee3431..57e5b15 100644 (file)
@@ -18,6 +18,7 @@
 #include "ap/hostapd.h"
 #include "ap/wpa_auth.h"
 #include "ap/sta_info.h"
+#include "ap/ieee802_11.h"
 #include "wpa_supplicant_i.h"
 #include "driver_i.h"
 #include "wpas_glue.h"
@@ -31,17 +32,31 @@ void mesh_auth_timer(void *eloop_ctx, void *user_data)
 {
        struct wpa_supplicant *wpa_s = eloop_ctx;
        struct sta_info *sta = user_data;
+       struct hostapd_data *hapd;
 
        if (sta->sae->state != SAE_ACCEPTED) {
                wpa_printf(MSG_DEBUG, "AUTH: Re-authenticate with " MACSTR
                           " (attempt %d) ",
                           MAC2STR(sta->addr), sta->sae_auth_retry);
+               wpa_msg(wpa_s, MSG_INFO, MESH_SAE_AUTH_FAILURE "addr=" MACSTR,
+                       MAC2STR(sta->addr));
                if (sta->sae_auth_retry < MESH_AUTH_RETRY) {
                        mesh_rsn_auth_sae_sta(wpa_s, sta);
                } else {
+                       hapd = wpa_s->ifmsh->bss[0];
+
+                       if (sta->sae_auth_retry > MESH_AUTH_RETRY) {
+                               ap_free_sta(hapd, sta);
+                               return;
+                       }
+
                        /* block the STA if exceeded the number of attempts */
-                       sta->plink_state = PLINK_BLOCKED;
+                       wpa_mesh_set_plink_state(wpa_s, sta, PLINK_BLOCKED);
                        sta->sae->state = SAE_NOTHING;
+                       wpa_msg(wpa_s, MSG_INFO, MESH_SAE_AUTH_BLOCKED "addr="
+                               MACSTR " duration=%d",
+                               MAC2STR(sta->addr),
+                               hapd->conf->ap_max_inactivity);
                }
                sta->sae_auth_retry++;
        }
@@ -121,7 +136,8 @@ static int auth_start_ampe(void *ctx, const u8 *addr)
 }
 
 
-static int __mesh_rsn_auth_init(struct mesh_rsn *rsn, const u8 *addr)
+static int __mesh_rsn_auth_init(struct mesh_rsn *rsn, const u8 *addr,
+                               enum mfp_options ieee80211w)
 {
        struct wpa_auth_config conf;
        struct wpa_auth_callbacks cb;
@@ -137,6 +153,11 @@ static int __mesh_rsn_auth_init(struct mesh_rsn *rsn, const u8 *addr)
        conf.wpa_group = WPA_CIPHER_CCMP;
        conf.eapol_version = 0;
        conf.wpa_group_rekey = -1;
+#ifdef CONFIG_IEEE80211W
+       conf.ieee80211w = ieee80211w;
+       if (ieee80211w != NO_MGMT_FRAME_PROTECTION)
+               conf.group_mgmt_cipher = WPA_CIPHER_AES_128_CMAC;
+#endif /* CONFIG_IEEE80211W */
 
        os_memset(&cb, 0, sizeof(cb));
        cb.ctx = rsn;
@@ -152,10 +173,8 @@ static int __mesh_rsn_auth_init(struct mesh_rsn *rsn, const u8 *addr)
        }
 
        /* TODO: support rekeying */
-       if (random_get_bytes(rsn->mgtk, 16) < 0) {
-               wpa_deinit(rsn->auth);
+       if (random_get_bytes(rsn->mgtk, 16) < 0)
                return -1;
-       }
 
        /* group mgmt */
        wpa_drv_set_key(rsn->wpa_s, WPA_ALG_IGTK, NULL, 4, 1,
@@ -172,7 +191,8 @@ static int __mesh_rsn_auth_init(struct mesh_rsn *rsn, const u8 *addr)
 static void mesh_rsn_deinit(struct mesh_rsn *rsn)
 {
        os_memset(rsn->mgtk, 0, sizeof(rsn->mgtk));
-       wpa_deinit(rsn->auth);
+       if (rsn->auth)
+               wpa_deinit(rsn->auth);
 }
 
 
@@ -189,16 +209,18 @@ struct mesh_rsn *mesh_rsn_auth_init(struct wpa_supplicant *wpa_s,
                return NULL;
        mesh_rsn->wpa_s = wpa_s;
 
-       if (__mesh_rsn_auth_init(mesh_rsn, wpa_s->own_addr) < 0) {
+       if (__mesh_rsn_auth_init(mesh_rsn, wpa_s->own_addr,
+                                conf->ieee80211w) < 0) {
                mesh_rsn_deinit(mesh_rsn);
+               os_free(mesh_rsn);
                return NULL;
        }
 
        bss->wpa_auth = mesh_rsn->auth;
 
        ie = wpa_auth_get_wpa_ie(mesh_rsn->auth, &ie_len);
-       conf->ies = (u8 *) ie;
-       conf->ie_len = ie_len;
+       conf->rsn_ie = (u8 *) ie;
+       conf->rsn_ie_len = ie_len;
 
        wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
 
@@ -245,79 +267,23 @@ static int mesh_rsn_sae_group(struct wpa_supplicant *wpa_s,
 }
 
 
-struct wpabuf *
-mesh_rsn_build_sae_commit(struct wpa_supplicant *wpa_s,
-                         struct wpa_ssid *ssid, struct sta_info *sta)
+static int mesh_rsn_build_sae_commit(struct wpa_supplicant *wpa_s,
+                                    struct wpa_ssid *ssid,
+                                    struct sta_info *sta)
 {
-       struct wpabuf *buf;
-       int len;
-
        if (ssid->passphrase == NULL) {
                wpa_msg(wpa_s, MSG_DEBUG, "SAE: No password available");
-               return NULL;
+               return -1;
        }
 
        if (mesh_rsn_sae_group(wpa_s, sta->sae) < 0) {
                wpa_msg(wpa_s, MSG_DEBUG, "SAE: Failed to select group");
-               return NULL;
-       }
-
-       if (sae_prepare_commit(wpa_s->own_addr, sta->addr,
-                              (u8 *) ssid->passphrase,
-                              os_strlen(ssid->passphrase), sta->sae) < 0) {
-               wpa_msg(wpa_s, MSG_DEBUG, "SAE: Could not pick PWE");
-               return NULL;
+               return -1;
        }
 
-       len = wpa_s->mesh_rsn->sae_token ?
-               wpabuf_len(wpa_s->mesh_rsn->sae_token) : 0;
-       buf = wpabuf_alloc(4 + SAE_COMMIT_MAX_LEN + len);
-       if (buf == NULL)
-               return NULL;
-
-       sae_write_commit(sta->sae, buf, wpa_s->mesh_rsn->sae_token);
-
-       return buf;
-}
-
-
-static void mesh_rsn_send_auth(struct wpa_supplicant *wpa_s,
-                              const u8 *dst, const u8 *src,
-                              u16 auth_transaction, u16 resp,
-                              struct wpabuf *data)
-{
-       struct ieee80211_mgmt *auth;
-       u8 *buf;
-       size_t len, ielen = 0;
-
-       if (data)
-               ielen = wpabuf_len(data);
-       len = IEEE80211_HDRLEN + sizeof(auth->u.auth) + ielen;
-       buf = os_zalloc(len);
-       if (buf == NULL)
-               return;
-
-       auth = (struct ieee80211_mgmt *) buf;
-       auth->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
-                                          WLAN_FC_STYPE_AUTH);
-       os_memcpy(auth->da, dst, ETH_ALEN);
-       os_memcpy(auth->sa, src, ETH_ALEN);
-       os_memcpy(auth->bssid, src, ETH_ALEN);
-
-       auth->u.auth.auth_alg = host_to_le16(WLAN_AUTH_SAE);
-       auth->u.auth.auth_transaction = host_to_le16(auth_transaction);
-       auth->u.auth.status_code = host_to_le16(resp);
-
-       if (data)
-               os_memcpy(auth->u.auth.variable, wpabuf_head(data), ielen);
-
-       wpa_msg(wpa_s, MSG_DEBUG, "authentication frame: STA=" MACSTR
-               " auth_transaction=%d resp=%d (IE len=%lu)",
-               MAC2STR(dst), auth_transaction, resp, (unsigned long) ielen);
-       if (wpa_drv_send_mlme(wpa_s, buf, len, 0) < 0)
-               perror("send_auth_reply: send");
-
-       os_free(buf);
+       return sae_prepare_commit(wpa_s->own_addr, sta->addr,
+                                 (u8 *) ssid->passphrase,
+                                 os_strlen(ssid->passphrase), sta->sae);
 }
 
 
@@ -325,9 +291,17 @@ static void mesh_rsn_send_auth(struct wpa_supplicant *wpa_s,
 int mesh_rsn_auth_sae_sta(struct wpa_supplicant *wpa_s,
                          struct sta_info *sta)
 {
+       struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
        struct wpa_ssid *ssid = wpa_s->current_ssid;
-       struct wpabuf *buf;
+       struct rsn_pmksa_cache_entry *pmksa;
        unsigned int rnd;
+       int ret;
+
+       if (!ssid) {
+               wpa_msg(wpa_s, MSG_DEBUG,
+                       "AUTH: No current_ssid known to initiate new SAE");
+               return -1;
+       }
 
        if (!sta->sae) {
                sta->sae = os_zalloc(sizeof(*sta->sae));
@@ -335,35 +309,52 @@ int mesh_rsn_auth_sae_sta(struct wpa_supplicant *wpa_s,
                        return -1;
        }
 
-       buf = mesh_rsn_build_sae_commit(wpa_s, ssid, sta);
-       if (!buf)
+       pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr);
+       if (pmksa) {
+               if (!sta->wpa_sm)
+                       sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
+                                                       sta->addr, NULL);
+               if (!sta->wpa_sm) {
+                       wpa_printf(MSG_ERROR,
+                                  "mesh: Failed to initialize RSN state machine");
+                       return -1;
+               }
+
+               wpa_printf(MSG_DEBUG,
+                          "AUTH: Mesh PMKSA cache entry found for " MACSTR
+                          " - try to use PMKSA caching instead of new SAE authentication",
+                          MAC2STR(sta->addr));
+               wpa_auth_pmksa_set_to_sm(pmksa, sta->wpa_sm, hapd->wpa_auth,
+                                        sta->sae->pmkid, sta->sae->pmk);
+               sae_accept_sta(hapd, sta);
+               sta->mesh_sae_pmksa_caching = 1;
+               return 0;
+       }
+       sta->mesh_sae_pmksa_caching = 0;
+
+       if (mesh_rsn_build_sae_commit(wpa_s, ssid, sta))
                return -1;
 
        wpa_msg(wpa_s, MSG_DEBUG,
                "AUTH: started authentication with SAE peer: " MACSTR,
                MAC2STR(sta->addr));
 
-       sta->sae->state = SAE_COMMITTED;
        wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
+       ret = auth_sae_init_committed(hapd, sta);
+       if (ret)
+               return ret;
 
-       mesh_rsn_send_auth(wpa_s, sta->addr, wpa_s->own_addr,
-                          1, WLAN_STATUS_SUCCESS, buf);
-
+       eloop_cancel_timeout(mesh_auth_timer, wpa_s, sta);
        rnd = rand() % MESH_AUTH_TIMEOUT;
        eloop_register_timeout(MESH_AUTH_TIMEOUT + rnd, 0, mesh_auth_timer,
                               wpa_s, sta);
-       wpabuf_free(buf);
-
        return 0;
 }
 
 
 void mesh_rsn_get_pmkid(struct mesh_rsn *rsn, struct sta_info *sta, u8 *pmkid)
 {
-       /* don't expect wpa auth to cache the pmkid for now */
-       rsn_pmkid(sta->sae->pmk, PMK_LEN, rsn->wpa_s->own_addr,
-                 sta->addr, pmkid,
-                 wpa_key_mgmt_sha256(wpa_auth_sta_key_mgmt(sta->wpa_sm)));
+       os_memcpy(pmkid, sta->sae->pmkid, SAE_PMKID_LEN);
 }
 
 
@@ -375,8 +366,8 @@ mesh_rsn_derive_aek(struct mesh_rsn *rsn, struct sta_info *sta)
        u8 *addr1 = peer, *addr2 = myaddr;
        u8 context[AES_BLOCK_SIZE];
 
-       /* SAE */
-       RSN_SELECTOR_PUT(context, wpa_cipher_to_suite(0, WPA_CIPHER_GCMP));
+       /* Selected AKM Suite: SAE */
+       RSN_SELECTOR_PUT(context, RSN_AUTH_KEY_MGMT_SAE);
 
        if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
                addr1 = myaddr;
@@ -396,7 +387,6 @@ int mesh_rsn_derive_mtk(struct wpa_supplicant *wpa_s, struct sta_info *sta)
        u8 *ptr;
        u8 *min, *max;
        u16 min_lid, max_lid;
-       size_t nonce_len = sizeof(sta->my_nonce);
        size_t lid_len = sizeof(sta->my_lid);
        u8 *myaddr = wpa_s->own_addr;
        u8 *peer = sta->addr;
@@ -404,16 +394,16 @@ int mesh_rsn_derive_mtk(struct wpa_supplicant *wpa_s, struct sta_info *sta)
        u8 context[64 + 4 + 4 + 12];
 
        ptr = context;
-       if (os_memcmp(sta->my_nonce, sta->peer_nonce, nonce_len) < 0) {
+       if (os_memcmp(sta->my_nonce, sta->peer_nonce, WPA_NONCE_LEN) < 0) {
                min = sta->my_nonce;
                max = sta->peer_nonce;
        } else {
                min = sta->peer_nonce;
                max = sta->my_nonce;
        }
-       os_memcpy(ptr, min, nonce_len);
-       os_memcpy(ptr + nonce_len, max, nonce_len);
-       ptr += 2 * nonce_len;
+       os_memcpy(ptr, min, WPA_NONCE_LEN);
+       os_memcpy(ptr + WPA_NONCE_LEN, max, WPA_NONCE_LEN);
+       ptr += 2 * WPA_NONCE_LEN;
 
        if (sta->my_lid < sta->peer_lid) {
                min_lid = host_to_le16(sta->my_lid);
@@ -449,11 +439,11 @@ int mesh_rsn_derive_mtk(struct wpa_supplicant *wpa_s, struct sta_info *sta)
 
 void mesh_rsn_init_ampe_sta(struct wpa_supplicant *wpa_s, struct sta_info *sta)
 {
-       if (random_get_bytes(sta->my_nonce, 32) < 0) {
+       if (random_get_bytes(sta->my_nonce, WPA_NONCE_LEN) < 0) {
                wpa_printf(MSG_INFO, "mesh: Failed to derive random nonce");
                /* TODO: How to handle this more cleanly? */
        }
-       os_memset(sta->peer_nonce, 0, 32);
+       os_memset(sta->peer_nonce, 0, WPA_NONCE_LEN);
        mesh_rsn_derive_aek(wpa_s->mesh_rsn, sta);
 }
 
@@ -499,8 +489,8 @@ int mesh_rsn_protect_frame(struct mesh_rsn *rsn, struct sta_info *sta,
 
        RSN_SELECTOR_PUT(ampe->selected_pairwise_suite,
                     wpa_cipher_to_suite(WPA_PROTO_RSN, WPA_CIPHER_CCMP));
-       os_memcpy(ampe->local_nonce, sta->my_nonce, 32);
-       os_memcpy(ampe->peer_nonce, sta->peer_nonce, 32);
+       os_memcpy(ampe->local_nonce, sta->my_nonce, WPA_NONCE_LEN);
+       os_memcpy(ampe->peer_nonce, sta->peer_nonce, WPA_NONCE_LEN);
        /* incomplete: see 13.5.4 */
        /* TODO: static mgtk for now since we don't support rekeying! */
        os_memcpy(ampe->mgtk, rsn->mgtk, 16);
@@ -535,11 +525,12 @@ free:
 
 int mesh_rsn_process_ampe(struct wpa_supplicant *wpa_s, struct sta_info *sta,
                          struct ieee802_11_elems *elems, const u8 *cat,
+                         const u8 *chosen_pmk,
                          const u8 *start, size_t elems_len)
 {
        int ret = 0;
        struct ieee80211_ampe_ie *ampe;
-       u8 null_nonce[32] = {};
+       u8 null_nonce[WPA_NONCE_LEN] = {};
        u8 ampe_eid;
        u8 ampe_ie_len;
        u8 *ampe_buf, *crypt = NULL;
@@ -548,6 +539,23 @@ int mesh_rsn_process_ampe(struct wpa_supplicant *wpa_s, struct sta_info *sta,
        const size_t aad_len[] = { ETH_ALEN, ETH_ALEN,
                                   (elems->mic - 2) - cat };
 
+       if (!sta->sae) {
+               struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
+
+               if (!wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr)) {
+                       wpa_printf(MSG_INFO,
+                                  "Mesh RSN: SAE is not prepared yet");
+                       return -1;
+               }
+               mesh_rsn_auth_sae_sta(wpa_s, sta);
+       }
+
+       if (chosen_pmk && os_memcmp(chosen_pmk, sta->sae->pmkid, PMKID_LEN)) {
+               wpa_msg(wpa_s, MSG_DEBUG,
+                       "Mesh RSN: Invalid PMKID (Chosen PMK did not match calculated PMKID)");
+               return -1;
+       }
+
        if (!elems->mic || elems->mic_len < AES_BLOCK_SIZE) {
                wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: missing mic ie");
                return -1;
@@ -591,8 +599,8 @@ int mesh_rsn_process_ampe(struct wpa_supplicant *wpa_s, struct sta_info *sta,
        }
 
        ampe = (struct ieee80211_ampe_ie *) ampe_buf;
-       if (os_memcmp(ampe->peer_nonce, null_nonce, 32) != 0 &&
-           os_memcmp(ampe->peer_nonce, sta->my_nonce, 32) != 0) {
+       if (os_memcmp(ampe->peer_nonce, null_nonce, WPA_NONCE_LEN) != 0 &&
+           os_memcmp(ampe->peer_nonce, sta->my_nonce, WPA_NONCE_LEN) != 0) {
                wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid peer nonce");
                ret = -1;
                goto free;