pmksa_cache_auth.c 12 KB

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