2 * hostapd - PMKSA cache for IEEE 802.11i RSN
3 * Copyright (c) 2004-2006, 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.
22 #include "ieee802_1x.h"
24 #include "pmksa_cache.h"
27 static const int pmksa_cache_max_entries
= 1024;
28 static const int dot11RSNAConfigPMKLifetime
= 43200;
30 struct rsn_pmksa_cache
{
31 #define PMKID_HASH_SIZE 128
32 #define PMKID_HASH(pmkid) (unsigned int) ((pmkid)[0] & 0x7f)
33 struct rsn_pmksa_cache_entry
*pmkid
[PMKID_HASH_SIZE
];
34 struct rsn_pmksa_cache_entry
*pmksa
;
37 void (*free_cb
)(struct rsn_pmksa_cache_entry
*entry
, void *ctx
);
43 * rsn_pmkid - Calculate PMK identifier
44 * @pmk: Pairwise master key
45 * @pmk_len: Length of pmk in bytes
46 * @aa: Authenticator address
47 * @spa: Supplicant address
49 * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
50 * PMKID = HMAC-SHA1-128(PMK, "PMK Name" || AA || SPA)
52 void rsn_pmkid(const u8
*pmk
, size_t pmk_len
, const u8
*aa
, const u8
*spa
,
55 char *title
= "PMK Name";
57 const size_t len
[3] = { 8, ETH_ALEN
, ETH_ALEN
};
58 unsigned char hash
[SHA1_MAC_LEN
];
60 addr
[0] = (u8
*) title
;
64 hmac_sha1_vector(pmk
, pmk_len
, 3, addr
, len
, hash
);
65 memcpy(pmkid
, hash
, PMKID_LEN
);
69 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache
*pmksa
);
72 static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry
*entry
)
76 free(entry
->identity
);
77 ieee802_1x_free_radius_class(&entry
->radius_class
);
82 static void pmksa_cache_free_entry(struct rsn_pmksa_cache
*pmksa
,
83 struct rsn_pmksa_cache_entry
*entry
)
85 struct rsn_pmksa_cache_entry
*pos
, *prev
;
88 pmksa
->free_cb(entry
, pmksa
->ctx
);
89 pos
= pmksa
->pmkid
[PMKID_HASH(entry
->pmkid
)];
94 prev
->hnext
= pos
->hnext
;
96 pmksa
->pmkid
[PMKID_HASH(entry
->pmkid
)] =
110 prev
->next
= pos
->next
;
112 pmksa
->pmksa
= pos
->next
;
118 _pmksa_cache_free_entry(entry
);
122 static void pmksa_cache_expire(void *eloop_ctx
, void *timeout_ctx
)
124 struct rsn_pmksa_cache
*pmksa
= eloop_ctx
;
128 while (pmksa
->pmksa
&& pmksa
->pmksa
->expiration
<= now
.sec
) {
129 struct rsn_pmksa_cache_entry
*entry
= pmksa
->pmksa
;
130 pmksa
->pmksa
= entry
->next
;
131 wpa_printf(MSG_DEBUG
, "RSN: expired PMKSA cache entry for "
132 MACSTR
, MAC2STR(entry
->spa
));
133 pmksa_cache_free_entry(pmksa
, entry
);
136 pmksa_cache_set_expiration(pmksa
);
140 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache
*pmksa
)
145 eloop_cancel_timeout(pmksa_cache_expire
, pmksa
, NULL
);
146 if (pmksa
->pmksa
== NULL
)
149 sec
= pmksa
->pmksa
->expiration
- now
.sec
;
152 eloop_register_timeout(sec
+ 1, 0, pmksa_cache_expire
, pmksa
, NULL
);
156 static void pmksa_cache_from_eapol_data(struct rsn_pmksa_cache_entry
*entry
,
157 struct eapol_state_machine
*eapol
)
162 if (eapol
->identity
) {
163 entry
->identity
= malloc(eapol
->identity_len
);
164 if (entry
->identity
) {
165 entry
->identity_len
= eapol
->identity_len
;
166 memcpy(entry
->identity
, eapol
->identity
,
167 eapol
->identity_len
);
171 ieee802_1x_copy_radius_class(&entry
->radius_class
,
172 &eapol
->radius_class
);
174 entry
->eap_type_authsrv
= eapol
->eap_type_authsrv
;
175 entry
->vlan_id
= eapol
->sta
->vlan_id
;
179 void pmksa_cache_to_eapol_data(struct rsn_pmksa_cache_entry
*entry
,
180 struct eapol_state_machine
*eapol
)
182 if (entry
== NULL
|| eapol
== NULL
)
185 if (entry
->identity
) {
186 free(eapol
->identity
);
187 eapol
->identity
= malloc(entry
->identity_len
);
188 if (eapol
->identity
) {
189 eapol
->identity_len
= entry
->identity_len
;
190 memcpy(eapol
->identity
, entry
->identity
,
191 entry
->identity_len
);
193 wpa_hexdump_ascii(MSG_DEBUG
, "STA identity from PMKSA",
194 eapol
->identity
, eapol
->identity_len
);
197 ieee802_1x_free_radius_class(&eapol
->radius_class
);
198 ieee802_1x_copy_radius_class(&eapol
->radius_class
,
199 &entry
->radius_class
);
200 if (eapol
->radius_class
.attr
) {
201 wpa_printf(MSG_DEBUG
, "Copied %lu Class attribute(s) from "
202 "PMKSA", (unsigned long) eapol
->radius_class
.count
);
205 eapol
->eap_type_authsrv
= entry
->eap_type_authsrv
;
206 eapol
->sta
->vlan_id
= entry
->vlan_id
;
211 * pmksa_cache_add - Add a PMKSA cache entry
212 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
213 * @pmk: The new pairwise master key
214 * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
215 * @aa: Authenticator address
216 * @spa: Supplicant address
217 * @session_timeout: Session timeout
218 * @eapol: Pointer to EAPOL state machine data
219 * Returns: Pointer to the added PMKSA cache entry or %NULL on error
221 * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
222 * cache. If an old entry is already in the cache for the same Supplicant,
223 * this entry will be replaced with the new entry. PMKID will be calculated
226 struct rsn_pmksa_cache_entry
*
227 pmksa_cache_add(struct rsn_pmksa_cache
*pmksa
, const u8
*pmk
, size_t pmk_len
,
228 const u8
*aa
, const u8
*spa
, int session_timeout
,
229 struct eapol_state_machine
*eapol
)
231 struct rsn_pmksa_cache_entry
*entry
, *pos
, *prev
;
234 if (pmk_len
> PMK_LEN
)
237 entry
= wpa_zalloc(sizeof(*entry
));
240 memcpy(entry
->pmk
, pmk
, pmk_len
);
241 entry
->pmk_len
= pmk_len
;
242 rsn_pmkid(pmk
, pmk_len
, aa
, spa
, entry
->pmkid
);
244 entry
->expiration
= now
.sec
;
245 if (session_timeout
> 0)
246 entry
->expiration
+= session_timeout
;
248 entry
->expiration
+= dot11RSNAConfigPMKLifetime
;
249 entry
->akmp
= WPA_KEY_MGMT_IEEE8021X
;
250 memcpy(entry
->spa
, spa
, ETH_ALEN
);
251 pmksa_cache_from_eapol_data(entry
, eapol
);
253 /* Replace an old entry for the same STA (if found) with the new entry
255 pos
= pmksa_cache_get(pmksa
, spa
, NULL
);
257 pmksa_cache_free_entry(pmksa
, pos
);
259 if (pmksa
->pmksa_count
>= pmksa_cache_max_entries
&& pmksa
->pmksa
) {
260 /* Remove the oldest entry to make room for the new entry */
261 wpa_printf(MSG_DEBUG
, "RSN: removed the oldest PMKSA cache "
262 "entry (for " MACSTR
") to make room for new one",
263 MAC2STR(pmksa
->pmksa
->spa
));
264 pmksa_cache_free_entry(pmksa
, pmksa
->pmksa
);
267 /* Add the new entry; order by expiration time */
271 if (pos
->expiration
> entry
->expiration
)
277 entry
->next
= pmksa
->pmksa
;
278 pmksa
->pmksa
= entry
;
280 entry
->next
= prev
->next
;
283 entry
->hnext
= pmksa
->pmkid
[PMKID_HASH(entry
->pmkid
)];
284 pmksa
->pmkid
[PMKID_HASH(entry
->pmkid
)] = entry
;
286 pmksa
->pmksa_count
++;
287 wpa_printf(MSG_DEBUG
, "RSN: added PMKSA cache entry for " MACSTR
,
288 MAC2STR(entry
->spa
));
289 wpa_hexdump(MSG_DEBUG
, "RSN: added PMKID", entry
->pmkid
, PMKID_LEN
);
296 * pmksa_cache_deinit - Free all entries in PMKSA cache
297 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
299 void pmksa_cache_deinit(struct rsn_pmksa_cache
*pmksa
)
301 struct rsn_pmksa_cache_entry
*entry
, *prev
;
307 entry
= pmksa
->pmksa
;
311 _pmksa_cache_free_entry(prev
);
313 eloop_cancel_timeout(pmksa_cache_expire
, pmksa
, NULL
);
314 for (i
= 0; i
< PMKID_HASH_SIZE
; i
++)
315 pmksa
->pmkid
[i
] = NULL
;
321 * pmksa_cache_get - Fetch a PMKSA cache entry
322 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
323 * @spa: Supplicant address or %NULL to match any
324 * @pmkid: PMKID or %NULL to match any
325 * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
327 struct rsn_pmksa_cache_entry
* pmksa_cache_get(struct rsn_pmksa_cache
*pmksa
,
328 const u8
*spa
, const u8
*pmkid
)
330 struct rsn_pmksa_cache_entry
*entry
;
333 entry
= pmksa
->pmkid
[PMKID_HASH(pmkid
)];
335 entry
= pmksa
->pmksa
;
337 if ((spa
== NULL
|| memcmp(entry
->spa
, spa
, ETH_ALEN
) == 0) &&
339 memcmp(entry
->pmkid
, pmkid
, PMKID_LEN
) == 0))
341 entry
= pmkid
? entry
->hnext
: entry
->next
;
348 * pmksa_cache_init - Initialize PMKSA cache
349 * @free_cb: Callback function to be called when a PMKSA cache entry is freed
350 * @ctx: Context pointer for free_cb function
351 * Returns: Pointer to PMKSA cache data or %NULL on failure
353 struct rsn_pmksa_cache
*
354 pmksa_cache_init(void (*free_cb
)(struct rsn_pmksa_cache_entry
*entry
,
355 void *ctx
), void *ctx
)
357 struct rsn_pmksa_cache
*pmksa
;
359 pmksa
= wpa_zalloc(sizeof(*pmksa
));
361 pmksa
->free_cb
= free_cb
;