wpa_auth.c 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410
  1. /*
  2. * IEEE 802.11 RSN / WPA Authenticator
  3. * Copyright (c) 2004-2015, 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.h"
  15. #include "crypto/aes_wrap.h"
  16. #include "crypto/aes_siv.h"
  17. #include "crypto/crypto.h"
  18. #include "crypto/sha1.h"
  19. #include "crypto/sha256.h"
  20. #include "crypto/random.h"
  21. #include "eapol_auth/eapol_auth_sm.h"
  22. #include "ap_config.h"
  23. #include "ieee802_11.h"
  24. #include "wpa_auth.h"
  25. #include "pmksa_cache_auth.h"
  26. #include "wpa_auth_i.h"
  27. #include "wpa_auth_ie.h"
  28. #define STATE_MACHINE_DATA struct wpa_state_machine
  29. #define STATE_MACHINE_DEBUG_PREFIX "WPA"
  30. #define STATE_MACHINE_ADDR sm->addr
  31. static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
  32. static int wpa_sm_step(struct wpa_state_machine *sm);
  33. static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
  34. size_t data_len);
  35. #ifdef CONFIG_FILS
  36. static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
  37. u8 *buf, size_t buf_len, u16 *_key_data_len);
  38. static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm,
  39. const struct wpabuf *hlp);
  40. #endif /* CONFIG_FILS */
  41. static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
  42. static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
  43. struct wpa_group *group);
  44. static void wpa_request_new_ptk(struct wpa_state_machine *sm);
  45. static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
  46. struct wpa_group *group);
  47. static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
  48. struct wpa_group *group);
  49. static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
  50. const u8 *pmk, unsigned int pmk_len,
  51. struct wpa_ptk *ptk);
  52. static void wpa_group_free(struct wpa_authenticator *wpa_auth,
  53. struct wpa_group *group);
  54. static void wpa_group_get(struct wpa_authenticator *wpa_auth,
  55. struct wpa_group *group);
  56. static void wpa_group_put(struct wpa_authenticator *wpa_auth,
  57. struct wpa_group *group);
  58. static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos);
  59. static const u32 eapol_key_timeout_first = 100; /* ms */
  60. static const u32 eapol_key_timeout_subseq = 1000; /* ms */
  61. static const u32 eapol_key_timeout_first_group = 500; /* ms */
  62. /* TODO: make these configurable */
  63. static const int dot11RSNAConfigPMKLifetime = 43200;
  64. static const int dot11RSNAConfigPMKReauthThreshold = 70;
  65. static const int dot11RSNAConfigSATimeout = 60;
  66. static inline int wpa_auth_mic_failure_report(
  67. struct wpa_authenticator *wpa_auth, const u8 *addr)
  68. {
  69. if (wpa_auth->cb->mic_failure_report)
  70. return wpa_auth->cb->mic_failure_report(wpa_auth->cb_ctx, addr);
  71. return 0;
  72. }
  73. static inline void wpa_auth_psk_failure_report(
  74. struct wpa_authenticator *wpa_auth, const u8 *addr)
  75. {
  76. if (wpa_auth->cb->psk_failure_report)
  77. wpa_auth->cb->psk_failure_report(wpa_auth->cb_ctx, addr);
  78. }
  79. static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
  80. const u8 *addr, wpa_eapol_variable var,
  81. int value)
  82. {
  83. if (wpa_auth->cb->set_eapol)
  84. wpa_auth->cb->set_eapol(wpa_auth->cb_ctx, addr, var, value);
  85. }
  86. static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
  87. const u8 *addr, wpa_eapol_variable var)
  88. {
  89. if (wpa_auth->cb->get_eapol == NULL)
  90. return -1;
  91. return wpa_auth->cb->get_eapol(wpa_auth->cb_ctx, addr, var);
  92. }
  93. static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
  94. const u8 *addr,
  95. const u8 *p2p_dev_addr,
  96. const u8 *prev_psk)
  97. {
  98. if (wpa_auth->cb->get_psk == NULL)
  99. return NULL;
  100. return wpa_auth->cb->get_psk(wpa_auth->cb_ctx, addr, p2p_dev_addr,
  101. prev_psk);
  102. }
  103. static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
  104. const u8 *addr, u8 *msk, size_t *len)
  105. {
  106. if (wpa_auth->cb->get_msk == NULL)
  107. return -1;
  108. return wpa_auth->cb->get_msk(wpa_auth->cb_ctx, addr, msk, len);
  109. }
  110. static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
  111. int vlan_id,
  112. enum wpa_alg alg, const u8 *addr, int idx,
  113. u8 *key, size_t key_len)
  114. {
  115. if (wpa_auth->cb->set_key == NULL)
  116. return -1;
  117. return wpa_auth->cb->set_key(wpa_auth->cb_ctx, vlan_id, alg, addr, idx,
  118. key, key_len);
  119. }
  120. static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
  121. const u8 *addr, int idx, u8 *seq)
  122. {
  123. if (wpa_auth->cb->get_seqnum == NULL)
  124. return -1;
  125. return wpa_auth->cb->get_seqnum(wpa_auth->cb_ctx, addr, idx, seq);
  126. }
  127. static inline int
  128. wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
  129. const u8 *data, size_t data_len, int encrypt)
  130. {
  131. if (wpa_auth->cb->send_eapol == NULL)
  132. return -1;
  133. return wpa_auth->cb->send_eapol(wpa_auth->cb_ctx, addr, data, data_len,
  134. encrypt);
  135. }
  136. #ifdef CONFIG_MESH
  137. static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth,
  138. const u8 *addr)
  139. {
  140. if (wpa_auth->cb->start_ampe == NULL)
  141. return -1;
  142. return wpa_auth->cb->start_ampe(wpa_auth->cb_ctx, addr);
  143. }
  144. #endif /* CONFIG_MESH */
  145. int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
  146. int (*cb)(struct wpa_state_machine *sm, void *ctx),
  147. void *cb_ctx)
  148. {
  149. if (wpa_auth->cb->for_each_sta == NULL)
  150. return 0;
  151. return wpa_auth->cb->for_each_sta(wpa_auth->cb_ctx, cb, cb_ctx);
  152. }
  153. int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
  154. int (*cb)(struct wpa_authenticator *a, void *ctx),
  155. void *cb_ctx)
  156. {
  157. if (wpa_auth->cb->for_each_auth == NULL)
  158. return 0;
  159. return wpa_auth->cb->for_each_auth(wpa_auth->cb_ctx, cb, cb_ctx);
  160. }
  161. void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
  162. logger_level level, const char *txt)
  163. {
  164. if (wpa_auth->cb->logger == NULL)
  165. return;
  166. wpa_auth->cb->logger(wpa_auth->cb_ctx, addr, level, txt);
  167. }
  168. void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
  169. logger_level level, const char *fmt, ...)
  170. {
  171. char *format;
  172. int maxlen;
  173. va_list ap;
  174. if (wpa_auth->cb->logger == NULL)
  175. return;
  176. maxlen = os_strlen(fmt) + 100;
  177. format = os_malloc(maxlen);
  178. if (!format)
  179. return;
  180. va_start(ap, fmt);
  181. vsnprintf(format, maxlen, fmt, ap);
  182. va_end(ap);
  183. wpa_auth_logger(wpa_auth, addr, level, format);
  184. os_free(format);
  185. }
  186. static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
  187. const u8 *addr)
  188. {
  189. if (wpa_auth->cb->disconnect == NULL)
  190. return;
  191. wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR, MAC2STR(addr));
  192. wpa_auth->cb->disconnect(wpa_auth->cb_ctx, addr,
  193. WLAN_REASON_PREV_AUTH_NOT_VALID);
  194. }
  195. static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
  196. {
  197. int ret = 0;
  198. #ifdef CONFIG_IEEE80211R_AP
  199. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
  200. ret = 1;
  201. #endif /* CONFIG_IEEE80211R_AP */
  202. #ifdef CONFIG_IEEE80211W
  203. if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
  204. ret = 1;
  205. #endif /* CONFIG_IEEE80211W */
  206. if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN)
  207. ret = 1;
  208. return ret;
  209. }
  210. static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
  211. {
  212. struct wpa_authenticator *wpa_auth = eloop_ctx;
  213. if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
  214. wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
  215. "initialization.");
  216. } else {
  217. wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
  218. wpa_hexdump_key(MSG_DEBUG, "GMK",
  219. wpa_auth->group->GMK, WPA_GMK_LEN);
  220. }
  221. if (wpa_auth->conf.wpa_gmk_rekey) {
  222. eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
  223. wpa_rekey_gmk, wpa_auth, NULL);
  224. }
  225. }
  226. static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
  227. {
  228. struct wpa_authenticator *wpa_auth = eloop_ctx;
  229. struct wpa_group *group, *next;
  230. wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
  231. group = wpa_auth->group;
  232. while (group) {
  233. wpa_group_get(wpa_auth, group);
  234. group->GTKReKey = TRUE;
  235. do {
  236. group->changed = FALSE;
  237. wpa_group_sm_step(wpa_auth, group);
  238. } while (group->changed);
  239. next = group->next;
  240. wpa_group_put(wpa_auth, group);
  241. group = next;
  242. }
  243. if (wpa_auth->conf.wpa_group_rekey) {
  244. eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
  245. 0, wpa_rekey_gtk, wpa_auth, NULL);
  246. }
  247. }
  248. static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
  249. {
  250. struct wpa_authenticator *wpa_auth = eloop_ctx;
  251. struct wpa_state_machine *sm = timeout_ctx;
  252. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
  253. wpa_request_new_ptk(sm);
  254. wpa_sm_step(sm);
  255. }
  256. static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
  257. {
  258. if (sm->pmksa == ctx)
  259. sm->pmksa = NULL;
  260. return 0;
  261. }
  262. static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
  263. void *ctx)
  264. {
  265. struct wpa_authenticator *wpa_auth = ctx;
  266. wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
  267. }
  268. static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
  269. struct wpa_group *group)
  270. {
  271. u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
  272. u8 rkey[32];
  273. unsigned long ptr;
  274. if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
  275. return -1;
  276. wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
  277. /*
  278. * Counter = PRF-256(Random number, "Init Counter",
  279. * Local MAC Address || Time)
  280. */
  281. os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
  282. wpa_get_ntp_timestamp(buf + ETH_ALEN);
  283. ptr = (unsigned long) group;
  284. os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
  285. if (random_get_bytes(rkey, sizeof(rkey)) < 0)
  286. return -1;
  287. if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
  288. group->Counter, WPA_NONCE_LEN) < 0)
  289. return -1;
  290. wpa_hexdump_key(MSG_DEBUG, "Key Counter",
  291. group->Counter, WPA_NONCE_LEN);
  292. return 0;
  293. }
  294. static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
  295. int vlan_id, int delay_init)
  296. {
  297. struct wpa_group *group;
  298. group = os_zalloc(sizeof(struct wpa_group));
  299. if (group == NULL)
  300. return NULL;
  301. group->GTKAuthenticator = TRUE;
  302. group->vlan_id = vlan_id;
  303. group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
  304. if (random_pool_ready() != 1) {
  305. wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
  306. "for secure operations - update keys later when "
  307. "the first station connects");
  308. }
  309. /*
  310. * Set initial GMK/Counter value here. The actual values that will be
  311. * used in negotiations will be set once the first station tries to
  312. * connect. This allows more time for collecting additional randomness
  313. * on embedded devices.
  314. */
  315. if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
  316. wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
  317. "initialization.");
  318. os_free(group);
  319. return NULL;
  320. }
  321. group->GInit = TRUE;
  322. if (delay_init) {
  323. wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
  324. "until Beacon frames have been configured");
  325. /* Initialization is completed in wpa_init_keys(). */
  326. } else {
  327. wpa_group_sm_step(wpa_auth, group);
  328. group->GInit = FALSE;
  329. wpa_group_sm_step(wpa_auth, group);
  330. }
  331. return group;
  332. }
  333. /**
  334. * wpa_init - Initialize WPA authenticator
  335. * @addr: Authenticator address
  336. * @conf: Configuration for WPA authenticator
  337. * @cb: Callback functions for WPA authenticator
  338. * Returns: Pointer to WPA authenticator data or %NULL on failure
  339. */
  340. struct wpa_authenticator * wpa_init(const u8 *addr,
  341. struct wpa_auth_config *conf,
  342. const struct wpa_auth_callbacks *cb,
  343. void *cb_ctx)
  344. {
  345. struct wpa_authenticator *wpa_auth;
  346. wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
  347. if (wpa_auth == NULL)
  348. return NULL;
  349. os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
  350. os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
  351. wpa_auth->cb = cb;
  352. wpa_auth->cb_ctx = cb_ctx;
  353. if (wpa_auth_gen_wpa_ie(wpa_auth)) {
  354. wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
  355. os_free(wpa_auth);
  356. return NULL;
  357. }
  358. wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
  359. if (wpa_auth->group == NULL) {
  360. os_free(wpa_auth->wpa_ie);
  361. os_free(wpa_auth);
  362. return NULL;
  363. }
  364. wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
  365. wpa_auth);
  366. if (wpa_auth->pmksa == NULL) {
  367. wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
  368. os_free(wpa_auth->group);
  369. os_free(wpa_auth->wpa_ie);
  370. os_free(wpa_auth);
  371. return NULL;
  372. }
  373. #ifdef CONFIG_IEEE80211R_AP
  374. wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
  375. if (wpa_auth->ft_pmk_cache == NULL) {
  376. wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
  377. os_free(wpa_auth->group);
  378. os_free(wpa_auth->wpa_ie);
  379. pmksa_cache_auth_deinit(wpa_auth->pmksa);
  380. os_free(wpa_auth);
  381. return NULL;
  382. }
  383. #endif /* CONFIG_IEEE80211R_AP */
  384. if (wpa_auth->conf.wpa_gmk_rekey) {
  385. eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
  386. wpa_rekey_gmk, wpa_auth, NULL);
  387. }
  388. if (wpa_auth->conf.wpa_group_rekey) {
  389. eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
  390. wpa_rekey_gtk, wpa_auth, NULL);
  391. }
  392. #ifdef CONFIG_P2P
  393. if (WPA_GET_BE32(conf->ip_addr_start)) {
  394. int count = WPA_GET_BE32(conf->ip_addr_end) -
  395. WPA_GET_BE32(conf->ip_addr_start) + 1;
  396. if (count > 1000)
  397. count = 1000;
  398. if (count > 0)
  399. wpa_auth->ip_pool = bitfield_alloc(count);
  400. }
  401. #endif /* CONFIG_P2P */
  402. return wpa_auth;
  403. }
  404. int wpa_init_keys(struct wpa_authenticator *wpa_auth)
  405. {
  406. struct wpa_group *group = wpa_auth->group;
  407. wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
  408. "keys");
  409. wpa_group_sm_step(wpa_auth, group);
  410. group->GInit = FALSE;
  411. wpa_group_sm_step(wpa_auth, group);
  412. if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
  413. return -1;
  414. return 0;
  415. }
  416. /**
  417. * wpa_deinit - Deinitialize WPA authenticator
  418. * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
  419. */
  420. void wpa_deinit(struct wpa_authenticator *wpa_auth)
  421. {
  422. struct wpa_group *group, *prev;
  423. eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
  424. eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
  425. pmksa_cache_auth_deinit(wpa_auth->pmksa);
  426. #ifdef CONFIG_IEEE80211R_AP
  427. wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
  428. wpa_auth->ft_pmk_cache = NULL;
  429. wpa_ft_deinit(wpa_auth);
  430. #endif /* CONFIG_IEEE80211R_AP */
  431. #ifdef CONFIG_P2P
  432. bitfield_free(wpa_auth->ip_pool);
  433. #endif /* CONFIG_P2P */
  434. os_free(wpa_auth->wpa_ie);
  435. group = wpa_auth->group;
  436. while (group) {
  437. prev = group;
  438. group = group->next;
  439. os_free(prev);
  440. }
  441. os_free(wpa_auth);
  442. }
  443. /**
  444. * wpa_reconfig - Update WPA authenticator configuration
  445. * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
  446. * @conf: Configuration for WPA authenticator
  447. */
  448. int wpa_reconfig(struct wpa_authenticator *wpa_auth,
  449. struct wpa_auth_config *conf)
  450. {
  451. struct wpa_group *group;
  452. if (wpa_auth == NULL)
  453. return 0;
  454. os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
  455. if (wpa_auth_gen_wpa_ie(wpa_auth)) {
  456. wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
  457. return -1;
  458. }
  459. /*
  460. * Reinitialize GTK to make sure it is suitable for the new
  461. * configuration.
  462. */
  463. group = wpa_auth->group;
  464. group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
  465. group->GInit = TRUE;
  466. wpa_group_sm_step(wpa_auth, group);
  467. group->GInit = FALSE;
  468. wpa_group_sm_step(wpa_auth, group);
  469. return 0;
  470. }
  471. struct wpa_state_machine *
  472. wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
  473. const u8 *p2p_dev_addr)
  474. {
  475. struct wpa_state_machine *sm;
  476. if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
  477. return NULL;
  478. sm = os_zalloc(sizeof(struct wpa_state_machine));
  479. if (sm == NULL)
  480. return NULL;
  481. os_memcpy(sm->addr, addr, ETH_ALEN);
  482. if (p2p_dev_addr)
  483. os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
  484. sm->wpa_auth = wpa_auth;
  485. sm->group = wpa_auth->group;
  486. wpa_group_get(sm->wpa_auth, sm->group);
  487. return sm;
  488. }
  489. int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
  490. struct wpa_state_machine *sm)
  491. {
  492. if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
  493. return -1;
  494. #ifdef CONFIG_IEEE80211R_AP
  495. if (sm->ft_completed) {
  496. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  497. "FT authentication already completed - do not "
  498. "start 4-way handshake");
  499. /* Go to PTKINITDONE state to allow GTK rekeying */
  500. sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
  501. sm->Pair = TRUE;
  502. return 0;
  503. }
  504. #endif /* CONFIG_IEEE80211R_AP */
  505. #ifdef CONFIG_FILS
  506. if (sm->fils_completed) {
  507. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  508. "FILS authentication already completed - do not start 4-way handshake");
  509. /* Go to PTKINITDONE state to allow GTK rekeying */
  510. sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
  511. sm->Pair = TRUE;
  512. return 0;
  513. }
  514. #endif /* CONFIG_FILS */
  515. if (sm->started) {
  516. os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
  517. sm->ReAuthenticationRequest = TRUE;
  518. return wpa_sm_step(sm);
  519. }
  520. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  521. "start authentication");
  522. sm->started = 1;
  523. sm->Init = TRUE;
  524. if (wpa_sm_step(sm) == 1)
  525. return 1; /* should not really happen */
  526. sm->Init = FALSE;
  527. sm->AuthenticationRequest = TRUE;
  528. return wpa_sm_step(sm);
  529. }
  530. void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
  531. {
  532. /* WPA/RSN was not used - clear WPA state. This is needed if the STA
  533. * reassociates back to the same AP while the previous entry for the
  534. * STA has not yet been removed. */
  535. if (sm == NULL)
  536. return;
  537. sm->wpa_key_mgmt = 0;
  538. }
  539. static void wpa_free_sta_sm(struct wpa_state_machine *sm)
  540. {
  541. #ifdef CONFIG_P2P
  542. if (WPA_GET_BE32(sm->ip_addr)) {
  543. u32 start;
  544. wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
  545. "address %u.%u.%u.%u from " MACSTR,
  546. sm->ip_addr[0], sm->ip_addr[1],
  547. sm->ip_addr[2], sm->ip_addr[3],
  548. MAC2STR(sm->addr));
  549. start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
  550. bitfield_clear(sm->wpa_auth->ip_pool,
  551. WPA_GET_BE32(sm->ip_addr) - start);
  552. }
  553. #endif /* CONFIG_P2P */
  554. if (sm->GUpdateStationKeys) {
  555. sm->group->GKeyDoneStations--;
  556. sm->GUpdateStationKeys = FALSE;
  557. }
  558. #ifdef CONFIG_IEEE80211R_AP
  559. os_free(sm->assoc_resp_ftie);
  560. wpabuf_free(sm->ft_pending_req_ies);
  561. #endif /* CONFIG_IEEE80211R_AP */
  562. os_free(sm->last_rx_eapol_key);
  563. os_free(sm->wpa_ie);
  564. wpa_group_put(sm->wpa_auth, sm->group);
  565. os_free(sm);
  566. }
  567. void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
  568. {
  569. if (sm == NULL)
  570. return;
  571. if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
  572. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  573. "strict rekeying - force GTK rekey since STA "
  574. "is leaving");
  575. eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
  576. eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
  577. NULL);
  578. }
  579. eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
  580. sm->pending_1_of_4_timeout = 0;
  581. eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
  582. eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
  583. #ifdef CONFIG_IEEE80211R_AP
  584. wpa_ft_sta_deinit(sm);
  585. #endif /* CONFIG_IEEE80211R_AP */
  586. if (sm->in_step_loop) {
  587. /* Must not free state machine while wpa_sm_step() is running.
  588. * Freeing will be completed in the end of wpa_sm_step(). */
  589. wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
  590. "machine deinit for " MACSTR, MAC2STR(sm->addr));
  591. sm->pending_deinit = 1;
  592. } else
  593. wpa_free_sta_sm(sm);
  594. }
  595. static void wpa_request_new_ptk(struct wpa_state_machine *sm)
  596. {
  597. if (sm == NULL)
  598. return;
  599. sm->PTKRequest = TRUE;
  600. sm->PTK_valid = 0;
  601. }
  602. static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
  603. const u8 *replay_counter)
  604. {
  605. int i;
  606. for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
  607. if (!ctr[i].valid)
  608. break;
  609. if (os_memcmp(replay_counter, ctr[i].counter,
  610. WPA_REPLAY_COUNTER_LEN) == 0)
  611. return 1;
  612. }
  613. return 0;
  614. }
  615. static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
  616. const u8 *replay_counter)
  617. {
  618. int i;
  619. for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
  620. if (ctr[i].valid &&
  621. (replay_counter == NULL ||
  622. os_memcmp(replay_counter, ctr[i].counter,
  623. WPA_REPLAY_COUNTER_LEN) == 0))
  624. ctr[i].valid = FALSE;
  625. }
  626. }
  627. #ifdef CONFIG_IEEE80211R_AP
  628. static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
  629. struct wpa_state_machine *sm,
  630. struct wpa_eapol_ie_parse *kde)
  631. {
  632. struct wpa_ie_data ie;
  633. struct rsn_mdie *mdie;
  634. if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
  635. ie.num_pmkid != 1 || ie.pmkid == NULL) {
  636. wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
  637. "FT 4-way handshake message 2/4");
  638. return -1;
  639. }
  640. os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
  641. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
  642. sm->sup_pmk_r1_name, PMKID_LEN);
  643. if (!kde->mdie || !kde->ftie) {
  644. wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
  645. "message 2/4", kde->mdie ? "FTIE" : "MDIE");
  646. return -1;
  647. }
  648. mdie = (struct rsn_mdie *) (kde->mdie + 2);
  649. if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
  650. os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
  651. MOBILITY_DOMAIN_ID_LEN) != 0) {
  652. wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
  653. return -1;
  654. }
  655. if (sm->assoc_resp_ftie &&
  656. (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
  657. os_memcmp(kde->ftie, sm->assoc_resp_ftie,
  658. 2 + sm->assoc_resp_ftie[1]) != 0)) {
  659. wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
  660. wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
  661. kde->ftie, kde->ftie_len);
  662. wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
  663. sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
  664. return -1;
  665. }
  666. return 0;
  667. }
  668. #endif /* CONFIG_IEEE80211R_AP */
  669. static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
  670. struct wpa_state_machine *sm, int group)
  671. {
  672. /* Supplicant reported a Michael MIC error */
  673. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  674. "received EAPOL-Key Error Request "
  675. "(STA detected Michael MIC failure (group=%d))",
  676. group);
  677. if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
  678. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  679. "ignore Michael MIC failure report since "
  680. "group cipher is not TKIP");
  681. } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
  682. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  683. "ignore Michael MIC failure report since "
  684. "pairwise cipher is not TKIP");
  685. } else {
  686. if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
  687. return 1; /* STA entry was removed */
  688. sm->dot11RSNAStatsTKIPRemoteMICFailures++;
  689. wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
  690. }
  691. /*
  692. * Error report is not a request for a new key handshake, but since
  693. * Authenticator may do it, let's change the keys now anyway.
  694. */
  695. wpa_request_new_ptk(sm);
  696. return 0;
  697. }
  698. static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
  699. size_t data_len)
  700. {
  701. struct wpa_ptk PTK;
  702. int ok = 0;
  703. const u8 *pmk = NULL;
  704. unsigned int pmk_len;
  705. os_memset(&PTK, 0, sizeof(PTK));
  706. for (;;) {
  707. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  708. pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
  709. sm->p2p_dev_addr, pmk);
  710. if (pmk == NULL)
  711. break;
  712. pmk_len = PMK_LEN;
  713. } else {
  714. pmk = sm->PMK;
  715. pmk_len = sm->pmk_len;
  716. }
  717. if (wpa_derive_ptk(sm, sm->alt_SNonce, pmk, pmk_len, &PTK) < 0)
  718. break;
  719. if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK, data, data_len)
  720. == 0) {
  721. ok = 1;
  722. break;
  723. }
  724. if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
  725. break;
  726. }
  727. if (!ok) {
  728. wpa_printf(MSG_DEBUG,
  729. "WPA: Earlier SNonce did not result in matching MIC");
  730. return -1;
  731. }
  732. wpa_printf(MSG_DEBUG,
  733. "WPA: Earlier SNonce resulted in matching MIC");
  734. sm->alt_snonce_valid = 0;
  735. os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
  736. os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
  737. sm->PTK_valid = TRUE;
  738. return 0;
  739. }
  740. void wpa_receive(struct wpa_authenticator *wpa_auth,
  741. struct wpa_state_machine *sm,
  742. u8 *data, size_t data_len)
  743. {
  744. struct ieee802_1x_hdr *hdr;
  745. struct wpa_eapol_key *key;
  746. u16 key_info, key_data_length;
  747. enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
  748. SMK_M1, SMK_M3, SMK_ERROR } msg;
  749. char *msgtxt;
  750. struct wpa_eapol_ie_parse kde;
  751. const u8 *key_data;
  752. size_t keyhdrlen, mic_len;
  753. u8 *mic;
  754. if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
  755. return;
  756. wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL data", data, data_len);
  757. mic_len = wpa_mic_len(sm->wpa_key_mgmt);
  758. keyhdrlen = sizeof(*key) + mic_len + 2;
  759. if (data_len < sizeof(*hdr) + keyhdrlen) {
  760. wpa_printf(MSG_DEBUG, "WPA: Ignore too short EAPOL-Key frame");
  761. return;
  762. }
  763. hdr = (struct ieee802_1x_hdr *) data;
  764. key = (struct wpa_eapol_key *) (hdr + 1);
  765. mic = (u8 *) (key + 1);
  766. key_info = WPA_GET_BE16(key->key_info);
  767. key_data = mic + mic_len + 2;
  768. key_data_length = WPA_GET_BE16(mic + mic_len);
  769. wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
  770. " key_info=0x%x type=%u mic_len=%u key_data_length=%u",
  771. MAC2STR(sm->addr), key_info, key->type,
  772. (unsigned int) mic_len, key_data_length);
  773. wpa_hexdump(MSG_MSGDUMP,
  774. "WPA: EAPOL-Key header (ending before Key MIC)",
  775. key, sizeof(*key));
  776. wpa_hexdump(MSG_MSGDUMP, "WPA: EAPOL-Key Key MIC",
  777. mic, mic_len);
  778. if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) {
  779. wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
  780. "key_data overflow (%d > %lu)",
  781. key_data_length,
  782. (unsigned long) (data_len - sizeof(*hdr) -
  783. keyhdrlen));
  784. return;
  785. }
  786. if (sm->wpa == WPA_VERSION_WPA2) {
  787. if (key->type == EAPOL_KEY_TYPE_WPA) {
  788. /*
  789. * Some deployed station implementations seem to send
  790. * msg 4/4 with incorrect type value in WPA2 mode.
  791. */
  792. wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
  793. "with unexpected WPA type in RSN mode");
  794. } else if (key->type != EAPOL_KEY_TYPE_RSN) {
  795. wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
  796. "unexpected type %d in RSN mode",
  797. key->type);
  798. return;
  799. }
  800. } else {
  801. if (key->type != EAPOL_KEY_TYPE_WPA) {
  802. wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
  803. "unexpected type %d in WPA mode",
  804. key->type);
  805. return;
  806. }
  807. }
  808. wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
  809. WPA_NONCE_LEN);
  810. wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
  811. key->replay_counter, WPA_REPLAY_COUNTER_LEN);
  812. /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
  813. * are set */
  814. if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
  815. (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
  816. if (key_info & WPA_KEY_INFO_ERROR) {
  817. msg = SMK_ERROR;
  818. msgtxt = "SMK Error";
  819. } else {
  820. msg = SMK_M1;
  821. msgtxt = "SMK M1";
  822. }
  823. } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
  824. msg = SMK_M3;
  825. msgtxt = "SMK M3";
  826. } else if (key_info & WPA_KEY_INFO_REQUEST) {
  827. msg = REQUEST;
  828. msgtxt = "Request";
  829. } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
  830. msg = GROUP_2;
  831. msgtxt = "2/2 Group";
  832. } else if (key_data_length == 0 ||
  833. (mic_len == 0 && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) &&
  834. key_data_length == AES_BLOCK_SIZE)) {
  835. msg = PAIRWISE_4;
  836. msgtxt = "4/4 Pairwise";
  837. } else {
  838. msg = PAIRWISE_2;
  839. msgtxt = "2/4 Pairwise";
  840. }
  841. /* TODO: key_info type validation for PeerKey */
  842. if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
  843. msg == GROUP_2) {
  844. u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  845. if (sm->pairwise == WPA_CIPHER_CCMP ||
  846. sm->pairwise == WPA_CIPHER_GCMP) {
  847. if (wpa_use_aes_cmac(sm) &&
  848. sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN &&
  849. !wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
  850. !wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
  851. ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  852. wpa_auth_logger(wpa_auth, sm->addr,
  853. LOGGER_WARNING,
  854. "advertised support for "
  855. "AES-128-CMAC, but did not "
  856. "use it");
  857. return;
  858. }
  859. if (!wpa_use_aes_cmac(sm) &&
  860. !wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
  861. sm->wpa_key_mgmt != WPA_KEY_MGMT_OWE &&
  862. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
  863. wpa_auth_logger(wpa_auth, sm->addr,
  864. LOGGER_WARNING,
  865. "did not use HMAC-SHA1-AES "
  866. "with CCMP/GCMP");
  867. return;
  868. }
  869. }
  870. if ((wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
  871. wpa_key_mgmt_fils(sm->wpa_key_mgmt) ||
  872. sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) &&
  873. ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
  874. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
  875. "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
  876. return;
  877. }
  878. }
  879. if (key_info & WPA_KEY_INFO_REQUEST) {
  880. if (sm->req_replay_counter_used &&
  881. os_memcmp(key->replay_counter, sm->req_replay_counter,
  882. WPA_REPLAY_COUNTER_LEN) <= 0) {
  883. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
  884. "received EAPOL-Key request with "
  885. "replayed counter");
  886. return;
  887. }
  888. }
  889. if (!(key_info & WPA_KEY_INFO_REQUEST) &&
  890. !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
  891. int i;
  892. if (msg == PAIRWISE_2 &&
  893. wpa_replay_counter_valid(sm->prev_key_replay,
  894. key->replay_counter) &&
  895. sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
  896. os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
  897. {
  898. /*
  899. * Some supplicant implementations (e.g., Windows XP
  900. * WZC) update SNonce for each EAPOL-Key 2/4. This
  901. * breaks the workaround on accepting any of the
  902. * pending requests, so allow the SNonce to be updated
  903. * even if we have already sent out EAPOL-Key 3/4.
  904. */
  905. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  906. "Process SNonce update from STA "
  907. "based on retransmitted EAPOL-Key "
  908. "1/4");
  909. sm->update_snonce = 1;
  910. os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
  911. sm->alt_snonce_valid = TRUE;
  912. os_memcpy(sm->alt_replay_counter,
  913. sm->key_replay[0].counter,
  914. WPA_REPLAY_COUNTER_LEN);
  915. goto continue_processing;
  916. }
  917. if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
  918. sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
  919. os_memcmp(key->replay_counter, sm->alt_replay_counter,
  920. WPA_REPLAY_COUNTER_LEN) == 0) {
  921. /*
  922. * Supplicant may still be using the old SNonce since
  923. * there was two EAPOL-Key 2/4 messages and they had
  924. * different SNonce values.
  925. */
  926. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  927. "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
  928. goto continue_processing;
  929. }
  930. if (msg == PAIRWISE_2 &&
  931. wpa_replay_counter_valid(sm->prev_key_replay,
  932. key->replay_counter) &&
  933. sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
  934. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  935. "ignore retransmitted EAPOL-Key %s - "
  936. "SNonce did not change", msgtxt);
  937. } else {
  938. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  939. "received EAPOL-Key %s with "
  940. "unexpected replay counter", msgtxt);
  941. }
  942. for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
  943. if (!sm->key_replay[i].valid)
  944. break;
  945. wpa_hexdump(MSG_DEBUG, "pending replay counter",
  946. sm->key_replay[i].counter,
  947. WPA_REPLAY_COUNTER_LEN);
  948. }
  949. wpa_hexdump(MSG_DEBUG, "received replay counter",
  950. key->replay_counter, WPA_REPLAY_COUNTER_LEN);
  951. return;
  952. }
  953. continue_processing:
  954. #ifdef CONFIG_FILS
  955. if (sm->wpa == WPA_VERSION_WPA2 && mic_len == 0 &&
  956. !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  957. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  958. "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
  959. return;
  960. }
  961. #endif /* CONFIG_FILS */
  962. switch (msg) {
  963. case PAIRWISE_2:
  964. if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
  965. sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
  966. (!sm->update_snonce ||
  967. sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
  968. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  969. "received EAPOL-Key msg 2/4 in "
  970. "invalid state (%d) - dropped",
  971. sm->wpa_ptk_state);
  972. return;
  973. }
  974. random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
  975. if (sm->group->reject_4way_hs_for_entropy) {
  976. /*
  977. * The system did not have enough entropy to generate
  978. * strong random numbers. Reject the first 4-way
  979. * handshake(s) and collect some entropy based on the
  980. * information from it. Once enough entropy is
  981. * available, the next atempt will trigger GMK/Key
  982. * Counter update and the station will be allowed to
  983. * continue.
  984. */
  985. wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
  986. "collect more entropy for random number "
  987. "generation");
  988. random_mark_pool_ready();
  989. wpa_sta_disconnect(wpa_auth, sm->addr);
  990. return;
  991. }
  992. break;
  993. case PAIRWISE_4:
  994. if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
  995. !sm->PTK_valid) {
  996. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  997. "received EAPOL-Key msg 4/4 in "
  998. "invalid state (%d) - dropped",
  999. sm->wpa_ptk_state);
  1000. return;
  1001. }
  1002. break;
  1003. case GROUP_2:
  1004. if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
  1005. || !sm->PTK_valid) {
  1006. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  1007. "received EAPOL-Key msg 2/2 in "
  1008. "invalid state (%d) - dropped",
  1009. sm->wpa_ptk_group_state);
  1010. return;
  1011. }
  1012. break;
  1013. #ifdef CONFIG_PEERKEY
  1014. case SMK_M1:
  1015. case SMK_M3:
  1016. case SMK_ERROR:
  1017. if (!wpa_auth->conf.peerkey) {
  1018. wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
  1019. "PeerKey use disabled - ignoring message");
  1020. return;
  1021. }
  1022. if (!sm->PTK_valid) {
  1023. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1024. "received EAPOL-Key msg SMK in "
  1025. "invalid state - dropped");
  1026. return;
  1027. }
  1028. break;
  1029. #else /* CONFIG_PEERKEY */
  1030. case SMK_M1:
  1031. case SMK_M3:
  1032. case SMK_ERROR:
  1033. return; /* STSL disabled - ignore SMK messages */
  1034. #endif /* CONFIG_PEERKEY */
  1035. case REQUEST:
  1036. break;
  1037. }
  1038. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  1039. "received EAPOL-Key frame (%s)", msgtxt);
  1040. if (key_info & WPA_KEY_INFO_ACK) {
  1041. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1042. "received invalid EAPOL-Key: Key Ack set");
  1043. return;
  1044. }
  1045. if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
  1046. !(key_info & WPA_KEY_INFO_MIC)) {
  1047. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1048. "received invalid EAPOL-Key: Key MIC not set");
  1049. return;
  1050. }
  1051. #ifdef CONFIG_FILS
  1052. if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
  1053. (key_info & WPA_KEY_INFO_MIC)) {
  1054. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1055. "received invalid EAPOL-Key: Key MIC set");
  1056. return;
  1057. }
  1058. #endif /* CONFIG_FILS */
  1059. sm->MICVerified = FALSE;
  1060. if (sm->PTK_valid && !sm->update_snonce) {
  1061. if (mic_len &&
  1062. wpa_verify_key_mic(sm->wpa_key_mgmt, &sm->PTK, data,
  1063. data_len) &&
  1064. (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
  1065. wpa_try_alt_snonce(sm, data, data_len))) {
  1066. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1067. "received EAPOL-Key with invalid MIC");
  1068. return;
  1069. }
  1070. #ifdef CONFIG_FILS
  1071. if (!mic_len &&
  1072. wpa_aead_decrypt(sm, &sm->PTK, data, data_len,
  1073. &key_data_length) < 0) {
  1074. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1075. "received EAPOL-Key with invalid MIC");
  1076. return;
  1077. }
  1078. #endif /* CONFIG_FILS */
  1079. sm->MICVerified = TRUE;
  1080. eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
  1081. sm->pending_1_of_4_timeout = 0;
  1082. }
  1083. if (key_info & WPA_KEY_INFO_REQUEST) {
  1084. if (sm->MICVerified) {
  1085. sm->req_replay_counter_used = 1;
  1086. os_memcpy(sm->req_replay_counter, key->replay_counter,
  1087. WPA_REPLAY_COUNTER_LEN);
  1088. } else {
  1089. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1090. "received EAPOL-Key request with "
  1091. "invalid MIC");
  1092. return;
  1093. }
  1094. /*
  1095. * TODO: should decrypt key data field if encryption was used;
  1096. * even though MAC address KDE is not normally encrypted,
  1097. * supplicant is allowed to encrypt it.
  1098. */
  1099. if (msg == SMK_ERROR) {
  1100. #ifdef CONFIG_PEERKEY
  1101. wpa_smk_error(wpa_auth, sm, key_data, key_data_length);
  1102. #endif /* CONFIG_PEERKEY */
  1103. return;
  1104. } else if (key_info & WPA_KEY_INFO_ERROR) {
  1105. if (wpa_receive_error_report(
  1106. wpa_auth, sm,
  1107. !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
  1108. return; /* STA entry was removed */
  1109. } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
  1110. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1111. "received EAPOL-Key Request for new "
  1112. "4-Way Handshake");
  1113. wpa_request_new_ptk(sm);
  1114. #ifdef CONFIG_PEERKEY
  1115. } else if (msg == SMK_M1) {
  1116. wpa_smk_m1(wpa_auth, sm, key, key_data,
  1117. key_data_length);
  1118. #endif /* CONFIG_PEERKEY */
  1119. } else if (key_data_length > 0 &&
  1120. wpa_parse_kde_ies(key_data, key_data_length,
  1121. &kde) == 0 &&
  1122. kde.mac_addr) {
  1123. } else {
  1124. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1125. "received EAPOL-Key Request for GTK "
  1126. "rekeying");
  1127. eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
  1128. wpa_rekey_gtk(wpa_auth, NULL);
  1129. }
  1130. } else {
  1131. /* Do not allow the same key replay counter to be reused. */
  1132. wpa_replay_counter_mark_invalid(sm->key_replay,
  1133. key->replay_counter);
  1134. if (msg == PAIRWISE_2) {
  1135. /*
  1136. * Maintain a copy of the pending EAPOL-Key frames in
  1137. * case the EAPOL-Key frame was retransmitted. This is
  1138. * needed to allow EAPOL-Key msg 2/4 reply to another
  1139. * pending msg 1/4 to update the SNonce to work around
  1140. * unexpected supplicant behavior.
  1141. */
  1142. os_memcpy(sm->prev_key_replay, sm->key_replay,
  1143. sizeof(sm->key_replay));
  1144. } else {
  1145. os_memset(sm->prev_key_replay, 0,
  1146. sizeof(sm->prev_key_replay));
  1147. }
  1148. /*
  1149. * Make sure old valid counters are not accepted anymore and
  1150. * do not get copied again.
  1151. */
  1152. wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
  1153. }
  1154. #ifdef CONFIG_PEERKEY
  1155. if (msg == SMK_M3) {
  1156. wpa_smk_m3(wpa_auth, sm, key, key_data, key_data_length);
  1157. return;
  1158. }
  1159. #endif /* CONFIG_PEERKEY */
  1160. os_free(sm->last_rx_eapol_key);
  1161. sm->last_rx_eapol_key = os_memdup(data, data_len);
  1162. if (sm->last_rx_eapol_key == NULL)
  1163. return;
  1164. sm->last_rx_eapol_key_len = data_len;
  1165. sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
  1166. sm->EAPOLKeyReceived = TRUE;
  1167. sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
  1168. sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
  1169. os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
  1170. wpa_sm_step(sm);
  1171. }
  1172. static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
  1173. const u8 *gnonce, u8 *gtk, size_t gtk_len)
  1174. {
  1175. u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16];
  1176. u8 *pos;
  1177. int ret = 0;
  1178. /* GTK = PRF-X(GMK, "Group key expansion",
  1179. * AA || GNonce || Time || random data)
  1180. * The example described in the IEEE 802.11 standard uses only AA and
  1181. * GNonce as inputs here. Add some more entropy since this derivation
  1182. * is done only at the Authenticator and as such, does not need to be
  1183. * exactly same.
  1184. */
  1185. os_memcpy(data, addr, ETH_ALEN);
  1186. os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
  1187. pos = data + ETH_ALEN + WPA_NONCE_LEN;
  1188. wpa_get_ntp_timestamp(pos);
  1189. pos += 8;
  1190. if (random_get_bytes(pos, 16) < 0)
  1191. ret = -1;
  1192. #ifdef CONFIG_IEEE80211W
  1193. sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len);
  1194. #else /* CONFIG_IEEE80211W */
  1195. if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len)
  1196. < 0)
  1197. ret = -1;
  1198. #endif /* CONFIG_IEEE80211W */
  1199. return ret;
  1200. }
  1201. static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
  1202. {
  1203. struct wpa_authenticator *wpa_auth = eloop_ctx;
  1204. struct wpa_state_machine *sm = timeout_ctx;
  1205. sm->pending_1_of_4_timeout = 0;
  1206. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
  1207. sm->TimeoutEvt = TRUE;
  1208. wpa_sm_step(sm);
  1209. }
  1210. void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
  1211. struct wpa_state_machine *sm, int key_info,
  1212. const u8 *key_rsc, const u8 *nonce,
  1213. const u8 *kde, size_t kde_len,
  1214. int keyidx, int encr, int force_version)
  1215. {
  1216. struct ieee802_1x_hdr *hdr;
  1217. struct wpa_eapol_key *key;
  1218. size_t len, mic_len, keyhdrlen;
  1219. int alg;
  1220. int key_data_len, pad_len = 0;
  1221. u8 *buf, *pos;
  1222. int version, pairwise;
  1223. int i;
  1224. u8 *key_mic, *key_data;
  1225. mic_len = wpa_mic_len(sm->wpa_key_mgmt);
  1226. keyhdrlen = sizeof(*key) + mic_len + 2;
  1227. len = sizeof(struct ieee802_1x_hdr) + keyhdrlen;
  1228. if (force_version)
  1229. version = force_version;
  1230. else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
  1231. sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE ||
  1232. wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
  1233. wpa_key_mgmt_fils(sm->wpa_key_mgmt))
  1234. version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
  1235. else if (wpa_use_aes_cmac(sm))
  1236. version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
  1237. else if (sm->pairwise != WPA_CIPHER_TKIP)
  1238. version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
  1239. else
  1240. version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
  1241. pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
  1242. wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
  1243. "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
  1244. "encr=%d)",
  1245. version,
  1246. (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
  1247. (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
  1248. (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
  1249. (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
  1250. pairwise, (unsigned long) kde_len, keyidx, encr);
  1251. key_data_len = kde_len;
  1252. if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
  1253. sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE ||
  1254. sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
  1255. wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
  1256. version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
  1257. pad_len = key_data_len % 8;
  1258. if (pad_len)
  1259. pad_len = 8 - pad_len;
  1260. key_data_len += pad_len + 8;
  1261. }
  1262. len += key_data_len;
  1263. if (!mic_len && encr)
  1264. len += AES_BLOCK_SIZE;
  1265. hdr = os_zalloc(len);
  1266. if (hdr == NULL)
  1267. return;
  1268. hdr->version = wpa_auth->conf.eapol_version;
  1269. hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
  1270. hdr->length = host_to_be16(len - sizeof(*hdr));
  1271. key = (struct wpa_eapol_key *) (hdr + 1);
  1272. key_mic = (u8 *) (key + 1);
  1273. key_data = ((u8 *) (hdr + 1)) + keyhdrlen;
  1274. key->type = sm->wpa == WPA_VERSION_WPA2 ?
  1275. EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
  1276. key_info |= version;
  1277. if (encr && sm->wpa == WPA_VERSION_WPA2)
  1278. key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
  1279. if (sm->wpa != WPA_VERSION_WPA2)
  1280. key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
  1281. WPA_PUT_BE16(key->key_info, key_info);
  1282. alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
  1283. if ((key_info & WPA_KEY_INFO_SMK_MESSAGE) ||
  1284. (sm->wpa == WPA_VERSION_WPA2 && !pairwise))
  1285. WPA_PUT_BE16(key->key_length, 0);
  1286. else
  1287. WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
  1288. /* FIX: STSL: what to use as key_replay_counter? */
  1289. for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
  1290. sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
  1291. os_memcpy(sm->key_replay[i].counter,
  1292. sm->key_replay[i - 1].counter,
  1293. WPA_REPLAY_COUNTER_LEN);
  1294. }
  1295. inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
  1296. os_memcpy(key->replay_counter, sm->key_replay[0].counter,
  1297. WPA_REPLAY_COUNTER_LEN);
  1298. wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
  1299. key->replay_counter, WPA_REPLAY_COUNTER_LEN);
  1300. sm->key_replay[0].valid = TRUE;
  1301. if (nonce)
  1302. os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
  1303. if (key_rsc)
  1304. os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
  1305. if (kde && !encr) {
  1306. os_memcpy(key_data, kde, kde_len);
  1307. WPA_PUT_BE16(key_mic + mic_len, kde_len);
  1308. #ifdef CONFIG_FILS
  1309. } else if (!mic_len) {
  1310. const u8 *aad[1];
  1311. size_t aad_len[1];
  1312. WPA_PUT_BE16(key_mic, AES_BLOCK_SIZE + kde_len);
  1313. wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
  1314. kde, kde_len);
  1315. wpa_hexdump_key(MSG_DEBUG, "WPA: KEK",
  1316. sm->PTK.kek, sm->PTK.kek_len);
  1317. /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
  1318. * to Key Data (exclusive). */
  1319. aad[0] = (u8 *) hdr;
  1320. aad_len[0] = key_mic + 2 - (u8 *) hdr;
  1321. if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len, kde, kde_len,
  1322. 1, aad, aad_len, key_mic + 2) < 0) {
  1323. wpa_printf(MSG_DEBUG, "WPA: AES-SIV encryption failed");
  1324. return;
  1325. }
  1326. wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
  1327. key_mic + 2, AES_BLOCK_SIZE + kde_len);
  1328. #endif /* CONFIG_FILS */
  1329. } else if (encr && kde) {
  1330. buf = os_zalloc(key_data_len);
  1331. if (buf == NULL) {
  1332. os_free(hdr);
  1333. return;
  1334. }
  1335. pos = buf;
  1336. os_memcpy(pos, kde, kde_len);
  1337. pos += kde_len;
  1338. if (pad_len)
  1339. *pos++ = 0xdd;
  1340. wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
  1341. buf, key_data_len);
  1342. if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
  1343. sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE ||
  1344. sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
  1345. wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
  1346. version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  1347. wpa_printf(MSG_DEBUG,
  1348. "WPA: Encrypt Key Data using AES-WRAP (KEK length %u)",
  1349. (unsigned int) sm->PTK.kek_len);
  1350. if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len,
  1351. (key_data_len - 8) / 8, buf, key_data)) {
  1352. os_free(hdr);
  1353. os_free(buf);
  1354. return;
  1355. }
  1356. WPA_PUT_BE16(key_mic + mic_len, key_data_len);
  1357. #ifndef CONFIG_NO_RC4
  1358. } else if (sm->PTK.kek_len == 16) {
  1359. u8 ek[32];
  1360. wpa_printf(MSG_DEBUG,
  1361. "WPA: Encrypt Key Data using RC4");
  1362. os_memcpy(key->key_iv,
  1363. sm->group->Counter + WPA_NONCE_LEN - 16, 16);
  1364. inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
  1365. os_memcpy(ek, key->key_iv, 16);
  1366. os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len);
  1367. os_memcpy(key_data, buf, key_data_len);
  1368. rc4_skip(ek, 32, 256, key_data, key_data_len);
  1369. WPA_PUT_BE16(key_mic + mic_len, key_data_len);
  1370. #endif /* CONFIG_NO_RC4 */
  1371. } else {
  1372. os_free(hdr);
  1373. os_free(buf);
  1374. return;
  1375. }
  1376. os_free(buf);
  1377. }
  1378. if (key_info & WPA_KEY_INFO_MIC) {
  1379. if (!sm->PTK_valid || !mic_len) {
  1380. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  1381. "PTK not valid when sending EAPOL-Key "
  1382. "frame");
  1383. os_free(hdr);
  1384. return;
  1385. }
  1386. wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len,
  1387. sm->wpa_key_mgmt, version,
  1388. (u8 *) hdr, len, key_mic);
  1389. #ifdef CONFIG_TESTING_OPTIONS
  1390. if (!pairwise &&
  1391. wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
  1392. drand48() <
  1393. wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
  1394. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1395. "Corrupting group EAPOL-Key Key MIC");
  1396. key_mic[0]++;
  1397. }
  1398. #endif /* CONFIG_TESTING_OPTIONS */
  1399. }
  1400. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
  1401. 1);
  1402. wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
  1403. sm->pairwise_set);
  1404. os_free(hdr);
  1405. }
  1406. static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
  1407. struct wpa_state_machine *sm, int key_info,
  1408. const u8 *key_rsc, const u8 *nonce,
  1409. const u8 *kde, size_t kde_len,
  1410. int keyidx, int encr)
  1411. {
  1412. int timeout_ms;
  1413. int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
  1414. u32 ctr;
  1415. if (sm == NULL)
  1416. return;
  1417. __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
  1418. keyidx, encr, 0);
  1419. ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
  1420. if (ctr == 1 && wpa_auth->conf.tx_status)
  1421. timeout_ms = pairwise ? eapol_key_timeout_first :
  1422. eapol_key_timeout_first_group;
  1423. else
  1424. timeout_ms = eapol_key_timeout_subseq;
  1425. if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
  1426. sm->pending_1_of_4_timeout = 1;
  1427. wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
  1428. "counter %u)", timeout_ms, ctr);
  1429. eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
  1430. wpa_send_eapol_timeout, wpa_auth, sm);
  1431. }
  1432. static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
  1433. size_t data_len)
  1434. {
  1435. struct ieee802_1x_hdr *hdr;
  1436. struct wpa_eapol_key *key;
  1437. u16 key_info;
  1438. int ret = 0;
  1439. u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN], *mic_pos;
  1440. size_t mic_len = wpa_mic_len(akmp);
  1441. if (data_len < sizeof(*hdr) + sizeof(*key))
  1442. return -1;
  1443. hdr = (struct ieee802_1x_hdr *) data;
  1444. key = (struct wpa_eapol_key *) (hdr + 1);
  1445. mic_pos = (u8 *) (key + 1);
  1446. key_info = WPA_GET_BE16(key->key_info);
  1447. os_memcpy(mic, mic_pos, mic_len);
  1448. os_memset(mic_pos, 0, mic_len);
  1449. if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp,
  1450. key_info & WPA_KEY_INFO_TYPE_MASK,
  1451. data, data_len, mic_pos) ||
  1452. os_memcmp_const(mic, mic_pos, mic_len) != 0)
  1453. ret = -1;
  1454. os_memcpy(mic_pos, mic, mic_len);
  1455. return ret;
  1456. }
  1457. void wpa_remove_ptk(struct wpa_state_machine *sm)
  1458. {
  1459. sm->PTK_valid = FALSE;
  1460. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  1461. if (wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL,
  1462. 0))
  1463. wpa_printf(MSG_DEBUG,
  1464. "RSN: PTK removal from the driver failed");
  1465. sm->pairwise_set = FALSE;
  1466. eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
  1467. }
  1468. int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)
  1469. {
  1470. int remove_ptk = 1;
  1471. if (sm == NULL)
  1472. return -1;
  1473. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1474. "event %d notification", event);
  1475. switch (event) {
  1476. case WPA_AUTH:
  1477. #ifdef CONFIG_MESH
  1478. /* PTKs are derived through AMPE */
  1479. if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
  1480. /* not mesh */
  1481. break;
  1482. }
  1483. return 0;
  1484. #endif /* CONFIG_MESH */
  1485. case WPA_ASSOC:
  1486. break;
  1487. case WPA_DEAUTH:
  1488. case WPA_DISASSOC:
  1489. sm->DeauthenticationRequest = TRUE;
  1490. break;
  1491. case WPA_REAUTH:
  1492. case WPA_REAUTH_EAPOL:
  1493. if (!sm->started) {
  1494. /*
  1495. * When using WPS, we may end up here if the STA
  1496. * manages to re-associate without the previous STA
  1497. * entry getting removed. Consequently, we need to make
  1498. * sure that the WPA state machines gets initialized
  1499. * properly at this point.
  1500. */
  1501. wpa_printf(MSG_DEBUG, "WPA state machine had not been "
  1502. "started - initialize now");
  1503. sm->started = 1;
  1504. sm->Init = TRUE;
  1505. if (wpa_sm_step(sm) == 1)
  1506. return 1; /* should not really happen */
  1507. sm->Init = FALSE;
  1508. sm->AuthenticationRequest = TRUE;
  1509. break;
  1510. }
  1511. if (sm->GUpdateStationKeys) {
  1512. /*
  1513. * Reauthentication cancels the pending group key
  1514. * update for this STA.
  1515. */
  1516. sm->group->GKeyDoneStations--;
  1517. sm->GUpdateStationKeys = FALSE;
  1518. sm->PtkGroupInit = TRUE;
  1519. }
  1520. sm->ReAuthenticationRequest = TRUE;
  1521. break;
  1522. case WPA_ASSOC_FT:
  1523. #ifdef CONFIG_IEEE80211R_AP
  1524. wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
  1525. "after association");
  1526. wpa_ft_install_ptk(sm);
  1527. /* Using FT protocol, not WPA auth state machine */
  1528. sm->ft_completed = 1;
  1529. return 0;
  1530. #else /* CONFIG_IEEE80211R_AP */
  1531. break;
  1532. #endif /* CONFIG_IEEE80211R_AP */
  1533. case WPA_ASSOC_FILS:
  1534. #ifdef CONFIG_FILS
  1535. wpa_printf(MSG_DEBUG,
  1536. "FILS: TK configuration after association");
  1537. fils_set_tk(sm);
  1538. sm->fils_completed = 1;
  1539. return 0;
  1540. #else /* CONFIG_FILS */
  1541. break;
  1542. #endif /* CONFIG_FILS */
  1543. }
  1544. #ifdef CONFIG_IEEE80211R_AP
  1545. sm->ft_completed = 0;
  1546. #endif /* CONFIG_IEEE80211R_AP */
  1547. #ifdef CONFIG_IEEE80211W
  1548. if (sm->mgmt_frame_prot && event == WPA_AUTH)
  1549. remove_ptk = 0;
  1550. #endif /* CONFIG_IEEE80211W */
  1551. #ifdef CONFIG_FILS
  1552. if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
  1553. (event == WPA_AUTH || event == WPA_ASSOC))
  1554. remove_ptk = 0;
  1555. #endif /* CONFIG_FILS */
  1556. if (remove_ptk) {
  1557. sm->PTK_valid = FALSE;
  1558. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  1559. if (event != WPA_REAUTH_EAPOL)
  1560. wpa_remove_ptk(sm);
  1561. }
  1562. if (sm->in_step_loop) {
  1563. /*
  1564. * wpa_sm_step() is already running - avoid recursive call to
  1565. * it by making the existing loop process the new update.
  1566. */
  1567. sm->changed = TRUE;
  1568. return 0;
  1569. }
  1570. return wpa_sm_step(sm);
  1571. }
  1572. SM_STATE(WPA_PTK, INITIALIZE)
  1573. {
  1574. SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
  1575. if (sm->Init) {
  1576. /* Init flag is not cleared here, so avoid busy
  1577. * loop by claiming nothing changed. */
  1578. sm->changed = FALSE;
  1579. }
  1580. sm->keycount = 0;
  1581. if (sm->GUpdateStationKeys)
  1582. sm->group->GKeyDoneStations--;
  1583. sm->GUpdateStationKeys = FALSE;
  1584. if (sm->wpa == WPA_VERSION_WPA)
  1585. sm->PInitAKeys = FALSE;
  1586. if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
  1587. * Local AA > Remote AA)) */) {
  1588. sm->Pair = TRUE;
  1589. }
  1590. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
  1591. wpa_remove_ptk(sm);
  1592. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
  1593. sm->TimeoutCtr = 0;
  1594. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
  1595. sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
  1596. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1597. WPA_EAPOL_authorized, 0);
  1598. }
  1599. }
  1600. SM_STATE(WPA_PTK, DISCONNECT)
  1601. {
  1602. SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
  1603. sm->Disconnect = FALSE;
  1604. wpa_sta_disconnect(sm->wpa_auth, sm->addr);
  1605. }
  1606. SM_STATE(WPA_PTK, DISCONNECTED)
  1607. {
  1608. SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
  1609. sm->DeauthenticationRequest = FALSE;
  1610. }
  1611. SM_STATE(WPA_PTK, AUTHENTICATION)
  1612. {
  1613. SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
  1614. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  1615. sm->PTK_valid = FALSE;
  1616. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
  1617. 1);
  1618. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
  1619. sm->AuthenticationRequest = FALSE;
  1620. }
  1621. static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
  1622. struct wpa_group *group)
  1623. {
  1624. if (group->first_sta_seen)
  1625. return;
  1626. /*
  1627. * System has run bit further than at the time hostapd was started
  1628. * potentially very early during boot up. This provides better chances
  1629. * of collecting more randomness on embedded systems. Re-initialize the
  1630. * GMK and Counter here to improve their strength if there was not
  1631. * enough entropy available immediately after system startup.
  1632. */
  1633. wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
  1634. "station");
  1635. if (random_pool_ready() != 1) {
  1636. wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
  1637. "to proceed - reject first 4-way handshake");
  1638. group->reject_4way_hs_for_entropy = TRUE;
  1639. } else {
  1640. group->first_sta_seen = TRUE;
  1641. group->reject_4way_hs_for_entropy = FALSE;
  1642. }
  1643. if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 ||
  1644. wpa_gtk_update(wpa_auth, group) < 0 ||
  1645. wpa_group_config_group_keys(wpa_auth, group) < 0) {
  1646. wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed");
  1647. group->first_sta_seen = FALSE;
  1648. group->reject_4way_hs_for_entropy = TRUE;
  1649. }
  1650. }
  1651. SM_STATE(WPA_PTK, AUTHENTICATION2)
  1652. {
  1653. SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
  1654. wpa_group_ensure_init(sm->wpa_auth, sm->group);
  1655. sm->ReAuthenticationRequest = FALSE;
  1656. /*
  1657. * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
  1658. * ambiguous. The Authenticator state machine uses a counter that is
  1659. * incremented by one for each 4-way handshake. However, the security
  1660. * analysis of 4-way handshake points out that unpredictable nonces
  1661. * help in preventing precomputation attacks. Instead of the state
  1662. * machine definition, use an unpredictable nonce value here to provide
  1663. * stronger protection against potential precomputation attacks.
  1664. */
  1665. if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
  1666. wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
  1667. "ANonce.");
  1668. sm->Disconnect = TRUE;
  1669. return;
  1670. }
  1671. wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
  1672. WPA_NONCE_LEN);
  1673. /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
  1674. * logical place than INITIALIZE since AUTHENTICATION2 can be
  1675. * re-entered on ReAuthenticationRequest without going through
  1676. * INITIALIZE. */
  1677. sm->TimeoutCtr = 0;
  1678. }
  1679. SM_STATE(WPA_PTK, INITPMK)
  1680. {
  1681. u8 msk[2 * PMK_LEN];
  1682. size_t len = 2 * PMK_LEN;
  1683. SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
  1684. #ifdef CONFIG_IEEE80211R_AP
  1685. sm->xxkey_len = 0;
  1686. #endif /* CONFIG_IEEE80211R_AP */
  1687. if (sm->pmksa) {
  1688. wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
  1689. os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
  1690. sm->pmk_len = sm->pmksa->pmk_len;
  1691. } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
  1692. unsigned int pmk_len;
  1693. if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt))
  1694. pmk_len = PMK_LEN_SUITE_B_192;
  1695. else
  1696. pmk_len = PMK_LEN;
  1697. wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
  1698. "(MSK len=%lu PMK len=%u)", (unsigned long) len,
  1699. pmk_len);
  1700. if (len < pmk_len) {
  1701. wpa_printf(MSG_DEBUG,
  1702. "WPA: MSK not long enough (%u) to create PMK (%u)",
  1703. (unsigned int) len, (unsigned int) pmk_len);
  1704. sm->Disconnect = TRUE;
  1705. return;
  1706. }
  1707. os_memcpy(sm->PMK, msk, pmk_len);
  1708. sm->pmk_len = pmk_len;
  1709. #ifdef CONFIG_IEEE80211R_AP
  1710. if (len >= 2 * PMK_LEN) {
  1711. os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
  1712. sm->xxkey_len = PMK_LEN;
  1713. }
  1714. #endif /* CONFIG_IEEE80211R_AP */
  1715. } else {
  1716. wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p",
  1717. sm->wpa_auth->cb->get_msk);
  1718. sm->Disconnect = TRUE;
  1719. return;
  1720. }
  1721. os_memset(msk, 0, sizeof(msk));
  1722. sm->req_replay_counter_used = 0;
  1723. /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
  1724. * will break reauthentication since EAPOL state machines may not be
  1725. * get into AUTHENTICATING state that clears keyRun before WPA state
  1726. * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
  1727. * state and takes PMK from the previously used AAA Key. This will
  1728. * eventually fail in 4-Way Handshake because Supplicant uses PMK
  1729. * derived from the new AAA Key. Setting keyRun = FALSE here seems to
  1730. * be good workaround for this issue. */
  1731. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
  1732. }
  1733. SM_STATE(WPA_PTK, INITPSK)
  1734. {
  1735. const u8 *psk;
  1736. SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
  1737. psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL);
  1738. if (psk) {
  1739. os_memcpy(sm->PMK, psk, PMK_LEN);
  1740. sm->pmk_len = PMK_LEN;
  1741. #ifdef CONFIG_IEEE80211R_AP
  1742. os_memcpy(sm->xxkey, psk, PMK_LEN);
  1743. sm->xxkey_len = PMK_LEN;
  1744. #endif /* CONFIG_IEEE80211R_AP */
  1745. }
  1746. sm->req_replay_counter_used = 0;
  1747. }
  1748. SM_STATE(WPA_PTK, PTKSTART)
  1749. {
  1750. u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
  1751. size_t pmkid_len = 0;
  1752. SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
  1753. sm->PTKRequest = FALSE;
  1754. sm->TimeoutEvt = FALSE;
  1755. sm->alt_snonce_valid = FALSE;
  1756. sm->TimeoutCtr++;
  1757. if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
  1758. /* No point in sending the EAPOL-Key - we will disconnect
  1759. * immediately following this. */
  1760. return;
  1761. }
  1762. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1763. "sending 1/4 msg of 4-Way Handshake");
  1764. /*
  1765. * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
  1766. * one possible PSK for this STA.
  1767. */
  1768. if (sm->wpa == WPA_VERSION_WPA2 &&
  1769. wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
  1770. sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
  1771. pmkid = buf;
  1772. pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
  1773. pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
  1774. pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
  1775. RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
  1776. if (sm->pmksa) {
  1777. os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
  1778. sm->pmksa->pmkid, PMKID_LEN);
  1779. } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
  1780. /* No KCK available to derive PMKID */
  1781. pmkid = NULL;
  1782. } else {
  1783. /*
  1784. * Calculate PMKID since no PMKSA cache entry was
  1785. * available with pre-calculated PMKID.
  1786. */
  1787. rsn_pmkid(sm->PMK, sm->pmk_len, sm->wpa_auth->addr,
  1788. sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
  1789. wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
  1790. }
  1791. }
  1792. wpa_send_eapol(sm->wpa_auth, sm,
  1793. WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
  1794. sm->ANonce, pmkid, pmkid_len, 0, 0);
  1795. }
  1796. static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
  1797. const u8 *pmk, unsigned int pmk_len,
  1798. struct wpa_ptk *ptk)
  1799. {
  1800. #ifdef CONFIG_IEEE80211R_AP
  1801. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
  1802. return wpa_auth_derive_ptk_ft(sm, pmk, ptk);
  1803. #endif /* CONFIG_IEEE80211R_AP */
  1804. return wpa_pmk_to_ptk(pmk, pmk_len, "Pairwise key expansion",
  1805. sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce,
  1806. ptk, sm->wpa_key_mgmt, sm->pairwise);
  1807. }
  1808. #ifdef CONFIG_FILS
  1809. int fils_auth_pmk_to_ptk(struct wpa_state_machine *sm, const u8 *pmk,
  1810. size_t pmk_len, const u8 *snonce, const u8 *anonce,
  1811. struct wpabuf *g_sta, struct wpabuf *g_ap)
  1812. {
  1813. u8 ick[FILS_ICK_MAX_LEN];
  1814. size_t ick_len;
  1815. int res;
  1816. res = fils_pmk_to_ptk(pmk, pmk_len, sm->addr, sm->wpa_auth->addr,
  1817. snonce, anonce, &sm->PTK, ick, &ick_len,
  1818. sm->wpa_key_mgmt, sm->pairwise);
  1819. if (res < 0)
  1820. return res;
  1821. sm->PTK_valid = TRUE;
  1822. res = fils_key_auth_sk(ick, ick_len, snonce, anonce,
  1823. sm->addr, sm->wpa_auth->addr,
  1824. g_sta ? wpabuf_head(g_sta) : NULL,
  1825. g_sta ? wpabuf_len(g_sta) : 0,
  1826. g_ap ? wpabuf_head(g_ap) : NULL,
  1827. g_ap ? wpabuf_len(g_ap) : 0,
  1828. sm->wpa_key_mgmt, sm->fils_key_auth_sta,
  1829. sm->fils_key_auth_ap,
  1830. &sm->fils_key_auth_len);
  1831. os_memset(ick, 0, sizeof(ick));
  1832. /* Store nonces for (Re)Association Request/Response frame processing */
  1833. os_memcpy(sm->SNonce, snonce, FILS_NONCE_LEN);
  1834. os_memcpy(sm->ANonce, anonce, FILS_NONCE_LEN);
  1835. return res;
  1836. }
  1837. static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
  1838. u8 *buf, size_t buf_len, u16 *_key_data_len)
  1839. {
  1840. struct ieee802_1x_hdr *hdr;
  1841. struct wpa_eapol_key *key;
  1842. u8 *pos;
  1843. u16 key_data_len;
  1844. u8 *tmp;
  1845. const u8 *aad[1];
  1846. size_t aad_len[1];
  1847. hdr = (struct ieee802_1x_hdr *) buf;
  1848. key = (struct wpa_eapol_key *) (hdr + 1);
  1849. pos = (u8 *) (key + 1);
  1850. key_data_len = WPA_GET_BE16(pos);
  1851. if (key_data_len < AES_BLOCK_SIZE ||
  1852. key_data_len > buf_len - sizeof(*hdr) - sizeof(*key) - 2) {
  1853. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1854. "No room for AES-SIV data in the frame");
  1855. return -1;
  1856. }
  1857. pos += 2; /* Pointing at the Encrypted Key Data field */
  1858. tmp = os_malloc(key_data_len);
  1859. if (!tmp)
  1860. return -1;
  1861. /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
  1862. * to Key Data (exclusive). */
  1863. aad[0] = buf;
  1864. aad_len[0] = pos - buf;
  1865. if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, key_data_len,
  1866. 1, aad, aad_len, tmp) < 0) {
  1867. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1868. "Invalid AES-SIV data in the frame");
  1869. bin_clear_free(tmp, key_data_len);
  1870. return -1;
  1871. }
  1872. /* AEAD decryption and validation completed successfully */
  1873. key_data_len -= AES_BLOCK_SIZE;
  1874. wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
  1875. tmp, key_data_len);
  1876. /* Replace Key Data field with the decrypted version */
  1877. os_memcpy(pos, tmp, key_data_len);
  1878. pos -= 2; /* Key Data Length field */
  1879. WPA_PUT_BE16(pos, key_data_len);
  1880. bin_clear_free(tmp, key_data_len);
  1881. if (_key_data_len)
  1882. *_key_data_len = key_data_len;
  1883. return 0;
  1884. }
  1885. const u8 * wpa_fils_validate_fils_session(struct wpa_state_machine *sm,
  1886. const u8 *ies, size_t ies_len,
  1887. const u8 *fils_session)
  1888. {
  1889. const u8 *ie, *end;
  1890. const u8 *session = NULL;
  1891. if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
  1892. wpa_printf(MSG_DEBUG,
  1893. "FILS: Not a FILS AKM - reject association");
  1894. return NULL;
  1895. }
  1896. /* Verify Session element */
  1897. ie = ies;
  1898. end = ((const u8 *) ie) + ies_len;
  1899. while (ie + 1 < end) {
  1900. if (ie + 2 + ie[1] > end)
  1901. break;
  1902. if (ie[0] == WLAN_EID_EXTENSION &&
  1903. ie[1] >= 1 + FILS_SESSION_LEN &&
  1904. ie[2] == WLAN_EID_EXT_FILS_SESSION) {
  1905. session = ie;
  1906. break;
  1907. }
  1908. ie += 2 + ie[1];
  1909. }
  1910. if (!session) {
  1911. wpa_printf(MSG_DEBUG,
  1912. "FILS: %s: Could not find FILS Session element in Assoc Req - reject",
  1913. __func__);
  1914. return NULL;
  1915. }
  1916. if (!fils_session) {
  1917. wpa_printf(MSG_DEBUG,
  1918. "FILS: %s: Could not find FILS Session element in STA entry - reject",
  1919. __func__);
  1920. return NULL;
  1921. }
  1922. if (os_memcmp(fils_session, session + 3, FILS_SESSION_LEN) != 0) {
  1923. wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
  1924. wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
  1925. fils_session, FILS_SESSION_LEN);
  1926. wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
  1927. session + 3, FILS_SESSION_LEN);
  1928. return NULL;
  1929. }
  1930. return session;
  1931. }
  1932. int wpa_fils_validate_key_confirm(struct wpa_state_machine *sm, const u8 *ies,
  1933. size_t ies_len)
  1934. {
  1935. struct ieee802_11_elems elems;
  1936. if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
  1937. wpa_printf(MSG_DEBUG,
  1938. "FILS: Failed to parse decrypted elements");
  1939. return -1;
  1940. }
  1941. if (!elems.fils_key_confirm) {
  1942. wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
  1943. return -1;
  1944. }
  1945. if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
  1946. wpa_printf(MSG_DEBUG,
  1947. "FILS: Unexpected Key-Auth length %d (expected %d)",
  1948. elems.fils_key_confirm_len,
  1949. (int) sm->fils_key_auth_len);
  1950. return -1;
  1951. }
  1952. if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_sta,
  1953. sm->fils_key_auth_len) != 0) {
  1954. wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
  1955. wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
  1956. elems.fils_key_confirm, elems.fils_key_confirm_len);
  1957. wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
  1958. sm->fils_key_auth_sta, sm->fils_key_auth_len);
  1959. return -1;
  1960. }
  1961. return 0;
  1962. }
  1963. int fils_decrypt_assoc(struct wpa_state_machine *sm, const u8 *fils_session,
  1964. const struct ieee80211_mgmt *mgmt, size_t frame_len,
  1965. u8 *pos, size_t left)
  1966. {
  1967. u16 fc, stype;
  1968. const u8 *end, *ie_start, *ie, *session, *crypt;
  1969. const u8 *aad[5];
  1970. size_t aad_len[5];
  1971. if (!sm || !sm->PTK_valid) {
  1972. wpa_printf(MSG_DEBUG,
  1973. "FILS: No KEK to decrypt Assocication Request frame");
  1974. return -1;
  1975. }
  1976. if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
  1977. wpa_printf(MSG_DEBUG,
  1978. "FILS: Not a FILS AKM - reject association");
  1979. return -1;
  1980. }
  1981. end = ((const u8 *) mgmt) + frame_len;
  1982. fc = le_to_host16(mgmt->frame_control);
  1983. stype = WLAN_FC_GET_STYPE(fc);
  1984. if (stype == WLAN_FC_STYPE_REASSOC_REQ)
  1985. ie_start = mgmt->u.reassoc_req.variable;
  1986. else
  1987. ie_start = mgmt->u.assoc_req.variable;
  1988. ie = ie_start;
  1989. /*
  1990. * Find FILS Session element which is the last unencrypted element in
  1991. * the frame.
  1992. */
  1993. session = wpa_fils_validate_fils_session(sm, ie, end - ie,
  1994. fils_session);
  1995. if (!session) {
  1996. wpa_printf(MSG_DEBUG, "FILS: Session validation failed");
  1997. return -1;
  1998. }
  1999. crypt = session + 2 + session[1];
  2000. if (end - crypt < AES_BLOCK_SIZE) {
  2001. wpa_printf(MSG_DEBUG,
  2002. "FILS: Too short frame to include AES-SIV data");
  2003. return -1;
  2004. }
  2005. /* AES-SIV AAD vectors */
  2006. /* The STA's MAC address */
  2007. aad[0] = mgmt->sa;
  2008. aad_len[0] = ETH_ALEN;
  2009. /* The AP's BSSID */
  2010. aad[1] = mgmt->da;
  2011. aad_len[1] = ETH_ALEN;
  2012. /* The STA's nonce */
  2013. aad[2] = sm->SNonce;
  2014. aad_len[2] = FILS_NONCE_LEN;
  2015. /* The AP's nonce */
  2016. aad[3] = sm->ANonce;
  2017. aad_len[3] = FILS_NONCE_LEN;
  2018. /*
  2019. * The (Re)Association Request frame from the Capability Information
  2020. * field to the FILS Session element (both inclusive).
  2021. */
  2022. aad[4] = (const u8 *) &mgmt->u.assoc_req.capab_info;
  2023. aad_len[4] = crypt - aad[4];
  2024. if (aes_siv_decrypt(sm->PTK.kek, sm->PTK.kek_len, crypt, end - crypt,
  2025. 5, aad, aad_len, pos + (crypt - ie_start)) < 0) {
  2026. wpa_printf(MSG_DEBUG,
  2027. "FILS: Invalid AES-SIV data in the frame");
  2028. return -1;
  2029. }
  2030. wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Request elements",
  2031. pos, left - AES_BLOCK_SIZE);
  2032. if (wpa_fils_validate_key_confirm(sm, pos, left - AES_BLOCK_SIZE) < 0) {
  2033. wpa_printf(MSG_DEBUG, "FILS: Key Confirm validation failed");
  2034. return -1;
  2035. }
  2036. return left - AES_BLOCK_SIZE;
  2037. }
  2038. int fils_encrypt_assoc(struct wpa_state_machine *sm, u8 *buf,
  2039. size_t current_len, size_t max_len,
  2040. const struct wpabuf *hlp)
  2041. {
  2042. u8 *end = buf + max_len;
  2043. u8 *pos = buf + current_len;
  2044. struct ieee80211_mgmt *mgmt;
  2045. struct wpabuf *plain;
  2046. const u8 *aad[5];
  2047. size_t aad_len[5];
  2048. if (!sm || !sm->PTK_valid)
  2049. return -1;
  2050. wpa_hexdump(MSG_DEBUG,
  2051. "FILS: Association Response frame before FILS processing",
  2052. buf, current_len);
  2053. mgmt = (struct ieee80211_mgmt *) buf;
  2054. /* AES-SIV AAD vectors */
  2055. /* The AP's BSSID */
  2056. aad[0] = mgmt->sa;
  2057. aad_len[0] = ETH_ALEN;
  2058. /* The STA's MAC address */
  2059. aad[1] = mgmt->da;
  2060. aad_len[1] = ETH_ALEN;
  2061. /* The AP's nonce */
  2062. aad[2] = sm->ANonce;
  2063. aad_len[2] = FILS_NONCE_LEN;
  2064. /* The STA's nonce */
  2065. aad[3] = sm->SNonce;
  2066. aad_len[3] = FILS_NONCE_LEN;
  2067. /*
  2068. * The (Re)Association Response frame from the Capability Information
  2069. * field (the same offset in both Association and Reassociation
  2070. * Response frames) to the FILS Session element (both inclusive).
  2071. */
  2072. aad[4] = (const u8 *) &mgmt->u.assoc_resp.capab_info;
  2073. aad_len[4] = pos - aad[4];
  2074. /* The following elements will be encrypted with AES-SIV */
  2075. plain = fils_prepare_plainbuf(sm, hlp);
  2076. if (!plain) {
  2077. wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
  2078. return -1;
  2079. }
  2080. if (pos + wpabuf_len(plain) + AES_BLOCK_SIZE > end) {
  2081. wpa_printf(MSG_DEBUG,
  2082. "FILS: Not enough room for FILS elements");
  2083. wpabuf_free(plain);
  2084. return -1;
  2085. }
  2086. wpa_hexdump_buf_key(MSG_DEBUG, "FILS: Association Response plaintext",
  2087. plain);
  2088. if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len,
  2089. wpabuf_head(plain), wpabuf_len(plain),
  2090. 5, aad, aad_len, pos) < 0) {
  2091. wpabuf_free(plain);
  2092. return -1;
  2093. }
  2094. wpa_hexdump(MSG_DEBUG,
  2095. "FILS: Encrypted Association Response elements",
  2096. pos, AES_BLOCK_SIZE + wpabuf_len(plain));
  2097. current_len += wpabuf_len(plain) + AES_BLOCK_SIZE;
  2098. wpabuf_free(plain);
  2099. sm->fils_completed = 1;
  2100. return current_len;
  2101. }
  2102. static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm,
  2103. const struct wpabuf *hlp)
  2104. {
  2105. struct wpabuf *plain;
  2106. u8 *len, *tmp, *tmp2;
  2107. u8 hdr[2];
  2108. u8 *gtk, dummy_gtk[32];
  2109. size_t gtk_len;
  2110. struct wpa_group *gsm;
  2111. plain = wpabuf_alloc(1000);
  2112. if (!plain)
  2113. return NULL;
  2114. /* TODO: FILS Public Key */
  2115. /* FILS Key Confirmation */
  2116. wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
  2117. wpabuf_put_u8(plain, 1 + sm->fils_key_auth_len); /* Length */
  2118. /* Element ID Extension */
  2119. wpabuf_put_u8(plain, WLAN_EID_EXT_FILS_KEY_CONFIRM);
  2120. wpabuf_put_data(plain, sm->fils_key_auth_ap, sm->fils_key_auth_len);
  2121. /* FILS HLP Container */
  2122. if (hlp)
  2123. wpabuf_put_buf(plain, hlp);
  2124. /* TODO: FILS IP Address Assignment */
  2125. /* Key Delivery */
  2126. gsm = sm->group;
  2127. wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
  2128. len = wpabuf_put(plain, 1);
  2129. wpabuf_put_u8(plain, WLAN_EID_EXT_KEY_DELIVERY);
  2130. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN,
  2131. wpabuf_put(plain, WPA_KEY_RSC_LEN));
  2132. /* GTK KDE */
  2133. gtk = gsm->GTK[gsm->GN - 1];
  2134. gtk_len = gsm->GTK_len;
  2135. if (sm->wpa_auth->conf.disable_gtk) {
  2136. /*
  2137. * Provide unique random GTK to each STA to prevent use
  2138. * of GTK in the BSS.
  2139. */
  2140. if (random_get_bytes(dummy_gtk, gtk_len) < 0) {
  2141. wpabuf_free(plain);
  2142. return NULL;
  2143. }
  2144. gtk = dummy_gtk;
  2145. }
  2146. hdr[0] = gsm->GN & 0x03;
  2147. hdr[1] = 0;
  2148. tmp = wpabuf_put(plain, 0);
  2149. tmp2 = wpa_add_kde(tmp, RSN_KEY_DATA_GROUPKEY, hdr, 2,
  2150. gtk, gtk_len);
  2151. wpabuf_put(plain, tmp2 - tmp);
  2152. /* IGTK KDE */
  2153. tmp = wpabuf_put(plain, 0);
  2154. tmp2 = ieee80211w_kde_add(sm, tmp);
  2155. wpabuf_put(plain, tmp2 - tmp);
  2156. *len = (u8 *) wpabuf_put(plain, 0) - len - 1;
  2157. return plain;
  2158. }
  2159. int fils_set_tk(struct wpa_state_machine *sm)
  2160. {
  2161. enum wpa_alg alg;
  2162. int klen;
  2163. if (!sm || !sm->PTK_valid)
  2164. return -1;
  2165. alg = wpa_cipher_to_alg(sm->pairwise);
  2166. klen = wpa_cipher_key_len(sm->pairwise);
  2167. wpa_printf(MSG_DEBUG, "FILS: Configure TK to the driver");
  2168. if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
  2169. sm->PTK.tk, klen)) {
  2170. wpa_printf(MSG_DEBUG, "FILS: Failed to set TK to the driver");
  2171. return -1;
  2172. }
  2173. return 0;
  2174. }
  2175. u8 * hostapd_eid_assoc_fils_session(struct wpa_state_machine *sm, u8 *buf,
  2176. const u8 *fils_session)
  2177. {
  2178. struct wpabuf *plain;
  2179. u8 *pos = buf;
  2180. /* FILS Session */
  2181. *pos++ = WLAN_EID_EXTENSION; /* Element ID */
  2182. *pos++ = 1 + FILS_SESSION_LEN; /* Length */
  2183. *pos++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */
  2184. os_memcpy(pos, fils_session, FILS_SESSION_LEN);
  2185. pos += FILS_SESSION_LEN;
  2186. plain = fils_prepare_plainbuf(sm, NULL);
  2187. if (!plain) {
  2188. wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
  2189. return NULL;
  2190. }
  2191. os_memcpy(pos, wpabuf_head(plain), wpabuf_len(plain));
  2192. pos += wpabuf_len(plain);
  2193. wpa_printf(MSG_DEBUG, "%s: plain buf_len: %u", __func__,
  2194. (unsigned int) wpabuf_len(plain));
  2195. wpabuf_free(plain);
  2196. sm->fils_completed = 1;
  2197. return pos;
  2198. }
  2199. #endif /* CONFIG_FILS */
  2200. SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
  2201. {
  2202. struct wpa_authenticator *wpa_auth = sm->wpa_auth;
  2203. struct wpa_ptk PTK;
  2204. int ok = 0, psk_found = 0;
  2205. const u8 *pmk = NULL;
  2206. unsigned int pmk_len;
  2207. int ft;
  2208. const u8 *eapol_key_ie, *key_data, *mic;
  2209. u16 key_data_length;
  2210. size_t mic_len, eapol_key_ie_len;
  2211. struct ieee802_1x_hdr *hdr;
  2212. struct wpa_eapol_key *key;
  2213. struct wpa_eapol_ie_parse kde;
  2214. SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
  2215. sm->EAPOLKeyReceived = FALSE;
  2216. sm->update_snonce = FALSE;
  2217. os_memset(&PTK, 0, sizeof(PTK));
  2218. mic_len = wpa_mic_len(sm->wpa_key_mgmt);
  2219. /* WPA with IEEE 802.1X: use the derived PMK from EAP
  2220. * WPA-PSK: iterate through possible PSKs and select the one matching
  2221. * the packet */
  2222. for (;;) {
  2223. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  2224. pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
  2225. sm->p2p_dev_addr, pmk);
  2226. if (pmk == NULL)
  2227. break;
  2228. psk_found = 1;
  2229. pmk_len = PMK_LEN;
  2230. } else {
  2231. pmk = sm->PMK;
  2232. pmk_len = sm->pmk_len;
  2233. }
  2234. if (wpa_derive_ptk(sm, sm->SNonce, pmk, pmk_len, &PTK) < 0)
  2235. break;
  2236. if (mic_len &&
  2237. wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK,
  2238. sm->last_rx_eapol_key,
  2239. sm->last_rx_eapol_key_len) == 0) {
  2240. ok = 1;
  2241. break;
  2242. }
  2243. #ifdef CONFIG_FILS
  2244. if (!mic_len &&
  2245. wpa_aead_decrypt(sm, &PTK, sm->last_rx_eapol_key,
  2246. sm->last_rx_eapol_key_len, NULL) == 0) {
  2247. ok = 1;
  2248. break;
  2249. }
  2250. #endif /* CONFIG_FILS */
  2251. if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
  2252. break;
  2253. }
  2254. if (!ok) {
  2255. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2256. "invalid MIC in msg 2/4 of 4-Way Handshake");
  2257. if (psk_found)
  2258. wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr);
  2259. return;
  2260. }
  2261. /*
  2262. * Note: last_rx_eapol_key length fields have already been validated in
  2263. * wpa_receive().
  2264. */
  2265. hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
  2266. key = (struct wpa_eapol_key *) (hdr + 1);
  2267. mic = (u8 *) (key + 1);
  2268. key_data = mic + mic_len + 2;
  2269. key_data_length = WPA_GET_BE16(mic + mic_len);
  2270. if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
  2271. sizeof(*key) - mic_len - 2)
  2272. return;
  2273. if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
  2274. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  2275. "received EAPOL-Key msg 2/4 with invalid Key Data contents");
  2276. return;
  2277. }
  2278. if (kde.rsn_ie) {
  2279. eapol_key_ie = kde.rsn_ie;
  2280. eapol_key_ie_len = kde.rsn_ie_len;
  2281. } else if (kde.osen) {
  2282. eapol_key_ie = kde.osen;
  2283. eapol_key_ie_len = kde.osen_len;
  2284. } else {
  2285. eapol_key_ie = kde.wpa_ie;
  2286. eapol_key_ie_len = kde.wpa_ie_len;
  2287. }
  2288. ft = sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt);
  2289. if (sm->wpa_ie == NULL ||
  2290. wpa_compare_rsn_ie(ft, sm->wpa_ie, sm->wpa_ie_len,
  2291. eapol_key_ie, eapol_key_ie_len)) {
  2292. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  2293. "WPA IE from (Re)AssocReq did not match with msg 2/4");
  2294. if (sm->wpa_ie) {
  2295. wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
  2296. sm->wpa_ie, sm->wpa_ie_len);
  2297. }
  2298. wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
  2299. eapol_key_ie, eapol_key_ie_len);
  2300. /* MLME-DEAUTHENTICATE.request */
  2301. wpa_sta_disconnect(wpa_auth, sm->addr);
  2302. return;
  2303. }
  2304. #ifdef CONFIG_IEEE80211R_AP
  2305. if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
  2306. wpa_sta_disconnect(wpa_auth, sm->addr);
  2307. return;
  2308. }
  2309. #endif /* CONFIG_IEEE80211R_AP */
  2310. #ifdef CONFIG_P2P
  2311. if (kde.ip_addr_req && kde.ip_addr_req[0] &&
  2312. wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
  2313. int idx;
  2314. wpa_printf(MSG_DEBUG,
  2315. "P2P: IP address requested in EAPOL-Key exchange");
  2316. idx = bitfield_get_first_zero(wpa_auth->ip_pool);
  2317. if (idx >= 0) {
  2318. u32 start = WPA_GET_BE32(wpa_auth->conf.ip_addr_start);
  2319. bitfield_set(wpa_auth->ip_pool, idx);
  2320. WPA_PUT_BE32(sm->ip_addr, start + idx);
  2321. wpa_printf(MSG_DEBUG,
  2322. "P2P: Assigned IP address %u.%u.%u.%u to "
  2323. MACSTR, sm->ip_addr[0], sm->ip_addr[1],
  2324. sm->ip_addr[2], sm->ip_addr[3],
  2325. MAC2STR(sm->addr));
  2326. }
  2327. }
  2328. #endif /* CONFIG_P2P */
  2329. #ifdef CONFIG_IEEE80211R_AP
  2330. if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
  2331. /*
  2332. * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
  2333. * with the value we derived.
  2334. */
  2335. if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
  2336. WPA_PMK_NAME_LEN) != 0) {
  2337. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2338. "PMKR1Name mismatch in FT 4-way "
  2339. "handshake");
  2340. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
  2341. "Supplicant",
  2342. sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
  2343. wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
  2344. sm->pmk_r1_name, WPA_PMK_NAME_LEN);
  2345. return;
  2346. }
  2347. }
  2348. #endif /* CONFIG_IEEE80211R_AP */
  2349. sm->pending_1_of_4_timeout = 0;
  2350. eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
  2351. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  2352. /* PSK may have changed from the previous choice, so update
  2353. * state machine data based on whatever PSK was selected here.
  2354. */
  2355. os_memcpy(sm->PMK, pmk, PMK_LEN);
  2356. sm->pmk_len = PMK_LEN;
  2357. }
  2358. sm->MICVerified = TRUE;
  2359. os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
  2360. sm->PTK_valid = TRUE;
  2361. }
  2362. SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
  2363. {
  2364. SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
  2365. sm->TimeoutCtr = 0;
  2366. }
  2367. #ifdef CONFIG_IEEE80211W
  2368. static int ieee80211w_kde_len(struct wpa_state_machine *sm)
  2369. {
  2370. if (sm->mgmt_frame_prot) {
  2371. size_t len;
  2372. len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
  2373. return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
  2374. }
  2375. return 0;
  2376. }
  2377. static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
  2378. {
  2379. struct wpa_igtk_kde igtk;
  2380. struct wpa_group *gsm = sm->group;
  2381. u8 rsc[WPA_KEY_RSC_LEN];
  2382. size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
  2383. if (!sm->mgmt_frame_prot)
  2384. return pos;
  2385. igtk.keyid[0] = gsm->GN_igtk;
  2386. igtk.keyid[1] = 0;
  2387. if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
  2388. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
  2389. os_memset(igtk.pn, 0, sizeof(igtk.pn));
  2390. else
  2391. os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
  2392. os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
  2393. if (sm->wpa_auth->conf.disable_gtk) {
  2394. /*
  2395. * Provide unique random IGTK to each STA to prevent use of
  2396. * IGTK in the BSS.
  2397. */
  2398. if (random_get_bytes(igtk.igtk, len) < 0)
  2399. return pos;
  2400. }
  2401. pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
  2402. (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
  2403. NULL, 0);
  2404. return pos;
  2405. }
  2406. #else /* CONFIG_IEEE80211W */
  2407. static int ieee80211w_kde_len(struct wpa_state_machine *sm)
  2408. {
  2409. return 0;
  2410. }
  2411. static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
  2412. {
  2413. return pos;
  2414. }
  2415. #endif /* CONFIG_IEEE80211W */
  2416. SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
  2417. {
  2418. u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
  2419. size_t gtk_len, kde_len;
  2420. struct wpa_group *gsm = sm->group;
  2421. u8 *wpa_ie;
  2422. int wpa_ie_len, secure, keyidx, encr = 0;
  2423. SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
  2424. sm->TimeoutEvt = FALSE;
  2425. sm->TimeoutCtr++;
  2426. if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
  2427. /* No point in sending the EAPOL-Key - we will disconnect
  2428. * immediately following this. */
  2429. return;
  2430. }
  2431. /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
  2432. GTK[GN], IGTK, [FTIE], [TIE * 2])
  2433. */
  2434. os_memset(rsc, 0, WPA_KEY_RSC_LEN);
  2435. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
  2436. /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
  2437. wpa_ie = sm->wpa_auth->wpa_ie;
  2438. wpa_ie_len = sm->wpa_auth->wpa_ie_len;
  2439. if (sm->wpa == WPA_VERSION_WPA &&
  2440. (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
  2441. wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
  2442. /* WPA-only STA, remove RSN IE and possible MDIE */
  2443. wpa_ie = wpa_ie + wpa_ie[1] + 2;
  2444. if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
  2445. wpa_ie = wpa_ie + wpa_ie[1] + 2;
  2446. wpa_ie_len = wpa_ie[1] + 2;
  2447. }
  2448. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2449. "sending 3/4 msg of 4-Way Handshake");
  2450. if (sm->wpa == WPA_VERSION_WPA2) {
  2451. /* WPA2 send GTK in the 4-way handshake */
  2452. secure = 1;
  2453. gtk = gsm->GTK[gsm->GN - 1];
  2454. gtk_len = gsm->GTK_len;
  2455. if (sm->wpa_auth->conf.disable_gtk) {
  2456. /*
  2457. * Provide unique random GTK to each STA to prevent use
  2458. * of GTK in the BSS.
  2459. */
  2460. if (random_get_bytes(dummy_gtk, gtk_len) < 0)
  2461. return;
  2462. gtk = dummy_gtk;
  2463. }
  2464. keyidx = gsm->GN;
  2465. _rsc = rsc;
  2466. encr = 1;
  2467. } else {
  2468. /* WPA does not include GTK in msg 3/4 */
  2469. secure = 0;
  2470. gtk = NULL;
  2471. gtk_len = 0;
  2472. keyidx = 0;
  2473. _rsc = NULL;
  2474. if (sm->rx_eapol_key_secure) {
  2475. /*
  2476. * It looks like Windows 7 supplicant tries to use
  2477. * Secure bit in msg 2/4 after having reported Michael
  2478. * MIC failure and it then rejects the 4-way handshake
  2479. * if msg 3/4 does not set Secure bit. Work around this
  2480. * by setting the Secure bit here even in the case of
  2481. * WPA if the supplicant used it first.
  2482. */
  2483. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2484. "STA used Secure bit in WPA msg 2/4 - "
  2485. "set Secure for 3/4 as workaround");
  2486. secure = 1;
  2487. }
  2488. }
  2489. kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
  2490. if (gtk)
  2491. kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
  2492. #ifdef CONFIG_IEEE80211R_AP
  2493. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
  2494. kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
  2495. kde_len += 300; /* FTIE + 2 * TIE */
  2496. }
  2497. #endif /* CONFIG_IEEE80211R_AP */
  2498. #ifdef CONFIG_P2P
  2499. if (WPA_GET_BE32(sm->ip_addr) > 0)
  2500. kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
  2501. #endif /* CONFIG_P2P */
  2502. kde = os_malloc(kde_len);
  2503. if (kde == NULL)
  2504. return;
  2505. pos = kde;
  2506. os_memcpy(pos, wpa_ie, wpa_ie_len);
  2507. pos += wpa_ie_len;
  2508. #ifdef CONFIG_IEEE80211R_AP
  2509. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
  2510. int res;
  2511. size_t elen;
  2512. elen = pos - kde;
  2513. res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
  2514. if (res < 0) {
  2515. wpa_printf(MSG_ERROR, "FT: Failed to insert "
  2516. "PMKR1Name into RSN IE in EAPOL-Key data");
  2517. os_free(kde);
  2518. return;
  2519. }
  2520. pos -= wpa_ie_len;
  2521. pos += elen;
  2522. }
  2523. #endif /* CONFIG_IEEE80211R_AP */
  2524. if (gtk) {
  2525. u8 hdr[2];
  2526. hdr[0] = keyidx & 0x03;
  2527. hdr[1] = 0;
  2528. pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
  2529. gtk, gtk_len);
  2530. }
  2531. pos = ieee80211w_kde_add(sm, pos);
  2532. #ifdef CONFIG_IEEE80211R_AP
  2533. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
  2534. int res;
  2535. struct wpa_auth_config *conf;
  2536. conf = &sm->wpa_auth->conf;
  2537. if (sm->assoc_resp_ftie &&
  2538. kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
  2539. os_memcpy(pos, sm->assoc_resp_ftie,
  2540. 2 + sm->assoc_resp_ftie[1]);
  2541. res = 2 + sm->assoc_resp_ftie[1];
  2542. } else {
  2543. res = wpa_write_ftie(conf, conf->r0_key_holder,
  2544. conf->r0_key_holder_len,
  2545. NULL, NULL, pos,
  2546. kde + kde_len - pos,
  2547. NULL, 0);
  2548. }
  2549. if (res < 0) {
  2550. wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
  2551. "into EAPOL-Key Key Data");
  2552. os_free(kde);
  2553. return;
  2554. }
  2555. pos += res;
  2556. /* TIE[ReassociationDeadline] (TU) */
  2557. *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
  2558. *pos++ = 5;
  2559. *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
  2560. WPA_PUT_LE32(pos, conf->reassociation_deadline);
  2561. pos += 4;
  2562. /* TIE[KeyLifetime] (seconds) */
  2563. *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
  2564. *pos++ = 5;
  2565. *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
  2566. WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60);
  2567. pos += 4;
  2568. }
  2569. #endif /* CONFIG_IEEE80211R_AP */
  2570. #ifdef CONFIG_P2P
  2571. if (WPA_GET_BE32(sm->ip_addr) > 0) {
  2572. u8 addr[3 * 4];
  2573. os_memcpy(addr, sm->ip_addr, 4);
  2574. os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
  2575. os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
  2576. pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
  2577. addr, sizeof(addr), NULL, 0);
  2578. }
  2579. #endif /* CONFIG_P2P */
  2580. wpa_send_eapol(sm->wpa_auth, sm,
  2581. (secure ? WPA_KEY_INFO_SECURE : 0) |
  2582. (wpa_mic_len(sm->wpa_key_mgmt) ? WPA_KEY_INFO_MIC : 0) |
  2583. WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
  2584. WPA_KEY_INFO_KEY_TYPE,
  2585. _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
  2586. os_free(kde);
  2587. }
  2588. SM_STATE(WPA_PTK, PTKINITDONE)
  2589. {
  2590. SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
  2591. sm->EAPOLKeyReceived = FALSE;
  2592. if (sm->Pair) {
  2593. enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
  2594. int klen = wpa_cipher_key_len(sm->pairwise);
  2595. if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
  2596. sm->PTK.tk, klen)) {
  2597. wpa_sta_disconnect(sm->wpa_auth, sm->addr);
  2598. return;
  2599. }
  2600. /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
  2601. sm->pairwise_set = TRUE;
  2602. if (sm->wpa_auth->conf.wpa_ptk_rekey) {
  2603. eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
  2604. eloop_register_timeout(sm->wpa_auth->conf.
  2605. wpa_ptk_rekey, 0, wpa_rekey_ptk,
  2606. sm->wpa_auth, sm);
  2607. }
  2608. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
  2609. sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
  2610. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  2611. WPA_EAPOL_authorized, 1);
  2612. }
  2613. }
  2614. if (0 /* IBSS == TRUE */) {
  2615. sm->keycount++;
  2616. if (sm->keycount == 2) {
  2617. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  2618. WPA_EAPOL_portValid, 1);
  2619. }
  2620. } else {
  2621. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
  2622. 1);
  2623. }
  2624. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
  2625. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
  2626. if (sm->wpa == WPA_VERSION_WPA)
  2627. sm->PInitAKeys = TRUE;
  2628. else
  2629. sm->has_GTK = TRUE;
  2630. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  2631. "pairwise key handshake completed (%s)",
  2632. sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
  2633. #ifdef CONFIG_IEEE80211R_AP
  2634. wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
  2635. #endif /* CONFIG_IEEE80211R_AP */
  2636. }
  2637. SM_STEP(WPA_PTK)
  2638. {
  2639. struct wpa_authenticator *wpa_auth = sm->wpa_auth;
  2640. if (sm->Init)
  2641. SM_ENTER(WPA_PTK, INITIALIZE);
  2642. else if (sm->Disconnect
  2643. /* || FIX: dot11RSNAConfigSALifetime timeout */) {
  2644. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  2645. "WPA_PTK: sm->Disconnect");
  2646. SM_ENTER(WPA_PTK, DISCONNECT);
  2647. }
  2648. else if (sm->DeauthenticationRequest)
  2649. SM_ENTER(WPA_PTK, DISCONNECTED);
  2650. else if (sm->AuthenticationRequest)
  2651. SM_ENTER(WPA_PTK, AUTHENTICATION);
  2652. else if (sm->ReAuthenticationRequest)
  2653. SM_ENTER(WPA_PTK, AUTHENTICATION2);
  2654. else if (sm->PTKRequest)
  2655. SM_ENTER(WPA_PTK, PTKSTART);
  2656. else switch (sm->wpa_ptk_state) {
  2657. case WPA_PTK_INITIALIZE:
  2658. break;
  2659. case WPA_PTK_DISCONNECT:
  2660. SM_ENTER(WPA_PTK, DISCONNECTED);
  2661. break;
  2662. case WPA_PTK_DISCONNECTED:
  2663. SM_ENTER(WPA_PTK, INITIALIZE);
  2664. break;
  2665. case WPA_PTK_AUTHENTICATION:
  2666. SM_ENTER(WPA_PTK, AUTHENTICATION2);
  2667. break;
  2668. case WPA_PTK_AUTHENTICATION2:
  2669. if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
  2670. wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
  2671. WPA_EAPOL_keyRun) > 0)
  2672. SM_ENTER(WPA_PTK, INITPMK);
  2673. else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
  2674. sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE
  2675. /* FIX: && 802.1X::keyRun */)
  2676. SM_ENTER(WPA_PTK, INITPSK);
  2677. break;
  2678. case WPA_PTK_INITPMK:
  2679. if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
  2680. WPA_EAPOL_keyAvailable) > 0)
  2681. SM_ENTER(WPA_PTK, PTKSTART);
  2682. else {
  2683. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  2684. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  2685. "INITPMK - keyAvailable = false");
  2686. SM_ENTER(WPA_PTK, DISCONNECT);
  2687. }
  2688. break;
  2689. case WPA_PTK_INITPSK:
  2690. if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
  2691. NULL))
  2692. SM_ENTER(WPA_PTK, PTKSTART);
  2693. else {
  2694. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  2695. "no PSK configured for the STA");
  2696. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  2697. SM_ENTER(WPA_PTK, DISCONNECT);
  2698. }
  2699. break;
  2700. case WPA_PTK_PTKSTART:
  2701. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  2702. sm->EAPOLKeyPairwise)
  2703. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
  2704. else if (sm->TimeoutCtr >
  2705. sm->wpa_auth->conf.wpa_pairwise_update_count) {
  2706. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  2707. wpa_auth_vlogger(
  2708. sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2709. "PTKSTART: Retry limit %u reached",
  2710. sm->wpa_auth->conf.wpa_pairwise_update_count);
  2711. SM_ENTER(WPA_PTK, DISCONNECT);
  2712. } else if (sm->TimeoutEvt)
  2713. SM_ENTER(WPA_PTK, PTKSTART);
  2714. break;
  2715. case WPA_PTK_PTKCALCNEGOTIATING:
  2716. if (sm->MICVerified)
  2717. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
  2718. else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  2719. sm->EAPOLKeyPairwise)
  2720. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
  2721. else if (sm->TimeoutEvt)
  2722. SM_ENTER(WPA_PTK, PTKSTART);
  2723. break;
  2724. case WPA_PTK_PTKCALCNEGOTIATING2:
  2725. SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
  2726. break;
  2727. case WPA_PTK_PTKINITNEGOTIATING:
  2728. if (sm->update_snonce)
  2729. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
  2730. else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  2731. sm->EAPOLKeyPairwise && sm->MICVerified)
  2732. SM_ENTER(WPA_PTK, PTKINITDONE);
  2733. else if (sm->TimeoutCtr >
  2734. sm->wpa_auth->conf.wpa_pairwise_update_count) {
  2735. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  2736. wpa_auth_vlogger(
  2737. sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2738. "PTKINITNEGOTIATING: Retry limit %u reached",
  2739. sm->wpa_auth->conf.wpa_pairwise_update_count);
  2740. SM_ENTER(WPA_PTK, DISCONNECT);
  2741. } else if (sm->TimeoutEvt)
  2742. SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
  2743. break;
  2744. case WPA_PTK_PTKINITDONE:
  2745. break;
  2746. }
  2747. }
  2748. SM_STATE(WPA_PTK_GROUP, IDLE)
  2749. {
  2750. SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
  2751. if (sm->Init) {
  2752. /* Init flag is not cleared here, so avoid busy
  2753. * loop by claiming nothing changed. */
  2754. sm->changed = FALSE;
  2755. }
  2756. sm->GTimeoutCtr = 0;
  2757. }
  2758. SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
  2759. {
  2760. u8 rsc[WPA_KEY_RSC_LEN];
  2761. struct wpa_group *gsm = sm->group;
  2762. const u8 *kde;
  2763. u8 *kde_buf = NULL, *pos, hdr[2];
  2764. size_t kde_len;
  2765. u8 *gtk, dummy_gtk[32];
  2766. SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
  2767. sm->GTimeoutCtr++;
  2768. if (sm->GTimeoutCtr > sm->wpa_auth->conf.wpa_group_update_count) {
  2769. /* No point in sending the EAPOL-Key - we will disconnect
  2770. * immediately following this. */
  2771. return;
  2772. }
  2773. if (sm->wpa == WPA_VERSION_WPA)
  2774. sm->PInitAKeys = FALSE;
  2775. sm->TimeoutEvt = FALSE;
  2776. /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
  2777. os_memset(rsc, 0, WPA_KEY_RSC_LEN);
  2778. if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
  2779. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
  2780. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2781. "sending 1/2 msg of Group Key Handshake");
  2782. gtk = gsm->GTK[gsm->GN - 1];
  2783. if (sm->wpa_auth->conf.disable_gtk) {
  2784. /*
  2785. * Provide unique random GTK to each STA to prevent use
  2786. * of GTK in the BSS.
  2787. */
  2788. if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
  2789. return;
  2790. gtk = dummy_gtk;
  2791. }
  2792. if (sm->wpa == WPA_VERSION_WPA2) {
  2793. kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
  2794. ieee80211w_kde_len(sm);
  2795. kde_buf = os_malloc(kde_len);
  2796. if (kde_buf == NULL)
  2797. return;
  2798. kde = pos = kde_buf;
  2799. hdr[0] = gsm->GN & 0x03;
  2800. hdr[1] = 0;
  2801. pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
  2802. gtk, gsm->GTK_len);
  2803. pos = ieee80211w_kde_add(sm, pos);
  2804. kde_len = pos - kde;
  2805. } else {
  2806. kde = gtk;
  2807. kde_len = gsm->GTK_len;
  2808. }
  2809. wpa_send_eapol(sm->wpa_auth, sm,
  2810. WPA_KEY_INFO_SECURE |
  2811. (wpa_mic_len(sm->wpa_key_mgmt) ? WPA_KEY_INFO_MIC : 0) |
  2812. WPA_KEY_INFO_ACK |
  2813. (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
  2814. rsc, NULL, kde, kde_len, gsm->GN, 1);
  2815. os_free(kde_buf);
  2816. }
  2817. SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
  2818. {
  2819. SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
  2820. sm->EAPOLKeyReceived = FALSE;
  2821. if (sm->GUpdateStationKeys)
  2822. sm->group->GKeyDoneStations--;
  2823. sm->GUpdateStationKeys = FALSE;
  2824. sm->GTimeoutCtr = 0;
  2825. /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
  2826. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  2827. "group key handshake completed (%s)",
  2828. sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
  2829. sm->has_GTK = TRUE;
  2830. }
  2831. SM_STATE(WPA_PTK_GROUP, KEYERROR)
  2832. {
  2833. SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
  2834. if (sm->GUpdateStationKeys)
  2835. sm->group->GKeyDoneStations--;
  2836. sm->GUpdateStationKeys = FALSE;
  2837. sm->Disconnect = TRUE;
  2838. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  2839. "group key handshake failed (%s) after %u tries",
  2840. sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN",
  2841. sm->wpa_auth->conf.wpa_group_update_count);
  2842. }
  2843. SM_STEP(WPA_PTK_GROUP)
  2844. {
  2845. if (sm->Init || sm->PtkGroupInit) {
  2846. SM_ENTER(WPA_PTK_GROUP, IDLE);
  2847. sm->PtkGroupInit = FALSE;
  2848. } else switch (sm->wpa_ptk_group_state) {
  2849. case WPA_PTK_GROUP_IDLE:
  2850. if (sm->GUpdateStationKeys ||
  2851. (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
  2852. SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
  2853. break;
  2854. case WPA_PTK_GROUP_REKEYNEGOTIATING:
  2855. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  2856. !sm->EAPOLKeyPairwise && sm->MICVerified)
  2857. SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
  2858. else if (sm->GTimeoutCtr >
  2859. sm->wpa_auth->conf.wpa_group_update_count)
  2860. SM_ENTER(WPA_PTK_GROUP, KEYERROR);
  2861. else if (sm->TimeoutEvt)
  2862. SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
  2863. break;
  2864. case WPA_PTK_GROUP_KEYERROR:
  2865. SM_ENTER(WPA_PTK_GROUP, IDLE);
  2866. break;
  2867. case WPA_PTK_GROUP_REKEYESTABLISHED:
  2868. SM_ENTER(WPA_PTK_GROUP, IDLE);
  2869. break;
  2870. }
  2871. }
  2872. static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
  2873. struct wpa_group *group)
  2874. {
  2875. int ret = 0;
  2876. os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
  2877. inc_byte_array(group->Counter, WPA_NONCE_LEN);
  2878. if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
  2879. wpa_auth->addr, group->GNonce,
  2880. group->GTK[group->GN - 1], group->GTK_len) < 0)
  2881. ret = -1;
  2882. wpa_hexdump_key(MSG_DEBUG, "GTK",
  2883. group->GTK[group->GN - 1], group->GTK_len);
  2884. #ifdef CONFIG_IEEE80211W
  2885. if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
  2886. size_t len;
  2887. len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
  2888. os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
  2889. inc_byte_array(group->Counter, WPA_NONCE_LEN);
  2890. if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
  2891. wpa_auth->addr, group->GNonce,
  2892. group->IGTK[group->GN_igtk - 4], len) < 0)
  2893. ret = -1;
  2894. wpa_hexdump_key(MSG_DEBUG, "IGTK",
  2895. group->IGTK[group->GN_igtk - 4], len);
  2896. }
  2897. #endif /* CONFIG_IEEE80211W */
  2898. return ret;
  2899. }
  2900. static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
  2901. struct wpa_group *group)
  2902. {
  2903. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  2904. "GTK_INIT (VLAN-ID %d)", group->vlan_id);
  2905. group->changed = FALSE; /* GInit is not cleared here; avoid loop */
  2906. group->wpa_group_state = WPA_GROUP_GTK_INIT;
  2907. /* GTK[0..N] = 0 */
  2908. os_memset(group->GTK, 0, sizeof(group->GTK));
  2909. group->GN = 1;
  2910. group->GM = 2;
  2911. #ifdef CONFIG_IEEE80211W
  2912. group->GN_igtk = 4;
  2913. group->GM_igtk = 5;
  2914. #endif /* CONFIG_IEEE80211W */
  2915. /* GTK[GN] = CalcGTK() */
  2916. wpa_gtk_update(wpa_auth, group);
  2917. }
  2918. static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
  2919. {
  2920. if (ctx != NULL && ctx != sm->group)
  2921. return 0;
  2922. if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
  2923. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2924. "Not in PTKINITDONE; skip Group Key update");
  2925. sm->GUpdateStationKeys = FALSE;
  2926. return 0;
  2927. }
  2928. if (sm->GUpdateStationKeys) {
  2929. /*
  2930. * This should not really happen, so add a debug log entry.
  2931. * Since we clear the GKeyDoneStations before the loop, the
  2932. * station needs to be counted here anyway.
  2933. */
  2934. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2935. "GUpdateStationKeys was already set when "
  2936. "marking station for GTK rekeying");
  2937. }
  2938. /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
  2939. if (sm->is_wnmsleep)
  2940. return 0;
  2941. sm->group->GKeyDoneStations++;
  2942. sm->GUpdateStationKeys = TRUE;
  2943. wpa_sm_step(sm);
  2944. return 0;
  2945. }
  2946. #ifdef CONFIG_WNM
  2947. /* update GTK when exiting WNM-Sleep Mode */
  2948. void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
  2949. {
  2950. if (sm == NULL || sm->is_wnmsleep)
  2951. return;
  2952. wpa_group_update_sta(sm, NULL);
  2953. }
  2954. void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
  2955. {
  2956. if (sm)
  2957. sm->is_wnmsleep = !!flag;
  2958. }
  2959. int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
  2960. {
  2961. struct wpa_group *gsm = sm->group;
  2962. u8 *start = pos;
  2963. /*
  2964. * GTK subelement:
  2965. * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
  2966. * Key[5..32]
  2967. */
  2968. *pos++ = WNM_SLEEP_SUBELEM_GTK;
  2969. *pos++ = 11 + gsm->GTK_len;
  2970. /* Key ID in B0-B1 of Key Info */
  2971. WPA_PUT_LE16(pos, gsm->GN & 0x03);
  2972. pos += 2;
  2973. *pos++ = gsm->GTK_len;
  2974. if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
  2975. return 0;
  2976. pos += 8;
  2977. os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
  2978. pos += gsm->GTK_len;
  2979. wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
  2980. gsm->GN);
  2981. wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
  2982. gsm->GTK[gsm->GN - 1], gsm->GTK_len);
  2983. return pos - start;
  2984. }
  2985. #ifdef CONFIG_IEEE80211W
  2986. int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
  2987. {
  2988. struct wpa_group *gsm = sm->group;
  2989. u8 *start = pos;
  2990. size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
  2991. /*
  2992. * IGTK subelement:
  2993. * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
  2994. */
  2995. *pos++ = WNM_SLEEP_SUBELEM_IGTK;
  2996. *pos++ = 2 + 6 + len;
  2997. WPA_PUT_LE16(pos, gsm->GN_igtk);
  2998. pos += 2;
  2999. if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
  3000. return 0;
  3001. pos += 6;
  3002. os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
  3003. pos += len;
  3004. wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
  3005. gsm->GN_igtk);
  3006. wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
  3007. gsm->IGTK[gsm->GN_igtk - 4], len);
  3008. return pos - start;
  3009. }
  3010. #endif /* CONFIG_IEEE80211W */
  3011. #endif /* CONFIG_WNM */
  3012. static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
  3013. struct wpa_group *group)
  3014. {
  3015. int tmp;
  3016. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  3017. "SETKEYS (VLAN-ID %d)", group->vlan_id);
  3018. group->changed = TRUE;
  3019. group->wpa_group_state = WPA_GROUP_SETKEYS;
  3020. group->GTKReKey = FALSE;
  3021. tmp = group->GM;
  3022. group->GM = group->GN;
  3023. group->GN = tmp;
  3024. #ifdef CONFIG_IEEE80211W
  3025. tmp = group->GM_igtk;
  3026. group->GM_igtk = group->GN_igtk;
  3027. group->GN_igtk = tmp;
  3028. #endif /* CONFIG_IEEE80211W */
  3029. /* "GKeyDoneStations = GNoStations" is done in more robust way by
  3030. * counting the STAs that are marked with GUpdateStationKeys instead of
  3031. * including all STAs that could be in not-yet-completed state. */
  3032. wpa_gtk_update(wpa_auth, group);
  3033. if (group->GKeyDoneStations) {
  3034. wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
  3035. "GKeyDoneStations=%d when starting new GTK rekey",
  3036. group->GKeyDoneStations);
  3037. group->GKeyDoneStations = 0;
  3038. }
  3039. wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
  3040. wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
  3041. group->GKeyDoneStations);
  3042. }
  3043. static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
  3044. struct wpa_group *group)
  3045. {
  3046. int ret = 0;
  3047. if (wpa_auth_set_key(wpa_auth, group->vlan_id,
  3048. wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
  3049. broadcast_ether_addr, group->GN,
  3050. group->GTK[group->GN - 1], group->GTK_len) < 0)
  3051. ret = -1;
  3052. #ifdef CONFIG_IEEE80211W
  3053. if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
  3054. enum wpa_alg alg;
  3055. size_t len;
  3056. alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
  3057. len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
  3058. if (ret == 0 &&
  3059. wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
  3060. broadcast_ether_addr, group->GN_igtk,
  3061. group->IGTK[group->GN_igtk - 4], len) < 0)
  3062. ret = -1;
  3063. }
  3064. #endif /* CONFIG_IEEE80211W */
  3065. return ret;
  3066. }
  3067. static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
  3068. {
  3069. if (sm->group == ctx) {
  3070. wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
  3071. " for discconnection due to fatal failure",
  3072. MAC2STR(sm->addr));
  3073. sm->Disconnect = TRUE;
  3074. }
  3075. return 0;
  3076. }
  3077. static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
  3078. struct wpa_group *group)
  3079. {
  3080. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
  3081. group->changed = TRUE;
  3082. group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
  3083. wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
  3084. }
  3085. static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
  3086. struct wpa_group *group)
  3087. {
  3088. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  3089. "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
  3090. group->changed = TRUE;
  3091. group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
  3092. if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
  3093. wpa_group_fatal_failure(wpa_auth, group);
  3094. return -1;
  3095. }
  3096. return 0;
  3097. }
  3098. static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
  3099. struct wpa_group *group)
  3100. {
  3101. if (group->GInit) {
  3102. wpa_group_gtk_init(wpa_auth, group);
  3103. } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
  3104. /* Do not allow group operations */
  3105. } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
  3106. group->GTKAuthenticator) {
  3107. wpa_group_setkeysdone(wpa_auth, group);
  3108. } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
  3109. group->GTKReKey) {
  3110. wpa_group_setkeys(wpa_auth, group);
  3111. } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
  3112. if (group->GKeyDoneStations == 0)
  3113. wpa_group_setkeysdone(wpa_auth, group);
  3114. else if (group->GTKReKey)
  3115. wpa_group_setkeys(wpa_auth, group);
  3116. }
  3117. }
  3118. static int wpa_sm_step(struct wpa_state_machine *sm)
  3119. {
  3120. if (sm == NULL)
  3121. return 0;
  3122. if (sm->in_step_loop) {
  3123. /* This should not happen, but if it does, make sure we do not
  3124. * end up freeing the state machine too early by exiting the
  3125. * recursive call. */
  3126. wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
  3127. return 0;
  3128. }
  3129. sm->in_step_loop = 1;
  3130. do {
  3131. if (sm->pending_deinit)
  3132. break;
  3133. sm->changed = FALSE;
  3134. sm->wpa_auth->group->changed = FALSE;
  3135. SM_STEP_RUN(WPA_PTK);
  3136. if (sm->pending_deinit)
  3137. break;
  3138. SM_STEP_RUN(WPA_PTK_GROUP);
  3139. if (sm->pending_deinit)
  3140. break;
  3141. wpa_group_sm_step(sm->wpa_auth, sm->group);
  3142. } while (sm->changed || sm->wpa_auth->group->changed);
  3143. sm->in_step_loop = 0;
  3144. if (sm->pending_deinit) {
  3145. wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
  3146. "machine deinit for " MACSTR, MAC2STR(sm->addr));
  3147. wpa_free_sta_sm(sm);
  3148. return 1;
  3149. }
  3150. return 0;
  3151. }
  3152. static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
  3153. {
  3154. struct wpa_state_machine *sm = eloop_ctx;
  3155. wpa_sm_step(sm);
  3156. }
  3157. void wpa_auth_sm_notify(struct wpa_state_machine *sm)
  3158. {
  3159. if (sm == NULL)
  3160. return;
  3161. eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
  3162. }
  3163. void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
  3164. {
  3165. int tmp, i;
  3166. struct wpa_group *group;
  3167. if (wpa_auth == NULL)
  3168. return;
  3169. group = wpa_auth->group;
  3170. for (i = 0; i < 2; i++) {
  3171. tmp = group->GM;
  3172. group->GM = group->GN;
  3173. group->GN = tmp;
  3174. #ifdef CONFIG_IEEE80211W
  3175. tmp = group->GM_igtk;
  3176. group->GM_igtk = group->GN_igtk;
  3177. group->GN_igtk = tmp;
  3178. #endif /* CONFIG_IEEE80211W */
  3179. wpa_gtk_update(wpa_auth, group);
  3180. wpa_group_config_group_keys(wpa_auth, group);
  3181. }
  3182. }
  3183. static const char * wpa_bool_txt(int val)
  3184. {
  3185. return val ? "TRUE" : "FALSE";
  3186. }
  3187. #define RSN_SUITE "%02x-%02x-%02x-%d"
  3188. #define RSN_SUITE_ARG(s) \
  3189. ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
  3190. int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
  3191. {
  3192. int len = 0, ret;
  3193. char pmkid_txt[PMKID_LEN * 2 + 1];
  3194. #ifdef CONFIG_RSN_PREAUTH
  3195. const int preauth = 1;
  3196. #else /* CONFIG_RSN_PREAUTH */
  3197. const int preauth = 0;
  3198. #endif /* CONFIG_RSN_PREAUTH */
  3199. if (wpa_auth == NULL)
  3200. return len;
  3201. ret = os_snprintf(buf + len, buflen - len,
  3202. "dot11RSNAOptionImplemented=TRUE\n"
  3203. "dot11RSNAPreauthenticationImplemented=%s\n"
  3204. "dot11RSNAEnabled=%s\n"
  3205. "dot11RSNAPreauthenticationEnabled=%s\n",
  3206. wpa_bool_txt(preauth),
  3207. wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
  3208. wpa_bool_txt(wpa_auth->conf.rsn_preauth));
  3209. if (os_snprintf_error(buflen - len, ret))
  3210. return len;
  3211. len += ret;
  3212. wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
  3213. wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
  3214. ret = os_snprintf(
  3215. buf + len, buflen - len,
  3216. "dot11RSNAConfigVersion=%u\n"
  3217. "dot11RSNAConfigPairwiseKeysSupported=9999\n"
  3218. /* FIX: dot11RSNAConfigGroupCipher */
  3219. /* FIX: dot11RSNAConfigGroupRekeyMethod */
  3220. /* FIX: dot11RSNAConfigGroupRekeyTime */
  3221. /* FIX: dot11RSNAConfigGroupRekeyPackets */
  3222. "dot11RSNAConfigGroupRekeyStrict=%u\n"
  3223. "dot11RSNAConfigGroupUpdateCount=%u\n"
  3224. "dot11RSNAConfigPairwiseUpdateCount=%u\n"
  3225. "dot11RSNAConfigGroupCipherSize=%u\n"
  3226. "dot11RSNAConfigPMKLifetime=%u\n"
  3227. "dot11RSNAConfigPMKReauthThreshold=%u\n"
  3228. "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
  3229. "dot11RSNAConfigSATimeout=%u\n"
  3230. "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
  3231. "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
  3232. "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
  3233. "dot11RSNAPMKIDUsed=%s\n"
  3234. "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
  3235. "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
  3236. "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
  3237. "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
  3238. "dot11RSNA4WayHandshakeFailures=%u\n"
  3239. "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
  3240. RSN_VERSION,
  3241. !!wpa_auth->conf.wpa_strict_rekey,
  3242. wpa_auth->conf.wpa_group_update_count,
  3243. wpa_auth->conf.wpa_pairwise_update_count,
  3244. wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
  3245. dot11RSNAConfigPMKLifetime,
  3246. dot11RSNAConfigPMKReauthThreshold,
  3247. dot11RSNAConfigSATimeout,
  3248. RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
  3249. RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
  3250. RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
  3251. pmkid_txt,
  3252. RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
  3253. RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
  3254. RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
  3255. wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
  3256. wpa_auth->dot11RSNA4WayHandshakeFailures);
  3257. if (os_snprintf_error(buflen - len, ret))
  3258. return len;
  3259. len += ret;
  3260. /* TODO: dot11RSNAConfigPairwiseCiphersTable */
  3261. /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
  3262. /* Private MIB */
  3263. ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
  3264. wpa_auth->group->wpa_group_state);
  3265. if (os_snprintf_error(buflen - len, ret))
  3266. return len;
  3267. len += ret;
  3268. return len;
  3269. }
  3270. int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
  3271. {
  3272. int len = 0, ret;
  3273. u32 pairwise = 0;
  3274. if (sm == NULL)
  3275. return 0;
  3276. /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
  3277. /* dot11RSNAStatsEntry */
  3278. pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
  3279. WPA_PROTO_RSN : WPA_PROTO_WPA,
  3280. sm->pairwise);
  3281. if (pairwise == 0)
  3282. return 0;
  3283. ret = os_snprintf(
  3284. buf + len, buflen - len,
  3285. /* TODO: dot11RSNAStatsIndex */
  3286. "dot11RSNAStatsSTAAddress=" MACSTR "\n"
  3287. "dot11RSNAStatsVersion=1\n"
  3288. "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
  3289. /* TODO: dot11RSNAStatsTKIPICVErrors */
  3290. "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
  3291. "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
  3292. /* TODO: dot11RSNAStatsCCMPReplays */
  3293. /* TODO: dot11RSNAStatsCCMPDecryptErrors */
  3294. /* TODO: dot11RSNAStatsTKIPReplays */,
  3295. MAC2STR(sm->addr),
  3296. RSN_SUITE_ARG(pairwise),
  3297. sm->dot11RSNAStatsTKIPLocalMICFailures,
  3298. sm->dot11RSNAStatsTKIPRemoteMICFailures);
  3299. if (os_snprintf_error(buflen - len, ret))
  3300. return len;
  3301. len += ret;
  3302. /* Private MIB */
  3303. ret = os_snprintf(buf + len, buflen - len,
  3304. "hostapdWPAPTKState=%d\n"
  3305. "hostapdWPAPTKGroupState=%d\n",
  3306. sm->wpa_ptk_state,
  3307. sm->wpa_ptk_group_state);
  3308. if (os_snprintf_error(buflen - len, ret))
  3309. return len;
  3310. len += ret;
  3311. return len;
  3312. }
  3313. void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
  3314. {
  3315. if (wpa_auth)
  3316. wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
  3317. }
  3318. int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
  3319. {
  3320. return sm && sm->pairwise_set;
  3321. }
  3322. int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
  3323. {
  3324. return sm->pairwise;
  3325. }
  3326. int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
  3327. {
  3328. if (sm == NULL)
  3329. return -1;
  3330. return sm->wpa_key_mgmt;
  3331. }
  3332. int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
  3333. {
  3334. if (sm == NULL)
  3335. return 0;
  3336. return sm->wpa;
  3337. }
  3338. int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
  3339. struct rsn_pmksa_cache_entry *entry)
  3340. {
  3341. if (sm == NULL || sm->pmksa != entry)
  3342. return -1;
  3343. sm->pmksa = NULL;
  3344. return 0;
  3345. }
  3346. struct rsn_pmksa_cache_entry *
  3347. wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
  3348. {
  3349. return sm ? sm->pmksa : NULL;
  3350. }
  3351. void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
  3352. {
  3353. if (sm)
  3354. sm->dot11RSNAStatsTKIPLocalMICFailures++;
  3355. }
  3356. const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
  3357. {
  3358. if (wpa_auth == NULL)
  3359. return NULL;
  3360. *len = wpa_auth->wpa_ie_len;
  3361. return wpa_auth->wpa_ie;
  3362. }
  3363. int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
  3364. unsigned int pmk_len,
  3365. int session_timeout, struct eapol_state_machine *eapol)
  3366. {
  3367. if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
  3368. sm->wpa_auth->conf.disable_pmksa_caching)
  3369. return -1;
  3370. if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
  3371. if (pmk_len > PMK_LEN_SUITE_B_192)
  3372. pmk_len = PMK_LEN_SUITE_B_192;
  3373. } else if (pmk_len > PMK_LEN) {
  3374. pmk_len = PMK_LEN;
  3375. }
  3376. if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, pmk_len, NULL,
  3377. sm->PTK.kck, sm->PTK.kck_len,
  3378. sm->wpa_auth->addr, sm->addr, session_timeout,
  3379. eapol, sm->wpa_key_mgmt))
  3380. return 0;
  3381. return -1;
  3382. }
  3383. int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
  3384. const u8 *pmk, size_t len, const u8 *sta_addr,
  3385. int session_timeout,
  3386. struct eapol_state_machine *eapol)
  3387. {
  3388. if (wpa_auth == NULL)
  3389. return -1;
  3390. if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, NULL,
  3391. NULL, 0,
  3392. wpa_auth->addr,
  3393. sta_addr, session_timeout, eapol,
  3394. WPA_KEY_MGMT_IEEE8021X))
  3395. return 0;
  3396. return -1;
  3397. }
  3398. int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
  3399. const u8 *pmk, const u8 *pmkid)
  3400. {
  3401. if (wpa_auth->conf.disable_pmksa_caching)
  3402. return -1;
  3403. if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN, pmkid,
  3404. NULL, 0,
  3405. wpa_auth->addr, addr, 0, NULL,
  3406. WPA_KEY_MGMT_SAE))
  3407. return 0;
  3408. return -1;
  3409. }
  3410. void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
  3411. const u8 *sta_addr)
  3412. {
  3413. struct rsn_pmksa_cache_entry *pmksa;
  3414. if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
  3415. return;
  3416. pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
  3417. if (pmksa) {
  3418. wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
  3419. MACSTR " based on request", MAC2STR(sta_addr));
  3420. pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
  3421. }
  3422. }
  3423. int wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf,
  3424. size_t len)
  3425. {
  3426. if (!wpa_auth || !wpa_auth->pmksa)
  3427. return 0;
  3428. return pmksa_cache_auth_list(wpa_auth->pmksa, buf, len);
  3429. }
  3430. void wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth)
  3431. {
  3432. if (wpa_auth && wpa_auth->pmksa)
  3433. pmksa_cache_auth_flush(wpa_auth->pmksa);
  3434. }
  3435. #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
  3436. #ifdef CONFIG_MESH
  3437. int wpa_auth_pmksa_list_mesh(struct wpa_authenticator *wpa_auth, const u8 *addr,
  3438. char *buf, size_t len)
  3439. {
  3440. if (!wpa_auth || !wpa_auth->pmksa)
  3441. return 0;
  3442. return pmksa_cache_auth_list_mesh(wpa_auth->pmksa, addr, buf, len);
  3443. }
  3444. struct rsn_pmksa_cache_entry *
  3445. wpa_auth_pmksa_create_entry(const u8 *aa, const u8 *spa, const u8 *pmk,
  3446. const u8 *pmkid, int expiration)
  3447. {
  3448. struct rsn_pmksa_cache_entry *entry;
  3449. struct os_reltime now;
  3450. entry = pmksa_cache_auth_create_entry(pmk, PMK_LEN, pmkid, NULL, 0, aa,
  3451. spa, 0, NULL, WPA_KEY_MGMT_SAE);
  3452. if (!entry)
  3453. return NULL;
  3454. os_get_reltime(&now);
  3455. entry->expiration = now.sec + expiration;
  3456. return entry;
  3457. }
  3458. int wpa_auth_pmksa_add_entry(struct wpa_authenticator *wpa_auth,
  3459. struct rsn_pmksa_cache_entry *entry)
  3460. {
  3461. int ret;
  3462. if (!wpa_auth || !wpa_auth->pmksa)
  3463. return -1;
  3464. ret = pmksa_cache_auth_add_entry(wpa_auth->pmksa, entry);
  3465. if (ret < 0)
  3466. wpa_printf(MSG_DEBUG,
  3467. "RSN: Failed to store external PMKSA cache for "
  3468. MACSTR, MAC2STR(entry->spa));
  3469. return ret;
  3470. }
  3471. #endif /* CONFIG_MESH */
  3472. #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
  3473. struct rsn_pmksa_cache_entry *
  3474. wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
  3475. const u8 *pmkid)
  3476. {
  3477. if (!wpa_auth || !wpa_auth->pmksa)
  3478. return NULL;
  3479. return pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, pmkid);
  3480. }
  3481. void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa,
  3482. struct wpa_state_machine *sm,
  3483. struct wpa_authenticator *wpa_auth,
  3484. u8 *pmkid, u8 *pmk)
  3485. {
  3486. if (!sm)
  3487. return;
  3488. sm->pmksa = pmksa;
  3489. os_memcpy(pmk, pmksa->pmk, PMK_LEN);
  3490. os_memcpy(pmkid, pmksa->pmkid, PMKID_LEN);
  3491. os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmksa->pmkid, PMKID_LEN);
  3492. }
  3493. /*
  3494. * Remove and free the group from wpa_authenticator. This is triggered by a
  3495. * callback to make sure nobody is currently iterating the group list while it
  3496. * gets modified.
  3497. */
  3498. static void wpa_group_free(struct wpa_authenticator *wpa_auth,
  3499. struct wpa_group *group)
  3500. {
  3501. struct wpa_group *prev = wpa_auth->group;
  3502. wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d",
  3503. group->vlan_id);
  3504. while (prev) {
  3505. if (prev->next == group) {
  3506. /* This never frees the special first group as needed */
  3507. prev->next = group->next;
  3508. os_free(group);
  3509. break;
  3510. }
  3511. prev = prev->next;
  3512. }
  3513. }
  3514. /* Increase the reference counter for group */
  3515. static void wpa_group_get(struct wpa_authenticator *wpa_auth,
  3516. struct wpa_group *group)
  3517. {
  3518. /* Skip the special first group */
  3519. if (wpa_auth->group == group)
  3520. return;
  3521. group->references++;
  3522. }
  3523. /* Decrease the reference counter and maybe free the group */
  3524. static void wpa_group_put(struct wpa_authenticator *wpa_auth,
  3525. struct wpa_group *group)
  3526. {
  3527. /* Skip the special first group */
  3528. if (wpa_auth->group == group)
  3529. return;
  3530. group->references--;
  3531. if (group->references)
  3532. return;
  3533. wpa_group_free(wpa_auth, group);
  3534. }
  3535. /*
  3536. * Add a group that has its references counter set to zero. Caller needs to
  3537. * call wpa_group_get() on the return value to mark the entry in use.
  3538. */
  3539. static struct wpa_group *
  3540. wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
  3541. {
  3542. struct wpa_group *group;
  3543. if (wpa_auth == NULL || wpa_auth->group == NULL)
  3544. return NULL;
  3545. wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
  3546. vlan_id);
  3547. group = wpa_group_init(wpa_auth, vlan_id, 0);
  3548. if (group == NULL)
  3549. return NULL;
  3550. group->next = wpa_auth->group->next;
  3551. wpa_auth->group->next = group;
  3552. return group;
  3553. }
  3554. /*
  3555. * Enforce that the group state machine for the VLAN is running, increase
  3556. * reference counter as interface is up. References might have been increased
  3557. * even if a negative value is returned.
  3558. * Returns: -1 on error (group missing, group already failed); otherwise, 0
  3559. */
  3560. int wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id)
  3561. {
  3562. struct wpa_group *group;
  3563. if (wpa_auth == NULL)
  3564. return 0;
  3565. group = wpa_auth->group;
  3566. while (group) {
  3567. if (group->vlan_id == vlan_id)
  3568. break;
  3569. group = group->next;
  3570. }
  3571. if (group == NULL) {
  3572. group = wpa_auth_add_group(wpa_auth, vlan_id);
  3573. if (group == NULL)
  3574. return -1;
  3575. }
  3576. wpa_printf(MSG_DEBUG,
  3577. "WPA: Ensure group state machine running for VLAN ID %d",
  3578. vlan_id);
  3579. wpa_group_get(wpa_auth, group);
  3580. group->num_setup_iface++;
  3581. if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
  3582. return -1;
  3583. return 0;
  3584. }
  3585. /*
  3586. * Decrease reference counter, expected to be zero afterwards.
  3587. * returns: -1 on error (group not found, group in fail state)
  3588. * -2 if wpa_group is still referenced
  3589. * 0 else
  3590. */
  3591. int wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id)
  3592. {
  3593. struct wpa_group *group;
  3594. int ret = 0;
  3595. if (wpa_auth == NULL)
  3596. return 0;
  3597. group = wpa_auth->group;
  3598. while (group) {
  3599. if (group->vlan_id == vlan_id)
  3600. break;
  3601. group = group->next;
  3602. }
  3603. if (group == NULL)
  3604. return -1;
  3605. wpa_printf(MSG_DEBUG,
  3606. "WPA: Try stopping group state machine for VLAN ID %d",
  3607. vlan_id);
  3608. if (group->num_setup_iface <= 0) {
  3609. wpa_printf(MSG_ERROR,
  3610. "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
  3611. vlan_id);
  3612. return -1;
  3613. }
  3614. group->num_setup_iface--;
  3615. if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
  3616. ret = -1;
  3617. if (group->references > 1) {
  3618. wpa_printf(MSG_DEBUG,
  3619. "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
  3620. vlan_id);
  3621. ret = -2;
  3622. }
  3623. wpa_group_put(wpa_auth, group);
  3624. return ret;
  3625. }
  3626. int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
  3627. {
  3628. struct wpa_group *group;
  3629. if (sm == NULL || sm->wpa_auth == NULL)
  3630. return 0;
  3631. group = sm->wpa_auth->group;
  3632. while (group) {
  3633. if (group->vlan_id == vlan_id)
  3634. break;
  3635. group = group->next;
  3636. }
  3637. if (group == NULL) {
  3638. group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
  3639. if (group == NULL)
  3640. return -1;
  3641. }
  3642. if (sm->group == group)
  3643. return 0;
  3644. if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
  3645. return -1;
  3646. wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
  3647. "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
  3648. wpa_group_get(sm->wpa_auth, group);
  3649. wpa_group_put(sm->wpa_auth, sm->group);
  3650. sm->group = group;
  3651. return 0;
  3652. }
  3653. void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
  3654. struct wpa_state_machine *sm, int ack)
  3655. {
  3656. if (wpa_auth == NULL || sm == NULL)
  3657. return;
  3658. wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
  3659. " ack=%d", MAC2STR(sm->addr), ack);
  3660. if (sm->pending_1_of_4_timeout && ack) {
  3661. /*
  3662. * Some deployed supplicant implementations update their SNonce
  3663. * for each EAPOL-Key 2/4 message even within the same 4-way
  3664. * handshake and then fail to use the first SNonce when
  3665. * deriving the PTK. This results in unsuccessful 4-way
  3666. * handshake whenever the relatively short initial timeout is
  3667. * reached and EAPOL-Key 1/4 is retransmitted. Try to work
  3668. * around this by increasing the timeout now that we know that
  3669. * the station has received the frame.
  3670. */
  3671. int timeout_ms = eapol_key_timeout_subseq;
  3672. wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
  3673. "timeout by %u ms because of acknowledged frame",
  3674. timeout_ms);
  3675. eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
  3676. eloop_register_timeout(timeout_ms / 1000,
  3677. (timeout_ms % 1000) * 1000,
  3678. wpa_send_eapol_timeout, wpa_auth, sm);
  3679. }
  3680. }
  3681. int wpa_auth_uses_sae(struct wpa_state_machine *sm)
  3682. {
  3683. if (sm == NULL)
  3684. return 0;
  3685. return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
  3686. }
  3687. int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
  3688. {
  3689. if (sm == NULL)
  3690. return 0;
  3691. return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
  3692. }
  3693. #ifdef CONFIG_P2P
  3694. int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
  3695. {
  3696. if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
  3697. return -1;
  3698. os_memcpy(addr, sm->ip_addr, 4);
  3699. return 0;
  3700. }
  3701. #endif /* CONFIG_P2P */
  3702. int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
  3703. struct radius_das_attrs *attr)
  3704. {
  3705. return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
  3706. }
  3707. void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)
  3708. {
  3709. struct wpa_group *group;
  3710. if (!wpa_auth)
  3711. return;
  3712. for (group = wpa_auth->group; group; group = group->next)
  3713. wpa_group_config_group_keys(wpa_auth, group);
  3714. }
  3715. #ifdef CONFIG_FILS
  3716. struct wpa_auth_fils_iter_data {
  3717. struct wpa_authenticator *auth;
  3718. const u8 *cache_id;
  3719. struct rsn_pmksa_cache_entry *pmksa;
  3720. const u8 *spa;
  3721. const u8 *pmkid;
  3722. };
  3723. static int wpa_auth_fils_iter(struct wpa_authenticator *a, void *ctx)
  3724. {
  3725. struct wpa_auth_fils_iter_data *data = ctx;
  3726. if (a == data->auth || !a->conf.fils_cache_id_set ||
  3727. os_memcmp(a->conf.fils_cache_id, data->cache_id,
  3728. FILS_CACHE_ID_LEN) != 0)
  3729. return 0;
  3730. data->pmksa = pmksa_cache_auth_get(a->pmksa, data->spa, data->pmkid);
  3731. return data->pmksa != NULL;
  3732. }
  3733. struct rsn_pmksa_cache_entry *
  3734. wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator *wpa_auth,
  3735. const u8 *sta_addr, const u8 *pmkid)
  3736. {
  3737. struct wpa_auth_fils_iter_data idata;
  3738. if (!wpa_auth->conf.fils_cache_id_set)
  3739. return NULL;
  3740. idata.auth = wpa_auth;
  3741. idata.cache_id = wpa_auth->conf.fils_cache_id;
  3742. idata.pmksa = NULL;
  3743. idata.spa = sta_addr;
  3744. idata.pmkid = pmkid;
  3745. wpa_auth_for_each_auth(wpa_auth, wpa_auth_fils_iter, &idata);
  3746. return idata.pmksa;
  3747. }
  3748. #ifdef CONFIG_IEEE80211R_AP
  3749. int wpa_auth_write_fte(struct wpa_authenticator *wpa_auth, u8 *buf, size_t len)
  3750. {
  3751. struct wpa_auth_config *conf = &wpa_auth->conf;
  3752. return wpa_write_ftie(conf, conf->r0_key_holder,
  3753. conf->r0_key_holder_len,
  3754. NULL, NULL, buf, len, NULL, 0);
  3755. }
  3756. #endif /* CONFIG_IEEE80211R_AP */
  3757. void wpa_auth_get_fils_aead_params(struct wpa_state_machine *sm,
  3758. u8 *fils_anonce, u8 *fils_snonce,
  3759. u8 *fils_kek, size_t *fils_kek_len)
  3760. {
  3761. os_memcpy(fils_anonce, sm->ANonce, WPA_NONCE_LEN);
  3762. os_memcpy(fils_snonce, sm->SNonce, WPA_NONCE_LEN);
  3763. os_memcpy(fils_kek, sm->PTK.kek, WPA_KEK_MAX_LEN);
  3764. *fils_kek_len = sm->PTK.kek_len;
  3765. }
  3766. #endif /* CONFIG_FILS */