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