wpa_ft.c 23 KB

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