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