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