wpa_auth.c 117 KB

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