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