wpa_auth_ft.c 49 KB

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