2 * hostapd - PMKSA cache for IEEE 802.11i RSN
3 * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
24 #include "ieee802_1x.h"
26 #include "pmksa_cache.h"
29 static const int pmksa_cache_max_entries = 1024;
30 static const int dot11RSNAConfigPMKLifetime = 43200;
32 struct rsn_pmksa_cache {
33 #define PMKID_HASH_SIZE 128
34 #define PMKID_HASH(pmkid) (unsigned int) ((pmkid)[0] & 0x7f)
35 struct rsn_pmksa_cache_entry *pmkid[PMKID_HASH_SIZE];
36 struct rsn_pmksa_cache_entry *pmksa;
39 void (*free_cb)(struct rsn_pmksa_cache_entry *entry, void *ctx);
45 * rsn_pmkid - Calculate PMK identifier
46 * @pmk: Pairwise master key
47 * @pmk_len: Length of pmk in bytes
48 * @aa: Authenticator address
49 * @spa: Supplicant address
50 * @use_sha256: Whether to use SHA256-based KDF
52 * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
53 * PMKID = HMAC-SHA1-128(PMK, "PMK Name" || AA || SPA)
55 void rsn_pmkid(const u8 *pmk, size_t pmk_len, const u8 *aa, const u8 *spa,
56 u8 *pmkid, int use_sha256)
58 char *title = "PMK Name";
60 const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
61 unsigned char hash[SHA256_MAC_LEN];
63 addr[0] = (u8 *) title;
67 #ifdef CONFIG_IEEE80211W
69 hmac_sha256_vector(pmk, pmk_len, 3, addr, len, hash);
71 #endif /* CONFIG_IEEE80211W */
72 hmac_sha1_vector(pmk, pmk_len, 3, addr, len, hash);
73 os_memcpy(pmkid, hash, PMKID_LEN);
77 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa);
80 static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry *entry)
84 os_free(entry->identity);
85 ieee802_1x_free_radius_class(&entry->radius_class);
90 static void pmksa_cache_free_entry(struct rsn_pmksa_cache *pmksa,
91 struct rsn_pmksa_cache_entry *entry)
93 struct rsn_pmksa_cache_entry *pos, *prev;
96 pmksa->free_cb(entry, pmksa->ctx);
97 pos = pmksa->pmkid[PMKID_HASH(entry->pmkid)];
102 prev->hnext = pos->hnext;
104 pmksa->pmkid[PMKID_HASH(entry->pmkid)] =
118 prev->next = pos->next;
120 pmksa->pmksa = pos->next;
126 _pmksa_cache_free_entry(entry);
130 static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx)
132 struct rsn_pmksa_cache *pmksa = eloop_ctx;
136 while (pmksa->pmksa && pmksa->pmksa->expiration <= now.sec) {
137 struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
138 pmksa->pmksa = entry->next;
139 wpa_printf(MSG_DEBUG, "RSN: expired PMKSA cache entry for "
140 MACSTR, MAC2STR(entry->spa));
141 pmksa_cache_free_entry(pmksa, entry);
144 pmksa_cache_set_expiration(pmksa);
148 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa)
153 eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
154 if (pmksa->pmksa == NULL)
157 sec = pmksa->pmksa->expiration - now.sec;
160 eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, pmksa, NULL);
164 static void pmksa_cache_from_eapol_data(struct rsn_pmksa_cache_entry *entry,
165 struct eapol_state_machine *eapol)
170 if (eapol->identity) {
171 entry->identity = os_malloc(eapol->identity_len);
172 if (entry->identity) {
173 entry->identity_len = eapol->identity_len;
174 os_memcpy(entry->identity, eapol->identity,
175 eapol->identity_len);
179 ieee802_1x_copy_radius_class(&entry->radius_class,
180 &eapol->radius_class);
182 entry->eap_type_authsrv = eapol->eap_type_authsrv;
183 entry->vlan_id = eapol->sta->vlan_id;
187 void pmksa_cache_to_eapol_data(struct rsn_pmksa_cache_entry *entry,
188 struct eapol_state_machine *eapol)
190 if (entry == NULL || eapol == NULL)
193 if (entry->identity) {
194 os_free(eapol->identity);
195 eapol->identity = os_malloc(entry->identity_len);
196 if (eapol->identity) {
197 eapol->identity_len = entry->identity_len;
198 os_memcpy(eapol->identity, entry->identity,
199 entry->identity_len);
201 wpa_hexdump_ascii(MSG_DEBUG, "STA identity from PMKSA",
202 eapol->identity, eapol->identity_len);
205 ieee802_1x_free_radius_class(&eapol->radius_class);
206 ieee802_1x_copy_radius_class(&eapol->radius_class,
207 &entry->radius_class);
208 if (eapol->radius_class.attr) {
209 wpa_printf(MSG_DEBUG, "Copied %lu Class attribute(s) from "
210 "PMKSA", (unsigned long) eapol->radius_class.count);
213 eapol->eap_type_authsrv = entry->eap_type_authsrv;
214 eapol->sta->vlan_id = entry->vlan_id;
218 static void pmksa_cache_link_entry(struct rsn_pmksa_cache *pmksa,
219 struct rsn_pmksa_cache_entry *entry)
221 struct rsn_pmksa_cache_entry *pos, *prev;
223 /* Add the new entry; order by expiration time */
227 if (pos->expiration > entry->expiration)
233 entry->next = pmksa->pmksa;
234 pmksa->pmksa = entry;
236 entry->next = prev->next;
239 entry->hnext = pmksa->pmkid[PMKID_HASH(entry->pmkid)];
240 pmksa->pmkid[PMKID_HASH(entry->pmkid)] = entry;
242 pmksa->pmksa_count++;
243 wpa_printf(MSG_DEBUG, "RSN: added PMKSA cache entry for " MACSTR,
244 MAC2STR(entry->spa));
245 wpa_hexdump(MSG_DEBUG, "RSN: added PMKID", entry->pmkid, PMKID_LEN);
250 * pmksa_cache_add - Add a PMKSA cache entry
251 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
252 * @pmk: The new pairwise master key
253 * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
254 * @aa: Authenticator address
255 * @spa: Supplicant address
256 * @session_timeout: Session timeout
257 * @eapol: Pointer to EAPOL state machine data
258 * @akmp: WPA_KEY_MGMT_* used in key derivation
259 * Returns: Pointer to the added PMKSA cache entry or %NULL on error
261 * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
262 * cache. If an old entry is already in the cache for the same Supplicant,
263 * this entry will be replaced with the new entry. PMKID will be calculated
266 struct rsn_pmksa_cache_entry *
267 pmksa_cache_add(struct rsn_pmksa_cache *pmksa, const u8 *pmk, size_t pmk_len,
268 const u8 *aa, const u8 *spa, int session_timeout,
269 struct eapol_state_machine *eapol, int akmp)
271 struct rsn_pmksa_cache_entry *entry, *pos;
274 if (pmk_len > PMK_LEN)
277 entry = os_zalloc(sizeof(*entry));
280 os_memcpy(entry->pmk, pmk, pmk_len);
281 entry->pmk_len = pmk_len;
282 rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid,
283 wpa_key_mgmt_sha256(akmp));
285 entry->expiration = now.sec;
286 if (session_timeout > 0)
287 entry->expiration += session_timeout;
289 entry->expiration += dot11RSNAConfigPMKLifetime;
291 os_memcpy(entry->spa, spa, ETH_ALEN);
292 pmksa_cache_from_eapol_data(entry, eapol);
294 /* Replace an old entry for the same STA (if found) with the new entry
296 pos = pmksa_cache_get(pmksa, spa, NULL);
298 pmksa_cache_free_entry(pmksa, pos);
300 if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) {
301 /* Remove the oldest entry to make room for the new entry */
302 wpa_printf(MSG_DEBUG, "RSN: removed the oldest PMKSA cache "
303 "entry (for " MACSTR ") to make room for new one",
304 MAC2STR(pmksa->pmksa->spa));
305 pmksa_cache_free_entry(pmksa, pmksa->pmksa);
308 pmksa_cache_link_entry(pmksa, entry);
314 struct rsn_pmksa_cache_entry *
315 pmksa_cache_add_okc(struct rsn_pmksa_cache *pmksa,
316 const struct rsn_pmksa_cache_entry *old_entry,
317 const u8 *aa, const u8 *pmkid)
319 struct rsn_pmksa_cache_entry *entry;
321 entry = os_zalloc(sizeof(*entry));
324 os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
325 os_memcpy(entry->pmk, old_entry->pmk, old_entry->pmk_len);
326 entry->pmk_len = old_entry->pmk_len;
327 entry->expiration = old_entry->expiration;
328 entry->akmp = old_entry->akmp;
329 os_memcpy(entry->spa, old_entry->spa, ETH_ALEN);
330 entry->opportunistic = 1;
331 if (old_entry->identity) {
332 entry->identity = os_malloc(old_entry->identity_len);
333 if (entry->identity) {
334 entry->identity_len = old_entry->identity_len;
335 os_memcpy(entry->identity, old_entry->identity,
336 old_entry->identity_len);
339 ieee802_1x_copy_radius_class(&entry->radius_class,
340 &old_entry->radius_class);
341 entry->eap_type_authsrv = old_entry->eap_type_authsrv;
342 entry->vlan_id = old_entry->vlan_id;
343 entry->opportunistic = 1;
345 pmksa_cache_link_entry(pmksa, entry);
352 * pmksa_cache_deinit - Free all entries in PMKSA cache
353 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
355 void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa)
357 struct rsn_pmksa_cache_entry *entry, *prev;
363 entry = pmksa->pmksa;
367 _pmksa_cache_free_entry(prev);
369 eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
370 for (i = 0; i < PMKID_HASH_SIZE; i++)
371 pmksa->pmkid[i] = NULL;
377 * pmksa_cache_get - Fetch a PMKSA cache entry
378 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
379 * @spa: Supplicant address or %NULL to match any
380 * @pmkid: PMKID or %NULL to match any
381 * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
383 struct rsn_pmksa_cache_entry * pmksa_cache_get(struct rsn_pmksa_cache *pmksa,
384 const u8 *spa, const u8 *pmkid)
386 struct rsn_pmksa_cache_entry *entry;
389 entry = pmksa->pmkid[PMKID_HASH(pmkid)];
391 entry = pmksa->pmksa;
394 os_memcmp(entry->spa, spa, ETH_ALEN) == 0) &&
396 os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0))
398 entry = pmkid ? entry->hnext : entry->next;
405 * pmksa_cache_get_okc - Fetch a PMKSA cache entry using OKC
406 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
407 * @spa: Supplicant address
409 * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
411 * Use opportunistic key caching (OKC) to find a PMK for a supplicant.
413 struct rsn_pmksa_cache_entry * pmksa_cache_get_okc(
414 struct rsn_pmksa_cache *pmksa, const u8 *aa, const u8 *spa,
417 struct rsn_pmksa_cache_entry *entry;
418 u8 new_pmkid[PMKID_LEN];
420 entry = pmksa->pmksa;
422 if (os_memcmp(entry->spa, spa, ETH_ALEN) != 0)
424 rsn_pmkid(entry->pmk, entry->pmk_len, aa, spa, new_pmkid,
425 wpa_key_mgmt_sha256(entry->akmp));
426 if (os_memcmp(new_pmkid, pmkid, PMKID_LEN) == 0)
435 * pmksa_cache_init - Initialize PMKSA cache
436 * @free_cb: Callback function to be called when a PMKSA cache entry is freed
437 * @ctx: Context pointer for free_cb function
438 * Returns: Pointer to PMKSA cache data or %NULL on failure
440 struct rsn_pmksa_cache *
441 pmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
442 void *ctx), void *ctx)
444 struct rsn_pmksa_cache *pmksa;
446 pmksa = os_zalloc(sizeof(*pmksa));
448 pmksa->free_cb = free_cb;