pmksa_cache.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * hostapd - PMKSA cache for IEEE 802.11i RSN
  3. * Copyright (c) 2004-2006, 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 "ap.h"
  17. #include "config.h"
  18. #include "common.h"
  19. #include "eloop.h"
  20. #include "sha1.h"
  21. #include "ieee802_1x.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. /**
  36. * rsn_pmkid - Calculate PMK identifier
  37. * @pmk: Pairwise master key
  38. * @pmk_len: Length of pmk in bytes
  39. * @aa: Authenticator address
  40. * @spa: Supplicant address
  41. *
  42. * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
  43. * PMKID = HMAC-SHA1-128(PMK, "PMK Name" || AA || SPA)
  44. */
  45. void rsn_pmkid(const u8 *pmk, size_t pmk_len, const u8 *aa, const u8 *spa,
  46. u8 *pmkid)
  47. {
  48. char *title = "PMK Name";
  49. const u8 *addr[3];
  50. const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
  51. unsigned char hash[SHA1_MAC_LEN];
  52. addr[0] = (u8 *) title;
  53. addr[1] = aa;
  54. addr[2] = spa;
  55. hmac_sha1_vector(pmk, pmk_len, 3, addr, len, hash);
  56. os_memcpy(pmkid, hash, PMKID_LEN);
  57. }
  58. static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa);
  59. static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry *entry)
  60. {
  61. if (entry == NULL)
  62. return;
  63. os_free(entry->identity);
  64. ieee802_1x_free_radius_class(&entry->radius_class);
  65. os_free(entry);
  66. }
  67. static void pmksa_cache_free_entry(struct rsn_pmksa_cache *pmksa,
  68. struct rsn_pmksa_cache_entry *entry)
  69. {
  70. struct rsn_pmksa_cache_entry *pos, *prev;
  71. pmksa->pmksa_count--;
  72. pmksa->free_cb(entry, pmksa->ctx);
  73. pos = pmksa->pmkid[PMKID_HASH(entry->pmkid)];
  74. prev = NULL;
  75. while (pos) {
  76. if (pos == entry) {
  77. if (prev != NULL) {
  78. prev->hnext = pos->hnext;
  79. } else {
  80. pmksa->pmkid[PMKID_HASH(entry->pmkid)] =
  81. pos->hnext;
  82. }
  83. break;
  84. }
  85. prev = pos;
  86. pos = pos->hnext;
  87. }
  88. pos = pmksa->pmksa;
  89. prev = NULL;
  90. while (pos) {
  91. if (pos == entry) {
  92. if (prev != NULL)
  93. prev->next = pos->next;
  94. else
  95. pmksa->pmksa = pos->next;
  96. break;
  97. }
  98. prev = pos;
  99. pos = pos->next;
  100. }
  101. _pmksa_cache_free_entry(entry);
  102. }
  103. static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx)
  104. {
  105. struct rsn_pmksa_cache *pmksa = eloop_ctx;
  106. struct os_time now;
  107. os_get_time(&now);
  108. while (pmksa->pmksa && pmksa->pmksa->expiration <= now.sec) {
  109. struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
  110. pmksa->pmksa = entry->next;
  111. wpa_printf(MSG_DEBUG, "RSN: expired PMKSA cache entry for "
  112. MACSTR, MAC2STR(entry->spa));
  113. pmksa_cache_free_entry(pmksa, entry);
  114. }
  115. pmksa_cache_set_expiration(pmksa);
  116. }
  117. static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa)
  118. {
  119. int sec;
  120. struct os_time now;
  121. eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
  122. if (pmksa->pmksa == NULL)
  123. return;
  124. os_get_time(&now);
  125. sec = pmksa->pmksa->expiration - now.sec;
  126. if (sec < 0)
  127. sec = 0;
  128. eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, pmksa, NULL);
  129. }
  130. static void pmksa_cache_from_eapol_data(struct rsn_pmksa_cache_entry *entry,
  131. struct eapol_state_machine *eapol)
  132. {
  133. if (eapol == NULL)
  134. return;
  135. if (eapol->identity) {
  136. entry->identity = os_malloc(eapol->identity_len);
  137. if (entry->identity) {
  138. entry->identity_len = eapol->identity_len;
  139. os_memcpy(entry->identity, eapol->identity,
  140. eapol->identity_len);
  141. }
  142. }
  143. ieee802_1x_copy_radius_class(&entry->radius_class,
  144. &eapol->radius_class);
  145. entry->eap_type_authsrv = eapol->eap_type_authsrv;
  146. entry->vlan_id = eapol->sta->vlan_id;
  147. }
  148. void pmksa_cache_to_eapol_data(struct rsn_pmksa_cache_entry *entry,
  149. struct eapol_state_machine *eapol)
  150. {
  151. if (entry == NULL || eapol == NULL)
  152. return;
  153. if (entry->identity) {
  154. os_free(eapol->identity);
  155. eapol->identity = os_malloc(entry->identity_len);
  156. if (eapol->identity) {
  157. eapol->identity_len = entry->identity_len;
  158. os_memcpy(eapol->identity, entry->identity,
  159. entry->identity_len);
  160. }
  161. wpa_hexdump_ascii(MSG_DEBUG, "STA identity from PMKSA",
  162. eapol->identity, eapol->identity_len);
  163. }
  164. ieee802_1x_free_radius_class(&eapol->radius_class);
  165. ieee802_1x_copy_radius_class(&eapol->radius_class,
  166. &entry->radius_class);
  167. if (eapol->radius_class.attr) {
  168. wpa_printf(MSG_DEBUG, "Copied %lu Class attribute(s) from "
  169. "PMKSA", (unsigned long) eapol->radius_class.count);
  170. }
  171. eapol->eap_type_authsrv = entry->eap_type_authsrv;
  172. eapol->sta->vlan_id = entry->vlan_id;
  173. }
  174. /**
  175. * pmksa_cache_add - Add a PMKSA cache entry
  176. * @pmksa: Pointer to PMKSA cache data from pmksa_cache_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. * Returns: Pointer to the added PMKSA cache entry or %NULL on error
  184. *
  185. * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
  186. * cache. If an old entry is already in the cache for the same Supplicant,
  187. * this entry will be replaced with the new entry. PMKID will be calculated
  188. * based on the PMK.
  189. */
  190. struct rsn_pmksa_cache_entry *
  191. pmksa_cache_add(struct rsn_pmksa_cache *pmksa, const u8 *pmk, size_t pmk_len,
  192. const u8 *aa, const u8 *spa, int session_timeout,
  193. struct eapol_state_machine *eapol)
  194. {
  195. struct rsn_pmksa_cache_entry *entry, *pos, *prev;
  196. struct os_time now;
  197. if (pmk_len > PMK_LEN)
  198. return NULL;
  199. entry = os_zalloc(sizeof(*entry));
  200. if (entry == NULL)
  201. return NULL;
  202. os_memcpy(entry->pmk, pmk, pmk_len);
  203. entry->pmk_len = pmk_len;
  204. rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid);
  205. os_get_time(&now);
  206. entry->expiration = now.sec;
  207. if (session_timeout > 0)
  208. entry->expiration += session_timeout;
  209. else
  210. entry->expiration += dot11RSNAConfigPMKLifetime;
  211. entry->akmp = WPA_KEY_MGMT_IEEE8021X;
  212. os_memcpy(entry->spa, spa, ETH_ALEN);
  213. pmksa_cache_from_eapol_data(entry, eapol);
  214. /* Replace an old entry for the same STA (if found) with the new entry
  215. */
  216. pos = pmksa_cache_get(pmksa, spa, NULL);
  217. if (pos)
  218. pmksa_cache_free_entry(pmksa, pos);
  219. if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) {
  220. /* Remove the oldest entry to make room for the new entry */
  221. wpa_printf(MSG_DEBUG, "RSN: removed the oldest PMKSA cache "
  222. "entry (for " MACSTR ") to make room for new one",
  223. MAC2STR(pmksa->pmksa->spa));
  224. pmksa_cache_free_entry(pmksa, pmksa->pmksa);
  225. }
  226. /* Add the new entry; order by expiration time */
  227. pos = pmksa->pmksa;
  228. prev = NULL;
  229. while (pos) {
  230. if (pos->expiration > entry->expiration)
  231. break;
  232. prev = pos;
  233. pos = pos->next;
  234. }
  235. if (prev == NULL) {
  236. entry->next = pmksa->pmksa;
  237. pmksa->pmksa = entry;
  238. } else {
  239. entry->next = prev->next;
  240. prev->next = entry;
  241. }
  242. entry->hnext = pmksa->pmkid[PMKID_HASH(entry->pmkid)];
  243. pmksa->pmkid[PMKID_HASH(entry->pmkid)] = entry;
  244. pmksa->pmksa_count++;
  245. wpa_printf(MSG_DEBUG, "RSN: added PMKSA cache entry for " MACSTR,
  246. MAC2STR(entry->spa));
  247. wpa_hexdump(MSG_DEBUG, "RSN: added PMKID", entry->pmkid, PMKID_LEN);
  248. return entry;
  249. }
  250. /**
  251. * pmksa_cache_deinit - Free all entries in PMKSA cache
  252. * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
  253. */
  254. void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa)
  255. {
  256. struct rsn_pmksa_cache_entry *entry, *prev;
  257. int i;
  258. if (pmksa == NULL)
  259. return;
  260. entry = pmksa->pmksa;
  261. while (entry) {
  262. prev = entry;
  263. entry = entry->next;
  264. _pmksa_cache_free_entry(prev);
  265. }
  266. eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
  267. for (i = 0; i < PMKID_HASH_SIZE; i++)
  268. pmksa->pmkid[i] = NULL;
  269. os_free(pmksa);
  270. }
  271. /**
  272. * pmksa_cache_get - Fetch a PMKSA cache entry
  273. * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
  274. * @spa: Supplicant address or %NULL to match any
  275. * @pmkid: PMKID or %NULL to match any
  276. * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
  277. */
  278. struct rsn_pmksa_cache_entry * pmksa_cache_get(struct rsn_pmksa_cache *pmksa,
  279. const u8 *spa, const u8 *pmkid)
  280. {
  281. struct rsn_pmksa_cache_entry *entry;
  282. if (pmkid)
  283. entry = pmksa->pmkid[PMKID_HASH(pmkid)];
  284. else
  285. entry = pmksa->pmksa;
  286. while (entry) {
  287. if ((spa == NULL ||
  288. os_memcmp(entry->spa, spa, ETH_ALEN) == 0) &&
  289. (pmkid == NULL ||
  290. os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0))
  291. return entry;
  292. entry = pmkid ? entry->hnext : entry->next;
  293. }
  294. return NULL;
  295. }
  296. /**
  297. * pmksa_cache_init - Initialize PMKSA cache
  298. * @free_cb: Callback function to be called when a PMKSA cache entry is freed
  299. * @ctx: Context pointer for free_cb function
  300. * Returns: Pointer to PMKSA cache data or %NULL on failure
  301. */
  302. struct rsn_pmksa_cache *
  303. pmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
  304. void *ctx), void *ctx)
  305. {
  306. struct rsn_pmksa_cache *pmksa;
  307. pmksa = os_zalloc(sizeof(*pmksa));
  308. if (pmksa) {
  309. pmksa->free_cb = free_cb;
  310. pmksa->ctx = ctx;
  311. }
  312. return pmksa;
  313. }