Fix memory leak in config freeing
[libeap.git] / hostapd / pmksa_cache.c
index e1f8d9b..2df6c81 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * hostapd - PMKSA cache for IEEE 802.11i RSN
- * Copyright (c) 2004-2006, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
 #include "includes.h"
 
 #include "common.h"
-#include "ap.h"
+#include "sta_info.h"
 #include "config.h"
 #include "common.h"
 #include "eloop.h"
-#include "sha1.h"
-#include "ieee802_1x.h"
-#include "eapol_sm.h"
+#include "eapol_auth/eapol_auth_sm.h"
+#include "eapol_auth/eapol_auth_sm_i.h"
 #include "pmksa_cache.h"
 
 
@@ -40,33 +39,6 @@ struct rsn_pmksa_cache {
 };
 
 
-/**
- * rsn_pmkid - Calculate PMK identifier
- * @pmk: Pairwise master key
- * @pmk_len: Length of pmk in bytes
- * @aa: Authenticator address
- * @spa: Supplicant address
- *
- * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
- * PMKID = HMAC-SHA1-128(PMK, "PMK Name" || AA || SPA)
- */
-void rsn_pmkid(const u8 *pmk, size_t pmk_len, const u8 *aa, const u8 *spa,
-              u8 *pmkid)
-{
-       char *title = "PMK Name";
-       const u8 *addr[3];
-       const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
-       unsigned char hash[SHA1_MAC_LEN];
-
-       addr[0] = (u8 *) title;
-       addr[1] = aa;
-       addr[2] = spa;
-
-       hmac_sha1_vector(pmk, pmk_len, 3, addr, len, hash);
-       os_memcpy(pmkid, hash, PMKID_LEN);
-}
-
-
 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa);
 
 
@@ -75,7 +47,9 @@ static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry *entry)
        if (entry == NULL)
                return;
        os_free(entry->identity);
-       ieee802_1x_free_radius_class(&entry->radius_class);
+#ifndef CONFIG_NO_RADIUS
+       radius_free_class(&entry->radius_class);
+#endif /* CONFIG_NO_RADIUS */
        os_free(entry);
 }
 
@@ -169,11 +143,12 @@ static void pmksa_cache_from_eapol_data(struct rsn_pmksa_cache_entry *entry,
                }
        }
 
-       ieee802_1x_copy_radius_class(&entry->radius_class,
-                                    &eapol->radius_class);
+#ifndef CONFIG_NO_RADIUS
+       radius_copy_class(&entry->radius_class, &eapol->radius_class);
+#endif /* CONFIG_NO_RADIUS */
 
        entry->eap_type_authsrv = eapol->eap_type_authsrv;
-       entry->vlan_id = eapol->sta->vlan_id;
+       entry->vlan_id = ((struct sta_info *) eapol->sta)->vlan_id;
 }
 
 
@@ -195,28 +170,61 @@ void pmksa_cache_to_eapol_data(struct rsn_pmksa_cache_entry *entry,
                                  eapol->identity, eapol->identity_len);
        }
 
-       ieee802_1x_free_radius_class(&eapol->radius_class);
-       ieee802_1x_copy_radius_class(&eapol->radius_class,
-                                    &entry->radius_class);
+#ifndef CONFIG_NO_RADIUS
+       radius_free_class(&eapol->radius_class);
+       radius_copy_class(&eapol->radius_class, &entry->radius_class);
+#endif /* CONFIG_NO_RADIUS */
        if (eapol->radius_class.attr) {
                wpa_printf(MSG_DEBUG, "Copied %lu Class attribute(s) from "
                           "PMKSA", (unsigned long) eapol->radius_class.count);
        }
 
        eapol->eap_type_authsrv = entry->eap_type_authsrv;
-       eapol->sta->vlan_id = entry->vlan_id;
+       ((struct sta_info *) eapol->sta)->vlan_id = entry->vlan_id;
+}
+
+
+static void pmksa_cache_link_entry(struct rsn_pmksa_cache *pmksa,
+                                  struct rsn_pmksa_cache_entry *entry)
+{
+       struct rsn_pmksa_cache_entry *pos, *prev;
+
+       /* Add the new entry; order by expiration time */
+       pos = pmksa->pmksa;
+       prev = NULL;
+       while (pos) {
+               if (pos->expiration > entry->expiration)
+                       break;
+               prev = pos;
+               pos = pos->next;
+       }
+       if (prev == NULL) {
+               entry->next = pmksa->pmksa;
+               pmksa->pmksa = entry;
+       } else {
+               entry->next = prev->next;
+               prev->next = entry;
+       }
+       entry->hnext = pmksa->pmkid[PMKID_HASH(entry->pmkid)];
+       pmksa->pmkid[PMKID_HASH(entry->pmkid)] = entry;
+
+       pmksa->pmksa_count++;
+       wpa_printf(MSG_DEBUG, "RSN: added PMKSA cache entry for " MACSTR,
+                  MAC2STR(entry->spa));
+       wpa_hexdump(MSG_DEBUG, "RSN: added PMKID", entry->pmkid, PMKID_LEN);
 }
 
 
 /**
- * pmksa_cache_add - Add a PMKSA cache entry
- * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
+ * pmksa_cache_auth_add - Add a PMKSA cache entry
+ * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
  * @pmk: The new pairwise master key
  * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
  * @aa: Authenticator address
  * @spa: Supplicant address
  * @session_timeout: Session timeout
  * @eapol: Pointer to EAPOL state machine data
+ * @akmp: WPA_KEY_MGMT_* used in key derivation
  * Returns: Pointer to the added PMKSA cache entry or %NULL on error
  *
  * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
@@ -225,11 +233,12 @@ void pmksa_cache_to_eapol_data(struct rsn_pmksa_cache_entry *entry,
  * based on the PMK.
  */
 struct rsn_pmksa_cache_entry *
-pmksa_cache_add(struct rsn_pmksa_cache *pmksa, const u8 *pmk, size_t pmk_len,
+pmksa_cache_auth_add(struct rsn_pmksa_cache *pmksa,
+                    const u8 *pmk, size_t pmk_len,
                const u8 *aa, const u8 *spa, int session_timeout,
-               struct eapol_state_machine *eapol)
+               struct eapol_state_machine *eapol, int akmp)
 {
-       struct rsn_pmksa_cache_entry *entry, *pos, *prev;
+       struct rsn_pmksa_cache_entry *entry, *pos;
        struct os_time now;
 
        if (pmk_len > PMK_LEN)
@@ -240,20 +249,21 @@ pmksa_cache_add(struct rsn_pmksa_cache *pmksa, const u8 *pmk, size_t pmk_len,
                return NULL;
        os_memcpy(entry->pmk, pmk, pmk_len);
        entry->pmk_len = pmk_len;
-       rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid);
+       rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid,
+                 wpa_key_mgmt_sha256(akmp));
        os_get_time(&now);
        entry->expiration = now.sec;
        if (session_timeout > 0)
                entry->expiration += session_timeout;
        else
                entry->expiration += dot11RSNAConfigPMKLifetime;
-       entry->akmp = WPA_KEY_MGMT_IEEE8021X;
+       entry->akmp = akmp;
        os_memcpy(entry->spa, spa, ETH_ALEN);
        pmksa_cache_from_eapol_data(entry, eapol);
 
        /* Replace an old entry for the same STA (if found) with the new entry
         */
-       pos = pmksa_cache_get(pmksa, spa, NULL);
+       pos = pmksa_cache_auth_get(pmksa, spa, NULL);
        if (pos)
                pmksa_cache_free_entry(pmksa, pos);
 
@@ -265,39 +275,55 @@ pmksa_cache_add(struct rsn_pmksa_cache *pmksa, const u8 *pmk, size_t pmk_len,
                pmksa_cache_free_entry(pmksa, pmksa->pmksa);
        }
 
-       /* Add the new entry; order by expiration time */
-       pos = pmksa->pmksa;
-       prev = NULL;
-       while (pos) {
-               if (pos->expiration > entry->expiration)
-                       break;
-               prev = pos;
-               pos = pos->next;
-       }
-       if (prev == NULL) {
-               entry->next = pmksa->pmksa;
-               pmksa->pmksa = entry;
-       } else {
-               entry->next = prev->next;
-               prev->next = entry;
+       pmksa_cache_link_entry(pmksa, entry);
+
+       return entry;
+}
+
+
+struct rsn_pmksa_cache_entry *
+pmksa_cache_add_okc(struct rsn_pmksa_cache *pmksa,
+                   const struct rsn_pmksa_cache_entry *old_entry,
+                   const u8 *aa, const u8 *pmkid)
+{
+       struct rsn_pmksa_cache_entry *entry;
+
+       entry = os_zalloc(sizeof(*entry));
+       if (entry == NULL)
+               return NULL;
+       os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
+       os_memcpy(entry->pmk, old_entry->pmk, old_entry->pmk_len);
+       entry->pmk_len = old_entry->pmk_len;
+       entry->expiration = old_entry->expiration;
+       entry->akmp = old_entry->akmp;
+       os_memcpy(entry->spa, old_entry->spa, ETH_ALEN);
+       entry->opportunistic = 1;
+       if (old_entry->identity) {
+               entry->identity = os_malloc(old_entry->identity_len);
+               if (entry->identity) {
+                       entry->identity_len = old_entry->identity_len;
+                       os_memcpy(entry->identity, old_entry->identity,
+                                 old_entry->identity_len);
+               }
        }
-       entry->hnext = pmksa->pmkid[PMKID_HASH(entry->pmkid)];
-       pmksa->pmkid[PMKID_HASH(entry->pmkid)] = entry;
+#ifndef CONFIG_NO_RADIUS
+       radius_copy_class(&entry->radius_class, &old_entry->radius_class);
+#endif /* CONFIG_NO_RADIUS */
+       entry->eap_type_authsrv = old_entry->eap_type_authsrv;
+       entry->vlan_id = old_entry->vlan_id;
+       entry->opportunistic = 1;
 
-       pmksa->pmksa_count++;
-       wpa_printf(MSG_DEBUG, "RSN: added PMKSA cache entry for " MACSTR,
-                  MAC2STR(entry->spa));
-       wpa_hexdump(MSG_DEBUG, "RSN: added PMKID", entry->pmkid, PMKID_LEN);
+       pmksa_cache_link_entry(pmksa, entry);
 
        return entry;
 }
 
 
 /**
- * pmksa_cache_deinit - Free all entries in PMKSA cache
- * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
+ * pmksa_cache_auth_deinit - Free all entries in PMKSA cache
+ * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
  */
-void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa)
+void pmksa_cache_auth_deinit(struct rsn_pmksa_cache *pmksa)
 {
        struct rsn_pmksa_cache_entry *entry, *prev;
        int i;
@@ -319,14 +345,15 @@ void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa)
 
 
 /**
- * pmksa_cache_get - Fetch a PMKSA cache entry
- * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
+ * pmksa_cache_auth_get - Fetch a PMKSA cache entry
+ * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
  * @spa: Supplicant address or %NULL to match any
  * @pmkid: PMKID or %NULL to match any
  * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
  */
-struct rsn_pmksa_cache_entry * pmksa_cache_get(struct rsn_pmksa_cache *pmksa,
-                                              const u8 *spa, const u8 *pmkid)
+struct rsn_pmksa_cache_entry *
+pmksa_cache_auth_get(struct rsn_pmksa_cache *pmksa,
+                    const u8 *spa, const u8 *pmkid)
 {
        struct rsn_pmksa_cache_entry *entry;
 
@@ -347,14 +374,45 @@ struct rsn_pmksa_cache_entry * pmksa_cache_get(struct rsn_pmksa_cache *pmksa,
 
 
 /**
- * pmksa_cache_init - Initialize PMKSA cache
+ * pmksa_cache_get_okc - Fetch a PMKSA cache entry using OKC
+ * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
+ * @aa: Authenticator address
+ * @spa: Supplicant address
+ * @pmkid: PMKID
+ * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
+ *
+ * Use opportunistic key caching (OKC) to find a PMK for a supplicant.
+ */
+struct rsn_pmksa_cache_entry * pmksa_cache_get_okc(
+       struct rsn_pmksa_cache *pmksa, const u8 *aa, const u8 *spa,
+       const u8 *pmkid)
+{
+       struct rsn_pmksa_cache_entry *entry;
+       u8 new_pmkid[PMKID_LEN];
+
+       entry = pmksa->pmksa;
+       while (entry) {
+               if (os_memcmp(entry->spa, spa, ETH_ALEN) != 0)
+                       continue;
+               rsn_pmkid(entry->pmk, entry->pmk_len, aa, spa, new_pmkid,
+                         wpa_key_mgmt_sha256(entry->akmp));
+               if (os_memcmp(new_pmkid, pmkid, PMKID_LEN) == 0)
+                       return entry;
+               entry = entry->next;
+       }
+       return NULL;
+}
+
+
+/**
+ * pmksa_cache_auth_init - Initialize PMKSA cache
  * @free_cb: Callback function to be called when a PMKSA cache entry is freed
  * @ctx: Context pointer for free_cb function
  * Returns: Pointer to PMKSA cache data or %NULL on failure
  */
 struct rsn_pmksa_cache *
-pmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
-                                void *ctx), void *ctx)
+pmksa_cache_auth_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
+                                     void *ctx), void *ctx)
 {
        struct rsn_pmksa_cache *pmksa;