• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * WPA Supplicant - WPA state machine and EAPOL-Key processing
3  * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14 
15 #include "includes.h"
16 
17 #include "common.h"
18 #include "crypto/aes_wrap.h"
19 #include "crypto/crypto.h"
20 #include "crypto/random.h"
21 #include "common/ieee802_11_defs.h"
22 #include "eapol_supp/eapol_supp_sm.h"
23 #include "wpa.h"
24 #include "eloop.h"
25 #include "preauth.h"
26 #include "pmksa_cache.h"
27 #include "wpa_i.h"
28 #include "wpa_ie.h"
29 #include "peerkey.h"
30 
31 
32 /**
33  * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
34  * @sm: Pointer to WPA state machine data from wpa_sm_init()
35  * @kck: Key Confirmation Key (KCK, part of PTK)
36  * @ver: Version field from Key Info
37  * @dest: Destination address for the frame
38  * @proto: Ethertype (usually ETH_P_EAPOL)
39  * @msg: EAPOL-Key message
40  * @msg_len: Length of message
41  * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
42  */
wpa_eapol_key_send(struct wpa_sm * sm,const u8 * kck,int ver,const u8 * dest,u16 proto,u8 * msg,size_t msg_len,u8 * key_mic)43 void wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck,
44 			int ver, const u8 *dest, u16 proto,
45 			u8 *msg, size_t msg_len, u8 *key_mic)
46 {
47 	if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
48 		/*
49 		 * Association event was not yet received; try to fetch
50 		 * BSSID from the driver.
51 		 */
52 		if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
53 			wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
54 				"WPA: Failed to read BSSID for "
55 				"EAPOL-Key destination address");
56 		} else {
57 			dest = sm->bssid;
58 			wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
59 				"WPA: Use BSSID (" MACSTR
60 				") as the destination for EAPOL-Key",
61 				MAC2STR(dest));
62 		}
63 	}
64 	if (key_mic &&
65 	    wpa_eapol_key_mic(kck, ver, msg, msg_len, key_mic)) {
66 		wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
67 			"WPA: Failed to generate EAPOL-Key "
68 			"version %d MIC", ver);
69 		goto out;
70 	}
71 	wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", kck, 16);
72 	wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC", key_mic, 16);
73 	wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
74 	wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
75 	eapol_sm_notify_tx_eapol_key(sm->eapol);
76 out:
77 	os_free(msg);
78 }
79 
80 
81 /**
82  * wpa_sm_key_request - Send EAPOL-Key Request
83  * @sm: Pointer to WPA state machine data from wpa_sm_init()
84  * @error: Indicate whether this is an Michael MIC error report
85  * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
86  *
87  * Send an EAPOL-Key Request to the current authenticator. This function is
88  * used to request rekeying and it is usually called when a local Michael MIC
89  * failure is detected.
90  */
wpa_sm_key_request(struct wpa_sm * sm,int error,int pairwise)91 void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
92 {
93 	size_t rlen;
94 	struct wpa_eapol_key *reply;
95 	int key_info, ver;
96 	u8 bssid[ETH_ALEN], *rbuf;
97 
98 	if (wpa_key_mgmt_ft(sm->key_mgmt) || wpa_key_mgmt_sha256(sm->key_mgmt))
99 		ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
100 	else if (sm->pairwise_cipher == WPA_CIPHER_CCMP)
101 		ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
102 	else
103 		ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
104 
105 	if (wpa_sm_get_bssid(sm, bssid) < 0) {
106 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
107 			"Failed to read BSSID for EAPOL-Key request");
108 		return;
109 	}
110 
111 	rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
112 				  sizeof(*reply), &rlen, (void *) &reply);
113 	if (rbuf == NULL)
114 		return;
115 
116 	reply->type = sm->proto == WPA_PROTO_RSN ?
117 		EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
118 	key_info = WPA_KEY_INFO_REQUEST | ver;
119 	if (sm->ptk_set)
120 		key_info |= WPA_KEY_INFO_MIC;
121 	if (error)
122 		key_info |= WPA_KEY_INFO_ERROR;
123 	if (pairwise)
124 		key_info |= WPA_KEY_INFO_KEY_TYPE;
125 	WPA_PUT_BE16(reply->key_info, key_info);
126 	WPA_PUT_BE16(reply->key_length, 0);
127 	os_memcpy(reply->replay_counter, sm->request_counter,
128 		  WPA_REPLAY_COUNTER_LEN);
129 	inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
130 
131 	WPA_PUT_BE16(reply->key_data_length, 0);
132 
133 	wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
134 		"WPA: Sending EAPOL-Key Request (error=%d "
135 		"pairwise=%d ptk_set=%d len=%lu)",
136 		error, pairwise, sm->ptk_set, (unsigned long) rlen);
137 	wpa_eapol_key_send(sm, sm->ptk.kck, ver, bssid, ETH_P_EAPOL,
138 			   rbuf, rlen, key_info & WPA_KEY_INFO_MIC ?
139 			   reply->key_mic : NULL);
140 }
141 
142 
wpa_supplicant_get_pmk(struct wpa_sm * sm,const unsigned char * src_addr,const u8 * pmkid)143 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
144 				  const unsigned char *src_addr,
145 				  const u8 *pmkid)
146 {
147 	int abort_cached = 0;
148 
149 	if (pmkid && !sm->cur_pmksa) {
150 		/* When using drivers that generate RSN IE, wpa_supplicant may
151 		 * not have enough time to get the association information
152 		 * event before receiving this 1/4 message, so try to find a
153 		 * matching PMKSA cache entry here. */
154 		sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid);
155 		if (sm->cur_pmksa) {
156 			wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
157 				"RSN: found matching PMKID from PMKSA cache");
158 		} else {
159 			wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
160 				"RSN: no matching PMKID found");
161 			abort_cached = 1;
162 		}
163 	}
164 
165 	if (pmkid && sm->cur_pmksa &&
166 	    os_memcmp(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
167 		wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
168 		wpa_sm_set_pmk_from_pmksa(sm);
169 		wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
170 				sm->pmk, sm->pmk_len);
171 		eapol_sm_notify_cached(sm->eapol);
172 #ifdef CONFIG_IEEE80211R
173 		sm->xxkey_len = 0;
174 #endif /* CONFIG_IEEE80211R */
175 	} else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
176 		int res, pmk_len;
177 		pmk_len = PMK_LEN;
178 		res = eapol_sm_get_key(sm->eapol, sm->pmk, PMK_LEN);
179 		if (res) {
180 			/*
181 			 * EAP-LEAP is an exception from other EAP methods: it
182 			 * uses only 16-byte PMK.
183 			 */
184 			res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
185 			pmk_len = 16;
186 		} else {
187 #ifdef CONFIG_IEEE80211R
188 			u8 buf[2 * PMK_LEN];
189 			if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
190 			{
191 				os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
192 				sm->xxkey_len = PMK_LEN;
193 				os_memset(buf, 0, sizeof(buf));
194 			}
195 #endif /* CONFIG_IEEE80211R */
196 		}
197 		if (res == 0) {
198 			wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
199 					"machines", sm->pmk, pmk_len);
200 			sm->pmk_len = pmk_len;
201 			if (sm->proto == WPA_PROTO_RSN &&
202 			    !wpa_key_mgmt_ft(sm->key_mgmt)) {
203 				pmksa_cache_add(sm->pmksa, sm->pmk, pmk_len,
204 						src_addr, sm->own_addr,
205 						sm->network_ctx, sm->key_mgmt);
206 			}
207 			if (!sm->cur_pmksa && pmkid &&
208 			    pmksa_cache_get(sm->pmksa, src_addr, pmkid)) {
209 				wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
210 					"RSN: the new PMK matches with the "
211 					"PMKID");
212 				abort_cached = 0;
213 			}
214 		} else {
215 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
216 				"WPA: Failed to get master session key from "
217 				"EAPOL state machines - key handshake "
218 				"aborted");
219 			if (sm->cur_pmksa) {
220 				wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
221 					"RSN: Cancelled PMKSA caching "
222 					"attempt");
223 				sm->cur_pmksa = NULL;
224 				abort_cached = 1;
225 			} else if (!abort_cached) {
226 				return -1;
227 			}
228 		}
229 	}
230 
231 	if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
232 	    !wpa_key_mgmt_ft(sm->key_mgmt)) {
233 		/* Send EAPOL-Start to trigger full EAP authentication. */
234 		u8 *buf;
235 		size_t buflen;
236 
237 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
238 			"RSN: no PMKSA entry found - trigger "
239 			"full EAP authentication");
240 		buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
241 					 NULL, 0, &buflen, NULL);
242 		if (buf) {
243 			wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
244 					  buf, buflen);
245 			os_free(buf);
246 			return -2;
247 		}
248 
249 		return -1;
250 	}
251 
252 	return 0;
253 }
254 
255 
256 /**
257  * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
258  * @sm: Pointer to WPA state machine data from wpa_sm_init()
259  * @dst: Destination address for the frame
260  * @key: Pointer to the EAPOL-Key frame header
261  * @ver: Version bits from EAPOL-Key Key Info
262  * @nonce: Nonce value for the EAPOL-Key frame
263  * @wpa_ie: WPA/RSN IE
264  * @wpa_ie_len: Length of the WPA/RSN IE
265  * @ptk: PTK to use for keyed hash and encryption
266  * Returns: 0 on success, -1 on failure
267  */
wpa_supplicant_send_2_of_4(struct wpa_sm * sm,const unsigned char * dst,const struct wpa_eapol_key * key,int ver,const u8 * nonce,const u8 * wpa_ie,size_t wpa_ie_len,struct wpa_ptk * ptk)268 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
269 			       const struct wpa_eapol_key *key,
270 			       int ver, const u8 *nonce,
271 			       const u8 *wpa_ie, size_t wpa_ie_len,
272 			       struct wpa_ptk *ptk)
273 {
274 	size_t rlen;
275 	struct wpa_eapol_key *reply;
276 	u8 *rbuf;
277 	u8 *rsn_ie_buf = NULL;
278 
279 	if (wpa_ie == NULL) {
280 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
281 			"cannot generate msg 2/4");
282 		return -1;
283 	}
284 
285 #ifdef CONFIG_IEEE80211R
286 	if (wpa_key_mgmt_ft(sm->key_mgmt)) {
287 		int res;
288 
289 		/*
290 		 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
291 		 * FTIE from (Re)Association Response.
292 		 */
293 		rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
294 				       sm->assoc_resp_ies_len);
295 		if (rsn_ie_buf == NULL)
296 			return -1;
297 		os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
298 		res = wpa_insert_pmkid(rsn_ie_buf, wpa_ie_len,
299 				       sm->pmk_r1_name);
300 		if (res < 0) {
301 			os_free(rsn_ie_buf);
302 			return -1;
303 		}
304 		wpa_ie_len += res;
305 
306 		if (sm->assoc_resp_ies) {
307 			os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
308 				  sm->assoc_resp_ies_len);
309 			wpa_ie_len += sm->assoc_resp_ies_len;
310 		}
311 
312 		wpa_ie = rsn_ie_buf;
313 	}
314 #endif /* CONFIG_IEEE80211R */
315 
316 	wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
317 
318 	rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
319 				  NULL, sizeof(*reply) + wpa_ie_len,
320 				  &rlen, (void *) &reply);
321 	if (rbuf == NULL) {
322 		os_free(rsn_ie_buf);
323 		return -1;
324 	}
325 
326 	reply->type = sm->proto == WPA_PROTO_RSN ?
327 		EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
328 	WPA_PUT_BE16(reply->key_info,
329 		     ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
330 	if (sm->proto == WPA_PROTO_RSN)
331 		WPA_PUT_BE16(reply->key_length, 0);
332 	else
333 		os_memcpy(reply->key_length, key->key_length, 2);
334 	os_memcpy(reply->replay_counter, key->replay_counter,
335 		  WPA_REPLAY_COUNTER_LEN);
336 	wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
337 		    WPA_REPLAY_COUNTER_LEN);
338 
339 	WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
340 	os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
341 	os_free(rsn_ie_buf);
342 
343 	os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
344 
345 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
346 	wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
347 			   rbuf, rlen, reply->key_mic);
348 
349 	return 0;
350 }
351 
352 
wpa_derive_ptk(struct wpa_sm * sm,const unsigned char * src_addr,const struct wpa_eapol_key * key,struct wpa_ptk * ptk)353 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
354 			  const struct wpa_eapol_key *key,
355 			  struct wpa_ptk *ptk)
356 {
357 	size_t ptk_len = sm->pairwise_cipher == WPA_CIPHER_CCMP ? 48 : 64;
358 #ifdef CONFIG_IEEE80211R
359 	if (wpa_key_mgmt_ft(sm->key_mgmt))
360 		return wpa_derive_ptk_ft(sm, src_addr, key, ptk, ptk_len);
361 #endif /* CONFIG_IEEE80211R */
362 
363 	wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
364 		       sm->own_addr, sm->bssid, sm->snonce, key->key_nonce,
365 		       (u8 *) ptk, ptk_len,
366 		       wpa_key_mgmt_sha256(sm->key_mgmt));
367 	return 0;
368 }
369 
370 
wpa_supplicant_process_1_of_4(struct wpa_sm * sm,const unsigned char * src_addr,const struct wpa_eapol_key * key,u16 ver)371 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
372 					  const unsigned char *src_addr,
373 					  const struct wpa_eapol_key *key,
374 					  u16 ver)
375 {
376 	struct wpa_eapol_ie_parse ie;
377 	struct wpa_ptk *ptk;
378 	u8 buf[8];
379 	int res;
380 
381 	if (wpa_sm_get_network_ctx(sm) == NULL) {
382 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
383 			"found (msg 1 of 4)");
384 		return;
385 	}
386 
387 	wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
388 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
389 		"Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
390 
391 	os_memset(&ie, 0, sizeof(ie));
392 
393 #ifndef CONFIG_NO_WPA2
394 	if (sm->proto == WPA_PROTO_RSN) {
395 		/* RSN: msg 1/4 should contain PMKID for the selected PMK */
396 		const u8 *_buf = (const u8 *) (key + 1);
397 		size_t len = WPA_GET_BE16(key->key_data_length);
398 		wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", _buf, len);
399 		wpa_supplicant_parse_ies(_buf, len, &ie);
400 		if (ie.pmkid) {
401 			wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
402 				    "Authenticator", ie.pmkid, PMKID_LEN);
403 		}
404 	}
405 #endif /* CONFIG_NO_WPA2 */
406 
407 	res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
408 	if (res == -2) {
409 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
410 			"msg 1/4 - requesting full EAP authentication");
411 		return;
412 	}
413 	if (res)
414 		goto failed;
415 
416 	if (sm->renew_snonce) {
417 		if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
418 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
419 				"WPA: Failed to get random data for SNonce");
420 			goto failed;
421 		}
422 		sm->renew_snonce = 0;
423 		wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
424 			    sm->snonce, WPA_NONCE_LEN);
425 	}
426 
427 	/* Calculate PTK which will be stored as a temporary PTK until it has
428 	 * been verified when processing message 3/4. */
429 	ptk = &sm->tptk;
430 	wpa_derive_ptk(sm, src_addr, key, ptk);
431 	/* Supplicant: swap tx/rx Mic keys */
432 	os_memcpy(buf, ptk->u.auth.tx_mic_key, 8);
433 	os_memcpy(ptk->u.auth.tx_mic_key, ptk->u.auth.rx_mic_key, 8);
434 	os_memcpy(ptk->u.auth.rx_mic_key, buf, 8);
435 	sm->tptk_set = 1;
436 
437 	if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
438 				       sm->assoc_wpa_ie, sm->assoc_wpa_ie_len,
439 				       ptk))
440 		goto failed;
441 
442 	os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
443 	return;
444 
445 failed:
446 	wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
447 }
448 
449 
wpa_sm_start_preauth(void * eloop_ctx,void * timeout_ctx)450 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
451 {
452 	struct wpa_sm *sm = eloop_ctx;
453 	rsn_preauth_candidate_process(sm);
454 }
455 
456 
wpa_supplicant_key_neg_complete(struct wpa_sm * sm,const u8 * addr,int secure)457 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
458 					    const u8 *addr, int secure)
459 {
460 	wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
461 		"WPA: Key negotiation completed with "
462 		MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
463 		wpa_cipher_txt(sm->pairwise_cipher),
464 		wpa_cipher_txt(sm->group_cipher));
465 	wpa_sm_cancel_auth_timeout(sm);
466 	wpa_sm_set_state(sm, WPA_COMPLETED);
467 
468 	if (secure) {
469 		wpa_sm_mlme_setprotection(
470 			sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
471 			MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
472 		eapol_sm_notify_portValid(sm->eapol, TRUE);
473 		if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
474 			eapol_sm_notify_eap_success(sm->eapol, TRUE);
475 		/*
476 		 * Start preauthentication after a short wait to avoid a
477 		 * possible race condition between the data receive and key
478 		 * configuration after the 4-Way Handshake. This increases the
479 		 * likelyhood of the first preauth EAPOL-Start frame getting to
480 		 * the target AP.
481 		 */
482 		eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
483 	}
484 
485 	if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
486 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
487 			"RSN: Authenticator accepted "
488 			"opportunistic PMKSA entry - marking it valid");
489 		sm->cur_pmksa->opportunistic = 0;
490 	}
491 
492 #ifdef CONFIG_IEEE80211R
493 	if (wpa_key_mgmt_ft(sm->key_mgmt)) {
494 		/* Prepare for the next transition */
495 		wpa_ft_prepare_auth_request(sm, NULL);
496 	}
497 #endif /* CONFIG_IEEE80211R */
498 }
499 
500 
wpa_sm_rekey_ptk(void * eloop_ctx,void * timeout_ctx)501 static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
502 {
503 	struct wpa_sm *sm = eloop_ctx;
504 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
505 	wpa_sm_key_request(sm, 0, 1);
506 }
507 
508 
wpa_supplicant_install_ptk(struct wpa_sm * sm,const struct wpa_eapol_key * key)509 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
510 				      const struct wpa_eapol_key *key)
511 {
512 	int keylen, rsclen;
513 	enum wpa_alg alg;
514 	const u8 *key_rsc;
515 	u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
516 
517 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
518 		"WPA: Installing PTK to the driver");
519 
520 	switch (sm->pairwise_cipher) {
521 	case WPA_CIPHER_CCMP:
522 		alg = WPA_ALG_CCMP;
523 		keylen = 16;
524 		rsclen = 6;
525 		break;
526 	case WPA_CIPHER_TKIP:
527 		alg = WPA_ALG_TKIP;
528 		keylen = 32;
529 		rsclen = 6;
530 		break;
531 	case WPA_CIPHER_NONE:
532 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
533 			"Suite: NONE - do not use pairwise keys");
534 		return 0;
535 	default:
536 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
537 			"WPA: Unsupported pairwise cipher %d",
538 			sm->pairwise_cipher);
539 		return -1;
540 	}
541 
542 	if (sm->proto == WPA_PROTO_RSN) {
543 		key_rsc = null_rsc;
544 	} else {
545 		key_rsc = key->key_rsc;
546 		wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
547 	}
548 
549 	if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
550 			   (u8 *) sm->ptk.tk1, keylen) < 0) {
551 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
552 			"WPA: Failed to set PTK to the "
553 			"driver (alg=%d keylen=%d bssid=" MACSTR ")",
554 			alg, keylen, MAC2STR(sm->bssid));
555 		return -1;
556 	}
557 
558 	if (sm->wpa_ptk_rekey) {
559 		eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
560 		eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
561 				       sm, NULL);
562 	}
563 
564 	return 0;
565 }
566 
567 
wpa_supplicant_check_group_cipher(struct wpa_sm * sm,int group_cipher,int keylen,int maxkeylen,int * key_rsc_len,enum wpa_alg * alg)568 static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
569 					     int group_cipher,
570 					     int keylen, int maxkeylen,
571 					     int *key_rsc_len,
572 					     enum wpa_alg *alg)
573 {
574 	int ret = 0;
575 
576 	switch (group_cipher) {
577 	case WPA_CIPHER_CCMP:
578 		if (keylen != 16 || maxkeylen < 16) {
579 			ret = -1;
580 			break;
581 		}
582 		*key_rsc_len = 6;
583 		*alg = WPA_ALG_CCMP;
584 		break;
585 	case WPA_CIPHER_TKIP:
586 		if (keylen != 32 || maxkeylen < 32) {
587 			ret = -1;
588 			break;
589 		}
590 		*key_rsc_len = 6;
591 		*alg = WPA_ALG_TKIP;
592 		break;
593 	case WPA_CIPHER_WEP104:
594 		if (keylen != 13 || maxkeylen < 13) {
595 			ret = -1;
596 			break;
597 		}
598 		*key_rsc_len = 0;
599 		*alg = WPA_ALG_WEP;
600 		break;
601 	case WPA_CIPHER_WEP40:
602 		if (keylen != 5 || maxkeylen < 5) {
603 			ret = -1;
604 			break;
605 		}
606 		*key_rsc_len = 0;
607 		*alg = WPA_ALG_WEP;
608 		break;
609 	default:
610 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
611 			"WPA: Unsupported Group Cipher %d",
612 			group_cipher);
613 		return -1;
614 	}
615 
616 	if (ret < 0 ) {
617 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
618 			"WPA: Unsupported %s Group Cipher key length %d (%d)",
619 			wpa_cipher_txt(group_cipher), keylen, maxkeylen);
620 	}
621 
622 	return ret;
623 }
624 
625 
626 struct wpa_gtk_data {
627 	enum wpa_alg alg;
628 	int tx, key_rsc_len, keyidx;
629 	u8 gtk[32];
630 	int gtk_len;
631 };
632 
633 
wpa_supplicant_install_gtk(struct wpa_sm * sm,const struct wpa_gtk_data * gd,const u8 * key_rsc)634 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
635 				      const struct wpa_gtk_data *gd,
636 				      const u8 *key_rsc)
637 {
638 	const u8 *_gtk = gd->gtk;
639 	u8 gtk_buf[32];
640 
641 	wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
642 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
643 		"WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
644 		gd->keyidx, gd->tx, gd->gtk_len);
645 	wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
646 	if (sm->group_cipher == WPA_CIPHER_TKIP) {
647 		/* Swap Tx/Rx keys for Michael MIC */
648 		os_memcpy(gtk_buf, gd->gtk, 16);
649 		os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
650 		os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
651 		_gtk = gtk_buf;
652 	}
653 	if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
654 		if (wpa_sm_set_key(sm, gd->alg, NULL,
655 				   gd->keyidx, 1, key_rsc, gd->key_rsc_len,
656 				   _gtk, gd->gtk_len) < 0) {
657 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
658 				"WPA: Failed to set GTK to the driver "
659 				"(Group only)");
660 			return -1;
661 		}
662 	} else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
663 				  gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
664 				  _gtk, gd->gtk_len) < 0) {
665 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
666 			"WPA: Failed to set GTK to "
667 			"the driver (alg=%d keylen=%d keyidx=%d)",
668 			gd->alg, gd->gtk_len, gd->keyidx);
669 		return -1;
670 	}
671 
672 	return 0;
673 }
674 
675 
wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm * sm,int tx)676 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
677 						int tx)
678 {
679 	if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
680 		/* Ignore Tx bit for GTK if a pairwise key is used. One AP
681 		 * seemed to set this bit (incorrectly, since Tx is only when
682 		 * doing Group Key only APs) and without this workaround, the
683 		 * data connection does not work because wpa_supplicant
684 		 * configured non-zero keyidx to be used for unicast. */
685 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
686 			"WPA: Tx bit set for GTK, but pairwise "
687 			"keys are used - ignore Tx bit");
688 		return 0;
689 	}
690 	return tx;
691 }
692 
693 
wpa_supplicant_pairwise_gtk(struct wpa_sm * sm,const struct wpa_eapol_key * key,const u8 * gtk,size_t gtk_len,int key_info)694 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
695 				       const struct wpa_eapol_key *key,
696 				       const u8 *gtk, size_t gtk_len,
697 				       int key_info)
698 {
699 #ifndef CONFIG_NO_WPA2
700 	struct wpa_gtk_data gd;
701 
702 	/*
703 	 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
704 	 * GTK KDE format:
705 	 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
706 	 * Reserved [bits 0-7]
707 	 * GTK
708 	 */
709 
710 	os_memset(&gd, 0, sizeof(gd));
711 	wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
712 			gtk, gtk_len);
713 
714 	if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
715 		return -1;
716 
717 	gd.keyidx = gtk[0] & 0x3;
718 	gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
719 						     !!(gtk[0] & BIT(2)));
720 	gtk += 2;
721 	gtk_len -= 2;
722 
723 	os_memcpy(gd.gtk, gtk, gtk_len);
724 	gd.gtk_len = gtk_len;
725 
726 	if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
727 					      gtk_len, gtk_len,
728 					      &gd.key_rsc_len, &gd.alg) ||
729 	    wpa_supplicant_install_gtk(sm, &gd, key->key_rsc)) {
730 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
731 			"RSN: Failed to install GTK");
732 		return -1;
733 	}
734 
735 	wpa_supplicant_key_neg_complete(sm, sm->bssid,
736 					key_info & WPA_KEY_INFO_SECURE);
737 	return 0;
738 #else /* CONFIG_NO_WPA2 */
739 	return -1;
740 #endif /* CONFIG_NO_WPA2 */
741 }
742 
743 
ieee80211w_set_keys(struct wpa_sm * sm,struct wpa_eapol_ie_parse * ie)744 static int ieee80211w_set_keys(struct wpa_sm *sm,
745 			       struct wpa_eapol_ie_parse *ie)
746 {
747 #ifdef CONFIG_IEEE80211W
748 	if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC)
749 		return 0;
750 
751 	if (ie->igtk) {
752 		const struct wpa_igtk_kde *igtk;
753 		u16 keyidx;
754 		if (ie->igtk_len != sizeof(*igtk))
755 			return -1;
756 		igtk = (const struct wpa_igtk_kde *) ie->igtk;
757 		keyidx = WPA_GET_LE16(igtk->keyid);
758 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: IGTK keyid %d "
759 			"pn %02x%02x%02x%02x%02x%02x",
760 			keyidx, MAC2STR(igtk->pn));
761 		wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
762 				igtk->igtk, WPA_IGTK_LEN);
763 		if (keyidx > 4095) {
764 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
765 				"WPA: Invalid IGTK KeyID %d", keyidx);
766 			return -1;
767 		}
768 		if (wpa_sm_set_key(sm, WPA_ALG_IGTK, broadcast_ether_addr,
769 				   keyidx, 0, igtk->pn, sizeof(igtk->pn),
770 				   igtk->igtk, WPA_IGTK_LEN) < 0) {
771 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
772 				"WPA: Failed to configure IGTK to the driver");
773 			return -1;
774 		}
775 	}
776 
777 	return 0;
778 #else /* CONFIG_IEEE80211W */
779 	return 0;
780 #endif /* CONFIG_IEEE80211W */
781 }
782 
783 
wpa_report_ie_mismatch(struct wpa_sm * sm,const char * reason,const u8 * src_addr,const u8 * wpa_ie,size_t wpa_ie_len,const u8 * rsn_ie,size_t rsn_ie_len)784 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
785 				   const char *reason, const u8 *src_addr,
786 				   const u8 *wpa_ie, size_t wpa_ie_len,
787 				   const u8 *rsn_ie, size_t rsn_ie_len)
788 {
789 	wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
790 		reason, MAC2STR(src_addr));
791 
792 	if (sm->ap_wpa_ie) {
793 		wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
794 			    sm->ap_wpa_ie, sm->ap_wpa_ie_len);
795 	}
796 	if (wpa_ie) {
797 		if (!sm->ap_wpa_ie) {
798 			wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
799 				"WPA: No WPA IE in Beacon/ProbeResp");
800 		}
801 		wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
802 			    wpa_ie, wpa_ie_len);
803 	}
804 
805 	if (sm->ap_rsn_ie) {
806 		wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
807 			    sm->ap_rsn_ie, sm->ap_rsn_ie_len);
808 	}
809 	if (rsn_ie) {
810 		if (!sm->ap_rsn_ie) {
811 			wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
812 				"WPA: No RSN IE in Beacon/ProbeResp");
813 		}
814 		wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
815 			    rsn_ie, rsn_ie_len);
816 	}
817 
818 	wpa_sm_disassociate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
819 }
820 
821 
822 #ifdef CONFIG_IEEE80211R
823 
ft_validate_mdie(struct wpa_sm * sm,const unsigned char * src_addr,struct wpa_eapol_ie_parse * ie,const u8 * assoc_resp_mdie)824 static int ft_validate_mdie(struct wpa_sm *sm,
825 			    const unsigned char *src_addr,
826 			    struct wpa_eapol_ie_parse *ie,
827 			    const u8 *assoc_resp_mdie)
828 {
829 	struct rsn_mdie *mdie;
830 
831 	mdie = (struct rsn_mdie *) (ie->mdie + 2);
832 	if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
833 	    os_memcmp(mdie->mobility_domain, sm->mobility_domain,
834 		      MOBILITY_DOMAIN_ID_LEN) != 0) {
835 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
836 			"not match with the current mobility domain");
837 		return -1;
838 	}
839 
840 	if (assoc_resp_mdie &&
841 	    (assoc_resp_mdie[1] != ie->mdie[1] ||
842 	     os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
843 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
844 		wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
845 			    ie->mdie, 2 + ie->mdie[1]);
846 		wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
847 			    assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
848 		return -1;
849 	}
850 
851 	return 0;
852 }
853 
854 
ft_validate_ftie(struct wpa_sm * sm,const unsigned char * src_addr,struct wpa_eapol_ie_parse * ie,const u8 * assoc_resp_ftie)855 static int ft_validate_ftie(struct wpa_sm *sm,
856 			    const unsigned char *src_addr,
857 			    struct wpa_eapol_ie_parse *ie,
858 			    const u8 *assoc_resp_ftie)
859 {
860 	if (ie->ftie == NULL) {
861 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
862 			"FT: No FTIE in EAPOL-Key msg 3/4");
863 		return -1;
864 	}
865 
866 	if (assoc_resp_ftie == NULL)
867 		return 0;
868 
869 	if (assoc_resp_ftie[1] != ie->ftie[1] ||
870 	    os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
871 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
872 		wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
873 			    ie->ftie, 2 + ie->ftie[1]);
874 		wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
875 			    assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
876 		return -1;
877 	}
878 
879 	return 0;
880 }
881 
882 
ft_validate_rsnie(struct wpa_sm * sm,const unsigned char * src_addr,struct wpa_eapol_ie_parse * ie)883 static int ft_validate_rsnie(struct wpa_sm *sm,
884 			     const unsigned char *src_addr,
885 			     struct wpa_eapol_ie_parse *ie)
886 {
887 	struct wpa_ie_data rsn;
888 
889 	if (!ie->rsn_ie)
890 		return 0;
891 
892 	/*
893 	 * Verify that PMKR1Name from EAPOL-Key message 3/4
894 	 * matches with the value we derived.
895 	 */
896 	if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
897 	    rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
898 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
899 			"FT 4-way handshake message 3/4");
900 		return -1;
901 	}
902 
903 	if (os_memcmp(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0) {
904 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
905 			"FT: PMKR1Name mismatch in "
906 			"FT 4-way handshake message 3/4");
907 		wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
908 			    rsn.pmkid, WPA_PMK_NAME_LEN);
909 		wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
910 			    sm->pmk_r1_name, WPA_PMK_NAME_LEN);
911 		return -1;
912 	}
913 
914 	return 0;
915 }
916 
917 
wpa_supplicant_validate_ie_ft(struct wpa_sm * sm,const unsigned char * src_addr,struct wpa_eapol_ie_parse * ie)918 static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
919 					 const unsigned char *src_addr,
920 					 struct wpa_eapol_ie_parse *ie)
921 {
922 	const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
923 
924 	if (sm->assoc_resp_ies) {
925 		pos = sm->assoc_resp_ies;
926 		end = pos + sm->assoc_resp_ies_len;
927 		while (pos + 2 < end) {
928 			if (pos + 2 + pos[1] > end)
929 				break;
930 			switch (*pos) {
931 			case WLAN_EID_MOBILITY_DOMAIN:
932 				mdie = pos;
933 				break;
934 			case WLAN_EID_FAST_BSS_TRANSITION:
935 				ftie = pos;
936 				break;
937 			}
938 			pos += 2 + pos[1];
939 		}
940 	}
941 
942 	if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
943 	    ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
944 	    ft_validate_rsnie(sm, src_addr, ie) < 0)
945 		return -1;
946 
947 	return 0;
948 }
949 
950 #endif /* CONFIG_IEEE80211R */
951 
952 
wpa_supplicant_validate_ie(struct wpa_sm * sm,const unsigned char * src_addr,struct wpa_eapol_ie_parse * ie)953 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
954 				      const unsigned char *src_addr,
955 				      struct wpa_eapol_ie_parse *ie)
956 {
957 	if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
958 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
959 			"WPA: No WPA/RSN IE for this AP known. "
960 			"Trying to get from scan results");
961 		if (wpa_sm_get_beacon_ie(sm) < 0) {
962 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
963 				"WPA: Could not find AP from "
964 				"the scan results");
965 		} else {
966 			wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
967 				"WPA: Found the current AP from "
968 				"updated scan results");
969 		}
970 	}
971 
972 	if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
973 	    (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
974 		wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
975 				       "with IE in Beacon/ProbeResp (no IE?)",
976 				       src_addr, ie->wpa_ie, ie->wpa_ie_len,
977 				       ie->rsn_ie, ie->rsn_ie_len);
978 		return -1;
979 	}
980 
981 	if ((ie->wpa_ie && sm->ap_wpa_ie &&
982 	     (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
983 	      os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
984 	    (ie->rsn_ie && sm->ap_rsn_ie &&
985 	     wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
986 				sm->ap_rsn_ie, sm->ap_rsn_ie_len,
987 				ie->rsn_ie, ie->rsn_ie_len))) {
988 		wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
989 				       "with IE in Beacon/ProbeResp",
990 				       src_addr, ie->wpa_ie, ie->wpa_ie_len,
991 				       ie->rsn_ie, ie->rsn_ie_len);
992 		return -1;
993 	}
994 
995 	if (sm->proto == WPA_PROTO_WPA &&
996 	    ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
997 		wpa_report_ie_mismatch(sm, "Possible downgrade attack "
998 				       "detected - RSN was enabled and RSN IE "
999 				       "was in msg 3/4, but not in "
1000 				       "Beacon/ProbeResp",
1001 				       src_addr, ie->wpa_ie, ie->wpa_ie_len,
1002 				       ie->rsn_ie, ie->rsn_ie_len);
1003 		return -1;
1004 	}
1005 
1006 #ifdef CONFIG_IEEE80211R
1007 	if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1008 	    wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1009 		return -1;
1010 #endif /* CONFIG_IEEE80211R */
1011 
1012 	return 0;
1013 }
1014 
1015 
1016 /**
1017  * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1018  * @sm: Pointer to WPA state machine data from wpa_sm_init()
1019  * @dst: Destination address for the frame
1020  * @key: Pointer to the EAPOL-Key frame header
1021  * @ver: Version bits from EAPOL-Key Key Info
1022  * @key_info: Key Info
1023  * @kde: KDEs to include the EAPOL-Key frame
1024  * @kde_len: Length of KDEs
1025  * @ptk: PTK to use for keyed hash and encryption
1026  * Returns: 0 on success, -1 on failure
1027  */
wpa_supplicant_send_4_of_4(struct wpa_sm * sm,const unsigned char * dst,const struct wpa_eapol_key * key,u16 ver,u16 key_info,const u8 * kde,size_t kde_len,struct wpa_ptk * ptk)1028 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1029 			       const struct wpa_eapol_key *key,
1030 			       u16 ver, u16 key_info,
1031 			       const u8 *kde, size_t kde_len,
1032 			       struct wpa_ptk *ptk)
1033 {
1034 	size_t rlen;
1035 	struct wpa_eapol_key *reply;
1036 	u8 *rbuf;
1037 
1038 	if (kde)
1039 		wpa_hexdump(MSG_DEBUG, "WPA: KDE for msg 4/4", kde, kde_len);
1040 
1041 	rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1042 				  sizeof(*reply) + kde_len,
1043 				  &rlen, (void *) &reply);
1044 	if (rbuf == NULL)
1045 		return -1;
1046 
1047 	reply->type = sm->proto == WPA_PROTO_RSN ?
1048 		EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1049 	key_info &= WPA_KEY_INFO_SECURE;
1050 	key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
1051 	WPA_PUT_BE16(reply->key_info, key_info);
1052 	if (sm->proto == WPA_PROTO_RSN)
1053 		WPA_PUT_BE16(reply->key_length, 0);
1054 	else
1055 		os_memcpy(reply->key_length, key->key_length, 2);
1056 	os_memcpy(reply->replay_counter, key->replay_counter,
1057 		  WPA_REPLAY_COUNTER_LEN);
1058 
1059 	WPA_PUT_BE16(reply->key_data_length, kde_len);
1060 	if (kde)
1061 		os_memcpy(reply + 1, kde, kde_len);
1062 
1063 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
1064 	wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
1065 			   rbuf, rlen, reply->key_mic);
1066 
1067 	return 0;
1068 }
1069 
1070 
wpa_supplicant_process_3_of_4(struct wpa_sm * sm,const struct wpa_eapol_key * key,u16 ver)1071 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1072 					  const struct wpa_eapol_key *key,
1073 					  u16 ver)
1074 {
1075 	u16 key_info, keylen, len;
1076 	const u8 *pos;
1077 	struct wpa_eapol_ie_parse ie;
1078 
1079 	wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1080 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1081 		"Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1082 
1083 	key_info = WPA_GET_BE16(key->key_info);
1084 
1085 	pos = (const u8 *) (key + 1);
1086 	len = WPA_GET_BE16(key->key_data_length);
1087 	wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", pos, len);
1088 	wpa_supplicant_parse_ies(pos, len, &ie);
1089 	if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1090 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1091 			"WPA: GTK IE in unencrypted key data");
1092 		goto failed;
1093 	}
1094 #ifdef CONFIG_IEEE80211W
1095 	if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1096 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1097 			"WPA: IGTK KDE in unencrypted key data");
1098 		goto failed;
1099 	}
1100 
1101 	if (ie.igtk && ie.igtk_len != sizeof(struct wpa_igtk_kde)) {
1102 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1103 			"WPA: Invalid IGTK KDE length %lu",
1104 			(unsigned long) ie.igtk_len);
1105 		goto failed;
1106 	}
1107 #endif /* CONFIG_IEEE80211W */
1108 
1109 	if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1110 		goto failed;
1111 
1112 	if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1113 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1114 			"WPA: ANonce from message 1 of 4-Way Handshake "
1115 			"differs from 3 of 4-Way Handshake - drop packet (src="
1116 			MACSTR ")", MAC2STR(sm->bssid));
1117 		goto failed;
1118 	}
1119 
1120 	keylen = WPA_GET_BE16(key->key_length);
1121 	switch (sm->pairwise_cipher) {
1122 	case WPA_CIPHER_CCMP:
1123 		if (keylen != 16) {
1124 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1125 				"WPA: Invalid CCMP key length %d (src=" MACSTR
1126 				")", keylen, MAC2STR(sm->bssid));
1127 			goto failed;
1128 		}
1129 		break;
1130 	case WPA_CIPHER_TKIP:
1131 		if (keylen != 32) {
1132 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1133 				"WPA: Invalid TKIP key length %d (src=" MACSTR
1134 				")", keylen, MAC2STR(sm->bssid));
1135 			goto failed;
1136 		}
1137 		break;
1138 	}
1139 
1140 	if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
1141 				       NULL, 0, &sm->ptk)) {
1142 		goto failed;
1143 	}
1144 
1145 	/* SNonce was successfully used in msg 3/4, so mark it to be renewed
1146 	 * for the next 4-Way Handshake. If msg 3 is received again, the old
1147 	 * SNonce will still be used to avoid changing PTK. */
1148 	sm->renew_snonce = 1;
1149 
1150 	if (key_info & WPA_KEY_INFO_INSTALL) {
1151 		if (wpa_supplicant_install_ptk(sm, key))
1152 			goto failed;
1153 	}
1154 
1155 	if (key_info & WPA_KEY_INFO_SECURE) {
1156 		wpa_sm_mlme_setprotection(
1157 			sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1158 			MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1159 		eapol_sm_notify_portValid(sm->eapol, TRUE);
1160 	}
1161 	wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1162 
1163 	if (ie.gtk &&
1164 	    wpa_supplicant_pairwise_gtk(sm, key,
1165 					ie.gtk, ie.gtk_len, key_info) < 0) {
1166 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1167 			"RSN: Failed to configure GTK");
1168 		goto failed;
1169 	}
1170 
1171 	if (ieee80211w_set_keys(sm, &ie) < 0) {
1172 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1173 			"RSN: Failed to configure IGTK");
1174 		goto failed;
1175 	}
1176 
1177 	return;
1178 
1179 failed:
1180 	wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1181 }
1182 
1183 
wpa_supplicant_process_1_of_2_rsn(struct wpa_sm * sm,const u8 * keydata,size_t keydatalen,u16 key_info,struct wpa_gtk_data * gd)1184 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1185 					     const u8 *keydata,
1186 					     size_t keydatalen,
1187 					     u16 key_info,
1188 					     struct wpa_gtk_data *gd)
1189 {
1190 	int maxkeylen;
1191 	struct wpa_eapol_ie_parse ie;
1192 
1193 	wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
1194 	wpa_supplicant_parse_ies(keydata, keydatalen, &ie);
1195 	if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1196 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1197 			"WPA: GTK IE in unencrypted key data");
1198 		return -1;
1199 	}
1200 	if (ie.gtk == NULL) {
1201 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1202 			"WPA: No GTK IE in Group Key msg 1/2");
1203 		return -1;
1204 	}
1205 	maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1206 
1207 	if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1208 					      gd->gtk_len, maxkeylen,
1209 					      &gd->key_rsc_len, &gd->alg))
1210 		return -1;
1211 
1212 	wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
1213 		    ie.gtk, ie.gtk_len);
1214 	gd->keyidx = ie.gtk[0] & 0x3;
1215 	gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1216 						      !!(ie.gtk[0] & BIT(2)));
1217 	if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1218 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1219 			"RSN: Too long GTK in GTK IE (len=%lu)",
1220 			(unsigned long) ie.gtk_len - 2);
1221 		return -1;
1222 	}
1223 	os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1224 
1225 	if (ieee80211w_set_keys(sm, &ie) < 0)
1226 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1227 			"RSN: Failed to configure IGTK");
1228 
1229 	return 0;
1230 }
1231 
1232 
wpa_supplicant_process_1_of_2_wpa(struct wpa_sm * sm,const struct wpa_eapol_key * key,size_t keydatalen,int key_info,size_t extra_len,u16 ver,struct wpa_gtk_data * gd)1233 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1234 					     const struct wpa_eapol_key *key,
1235 					     size_t keydatalen, int key_info,
1236 					     size_t extra_len, u16 ver,
1237 					     struct wpa_gtk_data *gd)
1238 {
1239 	size_t maxkeylen;
1240 	u8 ek[32];
1241 
1242 	gd->gtk_len = WPA_GET_BE16(key->key_length);
1243 	maxkeylen = keydatalen;
1244 	if (keydatalen > extra_len) {
1245 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1246 			"WPA: Truncated EAPOL-Key packet: "
1247 			"key_data_length=%lu > extra_len=%lu",
1248 			(unsigned long) keydatalen, (unsigned long) extra_len);
1249 		return -1;
1250 	}
1251 	if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1252 		if (maxkeylen < 8) {
1253 			wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1254 				"WPA: Too short maxkeylen (%lu)",
1255 				(unsigned long) maxkeylen);
1256 			return -1;
1257 		}
1258 		maxkeylen -= 8;
1259 	}
1260 
1261 	if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1262 					      gd->gtk_len, maxkeylen,
1263 					      &gd->key_rsc_len, &gd->alg))
1264 		return -1;
1265 
1266 	gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1267 		WPA_KEY_INFO_KEY_INDEX_SHIFT;
1268 	if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1269 		os_memcpy(ek, key->key_iv, 16);
1270 		os_memcpy(ek + 16, sm->ptk.kek, 16);
1271 		if (keydatalen > sizeof(gd->gtk)) {
1272 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1273 				"WPA: RC4 key data too long (%lu)",
1274 				(unsigned long) keydatalen);
1275 			return -1;
1276 		}
1277 		os_memcpy(gd->gtk, key + 1, keydatalen);
1278 		if (rc4_skip(ek, 32, 256, gd->gtk, keydatalen)) {
1279 			wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1280 				"WPA: RC4 failed");
1281 			return -1;
1282 		}
1283 	} else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1284 		if (keydatalen % 8) {
1285 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1286 				"WPA: Unsupported AES-WRAP len %lu",
1287 				(unsigned long) keydatalen);
1288 			return -1;
1289 		}
1290 		if (maxkeylen > sizeof(gd->gtk)) {
1291 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1292 				"WPA: AES-WRAP key data "
1293 				"too long (keydatalen=%lu maxkeylen=%lu)",
1294 				(unsigned long) keydatalen,
1295 				(unsigned long) maxkeylen);
1296 			return -1;
1297 		}
1298 		if (aes_unwrap(sm->ptk.kek, maxkeylen / 8,
1299 			       (const u8 *) (key + 1), gd->gtk)) {
1300 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1301 				"WPA: AES unwrap failed - could not decrypt "
1302 				"GTK");
1303 			return -1;
1304 		}
1305 	} else {
1306 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1307 			"WPA: Unsupported key_info type %d", ver);
1308 		return -1;
1309 	}
1310 	gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1311 		sm, !!(key_info & WPA_KEY_INFO_TXRX));
1312 	return 0;
1313 }
1314 
1315 
wpa_supplicant_send_2_of_2(struct wpa_sm * sm,const struct wpa_eapol_key * key,int ver,u16 key_info)1316 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1317 				      const struct wpa_eapol_key *key,
1318 				      int ver, u16 key_info)
1319 {
1320 	size_t rlen;
1321 	struct wpa_eapol_key *reply;
1322 	u8 *rbuf;
1323 
1324 	rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1325 				  sizeof(*reply), &rlen, (void *) &reply);
1326 	if (rbuf == NULL)
1327 		return -1;
1328 
1329 	reply->type = sm->proto == WPA_PROTO_RSN ?
1330 		EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1331 	key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1332 	key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1333 	WPA_PUT_BE16(reply->key_info, key_info);
1334 	if (sm->proto == WPA_PROTO_RSN)
1335 		WPA_PUT_BE16(reply->key_length, 0);
1336 	else
1337 		os_memcpy(reply->key_length, key->key_length, 2);
1338 	os_memcpy(reply->replay_counter, key->replay_counter,
1339 		  WPA_REPLAY_COUNTER_LEN);
1340 
1341 	WPA_PUT_BE16(reply->key_data_length, 0);
1342 
1343 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1344 	wpa_eapol_key_send(sm, sm->ptk.kck, ver, sm->bssid, ETH_P_EAPOL,
1345 			   rbuf, rlen, reply->key_mic);
1346 
1347 	return 0;
1348 }
1349 
1350 
wpa_supplicant_process_1_of_2(struct wpa_sm * sm,const unsigned char * src_addr,const struct wpa_eapol_key * key,int extra_len,u16 ver)1351 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1352 					  const unsigned char *src_addr,
1353 					  const struct wpa_eapol_key *key,
1354 					  int extra_len, u16 ver)
1355 {
1356 	u16 key_info, keydatalen;
1357 	int rekey, ret;
1358 	struct wpa_gtk_data gd;
1359 
1360 	os_memset(&gd, 0, sizeof(gd));
1361 
1362 	rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1363 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1364 		"Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1365 
1366 	key_info = WPA_GET_BE16(key->key_info);
1367 	keydatalen = WPA_GET_BE16(key->key_data_length);
1368 
1369 	if (sm->proto == WPA_PROTO_RSN) {
1370 		ret = wpa_supplicant_process_1_of_2_rsn(sm,
1371 							(const u8 *) (key + 1),
1372 							keydatalen, key_info,
1373 							&gd);
1374 	} else {
1375 		ret = wpa_supplicant_process_1_of_2_wpa(sm, key, keydatalen,
1376 							key_info, extra_len,
1377 							ver, &gd);
1378 	}
1379 
1380 	wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1381 
1382 	if (ret)
1383 		goto failed;
1384 
1385 	if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
1386 	    wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
1387 		goto failed;
1388 
1389 	if (rekey) {
1390 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1391 			"completed with " MACSTR " [GTK=%s]",
1392 			MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1393 		wpa_sm_cancel_auth_timeout(sm);
1394 		wpa_sm_set_state(sm, WPA_COMPLETED);
1395 	} else {
1396 		wpa_supplicant_key_neg_complete(sm, sm->bssid,
1397 						key_info &
1398 						WPA_KEY_INFO_SECURE);
1399 	}
1400 	return;
1401 
1402 failed:
1403 	wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1404 }
1405 
1406 
wpa_supplicant_verify_eapol_key_mic(struct wpa_sm * sm,struct wpa_eapol_key * key,u16 ver,const u8 * buf,size_t len)1407 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1408 					       struct wpa_eapol_key *key,
1409 					       u16 ver,
1410 					       const u8 *buf, size_t len)
1411 {
1412 	u8 mic[16];
1413 	int ok = 0;
1414 
1415 	os_memcpy(mic, key->key_mic, 16);
1416 	if (sm->tptk_set) {
1417 		os_memset(key->key_mic, 0, 16);
1418 		wpa_eapol_key_mic(sm->tptk.kck, ver, buf, len,
1419 				  key->key_mic);
1420 		if (os_memcmp(mic, key->key_mic, 16) != 0) {
1421 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1422 				"WPA: Invalid EAPOL-Key MIC "
1423 				"when using TPTK - ignoring TPTK");
1424 		} else {
1425 			ok = 1;
1426 			sm->tptk_set = 0;
1427 			sm->ptk_set = 1;
1428 			os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1429 		}
1430 	}
1431 
1432 	if (!ok && sm->ptk_set) {
1433 		os_memset(key->key_mic, 0, 16);
1434 		wpa_eapol_key_mic(sm->ptk.kck, ver, buf, len,
1435 				  key->key_mic);
1436 		if (os_memcmp(mic, key->key_mic, 16) != 0) {
1437 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1438 				"WPA: Invalid EAPOL-Key MIC - "
1439 				"dropping packet");
1440 			return -1;
1441 		}
1442 		ok = 1;
1443 	}
1444 
1445 	if (!ok) {
1446 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1447 			"WPA: Could not verify EAPOL-Key MIC - "
1448 			"dropping packet");
1449 		return -1;
1450 	}
1451 
1452 	os_memcpy(sm->rx_replay_counter, key->replay_counter,
1453 		  WPA_REPLAY_COUNTER_LEN);
1454 	sm->rx_replay_counter_set = 1;
1455 	return 0;
1456 }
1457 
1458 
1459 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
wpa_supplicant_decrypt_key_data(struct wpa_sm * sm,struct wpa_eapol_key * key,u16 ver)1460 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1461 					   struct wpa_eapol_key *key, u16 ver)
1462 {
1463 	u16 keydatalen = WPA_GET_BE16(key->key_data_length);
1464 
1465 	wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1466 		    (u8 *) (key + 1), keydatalen);
1467 	if (!sm->ptk_set) {
1468 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1469 			"WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1470 			"Data");
1471 		return -1;
1472 	}
1473 
1474 	/* Decrypt key data here so that this operation does not need
1475 	 * to be implemented separately for each message type. */
1476 	if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1477 		u8 ek[32];
1478 		os_memcpy(ek, key->key_iv, 16);
1479 		os_memcpy(ek + 16, sm->ptk.kek, 16);
1480 		if (rc4_skip(ek, 32, 256, (u8 *) (key + 1), keydatalen)) {
1481 			wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1482 				"WPA: RC4 failed");
1483 			return -1;
1484 		}
1485 	} else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1486 		   ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1487 		u8 *buf;
1488 		if (keydatalen % 8) {
1489 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1490 				"WPA: Unsupported AES-WRAP len %d",
1491 				keydatalen);
1492 			return -1;
1493 		}
1494 		keydatalen -= 8; /* AES-WRAP adds 8 bytes */
1495 		buf = os_malloc(keydatalen);
1496 		if (buf == NULL) {
1497 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1498 				"WPA: No memory for AES-UNWRAP buffer");
1499 			return -1;
1500 		}
1501 		if (aes_unwrap(sm->ptk.kek, keydatalen / 8,
1502 			       (u8 *) (key + 1), buf)) {
1503 			os_free(buf);
1504 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1505 				"WPA: AES unwrap failed - "
1506 				"could not decrypt EAPOL-Key key data");
1507 			return -1;
1508 		}
1509 		os_memcpy(key + 1, buf, keydatalen);
1510 		os_free(buf);
1511 		WPA_PUT_BE16(key->key_data_length, keydatalen);
1512 	} else {
1513 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1514 			"WPA: Unsupported key_info type %d", ver);
1515 		return -1;
1516 	}
1517 	wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1518 			(u8 *) (key + 1), keydatalen);
1519 	return 0;
1520 }
1521 
1522 
1523 /**
1524  * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1525  * @sm: Pointer to WPA state machine data from wpa_sm_init()
1526  */
wpa_sm_aborted_cached(struct wpa_sm * sm)1527 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1528 {
1529 	if (sm && sm->cur_pmksa) {
1530 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1531 			"RSN: Cancelling PMKSA caching attempt");
1532 		sm->cur_pmksa = NULL;
1533 	}
1534 }
1535 
1536 
wpa_eapol_key_dump(struct wpa_sm * sm,const struct wpa_eapol_key * key)1537 static void wpa_eapol_key_dump(struct wpa_sm *sm,
1538 			       const struct wpa_eapol_key *key)
1539 {
1540 #ifndef CONFIG_NO_STDOUT_DEBUG
1541 	u16 key_info = WPA_GET_BE16(key->key_info);
1542 
1543 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "  EAPOL-Key type=%d", key->type);
1544 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1545 		"  key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1546 		key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1547 		(key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1548 		WPA_KEY_INFO_KEY_INDEX_SHIFT,
1549 		(key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1550 		key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1551 		key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1552 		key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1553 		key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1554 		key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1555 		key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1556 		key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1557 		key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1558 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1559 		"  key_length=%u key_data_length=%u",
1560 		WPA_GET_BE16(key->key_length),
1561 		WPA_GET_BE16(key->key_data_length));
1562 	wpa_hexdump(MSG_DEBUG, "  replay_counter",
1563 		    key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1564 	wpa_hexdump(MSG_DEBUG, "  key_nonce", key->key_nonce, WPA_NONCE_LEN);
1565 	wpa_hexdump(MSG_DEBUG, "  key_iv", key->key_iv, 16);
1566 	wpa_hexdump(MSG_DEBUG, "  key_rsc", key->key_rsc, 8);
1567 	wpa_hexdump(MSG_DEBUG, "  key_id (reserved)", key->key_id, 8);
1568 	wpa_hexdump(MSG_DEBUG, "  key_mic", key->key_mic, 16);
1569 #endif /* CONFIG_NO_STDOUT_DEBUG */
1570 }
1571 
1572 
1573 /**
1574  * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1575  * @sm: Pointer to WPA state machine data from wpa_sm_init()
1576  * @src_addr: Source MAC address of the EAPOL packet
1577  * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1578  * @len: Length of the EAPOL frame
1579  * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1580  *
1581  * This function is called for each received EAPOL frame. Other than EAPOL-Key
1582  * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1583  * only processing WPA and WPA2 EAPOL-Key frames.
1584  *
1585  * The received EAPOL-Key packets are validated and valid packets are replied
1586  * to. In addition, key material (PTK, GTK) is configured at the end of a
1587  * successful key handshake.
1588  */
wpa_sm_rx_eapol(struct wpa_sm * sm,const u8 * src_addr,const u8 * buf,size_t len)1589 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1590 		    const u8 *buf, size_t len)
1591 {
1592 	size_t plen, data_len, extra_len;
1593 	struct ieee802_1x_hdr *hdr;
1594 	struct wpa_eapol_key *key;
1595 	u16 key_info, ver;
1596 	u8 *tmp;
1597 	int ret = -1;
1598 	struct wpa_peerkey *peerkey = NULL;
1599 
1600 #ifdef CONFIG_IEEE80211R
1601 	sm->ft_completed = 0;
1602 #endif /* CONFIG_IEEE80211R */
1603 
1604 	if (len < sizeof(*hdr) + sizeof(*key)) {
1605 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1606 			"WPA: EAPOL frame too short to be a WPA "
1607 			"EAPOL-Key (len %lu, expecting at least %lu)",
1608 			(unsigned long) len,
1609 			(unsigned long) sizeof(*hdr) + sizeof(*key));
1610 		return 0;
1611 	}
1612 
1613 	tmp = os_malloc(len);
1614 	if (tmp == NULL)
1615 		return -1;
1616 	os_memcpy(tmp, buf, len);
1617 
1618 	hdr = (struct ieee802_1x_hdr *) tmp;
1619 	key = (struct wpa_eapol_key *) (hdr + 1);
1620 	plen = be_to_host16(hdr->length);
1621 	data_len = plen + sizeof(*hdr);
1622 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1623 		"IEEE 802.1X RX: version=%d type=%d length=%lu",
1624 		hdr->version, hdr->type, (unsigned long) plen);
1625 
1626 	if (hdr->version < EAPOL_VERSION) {
1627 		/* TODO: backwards compatibility */
1628 	}
1629 	if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1630 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1631 			"WPA: EAPOL frame (type %u) discarded, "
1632 			"not a Key frame", hdr->type);
1633 		ret = 0;
1634 		goto out;
1635 	}
1636 	if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
1637 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1638 			"WPA: EAPOL frame payload size %lu "
1639 			"invalid (frame size %lu)",
1640 			(unsigned long) plen, (unsigned long) len);
1641 		ret = 0;
1642 		goto out;
1643 	}
1644 
1645 	if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1646 	{
1647 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1648 			"WPA: EAPOL-Key type (%d) unknown, discarded",
1649 			key->type);
1650 		ret = 0;
1651 		goto out;
1652 	}
1653 	wpa_eapol_key_dump(sm, key);
1654 
1655 	eapol_sm_notify_lower_layer_success(sm->eapol, 0);
1656 	wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", tmp, len);
1657 	if (data_len < len) {
1658 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1659 			"WPA: ignoring %lu bytes after the IEEE 802.1X data",
1660 			(unsigned long) len - data_len);
1661 	}
1662 	key_info = WPA_GET_BE16(key->key_info);
1663 	ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1664 	if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1665 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1666 	    ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1667 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
1668 	    ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1669 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1670 			"WPA: Unsupported EAPOL-Key descriptor version %d",
1671 			ver);
1672 		goto out;
1673 	}
1674 
1675 #ifdef CONFIG_IEEE80211R
1676 	if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1677 		/* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1678 		if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1679 			wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1680 				"FT: AP did not use AES-128-CMAC");
1681 			goto out;
1682 		}
1683 	} else
1684 #endif /* CONFIG_IEEE80211R */
1685 #ifdef CONFIG_IEEE80211W
1686 	if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
1687 		if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1688 			wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1689 				"WPA: AP did not use the "
1690 				"negotiated AES-128-CMAC");
1691 			goto out;
1692 		}
1693 	} else
1694 #endif /* CONFIG_IEEE80211W */
1695 	if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
1696 	    ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1697 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1698 			"WPA: CCMP is used, but EAPOL-Key "
1699 			"descriptor version (%d) is not 2", ver);
1700 		if (sm->group_cipher != WPA_CIPHER_CCMP &&
1701 		    !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1702 			/* Earlier versions of IEEE 802.11i did not explicitly
1703 			 * require version 2 descriptor for all EAPOL-Key
1704 			 * packets, so allow group keys to use version 1 if
1705 			 * CCMP is not used for them. */
1706 			wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1707 				"WPA: Backwards compatibility: allow invalid "
1708 				"version for non-CCMP group keys");
1709 		} else
1710 			goto out;
1711 	}
1712 
1713 #ifdef CONFIG_PEERKEY
1714 	for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1715 		if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1716 			break;
1717 	}
1718 
1719 	if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1720 		if (!peerkey->initiator && peerkey->replay_counter_set &&
1721 		    os_memcmp(key->replay_counter, peerkey->replay_counter,
1722 			      WPA_REPLAY_COUNTER_LEN) <= 0) {
1723 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1724 				"RSN: EAPOL-Key Replay Counter did not "
1725 				"increase (STK) - dropping packet");
1726 			goto out;
1727 		} else if (peerkey->initiator) {
1728 			u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1729 			os_memcpy(_tmp, key->replay_counter,
1730 				  WPA_REPLAY_COUNTER_LEN);
1731 			inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1732 			if (os_memcmp(_tmp, peerkey->replay_counter,
1733 				      WPA_REPLAY_COUNTER_LEN) != 0) {
1734 				wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1735 					"RSN: EAPOL-Key Replay "
1736 					"Counter did not match (STK) - "
1737 					"dropping packet");
1738 				goto out;
1739 			}
1740 		}
1741 	}
1742 
1743 	if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1744 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1745 			"RSN: Ack bit in key_info from STK peer");
1746 		goto out;
1747 	}
1748 #endif /* CONFIG_PEERKEY */
1749 
1750 	if (!peerkey && sm->rx_replay_counter_set &&
1751 	    os_memcmp(key->replay_counter, sm->rx_replay_counter,
1752 		      WPA_REPLAY_COUNTER_LEN) <= 0) {
1753 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1754 			"WPA: EAPOL-Key Replay Counter did not increase - "
1755 			"dropping packet");
1756 		goto out;
1757 	}
1758 
1759 	if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1760 #ifdef CONFIG_PEERKEY
1761 	    && (peerkey == NULL || !peerkey->initiator)
1762 #endif /* CONFIG_PEERKEY */
1763 		) {
1764 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1765 			"WPA: No Ack bit in key_info");
1766 		goto out;
1767 	}
1768 
1769 	if (key_info & WPA_KEY_INFO_REQUEST) {
1770 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1771 			"WPA: EAPOL-Key with Request bit - dropped");
1772 		goto out;
1773 	}
1774 
1775 	if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
1776 	    wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
1777 		goto out;
1778 
1779 #ifdef CONFIG_PEERKEY
1780 	if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
1781 	    peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp, data_len))
1782 		goto out;
1783 #endif /* CONFIG_PEERKEY */
1784 
1785 	extra_len = data_len - sizeof(*hdr) - sizeof(*key);
1786 
1787 	if (WPA_GET_BE16(key->key_data_length) > extra_len) {
1788 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1789 			"frame - key_data overflow (%d > %lu)",
1790 			WPA_GET_BE16(key->key_data_length),
1791 			(unsigned long) extra_len);
1792 		goto out;
1793 	}
1794 	extra_len = WPA_GET_BE16(key->key_data_length);
1795 
1796 	if (sm->proto == WPA_PROTO_RSN &&
1797 	    (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1798 		if (wpa_supplicant_decrypt_key_data(sm, key, ver))
1799 			goto out;
1800 		extra_len = WPA_GET_BE16(key->key_data_length);
1801 	}
1802 
1803 	if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1804 		if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
1805 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1806 				"WPA: Ignored EAPOL-Key (Pairwise) with "
1807 				"non-zero key index");
1808 			goto out;
1809 		}
1810 		if (peerkey) {
1811 			/* PeerKey 4-Way Handshake */
1812 			peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver);
1813 		} else if (key_info & WPA_KEY_INFO_MIC) {
1814 			/* 3/4 4-Way Handshake */
1815 			wpa_supplicant_process_3_of_4(sm, key, ver);
1816 		} else {
1817 			/* 1/4 4-Way Handshake */
1818 			wpa_supplicant_process_1_of_4(sm, src_addr, key,
1819 						      ver);
1820 		}
1821 	} else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1822 		/* PeerKey SMK Handshake */
1823 		peerkey_rx_eapol_smk(sm, src_addr, key, extra_len, key_info,
1824 				     ver);
1825 	} else {
1826 		if (key_info & WPA_KEY_INFO_MIC) {
1827 			/* 1/2 Group Key Handshake */
1828 			wpa_supplicant_process_1_of_2(sm, src_addr, key,
1829 						      extra_len, ver);
1830 		} else {
1831 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1832 				"WPA: EAPOL-Key (Group) without Mic bit - "
1833 				"dropped");
1834 		}
1835 	}
1836 
1837 	ret = 1;
1838 
1839 out:
1840 	os_free(tmp);
1841 	return ret;
1842 }
1843 
1844 
1845 #ifdef CONFIG_CTRL_IFACE
wpa_cipher_bits(int cipher)1846 static int wpa_cipher_bits(int cipher)
1847 {
1848 	switch (cipher) {
1849 	case WPA_CIPHER_CCMP:
1850 		return 128;
1851 	case WPA_CIPHER_TKIP:
1852 		return 256;
1853 	case WPA_CIPHER_WEP104:
1854 		return 104;
1855 	case WPA_CIPHER_WEP40:
1856 		return 40;
1857 	default:
1858 		return 0;
1859 	}
1860 }
1861 
1862 
wpa_key_mgmt_suite(struct wpa_sm * sm)1863 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
1864 {
1865 	switch (sm->key_mgmt) {
1866 	case WPA_KEY_MGMT_IEEE8021X:
1867 		return (sm->proto == WPA_PROTO_RSN ?
1868 			RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
1869 			WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
1870 	case WPA_KEY_MGMT_PSK:
1871 		return (sm->proto == WPA_PROTO_RSN ?
1872 			RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
1873 			WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
1874 #ifdef CONFIG_IEEE80211R
1875 	case WPA_KEY_MGMT_FT_IEEE8021X:
1876 		return RSN_AUTH_KEY_MGMT_FT_802_1X;
1877 	case WPA_KEY_MGMT_FT_PSK:
1878 		return RSN_AUTH_KEY_MGMT_FT_PSK;
1879 #endif /* CONFIG_IEEE80211R */
1880 #ifdef CONFIG_IEEE80211W
1881 	case WPA_KEY_MGMT_IEEE8021X_SHA256:
1882 		return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
1883 	case WPA_KEY_MGMT_PSK_SHA256:
1884 		return RSN_AUTH_KEY_MGMT_PSK_SHA256;
1885 #endif /* CONFIG_IEEE80211W */
1886 	case WPA_KEY_MGMT_WPA_NONE:
1887 		return WPA_AUTH_KEY_MGMT_NONE;
1888 	default:
1889 		return 0;
1890 	}
1891 }
1892 
1893 
wpa_cipher_suite(struct wpa_sm * sm,int cipher)1894 static u32 wpa_cipher_suite(struct wpa_sm *sm, int cipher)
1895 {
1896 	switch (cipher) {
1897 	case WPA_CIPHER_CCMP:
1898 		return (sm->proto == WPA_PROTO_RSN ?
1899 			RSN_CIPHER_SUITE_CCMP : WPA_CIPHER_SUITE_CCMP);
1900 	case WPA_CIPHER_TKIP:
1901 		return (sm->proto == WPA_PROTO_RSN ?
1902 			RSN_CIPHER_SUITE_TKIP : WPA_CIPHER_SUITE_TKIP);
1903 	case WPA_CIPHER_WEP104:
1904 		return (sm->proto == WPA_PROTO_RSN ?
1905 			RSN_CIPHER_SUITE_WEP104 : WPA_CIPHER_SUITE_WEP104);
1906 	case WPA_CIPHER_WEP40:
1907 		return (sm->proto == WPA_PROTO_RSN ?
1908 			RSN_CIPHER_SUITE_WEP40 : WPA_CIPHER_SUITE_WEP40);
1909 	case WPA_CIPHER_NONE:
1910 		return (sm->proto == WPA_PROTO_RSN ?
1911 			RSN_CIPHER_SUITE_NONE : WPA_CIPHER_SUITE_NONE);
1912 	default:
1913 		return 0;
1914 	}
1915 }
1916 
1917 
1918 #define RSN_SUITE "%02x-%02x-%02x-%d"
1919 #define RSN_SUITE_ARG(s) \
1920 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
1921 
1922 /**
1923  * wpa_sm_get_mib - Dump text list of MIB entries
1924  * @sm: Pointer to WPA state machine data from wpa_sm_init()
1925  * @buf: Buffer for the list
1926  * @buflen: Length of the buffer
1927  * Returns: Number of bytes written to buffer
1928  *
1929  * This function is used fetch dot11 MIB variables.
1930  */
wpa_sm_get_mib(struct wpa_sm * sm,char * buf,size_t buflen)1931 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
1932 {
1933 	char pmkid_txt[PMKID_LEN * 2 + 1];
1934 	int rsna, ret;
1935 	size_t len;
1936 
1937 	if (sm->cur_pmksa) {
1938 		wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
1939 				 sm->cur_pmksa->pmkid, PMKID_LEN);
1940 	} else
1941 		pmkid_txt[0] = '\0';
1942 
1943 	if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
1944 	     wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
1945 	    sm->proto == WPA_PROTO_RSN)
1946 		rsna = 1;
1947 	else
1948 		rsna = 0;
1949 
1950 	ret = os_snprintf(buf, buflen,
1951 			  "dot11RSNAOptionImplemented=TRUE\n"
1952 			  "dot11RSNAPreauthenticationImplemented=TRUE\n"
1953 			  "dot11RSNAEnabled=%s\n"
1954 			  "dot11RSNAPreauthenticationEnabled=%s\n"
1955 			  "dot11RSNAConfigVersion=%d\n"
1956 			  "dot11RSNAConfigPairwiseKeysSupported=5\n"
1957 			  "dot11RSNAConfigGroupCipherSize=%d\n"
1958 			  "dot11RSNAConfigPMKLifetime=%d\n"
1959 			  "dot11RSNAConfigPMKReauthThreshold=%d\n"
1960 			  "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
1961 			  "dot11RSNAConfigSATimeout=%d\n",
1962 			  rsna ? "TRUE" : "FALSE",
1963 			  rsna ? "TRUE" : "FALSE",
1964 			  RSN_VERSION,
1965 			  wpa_cipher_bits(sm->group_cipher),
1966 			  sm->dot11RSNAConfigPMKLifetime,
1967 			  sm->dot11RSNAConfigPMKReauthThreshold,
1968 			  sm->dot11RSNAConfigSATimeout);
1969 	if (ret < 0 || (size_t) ret >= buflen)
1970 		return 0;
1971 	len = ret;
1972 
1973 	ret = os_snprintf(
1974 		buf + len, buflen - len,
1975 		"dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
1976 		"dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
1977 		"dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
1978 		"dot11RSNAPMKIDUsed=%s\n"
1979 		"dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
1980 		"dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
1981 		"dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
1982 		"dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
1983 		"dot11RSNA4WayHandshakeFailures=%u\n",
1984 		RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1985 		RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->pairwise_cipher)),
1986 		RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
1987 		pmkid_txt,
1988 		RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1989 		RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->pairwise_cipher)),
1990 		RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
1991 		sm->dot11RSNA4WayHandshakeFailures);
1992 	if (ret >= 0 && (size_t) ret < buflen)
1993 		len += ret;
1994 
1995 	return (int) len;
1996 }
1997 #endif /* CONFIG_CTRL_IFACE */
1998 
1999 
wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry * entry,void * ctx,int replace)2000 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
2001 				 void *ctx, int replace)
2002 {
2003 	struct wpa_sm *sm = ctx;
2004 
2005 	if (sm->cur_pmksa == entry ||
2006 	    (sm->pmk_len == entry->pmk_len &&
2007 	     os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2008 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2009 			"RSN: removed current PMKSA entry");
2010 		sm->cur_pmksa = NULL;
2011 
2012 		if (replace) {
2013 			/* A new entry is being added, so no need to
2014 			 * deauthenticate in this case. This happens when EAP
2015 			 * authentication is completed again (reauth or failed
2016 			 * PMKSA caching attempt). */
2017 			return;
2018 		}
2019 
2020 		os_memset(sm->pmk, 0, sizeof(sm->pmk));
2021 		wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2022 	}
2023 }
2024 
2025 
2026 /**
2027  * wpa_sm_init - Initialize WPA state machine
2028  * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2029  * Returns: Pointer to the allocated WPA state machine data
2030  *
2031  * This function is used to allocate a new WPA state machine and the returned
2032  * value is passed to all WPA state machine calls.
2033  */
wpa_sm_init(struct wpa_sm_ctx * ctx)2034 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2035 {
2036 	struct wpa_sm *sm;
2037 
2038 	sm = os_zalloc(sizeof(*sm));
2039 	if (sm == NULL)
2040 		return NULL;
2041 	dl_list_init(&sm->pmksa_candidates);
2042 	sm->renew_snonce = 1;
2043 	sm->ctx = ctx;
2044 
2045 	sm->dot11RSNAConfigPMKLifetime = 43200;
2046 	sm->dot11RSNAConfigPMKReauthThreshold = 70;
2047 	sm->dot11RSNAConfigSATimeout = 60;
2048 
2049 	sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2050 	if (sm->pmksa == NULL) {
2051 		wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2052 			"RSN: PMKSA cache initialization failed");
2053 		os_free(sm);
2054 		return NULL;
2055 	}
2056 
2057 	return sm;
2058 }
2059 
2060 
2061 /**
2062  * wpa_sm_deinit - Deinitialize WPA state machine
2063  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2064  */
wpa_sm_deinit(struct wpa_sm * sm)2065 void wpa_sm_deinit(struct wpa_sm *sm)
2066 {
2067 	if (sm == NULL)
2068 		return;
2069 	pmksa_cache_deinit(sm->pmksa);
2070 	eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2071 	eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2072 	os_free(sm->assoc_wpa_ie);
2073 	os_free(sm->ap_wpa_ie);
2074 	os_free(sm->ap_rsn_ie);
2075 	os_free(sm->ctx);
2076 	peerkey_deinit(sm);
2077 #ifdef CONFIG_IEEE80211R
2078 	os_free(sm->assoc_resp_ies);
2079 #endif /* CONFIG_IEEE80211R */
2080 	os_free(sm);
2081 }
2082 
2083 
2084 /**
2085  * wpa_sm_notify_assoc - Notify WPA state machine about association
2086  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2087  * @bssid: The BSSID of the new association
2088  *
2089  * This function is called to let WPA state machine know that the connection
2090  * was established.
2091  */
wpa_sm_notify_assoc(struct wpa_sm * sm,const u8 * bssid)2092 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2093 {
2094 	int clear_ptk = 1;
2095 
2096 	if (sm == NULL)
2097 		return;
2098 
2099 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2100 		"WPA: Association event - clear replay counter");
2101 	os_memcpy(sm->bssid, bssid, ETH_ALEN);
2102 	os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2103 	sm->rx_replay_counter_set = 0;
2104 	sm->renew_snonce = 1;
2105 	if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2106 		rsn_preauth_deinit(sm);
2107 
2108 #ifdef CONFIG_IEEE80211R
2109 	if (wpa_ft_is_completed(sm)) {
2110 		/*
2111 		 * Clear portValid to kick EAPOL state machine to re-enter
2112 		 * AUTHENTICATED state to get the EAPOL port Authorized.
2113 		 */
2114 		eapol_sm_notify_portValid(sm->eapol, FALSE);
2115 		wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2116 
2117 		/* Prepare for the next transition */
2118 		wpa_ft_prepare_auth_request(sm, NULL);
2119 
2120 		clear_ptk = 0;
2121 	}
2122 #endif /* CONFIG_IEEE80211R */
2123 
2124 	if (clear_ptk) {
2125 		/*
2126 		 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2127 		 * this is not part of a Fast BSS Transition.
2128 		 */
2129 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2130 		sm->ptk_set = 0;
2131 		sm->tptk_set = 0;
2132 	}
2133 
2134 #ifdef CONFIG_TDLS
2135 	wpa_tdls_assoc(sm);
2136 #endif /* CONFIG_TDLS */
2137 }
2138 
2139 
2140 /**
2141  * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2142  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2143  *
2144  * This function is called to let WPA state machine know that the connection
2145  * was lost. This will abort any existing pre-authentication session.
2146  */
wpa_sm_notify_disassoc(struct wpa_sm * sm)2147 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2148 {
2149 	rsn_preauth_deinit(sm);
2150 	if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2151 		sm->dot11RSNA4WayHandshakeFailures++;
2152 #ifdef CONFIG_TDLS
2153 	wpa_tdls_disassoc(sm);
2154 #endif /* CONFIG_TDLS */
2155 }
2156 
2157 
2158 /**
2159  * wpa_sm_set_pmk - Set PMK
2160  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2161  * @pmk: The new PMK
2162  * @pmk_len: The length of the new PMK in bytes
2163  *
2164  * Configure the PMK for WPA state machine.
2165  */
wpa_sm_set_pmk(struct wpa_sm * sm,const u8 * pmk,size_t pmk_len)2166 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len)
2167 {
2168 	if (sm == NULL)
2169 		return;
2170 
2171 	sm->pmk_len = pmk_len;
2172 	os_memcpy(sm->pmk, pmk, pmk_len);
2173 
2174 #ifdef CONFIG_IEEE80211R
2175 	/* Set XXKey to be PSK for FT key derivation */
2176 	sm->xxkey_len = pmk_len;
2177 	os_memcpy(sm->xxkey, pmk, pmk_len);
2178 #endif /* CONFIG_IEEE80211R */
2179 }
2180 
2181 
2182 /**
2183  * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2184  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2185  *
2186  * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2187  * will be cleared.
2188  */
wpa_sm_set_pmk_from_pmksa(struct wpa_sm * sm)2189 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2190 {
2191 	if (sm == NULL)
2192 		return;
2193 
2194 	if (sm->cur_pmksa) {
2195 		sm->pmk_len = sm->cur_pmksa->pmk_len;
2196 		os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2197 	} else {
2198 		sm->pmk_len = PMK_LEN;
2199 		os_memset(sm->pmk, 0, PMK_LEN);
2200 	}
2201 }
2202 
2203 
2204 /**
2205  * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2206  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2207  * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2208  */
wpa_sm_set_fast_reauth(struct wpa_sm * sm,int fast_reauth)2209 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2210 {
2211 	if (sm)
2212 		sm->fast_reauth = fast_reauth;
2213 }
2214 
2215 
2216 /**
2217  * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2218  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2219  * @scard_ctx: Context pointer for smartcard related callback functions
2220  */
wpa_sm_set_scard_ctx(struct wpa_sm * sm,void * scard_ctx)2221 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2222 {
2223 	if (sm == NULL)
2224 		return;
2225 	sm->scard_ctx = scard_ctx;
2226 	if (sm->preauth_eapol)
2227 		eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2228 }
2229 
2230 
2231 /**
2232  * wpa_sm_set_config - Notification of current configration change
2233  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2234  * @config: Pointer to current network configuration
2235  *
2236  * Notify WPA state machine that configuration has changed. config will be
2237  * stored as a backpointer to network configuration. This can be %NULL to clear
2238  * the stored pointed.
2239  */
wpa_sm_set_config(struct wpa_sm * sm,struct rsn_supp_config * config)2240 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2241 {
2242 	if (!sm)
2243 		return;
2244 
2245 	if (config) {
2246 		sm->network_ctx = config->network_ctx;
2247 		sm->peerkey_enabled = config->peerkey_enabled;
2248 		sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2249 		sm->proactive_key_caching = config->proactive_key_caching;
2250 		sm->eap_workaround = config->eap_workaround;
2251 		sm->eap_conf_ctx = config->eap_conf_ctx;
2252 		if (config->ssid) {
2253 			os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2254 			sm->ssid_len = config->ssid_len;
2255 		} else
2256 			sm->ssid_len = 0;
2257 		sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
2258 	} else {
2259 		sm->network_ctx = NULL;
2260 		sm->peerkey_enabled = 0;
2261 		sm->allowed_pairwise_cipher = 0;
2262 		sm->proactive_key_caching = 0;
2263 		sm->eap_workaround = 0;
2264 		sm->eap_conf_ctx = NULL;
2265 		sm->ssid_len = 0;
2266 		sm->wpa_ptk_rekey = 0;
2267 	}
2268 	if (config == NULL || config->network_ctx != sm->network_ctx)
2269 		pmksa_cache_notify_reconfig(sm->pmksa);
2270 }
2271 
2272 
2273 /**
2274  * wpa_sm_set_own_addr - Set own MAC address
2275  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2276  * @addr: Own MAC address
2277  */
wpa_sm_set_own_addr(struct wpa_sm * sm,const u8 * addr)2278 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2279 {
2280 	if (sm)
2281 		os_memcpy(sm->own_addr, addr, ETH_ALEN);
2282 }
2283 
2284 
2285 /**
2286  * wpa_sm_set_ifname - Set network interface name
2287  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2288  * @ifname: Interface name
2289  * @bridge_ifname: Optional bridge interface name (for pre-auth)
2290  */
wpa_sm_set_ifname(struct wpa_sm * sm,const char * ifname,const char * bridge_ifname)2291 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2292 		       const char *bridge_ifname)
2293 {
2294 	if (sm) {
2295 		sm->ifname = ifname;
2296 		sm->bridge_ifname = bridge_ifname;
2297 	}
2298 }
2299 
2300 
2301 /**
2302  * wpa_sm_set_eapol - Set EAPOL state machine pointer
2303  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2304  * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2305  */
wpa_sm_set_eapol(struct wpa_sm * sm,struct eapol_sm * eapol)2306 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2307 {
2308 	if (sm)
2309 		sm->eapol = eapol;
2310 }
2311 
2312 
2313 /**
2314  * wpa_sm_set_param - Set WPA state machine parameters
2315  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2316  * @param: Parameter field
2317  * @value: Parameter value
2318  * Returns: 0 on success, -1 on failure
2319  */
wpa_sm_set_param(struct wpa_sm * sm,enum wpa_sm_conf_params param,unsigned int value)2320 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2321 		     unsigned int value)
2322 {
2323 	int ret = 0;
2324 
2325 	if (sm == NULL)
2326 		return -1;
2327 
2328 	switch (param) {
2329 	case RSNA_PMK_LIFETIME:
2330 		if (value > 0)
2331 			sm->dot11RSNAConfigPMKLifetime = value;
2332 		else
2333 			ret = -1;
2334 		break;
2335 	case RSNA_PMK_REAUTH_THRESHOLD:
2336 		if (value > 0 && value <= 100)
2337 			sm->dot11RSNAConfigPMKReauthThreshold = value;
2338 		else
2339 			ret = -1;
2340 		break;
2341 	case RSNA_SA_TIMEOUT:
2342 		if (value > 0)
2343 			sm->dot11RSNAConfigSATimeout = value;
2344 		else
2345 			ret = -1;
2346 		break;
2347 	case WPA_PARAM_PROTO:
2348 		sm->proto = value;
2349 		break;
2350 	case WPA_PARAM_PAIRWISE:
2351 		sm->pairwise_cipher = value;
2352 		break;
2353 	case WPA_PARAM_GROUP:
2354 		sm->group_cipher = value;
2355 		break;
2356 	case WPA_PARAM_KEY_MGMT:
2357 		sm->key_mgmt = value;
2358 		break;
2359 #ifdef CONFIG_IEEE80211W
2360 	case WPA_PARAM_MGMT_GROUP:
2361 		sm->mgmt_group_cipher = value;
2362 		break;
2363 #endif /* CONFIG_IEEE80211W */
2364 	case WPA_PARAM_RSN_ENABLED:
2365 		sm->rsn_enabled = value;
2366 		break;
2367 	case WPA_PARAM_MFP:
2368 		sm->mfp = value;
2369 		break;
2370 	default:
2371 		break;
2372 	}
2373 
2374 	return ret;
2375 }
2376 
2377 
2378 /**
2379  * wpa_sm_get_param - Get WPA state machine parameters
2380  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2381  * @param: Parameter field
2382  * Returns: Parameter value
2383  */
wpa_sm_get_param(struct wpa_sm * sm,enum wpa_sm_conf_params param)2384 unsigned int wpa_sm_get_param(struct wpa_sm *sm, enum wpa_sm_conf_params param)
2385 {
2386 	if (sm == NULL)
2387 		return 0;
2388 
2389 	switch (param) {
2390 	case RSNA_PMK_LIFETIME:
2391 		return sm->dot11RSNAConfigPMKLifetime;
2392 	case RSNA_PMK_REAUTH_THRESHOLD:
2393 		return sm->dot11RSNAConfigPMKReauthThreshold;
2394 	case RSNA_SA_TIMEOUT:
2395 		return sm->dot11RSNAConfigSATimeout;
2396 	case WPA_PARAM_PROTO:
2397 		return sm->proto;
2398 	case WPA_PARAM_PAIRWISE:
2399 		return sm->pairwise_cipher;
2400 	case WPA_PARAM_GROUP:
2401 		return sm->group_cipher;
2402 	case WPA_PARAM_KEY_MGMT:
2403 		return sm->key_mgmt;
2404 #ifdef CONFIG_IEEE80211W
2405 	case WPA_PARAM_MGMT_GROUP:
2406 		return sm->mgmt_group_cipher;
2407 #endif /* CONFIG_IEEE80211W */
2408 	case WPA_PARAM_RSN_ENABLED:
2409 		return sm->rsn_enabled;
2410 	default:
2411 		return 0;
2412 	}
2413 }
2414 
2415 
2416 /**
2417  * wpa_sm_get_status - Get WPA state machine
2418  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2419  * @buf: Buffer for status information
2420  * @buflen: Maximum buffer length
2421  * @verbose: Whether to include verbose status information
2422  * Returns: Number of bytes written to buf.
2423  *
2424  * Query WPA state machine for status information. This function fills in
2425  * a text area with current status information. If the buffer (buf) is not
2426  * large enough, status information will be truncated to fit the buffer.
2427  */
wpa_sm_get_status(struct wpa_sm * sm,char * buf,size_t buflen,int verbose)2428 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2429 		      int verbose)
2430 {
2431 	char *pos = buf, *end = buf + buflen;
2432 	int ret;
2433 
2434 	ret = os_snprintf(pos, end - pos,
2435 			  "pairwise_cipher=%s\n"
2436 			  "group_cipher=%s\n"
2437 			  "key_mgmt=%s\n",
2438 			  wpa_cipher_txt(sm->pairwise_cipher),
2439 			  wpa_cipher_txt(sm->group_cipher),
2440 			  wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2441 	if (ret < 0 || ret >= end - pos)
2442 		return pos - buf;
2443 	pos += ret;
2444 	return pos - buf;
2445 }
2446 
2447 
2448 /**
2449  * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2450  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2451  * @wpa_ie: Pointer to buffer for WPA/RSN IE
2452  * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2453  * Returns: 0 on success, -1 on failure
2454  */
wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm * sm,u8 * wpa_ie,size_t * wpa_ie_len)2455 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2456 				    size_t *wpa_ie_len)
2457 {
2458 	int res;
2459 
2460 	if (sm == NULL)
2461 		return -1;
2462 
2463 	res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2464 	if (res < 0)
2465 		return -1;
2466 	*wpa_ie_len = res;
2467 
2468 	wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2469 		    wpa_ie, *wpa_ie_len);
2470 
2471 	if (sm->assoc_wpa_ie == NULL) {
2472 		/*
2473 		 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2474 		 * the correct version of the IE even if PMKSA caching is
2475 		 * aborted (which would remove PMKID from IE generation).
2476 		 */
2477 		sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2478 		if (sm->assoc_wpa_ie == NULL)
2479 			return -1;
2480 
2481 		os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2482 		sm->assoc_wpa_ie_len = *wpa_ie_len;
2483 	}
2484 
2485 	return 0;
2486 }
2487 
2488 
2489 /**
2490  * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2491  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2492  * @ie: Pointer to IE data (starting from id)
2493  * @len: IE length
2494  * Returns: 0 on success, -1 on failure
2495  *
2496  * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2497  * Request frame. The IE will be used to override the default value generated
2498  * with wpa_sm_set_assoc_wpa_ie_default().
2499  */
wpa_sm_set_assoc_wpa_ie(struct wpa_sm * sm,const u8 * ie,size_t len)2500 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2501 {
2502 	if (sm == NULL)
2503 		return -1;
2504 
2505 	os_free(sm->assoc_wpa_ie);
2506 	if (ie == NULL || len == 0) {
2507 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2508 			"WPA: clearing own WPA/RSN IE");
2509 		sm->assoc_wpa_ie = NULL;
2510 		sm->assoc_wpa_ie_len = 0;
2511 	} else {
2512 		wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2513 		sm->assoc_wpa_ie = os_malloc(len);
2514 		if (sm->assoc_wpa_ie == NULL)
2515 			return -1;
2516 
2517 		os_memcpy(sm->assoc_wpa_ie, ie, len);
2518 		sm->assoc_wpa_ie_len = len;
2519 	}
2520 
2521 	return 0;
2522 }
2523 
2524 
2525 /**
2526  * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2527  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2528  * @ie: Pointer to IE data (starting from id)
2529  * @len: IE length
2530  * Returns: 0 on success, -1 on failure
2531  *
2532  * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2533  * frame.
2534  */
wpa_sm_set_ap_wpa_ie(struct wpa_sm * sm,const u8 * ie,size_t len)2535 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2536 {
2537 	if (sm == NULL)
2538 		return -1;
2539 
2540 	os_free(sm->ap_wpa_ie);
2541 	if (ie == NULL || len == 0) {
2542 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2543 			"WPA: clearing AP WPA IE");
2544 		sm->ap_wpa_ie = NULL;
2545 		sm->ap_wpa_ie_len = 0;
2546 	} else {
2547 		wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2548 		sm->ap_wpa_ie = os_malloc(len);
2549 		if (sm->ap_wpa_ie == NULL)
2550 			return -1;
2551 
2552 		os_memcpy(sm->ap_wpa_ie, ie, len);
2553 		sm->ap_wpa_ie_len = len;
2554 	}
2555 
2556 	return 0;
2557 }
2558 
2559 
2560 /**
2561  * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2562  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2563  * @ie: Pointer to IE data (starting from id)
2564  * @len: IE length
2565  * Returns: 0 on success, -1 on failure
2566  *
2567  * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2568  * frame.
2569  */
wpa_sm_set_ap_rsn_ie(struct wpa_sm * sm,const u8 * ie,size_t len)2570 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2571 {
2572 	if (sm == NULL)
2573 		return -1;
2574 
2575 	os_free(sm->ap_rsn_ie);
2576 	if (ie == NULL || len == 0) {
2577 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2578 			"WPA: clearing AP RSN IE");
2579 		sm->ap_rsn_ie = NULL;
2580 		sm->ap_rsn_ie_len = 0;
2581 	} else {
2582 		wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2583 		sm->ap_rsn_ie = os_malloc(len);
2584 		if (sm->ap_rsn_ie == NULL)
2585 			return -1;
2586 
2587 		os_memcpy(sm->ap_rsn_ie, ie, len);
2588 		sm->ap_rsn_ie_len = len;
2589 	}
2590 
2591 	return 0;
2592 }
2593 
2594 
2595 /**
2596  * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2597  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2598  * @data: Pointer to data area for parsing results
2599  * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2600  *
2601  * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2602  * parsed data into data.
2603  */
wpa_sm_parse_own_wpa_ie(struct wpa_sm * sm,struct wpa_ie_data * data)2604 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2605 {
2606 	if (sm == NULL)
2607 		return -1;
2608 
2609 	if (sm->assoc_wpa_ie == NULL) {
2610 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2611 			"WPA: No WPA/RSN IE available from association info");
2612 		return -1;
2613 	}
2614 	if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2615 		return -2;
2616 	return 0;
2617 }
2618 
2619 
wpa_sm_pmksa_cache_list(struct wpa_sm * sm,char * buf,size_t len)2620 int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2621 {
2622 #ifndef CONFIG_NO_WPA2
2623 	return pmksa_cache_list(sm->pmksa, buf, len);
2624 #else /* CONFIG_NO_WPA2 */
2625 	return -1;
2626 #endif /* CONFIG_NO_WPA2 */
2627 }
2628 
2629 
wpa_sm_drop_sa(struct wpa_sm * sm)2630 void wpa_sm_drop_sa(struct wpa_sm *sm)
2631 {
2632 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
2633 	sm->ptk_set = 0;
2634 	sm->tptk_set = 0;
2635 	os_memset(sm->pmk, 0, sizeof(sm->pmk));
2636 	os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2637 	os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2638 }
2639 
2640 
wpa_sm_has_ptk(struct wpa_sm * sm)2641 int wpa_sm_has_ptk(struct wpa_sm *sm)
2642 {
2643 	if (sm == NULL)
2644 		return 0;
2645 	return sm->ptk_set;
2646 }
2647