wpa_ie.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * wpa_supplicant - WPA/RSN IE and KDE processing
  3. * Copyright (c) 2003-2008, 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. else
  30. return wpa_parse_wpa_ie_wpa(wpa_ie, wpa_ie_len, data);
  31. }
  32. static int wpa_gen_wpa_ie_wpa(u8 *wpa_ie, size_t wpa_ie_len,
  33. int pairwise_cipher, int group_cipher,
  34. int key_mgmt)
  35. {
  36. u8 *pos;
  37. struct wpa_ie_hdr *hdr;
  38. u32 suite;
  39. if (wpa_ie_len < sizeof(*hdr) + WPA_SELECTOR_LEN +
  40. 2 + WPA_SELECTOR_LEN + 2 + WPA_SELECTOR_LEN)
  41. return -1;
  42. hdr = (struct wpa_ie_hdr *) wpa_ie;
  43. hdr->elem_id = WLAN_EID_VENDOR_SPECIFIC;
  44. RSN_SELECTOR_PUT(hdr->oui, WPA_OUI_TYPE);
  45. WPA_PUT_LE16(hdr->version, WPA_VERSION);
  46. pos = (u8 *) (hdr + 1);
  47. suite = wpa_cipher_to_suite(WPA_PROTO_WPA, group_cipher);
  48. if (suite == 0) {
  49. wpa_printf(MSG_WARNING, "Invalid group cipher (%d).",
  50. group_cipher);
  51. return -1;
  52. }
  53. RSN_SELECTOR_PUT(pos, suite);
  54. pos += WPA_SELECTOR_LEN;
  55. *pos++ = 1;
  56. *pos++ = 0;
  57. suite = wpa_cipher_to_suite(WPA_PROTO_WPA, pairwise_cipher);
  58. if (suite == 0 ||
  59. (!wpa_cipher_valid_pairwise(pairwise_cipher) &&
  60. pairwise_cipher != WPA_CIPHER_NONE)) {
  61. wpa_printf(MSG_WARNING, "Invalid pairwise cipher (%d).",
  62. pairwise_cipher);
  63. return -1;
  64. }
  65. RSN_SELECTOR_PUT(pos, suite);
  66. pos += WPA_SELECTOR_LEN;
  67. *pos++ = 1;
  68. *pos++ = 0;
  69. if (key_mgmt == WPA_KEY_MGMT_IEEE8021X) {
  70. RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
  71. } else if (key_mgmt == WPA_KEY_MGMT_PSK) {
  72. RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
  73. } else if (key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  74. RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_NONE);
  75. } else if (key_mgmt == WPA_KEY_MGMT_CCKM) {
  76. RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_CCKM);
  77. } else {
  78. wpa_printf(MSG_WARNING, "Invalid key management type (%d).",
  79. key_mgmt);
  80. return -1;
  81. }
  82. pos += WPA_SELECTOR_LEN;
  83. /* WPA Capabilities; use defaults, so no need to include it */
  84. hdr->len = (pos - wpa_ie) - 2;
  85. WPA_ASSERT((size_t) (pos - wpa_ie) <= wpa_ie_len);
  86. return pos - wpa_ie;
  87. }
  88. static int wpa_gen_wpa_ie_rsn(u8 *rsn_ie, size_t rsn_ie_len,
  89. int pairwise_cipher, int group_cipher,
  90. int key_mgmt, int mgmt_group_cipher,
  91. struct wpa_sm *sm)
  92. {
  93. #ifndef CONFIG_NO_WPA2
  94. u8 *pos;
  95. struct rsn_ie_hdr *hdr;
  96. u16 capab;
  97. u32 suite;
  98. if (rsn_ie_len < sizeof(*hdr) + RSN_SELECTOR_LEN +
  99. 2 + RSN_SELECTOR_LEN + 2 + RSN_SELECTOR_LEN + 2 +
  100. (sm->cur_pmksa ? 2 + PMKID_LEN : 0)) {
  101. wpa_printf(MSG_DEBUG, "RSN: Too short IE buffer (%lu bytes)",
  102. (unsigned long) rsn_ie_len);
  103. return -1;
  104. }
  105. hdr = (struct rsn_ie_hdr *) rsn_ie;
  106. hdr->elem_id = WLAN_EID_RSN;
  107. WPA_PUT_LE16(hdr->version, RSN_VERSION);
  108. pos = (u8 *) (hdr + 1);
  109. suite = wpa_cipher_to_suite(WPA_PROTO_RSN, group_cipher);
  110. if (suite == 0) {
  111. wpa_printf(MSG_WARNING, "Invalid group cipher (%d).",
  112. group_cipher);
  113. return -1;
  114. }
  115. RSN_SELECTOR_PUT(pos, suite);
  116. pos += RSN_SELECTOR_LEN;
  117. *pos++ = 1;
  118. *pos++ = 0;
  119. suite = wpa_cipher_to_suite(WPA_PROTO_RSN, pairwise_cipher);
  120. if (suite == 0 ||
  121. (!wpa_cipher_valid_pairwise(pairwise_cipher) &&
  122. pairwise_cipher != WPA_CIPHER_NONE)) {
  123. wpa_printf(MSG_WARNING, "Invalid pairwise cipher (%d).",
  124. pairwise_cipher);
  125. return -1;
  126. }
  127. RSN_SELECTOR_PUT(pos, suite);
  128. pos += RSN_SELECTOR_LEN;
  129. *pos++ = 1;
  130. *pos++ = 0;
  131. if (key_mgmt == WPA_KEY_MGMT_IEEE8021X) {
  132. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_UNSPEC_802_1X);
  133. } else if (key_mgmt == WPA_KEY_MGMT_PSK) {
  134. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X);
  135. } else if (key_mgmt == WPA_KEY_MGMT_CCKM) {
  136. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_CCKM);
  137. #ifdef CONFIG_IEEE80211R
  138. } else if (key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
  139. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
  140. } else if (key_mgmt == WPA_KEY_MGMT_FT_PSK) {
  141. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
  142. #endif /* CONFIG_IEEE80211R */
  143. #ifdef CONFIG_IEEE80211W
  144. } else if (key_mgmt == WPA_KEY_MGMT_IEEE8021X_SHA256) {
  145. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SHA256);
  146. } else if (key_mgmt == WPA_KEY_MGMT_PSK_SHA256) {
  147. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_SHA256);
  148. #endif /* CONFIG_IEEE80211W */
  149. #ifdef CONFIG_SAE
  150. } else if (key_mgmt == WPA_KEY_MGMT_SAE) {
  151. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_SAE);
  152. } else if (key_mgmt == WPA_KEY_MGMT_FT_SAE) {
  153. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE);
  154. #endif /* CONFIG_SAE */
  155. } else {
  156. wpa_printf(MSG_WARNING, "Invalid key management type (%d).",
  157. key_mgmt);
  158. return -1;
  159. }
  160. pos += RSN_SELECTOR_LEN;
  161. /* RSN Capabilities */
  162. capab = 0;
  163. #ifdef CONFIG_IEEE80211W
  164. if (sm->mfp)
  165. capab |= WPA_CAPABILITY_MFPC;
  166. if (sm->mfp == 2)
  167. capab |= WPA_CAPABILITY_MFPR;
  168. #endif /* CONFIG_IEEE80211W */
  169. WPA_PUT_LE16(pos, capab);
  170. pos += 2;
  171. if (sm->cur_pmksa) {
  172. /* PMKID Count (2 octets, little endian) */
  173. *pos++ = 1;
  174. *pos++ = 0;
  175. /* PMKID */
  176. os_memcpy(pos, sm->cur_pmksa->pmkid, PMKID_LEN);
  177. pos += PMKID_LEN;
  178. }
  179. #ifdef CONFIG_IEEE80211W
  180. if (mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
  181. if (!sm->cur_pmksa) {
  182. /* PMKID Count */
  183. WPA_PUT_LE16(pos, 0);
  184. pos += 2;
  185. }
  186. /* Management Group Cipher Suite */
  187. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
  188. pos += RSN_SELECTOR_LEN;
  189. }
  190. #endif /* CONFIG_IEEE80211W */
  191. hdr->len = (pos - rsn_ie) - 2;
  192. WPA_ASSERT((size_t) (pos - rsn_ie) <= rsn_ie_len);
  193. return pos - rsn_ie;
  194. #else /* CONFIG_NO_WPA2 */
  195. return -1;
  196. #endif /* CONFIG_NO_WPA2 */
  197. }
  198. /**
  199. * wpa_gen_wpa_ie - Generate WPA/RSN IE based on current security policy
  200. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  201. * @wpa_ie: Pointer to memory area for the generated WPA/RSN IE
  202. * @wpa_ie_len: Maximum length of the generated WPA/RSN IE
  203. * Returns: Length of the generated WPA/RSN IE or -1 on failure
  204. */
  205. int wpa_gen_wpa_ie(struct wpa_sm *sm, u8 *wpa_ie, size_t wpa_ie_len)
  206. {
  207. if (sm->proto == WPA_PROTO_RSN)
  208. return wpa_gen_wpa_ie_rsn(wpa_ie, wpa_ie_len,
  209. sm->pairwise_cipher,
  210. sm->group_cipher,
  211. sm->key_mgmt, sm->mgmt_group_cipher,
  212. sm);
  213. else
  214. return wpa_gen_wpa_ie_wpa(wpa_ie, wpa_ie_len,
  215. sm->pairwise_cipher,
  216. sm->group_cipher,
  217. sm->key_mgmt);
  218. }
  219. /**
  220. * wpa_parse_generic - Parse EAPOL-Key Key Data Generic IEs
  221. * @pos: Pointer to the IE header
  222. * @end: Pointer to the end of the Key Data buffer
  223. * @ie: Pointer to parsed IE data
  224. * Returns: 0 on success, 1 if end mark is found, -1 on failure
  225. */
  226. static int wpa_parse_generic(const u8 *pos, const u8 *end,
  227. struct wpa_eapol_ie_parse *ie)
  228. {
  229. if (pos[1] == 0)
  230. return 1;
  231. if (pos[1] >= 6 &&
  232. RSN_SELECTOR_GET(pos + 2) == WPA_OUI_TYPE &&
  233. pos[2 + WPA_SELECTOR_LEN] == 1 &&
  234. pos[2 + WPA_SELECTOR_LEN + 1] == 0) {
  235. ie->wpa_ie = pos;
  236. ie->wpa_ie_len = pos[1] + 2;
  237. wpa_hexdump(MSG_DEBUG, "WPA: WPA IE in EAPOL-Key",
  238. ie->wpa_ie, ie->wpa_ie_len);
  239. return 0;
  240. }
  241. if (pos + 1 + RSN_SELECTOR_LEN < end &&
  242. pos[1] >= RSN_SELECTOR_LEN + PMKID_LEN &&
  243. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_PMKID) {
  244. ie->pmkid = pos + 2 + RSN_SELECTOR_LEN;
  245. wpa_hexdump(MSG_DEBUG, "WPA: PMKID in EAPOL-Key",
  246. pos, pos[1] + 2);
  247. return 0;
  248. }
  249. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  250. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_GROUPKEY) {
  251. ie->gtk = pos + 2 + RSN_SELECTOR_LEN;
  252. ie->gtk_len = pos[1] - RSN_SELECTOR_LEN;
  253. wpa_hexdump_key(MSG_DEBUG, "WPA: GTK in EAPOL-Key",
  254. pos, pos[1] + 2);
  255. return 0;
  256. }
  257. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  258. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_MAC_ADDR) {
  259. ie->mac_addr = pos + 2 + RSN_SELECTOR_LEN;
  260. ie->mac_addr_len = pos[1] - RSN_SELECTOR_LEN;
  261. wpa_hexdump(MSG_DEBUG, "WPA: MAC Address in EAPOL-Key",
  262. pos, pos[1] + 2);
  263. return 0;
  264. }
  265. #ifdef CONFIG_PEERKEY
  266. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  267. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_SMK) {
  268. ie->smk = pos + 2 + RSN_SELECTOR_LEN;
  269. ie->smk_len = pos[1] - RSN_SELECTOR_LEN;
  270. wpa_hexdump_key(MSG_DEBUG, "WPA: SMK in EAPOL-Key",
  271. pos, pos[1] + 2);
  272. return 0;
  273. }
  274. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  275. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_NONCE) {
  276. ie->nonce = pos + 2 + RSN_SELECTOR_LEN;
  277. ie->nonce_len = pos[1] - RSN_SELECTOR_LEN;
  278. wpa_hexdump(MSG_DEBUG, "WPA: Nonce in EAPOL-Key",
  279. pos, pos[1] + 2);
  280. return 0;
  281. }
  282. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  283. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_LIFETIME) {
  284. ie->lifetime = pos + 2 + RSN_SELECTOR_LEN;
  285. ie->lifetime_len = pos[1] - RSN_SELECTOR_LEN;
  286. wpa_hexdump(MSG_DEBUG, "WPA: Lifetime in EAPOL-Key",
  287. pos, pos[1] + 2);
  288. return 0;
  289. }
  290. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  291. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_ERROR) {
  292. ie->error = pos + 2 + RSN_SELECTOR_LEN;
  293. ie->error_len = pos[1] - RSN_SELECTOR_LEN;
  294. wpa_hexdump(MSG_DEBUG, "WPA: Error in EAPOL-Key",
  295. pos, pos[1] + 2);
  296. return 0;
  297. }
  298. #endif /* CONFIG_PEERKEY */
  299. #ifdef CONFIG_IEEE80211W
  300. if (pos[1] > RSN_SELECTOR_LEN + 2 &&
  301. RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_IGTK) {
  302. ie->igtk = pos + 2 + RSN_SELECTOR_LEN;
  303. ie->igtk_len = pos[1] - RSN_SELECTOR_LEN;
  304. wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK in EAPOL-Key",
  305. pos, pos[1] + 2);
  306. return 0;
  307. }
  308. #endif /* CONFIG_IEEE80211W */
  309. return 0;
  310. }
  311. /**
  312. * wpa_supplicant_parse_ies - Parse EAPOL-Key Key Data IEs
  313. * @buf: Pointer to the Key Data buffer
  314. * @len: Key Data Length
  315. * @ie: Pointer to parsed IE data
  316. * Returns: 0 on success, -1 on failure
  317. */
  318. int wpa_supplicant_parse_ies(const u8 *buf, size_t len,
  319. struct wpa_eapol_ie_parse *ie)
  320. {
  321. const u8 *pos, *end;
  322. int ret = 0;
  323. os_memset(ie, 0, sizeof(*ie));
  324. for (pos = buf, end = pos + len; pos + 1 < end; pos += 2 + pos[1]) {
  325. if (pos[0] == 0xdd &&
  326. ((pos == buf + len - 1) || pos[1] == 0)) {
  327. /* Ignore padding */
  328. break;
  329. }
  330. if (pos + 2 + pos[1] > end) {
  331. wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key Key Data "
  332. "underflow (ie=%d len=%d pos=%d)",
  333. pos[0], pos[1], (int) (pos - buf));
  334. wpa_hexdump_key(MSG_DEBUG, "WPA: Key Data",
  335. buf, len);
  336. ret = -1;
  337. break;
  338. }
  339. if (*pos == WLAN_EID_RSN) {
  340. ie->rsn_ie = pos;
  341. ie->rsn_ie_len = pos[1] + 2;
  342. wpa_hexdump(MSG_DEBUG, "WPA: RSN IE in EAPOL-Key",
  343. ie->rsn_ie, ie->rsn_ie_len);
  344. } else if (*pos == WLAN_EID_MOBILITY_DOMAIN) {
  345. ie->mdie = pos;
  346. ie->mdie_len = pos[1] + 2;
  347. wpa_hexdump(MSG_DEBUG, "WPA: MDIE in EAPOL-Key",
  348. ie->mdie, ie->mdie_len);
  349. } else if (*pos == WLAN_EID_FAST_BSS_TRANSITION) {
  350. ie->ftie = pos;
  351. ie->ftie_len = pos[1] + 2;
  352. wpa_hexdump(MSG_DEBUG, "WPA: FTIE in EAPOL-Key",
  353. ie->ftie, ie->ftie_len);
  354. } else if (*pos == WLAN_EID_TIMEOUT_INTERVAL && pos[1] >= 5) {
  355. if (pos[2] == WLAN_TIMEOUT_REASSOC_DEADLINE) {
  356. ie->reassoc_deadline = pos;
  357. wpa_hexdump(MSG_DEBUG, "WPA: Reassoc Deadline "
  358. "in EAPOL-Key",
  359. ie->reassoc_deadline, pos[1] + 2);
  360. } else if (pos[2] == WLAN_TIMEOUT_KEY_LIFETIME) {
  361. ie->key_lifetime = pos;
  362. wpa_hexdump(MSG_DEBUG, "WPA: KeyLifetime "
  363. "in EAPOL-Key",
  364. ie->key_lifetime, pos[1] + 2);
  365. } else {
  366. wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized "
  367. "EAPOL-Key Key Data IE",
  368. pos, 2 + pos[1]);
  369. }
  370. } else if (*pos == WLAN_EID_LINK_ID) {
  371. if (pos[1] >= 18) {
  372. ie->lnkid = pos;
  373. ie->lnkid_len = pos[1] + 2;
  374. }
  375. } else if (*pos == WLAN_EID_EXT_CAPAB) {
  376. ie->ext_capab = pos;
  377. ie->ext_capab_len = pos[1] + 2;
  378. } else if (*pos == WLAN_EID_SUPP_RATES) {
  379. ie->supp_rates = pos;
  380. ie->supp_rates_len = pos[1] + 2;
  381. } else if (*pos == WLAN_EID_EXT_SUPP_RATES) {
  382. ie->ext_supp_rates = pos;
  383. ie->ext_supp_rates_len = pos[1] + 2;
  384. } else if (*pos == WLAN_EID_HT_CAP) {
  385. ie->ht_capabilities = pos + 2;
  386. ie->ht_capabilities_len = pos[1];
  387. } else if (*pos == WLAN_EID_VHT_CAP) {
  388. ie->vht_capabilities = pos + 2;
  389. ie->vht_capabilities_len = pos[1];
  390. } else if (*pos == WLAN_EID_QOS && pos[1] >= 1) {
  391. ie->qosinfo = pos[2];
  392. } else if (*pos == WLAN_EID_VENDOR_SPECIFIC) {
  393. ret = wpa_parse_generic(pos, end, ie);
  394. if (ret < 0)
  395. break;
  396. if (ret > 0) {
  397. ret = 0;
  398. break;
  399. }
  400. } else {
  401. wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized EAPOL-Key "
  402. "Key Data IE", pos, 2 + pos[1]);
  403. }
  404. }
  405. return ret;
  406. }