blob: c5f5d692bba1e177ab800c1632db2a12d3320e52 [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;
143 struct wpa_auth_callbacks cb;
144 u8 seq[6] = {};
145
146 wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
147
148 os_memset(&conf, 0, sizeof(conf));
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700149 conf.wpa = WPA_PROTO_RSN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800150 conf.wpa_key_mgmt = WPA_KEY_MGMT_SAE;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700151 conf.wpa_pairwise = rsn->pairwise_cipher;
152 conf.rsn_pairwise = rsn->pairwise_cipher;
153 conf.wpa_group = rsn->group_cipher;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800154 conf.eapol_version = 0;
155 conf.wpa_group_rekey = -1;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700156#ifdef CONFIG_IEEE80211W
157 conf.ieee80211w = ieee80211w;
158 if (ieee80211w != NO_MGMT_FRAME_PROTECTION)
159 conf.group_mgmt_cipher = rsn->mgmt_group_cipher;
160#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800161
162 os_memset(&cb, 0, sizeof(cb));
163 cb.ctx = rsn;
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;
168
169 rsn->auth = wpa_init(addr, &conf, &cb);
170 if (rsn->auth == NULL) {
171 wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
172 return -1;
173 }
174
175 /* TODO: support rekeying */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700176 rsn->mgtk_len = wpa_cipher_key_len(conf.wpa_group);
177 if (random_get_bytes(rsn->mgtk, rsn->mgtk_len) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800178 return -1;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700179 rsn->mgtk_key_id = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800180
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700181#ifdef CONFIG_IEEE80211W
182 if (ieee80211w != NO_MGMT_FRAME_PROTECTION) {
183 rsn->igtk_len = wpa_cipher_key_len(conf.group_mgmt_cipher);
184 if (random_get_bytes(rsn->igtk, rsn->igtk_len) < 0)
185 return -1;
186 rsn->igtk_key_id = 4;
187
188 /* group mgmt */
189 wpa_hexdump_key(MSG_DEBUG, "mesh: Own TX IGTK",
190 rsn->igtk, rsn->igtk_len);
191 wpa_drv_set_key(rsn->wpa_s,
192 wpa_cipher_to_alg(rsn->mgmt_group_cipher), NULL,
193 rsn->igtk_key_id, 1,
194 seq, sizeof(seq), rsn->igtk, rsn->igtk_len);
195 }
196#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800197
198 /* group privacy / data frames */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700199 wpa_hexdump_key(MSG_DEBUG, "mesh: Own TX MGTK",
200 rsn->mgtk, rsn->mgtk_len);
201 wpa_drv_set_key(rsn->wpa_s, wpa_cipher_to_alg(rsn->group_cipher), NULL,
202 rsn->mgtk_key_id, 1, seq, sizeof(seq),
203 rsn->mgtk, rsn->mgtk_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800204
205 return 0;
206}
207
208
209static void mesh_rsn_deinit(struct mesh_rsn *rsn)
210{
211 os_memset(rsn->mgtk, 0, sizeof(rsn->mgtk));
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700212 rsn->mgtk_len = 0;
213 os_memset(rsn->igtk, 0, sizeof(rsn->igtk));
214 rsn->igtk_len = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800215 if (rsn->auth)
216 wpa_deinit(rsn->auth);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800217}
218
219
220struct mesh_rsn *mesh_rsn_auth_init(struct wpa_supplicant *wpa_s,
221 struct mesh_conf *conf)
222{
223 struct mesh_rsn *mesh_rsn;
224 struct hostapd_data *bss = wpa_s->ifmsh->bss[0];
225 const u8 *ie;
226 size_t ie_len;
227
228 mesh_rsn = os_zalloc(sizeof(*mesh_rsn));
229 if (mesh_rsn == NULL)
230 return NULL;
231 mesh_rsn->wpa_s = wpa_s;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700232 mesh_rsn->pairwise_cipher = conf->pairwise_cipher;
233 mesh_rsn->group_cipher = conf->group_cipher;
234 mesh_rsn->mgmt_group_cipher = conf->mgmt_group_cipher;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800235
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700236 if (__mesh_rsn_auth_init(mesh_rsn, wpa_s->own_addr,
237 conf->ieee80211w) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800238 mesh_rsn_deinit(mesh_rsn);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800239 os_free(mesh_rsn);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800240 return NULL;
241 }
242
243 bss->wpa_auth = mesh_rsn->auth;
244
245 ie = wpa_auth_get_wpa_ie(mesh_rsn->auth, &ie_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800246 conf->rsn_ie = (u8 *) ie;
247 conf->rsn_ie_len = ie_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800248
249 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
250
251 return mesh_rsn;
252}
253
254
255static int index_within_array(const int *array, int idx)
256{
257 int i;
258
259 for (i = 0; i < idx; i++) {
260 if (array[i] == -1)
261 return 0;
262 }
263
264 return 1;
265}
266
267
268static int mesh_rsn_sae_group(struct wpa_supplicant *wpa_s,
269 struct sae_data *sae)
270{
271 int *groups = wpa_s->ifmsh->bss[0]->conf->sae_groups;
272
273 /* Configuration may have changed, so validate current index */
274 if (!index_within_array(groups, wpa_s->mesh_rsn->sae_group_index))
275 return -1;
276
277 for (;;) {
278 int group = groups[wpa_s->mesh_rsn->sae_group_index];
279
280 if (group <= 0)
281 break;
282 if (sae_set_group(sae, group) == 0) {
283 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected SAE group %d",
284 sae->group);
285 return 0;
286 }
287 wpa_s->mesh_rsn->sae_group_index++;
288 }
289
290 return -1;
291}
292
293
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800294static int mesh_rsn_build_sae_commit(struct wpa_supplicant *wpa_s,
295 struct wpa_ssid *ssid,
296 struct sta_info *sta)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800297{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800298 if (ssid->passphrase == NULL) {
299 wpa_msg(wpa_s, MSG_DEBUG, "SAE: No password available");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800300 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800301 }
302
303 if (mesh_rsn_sae_group(wpa_s, sta->sae) < 0) {
304 wpa_msg(wpa_s, MSG_DEBUG, "SAE: Failed to select group");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800305 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800306 }
307
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800308 return sae_prepare_commit(wpa_s->own_addr, sta->addr,
309 (u8 *) ssid->passphrase,
310 os_strlen(ssid->passphrase), sta->sae);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800311}
312
313
314/* initiate new SAE authentication with sta */
315int mesh_rsn_auth_sae_sta(struct wpa_supplicant *wpa_s,
316 struct sta_info *sta)
317{
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800318 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800319 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidte4663042016-04-04 10:07:49 -0700320 struct rsn_pmksa_cache_entry *pmksa;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800321 unsigned int rnd;
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800322 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800323
324 if (!ssid) {
325 wpa_msg(wpa_s, MSG_DEBUG,
326 "AUTH: No current_ssid known to initiate new SAE");
327 return -1;
328 }
329
330 if (!sta->sae) {
331 sta->sae = os_zalloc(sizeof(*sta->sae));
332 if (sta->sae == NULL)
333 return -1;
334 }
335
Dmitry Shmidte4663042016-04-04 10:07:49 -0700336 pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr);
337 if (pmksa) {
338 if (!sta->wpa_sm)
339 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
340 sta->addr, NULL);
341 if (!sta->wpa_sm) {
342 wpa_printf(MSG_ERROR,
343 "mesh: Failed to initialize RSN state machine");
344 return -1;
345 }
346
347 wpa_printf(MSG_DEBUG,
348 "AUTH: Mesh PMKSA cache entry found for " MACSTR
349 " - try to use PMKSA caching instead of new SAE authentication",
350 MAC2STR(sta->addr));
351 wpa_auth_pmksa_set_to_sm(pmksa, sta->wpa_sm, hapd->wpa_auth,
352 sta->sae->pmkid, sta->sae->pmk);
353 sae_accept_sta(hapd, sta);
354 sta->mesh_sae_pmksa_caching = 1;
355 return 0;
356 }
357 sta->mesh_sae_pmksa_caching = 0;
358
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800359 if (mesh_rsn_build_sae_commit(wpa_s, ssid, sta))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800360 return -1;
361
362 wpa_msg(wpa_s, MSG_DEBUG,
363 "AUTH: started authentication with SAE peer: " MACSTR,
364 MAC2STR(sta->addr));
365
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800366 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800367 ret = auth_sae_init_committed(hapd, sta);
368 if (ret)
369 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800370
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800371 eloop_cancel_timeout(mesh_auth_timer, wpa_s, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800372 rnd = rand() % MESH_AUTH_TIMEOUT;
373 eloop_register_timeout(MESH_AUTH_TIMEOUT + rnd, 0, mesh_auth_timer,
374 wpa_s, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800375 return 0;
376}
377
378
379void mesh_rsn_get_pmkid(struct mesh_rsn *rsn, struct sta_info *sta, u8 *pmkid)
380{
Dmitry Shmidtd97138d2015-12-28 13:27:49 -0800381 os_memcpy(pmkid, sta->sae->pmkid, SAE_PMKID_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800382}
383
384
385static void
386mesh_rsn_derive_aek(struct mesh_rsn *rsn, struct sta_info *sta)
387{
388 u8 *myaddr = rsn->wpa_s->own_addr;
389 u8 *peer = sta->addr;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700390 u8 *addr1, *addr2;
391 u8 context[RSN_SELECTOR_LEN + 2 * ETH_ALEN], *ptr = context;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800392
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700393 /*
394 * AEK = KDF-Hash-256(PMK, "AEK Derivation", Selected AKM Suite ||
395 * min(localMAC, peerMAC) || max(localMAC, peerMAC))
396 */
397 /* Selected AKM Suite: SAE */
398 RSN_SELECTOR_PUT(ptr, RSN_AUTH_KEY_MGMT_SAE);
399 ptr += RSN_SELECTOR_LEN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800400
401 if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
402 addr1 = myaddr;
403 addr2 = peer;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700404 } else {
405 addr1 = peer;
406 addr2 = myaddr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800407 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700408 os_memcpy(ptr, addr1, ETH_ALEN);
409 ptr += ETH_ALEN;
410 os_memcpy(ptr, addr2, ETH_ALEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800411
412 sha256_prf(sta->sae->pmk, sizeof(sta->sae->pmk), "AEK Derivation",
413 context, sizeof(context), sta->aek, sizeof(sta->aek));
414}
415
416
417/* derive mesh temporal key from pmk */
418int mesh_rsn_derive_mtk(struct wpa_supplicant *wpa_s, struct sta_info *sta)
419{
420 u8 *ptr;
421 u8 *min, *max;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800422 u8 *myaddr = wpa_s->own_addr;
423 u8 *peer = sta->addr;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700424 u8 context[2 * WPA_NONCE_LEN + 2 * 2 + RSN_SELECTOR_LEN + 2 * ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800425
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700426 /*
427 * MTK = KDF-Hash-Length(PMK, "Temporal Key Derivation", min(localNonce,
428 * peerNonce) || max(localNonce, peerNonce) || min(localLinkID,
429 * peerLinkID) || max(localLinkID, peerLinkID) || Selected AKM Suite ||
430 * min(localMAC, peerMAC) || max(localMAC, peerMAC))
431 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800432 ptr = context;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700433 if (os_memcmp(sta->my_nonce, sta->peer_nonce, WPA_NONCE_LEN) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800434 min = sta->my_nonce;
435 max = sta->peer_nonce;
436 } else {
437 min = sta->peer_nonce;
438 max = sta->my_nonce;
439 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700440 os_memcpy(ptr, min, WPA_NONCE_LEN);
441 ptr += WPA_NONCE_LEN;
442 os_memcpy(ptr, max, WPA_NONCE_LEN);
443 ptr += WPA_NONCE_LEN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800444
445 if (sta->my_lid < sta->peer_lid) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700446 WPA_PUT_LE16(ptr, sta->my_lid);
447 ptr += 2;
448 WPA_PUT_LE16(ptr, sta->peer_lid);
449 ptr += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800450 } else {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700451 WPA_PUT_LE16(ptr, sta->peer_lid);
452 ptr += 2;
453 WPA_PUT_LE16(ptr, sta->my_lid);
454 ptr += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800455 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800456
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700457 /* Selected AKM Suite: SAE */
458 RSN_SELECTOR_PUT(ptr, RSN_AUTH_KEY_MGMT_SAE);
459 ptr += RSN_SELECTOR_LEN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800460
461 if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
462 min = myaddr;
463 max = peer;
464 } else {
465 min = peer;
466 max = myaddr;
467 }
468 os_memcpy(ptr, min, ETH_ALEN);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700469 ptr += ETH_ALEN;
470 os_memcpy(ptr, max, ETH_ALEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800471
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700472 sta->mtk_len = wpa_cipher_key_len(wpa_s->mesh_rsn->pairwise_cipher);
473 sha256_prf(sta->sae->pmk, SAE_PMK_LEN,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800474 "Temporal Key Derivation", context, sizeof(context),
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700475 sta->mtk, sta->mtk_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800476 return 0;
477}
478
479
480void mesh_rsn_init_ampe_sta(struct wpa_supplicant *wpa_s, struct sta_info *sta)
481{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700482 if (random_get_bytes(sta->my_nonce, WPA_NONCE_LEN) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800483 wpa_printf(MSG_INFO, "mesh: Failed to derive random nonce");
484 /* TODO: How to handle this more cleanly? */
485 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700486 os_memset(sta->peer_nonce, 0, WPA_NONCE_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800487 mesh_rsn_derive_aek(wpa_s->mesh_rsn, sta);
488}
489
490
491/* insert AMPE and encrypted MIC at @ie.
492 * @mesh_rsn: mesh RSN context
493 * @sta: STA we're sending to
494 * @cat: pointer to category code in frame header.
495 * @buf: wpabuf to add encrypted AMPE and MIC to.
496 * */
497int mesh_rsn_protect_frame(struct mesh_rsn *rsn, struct sta_info *sta,
498 const u8 *cat, struct wpabuf *buf)
499{
500 struct ieee80211_ampe_ie *ampe;
501 u8 const *ie = wpabuf_head_u8(buf) + wpabuf_len(buf);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700502 u8 *ampe_ie, *pos, *mic_payload;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800503 const u8 *aad[] = { rsn->wpa_s->own_addr, sta->addr, cat };
504 const size_t aad_len[] = { ETH_ALEN, ETH_ALEN, ie - cat };
505 int ret = 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700506 size_t len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800507
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700508 len = sizeof(*ampe) + rsn->mgtk_len + WPA_KEY_RSC_LEN + 4;
509#ifdef CONFIG_IEEE80211W
510 if (rsn->igtk_len)
511 len += 2 + 6 + rsn->igtk_len;
512#endif /* CONFIG_IEEE80211W */
513
514 if (2 + AES_BLOCK_SIZE + 2 + len > wpabuf_tailroom(buf)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800515 wpa_printf(MSG_ERROR, "protect frame: buffer too small");
516 return -EINVAL;
517 }
518
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700519 ampe_ie = os_zalloc(2 + len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800520 if (!ampe_ie) {
521 wpa_printf(MSG_ERROR, "protect frame: out of memory");
522 return -ENOMEM;
523 }
524
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800525 /* IE: AMPE */
526 ampe_ie[0] = WLAN_EID_AMPE;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700527 ampe_ie[1] = len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800528 ampe = (struct ieee80211_ampe_ie *) (ampe_ie + 2);
529
530 RSN_SELECTOR_PUT(ampe->selected_pairwise_suite,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700531 RSN_CIPHER_SUITE_CCMP);
532 os_memcpy(ampe->local_nonce, sta->my_nonce, WPA_NONCE_LEN);
533 os_memcpy(ampe->peer_nonce, sta->peer_nonce, WPA_NONCE_LEN);
534
535 pos = (u8 *) (ampe + 1);
536
537 /* TODO: Key Replay Counter[8] optionally for
538 * Mesh Group Key Inform/Acknowledge frames */
539
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800540 /* TODO: static mgtk for now since we don't support rekeying! */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700541 /*
542 * GTKdata[variable]:
543 * MGTK[variable] || Key RSC[8] || GTKExpirationTime[4]
544 */
545 os_memcpy(pos, rsn->mgtk, rsn->mgtk_len);
546 pos += rsn->mgtk_len;
547 wpa_drv_get_seqnum(rsn->wpa_s, NULL, rsn->mgtk_key_id, pos);
548 pos += WPA_KEY_RSC_LEN;
549 /* Use fixed GTKExpirationTime for now */
550 WPA_PUT_LE32(pos, 0xffffffff);
551 pos += 4;
552
553#ifdef CONFIG_IEEE80211W
554 /*
555 * IGTKdata[variable]:
556 * Key ID[2], IPN[6], IGTK[variable]
557 */
558 if (rsn->igtk_len) {
559 WPA_PUT_LE16(pos, rsn->igtk_key_id);
560 pos += 2;
561 wpa_drv_get_seqnum(rsn->wpa_s, NULL, rsn->igtk_key_id, pos);
562 pos += 6;
563 os_memcpy(pos, rsn->igtk, rsn->igtk_len);
564 }
565#endif /* CONFIG_IEEE80211W */
566
567 wpa_hexdump_key(MSG_DEBUG, "mesh: Plaintext AMPE element",
568 ampe_ie, 2 + len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800569
570 /* IE: MIC */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700571 wpabuf_put_u8(buf, WLAN_EID_MIC);
572 wpabuf_put_u8(buf, AES_BLOCK_SIZE);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800573 /* MIC field is output ciphertext */
574
575 /* encrypt after MIC */
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700576 mic_payload = wpabuf_put(buf, 2 + len + AES_BLOCK_SIZE);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800577
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700578 if (aes_siv_encrypt(sta->aek, ampe_ie, 2 + len, 3,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800579 aad, aad_len, mic_payload)) {
580 wpa_printf(MSG_ERROR, "protect frame: failed to encrypt");
581 ret = -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800582 }
583
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800584 os_free(ampe_ie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800585
586 return ret;
587}
588
589
590int mesh_rsn_process_ampe(struct wpa_supplicant *wpa_s, struct sta_info *sta,
591 struct ieee802_11_elems *elems, const u8 *cat,
Dmitry Shmidtd97138d2015-12-28 13:27:49 -0800592 const u8 *chosen_pmk,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800593 const u8 *start, size_t elems_len)
594{
595 int ret = 0;
596 struct ieee80211_ampe_ie *ampe;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700597 u8 null_nonce[WPA_NONCE_LEN] = {};
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800598 u8 ampe_eid;
599 u8 ampe_ie_len;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700600 u8 *ampe_buf, *crypt = NULL, *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800601 size_t crypt_len;
602 const u8 *aad[] = { sta->addr, wpa_s->own_addr, cat };
603 const size_t aad_len[] = { ETH_ALEN, ETH_ALEN,
604 (elems->mic - 2) - cat };
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700605 size_t key_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800606
Dmitry Shmidte4663042016-04-04 10:07:49 -0700607 if (!sta->sae) {
608 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
609
610 if (!wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr)) {
611 wpa_printf(MSG_INFO,
612 "Mesh RSN: SAE is not prepared yet");
613 return -1;
614 }
615 mesh_rsn_auth_sae_sta(wpa_s, sta);
616 }
617
Dmitry Shmidtd97138d2015-12-28 13:27:49 -0800618 if (chosen_pmk && os_memcmp(chosen_pmk, sta->sae->pmkid, PMKID_LEN)) {
619 wpa_msg(wpa_s, MSG_DEBUG,
620 "Mesh RSN: Invalid PMKID (Chosen PMK did not match calculated PMKID)");
621 return -1;
622 }
623
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800624 if (!elems->mic || elems->mic_len < AES_BLOCK_SIZE) {
625 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: missing mic ie");
626 return -1;
627 }
628
629 ampe_buf = (u8 *) elems->mic + elems->mic_len;
630 if ((int) elems_len < ampe_buf - start)
631 return -1;
632
633 crypt_len = elems_len - (elems->mic - start);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700634 if (crypt_len < 2 + AES_BLOCK_SIZE) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800635 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: missing ampe ie");
636 return -1;
637 }
638
639 /* crypt is modified by siv_decrypt */
640 crypt = os_zalloc(crypt_len);
641 if (!crypt) {
642 wpa_printf(MSG_ERROR, "Mesh RSN: out of memory");
643 ret = -ENOMEM;
644 goto free;
645 }
646
647 os_memcpy(crypt, elems->mic, crypt_len);
648
649 if (aes_siv_decrypt(sta->aek, crypt, crypt_len, 3,
650 aad, aad_len, ampe_buf)) {
651 wpa_printf(MSG_ERROR, "Mesh RSN: frame verification failed!");
652 ret = -1;
653 goto free;
654 }
655
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700656 crypt_len -= AES_BLOCK_SIZE;
657 wpa_hexdump_key(MSG_DEBUG, "mesh: Decrypted AMPE element",
658 ampe_buf, crypt_len);
659
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800660 ampe_eid = *ampe_buf++;
661 ampe_ie_len = *ampe_buf++;
662
663 if (ampe_eid != WLAN_EID_AMPE ||
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700664 (size_t) 2 + ampe_ie_len > crypt_len ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800665 ampe_ie_len < sizeof(struct ieee80211_ampe_ie)) {
666 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid ampe ie");
667 ret = -1;
668 goto free;
669 }
670
671 ampe = (struct ieee80211_ampe_ie *) ampe_buf;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700672 pos = (u8 *) (ampe + 1);
673 end = ampe_buf + ampe_ie_len;
674 if (os_memcmp(ampe->peer_nonce, null_nonce, WPA_NONCE_LEN) != 0 &&
675 os_memcmp(ampe->peer_nonce, sta->my_nonce, WPA_NONCE_LEN) != 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800676 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid peer nonce");
677 ret = -1;
678 goto free;
679 }
680 os_memcpy(sta->peer_nonce, ampe->local_nonce,
681 sizeof(ampe->local_nonce));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800682
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700683 /* TODO: Key Replay Counter[8] in Mesh Group Key Inform/Acknowledge
684 * frames */
685
686 /*
687 * GTKdata[variable]:
688 * MGTK[variable] || Key RSC[8] || GTKExpirationTime[4]
689 */
690 sta->mgtk_key_id = 1; /* FIX: Where to get Key ID? */
691 key_len = wpa_cipher_key_len(wpa_s->mesh_rsn->group_cipher);
692 if ((int) key_len + WPA_KEY_RSC_LEN + 4 > end - pos) {
693 wpa_dbg(wpa_s, MSG_DEBUG, "mesh: Truncated AMPE element");
694 ret = -1;
695 goto free;
696 }
697 sta->mgtk_len = key_len;
698 os_memcpy(sta->mgtk, pos, sta->mgtk_len);
699 wpa_hexdump_key(MSG_DEBUG, "mesh: GTKdata - MGTK",
700 sta->mgtk, sta->mgtk_len);
701 pos += sta->mgtk_len;
702 wpa_hexdump(MSG_DEBUG, "mesh: GTKdata - MGTK - Key RSC",
703 pos, WPA_KEY_RSC_LEN);
704 os_memcpy(sta->mgtk_rsc, pos, sizeof(sta->mgtk_rsc));
705 pos += WPA_KEY_RSC_LEN;
706 wpa_printf(MSG_DEBUG,
707 "mesh: GTKdata - MGTK - GTKExpirationTime: %u seconds",
708 WPA_GET_LE32(pos));
709 pos += 4;
710
711#ifdef CONFIG_IEEE80211W
712 /*
713 * IGTKdata[variable]:
714 * Key ID[2], IPN[6], IGTK[variable]
715 */
716 key_len = wpa_cipher_key_len(wpa_s->mesh_rsn->mgmt_group_cipher);
717 if (end - pos >= (int) (2 + 6 + key_len)) {
718 sta->igtk_key_id = WPA_GET_LE16(pos);
719 wpa_printf(MSG_DEBUG, "mesh: IGTKdata - Key ID %u",
720 sta->igtk_key_id);
721 pos += 2;
722 os_memcpy(sta->igtk_rsc, pos, sizeof(sta->igtk_rsc));
723 wpa_hexdump(MSG_DEBUG, "mesh: IGTKdata - IPN",
724 sta->igtk_rsc, sizeof(sta->igtk_rsc));
725 pos += 6;
726 os_memcpy(sta->igtk, pos, key_len);
727 sta->igtk_len = key_len;
728 wpa_hexdump_key(MSG_DEBUG, "mesh: IGTKdata - IGTK",
729 sta->igtk, sta->igtk_len);
730 }
731#endif /* CONFIG_IEEE80211W */
732
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800733free:
734 os_free(crypt);
735 return ret;
736}