wpa_auth_ie.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /*
  2. * hostapd - WPA/RSN IE and KDE definitions
  3. * Copyright (c) 2004-2008, 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 "common/ieee802_11_defs.h"
  11. #include "eapol_auth/eapol_auth_sm.h"
  12. #include "ap_config.h"
  13. #include "ieee802_11.h"
  14. #include "wpa_auth.h"
  15. #include "pmksa_cache_auth.h"
  16. #include "wpa_auth_ie.h"
  17. #include "wpa_auth_i.h"
  18. #ifdef CONFIG_RSN_TESTING
  19. int rsn_testing = 0;
  20. #endif /* CONFIG_RSN_TESTING */
  21. static int wpa_write_wpa_ie(struct wpa_auth_config *conf, u8 *buf, size_t len)
  22. {
  23. struct wpa_ie_hdr *hdr;
  24. int num_suites;
  25. u8 *pos, *count;
  26. u32 suite;
  27. hdr = (struct wpa_ie_hdr *) buf;
  28. hdr->elem_id = WLAN_EID_VENDOR_SPECIFIC;
  29. RSN_SELECTOR_PUT(hdr->oui, WPA_OUI_TYPE);
  30. WPA_PUT_LE16(hdr->version, WPA_VERSION);
  31. pos = (u8 *) (hdr + 1);
  32. suite = wpa_cipher_to_suite(WPA_PROTO_WPA, conf->wpa_group);
  33. if (suite == 0) {
  34. wpa_printf(MSG_DEBUG, "Invalid group cipher (%d).",
  35. conf->wpa_group);
  36. return -1;
  37. }
  38. RSN_SELECTOR_PUT(pos, suite);
  39. pos += WPA_SELECTOR_LEN;
  40. count = pos;
  41. pos += 2;
  42. num_suites = wpa_cipher_put_suites(pos, conf->wpa_pairwise);
  43. if (num_suites == 0) {
  44. wpa_printf(MSG_DEBUG, "Invalid pairwise cipher (%d).",
  45. conf->wpa_pairwise);
  46. return -1;
  47. }
  48. pos += num_suites * WPA_SELECTOR_LEN;
  49. WPA_PUT_LE16(count, num_suites);
  50. num_suites = 0;
  51. count = pos;
  52. pos += 2;
  53. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  54. RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
  55. pos += WPA_SELECTOR_LEN;
  56. num_suites++;
  57. }
  58. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
  59. RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
  60. pos += WPA_SELECTOR_LEN;
  61. num_suites++;
  62. }
  63. if (num_suites == 0) {
  64. wpa_printf(MSG_DEBUG, "Invalid key management type (%d).",
  65. conf->wpa_key_mgmt);
  66. return -1;
  67. }
  68. WPA_PUT_LE16(count, num_suites);
  69. /* WPA Capabilities; use defaults, so no need to include it */
  70. hdr->len = (pos - buf) - 2;
  71. return pos - buf;
  72. }
  73. int wpa_write_rsn_ie(struct wpa_auth_config *conf, u8 *buf, size_t len,
  74. const u8 *pmkid)
  75. {
  76. struct rsn_ie_hdr *hdr;
  77. int num_suites, res;
  78. u8 *pos, *count;
  79. u16 capab;
  80. u32 suite;
  81. hdr = (struct rsn_ie_hdr *) buf;
  82. hdr->elem_id = WLAN_EID_RSN;
  83. WPA_PUT_LE16(hdr->version, RSN_VERSION);
  84. pos = (u8 *) (hdr + 1);
  85. suite = wpa_cipher_to_suite(WPA_PROTO_RSN, conf->wpa_group);
  86. if (suite == 0) {
  87. wpa_printf(MSG_DEBUG, "Invalid group cipher (%d).",
  88. conf->wpa_group);
  89. return -1;
  90. }
  91. RSN_SELECTOR_PUT(pos, suite);
  92. pos += RSN_SELECTOR_LEN;
  93. num_suites = 0;
  94. count = pos;
  95. pos += 2;
  96. #ifdef CONFIG_RSN_TESTING
  97. if (rsn_testing) {
  98. RSN_SELECTOR_PUT(pos, RSN_SELECTOR(0x12, 0x34, 0x56, 1));
  99. pos += RSN_SELECTOR_LEN;
  100. num_suites++;
  101. }
  102. #endif /* CONFIG_RSN_TESTING */
  103. res = rsn_cipher_put_suites(pos, conf->rsn_pairwise);
  104. num_suites += res;
  105. pos += res * RSN_SELECTOR_LEN;
  106. #ifdef CONFIG_RSN_TESTING
  107. if (rsn_testing) {
  108. RSN_SELECTOR_PUT(pos, RSN_SELECTOR(0x12, 0x34, 0x56, 2));
  109. pos += RSN_SELECTOR_LEN;
  110. num_suites++;
  111. }
  112. #endif /* CONFIG_RSN_TESTING */
  113. if (num_suites == 0) {
  114. wpa_printf(MSG_DEBUG, "Invalid pairwise cipher (%d).",
  115. conf->rsn_pairwise);
  116. return -1;
  117. }
  118. WPA_PUT_LE16(count, num_suites);
  119. num_suites = 0;
  120. count = pos;
  121. pos += 2;
  122. #ifdef CONFIG_RSN_TESTING
  123. if (rsn_testing) {
  124. RSN_SELECTOR_PUT(pos, RSN_SELECTOR(0x12, 0x34, 0x56, 1));
  125. pos += RSN_SELECTOR_LEN;
  126. num_suites++;
  127. }
  128. #endif /* CONFIG_RSN_TESTING */
  129. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  130. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_UNSPEC_802_1X);
  131. pos += RSN_SELECTOR_LEN;
  132. num_suites++;
  133. }
  134. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
  135. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X);
  136. pos += RSN_SELECTOR_LEN;
  137. num_suites++;
  138. }
  139. #ifdef CONFIG_IEEE80211R
  140. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  141. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
  142. pos += RSN_SELECTOR_LEN;
  143. num_suites++;
  144. }
  145. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  146. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
  147. pos += RSN_SELECTOR_LEN;
  148. num_suites++;
  149. }
  150. #endif /* CONFIG_IEEE80211R */
  151. #ifdef CONFIG_IEEE80211W
  152. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  153. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SHA256);
  154. pos += RSN_SELECTOR_LEN;
  155. num_suites++;
  156. }
  157. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  158. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_SHA256);
  159. pos += RSN_SELECTOR_LEN;
  160. num_suites++;
  161. }
  162. #endif /* CONFIG_IEEE80211W */
  163. #ifdef CONFIG_SAE
  164. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
  165. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_SAE);
  166. pos += RSN_SELECTOR_LEN;
  167. num_suites++;
  168. }
  169. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
  170. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE);
  171. pos += RSN_SELECTOR_LEN;
  172. num_suites++;
  173. }
  174. #endif /* CONFIG_SAE */
  175. #ifdef CONFIG_RSN_TESTING
  176. if (rsn_testing) {
  177. RSN_SELECTOR_PUT(pos, RSN_SELECTOR(0x12, 0x34, 0x56, 2));
  178. pos += RSN_SELECTOR_LEN;
  179. num_suites++;
  180. }
  181. #endif /* CONFIG_RSN_TESTING */
  182. if (num_suites == 0) {
  183. wpa_printf(MSG_DEBUG, "Invalid key management type (%d).",
  184. conf->wpa_key_mgmt);
  185. return -1;
  186. }
  187. WPA_PUT_LE16(count, num_suites);
  188. /* RSN Capabilities */
  189. capab = 0;
  190. if (conf->rsn_preauth)
  191. capab |= WPA_CAPABILITY_PREAUTH;
  192. if (conf->peerkey)
  193. capab |= WPA_CAPABILITY_PEERKEY_ENABLED;
  194. if (conf->wmm_enabled) {
  195. /* 4 PTKSA replay counters when using WMM */
  196. capab |= (RSN_NUM_REPLAY_COUNTERS_16 << 2);
  197. }
  198. #ifdef CONFIG_IEEE80211W
  199. if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
  200. capab |= WPA_CAPABILITY_MFPC;
  201. if (conf->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED)
  202. capab |= WPA_CAPABILITY_MFPR;
  203. }
  204. #endif /* CONFIG_IEEE80211W */
  205. #ifdef CONFIG_RSN_TESTING
  206. if (rsn_testing)
  207. capab |= BIT(8) | BIT(14) | BIT(15);
  208. #endif /* CONFIG_RSN_TESTING */
  209. WPA_PUT_LE16(pos, capab);
  210. pos += 2;
  211. if (pmkid) {
  212. if (pos + 2 + PMKID_LEN > buf + len)
  213. return -1;
  214. /* PMKID Count */
  215. WPA_PUT_LE16(pos, 1);
  216. pos += 2;
  217. os_memcpy(pos, pmkid, PMKID_LEN);
  218. pos += PMKID_LEN;
  219. }
  220. #ifdef CONFIG_IEEE80211W
  221. if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
  222. if (pos + 2 + 4 > buf + len)
  223. return -1;
  224. if (pmkid == NULL) {
  225. /* PMKID Count */
  226. WPA_PUT_LE16(pos, 0);
  227. pos += 2;
  228. }
  229. /* Management Group Cipher Suite */
  230. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
  231. pos += RSN_SELECTOR_LEN;
  232. }
  233. #endif /* CONFIG_IEEE80211W */
  234. #ifdef CONFIG_RSN_TESTING
  235. if (rsn_testing) {
  236. /*
  237. * Fill in any defined fields and add extra data to the end of
  238. * the element.
  239. */
  240. int pmkid_count_set = pmkid != NULL;
  241. if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION)
  242. pmkid_count_set = 1;
  243. /* PMKID Count */
  244. WPA_PUT_LE16(pos, 0);
  245. pos += 2;
  246. if (conf->ieee80211w == NO_MGMT_FRAME_PROTECTION) {
  247. /* Management Group Cipher Suite */
  248. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
  249. pos += RSN_SELECTOR_LEN;
  250. }
  251. os_memset(pos, 0x12, 17);
  252. pos += 17;
  253. }
  254. #endif /* CONFIG_RSN_TESTING */
  255. hdr->len = (pos - buf) - 2;
  256. return pos - buf;
  257. }
  258. int wpa_auth_gen_wpa_ie(struct wpa_authenticator *wpa_auth)
  259. {
  260. u8 *pos, buf[128];
  261. int res;
  262. pos = buf;
  263. if (wpa_auth->conf.wpa & WPA_PROTO_RSN) {
  264. res = wpa_write_rsn_ie(&wpa_auth->conf,
  265. pos, buf + sizeof(buf) - pos, NULL);
  266. if (res < 0)
  267. return res;
  268. pos += res;
  269. }
  270. #ifdef CONFIG_IEEE80211R
  271. if (wpa_key_mgmt_ft(wpa_auth->conf.wpa_key_mgmt)) {
  272. res = wpa_write_mdie(&wpa_auth->conf, pos,
  273. buf + sizeof(buf) - pos);
  274. if (res < 0)
  275. return res;
  276. pos += res;
  277. }
  278. #endif /* CONFIG_IEEE80211R */
  279. if (wpa_auth->conf.wpa & WPA_PROTO_WPA) {
  280. res = wpa_write_wpa_ie(&wpa_auth->conf,
  281. pos, buf + sizeof(buf) - pos);
  282. if (res < 0)
  283. return res;
  284. pos += res;
  285. }
  286. os_free(wpa_auth->wpa_ie);
  287. wpa_auth->wpa_ie = os_malloc(pos - buf);
  288. if (wpa_auth->wpa_ie == NULL)
  289. return -1;
  290. os_memcpy(wpa_auth->wpa_ie, buf, pos - buf);
  291. wpa_auth->wpa_ie_len = pos - buf;
  292. return 0;
  293. }
  294. u8 * wpa_add_kde(u8 *pos, u32 kde, const u8 *data, size_t data_len,
  295. const u8 *data2, size_t data2_len)
  296. {
  297. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  298. *pos++ = RSN_SELECTOR_LEN + data_len + data2_len;
  299. RSN_SELECTOR_PUT(pos, kde);
  300. pos += RSN_SELECTOR_LEN;
  301. os_memcpy(pos, data, data_len);
  302. pos += data_len;
  303. if (data2) {
  304. os_memcpy(pos, data2, data2_len);
  305. pos += data2_len;
  306. }
  307. return pos;
  308. }
  309. struct wpa_auth_okc_iter_data {
  310. struct rsn_pmksa_cache_entry *pmksa;
  311. const u8 *aa;
  312. const u8 *spa;
  313. const u8 *pmkid;
  314. };
  315. static int wpa_auth_okc_iter(struct wpa_authenticator *a, void *ctx)
  316. {
  317. struct wpa_auth_okc_iter_data *data = ctx;
  318. data->pmksa = pmksa_cache_get_okc(a->pmksa, data->aa, data->spa,
  319. data->pmkid);
  320. if (data->pmksa)
  321. return 1;
  322. return 0;
  323. }
  324. int wpa_validate_wpa_ie(struct wpa_authenticator *wpa_auth,
  325. struct wpa_state_machine *sm,
  326. const u8 *wpa_ie, size_t wpa_ie_len,
  327. const u8 *mdie, size_t mdie_len)
  328. {
  329. struct wpa_ie_data data;
  330. int ciphers, key_mgmt, res, version;
  331. u32 selector;
  332. size_t i;
  333. const u8 *pmkid = NULL;
  334. if (wpa_auth == NULL || sm == NULL)
  335. return WPA_NOT_ENABLED;
  336. if (wpa_ie == NULL || wpa_ie_len < 1)
  337. return WPA_INVALID_IE;
  338. if (wpa_ie[0] == WLAN_EID_RSN)
  339. version = WPA_PROTO_RSN;
  340. else
  341. version = WPA_PROTO_WPA;
  342. if (!(wpa_auth->conf.wpa & version)) {
  343. wpa_printf(MSG_DEBUG, "Invalid WPA proto (%d) from " MACSTR,
  344. version, MAC2STR(sm->addr));
  345. return WPA_INVALID_PROTO;
  346. }
  347. if (version == WPA_PROTO_RSN) {
  348. res = wpa_parse_wpa_ie_rsn(wpa_ie, wpa_ie_len, &data);
  349. selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
  350. if (0) {
  351. }
  352. #ifdef CONFIG_IEEE80211R
  353. else if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
  354. selector = RSN_AUTH_KEY_MGMT_FT_802_1X;
  355. else if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK)
  356. selector = RSN_AUTH_KEY_MGMT_FT_PSK;
  357. #endif /* CONFIG_IEEE80211R */
  358. #ifdef CONFIG_IEEE80211W
  359. else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
  360. selector = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
  361. else if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
  362. selector = RSN_AUTH_KEY_MGMT_PSK_SHA256;
  363. #endif /* CONFIG_IEEE80211W */
  364. #ifdef CONFIG_SAE
  365. else if (data.key_mgmt & WPA_KEY_MGMT_SAE)
  366. selector = RSN_AUTH_KEY_MGMT_SAE;
  367. else if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE)
  368. selector = RSN_AUTH_KEY_MGMT_FT_SAE;
  369. #endif /* CONFIG_SAE */
  370. else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  371. selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
  372. else if (data.key_mgmt & WPA_KEY_MGMT_PSK)
  373. selector = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
  374. wpa_auth->dot11RSNAAuthenticationSuiteSelected = selector;
  375. selector = wpa_cipher_to_suite(WPA_PROTO_RSN,
  376. data.pairwise_cipher);
  377. if (!selector)
  378. selector = RSN_CIPHER_SUITE_CCMP;
  379. wpa_auth->dot11RSNAPairwiseCipherSelected = selector;
  380. selector = wpa_cipher_to_suite(WPA_PROTO_RSN,
  381. data.group_cipher);
  382. if (!selector)
  383. selector = RSN_CIPHER_SUITE_CCMP;
  384. wpa_auth->dot11RSNAGroupCipherSelected = selector;
  385. } else {
  386. res = wpa_parse_wpa_ie_wpa(wpa_ie, wpa_ie_len, &data);
  387. selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X;
  388. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  389. selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X;
  390. else if (data.key_mgmt & WPA_KEY_MGMT_PSK)
  391. selector = WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X;
  392. wpa_auth->dot11RSNAAuthenticationSuiteSelected = selector;
  393. selector = wpa_cipher_to_suite(WPA_PROTO_WPA,
  394. data.pairwise_cipher);
  395. if (!selector)
  396. selector = RSN_CIPHER_SUITE_TKIP;
  397. wpa_auth->dot11RSNAPairwiseCipherSelected = selector;
  398. selector = wpa_cipher_to_suite(WPA_PROTO_WPA,
  399. data.group_cipher);
  400. if (!selector)
  401. selector = WPA_CIPHER_SUITE_TKIP;
  402. wpa_auth->dot11RSNAGroupCipherSelected = selector;
  403. }
  404. if (res) {
  405. wpa_printf(MSG_DEBUG, "Failed to parse WPA/RSN IE from "
  406. MACSTR " (res=%d)", MAC2STR(sm->addr), res);
  407. wpa_hexdump(MSG_DEBUG, "WPA/RSN IE", wpa_ie, wpa_ie_len);
  408. return WPA_INVALID_IE;
  409. }
  410. if (data.group_cipher != wpa_auth->conf.wpa_group) {
  411. wpa_printf(MSG_DEBUG, "Invalid WPA group cipher (0x%x) from "
  412. MACSTR, data.group_cipher, MAC2STR(sm->addr));
  413. return WPA_INVALID_GROUP;
  414. }
  415. key_mgmt = data.key_mgmt & wpa_auth->conf.wpa_key_mgmt;
  416. if (!key_mgmt) {
  417. wpa_printf(MSG_DEBUG, "Invalid WPA key mgmt (0x%x) from "
  418. MACSTR, data.key_mgmt, MAC2STR(sm->addr));
  419. return WPA_INVALID_AKMP;
  420. }
  421. if (0) {
  422. }
  423. #ifdef CONFIG_IEEE80211R
  424. else if (key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
  425. sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
  426. else if (key_mgmt & WPA_KEY_MGMT_FT_PSK)
  427. sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_PSK;
  428. #endif /* CONFIG_IEEE80211R */
  429. #ifdef CONFIG_IEEE80211W
  430. else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
  431. sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
  432. else if (key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
  433. sm->wpa_key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
  434. #endif /* CONFIG_IEEE80211W */
  435. #ifdef CONFIG_SAE
  436. else if (key_mgmt & WPA_KEY_MGMT_SAE)
  437. sm->wpa_key_mgmt = WPA_KEY_MGMT_SAE;
  438. else if (key_mgmt & WPA_KEY_MGMT_FT_SAE)
  439. sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_SAE;
  440. #endif /* CONFIG_SAE */
  441. else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  442. sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  443. else
  444. sm->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
  445. if (version == WPA_PROTO_RSN)
  446. ciphers = data.pairwise_cipher & wpa_auth->conf.rsn_pairwise;
  447. else
  448. ciphers = data.pairwise_cipher & wpa_auth->conf.wpa_pairwise;
  449. if (!ciphers) {
  450. wpa_printf(MSG_DEBUG, "Invalid %s pairwise cipher (0x%x) "
  451. "from " MACSTR,
  452. version == WPA_PROTO_RSN ? "RSN" : "WPA",
  453. data.pairwise_cipher, MAC2STR(sm->addr));
  454. return WPA_INVALID_PAIRWISE;
  455. }
  456. #ifdef CONFIG_IEEE80211W
  457. if (wpa_auth->conf.ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
  458. if (!(data.capabilities & WPA_CAPABILITY_MFPC)) {
  459. wpa_printf(MSG_DEBUG, "Management frame protection "
  460. "required, but client did not enable it");
  461. return WPA_MGMT_FRAME_PROTECTION_VIOLATION;
  462. }
  463. if (ciphers & WPA_CIPHER_TKIP) {
  464. wpa_printf(MSG_DEBUG, "Management frame protection "
  465. "cannot use TKIP");
  466. return WPA_MGMT_FRAME_PROTECTION_VIOLATION;
  467. }
  468. if (data.mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC) {
  469. wpa_printf(MSG_DEBUG, "Unsupported management group "
  470. "cipher %d", data.mgmt_group_cipher);
  471. return WPA_INVALID_MGMT_GROUP_CIPHER;
  472. }
  473. }
  474. if (wpa_auth->conf.ieee80211w == NO_MGMT_FRAME_PROTECTION ||
  475. !(data.capabilities & WPA_CAPABILITY_MFPC))
  476. sm->mgmt_frame_prot = 0;
  477. else
  478. sm->mgmt_frame_prot = 1;
  479. #endif /* CONFIG_IEEE80211W */
  480. #ifdef CONFIG_IEEE80211R
  481. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
  482. if (mdie == NULL || mdie_len < MOBILITY_DOMAIN_ID_LEN + 1) {
  483. wpa_printf(MSG_DEBUG, "RSN: Trying to use FT, but "
  484. "MDIE not included");
  485. return WPA_INVALID_MDIE;
  486. }
  487. if (os_memcmp(mdie, wpa_auth->conf.mobility_domain,
  488. MOBILITY_DOMAIN_ID_LEN) != 0) {
  489. wpa_hexdump(MSG_DEBUG, "RSN: Attempted to use unknown "
  490. "MDIE", mdie, MOBILITY_DOMAIN_ID_LEN);
  491. return WPA_INVALID_MDIE;
  492. }
  493. }
  494. #endif /* CONFIG_IEEE80211R */
  495. sm->pairwise = wpa_pick_pairwise_cipher(ciphers, 0);
  496. if (sm->pairwise < 0)
  497. return WPA_INVALID_PAIRWISE;
  498. /* TODO: clear WPA/WPA2 state if STA changes from one to another */
  499. if (wpa_ie[0] == WLAN_EID_RSN)
  500. sm->wpa = WPA_VERSION_WPA2;
  501. else
  502. sm->wpa = WPA_VERSION_WPA;
  503. sm->pmksa = NULL;
  504. for (i = 0; i < data.num_pmkid; i++) {
  505. wpa_hexdump(MSG_DEBUG, "RSN IE: STA PMKID",
  506. &data.pmkid[i * PMKID_LEN], PMKID_LEN);
  507. sm->pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sm->addr,
  508. &data.pmkid[i * PMKID_LEN]);
  509. if (sm->pmksa) {
  510. pmkid = sm->pmksa->pmkid;
  511. break;
  512. }
  513. }
  514. for (i = 0; sm->pmksa == NULL && wpa_auth->conf.okc &&
  515. i < data.num_pmkid; i++) {
  516. struct wpa_auth_okc_iter_data idata;
  517. idata.pmksa = NULL;
  518. idata.aa = wpa_auth->addr;
  519. idata.spa = sm->addr;
  520. idata.pmkid = &data.pmkid[i * PMKID_LEN];
  521. wpa_auth_for_each_auth(wpa_auth, wpa_auth_okc_iter, &idata);
  522. if (idata.pmksa) {
  523. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  524. "OKC match for PMKID");
  525. sm->pmksa = pmksa_cache_add_okc(wpa_auth->pmksa,
  526. idata.pmksa,
  527. wpa_auth->addr,
  528. idata.pmkid);
  529. pmkid = idata.pmkid;
  530. break;
  531. }
  532. }
  533. if (sm->pmksa) {
  534. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  535. "PMKID found from PMKSA cache "
  536. "eap_type=%d vlan_id=%d",
  537. sm->pmksa->eap_type_authsrv,
  538. sm->pmksa->vlan_id);
  539. os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmkid, PMKID_LEN);
  540. }
  541. if (sm->wpa_ie == NULL || sm->wpa_ie_len < wpa_ie_len) {
  542. os_free(sm->wpa_ie);
  543. sm->wpa_ie = os_malloc(wpa_ie_len);
  544. if (sm->wpa_ie == NULL)
  545. return WPA_ALLOC_FAIL;
  546. }
  547. os_memcpy(sm->wpa_ie, wpa_ie, wpa_ie_len);
  548. sm->wpa_ie_len = wpa_ie_len;
  549. return WPA_IE_OK;
  550. }
  551. /**
  552. * wpa_parse_generic - Parse EAPOL-Key Key Data Generic IEs
  553. * @pos: Pointer to the IE header
  554. * @end: Pointer to the end of the Key Data buffer
  555. * @ie: Pointer to parsed IE data
  556. * Returns: 0 on success, 1 if end mark is found, -1 on failure
  557. */
  558. static int wpa_parse_generic(const u8 *pos, const u8 *end,
  559. struct wpa_eapol_ie_parse *ie)
  560. {
  561. if (pos[1] == 0)
  562. return 1;
  563. if (pos[1] >= 6 &&
  564. RSN_SELECTOR_GET(pos + 2) == WPA_OUI_TYPE &&
  565. pos[2 + WPA_SELECTOR_LEN] == 1 &&
  566. pos[2 + WPA_SELECTOR_LEN + 1] == 0) {
  567. ie->wpa_ie = pos;
  568. ie->wpa_ie_len = pos[1] + 2;
  569. return 0;
  570. }
  571. if (pos + 1 + RSN_SELECTOR_LEN < end &&
  572. pos[1] >= RSN_SELECTOR_LEN + PMKID_LEN &&
  573. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_PMKID) {
  574. ie->pmkid = pos + 2 + RSN_SELECTOR_LEN;
  575. return 0;
  576. }
  577. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  578. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_GROUPKEY) {
  579. ie->gtk = pos + 2 + RSN_SELECTOR_LEN;
  580. ie->gtk_len = pos[1] - RSN_SELECTOR_LEN;
  581. return 0;
  582. }
  583. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  584. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_MAC_ADDR) {
  585. ie->mac_addr = pos + 2 + RSN_SELECTOR_LEN;
  586. ie->mac_addr_len = pos[1] - RSN_SELECTOR_LEN;
  587. return 0;
  588. }
  589. #ifdef CONFIG_PEERKEY
  590. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  591. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_SMK) {
  592. ie->smk = pos + 2 + RSN_SELECTOR_LEN;
  593. ie->smk_len = pos[1] - RSN_SELECTOR_LEN;
  594. return 0;
  595. }
  596. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  597. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_NONCE) {
  598. ie->nonce = pos + 2 + RSN_SELECTOR_LEN;
  599. ie->nonce_len = pos[1] - RSN_SELECTOR_LEN;
  600. return 0;
  601. }
  602. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  603. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_LIFETIME) {
  604. ie->lifetime = pos + 2 + RSN_SELECTOR_LEN;
  605. ie->lifetime_len = pos[1] - RSN_SELECTOR_LEN;
  606. return 0;
  607. }
  608. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  609. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_ERROR) {
  610. ie->error = pos + 2 + RSN_SELECTOR_LEN;
  611. ie->error_len = pos[1] - RSN_SELECTOR_LEN;
  612. return 0;
  613. }
  614. #endif /* CONFIG_PEERKEY */
  615. #ifdef CONFIG_IEEE80211W
  616. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  617. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_IGTK) {
  618. ie->igtk = pos + 2 + RSN_SELECTOR_LEN;
  619. ie->igtk_len = pos[1] - RSN_SELECTOR_LEN;
  620. return 0;
  621. }
  622. #endif /* CONFIG_IEEE80211W */
  623. return 0;
  624. }
  625. /**
  626. * wpa_parse_kde_ies - Parse EAPOL-Key Key Data IEs
  627. * @buf: Pointer to the Key Data buffer
  628. * @len: Key Data Length
  629. * @ie: Pointer to parsed IE data
  630. * Returns: 0 on success, -1 on failure
  631. */
  632. int wpa_parse_kde_ies(const u8 *buf, size_t len, struct wpa_eapol_ie_parse *ie)
  633. {
  634. const u8 *pos, *end;
  635. int ret = 0;
  636. os_memset(ie, 0, sizeof(*ie));
  637. for (pos = buf, end = pos + len; pos + 1 < end; pos += 2 + pos[1]) {
  638. if (pos[0] == 0xdd &&
  639. ((pos == buf + len - 1) || pos[1] == 0)) {
  640. /* Ignore padding */
  641. break;
  642. }
  643. if (pos + 2 + pos[1] > end) {
  644. wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key Key Data "
  645. "underflow (ie=%d len=%d pos=%d)",
  646. pos[0], pos[1], (int) (pos - buf));
  647. wpa_hexdump_key(MSG_DEBUG, "WPA: Key Data",
  648. buf, len);
  649. ret = -1;
  650. break;
  651. }
  652. if (*pos == WLAN_EID_RSN) {
  653. ie->rsn_ie = pos;
  654. ie->rsn_ie_len = pos[1] + 2;
  655. #ifdef CONFIG_IEEE80211R
  656. } else if (*pos == WLAN_EID_MOBILITY_DOMAIN) {
  657. ie->mdie = pos;
  658. ie->mdie_len = pos[1] + 2;
  659. } else if (*pos == WLAN_EID_FAST_BSS_TRANSITION) {
  660. ie->ftie = pos;
  661. ie->ftie_len = pos[1] + 2;
  662. #endif /* CONFIG_IEEE80211R */
  663. } else if (*pos == WLAN_EID_VENDOR_SPECIFIC) {
  664. ret = wpa_parse_generic(pos, end, ie);
  665. if (ret < 0)
  666. break;
  667. if (ret > 0) {
  668. ret = 0;
  669. break;
  670. }
  671. } else {
  672. wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized EAPOL-Key "
  673. "Key Data IE", pos, 2 + pos[1]);
  674. }
  675. }
  676. return ret;
  677. }
  678. int wpa_auth_uses_mfp(struct wpa_state_machine *sm)
  679. {
  680. return sm ? sm->mgmt_frame_prot : 0;
  681. }