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