wpa_auth_ie.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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->wmm_enabled) {
  193. /* 4 PTKSA replay counters when using WMM */
  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 (!(wpa_auth->conf.wpa & version)) {
  408. wpa_printf(MSG_DEBUG, "Invalid WPA proto (%d) from " MACSTR,
  409. version, MAC2STR(sm->addr));
  410. return WPA_INVALID_PROTO;
  411. }
  412. if (version == WPA_PROTO_RSN) {
  413. res = wpa_parse_wpa_ie_rsn(wpa_ie, wpa_ie_len, &data);
  414. selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
  415. if (0) {
  416. }
  417. #ifdef CONFIG_IEEE80211R
  418. else if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
  419. selector = RSN_AUTH_KEY_MGMT_FT_802_1X;
  420. else if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK)
  421. selector = RSN_AUTH_KEY_MGMT_FT_PSK;
  422. #endif /* CONFIG_IEEE80211R */
  423. #ifdef CONFIG_IEEE80211W
  424. else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
  425. selector = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
  426. else if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
  427. selector = RSN_AUTH_KEY_MGMT_PSK_SHA256;
  428. #endif /* CONFIG_IEEE80211W */
  429. else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  430. selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
  431. else if (data.key_mgmt & WPA_KEY_MGMT_PSK)
  432. selector = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
  433. wpa_auth->dot11RSNAAuthenticationSuiteSelected = selector;
  434. selector = RSN_CIPHER_SUITE_CCMP;
  435. if (data.pairwise_cipher & WPA_CIPHER_CCMP)
  436. selector = RSN_CIPHER_SUITE_CCMP;
  437. else if (data.pairwise_cipher & WPA_CIPHER_TKIP)
  438. selector = RSN_CIPHER_SUITE_TKIP;
  439. else if (data.pairwise_cipher & WPA_CIPHER_WEP104)
  440. selector = RSN_CIPHER_SUITE_WEP104;
  441. else if (data.pairwise_cipher & WPA_CIPHER_WEP40)
  442. selector = RSN_CIPHER_SUITE_WEP40;
  443. else if (data.pairwise_cipher & WPA_CIPHER_NONE)
  444. selector = RSN_CIPHER_SUITE_NONE;
  445. wpa_auth->dot11RSNAPairwiseCipherSelected = selector;
  446. selector = RSN_CIPHER_SUITE_CCMP;
  447. if (data.group_cipher & WPA_CIPHER_CCMP)
  448. selector = RSN_CIPHER_SUITE_CCMP;
  449. else if (data.group_cipher & WPA_CIPHER_TKIP)
  450. selector = RSN_CIPHER_SUITE_TKIP;
  451. else if (data.group_cipher & WPA_CIPHER_WEP104)
  452. selector = RSN_CIPHER_SUITE_WEP104;
  453. else if (data.group_cipher & WPA_CIPHER_WEP40)
  454. selector = RSN_CIPHER_SUITE_WEP40;
  455. else if (data.group_cipher & WPA_CIPHER_NONE)
  456. selector = RSN_CIPHER_SUITE_NONE;
  457. wpa_auth->dot11RSNAGroupCipherSelected = selector;
  458. } else {
  459. res = wpa_parse_wpa_ie_wpa(wpa_ie, wpa_ie_len, &data);
  460. selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X;
  461. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  462. selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X;
  463. else if (data.key_mgmt & WPA_KEY_MGMT_PSK)
  464. selector = WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X;
  465. wpa_auth->dot11RSNAAuthenticationSuiteSelected = selector;
  466. selector = WPA_CIPHER_SUITE_TKIP;
  467. if (data.pairwise_cipher & WPA_CIPHER_CCMP)
  468. selector = WPA_CIPHER_SUITE_CCMP;
  469. else if (data.pairwise_cipher & WPA_CIPHER_TKIP)
  470. selector = WPA_CIPHER_SUITE_TKIP;
  471. else if (data.pairwise_cipher & WPA_CIPHER_WEP104)
  472. selector = WPA_CIPHER_SUITE_WEP104;
  473. else if (data.pairwise_cipher & WPA_CIPHER_WEP40)
  474. selector = WPA_CIPHER_SUITE_WEP40;
  475. else if (data.pairwise_cipher & WPA_CIPHER_NONE)
  476. selector = WPA_CIPHER_SUITE_NONE;
  477. wpa_auth->dot11RSNAPairwiseCipherSelected = selector;
  478. selector = WPA_CIPHER_SUITE_TKIP;
  479. if (data.group_cipher & WPA_CIPHER_CCMP)
  480. selector = WPA_CIPHER_SUITE_CCMP;
  481. else if (data.group_cipher & WPA_CIPHER_TKIP)
  482. selector = WPA_CIPHER_SUITE_TKIP;
  483. else if (data.group_cipher & WPA_CIPHER_WEP104)
  484. selector = WPA_CIPHER_SUITE_WEP104;
  485. else if (data.group_cipher & WPA_CIPHER_WEP40)
  486. selector = WPA_CIPHER_SUITE_WEP40;
  487. else if (data.group_cipher & WPA_CIPHER_NONE)
  488. selector = WPA_CIPHER_SUITE_NONE;
  489. wpa_auth->dot11RSNAGroupCipherSelected = selector;
  490. }
  491. if (res) {
  492. wpa_printf(MSG_DEBUG, "Failed to parse WPA/RSN IE from "
  493. MACSTR " (res=%d)", MAC2STR(sm->addr), res);
  494. wpa_hexdump(MSG_DEBUG, "WPA/RSN IE", wpa_ie, wpa_ie_len);
  495. return WPA_INVALID_IE;
  496. }
  497. if (data.group_cipher != wpa_auth->conf.wpa_group) {
  498. wpa_printf(MSG_DEBUG, "Invalid WPA group cipher (0x%x) from "
  499. MACSTR, data.group_cipher, MAC2STR(sm->addr));
  500. return WPA_INVALID_GROUP;
  501. }
  502. key_mgmt = data.key_mgmt & wpa_auth->conf.wpa_key_mgmt;
  503. if (!key_mgmt) {
  504. wpa_printf(MSG_DEBUG, "Invalid WPA key mgmt (0x%x) from "
  505. MACSTR, data.key_mgmt, MAC2STR(sm->addr));
  506. return WPA_INVALID_AKMP;
  507. }
  508. if (0) {
  509. }
  510. #ifdef CONFIG_IEEE80211R
  511. else if (key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
  512. sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
  513. else if (key_mgmt & WPA_KEY_MGMT_FT_PSK)
  514. sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_PSK;
  515. #endif /* CONFIG_IEEE80211R */
  516. #ifdef CONFIG_IEEE80211W
  517. else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
  518. sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
  519. else if (key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
  520. sm->wpa_key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
  521. #endif /* CONFIG_IEEE80211W */
  522. else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  523. sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  524. else
  525. sm->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
  526. if (version == WPA_PROTO_RSN)
  527. ciphers = data.pairwise_cipher & wpa_auth->conf.rsn_pairwise;
  528. else
  529. ciphers = data.pairwise_cipher & wpa_auth->conf.wpa_pairwise;
  530. if (!ciphers) {
  531. wpa_printf(MSG_DEBUG, "Invalid %s pairwise cipher (0x%x) "
  532. "from " MACSTR,
  533. version == WPA_PROTO_RSN ? "RSN" : "WPA",
  534. data.pairwise_cipher, MAC2STR(sm->addr));
  535. return WPA_INVALID_PAIRWISE;
  536. }
  537. #ifdef CONFIG_IEEE80211W
  538. if (wpa_auth->conf.ieee80211w == WPA_IEEE80211W_REQUIRED) {
  539. if (!(data.capabilities & WPA_CAPABILITY_MFPC)) {
  540. wpa_printf(MSG_DEBUG, "Management frame protection "
  541. "required, but client did not enable it");
  542. return WPA_MGMT_FRAME_PROTECTION_VIOLATION;
  543. }
  544. if (ciphers & WPA_CIPHER_TKIP) {
  545. wpa_printf(MSG_DEBUG, "Management frame protection "
  546. "cannot use TKIP");
  547. return WPA_MGMT_FRAME_PROTECTION_VIOLATION;
  548. }
  549. if (data.mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC) {
  550. wpa_printf(MSG_DEBUG, "Unsupported management group "
  551. "cipher %d", data.mgmt_group_cipher);
  552. return WPA_INVALID_MGMT_GROUP_CIPHER;
  553. }
  554. }
  555. if (wpa_auth->conf.ieee80211w == WPA_NO_IEEE80211W ||
  556. !(data.capabilities & WPA_CAPABILITY_MFPC))
  557. sm->mgmt_frame_prot = 0;
  558. else
  559. sm->mgmt_frame_prot = 1;
  560. #endif /* CONFIG_IEEE80211W */
  561. #ifdef CONFIG_IEEE80211R
  562. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
  563. if (mdie == NULL || mdie_len < MOBILITY_DOMAIN_ID_LEN + 1) {
  564. wpa_printf(MSG_DEBUG, "RSN: Trying to use FT, but "
  565. "MDIE not included");
  566. return WPA_INVALID_MDIE;
  567. }
  568. if (os_memcmp(mdie, wpa_auth->conf.mobility_domain,
  569. MOBILITY_DOMAIN_ID_LEN) != 0) {
  570. wpa_hexdump(MSG_DEBUG, "RSN: Attempted to use unknown "
  571. "MDIE", mdie, MOBILITY_DOMAIN_ID_LEN);
  572. return WPA_INVALID_MDIE;
  573. }
  574. }
  575. #endif /* CONFIG_IEEE80211R */
  576. if (ciphers & WPA_CIPHER_CCMP)
  577. sm->pairwise = WPA_CIPHER_CCMP;
  578. else
  579. sm->pairwise = WPA_CIPHER_TKIP;
  580. /* TODO: clear WPA/WPA2 state if STA changes from one to another */
  581. if (wpa_ie[0] == WLAN_EID_RSN)
  582. sm->wpa = WPA_VERSION_WPA2;
  583. else
  584. sm->wpa = WPA_VERSION_WPA;
  585. sm->pmksa = NULL;
  586. for (i = 0; i < data.num_pmkid; i++) {
  587. wpa_hexdump(MSG_DEBUG, "RSN IE: STA PMKID",
  588. &data.pmkid[i * PMKID_LEN], PMKID_LEN);
  589. sm->pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sm->addr,
  590. &data.pmkid[i * PMKID_LEN]);
  591. if (sm->pmksa) {
  592. pmkid = sm->pmksa->pmkid;
  593. break;
  594. }
  595. }
  596. for (i = 0; sm->pmksa == NULL && wpa_auth->conf.okc &&
  597. i < data.num_pmkid; i++) {
  598. struct wpa_auth_okc_iter_data idata;
  599. idata.pmksa = NULL;
  600. idata.aa = wpa_auth->addr;
  601. idata.spa = sm->addr;
  602. idata.pmkid = &data.pmkid[i * PMKID_LEN];
  603. wpa_auth_for_each_auth(wpa_auth, wpa_auth_okc_iter, &idata);
  604. if (idata.pmksa) {
  605. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  606. "OKC match for PMKID");
  607. sm->pmksa = pmksa_cache_add_okc(wpa_auth->pmksa,
  608. idata.pmksa,
  609. wpa_auth->addr,
  610. idata.pmkid);
  611. pmkid = idata.pmkid;
  612. break;
  613. }
  614. }
  615. if (sm->pmksa) {
  616. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  617. "PMKID found from PMKSA cache "
  618. "eap_type=%d vlan_id=%d",
  619. sm->pmksa->eap_type_authsrv,
  620. sm->pmksa->vlan_id);
  621. os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmkid, PMKID_LEN);
  622. }
  623. if (sm->wpa_ie == NULL || sm->wpa_ie_len < wpa_ie_len) {
  624. os_free(sm->wpa_ie);
  625. sm->wpa_ie = os_malloc(wpa_ie_len);
  626. if (sm->wpa_ie == NULL)
  627. return WPA_ALLOC_FAIL;
  628. }
  629. os_memcpy(sm->wpa_ie, wpa_ie, wpa_ie_len);
  630. sm->wpa_ie_len = wpa_ie_len;
  631. return WPA_IE_OK;
  632. }
  633. /**
  634. * wpa_parse_generic - Parse EAPOL-Key Key Data Generic IEs
  635. * @pos: Pointer to the IE header
  636. * @end: Pointer to the end of the Key Data buffer
  637. * @ie: Pointer to parsed IE data
  638. * Returns: 0 on success, 1 if end mark is found, -1 on failure
  639. */
  640. static int wpa_parse_generic(const u8 *pos, const u8 *end,
  641. struct wpa_eapol_ie_parse *ie)
  642. {
  643. if (pos[1] == 0)
  644. return 1;
  645. if (pos[1] >= 6 &&
  646. RSN_SELECTOR_GET(pos + 2) == WPA_OUI_TYPE &&
  647. pos[2 + WPA_SELECTOR_LEN] == 1 &&
  648. pos[2 + WPA_SELECTOR_LEN + 1] == 0) {
  649. ie->wpa_ie = pos;
  650. ie->wpa_ie_len = pos[1] + 2;
  651. return 0;
  652. }
  653. if (pos + 1 + RSN_SELECTOR_LEN < end &&
  654. pos[1] >= RSN_SELECTOR_LEN + PMKID_LEN &&
  655. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_PMKID) {
  656. ie->pmkid = pos + 2 + RSN_SELECTOR_LEN;
  657. return 0;
  658. }
  659. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  660. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_GROUPKEY) {
  661. ie->gtk = pos + 2 + RSN_SELECTOR_LEN;
  662. ie->gtk_len = pos[1] - RSN_SELECTOR_LEN;
  663. return 0;
  664. }
  665. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  666. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_MAC_ADDR) {
  667. ie->mac_addr = pos + 2 + RSN_SELECTOR_LEN;
  668. ie->mac_addr_len = pos[1] - RSN_SELECTOR_LEN;
  669. return 0;
  670. }
  671. #ifdef CONFIG_PEERKEY
  672. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  673. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_SMK) {
  674. ie->smk = pos + 2 + RSN_SELECTOR_LEN;
  675. ie->smk_len = pos[1] - RSN_SELECTOR_LEN;
  676. return 0;
  677. }
  678. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  679. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_NONCE) {
  680. ie->nonce = pos + 2 + RSN_SELECTOR_LEN;
  681. ie->nonce_len = pos[1] - RSN_SELECTOR_LEN;
  682. return 0;
  683. }
  684. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  685. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_LIFETIME) {
  686. ie->lifetime = pos + 2 + RSN_SELECTOR_LEN;
  687. ie->lifetime_len = pos[1] - RSN_SELECTOR_LEN;
  688. return 0;
  689. }
  690. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  691. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_ERROR) {
  692. ie->error = pos + 2 + RSN_SELECTOR_LEN;
  693. ie->error_len = pos[1] - RSN_SELECTOR_LEN;
  694. return 0;
  695. }
  696. #endif /* CONFIG_PEERKEY */
  697. #ifdef CONFIG_IEEE80211W
  698. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  699. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_IGTK) {
  700. ie->igtk = pos + 2 + RSN_SELECTOR_LEN;
  701. ie->igtk_len = pos[1] - RSN_SELECTOR_LEN;
  702. return 0;
  703. }
  704. #endif /* CONFIG_IEEE80211W */
  705. return 0;
  706. }
  707. /**
  708. * wpa_parse_kde_ies - Parse EAPOL-Key Key Data IEs
  709. * @buf: Pointer to the Key Data buffer
  710. * @len: Key Data Length
  711. * @ie: Pointer to parsed IE data
  712. * Returns: 0 on success, -1 on failure
  713. */
  714. int wpa_parse_kde_ies(const u8 *buf, size_t len, struct wpa_eapol_ie_parse *ie)
  715. {
  716. const u8 *pos, *end;
  717. int ret = 0;
  718. os_memset(ie, 0, sizeof(*ie));
  719. for (pos = buf, end = pos + len; pos + 1 < end; pos += 2 + pos[1]) {
  720. if (pos[0] == 0xdd &&
  721. ((pos == buf + len - 1) || pos[1] == 0)) {
  722. /* Ignore padding */
  723. break;
  724. }
  725. if (pos + 2 + pos[1] > end) {
  726. wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key Key Data "
  727. "underflow (ie=%d len=%d pos=%d)",
  728. pos[0], pos[1], (int) (pos - buf));
  729. wpa_hexdump_key(MSG_DEBUG, "WPA: Key Data",
  730. buf, len);
  731. ret = -1;
  732. break;
  733. }
  734. if (*pos == WLAN_EID_RSN) {
  735. ie->rsn_ie = pos;
  736. ie->rsn_ie_len = pos[1] + 2;
  737. #ifdef CONFIG_IEEE80211R
  738. } else if (*pos == WLAN_EID_MOBILITY_DOMAIN) {
  739. ie->mdie = pos;
  740. ie->mdie_len = pos[1] + 2;
  741. #endif /* CONFIG_IEEE80211R */
  742. } else if (*pos == WLAN_EID_VENDOR_SPECIFIC) {
  743. ret = wpa_parse_generic(pos, end, ie);
  744. if (ret < 0)
  745. break;
  746. if (ret > 0) {
  747. ret = 0;
  748. break;
  749. }
  750. } else {
  751. wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized EAPOL-Key "
  752. "Key Data IE", pos, 2 + pos[1]);
  753. }
  754. }
  755. return ret;
  756. }
  757. int wpa_auth_uses_mfp(struct wpa_state_machine *sm)
  758. {
  759. return sm ? sm->mgmt_frame_prot : 0;
  760. }