wpa_ie.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*
  2. * wpa_supplicant - WPA/RSN IE and KDE processing
  3. * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "wpa.h"
  11. #include "pmksa_cache.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "wpa_i.h"
  14. #include "wpa_ie.h"
  15. /**
  16. * wpa_parse_wpa_ie - Parse WPA/RSN IE
  17. * @wpa_ie: Pointer to WPA or RSN IE
  18. * @wpa_ie_len: Length of the WPA/RSN IE
  19. * @data: Pointer to data area for parsing results
  20. * Returns: 0 on success, -1 on failure
  21. *
  22. * Parse the contents of WPA or RSN IE and write the parsed data into data.
  23. */
  24. int wpa_parse_wpa_ie(const u8 *wpa_ie, size_t wpa_ie_len,
  25. struct wpa_ie_data *data)
  26. {
  27. if (wpa_ie_len >= 1 && wpa_ie[0] == WLAN_EID_RSN)
  28. return wpa_parse_wpa_ie_rsn(wpa_ie, wpa_ie_len, data);
  29. if (wpa_ie_len >= 6 && wpa_ie[0] == WLAN_EID_VENDOR_SPECIFIC &&
  30. wpa_ie[1] >= 4 && WPA_GET_BE32(&wpa_ie[2]) == OSEN_IE_VENDOR_TYPE)
  31. return wpa_parse_wpa_ie_rsn(wpa_ie, wpa_ie_len, data);
  32. else
  33. return wpa_parse_wpa_ie_wpa(wpa_ie, wpa_ie_len, data);
  34. }
  35. static int wpa_gen_wpa_ie_wpa(u8 *wpa_ie, size_t wpa_ie_len,
  36. int pairwise_cipher, int group_cipher,
  37. int key_mgmt)
  38. {
  39. u8 *pos;
  40. struct wpa_ie_hdr *hdr;
  41. u32 suite;
  42. if (wpa_ie_len < sizeof(*hdr) + WPA_SELECTOR_LEN +
  43. 2 + WPA_SELECTOR_LEN + 2 + WPA_SELECTOR_LEN)
  44. return -1;
  45. hdr = (struct wpa_ie_hdr *) wpa_ie;
  46. hdr->elem_id = WLAN_EID_VENDOR_SPECIFIC;
  47. RSN_SELECTOR_PUT(hdr->oui, WPA_OUI_TYPE);
  48. WPA_PUT_LE16(hdr->version, WPA_VERSION);
  49. pos = (u8 *) (hdr + 1);
  50. suite = wpa_cipher_to_suite(WPA_PROTO_WPA, group_cipher);
  51. if (suite == 0) {
  52. wpa_printf(MSG_WARNING, "Invalid group cipher (%d).",
  53. group_cipher);
  54. return -1;
  55. }
  56. RSN_SELECTOR_PUT(pos, suite);
  57. pos += WPA_SELECTOR_LEN;
  58. *pos++ = 1;
  59. *pos++ = 0;
  60. suite = wpa_cipher_to_suite(WPA_PROTO_WPA, pairwise_cipher);
  61. if (suite == 0 ||
  62. (!wpa_cipher_valid_pairwise(pairwise_cipher) &&
  63. pairwise_cipher != WPA_CIPHER_NONE)) {
  64. wpa_printf(MSG_WARNING, "Invalid pairwise cipher (%d).",
  65. pairwise_cipher);
  66. return -1;
  67. }
  68. RSN_SELECTOR_PUT(pos, suite);
  69. pos += WPA_SELECTOR_LEN;
  70. *pos++ = 1;
  71. *pos++ = 0;
  72. if (key_mgmt == WPA_KEY_MGMT_IEEE8021X) {
  73. RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
  74. } else if (key_mgmt == WPA_KEY_MGMT_PSK) {
  75. RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
  76. } else if (key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  77. RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_NONE);
  78. } else if (key_mgmt == WPA_KEY_MGMT_CCKM) {
  79. RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_CCKM);
  80. } else {
  81. wpa_printf(MSG_WARNING, "Invalid key management type (%d).",
  82. key_mgmt);
  83. return -1;
  84. }
  85. pos += WPA_SELECTOR_LEN;
  86. /* WPA Capabilities; use defaults, so no need to include it */
  87. hdr->len = (pos - wpa_ie) - 2;
  88. WPA_ASSERT((size_t) (pos - wpa_ie) <= wpa_ie_len);
  89. return pos - wpa_ie;
  90. }
  91. static int wpa_gen_wpa_ie_rsn(u8 *rsn_ie, size_t rsn_ie_len,
  92. int pairwise_cipher, int group_cipher,
  93. int key_mgmt, int mgmt_group_cipher,
  94. struct wpa_sm *sm)
  95. {
  96. u8 *pos;
  97. struct rsn_ie_hdr *hdr;
  98. u16 capab;
  99. u32 suite;
  100. if (rsn_ie_len < sizeof(*hdr) + RSN_SELECTOR_LEN +
  101. 2 + RSN_SELECTOR_LEN + 2 + RSN_SELECTOR_LEN + 2 +
  102. (sm->cur_pmksa ? 2 + PMKID_LEN : 0)) {
  103. wpa_printf(MSG_DEBUG, "RSN: Too short IE buffer (%lu bytes)",
  104. (unsigned long) rsn_ie_len);
  105. return -1;
  106. }
  107. hdr = (struct rsn_ie_hdr *) rsn_ie;
  108. hdr->elem_id = WLAN_EID_RSN;
  109. WPA_PUT_LE16(hdr->version, RSN_VERSION);
  110. pos = (u8 *) (hdr + 1);
  111. suite = wpa_cipher_to_suite(WPA_PROTO_RSN, group_cipher);
  112. if (suite == 0) {
  113. wpa_printf(MSG_WARNING, "Invalid group cipher (%d).",
  114. group_cipher);
  115. return -1;
  116. }
  117. RSN_SELECTOR_PUT(pos, suite);
  118. pos += RSN_SELECTOR_LEN;
  119. *pos++ = 1;
  120. *pos++ = 0;
  121. suite = wpa_cipher_to_suite(WPA_PROTO_RSN, pairwise_cipher);
  122. if (suite == 0 ||
  123. (!wpa_cipher_valid_pairwise(pairwise_cipher) &&
  124. pairwise_cipher != WPA_CIPHER_NONE)) {
  125. wpa_printf(MSG_WARNING, "Invalid pairwise cipher (%d).",
  126. pairwise_cipher);
  127. return -1;
  128. }
  129. RSN_SELECTOR_PUT(pos, suite);
  130. pos += RSN_SELECTOR_LEN;
  131. *pos++ = 1;
  132. *pos++ = 0;
  133. if (key_mgmt == WPA_KEY_MGMT_IEEE8021X) {
  134. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_UNSPEC_802_1X);
  135. } else if (key_mgmt == WPA_KEY_MGMT_PSK) {
  136. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X);
  137. } else if (key_mgmt == WPA_KEY_MGMT_CCKM) {
  138. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_CCKM);
  139. #ifdef CONFIG_IEEE80211R
  140. } else if (key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
  141. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
  142. } else if (key_mgmt == WPA_KEY_MGMT_FT_PSK) {
  143. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
  144. #endif /* CONFIG_IEEE80211R */
  145. #ifdef CONFIG_IEEE80211W
  146. } else if (key_mgmt == WPA_KEY_MGMT_IEEE8021X_SHA256) {
  147. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SHA256);
  148. } else if (key_mgmt == WPA_KEY_MGMT_PSK_SHA256) {
  149. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_SHA256);
  150. #endif /* CONFIG_IEEE80211W */
  151. #ifdef CONFIG_SAE
  152. } else if (key_mgmt == WPA_KEY_MGMT_SAE) {
  153. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_SAE);
  154. } else if (key_mgmt == WPA_KEY_MGMT_FT_SAE) {
  155. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE);
  156. #endif /* CONFIG_SAE */
  157. } else if (key_mgmt == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
  158. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192);
  159. } else if (key_mgmt == WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
  160. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SUITE_B);
  161. #ifdef CONFIG_FILS
  162. } else if (key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
  163. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FILS_SHA256);
  164. } else if (key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
  165. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FILS_SHA384);
  166. #ifdef CONFIG_IEEE80211R
  167. } else if (key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
  168. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
  169. } else if (key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
  170. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
  171. #endif /* CONFIG_IEEE80211R */
  172. #endif /* CONFIG_FILS */
  173. #ifdef CONFIG_OWE
  174. } else if (key_mgmt & WPA_KEY_MGMT_OWE) {
  175. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_OWE);
  176. #endif /* CONFIG_OWE */
  177. } else {
  178. wpa_printf(MSG_WARNING, "Invalid key management type (%d).",
  179. key_mgmt);
  180. return -1;
  181. }
  182. pos += RSN_SELECTOR_LEN;
  183. /* RSN Capabilities */
  184. capab = 0;
  185. #ifdef CONFIG_IEEE80211W
  186. if (sm->mfp)
  187. capab |= WPA_CAPABILITY_MFPC;
  188. if (sm->mfp == 2)
  189. capab |= WPA_CAPABILITY_MFPR;
  190. #endif /* CONFIG_IEEE80211W */
  191. WPA_PUT_LE16(pos, capab);
  192. pos += 2;
  193. if (sm->cur_pmksa) {
  194. /* PMKID Count (2 octets, little endian) */
  195. *pos++ = 1;
  196. *pos++ = 0;
  197. /* PMKID */
  198. os_memcpy(pos, sm->cur_pmksa->pmkid, PMKID_LEN);
  199. pos += PMKID_LEN;
  200. }
  201. #ifdef CONFIG_IEEE80211W
  202. if (wpa_cipher_valid_mgmt_group(mgmt_group_cipher)) {
  203. if (!sm->cur_pmksa) {
  204. /* PMKID Count */
  205. WPA_PUT_LE16(pos, 0);
  206. pos += 2;
  207. }
  208. /* Management Group Cipher Suite */
  209. RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
  210. mgmt_group_cipher));
  211. pos += RSN_SELECTOR_LEN;
  212. }
  213. #endif /* CONFIG_IEEE80211W */
  214. hdr->len = (pos - rsn_ie) - 2;
  215. WPA_ASSERT((size_t) (pos - rsn_ie) <= rsn_ie_len);
  216. return pos - rsn_ie;
  217. }
  218. #ifdef CONFIG_HS20
  219. static int wpa_gen_wpa_ie_osen(u8 *wpa_ie, size_t wpa_ie_len,
  220. int pairwise_cipher, int group_cipher,
  221. int key_mgmt)
  222. {
  223. u8 *pos, *len;
  224. u32 suite;
  225. if (wpa_ie_len < 2 + 4 + RSN_SELECTOR_LEN +
  226. 2 + RSN_SELECTOR_LEN + 2 + RSN_SELECTOR_LEN)
  227. return -1;
  228. pos = wpa_ie;
  229. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  230. len = pos++; /* to be filled */
  231. WPA_PUT_BE24(pos, OUI_WFA);
  232. pos += 3;
  233. *pos++ = HS20_OSEN_OUI_TYPE;
  234. /* Group Data Cipher Suite */
  235. suite = wpa_cipher_to_suite(WPA_PROTO_RSN, group_cipher);
  236. if (suite == 0) {
  237. wpa_printf(MSG_WARNING, "Invalid group cipher (%d).",
  238. group_cipher);
  239. return -1;
  240. }
  241. RSN_SELECTOR_PUT(pos, suite);
  242. pos += RSN_SELECTOR_LEN;
  243. /* Pairwise Cipher Suite Count and List */
  244. WPA_PUT_LE16(pos, 1);
  245. pos += 2;
  246. suite = wpa_cipher_to_suite(WPA_PROTO_RSN, pairwise_cipher);
  247. if (suite == 0 ||
  248. (!wpa_cipher_valid_pairwise(pairwise_cipher) &&
  249. pairwise_cipher != WPA_CIPHER_NONE)) {
  250. wpa_printf(MSG_WARNING, "Invalid pairwise cipher (%d).",
  251. pairwise_cipher);
  252. return -1;
  253. }
  254. RSN_SELECTOR_PUT(pos, suite);
  255. pos += RSN_SELECTOR_LEN;
  256. /* AKM Suite Count and List */
  257. WPA_PUT_LE16(pos, 1);
  258. pos += 2;
  259. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_OSEN);
  260. pos += RSN_SELECTOR_LEN;
  261. *len = pos - len - 1;
  262. WPA_ASSERT((size_t) (pos - wpa_ie) <= wpa_ie_len);
  263. return pos - wpa_ie;
  264. }
  265. #endif /* CONFIG_HS20 */
  266. /**
  267. * wpa_gen_wpa_ie - Generate WPA/RSN IE based on current security policy
  268. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  269. * @wpa_ie: Pointer to memory area for the generated WPA/RSN IE
  270. * @wpa_ie_len: Maximum length of the generated WPA/RSN IE
  271. * Returns: Length of the generated WPA/RSN IE or -1 on failure
  272. */
  273. int wpa_gen_wpa_ie(struct wpa_sm *sm, u8 *wpa_ie, size_t wpa_ie_len)
  274. {
  275. if (sm->proto == WPA_PROTO_RSN)
  276. return wpa_gen_wpa_ie_rsn(wpa_ie, wpa_ie_len,
  277. sm->pairwise_cipher,
  278. sm->group_cipher,
  279. sm->key_mgmt, sm->mgmt_group_cipher,
  280. sm);
  281. #ifdef CONFIG_HS20
  282. else if (sm->proto == WPA_PROTO_OSEN)
  283. return wpa_gen_wpa_ie_osen(wpa_ie, wpa_ie_len,
  284. sm->pairwise_cipher,
  285. sm->group_cipher,
  286. sm->key_mgmt);
  287. #endif /* CONFIG_HS20 */
  288. else
  289. return wpa_gen_wpa_ie_wpa(wpa_ie, wpa_ie_len,
  290. sm->pairwise_cipher,
  291. sm->group_cipher,
  292. sm->key_mgmt);
  293. }
  294. /**
  295. * wpa_parse_vendor_specific - Parse Vendor Specific IEs
  296. * @pos: Pointer to the IE header
  297. * @end: Pointer to the end of the Key Data buffer
  298. * @ie: Pointer to parsed IE data
  299. * Returns: 0 on success, 1 if end mark is found, -1 on failure
  300. */
  301. static int wpa_parse_vendor_specific(const u8 *pos, const u8 *end,
  302. struct wpa_eapol_ie_parse *ie)
  303. {
  304. unsigned int oui;
  305. if (pos[1] < 4) {
  306. wpa_printf(MSG_MSGDUMP, "Too short vendor specific IE ignored (len=%u)",
  307. pos[1]);
  308. return 1;
  309. }
  310. oui = WPA_GET_BE24(&pos[2]);
  311. if (oui == OUI_MICROSOFT && pos[5] == WMM_OUI_TYPE && pos[1] > 4) {
  312. if (pos[6] == WMM_OUI_SUBTYPE_INFORMATION_ELEMENT) {
  313. ie->wmm = &pos[2];
  314. ie->wmm_len = pos[1];
  315. wpa_hexdump(MSG_DEBUG, "WPA: WMM IE",
  316. ie->wmm, ie->wmm_len);
  317. } else if (pos[6] == WMM_OUI_SUBTYPE_PARAMETER_ELEMENT) {
  318. ie->wmm = &pos[2];
  319. ie->wmm_len = pos[1];
  320. wpa_hexdump(MSG_DEBUG, "WPA: WMM Parameter Element",
  321. ie->wmm, ie->wmm_len);
  322. }
  323. }
  324. return 0;
  325. }
  326. /**
  327. * wpa_parse_generic - Parse EAPOL-Key Key Data Generic IEs
  328. * @pos: Pointer to the IE header
  329. * @end: Pointer to the end of the Key Data buffer
  330. * @ie: Pointer to parsed IE data
  331. * Returns: 0 on success, 1 if end mark is found, -1 on failure
  332. */
  333. static int wpa_parse_generic(const u8 *pos, const u8 *end,
  334. struct wpa_eapol_ie_parse *ie)
  335. {
  336. if (pos[1] == 0)
  337. return 1;
  338. if (pos[1] >= 6 &&
  339. RSN_SELECTOR_GET(pos + 2) == WPA_OUI_TYPE &&
  340. pos[2 + WPA_SELECTOR_LEN] == 1 &&
  341. pos[2 + WPA_SELECTOR_LEN + 1] == 0) {
  342. ie->wpa_ie = pos;
  343. ie->wpa_ie_len = pos[1] + 2;
  344. wpa_hexdump(MSG_DEBUG, "WPA: WPA IE in EAPOL-Key",
  345. ie->wpa_ie, ie->wpa_ie_len);
  346. return 0;
  347. }
  348. if (1 + RSN_SELECTOR_LEN < end - pos &&
  349. pos[1] >= RSN_SELECTOR_LEN + PMKID_LEN &&
  350. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_PMKID) {
  351. ie->pmkid = pos + 2 + RSN_SELECTOR_LEN;
  352. wpa_hexdump(MSG_DEBUG, "WPA: PMKID in EAPOL-Key",
  353. pos, pos[1] + 2);
  354. return 0;
  355. }
  356. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  357. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_GROUPKEY) {
  358. ie->gtk = pos + 2 + RSN_SELECTOR_LEN;
  359. ie->gtk_len = pos[1] - RSN_SELECTOR_LEN;
  360. wpa_hexdump_key(MSG_DEBUG, "WPA: GTK in EAPOL-Key",
  361. pos, pos[1] + 2);
  362. return 0;
  363. }
  364. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  365. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_MAC_ADDR) {
  366. ie->mac_addr = pos + 2 + RSN_SELECTOR_LEN;
  367. ie->mac_addr_len = pos[1] - RSN_SELECTOR_LEN;
  368. wpa_hexdump(MSG_DEBUG, "WPA: MAC Address in EAPOL-Key",
  369. pos, pos[1] + 2);
  370. return 0;
  371. }
  372. #ifdef CONFIG_PEERKEY
  373. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  374. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_SMK) {
  375. ie->smk = pos + 2 + RSN_SELECTOR_LEN;
  376. ie->smk_len = pos[1] - RSN_SELECTOR_LEN;
  377. wpa_hexdump_key(MSG_DEBUG, "WPA: SMK in EAPOL-Key",
  378. pos, pos[1] + 2);
  379. return 0;
  380. }
  381. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  382. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_NONCE) {
  383. ie->nonce = pos + 2 + RSN_SELECTOR_LEN;
  384. ie->nonce_len = pos[1] - RSN_SELECTOR_LEN;
  385. wpa_hexdump(MSG_DEBUG, "WPA: Nonce in EAPOL-Key",
  386. pos, pos[1] + 2);
  387. return 0;
  388. }
  389. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  390. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_LIFETIME) {
  391. ie->lifetime = pos + 2 + RSN_SELECTOR_LEN;
  392. ie->lifetime_len = pos[1] - RSN_SELECTOR_LEN;
  393. wpa_hexdump(MSG_DEBUG, "WPA: Lifetime in EAPOL-Key",
  394. pos, pos[1] + 2);
  395. return 0;
  396. }
  397. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  398. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_ERROR) {
  399. ie->error = pos + 2 + RSN_SELECTOR_LEN;
  400. ie->error_len = pos[1] - RSN_SELECTOR_LEN;
  401. wpa_hexdump(MSG_DEBUG, "WPA: Error in EAPOL-Key",
  402. pos, pos[1] + 2);
  403. return 0;
  404. }
  405. #endif /* CONFIG_PEERKEY */
  406. #ifdef CONFIG_IEEE80211W
  407. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  408. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_IGTK) {
  409. ie->igtk = pos + 2 + RSN_SELECTOR_LEN;
  410. ie->igtk_len = pos[1] - RSN_SELECTOR_LEN;
  411. wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK in EAPOL-Key",
  412. pos, pos[1] + 2);
  413. return 0;
  414. }
  415. #endif /* CONFIG_IEEE80211W */
  416. #ifdef CONFIG_P2P
  417. if (pos[1] >= RSN_SELECTOR_LEN + 1 &&
  418. RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_IP_ADDR_REQ) {
  419. ie->ip_addr_req = pos + 2 + RSN_SELECTOR_LEN;
  420. wpa_hexdump(MSG_DEBUG, "WPA: IP Address Request in EAPOL-Key",
  421. ie->ip_addr_req, pos[1] - RSN_SELECTOR_LEN);
  422. return 0;
  423. }
  424. if (pos[1] >= RSN_SELECTOR_LEN + 3 * 4 &&
  425. RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_IP_ADDR_ALLOC) {
  426. ie->ip_addr_alloc = pos + 2 + RSN_SELECTOR_LEN;
  427. wpa_hexdump(MSG_DEBUG,
  428. "WPA: IP Address Allocation in EAPOL-Key",
  429. ie->ip_addr_alloc, pos[1] - RSN_SELECTOR_LEN);
  430. return 0;
  431. }
  432. #endif /* CONFIG_P2P */
  433. return 0;
  434. }
  435. /**
  436. * wpa_supplicant_parse_ies - Parse EAPOL-Key Key Data IEs
  437. * @buf: Pointer to the Key Data buffer
  438. * @len: Key Data Length
  439. * @ie: Pointer to parsed IE data
  440. * Returns: 0 on success, -1 on failure
  441. */
  442. int wpa_supplicant_parse_ies(const u8 *buf, size_t len,
  443. struct wpa_eapol_ie_parse *ie)
  444. {
  445. const u8 *pos, *end;
  446. int ret = 0;
  447. os_memset(ie, 0, sizeof(*ie));
  448. for (pos = buf, end = pos + len; end - pos > 1; pos += 2 + pos[1]) {
  449. if (pos[0] == 0xdd &&
  450. ((pos == buf + len - 1) || pos[1] == 0)) {
  451. /* Ignore padding */
  452. break;
  453. }
  454. if (2 + pos[1] > end - pos) {
  455. wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key Key Data "
  456. "underflow (ie=%d len=%d pos=%d)",
  457. pos[0], pos[1], (int) (pos - buf));
  458. wpa_hexdump_key(MSG_DEBUG, "WPA: Key Data",
  459. buf, len);
  460. ret = -1;
  461. break;
  462. }
  463. if (*pos == WLAN_EID_RSN) {
  464. ie->rsn_ie = pos;
  465. ie->rsn_ie_len = pos[1] + 2;
  466. wpa_hexdump(MSG_DEBUG, "WPA: RSN IE in EAPOL-Key",
  467. ie->rsn_ie, ie->rsn_ie_len);
  468. } else if (*pos == WLAN_EID_MOBILITY_DOMAIN &&
  469. pos[1] >= sizeof(struct rsn_mdie)) {
  470. ie->mdie = pos;
  471. ie->mdie_len = pos[1] + 2;
  472. wpa_hexdump(MSG_DEBUG, "WPA: MDIE in EAPOL-Key",
  473. ie->mdie, ie->mdie_len);
  474. } else if (*pos == WLAN_EID_FAST_BSS_TRANSITION &&
  475. pos[1] >= sizeof(struct rsn_ftie)) {
  476. ie->ftie = pos;
  477. ie->ftie_len = pos[1] + 2;
  478. wpa_hexdump(MSG_DEBUG, "WPA: FTIE in EAPOL-Key",
  479. ie->ftie, ie->ftie_len);
  480. } else if (*pos == WLAN_EID_TIMEOUT_INTERVAL && pos[1] >= 5) {
  481. if (pos[2] == WLAN_TIMEOUT_REASSOC_DEADLINE) {
  482. ie->reassoc_deadline = pos;
  483. wpa_hexdump(MSG_DEBUG, "WPA: Reassoc Deadline "
  484. "in EAPOL-Key",
  485. ie->reassoc_deadline, pos[1] + 2);
  486. } else if (pos[2] == WLAN_TIMEOUT_KEY_LIFETIME) {
  487. ie->key_lifetime = pos;
  488. wpa_hexdump(MSG_DEBUG, "WPA: KeyLifetime "
  489. "in EAPOL-Key",
  490. ie->key_lifetime, pos[1] + 2);
  491. } else {
  492. wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized "
  493. "EAPOL-Key Key Data IE",
  494. pos, 2 + pos[1]);
  495. }
  496. } else if (*pos == WLAN_EID_LINK_ID) {
  497. if (pos[1] >= 18) {
  498. ie->lnkid = pos;
  499. ie->lnkid_len = pos[1] + 2;
  500. }
  501. } else if (*pos == WLAN_EID_EXT_CAPAB) {
  502. ie->ext_capab = pos;
  503. ie->ext_capab_len = pos[1] + 2;
  504. } else if (*pos == WLAN_EID_SUPP_RATES) {
  505. ie->supp_rates = pos;
  506. ie->supp_rates_len = pos[1] + 2;
  507. } else if (*pos == WLAN_EID_EXT_SUPP_RATES) {
  508. ie->ext_supp_rates = pos;
  509. ie->ext_supp_rates_len = pos[1] + 2;
  510. } else if (*pos == WLAN_EID_HT_CAP &&
  511. pos[1] >= sizeof(struct ieee80211_ht_capabilities)) {
  512. ie->ht_capabilities = pos + 2;
  513. } else if (*pos == WLAN_EID_VHT_AID) {
  514. if (pos[1] >= 2)
  515. ie->aid = WPA_GET_LE16(pos + 2) & 0x3fff;
  516. } else if (*pos == WLAN_EID_VHT_CAP &&
  517. pos[1] >= sizeof(struct ieee80211_vht_capabilities))
  518. {
  519. ie->vht_capabilities = pos + 2;
  520. } else if (*pos == WLAN_EID_QOS && pos[1] >= 1) {
  521. ie->qosinfo = pos[2];
  522. } else if (*pos == WLAN_EID_SUPPORTED_CHANNELS) {
  523. ie->supp_channels = pos + 2;
  524. ie->supp_channels_len = pos[1];
  525. } else if (*pos == WLAN_EID_SUPPORTED_OPERATING_CLASSES) {
  526. /*
  527. * The value of the Length field of the Supported
  528. * Operating Classes element is between 2 and 253.
  529. * Silently skip invalid elements to avoid interop
  530. * issues when trying to use the value.
  531. */
  532. if (pos[1] >= 2 && pos[1] <= 253) {
  533. ie->supp_oper_classes = pos + 2;
  534. ie->supp_oper_classes_len = pos[1];
  535. }
  536. } else if (*pos == WLAN_EID_VENDOR_SPECIFIC) {
  537. ret = wpa_parse_generic(pos, end, ie);
  538. if (ret < 0)
  539. break;
  540. if (ret > 0) {
  541. ret = 0;
  542. break;
  543. }
  544. ret = wpa_parse_vendor_specific(pos, end, ie);
  545. if (ret < 0)
  546. break;
  547. if (ret > 0) {
  548. ret = 0;
  549. break;
  550. }
  551. } else {
  552. wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized EAPOL-Key "
  553. "Key Data IE", pos, 2 + pos[1]);
  554. }
  555. }
  556. return ret;
  557. }