wpa_ft.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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 if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE)
  184. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE);
  185. else {
  186. wpa_printf(MSG_WARNING, "FT: Invalid key management type (%d)",
  187. sm->key_mgmt);
  188. os_free(buf);
  189. return NULL;
  190. }
  191. pos += RSN_SELECTOR_LEN;
  192. /* RSN Capabilities */
  193. capab = 0;
  194. #ifdef CONFIG_IEEE80211W
  195. if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC)
  196. capab |= WPA_CAPABILITY_MFPC;
  197. #endif /* CONFIG_IEEE80211W */
  198. WPA_PUT_LE16(pos, capab);
  199. pos += 2;
  200. /* PMKID Count */
  201. WPA_PUT_LE16(pos, 1);
  202. pos += 2;
  203. /* PMKID List [PMKR0Name/PMKR1Name] */
  204. os_memcpy(pos, pmk_name, WPA_PMK_NAME_LEN);
  205. pos += WPA_PMK_NAME_LEN;
  206. #ifdef CONFIG_IEEE80211W
  207. if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
  208. /* Management Group Cipher Suite */
  209. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
  210. pos += RSN_SELECTOR_LEN;
  211. }
  212. #endif /* CONFIG_IEEE80211W */
  213. rsnie->len = (pos - (u8 *) rsnie) - 2;
  214. /* MDIE */
  215. *pos++ = WLAN_EID_MOBILITY_DOMAIN;
  216. *pos++ = sizeof(*mdie);
  217. mdie = (struct rsn_mdie *) pos;
  218. pos += sizeof(*mdie);
  219. os_memcpy(mdie->mobility_domain, sm->mobility_domain,
  220. MOBILITY_DOMAIN_ID_LEN);
  221. mdie->ft_capab = ap_mdie && ap_mdie[1] >= 3 ? ap_mdie[4] :
  222. sm->mdie_ft_capab;
  223. /* FTIE[SNonce, [R1KH-ID,] R0KH-ID ] */
  224. ftie_pos = pos;
  225. *pos++ = WLAN_EID_FAST_BSS_TRANSITION;
  226. ftie_len = pos++;
  227. ftie = (struct rsn_ftie *) pos;
  228. pos += sizeof(*ftie);
  229. os_memcpy(ftie->snonce, sm->snonce, WPA_NONCE_LEN);
  230. if (anonce)
  231. os_memcpy(ftie->anonce, anonce, WPA_NONCE_LEN);
  232. if (kck) {
  233. /* R1KH-ID sub-element in third FT message */
  234. *pos++ = FTIE_SUBELEM_R1KH_ID;
  235. *pos++ = FT_R1KH_ID_LEN;
  236. os_memcpy(pos, sm->r1kh_id, FT_R1KH_ID_LEN);
  237. pos += FT_R1KH_ID_LEN;
  238. }
  239. /* R0KH-ID sub-element */
  240. *pos++ = FTIE_SUBELEM_R0KH_ID;
  241. *pos++ = sm->r0kh_id_len;
  242. os_memcpy(pos, sm->r0kh_id, sm->r0kh_id_len);
  243. pos += sm->r0kh_id_len;
  244. *ftie_len = pos - ftie_len - 1;
  245. if (ric_ies) {
  246. /* RIC Request */
  247. os_memcpy(pos, ric_ies, ric_ies_len);
  248. pos += ric_ies_len;
  249. }
  250. if (kck) {
  251. /*
  252. * IEEE Std 802.11r-2008, 11A.8.4
  253. * MIC shall be calculated over:
  254. * non-AP STA MAC address
  255. * Target AP MAC address
  256. * Transaction seq number (5 for ReassocReq, 3 otherwise)
  257. * RSN IE
  258. * MDIE
  259. * FTIE (with MIC field set to 0)
  260. * RIC-Request (if present)
  261. */
  262. /* Information element count */
  263. ftie->mic_control[1] = 3 + ieee802_11_ie_count(ric_ies,
  264. ric_ies_len);
  265. if (wpa_ft_mic(kck, sm->own_addr, target_ap, 5,
  266. ((u8 *) mdie) - 2, 2 + sizeof(*mdie),
  267. ftie_pos, 2 + *ftie_len,
  268. (u8 *) rsnie, 2 + rsnie->len, ric_ies,
  269. ric_ies_len, ftie->mic) < 0) {
  270. wpa_printf(MSG_INFO, "FT: Failed to calculate MIC");
  271. os_free(buf);
  272. return NULL;
  273. }
  274. }
  275. *len = pos - buf;
  276. return buf;
  277. }
  278. static int wpa_ft_install_ptk(struct wpa_sm *sm, const u8 *bssid)
  279. {
  280. int keylen;
  281. enum wpa_alg alg;
  282. u8 null_rsc[6] = { 0, 0, 0, 0, 0, 0 };
  283. wpa_printf(MSG_DEBUG, "FT: Installing PTK to the driver.");
  284. if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
  285. wpa_printf(MSG_WARNING, "FT: Unsupported pairwise cipher %d",
  286. sm->pairwise_cipher);
  287. return -1;
  288. }
  289. alg = wpa_cipher_to_alg(sm->pairwise_cipher);
  290. keylen = wpa_cipher_key_len(sm->pairwise_cipher);
  291. if (wpa_sm_set_key(sm, alg, bssid, 0, 1, null_rsc,
  292. sizeof(null_rsc), (u8 *) sm->ptk.tk1, keylen) < 0) {
  293. wpa_printf(MSG_WARNING, "FT: Failed to set PTK to the driver");
  294. return -1;
  295. }
  296. return 0;
  297. }
  298. /**
  299. * wpa_ft_prepare_auth_request - Generate over-the-air auth request
  300. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  301. * @mdie: Target AP MDIE
  302. * Returns: 0 on success, -1 on failure
  303. */
  304. int wpa_ft_prepare_auth_request(struct wpa_sm *sm, const u8 *mdie)
  305. {
  306. u8 *ft_ies;
  307. size_t ft_ies_len;
  308. /* Generate a new SNonce */
  309. if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
  310. wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
  311. return -1;
  312. }
  313. ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
  314. NULL, sm->bssid, NULL, 0, mdie);
  315. if (ft_ies) {
  316. wpa_sm_update_ft_ies(sm, sm->mobility_domain,
  317. ft_ies, ft_ies_len);
  318. os_free(ft_ies);
  319. }
  320. return 0;
  321. }
  322. int wpa_ft_process_response(struct wpa_sm *sm, const u8 *ies, size_t ies_len,
  323. int ft_action, const u8 *target_ap,
  324. const u8 *ric_ies, size_t ric_ies_len)
  325. {
  326. u8 *ft_ies;
  327. size_t ft_ies_len, ptk_len;
  328. struct wpa_ft_ies parse;
  329. struct rsn_mdie *mdie;
  330. struct rsn_ftie *ftie;
  331. u8 ptk_name[WPA_PMK_NAME_LEN];
  332. int ret;
  333. const u8 *bssid;
  334. wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
  335. wpa_hexdump(MSG_DEBUG, "FT: RIC IEs", ric_ies, ric_ies_len);
  336. if (ft_action) {
  337. if (!sm->over_the_ds_in_progress) {
  338. wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
  339. "- drop FT Action Response");
  340. return -1;
  341. }
  342. if (os_memcmp(target_ap, sm->target_ap, ETH_ALEN) != 0) {
  343. wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
  344. "with this Target AP - drop FT Action "
  345. "Response");
  346. return -1;
  347. }
  348. }
  349. if (!wpa_key_mgmt_ft(sm->key_mgmt)) {
  350. wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
  351. "enabled for this connection");
  352. return -1;
  353. }
  354. if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
  355. wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
  356. return -1;
  357. }
  358. mdie = (struct rsn_mdie *) parse.mdie;
  359. if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
  360. os_memcmp(mdie->mobility_domain, sm->mobility_domain,
  361. MOBILITY_DOMAIN_ID_LEN) != 0) {
  362. wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
  363. return -1;
  364. }
  365. ftie = (struct rsn_ftie *) parse.ftie;
  366. if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
  367. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
  368. return -1;
  369. }
  370. if (os_memcmp(ftie->snonce, sm->snonce, WPA_NONCE_LEN) != 0) {
  371. wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
  372. wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
  373. ftie->snonce, WPA_NONCE_LEN);
  374. wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
  375. sm->snonce, WPA_NONCE_LEN);
  376. return -1;
  377. }
  378. if (parse.r0kh_id == NULL) {
  379. wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
  380. return -1;
  381. }
  382. if (parse.r0kh_id_len != sm->r0kh_id_len ||
  383. os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) {
  384. wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
  385. "the current R0KH-ID");
  386. wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
  387. parse.r0kh_id, parse.r0kh_id_len);
  388. wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
  389. sm->r0kh_id, sm->r0kh_id_len);
  390. return -1;
  391. }
  392. if (parse.r1kh_id == NULL) {
  393. wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
  394. return -1;
  395. }
  396. if (parse.rsn_pmkid == NULL ||
  397. os_memcmp(parse.rsn_pmkid, sm->pmk_r0_name, WPA_PMK_NAME_LEN)) {
  398. wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name (PMKID) in "
  399. "RSNIE");
  400. return -1;
  401. }
  402. os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
  403. wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID", sm->r1kh_id, FT_R1KH_ID_LEN);
  404. wpa_hexdump(MSG_DEBUG, "FT: SNonce", sm->snonce, WPA_NONCE_LEN);
  405. wpa_hexdump(MSG_DEBUG, "FT: ANonce", ftie->anonce, WPA_NONCE_LEN);
  406. os_memcpy(sm->anonce, ftie->anonce, WPA_NONCE_LEN);
  407. wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_name, sm->r1kh_id,
  408. sm->own_addr, sm->pmk_r1, sm->pmk_r1_name);
  409. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", sm->pmk_r1, PMK_LEN);
  410. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name",
  411. sm->pmk_r1_name, WPA_PMK_NAME_LEN);
  412. bssid = target_ap;
  413. ptk_len = sm->pairwise_cipher != WPA_CIPHER_TKIP ? 48 : 64;
  414. wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->snonce, ftie->anonce, sm->own_addr,
  415. bssid, sm->pmk_r1_name,
  416. (u8 *) &sm->ptk, ptk_len, ptk_name);
  417. wpa_hexdump_key(MSG_DEBUG, "FT: PTK",
  418. (u8 *) &sm->ptk, ptk_len);
  419. wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
  420. ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, ftie->anonce,
  421. sm->pmk_r1_name, sm->ptk.kck, bssid,
  422. ric_ies, ric_ies_len,
  423. parse.mdie ? parse.mdie - 2 : NULL);
  424. if (ft_ies) {
  425. wpa_sm_update_ft_ies(sm, sm->mobility_domain,
  426. ft_ies, ft_ies_len);
  427. os_free(ft_ies);
  428. }
  429. wpa_sm_mark_authenticated(sm, bssid);
  430. ret = wpa_ft_install_ptk(sm, bssid);
  431. if (ret) {
  432. /*
  433. * Some drivers do not support key configuration when we are
  434. * not associated with the target AP. Work around this by
  435. * trying again after the following reassociation gets
  436. * completed.
  437. */
  438. wpa_printf(MSG_DEBUG, "FT: Failed to set PTK prior to "
  439. "association - try again after reassociation");
  440. sm->set_ptk_after_assoc = 1;
  441. } else
  442. sm->set_ptk_after_assoc = 0;
  443. sm->ft_completed = 1;
  444. if (ft_action) {
  445. /*
  446. * The caller is expected trigger re-association with the
  447. * Target AP.
  448. */
  449. os_memcpy(sm->bssid, target_ap, ETH_ALEN);
  450. }
  451. return 0;
  452. }
  453. int wpa_ft_is_completed(struct wpa_sm *sm)
  454. {
  455. if (sm == NULL)
  456. return 0;
  457. if (!wpa_key_mgmt_ft(sm->key_mgmt))
  458. return 0;
  459. return sm->ft_completed;
  460. }
  461. void wpa_reset_ft_completed(struct wpa_sm *sm)
  462. {
  463. if (sm != NULL)
  464. sm->ft_completed = 0;
  465. }
  466. static int wpa_ft_process_gtk_subelem(struct wpa_sm *sm, const u8 *gtk_elem,
  467. size_t gtk_elem_len)
  468. {
  469. u8 gtk[32];
  470. int keyidx;
  471. enum wpa_alg alg;
  472. size_t gtk_len, keylen, rsc_len;
  473. if (gtk_elem == NULL) {
  474. wpa_printf(MSG_DEBUG, "FT: No GTK included in FTIE");
  475. return 0;
  476. }
  477. wpa_hexdump_key(MSG_DEBUG, "FT: Received GTK in Reassoc Resp",
  478. gtk_elem, gtk_elem_len);
  479. if (gtk_elem_len < 11 + 24 || (gtk_elem_len - 11) % 8 ||
  480. gtk_elem_len - 19 > sizeof(gtk)) {
  481. wpa_printf(MSG_DEBUG, "FT: Invalid GTK sub-elem "
  482. "length %lu", (unsigned long) gtk_elem_len);
  483. return -1;
  484. }
  485. gtk_len = gtk_elem_len - 19;
  486. if (aes_unwrap(sm->ptk.kek, gtk_len / 8, gtk_elem + 11, gtk)) {
  487. wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
  488. "decrypt GTK");
  489. return -1;
  490. }
  491. keylen = wpa_cipher_key_len(sm->group_cipher);
  492. rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
  493. alg = wpa_cipher_to_alg(sm->group_cipher);
  494. if (alg == WPA_ALG_NONE) {
  495. wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
  496. sm->group_cipher);
  497. return -1;
  498. }
  499. if (gtk_len < keylen) {
  500. wpa_printf(MSG_DEBUG, "FT: Too short GTK in FTIE");
  501. return -1;
  502. }
  503. /* Key Info[2] | Key Length[1] | RSC[8] | Key[5..32]. */
  504. keyidx = WPA_GET_LE16(gtk_elem) & 0x03;
  505. if (gtk_elem[2] != keylen) {
  506. wpa_printf(MSG_DEBUG, "FT: GTK length mismatch: received %d "
  507. "negotiated %lu",
  508. gtk_elem[2], (unsigned long) keylen);
  509. return -1;
  510. }
  511. wpa_hexdump_key(MSG_DEBUG, "FT: GTK from Reassoc Resp", gtk, keylen);
  512. if (sm->group_cipher == WPA_CIPHER_TKIP) {
  513. /* Swap Tx/Rx keys for Michael MIC */
  514. u8 tmp[8];
  515. os_memcpy(tmp, gtk + 16, 8);
  516. os_memcpy(gtk + 16, gtk + 24, 8);
  517. os_memcpy(gtk + 24, tmp, 8);
  518. }
  519. if (wpa_sm_set_key(sm, alg, broadcast_ether_addr, keyidx, 0,
  520. gtk_elem + 3, rsc_len, gtk, keylen) < 0) {
  521. wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to the "
  522. "driver.");
  523. return -1;
  524. }
  525. return 0;
  526. }
  527. #ifdef CONFIG_IEEE80211W
  528. static int wpa_ft_process_igtk_subelem(struct wpa_sm *sm, const u8 *igtk_elem,
  529. size_t igtk_elem_len)
  530. {
  531. u8 igtk[WPA_IGTK_LEN];
  532. u16 keyidx;
  533. if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC)
  534. return 0;
  535. if (igtk_elem == NULL) {
  536. wpa_printf(MSG_DEBUG, "FT: No IGTK included in FTIE");
  537. return 0;
  538. }
  539. wpa_hexdump_key(MSG_DEBUG, "FT: Received IGTK in Reassoc Resp",
  540. igtk_elem, igtk_elem_len);
  541. if (igtk_elem_len != 2 + 6 + 1 + WPA_IGTK_LEN + 8) {
  542. wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem "
  543. "length %lu", (unsigned long) igtk_elem_len);
  544. return -1;
  545. }
  546. if (igtk_elem[8] != WPA_IGTK_LEN) {
  547. wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem Key Length "
  548. "%d", igtk_elem[8]);
  549. return -1;
  550. }
  551. if (aes_unwrap(sm->ptk.kek, WPA_IGTK_LEN / 8, igtk_elem + 9, igtk)) {
  552. wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
  553. "decrypt IGTK");
  554. return -1;
  555. }
  556. /* KeyID[2] | IPN[6] | Key Length[1] | Key[16+8] */
  557. keyidx = WPA_GET_LE16(igtk_elem);
  558. wpa_hexdump_key(MSG_DEBUG, "FT: IGTK from Reassoc Resp", igtk,
  559. WPA_IGTK_LEN);
  560. if (wpa_sm_set_key(sm, WPA_ALG_IGTK, broadcast_ether_addr, keyidx, 0,
  561. igtk_elem + 2, 6, igtk, WPA_IGTK_LEN) < 0) {
  562. wpa_printf(MSG_WARNING, "WPA: Failed to set IGTK to the "
  563. "driver.");
  564. return -1;
  565. }
  566. return 0;
  567. }
  568. #endif /* CONFIG_IEEE80211W */
  569. int wpa_ft_validate_reassoc_resp(struct wpa_sm *sm, const u8 *ies,
  570. size_t ies_len, const u8 *src_addr)
  571. {
  572. struct wpa_ft_ies parse;
  573. struct rsn_mdie *mdie;
  574. struct rsn_ftie *ftie;
  575. unsigned int count;
  576. u8 mic[16];
  577. wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
  578. if (!wpa_key_mgmt_ft(sm->key_mgmt)) {
  579. wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
  580. "enabled for this connection");
  581. return -1;
  582. }
  583. if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
  584. wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
  585. return -1;
  586. }
  587. mdie = (struct rsn_mdie *) parse.mdie;
  588. if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
  589. os_memcmp(mdie->mobility_domain, sm->mobility_domain,
  590. MOBILITY_DOMAIN_ID_LEN) != 0) {
  591. wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
  592. return -1;
  593. }
  594. ftie = (struct rsn_ftie *) parse.ftie;
  595. if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
  596. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
  597. return -1;
  598. }
  599. if (os_memcmp(ftie->snonce, sm->snonce, WPA_NONCE_LEN) != 0) {
  600. wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
  601. wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
  602. ftie->snonce, WPA_NONCE_LEN);
  603. wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
  604. sm->snonce, WPA_NONCE_LEN);
  605. return -1;
  606. }
  607. if (os_memcmp(ftie->anonce, sm->anonce, WPA_NONCE_LEN) != 0) {
  608. wpa_printf(MSG_DEBUG, "FT: ANonce mismatch in FTIE");
  609. wpa_hexdump(MSG_DEBUG, "FT: Received ANonce",
  610. ftie->anonce, WPA_NONCE_LEN);
  611. wpa_hexdump(MSG_DEBUG, "FT: Expected ANonce",
  612. sm->anonce, WPA_NONCE_LEN);
  613. return -1;
  614. }
  615. if (parse.r0kh_id == NULL) {
  616. wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
  617. return -1;
  618. }
  619. if (parse.r0kh_id_len != sm->r0kh_id_len ||
  620. os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) {
  621. wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
  622. "the current R0KH-ID");
  623. wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
  624. parse.r0kh_id, parse.r0kh_id_len);
  625. wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
  626. sm->r0kh_id, sm->r0kh_id_len);
  627. return -1;
  628. }
  629. if (parse.r1kh_id == NULL) {
  630. wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
  631. return -1;
  632. }
  633. if (os_memcmp(parse.r1kh_id, sm->r1kh_id, FT_R1KH_ID_LEN) != 0) {
  634. wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
  635. "ReassocResp");
  636. return -1;
  637. }
  638. if (parse.rsn_pmkid == NULL ||
  639. os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN)) {
  640. wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
  641. "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
  642. return -1;
  643. }
  644. count = 3;
  645. if (parse.ric)
  646. count += ieee802_11_ie_count(parse.ric, parse.ric_len);
  647. if (ftie->mic_control[1] != count) {
  648. wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in MIC "
  649. "Control: received %u expected %u",
  650. ftie->mic_control[1], count);
  651. return -1;
  652. }
  653. if (wpa_ft_mic(sm->ptk.kck, sm->own_addr, src_addr, 6,
  654. parse.mdie - 2, parse.mdie_len + 2,
  655. parse.ftie - 2, parse.ftie_len + 2,
  656. parse.rsn - 2, parse.rsn_len + 2,
  657. parse.ric, parse.ric_len,
  658. mic) < 0) {
  659. wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
  660. return -1;
  661. }
  662. if (os_memcmp(mic, ftie->mic, 16) != 0) {
  663. wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
  664. wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16);
  665. wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
  666. return -1;
  667. }
  668. if (wpa_ft_process_gtk_subelem(sm, parse.gtk, parse.gtk_len) < 0)
  669. return -1;
  670. #ifdef CONFIG_IEEE80211W
  671. if (wpa_ft_process_igtk_subelem(sm, parse.igtk, parse.igtk_len) < 0)
  672. return -1;
  673. #endif /* CONFIG_IEEE80211W */
  674. if (sm->set_ptk_after_assoc) {
  675. wpa_printf(MSG_DEBUG, "FT: Try to set PTK again now that we "
  676. "are associated");
  677. if (wpa_ft_install_ptk(sm, src_addr) < 0)
  678. return -1;
  679. sm->set_ptk_after_assoc = 0;
  680. }
  681. if (parse.ric) {
  682. wpa_hexdump(MSG_MSGDUMP, "FT: RIC Response",
  683. parse.ric, parse.ric_len);
  684. /* TODO: parse response and inform driver about results when
  685. * using wpa_supplicant SME */
  686. }
  687. wpa_printf(MSG_DEBUG, "FT: Completed successfully");
  688. return 0;
  689. }
  690. /**
  691. * wpa_ft_start_over_ds - Generate over-the-DS auth request
  692. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  693. * @target_ap: Target AP Address
  694. * @mdie: Mobility Domain IE from the target AP
  695. * Returns: 0 on success, -1 on failure
  696. */
  697. int wpa_ft_start_over_ds(struct wpa_sm *sm, const u8 *target_ap,
  698. const u8 *mdie)
  699. {
  700. u8 *ft_ies;
  701. size_t ft_ies_len;
  702. wpa_printf(MSG_DEBUG, "FT: Request over-the-DS with " MACSTR,
  703. MAC2STR(target_ap));
  704. /* Generate a new SNonce */
  705. if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
  706. wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
  707. return -1;
  708. }
  709. ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
  710. NULL, target_ap, NULL, 0, mdie);
  711. if (ft_ies) {
  712. sm->over_the_ds_in_progress = 1;
  713. os_memcpy(sm->target_ap, target_ap, ETH_ALEN);
  714. wpa_sm_send_ft_action(sm, 1, target_ap, ft_ies, ft_ies_len);
  715. os_free(ft_ies);
  716. }
  717. return 0;
  718. }
  719. #endif /* CONFIG_IEEE80211R */