wpa_ft.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /*
  2. * WPA Supplicant - IEEE 802.11r - Fast BSS Transition
  3. * Copyright (c) 2006-2007, 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 "crypto/aes_wrap.h"
  11. #include "crypto/random.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "common/ieee802_11_common.h"
  14. #include "wpa.h"
  15. #include "wpa_i.h"
  16. #ifdef CONFIG_IEEE80211R
  17. int wpa_derive_ptk_ft(struct wpa_sm *sm, const unsigned char *src_addr,
  18. const struct wpa_eapol_key *key,
  19. struct wpa_ptk *ptk, size_t ptk_len)
  20. {
  21. u8 ptk_name[WPA_PMK_NAME_LEN];
  22. const u8 *anonce = key->key_nonce;
  23. if (sm->xxkey_len == 0) {
  24. wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
  25. "derivation");
  26. return -1;
  27. }
  28. wpa_derive_pmk_r0(sm->xxkey, sm->xxkey_len, sm->ssid,
  29. sm->ssid_len, sm->mobility_domain,
  30. sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
  31. sm->pmk_r0, sm->pmk_r0_name);
  32. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", sm->pmk_r0, PMK_LEN);
  33. wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name",
  34. sm->pmk_r0_name, WPA_PMK_NAME_LEN);
  35. wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_name, sm->r1kh_id,
  36. sm->own_addr, sm->pmk_r1, sm->pmk_r1_name);
  37. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", sm->pmk_r1, PMK_LEN);
  38. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", sm->pmk_r1_name,
  39. WPA_PMK_NAME_LEN);
  40. wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->snonce, anonce, sm->own_addr,
  41. sm->bssid, sm->pmk_r1_name,
  42. (u8 *) ptk, ptk_len, ptk_name);
  43. wpa_hexdump_key(MSG_DEBUG, "FT: PTK", (u8 *) ptk, ptk_len);
  44. wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
  45. return 0;
  46. }
  47. /**
  48. * wpa_sm_set_ft_params - Set FT (IEEE 802.11r) parameters
  49. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  50. * @ies: Association Response IEs or %NULL to clear FT parameters
  51. * @ies_len: Length of ies buffer in octets
  52. * Returns: 0 on success, -1 on failure
  53. */
  54. int wpa_sm_set_ft_params(struct wpa_sm *sm, const u8 *ies, size_t ies_len)
  55. {
  56. struct wpa_ft_ies ft;
  57. if (sm == NULL)
  58. return 0;
  59. if (wpa_ft_parse_ies(ies, ies_len, &ft) < 0)
  60. return -1;
  61. if (ft.mdie && ft.mdie_len < MOBILITY_DOMAIN_ID_LEN + 1)
  62. return -1;
  63. if (ft.mdie) {
  64. wpa_hexdump(MSG_DEBUG, "FT: Mobility domain",
  65. ft.mdie, MOBILITY_DOMAIN_ID_LEN);
  66. os_memcpy(sm->mobility_domain, ft.mdie,
  67. MOBILITY_DOMAIN_ID_LEN);
  68. sm->mdie_ft_capab = ft.mdie[MOBILITY_DOMAIN_ID_LEN];
  69. wpa_printf(MSG_DEBUG, "FT: Capability and Policy: 0x%02x",
  70. sm->mdie_ft_capab);
  71. } else
  72. os_memset(sm->mobility_domain, 0, MOBILITY_DOMAIN_ID_LEN);
  73. if (ft.r0kh_id) {
  74. wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID",
  75. ft.r0kh_id, ft.r0kh_id_len);
  76. os_memcpy(sm->r0kh_id, ft.r0kh_id, ft.r0kh_id_len);
  77. sm->r0kh_id_len = ft.r0kh_id_len;
  78. } else {
  79. /* FIX: When should R0KH-ID be cleared? We need to keep the
  80. * old R0KH-ID in order to be able to use this during FT. */
  81. /*
  82. * os_memset(sm->r0kh_id, 0, FT_R0KH_ID_LEN);
  83. * sm->r0kh_id_len = 0;
  84. */
  85. }
  86. if (ft.r1kh_id) {
  87. wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID",
  88. ft.r1kh_id, FT_R1KH_ID_LEN);
  89. os_memcpy(sm->r1kh_id, ft.r1kh_id, FT_R1KH_ID_LEN);
  90. } else
  91. os_memset(sm->r1kh_id, 0, FT_R1KH_ID_LEN);
  92. os_free(sm->assoc_resp_ies);
  93. sm->assoc_resp_ies = os_malloc(ft.mdie_len + 2 + ft.ftie_len + 2);
  94. if (sm->assoc_resp_ies) {
  95. u8 *pos = sm->assoc_resp_ies;
  96. if (ft.mdie) {
  97. os_memcpy(pos, ft.mdie - 2, ft.mdie_len + 2);
  98. pos += ft.mdie_len + 2;
  99. }
  100. if (ft.ftie) {
  101. os_memcpy(pos, ft.ftie - 2, ft.ftie_len + 2);
  102. pos += ft.ftie_len + 2;
  103. }
  104. sm->assoc_resp_ies_len = pos - sm->assoc_resp_ies;
  105. wpa_hexdump(MSG_DEBUG, "FT: Stored MDIE and FTIE from "
  106. "(Re)Association Response",
  107. sm->assoc_resp_ies, sm->assoc_resp_ies_len);
  108. }
  109. return 0;
  110. }
  111. /**
  112. * wpa_ft_gen_req_ies - Generate FT (IEEE 802.11r) IEs for Auth/ReAssoc Request
  113. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  114. * @len: Buffer for returning the length of the IEs
  115. * @anonce: ANonce or %NULL if not yet available
  116. * @pmk_name: PMKR0Name or PMKR1Name to be added into the RSN IE PMKID List
  117. * @kck: 128-bit KCK for MIC or %NULL if no MIC is used
  118. * @target_ap: Target AP address
  119. * @ric_ies: Optional IE(s), e.g., WMM TSPEC(s), for RIC-Request or %NULL
  120. * @ric_ies_len: Length of ric_ies buffer in octets
  121. * @ap_mdie: Mobility Domain IE from the target AP
  122. * Returns: Pointer to buffer with IEs or %NULL on failure
  123. *
  124. * Caller is responsible for freeing the returned buffer with os_free();
  125. */
  126. static u8 * wpa_ft_gen_req_ies(struct wpa_sm *sm, size_t *len,
  127. const u8 *anonce, const u8 *pmk_name,
  128. const u8 *kck, const u8 *target_ap,
  129. const u8 *ric_ies, size_t ric_ies_len,
  130. const u8 *ap_mdie)
  131. {
  132. size_t buf_len;
  133. u8 *buf, *pos, *ftie_len, *ftie_pos;
  134. struct rsn_mdie *mdie;
  135. struct rsn_ftie *ftie;
  136. struct rsn_ie_hdr *rsnie;
  137. u16 capab;
  138. sm->ft_completed = 0;
  139. buf_len = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
  140. 2 + sm->r0kh_id_len + ric_ies_len + 100;
  141. buf = os_zalloc(buf_len);
  142. if (buf == NULL)
  143. return NULL;
  144. pos = buf;
  145. /* RSNIE[PMKR0Name/PMKR1Name] */
  146. rsnie = (struct rsn_ie_hdr *) pos;
  147. rsnie->elem_id = WLAN_EID_RSN;
  148. WPA_PUT_LE16(rsnie->version, RSN_VERSION);
  149. pos = (u8 *) (rsnie + 1);
  150. /* Group Suite Selector */
  151. if (sm->group_cipher != WPA_CIPHER_CCMP &&
  152. sm->group_cipher != WPA_CIPHER_GCMP &&
  153. sm->group_cipher != WPA_CIPHER_TKIP) {
  154. wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
  155. sm->group_cipher);
  156. os_free(buf);
  157. return NULL;
  158. }
  159. RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
  160. sm->group_cipher));
  161. pos += RSN_SELECTOR_LEN;
  162. /* Pairwise Suite Count */
  163. WPA_PUT_LE16(pos, 1);
  164. pos += 2;
  165. /* Pairwise Suite List */
  166. if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
  167. wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
  168. sm->pairwise_cipher);
  169. os_free(buf);
  170. return NULL;
  171. }
  172. RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
  173. sm->pairwise_cipher));
  174. pos += RSN_SELECTOR_LEN;
  175. /* Authenticated Key Management Suite Count */
  176. WPA_PUT_LE16(pos, 1);
  177. pos += 2;
  178. /* Authenticated Key Management Suite List */
  179. if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X)
  180. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
  181. else if (sm->key_mgmt == WPA_KEY_MGMT_FT_PSK)
  182. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
  183. else {
  184. wpa_printf(MSG_WARNING, "FT: Invalid key management type (%d)",
  185. sm->key_mgmt);
  186. os_free(buf);
  187. return NULL;
  188. }
  189. pos += RSN_SELECTOR_LEN;
  190. /* RSN Capabilities */
  191. capab = 0;
  192. #ifdef CONFIG_IEEE80211W
  193. if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC)
  194. capab |= WPA_CAPABILITY_MFPC;
  195. #endif /* CONFIG_IEEE80211W */
  196. WPA_PUT_LE16(pos, capab);
  197. pos += 2;
  198. /* PMKID Count */
  199. WPA_PUT_LE16(pos, 1);
  200. pos += 2;
  201. /* PMKID List [PMKR0Name/PMKR1Name] */
  202. os_memcpy(pos, pmk_name, WPA_PMK_NAME_LEN);
  203. pos += WPA_PMK_NAME_LEN;
  204. #ifdef CONFIG_IEEE80211W
  205. if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
  206. /* Management Group Cipher Suite */
  207. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
  208. pos += RSN_SELECTOR_LEN;
  209. }
  210. #endif /* CONFIG_IEEE80211W */
  211. rsnie->len = (pos - (u8 *) rsnie) - 2;
  212. /* MDIE */
  213. *pos++ = WLAN_EID_MOBILITY_DOMAIN;
  214. *pos++ = sizeof(*mdie);
  215. mdie = (struct rsn_mdie *) pos;
  216. pos += sizeof(*mdie);
  217. os_memcpy(mdie->mobility_domain, sm->mobility_domain,
  218. MOBILITY_DOMAIN_ID_LEN);
  219. mdie->ft_capab = ap_mdie && ap_mdie[1] >= 3 ? ap_mdie[4] :
  220. sm->mdie_ft_capab;
  221. /* FTIE[SNonce, [R1KH-ID,] R0KH-ID ] */
  222. ftie_pos = pos;
  223. *pos++ = WLAN_EID_FAST_BSS_TRANSITION;
  224. ftie_len = pos++;
  225. ftie = (struct rsn_ftie *) pos;
  226. pos += sizeof(*ftie);
  227. os_memcpy(ftie->snonce, sm->snonce, WPA_NONCE_LEN);
  228. if (anonce)
  229. os_memcpy(ftie->anonce, anonce, WPA_NONCE_LEN);
  230. if (kck) {
  231. /* R1KH-ID sub-element in third FT message */
  232. *pos++ = FTIE_SUBELEM_R1KH_ID;
  233. *pos++ = FT_R1KH_ID_LEN;
  234. os_memcpy(pos, sm->r1kh_id, FT_R1KH_ID_LEN);
  235. pos += FT_R1KH_ID_LEN;
  236. }
  237. /* R0KH-ID sub-element */
  238. *pos++ = FTIE_SUBELEM_R0KH_ID;
  239. *pos++ = sm->r0kh_id_len;
  240. os_memcpy(pos, sm->r0kh_id, sm->r0kh_id_len);
  241. pos += sm->r0kh_id_len;
  242. *ftie_len = pos - ftie_len - 1;
  243. if (ric_ies) {
  244. /* RIC Request */
  245. os_memcpy(pos, ric_ies, ric_ies_len);
  246. pos += ric_ies_len;
  247. }
  248. if (kck) {
  249. /*
  250. * IEEE Std 802.11r-2008, 11A.8.4
  251. * MIC shall be calculated over:
  252. * non-AP STA MAC address
  253. * Target AP MAC address
  254. * Transaction seq number (5 for ReassocReq, 3 otherwise)
  255. * RSN IE
  256. * MDIE
  257. * FTIE (with MIC field set to 0)
  258. * RIC-Request (if present)
  259. */
  260. /* Information element count */
  261. ftie->mic_control[1] = 3 + ieee802_11_ie_count(ric_ies,
  262. ric_ies_len);
  263. if (wpa_ft_mic(kck, sm->own_addr, target_ap, 5,
  264. ((u8 *) mdie) - 2, 2 + sizeof(*mdie),
  265. ftie_pos, 2 + *ftie_len,
  266. (u8 *) rsnie, 2 + rsnie->len, ric_ies,
  267. ric_ies_len, ftie->mic) < 0) {
  268. wpa_printf(MSG_INFO, "FT: Failed to calculate MIC");
  269. os_free(buf);
  270. return NULL;
  271. }
  272. }
  273. *len = pos - buf;
  274. return buf;
  275. }
  276. static int wpa_ft_install_ptk(struct wpa_sm *sm, const u8 *bssid)
  277. {
  278. int keylen;
  279. enum wpa_alg alg;
  280. u8 null_rsc[6] = { 0, 0, 0, 0, 0, 0 };
  281. wpa_printf(MSG_DEBUG, "FT: Installing PTK to the driver.");
  282. if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
  283. wpa_printf(MSG_WARNING, "FT: Unsupported pairwise cipher %d",
  284. sm->pairwise_cipher);
  285. return -1;
  286. }
  287. alg = wpa_cipher_to_alg(sm->pairwise_cipher);
  288. keylen = wpa_cipher_key_len(sm->pairwise_cipher);
  289. if (wpa_sm_set_key(sm, alg, bssid, 0, 1, null_rsc,
  290. sizeof(null_rsc), (u8 *) sm->ptk.tk1, keylen) < 0) {
  291. wpa_printf(MSG_WARNING, "FT: Failed to set PTK to the driver");
  292. return -1;
  293. }
  294. return 0;
  295. }
  296. /**
  297. * wpa_ft_prepare_auth_request - Generate over-the-air auth request
  298. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  299. * @mdie: Target AP MDIE
  300. * Returns: 0 on success, -1 on failure
  301. */
  302. int wpa_ft_prepare_auth_request(struct wpa_sm *sm, const u8 *mdie)
  303. {
  304. u8 *ft_ies;
  305. size_t ft_ies_len;
  306. /* Generate a new SNonce */
  307. if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
  308. wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
  309. return -1;
  310. }
  311. ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
  312. NULL, sm->bssid, NULL, 0, mdie);
  313. if (ft_ies) {
  314. wpa_sm_update_ft_ies(sm, sm->mobility_domain,
  315. ft_ies, ft_ies_len);
  316. os_free(ft_ies);
  317. }
  318. return 0;
  319. }
  320. int wpa_ft_process_response(struct wpa_sm *sm, const u8 *ies, size_t ies_len,
  321. int ft_action, const u8 *target_ap,
  322. const u8 *ric_ies, size_t ric_ies_len)
  323. {
  324. u8 *ft_ies;
  325. size_t ft_ies_len, ptk_len;
  326. struct wpa_ft_ies parse;
  327. struct rsn_mdie *mdie;
  328. struct rsn_ftie *ftie;
  329. u8 ptk_name[WPA_PMK_NAME_LEN];
  330. int ret;
  331. const u8 *bssid;
  332. wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
  333. wpa_hexdump(MSG_DEBUG, "FT: RIC IEs", ric_ies, ric_ies_len);
  334. if (ft_action) {
  335. if (!sm->over_the_ds_in_progress) {
  336. wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
  337. "- drop FT Action Response");
  338. return -1;
  339. }
  340. if (os_memcmp(target_ap, sm->target_ap, ETH_ALEN) != 0) {
  341. wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
  342. "with this Target AP - drop FT Action "
  343. "Response");
  344. return -1;
  345. }
  346. }
  347. if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
  348. sm->key_mgmt != WPA_KEY_MGMT_FT_PSK) {
  349. wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
  350. "enabled for this connection");
  351. return -1;
  352. }
  353. if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
  354. wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
  355. return -1;
  356. }
  357. mdie = (struct rsn_mdie *) parse.mdie;
  358. if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
  359. os_memcmp(mdie->mobility_domain, sm->mobility_domain,
  360. MOBILITY_DOMAIN_ID_LEN) != 0) {
  361. wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
  362. return -1;
  363. }
  364. ftie = (struct rsn_ftie *) parse.ftie;
  365. if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
  366. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
  367. return -1;
  368. }
  369. if (os_memcmp(ftie->snonce, sm->snonce, WPA_NONCE_LEN) != 0) {
  370. wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
  371. wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
  372. ftie->snonce, WPA_NONCE_LEN);
  373. wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
  374. sm->snonce, WPA_NONCE_LEN);
  375. return -1;
  376. }
  377. if (parse.r0kh_id == NULL) {
  378. wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
  379. return -1;
  380. }
  381. if (parse.r0kh_id_len != sm->r0kh_id_len ||
  382. os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) {
  383. wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
  384. "the current R0KH-ID");
  385. wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
  386. parse.r0kh_id, parse.r0kh_id_len);
  387. wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
  388. sm->r0kh_id, sm->r0kh_id_len);
  389. return -1;
  390. }
  391. if (parse.r1kh_id == NULL) {
  392. wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
  393. return -1;
  394. }
  395. if (parse.rsn_pmkid == NULL ||
  396. os_memcmp(parse.rsn_pmkid, sm->pmk_r0_name, WPA_PMK_NAME_LEN)) {
  397. wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name (PMKID) in "
  398. "RSNIE");
  399. return -1;
  400. }
  401. os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
  402. wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID", sm->r1kh_id, FT_R1KH_ID_LEN);
  403. wpa_hexdump(MSG_DEBUG, "FT: SNonce", sm->snonce, WPA_NONCE_LEN);
  404. wpa_hexdump(MSG_DEBUG, "FT: ANonce", ftie->anonce, WPA_NONCE_LEN);
  405. os_memcpy(sm->anonce, ftie->anonce, WPA_NONCE_LEN);
  406. wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_name, sm->r1kh_id,
  407. sm->own_addr, sm->pmk_r1, sm->pmk_r1_name);
  408. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", sm->pmk_r1, PMK_LEN);
  409. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name",
  410. sm->pmk_r1_name, WPA_PMK_NAME_LEN);
  411. bssid = target_ap;
  412. ptk_len = sm->pairwise_cipher != WPA_CIPHER_TKIP ? 48 : 64;
  413. wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->snonce, ftie->anonce, sm->own_addr,
  414. bssid, sm->pmk_r1_name,
  415. (u8 *) &sm->ptk, ptk_len, ptk_name);
  416. wpa_hexdump_key(MSG_DEBUG, "FT: PTK",
  417. (u8 *) &sm->ptk, ptk_len);
  418. wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
  419. ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, ftie->anonce,
  420. sm->pmk_r1_name, sm->ptk.kck, bssid,
  421. ric_ies, ric_ies_len,
  422. parse.mdie ? parse.mdie - 2 : NULL);
  423. if (ft_ies) {
  424. wpa_sm_update_ft_ies(sm, sm->mobility_domain,
  425. ft_ies, ft_ies_len);
  426. os_free(ft_ies);
  427. }
  428. wpa_sm_mark_authenticated(sm, bssid);
  429. ret = wpa_ft_install_ptk(sm, bssid);
  430. if (ret) {
  431. /*
  432. * Some drivers do not support key configuration when we are
  433. * not associated with the target AP. Work around this by
  434. * trying again after the following reassociation gets
  435. * completed.
  436. */
  437. wpa_printf(MSG_DEBUG, "FT: Failed to set PTK prior to "
  438. "association - try again after reassociation");
  439. sm->set_ptk_after_assoc = 1;
  440. } else
  441. sm->set_ptk_after_assoc = 0;
  442. sm->ft_completed = 1;
  443. if (ft_action) {
  444. /*
  445. * The caller is expected trigger re-association with the
  446. * Target AP.
  447. */
  448. os_memcpy(sm->bssid, target_ap, ETH_ALEN);
  449. }
  450. return 0;
  451. }
  452. int wpa_ft_is_completed(struct wpa_sm *sm)
  453. {
  454. if (sm == NULL)
  455. return 0;
  456. if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
  457. sm->key_mgmt != WPA_KEY_MGMT_FT_PSK)
  458. return 0;
  459. return sm->ft_completed;
  460. }
  461. static int wpa_ft_process_gtk_subelem(struct wpa_sm *sm, const u8 *gtk_elem,
  462. size_t gtk_elem_len)
  463. {
  464. u8 gtk[32];
  465. int keyidx;
  466. enum wpa_alg alg;
  467. size_t gtk_len, keylen, rsc_len;
  468. if (gtk_elem == NULL) {
  469. wpa_printf(MSG_DEBUG, "FT: No GTK included in FTIE");
  470. return 0;
  471. }
  472. wpa_hexdump_key(MSG_DEBUG, "FT: Received GTK in Reassoc Resp",
  473. gtk_elem, gtk_elem_len);
  474. if (gtk_elem_len < 11 + 24 || (gtk_elem_len - 11) % 8 ||
  475. gtk_elem_len - 19 > sizeof(gtk)) {
  476. wpa_printf(MSG_DEBUG, "FT: Invalid GTK sub-elem "
  477. "length %lu", (unsigned long) gtk_elem_len);
  478. return -1;
  479. }
  480. gtk_len = gtk_elem_len - 19;
  481. if (aes_unwrap(sm->ptk.kek, gtk_len / 8, gtk_elem + 11, gtk)) {
  482. wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
  483. "decrypt GTK");
  484. return -1;
  485. }
  486. keylen = wpa_cipher_key_len(sm->group_cipher);
  487. rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
  488. alg = wpa_cipher_to_alg(sm->group_cipher);
  489. if (alg == WPA_ALG_NONE) {
  490. wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
  491. sm->group_cipher);
  492. return -1;
  493. }
  494. if (gtk_len < keylen) {
  495. wpa_printf(MSG_DEBUG, "FT: Too short GTK in FTIE");
  496. return -1;
  497. }
  498. /* Key Info[2] | Key Length[1] | RSC[8] | Key[5..32]. */
  499. keyidx = WPA_GET_LE16(gtk_elem) & 0x03;
  500. if (gtk_elem[2] != keylen) {
  501. wpa_printf(MSG_DEBUG, "FT: GTK length mismatch: received %d "
  502. "negotiated %lu",
  503. gtk_elem[2], (unsigned long) keylen);
  504. return -1;
  505. }
  506. wpa_hexdump_key(MSG_DEBUG, "FT: GTK from Reassoc Resp", gtk, keylen);
  507. if (wpa_sm_set_key(sm, alg, broadcast_ether_addr, keyidx, 0,
  508. gtk_elem + 3, rsc_len, gtk, keylen) < 0) {
  509. wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to the "
  510. "driver.");
  511. return -1;
  512. }
  513. return 0;
  514. }
  515. #ifdef CONFIG_IEEE80211W
  516. static int wpa_ft_process_igtk_subelem(struct wpa_sm *sm, const u8 *igtk_elem,
  517. size_t igtk_elem_len)
  518. {
  519. u8 igtk[WPA_IGTK_LEN];
  520. u16 keyidx;
  521. if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC)
  522. return 0;
  523. if (igtk_elem == NULL) {
  524. wpa_printf(MSG_DEBUG, "FT: No IGTK included in FTIE");
  525. return 0;
  526. }
  527. wpa_hexdump_key(MSG_DEBUG, "FT: Received IGTK in Reassoc Resp",
  528. igtk_elem, igtk_elem_len);
  529. if (igtk_elem_len != 2 + 6 + 1 + WPA_IGTK_LEN + 8) {
  530. wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem "
  531. "length %lu", (unsigned long) igtk_elem_len);
  532. return -1;
  533. }
  534. if (igtk_elem[8] != WPA_IGTK_LEN) {
  535. wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem Key Length "
  536. "%d", igtk_elem[8]);
  537. return -1;
  538. }
  539. if (aes_unwrap(sm->ptk.kek, WPA_IGTK_LEN / 8, igtk_elem + 9, igtk)) {
  540. wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
  541. "decrypt IGTK");
  542. return -1;
  543. }
  544. /* KeyID[2] | IPN[6] | Key Length[1] | Key[16+8] */
  545. keyidx = WPA_GET_LE16(igtk_elem);
  546. wpa_hexdump_key(MSG_DEBUG, "FT: IGTK from Reassoc Resp", igtk,
  547. WPA_IGTK_LEN);
  548. if (wpa_sm_set_key(sm, WPA_ALG_IGTK, broadcast_ether_addr, keyidx, 0,
  549. igtk_elem + 2, 6, igtk, WPA_IGTK_LEN) < 0) {
  550. wpa_printf(MSG_WARNING, "WPA: Failed to set IGTK to the "
  551. "driver.");
  552. return -1;
  553. }
  554. return 0;
  555. }
  556. #endif /* CONFIG_IEEE80211W */
  557. int wpa_ft_validate_reassoc_resp(struct wpa_sm *sm, const u8 *ies,
  558. size_t ies_len, const u8 *src_addr)
  559. {
  560. struct wpa_ft_ies parse;
  561. struct rsn_mdie *mdie;
  562. struct rsn_ftie *ftie;
  563. unsigned int count;
  564. u8 mic[16];
  565. wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
  566. if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
  567. sm->key_mgmt != WPA_KEY_MGMT_FT_PSK) {
  568. wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
  569. "enabled for this connection");
  570. return -1;
  571. }
  572. if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
  573. wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
  574. return -1;
  575. }
  576. mdie = (struct rsn_mdie *) parse.mdie;
  577. if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
  578. os_memcmp(mdie->mobility_domain, sm->mobility_domain,
  579. MOBILITY_DOMAIN_ID_LEN) != 0) {
  580. wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
  581. return -1;
  582. }
  583. ftie = (struct rsn_ftie *) parse.ftie;
  584. if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
  585. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
  586. return -1;
  587. }
  588. if (os_memcmp(ftie->snonce, sm->snonce, WPA_NONCE_LEN) != 0) {
  589. wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
  590. wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
  591. ftie->snonce, WPA_NONCE_LEN);
  592. wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
  593. sm->snonce, WPA_NONCE_LEN);
  594. return -1;
  595. }
  596. if (os_memcmp(ftie->anonce, sm->anonce, WPA_NONCE_LEN) != 0) {
  597. wpa_printf(MSG_DEBUG, "FT: ANonce mismatch in FTIE");
  598. wpa_hexdump(MSG_DEBUG, "FT: Received ANonce",
  599. ftie->anonce, WPA_NONCE_LEN);
  600. wpa_hexdump(MSG_DEBUG, "FT: Expected ANonce",
  601. sm->anonce, WPA_NONCE_LEN);
  602. return -1;
  603. }
  604. if (parse.r0kh_id == NULL) {
  605. wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
  606. return -1;
  607. }
  608. if (parse.r0kh_id_len != sm->r0kh_id_len ||
  609. os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) {
  610. wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
  611. "the current R0KH-ID");
  612. wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
  613. parse.r0kh_id, parse.r0kh_id_len);
  614. wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
  615. sm->r0kh_id, sm->r0kh_id_len);
  616. return -1;
  617. }
  618. if (parse.r1kh_id == NULL) {
  619. wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
  620. return -1;
  621. }
  622. if (os_memcmp(parse.r1kh_id, sm->r1kh_id, FT_R1KH_ID_LEN) != 0) {
  623. wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
  624. "ReassocResp");
  625. return -1;
  626. }
  627. if (parse.rsn_pmkid == NULL ||
  628. os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN)) {
  629. wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
  630. "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
  631. return -1;
  632. }
  633. count = 3;
  634. if (parse.ric)
  635. count += ieee802_11_ie_count(parse.ric, parse.ric_len);
  636. if (ftie->mic_control[1] != count) {
  637. wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in MIC "
  638. "Control: received %u expected %u",
  639. ftie->mic_control[1], count);
  640. return -1;
  641. }
  642. if (wpa_ft_mic(sm->ptk.kck, sm->own_addr, src_addr, 6,
  643. parse.mdie - 2, parse.mdie_len + 2,
  644. parse.ftie - 2, parse.ftie_len + 2,
  645. parse.rsn - 2, parse.rsn_len + 2,
  646. parse.ric, parse.ric_len,
  647. mic) < 0) {
  648. wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
  649. return -1;
  650. }
  651. if (os_memcmp(mic, ftie->mic, 16) != 0) {
  652. wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
  653. wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16);
  654. wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
  655. return -1;
  656. }
  657. if (wpa_ft_process_gtk_subelem(sm, parse.gtk, parse.gtk_len) < 0)
  658. return -1;
  659. #ifdef CONFIG_IEEE80211W
  660. if (wpa_ft_process_igtk_subelem(sm, parse.igtk, parse.igtk_len) < 0)
  661. return -1;
  662. #endif /* CONFIG_IEEE80211W */
  663. if (sm->set_ptk_after_assoc) {
  664. wpa_printf(MSG_DEBUG, "FT: Try to set PTK again now that we "
  665. "are associated");
  666. if (wpa_ft_install_ptk(sm, src_addr) < 0)
  667. return -1;
  668. sm->set_ptk_after_assoc = 0;
  669. }
  670. if (parse.ric) {
  671. wpa_hexdump(MSG_MSGDUMP, "FT: RIC Response",
  672. parse.ric, parse.ric_len);
  673. /* TODO: parse response and inform driver about results when
  674. * using wpa_supplicant SME */
  675. }
  676. wpa_printf(MSG_DEBUG, "FT: Completed successfully");
  677. return 0;
  678. }
  679. /**
  680. * wpa_ft_start_over_ds - Generate over-the-DS auth request
  681. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  682. * @target_ap: Target AP Address
  683. * @mdie: Mobility Domain IE from the target AP
  684. * Returns: 0 on success, -1 on failure
  685. */
  686. int wpa_ft_start_over_ds(struct wpa_sm *sm, const u8 *target_ap,
  687. const u8 *mdie)
  688. {
  689. u8 *ft_ies;
  690. size_t ft_ies_len;
  691. wpa_printf(MSG_DEBUG, "FT: Request over-the-DS with " MACSTR,
  692. MAC2STR(target_ap));
  693. /* Generate a new SNonce */
  694. if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
  695. wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
  696. return -1;
  697. }
  698. ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
  699. NULL, target_ap, NULL, 0, mdie);
  700. if (ft_ies) {
  701. sm->over_the_ds_in_progress = 1;
  702. os_memcpy(sm->target_ap, target_ap, ETH_ALEN);
  703. wpa_sm_send_ft_action(sm, 1, target_ap, ft_ies, ft_ies_len);
  704. os_free(ft_ies);
  705. }
  706. return 0;
  707. }
  708. #endif /* CONFIG_IEEE80211R */