wpa_auth_ie.c 22 KB

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