pmksa_cache.c 12 KB

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