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