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