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