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