wpa_ft.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*
  2. * WPA Supplicant - IEEE 802.11r - Fast BSS Transition
  3. * Copyright (c) 2006-2015, 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, struct wpa_ptk *ptk)
  19. {
  20. u8 ptk_name[WPA_PMK_NAME_LEN];
  21. const u8 *anonce = key->key_nonce;
  22. if (sm->xxkey_len == 0) {
  23. wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
  24. "derivation");
  25. return -1;
  26. }
  27. wpa_derive_pmk_r0(sm->xxkey, sm->xxkey_len, sm->ssid,
  28. sm->ssid_len, sm->mobility_domain,
  29. sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
  30. sm->pmk_r0, sm->pmk_r0_name);
  31. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", sm->pmk_r0, PMK_LEN);
  32. wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name",
  33. sm->pmk_r0_name, WPA_PMK_NAME_LEN);
  34. wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_name, sm->r1kh_id,
  35. sm->own_addr, sm->pmk_r1, sm->pmk_r1_name);
  36. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", sm->pmk_r1, PMK_LEN);
  37. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", sm->pmk_r1_name,
  38. WPA_PMK_NAME_LEN);
  39. return wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->snonce, anonce, sm->own_addr,
  40. sm->bssid, sm->pmk_r1_name, ptk, ptk_name,
  41. sm->key_mgmt, sm->pairwise_cipher);
  42. }
  43. /**
  44. * wpa_sm_set_ft_params - Set FT (IEEE 802.11r) parameters
  45. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  46. * @ies: Association Response IEs or %NULL to clear FT parameters
  47. * @ies_len: Length of ies buffer in octets
  48. * Returns: 0 on success, -1 on failure
  49. */
  50. int wpa_sm_set_ft_params(struct wpa_sm *sm, const u8 *ies, size_t ies_len)
  51. {
  52. struct wpa_ft_ies ft;
  53. if (sm == NULL)
  54. return 0;
  55. if (wpa_ft_parse_ies(ies, ies_len, &ft) < 0)
  56. return -1;
  57. if (ft.mdie && ft.mdie_len < MOBILITY_DOMAIN_ID_LEN + 1)
  58. return -1;
  59. if (ft.mdie) {
  60. wpa_hexdump(MSG_DEBUG, "FT: Mobility domain",
  61. ft.mdie, MOBILITY_DOMAIN_ID_LEN);
  62. os_memcpy(sm->mobility_domain, ft.mdie,
  63. MOBILITY_DOMAIN_ID_LEN);
  64. sm->mdie_ft_capab = ft.mdie[MOBILITY_DOMAIN_ID_LEN];
  65. wpa_printf(MSG_DEBUG, "FT: Capability and Policy: 0x%02x",
  66. sm->mdie_ft_capab);
  67. } else
  68. os_memset(sm->mobility_domain, 0, MOBILITY_DOMAIN_ID_LEN);
  69. if (ft.r0kh_id) {
  70. wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID",
  71. ft.r0kh_id, ft.r0kh_id_len);
  72. os_memcpy(sm->r0kh_id, ft.r0kh_id, ft.r0kh_id_len);
  73. sm->r0kh_id_len = ft.r0kh_id_len;
  74. } else {
  75. /* FIX: When should R0KH-ID be cleared? We need to keep the
  76. * old R0KH-ID in order to be able to use this during FT. */
  77. /*
  78. * os_memset(sm->r0kh_id, 0, FT_R0KH_ID_LEN);
  79. * sm->r0kh_id_len = 0;
  80. */
  81. }
  82. if (ft.r1kh_id) {
  83. wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID",
  84. ft.r1kh_id, FT_R1KH_ID_LEN);
  85. os_memcpy(sm->r1kh_id, ft.r1kh_id, FT_R1KH_ID_LEN);
  86. } else
  87. os_memset(sm->r1kh_id, 0, FT_R1KH_ID_LEN);
  88. os_free(sm->assoc_resp_ies);
  89. sm->assoc_resp_ies = os_malloc(ft.mdie_len + 2 + ft.ftie_len + 2);
  90. if (sm->assoc_resp_ies) {
  91. u8 *pos = sm->assoc_resp_ies;
  92. if (ft.mdie) {
  93. os_memcpy(pos, ft.mdie - 2, ft.mdie_len + 2);
  94. pos += ft.mdie_len + 2;
  95. }
  96. if (ft.ftie) {
  97. os_memcpy(pos, ft.ftie - 2, ft.ftie_len + 2);
  98. pos += ft.ftie_len + 2;
  99. }
  100. sm->assoc_resp_ies_len = pos - sm->assoc_resp_ies;
  101. wpa_hexdump(MSG_DEBUG, "FT: Stored MDIE and FTIE from "
  102. "(Re)Association Response",
  103. sm->assoc_resp_ies, sm->assoc_resp_ies_len);
  104. }
  105. return 0;
  106. }
  107. /**
  108. * wpa_ft_gen_req_ies - Generate FT (IEEE 802.11r) IEs for Auth/ReAssoc Request
  109. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  110. * @len: Buffer for returning the length of the IEs
  111. * @anonce: ANonce or %NULL if not yet available
  112. * @pmk_name: PMKR0Name or PMKR1Name to be added into the RSN IE PMKID List
  113. * @kck: 128-bit KCK for MIC or %NULL if no MIC is used
  114. * @kck_len: KCK length in octets
  115. * @target_ap: Target AP address
  116. * @ric_ies: Optional IE(s), e.g., WMM TSPEC(s), for RIC-Request or %NULL
  117. * @ric_ies_len: Length of ric_ies buffer in octets
  118. * @ap_mdie: Mobility Domain IE from the target AP
  119. * Returns: Pointer to buffer with IEs or %NULL on failure
  120. *
  121. * Caller is responsible for freeing the returned buffer with os_free();
  122. */
  123. static u8 * wpa_ft_gen_req_ies(struct wpa_sm *sm, size_t *len,
  124. const u8 *anonce, const u8 *pmk_name,
  125. const u8 *kck, size_t kck_len,
  126. const u8 *target_ap,
  127. const u8 *ric_ies, size_t ric_ies_len,
  128. const u8 *ap_mdie)
  129. {
  130. size_t buf_len;
  131. u8 *buf, *pos, *ftie_len, *ftie_pos;
  132. struct rsn_mdie *mdie;
  133. struct rsn_ftie *ftie;
  134. struct rsn_ie_hdr *rsnie;
  135. u16 capab;
  136. sm->ft_completed = 0;
  137. buf_len = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
  138. 2 + sm->r0kh_id_len + ric_ies_len + 100;
  139. buf = os_zalloc(buf_len);
  140. if (buf == NULL)
  141. return NULL;
  142. pos = buf;
  143. /* RSNIE[PMKR0Name/PMKR1Name] */
  144. rsnie = (struct rsn_ie_hdr *) pos;
  145. rsnie->elem_id = WLAN_EID_RSN;
  146. WPA_PUT_LE16(rsnie->version, RSN_VERSION);
  147. pos = (u8 *) (rsnie + 1);
  148. /* Group Suite Selector */
  149. if (sm->group_cipher != WPA_CIPHER_CCMP &&
  150. sm->group_cipher != WPA_CIPHER_GCMP &&
  151. sm->group_cipher != WPA_CIPHER_TKIP) {
  152. wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
  153. sm->group_cipher);
  154. os_free(buf);
  155. return NULL;
  156. }
  157. RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
  158. sm->group_cipher));
  159. pos += RSN_SELECTOR_LEN;
  160. /* Pairwise Suite Count */
  161. WPA_PUT_LE16(pos, 1);
  162. pos += 2;
  163. /* Pairwise Suite List */
  164. if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
  165. wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
  166. sm->pairwise_cipher);
  167. os_free(buf);
  168. return NULL;
  169. }
  170. RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
  171. sm->pairwise_cipher));
  172. pos += RSN_SELECTOR_LEN;
  173. /* Authenticated Key Management Suite Count */
  174. WPA_PUT_LE16(pos, 1);
  175. pos += 2;
  176. /* Authenticated Key Management Suite List */
  177. if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X)
  178. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
  179. else if (sm->key_mgmt == WPA_KEY_MGMT_FT_PSK)
  180. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
  181. else if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE)
  182. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE);
  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, kck_len, 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.tk, 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, 0, 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;
  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 (!wpa_key_mgmt_ft(sm->key_mgmt)) {
  348. wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
  349. "enabled for this connection");
  350. return -1;
  351. }
  352. if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
  353. wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
  354. return -1;
  355. }
  356. mdie = (struct rsn_mdie *) parse.mdie;
  357. if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
  358. os_memcmp(mdie->mobility_domain, sm->mobility_domain,
  359. MOBILITY_DOMAIN_ID_LEN) != 0) {
  360. wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
  361. return -1;
  362. }
  363. ftie = (struct rsn_ftie *) parse.ftie;
  364. if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
  365. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
  366. return -1;
  367. }
  368. if (os_memcmp(ftie->snonce, sm->snonce, WPA_NONCE_LEN) != 0) {
  369. wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
  370. wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
  371. ftie->snonce, WPA_NONCE_LEN);
  372. wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
  373. sm->snonce, WPA_NONCE_LEN);
  374. return -1;
  375. }
  376. if (parse.r0kh_id == NULL) {
  377. wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
  378. return -1;
  379. }
  380. if (parse.r0kh_id_len != sm->r0kh_id_len ||
  381. os_memcmp_const(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0)
  382. {
  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_const(parse.rsn_pmkid, sm->pmk_r0_name, WPA_PMK_NAME_LEN))
  397. {
  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. if (wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->snonce, ftie->anonce,
  414. sm->own_addr, bssid, sm->pmk_r1_name, &sm->ptk,
  415. ptk_name, sm->key_mgmt, sm->pairwise_cipher) < 0)
  416. return -1;
  417. ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, ftie->anonce,
  418. sm->pmk_r1_name,
  419. sm->ptk.kck, sm->ptk.kck_len, bssid,
  420. ric_ies, ric_ies_len,
  421. parse.mdie ? parse.mdie - 2 : NULL);
  422. if (ft_ies) {
  423. wpa_sm_update_ft_ies(sm, sm->mobility_domain,
  424. ft_ies, ft_ies_len);
  425. os_free(ft_ies);
  426. }
  427. wpa_sm_mark_authenticated(sm, bssid);
  428. ret = wpa_ft_install_ptk(sm, bssid);
  429. if (ret) {
  430. /*
  431. * Some drivers do not support key configuration when we are
  432. * not associated with the target AP. Work around this by
  433. * trying again after the following reassociation gets
  434. * completed.
  435. */
  436. wpa_printf(MSG_DEBUG, "FT: Failed to set PTK prior to "
  437. "association - try again after reassociation");
  438. sm->set_ptk_after_assoc = 1;
  439. } else
  440. sm->set_ptk_after_assoc = 0;
  441. sm->ft_completed = 1;
  442. if (ft_action) {
  443. /*
  444. * The caller is expected trigger re-association with the
  445. * Target AP.
  446. */
  447. os_memcpy(sm->bssid, target_ap, ETH_ALEN);
  448. }
  449. return 0;
  450. }
  451. int wpa_ft_is_completed(struct wpa_sm *sm)
  452. {
  453. if (sm == NULL)
  454. return 0;
  455. if (!wpa_key_mgmt_ft(sm->key_mgmt))
  456. return 0;
  457. return sm->ft_completed;
  458. }
  459. void wpa_reset_ft_completed(struct wpa_sm *sm)
  460. {
  461. if (sm != NULL)
  462. sm->ft_completed = 0;
  463. }
  464. static int wpa_ft_process_gtk_subelem(struct wpa_sm *sm, const u8 *gtk_elem,
  465. size_t gtk_elem_len)
  466. {
  467. u8 gtk[32];
  468. int keyidx;
  469. enum wpa_alg alg;
  470. size_t gtk_len, keylen, rsc_len;
  471. if (gtk_elem == NULL) {
  472. wpa_printf(MSG_DEBUG, "FT: No GTK included in FTIE");
  473. return 0;
  474. }
  475. wpa_hexdump_key(MSG_DEBUG, "FT: Received GTK in Reassoc Resp",
  476. gtk_elem, gtk_elem_len);
  477. if (gtk_elem_len < 11 + 24 || (gtk_elem_len - 11) % 8 ||
  478. gtk_elem_len - 19 > sizeof(gtk)) {
  479. wpa_printf(MSG_DEBUG, "FT: Invalid GTK sub-elem "
  480. "length %lu", (unsigned long) gtk_elem_len);
  481. return -1;
  482. }
  483. gtk_len = gtk_elem_len - 19;
  484. if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, gtk_len / 8, gtk_elem + 11,
  485. gtk)) {
  486. wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
  487. "decrypt GTK");
  488. return -1;
  489. }
  490. keylen = wpa_cipher_key_len(sm->group_cipher);
  491. rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
  492. alg = wpa_cipher_to_alg(sm->group_cipher);
  493. if (alg == WPA_ALG_NONE) {
  494. wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
  495. sm->group_cipher);
  496. return -1;
  497. }
  498. if (gtk_len < keylen) {
  499. wpa_printf(MSG_DEBUG, "FT: Too short GTK in FTIE");
  500. return -1;
  501. }
  502. /* Key Info[2] | Key Length[1] | RSC[8] | Key[5..32]. */
  503. keyidx = WPA_GET_LE16(gtk_elem) & 0x03;
  504. if (gtk_elem[2] != keylen) {
  505. wpa_printf(MSG_DEBUG, "FT: GTK length mismatch: received %d "
  506. "negotiated %lu",
  507. gtk_elem[2], (unsigned long) keylen);
  508. return -1;
  509. }
  510. wpa_hexdump_key(MSG_DEBUG, "FT: GTK from Reassoc Resp", gtk, keylen);
  511. if (sm->group_cipher == WPA_CIPHER_TKIP) {
  512. /* Swap Tx/Rx keys for Michael MIC */
  513. u8 tmp[8];
  514. os_memcpy(tmp, gtk + 16, 8);
  515. os_memcpy(gtk + 16, gtk + 24, 8);
  516. os_memcpy(gtk + 24, tmp, 8);
  517. }
  518. if (wpa_sm_set_key(sm, alg, broadcast_ether_addr, keyidx, 0,
  519. gtk_elem + 3, rsc_len, gtk, keylen) < 0) {
  520. wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to the "
  521. "driver.");
  522. return -1;
  523. }
  524. return 0;
  525. }
  526. #ifdef CONFIG_IEEE80211W
  527. static int wpa_ft_process_igtk_subelem(struct wpa_sm *sm, const u8 *igtk_elem,
  528. size_t igtk_elem_len)
  529. {
  530. u8 igtk[WPA_IGTK_LEN];
  531. u16 keyidx;
  532. if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC)
  533. return 0;
  534. if (igtk_elem == NULL) {
  535. wpa_printf(MSG_DEBUG, "FT: No IGTK included in FTIE");
  536. return 0;
  537. }
  538. wpa_hexdump_key(MSG_DEBUG, "FT: Received IGTK in Reassoc Resp",
  539. igtk_elem, igtk_elem_len);
  540. if (igtk_elem_len != 2 + 6 + 1 + WPA_IGTK_LEN + 8) {
  541. wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem "
  542. "length %lu", (unsigned long) igtk_elem_len);
  543. return -1;
  544. }
  545. if (igtk_elem[8] != WPA_IGTK_LEN) {
  546. wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem Key Length "
  547. "%d", igtk_elem[8]);
  548. return -1;
  549. }
  550. if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, WPA_IGTK_LEN / 8,
  551. 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[WPA_EAPOL_KEY_MIC_MAX_LEN];
  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_const(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0)
  621. {
  622. wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
  623. "the current R0KH-ID");
  624. wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
  625. parse.r0kh_id, parse.r0kh_id_len);
  626. wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
  627. sm->r0kh_id, sm->r0kh_id_len);
  628. return -1;
  629. }
  630. if (parse.r1kh_id == NULL) {
  631. wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
  632. return -1;
  633. }
  634. if (os_memcmp_const(parse.r1kh_id, sm->r1kh_id, FT_R1KH_ID_LEN) != 0) {
  635. wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
  636. "ReassocResp");
  637. return -1;
  638. }
  639. if (parse.rsn_pmkid == NULL ||
  640. os_memcmp_const(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN))
  641. {
  642. wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
  643. "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
  644. return -1;
  645. }
  646. count = 3;
  647. if (parse.ric)
  648. count += ieee802_11_ie_count(parse.ric, parse.ric_len);
  649. if (ftie->mic_control[1] != count) {
  650. wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in MIC "
  651. "Control: received %u expected %u",
  652. ftie->mic_control[1], count);
  653. return -1;
  654. }
  655. if (wpa_ft_mic(sm->ptk.kck, sm->ptk.kck_len, sm->own_addr, src_addr, 6,
  656. parse.mdie - 2, parse.mdie_len + 2,
  657. parse.ftie - 2, parse.ftie_len + 2,
  658. parse.rsn - 2, parse.rsn_len + 2,
  659. parse.ric, parse.ric_len,
  660. mic) < 0) {
  661. wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
  662. return -1;
  663. }
  664. if (os_memcmp_const(mic, ftie->mic, 16) != 0) {
  665. wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
  666. wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16);
  667. wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
  668. return -1;
  669. }
  670. if (wpa_ft_process_gtk_subelem(sm, parse.gtk, parse.gtk_len) < 0)
  671. return -1;
  672. #ifdef CONFIG_IEEE80211W
  673. if (wpa_ft_process_igtk_subelem(sm, parse.igtk, parse.igtk_len) < 0)
  674. return -1;
  675. #endif /* CONFIG_IEEE80211W */
  676. if (sm->set_ptk_after_assoc) {
  677. wpa_printf(MSG_DEBUG, "FT: Try to set PTK again now that we "
  678. "are associated");
  679. if (wpa_ft_install_ptk(sm, src_addr) < 0)
  680. return -1;
  681. sm->set_ptk_after_assoc = 0;
  682. }
  683. if (parse.ric) {
  684. wpa_hexdump(MSG_MSGDUMP, "FT: RIC Response",
  685. parse.ric, parse.ric_len);
  686. /* TODO: parse response and inform driver about results when
  687. * using wpa_supplicant SME */
  688. }
  689. wpa_printf(MSG_DEBUG, "FT: Completed successfully");
  690. return 0;
  691. }
  692. /**
  693. * wpa_ft_start_over_ds - Generate over-the-DS auth request
  694. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  695. * @target_ap: Target AP Address
  696. * @mdie: Mobility Domain IE from the target AP
  697. * Returns: 0 on success, -1 on failure
  698. */
  699. int wpa_ft_start_over_ds(struct wpa_sm *sm, const u8 *target_ap,
  700. const u8 *mdie)
  701. {
  702. u8 *ft_ies;
  703. size_t ft_ies_len;
  704. wpa_printf(MSG_DEBUG, "FT: Request over-the-DS with " MACSTR,
  705. MAC2STR(target_ap));
  706. /* Generate a new SNonce */
  707. if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
  708. wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
  709. return -1;
  710. }
  711. ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
  712. NULL, 0, target_ap, NULL, 0, mdie);
  713. if (ft_ies) {
  714. sm->over_the_ds_in_progress = 1;
  715. os_memcpy(sm->target_ap, target_ap, ETH_ALEN);
  716. wpa_sm_send_ft_action(sm, 1, target_ap, ft_ies, ft_ies_len);
  717. os_free(ft_ies);
  718. }
  719. return 0;
  720. }
  721. #endif /* CONFIG_IEEE80211R */