]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/mesh_rsn.c
Introduce and add key_flag
[thirdparty/hostap.git] / wpa_supplicant / mesh_rsn.c
CommitLineData
0f950df0
TP
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"
c596f3f0 12#include "utils/eloop.h"
0f950df0
TP
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"
a206e2a1 21#include "ap/ieee802_11.h"
0f950df0
TP
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
c596f3f0
CYY
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;
d774c46a 35 struct hostapd_data *hapd;
c596f3f0
CYY
36
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);
dd2cbafc
MH
41 wpa_msg(wpa_s, MSG_INFO, MESH_SAE_AUTH_FAILURE "addr=" MACSTR,
42 MAC2STR(sta->addr));
c596f3f0
CYY
43 if (sta->sae_auth_retry < MESH_AUTH_RETRY) {
44 mesh_rsn_auth_sae_sta(wpa_s, sta);
45 } else {
d774c46a
MH
46 hapd = wpa_s->ifmsh->bss[0];
47
bf51f4f8 48 if (sta->sae_auth_retry > MESH_AUTH_RETRY) {
d774c46a 49 ap_free_sta(hapd, sta);
bf51f4f8
MH
50 return;
51 }
52
c596f3f0 53 /* block the STA if exceeded the number of attempts */
871ff0b7 54 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_BLOCKED);
c596f3f0 55 sta->sae->state = SAE_NOTHING;
79ddb206 56 wpa_msg(wpa_s, MSG_INFO, MESH_SAE_AUTH_BLOCKED "addr="
bf51f4f8
MH
57 MACSTR " duration=%d",
58 MAC2STR(sta->addr),
d774c46a 59 hapd->conf->ap_max_inactivity);
c596f3f0
CYY
60 }
61 sta->sae_auth_retry++;
62 }
63}
64
0f950df0
TP
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,
7a12edd1 78 const u8 *p2p_dev_addr, const u8 *prev_psk,
dbfa691d 79 size_t *psk_len, int *vlan_id)
0f950df0
TP
80{
81 struct mesh_rsn *mesh_rsn = ctx;
82 struct hostapd_data *hapd = mesh_rsn->wpa_s->ifmsh->bss[0];
83 struct sta_info *sta = ap_get_sta(hapd, addr);
84
7a12edd1
JM
85 if (psk_len)
86 *psk_len = PMK_LEN;
dbfa691d
JM
87 if (vlan_id)
88 *vlan_id = 0;
0f950df0
TP
89 wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
90 __func__, MAC2STR(addr), prev_psk);
91
92 if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
93 if (!sta->sae || prev_psk)
94 return NULL;
95 return sta->sae->pmk;
96 }
97
98 return NULL;
99}
100
101
102static int auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
a919a260
AW
103 const u8 *addr, int idx, u8 *key, size_t key_len,
104 enum key_flag key_flag)
0f950df0
TP
105{
106 struct mesh_rsn *mesh_rsn = ctx;
107 u8 seq[6];
108
109 os_memset(seq, 0, sizeof(seq));
110
111 if (addr) {
112 wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d addr=" MACSTR
113 " key_idx=%d)",
114 __func__, alg, MAC2STR(addr), idx);
115 } else {
116 wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d key_idx=%d)",
117 __func__, alg, idx);
118 }
119 wpa_hexdump_key(MSG_DEBUG, "AUTH: set_key - key", key, key_len);
120
121 return wpa_drv_set_key(mesh_rsn->wpa_s, alg, addr, idx,
a919a260 122 1, seq, 6, key, key_len, key_flag);
0f950df0
TP
123}
124
125
126static int auth_start_ampe(void *ctx, const u8 *addr)
127{
128 struct mesh_rsn *mesh_rsn = ctx;
c596f3f0
CYY
129 struct hostapd_data *hapd;
130 struct sta_info *sta;
0f950df0
TP
131
132 if (mesh_rsn->wpa_s->current_ssid->mode != WPAS_MODE_MESH)
133 return -1;
134
c596f3f0
CYY
135 hapd = mesh_rsn->wpa_s->ifmsh->bss[0];
136 sta = ap_get_sta(hapd, addr);
137 if (sta)
138 eloop_cancel_timeout(mesh_auth_timer, mesh_rsn->wpa_s, sta);
139
0f950df0
TP
140 mesh_mpm_auth_peer(mesh_rsn->wpa_s, addr);
141 return 0;
142}
143
144
18aca1a0 145static int __mesh_rsn_auth_init(struct mesh_rsn *rsn, const u8 *addr,
716ed96e 146 enum mfp_options ieee80211w, int ocv)
0f950df0
TP
147{
148 struct wpa_auth_config conf;
cef8fac0
JB
149 static const struct wpa_auth_callbacks cb = {
150 .logger = auth_logger,
151 .get_psk = auth_get_psk,
152 .set_key = auth_set_key,
153 .start_ampe = auth_start_ampe,
154 };
0f950df0
TP
155 u8 seq[6] = {};
156
157 wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
158
159 os_memset(&conf, 0, sizeof(conf));
3b6deac0 160 conf.wpa = WPA_PROTO_RSN;
0f950df0 161 conf.wpa_key_mgmt = WPA_KEY_MGMT_SAE;
3b6deac0
JM
162 conf.wpa_pairwise = rsn->pairwise_cipher;
163 conf.rsn_pairwise = rsn->pairwise_cipher;
164 conf.wpa_group = rsn->group_cipher;
0f950df0
TP
165 conf.eapol_version = 0;
166 conf.wpa_group_rekey = -1;
41f140d3
GK
167 conf.wpa_group_update_count = 4;
168 conf.wpa_pairwise_update_count = 4;
18aca1a0
JM
169 conf.ieee80211w = ieee80211w;
170 if (ieee80211w != NO_MGMT_FRAME_PROTECTION)
3b6deac0 171 conf.group_mgmt_cipher = rsn->mgmt_group_cipher;
716ed96e
MV
172#ifdef CONFIG_OCV
173 conf.ocv = ocv;
174#endif /* CONFIG_OCV */
0f950df0 175
cef8fac0 176 rsn->auth = wpa_init(addr, &conf, &cb, rsn);
0f950df0
TP
177 if (rsn->auth == NULL) {
178 wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
179 return -1;
180 }
181
182 /* TODO: support rekeying */
3b6deac0 183 rsn->mgtk_len = wpa_cipher_key_len(conf.wpa_group);
696f7923 184 if (random_get_bytes(rsn->mgtk, rsn->mgtk_len) < 0)
0f950df0 185 return -1;
f868d560 186 rsn->mgtk_key_id = 1;
0f950df0 187
fccba2c9 188 if (ieee80211w != NO_MGMT_FRAME_PROTECTION) {
3b6deac0 189 rsn->igtk_len = wpa_cipher_key_len(conf.group_mgmt_cipher);
f868d560 190 if (random_get_bytes(rsn->igtk, rsn->igtk_len) < 0)
fccba2c9 191 return -1;
f868d560 192 rsn->igtk_key_id = 4;
fccba2c9
JM
193
194 /* group mgmt */
195 wpa_hexdump_key(MSG_DEBUG, "mesh: Own TX IGTK",
196 rsn->igtk, rsn->igtk_len);
3b6deac0
JM
197 wpa_drv_set_key(rsn->wpa_s,
198 wpa_cipher_to_alg(rsn->mgmt_group_cipher), NULL,
f868d560 199 rsn->igtk_key_id, 1,
a919a260
AW
200 seq, sizeof(seq), rsn->igtk, rsn->igtk_len,
201 KEY_FLAG_GROUP_TX_DEFAULT);
fccba2c9 202 }
0f950df0
TP
203
204 /* group privacy / data frames */
696f7923
JM
205 wpa_hexdump_key(MSG_DEBUG, "mesh: Own TX MGTK",
206 rsn->mgtk, rsn->mgtk_len);
3b6deac0
JM
207 wpa_drv_set_key(rsn->wpa_s, wpa_cipher_to_alg(rsn->group_cipher), NULL,
208 rsn->mgtk_key_id, 1, seq, sizeof(seq),
a919a260 209 rsn->mgtk, rsn->mgtk_len, KEY_FLAG_GROUP_TX_DEFAULT);
0f950df0
TP
210
211 return 0;
212}
213
214
215static void mesh_rsn_deinit(struct mesh_rsn *rsn)
216{
217 os_memset(rsn->mgtk, 0, sizeof(rsn->mgtk));
696f7923 218 rsn->mgtk_len = 0;
fccba2c9
JM
219 os_memset(rsn->igtk, 0, sizeof(rsn->igtk));
220 rsn->igtk_len = 0;
a5d2bf24
MH
221 if (rsn->auth)
222 wpa_deinit(rsn->auth);
0f950df0
TP
223}
224
225
226struct mesh_rsn *mesh_rsn_auth_init(struct wpa_supplicant *wpa_s,
227 struct mesh_conf *conf)
228{
229 struct mesh_rsn *mesh_rsn;
230 struct hostapd_data *bss = wpa_s->ifmsh->bss[0];
231 const u8 *ie;
232 size_t ie_len;
4d77d80e
MH
233#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
234 struct external_pmksa_cache *entry;
235#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
0f950df0
TP
236
237 mesh_rsn = os_zalloc(sizeof(*mesh_rsn));
238 if (mesh_rsn == NULL)
239 return NULL;
240 mesh_rsn->wpa_s = wpa_s;
3b6deac0
JM
241 mesh_rsn->pairwise_cipher = conf->pairwise_cipher;
242 mesh_rsn->group_cipher = conf->group_cipher;
243 mesh_rsn->mgmt_group_cipher = conf->mgmt_group_cipher;
0f950df0 244
18aca1a0 245 if (__mesh_rsn_auth_init(mesh_rsn, wpa_s->own_addr,
716ed96e 246 conf->ieee80211w, conf->ocv) < 0) {
0f950df0 247 mesh_rsn_deinit(mesh_rsn);
449d63d6 248 os_free(mesh_rsn);
0f950df0
TP
249 return NULL;
250 }
251
252 bss->wpa_auth = mesh_rsn->auth;
253
4d77d80e
MH
254#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
255 while ((entry = dl_list_last(&wpa_s->mesh_external_pmksa_cache,
256 struct external_pmksa_cache,
257 list)) != NULL) {
258 int ret;
259
260 ret = wpa_auth_pmksa_add_entry(bss->wpa_auth,
261 entry->pmksa_cache);
262 dl_list_del(&entry->list);
263 os_free(entry);
264
265 if (ret < 0)
266 return NULL;
267 }
268#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
269
0f950df0 270 ie = wpa_auth_get_wpa_ie(mesh_rsn->auth, &ie_len);
8a51dcbc
MH
271 conf->rsn_ie = (u8 *) ie;
272 conf->rsn_ie_len = ie_len;
0f950df0
TP
273
274 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
275
276 return mesh_rsn;
277}
278
279
280static int index_within_array(const int *array, int idx)
281{
282 int i;
283
284 for (i = 0; i < idx; i++) {
285 if (array[i] == -1)
286 return 0;
287 }
288
289 return 1;
290}
291
292
293static int mesh_rsn_sae_group(struct wpa_supplicant *wpa_s,
294 struct sae_data *sae)
295{
296 int *groups = wpa_s->ifmsh->bss[0]->conf->sae_groups;
297
298 /* Configuration may have changed, so validate current index */
299 if (!index_within_array(groups, wpa_s->mesh_rsn->sae_group_index))
300 return -1;
301
302 for (;;) {
303 int group = groups[wpa_s->mesh_rsn->sae_group_index];
304
305 if (group <= 0)
306 break;
307 if (sae_set_group(sae, group) == 0) {
308 wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected SAE group %d",
309 sae->group);
310 return 0;
311 }
312 wpa_s->mesh_rsn->sae_group_index++;
313 }
314
315 return -1;
316}
317
318
a206e2a1
BC
319static int mesh_rsn_build_sae_commit(struct wpa_supplicant *wpa_s,
320 struct wpa_ssid *ssid,
321 struct sta_info *sta)
0f950df0 322{
a34ca59e
JM
323 const char *password;
324
325 password = ssid->sae_password;
326 if (!password)
327 password = ssid->passphrase;
328 if (!password) {
0f950df0 329 wpa_msg(wpa_s, MSG_DEBUG, "SAE: No password available");
a206e2a1 330 return -1;
0f950df0
TP
331 }
332
333 if (mesh_rsn_sae_group(wpa_s, sta->sae) < 0) {
334 wpa_msg(wpa_s, MSG_DEBUG, "SAE: Failed to select group");
a206e2a1 335 return -1;
0f950df0
TP
336 }
337
9be19d0b
JM
338 if (sta->sae->tmp && !sta->sae->tmp->pw_id && ssid->sae_password_id) {
339 sta->sae->tmp->pw_id = os_strdup(ssid->sae_password_id);
340 if (!sta->sae->tmp->pw_id)
341 return -1;
342 }
a206e2a1 343 return sae_prepare_commit(wpa_s->own_addr, sta->addr,
a34ca59e 344 (u8 *) password, os_strlen(password),
9be19d0b 345 ssid->sae_password_id,
a34ca59e 346 sta->sae);
0f950df0
TP
347}
348
349
350/* initiate new SAE authentication with sta */
351int mesh_rsn_auth_sae_sta(struct wpa_supplicant *wpa_s,
352 struct sta_info *sta)
353{
a206e2a1 354 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
0f950df0 355 struct wpa_ssid *ssid = wpa_s->current_ssid;
9f2cf23e 356 struct rsn_pmksa_cache_entry *pmksa;
c596f3f0 357 unsigned int rnd;
a206e2a1 358 int ret;
0f950df0 359
608b0ff5
JM
360 if (!ssid) {
361 wpa_msg(wpa_s, MSG_DEBUG,
362 "AUTH: No current_ssid known to initiate new SAE");
363 return -1;
364 }
365
0f950df0
TP
366 if (!sta->sae) {
367 sta->sae = os_zalloc(sizeof(*sta->sae));
368 if (sta->sae == NULL)
369 return -1;
370 }
371
c1bd4bac 372 pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr, NULL);
9f2cf23e
MH
373 if (pmksa) {
374 if (!sta->wpa_sm)
375 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
376 sta->addr, NULL);
377 if (!sta->wpa_sm) {
378 wpa_printf(MSG_ERROR,
379 "mesh: Failed to initialize RSN state machine");
380 return -1;
381 }
382
383 wpa_printf(MSG_DEBUG,
384 "AUTH: Mesh PMKSA cache entry found for " MACSTR
385 " - try to use PMKSA caching instead of new SAE authentication",
386 MAC2STR(sta->addr));
387 wpa_auth_pmksa_set_to_sm(pmksa, sta->wpa_sm, hapd->wpa_auth,
388 sta->sae->pmkid, sta->sae->pmk);
389 sae_accept_sta(hapd, sta);
390 sta->mesh_sae_pmksa_caching = 1;
391 return 0;
392 }
393 sta->mesh_sae_pmksa_caching = 0;
394
a206e2a1 395 if (mesh_rsn_build_sae_commit(wpa_s, ssid, sta))
0f950df0
TP
396 return -1;
397
398 wpa_msg(wpa_s, MSG_DEBUG,
399 "AUTH: started authentication with SAE peer: " MACSTR,
400 MAC2STR(sta->addr));
401
a206e2a1
BC
402 ret = auth_sae_init_committed(hapd, sta);
403 if (ret)
404 return ret;
0f950df0 405
bf51f4f8 406 eloop_cancel_timeout(mesh_auth_timer, wpa_s, sta);
c596f3f0
CYY
407 rnd = rand() % MESH_AUTH_TIMEOUT;
408 eloop_register_timeout(MESH_AUTH_TIMEOUT + rnd, 0, mesh_auth_timer,
409 wpa_s, sta);
0f950df0
TP
410 return 0;
411}
412
413
414void mesh_rsn_get_pmkid(struct mesh_rsn *rsn, struct sta_info *sta, u8 *pmkid)
415{
6c33eed3 416 os_memcpy(pmkid, sta->sae->pmkid, SAE_PMKID_LEN);
0f950df0
TP
417}
418
419
420static void
421mesh_rsn_derive_aek(struct mesh_rsn *rsn, struct sta_info *sta)
422{
423 u8 *myaddr = rsn->wpa_s->own_addr;
424 u8 *peer = sta->addr;
f5ba6923
JM
425 u8 *addr1, *addr2;
426 u8 context[RSN_SELECTOR_LEN + 2 * ETH_ALEN], *ptr = context;
0f950df0 427
f5ba6923
JM
428 /*
429 * AEK = KDF-Hash-256(PMK, "AEK Derivation", Selected AKM Suite ||
430 * min(localMAC, peerMAC) || max(localMAC, peerMAC))
431 */
a59c5e92 432 /* Selected AKM Suite: SAE */
f5ba6923
JM
433 RSN_SELECTOR_PUT(ptr, RSN_AUTH_KEY_MGMT_SAE);
434 ptr += RSN_SELECTOR_LEN;
0f950df0
TP
435
436 if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
437 addr1 = myaddr;
438 addr2 = peer;
f5ba6923
JM
439 } else {
440 addr1 = peer;
441 addr2 = myaddr;
0f950df0 442 }
f5ba6923
JM
443 os_memcpy(ptr, addr1, ETH_ALEN);
444 ptr += ETH_ALEN;
445 os_memcpy(ptr, addr2, ETH_ALEN);
0f950df0
TP
446
447 sha256_prf(sta->sae->pmk, sizeof(sta->sae->pmk), "AEK Derivation",
448 context, sizeof(context), sta->aek, sizeof(sta->aek));
449}
450
451
452/* derive mesh temporal key from pmk */
453int mesh_rsn_derive_mtk(struct wpa_supplicant *wpa_s, struct sta_info *sta)
454{
455 u8 *ptr;
456 u8 *min, *max;
0f950df0
TP
457 u8 *myaddr = wpa_s->own_addr;
458 u8 *peer = sta->addr;
846201df 459 u8 context[2 * WPA_NONCE_LEN + 2 * 2 + RSN_SELECTOR_LEN + 2 * ETH_ALEN];
0f950df0 460
846201df
JM
461 /*
462 * MTK = KDF-Hash-Length(PMK, "Temporal Key Derivation", min(localNonce,
463 * peerNonce) || max(localNonce, peerNonce) || min(localLinkID,
464 * peerLinkID) || max(localLinkID, peerLinkID) || Selected AKM Suite ||
465 * min(localMAC, peerMAC) || max(localMAC, peerMAC))
466 */
0f950df0 467 ptr = context;
b8b499e4 468 if (os_memcmp(sta->my_nonce, sta->peer_nonce, WPA_NONCE_LEN) < 0) {
0f950df0
TP
469 min = sta->my_nonce;
470 max = sta->peer_nonce;
471 } else {
472 min = sta->peer_nonce;
473 max = sta->my_nonce;
474 }
b8b499e4 475 os_memcpy(ptr, min, WPA_NONCE_LEN);
846201df
JM
476 ptr += WPA_NONCE_LEN;
477 os_memcpy(ptr, max, WPA_NONCE_LEN);
478 ptr += WPA_NONCE_LEN;
0f950df0
TP
479
480 if (sta->my_lid < sta->peer_lid) {
846201df
JM
481 WPA_PUT_LE16(ptr, sta->my_lid);
482 ptr += 2;
483 WPA_PUT_LE16(ptr, sta->peer_lid);
484 ptr += 2;
0f950df0 485 } else {
846201df
JM
486 WPA_PUT_LE16(ptr, sta->peer_lid);
487 ptr += 2;
488 WPA_PUT_LE16(ptr, sta->my_lid);
489 ptr += 2;
0f950df0 490 }
0f950df0 491
0f76d824
JM
492 /* Selected AKM Suite: SAE */
493 RSN_SELECTOR_PUT(ptr, RSN_AUTH_KEY_MGMT_SAE);
846201df 494 ptr += RSN_SELECTOR_LEN;
0f950df0
TP
495
496 if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
497 min = myaddr;
498 max = peer;
499 } else {
500 min = peer;
501 max = myaddr;
502 }
503 os_memcpy(ptr, min, ETH_ALEN);
846201df
JM
504 ptr += ETH_ALEN;
505 os_memcpy(ptr, max, ETH_ALEN);
0f950df0 506
3b6deac0 507 sta->mtk_len = wpa_cipher_key_len(wpa_s->mesh_rsn->pairwise_cipher);
846201df 508 sha256_prf(sta->sae->pmk, SAE_PMK_LEN,
0f950df0 509 "Temporal Key Derivation", context, sizeof(context),
b02f4d05 510 sta->mtk, sta->mtk_len);
0f950df0
TP
511 return 0;
512}
513
514
515void mesh_rsn_init_ampe_sta(struct wpa_supplicant *wpa_s, struct sta_info *sta)
516{
b8b499e4 517 if (random_get_bytes(sta->my_nonce, WPA_NONCE_LEN) < 0) {
0f950df0
TP
518 wpa_printf(MSG_INFO, "mesh: Failed to derive random nonce");
519 /* TODO: How to handle this more cleanly? */
520 }
b8b499e4 521 os_memset(sta->peer_nonce, 0, WPA_NONCE_LEN);
0f950df0
TP
522 mesh_rsn_derive_aek(wpa_s->mesh_rsn, sta);
523}
524
525
526/* insert AMPE and encrypted MIC at @ie.
527 * @mesh_rsn: mesh RSN context
528 * @sta: STA we're sending to
529 * @cat: pointer to category code in frame header.
530 * @buf: wpabuf to add encrypted AMPE and MIC to.
531 * */
532int mesh_rsn_protect_frame(struct mesh_rsn *rsn, struct sta_info *sta,
533 const u8 *cat, struct wpabuf *buf)
534{
535 struct ieee80211_ampe_ie *ampe;
536 u8 const *ie = wpabuf_head_u8(buf) + wpabuf_len(buf);
f868d560 537 u8 *ampe_ie, *pos, *mic_payload;
0f950df0
TP
538 const u8 *aad[] = { rsn->wpa_s->own_addr, sta->addr, cat };
539 const size_t aad_len[] = { ETH_ALEN, ETH_ALEN, ie - cat };
540 int ret = 0;
f868d560 541 size_t len;
0f950df0 542
ee0ad453
JM
543 len = sizeof(*ampe);
544 if (cat[1] == PLINK_OPEN)
545 len += rsn->mgtk_len + WPA_KEY_RSC_LEN + 4;
ee0ad453 546 if (cat[1] == PLINK_OPEN && rsn->igtk_len)
f868d560 547 len += 2 + 6 + rsn->igtk_len;
f868d560
JM
548
549 if (2 + AES_BLOCK_SIZE + 2 + len > wpabuf_tailroom(buf)) {
0f950df0
TP
550 wpa_printf(MSG_ERROR, "protect frame: buffer too small");
551 return -EINVAL;
552 }
553
f868d560 554 ampe_ie = os_zalloc(2 + len);
0f950df0
TP
555 if (!ampe_ie) {
556 wpa_printf(MSG_ERROR, "protect frame: out of memory");
557 return -ENOMEM;
558 }
559
0f950df0
TP
560 /* IE: AMPE */
561 ampe_ie[0] = WLAN_EID_AMPE;
f868d560 562 ampe_ie[1] = len;
0f950df0
TP
563 ampe = (struct ieee80211_ampe_ie *) (ampe_ie + 2);
564
565 RSN_SELECTOR_PUT(ampe->selected_pairwise_suite,
f868d560 566 RSN_CIPHER_SUITE_CCMP);
b8b499e4
JM
567 os_memcpy(ampe->local_nonce, sta->my_nonce, WPA_NONCE_LEN);
568 os_memcpy(ampe->peer_nonce, sta->peer_nonce, WPA_NONCE_LEN);
f868d560
JM
569
570 pos = (u8 *) (ampe + 1);
ee0ad453
JM
571 if (cat[1] != PLINK_OPEN)
572 goto skip_keys;
f868d560
JM
573
574 /* TODO: Key Replay Counter[8] optionally for
575 * Mesh Group Key Inform/Acknowledge frames */
576
0f950df0 577 /* TODO: static mgtk for now since we don't support rekeying! */
f868d560
JM
578 /*
579 * GTKdata[variable]:
580 * MGTK[variable] || Key RSC[8] || GTKExpirationTime[4]
581 */
582 os_memcpy(pos, rsn->mgtk, rsn->mgtk_len);
583 pos += rsn->mgtk_len;
584 wpa_drv_get_seqnum(rsn->wpa_s, NULL, rsn->mgtk_key_id, pos);
585 pos += WPA_KEY_RSC_LEN;
586 /* Use fixed GTKExpirationTime for now */
587 WPA_PUT_LE32(pos, 0xffffffff);
588 pos += 4;
589
f868d560
JM
590 /*
591 * IGTKdata[variable]:
592 * Key ID[2], IPN[6], IGTK[variable]
593 */
594 if (rsn->igtk_len) {
595 WPA_PUT_LE16(pos, rsn->igtk_key_id);
596 pos += 2;
597 wpa_drv_get_seqnum(rsn->wpa_s, NULL, rsn->igtk_key_id, pos);
598 pos += 6;
599 os_memcpy(pos, rsn->igtk, rsn->igtk_len);
600 }
f868d560 601
ee0ad453 602skip_keys:
f868d560
JM
603 wpa_hexdump_key(MSG_DEBUG, "mesh: Plaintext AMPE element",
604 ampe_ie, 2 + len);
0f950df0
TP
605
606 /* IE: MIC */
f868d560
JM
607 wpabuf_put_u8(buf, WLAN_EID_MIC);
608 wpabuf_put_u8(buf, AES_BLOCK_SIZE);
0f950df0
TP
609 /* MIC field is output ciphertext */
610
611 /* encrypt after MIC */
f868d560 612 mic_payload = wpabuf_put(buf, 2 + len + AES_BLOCK_SIZE);
0f950df0 613
325a85be 614 if (aes_siv_encrypt(sta->aek, sizeof(sta->aek), ampe_ie, 2 + len, 3,
0f950df0
TP
615 aad, aad_len, mic_payload)) {
616 wpa_printf(MSG_ERROR, "protect frame: failed to encrypt");
617 ret = -ENOMEM;
0f950df0
TP
618 }
619
0f950df0 620 os_free(ampe_ie);
0f950df0
TP
621
622 return ret;
623}
624
625
626int mesh_rsn_process_ampe(struct wpa_supplicant *wpa_s, struct sta_info *sta,
627 struct ieee802_11_elems *elems, const u8 *cat,
b2817cd5 628 const u8 *chosen_pmk,
0f950df0
TP
629 const u8 *start, size_t elems_len)
630{
631 int ret = 0;
632 struct ieee80211_ampe_ie *ampe;
b8b499e4 633 u8 null_nonce[WPA_NONCE_LEN] = {};
0f950df0
TP
634 u8 ampe_eid;
635 u8 ampe_ie_len;
f868d560 636 u8 *ampe_buf, *crypt = NULL, *pos, *end;
0f950df0
TP
637 size_t crypt_len;
638 const u8 *aad[] = { sta->addr, wpa_s->own_addr, cat };
639 const size_t aad_len[] = { ETH_ALEN, ETH_ALEN,
cc4cdefc 640 elems->mic ? (elems->mic - 2) - cat : 0 };
f868d560 641 size_t key_len;
0f950df0 642
9f2cf23e
MH
643 if (!sta->sae) {
644 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
645
c1bd4bac 646 if (!wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr, NULL)) {
9f2cf23e
MH
647 wpa_printf(MSG_INFO,
648 "Mesh RSN: SAE is not prepared yet");
649 return -1;
650 }
651 mesh_rsn_auth_sae_sta(wpa_s, sta);
652 }
653
9571f945
JM
654 if (chosen_pmk &&
655 (!sta->sae ||
656 os_memcmp(chosen_pmk, sta->sae->pmkid, PMKID_LEN) != 0)) {
b2817cd5
BC
657 wpa_msg(wpa_s, MSG_DEBUG,
658 "Mesh RSN: Invalid PMKID (Chosen PMK did not match calculated PMKID)");
659 return -1;
660 }
661
0f950df0
TP
662 if (!elems->mic || elems->mic_len < AES_BLOCK_SIZE) {
663 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: missing mic ie");
664 return -1;
665 }
666
667 ampe_buf = (u8 *) elems->mic + elems->mic_len;
668 if ((int) elems_len < ampe_buf - start)
669 return -1;
670
671 crypt_len = elems_len - (elems->mic - start);
f868d560 672 if (crypt_len < 2 + AES_BLOCK_SIZE) {
0f950df0
TP
673 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: missing ampe ie");
674 return -1;
675 }
676
677 /* crypt is modified by siv_decrypt */
678 crypt = os_zalloc(crypt_len);
679 if (!crypt) {
680 wpa_printf(MSG_ERROR, "Mesh RSN: out of memory");
681 ret = -ENOMEM;
682 goto free;
683 }
684
685 os_memcpy(crypt, elems->mic, crypt_len);
686
325a85be 687 if (aes_siv_decrypt(sta->aek, sizeof(sta->aek), crypt, crypt_len, 3,
0f950df0
TP
688 aad, aad_len, ampe_buf)) {
689 wpa_printf(MSG_ERROR, "Mesh RSN: frame verification failed!");
1f2f3f1c 690 ret = -2;
0f950df0
TP
691 goto free;
692 }
693
f868d560
JM
694 crypt_len -= AES_BLOCK_SIZE;
695 wpa_hexdump_key(MSG_DEBUG, "mesh: Decrypted AMPE element",
696 ampe_buf, crypt_len);
697
0f950df0
TP
698 ampe_eid = *ampe_buf++;
699 ampe_ie_len = *ampe_buf++;
700
701 if (ampe_eid != WLAN_EID_AMPE ||
f868d560 702 (size_t) 2 + ampe_ie_len > crypt_len ||
0f950df0
TP
703 ampe_ie_len < sizeof(struct ieee80211_ampe_ie)) {
704 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid ampe ie");
705 ret = -1;
706 goto free;
707 }
708
709 ampe = (struct ieee80211_ampe_ie *) ampe_buf;
f868d560
JM
710 pos = (u8 *) (ampe + 1);
711 end = ampe_buf + ampe_ie_len;
b8b499e4
JM
712 if (os_memcmp(ampe->peer_nonce, null_nonce, WPA_NONCE_LEN) != 0 &&
713 os_memcmp(ampe->peer_nonce, sta->my_nonce, WPA_NONCE_LEN) != 0) {
0f950df0
TP
714 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid peer nonce");
715 ret = -1;
716 goto free;
717 }
718 os_memcpy(sta->peer_nonce, ampe->local_nonce,
719 sizeof(ampe->local_nonce));
0f950df0 720
f868d560
JM
721 /* TODO: Key Replay Counter[8] in Mesh Group Key Inform/Acknowledge
722 * frames */
723
ee0ad453
JM
724 /*
725 * GTKdata shall not be included in Mesh Peering Confirm. While the
726 * standard does not state the same about IGTKdata, that same constraint
727 * needs to apply for it. It makes no sense to include the keys in Mesh
728 * Peering Close frames either, so while the standard does not seem to
729 * have a shall statement for these, they are described without
730 * mentioning GTKdata.
731 *
732 * An earlier implementation used to add GTKdata to both Mesh Peering
733 * Open and Mesh Peering Confirm frames, so ignore the possibly present
734 * GTKdata frame without rejecting the frame as a backwards
735 * compatibility mechanism.
736 */
737 if (cat[1] != PLINK_OPEN) {
738 if (end > pos) {
739 wpa_hexdump_key(MSG_DEBUG,
740 "mesh: Ignore unexpected GTKdata(etc.) fields in the end of AMPE element in Mesh Peering Confirm/Close",
741 pos, end - pos);
742 }
743 goto free;
744 }
745
f868d560
JM
746 /*
747 * GTKdata[variable]:
748 * MGTK[variable] || Key RSC[8] || GTKExpirationTime[4]
749 */
3b6deac0
JM
750 sta->mgtk_key_id = 1; /* FIX: Where to get Key ID? */
751 key_len = wpa_cipher_key_len(wpa_s->mesh_rsn->group_cipher);
f868d560
JM
752 if ((int) key_len + WPA_KEY_RSC_LEN + 4 > end - pos) {
753 wpa_dbg(wpa_s, MSG_DEBUG, "mesh: Truncated AMPE element");
754 ret = -1;
755 goto free;
756 }
757 sta->mgtk_len = key_len;
758 os_memcpy(sta->mgtk, pos, sta->mgtk_len);
759 wpa_hexdump_key(MSG_DEBUG, "mesh: GTKdata - MGTK",
760 sta->mgtk, sta->mgtk_len);
761 pos += sta->mgtk_len;
762 wpa_hexdump(MSG_DEBUG, "mesh: GTKdata - MGTK - Key RSC",
763 pos, WPA_KEY_RSC_LEN);
764 os_memcpy(sta->mgtk_rsc, pos, sizeof(sta->mgtk_rsc));
765 pos += WPA_KEY_RSC_LEN;
766 wpa_printf(MSG_DEBUG,
767 "mesh: GTKdata - MGTK - GTKExpirationTime: %u seconds",
768 WPA_GET_LE32(pos));
769 pos += 4;
770
f868d560
JM
771 /*
772 * IGTKdata[variable]:
773 * Key ID[2], IPN[6], IGTK[variable]
774 */
3b6deac0 775 key_len = wpa_cipher_key_len(wpa_s->mesh_rsn->mgmt_group_cipher);
f868d560
JM
776 if (end - pos >= (int) (2 + 6 + key_len)) {
777 sta->igtk_key_id = WPA_GET_LE16(pos);
778 wpa_printf(MSG_DEBUG, "mesh: IGTKdata - Key ID %u",
779 sta->igtk_key_id);
780 pos += 2;
781 os_memcpy(sta->igtk_rsc, pos, sizeof(sta->igtk_rsc));
782 wpa_hexdump(MSG_DEBUG, "mesh: IGTKdata - IPN",
783 sta->igtk_rsc, sizeof(sta->igtk_rsc));
784 pos += 6;
785 os_memcpy(sta->igtk, pos, key_len);
786 sta->igtk_len = key_len;
787 wpa_hexdump_key(MSG_DEBUG, "mesh: IGTKdata - IGTK",
788 sta->igtk, sta->igtk_len);
789 }
f868d560 790
0f950df0
TP
791free:
792 os_free(crypt);
793 return ret;
794}