pmksa_cache.c 11 KB

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