wpa_auth.c 87 KB

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