wpa_ft.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. /*
  2. * hostapd - IEEE 802.11r - Fast BSS Transition
  3. * Copyright (c) 2004-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 "config.h"
  17. #include "wpa.h"
  18. #include "aes_wrap.h"
  19. #include "ieee802_11.h"
  20. #include "wme.h"
  21. #include "defs.h"
  22. #include "wpa_auth_i.h"
  23. #include "wpa_auth_ie.h"
  24. #ifdef CONFIG_IEEE80211R
  25. struct wpa_ft_ies {
  26. const u8 *mdie;
  27. size_t mdie_len;
  28. const u8 *ftie;
  29. size_t ftie_len;
  30. const u8 *r1kh_id;
  31. const u8 *gtk;
  32. size_t gtk_len;
  33. const u8 *r0kh_id;
  34. size_t r0kh_id_len;
  35. const u8 *rsn;
  36. size_t rsn_len;
  37. const u8 *rsn_pmkid;
  38. const u8 *ric;
  39. size_t ric_len;
  40. };
  41. static int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
  42. struct wpa_ft_ies *parse);
  43. static int wpa_ft_rrb_send(struct wpa_authenticator *wpa_auth, const u8 *dst,
  44. const u8 *data, size_t data_len)
  45. {
  46. if (wpa_auth->cb.send_ether == NULL)
  47. return -1;
  48. return wpa_auth->cb.send_ether(wpa_auth->cb.ctx, dst, ETH_P_RRB,
  49. data, data_len);
  50. }
  51. static int wpa_ft_action_send(struct wpa_authenticator *wpa_auth,
  52. const u8 *dst, const u8 *data, size_t data_len)
  53. {
  54. if (wpa_auth->cb.send_ft_action == NULL)
  55. return -1;
  56. return wpa_auth->cb.send_ft_action(wpa_auth->cb.ctx, dst,
  57. data, data_len);
  58. }
  59. static struct wpa_state_machine *
  60. wpa_ft_add_sta(struct wpa_authenticator *wpa_auth, const u8 *sta_addr)
  61. {
  62. if (wpa_auth->cb.add_sta == NULL)
  63. return NULL;
  64. return wpa_auth->cb.add_sta(wpa_auth->cb.ctx, sta_addr);
  65. }
  66. int wpa_write_mdie(struct wpa_auth_config *conf, u8 *buf, size_t len)
  67. {
  68. u8 *pos = buf;
  69. u8 capab;
  70. if (len < 2 + sizeof(struct rsn_mdie))
  71. return -1;
  72. *pos++ = WLAN_EID_MOBILITY_DOMAIN;
  73. *pos++ = MOBILITY_DOMAIN_ID_LEN + 1;
  74. os_memcpy(pos, conf->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
  75. pos += MOBILITY_DOMAIN_ID_LEN;
  76. capab = RSN_FT_CAPAB_FT_OVER_DS;
  77. *pos++ = capab;
  78. return pos - buf;
  79. }
  80. static int wpa_write_ftie(struct wpa_auth_config *conf, const u8 *r0kh_id,
  81. size_t r0kh_id_len,
  82. const u8 *anonce, const u8 *snonce,
  83. u8 *buf, size_t len, const u8 *subelem,
  84. size_t subelem_len)
  85. {
  86. u8 *pos = buf, *ielen;
  87. struct rsn_ftie *hdr;
  88. if (len < 2 + sizeof(*hdr) + 2 + FT_R1KH_ID_LEN + 2 + r0kh_id_len +
  89. subelem_len)
  90. return -1;
  91. *pos++ = WLAN_EID_FAST_BSS_TRANSITION;
  92. ielen = pos++;
  93. hdr = (struct rsn_ftie *) pos;
  94. os_memset(hdr, 0, sizeof(*hdr));
  95. pos += sizeof(*hdr);
  96. WPA_PUT_LE16(hdr->mic_control, 0);
  97. if (anonce)
  98. os_memcpy(hdr->anonce, anonce, WPA_NONCE_LEN);
  99. if (snonce)
  100. os_memcpy(hdr->snonce, snonce, WPA_NONCE_LEN);
  101. /* Optional Parameters */
  102. *pos++ = FTIE_SUBELEM_R1KH_ID;
  103. *pos++ = FT_R1KH_ID_LEN;
  104. os_memcpy(pos, conf->r1_key_holder, FT_R1KH_ID_LEN);
  105. pos += FT_R1KH_ID_LEN;
  106. if (r0kh_id) {
  107. *pos++ = FTIE_SUBELEM_R0KH_ID;
  108. *pos++ = r0kh_id_len;
  109. os_memcpy(pos, r0kh_id, r0kh_id_len);
  110. pos += r0kh_id_len;
  111. }
  112. if (subelem) {
  113. os_memcpy(pos, subelem, subelem_len);
  114. pos += subelem_len;
  115. }
  116. *ielen = pos - buf - 2;
  117. return pos - buf;
  118. }
  119. struct wpa_ft_pmk_r0_sa {
  120. struct wpa_ft_pmk_r0_sa *next;
  121. u8 pmk_r0[PMK_LEN];
  122. u8 pmk_r0_name[WPA_PMK_NAME_LEN];
  123. u8 spa[ETH_ALEN];
  124. /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
  125. int pmk_r1_pushed;
  126. };
  127. struct wpa_ft_pmk_r1_sa {
  128. struct wpa_ft_pmk_r1_sa *next;
  129. u8 pmk_r1[PMK_LEN];
  130. u8 pmk_r1_name[WPA_PMK_NAME_LEN];
  131. u8 spa[ETH_ALEN];
  132. /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
  133. };
  134. struct wpa_ft_pmk_cache {
  135. struct wpa_ft_pmk_r0_sa *pmk_r0;
  136. struct wpa_ft_pmk_r1_sa *pmk_r1;
  137. };
  138. struct wpa_ft_pmk_cache * wpa_ft_pmk_cache_init(void)
  139. {
  140. struct wpa_ft_pmk_cache *cache;
  141. cache = os_zalloc(sizeof(*cache));
  142. return cache;
  143. }
  144. void wpa_ft_pmk_cache_deinit(struct wpa_ft_pmk_cache *cache)
  145. {
  146. struct wpa_ft_pmk_r0_sa *r0, *r0prev;
  147. struct wpa_ft_pmk_r1_sa *r1, *r1prev;
  148. r0 = cache->pmk_r0;
  149. while (r0) {
  150. r0prev = r0;
  151. r0 = r0->next;
  152. os_memset(r0prev->pmk_r0, 0, PMK_LEN);
  153. os_free(r0prev);
  154. }
  155. r1 = cache->pmk_r1;
  156. while (r1) {
  157. r1prev = r1;
  158. r1 = r1->next;
  159. os_memset(r1prev->pmk_r1, 0, PMK_LEN);
  160. os_free(r1prev);
  161. }
  162. os_free(cache);
  163. }
  164. static int wpa_ft_store_pmk_r0(struct wpa_authenticator *wpa_auth,
  165. const u8 *spa, const u8 *pmk_r0,
  166. const u8 *pmk_r0_name)
  167. {
  168. struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
  169. struct wpa_ft_pmk_r0_sa *r0;
  170. /* TODO: add expiration and limit on number of entries in cache */
  171. r0 = os_zalloc(sizeof(*r0));
  172. if (r0 == NULL)
  173. return -1;
  174. os_memcpy(r0->pmk_r0, pmk_r0, PMK_LEN);
  175. os_memcpy(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN);
  176. os_memcpy(r0->spa, spa, ETH_ALEN);
  177. r0->next = cache->pmk_r0;
  178. cache->pmk_r0 = r0;
  179. return 0;
  180. }
  181. static int wpa_ft_fetch_pmk_r0(struct wpa_authenticator *wpa_auth,
  182. const u8 *spa, const u8 *pmk_r0_name,
  183. u8 *pmk_r0)
  184. {
  185. struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
  186. struct wpa_ft_pmk_r0_sa *r0;
  187. r0 = cache->pmk_r0;
  188. while (r0) {
  189. if (os_memcmp(r0->spa, spa, ETH_ALEN) == 0 &&
  190. os_memcmp(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN)
  191. == 0) {
  192. os_memcpy(pmk_r0, r0->pmk_r0, PMK_LEN);
  193. return 0;
  194. }
  195. r0 = r0->next;
  196. }
  197. return -1;
  198. }
  199. static int wpa_ft_store_pmk_r1(struct wpa_authenticator *wpa_auth,
  200. const u8 *spa, const u8 *pmk_r1,
  201. const u8 *pmk_r1_name)
  202. {
  203. struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
  204. struct wpa_ft_pmk_r1_sa *r1;
  205. /* TODO: add expiration and limit on number of entries in cache */
  206. r1 = os_zalloc(sizeof(*r1));
  207. if (r1 == NULL)
  208. return -1;
  209. os_memcpy(r1->pmk_r1, pmk_r1, PMK_LEN);
  210. os_memcpy(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
  211. os_memcpy(r1->spa, spa, ETH_ALEN);
  212. r1->next = cache->pmk_r1;
  213. cache->pmk_r1 = r1;
  214. return 0;
  215. }
  216. static int wpa_ft_fetch_pmk_r1(struct wpa_authenticator *wpa_auth,
  217. const u8 *spa, const u8 *pmk_r1_name,
  218. u8 *pmk_r1)
  219. {
  220. struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
  221. struct wpa_ft_pmk_r1_sa *r1;
  222. r1 = cache->pmk_r1;
  223. while (r1) {
  224. if (os_memcmp(r1->spa, spa, ETH_ALEN) == 0 &&
  225. os_memcmp(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN)
  226. == 0) {
  227. os_memcpy(pmk_r1, r1->pmk_r1, PMK_LEN);
  228. return 0;
  229. }
  230. r1 = r1->next;
  231. }
  232. return -1;
  233. }
  234. static int wpa_ft_pull_pmk_r1(struct wpa_authenticator *wpa_auth,
  235. const u8 *s1kh_id, const u8 *r0kh_id,
  236. size_t r0kh_id_len, const u8 *pmk_r0_name)
  237. {
  238. struct ft_remote_r0kh *r0kh;
  239. struct ft_r0kh_r1kh_pull_frame frame, f;
  240. r0kh = wpa_auth->conf.r0kh_list;
  241. while (r0kh) {
  242. if (r0kh->id_len == r0kh_id_len &&
  243. os_memcmp(r0kh->id, r0kh_id, r0kh_id_len) == 0)
  244. break;
  245. r0kh = r0kh->next;
  246. }
  247. if (r0kh == NULL)
  248. return -1;
  249. wpa_printf(MSG_DEBUG, "FT: Send PMK-R1 pull request to remote R0KH "
  250. "address " MACSTR, MAC2STR(r0kh->addr));
  251. os_memset(&frame, 0, sizeof(frame));
  252. frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  253. frame.packet_type = FT_PACKET_R0KH_R1KH_PULL;
  254. frame.data_length = host_to_le16(FT_R0KH_R1KH_PULL_DATA_LEN);
  255. os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN);
  256. /* aes_wrap() does not support inplace encryption, so use a temporary
  257. * buffer for the data. */
  258. if (os_get_random(f.nonce, sizeof(f.nonce))) {
  259. wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
  260. "nonce");
  261. return -1;
  262. }
  263. os_memcpy(f.pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN);
  264. os_memcpy(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN);
  265. os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN);
  266. if (aes_wrap(r0kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8,
  267. f.nonce, frame.nonce) < 0)
  268. return -1;
  269. wpa_ft_rrb_send(wpa_auth, r0kh->addr, (u8 *) &frame, sizeof(frame));
  270. return 0;
  271. }
  272. int wpa_auth_derive_ptk_ft(struct wpa_state_machine *sm, const u8 *pmk,
  273. struct wpa_ptk *ptk, size_t ptk_len)
  274. {
  275. u8 pmk_r0[PMK_LEN], pmk_r0_name[WPA_PMK_NAME_LEN];
  276. u8 pmk_r1[PMK_LEN], pmk_r1_name[WPA_PMK_NAME_LEN];
  277. u8 ptk_name[WPA_PMK_NAME_LEN];
  278. const u8 *mdid = sm->wpa_auth->conf.mobility_domain;
  279. const u8 *r0kh = sm->wpa_auth->conf.r0_key_holder;
  280. size_t r0kh_len = sm->wpa_auth->conf.r0_key_holder_len;
  281. const u8 *r1kh = sm->wpa_auth->conf.r1_key_holder;
  282. const u8 *ssid = sm->wpa_auth->conf.ssid;
  283. size_t ssid_len = sm->wpa_auth->conf.ssid_len;
  284. if (sm->xxkey_len == 0) {
  285. wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
  286. "derivation");
  287. return -1;
  288. }
  289. wpa_derive_pmk_r0(sm->xxkey, sm->xxkey_len, ssid, ssid_len, mdid,
  290. r0kh, r0kh_len, sm->addr, pmk_r0, pmk_r0_name);
  291. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", pmk_r0, PMK_LEN);
  292. wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name", pmk_r0_name, WPA_PMK_NAME_LEN);
  293. wpa_ft_store_pmk_r0(sm->wpa_auth, sm->addr, pmk_r0, pmk_r0_name);
  294. wpa_derive_pmk_r1(pmk_r0, pmk_r0_name, r1kh, sm->addr,
  295. pmk_r1, pmk_r1_name);
  296. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", pmk_r1, PMK_LEN);
  297. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", pmk_r1_name, WPA_PMK_NAME_LEN);
  298. wpa_ft_store_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1, pmk_r1_name);
  299. wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr,
  300. sm->wpa_auth->addr, pmk_r1_name,
  301. (u8 *) ptk, ptk_len, ptk_name);
  302. wpa_hexdump_key(MSG_DEBUG, "FT: PTK", (u8 *) ptk, ptk_len);
  303. wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
  304. return 0;
  305. }
  306. static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
  307. const u8 *addr, int idx, u8 *seq)
  308. {
  309. if (wpa_auth->cb.get_seqnum == NULL)
  310. return -1;
  311. return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
  312. }
  313. #ifdef CONFIG_IEEE80211W
  314. static inline int wpa_auth_get_seqnum_igtk(struct wpa_authenticator *wpa_auth,
  315. const u8 *addr, int idx, u8 *seq)
  316. {
  317. if (wpa_auth->cb.get_seqnum_igtk == NULL)
  318. return -1;
  319. return wpa_auth->cb.get_seqnum_igtk(wpa_auth->cb.ctx, addr, idx, seq);
  320. }
  321. #endif /* CONFIG_IEEE80211W */
  322. static u8 * wpa_ft_gtk_subelem(struct wpa_state_machine *sm, size_t *len)
  323. {
  324. u8 *subelem;
  325. struct wpa_group *gsm = sm->group;
  326. size_t subelem_len, pad_len;
  327. const u8 *key;
  328. size_t key_len;
  329. u8 keybuf[32];
  330. key_len = gsm->GTK_len;
  331. if (key_len > sizeof(keybuf))
  332. return NULL;
  333. /*
  334. * Pad key for AES Key Wrap if it is not multiple of 8 bytes or is less
  335. * than 16 bytes.
  336. */
  337. pad_len = key_len % 8;
  338. if (pad_len)
  339. pad_len = 8 - pad_len;
  340. if (key_len + pad_len < 16)
  341. pad_len += 8;
  342. if (pad_len) {
  343. os_memcpy(keybuf, gsm->GTK[gsm->GN - 1], key_len);
  344. os_memset(keybuf + key_len, 0, pad_len);
  345. keybuf[key_len] = 0xdd;
  346. key_len += pad_len;
  347. key = keybuf;
  348. } else
  349. key = gsm->GTK[gsm->GN - 1];
  350. /*
  351. * Sub-elem ID[1] | Length[1] | Key Info[1] | Key Length[1] | RSC[8] |
  352. * Key[5..32].
  353. */
  354. subelem_len = 12 + key_len + 8;
  355. subelem = os_zalloc(subelem_len);
  356. if (subelem == NULL)
  357. return NULL;
  358. subelem[0] = FTIE_SUBELEM_GTK;
  359. subelem[1] = 10 + key_len + 8;
  360. subelem[2] = gsm->GN & 0x03; /* Key ID in B0-B1 of Key Info */
  361. subelem[3] = gsm->GTK_len;
  362. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, subelem + 4);
  363. if (aes_wrap(sm->PTK.kek, key_len / 8, key, subelem + 12)) {
  364. os_free(subelem);
  365. return NULL;
  366. }
  367. *len = subelem_len;
  368. return subelem;
  369. }
  370. #ifdef CONFIG_IEEE80211W
  371. static u8 * wpa_ft_igtk_subelem(struct wpa_state_machine *sm, size_t *len)
  372. {
  373. u8 *subelem, *pos;
  374. struct wpa_group *gsm = sm->group;
  375. size_t subelem_len;
  376. /* Sub-elem ID[1] | Length[1] | KeyID[2] | IPN[6] | Key Length[1] |
  377. * Key[16+8] */
  378. subelem_len = 1 + 1 + 2 + 6 + 1 + WPA_IGTK_LEN + 8;
  379. subelem = os_zalloc(subelem_len);
  380. if (subelem == NULL)
  381. return NULL;
  382. pos = subelem;
  383. *pos++ = FTIE_SUBELEM_IGTK;
  384. *pos++ = subelem_len - 2;
  385. WPA_PUT_LE16(pos, gsm->GN_igtk);
  386. pos += 2;
  387. wpa_auth_get_seqnum_igtk(sm->wpa_auth, NULL, gsm->GN_igtk, pos);
  388. pos += 6;
  389. *pos++ = WPA_IGTK_LEN;
  390. if (aes_wrap(sm->PTK.kek, WPA_IGTK_LEN / 8,
  391. gsm->IGTK[gsm->GN_igtk - 4], pos)) {
  392. os_free(subelem);
  393. return NULL;
  394. }
  395. *len = subelem_len;
  396. return subelem;
  397. }
  398. #endif /* CONFIG_IEEE80211W */
  399. static u8 * wpa_ft_process_rdie(u8 *pos, u8 *end, u8 id, u8 descr_count,
  400. const u8 *ies, size_t ies_len)
  401. {
  402. struct ieee802_11_elems parse;
  403. struct rsn_rdie *rdie;
  404. wpa_printf(MSG_DEBUG, "FT: Resource Request: id=%d descr_count=%d",
  405. id, descr_count);
  406. wpa_hexdump(MSG_MSGDUMP, "FT: Resource descriptor IE(s)",
  407. ies, ies_len);
  408. if (end - pos < (int) sizeof(*rdie)) {
  409. wpa_printf(MSG_ERROR, "FT: Not enough room for response RDIE");
  410. return pos;
  411. }
  412. *pos++ = WLAN_EID_RIC_DATA;
  413. *pos++ = sizeof(*rdie);
  414. rdie = (struct rsn_rdie *) pos;
  415. rdie->id = id;
  416. rdie->descr_count = 0;
  417. rdie->status_code = host_to_le16(WLAN_STATUS_SUCCESS);
  418. pos += sizeof(*rdie);
  419. if (ieee802_11_parse_elems((u8 *) ies, ies_len, &parse, 1) ==
  420. ParseFailed) {
  421. wpa_printf(MSG_DEBUG, "FT: Failed to parse request IEs");
  422. rdie->status_code =
  423. host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
  424. return pos;
  425. }
  426. if (parse.wmm_tspec) {
  427. struct wmm_tspec_element *tspec;
  428. int res;
  429. if (parse.wmm_tspec_len + 2 < (int) sizeof(*tspec)) {
  430. wpa_printf(MSG_DEBUG, "FT: Too short WMM TSPEC IE "
  431. "(%d)", (int) parse.wmm_tspec_len);
  432. rdie->status_code =
  433. host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
  434. return pos;
  435. }
  436. if (end - pos < (int) sizeof(*tspec)) {
  437. wpa_printf(MSG_ERROR, "FT: Not enough room for "
  438. "response TSPEC");
  439. rdie->status_code =
  440. host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
  441. return pos;
  442. }
  443. tspec = (struct wmm_tspec_element *) pos;
  444. os_memcpy(tspec, parse.wmm_tspec - 2, sizeof(*tspec));
  445. res = wmm_process_tspec(tspec);
  446. wpa_printf(MSG_DEBUG, "FT: ADDTS processing result: %d", res);
  447. if (res == WMM_ADDTS_STATUS_INVALID_PARAMETERS)
  448. rdie->status_code =
  449. host_to_le16(WLAN_STATUS_INVALID_PARAMETERS);
  450. else if (res == WMM_ADDTS_STATUS_REFUSED)
  451. rdie->status_code =
  452. host_to_le16(WLAN_STATUS_REQUEST_DECLINED);
  453. else {
  454. /* TSPEC accepted; include updated TSPEC in response */
  455. rdie->descr_count = 1;
  456. pos += sizeof(*tspec);
  457. }
  458. return pos;
  459. }
  460. wpa_printf(MSG_DEBUG, "FT: No supported resource requested");
  461. rdie->status_code = host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
  462. return pos;
  463. }
  464. static u8 * wpa_ft_process_ric(u8 *pos, u8 *end, const u8 *ric, size_t ric_len)
  465. {
  466. const u8 *rpos, *start;
  467. const struct rsn_rdie *rdie;
  468. wpa_hexdump(MSG_MSGDUMP, "FT: RIC Request", ric, ric_len);
  469. rpos = ric;
  470. while (rpos + sizeof(*rdie) < ric + ric_len) {
  471. if (rpos[0] != WLAN_EID_RIC_DATA || rpos[1] < sizeof(*rdie) ||
  472. rpos + 2 + rpos[1] > ric + ric_len)
  473. break;
  474. rdie = (const struct rsn_rdie *) (rpos + 2);
  475. rpos += 2 + rpos[1];
  476. start = rpos;
  477. while (rpos + 2 <= ric + ric_len &&
  478. rpos + 2 + rpos[1] <= ric + ric_len) {
  479. if (rpos[0] == WLAN_EID_RIC_DATA)
  480. break;
  481. rpos += 2 + rpos[1];
  482. }
  483. pos = wpa_ft_process_rdie(pos, end, rdie->id,
  484. rdie->descr_count,
  485. start, rpos - start);
  486. }
  487. return pos;
  488. }
  489. u8 * wpa_sm_write_assoc_resp_ies(struct wpa_state_machine *sm, u8 *pos,
  490. size_t max_len, int auth_alg,
  491. const u8 *req_ies, size_t req_ies_len)
  492. {
  493. u8 *end, *mdie, *ftie, *rsnie, *r0kh_id, *subelem = NULL;
  494. size_t mdie_len, ftie_len, rsnie_len, r0kh_id_len, subelem_len = 0;
  495. int res;
  496. struct wpa_auth_config *conf;
  497. struct rsn_ftie *_ftie;
  498. struct wpa_ft_ies parse;
  499. u8 *ric_start;
  500. if (sm == NULL)
  501. return pos;
  502. conf = &sm->wpa_auth->conf;
  503. if (sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
  504. sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_PSK)
  505. return pos;
  506. end = pos + max_len;
  507. /* RSN */
  508. res = wpa_write_rsn_ie(conf, pos, end - pos, sm->pmk_r1_name);
  509. if (res < 0)
  510. return pos;
  511. rsnie = pos;
  512. rsnie_len = res;
  513. pos += res;
  514. /* Mobility Domain Information */
  515. res = wpa_write_mdie(conf, pos, end - pos);
  516. if (res < 0)
  517. return pos;
  518. mdie = pos;
  519. mdie_len = res;
  520. pos += res;
  521. /* Fast BSS Transition Information */
  522. if (auth_alg == WLAN_AUTH_FT) {
  523. subelem = wpa_ft_gtk_subelem(sm, &subelem_len);
  524. r0kh_id = sm->r0kh_id;
  525. r0kh_id_len = sm->r0kh_id_len;
  526. #ifdef CONFIG_IEEE80211W
  527. if (sm->mgmt_frame_prot) {
  528. u8 *igtk;
  529. size_t igtk_len;
  530. u8 *nbuf;
  531. igtk = wpa_ft_igtk_subelem(sm, &igtk_len);
  532. if (igtk == NULL) {
  533. os_free(subelem);
  534. return pos;
  535. }
  536. nbuf = os_realloc(subelem, subelem_len + igtk_len);
  537. if (nbuf == NULL) {
  538. os_free(subelem);
  539. os_free(igtk);
  540. return pos;
  541. }
  542. subelem = nbuf;
  543. os_memcpy(subelem + subelem_len, igtk, igtk_len);
  544. subelem_len += igtk_len;
  545. os_free(igtk);
  546. }
  547. #endif /* CONFIG_IEEE80211W */
  548. } else {
  549. r0kh_id = conf->r0_key_holder;
  550. r0kh_id_len = conf->r0_key_holder_len;
  551. }
  552. res = wpa_write_ftie(conf, r0kh_id, r0kh_id_len, NULL, NULL, pos,
  553. end - pos, subelem, subelem_len);
  554. os_free(subelem);
  555. if (res < 0)
  556. return pos;
  557. ftie = pos;
  558. ftie_len = res;
  559. pos += res;
  560. _ftie = (struct rsn_ftie *) (ftie + 2);
  561. _ftie->mic_control[1] = 3; /* Information element count */
  562. ric_start = pos;
  563. if (wpa_ft_parse_ies(req_ies, req_ies_len, &parse) == 0 && parse.ric) {
  564. pos = wpa_ft_process_ric(pos, end, parse.ric, parse.ric_len);
  565. _ftie->mic_control[1] += ieee802_11_ie_count(ric_start,
  566. pos - ric_start);
  567. }
  568. if (ric_start == pos)
  569. ric_start = NULL;
  570. if (wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 6,
  571. mdie, mdie_len, ftie, ftie_len,
  572. rsnie, rsnie_len,
  573. ric_start, ric_start ? pos - ric_start : 0,
  574. _ftie->mic) < 0)
  575. wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
  576. return pos;
  577. }
  578. static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len,
  579. struct wpa_ft_ies *parse)
  580. {
  581. const u8 *end, *pos;
  582. parse->ftie = ie;
  583. parse->ftie_len = ie_len;
  584. pos = ie + sizeof(struct rsn_ftie);
  585. end = ie + ie_len;
  586. while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
  587. switch (pos[0]) {
  588. case FTIE_SUBELEM_R1KH_ID:
  589. if (pos[1] != FT_R1KH_ID_LEN) {
  590. wpa_printf(MSG_DEBUG, "FT: Invalid R1KH-ID "
  591. "length in FTIE: %d", pos[1]);
  592. return -1;
  593. }
  594. parse->r1kh_id = pos + 2;
  595. break;
  596. case FTIE_SUBELEM_GTK:
  597. parse->gtk = pos + 2;
  598. parse->gtk_len = pos[1];
  599. break;
  600. case FTIE_SUBELEM_R0KH_ID:
  601. if (pos[1] < 1 || pos[1] > FT_R0KH_ID_MAX_LEN) {
  602. wpa_printf(MSG_DEBUG, "FT: Invalid R0KH-ID "
  603. "length in FTIE: %d", pos[1]);
  604. return -1;
  605. }
  606. parse->r0kh_id = pos + 2;
  607. parse->r0kh_id_len = pos[1];
  608. break;
  609. }
  610. pos += 2 + pos[1];
  611. }
  612. return 0;
  613. }
  614. static int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
  615. struct wpa_ft_ies *parse)
  616. {
  617. const u8 *end, *pos;
  618. struct wpa_ie_data data;
  619. int ret;
  620. const struct rsn_ftie *ftie;
  621. int prot_ie_count = 0;
  622. os_memset(parse, 0, sizeof(*parse));
  623. if (ies == NULL)
  624. return 0;
  625. pos = ies;
  626. end = ies + ies_len;
  627. while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
  628. switch (pos[0]) {
  629. case WLAN_EID_RSN:
  630. parse->rsn = pos + 2;
  631. parse->rsn_len = pos[1];
  632. ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2,
  633. parse->rsn_len + 2,
  634. &data);
  635. if (ret < 0) {
  636. wpa_printf(MSG_DEBUG, "FT: Failed to parse "
  637. "RSN IE: %d", ret);
  638. return -1;
  639. }
  640. if (data.num_pmkid == 1 && data.pmkid)
  641. parse->rsn_pmkid = data.pmkid;
  642. break;
  643. case WLAN_EID_MOBILITY_DOMAIN:
  644. parse->mdie = pos + 2;
  645. parse->mdie_len = pos[1];
  646. break;
  647. case WLAN_EID_FAST_BSS_TRANSITION:
  648. if (pos[1] < sizeof(*ftie))
  649. return -1;
  650. ftie = (const struct rsn_ftie *) (pos + 2);
  651. prot_ie_count = ftie->mic_control[1];
  652. if (wpa_ft_parse_ftie(pos + 2, pos[1], parse) < 0)
  653. return -1;
  654. break;
  655. case WLAN_EID_RIC_DATA:
  656. if (parse->ric == NULL)
  657. parse->ric = pos;
  658. }
  659. pos += 2 + pos[1];
  660. }
  661. if (prot_ie_count == 0)
  662. return 0; /* no MIC */
  663. /*
  664. * Check that the protected IE count matches with IEs included in the
  665. * frame.
  666. */
  667. if (parse->rsn)
  668. prot_ie_count--;
  669. if (parse->mdie)
  670. prot_ie_count--;
  671. if (parse->ftie)
  672. prot_ie_count--;
  673. if (prot_ie_count < 0) {
  674. wpa_printf(MSG_DEBUG, "FT: Some required IEs not included in "
  675. "the protected IE count");
  676. return -1;
  677. }
  678. if (prot_ie_count == 0 && parse->ric) {
  679. wpa_printf(MSG_DEBUG, "FT: RIC IE(s) in the frame, but not "
  680. "included in protected IE count");
  681. return -1;
  682. }
  683. /* Determine the end of the RIC IE(s) */
  684. pos = parse->ric;
  685. while (pos && pos + 2 <= end && pos + 2 + pos[1] <= end &&
  686. prot_ie_count) {
  687. prot_ie_count--;
  688. pos += 2 + pos[1];
  689. }
  690. parse->ric_len = pos - parse->ric;
  691. if (prot_ie_count) {
  692. wpa_printf(MSG_DEBUG, "FT: %d protected IEs missing from "
  693. "frame", (int) prot_ie_count);
  694. return -1;
  695. }
  696. return 0;
  697. }
  698. static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
  699. int vlan_id,
  700. wpa_alg alg, const u8 *addr, int idx,
  701. u8 *key, size_t key_len)
  702. {
  703. if (wpa_auth->cb.set_key == NULL)
  704. return -1;
  705. return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
  706. key, key_len);
  707. }
  708. static void wpa_ft_install_ptk(struct wpa_state_machine *sm)
  709. {
  710. wpa_alg alg;
  711. int klen;
  712. /* MLME-SETKEYS.request(PTK) */
  713. if (sm->pairwise == WPA_CIPHER_TKIP) {
  714. alg = WPA_ALG_TKIP;
  715. klen = 32;
  716. } else if (sm->pairwise == WPA_CIPHER_CCMP) {
  717. alg = WPA_ALG_CCMP;
  718. klen = 16;
  719. } else
  720. return;
  721. /* FIX: add STA entry to kernel/driver here? The set_key will fail
  722. * most likely without this.. At the moment, STA entry is added only
  723. * after association has been completed. Alternatively, could
  724. * re-configure PTK at that point(?).
  725. */
  726. if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
  727. sm->PTK.tk1, klen))
  728. return;
  729. /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
  730. sm->pairwise_set = TRUE;
  731. }
  732. static u16 wpa_ft_process_auth_req(struct wpa_state_machine *sm,
  733. const u8 *ies, size_t ies_len,
  734. u8 **resp_ies, size_t *resp_ies_len)
  735. {
  736. struct rsn_mdie *mdie;
  737. struct rsn_ftie *ftie;
  738. u8 pmk_r1[PMK_LEN], pmk_r1_name[WPA_PMK_NAME_LEN];
  739. u8 ptk_name[WPA_PMK_NAME_LEN];
  740. struct wpa_auth_config *conf;
  741. struct wpa_ft_ies parse;
  742. size_t buflen, ptk_len;
  743. int ret;
  744. u8 *pos, *end;
  745. *resp_ies = NULL;
  746. *resp_ies_len = 0;
  747. sm->pmk_r1_name_valid = 0;
  748. conf = &sm->wpa_auth->conf;
  749. wpa_hexdump(MSG_DEBUG, "FT: Received authentication frame IEs",
  750. ies, ies_len);
  751. if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
  752. wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
  753. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  754. }
  755. mdie = (struct rsn_mdie *) parse.mdie;
  756. if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
  757. os_memcmp(mdie->mobility_domain,
  758. sm->wpa_auth->conf.mobility_domain,
  759. MOBILITY_DOMAIN_ID_LEN) != 0) {
  760. wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
  761. return WLAN_STATUS_INVALID_MDIE;
  762. }
  763. ftie = (struct rsn_ftie *) parse.ftie;
  764. if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
  765. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
  766. return WLAN_STATUS_INVALID_FTIE;
  767. }
  768. os_memcpy(sm->SNonce, ftie->snonce, WPA_NONCE_LEN);
  769. if (parse.r0kh_id == NULL) {
  770. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE - no R0KH-ID");
  771. return WLAN_STATUS_INVALID_FTIE;
  772. }
  773. wpa_hexdump(MSG_DEBUG, "FT: STA R0KH-ID",
  774. parse.r0kh_id, parse.r0kh_id_len);
  775. os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
  776. sm->r0kh_id_len = parse.r0kh_id_len;
  777. if (parse.rsn_pmkid == NULL) {
  778. wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
  779. return WLAN_STATUS_INVALID_PMKID;
  780. }
  781. wpa_hexdump(MSG_DEBUG, "FT: Requested PMKR0Name",
  782. parse.rsn_pmkid, WPA_PMK_NAME_LEN);
  783. wpa_derive_pmk_r1_name(parse.rsn_pmkid,
  784. sm->wpa_auth->conf.r1_key_holder, sm->addr,
  785. pmk_r1_name);
  786. wpa_hexdump(MSG_DEBUG, "FT: Derived requested PMKR1Name",
  787. pmk_r1_name, WPA_PMK_NAME_LEN);
  788. if (wpa_ft_fetch_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1_name, pmk_r1) <
  789. 0) {
  790. if (wpa_ft_pull_pmk_r1(sm->wpa_auth, sm->addr, sm->r0kh_id,
  791. sm->r0kh_id_len, parse.rsn_pmkid) < 0) {
  792. wpa_printf(MSG_DEBUG, "FT: Did not have matching "
  793. "PMK-R1 and unknown R0KH-ID");
  794. return WLAN_STATUS_INVALID_PMKID;
  795. }
  796. /*
  797. * TODO: Should return "status pending" (and the caller should
  798. * not send out response now). The real response will be sent
  799. * once the response from R0KH is received.
  800. */
  801. return WLAN_STATUS_INVALID_PMKID;
  802. }
  803. wpa_hexdump_key(MSG_DEBUG, "FT: Selected PMK-R1", pmk_r1, PMK_LEN);
  804. sm->pmk_r1_name_valid = 1;
  805. os_memcpy(sm->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
  806. if (os_get_random(sm->ANonce, WPA_NONCE_LEN)) {
  807. wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
  808. "ANonce");
  809. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  810. }
  811. wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
  812. sm->SNonce, WPA_NONCE_LEN);
  813. wpa_hexdump(MSG_DEBUG, "FT: Generated ANonce",
  814. sm->ANonce, WPA_NONCE_LEN);
  815. ptk_len = sm->pairwise == WPA_CIPHER_CCMP ? 48 : 64;
  816. wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr,
  817. sm->wpa_auth->addr, pmk_r1_name,
  818. (u8 *) &sm->PTK, ptk_len, ptk_name);
  819. wpa_hexdump_key(MSG_DEBUG, "FT: PTK",
  820. (u8 *) &sm->PTK, ptk_len);
  821. wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
  822. wpa_ft_install_ptk(sm);
  823. buflen = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
  824. 2 + FT_R1KH_ID_LEN + 200;
  825. *resp_ies = os_zalloc(buflen);
  826. if (*resp_ies == NULL) {
  827. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  828. }
  829. pos = *resp_ies;
  830. end = *resp_ies + buflen;
  831. ret = wpa_write_rsn_ie(conf, pos, end - pos, parse.rsn_pmkid);
  832. if (ret < 0) {
  833. os_free(*resp_ies);
  834. *resp_ies = NULL;
  835. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  836. }
  837. pos += ret;
  838. ret = wpa_write_mdie(conf, pos, end - pos);
  839. if (ret < 0) {
  840. os_free(*resp_ies);
  841. *resp_ies = NULL;
  842. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  843. }
  844. pos += ret;
  845. ret = wpa_write_ftie(conf, parse.r0kh_id, parse.r0kh_id_len,
  846. sm->ANonce, sm->SNonce, pos, end - pos, NULL, 0);
  847. if (ret < 0) {
  848. os_free(*resp_ies);
  849. *resp_ies = NULL;
  850. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  851. }
  852. pos += ret;
  853. *resp_ies_len = pos - *resp_ies;
  854. return WLAN_STATUS_SUCCESS;
  855. }
  856. void wpa_ft_process_auth(struct wpa_state_machine *sm, const u8 *bssid,
  857. u16 auth_transaction, const u8 *ies, size_t ies_len,
  858. void (*cb)(void *ctx, const u8 *dst, const u8 *bssid,
  859. u16 auth_transaction, u16 status,
  860. const u8 *ies, size_t ies_len),
  861. void *ctx)
  862. {
  863. u16 status;
  864. u8 *resp_ies;
  865. size_t resp_ies_len;
  866. if (sm == NULL) {
  867. wpa_printf(MSG_DEBUG, "FT: Received authentication frame, but "
  868. "WPA SM not available");
  869. return;
  870. }
  871. wpa_printf(MSG_DEBUG, "FT: Received authentication frame: STA=" MACSTR
  872. " BSSID=" MACSTR " transaction=%d",
  873. MAC2STR(sm->addr), MAC2STR(bssid), auth_transaction);
  874. status = wpa_ft_process_auth_req(sm, ies, ies_len, &resp_ies,
  875. &resp_ies_len);
  876. wpa_printf(MSG_DEBUG, "FT: FT authentication response: dst=" MACSTR
  877. " auth_transaction=%d status=%d",
  878. MAC2STR(sm->addr), auth_transaction + 1, status);
  879. wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
  880. cb(ctx, sm->addr, bssid, auth_transaction + 1, status,
  881. resp_ies, resp_ies_len);
  882. os_free(resp_ies);
  883. }
  884. u16 wpa_ft_validate_reassoc(struct wpa_state_machine *sm, const u8 *ies,
  885. size_t ies_len)
  886. {
  887. struct wpa_ft_ies parse;
  888. struct rsn_mdie *mdie;
  889. struct rsn_ftie *ftie;
  890. u8 mic[16];
  891. if (sm == NULL)
  892. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  893. wpa_hexdump(MSG_DEBUG, "FT: Reassoc Req IEs", ies, ies_len);
  894. if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
  895. wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
  896. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  897. }
  898. if (parse.rsn == NULL) {
  899. wpa_printf(MSG_DEBUG, "FT: No RSNIE in Reassoc Req");
  900. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  901. }
  902. if (parse.rsn_pmkid == NULL) {
  903. wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
  904. return WLAN_STATUS_INVALID_PMKID;
  905. }
  906. if (os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
  907. {
  908. wpa_printf(MSG_DEBUG, "FT: PMKID in Reassoc Req did not match "
  909. "with the PMKR1Name derived from auth request");
  910. return WLAN_STATUS_INVALID_PMKID;
  911. }
  912. mdie = (struct rsn_mdie *) parse.mdie;
  913. if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
  914. os_memcmp(mdie->mobility_domain,
  915. sm->wpa_auth->conf.mobility_domain,
  916. MOBILITY_DOMAIN_ID_LEN) != 0) {
  917. wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
  918. return WLAN_STATUS_INVALID_MDIE;
  919. }
  920. ftie = (struct rsn_ftie *) parse.ftie;
  921. if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
  922. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
  923. return WLAN_STATUS_INVALID_FTIE;
  924. }
  925. if (wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 5,
  926. parse.mdie - 2, parse.mdie_len + 2,
  927. parse.ftie - 2, parse.ftie_len + 2,
  928. parse.rsn - 2, parse.rsn_len + 2,
  929. parse.ric, parse.ric_len,
  930. mic) < 0) {
  931. wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
  932. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  933. }
  934. if (os_memcmp(mic, ftie->mic, 16) != 0) {
  935. wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
  936. wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16);
  937. wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
  938. return WLAN_STATUS_INVALID_FTIE;
  939. }
  940. return WLAN_STATUS_SUCCESS;
  941. }
  942. int wpa_ft_action_rx(struct wpa_state_machine *sm, const u8 *data, size_t len)
  943. {
  944. const u8 *sta_addr, *target_ap;
  945. const u8 *ies;
  946. size_t ies_len;
  947. u8 action;
  948. struct ft_rrb_frame *frame;
  949. if (sm == NULL)
  950. return -1;
  951. /*
  952. * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
  953. * FT Request action frame body[variable]
  954. */
  955. if (len < 14) {
  956. wpa_printf(MSG_DEBUG, "FT: Too short FT Action frame "
  957. "(len=%lu)", (unsigned long) len);
  958. return -1;
  959. }
  960. action = data[1];
  961. sta_addr = data + 2;
  962. target_ap = data + 8;
  963. ies = data + 14;
  964. ies_len = len - 14;
  965. wpa_printf(MSG_DEBUG, "FT: Received FT Action frame (STA=" MACSTR
  966. " Target AP=" MACSTR " Action=%d)",
  967. MAC2STR(sta_addr), MAC2STR(target_ap), action);
  968. if (os_memcmp(sta_addr, sm->addr, ETH_ALEN) != 0) {
  969. wpa_printf(MSG_DEBUG, "FT: Mismatch in FT Action STA address: "
  970. "STA=" MACSTR " STA-Address=" MACSTR,
  971. MAC2STR(sm->addr), MAC2STR(sta_addr));
  972. return -1;
  973. }
  974. /*
  975. * Do some sanity checking on the target AP address (not own and not
  976. * broadcast. This could be extended to filter based on a list of known
  977. * APs in the MD (if such a list were configured).
  978. */
  979. if ((target_ap[0] & 0x01) ||
  980. os_memcmp(target_ap, sm->wpa_auth->addr, ETH_ALEN) == 0) {
  981. wpa_printf(MSG_DEBUG, "FT: Invalid Target AP in FT Action "
  982. "frame");
  983. return -1;
  984. }
  985. wpa_hexdump(MSG_MSGDUMP, "FT: Action frame body", ies, ies_len);
  986. /* RRB - Forward action frame to the target AP */
  987. frame = os_malloc(sizeof(*frame) + len);
  988. frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  989. frame->packet_type = FT_PACKET_REQUEST;
  990. frame->action_length = host_to_le16(len);
  991. os_memcpy(frame->ap_address, sm->wpa_auth->addr, ETH_ALEN);
  992. os_memcpy(frame + 1, data, len);
  993. wpa_ft_rrb_send(sm->wpa_auth, target_ap, (u8 *) frame,
  994. sizeof(*frame) + len);
  995. os_free(frame);
  996. return 0;
  997. }
  998. static int wpa_ft_rrb_rx_request(struct wpa_authenticator *wpa_auth,
  999. const u8 *current_ap, const u8 *sta_addr,
  1000. const u8 *body, size_t len)
  1001. {
  1002. struct wpa_state_machine *sm;
  1003. u16 status;
  1004. u8 *resp_ies, *pos;
  1005. size_t resp_ies_len, rlen;
  1006. struct ft_rrb_frame *frame;
  1007. sm = wpa_ft_add_sta(wpa_auth, sta_addr);
  1008. if (sm == NULL) {
  1009. wpa_printf(MSG_DEBUG, "FT: Failed to add new STA based on "
  1010. "RRB Request");
  1011. return -1;
  1012. }
  1013. wpa_hexdump(MSG_MSGDUMP, "FT: RRB Request Frame body", body, len);
  1014. status = wpa_ft_process_auth_req(sm, body, len, &resp_ies,
  1015. &resp_ies_len);
  1016. wpa_printf(MSG_DEBUG, "FT: RRB authentication response: STA=" MACSTR
  1017. " CurrentAP=" MACSTR " status=%d",
  1018. MAC2STR(sm->addr), MAC2STR(current_ap), status);
  1019. wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
  1020. /* RRB - Forward action frame response to the Current AP */
  1021. /*
  1022. * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
  1023. * Status_Code[2] FT Request action frame body[variable]
  1024. */
  1025. rlen = 2 + 2 * ETH_ALEN + 2 + resp_ies_len;
  1026. frame = os_malloc(sizeof(*frame) + rlen);
  1027. frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  1028. frame->packet_type = FT_PACKET_RESPONSE;
  1029. frame->action_length = host_to_le16(rlen);
  1030. os_memcpy(frame->ap_address, wpa_auth->addr, ETH_ALEN);
  1031. pos = (u8 *) (frame + 1);
  1032. *pos++ = WLAN_ACTION_FT;
  1033. *pos++ = 2; /* Action: Response */
  1034. os_memcpy(pos, sta_addr, ETH_ALEN);
  1035. pos += ETH_ALEN;
  1036. os_memcpy(pos, wpa_auth->addr, ETH_ALEN);
  1037. pos += ETH_ALEN;
  1038. WPA_PUT_LE16(pos, status);
  1039. pos += 2;
  1040. if (resp_ies) {
  1041. os_memcpy(pos, resp_ies, resp_ies_len);
  1042. os_free(resp_ies);
  1043. }
  1044. wpa_ft_rrb_send(wpa_auth, current_ap, (u8 *) frame,
  1045. sizeof(*frame) + rlen);
  1046. os_free(frame);
  1047. return 0;
  1048. }
  1049. static int wpa_ft_rrb_rx_pull(struct wpa_authenticator *wpa_auth,
  1050. const u8 *src_addr,
  1051. const u8 *data, size_t data_len)
  1052. {
  1053. struct ft_r0kh_r1kh_pull_frame *frame, f;
  1054. struct ft_remote_r1kh *r1kh;
  1055. struct ft_r0kh_r1kh_resp_frame resp, r;
  1056. u8 pmk_r0[PMK_LEN];
  1057. wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull");
  1058. if (data_len < sizeof(*frame))
  1059. return -1;
  1060. r1kh = wpa_auth->conf.r1kh_list;
  1061. while (r1kh) {
  1062. if (os_memcmp(r1kh->addr, src_addr, ETH_ALEN) == 0)
  1063. break;
  1064. r1kh = r1kh->next;
  1065. }
  1066. if (r1kh == NULL) {
  1067. wpa_printf(MSG_DEBUG, "FT: No matching R1KH address found for "
  1068. "PMK-R1 pull source address " MACSTR,
  1069. MAC2STR(src_addr));
  1070. return -1;
  1071. }
  1072. frame = (struct ft_r0kh_r1kh_pull_frame *) data;
  1073. /* aes_unwrap() does not support inplace decryption, so use a temporary
  1074. * buffer for the data. */
  1075. if (aes_unwrap(r1kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8,
  1076. frame->nonce, f.nonce) < 0) {
  1077. wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
  1078. "request from " MACSTR, MAC2STR(src_addr));
  1079. return -1;
  1080. }
  1081. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
  1082. f.nonce, sizeof(f.nonce));
  1083. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR0Name",
  1084. f.pmk_r0_name, WPA_PMK_NAME_LEN);
  1085. wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="
  1086. MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
  1087. os_memset(&resp, 0, sizeof(resp));
  1088. resp.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  1089. resp.packet_type = FT_PACKET_R0KH_R1KH_RESP;
  1090. resp.data_length = host_to_le16(FT_R0KH_R1KH_RESP_DATA_LEN);
  1091. os_memcpy(resp.ap_address, wpa_auth->addr, ETH_ALEN);
  1092. /* aes_wrap() does not support inplace encryption, so use a temporary
  1093. * buffer for the data. */
  1094. os_memcpy(r.nonce, f.nonce, sizeof(f.nonce));
  1095. os_memcpy(r.r1kh_id, f.r1kh_id, FT_R1KH_ID_LEN);
  1096. os_memcpy(r.s1kh_id, f.s1kh_id, ETH_ALEN);
  1097. if (wpa_ft_fetch_pmk_r0(wpa_auth, f.s1kh_id, f.pmk_r0_name, pmk_r0) <
  1098. 0) {
  1099. wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name found for "
  1100. "PMK-R1 pull");
  1101. return -1;
  1102. }
  1103. wpa_derive_pmk_r1(pmk_r0, f.pmk_r0_name, f.r1kh_id, f.s1kh_id,
  1104. r.pmk_r1, r.pmk_r1_name);
  1105. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", r.pmk_r1, PMK_LEN);
  1106. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", r.pmk_r1_name,
  1107. WPA_PMK_NAME_LEN);
  1108. if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
  1109. r.nonce, resp.nonce) < 0) {
  1110. os_memset(pmk_r0, 0, PMK_LEN);
  1111. return -1;
  1112. }
  1113. os_memset(pmk_r0, 0, PMK_LEN);
  1114. wpa_ft_rrb_send(wpa_auth, src_addr, (u8 *) &resp, sizeof(resp));
  1115. return 0;
  1116. }
  1117. static int wpa_ft_rrb_rx_resp(struct wpa_authenticator *wpa_auth,
  1118. const u8 *src_addr,
  1119. const u8 *data, size_t data_len)
  1120. {
  1121. struct ft_r0kh_r1kh_resp_frame *frame, f;
  1122. struct ft_remote_r0kh *r0kh;
  1123. wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull response");
  1124. if (data_len < sizeof(*frame))
  1125. return -1;
  1126. r0kh = wpa_auth->conf.r0kh_list;
  1127. while (r0kh) {
  1128. if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
  1129. break;
  1130. r0kh = r0kh->next;
  1131. }
  1132. if (r0kh == NULL) {
  1133. wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
  1134. "PMK-R0 pull response source address " MACSTR,
  1135. MAC2STR(src_addr));
  1136. return -1;
  1137. }
  1138. frame = (struct ft_r0kh_r1kh_resp_frame *) data;
  1139. /* aes_unwrap() does not support inplace decryption, so use a temporary
  1140. * buffer for the data. */
  1141. if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
  1142. frame->nonce, f.nonce) < 0) {
  1143. wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
  1144. "response from " MACSTR, MAC2STR(src_addr));
  1145. return -1;
  1146. }
  1147. if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)
  1148. != 0) {
  1149. wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull response did not use a "
  1150. "matching R1KH-ID");
  1151. return -1;
  1152. }
  1153. /* TODO: verify that <nonce,s1kh_id> matches with a pending request
  1154. * and call this requests callback function to finish request
  1155. * processing */
  1156. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
  1157. f.nonce, sizeof(f.nonce));
  1158. wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="
  1159. MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
  1160. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 pull - PMK-R1",
  1161. f.pmk_r1, PMK_LEN);
  1162. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR1Name",
  1163. f.pmk_r1_name, WPA_PMK_NAME_LEN);
  1164. wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name);
  1165. os_memset(f.pmk_r1, 0, PMK_LEN);
  1166. return 0;
  1167. }
  1168. static int wpa_ft_rrb_rx_push(struct wpa_authenticator *wpa_auth,
  1169. const u8 *src_addr,
  1170. const u8 *data, size_t data_len)
  1171. {
  1172. struct ft_r0kh_r1kh_push_frame *frame, f;
  1173. struct ft_remote_r0kh *r0kh;
  1174. struct os_time now;
  1175. os_time_t tsend;
  1176. wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 push");
  1177. if (data_len < sizeof(*frame))
  1178. return -1;
  1179. r0kh = wpa_auth->conf.r0kh_list;
  1180. while (r0kh) {
  1181. if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
  1182. break;
  1183. r0kh = r0kh->next;
  1184. }
  1185. if (r0kh == NULL) {
  1186. wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
  1187. "PMK-R0 push source address " MACSTR,
  1188. MAC2STR(src_addr));
  1189. return -1;
  1190. }
  1191. frame = (struct ft_r0kh_r1kh_push_frame *) data;
  1192. /* aes_unwrap() does not support inplace decryption, so use a temporary
  1193. * buffer for the data. */
  1194. if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
  1195. frame->timestamp, f.timestamp) < 0) {
  1196. wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 push from "
  1197. MACSTR, MAC2STR(src_addr));
  1198. return -1;
  1199. }
  1200. os_get_time(&now);
  1201. tsend = WPA_GET_LE32(f.timestamp);
  1202. if ((now.sec > tsend && now.sec - tsend > 60) ||
  1203. (now.sec < tsend && tsend - now.sec > 60)) {
  1204. wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not have a valid "
  1205. "timestamp: sender time %d own time %d\n",
  1206. (int) tsend, (int) now.sec);
  1207. return -1;
  1208. }
  1209. if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)
  1210. != 0) {
  1211. wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not use a matching "
  1212. "R1KH-ID (received " MACSTR " own " MACSTR ")",
  1213. MAC2STR(f.r1kh_id),
  1214. MAC2STR(wpa_auth->conf.r1_key_holder));
  1215. return -1;
  1216. }
  1217. wpa_printf(MSG_DEBUG, "FT: PMK-R1 push - R1KH-ID=" MACSTR " S1KH-ID="
  1218. MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
  1219. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 push - PMK-R1",
  1220. f.pmk_r1, PMK_LEN);
  1221. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 push - PMKR1Name",
  1222. f.pmk_r1_name, WPA_PMK_NAME_LEN);
  1223. wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name);
  1224. os_memset(f.pmk_r1, 0, PMK_LEN);
  1225. return 0;
  1226. }
  1227. int wpa_ft_rrb_rx(struct wpa_authenticator *wpa_auth, const u8 *src_addr,
  1228. const u8 *data, size_t data_len)
  1229. {
  1230. struct ft_rrb_frame *frame;
  1231. u16 alen;
  1232. const u8 *pos, *end, *start;
  1233. u8 action;
  1234. const u8 *sta_addr, *target_ap_addr;
  1235. wpa_printf(MSG_DEBUG, "FT: RRB received frame from remote AP " MACSTR,
  1236. MAC2STR(src_addr));
  1237. if (data_len < sizeof(*frame)) {
  1238. wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (data_len=%lu)",
  1239. (unsigned long) data_len);
  1240. return -1;
  1241. }
  1242. pos = data;
  1243. frame = (struct ft_rrb_frame *) pos;
  1244. pos += sizeof(*frame);
  1245. alen = le_to_host16(frame->action_length);
  1246. wpa_printf(MSG_DEBUG, "FT: RRB frame - frame_type=%d packet_type=%d "
  1247. "action_length=%d ap_address=" MACSTR,
  1248. frame->frame_type, frame->packet_type, alen,
  1249. MAC2STR(frame->ap_address));
  1250. if (frame->frame_type != RSN_REMOTE_FRAME_TYPE_FT_RRB) {
  1251. /* Discard frame per IEEE Std 802.11r-2008, 11A.10.3 */
  1252. wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with "
  1253. "unrecognized type %d", frame->frame_type);
  1254. return -1;
  1255. }
  1256. if (alen > data_len - sizeof(*frame)) {
  1257. wpa_printf(MSG_DEBUG, "FT: RRB frame too short for action "
  1258. "frame");
  1259. return -1;
  1260. }
  1261. if (frame->packet_type == FT_PACKET_R0KH_R1KH_PULL)
  1262. return wpa_ft_rrb_rx_pull(wpa_auth, src_addr, data, data_len);
  1263. if (frame->packet_type == FT_PACKET_R0KH_R1KH_RESP)
  1264. return wpa_ft_rrb_rx_resp(wpa_auth, src_addr, data, data_len);
  1265. if (frame->packet_type == FT_PACKET_R0KH_R1KH_PUSH)
  1266. return wpa_ft_rrb_rx_push(wpa_auth, src_addr, data, data_len);
  1267. wpa_hexdump(MSG_MSGDUMP, "FT: RRB - FT Action frame", pos, alen);
  1268. if (alen < 1 + 1 + 2 * ETH_ALEN) {
  1269. wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (not enough "
  1270. "room for Action Frame body); alen=%lu",
  1271. (unsigned long) alen);
  1272. return -1;
  1273. }
  1274. start = pos;
  1275. end = pos + alen;
  1276. if (*pos != WLAN_ACTION_FT) {
  1277. wpa_printf(MSG_DEBUG, "FT: Unexpected Action frame category "
  1278. "%d", *pos);
  1279. return -1;
  1280. }
  1281. pos++;
  1282. action = *pos++;
  1283. sta_addr = pos;
  1284. pos += ETH_ALEN;
  1285. target_ap_addr = pos;
  1286. pos += ETH_ALEN;
  1287. wpa_printf(MSG_DEBUG, "FT: RRB Action Frame: action=%d sta_addr="
  1288. MACSTR " target_ap_addr=" MACSTR,
  1289. action, MAC2STR(sta_addr), MAC2STR(target_ap_addr));
  1290. if (frame->packet_type == FT_PACKET_REQUEST) {
  1291. wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Request");
  1292. if (action != 1) {
  1293. wpa_printf(MSG_DEBUG, "FT: Unexpected Action %d in "
  1294. "RRB Request", action);
  1295. return -1;
  1296. }
  1297. if (os_memcmp(target_ap_addr, wpa_auth->addr, ETH_ALEN) != 0) {
  1298. wpa_printf(MSG_DEBUG, "FT: Target AP address in the "
  1299. "RRB Request does not match with own "
  1300. "address");
  1301. return -1;
  1302. }
  1303. if (wpa_ft_rrb_rx_request(wpa_auth, frame->ap_address,
  1304. sta_addr, pos, end - pos) < 0)
  1305. return -1;
  1306. } else if (frame->packet_type == FT_PACKET_RESPONSE) {
  1307. u16 status_code;
  1308. if (end - pos < 2) {
  1309. wpa_printf(MSG_DEBUG, "FT: Not enough room for status "
  1310. "code in RRB Response");
  1311. return -1;
  1312. }
  1313. status_code = WPA_GET_LE16(pos);
  1314. pos += 2;
  1315. wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Response "
  1316. "(status_code=%d)", status_code);
  1317. if (wpa_ft_action_send(wpa_auth, sta_addr, start, alen) < 0)
  1318. return -1;
  1319. } else {
  1320. wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with unknown "
  1321. "packet_type %d", frame->packet_type);
  1322. return -1;
  1323. }
  1324. return 0;
  1325. }
  1326. static void wpa_ft_generate_pmk_r1(struct wpa_authenticator *wpa_auth,
  1327. struct wpa_ft_pmk_r0_sa *pmk_r0,
  1328. struct ft_remote_r1kh *r1kh,
  1329. const u8 *s1kh_id)
  1330. {
  1331. struct ft_r0kh_r1kh_push_frame frame, f;
  1332. struct os_time now;
  1333. os_memset(&frame, 0, sizeof(frame));
  1334. frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  1335. frame.packet_type = FT_PACKET_R0KH_R1KH_PUSH;
  1336. frame.data_length = host_to_le16(FT_R0KH_R1KH_PUSH_DATA_LEN);
  1337. os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN);
  1338. /* aes_wrap() does not support inplace encryption, so use a temporary
  1339. * buffer for the data. */
  1340. os_memcpy(f.r1kh_id, r1kh->id, FT_R1KH_ID_LEN);
  1341. os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN);
  1342. os_memcpy(f.pmk_r0_name, pmk_r0->pmk_r0_name, WPA_PMK_NAME_LEN);
  1343. wpa_derive_pmk_r1(pmk_r0->pmk_r0, pmk_r0->pmk_r0_name, r1kh->id,
  1344. s1kh_id, f.pmk_r1, f.pmk_r1_name);
  1345. wpa_printf(MSG_DEBUG, "FT: R1KH-ID " MACSTR, MAC2STR(r1kh->id));
  1346. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", f.pmk_r1, PMK_LEN);
  1347. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", f.pmk_r1_name,
  1348. WPA_PMK_NAME_LEN);
  1349. os_get_time(&now);
  1350. WPA_PUT_LE32(f.timestamp, now.sec);
  1351. if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
  1352. f.timestamp, frame.timestamp) < 0)
  1353. return;
  1354. wpa_ft_rrb_send(wpa_auth, r1kh->addr, (u8 *) &frame, sizeof(frame));
  1355. }
  1356. void wpa_ft_push_pmk_r1(struct wpa_authenticator *wpa_auth, const u8 *addr)
  1357. {
  1358. struct wpa_ft_pmk_r0_sa *r0;
  1359. struct ft_remote_r1kh *r1kh;
  1360. if (!wpa_auth->conf.pmk_r1_push)
  1361. return;
  1362. r0 = wpa_auth->ft_pmk_cache->pmk_r0;
  1363. while (r0) {
  1364. if (os_memcmp(r0->spa, addr, ETH_ALEN) == 0)
  1365. break;
  1366. r0 = r0->next;
  1367. }
  1368. if (r0 == NULL || r0->pmk_r1_pushed)
  1369. return;
  1370. r0->pmk_r1_pushed = 1;
  1371. wpa_printf(MSG_DEBUG, "FT: Deriving and pushing PMK-R1 keys to R1KHs "
  1372. "for STA " MACSTR, MAC2STR(addr));
  1373. r1kh = wpa_auth->conf.r1kh_list;
  1374. while (r1kh) {
  1375. wpa_ft_generate_pmk_r1(wpa_auth, r0, r1kh, addr);
  1376. r1kh = r1kh->next;
  1377. }
  1378. }
  1379. #endif /* CONFIG_IEEE80211R */