wpa_ie.c 13 KB

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