wpa_auth.c 113 KB

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