wpa_auth_ft.c 47 KB

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