wpa_auth_ie.c 23 KB

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