wpa_auth.c 129 KB

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