wpa_auth_ie.c 23 KB

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