pmksa_cache_auth.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * hostapd - PMKSA cache for IEEE 802.11i RSN
  3. * Copyright (c) 2004-2008, 2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "eapol_auth/eapol_auth_sm.h"
  12. #include "eapol_auth/eapol_auth_sm_i.h"
  13. #include "radius/radius_das.h"
  14. #include "sta_info.h"
  15. #include "ap_config.h"
  16. #include "pmksa_cache_auth.h"
  17. static const int pmksa_cache_max_entries = 1024;
  18. static const int dot11RSNAConfigPMKLifetime = 43200;
  19. struct rsn_pmksa_cache {
  20. #define PMKID_HASH_SIZE 128
  21. #define PMKID_HASH(pmkid) (unsigned int) ((pmkid)[0] & 0x7f)
  22. struct rsn_pmksa_cache_entry *pmkid[PMKID_HASH_SIZE];
  23. struct rsn_pmksa_cache_entry *pmksa;
  24. int pmksa_count;
  25. void (*free_cb)(struct rsn_pmksa_cache_entry *entry, void *ctx);
  26. void *ctx;
  27. };
  28. static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa);
  29. static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry *entry)
  30. {
  31. os_free(entry->identity);
  32. wpabuf_free(entry->cui);
  33. #ifndef CONFIG_NO_RADIUS
  34. radius_free_class(&entry->radius_class);
  35. #endif /* CONFIG_NO_RADIUS */
  36. bin_clear_free(entry, sizeof(*entry));
  37. }
  38. void pmksa_cache_free_entry(struct rsn_pmksa_cache *pmksa,
  39. struct rsn_pmksa_cache_entry *entry)
  40. {
  41. struct rsn_pmksa_cache_entry *pos, *prev;
  42. unsigned int hash;
  43. pmksa->pmksa_count--;
  44. pmksa->free_cb(entry, pmksa->ctx);
  45. /* unlink from hash list */
  46. hash = PMKID_HASH(entry->pmkid);
  47. pos = pmksa->pmkid[hash];
  48. prev = NULL;
  49. while (pos) {
  50. if (pos == entry) {
  51. if (prev != NULL)
  52. prev->hnext = entry->hnext;
  53. else
  54. pmksa->pmkid[hash] = entry->hnext;
  55. break;
  56. }
  57. prev = pos;
  58. pos = pos->hnext;
  59. }
  60. /* unlink from entry list */
  61. pos = pmksa->pmksa;
  62. prev = NULL;
  63. while (pos) {
  64. if (pos == entry) {
  65. if (prev != NULL)
  66. prev->next = entry->next;
  67. else
  68. pmksa->pmksa = entry->next;
  69. break;
  70. }
  71. prev = pos;
  72. pos = pos->next;
  73. }
  74. _pmksa_cache_free_entry(entry);
  75. }
  76. static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx)
  77. {
  78. struct rsn_pmksa_cache *pmksa = eloop_ctx;
  79. struct os_reltime now;
  80. os_get_reltime(&now);
  81. while (pmksa->pmksa && pmksa->pmksa->expiration <= now.sec) {
  82. wpa_printf(MSG_DEBUG, "RSN: expired PMKSA cache entry for "
  83. MACSTR, MAC2STR(pmksa->pmksa->spa));
  84. pmksa_cache_free_entry(pmksa, pmksa->pmksa);
  85. }
  86. pmksa_cache_set_expiration(pmksa);
  87. }
  88. static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa)
  89. {
  90. int sec;
  91. struct os_reltime now;
  92. eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
  93. if (pmksa->pmksa == NULL)
  94. return;
  95. os_get_reltime(&now);
  96. sec = pmksa->pmksa->expiration - now.sec;
  97. if (sec < 0)
  98. sec = 0;
  99. eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, pmksa, NULL);
  100. }
  101. static void pmksa_cache_from_eapol_data(struct rsn_pmksa_cache_entry *entry,
  102. struct eapol_state_machine *eapol)
  103. {
  104. if (eapol == NULL)
  105. return;
  106. if (eapol->identity) {
  107. entry->identity = os_malloc(eapol->identity_len);
  108. if (entry->identity) {
  109. entry->identity_len = eapol->identity_len;
  110. os_memcpy(entry->identity, eapol->identity,
  111. eapol->identity_len);
  112. }
  113. }
  114. if (eapol->radius_cui)
  115. entry->cui = wpabuf_dup(eapol->radius_cui);
  116. #ifndef CONFIG_NO_RADIUS
  117. radius_copy_class(&entry->radius_class, &eapol->radius_class);
  118. #endif /* CONFIG_NO_RADIUS */
  119. entry->eap_type_authsrv = eapol->eap_type_authsrv;
  120. entry->vlan_id = ((struct sta_info *) eapol->sta)->vlan_id;
  121. entry->acct_multi_session_id_hi = eapol->acct_multi_session_id_hi;
  122. entry->acct_multi_session_id_lo = eapol->acct_multi_session_id_lo;
  123. }
  124. void pmksa_cache_to_eapol_data(struct rsn_pmksa_cache_entry *entry,
  125. struct eapol_state_machine *eapol)
  126. {
  127. if (entry == NULL || eapol == NULL)
  128. return;
  129. if (entry->identity) {
  130. os_free(eapol->identity);
  131. eapol->identity = os_malloc(entry->identity_len);
  132. if (eapol->identity) {
  133. eapol->identity_len = entry->identity_len;
  134. os_memcpy(eapol->identity, entry->identity,
  135. entry->identity_len);
  136. }
  137. wpa_hexdump_ascii(MSG_DEBUG, "STA identity from PMKSA",
  138. eapol->identity, eapol->identity_len);
  139. }
  140. if (entry->cui) {
  141. wpabuf_free(eapol->radius_cui);
  142. eapol->radius_cui = wpabuf_dup(entry->cui);
  143. }
  144. #ifndef CONFIG_NO_RADIUS
  145. radius_free_class(&eapol->radius_class);
  146. radius_copy_class(&eapol->radius_class, &entry->radius_class);
  147. #endif /* CONFIG_NO_RADIUS */
  148. if (eapol->radius_class.attr) {
  149. wpa_printf(MSG_DEBUG, "Copied %lu Class attribute(s) from "
  150. "PMKSA", (unsigned long) eapol->radius_class.count);
  151. }
  152. eapol->eap_type_authsrv = entry->eap_type_authsrv;
  153. ((struct sta_info *) eapol->sta)->vlan_id = entry->vlan_id;
  154. eapol->acct_multi_session_id_hi = entry->acct_multi_session_id_hi;
  155. eapol->acct_multi_session_id_lo = entry->acct_multi_session_id_lo;
  156. }
  157. static void pmksa_cache_link_entry(struct rsn_pmksa_cache *pmksa,
  158. struct rsn_pmksa_cache_entry *entry)
  159. {
  160. struct rsn_pmksa_cache_entry *pos, *prev;
  161. int hash;
  162. /* Add the new entry; order by expiration time */
  163. pos = pmksa->pmksa;
  164. prev = NULL;
  165. while (pos) {
  166. if (pos->expiration > entry->expiration)
  167. break;
  168. prev = pos;
  169. pos = pos->next;
  170. }
  171. if (prev == NULL) {
  172. entry->next = pmksa->pmksa;
  173. pmksa->pmksa = entry;
  174. } else {
  175. entry->next = prev->next;
  176. prev->next = entry;
  177. }
  178. hash = PMKID_HASH(entry->pmkid);
  179. entry->hnext = pmksa->pmkid[hash];
  180. pmksa->pmkid[hash] = entry;
  181. pmksa->pmksa_count++;
  182. if (prev == NULL)
  183. pmksa_cache_set_expiration(pmksa);
  184. wpa_printf(MSG_DEBUG, "RSN: added PMKSA cache entry for " MACSTR,
  185. MAC2STR(entry->spa));
  186. wpa_hexdump(MSG_DEBUG, "RSN: added PMKID", entry->pmkid, PMKID_LEN);
  187. }
  188. /**
  189. * pmksa_cache_auth_add - Add a PMKSA cache entry
  190. * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
  191. * @pmk: The new pairwise master key
  192. * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
  193. * @kck: Key confirmation key or %NULL if not yet derived
  194. * @kck_len: KCK length in bytes
  195. * @aa: Authenticator address
  196. * @spa: Supplicant address
  197. * @session_timeout: Session timeout
  198. * @eapol: Pointer to EAPOL state machine data
  199. * @akmp: WPA_KEY_MGMT_* used in key derivation
  200. * Returns: Pointer to the added PMKSA cache entry or %NULL on error
  201. *
  202. * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
  203. * cache. If an old entry is already in the cache for the same Supplicant,
  204. * this entry will be replaced with the new entry. PMKID will be calculated
  205. * based on the PMK.
  206. */
  207. struct rsn_pmksa_cache_entry *
  208. pmksa_cache_auth_add(struct rsn_pmksa_cache *pmksa,
  209. const u8 *pmk, size_t pmk_len,
  210. const u8 *kck, size_t kck_len,
  211. const u8 *aa, const u8 *spa, int session_timeout,
  212. struct eapol_state_machine *eapol, int akmp)
  213. {
  214. struct rsn_pmksa_cache_entry *entry, *pos;
  215. struct os_reltime now;
  216. if (pmk_len > PMK_LEN)
  217. return NULL;
  218. if (wpa_key_mgmt_suite_b(akmp) && !kck)
  219. return NULL;
  220. entry = os_zalloc(sizeof(*entry));
  221. if (entry == NULL)
  222. return NULL;
  223. os_memcpy(entry->pmk, pmk, pmk_len);
  224. entry->pmk_len = pmk_len;
  225. if (wpa_key_mgmt_suite_b(akmp))
  226. rsn_pmkid_suite_b(kck, kck_len, aa, spa, entry->pmkid);
  227. else
  228. rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid,
  229. wpa_key_mgmt_sha256(akmp));
  230. os_get_reltime(&now);
  231. entry->expiration = now.sec;
  232. if (session_timeout > 0)
  233. entry->expiration += session_timeout;
  234. else
  235. entry->expiration += dot11RSNAConfigPMKLifetime;
  236. entry->akmp = akmp;
  237. os_memcpy(entry->spa, spa, ETH_ALEN);
  238. pmksa_cache_from_eapol_data(entry, eapol);
  239. /* Replace an old entry for the same STA (if found) with the new entry
  240. */
  241. pos = pmksa_cache_auth_get(pmksa, spa, NULL);
  242. if (pos)
  243. pmksa_cache_free_entry(pmksa, pos);
  244. if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) {
  245. /* Remove the oldest entry to make room for the new entry */
  246. wpa_printf(MSG_DEBUG, "RSN: removed the oldest PMKSA cache "
  247. "entry (for " MACSTR ") to make room for new one",
  248. MAC2STR(pmksa->pmksa->spa));
  249. pmksa_cache_free_entry(pmksa, pmksa->pmksa);
  250. }
  251. pmksa_cache_link_entry(pmksa, entry);
  252. return entry;
  253. }
  254. struct rsn_pmksa_cache_entry *
  255. pmksa_cache_add_okc(struct rsn_pmksa_cache *pmksa,
  256. const struct rsn_pmksa_cache_entry *old_entry,
  257. const u8 *aa, const u8 *pmkid)
  258. {
  259. struct rsn_pmksa_cache_entry *entry;
  260. entry = os_zalloc(sizeof(*entry));
  261. if (entry == NULL)
  262. return NULL;
  263. os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
  264. os_memcpy(entry->pmk, old_entry->pmk, old_entry->pmk_len);
  265. entry->pmk_len = old_entry->pmk_len;
  266. entry->expiration = old_entry->expiration;
  267. entry->akmp = old_entry->akmp;
  268. os_memcpy(entry->spa, old_entry->spa, ETH_ALEN);
  269. entry->opportunistic = 1;
  270. if (old_entry->identity) {
  271. entry->identity = os_malloc(old_entry->identity_len);
  272. if (entry->identity) {
  273. entry->identity_len = old_entry->identity_len;
  274. os_memcpy(entry->identity, old_entry->identity,
  275. old_entry->identity_len);
  276. }
  277. }
  278. if (old_entry->cui)
  279. entry->cui = wpabuf_dup(old_entry->cui);
  280. #ifndef CONFIG_NO_RADIUS
  281. radius_copy_class(&entry->radius_class, &old_entry->radius_class);
  282. #endif /* CONFIG_NO_RADIUS */
  283. entry->eap_type_authsrv = old_entry->eap_type_authsrv;
  284. entry->vlan_id = old_entry->vlan_id;
  285. entry->opportunistic = 1;
  286. pmksa_cache_link_entry(pmksa, entry);
  287. return entry;
  288. }
  289. /**
  290. * pmksa_cache_auth_deinit - Free all entries in PMKSA cache
  291. * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
  292. */
  293. void pmksa_cache_auth_deinit(struct rsn_pmksa_cache *pmksa)
  294. {
  295. struct rsn_pmksa_cache_entry *entry, *prev;
  296. int i;
  297. if (pmksa == NULL)
  298. return;
  299. entry = pmksa->pmksa;
  300. while (entry) {
  301. prev = entry;
  302. entry = entry->next;
  303. _pmksa_cache_free_entry(prev);
  304. }
  305. eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
  306. pmksa->pmksa_count = 0;
  307. pmksa->pmksa = NULL;
  308. for (i = 0; i < PMKID_HASH_SIZE; i++)
  309. pmksa->pmkid[i] = NULL;
  310. os_free(pmksa);
  311. }
  312. /**
  313. * pmksa_cache_auth_get - Fetch a PMKSA cache entry
  314. * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
  315. * @spa: Supplicant address or %NULL to match any
  316. * @pmkid: PMKID or %NULL to match any
  317. * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
  318. */
  319. struct rsn_pmksa_cache_entry *
  320. pmksa_cache_auth_get(struct rsn_pmksa_cache *pmksa,
  321. const u8 *spa, const u8 *pmkid)
  322. {
  323. struct rsn_pmksa_cache_entry *entry;
  324. if (pmkid) {
  325. for (entry = pmksa->pmkid[PMKID_HASH(pmkid)]; entry;
  326. entry = entry->hnext) {
  327. if ((spa == NULL ||
  328. os_memcmp(entry->spa, spa, ETH_ALEN) == 0) &&
  329. os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0)
  330. return entry;
  331. }
  332. } else {
  333. for (entry = pmksa->pmksa; entry; entry = entry->next) {
  334. if (spa == NULL ||
  335. os_memcmp(entry->spa, spa, ETH_ALEN) == 0)
  336. return entry;
  337. }
  338. }
  339. return NULL;
  340. }
  341. /**
  342. * pmksa_cache_get_okc - Fetch a PMKSA cache entry using OKC
  343. * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
  344. * @aa: Authenticator address
  345. * @spa: Supplicant address
  346. * @pmkid: PMKID
  347. * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
  348. *
  349. * Use opportunistic key caching (OKC) to find a PMK for a supplicant.
  350. */
  351. struct rsn_pmksa_cache_entry * pmksa_cache_get_okc(
  352. struct rsn_pmksa_cache *pmksa, const u8 *aa, const u8 *spa,
  353. const u8 *pmkid)
  354. {
  355. struct rsn_pmksa_cache_entry *entry;
  356. u8 new_pmkid[PMKID_LEN];
  357. for (entry = pmksa->pmksa; entry; entry = entry->next) {
  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. }
  365. return NULL;
  366. }
  367. /**
  368. * pmksa_cache_auth_init - Initialize PMKSA cache
  369. * @free_cb: Callback function to be called when a PMKSA cache entry is freed
  370. * @ctx: Context pointer for free_cb function
  371. * Returns: Pointer to PMKSA cache data or %NULL on failure
  372. */
  373. struct rsn_pmksa_cache *
  374. pmksa_cache_auth_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
  375. void *ctx), void *ctx)
  376. {
  377. struct rsn_pmksa_cache *pmksa;
  378. pmksa = os_zalloc(sizeof(*pmksa));
  379. if (pmksa) {
  380. pmksa->free_cb = free_cb;
  381. pmksa->ctx = ctx;
  382. }
  383. return pmksa;
  384. }
  385. static int das_attr_match(struct rsn_pmksa_cache_entry *entry,
  386. struct radius_das_attrs *attr)
  387. {
  388. int match = 0;
  389. if (attr->sta_addr) {
  390. if (os_memcmp(attr->sta_addr, entry->spa, ETH_ALEN) != 0)
  391. return 0;
  392. match++;
  393. }
  394. if (attr->acct_multi_session_id) {
  395. char buf[20];
  396. if (attr->acct_multi_session_id_len != 17)
  397. return 0;
  398. os_snprintf(buf, sizeof(buf), "%08X+%08X",
  399. entry->acct_multi_session_id_hi,
  400. entry->acct_multi_session_id_lo);
  401. if (os_memcmp(attr->acct_multi_session_id, buf, 17) != 0)
  402. return 0;
  403. match++;
  404. }
  405. if (attr->cui) {
  406. if (!entry->cui ||
  407. attr->cui_len != wpabuf_len(entry->cui) ||
  408. os_memcmp(attr->cui, wpabuf_head(entry->cui),
  409. attr->cui_len) != 0)
  410. return 0;
  411. match++;
  412. }
  413. if (attr->user_name) {
  414. if (!entry->identity ||
  415. attr->user_name_len != entry->identity_len ||
  416. os_memcmp(attr->user_name, entry->identity,
  417. attr->user_name_len) != 0)
  418. return 0;
  419. match++;
  420. }
  421. return match;
  422. }
  423. int pmksa_cache_auth_radius_das_disconnect(struct rsn_pmksa_cache *pmksa,
  424. struct radius_das_attrs *attr)
  425. {
  426. int found = 0;
  427. struct rsn_pmksa_cache_entry *entry, *prev;
  428. if (attr->acct_session_id)
  429. return -1;
  430. entry = pmksa->pmksa;
  431. while (entry) {
  432. if (das_attr_match(entry, attr)) {
  433. found++;
  434. prev = entry;
  435. entry = entry->next;
  436. pmksa_cache_free_entry(pmksa, prev);
  437. continue;
  438. }
  439. entry = entry->next;
  440. }
  441. return found ? 0 : -1;
  442. }