pmksa_cache_auth.c 13 KB

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