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