pmksa_cache_auth.c revision b97e428f8acf1ecb93f38f8d0063d2f2fd0bc36e
1/* 2 * hostapd - PMKSA cache for IEEE 802.11i RSN 3 * Copyright (c) 2004-2008, 2012-2015, Jouni Malinen <j@w1.fi> 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 */ 8 9#include "utils/includes.h" 10 11#include "utils/common.h" 12#include "utils/eloop.h" 13#include "eapol_auth/eapol_auth_sm.h" 14#include "eapol_auth/eapol_auth_sm_i.h" 15#include "radius/radius_das.h" 16#include "sta_info.h" 17#include "ap_config.h" 18#include "pmksa_cache_auth.h" 19 20 21static const int pmksa_cache_max_entries = 1024; 22static const int dot11RSNAConfigPMKLifetime = 43200; 23 24struct rsn_pmksa_cache { 25#define PMKID_HASH_SIZE 128 26#define PMKID_HASH(pmkid) (unsigned int) ((pmkid)[0] & 0x7f) 27 struct rsn_pmksa_cache_entry *pmkid[PMKID_HASH_SIZE]; 28 struct rsn_pmksa_cache_entry *pmksa; 29 int pmksa_count; 30 31 void (*free_cb)(struct rsn_pmksa_cache_entry *entry, void *ctx); 32 void *ctx; 33}; 34 35 36static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa); 37 38 39static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry *entry) 40{ 41 os_free(entry->identity); 42 wpabuf_free(entry->cui); 43#ifndef CONFIG_NO_RADIUS 44 radius_free_class(&entry->radius_class); 45#endif /* CONFIG_NO_RADIUS */ 46 bin_clear_free(entry, sizeof(*entry)); 47} 48 49 50void pmksa_cache_free_entry(struct rsn_pmksa_cache *pmksa, 51 struct rsn_pmksa_cache_entry *entry) 52{ 53 struct rsn_pmksa_cache_entry *pos, *prev; 54 unsigned int hash; 55 56 pmksa->pmksa_count--; 57 pmksa->free_cb(entry, pmksa->ctx); 58 59 /* unlink from hash list */ 60 hash = PMKID_HASH(entry->pmkid); 61 pos = pmksa->pmkid[hash]; 62 prev = NULL; 63 while (pos) { 64 if (pos == entry) { 65 if (prev != NULL) 66 prev->hnext = entry->hnext; 67 else 68 pmksa->pmkid[hash] = entry->hnext; 69 break; 70 } 71 prev = pos; 72 pos = pos->hnext; 73 } 74 75 /* unlink from entry list */ 76 pos = pmksa->pmksa; 77 prev = NULL; 78 while (pos) { 79 if (pos == entry) { 80 if (prev != NULL) 81 prev->next = entry->next; 82 else 83 pmksa->pmksa = entry->next; 84 break; 85 } 86 prev = pos; 87 pos = pos->next; 88 } 89 90 _pmksa_cache_free_entry(entry); 91} 92 93 94static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx) 95{ 96 struct rsn_pmksa_cache *pmksa = eloop_ctx; 97 struct os_reltime now; 98 99 os_get_reltime(&now); 100 while (pmksa->pmksa && pmksa->pmksa->expiration <= now.sec) { 101 wpa_printf(MSG_DEBUG, "RSN: expired PMKSA cache entry for " 102 MACSTR, MAC2STR(pmksa->pmksa->spa)); 103 pmksa_cache_free_entry(pmksa, pmksa->pmksa); 104 } 105 106 pmksa_cache_set_expiration(pmksa); 107} 108 109 110static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa) 111{ 112 int sec; 113 struct os_reltime now; 114 115 eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL); 116 if (pmksa->pmksa == NULL) 117 return; 118 os_get_reltime(&now); 119 sec = pmksa->pmksa->expiration - now.sec; 120 if (sec < 0) 121 sec = 0; 122 eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, pmksa, NULL); 123} 124 125 126static void pmksa_cache_from_eapol_data(struct rsn_pmksa_cache_entry *entry, 127 struct eapol_state_machine *eapol) 128{ 129 if (eapol == NULL) 130 return; 131 132 if (eapol->identity) { 133 entry->identity = os_malloc(eapol->identity_len); 134 if (entry->identity) { 135 entry->identity_len = eapol->identity_len; 136 os_memcpy(entry->identity, eapol->identity, 137 eapol->identity_len); 138 } 139 } 140 141 if (eapol->radius_cui) 142 entry->cui = wpabuf_dup(eapol->radius_cui); 143 144#ifndef CONFIG_NO_RADIUS 145 radius_copy_class(&entry->radius_class, &eapol->radius_class); 146#endif /* CONFIG_NO_RADIUS */ 147 148 entry->eap_type_authsrv = eapol->eap_type_authsrv; 149 entry->vlan_id = ((struct sta_info *) eapol->sta)->vlan_id; 150 151 entry->acct_multi_session_id = eapol->acct_multi_session_id; 152} 153 154 155void pmksa_cache_to_eapol_data(struct rsn_pmksa_cache_entry *entry, 156 struct eapol_state_machine *eapol) 157{ 158 if (entry == NULL || eapol == NULL) 159 return; 160 161 if (entry->identity) { 162 os_free(eapol->identity); 163 eapol->identity = os_malloc(entry->identity_len); 164 if (eapol->identity) { 165 eapol->identity_len = entry->identity_len; 166 os_memcpy(eapol->identity, entry->identity, 167 entry->identity_len); 168 } 169 wpa_hexdump_ascii(MSG_DEBUG, "STA identity from PMKSA", 170 eapol->identity, eapol->identity_len); 171 } 172 173 if (entry->cui) { 174 wpabuf_free(eapol->radius_cui); 175 eapol->radius_cui = wpabuf_dup(entry->cui); 176 } 177 178#ifndef CONFIG_NO_RADIUS 179 radius_free_class(&eapol->radius_class); 180 radius_copy_class(&eapol->radius_class, &entry->radius_class); 181#endif /* CONFIG_NO_RADIUS */ 182 if (eapol->radius_class.attr) { 183 wpa_printf(MSG_DEBUG, "Copied %lu Class attribute(s) from " 184 "PMKSA", (unsigned long) eapol->radius_class.count); 185 } 186 187 eapol->eap_type_authsrv = entry->eap_type_authsrv; 188 ((struct sta_info *) eapol->sta)->vlan_id = entry->vlan_id; 189 190 eapol->acct_multi_session_id = entry->acct_multi_session_id; 191} 192 193 194static void pmksa_cache_link_entry(struct rsn_pmksa_cache *pmksa, 195 struct rsn_pmksa_cache_entry *entry) 196{ 197 struct rsn_pmksa_cache_entry *pos, *prev; 198 int hash; 199 200 /* Add the new entry; order by expiration time */ 201 pos = pmksa->pmksa; 202 prev = NULL; 203 while (pos) { 204 if (pos->expiration > entry->expiration) 205 break; 206 prev = pos; 207 pos = pos->next; 208 } 209 if (prev == NULL) { 210 entry->next = pmksa->pmksa; 211 pmksa->pmksa = entry; 212 } else { 213 entry->next = prev->next; 214 prev->next = entry; 215 } 216 217 hash = PMKID_HASH(entry->pmkid); 218 entry->hnext = pmksa->pmkid[hash]; 219 pmksa->pmkid[hash] = entry; 220 221 pmksa->pmksa_count++; 222 if (prev == NULL) 223 pmksa_cache_set_expiration(pmksa); 224 wpa_printf(MSG_DEBUG, "RSN: added PMKSA cache entry for " MACSTR, 225 MAC2STR(entry->spa)); 226 wpa_hexdump(MSG_DEBUG, "RSN: added PMKID", entry->pmkid, PMKID_LEN); 227} 228 229 230/** 231 * pmksa_cache_auth_add - Add a PMKSA cache entry 232 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init() 233 * @pmk: The new pairwise master key 234 * @pmk_len: PMK length in bytes, usually PMK_LEN (32) 235 * @kck: Key confirmation key or %NULL if not yet derived 236 * @kck_len: KCK length in bytes 237 * @aa: Authenticator address 238 * @spa: Supplicant address 239 * @session_timeout: Session timeout 240 * @eapol: Pointer to EAPOL state machine data 241 * @akmp: WPA_KEY_MGMT_* used in key derivation 242 * Returns: Pointer to the added PMKSA cache entry or %NULL on error 243 * 244 * This function create a PMKSA entry for a new PMK and adds it to the PMKSA 245 * cache. If an old entry is already in the cache for the same Supplicant, 246 * this entry will be replaced with the new entry. PMKID will be calculated 247 * based on the PMK. 248 */ 249struct rsn_pmksa_cache_entry * 250pmksa_cache_auth_add(struct rsn_pmksa_cache *pmksa, 251 const u8 *pmk, size_t pmk_len, 252 const u8 *kck, size_t kck_len, 253 const u8 *aa, const u8 *spa, int session_timeout, 254 struct eapol_state_machine *eapol, int akmp) 255{ 256 struct rsn_pmksa_cache_entry *entry, *pos; 257 struct os_reltime now; 258 259 if (pmk_len > PMK_LEN_MAX) 260 return NULL; 261 262 if (wpa_key_mgmt_suite_b(akmp) && !kck) 263 return NULL; 264 265 entry = os_zalloc(sizeof(*entry)); 266 if (entry == NULL) 267 return NULL; 268 os_memcpy(entry->pmk, pmk, pmk_len); 269 entry->pmk_len = pmk_len; 270 if (akmp == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) 271 rsn_pmkid_suite_b_192(kck, kck_len, aa, spa, entry->pmkid); 272 else if (wpa_key_mgmt_suite_b(akmp)) 273 rsn_pmkid_suite_b(kck, kck_len, aa, spa, entry->pmkid); 274 else 275 rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid, 276 wpa_key_mgmt_sha256(akmp)); 277 os_get_reltime(&now); 278 entry->expiration = now.sec; 279 if (session_timeout > 0) 280 entry->expiration += session_timeout; 281 else 282 entry->expiration += dot11RSNAConfigPMKLifetime; 283 entry->akmp = akmp; 284 os_memcpy(entry->spa, spa, ETH_ALEN); 285 pmksa_cache_from_eapol_data(entry, eapol); 286 287 /* Replace an old entry for the same STA (if found) with the new entry 288 */ 289 pos = pmksa_cache_auth_get(pmksa, spa, NULL); 290 if (pos) 291 pmksa_cache_free_entry(pmksa, pos); 292 293 if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) { 294 /* Remove the oldest entry to make room for the new entry */ 295 wpa_printf(MSG_DEBUG, "RSN: removed the oldest PMKSA cache " 296 "entry (for " MACSTR ") to make room for new one", 297 MAC2STR(pmksa->pmksa->spa)); 298 pmksa_cache_free_entry(pmksa, pmksa->pmksa); 299 } 300 301 pmksa_cache_link_entry(pmksa, entry); 302 303 return entry; 304} 305 306 307struct rsn_pmksa_cache_entry * 308pmksa_cache_add_okc(struct rsn_pmksa_cache *pmksa, 309 const struct rsn_pmksa_cache_entry *old_entry, 310 const u8 *aa, const u8 *pmkid) 311{ 312 struct rsn_pmksa_cache_entry *entry; 313 314 entry = os_zalloc(sizeof(*entry)); 315 if (entry == NULL) 316 return NULL; 317 os_memcpy(entry->pmkid, pmkid, PMKID_LEN); 318 os_memcpy(entry->pmk, old_entry->pmk, old_entry->pmk_len); 319 entry->pmk_len = old_entry->pmk_len; 320 entry->expiration = old_entry->expiration; 321 entry->akmp = old_entry->akmp; 322 os_memcpy(entry->spa, old_entry->spa, ETH_ALEN); 323 entry->opportunistic = 1; 324 if (old_entry->identity) { 325 entry->identity = os_malloc(old_entry->identity_len); 326 if (entry->identity) { 327 entry->identity_len = old_entry->identity_len; 328 os_memcpy(entry->identity, old_entry->identity, 329 old_entry->identity_len); 330 } 331 } 332 if (old_entry->cui) 333 entry->cui = wpabuf_dup(old_entry->cui); 334#ifndef CONFIG_NO_RADIUS 335 radius_copy_class(&entry->radius_class, &old_entry->radius_class); 336#endif /* CONFIG_NO_RADIUS */ 337 entry->eap_type_authsrv = old_entry->eap_type_authsrv; 338 entry->vlan_id = old_entry->vlan_id; 339 entry->opportunistic = 1; 340 341 pmksa_cache_link_entry(pmksa, entry); 342 343 return entry; 344} 345 346 347/** 348 * pmksa_cache_auth_deinit - Free all entries in PMKSA cache 349 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init() 350 */ 351void pmksa_cache_auth_deinit(struct rsn_pmksa_cache *pmksa) 352{ 353 struct rsn_pmksa_cache_entry *entry, *prev; 354 int i; 355 356 if (pmksa == NULL) 357 return; 358 359 entry = pmksa->pmksa; 360 while (entry) { 361 prev = entry; 362 entry = entry->next; 363 _pmksa_cache_free_entry(prev); 364 } 365 eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL); 366 pmksa->pmksa_count = 0; 367 pmksa->pmksa = NULL; 368 for (i = 0; i < PMKID_HASH_SIZE; i++) 369 pmksa->pmkid[i] = NULL; 370 os_free(pmksa); 371} 372 373 374/** 375 * pmksa_cache_auth_get - Fetch a PMKSA cache entry 376 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init() 377 * @spa: Supplicant address or %NULL to match any 378 * @pmkid: PMKID or %NULL to match any 379 * Returns: Pointer to PMKSA cache entry or %NULL if no match was found 380 */ 381struct rsn_pmksa_cache_entry * 382pmksa_cache_auth_get(struct rsn_pmksa_cache *pmksa, 383 const u8 *spa, const u8 *pmkid) 384{ 385 struct rsn_pmksa_cache_entry *entry; 386 387 if (pmkid) { 388 for (entry = pmksa->pmkid[PMKID_HASH(pmkid)]; entry; 389 entry = entry->hnext) { 390 if ((spa == NULL || 391 os_memcmp(entry->spa, spa, ETH_ALEN) == 0) && 392 os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0) 393 return entry; 394 } 395 } else { 396 for (entry = pmksa->pmksa; entry; entry = entry->next) { 397 if (spa == NULL || 398 os_memcmp(entry->spa, spa, ETH_ALEN) == 0) 399 return entry; 400 } 401 } 402 403 return NULL; 404} 405 406 407/** 408 * pmksa_cache_get_okc - Fetch a PMKSA cache entry using OKC 409 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init() 410 * @aa: Authenticator address 411 * @spa: Supplicant address 412 * @pmkid: PMKID 413 * Returns: Pointer to PMKSA cache entry or %NULL if no match was found 414 * 415 * Use opportunistic key caching (OKC) to find a PMK for a supplicant. 416 */ 417struct rsn_pmksa_cache_entry * pmksa_cache_get_okc( 418 struct rsn_pmksa_cache *pmksa, const u8 *aa, const u8 *spa, 419 const u8 *pmkid) 420{ 421 struct rsn_pmksa_cache_entry *entry; 422 u8 new_pmkid[PMKID_LEN]; 423 424 for (entry = pmksa->pmksa; entry; entry = entry->next) { 425 if (os_memcmp(entry->spa, spa, ETH_ALEN) != 0) 426 continue; 427 rsn_pmkid(entry->pmk, entry->pmk_len, aa, spa, new_pmkid, 428 wpa_key_mgmt_sha256(entry->akmp)); 429 if (os_memcmp(new_pmkid, pmkid, PMKID_LEN) == 0) 430 return entry; 431 } 432 return NULL; 433} 434 435 436/** 437 * pmksa_cache_auth_init - Initialize PMKSA cache 438 * @free_cb: Callback function to be called when a PMKSA cache entry is freed 439 * @ctx: Context pointer for free_cb function 440 * Returns: Pointer to PMKSA cache data or %NULL on failure 441 */ 442struct rsn_pmksa_cache * 443pmksa_cache_auth_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry, 444 void *ctx), void *ctx) 445{ 446 struct rsn_pmksa_cache *pmksa; 447 448 pmksa = os_zalloc(sizeof(*pmksa)); 449 if (pmksa) { 450 pmksa->free_cb = free_cb; 451 pmksa->ctx = ctx; 452 } 453 454 return pmksa; 455} 456 457 458static int das_attr_match(struct rsn_pmksa_cache_entry *entry, 459 struct radius_das_attrs *attr) 460{ 461 int match = 0; 462 463 if (attr->sta_addr) { 464 if (os_memcmp(attr->sta_addr, entry->spa, ETH_ALEN) != 0) 465 return 0; 466 match++; 467 } 468 469 if (attr->acct_multi_session_id) { 470 char buf[20]; 471 472 if (attr->acct_multi_session_id_len != 16) 473 return 0; 474 os_snprintf(buf, sizeof(buf), "%016lX", 475 (long unsigned int) entry->acct_multi_session_id); 476 if (os_memcmp(attr->acct_multi_session_id, buf, 16) != 0) 477 return 0; 478 match++; 479 } 480 481 if (attr->cui) { 482 if (!entry->cui || 483 attr->cui_len != wpabuf_len(entry->cui) || 484 os_memcmp(attr->cui, wpabuf_head(entry->cui), 485 attr->cui_len) != 0) 486 return 0; 487 match++; 488 } 489 490 if (attr->user_name) { 491 if (!entry->identity || 492 attr->user_name_len != entry->identity_len || 493 os_memcmp(attr->user_name, entry->identity, 494 attr->user_name_len) != 0) 495 return 0; 496 match++; 497 } 498 499 return match; 500} 501 502 503int pmksa_cache_auth_radius_das_disconnect(struct rsn_pmksa_cache *pmksa, 504 struct radius_das_attrs *attr) 505{ 506 int found = 0; 507 struct rsn_pmksa_cache_entry *entry, *prev; 508 509 if (attr->acct_session_id) 510 return -1; 511 512 entry = pmksa->pmksa; 513 while (entry) { 514 if (das_attr_match(entry, attr)) { 515 found++; 516 prev = entry; 517 entry = entry->next; 518 pmksa_cache_free_entry(pmksa, prev); 519 continue; 520 } 521 entry = entry->next; 522 } 523 524 return found ? 0 : -1; 525} 526