1 /*
2  * WPA Supplicant - RSN PMKSA cache
3  * Copyright (c) 2004-2009, 2011-2015, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 #include "common.h"
11 #include "eloop.h"
12 #include "rsn_supp/wpa.h"
13 #include "rsn_supp/wpa_i.h"
14 #include "common/eapol_common.h"
15 #include "common/ieee802_11_defs.h"
16 #include "pmksa_cache.h"
17 
18 #ifdef IEEE8021X_EAPOL
19 
20 static const int pmksa_cache_max_entries = 10;
21 static const int dot11RSNAConfigPMKLifetime = 8640000; // 100 days = 3600 x 24 x 100 Seconds
22 static const int dot11RSNAConfigPMKReauthThreshold = 70;
23 
24 struct rsn_pmksa_cache {
25     struct rsn_pmksa_cache_entry *pmksa; /* PMKSA cache */
26     int pmksa_count; /* number of entries in PMKSA cache */
27     struct wpa_sm *sm; /* TODO: get rid of this reference(?) */
28 
29     void (*free_cb)(struct rsn_pmksa_cache_entry *entry, void *ctx,
30             enum pmksa_free_reason reason);
31     void *ctx;
32 };
33 
34 
35 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa);
36 
37 
_pmksa_cache_free_entry(struct rsn_pmksa_cache_entry * entry)38 static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry *entry)
39 {
40     bin_clear_free(entry, sizeof(*entry));
41 }
42 
43 
pmksa_cache_free_entry(struct rsn_pmksa_cache * pmksa,struct rsn_pmksa_cache_entry * entry,enum pmksa_free_reason reason)44 static void pmksa_cache_free_entry(struct rsn_pmksa_cache *pmksa,
45         struct rsn_pmksa_cache_entry *entry,
46         enum pmksa_free_reason reason)
47 {
48     pmksa->pmksa_count--;
49     pmksa->free_cb(entry, pmksa->ctx, reason);
50     _pmksa_cache_free_entry(entry);
51 }
52 
53 
pmksa_cache_expire(void * eloop_ctx,void * user_data)54 static void pmksa_cache_expire(void *eloop_ctx, void *user_data)
55 {
56     struct rsn_pmksa_cache *pmksa = eloop_ctx;
57     struct os_reltime now;
58 
59     os_get_reltime(&now);
60     while (pmksa->pmksa && pmksa->pmksa->expiration <= now.sec) {
61         struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
62         pmksa->pmksa = entry->next;
63         wpa_printf(MSG_DEBUG, "RSN: expired PMKSA cache entry for "
64                 MACSTR, MAC2STR(entry->aa));
65         pmksa_cache_free_entry(pmksa, entry, PMKSA_EXPIRE);
66     }
67 
68     pmksa_cache_set_expiration(pmksa);
69 }
70 
pmksa_cache_set_expiration(struct rsn_pmksa_cache * pmksa)71 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa)
72 {
73     int sec;
74     struct os_reltime now;
75 
76     eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
77     if (pmksa->pmksa == NULL)
78         return;
79 
80     os_get_reltime(&now);
81     sec = pmksa->pmksa->expiration - now.sec;
82     if (sec < 0)
83         sec = 0;
84 
85     eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, pmksa, NULL);
86 }
87 
88 /**
89  * pmksa_cache_add - Add a PMKSA cache entry
90  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
91  * @pmk: The new pairwise master key
92  * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
93  * @kck: Key confirmation key or %NULL if not yet derived
94  * @kck_len: KCK length in bytes
95  * @aa: Authenticator address
96  * @spa: Supplicant address
97  * @network_ctx: Network configuration context for this PMK
98  * @akmp: WPA_KEY_MGMT_* used in key derivation
99  * Returns: Pointer to the added PMKSA cache entry or %NULL on error
100  *
101  * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
102  * cache. If an old entry is already in the cache for the same Authenticator,
103  * this entry will be replaced with the new entry. PMKID will be calculated
104  * based on the PMK and the driver interface is notified of the new PMKID.
105  */
106 struct rsn_pmksa_cache_entry *
pmksa_cache_add(struct rsn_pmksa_cache * pmksa,const u8 * pmk,size_t pmk_len,const u8 * pmkid,const u8 * kck,size_t kck_len,const u8 * aa,const u8 * spa,void * network_ctx,int akmp)107 pmksa_cache_add(struct rsn_pmksa_cache *pmksa, const u8 *pmk, size_t pmk_len,
108         const u8 *pmkid, const u8 *kck, size_t kck_len,
109         const u8 *aa, const u8 *spa, void *network_ctx, int akmp)
110 {
111     struct rsn_pmksa_cache_entry *entry, *pos, *prev;
112     struct os_reltime now;
113 
114     if (pmk_len > PMK_LEN_MAX)
115         return NULL;
116 
117     if (wpa_key_mgmt_suite_b(akmp) && !kck)
118         return NULL;
119 
120     entry = os_zalloc(sizeof(*entry));
121     if (entry == NULL)
122         return NULL;
123     os_memcpy(entry->pmk, pmk, pmk_len);
124     entry->pmk_len = pmk_len;
125     if (pmkid)
126         os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
127     else if (akmp == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
128         rsn_pmkid_suite_b_192(kck, kck_len, aa, spa, entry->pmkid);
129     else if (wpa_key_mgmt_suite_b(akmp))
130         rsn_pmkid_suite_b(kck, kck_len, aa, spa, entry->pmkid);
131     else
132         rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid, akmp);
133 
134     os_get_reltime(&now);
135     entry->expiration = now.sec + dot11RSNAConfigPMKLifetime;
136     entry->reauth_time = now.sec + dot11RSNAConfigPMKLifetime *
137         dot11RSNAConfigPMKReauthThreshold / 100;
138     entry->akmp = akmp;
139     os_memcpy(entry->aa, aa, ETH_ALEN);
140     entry->network_ctx = network_ctx;
141 
142     /* Replace an old entry for the same Authenticator (if found) with the
143      * new entry */
144     pos = pmksa->pmksa;
145     prev = NULL;
146     while (pos) {
147         if (os_memcmp(aa, pos->aa, ETH_ALEN) == 0) {
148             if (pos->pmk_len == pmk_len &&
149                     os_memcmp_const(pos->pmk, pmk, pmk_len) == 0 &&
150                     os_memcmp_const(pos->pmkid, entry->pmkid,
151                         PMKID_LEN) == 0) {
152                 wpa_printf(MSG_DEBUG, "WPA: reusing previous "
153                         "PMKSA entry");
154                 os_free(entry);
155                 return pos;
156             }
157             if (prev == NULL)
158                 pmksa->pmksa = pos->next;
159             else
160                 prev->next = pos->next;
161 
162             /*
163              * If OKC is used, there may be other PMKSA cache
164              * entries based on the same PMK. These needs to be
165              * flushed so that a new entry can be created based on
166              * the new PMK. Only clear other entries if they have a
167              * matching PMK and this PMK has been used successfully
168              * with the current AP, i.e., if opportunistic flag has
169              * been cleared in wpa_supplicant_key_neg_complete().
170              */
171             wpa_printf(MSG_DEBUG, "RSN: Replace PMKSA entry for "
172                     "the current AP and any PMKSA cache entry "
173                     "that was based on the old PMK");
174             if (!pos->opportunistic)
175                 pmksa_cache_flush(pmksa, network_ctx, pos->pmk,
176                         pos->pmk_len);
177             pmksa_cache_free_entry(pmksa, pos, PMKSA_REPLACE);
178             break;
179         }
180         prev = pos;
181         pos = pos->next;
182     }
183 
184     if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) {
185         /* Remove the oldest entry to make room for the new entry */
186         pos = pmksa->pmksa;
187 
188         if (pos == pmksa->sm->cur_pmksa) {
189             /*
190              * Never remove the current PMKSA cache entry, since
191              * it's in use, and removing it triggers a needless
192              * deauthentication.
193              */
194             pos = pos->next;
195             pmksa->pmksa->next = pos ? pos->next : NULL;
196         } else
197             pmksa->pmksa = pos->next;
198 
199         if (pos) {
200             wpa_printf(MSG_DEBUG, "RSN: removed the oldest idle "
201                     "PMKSA cache entry (for " MACSTR ") to "
202                     "make room for new one",
203                     MAC2STR(pos->aa));
204             pmksa_cache_free_entry(pmksa, pos, PMKSA_FREE);
205         }
206     }
207 
208     /* Add the new entry; order by expiration time */
209     pos = pmksa->pmksa;
210     prev = NULL;
211     while (pos) {
212         if (pos->expiration > entry->expiration)
213             break;
214         prev = pos;
215         pos = pos->next;
216     }
217     if (prev == NULL) {
218         entry->next = pmksa->pmksa;
219         pmksa->pmksa = entry;
220         pmksa_cache_set_expiration(pmksa);
221     } else {
222         entry->next = prev->next;
223         prev->next = entry;
224     }
225     pmksa->pmksa_count++;
226     wpa_printf(MSG_DEBUG, "RSN: Added PMKSA cache entry for " MACSTR
227             " network_ctx=%p", MAC2STR(entry->aa), network_ctx);
228 
229     return entry;
230 }
231 
232 
233 /**
234  * pmksa_cache_flush - Flush PMKSA cache entries for a specific network
235  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
236  * @network_ctx: Network configuration context or %NULL to flush all entries
237  * @pmk: PMK to match for or %NYLL to match all PMKs
238  * @pmk_len: PMK length
239  */
pmksa_cache_flush(struct rsn_pmksa_cache * pmksa,void * network_ctx,const u8 * pmk,size_t pmk_len)240 void pmksa_cache_flush(struct rsn_pmksa_cache *pmksa, void *network_ctx,
241         const u8 *pmk, size_t pmk_len)
242 {
243     struct rsn_pmksa_cache_entry *entry, *prev = NULL, *tmp;
244     int removed = 0;
245 
246     entry = pmksa->pmksa;
247     while (entry) {
248         if ((entry->network_ctx == network_ctx ||
249                     network_ctx == NULL) &&
250                 (pmk == NULL ||
251                  (pmk_len == entry->pmk_len &&
252                   os_memcmp(pmk, entry->pmk, pmk_len) == 0))) {
253             wpa_printf(MSG_DEBUG, "RSN: Flush PMKSA cache entry "
254                     "for " MACSTR, MAC2STR(entry->aa));
255             if (prev)
256                 prev->next = entry->next;
257             else
258                 pmksa->pmksa = entry->next;
259             tmp = entry;
260             entry = entry->next;
261             pmksa_cache_free_entry(pmksa, tmp, PMKSA_FREE);
262             removed++;
263         } else {
264             prev = entry;
265             entry = entry->next;
266         }
267     }
268     /*if (removed)
269       pmksa_cache_set_expiration(pmksa);*/
270 }
271 
272 
273 /**
274  * pmksa_cache_deinit - Free all entries in PMKSA cache
275  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
276  */
pmksa_cache_deinit(struct rsn_pmksa_cache * pmksa)277 void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa)
278 {
279     struct rsn_pmksa_cache_entry *entry, *prev;
280 
281     if (pmksa == NULL)
282         return;
283 
284     entry = pmksa->pmksa;
285     pmksa->pmksa = NULL;
286     while (entry) {
287         prev = entry;
288         entry = entry->next;
289         os_free(prev);
290     }
291     pmksa_cache_set_expiration(pmksa);
292     eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
293     os_free(pmksa);
294 }
295 
296 
297 /**
298  * pmksa_cache_get - Fetch a PMKSA cache entry
299  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
300  * @aa: Authenticator address or %NULL to match any
301  * @pmkid: PMKID or %NULL to match any
302  * @network_ctx: Network context or %NULL to match any
303  * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
304  */
pmksa_cache_get(struct rsn_pmksa_cache * pmksa,const u8 * aa,const u8 * pmkid,const void * network_ctx)305 struct rsn_pmksa_cache_entry * pmksa_cache_get(struct rsn_pmksa_cache *pmksa,
306         const u8 *aa, const u8 *pmkid,
307         const void *network_ctx)
308 {
309     struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
310     while (entry) {
311         if ((aa == NULL || os_memcmp(entry->aa, aa, ETH_ALEN) == 0) &&
312                 (pmkid == NULL ||
313                  os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0) &&
314                 (network_ctx == NULL || network_ctx == entry->network_ctx))
315             return entry;
316         entry = entry->next;
317     }
318     return NULL;
319 }
320 
321 
322 static struct rsn_pmksa_cache_entry *
pmksa_cache_clone_entry(struct rsn_pmksa_cache * pmksa,const struct rsn_pmksa_cache_entry * old_entry,const u8 * aa)323 pmksa_cache_clone_entry(struct rsn_pmksa_cache *pmksa,
324         const struct rsn_pmksa_cache_entry *old_entry,
325         const u8 *aa)
326 {
327     struct rsn_pmksa_cache_entry *new_entry;
328 
329     new_entry = pmksa_cache_add(pmksa, old_entry->pmk, old_entry->pmk_len,
330             NULL, NULL, 0,
331             aa, pmksa->sm->own_addr,
332             old_entry->network_ctx, old_entry->akmp);
333     if (new_entry == NULL)
334         return NULL;
335 
336     /* TODO: reorder entries based on expiration time? */
337     new_entry->expiration = old_entry->expiration;
338     new_entry->opportunistic = 1;
339 
340     return new_entry;
341 }
342 
343 
344 /**
345  * pmksa_cache_get_opportunistic - Try to get an opportunistic PMKSA entry
346  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
347  * @network_ctx: Network configuration context
348  * @aa: Authenticator address for the new AP
349  * Returns: Pointer to a new PMKSA cache entry or %NULL if not available
350  *
351  * Try to create a new PMKSA cache entry opportunistically by guessing that the
352  * new AP is sharing the same PMK as another AP that has the same SSID and has
353  * already an entry in PMKSA cache.
354  */
355 struct rsn_pmksa_cache_entry *
pmksa_cache_get_opportunistic(struct rsn_pmksa_cache * pmksa,void * network_ctx,const u8 * aa)356 pmksa_cache_get_opportunistic(struct rsn_pmksa_cache *pmksa, void *network_ctx,
357         const u8 *aa)
358 {
359     struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
360 
361     wpa_printf(MSG_DEBUG, "RSN: Consider " MACSTR " for OKC", MAC2STR(aa));
362     if (network_ctx == NULL)
363         return NULL;
364     while (entry) {
365         if (entry->network_ctx == network_ctx) {
366             entry = pmksa_cache_clone_entry(pmksa, entry, aa);
367             if (entry) {
368                 wpa_printf(MSG_DEBUG, "RSN: added "
369                         "opportunistic PMKSA cache entry "
370                         "for " MACSTR, MAC2STR(aa));
371             }
372             return entry;
373         }
374         entry = entry->next;
375     }
376     return NULL;
377 }
378 
379 
380 /**
381  * pmksa_cache_get_current - Get the current used PMKSA entry
382  * @sm: Pointer to WPA state machine data from wpa_sm_init()
383  * Returns: Pointer to the current PMKSA cache entry or %NULL if not available
384  */
pmksa_cache_get_current(struct wpa_sm * sm)385 struct rsn_pmksa_cache_entry * pmksa_cache_get_current(struct wpa_sm *sm)
386 {
387     if (sm == NULL)
388         return NULL;
389     return sm->cur_pmksa;
390 }
391 
392 
393 /**
394  * pmksa_cache_clear_current - Clear the current PMKSA entry selection
395  * @sm: Pointer to WPA state machine data from wpa_sm_init()
396  */
pmksa_cache_clear_current(struct wpa_sm * sm)397 void pmksa_cache_clear_current(struct wpa_sm *sm)
398 {
399     if (sm == NULL)
400         return;
401     sm->cur_pmksa = NULL;
402 }
403 
404 
405 /**
406  * pmksa_cache_set_current - Set the current PMKSA entry selection
407  * @sm: Pointer to WPA state machine data from wpa_sm_init()
408  * @pmkid: PMKID for selecting PMKSA or %NULL if not used
409  * @bssid: BSSID for PMKSA or %NULL if not used
410  * @network_ctx: Network configuration context
411  * @try_opportunistic: Whether to allow opportunistic PMKSA caching
412  * Returns: 0 if PMKSA was found or -1 if no matching entry was found
413  */
pmksa_cache_set_current(struct wpa_sm * sm,const u8 * pmkid,const u8 * bssid,void * network_ctx,int try_opportunistic)414 int pmksa_cache_set_current(struct wpa_sm *sm, const u8 *pmkid,
415         const u8 *bssid, void *network_ctx,
416         int try_opportunistic)
417 {
418     struct rsn_pmksa_cache *pmksa = sm->pmksa;
419     wpa_printf(MSG_DEBUG, "RSN: PMKSA cache search - network_ctx=%p "
420             "try_opportunistic=%d", network_ctx, try_opportunistic);
421     if (pmkid)
422         wpa_hexdump(MSG_DEBUG, "RSN: Search for PMKID",
423                 pmkid, PMKID_LEN);
424     if (bssid)
425         wpa_printf(MSG_DEBUG, "RSN: Search for BSSID " MACSTR,
426                 MAC2STR(bssid));
427 
428     sm->cur_pmksa = NULL;
429     if (pmkid)
430         sm->cur_pmksa = pmksa_cache_get(pmksa, NULL, pmkid,
431                 network_ctx);
432     if (sm->cur_pmksa == NULL && bssid)
433         sm->cur_pmksa = pmksa_cache_get(pmksa, bssid, NULL,
434                 network_ctx);
435     if (sm->cur_pmksa == NULL && try_opportunistic && bssid)
436         sm->cur_pmksa = pmksa_cache_get_opportunistic(pmksa,
437                 network_ctx,
438                 bssid);
439     if (sm->cur_pmksa) {
440         wpa_hexdump(MSG_DEBUG, "RSN: PMKSA cache entry found - PMKID",
441                 sm->cur_pmksa->pmkid, PMKID_LEN);
442         return 0;
443     }
444     wpa_printf(MSG_DEBUG, "RSN: No PMKSA cache entry found");
445     return -1;
446 }
447 
448 
449 /**
450  * pmksa_cache_list - Dump text list of entries in PMKSA cache
451  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
452  * @buf: Buffer for the list
453  * @len: Length of the buffer
454  * Returns: number of bytes written to buffer
455  *
456  * This function is used to generate a text format representation of the
457  * current PMKSA cache contents for the ctrl_iface PMKSA command.
458  */
pmksa_cache_list(struct rsn_pmksa_cache * pmksa,char * buf,size_t len)459 int pmksa_cache_list(struct rsn_pmksa_cache *pmksa, char *buf, size_t len)
460 {
461     int i, ret;
462     char *pos = buf;
463     struct rsn_pmksa_cache_entry *entry;
464     struct os_reltime now;
465     ret = os_snprintf(pos, buf + len - pos,
466             "Index / AA / PMKID / expiration (in seconds) / "
467             "opportunistic\n");
468     if (os_snprintf_error(buf + len - pos, ret))
469         return pos - buf;
470     pos += ret;
471     i = 0;
472     entry = pmksa->pmksa;
473     os_get_reltime(&now);
474     while (entry) {
475         i++;
476         ret = os_snprintf(pos, buf + len - pos, "%d " MACSTR " ",
477                 i, MAC2STR(entry->aa));
478         if (os_snprintf_error(buf + len - pos, ret))
479             return pos - buf;
480         pos += ret;
481         pos += wpa_snprintf_hex(pos, buf + len - pos, entry->pmkid,
482                 PMKID_LEN);
483         ret = os_snprintf(pos, buf + len - pos, " %d %d\n",
484                 (int) (entry->expiration - now.sec),
485                 entry->opportunistic);
486         if (os_snprintf_error(buf + len - pos, ret))
487             return pos - buf;
488         pos += ret;
489         entry = entry->next;
490     }
491     return pos - buf;
492 }
493 
494 
495 /**
496  * pmksa_cache_init - Initialize PMKSA cache
497  * @free_cb: Callback function to be called when a PMKSA cache entry is freed
498  * @ctx: Context pointer for free_cb function
499  * @sm: Pointer to WPA state machine data from wpa_sm_init()
500  * Returns: Pointer to PMKSA cache data or %NULL on failure
501  */
502 struct rsn_pmksa_cache *
pmksa_cache_init(void (* free_cb)(struct rsn_pmksa_cache_entry * entry,void * ctx,enum pmksa_free_reason reason),void * ctx,struct wpa_sm * sm)503 pmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
504             void *ctx, enum pmksa_free_reason reason),
505         void *ctx, struct wpa_sm *sm)
506 {
507     struct rsn_pmksa_cache *pmksa;
508 
509     pmksa = os_zalloc(sizeof(*pmksa));
510     if (pmksa) {
511         pmksa->free_cb = free_cb;
512         pmksa->ctx = ctx;
513         pmksa->sm = sm;
514         pmksa->pmksa_count = 0;
515         pmksa->pmksa = NULL;
516     }
517 
518     return pmksa;
519 }
520 
521 #endif /* IEEE8021X_EAPOL */
522