]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/rsn_supp/wpa.c
Clear PMK length and check for this when deriving PTK
[thirdparty/hostap.git] / src / rsn_supp / wpa.c
1 /*
2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
3 * Copyright (c) 2003-2017, Jouni Malinen <j@w1.fi>
4 * Copyright(c) 2015 Intel Deutschland GmbH
5 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
10 #include "includes.h"
11
12 #include "common.h"
13 #include "crypto/aes.h"
14 #include "crypto/aes_wrap.h"
15 #include "crypto/crypto.h"
16 #include "crypto/random.h"
17 #include "crypto/aes_siv.h"
18 #include "crypto/sha256.h"
19 #include "crypto/sha384.h"
20 #include "crypto/sha512.h"
21 #include "common/ieee802_11_defs.h"
22 #include "common/ieee802_11_common.h"
23 #include "eap_common/eap_defs.h"
24 #include "eapol_supp/eapol_supp_sm.h"
25 #include "wpa.h"
26 #include "eloop.h"
27 #include "preauth.h"
28 #include "pmksa_cache.h"
29 #include "wpa_i.h"
30 #include "wpa_ie.h"
31
32
33 static const u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
34
35
36 /**
37 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
38 * @sm: Pointer to WPA state machine data from wpa_sm_init()
39 * @ptk: PTK for Key Confirmation/Encryption Key
40 * @ver: Version field from Key Info
41 * @dest: Destination address for the frame
42 * @proto: Ethertype (usually ETH_P_EAPOL)
43 * @msg: EAPOL-Key message
44 * @msg_len: Length of message
45 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
46 * Returns: >= 0 on success, < 0 on failure
47 */
48 int wpa_eapol_key_send(struct wpa_sm *sm, struct wpa_ptk *ptk,
49 int ver, const u8 *dest, u16 proto,
50 u8 *msg, size_t msg_len, u8 *key_mic)
51 {
52 int ret = -1;
53 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
54
55 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL-Key frame to " MACSTR
56 " ver=%d mic_len=%d key_mgmt=0x%x",
57 MAC2STR(dest), ver, (int) mic_len, sm->key_mgmt);
58 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
59 /*
60 * Association event was not yet received; try to fetch
61 * BSSID from the driver.
62 */
63 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
64 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
65 "WPA: Failed to read BSSID for "
66 "EAPOL-Key destination address");
67 } else {
68 dest = sm->bssid;
69 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
70 "WPA: Use BSSID (" MACSTR
71 ") as the destination for EAPOL-Key",
72 MAC2STR(dest));
73 }
74 }
75
76 if (mic_len) {
77 if (key_mic && (!ptk || !ptk->kck_len))
78 goto out;
79
80 if (key_mic &&
81 wpa_eapol_key_mic(ptk->kck, ptk->kck_len, sm->key_mgmt, ver,
82 msg, msg_len, key_mic)) {
83 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
84 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
85 ver, sm->key_mgmt);
86 goto out;
87 }
88 if (ptk)
89 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK",
90 ptk->kck, ptk->kck_len);
91 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC",
92 key_mic, mic_len);
93 } else {
94 #ifdef CONFIG_FILS
95 /* AEAD cipher - Key MIC field not used */
96 struct ieee802_1x_hdr *s_hdr, *hdr;
97 struct wpa_eapol_key *s_key, *key;
98 u8 *buf, *s_key_data, *key_data;
99 size_t buf_len = msg_len + AES_BLOCK_SIZE;
100 size_t key_data_len;
101 u16 eapol_len;
102 const u8 *aad[1];
103 size_t aad_len[1];
104
105 if (!ptk || !ptk->kek_len)
106 goto out;
107
108 key_data_len = msg_len - sizeof(struct ieee802_1x_hdr) -
109 sizeof(struct wpa_eapol_key) - 2;
110
111 buf = os_malloc(buf_len);
112 if (!buf)
113 goto out;
114
115 os_memcpy(buf, msg, msg_len);
116 hdr = (struct ieee802_1x_hdr *) buf;
117 key = (struct wpa_eapol_key *) (hdr + 1);
118 key_data = ((u8 *) (key + 1)) + 2;
119
120 /* Update EAPOL header to include AES-SIV overhead */
121 eapol_len = be_to_host16(hdr->length);
122 eapol_len += AES_BLOCK_SIZE;
123 hdr->length = host_to_be16(eapol_len);
124
125 /* Update Key Data Length field to include AES-SIV overhead */
126 WPA_PUT_BE16((u8 *) (key + 1), AES_BLOCK_SIZE + key_data_len);
127
128 s_hdr = (struct ieee802_1x_hdr *) msg;
129 s_key = (struct wpa_eapol_key *) (s_hdr + 1);
130 s_key_data = ((u8 *) (s_key + 1)) + 2;
131
132 wpa_hexdump_key(MSG_DEBUG, "WPA: Plaintext Key Data",
133 s_key_data, key_data_len);
134
135 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len);
136 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
137 * to Key Data (exclusive). */
138 aad[0] = buf;
139 aad_len[0] = key_data - buf;
140 if (aes_siv_encrypt(ptk->kek, ptk->kek_len,
141 s_key_data, key_data_len,
142 1, aad, aad_len, key_data) < 0) {
143 os_free(buf);
144 goto out;
145 }
146
147 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
148 key_data, AES_BLOCK_SIZE + key_data_len);
149
150 os_free(msg);
151 msg = buf;
152 msg_len = buf_len;
153 #else /* CONFIG_FILS */
154 goto out;
155 #endif /* CONFIG_FILS */
156 }
157
158 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
159 ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
160 eapol_sm_notify_tx_eapol_key(sm->eapol);
161 out:
162 os_free(msg);
163 return ret;
164 }
165
166
167 /**
168 * wpa_sm_key_request - Send EAPOL-Key Request
169 * @sm: Pointer to WPA state machine data from wpa_sm_init()
170 * @error: Indicate whether this is an Michael MIC error report
171 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
172 *
173 * Send an EAPOL-Key Request to the current authenticator. This function is
174 * used to request rekeying and it is usually called when a local Michael MIC
175 * failure is detected.
176 */
177 void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
178 {
179 size_t mic_len, hdrlen, rlen;
180 struct wpa_eapol_key *reply;
181 int key_info, ver;
182 u8 bssid[ETH_ALEN], *rbuf, *key_mic, *mic;
183
184 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
185 wpa_key_mgmt_suite_b(sm->key_mgmt))
186 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
187 else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
188 wpa_key_mgmt_sha256(sm->key_mgmt))
189 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
190 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
191 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
192 else
193 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
194
195 if (wpa_sm_get_bssid(sm, bssid) < 0) {
196 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
197 "Failed to read BSSID for EAPOL-Key request");
198 return;
199 }
200
201 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
202 hdrlen = sizeof(*reply) + mic_len + 2;
203 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
204 hdrlen, &rlen, (void *) &reply);
205 if (rbuf == NULL)
206 return;
207
208 reply->type = (sm->proto == WPA_PROTO_RSN ||
209 sm->proto == WPA_PROTO_OSEN) ?
210 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
211 key_info = WPA_KEY_INFO_REQUEST | ver;
212 if (sm->ptk_set)
213 key_info |= WPA_KEY_INFO_SECURE;
214 if (sm->ptk_set && mic_len)
215 key_info |= WPA_KEY_INFO_MIC;
216 if (error)
217 key_info |= WPA_KEY_INFO_ERROR;
218 if (pairwise)
219 key_info |= WPA_KEY_INFO_KEY_TYPE;
220 WPA_PUT_BE16(reply->key_info, key_info);
221 WPA_PUT_BE16(reply->key_length, 0);
222 os_memcpy(reply->replay_counter, sm->request_counter,
223 WPA_REPLAY_COUNTER_LEN);
224 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
225
226 mic = (u8 *) (reply + 1);
227 WPA_PUT_BE16(mic + mic_len, 0);
228 if (!(key_info & WPA_KEY_INFO_MIC))
229 key_mic = NULL;
230 else
231 key_mic = mic;
232
233 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
234 "WPA: Sending EAPOL-Key Request (error=%d "
235 "pairwise=%d ptk_set=%d len=%lu)",
236 error, pairwise, sm->ptk_set, (unsigned long) rlen);
237 wpa_eapol_key_send(sm, &sm->ptk, ver, bssid, ETH_P_EAPOL, rbuf, rlen,
238 key_mic);
239 }
240
241
242 static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
243 {
244 #ifdef CONFIG_IEEE80211R
245 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
246 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
247 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
248 "RSN: Cannot set low order 256 bits of MSK for key management offload");
249 } else {
250 #endif /* CONFIG_IEEE80211R */
251 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
252 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
253 "RSN: Cannot set PMK for key management offload");
254 #ifdef CONFIG_IEEE80211R
255 }
256 #endif /* CONFIG_IEEE80211R */
257 }
258
259
260 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
261 const unsigned char *src_addr,
262 const u8 *pmkid)
263 {
264 int abort_cached = 0;
265
266 if (pmkid && !sm->cur_pmksa) {
267 /* When using drivers that generate RSN IE, wpa_supplicant may
268 * not have enough time to get the association information
269 * event before receiving this 1/4 message, so try to find a
270 * matching PMKSA cache entry here. */
271 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
272 NULL);
273 if (sm->cur_pmksa) {
274 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
275 "RSN: found matching PMKID from PMKSA cache");
276 } else {
277 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
278 "RSN: no matching PMKID found");
279 abort_cached = 1;
280 }
281 }
282
283 if (pmkid && sm->cur_pmksa &&
284 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
285 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
286 wpa_sm_set_pmk_from_pmksa(sm);
287 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
288 sm->pmk, sm->pmk_len);
289 eapol_sm_notify_cached(sm->eapol);
290 #ifdef CONFIG_IEEE80211R
291 sm->xxkey_len = 0;
292 #endif /* CONFIG_IEEE80211R */
293 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
294 int res, pmk_len;
295
296 if (wpa_key_mgmt_sha384(sm->key_mgmt))
297 pmk_len = PMK_LEN_SUITE_B_192;
298 else
299 pmk_len = PMK_LEN;
300 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
301 if (res) {
302 if (pmk_len == PMK_LEN) {
303 /*
304 * EAP-LEAP is an exception from other EAP
305 * methods: it uses only 16-byte PMK.
306 */
307 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
308 pmk_len = 16;
309 }
310 } else {
311 #ifdef CONFIG_IEEE80211R
312 u8 buf[2 * PMK_LEN];
313 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
314 {
315 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
316 sm->xxkey_len = PMK_LEN;
317 os_memset(buf, 0, sizeof(buf));
318 }
319 #endif /* CONFIG_IEEE80211R */
320 }
321 if (res == 0) {
322 struct rsn_pmksa_cache_entry *sa = NULL;
323 const u8 *fils_cache_id = NULL;
324
325 #ifdef CONFIG_FILS
326 if (sm->fils_cache_id_set)
327 fils_cache_id = sm->fils_cache_id;
328 #endif /* CONFIG_FILS */
329
330 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
331 "machines", sm->pmk, pmk_len);
332 sm->pmk_len = pmk_len;
333 wpa_supplicant_key_mgmt_set_pmk(sm);
334 if (sm->proto == WPA_PROTO_RSN &&
335 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
336 !wpa_key_mgmt_ft(sm->key_mgmt)) {
337 sa = pmksa_cache_add(sm->pmksa,
338 sm->pmk, pmk_len, NULL,
339 NULL, 0,
340 src_addr, sm->own_addr,
341 sm->network_ctx,
342 sm->key_mgmt,
343 fils_cache_id);
344 }
345 if (!sm->cur_pmksa && pmkid &&
346 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL))
347 {
348 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
349 "RSN: the new PMK matches with the "
350 "PMKID");
351 abort_cached = 0;
352 } else if (sa && !sm->cur_pmksa && pmkid) {
353 /*
354 * It looks like the authentication server
355 * derived mismatching MSK. This should not
356 * really happen, but bugs happen.. There is not
357 * much we can do here without knowing what
358 * exactly caused the server to misbehave.
359 */
360 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
361 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
362 return -1;
363 }
364
365 if (!sm->cur_pmksa)
366 sm->cur_pmksa = sa;
367 } else {
368 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
369 "WPA: Failed to get master session key from "
370 "EAPOL state machines - key handshake "
371 "aborted");
372 if (sm->cur_pmksa) {
373 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
374 "RSN: Cancelled PMKSA caching "
375 "attempt");
376 sm->cur_pmksa = NULL;
377 abort_cached = 1;
378 } else if (!abort_cached) {
379 return -1;
380 }
381 }
382 }
383
384 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
385 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
386 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
387 {
388 /* Send EAPOL-Start to trigger full EAP authentication. */
389 u8 *buf;
390 size_t buflen;
391
392 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
393 "RSN: no PMKSA entry found - trigger "
394 "full EAP authentication");
395 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
396 NULL, 0, &buflen, NULL);
397 if (buf) {
398 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
399 buf, buflen);
400 os_free(buf);
401 return -2;
402 }
403
404 return -1;
405 }
406
407 return 0;
408 }
409
410
411 /**
412 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
413 * @sm: Pointer to WPA state machine data from wpa_sm_init()
414 * @dst: Destination address for the frame
415 * @key: Pointer to the EAPOL-Key frame header
416 * @ver: Version bits from EAPOL-Key Key Info
417 * @nonce: Nonce value for the EAPOL-Key frame
418 * @wpa_ie: WPA/RSN IE
419 * @wpa_ie_len: Length of the WPA/RSN IE
420 * @ptk: PTK to use for keyed hash and encryption
421 * Returns: >= 0 on success, < 0 on failure
422 */
423 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
424 const struct wpa_eapol_key *key,
425 int ver, const u8 *nonce,
426 const u8 *wpa_ie, size_t wpa_ie_len,
427 struct wpa_ptk *ptk)
428 {
429 size_t mic_len, hdrlen, rlen;
430 struct wpa_eapol_key *reply;
431 u8 *rbuf, *key_mic;
432 u8 *rsn_ie_buf = NULL;
433 u16 key_info;
434
435 if (wpa_ie == NULL) {
436 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
437 "cannot generate msg 2/4");
438 return -1;
439 }
440
441 #ifdef CONFIG_IEEE80211R
442 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
443 int res;
444
445 /*
446 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
447 * FTIE from (Re)Association Response.
448 */
449 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
450 sm->assoc_resp_ies_len);
451 if (rsn_ie_buf == NULL)
452 return -1;
453 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
454 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
455 sm->pmk_r1_name);
456 if (res < 0) {
457 os_free(rsn_ie_buf);
458 return -1;
459 }
460
461 if (sm->assoc_resp_ies) {
462 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
463 sm->assoc_resp_ies_len);
464 wpa_ie_len += sm->assoc_resp_ies_len;
465 }
466
467 wpa_ie = rsn_ie_buf;
468 }
469 #endif /* CONFIG_IEEE80211R */
470
471 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
472
473 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
474 hdrlen = sizeof(*reply) + mic_len + 2;
475 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
476 NULL, hdrlen + wpa_ie_len,
477 &rlen, (void *) &reply);
478 if (rbuf == NULL) {
479 os_free(rsn_ie_buf);
480 return -1;
481 }
482
483 reply->type = (sm->proto == WPA_PROTO_RSN ||
484 sm->proto == WPA_PROTO_OSEN) ?
485 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
486 key_info = ver | WPA_KEY_INFO_KEY_TYPE;
487 if (mic_len)
488 key_info |= WPA_KEY_INFO_MIC;
489 else
490 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
491 WPA_PUT_BE16(reply->key_info, key_info);
492 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
493 WPA_PUT_BE16(reply->key_length, 0);
494 else
495 os_memcpy(reply->key_length, key->key_length, 2);
496 os_memcpy(reply->replay_counter, key->replay_counter,
497 WPA_REPLAY_COUNTER_LEN);
498 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
499 WPA_REPLAY_COUNTER_LEN);
500
501 key_mic = (u8 *) (reply + 1);
502 WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */
503 os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
504 os_free(rsn_ie_buf);
505
506 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
507
508 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
509 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
510 key_mic);
511 }
512
513
514 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
515 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
516 {
517 #ifdef CONFIG_IEEE80211R
518 if (wpa_key_mgmt_ft(sm->key_mgmt))
519 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
520 #endif /* CONFIG_IEEE80211R */
521
522 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
523 sm->own_addr, sm->bssid, sm->snonce,
524 key->key_nonce, ptk, sm->key_mgmt,
525 sm->pairwise_cipher);
526 }
527
528
529 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
530 const unsigned char *src_addr,
531 const struct wpa_eapol_key *key,
532 u16 ver, const u8 *key_data,
533 size_t key_data_len)
534 {
535 struct wpa_eapol_ie_parse ie;
536 struct wpa_ptk *ptk;
537 int res;
538 u8 *kde, *kde_buf = NULL;
539 size_t kde_len;
540
541 if (wpa_sm_get_network_ctx(sm) == NULL) {
542 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
543 "found (msg 1 of 4)");
544 return;
545 }
546
547 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
548 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
549 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
550
551 os_memset(&ie, 0, sizeof(ie));
552
553 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
554 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
555 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
556 key_data, key_data_len);
557 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
558 goto failed;
559 if (ie.pmkid) {
560 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
561 "Authenticator", ie.pmkid, PMKID_LEN);
562 }
563 }
564
565 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
566 if (res == -2) {
567 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
568 "msg 1/4 - requesting full EAP authentication");
569 return;
570 }
571 if (res)
572 goto failed;
573
574 if (sm->renew_snonce) {
575 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
576 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
577 "WPA: Failed to get random data for SNonce");
578 goto failed;
579 }
580 sm->renew_snonce = 0;
581 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
582 sm->snonce, WPA_NONCE_LEN);
583 }
584
585 /* Calculate PTK which will be stored as a temporary PTK until it has
586 * been verified when processing message 3/4. */
587 ptk = &sm->tptk;
588 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
589 goto failed;
590 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
591 u8 buf[8];
592 /* Supplicant: swap tx/rx Mic keys */
593 os_memcpy(buf, &ptk->tk[16], 8);
594 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
595 os_memcpy(&ptk->tk[24], buf, 8);
596 os_memset(buf, 0, sizeof(buf));
597 }
598 sm->tptk_set = 1;
599
600 kde = sm->assoc_wpa_ie;
601 kde_len = sm->assoc_wpa_ie_len;
602
603 #ifdef CONFIG_P2P
604 if (sm->p2p) {
605 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
606 if (kde_buf) {
607 u8 *pos;
608 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
609 "into EAPOL-Key 2/4");
610 os_memcpy(kde_buf, kde, kde_len);
611 kde = kde_buf;
612 pos = kde + kde_len;
613 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
614 *pos++ = RSN_SELECTOR_LEN + 1;
615 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
616 pos += RSN_SELECTOR_LEN;
617 *pos++ = 0x01;
618 kde_len = pos - kde;
619 }
620 }
621 #endif /* CONFIG_P2P */
622
623 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
624 kde, kde_len, ptk) < 0)
625 goto failed;
626
627 os_free(kde_buf);
628 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
629 return;
630
631 failed:
632 os_free(kde_buf);
633 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
634 }
635
636
637 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
638 {
639 struct wpa_sm *sm = eloop_ctx;
640 rsn_preauth_candidate_process(sm);
641 }
642
643
644 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
645 const u8 *addr, int secure)
646 {
647 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
648 "WPA: Key negotiation completed with "
649 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
650 wpa_cipher_txt(sm->pairwise_cipher),
651 wpa_cipher_txt(sm->group_cipher));
652 wpa_sm_cancel_auth_timeout(sm);
653 wpa_sm_set_state(sm, WPA_COMPLETED);
654
655 if (secure) {
656 wpa_sm_mlme_setprotection(
657 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
658 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
659 eapol_sm_notify_portValid(sm->eapol, TRUE);
660 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
661 sm->key_mgmt == WPA_KEY_MGMT_DPP ||
662 sm->key_mgmt == WPA_KEY_MGMT_OWE)
663 eapol_sm_notify_eap_success(sm->eapol, TRUE);
664 /*
665 * Start preauthentication after a short wait to avoid a
666 * possible race condition between the data receive and key
667 * configuration after the 4-Way Handshake. This increases the
668 * likelihood of the first preauth EAPOL-Start frame getting to
669 * the target AP.
670 */
671 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
672 }
673
674 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
675 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
676 "RSN: Authenticator accepted "
677 "opportunistic PMKSA entry - marking it valid");
678 sm->cur_pmksa->opportunistic = 0;
679 }
680
681 #ifdef CONFIG_IEEE80211R
682 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
683 /* Prepare for the next transition */
684 wpa_ft_prepare_auth_request(sm, NULL);
685 }
686 #endif /* CONFIG_IEEE80211R */
687 }
688
689
690 static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
691 {
692 struct wpa_sm *sm = eloop_ctx;
693 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
694 wpa_sm_key_request(sm, 0, 1);
695 }
696
697
698 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
699 const struct wpa_eapol_key *key)
700 {
701 int keylen, rsclen;
702 enum wpa_alg alg;
703 const u8 *key_rsc;
704
705 if (sm->ptk.installed) {
706 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
707 "WPA: Do not re-install same PTK to the driver");
708 return 0;
709 }
710
711 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
712 "WPA: Installing PTK to the driver");
713
714 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
715 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
716 "Suite: NONE - do not use pairwise keys");
717 return 0;
718 }
719
720 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
721 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
722 "WPA: Unsupported pairwise cipher %d",
723 sm->pairwise_cipher);
724 return -1;
725 }
726
727 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
728 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
729 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
730
731 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
732 key_rsc = null_rsc;
733 } else {
734 key_rsc = key->key_rsc;
735 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
736 }
737
738 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
739 sm->ptk.tk, keylen) < 0) {
740 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
741 "WPA: Failed to set PTK to the "
742 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
743 alg, keylen, MAC2STR(sm->bssid));
744 return -1;
745 }
746
747 /* TK is not needed anymore in supplicant */
748 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
749 sm->ptk.installed = 1;
750
751 if (sm->wpa_ptk_rekey) {
752 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
753 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
754 sm, NULL);
755 }
756
757 return 0;
758 }
759
760
761 static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
762 int group_cipher,
763 int keylen, int maxkeylen,
764 int *key_rsc_len,
765 enum wpa_alg *alg)
766 {
767 int klen;
768
769 *alg = wpa_cipher_to_alg(group_cipher);
770 if (*alg == WPA_ALG_NONE) {
771 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
772 "WPA: Unsupported Group Cipher %d",
773 group_cipher);
774 return -1;
775 }
776 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
777
778 klen = wpa_cipher_key_len(group_cipher);
779 if (keylen != klen || maxkeylen < klen) {
780 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
781 "WPA: Unsupported %s Group Cipher key length %d (%d)",
782 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
783 return -1;
784 }
785 return 0;
786 }
787
788
789 struct wpa_gtk_data {
790 enum wpa_alg alg;
791 int tx, key_rsc_len, keyidx;
792 u8 gtk[32];
793 int gtk_len;
794 };
795
796
797 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
798 const struct wpa_gtk_data *gd,
799 const u8 *key_rsc, int wnm_sleep)
800 {
801 const u8 *_gtk = gd->gtk;
802 u8 gtk_buf[32];
803
804 /* Detect possible key reinstallation */
805 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
806 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
807 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
808 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
809 sm->gtk_wnm_sleep.gtk_len) == 0)) {
810 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
811 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
812 gd->keyidx, gd->tx, gd->gtk_len);
813 return 0;
814 }
815
816 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
817 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
818 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
819 gd->keyidx, gd->tx, gd->gtk_len);
820 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
821 if (sm->group_cipher == WPA_CIPHER_TKIP) {
822 /* Swap Tx/Rx keys for Michael MIC */
823 os_memcpy(gtk_buf, gd->gtk, 16);
824 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
825 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
826 _gtk = gtk_buf;
827 }
828 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
829 if (wpa_sm_set_key(sm, gd->alg, NULL,
830 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
831 _gtk, gd->gtk_len) < 0) {
832 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
833 "WPA: Failed to set GTK to the driver "
834 "(Group only)");
835 os_memset(gtk_buf, 0, sizeof(gtk_buf));
836 return -1;
837 }
838 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
839 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
840 _gtk, gd->gtk_len) < 0) {
841 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
842 "WPA: Failed to set GTK to "
843 "the driver (alg=%d keylen=%d keyidx=%d)",
844 gd->alg, gd->gtk_len, gd->keyidx);
845 os_memset(gtk_buf, 0, sizeof(gtk_buf));
846 return -1;
847 }
848 os_memset(gtk_buf, 0, sizeof(gtk_buf));
849
850 if (wnm_sleep) {
851 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
852 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
853 sm->gtk_wnm_sleep.gtk_len);
854 } else {
855 sm->gtk.gtk_len = gd->gtk_len;
856 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
857 }
858
859 return 0;
860 }
861
862
863 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
864 int tx)
865 {
866 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
867 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
868 * seemed to set this bit (incorrectly, since Tx is only when
869 * doing Group Key only APs) and without this workaround, the
870 * data connection does not work because wpa_supplicant
871 * configured non-zero keyidx to be used for unicast. */
872 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
873 "WPA: Tx bit set for GTK, but pairwise "
874 "keys are used - ignore Tx bit");
875 return 0;
876 }
877 return tx;
878 }
879
880
881 static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
882 const u8 *rsc)
883 {
884 int rsclen;
885
886 if (!sm->wpa_rsc_relaxation)
887 return 0;
888
889 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
890
891 /*
892 * Try to detect RSC (endian) corruption issue where the AP sends
893 * the RSC bytes in EAPOL-Key message in the wrong order, both if
894 * it's actually a 6-byte field (as it should be) and if it treats
895 * it as an 8-byte field.
896 * An AP model known to have this bug is the Sapido RB-1632.
897 */
898 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
899 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
900 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
901 rsc[0], rsc[1], rsc[2], rsc[3],
902 rsc[4], rsc[5], rsc[6], rsc[7]);
903
904 return 1;
905 }
906
907 return 0;
908 }
909
910
911 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
912 const struct wpa_eapol_key *key,
913 const u8 *gtk, size_t gtk_len,
914 int key_info)
915 {
916 struct wpa_gtk_data gd;
917 const u8 *key_rsc;
918
919 /*
920 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
921 * GTK KDE format:
922 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
923 * Reserved [bits 0-7]
924 * GTK
925 */
926
927 os_memset(&gd, 0, sizeof(gd));
928 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
929 gtk, gtk_len);
930
931 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
932 return -1;
933
934 gd.keyidx = gtk[0] & 0x3;
935 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
936 !!(gtk[0] & BIT(2)));
937 gtk += 2;
938 gtk_len -= 2;
939
940 os_memcpy(gd.gtk, gtk, gtk_len);
941 gd.gtk_len = gtk_len;
942
943 key_rsc = key->key_rsc;
944 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
945 key_rsc = null_rsc;
946
947 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
948 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
949 gtk_len, gtk_len,
950 &gd.key_rsc_len, &gd.alg) ||
951 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
952 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
953 "RSN: Failed to install GTK");
954 os_memset(&gd, 0, sizeof(gd));
955 return -1;
956 }
957 os_memset(&gd, 0, sizeof(gd));
958
959 wpa_supplicant_key_neg_complete(sm, sm->bssid,
960 key_info & WPA_KEY_INFO_SECURE);
961 return 0;
962 }
963
964
965 #ifdef CONFIG_IEEE80211W
966 static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
967 const struct wpa_igtk_kde *igtk,
968 int wnm_sleep)
969 {
970 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
971 u16 keyidx = WPA_GET_LE16(igtk->keyid);
972
973 /* Detect possible key reinstallation */
974 if ((sm->igtk.igtk_len == len &&
975 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
976 (sm->igtk_wnm_sleep.igtk_len == len &&
977 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
978 sm->igtk_wnm_sleep.igtk_len) == 0)) {
979 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
980 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
981 keyidx);
982 return 0;
983 }
984
985 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
986 "WPA: IGTK keyid %d pn %02x%02x%02x%02x%02x%02x",
987 keyidx, MAC2STR(igtk->pn));
988 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
989 if (keyidx > 4095) {
990 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
991 "WPA: Invalid IGTK KeyID %d", keyidx);
992 return -1;
993 }
994 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
995 broadcast_ether_addr,
996 keyidx, 0, igtk->pn, sizeof(igtk->pn),
997 igtk->igtk, len) < 0) {
998 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
999 "WPA: Failed to configure IGTK to the driver");
1000 return -1;
1001 }
1002
1003 if (wnm_sleep) {
1004 sm->igtk_wnm_sleep.igtk_len = len;
1005 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1006 sm->igtk_wnm_sleep.igtk_len);
1007 } else {
1008 sm->igtk.igtk_len = len;
1009 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
1010 }
1011
1012 return 0;
1013 }
1014 #endif /* CONFIG_IEEE80211W */
1015
1016
1017 static int ieee80211w_set_keys(struct wpa_sm *sm,
1018 struct wpa_eapol_ie_parse *ie)
1019 {
1020 #ifdef CONFIG_IEEE80211W
1021 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
1022 return 0;
1023
1024 if (ie->igtk) {
1025 size_t len;
1026 const struct wpa_igtk_kde *igtk;
1027
1028 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1029 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
1030 return -1;
1031
1032 igtk = (const struct wpa_igtk_kde *) ie->igtk;
1033 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
1034 return -1;
1035 }
1036
1037 return 0;
1038 #else /* CONFIG_IEEE80211W */
1039 return 0;
1040 #endif /* CONFIG_IEEE80211W */
1041 }
1042
1043
1044 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1045 const char *reason, const u8 *src_addr,
1046 const u8 *wpa_ie, size_t wpa_ie_len,
1047 const u8 *rsn_ie, size_t rsn_ie_len)
1048 {
1049 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
1050 reason, MAC2STR(src_addr));
1051
1052 if (sm->ap_wpa_ie) {
1053 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1054 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1055 }
1056 if (wpa_ie) {
1057 if (!sm->ap_wpa_ie) {
1058 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1059 "WPA: No WPA IE in Beacon/ProbeResp");
1060 }
1061 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1062 wpa_ie, wpa_ie_len);
1063 }
1064
1065 if (sm->ap_rsn_ie) {
1066 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1067 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1068 }
1069 if (rsn_ie) {
1070 if (!sm->ap_rsn_ie) {
1071 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1072 "WPA: No RSN IE in Beacon/ProbeResp");
1073 }
1074 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1075 rsn_ie, rsn_ie_len);
1076 }
1077
1078 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
1079 }
1080
1081
1082 #ifdef CONFIG_IEEE80211R
1083
1084 static int ft_validate_mdie(struct wpa_sm *sm,
1085 const unsigned char *src_addr,
1086 struct wpa_eapol_ie_parse *ie,
1087 const u8 *assoc_resp_mdie)
1088 {
1089 struct rsn_mdie *mdie;
1090
1091 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1092 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1093 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1094 MOBILITY_DOMAIN_ID_LEN) != 0) {
1095 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
1096 "not match with the current mobility domain");
1097 return -1;
1098 }
1099
1100 if (assoc_resp_mdie &&
1101 (assoc_resp_mdie[1] != ie->mdie[1] ||
1102 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
1103 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
1104 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
1105 ie->mdie, 2 + ie->mdie[1]);
1106 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1107 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1108 return -1;
1109 }
1110
1111 return 0;
1112 }
1113
1114
1115 static int ft_validate_ftie(struct wpa_sm *sm,
1116 const unsigned char *src_addr,
1117 struct wpa_eapol_ie_parse *ie,
1118 const u8 *assoc_resp_ftie)
1119 {
1120 if (ie->ftie == NULL) {
1121 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1122 "FT: No FTIE in EAPOL-Key msg 3/4");
1123 return -1;
1124 }
1125
1126 if (assoc_resp_ftie == NULL)
1127 return 0;
1128
1129 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1130 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
1131 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
1132 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1133 ie->ftie, 2 + ie->ftie[1]);
1134 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1135 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1136 return -1;
1137 }
1138
1139 return 0;
1140 }
1141
1142
1143 static int ft_validate_rsnie(struct wpa_sm *sm,
1144 const unsigned char *src_addr,
1145 struct wpa_eapol_ie_parse *ie)
1146 {
1147 struct wpa_ie_data rsn;
1148
1149 if (!ie->rsn_ie)
1150 return 0;
1151
1152 /*
1153 * Verify that PMKR1Name from EAPOL-Key message 3/4
1154 * matches with the value we derived.
1155 */
1156 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1157 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1158 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1159 "FT 4-way handshake message 3/4");
1160 return -1;
1161 }
1162
1163 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1164 {
1165 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1166 "FT: PMKR1Name mismatch in "
1167 "FT 4-way handshake message 3/4");
1168 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1169 rsn.pmkid, WPA_PMK_NAME_LEN);
1170 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1171 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1172 return -1;
1173 }
1174
1175 return 0;
1176 }
1177
1178
1179 static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1180 const unsigned char *src_addr,
1181 struct wpa_eapol_ie_parse *ie)
1182 {
1183 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1184
1185 if (sm->assoc_resp_ies) {
1186 pos = sm->assoc_resp_ies;
1187 end = pos + sm->assoc_resp_ies_len;
1188 while (end - pos > 2) {
1189 if (2 + pos[1] > end - pos)
1190 break;
1191 switch (*pos) {
1192 case WLAN_EID_MOBILITY_DOMAIN:
1193 mdie = pos;
1194 break;
1195 case WLAN_EID_FAST_BSS_TRANSITION:
1196 ftie = pos;
1197 break;
1198 }
1199 pos += 2 + pos[1];
1200 }
1201 }
1202
1203 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1204 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1205 ft_validate_rsnie(sm, src_addr, ie) < 0)
1206 return -1;
1207
1208 return 0;
1209 }
1210
1211 #endif /* CONFIG_IEEE80211R */
1212
1213
1214 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1215 const unsigned char *src_addr,
1216 struct wpa_eapol_ie_parse *ie)
1217 {
1218 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1219 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1220 "WPA: No WPA/RSN IE for this AP known. "
1221 "Trying to get from scan results");
1222 if (wpa_sm_get_beacon_ie(sm) < 0) {
1223 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1224 "WPA: Could not find AP from "
1225 "the scan results");
1226 } else {
1227 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1228 "WPA: Found the current AP from "
1229 "updated scan results");
1230 }
1231 }
1232
1233 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1234 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1235 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1236 "with IE in Beacon/ProbeResp (no IE?)",
1237 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1238 ie->rsn_ie, ie->rsn_ie_len);
1239 return -1;
1240 }
1241
1242 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1243 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1244 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1245 (ie->rsn_ie && sm->ap_rsn_ie &&
1246 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1247 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1248 ie->rsn_ie, ie->rsn_ie_len))) {
1249 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1250 "with IE in Beacon/ProbeResp",
1251 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1252 ie->rsn_ie, ie->rsn_ie_len);
1253 return -1;
1254 }
1255
1256 if (sm->proto == WPA_PROTO_WPA &&
1257 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1258 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1259 "detected - RSN was enabled and RSN IE "
1260 "was in msg 3/4, but not in "
1261 "Beacon/ProbeResp",
1262 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1263 ie->rsn_ie, ie->rsn_ie_len);
1264 return -1;
1265 }
1266
1267 #ifdef CONFIG_IEEE80211R
1268 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1269 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1270 return -1;
1271 #endif /* CONFIG_IEEE80211R */
1272
1273 return 0;
1274 }
1275
1276
1277 /**
1278 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1279 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1280 * @dst: Destination address for the frame
1281 * @key: Pointer to the EAPOL-Key frame header
1282 * @ver: Version bits from EAPOL-Key Key Info
1283 * @key_info: Key Info
1284 * @ptk: PTK to use for keyed hash and encryption
1285 * Returns: >= 0 on success, < 0 on failure
1286 */
1287 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1288 const struct wpa_eapol_key *key,
1289 u16 ver, u16 key_info,
1290 struct wpa_ptk *ptk)
1291 {
1292 size_t mic_len, hdrlen, rlen;
1293 struct wpa_eapol_key *reply;
1294 u8 *rbuf, *key_mic;
1295
1296 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
1297 hdrlen = sizeof(*reply) + mic_len + 2;
1298 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1299 hdrlen, &rlen, (void *) &reply);
1300 if (rbuf == NULL)
1301 return -1;
1302
1303 reply->type = (sm->proto == WPA_PROTO_RSN ||
1304 sm->proto == WPA_PROTO_OSEN) ?
1305 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1306 key_info &= WPA_KEY_INFO_SECURE;
1307 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
1308 if (mic_len)
1309 key_info |= WPA_KEY_INFO_MIC;
1310 else
1311 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1312 WPA_PUT_BE16(reply->key_info, key_info);
1313 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1314 WPA_PUT_BE16(reply->key_length, 0);
1315 else
1316 os_memcpy(reply->key_length, key->key_length, 2);
1317 os_memcpy(reply->replay_counter, key->replay_counter,
1318 WPA_REPLAY_COUNTER_LEN);
1319
1320 key_mic = (u8 *) (reply + 1);
1321 WPA_PUT_BE16(key_mic + mic_len, 0);
1322
1323 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
1324 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
1325 key_mic);
1326 }
1327
1328
1329 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1330 const struct wpa_eapol_key *key,
1331 u16 ver, const u8 *key_data,
1332 size_t key_data_len)
1333 {
1334 u16 key_info, keylen;
1335 struct wpa_eapol_ie_parse ie;
1336
1337 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1338 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1339 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1340
1341 key_info = WPA_GET_BE16(key->key_info);
1342
1343 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1344 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
1345 goto failed;
1346 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1347 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1348 "WPA: GTK IE in unencrypted key data");
1349 goto failed;
1350 }
1351 #ifdef CONFIG_IEEE80211W
1352 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1353 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1354 "WPA: IGTK KDE in unencrypted key data");
1355 goto failed;
1356 }
1357
1358 if (ie.igtk &&
1359 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1360 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1361 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
1362 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1363 "WPA: Invalid IGTK KDE length %lu",
1364 (unsigned long) ie.igtk_len);
1365 goto failed;
1366 }
1367 #endif /* CONFIG_IEEE80211W */
1368
1369 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1370 goto failed;
1371
1372 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1373 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1374 "WPA: ANonce from message 1 of 4-Way Handshake "
1375 "differs from 3 of 4-Way Handshake - drop packet (src="
1376 MACSTR ")", MAC2STR(sm->bssid));
1377 goto failed;
1378 }
1379
1380 keylen = WPA_GET_BE16(key->key_length);
1381 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1382 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1383 "WPA: Invalid %s key length %d (src=" MACSTR
1384 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1385 MAC2STR(sm->bssid));
1386 goto failed;
1387 }
1388
1389 #ifdef CONFIG_P2P
1390 if (ie.ip_addr_alloc) {
1391 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1392 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1393 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1394 }
1395 #endif /* CONFIG_P2P */
1396
1397 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
1398 &sm->ptk) < 0) {
1399 goto failed;
1400 }
1401
1402 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1403 * for the next 4-Way Handshake. If msg 3 is received again, the old
1404 * SNonce will still be used to avoid changing PTK. */
1405 sm->renew_snonce = 1;
1406
1407 if (key_info & WPA_KEY_INFO_INSTALL) {
1408 if (wpa_supplicant_install_ptk(sm, key))
1409 goto failed;
1410 }
1411
1412 if (key_info & WPA_KEY_INFO_SECURE) {
1413 wpa_sm_mlme_setprotection(
1414 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1415 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1416 eapol_sm_notify_portValid(sm->eapol, TRUE);
1417 }
1418 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1419
1420 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1421 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1422 key_info & WPA_KEY_INFO_SECURE);
1423 } else if (ie.gtk &&
1424 wpa_supplicant_pairwise_gtk(sm, key,
1425 ie.gtk, ie.gtk_len, key_info) < 0) {
1426 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1427 "RSN: Failed to configure GTK");
1428 goto failed;
1429 }
1430
1431 if (ieee80211w_set_keys(sm, &ie) < 0) {
1432 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1433 "RSN: Failed to configure IGTK");
1434 goto failed;
1435 }
1436
1437 if (ie.gtk)
1438 wpa_sm_set_rekey_offload(sm);
1439
1440 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1441 struct rsn_pmksa_cache_entry *sa;
1442
1443 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
1444 sm->ptk.kck, sm->ptk.kck_len,
1445 sm->bssid, sm->own_addr,
1446 sm->network_ctx, sm->key_mgmt, NULL);
1447 if (!sm->cur_pmksa)
1448 sm->cur_pmksa = sa;
1449 }
1450
1451 sm->msg_3_of_4_ok = 1;
1452 return;
1453
1454 failed:
1455 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1456 }
1457
1458
1459 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1460 const u8 *keydata,
1461 size_t keydatalen,
1462 u16 key_info,
1463 struct wpa_gtk_data *gd)
1464 {
1465 int maxkeylen;
1466 struct wpa_eapol_ie_parse ie;
1467
1468 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
1469 keydata, keydatalen);
1470 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1471 return -1;
1472 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1473 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1474 "WPA: GTK IE in unencrypted key data");
1475 return -1;
1476 }
1477 if (ie.gtk == NULL) {
1478 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1479 "WPA: No GTK IE in Group Key msg 1/2");
1480 return -1;
1481 }
1482 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1483
1484 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1485 gd->gtk_len, maxkeylen,
1486 &gd->key_rsc_len, &gd->alg))
1487 return -1;
1488
1489 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
1490 ie.gtk, ie.gtk_len);
1491 gd->keyidx = ie.gtk[0] & 0x3;
1492 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1493 !!(ie.gtk[0] & BIT(2)));
1494 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1495 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1496 "RSN: Too long GTK in GTK IE (len=%lu)",
1497 (unsigned long) ie.gtk_len - 2);
1498 return -1;
1499 }
1500 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1501
1502 if (ieee80211w_set_keys(sm, &ie) < 0)
1503 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1504 "RSN: Failed to configure IGTK");
1505
1506 return 0;
1507 }
1508
1509
1510 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1511 const struct wpa_eapol_key *key,
1512 const u8 *key_data,
1513 size_t key_data_len, u16 key_info,
1514 u16 ver, struct wpa_gtk_data *gd)
1515 {
1516 size_t maxkeylen;
1517 u16 gtk_len;
1518
1519 gtk_len = WPA_GET_BE16(key->key_length);
1520 maxkeylen = key_data_len;
1521 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1522 if (maxkeylen < 8) {
1523 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1524 "WPA: Too short maxkeylen (%lu)",
1525 (unsigned long) maxkeylen);
1526 return -1;
1527 }
1528 maxkeylen -= 8;
1529 }
1530
1531 if (gtk_len > maxkeylen ||
1532 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1533 gtk_len, maxkeylen,
1534 &gd->key_rsc_len, &gd->alg))
1535 return -1;
1536
1537 gd->gtk_len = gtk_len;
1538 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1539 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1540 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
1541 #ifdef CONFIG_NO_RC4
1542 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1543 "WPA: RC4 not supported in the build");
1544 return -1;
1545 #else /* CONFIG_NO_RC4 */
1546 u8 ek[32];
1547 if (key_data_len > sizeof(gd->gtk)) {
1548 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1549 "WPA: RC4 key data too long (%lu)",
1550 (unsigned long) key_data_len);
1551 return -1;
1552 }
1553 os_memcpy(ek, key->key_iv, 16);
1554 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
1555 os_memcpy(gd->gtk, key_data, key_data_len);
1556 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
1557 os_memset(ek, 0, sizeof(ek));
1558 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1559 "WPA: RC4 failed");
1560 return -1;
1561 }
1562 os_memset(ek, 0, sizeof(ek));
1563 #endif /* CONFIG_NO_RC4 */
1564 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1565 if (maxkeylen % 8) {
1566 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1567 "WPA: Unsupported AES-WRAP len %lu",
1568 (unsigned long) maxkeylen);
1569 return -1;
1570 }
1571 if (maxkeylen > sizeof(gd->gtk)) {
1572 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1573 "WPA: AES-WRAP key data "
1574 "too long (keydatalen=%lu maxkeylen=%lu)",
1575 (unsigned long) key_data_len,
1576 (unsigned long) maxkeylen);
1577 return -1;
1578 }
1579 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1580 key_data, gd->gtk)) {
1581 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1582 "WPA: AES unwrap failed - could not decrypt "
1583 "GTK");
1584 return -1;
1585 }
1586 } else {
1587 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1588 "WPA: Unsupported key_info type %d", ver);
1589 return -1;
1590 }
1591 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1592 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1593 return 0;
1594 }
1595
1596
1597 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1598 const struct wpa_eapol_key *key,
1599 int ver, u16 key_info)
1600 {
1601 size_t mic_len, hdrlen, rlen;
1602 struct wpa_eapol_key *reply;
1603 u8 *rbuf, *key_mic;
1604
1605 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
1606 hdrlen = sizeof(*reply) + mic_len + 2;
1607 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1608 hdrlen, &rlen, (void *) &reply);
1609 if (rbuf == NULL)
1610 return -1;
1611
1612 reply->type = (sm->proto == WPA_PROTO_RSN ||
1613 sm->proto == WPA_PROTO_OSEN) ?
1614 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1615 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1616 key_info |= ver | WPA_KEY_INFO_SECURE;
1617 if (mic_len)
1618 key_info |= WPA_KEY_INFO_MIC;
1619 else
1620 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1621 WPA_PUT_BE16(reply->key_info, key_info);
1622 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1623 WPA_PUT_BE16(reply->key_length, 0);
1624 else
1625 os_memcpy(reply->key_length, key->key_length, 2);
1626 os_memcpy(reply->replay_counter, key->replay_counter,
1627 WPA_REPLAY_COUNTER_LEN);
1628
1629 key_mic = (u8 *) (reply + 1);
1630 WPA_PUT_BE16(key_mic + mic_len, 0);
1631
1632 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1633 return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL,
1634 rbuf, rlen, key_mic);
1635 }
1636
1637
1638 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1639 const unsigned char *src_addr,
1640 const struct wpa_eapol_key *key,
1641 const u8 *key_data,
1642 size_t key_data_len, u16 ver)
1643 {
1644 u16 key_info;
1645 int rekey, ret;
1646 struct wpa_gtk_data gd;
1647 const u8 *key_rsc;
1648
1649 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
1650 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1651 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1652 goto failed;
1653 }
1654
1655 os_memset(&gd, 0, sizeof(gd));
1656
1657 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1658 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1659 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1660
1661 key_info = WPA_GET_BE16(key->key_info);
1662
1663 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
1664 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1665 key_data_len, key_info,
1666 &gd);
1667 } else {
1668 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1669 key_data_len,
1670 key_info, ver, &gd);
1671 }
1672
1673 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1674
1675 if (ret)
1676 goto failed;
1677
1678 key_rsc = key->key_rsc;
1679 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1680 key_rsc = null_rsc;
1681
1682 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
1683 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
1684 goto failed;
1685 os_memset(&gd, 0, sizeof(gd));
1686
1687 if (rekey) {
1688 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1689 "completed with " MACSTR " [GTK=%s]",
1690 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1691 wpa_sm_cancel_auth_timeout(sm);
1692 wpa_sm_set_state(sm, WPA_COMPLETED);
1693 } else {
1694 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1695 key_info &
1696 WPA_KEY_INFO_SECURE);
1697 }
1698
1699 wpa_sm_set_rekey_offload(sm);
1700
1701 return;
1702
1703 failed:
1704 os_memset(&gd, 0, sizeof(gd));
1705 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1706 }
1707
1708
1709 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1710 struct wpa_eapol_key *key,
1711 u16 ver,
1712 const u8 *buf, size_t len)
1713 {
1714 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
1715 int ok = 0;
1716 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
1717
1718 os_memcpy(mic, key + 1, mic_len);
1719 if (sm->tptk_set) {
1720 os_memset(key + 1, 0, mic_len);
1721 wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len, sm->key_mgmt,
1722 ver, buf, len, (u8 *) (key + 1));
1723 if (os_memcmp_const(mic, key + 1, mic_len) != 0) {
1724 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1725 "WPA: Invalid EAPOL-Key MIC "
1726 "when using TPTK - ignoring TPTK");
1727 } else {
1728 ok = 1;
1729 sm->tptk_set = 0;
1730 sm->ptk_set = 1;
1731 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1732 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
1733 /*
1734 * This assures the same TPTK in sm->tptk can never be
1735 * copied twice to sm->pkt as the new PTK. In
1736 * combination with the installed flag in the wpa_ptk
1737 * struct, this assures the same PTK is only installed
1738 * once.
1739 */
1740 sm->renew_snonce = 1;
1741 }
1742 }
1743
1744 if (!ok && sm->ptk_set) {
1745 os_memset(key + 1, 0, mic_len);
1746 wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len, sm->key_mgmt,
1747 ver, buf, len, (u8 *) (key + 1));
1748 if (os_memcmp_const(mic, key + 1, mic_len) != 0) {
1749 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1750 "WPA: Invalid EAPOL-Key MIC - "
1751 "dropping packet");
1752 return -1;
1753 }
1754 ok = 1;
1755 }
1756
1757 if (!ok) {
1758 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1759 "WPA: Could not verify EAPOL-Key MIC - "
1760 "dropping packet");
1761 return -1;
1762 }
1763
1764 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1765 WPA_REPLAY_COUNTER_LEN);
1766 sm->rx_replay_counter_set = 1;
1767 return 0;
1768 }
1769
1770
1771 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1772 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1773 struct wpa_eapol_key *key,
1774 size_t mic_len, u16 ver,
1775 u8 *key_data, size_t *key_data_len)
1776 {
1777 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1778 key_data, *key_data_len);
1779 if (!sm->ptk_set) {
1780 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1781 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1782 "Data");
1783 return -1;
1784 }
1785
1786 /* Decrypt key data here so that this operation does not need
1787 * to be implemented separately for each message type. */
1788 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
1789 #ifdef CONFIG_NO_RC4
1790 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1791 "WPA: RC4 not supported in the build");
1792 return -1;
1793 #else /* CONFIG_NO_RC4 */
1794 u8 ek[32];
1795
1796 wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
1797 os_memcpy(ek, key->key_iv, 16);
1798 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
1799 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
1800 os_memset(ek, 0, sizeof(ek));
1801 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1802 "WPA: RC4 failed");
1803 return -1;
1804 }
1805 os_memset(ek, 0, sizeof(ek));
1806 #endif /* CONFIG_NO_RC4 */
1807 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1808 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
1809 sm->key_mgmt == WPA_KEY_MGMT_OWE ||
1810 sm->key_mgmt == WPA_KEY_MGMT_DPP ||
1811 sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
1812 wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1813 u8 *buf;
1814
1815 wpa_printf(MSG_DEBUG,
1816 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
1817 (unsigned int) sm->ptk.kek_len);
1818 if (*key_data_len < 8 || *key_data_len % 8) {
1819 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1820 "WPA: Unsupported AES-WRAP len %u",
1821 (unsigned int) *key_data_len);
1822 return -1;
1823 }
1824 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1825 buf = os_malloc(*key_data_len);
1826 if (buf == NULL) {
1827 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1828 "WPA: No memory for AES-UNWRAP buffer");
1829 return -1;
1830 }
1831 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
1832 key_data, buf)) {
1833 bin_clear_free(buf, *key_data_len);
1834 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1835 "WPA: AES unwrap failed - "
1836 "could not decrypt EAPOL-Key key data");
1837 return -1;
1838 }
1839 os_memcpy(key_data, buf, *key_data_len);
1840 bin_clear_free(buf, *key_data_len);
1841 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
1842 } else {
1843 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1844 "WPA: Unsupported key_info type %d", ver);
1845 return -1;
1846 }
1847 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1848 key_data, *key_data_len);
1849 return 0;
1850 }
1851
1852
1853 /**
1854 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1855 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1856 */
1857 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1858 {
1859 if (sm && sm->cur_pmksa) {
1860 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1861 "RSN: Cancelling PMKSA caching attempt");
1862 sm->cur_pmksa = NULL;
1863 }
1864 }
1865
1866
1867 static void wpa_eapol_key_dump(struct wpa_sm *sm,
1868 const struct wpa_eapol_key *key,
1869 unsigned int key_data_len,
1870 const u8 *mic, unsigned int mic_len)
1871 {
1872 #ifndef CONFIG_NO_STDOUT_DEBUG
1873 u16 key_info = WPA_GET_BE16(key->key_info);
1874
1875 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1876 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1877 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1878 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1879 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1880 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1881 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1882 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1883 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1884 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1885 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1886 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1887 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1888 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1889 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1890 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1891 " key_length=%u key_data_length=%u",
1892 WPA_GET_BE16(key->key_length), key_data_len);
1893 wpa_hexdump(MSG_DEBUG, " replay_counter",
1894 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1895 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1896 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1897 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1898 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1899 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
1900 #endif /* CONFIG_NO_STDOUT_DEBUG */
1901 }
1902
1903
1904 #ifdef CONFIG_FILS
1905 static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
1906 size_t *key_data_len)
1907 {
1908 struct wpa_ptk *ptk;
1909 struct ieee802_1x_hdr *hdr;
1910 struct wpa_eapol_key *key;
1911 u8 *pos, *tmp;
1912 const u8 *aad[1];
1913 size_t aad_len[1];
1914
1915 if (*key_data_len < AES_BLOCK_SIZE) {
1916 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
1917 return -1;
1918 }
1919
1920 if (sm->tptk_set)
1921 ptk = &sm->tptk;
1922 else if (sm->ptk_set)
1923 ptk = &sm->ptk;
1924 else
1925 return -1;
1926
1927 hdr = (struct ieee802_1x_hdr *) buf;
1928 key = (struct wpa_eapol_key *) (hdr + 1);
1929 pos = (u8 *) (key + 1);
1930 pos += 2; /* Pointing at the Encrypted Key Data field */
1931
1932 tmp = os_malloc(*key_data_len);
1933 if (!tmp)
1934 return -1;
1935
1936 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1937 * to Key Data (exclusive). */
1938 aad[0] = buf;
1939 aad_len[0] = pos - buf;
1940 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
1941 1, aad, aad_len, tmp) < 0) {
1942 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
1943 bin_clear_free(tmp, *key_data_len);
1944 return -1;
1945 }
1946
1947 /* AEAD decryption and validation completed successfully */
1948 (*key_data_len) -= AES_BLOCK_SIZE;
1949 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
1950 tmp, *key_data_len);
1951
1952 /* Replace Key Data field with the decrypted version */
1953 os_memcpy(pos, tmp, *key_data_len);
1954 pos -= 2; /* Key Data Length field */
1955 WPA_PUT_BE16(pos, *key_data_len);
1956 bin_clear_free(tmp, *key_data_len);
1957
1958 if (sm->tptk_set) {
1959 sm->tptk_set = 0;
1960 sm->ptk_set = 1;
1961 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1962 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
1963 }
1964
1965 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1966 WPA_REPLAY_COUNTER_LEN);
1967 sm->rx_replay_counter_set = 1;
1968
1969 return 0;
1970 }
1971 #endif /* CONFIG_FILS */
1972
1973
1974 /**
1975 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1976 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1977 * @src_addr: Source MAC address of the EAPOL packet
1978 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1979 * @len: Length of the EAPOL frame
1980 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1981 *
1982 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1983 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1984 * only processing WPA and WPA2 EAPOL-Key frames.
1985 *
1986 * The received EAPOL-Key packets are validated and valid packets are replied
1987 * to. In addition, key material (PTK, GTK) is configured at the end of a
1988 * successful key handshake.
1989 */
1990 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1991 const u8 *buf, size_t len)
1992 {
1993 size_t plen, data_len, key_data_len;
1994 const struct ieee802_1x_hdr *hdr;
1995 struct wpa_eapol_key *key;
1996 u16 key_info, ver;
1997 u8 *tmp = NULL;
1998 int ret = -1;
1999 u8 *mic, *key_data;
2000 size_t mic_len, keyhdrlen;
2001
2002 #ifdef CONFIG_IEEE80211R
2003 sm->ft_completed = 0;
2004 #endif /* CONFIG_IEEE80211R */
2005
2006 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
2007 keyhdrlen = sizeof(*key) + mic_len + 2;
2008
2009 if (len < sizeof(*hdr) + keyhdrlen) {
2010 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2011 "WPA: EAPOL frame too short to be a WPA "
2012 "EAPOL-Key (len %lu, expecting at least %lu)",
2013 (unsigned long) len,
2014 (unsigned long) sizeof(*hdr) + keyhdrlen);
2015 return 0;
2016 }
2017
2018 hdr = (const struct ieee802_1x_hdr *) buf;
2019 plen = be_to_host16(hdr->length);
2020 data_len = plen + sizeof(*hdr);
2021 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2022 "IEEE 802.1X RX: version=%d type=%d length=%lu",
2023 hdr->version, hdr->type, (unsigned long) plen);
2024
2025 if (hdr->version < EAPOL_VERSION) {
2026 /* TODO: backwards compatibility */
2027 }
2028 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
2029 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2030 "WPA: EAPOL frame (type %u) discarded, "
2031 "not a Key frame", hdr->type);
2032 ret = 0;
2033 goto out;
2034 }
2035 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
2036 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
2037 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2038 "WPA: EAPOL frame payload size %lu "
2039 "invalid (frame size %lu)",
2040 (unsigned long) plen, (unsigned long) len);
2041 ret = 0;
2042 goto out;
2043 }
2044 if (data_len < len) {
2045 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2046 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
2047 (unsigned long) len - data_len);
2048 }
2049
2050 /*
2051 * Make a copy of the frame since we need to modify the buffer during
2052 * MAC validation and Key Data decryption.
2053 */
2054 tmp = os_memdup(buf, data_len);
2055 if (tmp == NULL)
2056 goto out;
2057 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
2058 mic = (u8 *) (key + 1);
2059 key_data = mic + mic_len + 2;
2060
2061 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
2062 {
2063 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2064 "WPA: EAPOL-Key type (%d) unknown, discarded",
2065 key->type);
2066 ret = 0;
2067 goto out;
2068 }
2069
2070 key_data_len = WPA_GET_BE16(mic + mic_len);
2071 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
2072
2073 if (key_data_len > plen - keyhdrlen) {
2074 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
2075 "frame - key_data overflow (%u > %u)",
2076 (unsigned int) key_data_len,
2077 (unsigned int) (plen - keyhdrlen));
2078 goto out;
2079 }
2080
2081 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
2082 key_info = WPA_GET_BE16(key->key_info);
2083 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
2084 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
2085 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
2086 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2087 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
2088 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
2089 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
2090 !wpa_key_mgmt_fils(sm->key_mgmt) &&
2091 sm->key_mgmt != WPA_KEY_MGMT_OWE &&
2092 sm->key_mgmt != WPA_KEY_MGMT_DPP &&
2093 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
2094 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2095 "WPA: Unsupported EAPOL-Key descriptor version %d",
2096 ver);
2097 goto out;
2098 }
2099
2100 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
2101 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
2102 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2103 "OSEN: Unsupported EAPOL-Key descriptor version %d",
2104 ver);
2105 goto out;
2106 }
2107
2108 if ((wpa_key_mgmt_suite_b(sm->key_mgmt) ||
2109 wpa_key_mgmt_fils(sm->key_mgmt) ||
2110 sm->key_mgmt == WPA_KEY_MGMT_DPP ||
2111 sm->key_mgmt == WPA_KEY_MGMT_OWE) &&
2112 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
2113 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2114 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
2115 ver);
2116 goto out;
2117 }
2118
2119 #ifdef CONFIG_IEEE80211R
2120 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
2121 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
2122 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
2123 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2124 "FT: AP did not use AES-128-CMAC");
2125 goto out;
2126 }
2127 } else
2128 #endif /* CONFIG_IEEE80211R */
2129 #ifdef CONFIG_IEEE80211W
2130 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
2131 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2132 sm->key_mgmt != WPA_KEY_MGMT_OSEN &&
2133 !wpa_key_mgmt_fils(sm->key_mgmt) &&
2134 !wpa_key_mgmt_suite_b(sm->key_mgmt)) {
2135 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2136 "WPA: AP did not use the "
2137 "negotiated AES-128-CMAC");
2138 goto out;
2139 }
2140 } else
2141 #endif /* CONFIG_IEEE80211W */
2142 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
2143 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
2144 !wpa_key_mgmt_fils(sm->key_mgmt) &&
2145 sm->key_mgmt != WPA_KEY_MGMT_OWE &&
2146 sm->key_mgmt != WPA_KEY_MGMT_DPP &&
2147 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
2148 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2149 "WPA: CCMP is used, but EAPOL-Key "
2150 "descriptor version (%d) is not 2", ver);
2151 if (sm->group_cipher != WPA_CIPHER_CCMP &&
2152 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
2153 /* Earlier versions of IEEE 802.11i did not explicitly
2154 * require version 2 descriptor for all EAPOL-Key
2155 * packets, so allow group keys to use version 1 if
2156 * CCMP is not used for them. */
2157 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2158 "WPA: Backwards compatibility: allow invalid "
2159 "version for non-CCMP group keys");
2160 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
2161 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2162 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
2163 } else
2164 goto out;
2165 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
2166 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
2167 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
2168 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2169 "WPA: GCMP is used, but EAPOL-Key "
2170 "descriptor version (%d) is not 2", ver);
2171 goto out;
2172 }
2173
2174 if (sm->rx_replay_counter_set &&
2175 os_memcmp(key->replay_counter, sm->rx_replay_counter,
2176 WPA_REPLAY_COUNTER_LEN) <= 0) {
2177 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2178 "WPA: EAPOL-Key Replay Counter did not increase - "
2179 "dropping packet");
2180 goto out;
2181 }
2182
2183 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2184 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2185 "WPA: Unsupported SMK bit in key_info");
2186 goto out;
2187 }
2188
2189 if (!(key_info & WPA_KEY_INFO_ACK)) {
2190 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2191 "WPA: No Ack bit in key_info");
2192 goto out;
2193 }
2194
2195 if (key_info & WPA_KEY_INFO_REQUEST) {
2196 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2197 "WPA: EAPOL-Key with Request bit - dropped");
2198 goto out;
2199 }
2200
2201 if ((key_info & WPA_KEY_INFO_MIC) &&
2202 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
2203 goto out;
2204
2205 #ifdef CONFIG_FILS
2206 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2207 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
2208 goto out;
2209 }
2210 #endif /* CONFIG_FILS */
2211
2212 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
2213 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
2214 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
2215 ver, key_data,
2216 &key_data_len))
2217 goto out;
2218 }
2219
2220 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2221 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2222 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2223 "WPA: Ignored EAPOL-Key (Pairwise) with "
2224 "non-zero key index");
2225 goto out;
2226 }
2227 if (key_info & (WPA_KEY_INFO_MIC |
2228 WPA_KEY_INFO_ENCR_KEY_DATA)) {
2229 /* 3/4 4-Way Handshake */
2230 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2231 key_data_len);
2232 } else {
2233 /* 1/4 4-Way Handshake */
2234 wpa_supplicant_process_1_of_4(sm, src_addr, key,
2235 ver, key_data,
2236 key_data_len);
2237 }
2238 } else {
2239 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
2240 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
2241 /* 1/2 Group Key Handshake */
2242 wpa_supplicant_process_1_of_2(sm, src_addr, key,
2243 key_data, key_data_len,
2244 ver);
2245 } else {
2246 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2247 "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
2248 "dropped");
2249 }
2250 }
2251
2252 ret = 1;
2253
2254 out:
2255 bin_clear_free(tmp, data_len);
2256 return ret;
2257 }
2258
2259
2260 #ifdef CONFIG_CTRL_IFACE
2261 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2262 {
2263 switch (sm->key_mgmt) {
2264 case WPA_KEY_MGMT_IEEE8021X:
2265 return ((sm->proto == WPA_PROTO_RSN ||
2266 sm->proto == WPA_PROTO_OSEN) ?
2267 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2268 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2269 case WPA_KEY_MGMT_PSK:
2270 return (sm->proto == WPA_PROTO_RSN ?
2271 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2272 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2273 #ifdef CONFIG_IEEE80211R
2274 case WPA_KEY_MGMT_FT_IEEE8021X:
2275 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2276 case WPA_KEY_MGMT_FT_PSK:
2277 return RSN_AUTH_KEY_MGMT_FT_PSK;
2278 #endif /* CONFIG_IEEE80211R */
2279 #ifdef CONFIG_IEEE80211W
2280 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2281 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2282 case WPA_KEY_MGMT_PSK_SHA256:
2283 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
2284 #endif /* CONFIG_IEEE80211W */
2285 case WPA_KEY_MGMT_CCKM:
2286 return (sm->proto == WPA_PROTO_RSN ?
2287 RSN_AUTH_KEY_MGMT_CCKM:
2288 WPA_AUTH_KEY_MGMT_CCKM);
2289 case WPA_KEY_MGMT_WPA_NONE:
2290 return WPA_AUTH_KEY_MGMT_NONE;
2291 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2292 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
2293 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2294 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
2295 default:
2296 return 0;
2297 }
2298 }
2299
2300
2301 #define RSN_SUITE "%02x-%02x-%02x-%d"
2302 #define RSN_SUITE_ARG(s) \
2303 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2304
2305 /**
2306 * wpa_sm_get_mib - Dump text list of MIB entries
2307 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2308 * @buf: Buffer for the list
2309 * @buflen: Length of the buffer
2310 * Returns: Number of bytes written to buffer
2311 *
2312 * This function is used fetch dot11 MIB variables.
2313 */
2314 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2315 {
2316 char pmkid_txt[PMKID_LEN * 2 + 1];
2317 int rsna, ret;
2318 size_t len;
2319
2320 if (sm->cur_pmksa) {
2321 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2322 sm->cur_pmksa->pmkid, PMKID_LEN);
2323 } else
2324 pmkid_txt[0] = '\0';
2325
2326 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2327 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2328 sm->proto == WPA_PROTO_RSN)
2329 rsna = 1;
2330 else
2331 rsna = 0;
2332
2333 ret = os_snprintf(buf, buflen,
2334 "dot11RSNAOptionImplemented=TRUE\n"
2335 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2336 "dot11RSNAEnabled=%s\n"
2337 "dot11RSNAPreauthenticationEnabled=%s\n"
2338 "dot11RSNAConfigVersion=%d\n"
2339 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2340 "dot11RSNAConfigGroupCipherSize=%d\n"
2341 "dot11RSNAConfigPMKLifetime=%d\n"
2342 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2343 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2344 "dot11RSNAConfigSATimeout=%d\n",
2345 rsna ? "TRUE" : "FALSE",
2346 rsna ? "TRUE" : "FALSE",
2347 RSN_VERSION,
2348 wpa_cipher_key_len(sm->group_cipher) * 8,
2349 sm->dot11RSNAConfigPMKLifetime,
2350 sm->dot11RSNAConfigPMKReauthThreshold,
2351 sm->dot11RSNAConfigSATimeout);
2352 if (os_snprintf_error(buflen, ret))
2353 return 0;
2354 len = ret;
2355
2356 ret = os_snprintf(
2357 buf + len, buflen - len,
2358 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2359 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2360 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2361 "dot11RSNAPMKIDUsed=%s\n"
2362 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2363 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2364 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2365 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2366 "dot11RSNA4WayHandshakeFailures=%u\n",
2367 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2368 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2369 sm->pairwise_cipher)),
2370 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2371 sm->group_cipher)),
2372 pmkid_txt,
2373 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2374 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2375 sm->pairwise_cipher)),
2376 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2377 sm->group_cipher)),
2378 sm->dot11RSNA4WayHandshakeFailures);
2379 if (!os_snprintf_error(buflen - len, ret))
2380 len += ret;
2381
2382 return (int) len;
2383 }
2384 #endif /* CONFIG_CTRL_IFACE */
2385
2386
2387 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
2388 void *ctx, enum pmksa_free_reason reason)
2389 {
2390 struct wpa_sm *sm = ctx;
2391 int deauth = 0;
2392
2393 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2394 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2395
2396 if (sm->cur_pmksa == entry) {
2397 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2398 "RSN: %s current PMKSA entry",
2399 reason == PMKSA_REPLACE ? "replaced" : "removed");
2400 pmksa_cache_clear_current(sm);
2401
2402 /*
2403 * If an entry is simply being replaced, there's no need to
2404 * deauthenticate because it will be immediately re-added.
2405 * This happens when EAP authentication is completed again
2406 * (reauth or failed PMKSA caching attempt).
2407 */
2408 if (reason != PMKSA_REPLACE)
2409 deauth = 1;
2410 }
2411
2412 if (reason == PMKSA_EXPIRE &&
2413 (sm->pmk_len == entry->pmk_len &&
2414 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2415 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2416 "RSN: deauthenticating due to expired PMK");
2417 pmksa_cache_clear_current(sm);
2418 deauth = 1;
2419 }
2420
2421 if (deauth) {
2422 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2423 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2424 }
2425 }
2426
2427
2428 /**
2429 * wpa_sm_init - Initialize WPA state machine
2430 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2431 * Returns: Pointer to the allocated WPA state machine data
2432 *
2433 * This function is used to allocate a new WPA state machine and the returned
2434 * value is passed to all WPA state machine calls.
2435 */
2436 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2437 {
2438 struct wpa_sm *sm;
2439
2440 sm = os_zalloc(sizeof(*sm));
2441 if (sm == NULL)
2442 return NULL;
2443 dl_list_init(&sm->pmksa_candidates);
2444 sm->renew_snonce = 1;
2445 sm->ctx = ctx;
2446
2447 sm->dot11RSNAConfigPMKLifetime = 43200;
2448 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2449 sm->dot11RSNAConfigSATimeout = 60;
2450
2451 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2452 if (sm->pmksa == NULL) {
2453 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2454 "RSN: PMKSA cache initialization failed");
2455 os_free(sm);
2456 return NULL;
2457 }
2458
2459 return sm;
2460 }
2461
2462
2463 /**
2464 * wpa_sm_deinit - Deinitialize WPA state machine
2465 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2466 */
2467 void wpa_sm_deinit(struct wpa_sm *sm)
2468 {
2469 if (sm == NULL)
2470 return;
2471 pmksa_cache_deinit(sm->pmksa);
2472 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2473 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2474 os_free(sm->assoc_wpa_ie);
2475 os_free(sm->ap_wpa_ie);
2476 os_free(sm->ap_rsn_ie);
2477 wpa_sm_drop_sa(sm);
2478 os_free(sm->ctx);
2479 #ifdef CONFIG_IEEE80211R
2480 os_free(sm->assoc_resp_ies);
2481 #endif /* CONFIG_IEEE80211R */
2482 #ifdef CONFIG_TESTING_OPTIONS
2483 wpabuf_free(sm->test_assoc_ie);
2484 #endif /* CONFIG_TESTING_OPTIONS */
2485 #ifdef CONFIG_FILS_SK_PFS
2486 crypto_ecdh_deinit(sm->fils_ecdh);
2487 #endif /* CONFIG_FILS_SK_PFS */
2488 #ifdef CONFIG_FILS
2489 wpabuf_free(sm->fils_ft_ies);
2490 #endif /* CONFIG_FILS */
2491 #ifdef CONFIG_OWE
2492 crypto_ecdh_deinit(sm->owe_ecdh);
2493 #endif /* CONFIG_OWE */
2494 os_free(sm);
2495 }
2496
2497
2498 /**
2499 * wpa_sm_notify_assoc - Notify WPA state machine about association
2500 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2501 * @bssid: The BSSID of the new association
2502 *
2503 * This function is called to let WPA state machine know that the connection
2504 * was established.
2505 */
2506 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2507 {
2508 int clear_keys = 1;
2509
2510 if (sm == NULL)
2511 return;
2512
2513 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2514 "WPA: Association event - clear replay counter");
2515 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2516 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2517 sm->rx_replay_counter_set = 0;
2518 sm->renew_snonce = 1;
2519 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2520 rsn_preauth_deinit(sm);
2521
2522 #ifdef CONFIG_IEEE80211R
2523 if (wpa_ft_is_completed(sm)) {
2524 /*
2525 * Clear portValid to kick EAPOL state machine to re-enter
2526 * AUTHENTICATED state to get the EAPOL port Authorized.
2527 */
2528 eapol_sm_notify_portValid(sm->eapol, FALSE);
2529 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2530
2531 /* Prepare for the next transition */
2532 wpa_ft_prepare_auth_request(sm, NULL);
2533
2534 clear_keys = 0;
2535 }
2536 #endif /* CONFIG_IEEE80211R */
2537 #ifdef CONFIG_FILS
2538 if (sm->fils_completed) {
2539 /*
2540 * Clear portValid to kick EAPOL state machine to re-enter
2541 * AUTHENTICATED state to get the EAPOL port Authorized.
2542 */
2543 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2544 clear_keys = 0;
2545 }
2546 #endif /* CONFIG_FILS */
2547
2548 if (clear_keys) {
2549 /*
2550 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2551 * this is not part of a Fast BSS Transition.
2552 */
2553 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2554 sm->ptk_set = 0;
2555 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2556 sm->tptk_set = 0;
2557 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2558 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
2559 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
2560 #ifdef CONFIG_IEEE80211W
2561 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
2562 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
2563 #endif /* CONFIG_IEEE80211W */
2564 }
2565
2566 #ifdef CONFIG_TDLS
2567 wpa_tdls_assoc(sm);
2568 #endif /* CONFIG_TDLS */
2569
2570 #ifdef CONFIG_P2P
2571 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2572 #endif /* CONFIG_P2P */
2573 }
2574
2575
2576 /**
2577 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2578 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2579 *
2580 * This function is called to let WPA state machine know that the connection
2581 * was lost. This will abort any existing pre-authentication session.
2582 */
2583 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2584 {
2585 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2586 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2587 rsn_preauth_deinit(sm);
2588 pmksa_cache_clear_current(sm);
2589 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2590 sm->dot11RSNA4WayHandshakeFailures++;
2591 #ifdef CONFIG_TDLS
2592 wpa_tdls_disassoc(sm);
2593 #endif /* CONFIG_TDLS */
2594 #ifdef CONFIG_FILS
2595 sm->fils_completed = 0;
2596 #endif /* CONFIG_FILS */
2597 #ifdef CONFIG_IEEE80211R
2598 sm->ft_reassoc_completed = 0;
2599 #endif /* CONFIG_IEEE80211R */
2600
2601 /* Keys are not needed in the WPA state machine anymore */
2602 wpa_sm_drop_sa(sm);
2603
2604 sm->msg_3_of_4_ok = 0;
2605 }
2606
2607
2608 /**
2609 * wpa_sm_set_pmk - Set PMK
2610 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2611 * @pmk: The new PMK
2612 * @pmk_len: The length of the new PMK in bytes
2613 * @pmkid: Calculated PMKID
2614 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
2615 *
2616 * Configure the PMK for WPA state machine.
2617 */
2618 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
2619 const u8 *pmkid, const u8 *bssid)
2620 {
2621 if (sm == NULL)
2622 return;
2623
2624 wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data",
2625 pmk, pmk_len);
2626 sm->pmk_len = pmk_len;
2627 os_memcpy(sm->pmk, pmk, pmk_len);
2628
2629 #ifdef CONFIG_IEEE80211R
2630 /* Set XXKey to be PSK for FT key derivation */
2631 sm->xxkey_len = pmk_len;
2632 os_memcpy(sm->xxkey, pmk, pmk_len);
2633 #endif /* CONFIG_IEEE80211R */
2634
2635 if (bssid) {
2636 pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
2637 bssid, sm->own_addr,
2638 sm->network_ctx, sm->key_mgmt, NULL);
2639 }
2640 }
2641
2642
2643 /**
2644 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2645 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2646 *
2647 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2648 * will be cleared.
2649 */
2650 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2651 {
2652 if (sm == NULL)
2653 return;
2654
2655 if (sm->cur_pmksa) {
2656 wpa_hexdump_key(MSG_DEBUG,
2657 "WPA: Set PMK based on current PMKSA",
2658 sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len);
2659 sm->pmk_len = sm->cur_pmksa->pmk_len;
2660 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2661 } else {
2662 wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK");
2663 sm->pmk_len = 0;
2664 os_memset(sm->pmk, 0, PMK_LEN_MAX);
2665 }
2666 }
2667
2668
2669 /**
2670 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2671 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2672 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2673 */
2674 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2675 {
2676 if (sm)
2677 sm->fast_reauth = fast_reauth;
2678 }
2679
2680
2681 /**
2682 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2683 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2684 * @scard_ctx: Context pointer for smartcard related callback functions
2685 */
2686 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2687 {
2688 if (sm == NULL)
2689 return;
2690 sm->scard_ctx = scard_ctx;
2691 if (sm->preauth_eapol)
2692 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2693 }
2694
2695
2696 /**
2697 * wpa_sm_set_config - Notification of current configration change
2698 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2699 * @config: Pointer to current network configuration
2700 *
2701 * Notify WPA state machine that configuration has changed. config will be
2702 * stored as a backpointer to network configuration. This can be %NULL to clear
2703 * the stored pointed.
2704 */
2705 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2706 {
2707 if (!sm)
2708 return;
2709
2710 if (config) {
2711 sm->network_ctx = config->network_ctx;
2712 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2713 sm->proactive_key_caching = config->proactive_key_caching;
2714 sm->eap_workaround = config->eap_workaround;
2715 sm->eap_conf_ctx = config->eap_conf_ctx;
2716 if (config->ssid) {
2717 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2718 sm->ssid_len = config->ssid_len;
2719 } else
2720 sm->ssid_len = 0;
2721 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
2722 sm->p2p = config->p2p;
2723 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
2724 #ifdef CONFIG_FILS
2725 if (config->fils_cache_id) {
2726 sm->fils_cache_id_set = 1;
2727 os_memcpy(sm->fils_cache_id, config->fils_cache_id,
2728 FILS_CACHE_ID_LEN);
2729 } else {
2730 sm->fils_cache_id_set = 0;
2731 }
2732 #endif /* CONFIG_FILS */
2733 } else {
2734 sm->network_ctx = NULL;
2735 sm->allowed_pairwise_cipher = 0;
2736 sm->proactive_key_caching = 0;
2737 sm->eap_workaround = 0;
2738 sm->eap_conf_ctx = NULL;
2739 sm->ssid_len = 0;
2740 sm->wpa_ptk_rekey = 0;
2741 sm->p2p = 0;
2742 sm->wpa_rsc_relaxation = 0;
2743 }
2744 }
2745
2746
2747 /**
2748 * wpa_sm_set_own_addr - Set own MAC address
2749 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2750 * @addr: Own MAC address
2751 */
2752 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2753 {
2754 if (sm)
2755 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2756 }
2757
2758
2759 /**
2760 * wpa_sm_set_ifname - Set network interface name
2761 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2762 * @ifname: Interface name
2763 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2764 */
2765 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2766 const char *bridge_ifname)
2767 {
2768 if (sm) {
2769 sm->ifname = ifname;
2770 sm->bridge_ifname = bridge_ifname;
2771 }
2772 }
2773
2774
2775 /**
2776 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2777 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2778 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2779 */
2780 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2781 {
2782 if (sm)
2783 sm->eapol = eapol;
2784 }
2785
2786
2787 /**
2788 * wpa_sm_set_param - Set WPA state machine parameters
2789 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2790 * @param: Parameter field
2791 * @value: Parameter value
2792 * Returns: 0 on success, -1 on failure
2793 */
2794 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2795 unsigned int value)
2796 {
2797 int ret = 0;
2798
2799 if (sm == NULL)
2800 return -1;
2801
2802 switch (param) {
2803 case RSNA_PMK_LIFETIME:
2804 if (value > 0)
2805 sm->dot11RSNAConfigPMKLifetime = value;
2806 else
2807 ret = -1;
2808 break;
2809 case RSNA_PMK_REAUTH_THRESHOLD:
2810 if (value > 0 && value <= 100)
2811 sm->dot11RSNAConfigPMKReauthThreshold = value;
2812 else
2813 ret = -1;
2814 break;
2815 case RSNA_SA_TIMEOUT:
2816 if (value > 0)
2817 sm->dot11RSNAConfigSATimeout = value;
2818 else
2819 ret = -1;
2820 break;
2821 case WPA_PARAM_PROTO:
2822 sm->proto = value;
2823 break;
2824 case WPA_PARAM_PAIRWISE:
2825 sm->pairwise_cipher = value;
2826 break;
2827 case WPA_PARAM_GROUP:
2828 sm->group_cipher = value;
2829 break;
2830 case WPA_PARAM_KEY_MGMT:
2831 sm->key_mgmt = value;
2832 break;
2833 #ifdef CONFIG_IEEE80211W
2834 case WPA_PARAM_MGMT_GROUP:
2835 sm->mgmt_group_cipher = value;
2836 break;
2837 #endif /* CONFIG_IEEE80211W */
2838 case WPA_PARAM_RSN_ENABLED:
2839 sm->rsn_enabled = value;
2840 break;
2841 case WPA_PARAM_MFP:
2842 sm->mfp = value;
2843 break;
2844 default:
2845 break;
2846 }
2847
2848 return ret;
2849 }
2850
2851
2852 /**
2853 * wpa_sm_get_status - Get WPA state machine
2854 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2855 * @buf: Buffer for status information
2856 * @buflen: Maximum buffer length
2857 * @verbose: Whether to include verbose status information
2858 * Returns: Number of bytes written to buf.
2859 *
2860 * Query WPA state machine for status information. This function fills in
2861 * a text area with current status information. If the buffer (buf) is not
2862 * large enough, status information will be truncated to fit the buffer.
2863 */
2864 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2865 int verbose)
2866 {
2867 char *pos = buf, *end = buf + buflen;
2868 int ret;
2869
2870 ret = os_snprintf(pos, end - pos,
2871 "pairwise_cipher=%s\n"
2872 "group_cipher=%s\n"
2873 "key_mgmt=%s\n",
2874 wpa_cipher_txt(sm->pairwise_cipher),
2875 wpa_cipher_txt(sm->group_cipher),
2876 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2877 if (os_snprintf_error(end - pos, ret))
2878 return pos - buf;
2879 pos += ret;
2880
2881 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2882 struct wpa_ie_data rsn;
2883 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2884 >= 0 &&
2885 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2886 WPA_CAPABILITY_MFPC)) {
2887 ret = os_snprintf(pos, end - pos, "pmf=%d\n"
2888 "mgmt_group_cipher=%s\n",
2889 (rsn.capabilities &
2890 WPA_CAPABILITY_MFPR) ? 2 : 1,
2891 wpa_cipher_txt(
2892 sm->mgmt_group_cipher));
2893 if (os_snprintf_error(end - pos, ret))
2894 return pos - buf;
2895 pos += ret;
2896 }
2897 }
2898
2899 return pos - buf;
2900 }
2901
2902
2903 int wpa_sm_pmf_enabled(struct wpa_sm *sm)
2904 {
2905 struct wpa_ie_data rsn;
2906
2907 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
2908 return 0;
2909
2910 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
2911 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
2912 return 1;
2913
2914 return 0;
2915 }
2916
2917
2918 /**
2919 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2920 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2921 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2922 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2923 * Returns: 0 on success, -1 on failure
2924 */
2925 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2926 size_t *wpa_ie_len)
2927 {
2928 int res;
2929
2930 if (sm == NULL)
2931 return -1;
2932
2933 #ifdef CONFIG_TESTING_OPTIONS
2934 if (sm->test_assoc_ie) {
2935 wpa_printf(MSG_DEBUG,
2936 "TESTING: Replace association WPA/RSN IE");
2937 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
2938 return -1;
2939 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
2940 wpabuf_len(sm->test_assoc_ie));
2941 res = wpabuf_len(sm->test_assoc_ie);
2942 } else
2943 #endif /* CONFIG_TESTING_OPTIONS */
2944 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2945 if (res < 0)
2946 return -1;
2947 *wpa_ie_len = res;
2948
2949 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2950 wpa_ie, *wpa_ie_len);
2951
2952 if (sm->assoc_wpa_ie == NULL) {
2953 /*
2954 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2955 * the correct version of the IE even if PMKSA caching is
2956 * aborted (which would remove PMKID from IE generation).
2957 */
2958 sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
2959 if (sm->assoc_wpa_ie == NULL)
2960 return -1;
2961
2962 sm->assoc_wpa_ie_len = *wpa_ie_len;
2963 } else {
2964 wpa_hexdump(MSG_DEBUG,
2965 "WPA: Leave previously set WPA IE default",
2966 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
2967 }
2968
2969 return 0;
2970 }
2971
2972
2973 /**
2974 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2975 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2976 * @ie: Pointer to IE data (starting from id)
2977 * @len: IE length
2978 * Returns: 0 on success, -1 on failure
2979 *
2980 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2981 * Request frame. The IE will be used to override the default value generated
2982 * with wpa_sm_set_assoc_wpa_ie_default().
2983 */
2984 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2985 {
2986 if (sm == NULL)
2987 return -1;
2988
2989 os_free(sm->assoc_wpa_ie);
2990 if (ie == NULL || len == 0) {
2991 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2992 "WPA: clearing own WPA/RSN IE");
2993 sm->assoc_wpa_ie = NULL;
2994 sm->assoc_wpa_ie_len = 0;
2995 } else {
2996 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2997 sm->assoc_wpa_ie = os_memdup(ie, len);
2998 if (sm->assoc_wpa_ie == NULL)
2999 return -1;
3000
3001 sm->assoc_wpa_ie_len = len;
3002 }
3003
3004 return 0;
3005 }
3006
3007
3008 /**
3009 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
3010 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3011 * @ie: Pointer to IE data (starting from id)
3012 * @len: IE length
3013 * Returns: 0 on success, -1 on failure
3014 *
3015 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
3016 * frame.
3017 */
3018 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3019 {
3020 if (sm == NULL)
3021 return -1;
3022
3023 os_free(sm->ap_wpa_ie);
3024 if (ie == NULL || len == 0) {
3025 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3026 "WPA: clearing AP WPA IE");
3027 sm->ap_wpa_ie = NULL;
3028 sm->ap_wpa_ie_len = 0;
3029 } else {
3030 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
3031 sm->ap_wpa_ie = os_memdup(ie, len);
3032 if (sm->ap_wpa_ie == NULL)
3033 return -1;
3034
3035 sm->ap_wpa_ie_len = len;
3036 }
3037
3038 return 0;
3039 }
3040
3041
3042 /**
3043 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
3044 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3045 * @ie: Pointer to IE data (starting from id)
3046 * @len: IE length
3047 * Returns: 0 on success, -1 on failure
3048 *
3049 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
3050 * frame.
3051 */
3052 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3053 {
3054 if (sm == NULL)
3055 return -1;
3056
3057 os_free(sm->ap_rsn_ie);
3058 if (ie == NULL || len == 0) {
3059 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3060 "WPA: clearing AP RSN IE");
3061 sm->ap_rsn_ie = NULL;
3062 sm->ap_rsn_ie_len = 0;
3063 } else {
3064 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
3065 sm->ap_rsn_ie = os_memdup(ie, len);
3066 if (sm->ap_rsn_ie == NULL)
3067 return -1;
3068
3069 sm->ap_rsn_ie_len = len;
3070 }
3071
3072 return 0;
3073 }
3074
3075
3076 /**
3077 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
3078 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3079 * @data: Pointer to data area for parsing results
3080 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
3081 *
3082 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
3083 * parsed data into data.
3084 */
3085 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
3086 {
3087 if (sm == NULL)
3088 return -1;
3089
3090 if (sm->assoc_wpa_ie == NULL) {
3091 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3092 "WPA: No WPA/RSN IE available from association info");
3093 return -1;
3094 }
3095 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
3096 return -2;
3097 return 0;
3098 }
3099
3100
3101 int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
3102 {
3103 return pmksa_cache_list(sm->pmksa, buf, len);
3104 }
3105
3106
3107 struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
3108 {
3109 return pmksa_cache_head(sm->pmksa);
3110 }
3111
3112
3113 struct rsn_pmksa_cache_entry *
3114 wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
3115 struct rsn_pmksa_cache_entry * entry)
3116 {
3117 return pmksa_cache_add_entry(sm->pmksa, entry);
3118 }
3119
3120
3121 void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
3122 const u8 *pmkid, const u8 *bssid,
3123 const u8 *fils_cache_id)
3124 {
3125 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
3126 bssid, sm->own_addr, sm->network_ctx,
3127 sm->key_mgmt, fils_cache_id);
3128 }
3129
3130
3131 int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid,
3132 const void *network_ctx)
3133 {
3134 return pmksa_cache_get(sm->pmksa, bssid, NULL, network_ctx) != NULL;
3135 }
3136
3137
3138 void wpa_sm_drop_sa(struct wpa_sm *sm)
3139 {
3140 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
3141 sm->ptk_set = 0;
3142 sm->tptk_set = 0;
3143 os_memset(sm->pmk, 0, sizeof(sm->pmk));
3144 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
3145 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
3146 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
3147 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
3148 #ifdef CONFIG_IEEE80211W
3149 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
3150 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
3151 #endif /* CONFIG_IEEE80211W */
3152 #ifdef CONFIG_IEEE80211R
3153 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
3154 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
3155 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
3156 #endif /* CONFIG_IEEE80211R */
3157 }
3158
3159
3160 int wpa_sm_has_ptk(struct wpa_sm *sm)
3161 {
3162 if (sm == NULL)
3163 return 0;
3164 return sm->ptk_set;
3165 }
3166
3167
3168 void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
3169 {
3170 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
3171 }
3172
3173
3174 void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
3175 {
3176 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
3177 }
3178
3179
3180 #ifdef CONFIG_WNM
3181 int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
3182 {
3183 u16 keyinfo;
3184 u8 keylen; /* plaintext key len */
3185 u8 *key_rsc;
3186
3187 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
3188 struct wpa_gtk_data gd;
3189
3190 os_memset(&gd, 0, sizeof(gd));
3191 keylen = wpa_cipher_key_len(sm->group_cipher);
3192 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
3193 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
3194 if (gd.alg == WPA_ALG_NONE) {
3195 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
3196 return -1;
3197 }
3198
3199 key_rsc = buf + 5;
3200 keyinfo = WPA_GET_LE16(buf + 2);
3201 gd.gtk_len = keylen;
3202 if (gd.gtk_len != buf[4]) {
3203 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
3204 gd.gtk_len, buf[4]);
3205 return -1;
3206 }
3207 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
3208 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
3209 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
3210
3211 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
3212
3213 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
3214 gd.gtk, gd.gtk_len);
3215 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
3216 os_memset(&gd, 0, sizeof(gd));
3217 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
3218 "WNM mode");
3219 return -1;
3220 }
3221 os_memset(&gd, 0, sizeof(gd));
3222 #ifdef CONFIG_IEEE80211W
3223 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
3224 const struct wpa_igtk_kde *igtk;
3225
3226 igtk = (const struct wpa_igtk_kde *) (buf + 2);
3227 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
3228 return -1;
3229 #endif /* CONFIG_IEEE80211W */
3230 } else {
3231 wpa_printf(MSG_DEBUG, "Unknown element id");
3232 return -1;
3233 }
3234
3235 return 0;
3236 }
3237 #endif /* CONFIG_WNM */
3238
3239
3240 #ifdef CONFIG_P2P
3241
3242 int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
3243 {
3244 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
3245 return -1;
3246 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
3247 return 0;
3248 }
3249
3250 #endif /* CONFIG_P2P */
3251
3252
3253 void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
3254 {
3255 if (rx_replay_counter == NULL)
3256 return;
3257
3258 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
3259 WPA_REPLAY_COUNTER_LEN);
3260 sm->rx_replay_counter_set = 1;
3261 wpa_printf(MSG_DEBUG, "Updated key replay counter");
3262 }
3263
3264
3265 void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
3266 const u8 *ptk_kck, size_t ptk_kck_len,
3267 const u8 *ptk_kek, size_t ptk_kek_len)
3268 {
3269 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
3270 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
3271 sm->ptk.kck_len = ptk_kck_len;
3272 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
3273 }
3274 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
3275 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
3276 sm->ptk.kek_len = ptk_kek_len;
3277 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
3278 }
3279 sm->ptk_set = 1;
3280 }
3281
3282
3283 #ifdef CONFIG_TESTING_OPTIONS
3284 void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
3285 {
3286 wpabuf_free(sm->test_assoc_ie);
3287 sm->test_assoc_ie = buf;
3288 }
3289 #endif /* CONFIG_TESTING_OPTIONS */
3290
3291
3292 #ifdef CONFIG_FILS
3293
3294 struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
3295 {
3296 struct wpabuf *buf = NULL;
3297 struct wpabuf *erp_msg;
3298 struct wpabuf *pub = NULL;
3299
3300 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
3301 if (!erp_msg && !sm->cur_pmksa) {
3302 wpa_printf(MSG_DEBUG,
3303 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
3304 goto fail;
3305 }
3306
3307 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
3308 erp_msg != NULL, sm->cur_pmksa != NULL);
3309
3310 sm->fils_completed = 0;
3311
3312 if (!sm->assoc_wpa_ie) {
3313 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
3314 goto fail;
3315 }
3316
3317 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
3318 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
3319 goto fail;
3320
3321 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
3322 sm->fils_nonce, FILS_NONCE_LEN);
3323 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
3324 sm->fils_session, FILS_SESSION_LEN);
3325
3326 #ifdef CONFIG_FILS_SK_PFS
3327 sm->fils_dh_group = dh_group;
3328 if (dh_group) {
3329 crypto_ecdh_deinit(sm->fils_ecdh);
3330 sm->fils_ecdh = crypto_ecdh_init(dh_group);
3331 if (!sm->fils_ecdh) {
3332 wpa_printf(MSG_INFO,
3333 "FILS: Could not initialize ECDH with group %d",
3334 dh_group);
3335 goto fail;
3336 }
3337 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
3338 if (!pub)
3339 goto fail;
3340 wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
3341 pub);
3342 sm->fils_dh_elem_len = wpabuf_len(pub);
3343 }
3344 #endif /* CONFIG_FILS_SK_PFS */
3345
3346 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
3347 (pub ? wpabuf_len(pub) : 0));
3348 if (!buf)
3349 goto fail;
3350
3351 /* Fields following the Authentication algorithm number field */
3352
3353 /* Authentication Transaction seq# */
3354 wpabuf_put_le16(buf, 1);
3355
3356 /* Status Code */
3357 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
3358
3359 /* TODO: FILS PK */
3360 #ifdef CONFIG_FILS_SK_PFS
3361 if (dh_group) {
3362 /* Finite Cyclic Group */
3363 wpabuf_put_le16(buf, dh_group);
3364 /* Element */
3365 wpabuf_put_buf(buf, pub);
3366 }
3367 #endif /* CONFIG_FILS_SK_PFS */
3368
3369 /* RSNE */
3370 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
3371 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3372 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3373
3374 if (md) {
3375 /* MDE when using FILS for FT initial association */
3376 struct rsn_mdie *mdie;
3377
3378 wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
3379 wpabuf_put_u8(buf, sizeof(*mdie));
3380 mdie = wpabuf_put(buf, sizeof(*mdie));
3381 os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
3382 mdie->ft_capab = 0;
3383 }
3384
3385 /* FILS Nonce */
3386 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3387 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
3388 /* Element ID Extension */
3389 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
3390 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
3391
3392 /* FILS Session */
3393 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3394 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
3395 /* Element ID Extension */
3396 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
3397 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
3398
3399 /* FILS Wrapped Data */
3400 sm->fils_erp_pmkid_set = 0;
3401 if (erp_msg) {
3402 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3403 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
3404 /* Element ID Extension */
3405 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_WRAPPED_DATA);
3406 wpabuf_put_buf(buf, erp_msg);
3407 /* Calculate pending PMKID here so that we do not need to
3408 * maintain a copy of the EAP-Initiate/Reauth message. */
3409 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
3410 wpabuf_len(erp_msg),
3411 sm->fils_erp_pmkid) == 0)
3412 sm->fils_erp_pmkid_set = 1;
3413 }
3414
3415 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
3416 buf);
3417
3418 fail:
3419 wpabuf_free(erp_msg);
3420 wpabuf_free(pub);
3421 return buf;
3422 }
3423
3424
3425 int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
3426 size_t len)
3427 {
3428 const u8 *pos, *end;
3429 struct ieee802_11_elems elems;
3430 struct wpa_ie_data rsn;
3431 int pmkid_match = 0;
3432 u8 ick[FILS_ICK_MAX_LEN];
3433 size_t ick_len;
3434 int res;
3435 struct wpabuf *dh_ss = NULL;
3436 const u8 *g_sta = NULL;
3437 size_t g_sta_len = 0;
3438 const u8 *g_ap = NULL;
3439 size_t g_ap_len = 0;
3440 struct wpabuf *pub = NULL;
3441
3442 os_memcpy(sm->bssid, bssid, ETH_ALEN);
3443
3444 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
3445 data, len);
3446 pos = data;
3447 end = data + len;
3448
3449 /* TODO: FILS PK */
3450 #ifdef CONFIG_FILS_SK_PFS
3451 if (sm->fils_dh_group) {
3452 u16 group;
3453
3454 /* Using FILS PFS */
3455
3456 /* Finite Cyclic Group */
3457 if (end - pos < 2) {
3458 wpa_printf(MSG_DEBUG,
3459 "FILS: No room for Finite Cyclic Group");
3460 goto fail;
3461 }
3462 group = WPA_GET_LE16(pos);
3463 pos += 2;
3464 if (group != sm->fils_dh_group) {
3465 wpa_printf(MSG_DEBUG,
3466 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
3467 group, sm->fils_dh_group);
3468 goto fail;
3469 }
3470
3471 /* Element */
3472 if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
3473 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
3474 goto fail;
3475 }
3476
3477 if (!sm->fils_ecdh) {
3478 wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
3479 goto fail;
3480 }
3481 dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
3482 sm->fils_dh_elem_len);
3483 if (!dh_ss) {
3484 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
3485 goto fail;
3486 }
3487 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
3488 g_ap = pos;
3489 g_ap_len = sm->fils_dh_elem_len;
3490 pos += sm->fils_dh_elem_len;
3491 }
3492 #endif /* CONFIG_FILS_SK_PFS */
3493
3494 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
3495 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
3496 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
3497 goto fail;
3498 }
3499
3500 /* RSNE */
3501 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
3502 elems.rsn_ie_len);
3503 if (!elems.rsn_ie ||
3504 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
3505 &rsn) < 0) {
3506 wpa_printf(MSG_DEBUG, "FILS: No RSN element");
3507 goto fail;
3508 }
3509
3510 if (!elems.fils_nonce) {
3511 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
3512 goto fail;
3513 }
3514 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
3515 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
3516
3517 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
3518 struct wpa_ft_ies parse;
3519
3520 if (!elems.mdie || !elems.ftie) {
3521 wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
3522 goto fail;
3523 }
3524
3525 if (wpa_ft_parse_ies(pos, end - pos, &parse) < 0) {
3526 wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
3527 goto fail;
3528 }
3529
3530 if (!parse.r0kh_id) {
3531 wpa_printf(MSG_DEBUG,
3532 "FILS+FT: No R0KH-ID subelem in FTE");
3533 goto fail;
3534 }
3535 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
3536 sm->r0kh_id_len = parse.r0kh_id_len;
3537 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
3538 sm->r0kh_id, sm->r0kh_id_len);
3539
3540 if (!parse.r1kh_id) {
3541 wpa_printf(MSG_DEBUG,
3542 "FILS+FT: No R1KH-ID subelem in FTE");
3543 goto fail;
3544 }
3545 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
3546 wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
3547 sm->r1kh_id, FT_R1KH_ID_LEN);
3548
3549 /* TODO: Check MDE and FTE payload */
3550
3551 wpabuf_free(sm->fils_ft_ies);
3552 sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
3553 2 + elems.ftie_len);
3554 if (!sm->fils_ft_ies)
3555 goto fail;
3556 wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
3557 2 + elems.mdie_len);
3558 wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
3559 2 + elems.ftie_len);
3560 } else {
3561 wpabuf_free(sm->fils_ft_ies);
3562 sm->fils_ft_ies = NULL;
3563 }
3564
3565 /* PMKID List */
3566 if (rsn.pmkid && rsn.num_pmkid > 0) {
3567 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
3568 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
3569
3570 if (rsn.num_pmkid != 1) {
3571 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
3572 goto fail;
3573 }
3574 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
3575 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
3576 {
3577 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
3578 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
3579 sm->cur_pmksa->pmkid, PMKID_LEN);
3580 goto fail;
3581 }
3582 wpa_printf(MSG_DEBUG,
3583 "FILS: Matching PMKID - continue using PMKSA caching");
3584 pmkid_match = 1;
3585 }
3586 if (!pmkid_match && sm->cur_pmksa) {
3587 wpa_printf(MSG_DEBUG,
3588 "FILS: No PMKID match - cannot use cached PMKSA entry");
3589 sm->cur_pmksa = NULL;
3590 }
3591
3592 /* FILS Session */
3593 if (!elems.fils_session) {
3594 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
3595 goto fail;
3596 }
3597 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
3598 FILS_SESSION_LEN);
3599 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
3600 != 0) {
3601 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
3602 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
3603 sm->fils_session, FILS_SESSION_LEN);
3604 goto fail;
3605 }
3606
3607 /* FILS Wrapped Data */
3608 if (!sm->cur_pmksa && elems.fils_wrapped_data) {
3609 u8 rmsk[ERP_MAX_KEY_LEN];
3610 size_t rmsk_len;
3611
3612 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
3613 elems.fils_wrapped_data,
3614 elems.fils_wrapped_data_len);
3615 eapol_sm_process_erp_finish(sm->eapol, elems.fils_wrapped_data,
3616 elems.fils_wrapped_data_len);
3617 if (eapol_sm_failed(sm->eapol))
3618 goto fail;
3619
3620 rmsk_len = ERP_MAX_KEY_LEN;
3621 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
3622 if (res == PMK_LEN) {
3623 rmsk_len = PMK_LEN;
3624 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
3625 }
3626 if (res)
3627 goto fail;
3628
3629 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
3630 sm->fils_nonce, sm->fils_anonce,
3631 dh_ss ? wpabuf_head(dh_ss) : NULL,
3632 dh_ss ? wpabuf_len(dh_ss) : 0,
3633 sm->pmk, &sm->pmk_len);
3634 os_memset(rmsk, 0, sizeof(rmsk));
3635
3636 /* Don't use DHss in PTK derivation if PMKSA caching is not
3637 * used. */
3638 wpabuf_clear_free(dh_ss);
3639 dh_ss = NULL;
3640
3641 if (res)
3642 goto fail;
3643
3644 if (!sm->fils_erp_pmkid_set) {
3645 wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
3646 goto fail;
3647 }
3648 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
3649 PMKID_LEN);
3650 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
3651 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
3652 sm->fils_erp_pmkid, NULL, 0,
3653 sm->bssid, sm->own_addr,
3654 sm->network_ctx, sm->key_mgmt,
3655 NULL);
3656 }
3657
3658 if (!sm->cur_pmksa) {
3659 wpa_printf(MSG_DEBUG,
3660 "FILS: No remaining options to continue FILS authentication");
3661 goto fail;
3662 }
3663
3664 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid,
3665 sm->fils_nonce, sm->fils_anonce,
3666 dh_ss ? wpabuf_head(dh_ss) : NULL,
3667 dh_ss ? wpabuf_len(dh_ss) : 0,
3668 &sm->ptk, ick, &ick_len,
3669 sm->key_mgmt, sm->pairwise_cipher,
3670 sm->fils_ft, &sm->fils_ft_len) < 0) {
3671 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
3672 goto fail;
3673 }
3674
3675 wpabuf_clear_free(dh_ss);
3676 dh_ss = NULL;
3677
3678 sm->ptk_set = 1;
3679 sm->tptk_set = 0;
3680 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
3681
3682 #ifdef CONFIG_FILS_SK_PFS
3683 if (sm->fils_dh_group) {
3684 if (!sm->fils_ecdh) {
3685 wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
3686 goto fail;
3687 }
3688 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
3689 if (!pub)
3690 goto fail;
3691 wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
3692 g_sta = wpabuf_head(pub);
3693 g_sta_len = wpabuf_len(pub);
3694 if (!g_ap) {
3695 wpa_printf(MSG_INFO, "FILS: gAP not available");
3696 goto fail;
3697 }
3698 wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
3699 }
3700 #endif /* CONFIG_FILS_SK_PFS */
3701
3702 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
3703 sm->fils_anonce, sm->own_addr, sm->bssid,
3704 g_sta, g_sta_len, g_ap, g_ap_len,
3705 sm->key_mgmt, sm->fils_key_auth_sta,
3706 sm->fils_key_auth_ap,
3707 &sm->fils_key_auth_len);
3708 wpabuf_free(pub);
3709 os_memset(ick, 0, sizeof(ick));
3710 return res;
3711 fail:
3712 wpabuf_free(pub);
3713 wpabuf_clear_free(dh_ss);
3714 return -1;
3715 }
3716
3717
3718 #ifdef CONFIG_IEEE80211R
3719 static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
3720 {
3721 struct rsn_ie_hdr *rsnie;
3722 u16 capab;
3723 u8 *pos;
3724
3725 /* RSNIE[PMKR0Name/PMKR1Name] */
3726 rsnie = wpabuf_put(buf, sizeof(*rsnie));
3727 rsnie->elem_id = WLAN_EID_RSN;
3728 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
3729
3730 /* Group Suite Selector */
3731 if (!wpa_cipher_valid_group(sm->group_cipher)) {
3732 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
3733 sm->group_cipher);
3734 return -1;
3735 }
3736 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3737 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
3738 sm->group_cipher));
3739
3740 /* Pairwise Suite Count */
3741 wpabuf_put_le16(buf, 1);
3742
3743 /* Pairwise Suite List */
3744 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
3745 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
3746 sm->pairwise_cipher);
3747 return -1;
3748 }
3749 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3750 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
3751 sm->pairwise_cipher));
3752
3753 /* Authenticated Key Management Suite Count */
3754 wpabuf_put_le16(buf, 1);
3755
3756 /* Authenticated Key Management Suite List */
3757 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3758 if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
3759 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
3760 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
3761 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
3762 else {
3763 wpa_printf(MSG_WARNING,
3764 "FILS+FT: Invalid key management type (%d)",
3765 sm->key_mgmt);
3766 return -1;
3767 }
3768
3769 /* RSN Capabilities */
3770 capab = 0;
3771 #ifdef CONFIG_IEEE80211W
3772 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC)
3773 capab |= WPA_CAPABILITY_MFPC;
3774 #endif /* CONFIG_IEEE80211W */
3775 wpabuf_put_le16(buf, capab);
3776
3777 /* PMKID Count */
3778 wpabuf_put_le16(buf, 1);
3779
3780 /* PMKID List [PMKR1Name] */
3781 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
3782 sm->fils_ft, sm->fils_ft_len);
3783 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
3784 wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
3785 sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
3786 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
3787 sm->r0kh_id, sm->r0kh_id_len);
3788 if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
3789 sm->ssid_len, sm->mobility_domain,
3790 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
3791 sm->pmk_r0, sm->pmk_r0_name) < 0) {
3792 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
3793 return -1;
3794 }
3795 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0", sm->pmk_r0, PMK_LEN);
3796 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name",
3797 sm->pmk_r0_name, WPA_PMK_NAME_LEN);
3798 wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
3799 MAC2STR(sm->r1kh_id));
3800 pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
3801 if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
3802 pos) < 0) {
3803 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
3804 return -1;
3805 }
3806 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", pos, WPA_PMK_NAME_LEN);
3807
3808 #ifdef CONFIG_IEEE80211W
3809 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
3810 /* Management Group Cipher Suite */
3811 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3812 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
3813 }
3814 #endif /* CONFIG_IEEE80211W */
3815
3816 rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
3817 return 0;
3818 }
3819 #endif /* CONFIG_IEEE80211R */
3820
3821
3822 struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
3823 size_t *kek_len, const u8 **snonce,
3824 const u8 **anonce,
3825 const struct wpabuf **hlp,
3826 unsigned int num_hlp)
3827 {
3828 struct wpabuf *buf;
3829 size_t len;
3830 unsigned int i;
3831
3832 len = 1000;
3833 #ifdef CONFIG_IEEE80211R
3834 if (sm->fils_ft_ies)
3835 len += wpabuf_len(sm->fils_ft_ies);
3836 if (wpa_key_mgmt_ft(sm->key_mgmt))
3837 len += 256;
3838 #endif /* CONFIG_IEEE80211R */
3839 for (i = 0; hlp && i < num_hlp; i++)
3840 len += 10 + wpabuf_len(hlp[i]);
3841 buf = wpabuf_alloc(len);
3842 if (!buf)
3843 return NULL;
3844
3845 #ifdef CONFIG_IEEE80211R
3846 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
3847 /* MDE and FTE when using FILS+FT */
3848 wpabuf_put_buf(buf, sm->fils_ft_ies);
3849 /* RSNE with PMKR1Name in PMKID field */
3850 if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
3851 wpabuf_free(buf);
3852 return NULL;
3853 }
3854 }
3855 #endif /* CONFIG_IEEE80211R */
3856
3857 /* FILS Session */
3858 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3859 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
3860 /* Element ID Extension */
3861 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
3862 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
3863
3864 /* Everything after FILS Session element gets encrypted in the driver
3865 * with KEK. The buffer returned from here is the plaintext version. */
3866
3867 /* TODO: FILS Public Key */
3868
3869 /* FILS Key Confirm */
3870 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3871 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
3872 /* Element ID Extension */
3873 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
3874 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
3875
3876 /* FILS HLP Container */
3877 for (i = 0; hlp && i < num_hlp; i++) {
3878 const u8 *pos = wpabuf_head(hlp[i]);
3879 size_t left = wpabuf_len(hlp[i]);
3880
3881 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3882 if (left <= 254)
3883 len = 1 + left;
3884 else
3885 len = 255;
3886 wpabuf_put_u8(buf, len); /* Length */
3887 /* Element ID Extension */
3888 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
3889 /* Destination MAC Address, Source MAC Address, HLP Packet.
3890 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
3891 * header when LPD is used). */
3892 wpabuf_put_data(buf, pos, len - 1);
3893 pos += len - 1;
3894 left -= len - 1;
3895 while (left) {
3896 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
3897 len = left > 255 ? 255 : left;
3898 wpabuf_put_u8(buf, len);
3899 wpabuf_put_data(buf, pos, len);
3900 pos += len;
3901 left -= len;
3902 }
3903 }
3904
3905 /* TODO: FILS IP Address Assignment */
3906
3907 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
3908
3909 *kek = sm->ptk.kek;
3910 *kek_len = sm->ptk.kek_len;
3911 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
3912 *snonce = sm->fils_nonce;
3913 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
3914 *snonce, FILS_NONCE_LEN);
3915 *anonce = sm->fils_anonce;
3916 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
3917 *anonce, FILS_NONCE_LEN);
3918
3919 return buf;
3920 }
3921
3922
3923 static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
3924 {
3925 const u8 *pos, *end;
3926
3927 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
3928 if (len < 2 * ETH_ALEN)
3929 return;
3930 pos = resp + 2 * ETH_ALEN;
3931 end = resp + len;
3932 if (end - pos >= 6 &&
3933 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
3934 pos += 6; /* Remove SNAP/LLC header */
3935 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
3936 }
3937
3938
3939 static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
3940 size_t len)
3941 {
3942 const u8 *end = pos + len;
3943 u8 *tmp, *tmp_pos;
3944
3945 /* Check if there are any FILS HLP Container elements */
3946 while (end - pos >= 2) {
3947 if (2 + pos[1] > end - pos)
3948 return;
3949 if (pos[0] == WLAN_EID_EXTENSION &&
3950 pos[1] >= 1 + 2 * ETH_ALEN &&
3951 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
3952 break;
3953 pos += 2 + pos[1];
3954 }
3955 if (end - pos < 2)
3956 return; /* No FILS HLP Container elements */
3957
3958 tmp = os_malloc(end - pos);
3959 if (!tmp)
3960 return;
3961
3962 while (end - pos >= 2) {
3963 if (2 + pos[1] > end - pos ||
3964 pos[0] != WLAN_EID_EXTENSION ||
3965 pos[1] < 1 + 2 * ETH_ALEN ||
3966 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
3967 break;
3968 tmp_pos = tmp;
3969 os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
3970 tmp_pos += pos[1] - 1;
3971 pos += 2 + pos[1];
3972
3973 /* Add possible fragments */
3974 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
3975 2 + pos[1] <= end - pos) {
3976 os_memcpy(tmp_pos, pos + 2, pos[1]);
3977 tmp_pos += pos[1];
3978 pos += 2 + pos[1];
3979 }
3980
3981 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
3982 }
3983
3984 os_free(tmp);
3985 }
3986
3987
3988 int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
3989 {
3990 const struct ieee80211_mgmt *mgmt;
3991 const u8 *end, *ie_start;
3992 struct ieee802_11_elems elems;
3993 int keylen, rsclen;
3994 enum wpa_alg alg;
3995 struct wpa_gtk_data gd;
3996 int maxkeylen;
3997 struct wpa_eapol_ie_parse kde;
3998
3999 if (!sm || !sm->ptk_set) {
4000 wpa_printf(MSG_DEBUG, "FILS: No KEK available");
4001 return -1;
4002 }
4003
4004 if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
4005 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
4006 return -1;
4007 }
4008
4009 if (sm->fils_completed) {
4010 wpa_printf(MSG_DEBUG,
4011 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
4012 return -1;
4013 }
4014
4015 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
4016 resp, len);
4017
4018 mgmt = (const struct ieee80211_mgmt *) resp;
4019 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
4020 return -1;
4021
4022 end = resp + len;
4023 /* Same offset for Association Response and Reassociation Response */
4024 ie_start = mgmt->u.assoc_resp.variable;
4025
4026 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
4027 ParseFailed) {
4028 wpa_printf(MSG_DEBUG,
4029 "FILS: Failed to parse decrypted elements");
4030 goto fail;
4031 }
4032
4033 if (!elems.fils_session) {
4034 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
4035 return -1;
4036 }
4037 if (os_memcmp(elems.fils_session, sm->fils_session,
4038 FILS_SESSION_LEN) != 0) {
4039 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
4040 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
4041 elems.fils_session, FILS_SESSION_LEN);
4042 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
4043 sm->fils_session, FILS_SESSION_LEN);
4044 }
4045
4046 /* TODO: FILS Public Key */
4047
4048 if (!elems.fils_key_confirm) {
4049 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
4050 goto fail;
4051 }
4052 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
4053 wpa_printf(MSG_DEBUG,
4054 "FILS: Unexpected Key-Auth length %d (expected %d)",
4055 elems.fils_key_confirm_len,
4056 (int) sm->fils_key_auth_len);
4057 goto fail;
4058 }
4059 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
4060 sm->fils_key_auth_len) != 0) {
4061 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
4062 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
4063 elems.fils_key_confirm,
4064 elems.fils_key_confirm_len);
4065 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
4066 sm->fils_key_auth_ap, sm->fils_key_auth_len);
4067 goto fail;
4068 }
4069
4070 /* Key Delivery */
4071 if (!elems.key_delivery) {
4072 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
4073 goto fail;
4074 }
4075
4076 /* Parse GTK and set the key to the driver */
4077 os_memset(&gd, 0, sizeof(gd));
4078 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
4079 elems.key_delivery_len - WPA_KEY_RSC_LEN,
4080 &kde) < 0) {
4081 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
4082 goto fail;
4083 }
4084 if (!kde.gtk) {
4085 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
4086 goto fail;
4087 }
4088 maxkeylen = gd.gtk_len = kde.gtk_len - 2;
4089 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
4090 gd.gtk_len, maxkeylen,
4091 &gd.key_rsc_len, &gd.alg))
4092 goto fail;
4093
4094 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
4095 gd.keyidx = kde.gtk[0] & 0x3;
4096 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
4097 !!(kde.gtk[0] & BIT(2)));
4098 if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
4099 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
4100 (unsigned long) kde.gtk_len - 2);
4101 goto fail;
4102 }
4103 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
4104
4105 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
4106 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
4107 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
4108 goto fail;
4109 }
4110
4111 if (ieee80211w_set_keys(sm, &kde) < 0) {
4112 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
4113 goto fail;
4114 }
4115
4116 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
4117 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
4118 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
4119 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
4120 sm->ptk.tk, keylen);
4121 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen,
4122 sm->ptk.tk, keylen) < 0) {
4123 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
4124 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
4125 MACSTR ")",
4126 alg, keylen, MAC2STR(sm->bssid));
4127 goto fail;
4128 }
4129
4130 /* TODO: TK could be cleared after auth frame exchange now that driver
4131 * takes care of association frame encryption/decryption. */
4132 /* TK is not needed anymore in supplicant */
4133 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
4134 sm->ptk.installed = 1;
4135
4136 /* FILS HLP Container */
4137 fils_process_hlp_container(sm, ie_start, end - ie_start);
4138
4139 /* TODO: FILS IP Address Assignment */
4140
4141 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
4142 sm->fils_completed = 1;
4143
4144 return 0;
4145 fail:
4146 return -1;
4147 }
4148
4149
4150 void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
4151 {
4152 if (sm)
4153 sm->fils_completed = !!set;
4154 }
4155
4156 #endif /* CONFIG_FILS */
4157
4158
4159 int wpa_fils_is_completed(struct wpa_sm *sm)
4160 {
4161 #ifdef CONFIG_FILS
4162 return sm && sm->fils_completed;
4163 #else /* CONFIG_FILS */
4164 return 0;
4165 #endif /* CONFIG_FILS */
4166 }
4167
4168
4169 #ifdef CONFIG_OWE
4170
4171 struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
4172 {
4173 struct wpabuf *ie = NULL, *pub = NULL;
4174 size_t prime_len;
4175
4176 if (group == 19)
4177 prime_len = 32;
4178 else if (group == 20)
4179 prime_len = 48;
4180 else if (group == 21)
4181 prime_len = 66;
4182 else
4183 return NULL;
4184
4185 crypto_ecdh_deinit(sm->owe_ecdh);
4186 sm->owe_ecdh = crypto_ecdh_init(group);
4187 if (!sm->owe_ecdh)
4188 goto fail;
4189 sm->owe_group = group;
4190 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
4191 pub = wpabuf_zeropad(pub, prime_len);
4192 if (!pub)
4193 goto fail;
4194
4195 ie = wpabuf_alloc(5 + wpabuf_len(pub));
4196 if (!ie)
4197 goto fail;
4198 wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
4199 wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
4200 wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
4201 wpabuf_put_le16(ie, group);
4202 wpabuf_put_buf(ie, pub);
4203 wpabuf_free(pub);
4204 wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
4205 ie);
4206
4207 return ie;
4208 fail:
4209 wpabuf_free(pub);
4210 crypto_ecdh_deinit(sm->owe_ecdh);
4211 sm->owe_ecdh = NULL;
4212 return NULL;
4213 }
4214
4215
4216 int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
4217 const u8 *resp_ies, size_t resp_ies_len)
4218 {
4219 struct ieee802_11_elems elems;
4220 u16 group;
4221 struct wpabuf *secret, *pub, *hkey;
4222 int res;
4223 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
4224 const char *info = "OWE Key Generation";
4225 const u8 *addr[2];
4226 size_t len[2];
4227 size_t hash_len, prime_len;
4228 struct wpa_ie_data data;
4229
4230 if (!resp_ies ||
4231 ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
4232 ParseFailed) {
4233 wpa_printf(MSG_INFO,
4234 "OWE: Could not parse Association Response frame elements");
4235 return -1;
4236 }
4237
4238 if (sm->cur_pmksa && elems.rsn_ie &&
4239 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
4240 &data) == 0 &&
4241 data.num_pmkid == 1 && data.pmkid &&
4242 os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
4243 wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
4244 wpa_sm_set_pmk_from_pmksa(sm);
4245 return 0;
4246 }
4247
4248 if (!elems.owe_dh) {
4249 wpa_printf(MSG_INFO,
4250 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
4251 return -1;
4252 }
4253
4254 group = WPA_GET_LE16(elems.owe_dh);
4255 if (group != sm->owe_group) {
4256 wpa_printf(MSG_INFO,
4257 "OWE: Unexpected Diffie-Hellman group in response: %u",
4258 group);
4259 return -1;
4260 }
4261
4262 if (!sm->owe_ecdh) {
4263 wpa_printf(MSG_INFO, "OWE: No ECDH state available");
4264 return -1;
4265 }
4266
4267 if (group == 19)
4268 prime_len = 32;
4269 else if (group == 20)
4270 prime_len = 48;
4271 else if (group == 21)
4272 prime_len = 66;
4273 else
4274 return -1;
4275
4276 secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
4277 elems.owe_dh + 2,
4278 elems.owe_dh_len - 2);
4279 secret = wpabuf_zeropad(secret, prime_len);
4280 if (!secret) {
4281 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
4282 return -1;
4283 }
4284 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
4285
4286 /* prk = HKDF-extract(C | A | group, z) */
4287
4288 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
4289 if (!pub) {
4290 wpabuf_clear_free(secret);
4291 return -1;
4292 }
4293
4294 /* PMKID = Truncate-128(Hash(C | A)) */
4295 addr[0] = wpabuf_head(pub);
4296 len[0] = wpabuf_len(pub);
4297 addr[1] = elems.owe_dh + 2;
4298 len[1] = elems.owe_dh_len - 2;
4299 if (group == 19) {
4300 res = sha256_vector(2, addr, len, pmkid);
4301 hash_len = SHA256_MAC_LEN;
4302 } else if (group == 20) {
4303 res = sha384_vector(2, addr, len, pmkid);
4304 hash_len = SHA384_MAC_LEN;
4305 } else if (group == 21) {
4306 res = sha512_vector(2, addr, len, pmkid);
4307 hash_len = SHA512_MAC_LEN;
4308 } else {
4309 res = -1;
4310 hash_len = 0;
4311 }
4312 pub = wpabuf_zeropad(pub, prime_len);
4313 if (res < 0 || !pub) {
4314 wpabuf_free(pub);
4315 wpabuf_clear_free(secret);
4316 return -1;
4317 }
4318
4319 hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
4320 if (!hkey) {
4321 wpabuf_free(pub);
4322 wpabuf_clear_free(secret);
4323 return -1;
4324 }
4325
4326 wpabuf_put_buf(hkey, pub); /* C */
4327 wpabuf_free(pub);
4328 wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
4329 wpabuf_put_le16(hkey, sm->owe_group); /* group */
4330 if (group == 19)
4331 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
4332 wpabuf_head(secret), wpabuf_len(secret), prk);
4333 else if (group == 20)
4334 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
4335 wpabuf_head(secret), wpabuf_len(secret), prk);
4336 else if (group == 21)
4337 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
4338 wpabuf_head(secret), wpabuf_len(secret), prk);
4339 wpabuf_clear_free(hkey);
4340 wpabuf_clear_free(secret);
4341 if (res < 0)
4342 return -1;
4343
4344 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
4345
4346 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
4347
4348 if (group == 19)
4349 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
4350 os_strlen(info), sm->pmk, hash_len);
4351 else if (group == 20)
4352 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
4353 os_strlen(info), sm->pmk, hash_len);
4354 else if (group == 21)
4355 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
4356 os_strlen(info), sm->pmk, hash_len);
4357 os_memset(prk, 0, SHA512_MAC_LEN);
4358 if (res < 0)
4359 return -1;
4360 sm->pmk_len = hash_len;
4361
4362 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
4363 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
4364 pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
4365 bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
4366 NULL);
4367
4368 return 0;
4369 }
4370
4371 #endif /* CONFIG_OWE */
4372
4373
4374 void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
4375 {
4376 #ifdef CONFIG_FILS
4377 if (sm && fils_cache_id) {
4378 sm->fils_cache_id_set = 1;
4379 os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
4380 }
4381 #endif /* CONFIG_FILS */
4382 }