wpa.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466
  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. wpa_alg 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. if (sm->wpa == WPA_VERSION_WPA2) {
  503. if (key->type != EAPOL_KEY_TYPE_RSN) {
  504. wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
  505. "unexpected type %d in RSN mode",
  506. key->type);
  507. return;
  508. }
  509. } else {
  510. if (key->type != EAPOL_KEY_TYPE_WPA) {
  511. wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
  512. "unexpected type %d in WPA mode",
  513. key->type);
  514. return;
  515. }
  516. }
  517. /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
  518. * are set */
  519. if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
  520. (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
  521. if (key_info & WPA_KEY_INFO_ERROR) {
  522. msg = SMK_ERROR;
  523. msgtxt = "SMK Error";
  524. } else {
  525. msg = SMK_M1;
  526. msgtxt = "SMK M1";
  527. }
  528. } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
  529. msg = SMK_M3;
  530. msgtxt = "SMK M3";
  531. } else if (key_info & WPA_KEY_INFO_REQUEST) {
  532. msg = REQUEST;
  533. msgtxt = "Request";
  534. } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
  535. msg = GROUP_2;
  536. msgtxt = "2/2 Group";
  537. } else if (key_data_length == 0) {
  538. msg = PAIRWISE_4;
  539. msgtxt = "4/4 Pairwise";
  540. } else {
  541. msg = PAIRWISE_2;
  542. msgtxt = "2/4 Pairwise";
  543. }
  544. /* TODO: key_info type validation for PeerKey */
  545. if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
  546. msg == GROUP_2) {
  547. u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  548. if (sm->pairwise == WPA_CIPHER_CCMP) {
  549. if (wpa_use_aes_cmac(sm) &&
  550. ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  551. wpa_auth_logger(wpa_auth, sm->addr,
  552. LOGGER_WARNING,
  553. "advertised support for "
  554. "AES-128-CMAC, but did not "
  555. "use it");
  556. return;
  557. }
  558. if (!wpa_use_aes_cmac(sm) &&
  559. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
  560. wpa_auth_logger(wpa_auth, sm->addr,
  561. LOGGER_WARNING,
  562. "did not use HMAC-SHA1-AES "
  563. "with CCMP");
  564. return;
  565. }
  566. }
  567. }
  568. if (key_info & WPA_KEY_INFO_REQUEST) {
  569. if (sm->req_replay_counter_used &&
  570. os_memcmp(key->replay_counter, sm->req_replay_counter,
  571. WPA_REPLAY_COUNTER_LEN) <= 0) {
  572. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
  573. "received EAPOL-Key request with "
  574. "replayed counter");
  575. return;
  576. }
  577. }
  578. if (!(key_info & WPA_KEY_INFO_REQUEST) &&
  579. !wpa_replay_counter_valid(sm, key->replay_counter)) {
  580. int i;
  581. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  582. "received EAPOL-Key %s with unexpected "
  583. "replay counter", msgtxt);
  584. for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
  585. if (!sm->key_replay[i].valid)
  586. break;
  587. wpa_hexdump(MSG_DEBUG, "pending replay counter",
  588. sm->key_replay[i].counter,
  589. WPA_REPLAY_COUNTER_LEN);
  590. }
  591. wpa_hexdump(MSG_DEBUG, "received replay counter",
  592. key->replay_counter, WPA_REPLAY_COUNTER_LEN);
  593. return;
  594. }
  595. switch (msg) {
  596. case PAIRWISE_2:
  597. if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
  598. sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING) {
  599. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  600. "received EAPOL-Key msg 2/4 in "
  601. "invalid state (%d) - dropped",
  602. sm->wpa_ptk_state);
  603. return;
  604. }
  605. if (sm->wpa_ie == NULL ||
  606. sm->wpa_ie_len != key_data_length ||
  607. os_memcmp(sm->wpa_ie, key + 1, key_data_length) != 0) {
  608. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  609. "WPA IE from (Re)AssocReq did not "
  610. "match with msg 2/4");
  611. if (sm->wpa_ie) {
  612. wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
  613. sm->wpa_ie, sm->wpa_ie_len);
  614. }
  615. wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
  616. (u8 *) (key + 1), key_data_length);
  617. /* MLME-DEAUTHENTICATE.request */
  618. wpa_sta_disconnect(wpa_auth, sm->addr);
  619. return;
  620. }
  621. break;
  622. case PAIRWISE_4:
  623. if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
  624. !sm->PTK_valid) {
  625. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  626. "received EAPOL-Key msg 4/4 in "
  627. "invalid state (%d) - dropped",
  628. sm->wpa_ptk_state);
  629. return;
  630. }
  631. break;
  632. case GROUP_2:
  633. if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
  634. || !sm->PTK_valid) {
  635. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  636. "received EAPOL-Key msg 2/2 in "
  637. "invalid state (%d) - dropped",
  638. sm->wpa_ptk_group_state);
  639. return;
  640. }
  641. break;
  642. #ifdef CONFIG_PEERKEY
  643. case SMK_M1:
  644. case SMK_M3:
  645. case SMK_ERROR:
  646. if (!wpa_auth->conf.peerkey) {
  647. wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
  648. "PeerKey use disabled - ignoring message");
  649. return;
  650. }
  651. if (!sm->PTK_valid) {
  652. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  653. "received EAPOL-Key msg SMK in "
  654. "invalid state - dropped");
  655. return;
  656. }
  657. break;
  658. #else /* CONFIG_PEERKEY */
  659. case SMK_M1:
  660. case SMK_M3:
  661. case SMK_ERROR:
  662. return; /* STSL disabled - ignore SMK messages */
  663. #endif /* CONFIG_PEERKEY */
  664. case REQUEST:
  665. break;
  666. }
  667. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  668. "received EAPOL-Key frame (%s)", msgtxt);
  669. if (key_info & WPA_KEY_INFO_ACK) {
  670. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  671. "received invalid EAPOL-Key: Key Ack set");
  672. return;
  673. }
  674. if (!(key_info & WPA_KEY_INFO_MIC)) {
  675. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  676. "received invalid EAPOL-Key: Key MIC not set");
  677. return;
  678. }
  679. sm->MICVerified = FALSE;
  680. if (sm->PTK_valid) {
  681. if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
  682. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  683. "received EAPOL-Key with invalid MIC");
  684. return;
  685. }
  686. sm->MICVerified = TRUE;
  687. eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
  688. }
  689. if (key_info & WPA_KEY_INFO_REQUEST) {
  690. if (sm->MICVerified) {
  691. sm->req_replay_counter_used = 1;
  692. os_memcpy(sm->req_replay_counter, key->replay_counter,
  693. WPA_REPLAY_COUNTER_LEN);
  694. } else {
  695. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  696. "received EAPOL-Key request with "
  697. "invalid MIC");
  698. return;
  699. }
  700. /*
  701. * TODO: should decrypt key data field if encryption was used;
  702. * even though MAC address KDE is not normally encrypted,
  703. * supplicant is allowed to encrypt it.
  704. */
  705. if (msg == SMK_ERROR) {
  706. #ifdef CONFIG_PEERKEY
  707. wpa_smk_error(wpa_auth, sm, key);
  708. #endif /* CONFIG_PEERKEY */
  709. return;
  710. } else if (key_info & WPA_KEY_INFO_ERROR) {
  711. /* Supplicant reported a Michael MIC error */
  712. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  713. "received EAPOL-Key Error Request "
  714. "(STA detected Michael MIC failure)");
  715. wpa_auth_mic_failure_report(wpa_auth, sm->addr);
  716. sm->dot11RSNAStatsTKIPRemoteMICFailures++;
  717. wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
  718. /* Error report is not a request for a new key
  719. * handshake, but since Authenticator may do it, let's
  720. * change the keys now anyway. */
  721. wpa_request_new_ptk(sm);
  722. } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
  723. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  724. "received EAPOL-Key Request for new "
  725. "4-Way Handshake");
  726. wpa_request_new_ptk(sm);
  727. #ifdef CONFIG_PEERKEY
  728. } else if (msg == SMK_M1) {
  729. wpa_smk_m1(wpa_auth, sm, key);
  730. #endif /* CONFIG_PEERKEY */
  731. } else if (key_data_length > 0 &&
  732. wpa_parse_kde_ies((const u8 *) (key + 1),
  733. key_data_length, &kde) == 0 &&
  734. kde.mac_addr) {
  735. } else {
  736. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  737. "received EAPOL-Key Request for GTK "
  738. "rekeying");
  739. /* FIX: why was this triggering PTK rekeying for the
  740. * STA that requested Group Key rekeying?? */
  741. /* wpa_request_new_ptk(sta->wpa_sm); */
  742. eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
  743. wpa_rekey_gtk(wpa_auth, NULL);
  744. }
  745. } else {
  746. /* Do not allow the same key replay counter to be reused. This
  747. * does also invalidate all other pending replay counters if
  748. * retransmissions were used, i.e., we will only process one of
  749. * the pending replies and ignore rest if more than one is
  750. * received. */
  751. sm->key_replay[0].valid = FALSE;
  752. }
  753. #ifdef CONFIG_PEERKEY
  754. if (msg == SMK_M3) {
  755. wpa_smk_m3(wpa_auth, sm, key);
  756. return;
  757. }
  758. #endif /* CONFIG_PEERKEY */
  759. os_free(sm->last_rx_eapol_key);
  760. sm->last_rx_eapol_key = os_malloc(data_len);
  761. if (sm->last_rx_eapol_key == NULL)
  762. return;
  763. os_memcpy(sm->last_rx_eapol_key, data, data_len);
  764. sm->last_rx_eapol_key_len = data_len;
  765. sm->EAPOLKeyReceived = TRUE;
  766. sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
  767. sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
  768. os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
  769. wpa_sm_step(sm);
  770. }
  771. static void wpa_gmk_to_gtk(const u8 *gmk, const u8 *addr, const u8 *gnonce,
  772. u8 *gtk, size_t gtk_len)
  773. {
  774. u8 data[ETH_ALEN + WPA_NONCE_LEN];
  775. /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */
  776. os_memcpy(data, addr, ETH_ALEN);
  777. os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
  778. #ifdef CONFIG_IEEE80211W
  779. sha256_prf(gmk, WPA_GMK_LEN, "Group key expansion",
  780. data, sizeof(data), gtk, gtk_len);
  781. #else /* CONFIG_IEEE80211W */
  782. sha1_prf(gmk, WPA_GMK_LEN, "Group key expansion",
  783. data, sizeof(data), gtk, gtk_len);
  784. #endif /* CONFIG_IEEE80211W */
  785. wpa_hexdump_key(MSG_DEBUG, "GMK", gmk, WPA_GMK_LEN);
  786. wpa_hexdump_key(MSG_DEBUG, "GTK", gtk, gtk_len);
  787. }
  788. static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
  789. {
  790. struct wpa_authenticator *wpa_auth = eloop_ctx;
  791. struct wpa_state_machine *sm = timeout_ctx;
  792. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
  793. sm->TimeoutEvt = TRUE;
  794. wpa_sm_step(sm);
  795. }
  796. void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
  797. struct wpa_state_machine *sm, int key_info,
  798. const u8 *key_rsc, const u8 *nonce,
  799. const u8 *kde, size_t kde_len,
  800. int keyidx, int encr, int force_version)
  801. {
  802. struct ieee802_1x_hdr *hdr;
  803. struct wpa_eapol_key *key;
  804. size_t len;
  805. int alg;
  806. int key_data_len, pad_len = 0;
  807. u8 *buf, *pos;
  808. int version, pairwise;
  809. int i;
  810. len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
  811. if (force_version)
  812. version = force_version;
  813. else if (wpa_use_aes_cmac(sm))
  814. version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
  815. else if (sm->pairwise == WPA_CIPHER_CCMP)
  816. version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
  817. else
  818. version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
  819. pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
  820. wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
  821. "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
  822. "encr=%d)",
  823. version,
  824. (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
  825. (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
  826. (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
  827. (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
  828. pairwise, (unsigned long) kde_len, keyidx, encr);
  829. key_data_len = kde_len;
  830. if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
  831. version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
  832. pad_len = key_data_len % 8;
  833. if (pad_len)
  834. pad_len = 8 - pad_len;
  835. key_data_len += pad_len + 8;
  836. }
  837. len += key_data_len;
  838. hdr = os_zalloc(len);
  839. if (hdr == NULL)
  840. return;
  841. hdr->version = wpa_auth->conf.eapol_version;
  842. hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
  843. hdr->length = host_to_be16(len - sizeof(*hdr));
  844. key = (struct wpa_eapol_key *) (hdr + 1);
  845. key->type = sm->wpa == WPA_VERSION_WPA2 ?
  846. EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
  847. key_info |= version;
  848. if (encr && sm->wpa == WPA_VERSION_WPA2)
  849. key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
  850. if (sm->wpa != WPA_VERSION_WPA2)
  851. key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
  852. WPA_PUT_BE16(key->key_info, key_info);
  853. alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
  854. switch (alg) {
  855. case WPA_CIPHER_CCMP:
  856. WPA_PUT_BE16(key->key_length, 16);
  857. break;
  858. case WPA_CIPHER_TKIP:
  859. WPA_PUT_BE16(key->key_length, 32);
  860. break;
  861. case WPA_CIPHER_WEP40:
  862. WPA_PUT_BE16(key->key_length, 5);
  863. break;
  864. case WPA_CIPHER_WEP104:
  865. WPA_PUT_BE16(key->key_length, 13);
  866. break;
  867. }
  868. if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
  869. WPA_PUT_BE16(key->key_length, 0);
  870. /* FIX: STSL: what to use as key_replay_counter? */
  871. for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
  872. sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
  873. os_memcpy(sm->key_replay[i].counter,
  874. sm->key_replay[i - 1].counter,
  875. WPA_REPLAY_COUNTER_LEN);
  876. }
  877. inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
  878. os_memcpy(key->replay_counter, sm->key_replay[0].counter,
  879. WPA_REPLAY_COUNTER_LEN);
  880. sm->key_replay[0].valid = TRUE;
  881. if (nonce)
  882. os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
  883. if (key_rsc)
  884. os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
  885. if (kde && !encr) {
  886. os_memcpy(key + 1, kde, kde_len);
  887. WPA_PUT_BE16(key->key_data_length, kde_len);
  888. } else if (encr && kde) {
  889. buf = os_zalloc(key_data_len);
  890. if (buf == NULL) {
  891. os_free(hdr);
  892. return;
  893. }
  894. pos = buf;
  895. os_memcpy(pos, kde, kde_len);
  896. pos += kde_len;
  897. if (pad_len)
  898. *pos++ = 0xdd;
  899. wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
  900. buf, key_data_len);
  901. if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
  902. version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  903. if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf,
  904. (u8 *) (key + 1))) {
  905. os_free(hdr);
  906. os_free(buf);
  907. return;
  908. }
  909. WPA_PUT_BE16(key->key_data_length, key_data_len);
  910. } else {
  911. u8 ek[32];
  912. os_memcpy(key->key_iv,
  913. sm->group->Counter + WPA_NONCE_LEN - 16, 16);
  914. inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
  915. os_memcpy(ek, key->key_iv, 16);
  916. os_memcpy(ek + 16, sm->PTK.kek, 16);
  917. os_memcpy(key + 1, buf, key_data_len);
  918. rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
  919. WPA_PUT_BE16(key->key_data_length, key_data_len);
  920. }
  921. os_free(buf);
  922. }
  923. if (key_info & WPA_KEY_INFO_MIC) {
  924. if (!sm->PTK_valid) {
  925. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  926. "PTK not valid when sending EAPOL-Key "
  927. "frame");
  928. os_free(hdr);
  929. return;
  930. }
  931. wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len,
  932. key->key_mic);
  933. }
  934. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
  935. 1);
  936. wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
  937. sm->pairwise_set);
  938. os_free(hdr);
  939. }
  940. static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
  941. struct wpa_state_machine *sm, int key_info,
  942. const u8 *key_rsc, const u8 *nonce,
  943. const u8 *kde, size_t kde_len,
  944. int keyidx, int encr)
  945. {
  946. int timeout_ms;
  947. int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
  948. int ctr;
  949. if (sm == NULL)
  950. return;
  951. __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
  952. keyidx, encr, 0);
  953. ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
  954. if (ctr == 1)
  955. timeout_ms = eapol_key_timeout_first;
  956. else
  957. timeout_ms = eapol_key_timeout_subseq;
  958. eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
  959. wpa_send_eapol_timeout, wpa_auth, sm);
  960. }
  961. static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
  962. {
  963. struct ieee802_1x_hdr *hdr;
  964. struct wpa_eapol_key *key;
  965. u16 key_info;
  966. int ret = 0;
  967. u8 mic[16];
  968. if (data_len < sizeof(*hdr) + sizeof(*key))
  969. return -1;
  970. hdr = (struct ieee802_1x_hdr *) data;
  971. key = (struct wpa_eapol_key *) (hdr + 1);
  972. key_info = WPA_GET_BE16(key->key_info);
  973. os_memcpy(mic, key->key_mic, 16);
  974. os_memset(key->key_mic, 0, 16);
  975. if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  976. data, data_len, key->key_mic) ||
  977. os_memcmp(mic, key->key_mic, 16) != 0)
  978. ret = -1;
  979. os_memcpy(key->key_mic, mic, 16);
  980. return ret;
  981. }
  982. void wpa_remove_ptk(struct wpa_state_machine *sm)
  983. {
  984. sm->PTK_valid = FALSE;
  985. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  986. wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, (u8 *) "",
  987. 0);
  988. sm->pairwise_set = FALSE;
  989. eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
  990. }
  991. void wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
  992. {
  993. int remove_ptk = 1;
  994. if (sm == NULL)
  995. return;
  996. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  997. "event %d notification", event);
  998. switch (event) {
  999. case WPA_AUTH:
  1000. case WPA_ASSOC:
  1001. break;
  1002. case WPA_DEAUTH:
  1003. case WPA_DISASSOC:
  1004. sm->DeauthenticationRequest = TRUE;
  1005. break;
  1006. case WPA_REAUTH:
  1007. case WPA_REAUTH_EAPOL:
  1008. if (sm->GUpdateStationKeys) {
  1009. /*
  1010. * Reauthentication cancels the pending group key
  1011. * update for this STA.
  1012. */
  1013. sm->group->GKeyDoneStations--;
  1014. sm->GUpdateStationKeys = FALSE;
  1015. sm->PtkGroupInit = TRUE;
  1016. }
  1017. sm->ReAuthenticationRequest = TRUE;
  1018. break;
  1019. case WPA_ASSOC_FT:
  1020. #ifdef CONFIG_IEEE80211R
  1021. /* Using FT protocol, not WPA auth state machine */
  1022. sm->ft_completed = 1;
  1023. return;
  1024. #else /* CONFIG_IEEE80211R */
  1025. break;
  1026. #endif /* CONFIG_IEEE80211R */
  1027. }
  1028. #ifdef CONFIG_IEEE80211R
  1029. sm->ft_completed = 0;
  1030. #endif /* CONFIG_IEEE80211R */
  1031. #ifdef CONFIG_IEEE80211W
  1032. if (sm->mgmt_frame_prot && event == WPA_AUTH)
  1033. remove_ptk = 0;
  1034. #endif /* CONFIG_IEEE80211W */
  1035. if (remove_ptk) {
  1036. sm->PTK_valid = FALSE;
  1037. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  1038. if (event != WPA_REAUTH_EAPOL)
  1039. wpa_remove_ptk(sm);
  1040. }
  1041. wpa_sm_step(sm);
  1042. }
  1043. static wpa_alg wpa_alg_enum(int alg)
  1044. {
  1045. switch (alg) {
  1046. case WPA_CIPHER_CCMP:
  1047. return WPA_ALG_CCMP;
  1048. case WPA_CIPHER_TKIP:
  1049. return WPA_ALG_TKIP;
  1050. case WPA_CIPHER_WEP104:
  1051. case WPA_CIPHER_WEP40:
  1052. return WPA_ALG_WEP;
  1053. default:
  1054. return WPA_ALG_NONE;
  1055. }
  1056. }
  1057. SM_STATE(WPA_PTK, INITIALIZE)
  1058. {
  1059. SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
  1060. if (sm->Init) {
  1061. /* Init flag is not cleared here, so avoid busy
  1062. * loop by claiming nothing changed. */
  1063. sm->changed = FALSE;
  1064. }
  1065. sm->keycount = 0;
  1066. if (sm->GUpdateStationKeys)
  1067. sm->group->GKeyDoneStations--;
  1068. sm->GUpdateStationKeys = FALSE;
  1069. if (sm->wpa == WPA_VERSION_WPA)
  1070. sm->PInitAKeys = FALSE;
  1071. if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
  1072. * Local AA > Remote AA)) */) {
  1073. sm->Pair = TRUE;
  1074. }
  1075. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
  1076. wpa_remove_ptk(sm);
  1077. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
  1078. sm->TimeoutCtr = 0;
  1079. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1080. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1081. WPA_EAPOL_authorized, 0);
  1082. }
  1083. }
  1084. SM_STATE(WPA_PTK, DISCONNECT)
  1085. {
  1086. SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
  1087. sm->Disconnect = FALSE;
  1088. wpa_sta_disconnect(sm->wpa_auth, sm->addr);
  1089. }
  1090. SM_STATE(WPA_PTK, DISCONNECTED)
  1091. {
  1092. SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
  1093. sm->DeauthenticationRequest = FALSE;
  1094. }
  1095. SM_STATE(WPA_PTK, AUTHENTICATION)
  1096. {
  1097. SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
  1098. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  1099. sm->PTK_valid = FALSE;
  1100. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
  1101. 1);
  1102. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
  1103. sm->AuthenticationRequest = FALSE;
  1104. }
  1105. SM_STATE(WPA_PTK, AUTHENTICATION2)
  1106. {
  1107. SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
  1108. os_memcpy(sm->ANonce, sm->group->Counter, WPA_NONCE_LEN);
  1109. inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
  1110. sm->ReAuthenticationRequest = FALSE;
  1111. /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
  1112. * logical place than INITIALIZE since AUTHENTICATION2 can be
  1113. * re-entered on ReAuthenticationRequest without going through
  1114. * INITIALIZE. */
  1115. sm->TimeoutCtr = 0;
  1116. }
  1117. SM_STATE(WPA_PTK, INITPMK)
  1118. {
  1119. u8 msk[2 * PMK_LEN];
  1120. size_t len = 2 * PMK_LEN;
  1121. SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
  1122. #ifdef CONFIG_IEEE80211R
  1123. sm->xxkey_len = 0;
  1124. #endif /* CONFIG_IEEE80211R */
  1125. if (sm->pmksa) {
  1126. wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
  1127. os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
  1128. } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
  1129. wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
  1130. "(len=%lu)", (unsigned long) len);
  1131. os_memcpy(sm->PMK, msk, PMK_LEN);
  1132. #ifdef CONFIG_IEEE80211R
  1133. if (len >= 2 * PMK_LEN) {
  1134. os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
  1135. sm->xxkey_len = PMK_LEN;
  1136. }
  1137. #endif /* CONFIG_IEEE80211R */
  1138. } else {
  1139. wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
  1140. }
  1141. sm->req_replay_counter_used = 0;
  1142. /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
  1143. * will break reauthentication since EAPOL state machines may not be
  1144. * get into AUTHENTICATING state that clears keyRun before WPA state
  1145. * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
  1146. * state and takes PMK from the previously used AAA Key. This will
  1147. * eventually fail in 4-Way Handshake because Supplicant uses PMK
  1148. * derived from the new AAA Key. Setting keyRun = FALSE here seems to
  1149. * be good workaround for this issue. */
  1150. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
  1151. }
  1152. SM_STATE(WPA_PTK, INITPSK)
  1153. {
  1154. const u8 *psk;
  1155. SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
  1156. psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL);
  1157. if (psk) {
  1158. os_memcpy(sm->PMK, psk, PMK_LEN);
  1159. #ifdef CONFIG_IEEE80211R
  1160. os_memcpy(sm->xxkey, psk, PMK_LEN);
  1161. sm->xxkey_len = PMK_LEN;
  1162. #endif /* CONFIG_IEEE80211R */
  1163. }
  1164. sm->req_replay_counter_used = 0;
  1165. }
  1166. SM_STATE(WPA_PTK, PTKSTART)
  1167. {
  1168. u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
  1169. size_t pmkid_len = 0;
  1170. SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
  1171. sm->PTKRequest = FALSE;
  1172. sm->TimeoutEvt = FALSE;
  1173. sm->TimeoutCtr++;
  1174. if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
  1175. /* No point in sending the EAPOL-Key - we will disconnect
  1176. * immediately following this. */
  1177. return;
  1178. }
  1179. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1180. "sending 1/4 msg of 4-Way Handshake");
  1181. /*
  1182. * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
  1183. * one possible PSK for this STA.
  1184. */
  1185. if (sm->wpa == WPA_VERSION_WPA2 &&
  1186. wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) {
  1187. pmkid = buf;
  1188. pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
  1189. pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
  1190. pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
  1191. RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
  1192. if (sm->pmksa)
  1193. os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
  1194. sm->pmksa->pmkid, PMKID_LEN);
  1195. else {
  1196. /*
  1197. * Calculate PMKID since no PMKSA cache entry was
  1198. * available with pre-calculated PMKID.
  1199. */
  1200. rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
  1201. sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
  1202. wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
  1203. }
  1204. }
  1205. wpa_send_eapol(sm->wpa_auth, sm,
  1206. WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
  1207. sm->ANonce, pmkid, pmkid_len, 0, 0);
  1208. }
  1209. static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
  1210. struct wpa_ptk *ptk)
  1211. {
  1212. size_t ptk_len = sm->pairwise == WPA_CIPHER_CCMP ? 48 : 64;
  1213. #ifdef CONFIG_IEEE80211R
  1214. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
  1215. return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len);
  1216. #endif /* CONFIG_IEEE80211R */
  1217. wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
  1218. sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
  1219. (u8 *) ptk, ptk_len,
  1220. wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
  1221. return 0;
  1222. }
  1223. SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
  1224. {
  1225. struct wpa_ptk PTK;
  1226. int ok = 0;
  1227. const u8 *pmk = NULL;
  1228. SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
  1229. sm->EAPOLKeyReceived = FALSE;
  1230. /* WPA with IEEE 802.1X: use the derived PMK from EAP
  1231. * WPA-PSK: iterate through possible PSKs and select the one matching
  1232. * the packet */
  1233. for (;;) {
  1234. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1235. pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, pmk);
  1236. if (pmk == NULL)
  1237. break;
  1238. } else
  1239. pmk = sm->PMK;
  1240. wpa_derive_ptk(sm, pmk, &PTK);
  1241. if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
  1242. sm->last_rx_eapol_key_len) == 0) {
  1243. ok = 1;
  1244. break;
  1245. }
  1246. if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
  1247. break;
  1248. }
  1249. if (!ok) {
  1250. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1251. "invalid MIC in msg 2/4 of 4-Way Handshake");
  1252. return;
  1253. }
  1254. eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
  1255. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1256. /* PSK may have changed from the previous choice, so update
  1257. * state machine data based on whatever PSK was selected here.
  1258. */
  1259. os_memcpy(sm->PMK, pmk, PMK_LEN);
  1260. }
  1261. sm->MICVerified = TRUE;
  1262. os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
  1263. sm->PTK_valid = TRUE;
  1264. }
  1265. SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
  1266. {
  1267. SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
  1268. sm->TimeoutCtr = 0;
  1269. }
  1270. #ifdef CONFIG_IEEE80211W
  1271. static int ieee80211w_kde_len(struct wpa_state_machine *sm)
  1272. {
  1273. if (sm->mgmt_frame_prot) {
  1274. return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde);
  1275. }
  1276. return 0;
  1277. }
  1278. static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
  1279. {
  1280. struct wpa_igtk_kde igtk;
  1281. struct wpa_group *gsm = sm->group;
  1282. if (!sm->mgmt_frame_prot)
  1283. return pos;
  1284. igtk.keyid[0] = gsm->GN_igtk;
  1285. igtk.keyid[1] = 0;
  1286. if (wpa_auth_get_seqnum_igtk(sm->wpa_auth, NULL, gsm->GN_igtk, igtk.pn)
  1287. < 0)
  1288. os_memset(igtk.pn, 0, sizeof(igtk.pn));
  1289. os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
  1290. pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
  1291. (const u8 *) &igtk, sizeof(igtk), NULL, 0);
  1292. return pos;
  1293. }
  1294. #else /* CONFIG_IEEE80211W */
  1295. static int ieee80211w_kde_len(struct wpa_state_machine *sm)
  1296. {
  1297. return 0;
  1298. }
  1299. static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
  1300. {
  1301. return pos;
  1302. }
  1303. #endif /* CONFIG_IEEE80211W */
  1304. SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
  1305. {
  1306. u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
  1307. size_t gtk_len, kde_len;
  1308. struct wpa_group *gsm = sm->group;
  1309. u8 *wpa_ie;
  1310. int wpa_ie_len, secure, keyidx, encr = 0;
  1311. SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
  1312. sm->TimeoutEvt = FALSE;
  1313. sm->TimeoutCtr++;
  1314. if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
  1315. /* No point in sending the EAPOL-Key - we will disconnect
  1316. * immediately following this. */
  1317. return;
  1318. }
  1319. /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN])
  1320. */
  1321. os_memset(rsc, 0, WPA_KEY_RSC_LEN);
  1322. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
  1323. wpa_ie = sm->wpa_auth->wpa_ie;
  1324. wpa_ie_len = sm->wpa_auth->wpa_ie_len;
  1325. if (sm->wpa == WPA_VERSION_WPA &&
  1326. (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
  1327. wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
  1328. /* WPA-only STA, remove RSN IE */
  1329. wpa_ie = wpa_ie + wpa_ie[1] + 2;
  1330. wpa_ie_len = wpa_ie[1] + 2;
  1331. }
  1332. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1333. "sending 3/4 msg of 4-Way Handshake");
  1334. if (sm->wpa == WPA_VERSION_WPA2) {
  1335. /* WPA2 send GTK in the 4-way handshake */
  1336. secure = 1;
  1337. gtk = gsm->GTK[gsm->GN - 1];
  1338. gtk_len = gsm->GTK_len;
  1339. keyidx = gsm->GN;
  1340. _rsc = rsc;
  1341. encr = 1;
  1342. } else {
  1343. /* WPA does not include GTK in msg 3/4 */
  1344. secure = 0;
  1345. gtk = NULL;
  1346. gtk_len = 0;
  1347. keyidx = 0;
  1348. _rsc = NULL;
  1349. }
  1350. kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
  1351. if (gtk)
  1352. kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
  1353. kde = os_malloc(kde_len);
  1354. if (kde == NULL)
  1355. return;
  1356. pos = kde;
  1357. os_memcpy(pos, wpa_ie, wpa_ie_len);
  1358. pos += wpa_ie_len;
  1359. if (gtk) {
  1360. u8 hdr[2];
  1361. hdr[0] = keyidx & 0x03;
  1362. hdr[1] = 0;
  1363. pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
  1364. gtk, gtk_len);
  1365. }
  1366. pos = ieee80211w_kde_add(sm, pos);
  1367. wpa_send_eapol(sm->wpa_auth, sm,
  1368. (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
  1369. WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
  1370. WPA_KEY_INFO_KEY_TYPE,
  1371. _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
  1372. os_free(kde);
  1373. }
  1374. SM_STATE(WPA_PTK, PTKINITDONE)
  1375. {
  1376. SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
  1377. sm->EAPOLKeyReceived = FALSE;
  1378. if (sm->Pair) {
  1379. wpa_alg alg;
  1380. int klen;
  1381. if (sm->pairwise == WPA_CIPHER_TKIP) {
  1382. alg = WPA_ALG_TKIP;
  1383. klen = 32;
  1384. } else {
  1385. alg = WPA_ALG_CCMP;
  1386. klen = 16;
  1387. }
  1388. if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
  1389. sm->PTK.tk1, klen)) {
  1390. wpa_sta_disconnect(sm->wpa_auth, sm->addr);
  1391. return;
  1392. }
  1393. /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
  1394. sm->pairwise_set = TRUE;
  1395. if (sm->wpa_auth->conf.wpa_ptk_rekey) {
  1396. eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
  1397. eloop_register_timeout(sm->wpa_auth->conf.
  1398. wpa_ptk_rekey, 0, wpa_rekey_ptk,
  1399. sm->wpa_auth, sm);
  1400. }
  1401. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1402. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1403. WPA_EAPOL_authorized, 1);
  1404. }
  1405. }
  1406. if (0 /* IBSS == TRUE */) {
  1407. sm->keycount++;
  1408. if (sm->keycount == 2) {
  1409. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1410. WPA_EAPOL_portValid, 1);
  1411. }
  1412. } else {
  1413. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
  1414. 1);
  1415. }
  1416. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
  1417. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
  1418. if (sm->wpa == WPA_VERSION_WPA)
  1419. sm->PInitAKeys = TRUE;
  1420. else
  1421. sm->has_GTK = TRUE;
  1422. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1423. "pairwise key handshake completed (%s)",
  1424. sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
  1425. #ifdef CONFIG_IEEE80211R
  1426. wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
  1427. #endif /* CONFIG_IEEE80211R */
  1428. }
  1429. SM_STEP(WPA_PTK)
  1430. {
  1431. struct wpa_authenticator *wpa_auth = sm->wpa_auth;
  1432. if (sm->Init)
  1433. SM_ENTER(WPA_PTK, INITIALIZE);
  1434. else if (sm->Disconnect
  1435. /* || FIX: dot11RSNAConfigSALifetime timeout */)
  1436. SM_ENTER(WPA_PTK, DISCONNECT);
  1437. else if (sm->DeauthenticationRequest)
  1438. SM_ENTER(WPA_PTK, DISCONNECTED);
  1439. else if (sm->AuthenticationRequest)
  1440. SM_ENTER(WPA_PTK, AUTHENTICATION);
  1441. else if (sm->ReAuthenticationRequest)
  1442. SM_ENTER(WPA_PTK, AUTHENTICATION2);
  1443. else if (sm->PTKRequest)
  1444. SM_ENTER(WPA_PTK, PTKSTART);
  1445. else switch (sm->wpa_ptk_state) {
  1446. case WPA_PTK_INITIALIZE:
  1447. break;
  1448. case WPA_PTK_DISCONNECT:
  1449. SM_ENTER(WPA_PTK, DISCONNECTED);
  1450. break;
  1451. case WPA_PTK_DISCONNECTED:
  1452. SM_ENTER(WPA_PTK, INITIALIZE);
  1453. break;
  1454. case WPA_PTK_AUTHENTICATION:
  1455. SM_ENTER(WPA_PTK, AUTHENTICATION2);
  1456. break;
  1457. case WPA_PTK_AUTHENTICATION2:
  1458. if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
  1459. wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
  1460. WPA_EAPOL_keyRun) > 0)
  1461. SM_ENTER(WPA_PTK, INITPMK);
  1462. else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
  1463. /* FIX: && 802.1X::keyRun */)
  1464. SM_ENTER(WPA_PTK, INITPSK);
  1465. break;
  1466. case WPA_PTK_INITPMK:
  1467. if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
  1468. WPA_EAPOL_keyAvailable) > 0)
  1469. SM_ENTER(WPA_PTK, PTKSTART);
  1470. else {
  1471. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1472. SM_ENTER(WPA_PTK, DISCONNECT);
  1473. }
  1474. break;
  1475. case WPA_PTK_INITPSK:
  1476. if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL))
  1477. SM_ENTER(WPA_PTK, PTKSTART);
  1478. else {
  1479. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1480. "no PSK configured for the STA");
  1481. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1482. SM_ENTER(WPA_PTK, DISCONNECT);
  1483. }
  1484. break;
  1485. case WPA_PTK_PTKSTART:
  1486. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1487. sm->EAPOLKeyPairwise)
  1488. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
  1489. else if (sm->TimeoutCtr >
  1490. (int) dot11RSNAConfigPairwiseUpdateCount) {
  1491. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1492. SM_ENTER(WPA_PTK, DISCONNECT);
  1493. } else if (sm->TimeoutEvt)
  1494. SM_ENTER(WPA_PTK, PTKSTART);
  1495. break;
  1496. case WPA_PTK_PTKCALCNEGOTIATING:
  1497. if (sm->MICVerified)
  1498. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
  1499. else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1500. sm->EAPOLKeyPairwise)
  1501. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
  1502. else if (sm->TimeoutEvt)
  1503. SM_ENTER(WPA_PTK, PTKSTART);
  1504. break;
  1505. case WPA_PTK_PTKCALCNEGOTIATING2:
  1506. SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
  1507. break;
  1508. case WPA_PTK_PTKINITNEGOTIATING:
  1509. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1510. sm->EAPOLKeyPairwise && sm->MICVerified)
  1511. SM_ENTER(WPA_PTK, PTKINITDONE);
  1512. else if (sm->TimeoutCtr >
  1513. (int) dot11RSNAConfigPairwiseUpdateCount) {
  1514. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1515. SM_ENTER(WPA_PTK, DISCONNECT);
  1516. } else if (sm->TimeoutEvt)
  1517. SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
  1518. break;
  1519. case WPA_PTK_PTKINITDONE:
  1520. break;
  1521. }
  1522. }
  1523. SM_STATE(WPA_PTK_GROUP, IDLE)
  1524. {
  1525. SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
  1526. if (sm->Init) {
  1527. /* Init flag is not cleared here, so avoid busy
  1528. * loop by claiming nothing changed. */
  1529. sm->changed = FALSE;
  1530. }
  1531. sm->GTimeoutCtr = 0;
  1532. }
  1533. SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
  1534. {
  1535. u8 rsc[WPA_KEY_RSC_LEN];
  1536. struct wpa_group *gsm = sm->group;
  1537. u8 *kde, *pos, hdr[2];
  1538. size_t kde_len;
  1539. SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
  1540. sm->GTimeoutCtr++;
  1541. if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
  1542. /* No point in sending the EAPOL-Key - we will disconnect
  1543. * immediately following this. */
  1544. return;
  1545. }
  1546. if (sm->wpa == WPA_VERSION_WPA)
  1547. sm->PInitAKeys = FALSE;
  1548. sm->TimeoutEvt = FALSE;
  1549. /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
  1550. os_memset(rsc, 0, WPA_KEY_RSC_LEN);
  1551. if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
  1552. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
  1553. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1554. "sending 1/2 msg of Group Key Handshake");
  1555. if (sm->wpa == WPA_VERSION_WPA2) {
  1556. kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
  1557. ieee80211w_kde_len(sm);
  1558. kde = os_malloc(kde_len);
  1559. if (kde == NULL)
  1560. return;
  1561. pos = kde;
  1562. hdr[0] = gsm->GN & 0x03;
  1563. hdr[1] = 0;
  1564. pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
  1565. gsm->GTK[gsm->GN - 1], gsm->GTK_len);
  1566. pos = ieee80211w_kde_add(sm, pos);
  1567. } else {
  1568. kde = gsm->GTK[gsm->GN - 1];
  1569. pos = kde + gsm->GTK_len;
  1570. }
  1571. wpa_send_eapol(sm->wpa_auth, sm,
  1572. WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
  1573. WPA_KEY_INFO_ACK |
  1574. (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
  1575. rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1);
  1576. if (sm->wpa == WPA_VERSION_WPA2)
  1577. os_free(kde);
  1578. }
  1579. SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
  1580. {
  1581. SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
  1582. sm->EAPOLKeyReceived = FALSE;
  1583. if (sm->GUpdateStationKeys)
  1584. sm->group->GKeyDoneStations--;
  1585. sm->GUpdateStationKeys = FALSE;
  1586. sm->GTimeoutCtr = 0;
  1587. /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
  1588. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1589. "group key handshake completed (%s)",
  1590. sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
  1591. sm->has_GTK = TRUE;
  1592. }
  1593. SM_STATE(WPA_PTK_GROUP, KEYERROR)
  1594. {
  1595. SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
  1596. if (sm->GUpdateStationKeys)
  1597. sm->group->GKeyDoneStations--;
  1598. sm->GUpdateStationKeys = FALSE;
  1599. sm->Disconnect = TRUE;
  1600. }
  1601. SM_STEP(WPA_PTK_GROUP)
  1602. {
  1603. if (sm->Init || sm->PtkGroupInit) {
  1604. SM_ENTER(WPA_PTK_GROUP, IDLE);
  1605. sm->PtkGroupInit = FALSE;
  1606. } else switch (sm->wpa_ptk_group_state) {
  1607. case WPA_PTK_GROUP_IDLE:
  1608. if (sm->GUpdateStationKeys ||
  1609. (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
  1610. SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
  1611. break;
  1612. case WPA_PTK_GROUP_REKEYNEGOTIATING:
  1613. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1614. !sm->EAPOLKeyPairwise && sm->MICVerified)
  1615. SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
  1616. else if (sm->GTimeoutCtr >
  1617. (int) dot11RSNAConfigGroupUpdateCount)
  1618. SM_ENTER(WPA_PTK_GROUP, KEYERROR);
  1619. else if (sm->TimeoutEvt)
  1620. SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
  1621. break;
  1622. case WPA_PTK_GROUP_KEYERROR:
  1623. SM_ENTER(WPA_PTK_GROUP, IDLE);
  1624. break;
  1625. case WPA_PTK_GROUP_REKEYESTABLISHED:
  1626. SM_ENTER(WPA_PTK_GROUP, IDLE);
  1627. break;
  1628. }
  1629. }
  1630. static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
  1631. struct wpa_group *group)
  1632. {
  1633. int ret = 0;
  1634. /* FIX: is this the correct way of getting GNonce? */
  1635. os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
  1636. inc_byte_array(group->Counter, WPA_NONCE_LEN);
  1637. wpa_gmk_to_gtk(group->GMK, wpa_auth->addr, group->GNonce,
  1638. group->GTK[group->GN - 1], group->GTK_len);
  1639. #ifdef CONFIG_IEEE80211W
  1640. if (wpa_auth->conf.ieee80211w != WPA_NO_IEEE80211W) {
  1641. if (os_get_random(group->IGTK[group->GN_igtk - 4],
  1642. WPA_IGTK_LEN) < 0) {
  1643. wpa_printf(MSG_INFO, "RSN: Failed to get new random "
  1644. "IGTK");
  1645. ret = -1;
  1646. }
  1647. wpa_hexdump_key(MSG_DEBUG, "IGTK",
  1648. group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN);
  1649. }
  1650. #endif /* CONFIG_IEEE80211W */
  1651. return ret;
  1652. }
  1653. static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
  1654. struct wpa_group *group)
  1655. {
  1656. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  1657. "GTK_INIT (VLAN-ID %d)", group->vlan_id);
  1658. group->changed = FALSE; /* GInit is not cleared here; avoid loop */
  1659. group->wpa_group_state = WPA_GROUP_GTK_INIT;
  1660. /* GTK[0..N] = 0 */
  1661. os_memset(group->GTK, 0, sizeof(group->GTK));
  1662. group->GN = 1;
  1663. group->GM = 2;
  1664. #ifdef CONFIG_IEEE80211W
  1665. group->GN_igtk = 4;
  1666. group->GM_igtk = 5;
  1667. #endif /* CONFIG_IEEE80211W */
  1668. /* GTK[GN] = CalcGTK() */
  1669. wpa_gtk_update(wpa_auth, group);
  1670. }
  1671. static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
  1672. {
  1673. if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
  1674. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1675. "Not in PTKINITDONE; skip Group Key update");
  1676. return 0;
  1677. }
  1678. if (sm->GUpdateStationKeys) {
  1679. /*
  1680. * This should not really happen, but just in case, make sure
  1681. * we do not count the same STA twice in GKeyDoneStations.
  1682. */
  1683. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1684. "GUpdateStationKeys already set - do not "
  1685. "increment GKeyDoneStations");
  1686. } else {
  1687. sm->group->GKeyDoneStations++;
  1688. sm->GUpdateStationKeys = TRUE;
  1689. }
  1690. wpa_sm_step(sm);
  1691. return 0;
  1692. }
  1693. static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
  1694. struct wpa_group *group)
  1695. {
  1696. int tmp;
  1697. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  1698. "SETKEYS (VLAN-ID %d)", group->vlan_id);
  1699. group->changed = TRUE;
  1700. group->wpa_group_state = WPA_GROUP_SETKEYS;
  1701. group->GTKReKey = FALSE;
  1702. tmp = group->GM;
  1703. group->GM = group->GN;
  1704. group->GN = tmp;
  1705. #ifdef CONFIG_IEEE80211W
  1706. tmp = group->GM_igtk;
  1707. group->GM_igtk = group->GN_igtk;
  1708. group->GN_igtk = tmp;
  1709. #endif /* CONFIG_IEEE80211W */
  1710. /* "GKeyDoneStations = GNoStations" is done in more robust way by
  1711. * counting the STAs that are marked with GUpdateStationKeys instead of
  1712. * including all STAs that could be in not-yet-completed state. */
  1713. wpa_gtk_update(wpa_auth, group);
  1714. wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, NULL);
  1715. wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
  1716. group->GKeyDoneStations);
  1717. }
  1718. static void wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
  1719. struct wpa_group *group)
  1720. {
  1721. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  1722. "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
  1723. group->changed = TRUE;
  1724. group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
  1725. wpa_auth_set_key(wpa_auth, group->vlan_id,
  1726. wpa_alg_enum(wpa_auth->conf.wpa_group),
  1727. NULL, group->GN, group->GTK[group->GN - 1],
  1728. group->GTK_len);
  1729. #ifdef CONFIG_IEEE80211W
  1730. if (wpa_auth->conf.ieee80211w != WPA_NO_IEEE80211W) {
  1731. wpa_auth_set_key(wpa_auth, group->vlan_id, WPA_ALG_IGTK,
  1732. NULL, group->GN_igtk,
  1733. group->IGTK[group->GN_igtk - 4],
  1734. WPA_IGTK_LEN);
  1735. }
  1736. #endif /* CONFIG_IEEE80211W */
  1737. }
  1738. static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
  1739. struct wpa_group *group)
  1740. {
  1741. if (group->GInit) {
  1742. wpa_group_gtk_init(wpa_auth, group);
  1743. } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
  1744. group->GTKAuthenticator) {
  1745. wpa_group_setkeysdone(wpa_auth, group);
  1746. } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
  1747. group->GTKReKey) {
  1748. wpa_group_setkeys(wpa_auth, group);
  1749. } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
  1750. if (group->GKeyDoneStations == 0)
  1751. wpa_group_setkeysdone(wpa_auth, group);
  1752. else if (group->GTKReKey)
  1753. wpa_group_setkeys(wpa_auth, group);
  1754. }
  1755. }
  1756. static void wpa_sm_step(struct wpa_state_machine *sm)
  1757. {
  1758. if (sm == NULL)
  1759. return;
  1760. if (sm->in_step_loop) {
  1761. /* This should not happen, but if it does, make sure we do not
  1762. * end up freeing the state machine too early by exiting the
  1763. * recursive call. */
  1764. wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
  1765. return;
  1766. }
  1767. sm->in_step_loop = 1;
  1768. do {
  1769. if (sm->pending_deinit)
  1770. break;
  1771. sm->changed = FALSE;
  1772. sm->wpa_auth->group->changed = FALSE;
  1773. SM_STEP_RUN(WPA_PTK);
  1774. if (sm->pending_deinit)
  1775. break;
  1776. SM_STEP_RUN(WPA_PTK_GROUP);
  1777. if (sm->pending_deinit)
  1778. break;
  1779. wpa_group_sm_step(sm->wpa_auth, sm->group);
  1780. } while (sm->changed || sm->wpa_auth->group->changed);
  1781. sm->in_step_loop = 0;
  1782. if (sm->pending_deinit) {
  1783. wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
  1784. "machine deinit for " MACSTR, MAC2STR(sm->addr));
  1785. wpa_free_sta_sm(sm);
  1786. }
  1787. }
  1788. static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
  1789. {
  1790. struct wpa_state_machine *sm = eloop_ctx;
  1791. wpa_sm_step(sm);
  1792. }
  1793. void wpa_auth_sm_notify(struct wpa_state_machine *sm)
  1794. {
  1795. if (sm == NULL)
  1796. return;
  1797. eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
  1798. }
  1799. void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
  1800. {
  1801. int tmp, i;
  1802. struct wpa_group *group;
  1803. if (wpa_auth == NULL)
  1804. return;
  1805. group = wpa_auth->group;
  1806. for (i = 0; i < 2; i++) {
  1807. tmp = group->GM;
  1808. group->GM = group->GN;
  1809. group->GN = tmp;
  1810. #ifdef CONFIG_IEEE80211W
  1811. tmp = group->GM_igtk;
  1812. group->GM_igtk = group->GN_igtk;
  1813. group->GN_igtk = tmp;
  1814. #endif /* CONFIG_IEEE80211W */
  1815. wpa_gtk_update(wpa_auth, group);
  1816. }
  1817. }
  1818. static const char * wpa_bool_txt(int bool)
  1819. {
  1820. return bool ? "TRUE" : "FALSE";
  1821. }
  1822. static int wpa_cipher_bits(int cipher)
  1823. {
  1824. switch (cipher) {
  1825. case WPA_CIPHER_CCMP:
  1826. return 128;
  1827. case WPA_CIPHER_TKIP:
  1828. return 256;
  1829. case WPA_CIPHER_WEP104:
  1830. return 104;
  1831. case WPA_CIPHER_WEP40:
  1832. return 40;
  1833. default:
  1834. return 0;
  1835. }
  1836. }
  1837. #define RSN_SUITE "%02x-%02x-%02x-%d"
  1838. #define RSN_SUITE_ARG(s) \
  1839. ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
  1840. int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
  1841. {
  1842. int len = 0, ret;
  1843. char pmkid_txt[PMKID_LEN * 2 + 1];
  1844. if (wpa_auth == NULL)
  1845. return len;
  1846. ret = os_snprintf(buf + len, buflen - len,
  1847. "dot11RSNAOptionImplemented=TRUE\n"
  1848. #ifdef CONFIG_RSN_PREAUTH
  1849. "dot11RSNAPreauthenticationImplemented=TRUE\n"
  1850. #else /* CONFIG_RSN_PREAUTH */
  1851. "dot11RSNAPreauthenticationImplemented=FALSE\n"
  1852. #endif /* CONFIG_RSN_PREAUTH */
  1853. "dot11RSNAEnabled=%s\n"
  1854. "dot11RSNAPreauthenticationEnabled=%s\n",
  1855. wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
  1856. wpa_bool_txt(wpa_auth->conf.rsn_preauth));
  1857. if (ret < 0 || (size_t) ret >= buflen - len)
  1858. return len;
  1859. len += ret;
  1860. wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
  1861. wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
  1862. ret = os_snprintf(
  1863. buf + len, buflen - len,
  1864. "dot11RSNAConfigVersion=%u\n"
  1865. "dot11RSNAConfigPairwiseKeysSupported=9999\n"
  1866. /* FIX: dot11RSNAConfigGroupCipher */
  1867. /* FIX: dot11RSNAConfigGroupRekeyMethod */
  1868. /* FIX: dot11RSNAConfigGroupRekeyTime */
  1869. /* FIX: dot11RSNAConfigGroupRekeyPackets */
  1870. "dot11RSNAConfigGroupRekeyStrict=%u\n"
  1871. "dot11RSNAConfigGroupUpdateCount=%u\n"
  1872. "dot11RSNAConfigPairwiseUpdateCount=%u\n"
  1873. "dot11RSNAConfigGroupCipherSize=%u\n"
  1874. "dot11RSNAConfigPMKLifetime=%u\n"
  1875. "dot11RSNAConfigPMKReauthThreshold=%u\n"
  1876. "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
  1877. "dot11RSNAConfigSATimeout=%u\n"
  1878. "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
  1879. "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
  1880. "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
  1881. "dot11RSNAPMKIDUsed=%s\n"
  1882. "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
  1883. "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
  1884. "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
  1885. "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
  1886. "dot11RSNA4WayHandshakeFailures=%u\n"
  1887. "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
  1888. RSN_VERSION,
  1889. !!wpa_auth->conf.wpa_strict_rekey,
  1890. dot11RSNAConfigGroupUpdateCount,
  1891. dot11RSNAConfigPairwiseUpdateCount,
  1892. wpa_cipher_bits(wpa_auth->conf.wpa_group),
  1893. dot11RSNAConfigPMKLifetime,
  1894. dot11RSNAConfigPMKReauthThreshold,
  1895. dot11RSNAConfigSATimeout,
  1896. RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
  1897. RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
  1898. RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
  1899. pmkid_txt,
  1900. RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
  1901. RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
  1902. RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
  1903. wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
  1904. wpa_auth->dot11RSNA4WayHandshakeFailures);
  1905. if (ret < 0 || (size_t) ret >= buflen - len)
  1906. return len;
  1907. len += ret;
  1908. /* TODO: dot11RSNAConfigPairwiseCiphersTable */
  1909. /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
  1910. /* Private MIB */
  1911. ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
  1912. wpa_auth->group->wpa_group_state);
  1913. if (ret < 0 || (size_t) ret >= buflen - len)
  1914. return len;
  1915. len += ret;
  1916. return len;
  1917. }
  1918. int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
  1919. {
  1920. int len = 0, ret;
  1921. u32 pairwise = 0;
  1922. if (sm == NULL)
  1923. return 0;
  1924. /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
  1925. /* dot11RSNAStatsEntry */
  1926. if (sm->wpa == WPA_VERSION_WPA) {
  1927. if (sm->pairwise == WPA_CIPHER_CCMP)
  1928. pairwise = WPA_CIPHER_SUITE_CCMP;
  1929. else if (sm->pairwise == WPA_CIPHER_TKIP)
  1930. pairwise = WPA_CIPHER_SUITE_TKIP;
  1931. else if (sm->pairwise == WPA_CIPHER_WEP104)
  1932. pairwise = WPA_CIPHER_SUITE_WEP104;
  1933. else if (sm->pairwise == WPA_CIPHER_WEP40)
  1934. pairwise = WPA_CIPHER_SUITE_WEP40;
  1935. else if (sm->pairwise == WPA_CIPHER_NONE)
  1936. pairwise = WPA_CIPHER_SUITE_NONE;
  1937. } else if (sm->wpa == WPA_VERSION_WPA2) {
  1938. if (sm->pairwise == WPA_CIPHER_CCMP)
  1939. pairwise = RSN_CIPHER_SUITE_CCMP;
  1940. else if (sm->pairwise == WPA_CIPHER_TKIP)
  1941. pairwise = RSN_CIPHER_SUITE_TKIP;
  1942. else if (sm->pairwise == WPA_CIPHER_WEP104)
  1943. pairwise = RSN_CIPHER_SUITE_WEP104;
  1944. else if (sm->pairwise == WPA_CIPHER_WEP40)
  1945. pairwise = RSN_CIPHER_SUITE_WEP40;
  1946. else if (sm->pairwise == WPA_CIPHER_NONE)
  1947. pairwise = RSN_CIPHER_SUITE_NONE;
  1948. } else
  1949. return 0;
  1950. ret = os_snprintf(
  1951. buf + len, buflen - len,
  1952. /* TODO: dot11RSNAStatsIndex */
  1953. "dot11RSNAStatsSTAAddress=" MACSTR "\n"
  1954. "dot11RSNAStatsVersion=1\n"
  1955. "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
  1956. /* TODO: dot11RSNAStatsTKIPICVErrors */
  1957. "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
  1958. "dot11RSNAStatsTKIPRemoveMICFailures=%u\n"
  1959. /* TODO: dot11RSNAStatsCCMPReplays */
  1960. /* TODO: dot11RSNAStatsCCMPDecryptErrors */
  1961. /* TODO: dot11RSNAStatsTKIPReplays */,
  1962. MAC2STR(sm->addr),
  1963. RSN_SUITE_ARG(pairwise),
  1964. sm->dot11RSNAStatsTKIPLocalMICFailures,
  1965. sm->dot11RSNAStatsTKIPRemoteMICFailures);
  1966. if (ret < 0 || (size_t) ret >= buflen - len)
  1967. return len;
  1968. len += ret;
  1969. /* Private MIB */
  1970. ret = os_snprintf(buf + len, buflen - len,
  1971. "hostapdWPAPTKState=%d\n"
  1972. "hostapdWPAPTKGroupState=%d\n",
  1973. sm->wpa_ptk_state,
  1974. sm->wpa_ptk_group_state);
  1975. if (ret < 0 || (size_t) ret >= buflen - len)
  1976. return len;
  1977. len += ret;
  1978. return len;
  1979. }
  1980. void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
  1981. {
  1982. if (wpa_auth)
  1983. wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
  1984. }
  1985. int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
  1986. {
  1987. return sm && sm->pairwise_set;
  1988. }
  1989. int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
  1990. {
  1991. return sm->pairwise;
  1992. }
  1993. int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
  1994. {
  1995. if (sm == NULL)
  1996. return -1;
  1997. return sm->wpa_key_mgmt;
  1998. }
  1999. int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
  2000. {
  2001. if (sm == NULL)
  2002. return 0;
  2003. return sm->wpa;
  2004. }
  2005. int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
  2006. struct rsn_pmksa_cache_entry *entry)
  2007. {
  2008. if (sm == NULL || sm->pmksa != entry)
  2009. return -1;
  2010. sm->pmksa = NULL;
  2011. return 0;
  2012. }
  2013. struct rsn_pmksa_cache_entry *
  2014. wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
  2015. {
  2016. return sm ? sm->pmksa : NULL;
  2017. }
  2018. void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
  2019. {
  2020. if (sm)
  2021. sm->dot11RSNAStatsTKIPLocalMICFailures++;
  2022. }
  2023. const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
  2024. {
  2025. if (wpa_auth == NULL)
  2026. return NULL;
  2027. *len = wpa_auth->wpa_ie_len;
  2028. return wpa_auth->wpa_ie;
  2029. }
  2030. int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
  2031. int session_timeout, struct eapol_state_machine *eapol)
  2032. {
  2033. if (sm == NULL || sm->wpa != WPA_VERSION_WPA2)
  2034. return -1;
  2035. if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
  2036. sm->wpa_auth->addr, sm->addr, session_timeout,
  2037. eapol, sm->wpa_key_mgmt))
  2038. return 0;
  2039. return -1;
  2040. }
  2041. int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
  2042. const u8 *pmk, size_t len, const u8 *sta_addr,
  2043. int session_timeout,
  2044. struct eapol_state_machine *eapol)
  2045. {
  2046. if (wpa_auth == NULL)
  2047. return -1;
  2048. if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr,
  2049. sta_addr, session_timeout, eapol,
  2050. WPA_KEY_MGMT_IEEE8021X))
  2051. return 0;
  2052. return -1;
  2053. }
  2054. static struct wpa_group *
  2055. wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
  2056. {
  2057. struct wpa_group *group;
  2058. if (wpa_auth == NULL || wpa_auth->group == NULL)
  2059. return NULL;
  2060. wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
  2061. vlan_id);
  2062. group = wpa_group_init(wpa_auth, vlan_id);
  2063. if (group == NULL)
  2064. return NULL;
  2065. group->next = wpa_auth->group->next;
  2066. wpa_auth->group->next = group;
  2067. return group;
  2068. }
  2069. int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
  2070. {
  2071. struct wpa_group *group;
  2072. if (sm == NULL || sm->wpa_auth == NULL)
  2073. return 0;
  2074. group = sm->wpa_auth->group;
  2075. while (group) {
  2076. if (group->vlan_id == vlan_id)
  2077. break;
  2078. group = group->next;
  2079. }
  2080. if (group == NULL) {
  2081. group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
  2082. if (group == NULL)
  2083. return -1;
  2084. }
  2085. if (sm->group == group)
  2086. return 0;
  2087. wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
  2088. "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
  2089. sm->group = group;
  2090. return 0;
  2091. }
  2092. #endif /* CONFIG_NATIVE_WINDOWS */