wpa_auth_ie.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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_MFPC;
  187. if (conf->ieee80211w == IEEE80211W_REQUIRED)
  188. capab |= WPA_CAPABILITY_MFPR;
  189. }
  190. #endif /* CONFIG_IEEE80211W */
  191. WPA_PUT_LE16(pos, capab);
  192. pos += 2;
  193. if (pmkid) {
  194. if (pos + 2 + PMKID_LEN > buf + len)
  195. return -1;
  196. /* PMKID Count */
  197. WPA_PUT_LE16(pos, 1);
  198. pos += 2;
  199. os_memcpy(pos, pmkid, PMKID_LEN);
  200. pos += PMKID_LEN;
  201. }
  202. #ifdef CONFIG_IEEE80211W
  203. if (conf->ieee80211w != WPA_NO_IEEE80211W) {
  204. if (pos + 2 + 4 > buf + len)
  205. return -1;
  206. if (pmkid == NULL) {
  207. /* PMKID Count */
  208. WPA_PUT_LE16(pos, 0);
  209. pos += 2;
  210. }
  211. /* Management Group Cipher Suite */
  212. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
  213. pos += RSN_SELECTOR_LEN;
  214. }
  215. #endif /* CONFIG_IEEE80211W */
  216. hdr->len = (pos - buf) - 2;
  217. return pos - buf;
  218. }
  219. int wpa_auth_gen_wpa_ie(struct wpa_authenticator *wpa_auth)
  220. {
  221. u8 *pos, buf[128];
  222. int res;
  223. pos = buf;
  224. if (wpa_auth->conf.wpa & WPA_PROTO_RSN) {
  225. res = wpa_write_rsn_ie(&wpa_auth->conf,
  226. pos, buf + sizeof(buf) - pos, NULL);
  227. if (res < 0)
  228. return res;
  229. pos += res;
  230. }
  231. #ifdef CONFIG_IEEE80211R
  232. if (wpa_auth->conf.wpa_key_mgmt &
  233. (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) {
  234. res = wpa_write_mdie(&wpa_auth->conf, pos,
  235. buf + sizeof(buf) - pos);
  236. if (res < 0)
  237. return res;
  238. pos += res;
  239. }
  240. #endif /* CONFIG_IEEE80211R */
  241. if (wpa_auth->conf.wpa & WPA_PROTO_WPA) {
  242. res = wpa_write_wpa_ie(&wpa_auth->conf,
  243. pos, buf + sizeof(buf) - pos);
  244. if (res < 0)
  245. return res;
  246. pos += res;
  247. }
  248. os_free(wpa_auth->wpa_ie);
  249. wpa_auth->wpa_ie = os_malloc(pos - buf);
  250. if (wpa_auth->wpa_ie == NULL)
  251. return -1;
  252. os_memcpy(wpa_auth->wpa_ie, buf, pos - buf);
  253. wpa_auth->wpa_ie_len = pos - buf;
  254. return 0;
  255. }
  256. u8 * wpa_add_kde(u8 *pos, u32 kde, const u8 *data, size_t data_len,
  257. const u8 *data2, size_t data2_len)
  258. {
  259. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  260. *pos++ = RSN_SELECTOR_LEN + data_len + data2_len;
  261. RSN_SELECTOR_PUT(pos, kde);
  262. pos += RSN_SELECTOR_LEN;
  263. os_memcpy(pos, data, data_len);
  264. pos += data_len;
  265. if (data2) {
  266. os_memcpy(pos, data2, data2_len);
  267. pos += data2_len;
  268. }
  269. return pos;
  270. }
  271. static int wpa_selector_to_bitfield(const u8 *s)
  272. {
  273. if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_NONE)
  274. return WPA_CIPHER_NONE;
  275. if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_WEP40)
  276. return WPA_CIPHER_WEP40;
  277. if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_TKIP)
  278. return WPA_CIPHER_TKIP;
  279. if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_CCMP)
  280. return WPA_CIPHER_CCMP;
  281. if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_WEP104)
  282. return WPA_CIPHER_WEP104;
  283. return 0;
  284. }
  285. static int wpa_key_mgmt_to_bitfield(const u8 *s)
  286. {
  287. if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_UNSPEC_802_1X)
  288. return WPA_KEY_MGMT_IEEE8021X;
  289. if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X)
  290. return WPA_KEY_MGMT_PSK;
  291. if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_NONE)
  292. return WPA_KEY_MGMT_WPA_NONE;
  293. return 0;
  294. }
  295. static int wpa_parse_wpa_ie_wpa(const u8 *wpa_ie, size_t wpa_ie_len,
  296. struct wpa_ie_data *data)
  297. {
  298. const struct wpa_ie_hdr *hdr;
  299. const u8 *pos;
  300. int left;
  301. int i, count;
  302. os_memset(data, 0, sizeof(*data));
  303. data->pairwise_cipher = WPA_CIPHER_TKIP;
  304. data->group_cipher = WPA_CIPHER_TKIP;
  305. data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  306. data->mgmt_group_cipher = 0;
  307. if (wpa_ie_len < sizeof(struct wpa_ie_hdr))
  308. return -1;
  309. hdr = (const struct wpa_ie_hdr *) wpa_ie;
  310. if (hdr->elem_id != WLAN_EID_VENDOR_SPECIFIC ||
  311. hdr->len != wpa_ie_len - 2 ||
  312. RSN_SELECTOR_GET(hdr->oui) != WPA_OUI_TYPE ||
  313. WPA_GET_LE16(hdr->version) != WPA_VERSION) {
  314. return -2;
  315. }
  316. pos = (const u8 *) (hdr + 1);
  317. left = wpa_ie_len - sizeof(*hdr);
  318. if (left >= WPA_SELECTOR_LEN) {
  319. data->group_cipher = wpa_selector_to_bitfield(pos);
  320. pos += WPA_SELECTOR_LEN;
  321. left -= WPA_SELECTOR_LEN;
  322. } else if (left > 0)
  323. return -3;
  324. if (left >= 2) {
  325. data->pairwise_cipher = 0;
  326. count = WPA_GET_LE16(pos);
  327. pos += 2;
  328. left -= 2;
  329. if (count == 0 || left < count * WPA_SELECTOR_LEN)
  330. return -4;
  331. for (i = 0; i < count; i++) {
  332. data->pairwise_cipher |= wpa_selector_to_bitfield(pos);
  333. pos += WPA_SELECTOR_LEN;
  334. left -= WPA_SELECTOR_LEN;
  335. }
  336. } else if (left == 1)
  337. return -5;
  338. if (left >= 2) {
  339. data->key_mgmt = 0;
  340. count = WPA_GET_LE16(pos);
  341. pos += 2;
  342. left -= 2;
  343. if (count == 0 || left < count * WPA_SELECTOR_LEN)
  344. return -6;
  345. for (i = 0; i < count; i++) {
  346. data->key_mgmt |= wpa_key_mgmt_to_bitfield(pos);
  347. pos += WPA_SELECTOR_LEN;
  348. left -= WPA_SELECTOR_LEN;
  349. }
  350. } else if (left == 1)
  351. return -7;
  352. if (left >= 2) {
  353. data->capabilities = WPA_GET_LE16(pos);
  354. pos += 2;
  355. left -= 2;
  356. }
  357. if (left > 0) {
  358. return -8;
  359. }
  360. return 0;
  361. }
  362. struct wpa_auth_okc_iter_data {
  363. struct rsn_pmksa_cache_entry *pmksa;
  364. const u8 *aa;
  365. const u8 *spa;
  366. const u8 *pmkid;
  367. };
  368. static int wpa_auth_okc_iter(struct wpa_authenticator *a, void *ctx)
  369. {
  370. struct wpa_auth_okc_iter_data *data = ctx;
  371. data->pmksa = pmksa_cache_get_okc(a->pmksa, data->aa, data->spa,
  372. data->pmkid);
  373. if (data->pmksa)
  374. return 1;
  375. return 0;
  376. }
  377. int wpa_validate_wpa_ie(struct wpa_authenticator *wpa_auth,
  378. struct wpa_state_machine *sm,
  379. const u8 *wpa_ie, size_t wpa_ie_len,
  380. const u8 *mdie, size_t mdie_len)
  381. {
  382. struct wpa_ie_data data;
  383. int ciphers, key_mgmt, res, version;
  384. u32 selector;
  385. size_t i;
  386. const u8 *pmkid = NULL;
  387. if (wpa_auth == NULL || sm == NULL)
  388. return WPA_NOT_ENABLED;
  389. if (wpa_ie == NULL || wpa_ie_len < 1)
  390. return WPA_INVALID_IE;
  391. if (wpa_ie[0] == WLAN_EID_RSN)
  392. version = WPA_PROTO_RSN;
  393. else
  394. version = WPA_PROTO_WPA;
  395. if (version == WPA_PROTO_RSN) {
  396. res = wpa_parse_wpa_ie_rsn(wpa_ie, wpa_ie_len, &data);
  397. selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
  398. if (0) {
  399. }
  400. #ifdef CONFIG_IEEE80211R
  401. else if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
  402. selector = RSN_AUTH_KEY_MGMT_FT_802_1X;
  403. else if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK)
  404. selector = RSN_AUTH_KEY_MGMT_FT_PSK;
  405. #endif /* CONFIG_IEEE80211R */
  406. else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  407. selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
  408. else if (data.key_mgmt & WPA_KEY_MGMT_PSK)
  409. selector = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
  410. wpa_auth->dot11RSNAAuthenticationSuiteSelected = selector;
  411. selector = RSN_CIPHER_SUITE_CCMP;
  412. if (data.pairwise_cipher & WPA_CIPHER_CCMP)
  413. selector = RSN_CIPHER_SUITE_CCMP;
  414. else if (data.pairwise_cipher & WPA_CIPHER_TKIP)
  415. selector = RSN_CIPHER_SUITE_TKIP;
  416. else if (data.pairwise_cipher & WPA_CIPHER_WEP104)
  417. selector = RSN_CIPHER_SUITE_WEP104;
  418. else if (data.pairwise_cipher & WPA_CIPHER_WEP40)
  419. selector = RSN_CIPHER_SUITE_WEP40;
  420. else if (data.pairwise_cipher & WPA_CIPHER_NONE)
  421. selector = RSN_CIPHER_SUITE_NONE;
  422. wpa_auth->dot11RSNAPairwiseCipherSelected = selector;
  423. selector = RSN_CIPHER_SUITE_CCMP;
  424. if (data.group_cipher & WPA_CIPHER_CCMP)
  425. selector = RSN_CIPHER_SUITE_CCMP;
  426. else if (data.group_cipher & WPA_CIPHER_TKIP)
  427. selector = RSN_CIPHER_SUITE_TKIP;
  428. else if (data.group_cipher & WPA_CIPHER_WEP104)
  429. selector = RSN_CIPHER_SUITE_WEP104;
  430. else if (data.group_cipher & WPA_CIPHER_WEP40)
  431. selector = RSN_CIPHER_SUITE_WEP40;
  432. else if (data.group_cipher & WPA_CIPHER_NONE)
  433. selector = RSN_CIPHER_SUITE_NONE;
  434. wpa_auth->dot11RSNAGroupCipherSelected = selector;
  435. } else {
  436. res = wpa_parse_wpa_ie_wpa(wpa_ie, wpa_ie_len, &data);
  437. selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X;
  438. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  439. selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X;
  440. else if (data.key_mgmt & WPA_KEY_MGMT_PSK)
  441. selector = WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X;
  442. wpa_auth->dot11RSNAAuthenticationSuiteSelected = selector;
  443. selector = WPA_CIPHER_SUITE_TKIP;
  444. if (data.pairwise_cipher & WPA_CIPHER_CCMP)
  445. selector = WPA_CIPHER_SUITE_CCMP;
  446. else if (data.pairwise_cipher & WPA_CIPHER_TKIP)
  447. selector = WPA_CIPHER_SUITE_TKIP;
  448. else if (data.pairwise_cipher & WPA_CIPHER_WEP104)
  449. selector = WPA_CIPHER_SUITE_WEP104;
  450. else if (data.pairwise_cipher & WPA_CIPHER_WEP40)
  451. selector = WPA_CIPHER_SUITE_WEP40;
  452. else if (data.pairwise_cipher & WPA_CIPHER_NONE)
  453. selector = WPA_CIPHER_SUITE_NONE;
  454. wpa_auth->dot11RSNAPairwiseCipherSelected = selector;
  455. selector = WPA_CIPHER_SUITE_TKIP;
  456. if (data.group_cipher & WPA_CIPHER_CCMP)
  457. selector = WPA_CIPHER_SUITE_CCMP;
  458. else if (data.group_cipher & WPA_CIPHER_TKIP)
  459. selector = WPA_CIPHER_SUITE_TKIP;
  460. else if (data.group_cipher & WPA_CIPHER_WEP104)
  461. selector = WPA_CIPHER_SUITE_WEP104;
  462. else if (data.group_cipher & WPA_CIPHER_WEP40)
  463. selector = WPA_CIPHER_SUITE_WEP40;
  464. else if (data.group_cipher & WPA_CIPHER_NONE)
  465. selector = WPA_CIPHER_SUITE_NONE;
  466. wpa_auth->dot11RSNAGroupCipherSelected = selector;
  467. }
  468. if (res) {
  469. wpa_printf(MSG_DEBUG, "Failed to parse WPA/RSN IE from "
  470. MACSTR " (res=%d)", MAC2STR(sm->addr), res);
  471. wpa_hexdump(MSG_DEBUG, "WPA/RSN IE", wpa_ie, wpa_ie_len);
  472. return WPA_INVALID_IE;
  473. }
  474. if (data.group_cipher != wpa_auth->conf.wpa_group) {
  475. wpa_printf(MSG_DEBUG, "Invalid WPA group cipher (0x%x) from "
  476. MACSTR, data.group_cipher, MAC2STR(sm->addr));
  477. return WPA_INVALID_GROUP;
  478. }
  479. key_mgmt = data.key_mgmt & wpa_auth->conf.wpa_key_mgmt;
  480. if (!key_mgmt) {
  481. wpa_printf(MSG_DEBUG, "Invalid WPA key mgmt (0x%x) from "
  482. MACSTR, data.key_mgmt, MAC2STR(sm->addr));
  483. return WPA_INVALID_AKMP;
  484. }
  485. if (0) {
  486. }
  487. #ifdef CONFIG_IEEE80211R
  488. else if (key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
  489. sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
  490. else if (key_mgmt & WPA_KEY_MGMT_FT_PSK)
  491. sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_PSK;
  492. #endif /* CONFIG_IEEE80211R */
  493. else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  494. sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  495. else
  496. sm->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
  497. if (version == WPA_PROTO_RSN)
  498. ciphers = data.pairwise_cipher & wpa_auth->conf.rsn_pairwise;
  499. else
  500. ciphers = data.pairwise_cipher & wpa_auth->conf.wpa_pairwise;
  501. if (!ciphers) {
  502. wpa_printf(MSG_DEBUG, "Invalid %s pairwise cipher (0x%x) "
  503. "from " MACSTR,
  504. version == WPA_PROTO_RSN ? "RSN" : "WPA",
  505. data.pairwise_cipher, MAC2STR(sm->addr));
  506. return WPA_INVALID_PAIRWISE;
  507. }
  508. #ifdef CONFIG_IEEE80211W
  509. if (wpa_auth->conf.ieee80211w == WPA_IEEE80211W_REQUIRED) {
  510. if (!(data.capabilities & WPA_CAPABILITY_MFPC)) {
  511. wpa_printf(MSG_DEBUG, "Management frame protection "
  512. "required, but client did not enable it");
  513. return WPA_MGMT_FRAME_PROTECTION_VIOLATION;
  514. }
  515. if (ciphers & WPA_CIPHER_TKIP) {
  516. wpa_printf(MSG_DEBUG, "Management frame protection "
  517. "cannot use TKIP");
  518. return WPA_MGMT_FRAME_PROTECTION_VIOLATION;
  519. }
  520. if (data.mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC) {
  521. wpa_printf(MSG_DEBUG, "Unsupported management group "
  522. "cipher %d", data.mgmt_group_cipher);
  523. return WPA_INVALID_MGMT_GROUP_CIPHER;
  524. }
  525. }
  526. if (wpa_auth->conf.ieee80211w == WPA_NO_IEEE80211W ||
  527. !(data.capabilities & WPA_CAPABILITY_MFPC))
  528. sm->mgmt_frame_prot = 0;
  529. else
  530. sm->mgmt_frame_prot = 1;
  531. #endif /* CONFIG_IEEE80211W */
  532. #ifdef CONFIG_IEEE80211R
  533. if (sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X ||
  534. sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_PSK) {
  535. if (mdie == NULL || mdie_len < MOBILITY_DOMAIN_ID_LEN + 1) {
  536. wpa_printf(MSG_DEBUG, "RSN: Trying to use FT, but "
  537. "MDIE not included");
  538. return WPA_INVALID_MDIE;
  539. }
  540. if (os_memcmp(mdie, wpa_auth->conf.mobility_domain,
  541. MOBILITY_DOMAIN_ID_LEN) != 0) {
  542. wpa_hexdump(MSG_DEBUG, "RSN: Attempted to use unknown "
  543. "MDIE", mdie, MOBILITY_DOMAIN_ID_LEN);
  544. return WPA_INVALID_MDIE;
  545. }
  546. }
  547. #endif /* CONFIG_IEEE80211R */
  548. if (ciphers & WPA_CIPHER_CCMP)
  549. sm->pairwise = WPA_CIPHER_CCMP;
  550. else
  551. sm->pairwise = WPA_CIPHER_TKIP;
  552. /* TODO: clear WPA/WPA2 state if STA changes from one to another */
  553. if (wpa_ie[0] == WLAN_EID_RSN)
  554. sm->wpa = WPA_VERSION_WPA2;
  555. else
  556. sm->wpa = WPA_VERSION_WPA;
  557. sm->pmksa = NULL;
  558. for (i = 0; i < data.num_pmkid; i++) {
  559. wpa_hexdump(MSG_DEBUG, "RSN IE: STA PMKID",
  560. &data.pmkid[i * PMKID_LEN], PMKID_LEN);
  561. sm->pmksa = pmksa_cache_get(wpa_auth->pmksa, sm->addr,
  562. &data.pmkid[i * PMKID_LEN]);
  563. if (sm->pmksa) {
  564. pmkid = sm->pmksa->pmkid;
  565. break;
  566. }
  567. }
  568. for (i = 0; sm->pmksa == NULL && wpa_auth->conf.okc &&
  569. i < data.num_pmkid; i++) {
  570. struct wpa_auth_okc_iter_data idata;
  571. idata.pmksa = NULL;
  572. idata.aa = wpa_auth->addr;
  573. idata.spa = sm->addr;
  574. idata.pmkid = &data.pmkid[i * PMKID_LEN];
  575. wpa_auth_for_each_auth(wpa_auth, wpa_auth_okc_iter, &idata);
  576. if (idata.pmksa) {
  577. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  578. "OKC match for PMKID");
  579. sm->pmksa = pmksa_cache_add_okc(wpa_auth->pmksa,
  580. idata.pmksa,
  581. wpa_auth->addr,
  582. idata.pmkid);
  583. pmkid = idata.pmkid;
  584. break;
  585. }
  586. }
  587. if (sm->pmksa) {
  588. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  589. "PMKID found from PMKSA cache "
  590. "eap_type=%d vlan_id=%d",
  591. sm->pmksa->eap_type_authsrv,
  592. sm->pmksa->vlan_id);
  593. os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmkid, PMKID_LEN);
  594. }
  595. if (sm->wpa_ie == NULL || sm->wpa_ie_len < wpa_ie_len) {
  596. os_free(sm->wpa_ie);
  597. sm->wpa_ie = os_malloc(wpa_ie_len);
  598. if (sm->wpa_ie == NULL)
  599. return WPA_ALLOC_FAIL;
  600. }
  601. os_memcpy(sm->wpa_ie, wpa_ie, wpa_ie_len);
  602. sm->wpa_ie_len = wpa_ie_len;
  603. return WPA_IE_OK;
  604. }
  605. /**
  606. * wpa_parse_generic - Parse EAPOL-Key Key Data Generic IEs
  607. * @pos: Pointer to the IE header
  608. * @end: Pointer to the end of the Key Data buffer
  609. * @ie: Pointer to parsed IE data
  610. * Returns: 0 on success, 1 if end mark is found, -1 on failure
  611. */
  612. static int wpa_parse_generic(const u8 *pos, const u8 *end,
  613. struct wpa_eapol_ie_parse *ie)
  614. {
  615. if (pos[1] == 0)
  616. return 1;
  617. if (pos[1] >= 6 &&
  618. RSN_SELECTOR_GET(pos + 2) == WPA_OUI_TYPE &&
  619. pos[2 + WPA_SELECTOR_LEN] == 1 &&
  620. pos[2 + WPA_SELECTOR_LEN + 1] == 0) {
  621. ie->wpa_ie = pos;
  622. ie->wpa_ie_len = pos[1] + 2;
  623. return 0;
  624. }
  625. if (pos + 1 + RSN_SELECTOR_LEN < end &&
  626. pos[1] >= RSN_SELECTOR_LEN + PMKID_LEN &&
  627. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_PMKID) {
  628. ie->pmkid = pos + 2 + RSN_SELECTOR_LEN;
  629. return 0;
  630. }
  631. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  632. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_GROUPKEY) {
  633. ie->gtk = pos + 2 + RSN_SELECTOR_LEN;
  634. ie->gtk_len = pos[1] - RSN_SELECTOR_LEN;
  635. return 0;
  636. }
  637. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  638. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_MAC_ADDR) {
  639. ie->mac_addr = pos + 2 + RSN_SELECTOR_LEN;
  640. ie->mac_addr_len = pos[1] - RSN_SELECTOR_LEN;
  641. return 0;
  642. }
  643. #ifdef CONFIG_PEERKEY
  644. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  645. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_SMK) {
  646. ie->smk = pos + 2 + RSN_SELECTOR_LEN;
  647. ie->smk_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_NONCE) {
  652. ie->nonce = pos + 2 + RSN_SELECTOR_LEN;
  653. ie->nonce_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_LIFETIME) {
  658. ie->lifetime = pos + 2 + RSN_SELECTOR_LEN;
  659. ie->lifetime_len = pos[1] - RSN_SELECTOR_LEN;
  660. return 0;
  661. }
  662. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  663. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_ERROR) {
  664. ie->error = pos + 2 + RSN_SELECTOR_LEN;
  665. ie->error_len = pos[1] - RSN_SELECTOR_LEN;
  666. return 0;
  667. }
  668. #endif /* CONFIG_PEERKEY */
  669. #ifdef CONFIG_IEEE80211W
  670. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  671. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_IGTK) {
  672. ie->igtk = pos + 2 + RSN_SELECTOR_LEN;
  673. ie->igtk_len = pos[1] - RSN_SELECTOR_LEN;
  674. return 0;
  675. }
  676. #endif /* CONFIG_IEEE80211W */
  677. return 0;
  678. }
  679. /**
  680. * wpa_parse_kde_ies - Parse EAPOL-Key Key Data IEs
  681. * @buf: Pointer to the Key Data buffer
  682. * @len: Key Data Length
  683. * @ie: Pointer to parsed IE data
  684. * Returns: 0 on success, -1 on failure
  685. */
  686. int wpa_parse_kde_ies(const u8 *buf, size_t len, struct wpa_eapol_ie_parse *ie)
  687. {
  688. const u8 *pos, *end;
  689. int ret = 0;
  690. os_memset(ie, 0, sizeof(*ie));
  691. for (pos = buf, end = pos + len; pos + 1 < end; pos += 2 + pos[1]) {
  692. if (pos[0] == 0xdd &&
  693. ((pos == buf + len - 1) || pos[1] == 0)) {
  694. /* Ignore padding */
  695. break;
  696. }
  697. if (pos + 2 + pos[1] > end) {
  698. wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key Key Data "
  699. "underflow (ie=%d len=%d pos=%d)",
  700. pos[0], pos[1], (int) (pos - buf));
  701. wpa_hexdump_key(MSG_DEBUG, "WPA: Key Data",
  702. buf, len);
  703. ret = -1;
  704. break;
  705. }
  706. if (*pos == WLAN_EID_RSN) {
  707. ie->rsn_ie = pos;
  708. ie->rsn_ie_len = pos[1] + 2;
  709. #ifdef CONFIG_IEEE80211R
  710. } else if (*pos == WLAN_EID_MOBILITY_DOMAIN) {
  711. ie->mdie = pos;
  712. ie->mdie_len = pos[1] + 2;
  713. #endif /* CONFIG_IEEE80211R */
  714. } else if (*pos == WLAN_EID_VENDOR_SPECIFIC) {
  715. ret = wpa_parse_generic(pos, end, ie);
  716. if (ret < 0)
  717. break;
  718. if (ret > 0) {
  719. ret = 0;
  720. break;
  721. }
  722. } else {
  723. wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized EAPOL-Key "
  724. "Key Data IE", pos, 2 + pos[1]);
  725. }
  726. }
  727. return ret;
  728. }
  729. int wpa_auth_uses_mfp(struct wpa_state_machine *sm)
  730. {
  731. return sm ? sm->mgmt_frame_prot : 0;
  732. }