blob: 33040f30baa839d374d92d0adde949b97177f075 [file] [log] [blame]
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001/*
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"
Dmitry Shmidtff787d52015-01-12 13:01:47 -080021#include "ap/ieee802_11.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080022#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
31void mesh_auth_timer(void *eloop_ctx, void *user_data)
32{
33 struct wpa_supplicant *wpa_s = eloop_ctx;
34 struct sta_info *sta = user_data;
Dmitry Shmidte4663042016-04-04 10:07:49 -070035 struct hostapd_data *hapd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080036
37 if (sta->sae->state != SAE_ACCEPTED) {
38 wpa_printf(MSG_DEBUG, "AUTH: Re-authenticate with " MACSTR
39 " (attempt %d) ",
40 MAC2STR(sta->addr), sta->sae_auth_retry);
Dmitry Shmidt7f656022015-02-25 14:36:37 -080041 wpa_msg(wpa_s, MSG_INFO, MESH_SAE_AUTH_FAILURE "addr=" MACSTR,
42 MAC2STR(sta->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080043 if (sta->sae_auth_retry < MESH_AUTH_RETRY) {
44 mesh_rsn_auth_sae_sta(wpa_s, sta);
45 } else {
Dmitry Shmidte4663042016-04-04 10:07:49 -070046 hapd = wpa_s->ifmsh->bss[0];
47
Dmitry Shmidt7f656022015-02-25 14:36:37 -080048 if (sta->sae_auth_retry > MESH_AUTH_RETRY) {
Dmitry Shmidte4663042016-04-04 10:07:49 -070049 ap_free_sta(hapd, sta);
Dmitry Shmidt7f656022015-02-25 14:36:37 -080050 return;
51 }
52
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080053 /* block the STA if exceeded the number of attempts */
Dmitry Shmidt216983b2015-02-06 10:50:36 -080054 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_BLOCKED);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080055 sta->sae->state = SAE_NOTHING;
Dmitry Shmidt7f656022015-02-25 14:36:37 -080056 wpa_msg(wpa_s, MSG_INFO, MESH_SAE_AUTH_BLOCKED "addr="
57 MACSTR " duration=%d",
58 MAC2STR(sta->addr),
Dmitry Shmidte4663042016-04-04 10:07:49 -070059 hapd->conf->ap_max_inactivity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080060 }
61 sta->sae_auth_retry++;
62 }
63}
64
65
66static void auth_logger(void *ctx, const u8 *addr, logger_level level,
67 const char *txt)
68{
69 if (addr)
70 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
71 MAC2STR(addr), txt);
72 else
73 wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
74}
75
76
77static const u8 *auth_get_psk(void *ctx, const u8 *addr,
78 const u8 *p2p_dev_addr, const u8 *prev_psk)
79{
80 struct mesh_rsn *mesh_rsn = ctx;
81 struct hostapd_data *hapd = mesh_rsn->wpa_s->ifmsh->bss[0];
82 struct sta_info *sta = ap_get_sta(hapd, addr);
83
84 wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
85 __func__, MAC2STR(addr), prev_psk);
86
87 if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
88 if (!sta->sae || prev_psk)
89 return NULL;
90 return sta->sae->pmk;
91 }
92
93 return NULL;
94}
95
96
97static int auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
98 const u8 *addr, int idx, u8 *key, size_t key_len)
99{
100 struct mesh_rsn *mesh_rsn = ctx;
101 u8 seq[6];
102
103 os_memset(seq, 0, sizeof(seq));
104
105 if (addr) {
106 wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d addr=" MACSTR
107 " key_idx=%d)",
108 __func__, alg, MAC2STR(addr), idx);
109 } else {
110 wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d key_idx=%d)",
111 __func__, alg, idx);
112 }
113 wpa_hexdump_key(MSG_DEBUG, "AUTH: set_key - key", key, key_len);
114
115 return wpa_drv_set_key(mesh_rsn->wpa_s, alg, addr, idx,
116 1, seq, 6, key, key_len);
117}
118
119
120static int auth_start_ampe(void *ctx, const u8 *addr)
121{
122 struct mesh_rsn *mesh_rsn = ctx;
123 struct hostapd_data *hapd;
124 struct sta_info *sta;
125
126 if (mesh_rsn->wpa_s->current_ssid->mode != WPAS_MODE_MESH)
127 return -1;
128
129 hapd = mesh_rsn->wpa_s->ifmsh->bss[0];
130 sta = ap_get_sta(hapd, addr);
131 if (sta)
132 eloop_cancel_timeout(mesh_auth_timer, mesh_rsn->wpa_s, sta);
133
134 mesh_mpm_auth_peer(mesh_rsn->wpa_s, addr);
135 return 0;
136}
137
138
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700139static int __mesh_rsn_auth_init(struct mesh_rsn *rsn, const u8 *addr,
140 enum mfp_options ieee80211w)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800141{
142 struct wpa_auth_config conf;
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -0800143 static const struct wpa_auth_callbacks cb = {
144 .logger = auth_logger,
145 .get_psk = auth_get_psk,
146 .set_key = auth_set_key,
147 .start_ampe = auth_start_ampe,
148 };
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800149 u8 seq[6] = {};
150
151 wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
152
153 os_memset(&conf, 0, sizeof(conf));
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700154 conf.wpa = WPA_PROTO_RSN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800155 conf.wpa_key_mgmt = WPA_KEY_MGMT_SAE;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700156 conf.wpa_pairwise = rsn->pairwise_cipher;
157 conf.rsn_pairwise = rsn->pairwise_cipher;
158 conf.wpa_group = rsn->group_cipher;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800159 conf.eapol_version = 0;
160 conf.wpa_group_rekey = -1;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700161#ifdef CONFIG_IEEE80211W
162 conf.ieee80211w = ieee80211w;
163 if (ieee80211w != NO_MGMT_FRAME_PROTECTION)
164 conf.group_mgmt_cipher = rsn->mgmt_group_cipher;
165#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800166
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -0800167 rsn->auth = wpa_init(addr, &conf, &cb, rsn);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800168 if (rsn->auth == NULL) {
169 wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
170 return -1;
171 }
172
173 /* TODO: support rekeying */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700174 rsn->mgtk_len = wpa_cipher_key_len(conf.wpa_group);
175 if (random_get_bytes(rsn->mgtk, rsn->mgtk_len) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800176 return -1;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700177 rsn->mgtk_key_id = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800178
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700179#ifdef CONFIG_IEEE80211W
180 if (ieee80211w != NO_MGMT_FRAME_PROTECTION) {
181 rsn->igtk_len = wpa_cipher_key_len(conf.group_mgmt_cipher);
182 if (random_get_bytes(rsn->igtk, rsn->igtk_len) < 0)
183 return -1;
184 rsn->igtk_key_id = 4;
185
186 /* group mgmt */
187 wpa_hexdump_key(MSG_DEBUG, "mesh: Own TX IGTK",
188 rsn->igtk, rsn->igtk_len);
189 wpa_drv_set_key(rsn->wpa_s,
190 wpa_cipher_to_alg(rsn->mgmt_group_cipher), NULL,
191 rsn->igtk_key_id, 1,
192 seq, sizeof(seq), rsn->igtk, rsn->igtk_len);
193 }
194#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800195
196 /* group privacy / data frames */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700197 wpa_hexdump_key(MSG_DEBUG, "mesh: Own TX MGTK",
198 rsn->mgtk, rsn->mgtk_len);
199 wpa_drv_set_key(rsn->wpa_s, wpa_cipher_to_alg(rsn->group_cipher), NULL,
200 rsn->mgtk_key_id, 1, seq, sizeof(seq),
201 rsn->mgtk, rsn->mgtk_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800202
203 return 0;
204}
205
206
207static void mesh_rsn_deinit(struct mesh_rsn *rsn)
208{
209 os_memset(rsn->mgtk, 0, sizeof(rsn->mgtk));
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700210 rsn->mgtk_len = 0;
211 os_memset(rsn->igtk, 0, sizeof(rsn->igtk));
212 rsn->igtk_len = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800213 if (rsn->auth)
214 wpa_deinit(rsn->auth);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800215}
216
217
218struct mesh_rsn *mesh_rsn_auth_init(struct wpa_supplicant *wpa_s,
219 struct mesh_conf *conf)
220{
221 struct mesh_rsn *mesh_rsn;
222 struct hostapd_data *bss = wpa_s->ifmsh->bss[0];
223 const u8 *ie;
224 size_t ie_len;
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -0800225#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
226 struct external_pmksa_cache *entry;
227#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800228
229 mesh_rsn = os_zalloc(sizeof(*mesh_rsn));
230 if (mesh_rsn == NULL)
231 return NULL;
232 mesh_rsn->wpa_s = wpa_s;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700233 mesh_rsn->pairwise_cipher = conf->pairwise_cipher;
234 mesh_rsn->group_cipher = conf->group_cipher;
235 mesh_rsn->mgmt_group_cipher = conf->mgmt_group_cipher;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800236
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700237 if (__mesh_rsn_auth_init(mesh_rsn, wpa_s->own_addr,
238 conf->ieee80211w) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800239 mesh_rsn_deinit(mesh_rsn);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800240 os_free(mesh_rsn);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800241 return NULL;
242 }
243
244 bss->wpa_auth = mesh_rsn->auth;
245
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -0800246#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
247 while ((entry = dl_list_last(&wpa_s->mesh_external_pmksa_cache,
248 struct external_pmksa_cache,
249 list)) != NULL) {
250 int ret;
251
252 ret = wpa_auth_pmksa_add_entry(bss->wpa_auth,
253 entry->pmksa_cache);
254 dl_list_del(&entry->list);
255 os_free(entry);
256
257 if (ret < 0)
258 return NULL;
259 }
260#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
261
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800262 ie = wpa_auth_get_wpa_ie(mesh_rsn->auth, &ie_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800263 conf->rsn_ie = (u8 *) ie;
264 conf->rsn_ie_len = ie_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800265
266 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
267
268 return mesh_rsn;
269}
270
271
272static int index_within_array(const int *array, int idx)
273{
274 int i;
275
276 for (i = 0; i < idx; i++) {
277 if (array[i] == -1)
278 return 0;
279 }
280
281 return 1;
282}
283
284
285static int mesh_rsn_sae_group(struct wpa_supplicant *wpa_s,
286 struct sae_data *sae)
287{
288 int *groups = wpa_s->ifmsh->bss[0]->conf->sae_groups;
289
290 /* Configuration may have changed, so validate current index */
291 if (!index_within_array(groups, wpa_s->mesh_rsn->sae_group_index))
292 return -1;
293
294 for (;;) {
295 int group = groups[wpa_s->mesh_rsn->sae_group_index];
296
297 if (group <= 0)
298 break;
299 if (sae_set_group(sae, group) == 0) {
300 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected SAE group %d",
301 sae->group);
302 return 0;
303 }
304 wpa_s->mesh_rsn->sae_group_index++;
305 }
306
307 return -1;
308}
309
310
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800311static int mesh_rsn_build_sae_commit(struct wpa_supplicant *wpa_s,
312 struct wpa_ssid *ssid,
313 struct sta_info *sta)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800314{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800315 if (ssid->passphrase == NULL) {
316 wpa_msg(wpa_s, MSG_DEBUG, "SAE: No password available");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800317 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800318 }
319
320 if (mesh_rsn_sae_group(wpa_s, sta->sae) < 0) {
321 wpa_msg(wpa_s, MSG_DEBUG, "SAE: Failed to select group");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800322 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800323 }
324
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800325 return sae_prepare_commit(wpa_s->own_addr, sta->addr,
326 (u8 *) ssid->passphrase,
327 os_strlen(ssid->passphrase), sta->sae);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800328}
329
330
331/* initiate new SAE authentication with sta */
332int mesh_rsn_auth_sae_sta(struct wpa_supplicant *wpa_s,
333 struct sta_info *sta)
334{
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800335 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800336 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidte4663042016-04-04 10:07:49 -0700337 struct rsn_pmksa_cache_entry *pmksa;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800338 unsigned int rnd;
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800339 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800340
341 if (!ssid) {
342 wpa_msg(wpa_s, MSG_DEBUG,
343 "AUTH: No current_ssid known to initiate new SAE");
344 return -1;
345 }
346
347 if (!sta->sae) {
348 sta->sae = os_zalloc(sizeof(*sta->sae));
349 if (sta->sae == NULL)
350 return -1;
351 }
352
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800353 pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr, NULL);
Dmitry Shmidte4663042016-04-04 10:07:49 -0700354 if (pmksa) {
355 if (!sta->wpa_sm)
356 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
357 sta->addr, NULL);
358 if (!sta->wpa_sm) {
359 wpa_printf(MSG_ERROR,
360 "mesh: Failed to initialize RSN state machine");
361 return -1;
362 }
363
364 wpa_printf(MSG_DEBUG,
365 "AUTH: Mesh PMKSA cache entry found for " MACSTR
366 " - try to use PMKSA caching instead of new SAE authentication",
367 MAC2STR(sta->addr));
368 wpa_auth_pmksa_set_to_sm(pmksa, sta->wpa_sm, hapd->wpa_auth,
369 sta->sae->pmkid, sta->sae->pmk);
370 sae_accept_sta(hapd, sta);
371 sta->mesh_sae_pmksa_caching = 1;
372 return 0;
373 }
374 sta->mesh_sae_pmksa_caching = 0;
375
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800376 if (mesh_rsn_build_sae_commit(wpa_s, ssid, sta))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800377 return -1;
378
379 wpa_msg(wpa_s, MSG_DEBUG,
380 "AUTH: started authentication with SAE peer: " MACSTR,
381 MAC2STR(sta->addr));
382
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800383 ret = auth_sae_init_committed(hapd, sta);
384 if (ret)
385 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800386
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800387 eloop_cancel_timeout(mesh_auth_timer, wpa_s, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800388 rnd = rand() % MESH_AUTH_TIMEOUT;
389 eloop_register_timeout(MESH_AUTH_TIMEOUT + rnd, 0, mesh_auth_timer,
390 wpa_s, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800391 return 0;
392}
393
394
395void mesh_rsn_get_pmkid(struct mesh_rsn *rsn, struct sta_info *sta, u8 *pmkid)
396{
Dmitry Shmidtd97138d2015-12-28 13:27:49 -0800397 os_memcpy(pmkid, sta->sae->pmkid, SAE_PMKID_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800398}
399
400
401static void
402mesh_rsn_derive_aek(struct mesh_rsn *rsn, struct sta_info *sta)
403{
404 u8 *myaddr = rsn->wpa_s->own_addr;
405 u8 *peer = sta->addr;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700406 u8 *addr1, *addr2;
407 u8 context[RSN_SELECTOR_LEN + 2 * ETH_ALEN], *ptr = context;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800408
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700409 /*
410 * AEK = KDF-Hash-256(PMK, "AEK Derivation", Selected AKM Suite ||
411 * min(localMAC, peerMAC) || max(localMAC, peerMAC))
412 */
413 /* Selected AKM Suite: SAE */
414 RSN_SELECTOR_PUT(ptr, RSN_AUTH_KEY_MGMT_SAE);
415 ptr += RSN_SELECTOR_LEN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800416
417 if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
418 addr1 = myaddr;
419 addr2 = peer;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700420 } else {
421 addr1 = peer;
422 addr2 = myaddr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800423 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700424 os_memcpy(ptr, addr1, ETH_ALEN);
425 ptr += ETH_ALEN;
426 os_memcpy(ptr, addr2, ETH_ALEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800427
428 sha256_prf(sta->sae->pmk, sizeof(sta->sae->pmk), "AEK Derivation",
429 context, sizeof(context), sta->aek, sizeof(sta->aek));
430}
431
432
433/* derive mesh temporal key from pmk */
434int mesh_rsn_derive_mtk(struct wpa_supplicant *wpa_s, struct sta_info *sta)
435{
436 u8 *ptr;
437 u8 *min, *max;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800438 u8 *myaddr = wpa_s->own_addr;
439 u8 *peer = sta->addr;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700440 u8 context[2 * WPA_NONCE_LEN + 2 * 2 + RSN_SELECTOR_LEN + 2 * ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800441
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700442 /*
443 * MTK = KDF-Hash-Length(PMK, "Temporal Key Derivation", min(localNonce,
444 * peerNonce) || max(localNonce, peerNonce) || min(localLinkID,
445 * peerLinkID) || max(localLinkID, peerLinkID) || Selected AKM Suite ||
446 * min(localMAC, peerMAC) || max(localMAC, peerMAC))
447 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800448 ptr = context;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700449 if (os_memcmp(sta->my_nonce, sta->peer_nonce, WPA_NONCE_LEN) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800450 min = sta->my_nonce;
451 max = sta->peer_nonce;
452 } else {
453 min = sta->peer_nonce;
454 max = sta->my_nonce;
455 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700456 os_memcpy(ptr, min, WPA_NONCE_LEN);
457 ptr += WPA_NONCE_LEN;
458 os_memcpy(ptr, max, WPA_NONCE_LEN);
459 ptr += WPA_NONCE_LEN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800460
461 if (sta->my_lid < sta->peer_lid) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700462 WPA_PUT_LE16(ptr, sta->my_lid);
463 ptr += 2;
464 WPA_PUT_LE16(ptr, sta->peer_lid);
465 ptr += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800466 } else {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700467 WPA_PUT_LE16(ptr, sta->peer_lid);
468 ptr += 2;
469 WPA_PUT_LE16(ptr, sta->my_lid);
470 ptr += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800471 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800472
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700473 /* Selected AKM Suite: SAE */
474 RSN_SELECTOR_PUT(ptr, RSN_AUTH_KEY_MGMT_SAE);
475 ptr += RSN_SELECTOR_LEN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800476
477 if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
478 min = myaddr;
479 max = peer;
480 } else {
481 min = peer;
482 max = myaddr;
483 }
484 os_memcpy(ptr, min, ETH_ALEN);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700485 ptr += ETH_ALEN;
486 os_memcpy(ptr, max, ETH_ALEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800487
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700488 sta->mtk_len = wpa_cipher_key_len(wpa_s->mesh_rsn->pairwise_cipher);
489 sha256_prf(sta->sae->pmk, SAE_PMK_LEN,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800490 "Temporal Key Derivation", context, sizeof(context),
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700491 sta->mtk, sta->mtk_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800492 return 0;
493}
494
495
496void mesh_rsn_init_ampe_sta(struct wpa_supplicant *wpa_s, struct sta_info *sta)
497{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700498 if (random_get_bytes(sta->my_nonce, WPA_NONCE_LEN) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800499 wpa_printf(MSG_INFO, "mesh: Failed to derive random nonce");
500 /* TODO: How to handle this more cleanly? */
501 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700502 os_memset(sta->peer_nonce, 0, WPA_NONCE_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800503 mesh_rsn_derive_aek(wpa_s->mesh_rsn, sta);
504}
505
506
507/* insert AMPE and encrypted MIC at @ie.
508 * @mesh_rsn: mesh RSN context
509 * @sta: STA we're sending to
510 * @cat: pointer to category code in frame header.
511 * @buf: wpabuf to add encrypted AMPE and MIC to.
512 * */
513int mesh_rsn_protect_frame(struct mesh_rsn *rsn, struct sta_info *sta,
514 const u8 *cat, struct wpabuf *buf)
515{
516 struct ieee80211_ampe_ie *ampe;
517 u8 const *ie = wpabuf_head_u8(buf) + wpabuf_len(buf);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700518 u8 *ampe_ie, *pos, *mic_payload;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800519 const u8 *aad[] = { rsn->wpa_s->own_addr, sta->addr, cat };
520 const size_t aad_len[] = { ETH_ALEN, ETH_ALEN, ie - cat };
521 int ret = 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700522 size_t len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800523
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700524 len = sizeof(*ampe);
525 if (cat[1] == PLINK_OPEN)
526 len += rsn->mgtk_len + WPA_KEY_RSC_LEN + 4;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700527#ifdef CONFIG_IEEE80211W
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700528 if (cat[1] == PLINK_OPEN && rsn->igtk_len)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700529 len += 2 + 6 + rsn->igtk_len;
530#endif /* CONFIG_IEEE80211W */
531
532 if (2 + AES_BLOCK_SIZE + 2 + len > wpabuf_tailroom(buf)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800533 wpa_printf(MSG_ERROR, "protect frame: buffer too small");
534 return -EINVAL;
535 }
536
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700537 ampe_ie = os_zalloc(2 + len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800538 if (!ampe_ie) {
539 wpa_printf(MSG_ERROR, "protect frame: out of memory");
540 return -ENOMEM;
541 }
542
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800543 /* IE: AMPE */
544 ampe_ie[0] = WLAN_EID_AMPE;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700545 ampe_ie[1] = len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800546 ampe = (struct ieee80211_ampe_ie *) (ampe_ie + 2);
547
548 RSN_SELECTOR_PUT(ampe->selected_pairwise_suite,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700549 RSN_CIPHER_SUITE_CCMP);
550 os_memcpy(ampe->local_nonce, sta->my_nonce, WPA_NONCE_LEN);
551 os_memcpy(ampe->peer_nonce, sta->peer_nonce, WPA_NONCE_LEN);
552
553 pos = (u8 *) (ampe + 1);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700554 if (cat[1] != PLINK_OPEN)
555 goto skip_keys;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700556
557 /* TODO: Key Replay Counter[8] optionally for
558 * Mesh Group Key Inform/Acknowledge frames */
559
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800560 /* TODO: static mgtk for now since we don't support rekeying! */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700561 /*
562 * GTKdata[variable]:
563 * MGTK[variable] || Key RSC[8] || GTKExpirationTime[4]
564 */
565 os_memcpy(pos, rsn->mgtk, rsn->mgtk_len);
566 pos += rsn->mgtk_len;
567 wpa_drv_get_seqnum(rsn->wpa_s, NULL, rsn->mgtk_key_id, pos);
568 pos += WPA_KEY_RSC_LEN;
569 /* Use fixed GTKExpirationTime for now */
570 WPA_PUT_LE32(pos, 0xffffffff);
571 pos += 4;
572
573#ifdef CONFIG_IEEE80211W
574 /*
575 * IGTKdata[variable]:
576 * Key ID[2], IPN[6], IGTK[variable]
577 */
578 if (rsn->igtk_len) {
579 WPA_PUT_LE16(pos, rsn->igtk_key_id);
580 pos += 2;
581 wpa_drv_get_seqnum(rsn->wpa_s, NULL, rsn->igtk_key_id, pos);
582 pos += 6;
583 os_memcpy(pos, rsn->igtk, rsn->igtk_len);
584 }
585#endif /* CONFIG_IEEE80211W */
586
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700587skip_keys:
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700588 wpa_hexdump_key(MSG_DEBUG, "mesh: Plaintext AMPE element",
589 ampe_ie, 2 + len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800590
591 /* IE: MIC */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700592 wpabuf_put_u8(buf, WLAN_EID_MIC);
593 wpabuf_put_u8(buf, AES_BLOCK_SIZE);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800594 /* MIC field is output ciphertext */
595
596 /* encrypt after MIC */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700597 mic_payload = wpabuf_put(buf, 2 + len + AES_BLOCK_SIZE);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800598
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800599 if (aes_siv_encrypt(sta->aek, sizeof(sta->aek), ampe_ie, 2 + len, 3,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800600 aad, aad_len, mic_payload)) {
601 wpa_printf(MSG_ERROR, "protect frame: failed to encrypt");
602 ret = -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800603 }
604
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800605 os_free(ampe_ie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800606
607 return ret;
608}
609
610
611int mesh_rsn_process_ampe(struct wpa_supplicant *wpa_s, struct sta_info *sta,
612 struct ieee802_11_elems *elems, const u8 *cat,
Dmitry Shmidtd97138d2015-12-28 13:27:49 -0800613 const u8 *chosen_pmk,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800614 const u8 *start, size_t elems_len)
615{
616 int ret = 0;
617 struct ieee80211_ampe_ie *ampe;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700618 u8 null_nonce[WPA_NONCE_LEN] = {};
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800619 u8 ampe_eid;
620 u8 ampe_ie_len;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700621 u8 *ampe_buf, *crypt = NULL, *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800622 size_t crypt_len;
623 const u8 *aad[] = { sta->addr, wpa_s->own_addr, cat };
624 const size_t aad_len[] = { ETH_ALEN, ETH_ALEN,
625 (elems->mic - 2) - cat };
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700626 size_t key_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800627
Dmitry Shmidte4663042016-04-04 10:07:49 -0700628 if (!sta->sae) {
629 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
630
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800631 if (!wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr, NULL)) {
Dmitry Shmidte4663042016-04-04 10:07:49 -0700632 wpa_printf(MSG_INFO,
633 "Mesh RSN: SAE is not prepared yet");
634 return -1;
635 }
636 mesh_rsn_auth_sae_sta(wpa_s, sta);
637 }
638
Dmitry Shmidtd97138d2015-12-28 13:27:49 -0800639 if (chosen_pmk && os_memcmp(chosen_pmk, sta->sae->pmkid, PMKID_LEN)) {
640 wpa_msg(wpa_s, MSG_DEBUG,
641 "Mesh RSN: Invalid PMKID (Chosen PMK did not match calculated PMKID)");
642 return -1;
643 }
644
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800645 if (!elems->mic || elems->mic_len < AES_BLOCK_SIZE) {
646 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: missing mic ie");
647 return -1;
648 }
649
650 ampe_buf = (u8 *) elems->mic + elems->mic_len;
651 if ((int) elems_len < ampe_buf - start)
652 return -1;
653
654 crypt_len = elems_len - (elems->mic - start);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700655 if (crypt_len < 2 + AES_BLOCK_SIZE) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800656 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: missing ampe ie");
657 return -1;
658 }
659
660 /* crypt is modified by siv_decrypt */
661 crypt = os_zalloc(crypt_len);
662 if (!crypt) {
663 wpa_printf(MSG_ERROR, "Mesh RSN: out of memory");
664 ret = -ENOMEM;
665 goto free;
666 }
667
668 os_memcpy(crypt, elems->mic, crypt_len);
669
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800670 if (aes_siv_decrypt(sta->aek, sizeof(sta->aek), crypt, crypt_len, 3,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800671 aad, aad_len, ampe_buf)) {
672 wpa_printf(MSG_ERROR, "Mesh RSN: frame verification failed!");
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700673 ret = -2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800674 goto free;
675 }
676
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700677 crypt_len -= AES_BLOCK_SIZE;
678 wpa_hexdump_key(MSG_DEBUG, "mesh: Decrypted AMPE element",
679 ampe_buf, crypt_len);
680
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800681 ampe_eid = *ampe_buf++;
682 ampe_ie_len = *ampe_buf++;
683
684 if (ampe_eid != WLAN_EID_AMPE ||
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700685 (size_t) 2 + ampe_ie_len > crypt_len ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800686 ampe_ie_len < sizeof(struct ieee80211_ampe_ie)) {
687 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid ampe ie");
688 ret = -1;
689 goto free;
690 }
691
692 ampe = (struct ieee80211_ampe_ie *) ampe_buf;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700693 pos = (u8 *) (ampe + 1);
694 end = ampe_buf + ampe_ie_len;
695 if (os_memcmp(ampe->peer_nonce, null_nonce, WPA_NONCE_LEN) != 0 &&
696 os_memcmp(ampe->peer_nonce, sta->my_nonce, WPA_NONCE_LEN) != 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800697 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid peer nonce");
698 ret = -1;
699 goto free;
700 }
701 os_memcpy(sta->peer_nonce, ampe->local_nonce,
702 sizeof(ampe->local_nonce));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800703
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700704 /* TODO: Key Replay Counter[8] in Mesh Group Key Inform/Acknowledge
705 * frames */
706
707 /*
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700708 * GTKdata shall not be included in Mesh Peering Confirm. While the
709 * standard does not state the same about IGTKdata, that same constraint
710 * needs to apply for it. It makes no sense to include the keys in Mesh
711 * Peering Close frames either, so while the standard does not seem to
712 * have a shall statement for these, they are described without
713 * mentioning GTKdata.
714 *
715 * An earlier implementation used to add GTKdata to both Mesh Peering
716 * Open and Mesh Peering Confirm frames, so ignore the possibly present
717 * GTKdata frame without rejecting the frame as a backwards
718 * compatibility mechanism.
719 */
720 if (cat[1] != PLINK_OPEN) {
721 if (end > pos) {
722 wpa_hexdump_key(MSG_DEBUG,
723 "mesh: Ignore unexpected GTKdata(etc.) fields in the end of AMPE element in Mesh Peering Confirm/Close",
724 pos, end - pos);
725 }
726 goto free;
727 }
728
729 /*
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700730 * GTKdata[variable]:
731 * MGTK[variable] || Key RSC[8] || GTKExpirationTime[4]
732 */
733 sta->mgtk_key_id = 1; /* FIX: Where to get Key ID? */
734 key_len = wpa_cipher_key_len(wpa_s->mesh_rsn->group_cipher);
735 if ((int) key_len + WPA_KEY_RSC_LEN + 4 > end - pos) {
736 wpa_dbg(wpa_s, MSG_DEBUG, "mesh: Truncated AMPE element");
737 ret = -1;
738 goto free;
739 }
740 sta->mgtk_len = key_len;
741 os_memcpy(sta->mgtk, pos, sta->mgtk_len);
742 wpa_hexdump_key(MSG_DEBUG, "mesh: GTKdata - MGTK",
743 sta->mgtk, sta->mgtk_len);
744 pos += sta->mgtk_len;
745 wpa_hexdump(MSG_DEBUG, "mesh: GTKdata - MGTK - Key RSC",
746 pos, WPA_KEY_RSC_LEN);
747 os_memcpy(sta->mgtk_rsc, pos, sizeof(sta->mgtk_rsc));
748 pos += WPA_KEY_RSC_LEN;
749 wpa_printf(MSG_DEBUG,
750 "mesh: GTKdata - MGTK - GTKExpirationTime: %u seconds",
751 WPA_GET_LE32(pos));
752 pos += 4;
753
754#ifdef CONFIG_IEEE80211W
755 /*
756 * IGTKdata[variable]:
757 * Key ID[2], IPN[6], IGTK[variable]
758 */
759 key_len = wpa_cipher_key_len(wpa_s->mesh_rsn->mgmt_group_cipher);
760 if (end - pos >= (int) (2 + 6 + key_len)) {
761 sta->igtk_key_id = WPA_GET_LE16(pos);
762 wpa_printf(MSG_DEBUG, "mesh: IGTKdata - Key ID %u",
763 sta->igtk_key_id);
764 pos += 2;
765 os_memcpy(sta->igtk_rsc, pos, sizeof(sta->igtk_rsc));
766 wpa_hexdump(MSG_DEBUG, "mesh: IGTKdata - IPN",
767 sta->igtk_rsc, sizeof(sta->igtk_rsc));
768 pos += 6;
769 os_memcpy(sta->igtk, pos, key_len);
770 sta->igtk_len = key_len;
771 wpa_hexdump_key(MSG_DEBUG, "mesh: IGTKdata - IGTK",
772 sta->igtk, sta->igtk_len);
773 }
774#endif /* CONFIG_IEEE80211W */
775
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800776free:
777 os_free(crypt);
778 return ret;
779}