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