Moved RADIUS Class attribute helpers into RADIUS module
[libeap.git] / hostapd / pmksa_cache.c
1 /*
2  * hostapd - PMKSA cache for IEEE 802.11i RSN
3  * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "ap.h"
19 #include "config.h"
20 #include "common.h"
21 #include "eloop.h"
22 #include "sha1.h"
23 #include "sha256.h"
24 #include "eapol_sm.h"
25 #include "pmksa_cache.h"
26
27
28 static const int pmksa_cache_max_entries = 1024;
29 static const int dot11RSNAConfigPMKLifetime = 43200;
30
31 struct rsn_pmksa_cache {
32 #define PMKID_HASH_SIZE 128
33 #define PMKID_HASH(pmkid) (unsigned int) ((pmkid)[0] & 0x7f)
34         struct rsn_pmksa_cache_entry *pmkid[PMKID_HASH_SIZE];
35         struct rsn_pmksa_cache_entry *pmksa;
36         int pmksa_count;
37
38         void (*free_cb)(struct rsn_pmksa_cache_entry *entry, void *ctx);
39         void *ctx;
40 };
41
42
43 /**
44  * rsn_pmkid - Calculate PMK identifier
45  * @pmk: Pairwise master key
46  * @pmk_len: Length of pmk in bytes
47  * @aa: Authenticator address
48  * @spa: Supplicant address
49  * @pmkid: Buffer for PMKID
50  * @use_sha256: Whether to use SHA256-based KDF
51  *
52  * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
53  * PMKID = HMAC-SHA1-128(PMK, "PMK Name" || AA || SPA)
54  */
55 void rsn_pmkid(const u8 *pmk, size_t pmk_len, const u8 *aa, const u8 *spa,
56                u8 *pmkid, int use_sha256)
57 {
58         char *title = "PMK Name";
59         const u8 *addr[3];
60         const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
61         unsigned char hash[SHA256_MAC_LEN];
62
63         addr[0] = (u8 *) title;
64         addr[1] = aa;
65         addr[2] = spa;
66
67 #ifdef CONFIG_IEEE80211W
68         if (use_sha256)
69                 hmac_sha256_vector(pmk, pmk_len, 3, addr, len, hash);
70         else
71 #endif /* CONFIG_IEEE80211W */
72                 hmac_sha1_vector(pmk, pmk_len, 3, addr, len, hash);
73         os_memcpy(pmkid, hash, PMKID_LEN);
74 }
75
76
77 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa);
78
79
80 static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry *entry)
81 {
82         if (entry == NULL)
83                 return;
84         os_free(entry->identity);
85         radius_free_class(&entry->radius_class);
86         os_free(entry);
87 }
88
89
90 static void pmksa_cache_free_entry(struct rsn_pmksa_cache *pmksa,
91                                    struct rsn_pmksa_cache_entry *entry)
92 {
93         struct rsn_pmksa_cache_entry *pos, *prev;
94
95         pmksa->pmksa_count--;
96         pmksa->free_cb(entry, pmksa->ctx);
97         pos = pmksa->pmkid[PMKID_HASH(entry->pmkid)];
98         prev = NULL;
99         while (pos) {
100                 if (pos == entry) {
101                         if (prev != NULL) {
102                                 prev->hnext = pos->hnext;
103                         } else {
104                                 pmksa->pmkid[PMKID_HASH(entry->pmkid)] =
105                                         pos->hnext;
106                         }
107                         break;
108                 }
109                 prev = pos;
110                 pos = pos->hnext;
111         }
112
113         pos = pmksa->pmksa;
114         prev = NULL;
115         while (pos) {
116                 if (pos == entry) {
117                         if (prev != NULL)
118                                 prev->next = pos->next;
119                         else
120                                 pmksa->pmksa = pos->next;
121                         break;
122                 }
123                 prev = pos;
124                 pos = pos->next;
125         }
126         _pmksa_cache_free_entry(entry);
127 }
128
129
130 static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx)
131 {
132         struct rsn_pmksa_cache *pmksa = eloop_ctx;
133         struct os_time now;
134
135         os_get_time(&now);
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);
142         }
143
144         pmksa_cache_set_expiration(pmksa);
145 }
146
147
148 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa)
149 {
150         int sec;
151         struct os_time now;
152
153         eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
154         if (pmksa->pmksa == NULL)
155                 return;
156         os_get_time(&now);
157         sec = pmksa->pmksa->expiration - now.sec;
158         if (sec < 0)
159                 sec = 0;
160         eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, pmksa, NULL);
161 }
162
163
164 static void pmksa_cache_from_eapol_data(struct rsn_pmksa_cache_entry *entry,
165                                         struct eapol_state_machine *eapol)
166 {
167         if (eapol == NULL)
168                 return;
169
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);
176                 }
177         }
178
179         radius_copy_class(&entry->radius_class, &eapol->radius_class);
180
181         entry->eap_type_authsrv = eapol->eap_type_authsrv;
182         entry->vlan_id = eapol->sta->vlan_id;
183 }
184
185
186 void pmksa_cache_to_eapol_data(struct rsn_pmksa_cache_entry *entry,
187                                struct eapol_state_machine *eapol)
188 {
189         if (entry == NULL || eapol == NULL)
190                 return;
191
192         if (entry->identity) {
193                 os_free(eapol->identity);
194                 eapol->identity = os_malloc(entry->identity_len);
195                 if (eapol->identity) {
196                         eapol->identity_len = entry->identity_len;
197                         os_memcpy(eapol->identity, entry->identity,
198                                   entry->identity_len);
199                 }
200                 wpa_hexdump_ascii(MSG_DEBUG, "STA identity from PMKSA",
201                                   eapol->identity, eapol->identity_len);
202         }
203
204         radius_free_class(&eapol->radius_class);
205         radius_copy_class(&eapol->radius_class, &entry->radius_class);
206         if (eapol->radius_class.attr) {
207                 wpa_printf(MSG_DEBUG, "Copied %lu Class attribute(s) from "
208                            "PMKSA", (unsigned long) eapol->radius_class.count);
209         }
210
211         eapol->eap_type_authsrv = entry->eap_type_authsrv;
212         eapol->sta->vlan_id = entry->vlan_id;
213 }
214
215
216 static void pmksa_cache_link_entry(struct rsn_pmksa_cache *pmksa,
217                                    struct rsn_pmksa_cache_entry *entry)
218 {
219         struct rsn_pmksa_cache_entry *pos, *prev;
220
221         /* Add the new entry; order by expiration time */
222         pos = pmksa->pmksa;
223         prev = NULL;
224         while (pos) {
225                 if (pos->expiration > entry->expiration)
226                         break;
227                 prev = pos;
228                 pos = pos->next;
229         }
230         if (prev == NULL) {
231                 entry->next = pmksa->pmksa;
232                 pmksa->pmksa = entry;
233         } else {
234                 entry->next = prev->next;
235                 prev->next = entry;
236         }
237         entry->hnext = pmksa->pmkid[PMKID_HASH(entry->pmkid)];
238         pmksa->pmkid[PMKID_HASH(entry->pmkid)] = entry;
239
240         pmksa->pmksa_count++;
241         wpa_printf(MSG_DEBUG, "RSN: added PMKSA cache entry for " MACSTR,
242                    MAC2STR(entry->spa));
243         wpa_hexdump(MSG_DEBUG, "RSN: added PMKID", entry->pmkid, PMKID_LEN);
244 }
245
246
247 /**
248  * pmksa_cache_add - Add a PMKSA cache entry
249  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
250  * @pmk: The new pairwise master key
251  * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
252  * @aa: Authenticator address
253  * @spa: Supplicant address
254  * @session_timeout: Session timeout
255  * @eapol: Pointer to EAPOL state machine data
256  * @akmp: WPA_KEY_MGMT_* used in key derivation
257  * Returns: Pointer to the added PMKSA cache entry or %NULL on error
258  *
259  * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
260  * cache. If an old entry is already in the cache for the same Supplicant,
261  * this entry will be replaced with the new entry. PMKID will be calculated
262  * based on the PMK.
263  */
264 struct rsn_pmksa_cache_entry *
265 pmksa_cache_add(struct rsn_pmksa_cache *pmksa, const u8 *pmk, size_t pmk_len,
266                 const u8 *aa, const u8 *spa, int session_timeout,
267                 struct eapol_state_machine *eapol, int akmp)
268 {
269         struct rsn_pmksa_cache_entry *entry, *pos;
270         struct os_time now;
271
272         if (pmk_len > PMK_LEN)
273                 return NULL;
274
275         entry = os_zalloc(sizeof(*entry));
276         if (entry == NULL)
277                 return NULL;
278         os_memcpy(entry->pmk, pmk, pmk_len);
279         entry->pmk_len = pmk_len;
280         rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid,
281                   wpa_key_mgmt_sha256(akmp));
282         os_get_time(&now);
283         entry->expiration = now.sec;
284         if (session_timeout > 0)
285                 entry->expiration += session_timeout;
286         else
287                 entry->expiration += dot11RSNAConfigPMKLifetime;
288         entry->akmp = akmp;
289         os_memcpy(entry->spa, spa, ETH_ALEN);
290         pmksa_cache_from_eapol_data(entry, eapol);
291
292         /* Replace an old entry for the same STA (if found) with the new entry
293          */
294         pos = pmksa_cache_get(pmksa, spa, NULL);
295         if (pos)
296                 pmksa_cache_free_entry(pmksa, pos);
297
298         if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) {
299                 /* Remove the oldest entry to make room for the new entry */
300                 wpa_printf(MSG_DEBUG, "RSN: removed the oldest PMKSA cache "
301                            "entry (for " MACSTR ") to make room for new one",
302                            MAC2STR(pmksa->pmksa->spa));
303                 pmksa_cache_free_entry(pmksa, pmksa->pmksa);
304         }
305
306         pmksa_cache_link_entry(pmksa, entry);
307
308         return entry;
309 }
310
311
312 struct rsn_pmksa_cache_entry *
313 pmksa_cache_add_okc(struct rsn_pmksa_cache *pmksa,
314                     const struct rsn_pmksa_cache_entry *old_entry,
315                     const u8 *aa, const u8 *pmkid)
316 {
317         struct rsn_pmksa_cache_entry *entry;
318
319         entry = os_zalloc(sizeof(*entry));
320         if (entry == NULL)
321                 return NULL;
322         os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
323         os_memcpy(entry->pmk, old_entry->pmk, old_entry->pmk_len);
324         entry->pmk_len = old_entry->pmk_len;
325         entry->expiration = old_entry->expiration;
326         entry->akmp = old_entry->akmp;
327         os_memcpy(entry->spa, old_entry->spa, ETH_ALEN);
328         entry->opportunistic = 1;
329         if (old_entry->identity) {
330                 entry->identity = os_malloc(old_entry->identity_len);
331                 if (entry->identity) {
332                         entry->identity_len = old_entry->identity_len;
333                         os_memcpy(entry->identity, old_entry->identity,
334                                   old_entry->identity_len);
335                 }
336         }
337         radius_copy_class(&entry->radius_class, &old_entry->radius_class);
338         entry->eap_type_authsrv = old_entry->eap_type_authsrv;
339         entry->vlan_id = old_entry->vlan_id;
340         entry->opportunistic = 1;
341
342         pmksa_cache_link_entry(pmksa, entry);
343
344         return entry;
345 }
346
347
348 /**
349  * pmksa_cache_deinit - Free all entries in PMKSA cache
350  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
351  */
352 void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa)
353 {
354         struct rsn_pmksa_cache_entry *entry, *prev;
355         int i;
356
357         if (pmksa == NULL)
358                 return;
359
360         entry = pmksa->pmksa;
361         while (entry) {
362                 prev = entry;
363                 entry = entry->next;
364                 _pmksa_cache_free_entry(prev);
365         }
366         eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
367         for (i = 0; i < PMKID_HASH_SIZE; i++)
368                 pmksa->pmkid[i] = NULL;
369         os_free(pmksa);
370 }
371
372
373 /**
374  * pmksa_cache_get - Fetch a PMKSA cache entry
375  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
376  * @spa: Supplicant address or %NULL to match any
377  * @pmkid: PMKID or %NULL to match any
378  * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
379  */
380 struct rsn_pmksa_cache_entry * pmksa_cache_get(struct rsn_pmksa_cache *pmksa,
381                                                const u8 *spa, const u8 *pmkid)
382 {
383         struct rsn_pmksa_cache_entry *entry;
384
385         if (pmkid)
386                 entry = pmksa->pmkid[PMKID_HASH(pmkid)];
387         else
388                 entry = pmksa->pmksa;
389         while (entry) {
390                 if ((spa == NULL ||
391                      os_memcmp(entry->spa, spa, ETH_ALEN) == 0) &&
392                     (pmkid == NULL ||
393                      os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0))
394                         return entry;
395                 entry = pmkid ? entry->hnext : entry->next;
396         }
397         return NULL;
398 }
399
400
401 /**
402  * pmksa_cache_get_okc - Fetch a PMKSA cache entry using OKC
403  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
404  * @aa: Authenticator address
405  * @spa: Supplicant address
406  * @pmkid: PMKID
407  * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
408  *
409  * Use opportunistic key caching (OKC) to find a PMK for a supplicant.
410  */
411 struct rsn_pmksa_cache_entry * pmksa_cache_get_okc(
412         struct rsn_pmksa_cache *pmksa, const u8 *aa, const u8 *spa,
413         const u8 *pmkid)
414 {
415         struct rsn_pmksa_cache_entry *entry;
416         u8 new_pmkid[PMKID_LEN];
417
418         entry = pmksa->pmksa;
419         while (entry) {
420                 if (os_memcmp(entry->spa, spa, ETH_ALEN) != 0)
421                         continue;
422                 rsn_pmkid(entry->pmk, entry->pmk_len, aa, spa, new_pmkid,
423                           wpa_key_mgmt_sha256(entry->akmp));
424                 if (os_memcmp(new_pmkid, pmkid, PMKID_LEN) == 0)
425                         return entry;
426                 entry = entry->next;
427         }
428         return NULL;
429 }
430
431
432 /**
433  * pmksa_cache_init - Initialize PMKSA cache
434  * @free_cb: Callback function to be called when a PMKSA cache entry is freed
435  * @ctx: Context pointer for free_cb function
436  * Returns: Pointer to PMKSA cache data or %NULL on failure
437  */
438 struct rsn_pmksa_cache *
439 pmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
440                                  void *ctx), void *ctx)
441 {
442         struct rsn_pmksa_cache *pmksa;
443
444         pmksa = os_zalloc(sizeof(*pmksa));
445         if (pmksa) {
446                 pmksa->free_cb = free_cb;
447                 pmksa->ctx = ctx;
448         }
449
450         return pmksa;
451 }