Coverage Report

Created: 2026-06-09 06:10

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
2.15k
{
159
2.15k
  int sec;
160
2.15k
  struct rsn_pmksa_cache_entry *entry;
161
2.15k
  struct os_reltime now;
162
163
2.15k
  eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
164
2.15k
  eloop_cancel_timeout(pmksa_cache_reauth, pmksa, NULL);
165
2.15k
  if (pmksa->pmksa == NULL)
166
2.15k
    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, NULL);
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
 * @addr: AA to match or %NULL to match all addresses
453
 */
454
void pmksa_cache_flush(struct rsn_pmksa_cache *pmksa, void *network_ctx,
455
           const u8 *pmk, size_t pmk_len, bool external_only,
456
           const u8 *addr)
457
0
{
458
0
  struct rsn_pmksa_cache_entry *entry, *prev = NULL, *tmp;
459
0
  int removed = 0;
460
461
0
  entry = pmksa->pmksa;
462
0
  while (entry) {
463
0
    if ((entry->network_ctx == network_ctx ||
464
0
         network_ctx == NULL) &&
465
0
        (pmk == NULL ||
466
0
         (pmk_len == entry->pmk_len &&
467
0
          os_memcmp(pmk, entry->pmk, pmk_len) == 0)) &&
468
0
        (!external_only || entry->external)) {
469
0
      wpa_printf(MSG_DEBUG, "RSN: Flush PMKSA cache entry "
470
0
           "for " MACSTR, MAC2STR(entry->aa));
471
0
      if (prev)
472
0
        prev->next = entry->next;
473
0
      else
474
0
        pmksa->pmksa = entry->next;
475
0
      tmp = entry;
476
0
      entry = entry->next;
477
0
      pmksa_cache_free_entry(pmksa, tmp, PMKSA_FREE);
478
0
      removed++;
479
0
    } else {
480
0
      prev = entry;
481
0
      entry = entry->next;
482
0
    }
483
0
  }
484
0
  if (removed)
485
0
    pmksa_cache_set_expiration(pmksa);
486
0
}
487
488
489
/**
490
 * pmksa_cache_deinit - Free all entries in PMKSA cache
491
 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
492
 */
493
void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa)
494
2.15k
{
495
2.15k
  struct rsn_pmksa_cache_entry *entry, *prev;
496
497
2.15k
  if (pmksa == NULL)
498
0
    return;
499
500
2.15k
  entry = pmksa->pmksa;
501
2.15k
  pmksa->pmksa = NULL;
502
2.15k
  while (entry) {
503
0
    prev = entry;
504
0
    entry = entry->next;
505
0
    os_free(prev);
506
0
  }
507
2.15k
  pmksa_cache_set_expiration(pmksa);
508
2.15k
  os_free(pmksa);
509
2.15k
}
510
511
512
/**
513
 * pmksa_cache_get - Fetch a PMKSA cache entry
514
 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
515
 * @aa: Authenticator address or %NULL to match any
516
 * @pmkid: PMKID or %NULL to match any
517
 * @network_ctx: Network context or %NULL to match any
518
 * @akmp: Specific AKMP to search for or 0 for any
519
 * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
520
 */
521
struct rsn_pmksa_cache_entry * pmksa_cache_get(struct rsn_pmksa_cache *pmksa,
522
                 const u8 *aa, const u8 *spa,
523
                 const u8 *pmkid,
524
                 const void *network_ctx,
525
                 int akmp)
526
194
{
527
194
  struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
528
194
  while (entry) {
529
0
    if ((aa == NULL || ether_addr_equal(entry->aa, aa)) &&
530
0
        (!spa || ether_addr_equal(entry->spa, spa)) &&
531
0
        (pmkid == NULL ||
532
0
         os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0) &&
533
0
        (!akmp || akmp == entry->akmp) &&
534
0
        (network_ctx == NULL || network_ctx == entry->network_ctx))
535
0
      return entry;
536
0
    entry = entry->next;
537
0
  }
538
194
  return NULL;
539
194
}
540
541
542
static struct rsn_pmksa_cache_entry *
543
pmksa_cache_clone_entry(struct rsn_pmksa_cache *pmksa,
544
      const struct rsn_pmksa_cache_entry *old_entry,
545
      const u8 *aa)
546
0
{
547
0
  struct rsn_pmksa_cache_entry *new_entry;
548
0
  os_time_t old_expiration = old_entry->expiration;
549
0
  os_time_t old_reauth_time = old_entry->reauth_time;
550
0
  const u8 *pmkid = NULL;
551
552
0
  if (!pmksa->sm)
553
0
    return NULL;
554
555
0
  if (wpa_key_mgmt_sae(old_entry->akmp) ||
556
0
      wpa_key_mgmt_fils(old_entry->akmp))
557
0
    pmkid = old_entry->pmkid;
558
0
  new_entry = pmksa_cache_add(pmksa, old_entry->pmk, old_entry->pmk_len,
559
0
            pmkid, old_entry->kck, old_entry->kck_len,
560
0
            aa, pmksa->sm->own_addr,
561
0
            old_entry->network_ctx, old_entry->akmp,
562
0
            old_entry->fils_cache_id_set ?
563
0
            old_entry->fils_cache_id : NULL,
564
0
            old_entry->auth_alg);
565
0
  if (new_entry == NULL)
566
0
    return NULL;
567
568
  /* TODO: reorder entries based on expiration time? */
569
0
  new_entry->expiration = old_expiration;
570
0
  new_entry->reauth_time = old_reauth_time;
571
0
  new_entry->opportunistic = 1;
572
573
0
  return new_entry;
574
0
}
575
576
577
/**
578
 * pmksa_cache_get_opportunistic - Try to get an opportunistic PMKSA entry
579
 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
580
 * @network_ctx: Network configuration context
581
 * @aa: Authenticator address for the new AP
582
 * @akmp: Specific AKMP to search for or 0 for any
583
 * Returns: Pointer to a new PMKSA cache entry or %NULL if not available
584
 *
585
 * Try to create a new PMKSA cache entry opportunistically by guessing that the
586
 * new AP is sharing the same PMK as another AP that has the same SSID and has
587
 * already an entry in PMKSA cache.
588
 */
589
struct rsn_pmksa_cache_entry *
590
pmksa_cache_get_opportunistic(struct rsn_pmksa_cache *pmksa, void *network_ctx,
591
            const u8 *aa, int akmp)
592
0
{
593
0
  struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
594
595
0
  wpa_printf(MSG_DEBUG, "RSN: Consider " MACSTR " for OKC", MAC2STR(aa));
596
0
  if (network_ctx == NULL)
597
0
    return NULL;
598
0
  while (entry) {
599
0
    if (entry->network_ctx == network_ctx &&
600
0
        (!akmp || entry->akmp == akmp)) {
601
0
      struct os_reltime now;
602
603
0
      if (wpa_key_mgmt_sae(entry->akmp) &&
604
0
          os_get_reltime(&now) == 0 &&
605
0
          entry->reauth_time < now.sec) {
606
0
        wpa_printf(MSG_DEBUG,
607
0
             "RSN: Do not clone PMKSA cache entry for "
608
0
             MACSTR
609
0
             " since its reauth threshold has passed",
610
0
             MAC2STR(entry->aa));
611
0
        entry = entry->next;
612
0
        continue;
613
0
      }
614
615
0
      entry = pmksa_cache_clone_entry(pmksa, entry, aa);
616
0
      if (entry) {
617
0
        wpa_printf(MSG_DEBUG, "RSN: added "
618
0
             "opportunistic PMKSA cache entry "
619
0
             "for " MACSTR, MAC2STR(aa));
620
0
      }
621
0
      return entry;
622
0
    }
623
0
    entry = entry->next;
624
0
  }
625
0
  return NULL;
626
0
}
627
628
629
static struct rsn_pmksa_cache_entry *
630
pmksa_cache_get_fils_cache_id(struct rsn_pmksa_cache *pmksa,
631
            const void *network_ctx, const u8 *cache_id)
632
0
{
633
0
  struct rsn_pmksa_cache_entry *entry;
634
635
0
  for (entry = pmksa->pmksa; entry; entry = entry->next) {
636
0
    if (network_ctx == entry->network_ctx &&
637
0
        entry->fils_cache_id_set &&
638
0
        os_memcmp(cache_id, entry->fils_cache_id,
639
0
            FILS_CACHE_ID_LEN) == 0)
640
0
      return entry;
641
0
  }
642
643
0
  return NULL;
644
0
}
645
646
647
/**
648
 * pmksa_cache_get_current - Get the current used PMKSA entry
649
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
650
 * Returns: Pointer to the current PMKSA cache entry or %NULL if not available
651
 */
652
struct rsn_pmksa_cache_entry * pmksa_cache_get_current(struct wpa_sm *sm)
653
0
{
654
0
  if (sm == NULL)
655
0
    return NULL;
656
0
  return sm->cur_pmksa;
657
0
}
658
659
660
/**
661
 * pmksa_cache_clear_current - Clear the current PMKSA entry selection
662
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
663
 */
664
void pmksa_cache_clear_current(struct wpa_sm *sm)
665
0
{
666
0
  if (sm == NULL)
667
0
    return;
668
0
  if (sm->cur_pmksa)
669
0
    wpa_printf(MSG_DEBUG,
670
0
         "RSN: Clear current PMKSA entry selection");
671
0
  sm->cur_pmksa = NULL;
672
0
}
673
674
675
/**
676
 * pmksa_cache_set_current - Set the current PMKSA entry selection
677
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
678
 * @pmkid: PMKID for selecting PMKSA or %NULL if not used
679
 * @bssid: BSSID for PMKSA or %NULL if not used
680
 * @network_ctx: Network configuration context
681
 * @try_opportunistic: Whether to allow opportunistic PMKSA caching
682
 * @fils_cache_id: Pointer to FILS Cache Identifier or %NULL if not used
683
 * @akmp: Specific AKMP to search for or 0 for any
684
 * @associated: Whether the device is associated
685
 * Returns: 0 if PMKSA was found or -1 if no matching entry was found
686
 */
687
int pmksa_cache_set_current(struct wpa_sm *sm, const u8 *pmkid,
688
          const u8 *bssid, void *network_ctx,
689
          int try_opportunistic, const u8 *fils_cache_id,
690
          int akmp, bool associated)
691
0
{
692
0
  struct rsn_pmksa_cache *pmksa = sm->pmksa;
693
0
  wpa_printf(MSG_DEBUG, "RSN: PMKSA cache search - network_ctx=%p "
694
0
       "try_opportunistic=%d akmp=0x%x",
695
0
       network_ctx, try_opportunistic, akmp);
696
0
  if (pmkid)
697
0
    wpa_hexdump(MSG_DEBUG, "RSN: Search for PMKID",
698
0
          pmkid, PMKID_LEN);
699
0
  if (bssid)
700
0
    wpa_printf(MSG_DEBUG, "RSN: Search for BSSID " MACSTR,
701
0
         MAC2STR(bssid));
702
0
  if (fils_cache_id)
703
0
    wpa_printf(MSG_DEBUG,
704
0
         "RSN: Search for FILS Cache Identifier %02x%02x",
705
0
         fils_cache_id[0], fils_cache_id[1]);
706
707
0
  sm->cur_pmksa = NULL;
708
0
  if (pmkid)
709
0
    sm->cur_pmksa = pmksa_cache_get(pmksa, NULL, sm->own_addr,
710
0
            pmkid, network_ctx, akmp);
711
0
  if (sm->cur_pmksa == NULL && bssid)
712
0
    sm->cur_pmksa = pmksa_cache_get(pmksa, bssid, sm->own_addr,
713
0
            NULL, network_ctx, akmp);
714
0
  if (sm->cur_pmksa == NULL && try_opportunistic && bssid)
715
0
    sm->cur_pmksa = pmksa_cache_get_opportunistic(pmksa,
716
0
                    network_ctx,
717
0
                    bssid, akmp);
718
0
  if (sm->cur_pmksa == NULL && fils_cache_id)
719
0
    sm->cur_pmksa = pmksa_cache_get_fils_cache_id(pmksa,
720
0
                    network_ctx,
721
0
                    fils_cache_id);
722
0
  if (sm->cur_pmksa) {
723
0
    struct os_reltime now;
724
725
0
    if (wpa_key_mgmt_sae(sm->cur_pmksa->akmp) &&
726
0
        os_get_reltime(&now) == 0 &&
727
0
        sm->cur_pmksa->reauth_time < now.sec) {
728
      /* Driver-based roaming might have used a PMKSA entry
729
       * that is already past the reauthentication threshold.
730
       * Remove the related PMKID from the driver to avoid
731
       * further uses for this PMKSA, but allow the
732
       * association to continue since the PMKSA has not yet
733
       * expired. */
734
0
      wpa_sm_remove_pmkid(sm, sm->cur_pmksa->network_ctx,
735
0
              sm->cur_pmksa->aa,
736
0
              sm->cur_pmksa->pmkid, NULL);
737
0
      if (associated) {
738
0
        wpa_printf(MSG_DEBUG,
739
0
             "RSN: Associated with " MACSTR
740
0
             " using reauth threshold passed PMKSA cache entry",
741
0
             MAC2STR(sm->cur_pmksa->aa));
742
0
      } else {
743
0
        wpa_printf(MSG_DEBUG,
744
0
             "RSN: Do not allow PMKSA cache entry for "
745
0
             MACSTR
746
0
             " to be used for SAE since its reauth threshold has passed",
747
0
             MAC2STR(sm->cur_pmksa->aa));
748
0
        sm->cur_pmksa = NULL;
749
0
        return -1;
750
0
      }
751
0
    }
752
753
0
    wpa_hexdump(MSG_DEBUG, "RSN: PMKSA cache entry found - PMKID",
754
0
          sm->cur_pmksa->pmkid, PMKID_LEN);
755
0
    return 0;
756
0
  }
757
0
  wpa_printf(MSG_DEBUG, "RSN: No PMKSA cache entry found");
758
0
  return -1;
759
0
}
760
761
762
/**
763
 * pmksa_cache_list - Dump text list of entries in PMKSA cache
764
 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
765
 * @buf: Buffer for the list
766
 * @len: Length of the buffer
767
 * Returns: number of bytes written to buffer
768
 *
769
 * This function is used to generate a text format representation of the
770
 * current PMKSA cache contents for the ctrl_iface PMKSA command.
771
 */
772
int pmksa_cache_list(struct rsn_pmksa_cache *pmksa, char *buf, size_t len)
773
0
{
774
0
  int i, ret;
775
0
  char *pos = buf;
776
0
  struct rsn_pmksa_cache_entry *entry;
777
0
  struct os_reltime now;
778
0
  int cache_id_used = 0;
779
780
0
  for (entry = pmksa->pmksa; entry; entry = entry->next) {
781
0
    if (entry->fils_cache_id_set) {
782
0
      cache_id_used = 1;
783
0
      break;
784
0
    }
785
0
  }
786
787
0
  os_get_reltime(&now);
788
0
  ret = os_snprintf(pos, buf + len - pos,
789
0
        "Index / AA / PMKID / expiration (in seconds) / "
790
0
        "opportunistic%s\n",
791
0
        cache_id_used ? " / FILS Cache Identifier" : "");
792
0
  if (os_snprintf_error(buf + len - pos, ret))
793
0
    return pos - buf;
794
0
  pos += ret;
795
0
  i = 0;
796
0
  entry = pmksa->pmksa;
797
0
  while (entry) {
798
0
    i++;
799
0
    ret = os_snprintf(pos, buf + len - pos, "%d " MACSTR " ",
800
0
          i, MAC2STR(entry->aa));
801
0
    if (os_snprintf_error(buf + len - pos, ret))
802
0
      return pos - buf;
803
0
    pos += ret;
804
0
    pos += wpa_snprintf_hex(pos, buf + len - pos, entry->pmkid,
805
0
          PMKID_LEN);
806
0
    ret = os_snprintf(pos, buf + len - pos, " %d %d",
807
0
          (int) (entry->expiration - now.sec),
808
0
          entry->opportunistic);
809
0
    if (os_snprintf_error(buf + len - pos, ret))
810
0
      return pos - buf;
811
0
    pos += ret;
812
0
    if (entry->fils_cache_id_set) {
813
0
      ret = os_snprintf(pos, buf + len - pos, " %02x%02x",
814
0
            entry->fils_cache_id[0],
815
0
            entry->fils_cache_id[1]);
816
0
      if (os_snprintf_error(buf + len - pos, ret))
817
0
        return pos - buf;
818
0
      pos += ret;
819
0
    }
820
0
    ret = os_snprintf(pos, buf + len - pos, "\n");
821
0
    if (os_snprintf_error(buf + len - pos, ret))
822
0
      return pos - buf;
823
0
    pos += ret;
824
0
    entry = entry->next;
825
0
  }
826
0
  return pos - buf;
827
0
}
828
829
830
struct rsn_pmksa_cache_entry * pmksa_cache_head(struct rsn_pmksa_cache *pmksa)
831
0
{
832
0
  return pmksa->pmksa;
833
0
}
834
835
836
/**
837
 * pmksa_cache_init - Initialize PMKSA cache
838
 * @free_cb: Callback function to be called when a PMKSA cache entry is freed
839
 * @ctx: Context pointer for free_cb function
840
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
841
 * Returns: Pointer to PMKSA cache data or %NULL on failure
842
 */
843
struct rsn_pmksa_cache *
844
pmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
845
         void *ctx, enum pmksa_free_reason reason),
846
     bool (*is_current_cb)(struct rsn_pmksa_cache_entry *entry,
847
               void *ctx),
848
     void (*notify_cb)(struct rsn_pmksa_cache_entry *entry,
849
           void *ctx),
850
     void *ctx, struct wpa_sm *sm)
851
2.15k
{
852
2.15k
  struct rsn_pmksa_cache *pmksa;
853
854
2.15k
  pmksa = os_zalloc(sizeof(*pmksa));
855
2.15k
  if (pmksa) {
856
2.15k
    pmksa->free_cb = free_cb;
857
2.15k
    pmksa->is_current_cb = is_current_cb;
858
2.15k
    pmksa->notify_cb = notify_cb;
859
2.15k
    pmksa->ctx = ctx;
860
2.15k
    pmksa->sm = sm;
861
2.15k
  }
862
863
2.15k
  return pmksa;
864
2.15k
}
865
866
867
void pmksa_cache_reconfig(struct rsn_pmksa_cache *pmksa)
868
0
{
869
0
  struct rsn_pmksa_cache_entry *entry;
870
871
0
  if (!pmksa || !pmksa->pmksa)
872
0
    return;
873
874
0
  for (entry = pmksa->pmksa; entry; entry = entry->next)
875
0
    pmksa_cache_add_to_driver(pmksa, entry);
876
0
}
877
878
#else /* IEEE8021X_EAPOL */
879
880
struct rsn_pmksa_cache *
881
pmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
882
         void *ctx, enum pmksa_free_reason reason),
883
     bool (*is_current_cb)(struct rsn_pmksa_cache_entry *entry,
884
               void *ctx),
885
     void (*notify_cb)(struct rsn_pmksa_cache_entry *entry,
886
           void *ctx),
887
     void *ctx, struct wpa_sm *sm)
888
{
889
  return (void *) -1;
890
}
891
892
893
void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa)
894
{
895
}
896
897
898
struct rsn_pmksa_cache_entry *
899
pmksa_cache_get(struct rsn_pmksa_cache *pmksa, const u8 *aa, const u8 *spa,
900
    const u8 *pmkid, const void *network_ctx, int akmp)
901
{
902
  return NULL;
903
}
904
905
906
struct rsn_pmksa_cache_entry *
907
pmksa_cache_get_current(struct wpa_sm *sm)
908
{
909
  return NULL;
910
}
911
912
913
int pmksa_cache_list(struct rsn_pmksa_cache *pmksa, char *buf, size_t len)
914
{
915
  return -1;
916
}
917
918
919
struct rsn_pmksa_cache_entry *
920
pmksa_cache_head(struct rsn_pmksa_cache *pmksa)
921
{
922
  return NULL;
923
}
924
925
926
struct rsn_pmksa_cache_entry *
927
pmksa_cache_add_entry(struct rsn_pmksa_cache *pmksa,
928
          struct rsn_pmksa_cache_entry *entry)
929
{
930
  return NULL;
931
}
932
933
934
struct rsn_pmksa_cache_entry *
935
pmksa_cache_add(struct rsn_pmksa_cache *pmksa, const u8 *pmk, size_t pmk_len,
936
    const u8 *pmkid, const u8 *kck, size_t kck_len,
937
    const u8 *aa, const u8 *spa, void *network_ctx, int akmp,
938
    const u8 *cache_id, u16 auth_alg)
939
{
940
  return NULL;
941
}
942
943
944
void pmksa_cache_clear_current(struct wpa_sm *sm)
945
{
946
}
947
948
949
int pmksa_cache_set_current(struct wpa_sm *sm, const u8 *pmkid, const u8 *bssid,
950
          void *network_ctx, int try_opportunistic,
951
          const u8 *fils_cache_id, int akmp, bool associated)
952
{
953
  return -1;
954
}
955
956
957
void pmksa_cache_flush(struct rsn_pmksa_cache *pmksa, void *network_ctx,
958
           const u8 *pmk, size_t pmk_len, bool external_only,
959
           const u8 *addr)
960
{
961
}
962
963
964
void pmksa_cache_remove(struct rsn_pmksa_cache *pmksa,
965
      struct rsn_pmksa_cache_entry *entry)
966
{
967
}
968
969
970
void pmksa_cache_reconfig(struct rsn_pmksa_cache *pmksa)
971
{
972
}
973
974
#endif /* IEEE8021X_EAPOL */