Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

pmksa_cache_auth.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "utils/eloop.h"
  17. #include "eapol_auth/eapol_auth_sm.h"
  18. #include "eapol_auth/eapol_auth_sm_i.h"
  19. #include "sta_info.h"
  20. #include "ap_config.h"
  21. #include "pmksa_cache_auth.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. #ifndef CONFIG_NO_RADIUS
  40. radius_free_class(&entry->radius_class);
  41. #endif /* CONFIG_NO_RADIUS */
  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. #ifndef CONFIG_NO_RADIUS
  121. radius_copy_class(&entry->radius_class, &eapol->radius_class);
  122. #endif /* CONFIG_NO_RADIUS */
  123. entry->eap_type_authsrv = eapol->eap_type_authsrv;
  124. entry->vlan_id = ((struct sta_info *) eapol->sta)->vlan_id;
  125. }
  126. void pmksa_cache_to_eapol_data(struct rsn_pmksa_cache_entry *entry,
  127. struct eapol_state_machine *eapol)
  128. {
  129. if (entry == NULL || eapol == NULL)
  130. return;
  131. if (entry->identity) {
  132. os_free(eapol->identity);
  133. eapol->identity = os_malloc(entry->identity_len);
  134. if (eapol->identity) {
  135. eapol->identity_len = entry->identity_len;
  136. os_memcpy(eapol->identity, entry->identity,
  137. entry->identity_len);
  138. }
  139. wpa_hexdump_ascii(MSG_DEBUG, "STA identity from PMKSA",
  140. eapol->identity, eapol->identity_len);
  141. }
  142. #ifndef CONFIG_NO_RADIUS
  143. radius_free_class(&eapol->radius_class);
  144. radius_copy_class(&eapol->radius_class, &entry->radius_class);
  145. #endif /* CONFIG_NO_RADIUS */
  146. if (eapol->radius_class.attr) {
  147. wpa_printf(MSG_DEBUG, "Copied %lu Class attribute(s) from "
  148. "PMKSA", (unsigned long) eapol->radius_class.count);
  149. }
  150. eapol->eap_type_authsrv = entry->eap_type_authsrv;
  151. ((struct sta_info *) eapol->sta)->vlan_id = entry->vlan_id;
  152. }
  153. static void pmksa_cache_link_entry(struct rsn_pmksa_cache *pmksa,
  154. struct rsn_pmksa_cache_entry *entry)
  155. {
  156. struct rsn_pmksa_cache_entry *pos, *prev;
  157. /* Add the new entry; order by expiration time */
  158. pos = pmksa->pmksa;
  159. prev = NULL;
  160. while (pos) {
  161. if (pos->expiration > entry->expiration)
  162. break;
  163. prev = pos;
  164. pos = pos->next;
  165. }
  166. if (prev == NULL) {
  167. entry->next = pmksa->pmksa;
  168. pmksa->pmksa = entry;
  169. } else {
  170. entry->next = prev->next;
  171. prev->next = entry;
  172. }
  173. entry->hnext = pmksa->pmkid[PMKID_HASH(entry->pmkid)];
  174. pmksa->pmkid[PMKID_HASH(entry->pmkid)] = entry;
  175. pmksa->pmksa_count++;
  176. wpa_printf(MSG_DEBUG, "RSN: added PMKSA cache entry for " MACSTR,
  177. MAC2STR(entry->spa));
  178. wpa_hexdump(MSG_DEBUG, "RSN: added PMKID", entry->pmkid, PMKID_LEN);
  179. }
  180. /**
  181. * pmksa_cache_auth_add - Add a PMKSA cache entry
  182. * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
  183. * @pmk: The new pairwise master key
  184. * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
  185. * @aa: Authenticator address
  186. * @spa: Supplicant address
  187. * @session_timeout: Session timeout
  188. * @eapol: Pointer to EAPOL state machine data
  189. * @akmp: WPA_KEY_MGMT_* used in key derivation
  190. * Returns: Pointer to the added PMKSA cache entry or %NULL on error
  191. *
  192. * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
  193. * cache. If an old entry is already in the cache for the same Supplicant,
  194. * this entry will be replaced with the new entry. PMKID will be calculated
  195. * based on the PMK.
  196. */
  197. struct rsn_pmksa_cache_entry *
  198. pmksa_cache_auth_add(struct rsn_pmksa_cache *pmksa,
  199. const u8 *pmk, size_t pmk_len,
  200. const u8 *aa, const u8 *spa, int session_timeout,
  201. struct eapol_state_machine *eapol, int akmp)
  202. {
  203. struct rsn_pmksa_cache_entry *entry, *pos;
  204. struct os_time now;
  205. if (pmk_len > PMK_LEN)
  206. return NULL;
  207. entry = os_zalloc(sizeof(*entry));
  208. if (entry == NULL)
  209. return NULL;
  210. os_memcpy(entry->pmk, pmk, pmk_len);
  211. entry->pmk_len = pmk_len;
  212. rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid,
  213. wpa_key_mgmt_sha256(akmp));
  214. os_get_time(&now);
  215. entry->expiration = now.sec;
  216. if (session_timeout > 0)
  217. entry->expiration += session_timeout;
  218. else
  219. entry->expiration += dot11RSNAConfigPMKLifetime;
  220. entry->akmp = akmp;
  221. os_memcpy(entry->spa, spa, ETH_ALEN);
  222. pmksa_cache_from_eapol_data(entry, eapol);
  223. /* Replace an old entry for the same STA (if found) with the new entry
  224. */
  225. pos = pmksa_cache_auth_get(pmksa, spa, NULL);
  226. if (pos)
  227. pmksa_cache_free_entry(pmksa, pos);
  228. if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) {
  229. /* Remove the oldest entry to make room for the new entry */
  230. wpa_printf(MSG_DEBUG, "RSN: removed the oldest PMKSA cache "
  231. "entry (for " MACSTR ") to make room for new one",
  232. MAC2STR(pmksa->pmksa->spa));
  233. pmksa_cache_free_entry(pmksa, pmksa->pmksa);
  234. }
  235. pmksa_cache_link_entry(pmksa, entry);
  236. return entry;
  237. }
  238. struct rsn_pmksa_cache_entry *
  239. pmksa_cache_add_okc(struct rsn_pmksa_cache *pmksa,
  240. const struct rsn_pmksa_cache_entry *old_entry,
  241. const u8 *aa, const u8 *pmkid)
  242. {
  243. struct rsn_pmksa_cache_entry *entry;
  244. entry = os_zalloc(sizeof(*entry));
  245. if (entry == NULL)
  246. return NULL;
  247. os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
  248. os_memcpy(entry->pmk, old_entry->pmk, old_entry->pmk_len);
  249. entry->pmk_len = old_entry->pmk_len;
  250. entry->expiration = old_entry->expiration;
  251. entry->akmp = old_entry->akmp;
  252. os_memcpy(entry->spa, old_entry->spa, ETH_ALEN);
  253. entry->opportunistic = 1;
  254. if (old_entry->identity) {
  255. entry->identity = os_malloc(old_entry->identity_len);
  256. if (entry->identity) {
  257. entry->identity_len = old_entry->identity_len;
  258. os_memcpy(entry->identity, old_entry->identity,
  259. old_entry->identity_len);
  260. }
  261. }
  262. #ifndef CONFIG_NO_RADIUS
  263. radius_copy_class(&entry->radius_class, &old_entry->radius_class);
  264. #endif /* CONFIG_NO_RADIUS */
  265. entry->eap_type_authsrv = old_entry->eap_type_authsrv;
  266. entry->vlan_id = old_entry->vlan_id;
  267. entry->opportunistic = 1;
  268. pmksa_cache_link_entry(pmksa, entry);
  269. return entry;
  270. }
  271. /**
  272. * pmksa_cache_auth_deinit - Free all entries in PMKSA cache
  273. * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
  274. */
  275. void pmksa_cache_auth_deinit(struct rsn_pmksa_cache *pmksa)
  276. {
  277. struct rsn_pmksa_cache_entry *entry, *prev;
  278. int i;
  279. if (pmksa == NULL)
  280. return;
  281. entry = pmksa->pmksa;
  282. while (entry) {
  283. prev = entry;
  284. entry = entry->next;
  285. _pmksa_cache_free_entry(prev);
  286. }
  287. eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
  288. for (i = 0; i < PMKID_HASH_SIZE; i++)
  289. pmksa->pmkid[i] = NULL;
  290. os_free(pmksa);
  291. }
  292. /**
  293. * pmksa_cache_auth_get - Fetch a PMKSA cache entry
  294. * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
  295. * @spa: Supplicant address or %NULL to match any
  296. * @pmkid: PMKID or %NULL to match any
  297. * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
  298. */
  299. struct rsn_pmksa_cache_entry *
  300. pmksa_cache_auth_get(struct rsn_pmksa_cache *pmksa,
  301. const u8 *spa, const u8 *pmkid)
  302. {
  303. struct rsn_pmksa_cache_entry *entry;
  304. if (pmkid)
  305. entry = pmksa->pmkid[PMKID_HASH(pmkid)];
  306. else
  307. entry = pmksa->pmksa;
  308. while (entry) {
  309. if ((spa == NULL ||
  310. os_memcmp(entry->spa, spa, ETH_ALEN) == 0) &&
  311. (pmkid == NULL ||
  312. os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0))
  313. return entry;
  314. entry = pmkid ? entry->hnext : entry->next;
  315. }
  316. return NULL;
  317. }
  318. /**
  319. * pmksa_cache_get_okc - Fetch a PMKSA cache entry using OKC
  320. * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
  321. * @aa: Authenticator address
  322. * @spa: Supplicant address
  323. * @pmkid: PMKID
  324. * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
  325. *
  326. * Use opportunistic key caching (OKC) to find a PMK for a supplicant.
  327. */
  328. struct rsn_pmksa_cache_entry * pmksa_cache_get_okc(
  329. struct rsn_pmksa_cache *pmksa, const u8 *aa, const u8 *spa,
  330. const u8 *pmkid)
  331. {
  332. struct rsn_pmksa_cache_entry *entry;
  333. u8 new_pmkid[PMKID_LEN];
  334. entry = pmksa->pmksa;
  335. while (entry) {
  336. if (os_memcmp(entry->spa, spa, ETH_ALEN) != 0)
  337. continue;
  338. rsn_pmkid(entry->pmk, entry->pmk_len, aa, spa, new_pmkid,
  339. wpa_key_mgmt_sha256(entry->akmp));
  340. if (os_memcmp(new_pmkid, pmkid, PMKID_LEN) == 0)
  341. return entry;
  342. entry = entry->next;
  343. }
  344. return NULL;
  345. }
  346. /**
  347. * pmksa_cache_auth_init - Initialize PMKSA cache
  348. * @free_cb: Callback function to be called when a PMKSA cache entry is freed
  349. * @ctx: Context pointer for free_cb function
  350. * Returns: Pointer to PMKSA cache data or %NULL on failure
  351. */
  352. struct rsn_pmksa_cache *
  353. pmksa_cache_auth_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
  354. void *ctx), void *ctx)
  355. {
  356. struct rsn_pmksa_cache *pmksa;
  357. pmksa = os_zalloc(sizeof(*pmksa));
  358. if (pmksa) {
  359. pmksa->free_cb = free_cb;
  360. pmksa->ctx = ctx;
  361. }
  362. return pmksa;
  363. }