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