Remove src/crypto from default include path
[libeap.git] / src / rsn_supp / wpa.c
index bedc0c4..e84771f 100644 (file)
 #include "includes.h"
 
 #include "common.h"
-#include "rc4.h"
-#include "aes_wrap.h"
+#include "crypto/aes_wrap.h"
+#include "crypto/crypto.h"
+#include "common/ieee802_11_defs.h"
+#include "eapol_supp/eapol_supp_sm.h"
 #include "wpa.h"
 #include "eloop.h"
-#include "eapol_supp/eapol_supp_sm.h"
 #include "preauth.h"
 #include "pmksa_cache.h"
 #include "wpa_i.h"
 #include "wpa_ie.h"
 #include "peerkey.h"
-#include "ieee802_11_defs.h"
-
-
-/**
- * wpa_cipher_txt - Convert cipher suite to a text string
- * @cipher: Cipher suite (WPA_CIPHER_* enum)
- * Returns: Pointer to a text string of the cipher suite name
- */
-static const char * wpa_cipher_txt(int cipher)
-{
-       switch (cipher) {
-       case WPA_CIPHER_NONE:
-               return "NONE";
-       case WPA_CIPHER_WEP40:
-               return "WEP-40";
-       case WPA_CIPHER_WEP104:
-               return "WEP-104";
-       case WPA_CIPHER_TKIP:
-               return "TKIP";
-       case WPA_CIPHER_CCMP:
-               return "CCMP";
-       default:
-               return "UNKNOWN";
-       }
-}
-
-
-/**
- * wpa_key_mgmt_txt - Convert key management suite to a text string
- * @key_mgmt: Key management suite (WPA_KEY_MGMT_* enum)
- * @proto: WPA/WPA2 version (WPA_PROTO_*)
- * Returns: Pointer to a text string of the key management suite name
- */
-static const char * wpa_key_mgmt_txt(int key_mgmt, int proto)
-{
-       switch (key_mgmt) {
-       case WPA_KEY_MGMT_IEEE8021X:
-               return proto == WPA_PROTO_RSN ?
-                       "WPA2/IEEE 802.1X/EAP" : "WPA/IEEE 802.1X/EAP";
-       case WPA_KEY_MGMT_PSK:
-               return proto == WPA_PROTO_RSN ?
-                       "WPA2-PSK" : "WPA-PSK";
-       case WPA_KEY_MGMT_NONE:
-               return "NONE";
-       case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
-               return "IEEE 802.1X (no WPA)";
-#ifdef CONFIG_IEEE80211R
-       case WPA_KEY_MGMT_FT_IEEE8021X:
-               return "FT-EAP";
-       case WPA_KEY_MGMT_FT_PSK:
-               return "FT-PSK";
-#endif /* CONFIG_IEEE80211R */
-#ifdef CONFIG_IEEE80211W
-       case WPA_KEY_MGMT_IEEE8021X_SHA256:
-               return "WPA2-EAP-SHA256";
-       case WPA_KEY_MGMT_PSK_SHA256:
-               return "WPA2-PSK-SHA256";
-#endif /* CONFIG_IEEE80211W */
-       default:
-               return "UNKNOWN";
-       }
-}
 
 
 /**
@@ -119,11 +58,16 @@ void wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck,
                                   MAC2STR(dest));
                }
        }
-       if (key_mic)
-               wpa_eapol_key_mic(kck, ver, msg, msg_len, key_mic);
+       if (key_mic &&
+           wpa_eapol_key_mic(kck, ver, msg, msg_len, key_mic)) {
+               wpa_printf(MSG_ERROR, "WPA: Failed to generate EAPOL-Key "
+                          "version %d MIC", ver);
+               goto out;
+       }
        wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
        wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
        eapol_sm_notify_tx_eapol_key(sm->eapol);
+out:
        os_free(msg);
 }
 
@@ -410,13 +354,13 @@ static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
 #endif /* CONFIG_NO_WPA2 */
 
        if (wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid))
-               return;
+               goto failed;
 
        if (sm->renew_snonce) {
                if (os_get_random(sm->snonce, WPA_NONCE_LEN)) {
                        wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
                                "WPA: Failed to get random data for SNonce");
-                       return;
+                       goto failed;
                }
                sm->renew_snonce = 0;
                wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
@@ -436,9 +380,13 @@ static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
        if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
                                       sm->assoc_wpa_ie, sm->assoc_wpa_ie_len,
                                       ptk))
-               return;
+               goto failed;
 
        os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
+       return;
+
+failed:
+       wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
 }
 
 
@@ -541,7 +489,8 @@ static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
        if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
                           (u8 *) sm->ptk.tk1, keylen) < 0) {
                wpa_printf(MSG_WARNING, "WPA: Failed to set PTK to the "
-                          "driver.");
+                          "driver (alg=%d keylen=%d bssid=" MACSTR ")",
+                          alg, keylen, MAC2STR(sm->bssid));
                return -1;
        }
 
@@ -651,7 +600,8 @@ static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
                                  gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
                                  _gtk, gd->gtk_len) < 0) {
                wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to "
-                          "the driver.");
+                          "the driver (alg=%d keylen=%d keyidx=%d)",
+                          gd->alg, gd->gtk_len, gd->keyidx);
                return -1;
        }
 
@@ -948,30 +898,30 @@ static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
        wpa_supplicant_parse_ies(pos, len, &ie);
        if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
                wpa_printf(MSG_WARNING, "WPA: GTK IE in unencrypted key data");
-               return;
+               goto failed;
        }
 #ifdef CONFIG_IEEE80211W
        if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
                wpa_printf(MSG_WARNING, "WPA: IGTK KDE in unencrypted key "
                           "data");
-               return;
+               goto failed;
        }
 
        if (ie.igtk && ie.igtk_len != sizeof(struct wpa_igtk_kde)) {
                wpa_printf(MSG_WARNING, "WPA: Invalid IGTK KDE length %lu",
                           (unsigned long) ie.igtk_len);
-               return;
+               goto failed;
        }
 #endif /* CONFIG_IEEE80211W */
 
        if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
-               return;
+               goto failed;
 
        if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
                wpa_printf(MSG_WARNING, "WPA: ANonce from message 1 of 4-Way "
                           "Handshake differs from 3 of 4-Way Handshake - drop"
                           " packet (src=" MACSTR ")", MAC2STR(sm->bssid));
-               return;
+               goto failed;
        }
 
        keylen = WPA_GET_BE16(key->key_length);
@@ -981,7 +931,7 @@ static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
                        wpa_printf(MSG_WARNING, "WPA: Invalid CCMP key length "
                                   "%d (src=" MACSTR ")",
                                   keylen, MAC2STR(sm->bssid));
-                       return;
+                       goto failed;
                }
                break;
        case WPA_CIPHER_TKIP:
@@ -989,14 +939,15 @@ static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
                        wpa_printf(MSG_WARNING, "WPA: Invalid TKIP key length "
                                   "%d (src=" MACSTR ")",
                                   keylen, MAC2STR(sm->bssid));
-                       return;
+                       goto failed;
                }
                break;
        }
 
        if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
-                                      NULL, 0, &sm->ptk))
-               return;
+                                      NULL, 0, &sm->ptk)) {
+               goto failed;
+       }
 
        /* SNonce was successfully used in msg 3/4, so mark it to be renewed
         * for the next 4-Way Handshake. If msg 3 is received again, the old
@@ -1004,7 +955,8 @@ static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
        sm->renew_snonce = 1;
 
        if (key_info & WPA_KEY_INFO_INSTALL) {
-               wpa_supplicant_install_ptk(sm, key);
+               if (wpa_supplicant_install_ptk(sm, key))
+                       goto failed;
        }
 
        if (key_info & WPA_KEY_INFO_SECURE) {
@@ -1019,10 +971,18 @@ static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
            wpa_supplicant_pairwise_gtk(sm, key,
                                        ie.gtk, ie.gtk_len, key_info) < 0) {
                wpa_printf(MSG_INFO, "RSN: Failed to configure GTK");
+               goto failed;
        }
 
-       if (ieee80211w_set_keys(sm, &ie) < 0)
+       if (ieee80211w_set_keys(sm, &ie) < 0) {
                wpa_printf(MSG_INFO, "RSN: Failed to configure IGTK");
+               goto failed;
+       }
+
+       return;
+
+failed:
+       wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
 }
 
 
@@ -1115,7 +1075,10 @@ static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
                        return -1;
                }
                os_memcpy(gd->gtk, key + 1, keydatalen);
-               rc4_skip(ek, 32, 256, gd->gtk, keydatalen);
+               if (rc4_skip(ek, 32, 256, gd->gtk, keydatalen)) {
+                       wpa_printf(MSG_ERROR, "WPA: RC4 failed");
+                       return -1;
+               }
        } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
                if (keydatalen % 8) {
                        wpa_printf(MSG_WARNING, "WPA: Unsupported AES-WRAP "
@@ -1213,11 +1176,11 @@ static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
        wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
 
        if (ret)
-               return;
+               goto failed;
 
        if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
            wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
-               return;
+               goto failed;
 
        if (rekey) {
                wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
@@ -1230,6 +1193,10 @@ static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
                                                key_info &
                                                WPA_KEY_INFO_SECURE);
        }
+       return;
+
+failed:
+       wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
 }
 
 
@@ -1302,7 +1269,10 @@ static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
                u8 ek[32];
                os_memcpy(ek, key->key_iv, 16);
                os_memcpy(ek + 16, sm->ptk.kek, 16);
-               rc4_skip(ek, 32, 256, (u8 *) (key + 1), keydatalen);
+               if (rc4_skip(ek, 32, 256, (u8 *) (key + 1), keydatalen)) {
+                       wpa_printf(MSG_ERROR, "WPA: RC4 failed");
+                       return -1;
+               }
        } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
                   ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
                u8 *buf;