wpa_auth.c 121 KB

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