wpa_auth.c 90 KB

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