wpa_auth_ie.c 23 KB

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