Coverage Report

Created: 2026-03-19 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hostap/src/rsn_supp/pmksa_cache.c
Line
Count
Source
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
20
static const int pmksa_cache_max_entries = 32;
21
22
struct 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
  bool (*is_current_cb)(struct rsn_pmksa_cache_entry *entry,
30
            void *ctx);
31
  void (*notify_cb)(struct rsn_pmksa_cache_entry *entry, void *ctx);
32
  void *ctx;
33
};
34
35
36
static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa);
37
38
39
static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry *entry)
40
0
{
41
0
  bin_clear_free(entry, sizeof(*entry));
42
0
}
43
44
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
0
{
49
0
  if (pmksa->sm)
50
0
    wpa_sm_remove_pmkid(pmksa->sm, entry->network_ctx, entry->aa,
51
0
            entry->pmkid,
52
0
            entry->fils_cache_id_set ?
53
0
            entry->fils_cache_id : NULL);
54
0
  pmksa->pmksa_count--;
55
0
  if (pmksa->free_cb)
56
0
    pmksa->free_cb(entry, pmksa->ctx, reason);
57
0
  _pmksa_cache_free_entry(entry);
58
0
}
59
60
61
void pmksa_cache_remove(struct rsn_pmksa_cache *pmksa,
62
      struct rsn_pmksa_cache_entry *entry)
63
0
{
64
0
  struct rsn_pmksa_cache_entry *e;
65
66
0
  e = pmksa->pmksa;
67
0
  while (e) {
68
0
    if (e == entry) {
69
0
      pmksa->pmksa = entry->next;
70
0
      break;
71
0
    }
72
0
    if (e->next == entry) {
73
0
      e->next = entry->next;
74
0
      break;
75
0
    }
76
0
  }
77
78
0
  if (!e) {
79
0
    wpa_printf(MSG_DEBUG,
80
0
         "RSN: Could not remove PMKSA cache entry %p since it is not in the list",
81
0
         entry);
82
0
    return;
83
0
  }
84
85
0
  pmksa_cache_free_entry(pmksa, entry, PMKSA_FREE);
86
0
}
87
88
89
static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx)
90
0
{
91
0
  struct rsn_pmksa_cache *pmksa = eloop_ctx;
92
0
  struct os_reltime now;
93
0
  struct rsn_pmksa_cache_entry *prev = NULL, *tmp;
94
0
  struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
95
96
0
  os_get_reltime(&now);
97
0
  while (entry && entry->expiration <= now.sec) {
98
0
    if (wpa_key_mgmt_sae(entry->akmp) && pmksa->is_current_cb &&
99
0
        pmksa->is_current_cb(entry, pmksa->ctx)) {
100
      /* Do not expire the currently used PMKSA entry for SAE
101
       * since there is no convenient mechanism for
102
       * reauthenticating during an association with SAE. The
103
       * expired entry will be removed after this association
104
       * has been lost. */
105
0
      wpa_printf(MSG_DEBUG,
106
0
           "RSN: postpone PMKSA cache entry expiration for SAE with "
107
0
           MACSTR, MAC2STR(entry->aa));
108
0
      prev = entry;
109
0
      entry = entry->next;
110
0
      continue;
111
0
    }
112
113
0
    wpa_printf(MSG_DEBUG, "RSN: expired PMKSA cache entry for "
114
0
         MACSTR, MAC2STR(entry->aa));
115
0
    if (prev)
116
0
      prev->next = entry->next;
117
0
    else
118
0
      pmksa->pmksa = entry->next;
119
0
    tmp = entry;
120
0
    entry = entry->next;
121
0
    pmksa_cache_free_entry(pmksa, tmp, PMKSA_EXPIRE);
122
0
  }
123
124
0
  pmksa_cache_set_expiration(pmksa);
125
0
}
126
127
128
static void pmksa_cache_reauth(void *eloop_ctx, void *timeout_ctx)
129
0
{
130
0
  struct rsn_pmksa_cache *pmksa = eloop_ctx;
131
132
0
  if (!pmksa->sm)
133
0
    return;
134
135
0
  if (pmksa->sm->driver_bss_selection) {
136
0
    struct rsn_pmksa_cache_entry *entry;
137
138
0
    entry = pmksa->sm->cur_pmksa ?
139
0
      pmksa->sm->cur_pmksa :
140
0
      pmksa_cache_get(pmksa, pmksa->sm->bssid, NULL, NULL,
141
0
          NULL, 0);
142
0
    if (entry && wpa_key_mgmt_sae(entry->akmp)) {
143
0
      wpa_printf(MSG_DEBUG,
144
0
           "RSN: remove reauth threshold passed PMKSA from the driver for SAE");
145
0
      entry->sae_reauth_scheduled = true;
146
0
      wpa_sm_remove_pmkid(pmksa->sm, entry->network_ctx,
147
0
              entry->aa, entry->pmkid, NULL);
148
0
      return;
149
0
    }
150
0
  }
151
152
0
  pmksa->sm->cur_pmksa = NULL;
153
0
  eapol_sm_request_reauth(pmksa->sm->eapol);
154
0
}
155
156
157
static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa)
158
1.98k
{
159
1.98k
  int sec;
160
1.98k
  struct rsn_pmksa_cache_entry *entry;
161
1.98k
  struct os_reltime now;
162
163
1.98k
  eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
164
1.98k
  eloop_cancel_timeout(pmksa_cache_reauth, pmksa, NULL);
165
1.98k
  if (pmksa->pmksa == NULL)
166
1.98k
    return;
167
0
  os_get_reltime(&now);
168
0
  sec = pmksa->pmksa->expiration - now.sec;
169
0
  if (sec < 0) {
170
0
    sec = 0;
171
0
    if (wpa_key_mgmt_sae(pmksa->pmksa->akmp) &&
172
0
        pmksa->is_current_cb &&
173
0
        pmksa->is_current_cb(pmksa->pmksa, pmksa->ctx)) {
174
      /* Do not continue polling for the current PMKSA entry
175
       * from SAE to expire every second. Use the expiration
176
       * time to the following entry, if any, and wait at
177
       * maximum 10 minutes to check again.
178
       */
179
0
      entry = pmksa->pmksa->next;
180
0
      if (entry) {
181
0
        sec = entry->expiration - now.sec;
182
0
        if (sec < 0)
183
0
          sec = 0;
184
0
        else if (sec > 600)
185
0
          sec = 600;
186
0
      } else {
187
0
        sec = 600;
188
0
      }
189
0
    }
190
0
  }
191
0
  eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, pmksa, NULL);
192
193
0
  if (!pmksa->sm)
194
0
    return;
195
196
0
  entry = pmksa->sm->cur_pmksa ? pmksa->sm->cur_pmksa :
197
0
    pmksa_cache_get(pmksa, pmksa->sm->bssid, NULL, NULL, NULL, 0);
198
0
  if (entry &&
199
0
      (!wpa_key_mgmt_sae(entry->akmp) ||
200
0
       (pmksa->sm->driver_bss_selection &&
201
0
        !entry->sae_reauth_scheduled))) {
202
0
    sec = pmksa->pmksa->reauth_time - now.sec;
203
0
    if (sec < 0)
204
0
      sec = 0;
205
0
    eloop_register_timeout(sec, 0, pmksa_cache_reauth, pmksa,
206
0
               NULL);
207
0
  }
208
0
}
209
210
211
/**
212
 * pmksa_cache_add - Add a PMKSA cache entry
213
 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
214
 * @pmk: The new pairwise master key
215
 * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
216
 * @pmkid: Calculated PMKID
217
 * @kck: Key confirmation key or %NULL if not yet derived
218
 * @kck_len: KCK length in bytes
219
 * @aa: Authenticator address
220
 * @spa: Supplicant address
221
 * @network_ctx: Network configuration context for this PMK
222
 * @akmp: WPA_KEY_MGMT_* used in key derivation
223
 * @cache_id: Pointer to FILS Cache Identifier or %NULL if not advertised
224
 * @auth_alg: Authentication algorithm used for PMK derivation
225
 * Returns: Pointer to the added PMKSA cache entry or %NULL on error
226
 *
227
 * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
228
 * cache. If an old entry is already in the cache for the same Authenticator,
229
 * this entry will be replaced with the new entry. PMKID will be calculated
230
 * based on the PMK and the driver interface is notified of the new PMKID.
231
 */
232
struct rsn_pmksa_cache_entry *
233
pmksa_cache_add(struct rsn_pmksa_cache *pmksa, const u8 *pmk, size_t pmk_len,
234
    const u8 *pmkid, const u8 *kck, size_t kck_len,
235
    const u8 *aa, const u8 *spa, void *network_ctx, int akmp,
236
    const u8 *cache_id, u16 auth_alg)
237
0
{
238
0
  struct rsn_pmksa_cache_entry *entry;
239
0
  struct os_reltime now;
240
0
  unsigned int pmk_lifetime = 43200;
241
0
  unsigned int pmk_reauth_threshold = 70;
242
243
0
  if (pmk_len > PMK_LEN_MAX)
244
0
    return NULL;
245
246
0
  if (kck_len > WPA_KCK_MAX_LEN)
247
0
    return NULL;
248
249
0
  if (wpa_key_mgmt_suite_b(akmp) && !kck)
250
0
    return NULL;
251
252
0
  entry = os_zalloc(sizeof(*entry));
253
0
  if (entry == NULL)
254
0
    return NULL;
255
0
  os_memcpy(entry->pmk, pmk, pmk_len);
256
0
  entry->pmk_len = pmk_len;
257
0
  if (kck_len > 0)
258
0
    os_memcpy(entry->kck, kck, kck_len);
259
0
  entry->kck_len = kck_len;
260
0
  if (pmkid)
261
0
    os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
262
0
  else if (akmp == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
263
0
    rsn_pmkid_suite_b_192(kck, kck_len, aa, spa, entry->pmkid);
264
0
  else if (wpa_key_mgmt_suite_b(akmp))
265
0
    rsn_pmkid_suite_b(kck, kck_len, aa, spa, entry->pmkid);
266
0
  else
267
0
    rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid, akmp);
268
0
  os_get_reltime(&now);
269
0
  if (pmksa->sm) {
270
0
    pmk_lifetime = pmksa->sm->dot11RSNAConfigPMKLifetime;
271
0
    pmk_reauth_threshold =
272
0
      pmksa->sm->dot11RSNAConfigPMKReauthThreshold;
273
0
  }
274
0
  entry->expiration = now.sec + pmk_lifetime;
275
0
  entry->reauth_time = now.sec +
276
0
    pmk_lifetime * pmk_reauth_threshold / 100;
277
0
  entry->akmp = akmp;
278
0
  entry->auth_alg = auth_alg;
279
0
  if (cache_id) {
280
0
    entry->fils_cache_id_set = 1;
281
0
    os_memcpy(entry->fils_cache_id, cache_id, FILS_CACHE_ID_LEN);
282
0
  }
283
0
  os_memcpy(entry->aa, aa, ETH_ALEN);
284
0
  os_memcpy(entry->spa, spa, ETH_ALEN);
285
0
  entry->network_ctx = network_ctx;
286
287
0
  return pmksa_cache_add_entry(pmksa, entry);
288
0
}
289
290
291
/**
292
 * pmksa_cache_add_to_driver - Add PMKSA entry to driver with remaining lifetime
293
 * @pmksa: Pointer to PMKSA cache data
294
 * @entry: PMKSA cache entry
295
 *
296
 * Calculate the remaining PMK lifetime and reauthentication threshold based
297
 * on the entry's expiration and reauthentication times relative to the current
298
 * time, and configure the PMKSA entry to the driver with the current values.
299
 */
300
static void pmksa_cache_add_to_driver(struct rsn_pmksa_cache *pmksa,
301
              struct rsn_pmksa_cache_entry *entry)
302
0
{
303
0
  struct os_reltime now;
304
0
  u32 remaining_lifetime;
305
0
  u8 remaining_reauth_threshold;
306
307
0
  if (!pmksa->sm)
308
0
    return;
309
310
0
  os_get_reltime(&now);
311
312
   /* Do not configure entries that have expired or require immediate
313
    * reauthentication. */
314
0
  if (entry->expiration - now.sec < 1 ||
315
0
      entry->reauth_time - now.sec < 1)
316
0
    return;
317
318
0
  remaining_lifetime = entry->expiration - now.sec;
319
0
  remaining_reauth_threshold = ((entry->reauth_time - now.sec) * 100) /
320
0
              remaining_lifetime;
321
322
  /* Do not configure entries that have already reached reauthentication
323
   * threshold. */
324
0
  if (!remaining_reauth_threshold)
325
0
    return;
326
327
0
  wpa_sm_add_pmkid(pmksa->sm, entry->network_ctx, entry->aa, entry->pmkid,
328
0
       entry->fils_cache_id_set ? entry->fils_cache_id : NULL,
329
0
       entry->pmk, entry->pmk_len, remaining_lifetime,
330
0
       remaining_reauth_threshold, entry->akmp);
331
0
}
332
333
334
struct rsn_pmksa_cache_entry *
335
pmksa_cache_add_entry(struct rsn_pmksa_cache *pmksa,
336
          struct rsn_pmksa_cache_entry *entry)
337
0
{
338
0
  struct rsn_pmksa_cache_entry *pos, *prev;
339
340
  /* Replace an old entry for the same Authenticator (if found) with the
341
   * new entry */
342
0
  pos = pmksa->pmksa;
343
0
  prev = NULL;
344
0
  while (pos) {
345
0
    if (ether_addr_equal(entry->aa, pos->aa) &&
346
0
        ether_addr_equal(entry->spa, pos->spa)) {
347
0
      if (pos->pmk_len == entry->pmk_len &&
348
0
          os_memcmp_const(pos->pmk, entry->pmk,
349
0
              entry->pmk_len) == 0 &&
350
0
          os_memcmp_const(pos->pmkid, entry->pmkid,
351
0
              PMKID_LEN) == 0) {
352
0
        wpa_printf(MSG_DEBUG, "WPA: reusing previous "
353
0
             "PMKSA entry");
354
0
        os_free(entry);
355
0
        return pos;
356
0
      }
357
0
      if (prev == NULL)
358
0
        pmksa->pmksa = pos->next;
359
0
      else
360
0
        prev->next = pos->next;
361
362
      /*
363
       * If OKC is used, there may be other PMKSA cache
364
       * entries based on the same PMK. These needs to be
365
       * flushed so that a new entry can be created based on
366
       * the new PMK. Only clear other entries if they have a
367
       * matching PMK and this PMK has been used successfully
368
       * with the current AP, i.e., if opportunistic flag has
369
       * been cleared in wpa_supplicant_key_neg_complete().
370
       */
371
0
      wpa_printf(MSG_DEBUG, "RSN: Replace PMKSA entry for "
372
0
           "the current AP and any PMKSA cache entry "
373
0
           "that was based on the old PMK");
374
0
      if (!pos->opportunistic)
375
0
        pmksa_cache_flush(pmksa, entry->network_ctx,
376
0
              pos->pmk, pos->pmk_len,
377
0
              false);
378
0
      pmksa_cache_free_entry(pmksa, pos, PMKSA_REPLACE);
379
0
      break;
380
0
    }
381
0
    prev = pos;
382
0
    pos = pos->next;
383
0
  }
384
385
0
  if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) {
386
    /* Remove the oldest entry to make room for the new entry */
387
0
    pos = pmksa->pmksa;
388
389
0
    if (pmksa->sm && pos == pmksa->sm->cur_pmksa) {
390
      /*
391
       * Never remove the current PMKSA cache entry, since
392
       * it's in use, and removing it triggers a needless
393
       * deauthentication.
394
       */
395
0
      pos = pos->next;
396
0
      pmksa->pmksa->next = pos ? pos->next : NULL;
397
0
    } else
398
0
      pmksa->pmksa = pos->next;
399
400
0
    if (pos) {
401
0
      wpa_printf(MSG_DEBUG, "RSN: removed the oldest idle "
402
0
           "PMKSA cache entry (for " MACSTR ") to "
403
0
           "make room for new one",
404
0
           MAC2STR(pos->aa));
405
0
      pmksa_cache_free_entry(pmksa, pos, PMKSA_FREE);
406
0
    }
407
0
  }
408
409
  /* Add the new entry; order by expiration time */
410
0
  pos = pmksa->pmksa;
411
0
  prev = NULL;
412
0
  while (pos) {
413
0
    if (pos->expiration > entry->expiration)
414
0
      break;
415
0
    prev = pos;
416
0
    pos = pos->next;
417
0
  }
418
0
  if (prev == NULL) {
419
0
    entry->next = pmksa->pmksa;
420
0
    pmksa->pmksa = entry;
421
0
    pmksa_cache_set_expiration(pmksa);
422
0
  } else {
423
0
    entry->next = prev->next;
424
0
    prev->next = entry;
425
0
  }
426
0
  pmksa->pmksa_count++;
427
0
  wpa_printf(MSG_DEBUG, "RSN: Added PMKSA cache entry for " MACSTR
428
0
       " spa=" MACSTR " network_ctx=%p akmp=0x%x",
429
0
       MAC2STR(entry->aa), MAC2STR(entry->spa),
430
0
       entry->network_ctx, entry->akmp);
431
432
0
  if (!pmksa->sm)
433
0
    return entry;
434
435
0
  if (pmksa->notify_cb)
436
0
    pmksa->notify_cb(entry, pmksa->ctx);
437
438
0
  pmksa_cache_add_to_driver(pmksa, entry);
439
440
0
  return entry;
441
0
}
442
443
444
/**
445
 * pmksa_cache_flush - Flush PMKSA cache entries for a specific network
446
 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
447
 * @network_ctx: Network configuration context or %NULL to flush all entries
448
 * @pmk: PMK to match for or %NULL to match all PMKs
449
 * @pmk_len: PMK length
450
 * @external_only: Flush only PMKSA cache entries configured by external
451
 * applications
452
 */
453
void pmksa_cache_flush(struct rsn_pmksa_cache *pmksa, void *network_ctx,
454
           const u8 *pmk, size_t pmk_len, bool external_only)
455
0
{
456
0
  struct rsn_pmksa_cache_entry *entry, *prev = NULL, *tmp;
457
0
  int removed = 0;
458
459
0
  entry = pmksa->pmksa;
460
0
  while (entry) {
461
0
    if ((entry->network_ctx == network_ctx ||
462
0
         network_ctx == NULL) &&
463
0
        (pmk == NULL ||
464
0
         (pmk_len == entry->pmk_len &&
465
0
          os_memcmp(pmk, entry->pmk, pmk_len) == 0)) &&
466
0
        (!external_only || entry->external)) {
467
0
      wpa_printf(MSG_DEBUG, "RSN: Flush PMKSA cache entry "
468
0
           "for " MACSTR, MAC2STR(entry->aa));
469
0
      if (prev)
470
0
        prev->next = entry->next;
471
0
      else
472
0
        pmksa->pmksa = entry->next;
473
0
      tmp = entry;
474
0
      entry = entry->next;
475
0
      pmksa_cache_free_entry(pmksa, tmp, PMKSA_FREE);
476
0
      removed++;
477
0
    } else {
478
0
      prev = entry;
479
0
      entry = entry->next;
480
0
    }
481
0
  }
482
0
  if (removed)
483
0
    pmksa_cache_set_expiration(pmksa);
484
0
}
485
486
487
/**
488
 * pmksa_cache_deinit - Free all entries in PMKSA cache
489
 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
490
 */
491
void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa)
492
1.98k
{
493
1.98k
  struct rsn_pmksa_cache_entry *entry, *prev;
494
495
1.98k
  if (pmksa == NULL)
496
0
    return;
497
498
1.98k
  entry = pmksa->pmksa;
499
1.98k
  pmksa->pmksa = NULL;
500
1.98k
  while (entry) {
501
0
    prev = entry;
502
0
    entry = entry->next;
503
0
    os_free(prev);
504
0
  }
505
1.98k
  pmksa_cache_set_expiration(pmksa);
506
1.98k
  os_free(pmksa);
507
1.98k
}
508
509
510
/**
511
 * pmksa_cache_get - Fetch a PMKSA cache entry
512
 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
513
 * @aa: Authenticator address or %NULL to match any
514
 * @pmkid: PMKID or %NULL to match any
515
 * @network_ctx: Network context or %NULL to match any
516
 * @akmp: Specific AKMP to search for or 0 for any
517
 * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
518
 */
519
struct rsn_pmksa_cache_entry * pmksa_cache_get(struct rsn_pmksa_cache *pmksa,
520
                 const u8 *aa, const u8 *spa,
521
                 const u8 *pmkid,
522
                 const void *network_ctx,
523
                 int akmp)
524
8
{
525
8
  struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
526
8
  while (entry) {
527
0
    if ((aa == NULL || ether_addr_equal(entry->aa, aa)) &&
528
0
        (!spa || ether_addr_equal(entry->spa, spa)) &&
529
0
        (pmkid == NULL ||
530
0
         os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0) &&
531
0
        (!akmp || akmp == entry->akmp) &&
532
0
        (network_ctx == NULL || network_ctx == entry->network_ctx))
533
0
      return entry;
534
0
    entry = entry->next;
535
0
  }
536
8
  return NULL;
537
8
}
538
539
540
static struct rsn_pmksa_cache_entry *
541
pmksa_cache_clone_entry(struct rsn_pmksa_cache *pmksa,
542
      const struct rsn_pmksa_cache_entry *old_entry,
543
      const u8 *aa)
544
0
{
545
0
  struct rsn_pmksa_cache_entry *new_entry;
546
0
  os_time_t old_expiration = old_entry->expiration;
547
0
  os_time_t old_reauth_time = old_entry->reauth_time;
548
0
  const u8 *pmkid = NULL;
549
550
0
  if (!pmksa->sm)
551
0
    return NULL;
552
553
0
  if (wpa_key_mgmt_sae(old_entry->akmp) ||
554
0
      wpa_key_mgmt_fils(old_entry->akmp))
555
0
    pmkid = old_entry->pmkid;
556
0
  new_entry = pmksa_cache_add(pmksa, old_entry->pmk, old_entry->pmk_len,
557
0
            pmkid, old_entry->kck, old_entry->kck_len,
558
0
            aa, pmksa->sm->own_addr,
559
0
            old_entry->network_ctx, old_entry->akmp,
560
0
            old_entry->fils_cache_id_set ?
561
0
            old_entry->fils_cache_id : NULL,
562
0
            old_entry->auth_alg);
563
0
  if (new_entry == NULL)
564
0
    return NULL;
565
566
  /* TODO: reorder entries based on expiration time? */
567
0
  new_entry->expiration = old_expiration;
568
0
  new_entry->reauth_time = old_reauth_time;
569
0
  new_entry->opportunistic = 1;
570
571
0
  return new_entry;
572
0
}
573
574
575
/**
576
 * pmksa_cache_get_opportunistic - Try to get an opportunistic PMKSA entry
577
 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
578
 * @network_ctx: Network configuration context
579
 * @aa: Authenticator address for the new AP
580
 * @akmp: Specific AKMP to search for or 0 for any
581
 * Returns: Pointer to a new PMKSA cache entry or %NULL if not available
582
 *
583
 * Try to create a new PMKSA cache entry opportunistically by guessing that the
584
 * new AP is sharing the same PMK as another AP that has the same SSID and has
585
 * already an entry in PMKSA cache.
586
 */
587
struct rsn_pmksa_cache_entry *
588
pmksa_cache_get_opportunistic(struct rsn_pmksa_cache *pmksa, void *network_ctx,
589
            const u8 *aa, int akmp)
590
0
{
591
0
  struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
592
593
0
  wpa_printf(MSG_DEBUG, "RSN: Consider " MACSTR " for OKC", MAC2STR(aa));
594
0
  if (network_ctx == NULL)
595
0
    return NULL;
596
0
  while (entry) {
597
0
    if (entry->network_ctx == network_ctx &&
598
0
        (!akmp || entry->akmp == akmp)) {
599
0
      struct os_reltime now;
600
601
0
      if (wpa_key_mgmt_sae(entry->akmp) &&
602
0
          os_get_reltime(&now) == 0 &&
603
0
          entry->reauth_time < now.sec) {
604
0
        wpa_printf(MSG_DEBUG,
605
0
             "RSN: Do not clone PMKSA cache entry for "
606
0
             MACSTR
607
0
             " since its reauth threshold has passed",
608
0
             MAC2STR(entry->aa));
609
0
        entry = entry->next;
610
0
        continue;
611
0
      }
612
613
0
      entry = pmksa_cache_clone_entry(pmksa, entry, aa);
614
0
      if (entry) {
615
0
        wpa_printf(MSG_DEBUG, "RSN: added "
616
0
             "opportunistic PMKSA cache entry "
617
0
             "for " MACSTR, MAC2STR(aa));
618
0
      }
619
0
      return entry;
620
0
    }
621
0
    entry = entry->next;
622
0
  }
623
0
  return NULL;
624
0
}
625
626
627
static struct rsn_pmksa_cache_entry *
628
pmksa_cache_get_fils_cache_id(struct rsn_pmksa_cache *pmksa,
629
            const void *network_ctx, const u8 *cache_id)
630
0
{
631
0
  struct rsn_pmksa_cache_entry *entry;
632
633
0
  for (entry = pmksa->pmksa; entry; entry = entry->next) {
634
0
    if (network_ctx == entry->network_ctx &&
635
0
        entry->fils_cache_id_set &&
636
0
        os_memcmp(cache_id, entry->fils_cache_id,
637
0
            FILS_CACHE_ID_LEN) == 0)
638
0
      return entry;
639
0
  }
640
641
0
  return NULL;
642
0
}
643
644
645
/**
646
 * pmksa_cache_get_current - Get the current used PMKSA entry
647
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
648
 * Returns: Pointer to the current PMKSA cache entry or %NULL if not available
649
 */
650
struct rsn_pmksa_cache_entry * pmksa_cache_get_current(struct wpa_sm *sm)
651
0
{
652
0
  if (sm == NULL)
653
0
    return NULL;
654
0
  return sm->cur_pmksa;
655
0
}
656
657
658
/**
659
 * pmksa_cache_clear_current - Clear the current PMKSA entry selection
660
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
661
 */
662
void pmksa_cache_clear_current(struct wpa_sm *sm)
663
0
{
664
0
  if (sm == NULL)
665
0
    return;
666
0
  if (sm->cur_pmksa)
667
0
    wpa_printf(MSG_DEBUG,
668
0
         "RSN: Clear current PMKSA entry selection");
669
0
  sm->cur_pmksa = NULL;
670
0
}
671
672
673
/**
674
 * pmksa_cache_set_current - Set the current PMKSA entry selection
675
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
676
 * @pmkid: PMKID for selecting PMKSA or %NULL if not used
677
 * @bssid: BSSID for PMKSA or %NULL if not used
678
 * @network_ctx: Network configuration context
679
 * @try_opportunistic: Whether to allow opportunistic PMKSA caching
680
 * @fils_cache_id: Pointer to FILS Cache Identifier or %NULL if not used
681
 * @akmp: Specific AKMP to search for or 0 for any
682
 * @associated: Whether the device is associated
683
 * Returns: 0 if PMKSA was found or -1 if no matching entry was found
684
 */
685
int pmksa_cache_set_current(struct wpa_sm *sm, const u8 *pmkid,
686
          const u8 *bssid, void *network_ctx,
687
          int try_opportunistic, const u8 *fils_cache_id,
688
          int akmp, bool associated)
689
0
{
690
0
  struct rsn_pmksa_cache *pmksa = sm->pmksa;
691
0
  wpa_printf(MSG_DEBUG, "RSN: PMKSA cache search - network_ctx=%p "
692
0
       "try_opportunistic=%d akmp=0x%x",
693
0
       network_ctx, try_opportunistic, akmp);
694
0
  if (pmkid)
695
0
    wpa_hexdump(MSG_DEBUG, "RSN: Search for PMKID",
696
0
          pmkid, PMKID_LEN);
697
0
  if (bssid)
698
0
    wpa_printf(MSG_DEBUG, "RSN: Search for BSSID " MACSTR,
699
0
         MAC2STR(bssid));
700
0
  if (fils_cache_id)
701
0
    wpa_printf(MSG_DEBUG,
702
0
         "RSN: Search for FILS Cache Identifier %02x%02x",
703
0
         fils_cache_id[0], fils_cache_id[1]);
704
705
0
  sm->cur_pmksa = NULL;
706
0
  if (pmkid)
707
0
    sm->cur_pmksa = pmksa_cache_get(pmksa, NULL, sm->own_addr,
708
0
            pmkid, network_ctx, akmp);
709
0
  if (sm->cur_pmksa == NULL && bssid)
710
0
    sm->cur_pmksa = pmksa_cache_get(pmksa, bssid, sm->own_addr,
711
0
            NULL, network_ctx, akmp);
712
0
  if (sm->cur_pmksa == NULL && try_opportunistic && bssid)
713
0
    sm->cur_pmksa = pmksa_cache_get_opportunistic(pmksa,
714
0
                    network_ctx,
715
0
                    bssid, akmp);
716
0
  if (sm->cur_pmksa == NULL && fils_cache_id)
717
0
    sm->cur_pmksa = pmksa_cache_get_fils_cache_id(pmksa,
718
0
                    network_ctx,
719
0
                    fils_cache_id);
720
0
  if (sm->cur_pmksa) {
721
0
    struct os_reltime now;
722
723
0
    if (wpa_key_mgmt_sae(sm->cur_pmksa->akmp) &&
724
0
        os_get_reltime(&now) == 0 &&
725
0
        sm->cur_pmksa->reauth_time < now.sec) {
726
      /* Driver-based roaming might have used a PMKSA entry
727
       * that is already past the reauthentication threshold.
728
       * Remove the related PMKID from the driver to avoid
729
       * further uses for this PMKSA, but allow the
730
       * association to continue since the PMKSA has not yet
731
       * expired. */
732
0
      wpa_sm_remove_pmkid(sm, sm->cur_pmksa->network_ctx,
733
0
              sm->cur_pmksa->aa,
734
0
              sm->cur_pmksa->pmkid, NULL);
735
0
      if (associated) {
736
0
        wpa_printf(MSG_DEBUG,
737
0
             "RSN: Associated with " MACSTR
738
0
             " using reauth threshold passed PMKSA cache entry",
739
0
             MAC2STR(sm->cur_pmksa->aa));
740
0
      } else {
741
0
        wpa_printf(MSG_DEBUG,
742
0
             "RSN: Do not allow PMKSA cache entry for "
743
0
             MACSTR
744
0
             " to be used for SAE since its reauth threshold has passed",
745
0
             MAC2STR(sm->cur_pmksa->aa));
746
0
        sm->cur_pmksa = NULL;
747
0
        return -1;
748
0
      }
749
0
    }
750
751
0
    wpa_hexdump(MSG_DEBUG, "RSN: PMKSA cache entry found - PMKID",
752
0
          sm->cur_pmksa->pmkid, PMKID_LEN);
753
0
    return 0;
754
0
  }
755
0
  wpa_printf(MSG_DEBUG, "RSN: No PMKSA cache entry found");
756
0
  return -1;
757
0
}
758
759
760
/**
761
 * pmksa_cache_list - Dump text list of entries in PMKSA cache
762
 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
763
 * @buf: Buffer for the list
764
 * @len: Length of the buffer
765
 * Returns: number of bytes written to buffer
766
 *
767
 * This function is used to generate a text format representation of the
768
 * current PMKSA cache contents for the ctrl_iface PMKSA command.
769
 */
770
int pmksa_cache_list(struct rsn_pmksa_cache *pmksa, char *buf, size_t len)
771
0
{
772
0
  int i, ret;
773
0
  char *pos = buf;
774
0
  struct rsn_pmksa_cache_entry *entry;
775
0
  struct os_reltime now;
776
0
  int cache_id_used = 0;
777
778
0
  for (entry = pmksa->pmksa; entry; entry = entry->next) {
779
0
    if (entry->fils_cache_id_set) {
780
0
      cache_id_used = 1;
781
0
      break;
782
0
    }
783
0
  }
784
785
0
  os_get_reltime(&now);
786
0
  ret = os_snprintf(pos, buf + len - pos,
787
0
        "Index / AA / PMKID / expiration (in seconds) / "
788
0
        "opportunistic%s\n",
789
0
        cache_id_used ? " / FILS Cache Identifier" : "");
790
0
  if (os_snprintf_error(buf + len - pos, ret))
791
0
    return pos - buf;
792
0
  pos += ret;
793
0
  i = 0;
794
0
  entry = pmksa->pmksa;
795
0
  while (entry) {
796
0
    i++;
797
0
    ret = os_snprintf(pos, buf + len - pos, "%d " MACSTR " ",
798
0
          i, MAC2STR(entry->aa));
799
0
    if (os_snprintf_error(buf + len - pos, ret))
800
0
      return pos - buf;
801
0
    pos += ret;
802
0
    pos += wpa_snprintf_hex(pos, buf + len - pos, entry->pmkid,
803
0
          PMKID_LEN);
804
0
    ret = os_snprintf(pos, buf + len - pos, " %d %d",
805
0
          (int) (entry->expiration - now.sec),
806
0
          entry->opportunistic);
807
0
    if (os_snprintf_error(buf + len - pos, ret))
808
0
      return pos - buf;
809
0
    pos += ret;
810
0
    if (entry->fils_cache_id_set) {
811
0
      ret = os_snprintf(pos, buf + len - pos, " %02x%02x",
812
0
            entry->fils_cache_id[0],
813
0
            entry->fils_cache_id[1]);
814
0
      if (os_snprintf_error(buf + len - pos, ret))
815
0
        return pos - buf;
816
0
      pos += ret;
817
0
    }
818
0
    ret = os_snprintf(pos, buf + len - pos, "\n");
819
0
    if (os_snprintf_error(buf + len - pos, ret))
820
0
      return pos - buf;
821
0
    pos += ret;
822
0
    entry = entry->next;
823
0
  }
824
0
  return pos - buf;
825
0
}
826
827
828
struct rsn_pmksa_cache_entry * pmksa_cache_head(struct rsn_pmksa_cache *pmksa)
829
0
{
830
0
  return pmksa->pmksa;
831
0
}
832
833
834
/**
835
 * pmksa_cache_init - Initialize PMKSA cache
836
 * @free_cb: Callback function to be called when a PMKSA cache entry is freed
837
 * @ctx: Context pointer for free_cb function
838
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
839
 * Returns: Pointer to PMKSA cache data or %NULL on failure
840
 */
841
struct rsn_pmksa_cache *
842
pmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
843
         void *ctx, enum pmksa_free_reason reason),
844
     bool (*is_current_cb)(struct rsn_pmksa_cache_entry *entry,
845
               void *ctx),
846
     void (*notify_cb)(struct rsn_pmksa_cache_entry *entry,
847
           void *ctx),
848
     void *ctx, struct wpa_sm *sm)
849
1.98k
{
850
1.98k
  struct rsn_pmksa_cache *pmksa;
851
852
1.98k
  pmksa = os_zalloc(sizeof(*pmksa));
853
1.98k
  if (pmksa) {
854
1.98k
    pmksa->free_cb = free_cb;
855
1.98k
    pmksa->is_current_cb = is_current_cb;
856
1.98k
    pmksa->notify_cb = notify_cb;
857
1.98k
    pmksa->ctx = ctx;
858
1.98k
    pmksa->sm = sm;
859
1.98k
  }
860
861
1.98k
  return pmksa;
862
1.98k
}
863
864
865
void pmksa_cache_reconfig(struct rsn_pmksa_cache *pmksa)
866
0
{
867
0
  struct rsn_pmksa_cache_entry *entry;
868
869
0
  if (!pmksa || !pmksa->pmksa)
870
0
    return;
871
872
0
  for (entry = pmksa->pmksa; entry; entry = entry->next)
873
0
    pmksa_cache_add_to_driver(pmksa, entry);
874
0
}
875
876
#else /* IEEE8021X_EAPOL */
877
878
struct rsn_pmksa_cache *
879
pmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
880
         void *ctx, enum pmksa_free_reason reason),
881
     bool (*is_current_cb)(struct rsn_pmksa_cache_entry *entry,
882
               void *ctx),
883
     void (*notify_cb)(struct rsn_pmksa_cache_entry *entry,
884
           void *ctx),
885
     void *ctx, struct wpa_sm *sm)
886
{
887
  return (void *) -1;
888
}
889
890
891
void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa)
892
{
893
}
894
895
896
struct rsn_pmksa_cache_entry *
897
pmksa_cache_get(struct rsn_pmksa_cache *pmksa, const u8 *aa, const u8 *spa,
898
    const u8 *pmkid, const void *network_ctx, int akmp)
899
{
900
  return NULL;
901
}
902
903
904
struct rsn_pmksa_cache_entry *
905
pmksa_cache_get_current(struct wpa_sm *sm)
906
{
907
  return NULL;
908
}
909
910
911
int pmksa_cache_list(struct rsn_pmksa_cache *pmksa, char *buf, size_t len)
912
{
913
  return -1;
914
}
915
916
917
struct rsn_pmksa_cache_entry *
918
pmksa_cache_head(struct rsn_pmksa_cache *pmksa)
919
{
920
  return NULL;
921
}
922
923
924
struct rsn_pmksa_cache_entry *
925
pmksa_cache_add_entry(struct rsn_pmksa_cache *pmksa,
926
          struct rsn_pmksa_cache_entry *entry)
927
{
928
  return NULL;
929
}
930
931
932
struct rsn_pmksa_cache_entry *
933
pmksa_cache_add(struct rsn_pmksa_cache *pmksa, const u8 *pmk, size_t pmk_len,
934
    const u8 *pmkid, const u8 *kck, size_t kck_len,
935
    const u8 *aa, const u8 *spa, void *network_ctx, int akmp,
936
    const u8 *cache_id, u16 auth_alg)
937
{
938
  return NULL;
939
}
940
941
942
void pmksa_cache_clear_current(struct wpa_sm *sm)
943
{
944
}
945
946
947
int pmksa_cache_set_current(struct wpa_sm *sm, const u8 *pmkid, const u8 *bssid,
948
          void *network_ctx, int try_opportunistic,
949
          const u8 *fils_cache_id, int akmp, bool associated)
950
{
951
  return -1;
952
}
953
954
955
void pmksa_cache_flush(struct rsn_pmksa_cache *pmksa, void *network_ctx,
956
           const u8 *pmk, size_t pmk_len, bool external_only)
957
{
958
}
959
960
961
void pmksa_cache_remove(struct rsn_pmksa_cache *pmksa,
962
      struct rsn_pmksa_cache_entry *entry)
963
{
964
}
965
966
967
void pmksa_cache_reconfig(struct rsn_pmksa_cache *pmksa)
968
{
969
}
970
971
#endif /* IEEE8021X_EAPOL */