pmksa_cache.c 12 KB

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