pmksa_cache.c 12 KB

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