pmksa_cache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * hostapd - PMKSA cache for IEEE 802.11i RSN
  3. * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "sta_info.h"
  17. #include "config.h"
  18. #include "common.h"
  19. #include "eloop.h"
  20. #include "eapol_auth/eapol_auth_sm.h"
  21. #include "pmksa_cache.h"
  22. static const int pmksa_cache_max_entries = 1024;
  23. static const int dot11RSNAConfigPMKLifetime = 43200;
  24. struct 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. void (*free_cb)(struct rsn_pmksa_cache_entry *entry, void *ctx);
  31. void *ctx;
  32. };
  33. static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa);
  34. static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry *entry)
  35. {
  36. if (entry == NULL)
  37. return;
  38. os_free(entry->identity);
  39. radius_free_class(&entry->radius_class);
  40. os_free(entry);
  41. }
  42. static void pmksa_cache_free_entry(struct rsn_pmksa_cache *pmksa,
  43. struct rsn_pmksa_cache_entry *entry)
  44. {
  45. struct rsn_pmksa_cache_entry *pos, *prev;
  46. pmksa->pmksa_count--;
  47. pmksa->free_cb(entry, pmksa->ctx);
  48. pos = pmksa->pmkid[PMKID_HASH(entry->pmkid)];
  49. prev = NULL;
  50. while (pos) {
  51. if (pos == entry) {
  52. if (prev != NULL) {
  53. prev->hnext = pos->hnext;
  54. } else {
  55. pmksa->pmkid[PMKID_HASH(entry->pmkid)] =
  56. pos->hnext;
  57. }
  58. break;
  59. }
  60. prev = pos;
  61. pos = pos->hnext;
  62. }
  63. pos = pmksa->pmksa;
  64. prev = NULL;
  65. while (pos) {
  66. if (pos == entry) {
  67. if (prev != NULL)
  68. prev->next = pos->next;
  69. else
  70. pmksa->pmksa = pos->next;
  71. break;
  72. }
  73. prev = pos;
  74. pos = pos->next;
  75. }
  76. _pmksa_cache_free_entry(entry);
  77. }
  78. static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx)
  79. {
  80. struct rsn_pmksa_cache *pmksa = eloop_ctx;
  81. struct os_time now;
  82. os_get_time(&now);
  83. while (pmksa->pmksa && pmksa->pmksa->expiration <= now.sec) {
  84. struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
  85. pmksa->pmksa = entry->next;
  86. wpa_printf(MSG_DEBUG, "RSN: expired PMKSA cache entry for "
  87. MACSTR, MAC2STR(entry->spa));
  88. pmksa_cache_free_entry(pmksa, entry);
  89. }
  90. pmksa_cache_set_expiration(pmksa);
  91. }
  92. static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa)
  93. {
  94. int sec;
  95. struct os_time now;
  96. eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
  97. if (pmksa->pmksa == NULL)
  98. return;
  99. os_get_time(&now);
  100. sec = pmksa->pmksa->expiration - now.sec;
  101. if (sec < 0)
  102. sec = 0;
  103. eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, pmksa, NULL);
  104. }
  105. static void pmksa_cache_from_eapol_data(struct rsn_pmksa_cache_entry *entry,
  106. struct eapol_state_machine *eapol)
  107. {
  108. if (eapol == NULL)
  109. return;
  110. if (eapol->identity) {
  111. entry->identity = os_malloc(eapol->identity_len);
  112. if (entry->identity) {
  113. entry->identity_len = eapol->identity_len;
  114. os_memcpy(entry->identity, eapol->identity,
  115. eapol->identity_len);
  116. }
  117. }
  118. radius_copy_class(&entry->radius_class, &eapol->radius_class);
  119. entry->eap_type_authsrv = eapol->eap_type_authsrv;
  120. entry->vlan_id = ((struct sta_info *) eapol->sta)->vlan_id;
  121. }
  122. void pmksa_cache_to_eapol_data(struct rsn_pmksa_cache_entry *entry,
  123. struct eapol_state_machine *eapol)
  124. {
  125. if (entry == NULL || eapol == NULL)
  126. return;
  127. if (entry->identity) {
  128. os_free(eapol->identity);
  129. eapol->identity = os_malloc(entry->identity_len);
  130. if (eapol->identity) {
  131. eapol->identity_len = entry->identity_len;
  132. os_memcpy(eapol->identity, entry->identity,
  133. entry->identity_len);
  134. }
  135. wpa_hexdump_ascii(MSG_DEBUG, "STA identity from PMKSA",
  136. eapol->identity, eapol->identity_len);
  137. }
  138. radius_free_class(&eapol->radius_class);
  139. radius_copy_class(&eapol->radius_class, &entry->radius_class);
  140. if (eapol->radius_class.attr) {
  141. wpa_printf(MSG_DEBUG, "Copied %lu Class attribute(s) from "
  142. "PMKSA", (unsigned long) eapol->radius_class.count);
  143. }
  144. eapol->eap_type_authsrv = entry->eap_type_authsrv;
  145. ((struct sta_info *) eapol->sta)->vlan_id = entry->vlan_id;
  146. }
  147. static void pmksa_cache_link_entry(struct rsn_pmksa_cache *pmksa,
  148. struct rsn_pmksa_cache_entry *entry)
  149. {
  150. struct rsn_pmksa_cache_entry *pos, *prev;
  151. /* Add the new entry; order by expiration time */
  152. pos = pmksa->pmksa;
  153. prev = NULL;
  154. while (pos) {
  155. if (pos->expiration > entry->expiration)
  156. break;
  157. prev = pos;
  158. pos = pos->next;
  159. }
  160. if (prev == NULL) {
  161. entry->next = pmksa->pmksa;
  162. pmksa->pmksa = entry;
  163. } else {
  164. entry->next = prev->next;
  165. prev->next = entry;
  166. }
  167. entry->hnext = pmksa->pmkid[PMKID_HASH(entry->pmkid)];
  168. pmksa->pmkid[PMKID_HASH(entry->pmkid)] = entry;
  169. pmksa->pmksa_count++;
  170. wpa_printf(MSG_DEBUG, "RSN: added PMKSA cache entry for " MACSTR,
  171. MAC2STR(entry->spa));
  172. wpa_hexdump(MSG_DEBUG, "RSN: added PMKID", entry->pmkid, PMKID_LEN);
  173. }
  174. /**
  175. * pmksa_cache_auth_add - Add a PMKSA cache entry
  176. * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
  177. * @pmk: The new pairwise master key
  178. * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
  179. * @aa: Authenticator address
  180. * @spa: Supplicant address
  181. * @session_timeout: Session timeout
  182. * @eapol: Pointer to EAPOL state machine data
  183. * @akmp: WPA_KEY_MGMT_* used in key derivation
  184. * Returns: Pointer to the added PMKSA cache entry or %NULL on error
  185. *
  186. * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
  187. * cache. If an old entry is already in the cache for the same Supplicant,
  188. * this entry will be replaced with the new entry. PMKID will be calculated
  189. * based on the PMK.
  190. */
  191. struct rsn_pmksa_cache_entry *
  192. pmksa_cache_auth_add(struct rsn_pmksa_cache *pmksa,
  193. const u8 *pmk, size_t pmk_len,
  194. const u8 *aa, const u8 *spa, int session_timeout,
  195. struct eapol_state_machine *eapol, int akmp)
  196. {
  197. struct rsn_pmksa_cache_entry *entry, *pos;
  198. struct os_time now;
  199. if (pmk_len > PMK_LEN)
  200. return NULL;
  201. entry = os_zalloc(sizeof(*entry));
  202. if (entry == NULL)
  203. return NULL;
  204. os_memcpy(entry->pmk, pmk, pmk_len);
  205. entry->pmk_len = pmk_len;
  206. rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid,
  207. wpa_key_mgmt_sha256(akmp));
  208. os_get_time(&now);
  209. entry->expiration = now.sec;
  210. if (session_timeout > 0)
  211. entry->expiration += session_timeout;
  212. else
  213. entry->expiration += dot11RSNAConfigPMKLifetime;
  214. entry->akmp = akmp;
  215. os_memcpy(entry->spa, spa, ETH_ALEN);
  216. pmksa_cache_from_eapol_data(entry, eapol);
  217. /* Replace an old entry for the same STA (if found) with the new entry
  218. */
  219. pos = pmksa_cache_auth_get(pmksa, spa, NULL);
  220. if (pos)
  221. pmksa_cache_free_entry(pmksa, pos);
  222. if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) {
  223. /* Remove the oldest entry to make room for the new entry */
  224. wpa_printf(MSG_DEBUG, "RSN: removed the oldest PMKSA cache "
  225. "entry (for " MACSTR ") to make room for new one",
  226. MAC2STR(pmksa->pmksa->spa));
  227. pmksa_cache_free_entry(pmksa, pmksa->pmksa);
  228. }
  229. pmksa_cache_link_entry(pmksa, entry);
  230. return entry;
  231. }
  232. struct rsn_pmksa_cache_entry *
  233. pmksa_cache_add_okc(struct rsn_pmksa_cache *pmksa,
  234. const struct rsn_pmksa_cache_entry *old_entry,
  235. const u8 *aa, const u8 *pmkid)
  236. {
  237. struct rsn_pmksa_cache_entry *entry;
  238. entry = os_zalloc(sizeof(*entry));
  239. if (entry == NULL)
  240. return NULL;
  241. os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
  242. os_memcpy(entry->pmk, old_entry->pmk, old_entry->pmk_len);
  243. entry->pmk_len = old_entry->pmk_len;
  244. entry->expiration = old_entry->expiration;
  245. entry->akmp = old_entry->akmp;
  246. os_memcpy(entry->spa, old_entry->spa, ETH_ALEN);
  247. entry->opportunistic = 1;
  248. if (old_entry->identity) {
  249. entry->identity = os_malloc(old_entry->identity_len);
  250. if (entry->identity) {
  251. entry->identity_len = old_entry->identity_len;
  252. os_memcpy(entry->identity, old_entry->identity,
  253. old_entry->identity_len);
  254. }
  255. }
  256. radius_copy_class(&entry->radius_class, &old_entry->radius_class);
  257. entry->eap_type_authsrv = old_entry->eap_type_authsrv;
  258. entry->vlan_id = old_entry->vlan_id;
  259. entry->opportunistic = 1;
  260. pmksa_cache_link_entry(pmksa, entry);
  261. return entry;
  262. }
  263. /**
  264. * pmksa_cache_auth_deinit - Free all entries in PMKSA cache
  265. * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
  266. */
  267. void pmksa_cache_auth_deinit(struct rsn_pmksa_cache *pmksa)
  268. {
  269. struct rsn_pmksa_cache_entry *entry, *prev;
  270. int i;
  271. if (pmksa == NULL)
  272. return;
  273. entry = pmksa->pmksa;
  274. while (entry) {
  275. prev = entry;
  276. entry = entry->next;
  277. _pmksa_cache_free_entry(prev);
  278. }
  279. eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
  280. for (i = 0; i < PMKID_HASH_SIZE; i++)
  281. pmksa->pmkid[i] = NULL;
  282. os_free(pmksa);
  283. }
  284. /**
  285. * pmksa_cache_auth_get - Fetch a PMKSA cache entry
  286. * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
  287. * @spa: Supplicant address or %NULL to match any
  288. * @pmkid: PMKID or %NULL to match any
  289. * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
  290. */
  291. struct rsn_pmksa_cache_entry *
  292. pmksa_cache_auth_get(struct rsn_pmksa_cache *pmksa,
  293. const u8 *spa, const u8 *pmkid)
  294. {
  295. struct rsn_pmksa_cache_entry *entry;
  296. if (pmkid)
  297. entry = pmksa->pmkid[PMKID_HASH(pmkid)];
  298. else
  299. entry = pmksa->pmksa;
  300. while (entry) {
  301. if ((spa == NULL ||
  302. os_memcmp(entry->spa, spa, ETH_ALEN) == 0) &&
  303. (pmkid == NULL ||
  304. os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0))
  305. return entry;
  306. entry = pmkid ? entry->hnext : entry->next;
  307. }
  308. return NULL;
  309. }
  310. /**
  311. * pmksa_cache_get_okc - Fetch a PMKSA cache entry using OKC
  312. * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
  313. * @aa: Authenticator address
  314. * @spa: Supplicant address
  315. * @pmkid: PMKID
  316. * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
  317. *
  318. * Use opportunistic key caching (OKC) to find a PMK for a supplicant.
  319. */
  320. struct rsn_pmksa_cache_entry * pmksa_cache_get_okc(
  321. struct rsn_pmksa_cache *pmksa, const u8 *aa, const u8 *spa,
  322. const u8 *pmkid)
  323. {
  324. struct rsn_pmksa_cache_entry *entry;
  325. u8 new_pmkid[PMKID_LEN];
  326. entry = pmksa->pmksa;
  327. while (entry) {
  328. if (os_memcmp(entry->spa, spa, ETH_ALEN) != 0)
  329. continue;
  330. rsn_pmkid(entry->pmk, entry->pmk_len, aa, spa, new_pmkid,
  331. wpa_key_mgmt_sha256(entry->akmp));
  332. if (os_memcmp(new_pmkid, pmkid, PMKID_LEN) == 0)
  333. return entry;
  334. entry = entry->next;
  335. }
  336. return NULL;
  337. }
  338. /**
  339. * pmksa_cache_auth_init - Initialize PMKSA cache
  340. * @free_cb: Callback function to be called when a PMKSA cache entry is freed
  341. * @ctx: Context pointer for free_cb function
  342. * Returns: Pointer to PMKSA cache data or %NULL on failure
  343. */
  344. struct rsn_pmksa_cache *
  345. pmksa_cache_auth_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
  346. void *ctx), void *ctx)
  347. {
  348. struct rsn_pmksa_cache *pmksa;
  349. pmksa = os_zalloc(sizeof(*pmksa));
  350. if (pmksa) {
  351. pmksa->free_cb = free_cb;
  352. pmksa->ctx = ctx;
  353. }
  354. return pmksa;
  355. }