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