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