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 
11 #include "common.h"
12 #include "eloop.h"
13 #include "eapol_supp/eapol_supp_sm.h"
14 #include "wpa.h"
15 #include "wpa_i.h"
16 #include "pmksa_cache.h"
17 #include "wpa_supplicant_i.h"
18 #include "notify.h"
19 
20 #if defined(IEEE8021X_EAPOL) && !defined(CONFIG_NO_WPA)
21 
22 static const int pmksa_cache_max_entries = 32;
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 	wpa_sm_remove_pmkid(pmksa->sm, entry->network_ctx, entry->aa,
49 			    entry->pmkid,
50 			    entry->fils_cache_id_set ? entry->fils_cache_id :
51 			    NULL);
52 	pmksa->pmksa_count--;
53 	pmksa->free_cb(entry, pmksa->ctx, reason);
54 	_pmksa_cache_free_entry(entry);
55 }
56 
57 
pmksa_cache_expire(void * eloop_ctx,void * timeout_ctx)58 static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx)
59 {
60 	struct rsn_pmksa_cache *pmksa = eloop_ctx;
61 	struct os_reltime now;
62 
63 	os_get_reltime(&now);
64 	while (pmksa->pmksa && pmksa->pmksa->expiration <= now.sec) {
65 		struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
66 		pmksa->pmksa = entry->next;
67 		wpa_printf(MSG_DEBUG, "RSN: expired PMKSA cache entry for "
68 			   MACSTR, MAC2STR(entry->aa));
69 		pmksa_cache_free_entry(pmksa, entry, PMKSA_EXPIRE);
70 	}
71 
72 	pmksa_cache_set_expiration(pmksa);
73 }
74 
75 
pmksa_cache_reauth(void * eloop_ctx,void * timeout_ctx)76 static void pmksa_cache_reauth(void *eloop_ctx, void *timeout_ctx)
77 {
78 	struct rsn_pmksa_cache *pmksa = eloop_ctx;
79 	pmksa->sm->cur_pmksa = NULL;
80 	eapol_sm_request_reauth(pmksa->sm->eapol);
81 }
82 
83 
pmksa_cache_set_expiration(struct rsn_pmksa_cache * pmksa)84 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa)
85 {
86 	int sec;
87 	struct rsn_pmksa_cache_entry *entry;
88 	struct os_reltime now;
89 
90 	eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
91 	eloop_cancel_timeout(pmksa_cache_reauth, pmksa, NULL);
92 	if (pmksa->pmksa == NULL)
93 		return;
94 	os_get_reltime(&now);
95 	sec = pmksa->pmksa->expiration - now.sec;
96 	if (sec < 0)
97 		sec = 0;
98 	eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, pmksa, NULL);
99 
100 	entry = pmksa->sm->cur_pmksa ? pmksa->sm->cur_pmksa :
101 		pmksa_cache_get(pmksa, pmksa->sm->bssid, NULL, NULL, 0);
102 	if (entry) {
103 		sec = pmksa->pmksa->reauth_time - now.sec;
104 		if (sec < 0)
105 			sec = 0;
106 		eloop_register_timeout(sec, 0, pmksa_cache_reauth, pmksa,
107 				       NULL);
108 	}
109 }
110 
111 
112 /**
113  * pmksa_cache_add - Add a PMKSA cache entry
114  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
115  * @pmk: The new pairwise master key
116  * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
117  * @pmkid: Calculated PMKID
118  * @kck: Key confirmation key or %NULL if not yet derived
119  * @kck_len: KCK length in bytes
120  * @aa: Authenticator address
121  * @spa: Supplicant address
122  * @network_ctx: Network configuration context for this PMK
123  * @akmp: WPA_KEY_MGMT_* used in key derivation
124  * @cache_id: Pointer to FILS Cache Identifier or %NULL if not advertised
125  * Returns: Pointer to the added PMKSA cache entry or %NULL on error
126  *
127  * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
128  * cache. If an old entry is already in the cache for the same Authenticator,
129  * this entry will be replaced with the new entry. PMKID will be calculated
130  * based on the PMK and the driver interface is notified of the new PMKID.
131  */
132 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,const u8 * cache_id)133 pmksa_cache_add(struct rsn_pmksa_cache *pmksa, const u8 *pmk, size_t pmk_len,
134 		const u8 *pmkid, const u8 *kck, size_t kck_len,
135 		const u8 *aa, const u8 *spa, void *network_ctx, int akmp,
136 		const u8 *cache_id)
137 {
138 	struct rsn_pmksa_cache_entry *entry;
139 	struct os_reltime now;
140 
141 	if (pmk_len > PMK_LEN_MAX)
142 		return NULL;
143 
144 	if (wpa_key_mgmt_suite_b(akmp) && !kck)
145 		return NULL;
146 
147 	entry = os_zalloc(sizeof(*entry));
148 	if (entry == NULL)
149 		return NULL;
150 	os_memcpy(entry->pmk, pmk, pmk_len);
151 	entry->pmk_len = pmk_len;
152 	if (pmkid)
153 		os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
154 	else if (akmp == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
155 		rsn_pmkid_suite_b_192(kck, kck_len, aa, spa, entry->pmkid);
156 	else if (wpa_key_mgmt_suite_b(akmp))
157 		rsn_pmkid_suite_b(kck, kck_len, aa, spa, entry->pmkid);
158 	else
159 		rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid, akmp);
160 	os_get_reltime(&now);
161 	entry->expiration = now.sec + pmksa->sm->dot11RSNAConfigPMKLifetime;
162 	entry->reauth_time = now.sec + pmksa->sm->dot11RSNAConfigPMKLifetime *
163 		pmksa->sm->dot11RSNAConfigPMKReauthThreshold / 100;
164 	entry->akmp = akmp;
165 	if (cache_id) {
166 		entry->fils_cache_id_set = 1;
167 		os_memcpy(entry->fils_cache_id, cache_id, FILS_CACHE_ID_LEN);
168 	}
169 	os_memcpy(entry->aa, aa, ETH_ALEN);
170 	entry->network_ctx = network_ctx;
171 
172 	return pmksa_cache_add_entry(pmksa, entry);
173 }
174 
175 
176 struct rsn_pmksa_cache_entry *
pmksa_cache_add_entry(struct rsn_pmksa_cache * pmksa,struct rsn_pmksa_cache_entry * entry)177 pmksa_cache_add_entry(struct rsn_pmksa_cache *pmksa,
178 		      struct rsn_pmksa_cache_entry *entry)
179 {
180 	struct rsn_pmksa_cache_entry *pos, *prev;
181 
182 	/* Replace an old entry for the same Authenticator (if found) with the
183 	 * new entry */
184 	pos = pmksa->pmksa;
185 	prev = NULL;
186 	while (pos) {
187 		if (os_memcmp(entry->aa, pos->aa, ETH_ALEN) == 0) {
188 			if (pos->pmk_len == entry->pmk_len &&
189 			    os_memcmp_const(pos->pmk, entry->pmk,
190 					    entry->pmk_len) == 0 &&
191 			    os_memcmp_const(pos->pmkid, entry->pmkid,
192 					    PMKID_LEN) == 0) {
193 				wpa_printf(MSG_DEBUG, "WPA: reusing previous "
194 					   "PMKSA entry");
195 				os_free(entry);
196 				return pos;
197 			}
198 			if (prev == NULL)
199 				pmksa->pmksa = pos->next;
200 			else
201 				prev->next = pos->next;
202 
203 			/*
204 			 * If OKC is used, there may be other PMKSA cache
205 			 * entries based on the same PMK. These needs to be
206 			 * flushed so that a new entry can be created based on
207 			 * the new PMK. Only clear other entries if they have a
208 			 * matching PMK and this PMK has been used successfully
209 			 * with the current AP, i.e., if opportunistic flag has
210 			 * been cleared in wpa_supplicant_key_neg_complete().
211 			 */
212 			wpa_printf(MSG_DEBUG, "RSN: Replace PMKSA entry for "
213 				   "the current AP and any PMKSA cache entry "
214 				   "that was based on the old PMK");
215 			if (!pos->opportunistic)
216 				pmksa_cache_flush(pmksa, entry->network_ctx,
217 						  pos->pmk, pos->pmk_len);
218 			pmksa_cache_free_entry(pmksa, pos, PMKSA_REPLACE);
219 			break;
220 		}
221 		prev = pos;
222 		pos = pos->next;
223 	}
224 
225 	if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) {
226 		/* Remove the oldest entry to make room for the new entry */
227 		pos = pmksa->pmksa;
228 
229 		if (pos == pmksa->sm->cur_pmksa) {
230 			/*
231 			 * Never remove the current PMKSA cache entry, since
232 			 * it's in use, and removing it triggers a needless
233 			 * deauthentication.
234 			 */
235 			pos = pos->next;
236 			pmksa->pmksa->next = pos ? pos->next : NULL;
237 		} else
238 			pmksa->pmksa = pos->next;
239 
240 		if (pos) {
241 			wpa_printf(MSG_DEBUG, "RSN: removed the oldest idle "
242 				   "PMKSA cache entry (for " MACSTR ") to "
243 				   "make room for new one",
244 				   MAC2STR(pos->aa));
245 			pmksa_cache_free_entry(pmksa, pos, PMKSA_FREE);
246 		}
247 	}
248 
249 	/* Add the new entry; order by expiration time */
250 	pos = pmksa->pmksa;
251 	prev = NULL;
252 	while (pos) {
253 		if (pos->expiration > entry->expiration)
254 			break;
255 		prev = pos;
256 		pos = pos->next;
257 	}
258 	if (prev == NULL) {
259 		entry->next = pmksa->pmksa;
260 		pmksa->pmksa = entry;
261 		pmksa_cache_set_expiration(pmksa);
262 	} else {
263 		entry->next = prev->next;
264 		prev->next = entry;
265 	}
266 	pmksa->pmksa_count++;
267 	wpa_printf(MSG_DEBUG, "RSN: Added PMKSA cache entry for " MACSTR
268 		   " network_ctx=%p akmp=0x%x", MAC2STR(entry->aa),
269 		   entry->network_ctx, entry->akmp);
270 	wpas_notify_pmk_cache_added((struct wpa_supplicant *)pmksa->sm->ctx->ctx, entry);
271 	wpa_sm_add_pmkid(pmksa->sm, entry->network_ctx, entry->aa, entry->pmkid,
272 			 entry->fils_cache_id_set ? entry->fils_cache_id : NULL,
273 			 entry->pmk, entry->pmk_len,
274 			 pmksa->sm->dot11RSNAConfigPMKLifetime,
275 			 pmksa->sm->dot11RSNAConfigPMKReauthThreshold,
276 			 entry->akmp);
277 
278 	return entry;
279 }
280 
281 
282 /**
283  * pmksa_cache_flush - Flush PMKSA cache entries for a specific network
284  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
285  * @network_ctx: Network configuration context or %NULL to flush all entries
286  * @pmk: PMK to match for or %NULL to match all PMKs
287  * @pmk_len: PMK length
288  */
pmksa_cache_flush(struct rsn_pmksa_cache * pmksa,void * network_ctx,const u8 * pmk,size_t pmk_len)289 void pmksa_cache_flush(struct rsn_pmksa_cache *pmksa, void *network_ctx,
290 		       const u8 *pmk, size_t pmk_len)
291 {
292 	struct rsn_pmksa_cache_entry *entry, *prev = NULL, *tmp;
293 	int removed = 0;
294 
295 	entry = pmksa->pmksa;
296 	while (entry) {
297 		if ((entry->network_ctx == network_ctx ||
298 		     network_ctx == NULL) &&
299 		    (pmk == NULL ||
300 		     (pmk_len == entry->pmk_len &&
301 		      os_memcmp(pmk, entry->pmk, pmk_len) == 0))) {
302 			wpa_printf(MSG_DEBUG, "RSN: Flush PMKSA cache entry "
303 				   "for " MACSTR, MAC2STR(entry->aa));
304 			if (prev)
305 				prev->next = entry->next;
306 			else
307 				pmksa->pmksa = entry->next;
308 			tmp = entry;
309 			entry = entry->next;
310 			pmksa_cache_free_entry(pmksa, tmp, PMKSA_FREE);
311 			removed++;
312 		} else {
313 			prev = entry;
314 			entry = entry->next;
315 		}
316 	}
317 	if (removed)
318 		pmksa_cache_set_expiration(pmksa);
319 }
320 
321 
322 /**
323  * pmksa_cache_deinit - Free all entries in PMKSA cache
324  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
325  */
pmksa_cache_deinit(struct rsn_pmksa_cache * pmksa)326 void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa)
327 {
328 	struct rsn_pmksa_cache_entry *entry, *prev;
329 
330 	if (pmksa == NULL)
331 		return;
332 
333 	entry = pmksa->pmksa;
334 	pmksa->pmksa = NULL;
335 	while (entry) {
336 		prev = entry;
337 		entry = entry->next;
338 		os_free(prev);
339 	}
340 	pmksa_cache_set_expiration(pmksa);
341 	os_free(pmksa);
342 }
343 
344 
345 /**
346  * pmksa_cache_get - Fetch a PMKSA cache entry
347  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
348  * @aa: Authenticator address or %NULL to match any
349  * @pmkid: PMKID or %NULL to match any
350  * @network_ctx: Network context or %NULL to match any
351  * @akmp: Specific AKMP to search for or 0 for any
352  * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
353  */
pmksa_cache_get(struct rsn_pmksa_cache * pmksa,const u8 * aa,const u8 * pmkid,const void * network_ctx,int akmp)354 struct rsn_pmksa_cache_entry * pmksa_cache_get(struct rsn_pmksa_cache *pmksa,
355 					       const u8 *aa, const u8 *pmkid,
356 					       const void *network_ctx,
357 					       int akmp)
358 {
359 	struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
360 	while (entry) {
361 		if ((aa == NULL || os_memcmp(entry->aa, aa, ETH_ALEN) == 0) &&
362 		    (pmkid == NULL ||
363 		     os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0) &&
364 		    (!akmp || akmp == entry->akmp) &&
365 		    (network_ctx == NULL || network_ctx == entry->network_ctx))
366 			return entry;
367 		entry = entry->next;
368 	}
369 	return NULL;
370 }
371 
372 
373 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)374 pmksa_cache_clone_entry(struct rsn_pmksa_cache *pmksa,
375 			const struct rsn_pmksa_cache_entry *old_entry,
376 			const u8 *aa)
377 {
378 	struct rsn_pmksa_cache_entry *new_entry;
379 	os_time_t old_expiration = old_entry->expiration;
380 	const u8 *pmkid = NULL;
381 
382 	if (wpa_key_mgmt_sae(old_entry->akmp))
383 		pmkid = old_entry->pmkid;
384 	new_entry = pmksa_cache_add(pmksa, old_entry->pmk, old_entry->pmk_len,
385 				    pmkid, NULL, 0,
386 				    aa, pmksa->sm->own_addr,
387 				    old_entry->network_ctx, old_entry->akmp,
388 				    old_entry->fils_cache_id_set ?
389 				    old_entry->fils_cache_id : NULL);
390 	if (new_entry == NULL)
391 		return NULL;
392 
393 	/* TODO: reorder entries based on expiration time? */
394 	new_entry->expiration = old_expiration;
395 	new_entry->opportunistic = 1;
396 
397 	return new_entry;
398 }
399 
400 
401 /**
402  * pmksa_cache_get_opportunistic - Try to get an opportunistic PMKSA entry
403  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
404  * @network_ctx: Network configuration context
405  * @aa: Authenticator address for the new AP
406  * @akmp: Specific AKMP to search for or 0 for any
407  * Returns: Pointer to a new PMKSA cache entry or %NULL if not available
408  *
409  * Try to create a new PMKSA cache entry opportunistically by guessing that the
410  * new AP is sharing the same PMK as another AP that has the same SSID and has
411  * already an entry in PMKSA cache.
412  */
413 struct rsn_pmksa_cache_entry *
pmksa_cache_get_opportunistic(struct rsn_pmksa_cache * pmksa,void * network_ctx,const u8 * aa,int akmp)414 pmksa_cache_get_opportunistic(struct rsn_pmksa_cache *pmksa, void *network_ctx,
415 			      const u8 *aa, int akmp)
416 {
417 	struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
418 
419 	wpa_printf(MSG_DEBUG, "RSN: Consider " MACSTR " for OKC", MAC2STR(aa));
420 	if (network_ctx == NULL)
421 		return NULL;
422 	while (entry) {
423 		if (entry->network_ctx == network_ctx &&
424 		    (!akmp || entry->akmp == akmp)) {
425 			struct os_reltime now;
426 
427 			if (wpa_key_mgmt_sae(entry->akmp) &&
428 			    os_get_reltime(&now) == 0 &&
429 			    entry->reauth_time < now.sec) {
430 				wpa_printf(MSG_DEBUG,
431 					   "RSN: Do not clone PMKSA cache entry for "
432 					   MACSTR
433 					   " since its reauth threshold has passed",
434 					   MAC2STR(entry->aa));
435 				entry = entry->next;
436 				continue;
437 			}
438 
439 			entry = pmksa_cache_clone_entry(pmksa, entry, aa);
440 			if (entry) {
441 				wpa_printf(MSG_DEBUG, "RSN: added "
442 					   "opportunistic PMKSA cache entry "
443 					   "for " MACSTR, MAC2STR(aa));
444 			}
445 			return entry;
446 		}
447 		entry = entry->next;
448 	}
449 	return NULL;
450 }
451 
452 
453 static struct rsn_pmksa_cache_entry *
pmksa_cache_get_fils_cache_id(struct rsn_pmksa_cache * pmksa,const void * network_ctx,const u8 * cache_id)454 pmksa_cache_get_fils_cache_id(struct rsn_pmksa_cache *pmksa,
455 			      const void *network_ctx, const u8 *cache_id)
456 {
457 	struct rsn_pmksa_cache_entry *entry;
458 
459 	for (entry = pmksa->pmksa; entry; entry = entry->next) {
460 		if (network_ctx == entry->network_ctx &&
461 		    entry->fils_cache_id_set &&
462 		    os_memcmp(cache_id, entry->fils_cache_id,
463 			      FILS_CACHE_ID_LEN) == 0)
464 			return entry;
465 	}
466 
467 	return NULL;
468 }
469 
470 
471 /**
472  * pmksa_cache_get_current - Get the current used PMKSA entry
473  * @sm: Pointer to WPA state machine data from wpa_sm_init()
474  * Returns: Pointer to the current PMKSA cache entry or %NULL if not available
475  */
pmksa_cache_get_current(struct wpa_sm * sm)476 struct rsn_pmksa_cache_entry * pmksa_cache_get_current(struct wpa_sm *sm)
477 {
478 	if (sm == NULL)
479 		return NULL;
480 	return sm->cur_pmksa;
481 }
482 
483 
484 /**
485  * pmksa_cache_clear_current - Clear the current PMKSA entry selection
486  * @sm: Pointer to WPA state machine data from wpa_sm_init()
487  */
pmksa_cache_clear_current(struct wpa_sm * sm)488 void pmksa_cache_clear_current(struct wpa_sm *sm)
489 {
490 	if (sm == NULL)
491 		return;
492 	if (sm->cur_pmksa)
493 		wpa_printf(MSG_DEBUG,
494 			   "RSN: Clear current PMKSA entry selection");
495 	sm->cur_pmksa = NULL;
496 }
497 
498 
499 /**
500  * pmksa_cache_set_current - Set the current PMKSA entry selection
501  * @sm: Pointer to WPA state machine data from wpa_sm_init()
502  * @pmkid: PMKID for selecting PMKSA or %NULL if not used
503  * @bssid: BSSID for PMKSA or %NULL if not used
504  * @network_ctx: Network configuration context
505  * @try_opportunistic: Whether to allow opportunistic PMKSA caching
506  * @fils_cache_id: Pointer to FILS Cache Identifier or %NULL if not used
507  * Returns: 0 if PMKSA was found or -1 if no matching entry was found
508  */
pmksa_cache_set_current(struct wpa_sm * sm,const u8 * pmkid,const u8 * bssid,void * network_ctx,int try_opportunistic,const u8 * fils_cache_id,int akmp)509 int pmksa_cache_set_current(struct wpa_sm *sm, const u8 *pmkid,
510 			    const u8 *bssid, void *network_ctx,
511 			    int try_opportunistic, const u8 *fils_cache_id,
512 			    int akmp)
513 {
514 	struct rsn_pmksa_cache *pmksa = sm->pmksa;
515 	wpa_printf(MSG_DEBUG, "RSN: PMKSA cache search - network_ctx=%p "
516 		   "try_opportunistic=%d akmp=0x%x",
517 		   network_ctx, try_opportunistic, akmp);
518 	if (pmkid)
519 		wpa_hexdump(MSG_DEBUG, "RSN: Search for PMKID",
520 			    pmkid, PMKID_LEN);
521 	if (bssid)
522 		wpa_printf(MSG_DEBUG, "RSN: Search for BSSID " MACSTR,
523 			   MAC2STR(bssid));
524 	if (fils_cache_id)
525 		wpa_printf(MSG_DEBUG,
526 			   "RSN: Search for FILS Cache Identifier %02x%02x",
527 			   fils_cache_id[0], fils_cache_id[1]);
528 
529 	sm->cur_pmksa = NULL;
530 	if (pmkid)
531 		sm->cur_pmksa = pmksa_cache_get(pmksa, NULL, pmkid,
532 						network_ctx, akmp);
533 	if (sm->cur_pmksa == NULL && bssid)
534 		sm->cur_pmksa = pmksa_cache_get(pmksa, bssid, NULL,
535 						network_ctx, akmp);
536 	if (sm->cur_pmksa == NULL && try_opportunistic && bssid)
537 		sm->cur_pmksa = pmksa_cache_get_opportunistic(pmksa,
538 							      network_ctx,
539 							      bssid, akmp);
540 	if (sm->cur_pmksa == NULL && fils_cache_id)
541 		sm->cur_pmksa = pmksa_cache_get_fils_cache_id(pmksa,
542 							      network_ctx,
543 							      fils_cache_id);
544 	if (sm->cur_pmksa) {
545 		struct os_reltime now;
546 
547 		if (wpa_key_mgmt_sae(sm->cur_pmksa->akmp) &&
548 		    os_get_reltime(&now) == 0 &&
549 		    sm->cur_pmksa->reauth_time < now.sec) {
550 			wpa_printf(MSG_DEBUG,
551 				   "RSN: Do not allow PMKSA cache entry for "
552 				   MACSTR
553 				   " to be used for SAE since its reauth threshold has passed",
554 				   MAC2STR(sm->cur_pmksa->aa));
555 			sm->cur_pmksa = NULL;
556 			return -1;
557 		}
558 
559 		wpa_hexdump(MSG_DEBUG, "RSN: PMKSA cache entry found - PMKID",
560 			    sm->cur_pmksa->pmkid, PMKID_LEN);
561 		return 0;
562 	}
563 	wpa_printf(MSG_DEBUG, "RSN: No PMKSA cache entry found");
564 	return -1;
565 }
566 
567 
568 /**
569  * pmksa_cache_list - Dump text list of entries in PMKSA cache
570  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
571  * @buf: Buffer for the list
572  * @len: Length of the buffer
573  * Returns: number of bytes written to buffer
574  *
575  * This function is used to generate a text format representation of the
576  * current PMKSA cache contents for the ctrl_iface PMKSA command.
577  */
pmksa_cache_list(struct rsn_pmksa_cache * pmksa,char * buf,size_t len)578 int pmksa_cache_list(struct rsn_pmksa_cache *pmksa, char *buf, size_t len)
579 {
580 	int i, ret;
581 	char *pos = buf;
582 	struct rsn_pmksa_cache_entry *entry;
583 	struct os_reltime now;
584 	int cache_id_used = 0;
585 
586 	for (entry = pmksa->pmksa; entry; entry = entry->next) {
587 		if (entry->fils_cache_id_set) {
588 			cache_id_used = 1;
589 			break;
590 		}
591 	}
592 
593 	os_get_reltime(&now);
594 	ret = os_snprintf(pos, buf + len - pos,
595 			  "Index / AA / PMKID / expiration (in seconds) / "
596 			  "opportunistic%s\n",
597 			  cache_id_used ? " / FILS Cache Identifier" : "");
598 	if (os_snprintf_error(buf + len - pos, ret))
599 		return pos - buf;
600 	pos += ret;
601 	i = 0;
602 	entry = pmksa->pmksa;
603 	while (entry) {
604 		i++;
605 		ret = os_snprintf(pos, buf + len - pos, "%d " MACSTR " ",
606 				  i, MAC2STR(entry->aa));
607 		if (os_snprintf_error(buf + len - pos, ret))
608 			return pos - buf;
609 		pos += ret;
610 		pos += wpa_snprintf_hex(pos, buf + len - pos, entry->pmkid,
611 					PMKID_LEN);
612 		ret = os_snprintf(pos, buf + len - pos, " %d %d",
613 				  (int) (entry->expiration - now.sec),
614 				  entry->opportunistic);
615 		if (os_snprintf_error(buf + len - pos, ret))
616 			return pos - buf;
617 		pos += ret;
618 		if (entry->fils_cache_id_set) {
619 			ret = os_snprintf(pos, buf + len - pos, " %02x%02x",
620 					  entry->fils_cache_id[0],
621 					  entry->fils_cache_id[1]);
622 			if (os_snprintf_error(buf + len - pos, ret))
623 				return pos - buf;
624 			pos += ret;
625 		}
626 		ret = os_snprintf(pos, buf + len - pos, "\n");
627 		if (os_snprintf_error(buf + len - pos, ret))
628 			return pos - buf;
629 		pos += ret;
630 		entry = entry->next;
631 	}
632 	return pos - buf;
633 }
634 
635 
pmksa_cache_head(struct rsn_pmksa_cache * pmksa)636 struct rsn_pmksa_cache_entry * pmksa_cache_head(struct rsn_pmksa_cache *pmksa)
637 {
638 	return pmksa->pmksa;
639 }
640 
641 
642 /**
643  * pmksa_cache_init - Initialize PMKSA cache
644  * @free_cb: Callback function to be called when a PMKSA cache entry is freed
645  * @ctx: Context pointer for free_cb function
646  * @sm: Pointer to WPA state machine data from wpa_sm_init()
647  * Returns: Pointer to PMKSA cache data or %NULL on failure
648  */
649 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)650 pmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
651 				 void *ctx, enum pmksa_free_reason reason),
652 		 void *ctx, struct wpa_sm *sm)
653 {
654 	struct rsn_pmksa_cache *pmksa;
655 
656 	pmksa = os_zalloc(sizeof(*pmksa));
657 	if (pmksa) {
658 		pmksa->free_cb = free_cb;
659 		pmksa->ctx = ctx;
660 		pmksa->sm = sm;
661 	}
662 
663 	return pmksa;
664 }
665 
666 #endif /* IEEE8021X_EAPOL */
667