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