pmksa_cache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. static void pmksa_cache_link_entry(struct rsn_pmksa_cache *pmksa,
  175. struct rsn_pmksa_cache_entry *entry)
  176. {
  177. struct rsn_pmksa_cache_entry *pos, *prev;
  178. /* Add the new entry; order by expiration time */
  179. pos = pmksa->pmksa;
  180. prev = NULL;
  181. while (pos) {
  182. if (pos->expiration > entry->expiration)
  183. break;
  184. prev = pos;
  185. pos = pos->next;
  186. }
  187. if (prev == NULL) {
  188. entry->next = pmksa->pmksa;
  189. pmksa->pmksa = entry;
  190. } else {
  191. entry->next = prev->next;
  192. prev->next = entry;
  193. }
  194. entry->hnext = pmksa->pmkid[PMKID_HASH(entry->pmkid)];
  195. pmksa->pmkid[PMKID_HASH(entry->pmkid)] = entry;
  196. pmksa->pmksa_count++;
  197. wpa_printf(MSG_DEBUG, "RSN: added PMKSA cache entry for " MACSTR,
  198. MAC2STR(entry->spa));
  199. wpa_hexdump(MSG_DEBUG, "RSN: added PMKID", entry->pmkid, PMKID_LEN);
  200. }
  201. /**
  202. * pmksa_cache_add - Add a PMKSA cache entry
  203. * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
  204. * @pmk: The new pairwise master key
  205. * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
  206. * @aa: Authenticator address
  207. * @spa: Supplicant address
  208. * @session_timeout: Session timeout
  209. * @eapol: Pointer to EAPOL state machine data
  210. * Returns: Pointer to the added PMKSA cache entry or %NULL on error
  211. *
  212. * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
  213. * cache. If an old entry is already in the cache for the same Supplicant,
  214. * this entry will be replaced with the new entry. PMKID will be calculated
  215. * based on the PMK.
  216. */
  217. struct rsn_pmksa_cache_entry *
  218. pmksa_cache_add(struct rsn_pmksa_cache *pmksa, const u8 *pmk, size_t pmk_len,
  219. const u8 *aa, const u8 *spa, int session_timeout,
  220. struct eapol_state_machine *eapol)
  221. {
  222. struct rsn_pmksa_cache_entry *entry, *pos;
  223. struct os_time now;
  224. if (pmk_len > PMK_LEN)
  225. return NULL;
  226. entry = os_zalloc(sizeof(*entry));
  227. if (entry == NULL)
  228. return NULL;
  229. os_memcpy(entry->pmk, pmk, pmk_len);
  230. entry->pmk_len = pmk_len;
  231. rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid);
  232. os_get_time(&now);
  233. entry->expiration = now.sec;
  234. if (session_timeout > 0)
  235. entry->expiration += session_timeout;
  236. else
  237. entry->expiration += dot11RSNAConfigPMKLifetime;
  238. entry->akmp = WPA_KEY_MGMT_IEEE8021X;
  239. os_memcpy(entry->spa, spa, ETH_ALEN);
  240. pmksa_cache_from_eapol_data(entry, eapol);
  241. /* Replace an old entry for the same STA (if found) with the new entry
  242. */
  243. pos = pmksa_cache_get(pmksa, spa, NULL);
  244. if (pos)
  245. pmksa_cache_free_entry(pmksa, pos);
  246. if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) {
  247. /* Remove the oldest entry to make room for the new entry */
  248. wpa_printf(MSG_DEBUG, "RSN: removed the oldest PMKSA cache "
  249. "entry (for " MACSTR ") to make room for new one",
  250. MAC2STR(pmksa->pmksa->spa));
  251. pmksa_cache_free_entry(pmksa, pmksa->pmksa);
  252. }
  253. pmksa_cache_link_entry(pmksa, entry);
  254. return entry;
  255. }
  256. struct rsn_pmksa_cache_entry *
  257. pmksa_cache_add_okc(struct rsn_pmksa_cache *pmksa,
  258. const struct rsn_pmksa_cache_entry *old_entry,
  259. const u8 *aa, const u8 *pmkid)
  260. {
  261. struct rsn_pmksa_cache_entry *entry;
  262. entry = os_zalloc(sizeof(*entry));
  263. if (entry == NULL)
  264. return NULL;
  265. os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
  266. os_memcpy(entry->pmk, old_entry->pmk, old_entry->pmk_len);
  267. entry->pmk_len = old_entry->pmk_len;
  268. entry->expiration = old_entry->expiration;
  269. entry->akmp = old_entry->akmp;
  270. os_memcpy(entry->spa, old_entry->spa, ETH_ALEN);
  271. entry->opportunistic = 1;
  272. if (old_entry->identity) {
  273. entry->identity = os_malloc(old_entry->identity_len);
  274. if (entry->identity) {
  275. entry->identity_len = old_entry->identity_len;
  276. os_memcpy(entry->identity, old_entry->identity,
  277. old_entry->identity_len);
  278. }
  279. }
  280. ieee802_1x_copy_radius_class(&entry->radius_class,
  281. &old_entry->radius_class);
  282. entry->eap_type_authsrv = old_entry->eap_type_authsrv;
  283. entry->vlan_id = old_entry->vlan_id;
  284. entry->opportunistic = 1;
  285. pmksa_cache_link_entry(pmksa, entry);
  286. return entry;
  287. }
  288. /**
  289. * pmksa_cache_deinit - Free all entries in PMKSA cache
  290. * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
  291. */
  292. void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa)
  293. {
  294. struct rsn_pmksa_cache_entry *entry, *prev;
  295. int i;
  296. if (pmksa == NULL)
  297. return;
  298. entry = pmksa->pmksa;
  299. while (entry) {
  300. prev = entry;
  301. entry = entry->next;
  302. _pmksa_cache_free_entry(prev);
  303. }
  304. eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
  305. for (i = 0; i < PMKID_HASH_SIZE; i++)
  306. pmksa->pmkid[i] = NULL;
  307. os_free(pmksa);
  308. }
  309. /**
  310. * pmksa_cache_get - Fetch a PMKSA cache entry
  311. * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
  312. * @spa: Supplicant address or %NULL to match any
  313. * @pmkid: PMKID or %NULL to match any
  314. * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
  315. */
  316. struct rsn_pmksa_cache_entry * pmksa_cache_get(struct rsn_pmksa_cache *pmksa,
  317. const u8 *spa, const u8 *pmkid)
  318. {
  319. struct rsn_pmksa_cache_entry *entry;
  320. if (pmkid)
  321. entry = pmksa->pmkid[PMKID_HASH(pmkid)];
  322. else
  323. entry = pmksa->pmksa;
  324. while (entry) {
  325. if ((spa == NULL ||
  326. os_memcmp(entry->spa, spa, ETH_ALEN) == 0) &&
  327. (pmkid == NULL ||
  328. os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0))
  329. return entry;
  330. entry = pmkid ? entry->hnext : entry->next;
  331. }
  332. return NULL;
  333. }
  334. /**
  335. * pmksa_cache_get_okc - Fetch a PMKSA cache entry using OKC
  336. * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
  337. * @spa: Supplicant address
  338. * @pmkid: PMKID
  339. * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
  340. *
  341. * Use opportunistic key caching (OKC) to find a PMK for a supplicant.
  342. */
  343. struct rsn_pmksa_cache_entry * pmksa_cache_get_okc(
  344. struct rsn_pmksa_cache *pmksa, const u8 *aa, const u8 *spa,
  345. const u8 *pmkid)
  346. {
  347. struct rsn_pmksa_cache_entry *entry;
  348. u8 new_pmkid[PMKID_LEN];
  349. entry = pmksa->pmksa;
  350. while (entry) {
  351. if (os_memcmp(entry->spa, spa, ETH_ALEN) != 0)
  352. continue;
  353. rsn_pmkid(entry->pmk, entry->pmk_len, aa, spa, new_pmkid);
  354. if (os_memcmp(new_pmkid, pmkid, PMKID_LEN) == 0)
  355. return entry;
  356. entry = entry->next;
  357. }
  358. return NULL;
  359. }
  360. /**
  361. * pmksa_cache_init - Initialize PMKSA cache
  362. * @free_cb: Callback function to be called when a PMKSA cache entry is freed
  363. * @ctx: Context pointer for free_cb function
  364. * Returns: Pointer to PMKSA cache data or %NULL on failure
  365. */
  366. struct rsn_pmksa_cache *
  367. pmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
  368. void *ctx), void *ctx)
  369. {
  370. struct rsn_pmksa_cache *pmksa;
  371. pmksa = os_zalloc(sizeof(*pmksa));
  372. if (pmksa) {
  373. pmksa->free_cb = free_cb;
  374. pmksa->ctx = ctx;
  375. }
  376. return pmksa;
  377. }