wpa_auth_ft.c 48 KB

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