You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wpa_auth.c 75KB

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