wpa.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448
  1. /*
  2. * hostapd - IEEE 802.11i-2004 / WPA Authenticator
  3. * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #ifndef CONFIG_NATIVE_WINDOWS
  16. #include "common.h"
  17. #include "config.h"
  18. #include "eapol_sm.h"
  19. #include "wpa.h"
  20. #include "sha1.h"
  21. #include "sha256.h"
  22. #include "rc4.h"
  23. #include "aes_wrap.h"
  24. #include "crypto.h"
  25. #include "eloop.h"
  26. #include "ieee802_11.h"
  27. #include "pmksa_cache.h"
  28. #include "state_machine.h"
  29. #include "wpa_auth_i.h"
  30. #include "wpa_auth_ie.h"
  31. #define STATE_MACHINE_DATA struct wpa_state_machine
  32. #define STATE_MACHINE_DEBUG_PREFIX "WPA"
  33. #define STATE_MACHINE_ADDR sm->addr
  34. static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
  35. static void wpa_sm_step(struct wpa_state_machine *sm);
  36. static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len);
  37. static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
  38. static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
  39. struct wpa_group *group);
  40. static void wpa_request_new_ptk(struct wpa_state_machine *sm);
  41. static const u32 dot11RSNAConfigGroupUpdateCount = 4;
  42. static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
  43. static const u32 eapol_key_timeout_first = 100; /* ms */
  44. static const u32 eapol_key_timeout_subseq = 1000; /* ms */
  45. /* TODO: make these configurable */
  46. static const int dot11RSNAConfigPMKLifetime = 43200;
  47. static const int dot11RSNAConfigPMKReauthThreshold = 70;
  48. static const int dot11RSNAConfigSATimeout = 60;
  49. static inline void wpa_auth_mic_failure_report(
  50. struct wpa_authenticator *wpa_auth, const u8 *addr)
  51. {
  52. if (wpa_auth->cb.mic_failure_report)
  53. wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
  54. }
  55. static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
  56. const u8 *addr, wpa_eapol_variable var,
  57. int value)
  58. {
  59. if (wpa_auth->cb.set_eapol)
  60. wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
  61. }
  62. static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
  63. const u8 *addr, wpa_eapol_variable var)
  64. {
  65. if (wpa_auth->cb.get_eapol == NULL)
  66. return -1;
  67. return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
  68. }
  69. static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
  70. const u8 *addr, const u8 *prev_psk)
  71. {
  72. if (wpa_auth->cb.get_psk == NULL)
  73. return NULL;
  74. return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, prev_psk);
  75. }
  76. static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
  77. const u8 *addr, u8 *msk, size_t *len)
  78. {
  79. if (wpa_auth->cb.get_msk == NULL)
  80. return -1;
  81. return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
  82. }
  83. static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
  84. int vlan_id,
  85. const char *alg, const u8 *addr, int idx,
  86. u8 *key, size_t key_len)
  87. {
  88. if (wpa_auth->cb.set_key == NULL)
  89. return -1;
  90. return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
  91. key, key_len);
  92. }
  93. static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
  94. const u8 *addr, int idx, u8 *seq)
  95. {
  96. if (wpa_auth->cb.get_seqnum == NULL)
  97. return -1;
  98. return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
  99. }
  100. static inline int wpa_auth_get_seqnum_igtk(struct wpa_authenticator *wpa_auth,
  101. const u8 *addr, int idx, u8 *seq)
  102. {
  103. if (wpa_auth->cb.get_seqnum_igtk == NULL)
  104. return -1;
  105. return wpa_auth->cb.get_seqnum_igtk(wpa_auth->cb.ctx, addr, idx, seq);
  106. }
  107. static inline int
  108. wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
  109. const u8 *data, size_t data_len, int encrypt)
  110. {
  111. if (wpa_auth->cb.send_eapol == NULL)
  112. return -1;
  113. return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
  114. encrypt);
  115. }
  116. int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
  117. int (*cb)(struct wpa_state_machine *sm, void *ctx),
  118. void *cb_ctx)
  119. {
  120. if (wpa_auth->cb.for_each_sta == NULL)
  121. return 0;
  122. return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
  123. }
  124. int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
  125. int (*cb)(struct wpa_authenticator *a, void *ctx),
  126. void *cb_ctx)
  127. {
  128. if (wpa_auth->cb.for_each_auth == NULL)
  129. return 0;
  130. return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
  131. }
  132. void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
  133. logger_level level, const char *txt)
  134. {
  135. if (wpa_auth->cb.logger == NULL)
  136. return;
  137. wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
  138. }
  139. void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
  140. logger_level level, const char *fmt, ...)
  141. {
  142. char *format;
  143. int maxlen;
  144. va_list ap;
  145. if (wpa_auth->cb.logger == NULL)
  146. return;
  147. maxlen = os_strlen(fmt) + 100;
  148. format = os_malloc(maxlen);
  149. if (!format)
  150. return;
  151. va_start(ap, fmt);
  152. vsnprintf(format, maxlen, fmt, ap);
  153. va_end(ap);
  154. wpa_auth_logger(wpa_auth, addr, level, format);
  155. os_free(format);
  156. }
  157. static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
  158. const u8 *addr)
  159. {
  160. if (wpa_auth->cb.disconnect == NULL)
  161. return;
  162. wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
  163. WLAN_REASON_PREV_AUTH_NOT_VALID);
  164. }
  165. static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
  166. {
  167. int ret = 0;
  168. #ifdef CONFIG_IEEE80211R
  169. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
  170. ret = 1;
  171. #endif /* CONFIG_IEEE80211R */
  172. #ifdef CONFIG_IEEE80211W
  173. if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
  174. ret = 1;
  175. #endif /* CONFIG_IEEE80211W */
  176. return ret;
  177. }
  178. static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
  179. {
  180. struct wpa_authenticator *wpa_auth = eloop_ctx;
  181. if (os_get_random(wpa_auth->group->GMK, WPA_GMK_LEN)) {
  182. wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
  183. "initialization.");
  184. } else {
  185. wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
  186. }
  187. if (wpa_auth->conf.wpa_gmk_rekey) {
  188. eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
  189. wpa_rekey_gmk, wpa_auth, NULL);
  190. }
  191. }
  192. static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
  193. {
  194. struct wpa_authenticator *wpa_auth = eloop_ctx;
  195. struct wpa_group *group;
  196. wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
  197. for (group = wpa_auth->group; group; group = group->next) {
  198. group->GTKReKey = TRUE;
  199. do {
  200. group->changed = FALSE;
  201. wpa_group_sm_step(wpa_auth, group);
  202. } while (group->changed);
  203. }
  204. if (wpa_auth->conf.wpa_group_rekey) {
  205. eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
  206. 0, wpa_rekey_gtk, wpa_auth, NULL);
  207. }
  208. }
  209. static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
  210. {
  211. struct wpa_authenticator *wpa_auth = eloop_ctx;
  212. struct wpa_state_machine *sm = timeout_ctx;
  213. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
  214. wpa_request_new_ptk(sm);
  215. wpa_sm_step(sm);
  216. }
  217. static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
  218. {
  219. if (sm->pmksa == ctx)
  220. sm->pmksa = NULL;
  221. return 0;
  222. }
  223. static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
  224. void *ctx)
  225. {
  226. struct wpa_authenticator *wpa_auth = ctx;
  227. wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
  228. }
  229. static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
  230. int vlan_id)
  231. {
  232. struct wpa_group *group;
  233. u8 buf[ETH_ALEN + 8 + sizeof(group)];
  234. u8 rkey[32];
  235. group = os_zalloc(sizeof(struct wpa_group));
  236. if (group == NULL)
  237. return NULL;
  238. group->GTKAuthenticator = TRUE;
  239. group->vlan_id = vlan_id;
  240. switch (wpa_auth->conf.wpa_group) {
  241. case WPA_CIPHER_CCMP:
  242. group->GTK_len = 16;
  243. break;
  244. case WPA_CIPHER_TKIP:
  245. group->GTK_len = 32;
  246. break;
  247. case WPA_CIPHER_WEP104:
  248. group->GTK_len = 13;
  249. break;
  250. case WPA_CIPHER_WEP40:
  251. group->GTK_len = 5;
  252. break;
  253. }
  254. /* Counter = PRF-256(Random number, "Init Counter",
  255. * Local MAC Address || Time)
  256. */
  257. os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
  258. wpa_get_ntp_timestamp(buf + ETH_ALEN);
  259. os_memcpy(buf + ETH_ALEN + 8, &group, sizeof(group));
  260. if (os_get_random(rkey, sizeof(rkey)) ||
  261. os_get_random(group->GMK, WPA_GMK_LEN)) {
  262. wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
  263. "initialization.");
  264. os_free(group);
  265. return NULL;
  266. }
  267. sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
  268. group->Counter, WPA_NONCE_LEN);
  269. group->GInit = TRUE;
  270. wpa_group_sm_step(wpa_auth, group);
  271. group->GInit = FALSE;
  272. wpa_group_sm_step(wpa_auth, group);
  273. return group;
  274. }
  275. /**
  276. * wpa_init - Initialize WPA authenticator
  277. * @addr: Authenticator address
  278. * @conf: Configuration for WPA authenticator
  279. * @cb: Callback functions for WPA authenticator
  280. * Returns: Pointer to WPA authenticator data or %NULL on failure
  281. */
  282. struct wpa_authenticator * wpa_init(const u8 *addr,
  283. struct wpa_auth_config *conf,
  284. struct wpa_auth_callbacks *cb)
  285. {
  286. struct wpa_authenticator *wpa_auth;
  287. wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
  288. if (wpa_auth == NULL)
  289. return NULL;
  290. os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
  291. os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
  292. os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
  293. if (wpa_auth_gen_wpa_ie(wpa_auth)) {
  294. wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
  295. os_free(wpa_auth);
  296. return NULL;
  297. }
  298. wpa_auth->group = wpa_group_init(wpa_auth, 0);
  299. if (wpa_auth->group == NULL) {
  300. os_free(wpa_auth->wpa_ie);
  301. os_free(wpa_auth);
  302. return NULL;
  303. }
  304. wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
  305. wpa_auth);
  306. if (wpa_auth->pmksa == NULL) {
  307. wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
  308. os_free(wpa_auth->wpa_ie);
  309. os_free(wpa_auth);
  310. return NULL;
  311. }
  312. #ifdef CONFIG_IEEE80211R
  313. wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
  314. if (wpa_auth->ft_pmk_cache == NULL) {
  315. wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
  316. os_free(wpa_auth->wpa_ie);
  317. pmksa_cache_auth_deinit(wpa_auth->pmksa);
  318. os_free(wpa_auth);
  319. return NULL;
  320. }
  321. #endif /* CONFIG_IEEE80211R */
  322. if (wpa_auth->conf.wpa_gmk_rekey) {
  323. eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
  324. wpa_rekey_gmk, wpa_auth, NULL);
  325. }
  326. if (wpa_auth->conf.wpa_group_rekey) {
  327. eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
  328. wpa_rekey_gtk, wpa_auth, NULL);
  329. }
  330. return wpa_auth;
  331. }
  332. /**
  333. * wpa_deinit - Deinitialize WPA authenticator
  334. * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
  335. */
  336. void wpa_deinit(struct wpa_authenticator *wpa_auth)
  337. {
  338. struct wpa_group *group, *prev;
  339. eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
  340. eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
  341. #ifdef CONFIG_PEERKEY
  342. while (wpa_auth->stsl_negotiations)
  343. wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
  344. #endif /* CONFIG_PEERKEY */
  345. pmksa_cache_auth_deinit(wpa_auth->pmksa);
  346. #ifdef CONFIG_IEEE80211R
  347. wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
  348. wpa_auth->ft_pmk_cache = NULL;
  349. #endif /* CONFIG_IEEE80211R */
  350. os_free(wpa_auth->wpa_ie);
  351. group = wpa_auth->group;
  352. while (group) {
  353. prev = group;
  354. group = group->next;
  355. os_free(prev);
  356. }
  357. os_free(wpa_auth);
  358. }
  359. /**
  360. * wpa_reconfig - Update WPA authenticator configuration
  361. * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
  362. * @conf: Configuration for WPA authenticator
  363. */
  364. int wpa_reconfig(struct wpa_authenticator *wpa_auth,
  365. struct wpa_auth_config *conf)
  366. {
  367. if (wpa_auth == NULL)
  368. return 0;
  369. os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
  370. if (wpa_auth_gen_wpa_ie(wpa_auth)) {
  371. wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
  372. return -1;
  373. }
  374. return 0;
  375. }
  376. struct wpa_state_machine *
  377. wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr)
  378. {
  379. struct wpa_state_machine *sm;
  380. sm = os_zalloc(sizeof(struct wpa_state_machine));
  381. if (sm == NULL)
  382. return NULL;
  383. os_memcpy(sm->addr, addr, ETH_ALEN);
  384. sm->wpa_auth = wpa_auth;
  385. sm->group = wpa_auth->group;
  386. return sm;
  387. }
  388. void wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
  389. struct wpa_state_machine *sm)
  390. {
  391. if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
  392. return;
  393. #ifdef CONFIG_IEEE80211R
  394. if (sm->ft_completed) {
  395. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  396. "FT authentication already completed - do not "
  397. "start 4-way handshake");
  398. return;
  399. }
  400. #endif /* CONFIG_IEEE80211R */
  401. if (sm->started) {
  402. os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
  403. sm->ReAuthenticationRequest = TRUE;
  404. wpa_sm_step(sm);
  405. return;
  406. }
  407. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  408. "start authentication");
  409. sm->started = 1;
  410. sm->Init = TRUE;
  411. wpa_sm_step(sm);
  412. sm->Init = FALSE;
  413. sm->AuthenticationRequest = TRUE;
  414. wpa_sm_step(sm);
  415. }
  416. void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
  417. {
  418. /* WPA/RSN was not used - clear WPA state. This is needed if the STA
  419. * reassociates back to the same AP while the previous entry for the
  420. * STA has not yet been removed. */
  421. if (sm == NULL)
  422. return;
  423. sm->wpa_key_mgmt = 0;
  424. }
  425. static void wpa_free_sta_sm(struct wpa_state_machine *sm)
  426. {
  427. os_free(sm->last_rx_eapol_key);
  428. os_free(sm->wpa_ie);
  429. os_free(sm);
  430. }
  431. void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
  432. {
  433. if (sm == NULL)
  434. return;
  435. if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
  436. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  437. "strict rekeying - force GTK rekey since STA "
  438. "is leaving");
  439. eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
  440. eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
  441. NULL);
  442. }
  443. eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
  444. eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
  445. eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
  446. if (sm->in_step_loop) {
  447. /* Must not free state machine while wpa_sm_step() is running.
  448. * Freeing will be completed in the end of wpa_sm_step(). */
  449. wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
  450. "machine deinit for " MACSTR, MAC2STR(sm->addr));
  451. sm->pending_deinit = 1;
  452. } else
  453. wpa_free_sta_sm(sm);
  454. }
  455. static void wpa_request_new_ptk(struct wpa_state_machine *sm)
  456. {
  457. if (sm == NULL)
  458. return;
  459. sm->PTKRequest = TRUE;
  460. sm->PTK_valid = 0;
  461. }
  462. static int wpa_replay_counter_valid(struct wpa_state_machine *sm,
  463. const u8 *replay_counter)
  464. {
  465. int i;
  466. for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
  467. if (!sm->key_replay[i].valid)
  468. break;
  469. if (os_memcmp(replay_counter, sm->key_replay[i].counter,
  470. WPA_REPLAY_COUNTER_LEN) == 0)
  471. return 1;
  472. }
  473. return 0;
  474. }
  475. void wpa_receive(struct wpa_authenticator *wpa_auth,
  476. struct wpa_state_machine *sm,
  477. u8 *data, size_t data_len)
  478. {
  479. struct ieee802_1x_hdr *hdr;
  480. struct wpa_eapol_key *key;
  481. u16 key_info, key_data_length;
  482. enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
  483. SMK_M1, SMK_M3, SMK_ERROR } msg;
  484. char *msgtxt;
  485. struct wpa_eapol_ie_parse kde;
  486. if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
  487. return;
  488. if (data_len < sizeof(*hdr) + sizeof(*key))
  489. return;
  490. hdr = (struct ieee802_1x_hdr *) data;
  491. key = (struct wpa_eapol_key *) (hdr + 1);
  492. key_info = WPA_GET_BE16(key->key_info);
  493. key_data_length = WPA_GET_BE16(key->key_data_length);
  494. if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
  495. wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
  496. "key_data overflow (%d > %lu)",
  497. key_data_length,
  498. (unsigned long) (data_len - sizeof(*hdr) -
  499. sizeof(*key)));
  500. return;
  501. }
  502. /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
  503. * are set */
  504. if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
  505. (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
  506. if (key_info & WPA_KEY_INFO_ERROR) {
  507. msg = SMK_ERROR;
  508. msgtxt = "SMK Error";
  509. } else {
  510. msg = SMK_M1;
  511. msgtxt = "SMK M1";
  512. }
  513. } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
  514. msg = SMK_M3;
  515. msgtxt = "SMK M3";
  516. } else if (key_info & WPA_KEY_INFO_REQUEST) {
  517. msg = REQUEST;
  518. msgtxt = "Request";
  519. } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
  520. msg = GROUP_2;
  521. msgtxt = "2/2 Group";
  522. } else if (key_data_length == 0) {
  523. msg = PAIRWISE_4;
  524. msgtxt = "4/4 Pairwise";
  525. } else {
  526. msg = PAIRWISE_2;
  527. msgtxt = "2/4 Pairwise";
  528. }
  529. /* TODO: key_info type validation for PeerKey */
  530. if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
  531. msg == GROUP_2) {
  532. u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  533. if (sm->pairwise == WPA_CIPHER_CCMP) {
  534. if (wpa_use_aes_cmac(sm) &&
  535. ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  536. wpa_auth_logger(wpa_auth, sm->addr,
  537. LOGGER_WARNING,
  538. "advertised support for "
  539. "AES-128-CMAC, but did not "
  540. "use it");
  541. return;
  542. }
  543. if (!wpa_use_aes_cmac(sm) &&
  544. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
  545. wpa_auth_logger(wpa_auth, sm->addr,
  546. LOGGER_WARNING,
  547. "did not use HMAC-SHA1-AES "
  548. "with CCMP");
  549. return;
  550. }
  551. }
  552. }
  553. if (key_info & WPA_KEY_INFO_REQUEST) {
  554. if (sm->req_replay_counter_used &&
  555. os_memcmp(key->replay_counter, sm->req_replay_counter,
  556. WPA_REPLAY_COUNTER_LEN) <= 0) {
  557. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
  558. "received EAPOL-Key request with "
  559. "replayed counter");
  560. return;
  561. }
  562. }
  563. if (!(key_info & WPA_KEY_INFO_REQUEST) &&
  564. !wpa_replay_counter_valid(sm, key->replay_counter)) {
  565. int i;
  566. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  567. "received EAPOL-Key %s with unexpected "
  568. "replay counter", msgtxt);
  569. for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
  570. if (!sm->key_replay[i].valid)
  571. break;
  572. wpa_hexdump(MSG_DEBUG, "pending replay counter",
  573. sm->key_replay[i].counter,
  574. WPA_REPLAY_COUNTER_LEN);
  575. }
  576. wpa_hexdump(MSG_DEBUG, "received replay counter",
  577. key->replay_counter, WPA_REPLAY_COUNTER_LEN);
  578. return;
  579. }
  580. switch (msg) {
  581. case PAIRWISE_2:
  582. if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
  583. sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING) {
  584. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  585. "received EAPOL-Key msg 2/4 in "
  586. "invalid state (%d) - dropped",
  587. sm->wpa_ptk_state);
  588. return;
  589. }
  590. if (sm->wpa_ie == NULL ||
  591. sm->wpa_ie_len != key_data_length ||
  592. os_memcmp(sm->wpa_ie, key + 1, key_data_length) != 0) {
  593. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  594. "WPA IE from (Re)AssocReq did not "
  595. "match with msg 2/4");
  596. if (sm->wpa_ie) {
  597. wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
  598. sm->wpa_ie, sm->wpa_ie_len);
  599. }
  600. wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
  601. (u8 *) (key + 1), key_data_length);
  602. /* MLME-DEAUTHENTICATE.request */
  603. wpa_sta_disconnect(wpa_auth, sm->addr);
  604. return;
  605. }
  606. break;
  607. case PAIRWISE_4:
  608. if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
  609. !sm->PTK_valid) {
  610. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  611. "received EAPOL-Key msg 4/4 in "
  612. "invalid state (%d) - dropped",
  613. sm->wpa_ptk_state);
  614. return;
  615. }
  616. break;
  617. case GROUP_2:
  618. if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
  619. || !sm->PTK_valid) {
  620. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  621. "received EAPOL-Key msg 2/2 in "
  622. "invalid state (%d) - dropped",
  623. sm->wpa_ptk_group_state);
  624. return;
  625. }
  626. break;
  627. #ifdef CONFIG_PEERKEY
  628. case SMK_M1:
  629. case SMK_M3:
  630. case SMK_ERROR:
  631. if (!wpa_auth->conf.peerkey) {
  632. wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
  633. "PeerKey use disabled - ignoring message");
  634. return;
  635. }
  636. if (!sm->PTK_valid) {
  637. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  638. "received EAPOL-Key msg SMK in "
  639. "invalid state - dropped");
  640. return;
  641. }
  642. break;
  643. #else /* CONFIG_PEERKEY */
  644. case SMK_M1:
  645. case SMK_M3:
  646. case SMK_ERROR:
  647. return; /* STSL disabled - ignore SMK messages */
  648. #endif /* CONFIG_PEERKEY */
  649. case REQUEST:
  650. break;
  651. }
  652. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  653. "received EAPOL-Key frame (%s)", msgtxt);
  654. if (key_info & WPA_KEY_INFO_ACK) {
  655. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  656. "received invalid EAPOL-Key: Key Ack set");
  657. return;
  658. }
  659. if (!(key_info & WPA_KEY_INFO_MIC)) {
  660. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  661. "received invalid EAPOL-Key: Key MIC not set");
  662. return;
  663. }
  664. sm->MICVerified = FALSE;
  665. if (sm->PTK_valid) {
  666. if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
  667. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  668. "received EAPOL-Key with invalid MIC");
  669. return;
  670. }
  671. sm->MICVerified = TRUE;
  672. eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
  673. }
  674. if (key_info & WPA_KEY_INFO_REQUEST) {
  675. if (sm->MICVerified) {
  676. sm->req_replay_counter_used = 1;
  677. os_memcpy(sm->req_replay_counter, key->replay_counter,
  678. WPA_REPLAY_COUNTER_LEN);
  679. } else {
  680. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  681. "received EAPOL-Key request with "
  682. "invalid MIC");
  683. return;
  684. }
  685. /*
  686. * TODO: should decrypt key data field if encryption was used;
  687. * even though MAC address KDE is not normally encrypted,
  688. * supplicant is allowed to encrypt it.
  689. */
  690. if (msg == SMK_ERROR) {
  691. #ifdef CONFIG_PEERKEY
  692. wpa_smk_error(wpa_auth, sm, key);
  693. #endif /* CONFIG_PEERKEY */
  694. return;
  695. } else if (key_info & WPA_KEY_INFO_ERROR) {
  696. /* Supplicant reported a Michael MIC error */
  697. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  698. "received EAPOL-Key Error Request "
  699. "(STA detected Michael MIC failure)");
  700. wpa_auth_mic_failure_report(wpa_auth, sm->addr);
  701. sm->dot11RSNAStatsTKIPRemoteMICFailures++;
  702. wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
  703. /* Error report is not a request for a new key
  704. * handshake, but since Authenticator may do it, let's
  705. * change the keys now anyway. */
  706. wpa_request_new_ptk(sm);
  707. } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
  708. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  709. "received EAPOL-Key Request for new "
  710. "4-Way Handshake");
  711. wpa_request_new_ptk(sm);
  712. #ifdef CONFIG_PEERKEY
  713. } else if (msg == SMK_M1) {
  714. wpa_smk_m1(wpa_auth, sm, key);
  715. #endif /* CONFIG_PEERKEY */
  716. } else if (key_data_length > 0 &&
  717. wpa_parse_kde_ies((const u8 *) (key + 1),
  718. key_data_length, &kde) == 0 &&
  719. kde.mac_addr) {
  720. } else {
  721. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  722. "received EAPOL-Key Request for GTK "
  723. "rekeying");
  724. /* FIX: why was this triggering PTK rekeying for the
  725. * STA that requested Group Key rekeying?? */
  726. /* wpa_request_new_ptk(sta->wpa_sm); */
  727. eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
  728. wpa_rekey_gtk(wpa_auth, NULL);
  729. }
  730. } else {
  731. /* Do not allow the same key replay counter to be reused. This
  732. * does also invalidate all other pending replay counters if
  733. * retransmissions were used, i.e., we will only process one of
  734. * the pending replies and ignore rest if more than one is
  735. * received. */
  736. sm->key_replay[0].valid = FALSE;
  737. }
  738. #ifdef CONFIG_PEERKEY
  739. if (msg == SMK_M3) {
  740. wpa_smk_m3(wpa_auth, sm, key);
  741. return;
  742. }
  743. #endif /* CONFIG_PEERKEY */
  744. os_free(sm->last_rx_eapol_key);
  745. sm->last_rx_eapol_key = os_malloc(data_len);
  746. if (sm->last_rx_eapol_key == NULL)
  747. return;
  748. os_memcpy(sm->last_rx_eapol_key, data, data_len);
  749. sm->last_rx_eapol_key_len = data_len;
  750. sm->EAPOLKeyReceived = TRUE;
  751. sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
  752. sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
  753. os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
  754. wpa_sm_step(sm);
  755. }
  756. static void wpa_gmk_to_gtk(const u8 *gmk, const u8 *addr, const u8 *gnonce,
  757. u8 *gtk, size_t gtk_len)
  758. {
  759. u8 data[ETH_ALEN + WPA_NONCE_LEN];
  760. /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */
  761. os_memcpy(data, addr, ETH_ALEN);
  762. os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
  763. #ifdef CONFIG_IEEE80211W
  764. sha256_prf(gmk, WPA_GMK_LEN, "Group key expansion",
  765. data, sizeof(data), gtk, gtk_len);
  766. #else /* CONFIG_IEEE80211W */
  767. sha1_prf(gmk, WPA_GMK_LEN, "Group key expansion",
  768. data, sizeof(data), gtk, gtk_len);
  769. #endif /* CONFIG_IEEE80211W */
  770. wpa_hexdump_key(MSG_DEBUG, "GMK", gmk, WPA_GMK_LEN);
  771. wpa_hexdump_key(MSG_DEBUG, "GTK", gtk, gtk_len);
  772. }
  773. static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
  774. {
  775. struct wpa_authenticator *wpa_auth = eloop_ctx;
  776. struct wpa_state_machine *sm = timeout_ctx;
  777. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
  778. sm->TimeoutEvt = TRUE;
  779. wpa_sm_step(sm);
  780. }
  781. void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
  782. struct wpa_state_machine *sm, int key_info,
  783. const u8 *key_rsc, const u8 *nonce,
  784. const u8 *kde, size_t kde_len,
  785. int keyidx, int encr, int force_version)
  786. {
  787. struct ieee802_1x_hdr *hdr;
  788. struct wpa_eapol_key *key;
  789. size_t len;
  790. int alg;
  791. int key_data_len, pad_len = 0;
  792. u8 *buf, *pos;
  793. int version, pairwise;
  794. int i;
  795. len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
  796. if (force_version)
  797. version = force_version;
  798. else if (wpa_use_aes_cmac(sm))
  799. version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
  800. else if (sm->pairwise == WPA_CIPHER_CCMP)
  801. version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
  802. else
  803. version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
  804. pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
  805. wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
  806. "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
  807. "encr=%d)",
  808. version,
  809. (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
  810. (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
  811. (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
  812. (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
  813. pairwise, (unsigned long) kde_len, keyidx, encr);
  814. key_data_len = kde_len;
  815. if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
  816. version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
  817. pad_len = key_data_len % 8;
  818. if (pad_len)
  819. pad_len = 8 - pad_len;
  820. key_data_len += pad_len + 8;
  821. }
  822. len += key_data_len;
  823. hdr = os_zalloc(len);
  824. if (hdr == NULL)
  825. return;
  826. hdr->version = wpa_auth->conf.eapol_version;
  827. hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
  828. hdr->length = host_to_be16(len - sizeof(*hdr));
  829. key = (struct wpa_eapol_key *) (hdr + 1);
  830. key->type = sm->wpa == WPA_VERSION_WPA2 ?
  831. EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
  832. key_info |= version;
  833. if (encr && sm->wpa == WPA_VERSION_WPA2)
  834. key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
  835. if (sm->wpa != WPA_VERSION_WPA2)
  836. key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
  837. WPA_PUT_BE16(key->key_info, key_info);
  838. alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
  839. switch (alg) {
  840. case WPA_CIPHER_CCMP:
  841. WPA_PUT_BE16(key->key_length, 16);
  842. break;
  843. case WPA_CIPHER_TKIP:
  844. WPA_PUT_BE16(key->key_length, 32);
  845. break;
  846. case WPA_CIPHER_WEP40:
  847. WPA_PUT_BE16(key->key_length, 5);
  848. break;
  849. case WPA_CIPHER_WEP104:
  850. WPA_PUT_BE16(key->key_length, 13);
  851. break;
  852. }
  853. if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
  854. WPA_PUT_BE16(key->key_length, 0);
  855. /* FIX: STSL: what to use as key_replay_counter? */
  856. for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
  857. sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
  858. os_memcpy(sm->key_replay[i].counter,
  859. sm->key_replay[i - 1].counter,
  860. WPA_REPLAY_COUNTER_LEN);
  861. }
  862. inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
  863. os_memcpy(key->replay_counter, sm->key_replay[0].counter,
  864. WPA_REPLAY_COUNTER_LEN);
  865. sm->key_replay[0].valid = TRUE;
  866. if (nonce)
  867. os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
  868. if (key_rsc)
  869. os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
  870. if (kde && !encr) {
  871. os_memcpy(key + 1, kde, kde_len);
  872. WPA_PUT_BE16(key->key_data_length, kde_len);
  873. } else if (encr && kde) {
  874. buf = os_zalloc(key_data_len);
  875. if (buf == NULL) {
  876. os_free(hdr);
  877. return;
  878. }
  879. pos = buf;
  880. os_memcpy(pos, kde, kde_len);
  881. pos += kde_len;
  882. if (pad_len)
  883. *pos++ = 0xdd;
  884. wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
  885. buf, key_data_len);
  886. if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
  887. version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  888. if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf,
  889. (u8 *) (key + 1))) {
  890. os_free(hdr);
  891. os_free(buf);
  892. return;
  893. }
  894. WPA_PUT_BE16(key->key_data_length, key_data_len);
  895. } else {
  896. u8 ek[32];
  897. os_memcpy(key->key_iv,
  898. sm->group->Counter + WPA_NONCE_LEN - 16, 16);
  899. inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
  900. os_memcpy(ek, key->key_iv, 16);
  901. os_memcpy(ek + 16, sm->PTK.kek, 16);
  902. os_memcpy(key + 1, buf, key_data_len);
  903. rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
  904. WPA_PUT_BE16(key->key_data_length, key_data_len);
  905. }
  906. os_free(buf);
  907. }
  908. if (key_info & WPA_KEY_INFO_MIC) {
  909. if (!sm->PTK_valid) {
  910. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  911. "PTK not valid when sending EAPOL-Key "
  912. "frame");
  913. os_free(hdr);
  914. return;
  915. }
  916. wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len,
  917. key->key_mic);
  918. }
  919. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
  920. 1);
  921. wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
  922. sm->pairwise_set);
  923. os_free(hdr);
  924. }
  925. static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
  926. struct wpa_state_machine *sm, int key_info,
  927. const u8 *key_rsc, const u8 *nonce,
  928. const u8 *kde, size_t kde_len,
  929. int keyidx, int encr)
  930. {
  931. int timeout_ms;
  932. int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
  933. int ctr;
  934. if (sm == NULL)
  935. return;
  936. __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
  937. keyidx, encr, 0);
  938. ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
  939. if (ctr == 1)
  940. timeout_ms = eapol_key_timeout_first;
  941. else
  942. timeout_ms = eapol_key_timeout_subseq;
  943. eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
  944. wpa_send_eapol_timeout, wpa_auth, sm);
  945. }
  946. static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
  947. {
  948. struct ieee802_1x_hdr *hdr;
  949. struct wpa_eapol_key *key;
  950. u16 key_info;
  951. int ret = 0;
  952. u8 mic[16];
  953. if (data_len < sizeof(*hdr) + sizeof(*key))
  954. return -1;
  955. hdr = (struct ieee802_1x_hdr *) data;
  956. key = (struct wpa_eapol_key *) (hdr + 1);
  957. key_info = WPA_GET_BE16(key->key_info);
  958. os_memcpy(mic, key->key_mic, 16);
  959. os_memset(key->key_mic, 0, 16);
  960. if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  961. data, data_len, key->key_mic) ||
  962. os_memcmp(mic, key->key_mic, 16) != 0)
  963. ret = -1;
  964. os_memcpy(key->key_mic, mic, 16);
  965. return ret;
  966. }
  967. void wpa_remove_ptk(struct wpa_state_machine *sm)
  968. {
  969. sm->PTK_valid = FALSE;
  970. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  971. wpa_auth_set_key(sm->wpa_auth, 0, "none", sm->addr, 0, (u8 *) "", 0);
  972. sm->pairwise_set = FALSE;
  973. eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
  974. }
  975. void wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
  976. {
  977. int remove_ptk = 1;
  978. if (sm == NULL)
  979. return;
  980. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  981. "event %d notification", event);
  982. switch (event) {
  983. case WPA_AUTH:
  984. case WPA_ASSOC:
  985. break;
  986. case WPA_DEAUTH:
  987. case WPA_DISASSOC:
  988. sm->DeauthenticationRequest = TRUE;
  989. break;
  990. case WPA_REAUTH:
  991. case WPA_REAUTH_EAPOL:
  992. if (sm->GUpdateStationKeys) {
  993. /*
  994. * Reauthentication cancels the pending group key
  995. * update for this STA.
  996. */
  997. sm->group->GKeyDoneStations--;
  998. sm->GUpdateStationKeys = FALSE;
  999. sm->PtkGroupInit = TRUE;
  1000. }
  1001. sm->ReAuthenticationRequest = TRUE;
  1002. break;
  1003. case WPA_ASSOC_FT:
  1004. #ifdef CONFIG_IEEE80211R
  1005. /* Using FT protocol, not WPA auth state machine */
  1006. sm->ft_completed = 1;
  1007. return;
  1008. #else /* CONFIG_IEEE80211R */
  1009. break;
  1010. #endif /* CONFIG_IEEE80211R */
  1011. }
  1012. #ifdef CONFIG_IEEE80211R
  1013. sm->ft_completed = 0;
  1014. #endif /* CONFIG_IEEE80211R */
  1015. #ifdef CONFIG_IEEE80211W
  1016. if (sm->mgmt_frame_prot && event == WPA_AUTH)
  1017. remove_ptk = 0;
  1018. #endif /* CONFIG_IEEE80211W */
  1019. if (remove_ptk) {
  1020. sm->PTK_valid = FALSE;
  1021. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  1022. if (event != WPA_REAUTH_EAPOL)
  1023. wpa_remove_ptk(sm);
  1024. }
  1025. wpa_sm_step(sm);
  1026. }
  1027. static const char * wpa_alg_txt(int alg)
  1028. {
  1029. switch (alg) {
  1030. case WPA_CIPHER_CCMP:
  1031. return "CCMP";
  1032. case WPA_CIPHER_TKIP:
  1033. return "TKIP";
  1034. case WPA_CIPHER_WEP104:
  1035. case WPA_CIPHER_WEP40:
  1036. return "WEP";
  1037. default:
  1038. return "";
  1039. }
  1040. }
  1041. SM_STATE(WPA_PTK, INITIALIZE)
  1042. {
  1043. SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
  1044. if (sm->Init) {
  1045. /* Init flag is not cleared here, so avoid busy
  1046. * loop by claiming nothing changed. */
  1047. sm->changed = FALSE;
  1048. }
  1049. sm->keycount = 0;
  1050. if (sm->GUpdateStationKeys)
  1051. sm->group->GKeyDoneStations--;
  1052. sm->GUpdateStationKeys = FALSE;
  1053. if (sm->wpa == WPA_VERSION_WPA)
  1054. sm->PInitAKeys = FALSE;
  1055. if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
  1056. * Local AA > Remote AA)) */) {
  1057. sm->Pair = TRUE;
  1058. }
  1059. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
  1060. wpa_remove_ptk(sm);
  1061. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
  1062. sm->TimeoutCtr = 0;
  1063. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1064. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1065. WPA_EAPOL_authorized, 0);
  1066. }
  1067. }
  1068. SM_STATE(WPA_PTK, DISCONNECT)
  1069. {
  1070. SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
  1071. sm->Disconnect = FALSE;
  1072. wpa_sta_disconnect(sm->wpa_auth, sm->addr);
  1073. }
  1074. SM_STATE(WPA_PTK, DISCONNECTED)
  1075. {
  1076. SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
  1077. sm->DeauthenticationRequest = FALSE;
  1078. }
  1079. SM_STATE(WPA_PTK, AUTHENTICATION)
  1080. {
  1081. SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
  1082. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  1083. sm->PTK_valid = FALSE;
  1084. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
  1085. 1);
  1086. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
  1087. sm->AuthenticationRequest = FALSE;
  1088. }
  1089. SM_STATE(WPA_PTK, AUTHENTICATION2)
  1090. {
  1091. SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
  1092. os_memcpy(sm->ANonce, sm->group->Counter, WPA_NONCE_LEN);
  1093. inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
  1094. sm->ReAuthenticationRequest = FALSE;
  1095. /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
  1096. * logical place than INITIALIZE since AUTHENTICATION2 can be
  1097. * re-entered on ReAuthenticationRequest without going through
  1098. * INITIALIZE. */
  1099. sm->TimeoutCtr = 0;
  1100. }
  1101. SM_STATE(WPA_PTK, INITPMK)
  1102. {
  1103. u8 msk[2 * PMK_LEN];
  1104. size_t len = 2 * PMK_LEN;
  1105. SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
  1106. #ifdef CONFIG_IEEE80211R
  1107. sm->xxkey_len = 0;
  1108. #endif /* CONFIG_IEEE80211R */
  1109. if (sm->pmksa) {
  1110. wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
  1111. os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
  1112. } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
  1113. wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
  1114. "(len=%lu)", (unsigned long) len);
  1115. os_memcpy(sm->PMK, msk, PMK_LEN);
  1116. #ifdef CONFIG_IEEE80211R
  1117. if (len >= 2 * PMK_LEN) {
  1118. os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
  1119. sm->xxkey_len = PMK_LEN;
  1120. }
  1121. #endif /* CONFIG_IEEE80211R */
  1122. } else {
  1123. wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
  1124. }
  1125. sm->req_replay_counter_used = 0;
  1126. /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
  1127. * will break reauthentication since EAPOL state machines may not be
  1128. * get into AUTHENTICATING state that clears keyRun before WPA state
  1129. * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
  1130. * state and takes PMK from the previously used AAA Key. This will
  1131. * eventually fail in 4-Way Handshake because Supplicant uses PMK
  1132. * derived from the new AAA Key. Setting keyRun = FALSE here seems to
  1133. * be good workaround for this issue. */
  1134. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
  1135. }
  1136. SM_STATE(WPA_PTK, INITPSK)
  1137. {
  1138. const u8 *psk;
  1139. SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
  1140. psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL);
  1141. if (psk) {
  1142. os_memcpy(sm->PMK, psk, PMK_LEN);
  1143. #ifdef CONFIG_IEEE80211R
  1144. os_memcpy(sm->xxkey, psk, PMK_LEN);
  1145. sm->xxkey_len = PMK_LEN;
  1146. #endif /* CONFIG_IEEE80211R */
  1147. }
  1148. sm->req_replay_counter_used = 0;
  1149. }
  1150. SM_STATE(WPA_PTK, PTKSTART)
  1151. {
  1152. u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
  1153. size_t pmkid_len = 0;
  1154. SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
  1155. sm->PTKRequest = FALSE;
  1156. sm->TimeoutEvt = FALSE;
  1157. sm->TimeoutCtr++;
  1158. if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
  1159. /* No point in sending the EAPOL-Key - we will disconnect
  1160. * immediately following this. */
  1161. return;
  1162. }
  1163. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1164. "sending 1/4 msg of 4-Way Handshake");
  1165. /*
  1166. * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
  1167. * one possible PSK for this STA.
  1168. */
  1169. if (sm->wpa == WPA_VERSION_WPA2 &&
  1170. wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) {
  1171. pmkid = buf;
  1172. pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
  1173. pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
  1174. pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
  1175. RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
  1176. if (sm->pmksa)
  1177. os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
  1178. sm->pmksa->pmkid, PMKID_LEN);
  1179. else {
  1180. /*
  1181. * Calculate PMKID since no PMKSA cache entry was
  1182. * available with pre-calculated PMKID.
  1183. */
  1184. rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
  1185. sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
  1186. wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
  1187. }
  1188. }
  1189. wpa_send_eapol(sm->wpa_auth, sm,
  1190. WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
  1191. sm->ANonce, pmkid, pmkid_len, 0, 0);
  1192. }
  1193. static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
  1194. struct wpa_ptk *ptk)
  1195. {
  1196. #ifdef CONFIG_IEEE80211R
  1197. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
  1198. return wpa_auth_derive_ptk_ft(sm, pmk, ptk);
  1199. #endif /* CONFIG_IEEE80211R */
  1200. wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
  1201. sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
  1202. (u8 *) ptk, sizeof(*ptk),
  1203. wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
  1204. return 0;
  1205. }
  1206. SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
  1207. {
  1208. struct wpa_ptk PTK;
  1209. int ok = 0;
  1210. const u8 *pmk = NULL;
  1211. SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
  1212. sm->EAPOLKeyReceived = FALSE;
  1213. /* WPA with IEEE 802.1X: use the derived PMK from EAP
  1214. * WPA-PSK: iterate through possible PSKs and select the one matching
  1215. * the packet */
  1216. for (;;) {
  1217. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1218. pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, pmk);
  1219. if (pmk == NULL)
  1220. break;
  1221. } else
  1222. pmk = sm->PMK;
  1223. wpa_derive_ptk(sm, pmk, &PTK);
  1224. if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
  1225. sm->last_rx_eapol_key_len) == 0) {
  1226. ok = 1;
  1227. break;
  1228. }
  1229. if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
  1230. break;
  1231. }
  1232. if (!ok) {
  1233. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1234. "invalid MIC in msg 2/4 of 4-Way Handshake");
  1235. return;
  1236. }
  1237. eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
  1238. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1239. /* PSK may have changed from the previous choice, so update
  1240. * state machine data based on whatever PSK was selected here.
  1241. */
  1242. os_memcpy(sm->PMK, pmk, PMK_LEN);
  1243. }
  1244. sm->MICVerified = TRUE;
  1245. os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
  1246. sm->PTK_valid = TRUE;
  1247. }
  1248. SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
  1249. {
  1250. SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
  1251. sm->TimeoutCtr = 0;
  1252. }
  1253. #ifdef CONFIG_IEEE80211W
  1254. static int ieee80211w_kde_len(struct wpa_state_machine *sm)
  1255. {
  1256. if (sm->mgmt_frame_prot) {
  1257. return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde);
  1258. }
  1259. return 0;
  1260. }
  1261. static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
  1262. {
  1263. struct wpa_igtk_kde igtk;
  1264. struct wpa_group *gsm = sm->group;
  1265. if (!sm->mgmt_frame_prot)
  1266. return pos;
  1267. igtk.keyid[0] = gsm->GN_igtk;
  1268. igtk.keyid[1] = 0;
  1269. if (wpa_auth_get_seqnum_igtk(sm->wpa_auth, NULL, gsm->GN_igtk, igtk.pn)
  1270. < 0)
  1271. os_memset(igtk.pn, 0, sizeof(igtk.pn));
  1272. os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
  1273. pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
  1274. (const u8 *) &igtk, sizeof(igtk), NULL, 0);
  1275. return pos;
  1276. }
  1277. #else /* CONFIG_IEEE80211W */
  1278. static int ieee80211w_kde_len(struct wpa_state_machine *sm)
  1279. {
  1280. return 0;
  1281. }
  1282. static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
  1283. {
  1284. return pos;
  1285. }
  1286. #endif /* CONFIG_IEEE80211W */
  1287. SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
  1288. {
  1289. u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
  1290. size_t gtk_len, kde_len;
  1291. struct wpa_group *gsm = sm->group;
  1292. u8 *wpa_ie;
  1293. int wpa_ie_len, secure, keyidx, encr = 0;
  1294. SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
  1295. sm->TimeoutEvt = FALSE;
  1296. sm->TimeoutCtr++;
  1297. if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
  1298. /* No point in sending the EAPOL-Key - we will disconnect
  1299. * immediately following this. */
  1300. return;
  1301. }
  1302. /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN])
  1303. */
  1304. os_memset(rsc, 0, WPA_KEY_RSC_LEN);
  1305. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
  1306. wpa_ie = sm->wpa_auth->wpa_ie;
  1307. wpa_ie_len = sm->wpa_auth->wpa_ie_len;
  1308. if (sm->wpa == WPA_VERSION_WPA &&
  1309. (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
  1310. wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
  1311. /* WPA-only STA, remove RSN IE */
  1312. wpa_ie = wpa_ie + wpa_ie[1] + 2;
  1313. wpa_ie_len = wpa_ie[1] + 2;
  1314. }
  1315. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1316. "sending 3/4 msg of 4-Way Handshake");
  1317. if (sm->wpa == WPA_VERSION_WPA2) {
  1318. /* WPA2 send GTK in the 4-way handshake */
  1319. secure = 1;
  1320. gtk = gsm->GTK[gsm->GN - 1];
  1321. gtk_len = gsm->GTK_len;
  1322. keyidx = gsm->GN;
  1323. _rsc = rsc;
  1324. encr = 1;
  1325. } else {
  1326. /* WPA does not include GTK in msg 3/4 */
  1327. secure = 0;
  1328. gtk = NULL;
  1329. gtk_len = 0;
  1330. keyidx = 0;
  1331. _rsc = NULL;
  1332. }
  1333. kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
  1334. if (gtk)
  1335. kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
  1336. kde = os_malloc(kde_len);
  1337. if (kde == NULL)
  1338. return;
  1339. pos = kde;
  1340. os_memcpy(pos, wpa_ie, wpa_ie_len);
  1341. pos += wpa_ie_len;
  1342. if (gtk) {
  1343. u8 hdr[2];
  1344. hdr[0] = keyidx & 0x03;
  1345. hdr[1] = 0;
  1346. pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
  1347. gtk, gtk_len);
  1348. }
  1349. pos = ieee80211w_kde_add(sm, pos);
  1350. wpa_send_eapol(sm->wpa_auth, sm,
  1351. (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
  1352. WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
  1353. WPA_KEY_INFO_KEY_TYPE,
  1354. _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
  1355. os_free(kde);
  1356. }
  1357. SM_STATE(WPA_PTK, PTKINITDONE)
  1358. {
  1359. SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
  1360. sm->EAPOLKeyReceived = FALSE;
  1361. if (sm->Pair) {
  1362. char *alg;
  1363. int klen;
  1364. if (sm->pairwise == WPA_CIPHER_TKIP) {
  1365. alg = "TKIP";
  1366. klen = 32;
  1367. } else {
  1368. alg = "CCMP";
  1369. klen = 16;
  1370. }
  1371. if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
  1372. sm->PTK.tk1, klen)) {
  1373. wpa_sta_disconnect(sm->wpa_auth, sm->addr);
  1374. return;
  1375. }
  1376. /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
  1377. sm->pairwise_set = TRUE;
  1378. if (sm->wpa_auth->conf.wpa_ptk_rekey) {
  1379. eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
  1380. eloop_register_timeout(sm->wpa_auth->conf.
  1381. wpa_ptk_rekey, 0, wpa_rekey_ptk,
  1382. sm->wpa_auth, sm);
  1383. }
  1384. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1385. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1386. WPA_EAPOL_authorized, 1);
  1387. }
  1388. }
  1389. if (0 /* IBSS == TRUE */) {
  1390. sm->keycount++;
  1391. if (sm->keycount == 2) {
  1392. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1393. WPA_EAPOL_portValid, 1);
  1394. }
  1395. } else {
  1396. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
  1397. 1);
  1398. }
  1399. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
  1400. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
  1401. if (sm->wpa == WPA_VERSION_WPA)
  1402. sm->PInitAKeys = TRUE;
  1403. else
  1404. sm->has_GTK = TRUE;
  1405. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1406. "pairwise key handshake completed (%s)",
  1407. sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
  1408. #ifdef CONFIG_IEEE80211R
  1409. wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
  1410. #endif /* CONFIG_IEEE80211R */
  1411. }
  1412. SM_STEP(WPA_PTK)
  1413. {
  1414. struct wpa_authenticator *wpa_auth = sm->wpa_auth;
  1415. if (sm->Init)
  1416. SM_ENTER(WPA_PTK, INITIALIZE);
  1417. else if (sm->Disconnect
  1418. /* || FIX: dot11RSNAConfigSALifetime timeout */)
  1419. SM_ENTER(WPA_PTK, DISCONNECT);
  1420. else if (sm->DeauthenticationRequest)
  1421. SM_ENTER(WPA_PTK, DISCONNECTED);
  1422. else if (sm->AuthenticationRequest)
  1423. SM_ENTER(WPA_PTK, AUTHENTICATION);
  1424. else if (sm->ReAuthenticationRequest)
  1425. SM_ENTER(WPA_PTK, AUTHENTICATION2);
  1426. else if (sm->PTKRequest)
  1427. SM_ENTER(WPA_PTK, PTKSTART);
  1428. else switch (sm->wpa_ptk_state) {
  1429. case WPA_PTK_INITIALIZE:
  1430. break;
  1431. case WPA_PTK_DISCONNECT:
  1432. SM_ENTER(WPA_PTK, DISCONNECTED);
  1433. break;
  1434. case WPA_PTK_DISCONNECTED:
  1435. SM_ENTER(WPA_PTK, INITIALIZE);
  1436. break;
  1437. case WPA_PTK_AUTHENTICATION:
  1438. SM_ENTER(WPA_PTK, AUTHENTICATION2);
  1439. break;
  1440. case WPA_PTK_AUTHENTICATION2:
  1441. if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
  1442. wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
  1443. WPA_EAPOL_keyRun) > 0)
  1444. SM_ENTER(WPA_PTK, INITPMK);
  1445. else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
  1446. /* FIX: && 802.1X::keyRun */)
  1447. SM_ENTER(WPA_PTK, INITPSK);
  1448. break;
  1449. case WPA_PTK_INITPMK:
  1450. if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
  1451. WPA_EAPOL_keyAvailable) > 0)
  1452. SM_ENTER(WPA_PTK, PTKSTART);
  1453. else {
  1454. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1455. SM_ENTER(WPA_PTK, DISCONNECT);
  1456. }
  1457. break;
  1458. case WPA_PTK_INITPSK:
  1459. if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL))
  1460. SM_ENTER(WPA_PTK, PTKSTART);
  1461. else {
  1462. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1463. "no PSK configured for the STA");
  1464. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1465. SM_ENTER(WPA_PTK, DISCONNECT);
  1466. }
  1467. break;
  1468. case WPA_PTK_PTKSTART:
  1469. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1470. sm->EAPOLKeyPairwise)
  1471. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
  1472. else if (sm->TimeoutCtr >
  1473. (int) dot11RSNAConfigPairwiseUpdateCount) {
  1474. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1475. SM_ENTER(WPA_PTK, DISCONNECT);
  1476. } else if (sm->TimeoutEvt)
  1477. SM_ENTER(WPA_PTK, PTKSTART);
  1478. break;
  1479. case WPA_PTK_PTKCALCNEGOTIATING:
  1480. if (sm->MICVerified)
  1481. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
  1482. else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1483. sm->EAPOLKeyPairwise)
  1484. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
  1485. else if (sm->TimeoutEvt)
  1486. SM_ENTER(WPA_PTK, PTKSTART);
  1487. break;
  1488. case WPA_PTK_PTKCALCNEGOTIATING2:
  1489. SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
  1490. break;
  1491. case WPA_PTK_PTKINITNEGOTIATING:
  1492. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1493. sm->EAPOLKeyPairwise && sm->MICVerified)
  1494. SM_ENTER(WPA_PTK, PTKINITDONE);
  1495. else if (sm->TimeoutCtr >
  1496. (int) dot11RSNAConfigPairwiseUpdateCount) {
  1497. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1498. SM_ENTER(WPA_PTK, DISCONNECT);
  1499. } else if (sm->TimeoutEvt)
  1500. SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
  1501. break;
  1502. case WPA_PTK_PTKINITDONE:
  1503. break;
  1504. }
  1505. }
  1506. SM_STATE(WPA_PTK_GROUP, IDLE)
  1507. {
  1508. SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
  1509. if (sm->Init) {
  1510. /* Init flag is not cleared here, so avoid busy
  1511. * loop by claiming nothing changed. */
  1512. sm->changed = FALSE;
  1513. }
  1514. sm->GTimeoutCtr = 0;
  1515. }
  1516. SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
  1517. {
  1518. u8 rsc[WPA_KEY_RSC_LEN];
  1519. struct wpa_group *gsm = sm->group;
  1520. u8 *kde, *pos, hdr[2];
  1521. size_t kde_len;
  1522. SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
  1523. sm->GTimeoutCtr++;
  1524. if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
  1525. /* No point in sending the EAPOL-Key - we will disconnect
  1526. * immediately following this. */
  1527. return;
  1528. }
  1529. if (sm->wpa == WPA_VERSION_WPA)
  1530. sm->PInitAKeys = FALSE;
  1531. sm->TimeoutEvt = FALSE;
  1532. /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
  1533. os_memset(rsc, 0, WPA_KEY_RSC_LEN);
  1534. if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
  1535. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
  1536. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1537. "sending 1/2 msg of Group Key Handshake");
  1538. if (sm->wpa == WPA_VERSION_WPA2) {
  1539. kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
  1540. ieee80211w_kde_len(sm);
  1541. kde = os_malloc(kde_len);
  1542. if (kde == NULL)
  1543. return;
  1544. pos = kde;
  1545. hdr[0] = gsm->GN & 0x03;
  1546. hdr[1] = 0;
  1547. pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
  1548. gsm->GTK[gsm->GN - 1], gsm->GTK_len);
  1549. pos = ieee80211w_kde_add(sm, pos);
  1550. } else {
  1551. kde = gsm->GTK[gsm->GN - 1];
  1552. pos = kde + gsm->GTK_len;
  1553. }
  1554. wpa_send_eapol(sm->wpa_auth, sm,
  1555. WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
  1556. WPA_KEY_INFO_ACK |
  1557. (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
  1558. rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1);
  1559. if (sm->wpa == WPA_VERSION_WPA2)
  1560. os_free(kde);
  1561. }
  1562. SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
  1563. {
  1564. SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
  1565. sm->EAPOLKeyReceived = FALSE;
  1566. if (sm->GUpdateStationKeys)
  1567. sm->group->GKeyDoneStations--;
  1568. sm->GUpdateStationKeys = FALSE;
  1569. sm->GTimeoutCtr = 0;
  1570. /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
  1571. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1572. "group key handshake completed (%s)",
  1573. sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
  1574. sm->has_GTK = TRUE;
  1575. }
  1576. SM_STATE(WPA_PTK_GROUP, KEYERROR)
  1577. {
  1578. SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
  1579. if (sm->GUpdateStationKeys)
  1580. sm->group->GKeyDoneStations--;
  1581. sm->GUpdateStationKeys = FALSE;
  1582. sm->Disconnect = TRUE;
  1583. }
  1584. SM_STEP(WPA_PTK_GROUP)
  1585. {
  1586. if (sm->Init || sm->PtkGroupInit) {
  1587. SM_ENTER(WPA_PTK_GROUP, IDLE);
  1588. sm->PtkGroupInit = FALSE;
  1589. } else switch (sm->wpa_ptk_group_state) {
  1590. case WPA_PTK_GROUP_IDLE:
  1591. if (sm->GUpdateStationKeys ||
  1592. (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
  1593. SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
  1594. break;
  1595. case WPA_PTK_GROUP_REKEYNEGOTIATING:
  1596. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1597. !sm->EAPOLKeyPairwise && sm->MICVerified)
  1598. SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
  1599. else if (sm->GTimeoutCtr >
  1600. (int) dot11RSNAConfigGroupUpdateCount)
  1601. SM_ENTER(WPA_PTK_GROUP, KEYERROR);
  1602. else if (sm->TimeoutEvt)
  1603. SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
  1604. break;
  1605. case WPA_PTK_GROUP_KEYERROR:
  1606. SM_ENTER(WPA_PTK_GROUP, IDLE);
  1607. break;
  1608. case WPA_PTK_GROUP_REKEYESTABLISHED:
  1609. SM_ENTER(WPA_PTK_GROUP, IDLE);
  1610. break;
  1611. }
  1612. }
  1613. static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
  1614. struct wpa_group *group)
  1615. {
  1616. int ret = 0;
  1617. /* FIX: is this the correct way of getting GNonce? */
  1618. os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
  1619. inc_byte_array(group->Counter, WPA_NONCE_LEN);
  1620. wpa_gmk_to_gtk(group->GMK, wpa_auth->addr, group->GNonce,
  1621. group->GTK[group->GN - 1], group->GTK_len);
  1622. #ifdef CONFIG_IEEE80211W
  1623. if (wpa_auth->conf.ieee80211w != WPA_NO_IEEE80211W) {
  1624. if (os_get_random(group->IGTK[group->GN_igtk - 4],
  1625. WPA_IGTK_LEN) < 0) {
  1626. wpa_printf(MSG_INFO, "RSN: Failed to get new random "
  1627. "IGTK");
  1628. ret = -1;
  1629. }
  1630. wpa_hexdump_key(MSG_DEBUG, "IGTK",
  1631. group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN);
  1632. }
  1633. #endif /* CONFIG_IEEE80211W */
  1634. return ret;
  1635. }
  1636. static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
  1637. struct wpa_group *group)
  1638. {
  1639. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  1640. "GTK_INIT (VLAN-ID %d)", group->vlan_id);
  1641. group->changed = FALSE; /* GInit is not cleared here; avoid loop */
  1642. group->wpa_group_state = WPA_GROUP_GTK_INIT;
  1643. /* GTK[0..N] = 0 */
  1644. os_memset(group->GTK, 0, sizeof(group->GTK));
  1645. group->GN = 1;
  1646. group->GM = 2;
  1647. #ifdef CONFIG_IEEE80211W
  1648. group->GN_igtk = 4;
  1649. group->GM_igtk = 5;
  1650. #endif /* CONFIG_IEEE80211W */
  1651. /* GTK[GN] = CalcGTK() */
  1652. wpa_gtk_update(wpa_auth, group);
  1653. }
  1654. static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
  1655. {
  1656. if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
  1657. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1658. "Not in PTKINITDONE; skip Group Key update");
  1659. return 0;
  1660. }
  1661. if (sm->GUpdateStationKeys) {
  1662. /*
  1663. * This should not really happen, but just in case, make sure
  1664. * we do not count the same STA twice in GKeyDoneStations.
  1665. */
  1666. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1667. "GUpdateStationKeys already set - do not "
  1668. "increment GKeyDoneStations");
  1669. } else {
  1670. sm->group->GKeyDoneStations++;
  1671. sm->GUpdateStationKeys = TRUE;
  1672. }
  1673. wpa_sm_step(sm);
  1674. return 0;
  1675. }
  1676. static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
  1677. struct wpa_group *group)
  1678. {
  1679. int tmp;
  1680. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  1681. "SETKEYS (VLAN-ID %d)", group->vlan_id);
  1682. group->changed = TRUE;
  1683. group->wpa_group_state = WPA_GROUP_SETKEYS;
  1684. group->GTKReKey = FALSE;
  1685. tmp = group->GM;
  1686. group->GM = group->GN;
  1687. group->GN = tmp;
  1688. #ifdef CONFIG_IEEE80211W
  1689. tmp = group->GM_igtk;
  1690. group->GM_igtk = group->GN_igtk;
  1691. group->GN_igtk = tmp;
  1692. #endif /* CONFIG_IEEE80211W */
  1693. /* "GKeyDoneStations = GNoStations" is done in more robust way by
  1694. * counting the STAs that are marked with GUpdateStationKeys instead of
  1695. * including all STAs that could be in not-yet-completed state. */
  1696. wpa_gtk_update(wpa_auth, group);
  1697. wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, NULL);
  1698. wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
  1699. group->GKeyDoneStations);
  1700. }
  1701. static void wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
  1702. struct wpa_group *group)
  1703. {
  1704. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  1705. "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
  1706. group->changed = TRUE;
  1707. group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
  1708. wpa_auth_set_key(wpa_auth, group->vlan_id,
  1709. wpa_alg_txt(wpa_auth->conf.wpa_group),
  1710. NULL, group->GN, group->GTK[group->GN - 1],
  1711. group->GTK_len);
  1712. #ifdef CONFIG_IEEE80211W
  1713. if (wpa_auth->conf.ieee80211w != WPA_NO_IEEE80211W) {
  1714. wpa_auth_set_key(wpa_auth, group->vlan_id, "IGTK",
  1715. NULL, group->GN_igtk,
  1716. group->IGTK[group->GN_igtk - 4],
  1717. WPA_IGTK_LEN);
  1718. }
  1719. #endif /* CONFIG_IEEE80211W */
  1720. }
  1721. static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
  1722. struct wpa_group *group)
  1723. {
  1724. if (group->GInit) {
  1725. wpa_group_gtk_init(wpa_auth, group);
  1726. } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
  1727. group->GTKAuthenticator) {
  1728. wpa_group_setkeysdone(wpa_auth, group);
  1729. } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
  1730. group->GTKReKey) {
  1731. wpa_group_setkeys(wpa_auth, group);
  1732. } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
  1733. if (group->GKeyDoneStations == 0)
  1734. wpa_group_setkeysdone(wpa_auth, group);
  1735. else if (group->GTKReKey)
  1736. wpa_group_setkeys(wpa_auth, group);
  1737. }
  1738. }
  1739. static void wpa_sm_step(struct wpa_state_machine *sm)
  1740. {
  1741. if (sm == NULL)
  1742. return;
  1743. if (sm->in_step_loop) {
  1744. /* This should not happen, but if it does, make sure we do not
  1745. * end up freeing the state machine too early by exiting the
  1746. * recursive call. */
  1747. wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
  1748. return;
  1749. }
  1750. sm->in_step_loop = 1;
  1751. do {
  1752. if (sm->pending_deinit)
  1753. break;
  1754. sm->changed = FALSE;
  1755. sm->wpa_auth->group->changed = FALSE;
  1756. SM_STEP_RUN(WPA_PTK);
  1757. if (sm->pending_deinit)
  1758. break;
  1759. SM_STEP_RUN(WPA_PTK_GROUP);
  1760. if (sm->pending_deinit)
  1761. break;
  1762. wpa_group_sm_step(sm->wpa_auth, sm->group);
  1763. } while (sm->changed || sm->wpa_auth->group->changed);
  1764. sm->in_step_loop = 0;
  1765. if (sm->pending_deinit) {
  1766. wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
  1767. "machine deinit for " MACSTR, MAC2STR(sm->addr));
  1768. wpa_free_sta_sm(sm);
  1769. }
  1770. }
  1771. static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
  1772. {
  1773. struct wpa_state_machine *sm = eloop_ctx;
  1774. wpa_sm_step(sm);
  1775. }
  1776. void wpa_auth_sm_notify(struct wpa_state_machine *sm)
  1777. {
  1778. if (sm == NULL)
  1779. return;
  1780. eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
  1781. }
  1782. void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
  1783. {
  1784. int tmp, i;
  1785. struct wpa_group *group;
  1786. if (wpa_auth == NULL)
  1787. return;
  1788. group = wpa_auth->group;
  1789. for (i = 0; i < 2; i++) {
  1790. tmp = group->GM;
  1791. group->GM = group->GN;
  1792. group->GN = tmp;
  1793. #ifdef CONFIG_IEEE80211W
  1794. tmp = group->GM_igtk;
  1795. group->GM_igtk = group->GN_igtk;
  1796. group->GN_igtk = tmp;
  1797. #endif /* CONFIG_IEEE80211W */
  1798. wpa_gtk_update(wpa_auth, group);
  1799. }
  1800. }
  1801. static const char * wpa_bool_txt(int bool)
  1802. {
  1803. return bool ? "TRUE" : "FALSE";
  1804. }
  1805. static int wpa_cipher_bits(int cipher)
  1806. {
  1807. switch (cipher) {
  1808. case WPA_CIPHER_CCMP:
  1809. return 128;
  1810. case WPA_CIPHER_TKIP:
  1811. return 256;
  1812. case WPA_CIPHER_WEP104:
  1813. return 104;
  1814. case WPA_CIPHER_WEP40:
  1815. return 40;
  1816. default:
  1817. return 0;
  1818. }
  1819. }
  1820. #define RSN_SUITE "%02x-%02x-%02x-%d"
  1821. #define RSN_SUITE_ARG(s) \
  1822. ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
  1823. int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
  1824. {
  1825. int len = 0, ret;
  1826. char pmkid_txt[PMKID_LEN * 2 + 1];
  1827. if (wpa_auth == NULL)
  1828. return len;
  1829. ret = os_snprintf(buf + len, buflen - len,
  1830. "dot11RSNAOptionImplemented=TRUE\n"
  1831. #ifdef CONFIG_RSN_PREAUTH
  1832. "dot11RSNAPreauthenticationImplemented=TRUE\n"
  1833. #else /* CONFIG_RSN_PREAUTH */
  1834. "dot11RSNAPreauthenticationImplemented=FALSE\n"
  1835. #endif /* CONFIG_RSN_PREAUTH */
  1836. "dot11RSNAEnabled=%s\n"
  1837. "dot11RSNAPreauthenticationEnabled=%s\n",
  1838. wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
  1839. wpa_bool_txt(wpa_auth->conf.rsn_preauth));
  1840. if (ret < 0 || (size_t) ret >= buflen - len)
  1841. return len;
  1842. len += ret;
  1843. wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
  1844. wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
  1845. ret = os_snprintf(
  1846. buf + len, buflen - len,
  1847. "dot11RSNAConfigVersion=%u\n"
  1848. "dot11RSNAConfigPairwiseKeysSupported=9999\n"
  1849. /* FIX: dot11RSNAConfigGroupCipher */
  1850. /* FIX: dot11RSNAConfigGroupRekeyMethod */
  1851. /* FIX: dot11RSNAConfigGroupRekeyTime */
  1852. /* FIX: dot11RSNAConfigGroupRekeyPackets */
  1853. "dot11RSNAConfigGroupRekeyStrict=%u\n"
  1854. "dot11RSNAConfigGroupUpdateCount=%u\n"
  1855. "dot11RSNAConfigPairwiseUpdateCount=%u\n"
  1856. "dot11RSNAConfigGroupCipherSize=%u\n"
  1857. "dot11RSNAConfigPMKLifetime=%u\n"
  1858. "dot11RSNAConfigPMKReauthThreshold=%u\n"
  1859. "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
  1860. "dot11RSNAConfigSATimeout=%u\n"
  1861. "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
  1862. "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
  1863. "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
  1864. "dot11RSNAPMKIDUsed=%s\n"
  1865. "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
  1866. "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
  1867. "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
  1868. "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
  1869. "dot11RSNA4WayHandshakeFailures=%u\n"
  1870. "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
  1871. RSN_VERSION,
  1872. !!wpa_auth->conf.wpa_strict_rekey,
  1873. dot11RSNAConfigGroupUpdateCount,
  1874. dot11RSNAConfigPairwiseUpdateCount,
  1875. wpa_cipher_bits(wpa_auth->conf.wpa_group),
  1876. dot11RSNAConfigPMKLifetime,
  1877. dot11RSNAConfigPMKReauthThreshold,
  1878. dot11RSNAConfigSATimeout,
  1879. RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
  1880. RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
  1881. RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
  1882. pmkid_txt,
  1883. RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
  1884. RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
  1885. RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
  1886. wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
  1887. wpa_auth->dot11RSNA4WayHandshakeFailures);
  1888. if (ret < 0 || (size_t) ret >= buflen - len)
  1889. return len;
  1890. len += ret;
  1891. /* TODO: dot11RSNAConfigPairwiseCiphersTable */
  1892. /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
  1893. /* Private MIB */
  1894. ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
  1895. wpa_auth->group->wpa_group_state);
  1896. if (ret < 0 || (size_t) ret >= buflen - len)
  1897. return len;
  1898. len += ret;
  1899. return len;
  1900. }
  1901. int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
  1902. {
  1903. int len = 0, ret;
  1904. u32 pairwise = 0;
  1905. if (sm == NULL)
  1906. return 0;
  1907. /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
  1908. /* dot11RSNAStatsEntry */
  1909. if (sm->wpa == WPA_VERSION_WPA) {
  1910. if (sm->pairwise == WPA_CIPHER_CCMP)
  1911. pairwise = WPA_CIPHER_SUITE_CCMP;
  1912. else if (sm->pairwise == WPA_CIPHER_TKIP)
  1913. pairwise = WPA_CIPHER_SUITE_TKIP;
  1914. else if (sm->pairwise == WPA_CIPHER_WEP104)
  1915. pairwise = WPA_CIPHER_SUITE_WEP104;
  1916. else if (sm->pairwise == WPA_CIPHER_WEP40)
  1917. pairwise = WPA_CIPHER_SUITE_WEP40;
  1918. else if (sm->pairwise == WPA_CIPHER_NONE)
  1919. pairwise = WPA_CIPHER_SUITE_NONE;
  1920. } else if (sm->wpa == WPA_VERSION_WPA2) {
  1921. if (sm->pairwise == WPA_CIPHER_CCMP)
  1922. pairwise = RSN_CIPHER_SUITE_CCMP;
  1923. else if (sm->pairwise == WPA_CIPHER_TKIP)
  1924. pairwise = RSN_CIPHER_SUITE_TKIP;
  1925. else if (sm->pairwise == WPA_CIPHER_WEP104)
  1926. pairwise = RSN_CIPHER_SUITE_WEP104;
  1927. else if (sm->pairwise == WPA_CIPHER_WEP40)
  1928. pairwise = RSN_CIPHER_SUITE_WEP40;
  1929. else if (sm->pairwise == WPA_CIPHER_NONE)
  1930. pairwise = RSN_CIPHER_SUITE_NONE;
  1931. } else
  1932. return 0;
  1933. ret = os_snprintf(
  1934. buf + len, buflen - len,
  1935. /* TODO: dot11RSNAStatsIndex */
  1936. "dot11RSNAStatsSTAAddress=" MACSTR "\n"
  1937. "dot11RSNAStatsVersion=1\n"
  1938. "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
  1939. /* TODO: dot11RSNAStatsTKIPICVErrors */
  1940. "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
  1941. "dot11RSNAStatsTKIPRemoveMICFailures=%u\n"
  1942. /* TODO: dot11RSNAStatsCCMPReplays */
  1943. /* TODO: dot11RSNAStatsCCMPDecryptErrors */
  1944. /* TODO: dot11RSNAStatsTKIPReplays */,
  1945. MAC2STR(sm->addr),
  1946. RSN_SUITE_ARG(pairwise),
  1947. sm->dot11RSNAStatsTKIPLocalMICFailures,
  1948. sm->dot11RSNAStatsTKIPRemoteMICFailures);
  1949. if (ret < 0 || (size_t) ret >= buflen - len)
  1950. return len;
  1951. len += ret;
  1952. /* Private MIB */
  1953. ret = os_snprintf(buf + len, buflen - len,
  1954. "hostapdWPAPTKState=%d\n"
  1955. "hostapdWPAPTKGroupState=%d\n",
  1956. sm->wpa_ptk_state,
  1957. sm->wpa_ptk_group_state);
  1958. if (ret < 0 || (size_t) ret >= buflen - len)
  1959. return len;
  1960. len += ret;
  1961. return len;
  1962. }
  1963. void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
  1964. {
  1965. if (wpa_auth)
  1966. wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
  1967. }
  1968. int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
  1969. {
  1970. return sm && sm->pairwise_set;
  1971. }
  1972. int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
  1973. {
  1974. return sm->pairwise;
  1975. }
  1976. int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
  1977. {
  1978. if (sm == NULL)
  1979. return -1;
  1980. return sm->wpa_key_mgmt;
  1981. }
  1982. int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
  1983. {
  1984. if (sm == NULL)
  1985. return 0;
  1986. return sm->wpa;
  1987. }
  1988. int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
  1989. struct rsn_pmksa_cache_entry *entry)
  1990. {
  1991. if (sm == NULL || sm->pmksa != entry)
  1992. return -1;
  1993. sm->pmksa = NULL;
  1994. return 0;
  1995. }
  1996. struct rsn_pmksa_cache_entry *
  1997. wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
  1998. {
  1999. return sm ? sm->pmksa : NULL;
  2000. }
  2001. void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
  2002. {
  2003. if (sm)
  2004. sm->dot11RSNAStatsTKIPLocalMICFailures++;
  2005. }
  2006. const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
  2007. {
  2008. if (wpa_auth == NULL)
  2009. return NULL;
  2010. *len = wpa_auth->wpa_ie_len;
  2011. return wpa_auth->wpa_ie;
  2012. }
  2013. int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
  2014. int session_timeout, struct eapol_state_machine *eapol)
  2015. {
  2016. if (sm == NULL || sm->wpa != WPA_VERSION_WPA2)
  2017. return -1;
  2018. if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
  2019. sm->wpa_auth->addr, sm->addr, session_timeout,
  2020. eapol, sm->wpa_key_mgmt))
  2021. return 0;
  2022. return -1;
  2023. }
  2024. int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
  2025. const u8 *pmk, size_t len, const u8 *sta_addr,
  2026. int session_timeout,
  2027. struct eapol_state_machine *eapol)
  2028. {
  2029. if (wpa_auth == NULL)
  2030. return -1;
  2031. if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr,
  2032. sta_addr, session_timeout, eapol,
  2033. WPA_KEY_MGMT_IEEE8021X))
  2034. return 0;
  2035. return -1;
  2036. }
  2037. static struct wpa_group *
  2038. wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
  2039. {
  2040. struct wpa_group *group;
  2041. if (wpa_auth == NULL || wpa_auth->group == NULL)
  2042. return NULL;
  2043. wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
  2044. vlan_id);
  2045. group = wpa_group_init(wpa_auth, vlan_id);
  2046. if (group == NULL)
  2047. return NULL;
  2048. group->next = wpa_auth->group->next;
  2049. wpa_auth->group->next = group;
  2050. return group;
  2051. }
  2052. int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
  2053. {
  2054. struct wpa_group *group;
  2055. if (sm == NULL || sm->wpa_auth == NULL)
  2056. return 0;
  2057. group = sm->wpa_auth->group;
  2058. while (group) {
  2059. if (group->vlan_id == vlan_id)
  2060. break;
  2061. group = group->next;
  2062. }
  2063. if (group == NULL) {
  2064. group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
  2065. if (group == NULL)
  2066. return -1;
  2067. }
  2068. if (sm->group == group)
  2069. return 0;
  2070. wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
  2071. "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
  2072. sm->group = group;
  2073. return 0;
  2074. }
  2075. #endif /* CONFIG_NATIVE_WINDOWS */