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