wpa_auth_ie.c 22 KB

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