2 * WPA Supplicant - Mesh RSN routines
3 * Copyright (c) 2013-2014, cozybit, Inc. All rights reserved.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
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"
24 #include "wpas_glue.h"
28 #define MESH_AUTH_TIMEOUT 10
29 #define MESH_AUTH_RETRY 3
31 void mesh_auth_timer(void *eloop_ctx, void *user_data)
33 struct wpa_supplicant *wpa_s = eloop_ctx;
34 struct sta_info *sta = user_data;
35 struct hostapd_data *hapd;
37 if (sta->sae->state != SAE_ACCEPTED) {
38 wpa_printf(MSG_DEBUG, "AUTH: Re-authenticate with " MACSTR
40 MAC2STR(sta->addr), sta->sae_auth_retry);
41 wpa_msg(wpa_s, MSG_INFO, MESH_SAE_AUTH_FAILURE "addr=" MACSTR,
43 if (sta->sae_auth_retry < MESH_AUTH_RETRY) {
44 mesh_rsn_auth_sae_sta(wpa_s, sta);
46 hapd = wpa_s->ifmsh->bss[0];
48 if (sta->sae_auth_retry > MESH_AUTH_RETRY) {
49 ap_free_sta(hapd, sta);
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",
59 hapd->conf->ap_max_inactivity);
61 sta->sae_auth_retry++;
66 static void auth_logger(void *ctx, const u8 *addr, logger_level level,
70 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
73 wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
77 static const u8 *auth_get_psk(void *ctx, const u8 *addr,
78 const u8 *p2p_dev_addr, const u8 *prev_psk)
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);
84 wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
85 __func__, MAC2STR(addr), prev_psk);
87 if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
88 if (!sta->sae || prev_psk)
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)
100 struct mesh_rsn *mesh_rsn = ctx;
103 os_memset(seq, 0, sizeof(seq));
106 wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d addr=" MACSTR
108 __func__, alg, MAC2STR(addr), idx);
110 wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d key_idx=%d)",
113 wpa_hexdump_key(MSG_DEBUG, "AUTH: set_key - key", key, key_len);
115 return wpa_drv_set_key(mesh_rsn->wpa_s, alg, addr, idx,
116 1, seq, 6, key, key_len);
120 static int auth_start_ampe(void *ctx, const u8 *addr)
122 struct mesh_rsn *mesh_rsn = ctx;
123 struct hostapd_data *hapd;
124 struct sta_info *sta;
126 if (mesh_rsn->wpa_s->current_ssid->mode != WPAS_MODE_MESH)
129 hapd = mesh_rsn->wpa_s->ifmsh->bss[0];
130 sta = ap_get_sta(hapd, addr);
132 eloop_cancel_timeout(mesh_auth_timer, mesh_rsn->wpa_s, sta);
134 mesh_mpm_auth_peer(mesh_rsn->wpa_s, addr);
139 static int __mesh_rsn_auth_init(struct mesh_rsn *rsn, const u8 *addr,
140 enum mfp_options ieee80211w)
142 struct wpa_auth_config conf;
143 struct wpa_auth_callbacks cb;
146 wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
148 os_memset(&conf, 0, sizeof(conf));
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 */
162 os_memset(&cb, 0, sizeof(cb));
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;
169 rsn->auth = wpa_init(addr, &conf, &cb);
170 if (rsn->auth == NULL) {
171 wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
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)
181 wpa_drv_set_key(rsn->wpa_s, WPA_ALG_IGTK, NULL, 4, 1,
182 seq, sizeof(seq), rsn->mgtk, sizeof(rsn->mgtk));
184 /* group privacy / data frames */
185 wpa_hexdump_key(MSG_DEBUG, "mesh: Own TX MGTK",
186 rsn->mgtk, rsn->mgtk_len);
187 wpa_drv_set_key(rsn->wpa_s, WPA_ALG_CCMP, NULL, 1, 1,
188 seq, sizeof(seq), rsn->mgtk, rsn->mgtk_len);
194 static void mesh_rsn_deinit(struct mesh_rsn *rsn)
196 os_memset(rsn->mgtk, 0, sizeof(rsn->mgtk));
199 wpa_deinit(rsn->auth);
203 struct mesh_rsn *mesh_rsn_auth_init(struct wpa_supplicant *wpa_s,
204 struct mesh_conf *conf)
206 struct mesh_rsn *mesh_rsn;
207 struct hostapd_data *bss = wpa_s->ifmsh->bss[0];
211 mesh_rsn = os_zalloc(sizeof(*mesh_rsn));
212 if (mesh_rsn == NULL)
214 mesh_rsn->wpa_s = wpa_s;
216 if (__mesh_rsn_auth_init(mesh_rsn, wpa_s->own_addr,
217 conf->ieee80211w) < 0) {
218 mesh_rsn_deinit(mesh_rsn);
223 bss->wpa_auth = mesh_rsn->auth;
225 ie = wpa_auth_get_wpa_ie(mesh_rsn->auth, &ie_len);
226 conf->rsn_ie = (u8 *) ie;
227 conf->rsn_ie_len = ie_len;
229 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
235 static int index_within_array(const int *array, int idx)
239 for (i = 0; i < idx; i++) {
248 static int mesh_rsn_sae_group(struct wpa_supplicant *wpa_s,
249 struct sae_data *sae)
251 int *groups = wpa_s->ifmsh->bss[0]->conf->sae_groups;
253 /* Configuration may have changed, so validate current index */
254 if (!index_within_array(groups, wpa_s->mesh_rsn->sae_group_index))
258 int group = groups[wpa_s->mesh_rsn->sae_group_index];
262 if (sae_set_group(sae, group) == 0) {
263 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected SAE group %d",
267 wpa_s->mesh_rsn->sae_group_index++;
274 static int mesh_rsn_build_sae_commit(struct wpa_supplicant *wpa_s,
275 struct wpa_ssid *ssid,
276 struct sta_info *sta)
278 if (ssid->passphrase == NULL) {
279 wpa_msg(wpa_s, MSG_DEBUG, "SAE: No password available");
283 if (mesh_rsn_sae_group(wpa_s, sta->sae) < 0) {
284 wpa_msg(wpa_s, MSG_DEBUG, "SAE: Failed to select group");
288 return sae_prepare_commit(wpa_s->own_addr, sta->addr,
289 (u8 *) ssid->passphrase,
290 os_strlen(ssid->passphrase), sta->sae);
294 /* initiate new SAE authentication with sta */
295 int mesh_rsn_auth_sae_sta(struct wpa_supplicant *wpa_s,
296 struct sta_info *sta)
298 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
299 struct wpa_ssid *ssid = wpa_s->current_ssid;
300 struct rsn_pmksa_cache_entry *pmksa;
305 wpa_msg(wpa_s, MSG_DEBUG,
306 "AUTH: No current_ssid known to initiate new SAE");
311 sta->sae = os_zalloc(sizeof(*sta->sae));
312 if (sta->sae == NULL)
316 pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr);
319 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
322 wpa_printf(MSG_ERROR,
323 "mesh: Failed to initialize RSN state machine");
327 wpa_printf(MSG_DEBUG,
328 "AUTH: Mesh PMKSA cache entry found for " MACSTR
329 " - try to use PMKSA caching instead of new SAE authentication",
331 wpa_auth_pmksa_set_to_sm(pmksa, sta->wpa_sm, hapd->wpa_auth,
332 sta->sae->pmkid, sta->sae->pmk);
333 sae_accept_sta(hapd, sta);
334 sta->mesh_sae_pmksa_caching = 1;
337 sta->mesh_sae_pmksa_caching = 0;
339 if (mesh_rsn_build_sae_commit(wpa_s, ssid, sta))
342 wpa_msg(wpa_s, MSG_DEBUG,
343 "AUTH: started authentication with SAE peer: " MACSTR,
346 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
347 ret = auth_sae_init_committed(hapd, sta);
351 eloop_cancel_timeout(mesh_auth_timer, wpa_s, sta);
352 rnd = rand() % MESH_AUTH_TIMEOUT;
353 eloop_register_timeout(MESH_AUTH_TIMEOUT + rnd, 0, mesh_auth_timer,
359 void mesh_rsn_get_pmkid(struct mesh_rsn *rsn, struct sta_info *sta, u8 *pmkid)
361 os_memcpy(pmkid, sta->sae->pmkid, SAE_PMKID_LEN);
366 mesh_rsn_derive_aek(struct mesh_rsn *rsn, struct sta_info *sta)
368 u8 *myaddr = rsn->wpa_s->own_addr;
369 u8 *peer = sta->addr;
371 u8 context[RSN_SELECTOR_LEN + 2 * ETH_ALEN], *ptr = context;
374 * AEK = KDF-Hash-256(PMK, "AEK Derivation", Selected AKM Suite ||
375 * min(localMAC, peerMAC) || max(localMAC, peerMAC))
377 /* Selected AKM Suite: SAE */
378 RSN_SELECTOR_PUT(ptr, RSN_AUTH_KEY_MGMT_SAE);
379 ptr += RSN_SELECTOR_LEN;
381 if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
388 os_memcpy(ptr, addr1, ETH_ALEN);
390 os_memcpy(ptr, addr2, ETH_ALEN);
392 sha256_prf(sta->sae->pmk, sizeof(sta->sae->pmk), "AEK Derivation",
393 context, sizeof(context), sta->aek, sizeof(sta->aek));
397 /* derive mesh temporal key from pmk */
398 int mesh_rsn_derive_mtk(struct wpa_supplicant *wpa_s, struct sta_info *sta)
402 u8 *myaddr = wpa_s->own_addr;
403 u8 *peer = sta->addr;
404 u8 context[2 * WPA_NONCE_LEN + 2 * 2 + RSN_SELECTOR_LEN + 2 * ETH_ALEN];
407 * MTK = KDF-Hash-Length(PMK, "Temporal Key Derivation", min(localNonce,
408 * peerNonce) || max(localNonce, peerNonce) || min(localLinkID,
409 * peerLinkID) || max(localLinkID, peerLinkID) || Selected AKM Suite ||
410 * min(localMAC, peerMAC) || max(localMAC, peerMAC))
413 if (os_memcmp(sta->my_nonce, sta->peer_nonce, WPA_NONCE_LEN) < 0) {
415 max = sta->peer_nonce;
417 min = sta->peer_nonce;
420 os_memcpy(ptr, min, WPA_NONCE_LEN);
421 ptr += WPA_NONCE_LEN;
422 os_memcpy(ptr, max, WPA_NONCE_LEN);
423 ptr += WPA_NONCE_LEN;
425 if (sta->my_lid < sta->peer_lid) {
426 WPA_PUT_LE16(ptr, sta->my_lid);
428 WPA_PUT_LE16(ptr, sta->peer_lid);
431 WPA_PUT_LE16(ptr, sta->peer_lid);
433 WPA_PUT_LE16(ptr, sta->my_lid);
437 /* Selected AKM Suite: SAE */
438 RSN_SELECTOR_PUT(ptr, RSN_AUTH_KEY_MGMT_SAE);
439 ptr += RSN_SELECTOR_LEN;
441 if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
448 os_memcpy(ptr, min, ETH_ALEN);
450 os_memcpy(ptr, max, ETH_ALEN);
452 sta->mtk_len = wpa_cipher_key_len(WPA_CIPHER_CCMP);
453 sha256_prf(sta->sae->pmk, SAE_PMK_LEN,
454 "Temporal Key Derivation", context, sizeof(context),
455 sta->mtk, sta->mtk_len);
460 void mesh_rsn_init_ampe_sta(struct wpa_supplicant *wpa_s, struct sta_info *sta)
462 if (random_get_bytes(sta->my_nonce, WPA_NONCE_LEN) < 0) {
463 wpa_printf(MSG_INFO, "mesh: Failed to derive random nonce");
464 /* TODO: How to handle this more cleanly? */
466 os_memset(sta->peer_nonce, 0, WPA_NONCE_LEN);
467 mesh_rsn_derive_aek(wpa_s->mesh_rsn, sta);
471 /* insert AMPE and encrypted MIC at @ie.
472 * @mesh_rsn: mesh RSN context
473 * @sta: STA we're sending to
474 * @cat: pointer to category code in frame header.
475 * @buf: wpabuf to add encrypted AMPE and MIC to.
477 int mesh_rsn_protect_frame(struct mesh_rsn *rsn, struct sta_info *sta,
478 const u8 *cat, struct wpabuf *buf)
480 struct ieee80211_ampe_ie *ampe;
481 u8 const *ie = wpabuf_head_u8(buf) + wpabuf_len(buf);
482 u8 *ampe_ie = NULL, *mic_ie = NULL, *mic_payload;
483 const u8 *aad[] = { rsn->wpa_s->own_addr, sta->addr, cat };
484 const size_t aad_len[] = { ETH_ALEN, ETH_ALEN, ie - cat };
487 if (AES_BLOCK_SIZE + 2 + sizeof(*ampe) + 2 > wpabuf_tailroom(buf)) {
488 wpa_printf(MSG_ERROR, "protect frame: buffer too small");
492 ampe_ie = os_zalloc(2 + sizeof(*ampe));
494 wpa_printf(MSG_ERROR, "protect frame: out of memory");
498 mic_ie = os_zalloc(2 + AES_BLOCK_SIZE);
500 wpa_printf(MSG_ERROR, "protect frame: out of memory");
506 ampe_ie[0] = WLAN_EID_AMPE;
507 ampe_ie[1] = sizeof(*ampe);
508 ampe = (struct ieee80211_ampe_ie *) (ampe_ie + 2);
510 RSN_SELECTOR_PUT(ampe->selected_pairwise_suite,
511 wpa_cipher_to_suite(WPA_PROTO_RSN, WPA_CIPHER_CCMP));
512 os_memcpy(ampe->local_nonce, sta->my_nonce, WPA_NONCE_LEN);
513 os_memcpy(ampe->peer_nonce, sta->peer_nonce, WPA_NONCE_LEN);
514 /* incomplete: see 13.5.4 */
515 /* TODO: static mgtk for now since we don't support rekeying! */
516 os_memcpy(ampe->mgtk, rsn->mgtk, 16);
517 /* TODO: Populate Key RSC */
518 /* expire in 13 decades or so */
519 os_memset(ampe->key_expiration, 0xff, 4);
522 mic_ie[0] = WLAN_EID_MIC;
523 mic_ie[1] = AES_BLOCK_SIZE;
524 wpabuf_put_data(buf, mic_ie, 2);
525 /* MIC field is output ciphertext */
527 /* encrypt after MIC */
528 mic_payload = (u8 *) wpabuf_put(buf, 2 + sizeof(*ampe) +
531 if (aes_siv_encrypt(sta->aek, ampe_ie, 2 + sizeof(*ampe), 3,
532 aad, aad_len, mic_payload)) {
533 wpa_printf(MSG_ERROR, "protect frame: failed to encrypt");
546 int mesh_rsn_process_ampe(struct wpa_supplicant *wpa_s, struct sta_info *sta,
547 struct ieee802_11_elems *elems, const u8 *cat,
548 const u8 *chosen_pmk,
549 const u8 *start, size_t elems_len)
552 struct ieee80211_ampe_ie *ampe;
553 u8 null_nonce[WPA_NONCE_LEN] = {};
556 u8 *ampe_buf, *crypt = NULL;
558 const u8 *aad[] = { sta->addr, wpa_s->own_addr, cat };
559 const size_t aad_len[] = { ETH_ALEN, ETH_ALEN,
560 (elems->mic - 2) - cat };
563 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
565 if (!wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr)) {
567 "Mesh RSN: SAE is not prepared yet");
570 mesh_rsn_auth_sae_sta(wpa_s, sta);
573 if (chosen_pmk && os_memcmp(chosen_pmk, sta->sae->pmkid, PMKID_LEN)) {
574 wpa_msg(wpa_s, MSG_DEBUG,
575 "Mesh RSN: Invalid PMKID (Chosen PMK did not match calculated PMKID)");
579 if (!elems->mic || elems->mic_len < AES_BLOCK_SIZE) {
580 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: missing mic ie");
584 ampe_buf = (u8 *) elems->mic + elems->mic_len;
585 if ((int) elems_len < ampe_buf - start)
588 crypt_len = elems_len - (elems->mic - start);
590 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: missing ampe ie");
594 /* crypt is modified by siv_decrypt */
595 crypt = os_zalloc(crypt_len);
597 wpa_printf(MSG_ERROR, "Mesh RSN: out of memory");
602 os_memcpy(crypt, elems->mic, crypt_len);
604 if (aes_siv_decrypt(sta->aek, crypt, crypt_len, 3,
605 aad, aad_len, ampe_buf)) {
606 wpa_printf(MSG_ERROR, "Mesh RSN: frame verification failed!");
611 ampe_eid = *ampe_buf++;
612 ampe_ie_len = *ampe_buf++;
614 if (ampe_eid != WLAN_EID_AMPE ||
615 ampe_ie_len < sizeof(struct ieee80211_ampe_ie)) {
616 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid ampe ie");
621 ampe = (struct ieee80211_ampe_ie *) ampe_buf;
622 if (os_memcmp(ampe->peer_nonce, null_nonce, WPA_NONCE_LEN) != 0 &&
623 os_memcmp(ampe->peer_nonce, sta->my_nonce, WPA_NONCE_LEN) != 0) {
624 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid peer nonce");
628 os_memcpy(sta->peer_nonce, ampe->local_nonce,
629 sizeof(ampe->local_nonce));
630 os_memcpy(sta->mgtk, ampe->mgtk, sizeof(ampe->mgtk));
632 /* todo parse mgtk expiration */