wpa_auth_ie.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /*
  2. * hostapd - WPA/RSN IE and KDE definitions
  3. * Copyright (c) 2004-2015, 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. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
  176. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SUITE_B);
  177. pos += RSN_SELECTOR_LEN;
  178. num_suites++;
  179. }
  180. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
  181. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192);
  182. pos += RSN_SELECTOR_LEN;
  183. num_suites++;
  184. }
  185. #ifdef CONFIG_RSN_TESTING
  186. if (rsn_testing) {
  187. RSN_SELECTOR_PUT(pos, RSN_SELECTOR(0x12, 0x34, 0x56, 2));
  188. pos += RSN_SELECTOR_LEN;
  189. num_suites++;
  190. }
  191. #endif /* CONFIG_RSN_TESTING */
  192. if (num_suites == 0) {
  193. wpa_printf(MSG_DEBUG, "Invalid key management type (%d).",
  194. conf->wpa_key_mgmt);
  195. return -1;
  196. }
  197. WPA_PUT_LE16(count, num_suites);
  198. /* RSN Capabilities */
  199. capab = 0;
  200. if (conf->rsn_preauth)
  201. capab |= WPA_CAPABILITY_PREAUTH;
  202. if (conf->peerkey)
  203. capab |= WPA_CAPABILITY_PEERKEY_ENABLED;
  204. if (conf->wmm_enabled) {
  205. /* 4 PTKSA replay counters when using WMM */
  206. capab |= (RSN_NUM_REPLAY_COUNTERS_16 << 2);
  207. }
  208. #ifdef CONFIG_IEEE80211W
  209. if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
  210. capab |= WPA_CAPABILITY_MFPC;
  211. if (conf->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED)
  212. capab |= WPA_CAPABILITY_MFPR;
  213. }
  214. #endif /* CONFIG_IEEE80211W */
  215. #ifdef CONFIG_RSN_TESTING
  216. if (rsn_testing)
  217. capab |= BIT(8) | BIT(14) | BIT(15);
  218. #endif /* CONFIG_RSN_TESTING */
  219. WPA_PUT_LE16(pos, capab);
  220. pos += 2;
  221. if (pmkid) {
  222. if (2 + PMKID_LEN > buf + len - pos)
  223. return -1;
  224. /* PMKID Count */
  225. WPA_PUT_LE16(pos, 1);
  226. pos += 2;
  227. os_memcpy(pos, pmkid, PMKID_LEN);
  228. pos += PMKID_LEN;
  229. }
  230. #ifdef CONFIG_IEEE80211W
  231. if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION &&
  232. conf->group_mgmt_cipher != WPA_CIPHER_AES_128_CMAC) {
  233. if (2 + 4 > buf + len - pos)
  234. return -1;
  235. if (pmkid == NULL) {
  236. /* PMKID Count */
  237. WPA_PUT_LE16(pos, 0);
  238. pos += 2;
  239. }
  240. /* Management Group Cipher Suite */
  241. switch (conf->group_mgmt_cipher) {
  242. case WPA_CIPHER_AES_128_CMAC:
  243. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
  244. break;
  245. case WPA_CIPHER_BIP_GMAC_128:
  246. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_GMAC_128);
  247. break;
  248. case WPA_CIPHER_BIP_GMAC_256:
  249. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_GMAC_256);
  250. break;
  251. case WPA_CIPHER_BIP_CMAC_256:
  252. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_CMAC_256);
  253. break;
  254. default:
  255. wpa_printf(MSG_DEBUG,
  256. "Invalid group management cipher (0x%x)",
  257. conf->group_mgmt_cipher);
  258. return -1;
  259. }
  260. pos += RSN_SELECTOR_LEN;
  261. }
  262. #endif /* CONFIG_IEEE80211W */
  263. #ifdef CONFIG_RSN_TESTING
  264. if (rsn_testing) {
  265. /*
  266. * Fill in any defined fields and add extra data to the end of
  267. * the element.
  268. */
  269. int pmkid_count_set = pmkid != NULL;
  270. if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION)
  271. pmkid_count_set = 1;
  272. /* PMKID Count */
  273. WPA_PUT_LE16(pos, 0);
  274. pos += 2;
  275. if (conf->ieee80211w == NO_MGMT_FRAME_PROTECTION) {
  276. /* Management Group Cipher Suite */
  277. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
  278. pos += RSN_SELECTOR_LEN;
  279. }
  280. os_memset(pos, 0x12, 17);
  281. pos += 17;
  282. }
  283. #endif /* CONFIG_RSN_TESTING */
  284. hdr->len = (pos - buf) - 2;
  285. return pos - buf;
  286. }
  287. static u8 * wpa_write_osen(struct wpa_auth_config *conf, u8 *eid)
  288. {
  289. u8 *len;
  290. u16 capab;
  291. *eid++ = WLAN_EID_VENDOR_SPECIFIC;
  292. len = eid++; /* to be filled */
  293. WPA_PUT_BE24(eid, OUI_WFA);
  294. eid += 3;
  295. *eid++ = HS20_OSEN_OUI_TYPE;
  296. /* Group Data Cipher Suite */
  297. RSN_SELECTOR_PUT(eid, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
  298. eid += RSN_SELECTOR_LEN;
  299. /* Pairwise Cipher Suite Count and List */
  300. WPA_PUT_LE16(eid, 1);
  301. eid += 2;
  302. RSN_SELECTOR_PUT(eid, RSN_CIPHER_SUITE_CCMP);
  303. eid += RSN_SELECTOR_LEN;
  304. /* AKM Suite Count and List */
  305. WPA_PUT_LE16(eid, 1);
  306. eid += 2;
  307. RSN_SELECTOR_PUT(eid, RSN_AUTH_KEY_MGMT_OSEN);
  308. eid += RSN_SELECTOR_LEN;
  309. /* RSN Capabilities */
  310. capab = 0;
  311. if (conf->wmm_enabled) {
  312. /* 4 PTKSA replay counters when using WMM */
  313. capab |= (RSN_NUM_REPLAY_COUNTERS_16 << 2);
  314. }
  315. #ifdef CONFIG_IEEE80211W
  316. if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
  317. capab |= WPA_CAPABILITY_MFPC;
  318. if (conf->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED)
  319. capab |= WPA_CAPABILITY_MFPR;
  320. }
  321. #endif /* CONFIG_IEEE80211W */
  322. WPA_PUT_LE16(eid, capab);
  323. eid += 2;
  324. *len = eid - len - 1;
  325. return eid;
  326. }
  327. int wpa_auth_gen_wpa_ie(struct wpa_authenticator *wpa_auth)
  328. {
  329. u8 *pos, buf[128];
  330. int res;
  331. #ifdef CONFIG_TESTING_OPTIONS
  332. if (wpa_auth->conf.own_ie_override_len) {
  333. wpa_hexdump(MSG_DEBUG, "WPA: Forced own IE(s) for testing",
  334. wpa_auth->conf.own_ie_override,
  335. wpa_auth->conf.own_ie_override_len);
  336. os_free(wpa_auth->wpa_ie);
  337. wpa_auth->wpa_ie =
  338. os_malloc(wpa_auth->conf.own_ie_override_len);
  339. if (wpa_auth->wpa_ie == NULL)
  340. return -1;
  341. os_memcpy(wpa_auth->wpa_ie, wpa_auth->conf.own_ie_override,
  342. wpa_auth->conf.own_ie_override_len);
  343. wpa_auth->wpa_ie_len = wpa_auth->conf.own_ie_override_len;
  344. return 0;
  345. }
  346. #endif /* CONFIG_TESTING_OPTIONS */
  347. pos = buf;
  348. if (wpa_auth->conf.wpa == WPA_PROTO_OSEN) {
  349. pos = wpa_write_osen(&wpa_auth->conf, pos);
  350. }
  351. if (wpa_auth->conf.wpa & WPA_PROTO_RSN) {
  352. res = wpa_write_rsn_ie(&wpa_auth->conf,
  353. pos, buf + sizeof(buf) - pos, NULL);
  354. if (res < 0)
  355. return res;
  356. pos += res;
  357. }
  358. #ifdef CONFIG_IEEE80211R
  359. if (wpa_key_mgmt_ft(wpa_auth->conf.wpa_key_mgmt)) {
  360. res = wpa_write_mdie(&wpa_auth->conf, pos,
  361. buf + sizeof(buf) - pos);
  362. if (res < 0)
  363. return res;
  364. pos += res;
  365. }
  366. #endif /* CONFIG_IEEE80211R */
  367. if (wpa_auth->conf.wpa & WPA_PROTO_WPA) {
  368. res = wpa_write_wpa_ie(&wpa_auth->conf,
  369. pos, buf + sizeof(buf) - pos);
  370. if (res < 0)
  371. return res;
  372. pos += res;
  373. }
  374. os_free(wpa_auth->wpa_ie);
  375. wpa_auth->wpa_ie = os_malloc(pos - buf);
  376. if (wpa_auth->wpa_ie == NULL)
  377. return -1;
  378. os_memcpy(wpa_auth->wpa_ie, buf, pos - buf);
  379. wpa_auth->wpa_ie_len = pos - buf;
  380. return 0;
  381. }
  382. u8 * wpa_add_kde(u8 *pos, u32 kde, const u8 *data, size_t data_len,
  383. const u8 *data2, size_t data2_len)
  384. {
  385. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  386. *pos++ = RSN_SELECTOR_LEN + data_len + data2_len;
  387. RSN_SELECTOR_PUT(pos, kde);
  388. pos += RSN_SELECTOR_LEN;
  389. os_memcpy(pos, data, data_len);
  390. pos += data_len;
  391. if (data2) {
  392. os_memcpy(pos, data2, data2_len);
  393. pos += data2_len;
  394. }
  395. return pos;
  396. }
  397. struct wpa_auth_okc_iter_data {
  398. struct rsn_pmksa_cache_entry *pmksa;
  399. const u8 *aa;
  400. const u8 *spa;
  401. const u8 *pmkid;
  402. };
  403. static int wpa_auth_okc_iter(struct wpa_authenticator *a, void *ctx)
  404. {
  405. struct wpa_auth_okc_iter_data *data = ctx;
  406. data->pmksa = pmksa_cache_get_okc(a->pmksa, data->aa, data->spa,
  407. data->pmkid);
  408. if (data->pmksa)
  409. return 1;
  410. return 0;
  411. }
  412. int wpa_validate_wpa_ie(struct wpa_authenticator *wpa_auth,
  413. struct wpa_state_machine *sm,
  414. const u8 *wpa_ie, size_t wpa_ie_len,
  415. const u8 *mdie, size_t mdie_len)
  416. {
  417. struct wpa_ie_data data;
  418. int ciphers, key_mgmt, res, version;
  419. u32 selector;
  420. size_t i;
  421. const u8 *pmkid = NULL;
  422. if (wpa_auth == NULL || sm == NULL)
  423. return WPA_NOT_ENABLED;
  424. if (wpa_ie == NULL || wpa_ie_len < 1)
  425. return WPA_INVALID_IE;
  426. if (wpa_ie[0] == WLAN_EID_RSN)
  427. version = WPA_PROTO_RSN;
  428. else
  429. version = WPA_PROTO_WPA;
  430. if (!(wpa_auth->conf.wpa & version)) {
  431. wpa_printf(MSG_DEBUG, "Invalid WPA proto (%d) from " MACSTR,
  432. version, MAC2STR(sm->addr));
  433. return WPA_INVALID_PROTO;
  434. }
  435. if (version == WPA_PROTO_RSN) {
  436. res = wpa_parse_wpa_ie_rsn(wpa_ie, wpa_ie_len, &data);
  437. selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
  438. if (0) {
  439. }
  440. else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
  441. selector = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
  442. else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B)
  443. selector = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
  444. #ifdef CONFIG_IEEE80211R
  445. else if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
  446. selector = RSN_AUTH_KEY_MGMT_FT_802_1X;
  447. else if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK)
  448. selector = RSN_AUTH_KEY_MGMT_FT_PSK;
  449. #endif /* CONFIG_IEEE80211R */
  450. #ifdef CONFIG_IEEE80211W
  451. else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
  452. selector = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
  453. else if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
  454. selector = RSN_AUTH_KEY_MGMT_PSK_SHA256;
  455. #endif /* CONFIG_IEEE80211W */
  456. #ifdef CONFIG_SAE
  457. else if (data.key_mgmt & WPA_KEY_MGMT_SAE)
  458. selector = RSN_AUTH_KEY_MGMT_SAE;
  459. else if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE)
  460. selector = RSN_AUTH_KEY_MGMT_FT_SAE;
  461. #endif /* CONFIG_SAE */
  462. else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  463. selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
  464. else if (data.key_mgmt & WPA_KEY_MGMT_PSK)
  465. selector = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
  466. wpa_auth->dot11RSNAAuthenticationSuiteSelected = selector;
  467. selector = wpa_cipher_to_suite(WPA_PROTO_RSN,
  468. data.pairwise_cipher);
  469. if (!selector)
  470. selector = RSN_CIPHER_SUITE_CCMP;
  471. wpa_auth->dot11RSNAPairwiseCipherSelected = selector;
  472. selector = wpa_cipher_to_suite(WPA_PROTO_RSN,
  473. data.group_cipher);
  474. if (!selector)
  475. selector = RSN_CIPHER_SUITE_CCMP;
  476. wpa_auth->dot11RSNAGroupCipherSelected = selector;
  477. } else {
  478. res = wpa_parse_wpa_ie_wpa(wpa_ie, wpa_ie_len, &data);
  479. selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X;
  480. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  481. selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X;
  482. else if (data.key_mgmt & WPA_KEY_MGMT_PSK)
  483. selector = WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X;
  484. wpa_auth->dot11RSNAAuthenticationSuiteSelected = selector;
  485. selector = wpa_cipher_to_suite(WPA_PROTO_WPA,
  486. data.pairwise_cipher);
  487. if (!selector)
  488. selector = RSN_CIPHER_SUITE_TKIP;
  489. wpa_auth->dot11RSNAPairwiseCipherSelected = selector;
  490. selector = wpa_cipher_to_suite(WPA_PROTO_WPA,
  491. data.group_cipher);
  492. if (!selector)
  493. selector = WPA_CIPHER_SUITE_TKIP;
  494. wpa_auth->dot11RSNAGroupCipherSelected = selector;
  495. }
  496. if (res) {
  497. wpa_printf(MSG_DEBUG, "Failed to parse WPA/RSN IE from "
  498. MACSTR " (res=%d)", MAC2STR(sm->addr), res);
  499. wpa_hexdump(MSG_DEBUG, "WPA/RSN IE", wpa_ie, wpa_ie_len);
  500. return WPA_INVALID_IE;
  501. }
  502. if (data.group_cipher != wpa_auth->conf.wpa_group) {
  503. wpa_printf(MSG_DEBUG, "Invalid WPA group cipher (0x%x) from "
  504. MACSTR, data.group_cipher, MAC2STR(sm->addr));
  505. return WPA_INVALID_GROUP;
  506. }
  507. key_mgmt = data.key_mgmt & wpa_auth->conf.wpa_key_mgmt;
  508. if (!key_mgmt) {
  509. wpa_printf(MSG_DEBUG, "Invalid WPA key mgmt (0x%x) from "
  510. MACSTR, data.key_mgmt, MAC2STR(sm->addr));
  511. return WPA_INVALID_AKMP;
  512. }
  513. if (0) {
  514. }
  515. else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
  516. sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
  517. else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B)
  518. sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B;
  519. #ifdef CONFIG_IEEE80211R
  520. else if (key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
  521. sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
  522. else if (key_mgmt & WPA_KEY_MGMT_FT_PSK)
  523. sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_PSK;
  524. #endif /* CONFIG_IEEE80211R */
  525. #ifdef CONFIG_IEEE80211W
  526. else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
  527. sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
  528. else if (key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
  529. sm->wpa_key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
  530. #endif /* CONFIG_IEEE80211W */
  531. #ifdef CONFIG_SAE
  532. else if (key_mgmt & WPA_KEY_MGMT_SAE)
  533. sm->wpa_key_mgmt = WPA_KEY_MGMT_SAE;
  534. else if (key_mgmt & WPA_KEY_MGMT_FT_SAE)
  535. sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_SAE;
  536. #endif /* CONFIG_SAE */
  537. else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  538. sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  539. else
  540. sm->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
  541. if (version == WPA_PROTO_RSN)
  542. ciphers = data.pairwise_cipher & wpa_auth->conf.rsn_pairwise;
  543. else
  544. ciphers = data.pairwise_cipher & wpa_auth->conf.wpa_pairwise;
  545. if (!ciphers) {
  546. wpa_printf(MSG_DEBUG, "Invalid %s pairwise cipher (0x%x) "
  547. "from " MACSTR,
  548. version == WPA_PROTO_RSN ? "RSN" : "WPA",
  549. data.pairwise_cipher, MAC2STR(sm->addr));
  550. return WPA_INVALID_PAIRWISE;
  551. }
  552. #ifdef CONFIG_IEEE80211W
  553. if (wpa_auth->conf.ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
  554. if (!(data.capabilities & WPA_CAPABILITY_MFPC)) {
  555. wpa_printf(MSG_DEBUG, "Management frame protection "
  556. "required, but client did not enable it");
  557. return WPA_MGMT_FRAME_PROTECTION_VIOLATION;
  558. }
  559. if (ciphers & WPA_CIPHER_TKIP) {
  560. wpa_printf(MSG_DEBUG, "Management frame protection "
  561. "cannot use TKIP");
  562. return WPA_MGMT_FRAME_PROTECTION_VIOLATION;
  563. }
  564. if (data.mgmt_group_cipher != wpa_auth->conf.group_mgmt_cipher)
  565. {
  566. wpa_printf(MSG_DEBUG, "Unsupported management group "
  567. "cipher %d", data.mgmt_group_cipher);
  568. return WPA_INVALID_MGMT_GROUP_CIPHER;
  569. }
  570. }
  571. if (wpa_auth->conf.ieee80211w == NO_MGMT_FRAME_PROTECTION ||
  572. !(data.capabilities & WPA_CAPABILITY_MFPC))
  573. sm->mgmt_frame_prot = 0;
  574. else
  575. sm->mgmt_frame_prot = 1;
  576. #endif /* CONFIG_IEEE80211W */
  577. #ifdef CONFIG_IEEE80211R
  578. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
  579. if (mdie == NULL || mdie_len < MOBILITY_DOMAIN_ID_LEN + 1) {
  580. wpa_printf(MSG_DEBUG, "RSN: Trying to use FT, but "
  581. "MDIE not included");
  582. return WPA_INVALID_MDIE;
  583. }
  584. if (os_memcmp(mdie, wpa_auth->conf.mobility_domain,
  585. MOBILITY_DOMAIN_ID_LEN) != 0) {
  586. wpa_hexdump(MSG_DEBUG, "RSN: Attempted to use unknown "
  587. "MDIE", mdie, MOBILITY_DOMAIN_ID_LEN);
  588. return WPA_INVALID_MDIE;
  589. }
  590. }
  591. #endif /* CONFIG_IEEE80211R */
  592. sm->pairwise = wpa_pick_pairwise_cipher(ciphers, 0);
  593. if (sm->pairwise < 0)
  594. return WPA_INVALID_PAIRWISE;
  595. /* TODO: clear WPA/WPA2 state if STA changes from one to another */
  596. if (wpa_ie[0] == WLAN_EID_RSN)
  597. sm->wpa = WPA_VERSION_WPA2;
  598. else
  599. sm->wpa = WPA_VERSION_WPA;
  600. sm->pmksa = NULL;
  601. for (i = 0; i < data.num_pmkid; i++) {
  602. wpa_hexdump(MSG_DEBUG, "RSN IE: STA PMKID",
  603. &data.pmkid[i * PMKID_LEN], PMKID_LEN);
  604. sm->pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sm->addr,
  605. &data.pmkid[i * PMKID_LEN]);
  606. if (sm->pmksa) {
  607. pmkid = sm->pmksa->pmkid;
  608. break;
  609. }
  610. }
  611. for (i = 0; sm->pmksa == NULL && wpa_auth->conf.okc &&
  612. i < data.num_pmkid; i++) {
  613. struct wpa_auth_okc_iter_data idata;
  614. idata.pmksa = NULL;
  615. idata.aa = wpa_auth->addr;
  616. idata.spa = sm->addr;
  617. idata.pmkid = &data.pmkid[i * PMKID_LEN];
  618. wpa_auth_for_each_auth(wpa_auth, wpa_auth_okc_iter, &idata);
  619. if (idata.pmksa) {
  620. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  621. "OKC match for PMKID");
  622. sm->pmksa = pmksa_cache_add_okc(wpa_auth->pmksa,
  623. idata.pmksa,
  624. wpa_auth->addr,
  625. idata.pmkid);
  626. pmkid = idata.pmkid;
  627. break;
  628. }
  629. }
  630. if (sm->pmksa && pmkid) {
  631. struct vlan_description *vlan;
  632. vlan = sm->pmksa->vlan_desc;
  633. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  634. "PMKID found from PMKSA cache eap_type=%d vlan=%d%s",
  635. sm->pmksa->eap_type_authsrv,
  636. vlan ? vlan->untagged : 0,
  637. (vlan && vlan->tagged[0]) ? "+" : "");
  638. os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmkid, PMKID_LEN);
  639. }
  640. if (sm->wpa_ie == NULL || sm->wpa_ie_len < wpa_ie_len) {
  641. os_free(sm->wpa_ie);
  642. sm->wpa_ie = os_malloc(wpa_ie_len);
  643. if (sm->wpa_ie == NULL)
  644. return WPA_ALLOC_FAIL;
  645. }
  646. os_memcpy(sm->wpa_ie, wpa_ie, wpa_ie_len);
  647. sm->wpa_ie_len = wpa_ie_len;
  648. return WPA_IE_OK;
  649. }
  650. #ifdef CONFIG_HS20
  651. int wpa_validate_osen(struct wpa_authenticator *wpa_auth,
  652. struct wpa_state_machine *sm,
  653. const u8 *osen_ie, size_t osen_ie_len)
  654. {
  655. if (wpa_auth == NULL || sm == NULL)
  656. return -1;
  657. /* TODO: parse OSEN element */
  658. sm->wpa_key_mgmt = WPA_KEY_MGMT_OSEN;
  659. sm->mgmt_frame_prot = 1;
  660. sm->pairwise = WPA_CIPHER_CCMP;
  661. sm->wpa = WPA_VERSION_WPA2;
  662. if (sm->wpa_ie == NULL || sm->wpa_ie_len < osen_ie_len) {
  663. os_free(sm->wpa_ie);
  664. sm->wpa_ie = os_malloc(osen_ie_len);
  665. if (sm->wpa_ie == NULL)
  666. return -1;
  667. }
  668. os_memcpy(sm->wpa_ie, osen_ie, osen_ie_len);
  669. sm->wpa_ie_len = osen_ie_len;
  670. return 0;
  671. }
  672. #endif /* CONFIG_HS20 */
  673. /**
  674. * wpa_parse_generic - Parse EAPOL-Key Key Data Generic IEs
  675. * @pos: Pointer to the IE header
  676. * @end: Pointer to the end of the Key Data buffer
  677. * @ie: Pointer to parsed IE data
  678. * Returns: 0 on success, 1 if end mark is found, -1 on failure
  679. */
  680. static int wpa_parse_generic(const u8 *pos, const u8 *end,
  681. struct wpa_eapol_ie_parse *ie)
  682. {
  683. if (pos[1] == 0)
  684. return 1;
  685. if (pos[1] >= 6 &&
  686. RSN_SELECTOR_GET(pos + 2) == WPA_OUI_TYPE &&
  687. pos[2 + WPA_SELECTOR_LEN] == 1 &&
  688. pos[2 + WPA_SELECTOR_LEN + 1] == 0) {
  689. ie->wpa_ie = pos;
  690. ie->wpa_ie_len = pos[1] + 2;
  691. return 0;
  692. }
  693. if (pos[1] >= 4 && WPA_GET_BE32(pos + 2) == OSEN_IE_VENDOR_TYPE) {
  694. ie->osen = pos;
  695. ie->osen_len = pos[1] + 2;
  696. return 0;
  697. }
  698. if (1 + RSN_SELECTOR_LEN < end - pos &&
  699. pos[1] >= RSN_SELECTOR_LEN + PMKID_LEN &&
  700. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_PMKID) {
  701. ie->pmkid = pos + 2 + RSN_SELECTOR_LEN;
  702. return 0;
  703. }
  704. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  705. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_GROUPKEY) {
  706. ie->gtk = pos + 2 + RSN_SELECTOR_LEN;
  707. ie->gtk_len = pos[1] - RSN_SELECTOR_LEN;
  708. return 0;
  709. }
  710. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  711. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_MAC_ADDR) {
  712. ie->mac_addr = pos + 2 + RSN_SELECTOR_LEN;
  713. ie->mac_addr_len = pos[1] - RSN_SELECTOR_LEN;
  714. return 0;
  715. }
  716. #ifdef CONFIG_PEERKEY
  717. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  718. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_SMK) {
  719. ie->smk = pos + 2 + RSN_SELECTOR_LEN;
  720. ie->smk_len = pos[1] - RSN_SELECTOR_LEN;
  721. return 0;
  722. }
  723. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  724. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_NONCE) {
  725. ie->nonce = pos + 2 + RSN_SELECTOR_LEN;
  726. ie->nonce_len = pos[1] - RSN_SELECTOR_LEN;
  727. return 0;
  728. }
  729. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  730. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_LIFETIME) {
  731. ie->lifetime = pos + 2 + RSN_SELECTOR_LEN;
  732. ie->lifetime_len = pos[1] - RSN_SELECTOR_LEN;
  733. return 0;
  734. }
  735. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  736. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_ERROR) {
  737. ie->error = pos + 2 + RSN_SELECTOR_LEN;
  738. ie->error_len = pos[1] - RSN_SELECTOR_LEN;
  739. return 0;
  740. }
  741. #endif /* CONFIG_PEERKEY */
  742. #ifdef CONFIG_IEEE80211W
  743. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  744. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_IGTK) {
  745. ie->igtk = pos + 2 + RSN_SELECTOR_LEN;
  746. ie->igtk_len = pos[1] - RSN_SELECTOR_LEN;
  747. return 0;
  748. }
  749. #endif /* CONFIG_IEEE80211W */
  750. #ifdef CONFIG_P2P
  751. if (pos[1] >= RSN_SELECTOR_LEN + 1 &&
  752. RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_IP_ADDR_REQ) {
  753. ie->ip_addr_req = pos + 2 + RSN_SELECTOR_LEN;
  754. wpa_hexdump(MSG_DEBUG, "WPA: IP Address Request in EAPOL-Key",
  755. ie->ip_addr_req, pos[1] - RSN_SELECTOR_LEN);
  756. return 0;
  757. }
  758. if (pos[1] >= RSN_SELECTOR_LEN + 3 * 4 &&
  759. RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_IP_ADDR_ALLOC) {
  760. ie->ip_addr_alloc = pos + 2 + RSN_SELECTOR_LEN;
  761. wpa_hexdump(MSG_DEBUG,
  762. "WPA: IP Address Allocation in EAPOL-Key",
  763. ie->ip_addr_alloc, pos[1] - RSN_SELECTOR_LEN);
  764. return 0;
  765. }
  766. #endif /* CONFIG_P2P */
  767. return 0;
  768. }
  769. /**
  770. * wpa_parse_kde_ies - Parse EAPOL-Key Key Data IEs
  771. * @buf: Pointer to the Key Data buffer
  772. * @len: Key Data Length
  773. * @ie: Pointer to parsed IE data
  774. * Returns: 0 on success, -1 on failure
  775. */
  776. int wpa_parse_kde_ies(const u8 *buf, size_t len, struct wpa_eapol_ie_parse *ie)
  777. {
  778. const u8 *pos, *end;
  779. int ret = 0;
  780. os_memset(ie, 0, sizeof(*ie));
  781. for (pos = buf, end = pos + len; end - pos > 1; pos += 2 + pos[1]) {
  782. if (pos[0] == 0xdd &&
  783. ((pos == buf + len - 1) || pos[1] == 0)) {
  784. /* Ignore padding */
  785. break;
  786. }
  787. if (2 + pos[1] > end - pos) {
  788. wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key Key Data "
  789. "underflow (ie=%d len=%d pos=%d)",
  790. pos[0], pos[1], (int) (pos - buf));
  791. wpa_hexdump_key(MSG_DEBUG, "WPA: Key Data",
  792. buf, len);
  793. ret = -1;
  794. break;
  795. }
  796. if (*pos == WLAN_EID_RSN) {
  797. ie->rsn_ie = pos;
  798. ie->rsn_ie_len = pos[1] + 2;
  799. #ifdef CONFIG_IEEE80211R
  800. } else if (*pos == WLAN_EID_MOBILITY_DOMAIN) {
  801. ie->mdie = pos;
  802. ie->mdie_len = pos[1] + 2;
  803. } else if (*pos == WLAN_EID_FAST_BSS_TRANSITION) {
  804. ie->ftie = pos;
  805. ie->ftie_len = pos[1] + 2;
  806. #endif /* CONFIG_IEEE80211R */
  807. } else if (*pos == WLAN_EID_VENDOR_SPECIFIC) {
  808. ret = wpa_parse_generic(pos, end, ie);
  809. if (ret < 0)
  810. break;
  811. if (ret > 0) {
  812. ret = 0;
  813. break;
  814. }
  815. } else {
  816. wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized EAPOL-Key "
  817. "Key Data IE", pos, 2 + pos[1]);
  818. }
  819. }
  820. return ret;
  821. }
  822. int wpa_auth_uses_mfp(struct wpa_state_machine *sm)
  823. {
  824. return sm ? sm->mgmt_frame_prot : 0;
  825. }