wpa_auth.c 128 KB

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