wpa.c 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299
  1. /*
  2. * WPA Supplicant - WPA state machine and EAPOL-Key processing
  3. * Copyright (c) 2003-2017, Jouni Malinen <j@w1.fi>
  4. * Copyright(c) 2015 Intel Deutschland GmbH
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "includes.h"
  10. #include "common.h"
  11. #include "crypto/aes.h"
  12. #include "crypto/aes_wrap.h"
  13. #include "crypto/crypto.h"
  14. #include "crypto/random.h"
  15. #include "crypto/aes_siv.h"
  16. #include "crypto/sha256.h"
  17. #include "common/ieee802_11_defs.h"
  18. #include "common/ieee802_11_common.h"
  19. #include "eap_common/eap_defs.h"
  20. #include "eapol_supp/eapol_supp_sm.h"
  21. #include "wpa.h"
  22. #include "eloop.h"
  23. #include "preauth.h"
  24. #include "pmksa_cache.h"
  25. #include "wpa_i.h"
  26. #include "wpa_ie.h"
  27. #include "peerkey.h"
  28. static const u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  29. /**
  30. * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
  31. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  32. * @ptk: PTK for Key Confirmation/Encryption Key
  33. * @ver: Version field from Key Info
  34. * @dest: Destination address for the frame
  35. * @proto: Ethertype (usually ETH_P_EAPOL)
  36. * @msg: EAPOL-Key message
  37. * @msg_len: Length of message
  38. * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
  39. * Returns: >= 0 on success, < 0 on failure
  40. */
  41. int wpa_eapol_key_send(struct wpa_sm *sm, struct wpa_ptk *ptk,
  42. int ver, const u8 *dest, u16 proto,
  43. u8 *msg, size_t msg_len, u8 *key_mic)
  44. {
  45. int ret = -1;
  46. size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
  47. wpa_printf(MSG_DEBUG, "WPA: Send EAPOL-Key frame to " MACSTR
  48. " ver=%d mic_len=%d key_mgmt=0x%x",
  49. MAC2STR(dest), ver, (int) mic_len, sm->key_mgmt);
  50. if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
  51. /*
  52. * Association event was not yet received; try to fetch
  53. * BSSID from the driver.
  54. */
  55. if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
  56. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  57. "WPA: Failed to read BSSID for "
  58. "EAPOL-Key destination address");
  59. } else {
  60. dest = sm->bssid;
  61. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  62. "WPA: Use BSSID (" MACSTR
  63. ") as the destination for EAPOL-Key",
  64. MAC2STR(dest));
  65. }
  66. }
  67. if (mic_len) {
  68. if (key_mic && (!ptk || !ptk->kck_len))
  69. goto out;
  70. if (key_mic &&
  71. wpa_eapol_key_mic(ptk->kck, ptk->kck_len, sm->key_mgmt, ver,
  72. msg, msg_len, key_mic)) {
  73. wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
  74. "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
  75. ver, sm->key_mgmt);
  76. goto out;
  77. }
  78. if (ptk)
  79. wpa_hexdump_key(MSG_DEBUG, "WPA: KCK",
  80. ptk->kck, ptk->kck_len);
  81. wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC",
  82. key_mic, mic_len);
  83. } else {
  84. #ifdef CONFIG_FILS
  85. /* AEAD cipher - Key MIC field not used */
  86. struct ieee802_1x_hdr *s_hdr, *hdr;
  87. struct wpa_eapol_key *s_key, *key;
  88. u8 *buf, *s_key_data, *key_data;
  89. size_t buf_len = msg_len + AES_BLOCK_SIZE;
  90. size_t key_data_len;
  91. u16 eapol_len;
  92. const u8 *aad[1];
  93. size_t aad_len[1];
  94. if (!ptk || !ptk->kek_len)
  95. goto out;
  96. key_data_len = msg_len - sizeof(struct ieee802_1x_hdr) -
  97. sizeof(struct wpa_eapol_key) - 2;
  98. buf = os_malloc(buf_len);
  99. if (!buf)
  100. goto out;
  101. os_memcpy(buf, msg, msg_len);
  102. hdr = (struct ieee802_1x_hdr *) buf;
  103. key = (struct wpa_eapol_key *) (hdr + 1);
  104. key_data = ((u8 *) (key + 1)) + 2;
  105. /* Update EAPOL header to include AES-SIV overhead */
  106. eapol_len = be_to_host16(hdr->length);
  107. eapol_len += AES_BLOCK_SIZE;
  108. hdr->length = host_to_be16(eapol_len);
  109. /* Update Key Data Length field to include AES-SIV overhead */
  110. WPA_PUT_BE16((u8 *) (key + 1), AES_BLOCK_SIZE + key_data_len);
  111. s_hdr = (struct ieee802_1x_hdr *) msg;
  112. s_key = (struct wpa_eapol_key *) (s_hdr + 1);
  113. s_key_data = ((u8 *) (s_key + 1)) + 2;
  114. wpa_hexdump_key(MSG_DEBUG, "WPA: Plaintext Key Data",
  115. s_key_data, key_data_len);
  116. wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len);
  117. /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
  118. * to Key Data (exclusive). */
  119. aad[0] = buf;
  120. aad_len[0] = key_data - buf;
  121. if (aes_siv_encrypt(ptk->kek, ptk->kek_len,
  122. s_key_data, key_data_len,
  123. 1, aad, aad_len, key_data) < 0) {
  124. os_free(buf);
  125. goto out;
  126. }
  127. wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
  128. key_data, AES_BLOCK_SIZE + key_data_len);
  129. os_free(msg);
  130. msg = buf;
  131. msg_len = buf_len;
  132. #else /* CONFIG_FILS */
  133. goto out;
  134. #endif /* CONFIG_FILS */
  135. }
  136. wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
  137. ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
  138. eapol_sm_notify_tx_eapol_key(sm->eapol);
  139. out:
  140. os_free(msg);
  141. return ret;
  142. }
  143. /**
  144. * wpa_sm_key_request - Send EAPOL-Key Request
  145. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  146. * @error: Indicate whether this is an Michael MIC error report
  147. * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
  148. *
  149. * Send an EAPOL-Key Request to the current authenticator. This function is
  150. * used to request rekeying and it is usually called when a local Michael MIC
  151. * failure is detected.
  152. */
  153. void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
  154. {
  155. size_t mic_len, hdrlen, rlen;
  156. struct wpa_eapol_key *reply;
  157. int key_info, ver;
  158. u8 bssid[ETH_ALEN], *rbuf, *key_mic, *mic;
  159. if (sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
  160. wpa_key_mgmt_suite_b(sm->key_mgmt))
  161. ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
  162. else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
  163. wpa_key_mgmt_sha256(sm->key_mgmt))
  164. ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
  165. else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
  166. ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
  167. else
  168. ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
  169. if (wpa_sm_get_bssid(sm, bssid) < 0) {
  170. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  171. "Failed to read BSSID for EAPOL-Key request");
  172. return;
  173. }
  174. mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
  175. hdrlen = sizeof(*reply) + mic_len + 2;
  176. rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
  177. hdrlen, &rlen, (void *) &reply);
  178. if (rbuf == NULL)
  179. return;
  180. reply->type = (sm->proto == WPA_PROTO_RSN ||
  181. sm->proto == WPA_PROTO_OSEN) ?
  182. EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
  183. key_info = WPA_KEY_INFO_REQUEST | ver;
  184. if (sm->ptk_set)
  185. key_info |= WPA_KEY_INFO_SECURE;
  186. if (sm->ptk_set && mic_len)
  187. key_info |= WPA_KEY_INFO_MIC;
  188. if (error)
  189. key_info |= WPA_KEY_INFO_ERROR;
  190. if (pairwise)
  191. key_info |= WPA_KEY_INFO_KEY_TYPE;
  192. WPA_PUT_BE16(reply->key_info, key_info);
  193. WPA_PUT_BE16(reply->key_length, 0);
  194. os_memcpy(reply->replay_counter, sm->request_counter,
  195. WPA_REPLAY_COUNTER_LEN);
  196. inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
  197. mic = (u8 *) (reply + 1);
  198. WPA_PUT_BE16(mic + mic_len, 0);
  199. if (!(key_info & WPA_KEY_INFO_MIC))
  200. key_mic = NULL;
  201. else
  202. key_mic = mic;
  203. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  204. "WPA: Sending EAPOL-Key Request (error=%d "
  205. "pairwise=%d ptk_set=%d len=%lu)",
  206. error, pairwise, sm->ptk_set, (unsigned long) rlen);
  207. wpa_eapol_key_send(sm, &sm->ptk, ver, bssid, ETH_P_EAPOL, rbuf, rlen,
  208. key_mic);
  209. }
  210. static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
  211. {
  212. #ifdef CONFIG_IEEE80211R
  213. if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
  214. if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
  215. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  216. "RSN: Cannot set low order 256 bits of MSK for key management offload");
  217. } else {
  218. #endif /* CONFIG_IEEE80211R */
  219. if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
  220. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  221. "RSN: Cannot set PMK for key management offload");
  222. #ifdef CONFIG_IEEE80211R
  223. }
  224. #endif /* CONFIG_IEEE80211R */
  225. }
  226. static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
  227. const unsigned char *src_addr,
  228. const u8 *pmkid)
  229. {
  230. int abort_cached = 0;
  231. if (pmkid && !sm->cur_pmksa) {
  232. /* When using drivers that generate RSN IE, wpa_supplicant may
  233. * not have enough time to get the association information
  234. * event before receiving this 1/4 message, so try to find a
  235. * matching PMKSA cache entry here. */
  236. sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
  237. NULL);
  238. if (sm->cur_pmksa) {
  239. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  240. "RSN: found matching PMKID from PMKSA cache");
  241. } else {
  242. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  243. "RSN: no matching PMKID found");
  244. abort_cached = 1;
  245. }
  246. }
  247. if (pmkid && sm->cur_pmksa &&
  248. os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
  249. wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
  250. wpa_sm_set_pmk_from_pmksa(sm);
  251. wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
  252. sm->pmk, sm->pmk_len);
  253. eapol_sm_notify_cached(sm->eapol);
  254. #ifdef CONFIG_IEEE80211R
  255. sm->xxkey_len = 0;
  256. #endif /* CONFIG_IEEE80211R */
  257. } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
  258. int res, pmk_len;
  259. if (wpa_key_mgmt_sha384(sm->key_mgmt))
  260. pmk_len = PMK_LEN_SUITE_B_192;
  261. else
  262. pmk_len = PMK_LEN;
  263. res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
  264. if (res) {
  265. if (pmk_len == PMK_LEN) {
  266. /*
  267. * EAP-LEAP is an exception from other EAP
  268. * methods: it uses only 16-byte PMK.
  269. */
  270. res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
  271. pmk_len = 16;
  272. }
  273. } else {
  274. #ifdef CONFIG_IEEE80211R
  275. u8 buf[2 * PMK_LEN];
  276. if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
  277. {
  278. os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
  279. sm->xxkey_len = PMK_LEN;
  280. os_memset(buf, 0, sizeof(buf));
  281. }
  282. #endif /* CONFIG_IEEE80211R */
  283. }
  284. if (res == 0) {
  285. struct rsn_pmksa_cache_entry *sa = NULL;
  286. const u8 *fils_cache_id = NULL;
  287. #ifdef CONFIG_FILS
  288. if (sm->fils_cache_id_set)
  289. fils_cache_id = sm->fils_cache_id;
  290. #endif /* CONFIG_FILS */
  291. wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
  292. "machines", sm->pmk, pmk_len);
  293. sm->pmk_len = pmk_len;
  294. wpa_supplicant_key_mgmt_set_pmk(sm);
  295. if (sm->proto == WPA_PROTO_RSN &&
  296. !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
  297. !wpa_key_mgmt_ft(sm->key_mgmt)) {
  298. sa = pmksa_cache_add(sm->pmksa,
  299. sm->pmk, pmk_len, NULL,
  300. NULL, 0,
  301. src_addr, sm->own_addr,
  302. sm->network_ctx,
  303. sm->key_mgmt,
  304. fils_cache_id);
  305. }
  306. if (!sm->cur_pmksa && pmkid &&
  307. pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL))
  308. {
  309. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  310. "RSN: the new PMK matches with the "
  311. "PMKID");
  312. abort_cached = 0;
  313. } else if (sa && !sm->cur_pmksa && pmkid) {
  314. /*
  315. * It looks like the authentication server
  316. * derived mismatching MSK. This should not
  317. * really happen, but bugs happen.. There is not
  318. * much we can do here without knowing what
  319. * exactly caused the server to misbehave.
  320. */
  321. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  322. "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
  323. return -1;
  324. }
  325. if (!sm->cur_pmksa)
  326. sm->cur_pmksa = sa;
  327. } else {
  328. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  329. "WPA: Failed to get master session key from "
  330. "EAPOL state machines - key handshake "
  331. "aborted");
  332. if (sm->cur_pmksa) {
  333. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  334. "RSN: Cancelled PMKSA caching "
  335. "attempt");
  336. sm->cur_pmksa = NULL;
  337. abort_cached = 1;
  338. } else if (!abort_cached) {
  339. return -1;
  340. }
  341. }
  342. }
  343. if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
  344. !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
  345. !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
  346. {
  347. /* Send EAPOL-Start to trigger full EAP authentication. */
  348. u8 *buf;
  349. size_t buflen;
  350. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  351. "RSN: no PMKSA entry found - trigger "
  352. "full EAP authentication");
  353. buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
  354. NULL, 0, &buflen, NULL);
  355. if (buf) {
  356. wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
  357. buf, buflen);
  358. os_free(buf);
  359. return -2;
  360. }
  361. return -1;
  362. }
  363. return 0;
  364. }
  365. /**
  366. * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
  367. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  368. * @dst: Destination address for the frame
  369. * @key: Pointer to the EAPOL-Key frame header
  370. * @ver: Version bits from EAPOL-Key Key Info
  371. * @nonce: Nonce value for the EAPOL-Key frame
  372. * @wpa_ie: WPA/RSN IE
  373. * @wpa_ie_len: Length of the WPA/RSN IE
  374. * @ptk: PTK to use for keyed hash and encryption
  375. * Returns: >= 0 on success, < 0 on failure
  376. */
  377. int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
  378. const struct wpa_eapol_key *key,
  379. int ver, const u8 *nonce,
  380. const u8 *wpa_ie, size_t wpa_ie_len,
  381. struct wpa_ptk *ptk)
  382. {
  383. size_t mic_len, hdrlen, rlen;
  384. struct wpa_eapol_key *reply;
  385. u8 *rbuf, *key_mic;
  386. u8 *rsn_ie_buf = NULL;
  387. u16 key_info;
  388. if (wpa_ie == NULL) {
  389. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
  390. "cannot generate msg 2/4");
  391. return -1;
  392. }
  393. #ifdef CONFIG_IEEE80211R
  394. if (wpa_key_mgmt_ft(sm->key_mgmt)) {
  395. int res;
  396. /*
  397. * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
  398. * FTIE from (Re)Association Response.
  399. */
  400. rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
  401. sm->assoc_resp_ies_len);
  402. if (rsn_ie_buf == NULL)
  403. return -1;
  404. os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
  405. res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
  406. sm->pmk_r1_name);
  407. if (res < 0) {
  408. os_free(rsn_ie_buf);
  409. return -1;
  410. }
  411. if (sm->assoc_resp_ies) {
  412. os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
  413. sm->assoc_resp_ies_len);
  414. wpa_ie_len += sm->assoc_resp_ies_len;
  415. }
  416. wpa_ie = rsn_ie_buf;
  417. }
  418. #endif /* CONFIG_IEEE80211R */
  419. wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
  420. mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
  421. hdrlen = sizeof(*reply) + mic_len + 2;
  422. rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
  423. NULL, hdrlen + wpa_ie_len,
  424. &rlen, (void *) &reply);
  425. if (rbuf == NULL) {
  426. os_free(rsn_ie_buf);
  427. return -1;
  428. }
  429. reply->type = (sm->proto == WPA_PROTO_RSN ||
  430. sm->proto == WPA_PROTO_OSEN) ?
  431. EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
  432. key_info = ver | WPA_KEY_INFO_KEY_TYPE;
  433. if (mic_len)
  434. key_info |= WPA_KEY_INFO_MIC;
  435. else
  436. key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
  437. WPA_PUT_BE16(reply->key_info, key_info);
  438. if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
  439. WPA_PUT_BE16(reply->key_length, 0);
  440. else
  441. os_memcpy(reply->key_length, key->key_length, 2);
  442. os_memcpy(reply->replay_counter, key->replay_counter,
  443. WPA_REPLAY_COUNTER_LEN);
  444. wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
  445. WPA_REPLAY_COUNTER_LEN);
  446. key_mic = (u8 *) (reply + 1);
  447. WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */
  448. os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
  449. os_free(rsn_ie_buf);
  450. os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
  451. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
  452. return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
  453. key_mic);
  454. }
  455. static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
  456. const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
  457. {
  458. #ifdef CONFIG_IEEE80211R
  459. if (wpa_key_mgmt_ft(sm->key_mgmt))
  460. return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
  461. #endif /* CONFIG_IEEE80211R */
  462. return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
  463. sm->own_addr, sm->bssid, sm->snonce,
  464. key->key_nonce, ptk, sm->key_mgmt,
  465. sm->pairwise_cipher);
  466. }
  467. static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
  468. const unsigned char *src_addr,
  469. const struct wpa_eapol_key *key,
  470. u16 ver, const u8 *key_data,
  471. size_t key_data_len)
  472. {
  473. struct wpa_eapol_ie_parse ie;
  474. struct wpa_ptk *ptk;
  475. int res;
  476. u8 *kde, *kde_buf = NULL;
  477. size_t kde_len;
  478. if (wpa_sm_get_network_ctx(sm) == NULL) {
  479. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
  480. "found (msg 1 of 4)");
  481. return;
  482. }
  483. wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
  484. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
  485. "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
  486. os_memset(&ie, 0, sizeof(ie));
  487. if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
  488. /* RSN: msg 1/4 should contain PMKID for the selected PMK */
  489. wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
  490. key_data, key_data_len);
  491. if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
  492. goto failed;
  493. if (ie.pmkid) {
  494. wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
  495. "Authenticator", ie.pmkid, PMKID_LEN);
  496. }
  497. }
  498. res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
  499. if (res == -2) {
  500. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
  501. "msg 1/4 - requesting full EAP authentication");
  502. return;
  503. }
  504. if (res)
  505. goto failed;
  506. if (sm->renew_snonce) {
  507. if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
  508. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  509. "WPA: Failed to get random data for SNonce");
  510. goto failed;
  511. }
  512. sm->renew_snonce = 0;
  513. wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
  514. sm->snonce, WPA_NONCE_LEN);
  515. }
  516. /* Calculate PTK which will be stored as a temporary PTK until it has
  517. * been verified when processing message 3/4. */
  518. ptk = &sm->tptk;
  519. wpa_derive_ptk(sm, src_addr, key, ptk);
  520. if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
  521. u8 buf[8];
  522. /* Supplicant: swap tx/rx Mic keys */
  523. os_memcpy(buf, &ptk->tk[16], 8);
  524. os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
  525. os_memcpy(&ptk->tk[24], buf, 8);
  526. os_memset(buf, 0, sizeof(buf));
  527. }
  528. sm->tptk_set = 1;
  529. sm->tk_to_set = 1;
  530. kde = sm->assoc_wpa_ie;
  531. kde_len = sm->assoc_wpa_ie_len;
  532. #ifdef CONFIG_P2P
  533. if (sm->p2p) {
  534. kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
  535. if (kde_buf) {
  536. u8 *pos;
  537. wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
  538. "into EAPOL-Key 2/4");
  539. os_memcpy(kde_buf, kde, kde_len);
  540. kde = kde_buf;
  541. pos = kde + kde_len;
  542. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  543. *pos++ = RSN_SELECTOR_LEN + 1;
  544. RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
  545. pos += RSN_SELECTOR_LEN;
  546. *pos++ = 0x01;
  547. kde_len = pos - kde;
  548. }
  549. }
  550. #endif /* CONFIG_P2P */
  551. if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
  552. kde, kde_len, ptk) < 0)
  553. goto failed;
  554. os_free(kde_buf);
  555. os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
  556. return;
  557. failed:
  558. os_free(kde_buf);
  559. wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
  560. }
  561. static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
  562. {
  563. struct wpa_sm *sm = eloop_ctx;
  564. rsn_preauth_candidate_process(sm);
  565. }
  566. static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
  567. const u8 *addr, int secure)
  568. {
  569. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  570. "WPA: Key negotiation completed with "
  571. MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
  572. wpa_cipher_txt(sm->pairwise_cipher),
  573. wpa_cipher_txt(sm->group_cipher));
  574. wpa_sm_cancel_auth_timeout(sm);
  575. wpa_sm_set_state(sm, WPA_COMPLETED);
  576. if (secure) {
  577. wpa_sm_mlme_setprotection(
  578. sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
  579. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  580. eapol_sm_notify_portValid(sm->eapol, TRUE);
  581. if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
  582. sm->key_mgmt == WPA_KEY_MGMT_DPP ||
  583. sm->key_mgmt == WPA_KEY_MGMT_OWE)
  584. eapol_sm_notify_eap_success(sm->eapol, TRUE);
  585. /*
  586. * Start preauthentication after a short wait to avoid a
  587. * possible race condition between the data receive and key
  588. * configuration after the 4-Way Handshake. This increases the
  589. * likelihood of the first preauth EAPOL-Start frame getting to
  590. * the target AP.
  591. */
  592. eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
  593. }
  594. if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
  595. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  596. "RSN: Authenticator accepted "
  597. "opportunistic PMKSA entry - marking it valid");
  598. sm->cur_pmksa->opportunistic = 0;
  599. }
  600. #ifdef CONFIG_IEEE80211R
  601. if (wpa_key_mgmt_ft(sm->key_mgmt)) {
  602. /* Prepare for the next transition */
  603. wpa_ft_prepare_auth_request(sm, NULL);
  604. }
  605. #endif /* CONFIG_IEEE80211R */
  606. }
  607. static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
  608. {
  609. struct wpa_sm *sm = eloop_ctx;
  610. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
  611. wpa_sm_key_request(sm, 0, 1);
  612. }
  613. static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
  614. const struct wpa_eapol_key *key)
  615. {
  616. int keylen, rsclen;
  617. enum wpa_alg alg;
  618. const u8 *key_rsc;
  619. if (!sm->tk_to_set) {
  620. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  621. "WPA: Do not re-install same PTK to the driver");
  622. return 0;
  623. }
  624. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  625. "WPA: Installing PTK to the driver");
  626. if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
  627. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
  628. "Suite: NONE - do not use pairwise keys");
  629. return 0;
  630. }
  631. if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
  632. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  633. "WPA: Unsupported pairwise cipher %d",
  634. sm->pairwise_cipher);
  635. return -1;
  636. }
  637. alg = wpa_cipher_to_alg(sm->pairwise_cipher);
  638. keylen = wpa_cipher_key_len(sm->pairwise_cipher);
  639. rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
  640. if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
  641. key_rsc = null_rsc;
  642. } else {
  643. key_rsc = key->key_rsc;
  644. wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
  645. }
  646. if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
  647. sm->ptk.tk, keylen) < 0) {
  648. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  649. "WPA: Failed to set PTK to the "
  650. "driver (alg=%d keylen=%d bssid=" MACSTR ")",
  651. alg, keylen, MAC2STR(sm->bssid));
  652. return -1;
  653. }
  654. /* TK is not needed anymore in supplicant */
  655. os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
  656. sm->tk_to_set = 0;
  657. if (sm->wpa_ptk_rekey) {
  658. eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
  659. eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
  660. sm, NULL);
  661. }
  662. return 0;
  663. }
  664. static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
  665. int group_cipher,
  666. int keylen, int maxkeylen,
  667. int *key_rsc_len,
  668. enum wpa_alg *alg)
  669. {
  670. int klen;
  671. *alg = wpa_cipher_to_alg(group_cipher);
  672. if (*alg == WPA_ALG_NONE) {
  673. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  674. "WPA: Unsupported Group Cipher %d",
  675. group_cipher);
  676. return -1;
  677. }
  678. *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
  679. klen = wpa_cipher_key_len(group_cipher);
  680. if (keylen != klen || maxkeylen < klen) {
  681. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  682. "WPA: Unsupported %s Group Cipher key length %d (%d)",
  683. wpa_cipher_txt(group_cipher), keylen, maxkeylen);
  684. return -1;
  685. }
  686. return 0;
  687. }
  688. struct wpa_gtk_data {
  689. enum wpa_alg alg;
  690. int tx, key_rsc_len, keyidx;
  691. u8 gtk[32];
  692. int gtk_len;
  693. };
  694. static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
  695. const struct wpa_gtk_data *gd,
  696. const u8 *key_rsc)
  697. {
  698. const u8 *_gtk = gd->gtk;
  699. u8 gtk_buf[32];
  700. wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
  701. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  702. "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
  703. gd->keyidx, gd->tx, gd->gtk_len);
  704. wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
  705. if (sm->group_cipher == WPA_CIPHER_TKIP) {
  706. /* Swap Tx/Rx keys for Michael MIC */
  707. os_memcpy(gtk_buf, gd->gtk, 16);
  708. os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
  709. os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
  710. _gtk = gtk_buf;
  711. }
  712. if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
  713. if (wpa_sm_set_key(sm, gd->alg, NULL,
  714. gd->keyidx, 1, key_rsc, gd->key_rsc_len,
  715. _gtk, gd->gtk_len) < 0) {
  716. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  717. "WPA: Failed to set GTK to the driver "
  718. "(Group only)");
  719. os_memset(gtk_buf, 0, sizeof(gtk_buf));
  720. return -1;
  721. }
  722. } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
  723. gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
  724. _gtk, gd->gtk_len) < 0) {
  725. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  726. "WPA: Failed to set GTK to "
  727. "the driver (alg=%d keylen=%d keyidx=%d)",
  728. gd->alg, gd->gtk_len, gd->keyidx);
  729. os_memset(gtk_buf, 0, sizeof(gtk_buf));
  730. return -1;
  731. }
  732. os_memset(gtk_buf, 0, sizeof(gtk_buf));
  733. return 0;
  734. }
  735. static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
  736. int tx)
  737. {
  738. if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
  739. /* Ignore Tx bit for GTK if a pairwise key is used. One AP
  740. * seemed to set this bit (incorrectly, since Tx is only when
  741. * doing Group Key only APs) and without this workaround, the
  742. * data connection does not work because wpa_supplicant
  743. * configured non-zero keyidx to be used for unicast. */
  744. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  745. "WPA: Tx bit set for GTK, but pairwise "
  746. "keys are used - ignore Tx bit");
  747. return 0;
  748. }
  749. return tx;
  750. }
  751. static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
  752. const u8 *rsc)
  753. {
  754. int rsclen;
  755. if (!sm->wpa_rsc_relaxation)
  756. return 0;
  757. rsclen = wpa_cipher_rsc_len(sm->group_cipher);
  758. /*
  759. * Try to detect RSC (endian) corruption issue where the AP sends
  760. * the RSC bytes in EAPOL-Key message in the wrong order, both if
  761. * it's actually a 6-byte field (as it should be) and if it treats
  762. * it as an 8-byte field.
  763. * An AP model known to have this bug is the Sapido RB-1632.
  764. */
  765. if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
  766. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  767. "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
  768. rsc[0], rsc[1], rsc[2], rsc[3],
  769. rsc[4], rsc[5], rsc[6], rsc[7]);
  770. return 1;
  771. }
  772. return 0;
  773. }
  774. static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
  775. const struct wpa_eapol_key *key,
  776. const u8 *gtk, size_t gtk_len,
  777. int key_info)
  778. {
  779. struct wpa_gtk_data gd;
  780. const u8 *key_rsc;
  781. /*
  782. * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
  783. * GTK KDE format:
  784. * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
  785. * Reserved [bits 0-7]
  786. * GTK
  787. */
  788. os_memset(&gd, 0, sizeof(gd));
  789. wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
  790. gtk, gtk_len);
  791. if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
  792. return -1;
  793. gd.keyidx = gtk[0] & 0x3;
  794. gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
  795. !!(gtk[0] & BIT(2)));
  796. gtk += 2;
  797. gtk_len -= 2;
  798. os_memcpy(gd.gtk, gtk, gtk_len);
  799. gd.gtk_len = gtk_len;
  800. key_rsc = key->key_rsc;
  801. if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
  802. key_rsc = null_rsc;
  803. if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
  804. (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
  805. gtk_len, gtk_len,
  806. &gd.key_rsc_len, &gd.alg) ||
  807. wpa_supplicant_install_gtk(sm, &gd, key_rsc))) {
  808. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  809. "RSN: Failed to install GTK");
  810. os_memset(&gd, 0, sizeof(gd));
  811. return -1;
  812. }
  813. os_memset(&gd, 0, sizeof(gd));
  814. wpa_supplicant_key_neg_complete(sm, sm->bssid,
  815. key_info & WPA_KEY_INFO_SECURE);
  816. return 0;
  817. }
  818. static int ieee80211w_set_keys(struct wpa_sm *sm,
  819. struct wpa_eapol_ie_parse *ie)
  820. {
  821. #ifdef CONFIG_IEEE80211W
  822. if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
  823. return 0;
  824. if (ie->igtk) {
  825. size_t len;
  826. const struct wpa_igtk_kde *igtk;
  827. u16 keyidx;
  828. len = wpa_cipher_key_len(sm->mgmt_group_cipher);
  829. if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
  830. return -1;
  831. igtk = (const struct wpa_igtk_kde *) ie->igtk;
  832. keyidx = WPA_GET_LE16(igtk->keyid);
  833. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: IGTK keyid %d "
  834. "pn %02x%02x%02x%02x%02x%02x",
  835. keyidx, MAC2STR(igtk->pn));
  836. wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
  837. igtk->igtk, len);
  838. if (keyidx > 4095) {
  839. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  840. "WPA: Invalid IGTK KeyID %d", keyidx);
  841. return -1;
  842. }
  843. if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
  844. broadcast_ether_addr,
  845. keyidx, 0, igtk->pn, sizeof(igtk->pn),
  846. igtk->igtk, len) < 0) {
  847. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  848. "WPA: Failed to configure IGTK to the driver");
  849. return -1;
  850. }
  851. }
  852. return 0;
  853. #else /* CONFIG_IEEE80211W */
  854. return 0;
  855. #endif /* CONFIG_IEEE80211W */
  856. }
  857. static void wpa_report_ie_mismatch(struct wpa_sm *sm,
  858. const char *reason, const u8 *src_addr,
  859. const u8 *wpa_ie, size_t wpa_ie_len,
  860. const u8 *rsn_ie, size_t rsn_ie_len)
  861. {
  862. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
  863. reason, MAC2STR(src_addr));
  864. if (sm->ap_wpa_ie) {
  865. wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
  866. sm->ap_wpa_ie, sm->ap_wpa_ie_len);
  867. }
  868. if (wpa_ie) {
  869. if (!sm->ap_wpa_ie) {
  870. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  871. "WPA: No WPA IE in Beacon/ProbeResp");
  872. }
  873. wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
  874. wpa_ie, wpa_ie_len);
  875. }
  876. if (sm->ap_rsn_ie) {
  877. wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
  878. sm->ap_rsn_ie, sm->ap_rsn_ie_len);
  879. }
  880. if (rsn_ie) {
  881. if (!sm->ap_rsn_ie) {
  882. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  883. "WPA: No RSN IE in Beacon/ProbeResp");
  884. }
  885. wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
  886. rsn_ie, rsn_ie_len);
  887. }
  888. wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
  889. }
  890. #ifdef CONFIG_IEEE80211R
  891. static int ft_validate_mdie(struct wpa_sm *sm,
  892. const unsigned char *src_addr,
  893. struct wpa_eapol_ie_parse *ie,
  894. const u8 *assoc_resp_mdie)
  895. {
  896. struct rsn_mdie *mdie;
  897. mdie = (struct rsn_mdie *) (ie->mdie + 2);
  898. if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
  899. os_memcmp(mdie->mobility_domain, sm->mobility_domain,
  900. MOBILITY_DOMAIN_ID_LEN) != 0) {
  901. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
  902. "not match with the current mobility domain");
  903. return -1;
  904. }
  905. if (assoc_resp_mdie &&
  906. (assoc_resp_mdie[1] != ie->mdie[1] ||
  907. os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
  908. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
  909. wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
  910. ie->mdie, 2 + ie->mdie[1]);
  911. wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
  912. assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
  913. return -1;
  914. }
  915. return 0;
  916. }
  917. static int ft_validate_ftie(struct wpa_sm *sm,
  918. const unsigned char *src_addr,
  919. struct wpa_eapol_ie_parse *ie,
  920. const u8 *assoc_resp_ftie)
  921. {
  922. if (ie->ftie == NULL) {
  923. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  924. "FT: No FTIE in EAPOL-Key msg 3/4");
  925. return -1;
  926. }
  927. if (assoc_resp_ftie == NULL)
  928. return 0;
  929. if (assoc_resp_ftie[1] != ie->ftie[1] ||
  930. os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
  931. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
  932. wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
  933. ie->ftie, 2 + ie->ftie[1]);
  934. wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
  935. assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
  936. return -1;
  937. }
  938. return 0;
  939. }
  940. static int ft_validate_rsnie(struct wpa_sm *sm,
  941. const unsigned char *src_addr,
  942. struct wpa_eapol_ie_parse *ie)
  943. {
  944. struct wpa_ie_data rsn;
  945. if (!ie->rsn_ie)
  946. return 0;
  947. /*
  948. * Verify that PMKR1Name from EAPOL-Key message 3/4
  949. * matches with the value we derived.
  950. */
  951. if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
  952. rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
  953. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
  954. "FT 4-way handshake message 3/4");
  955. return -1;
  956. }
  957. if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
  958. {
  959. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  960. "FT: PMKR1Name mismatch in "
  961. "FT 4-way handshake message 3/4");
  962. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
  963. rsn.pmkid, WPA_PMK_NAME_LEN);
  964. wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
  965. sm->pmk_r1_name, WPA_PMK_NAME_LEN);
  966. return -1;
  967. }
  968. return 0;
  969. }
  970. static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
  971. const unsigned char *src_addr,
  972. struct wpa_eapol_ie_parse *ie)
  973. {
  974. const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
  975. if (sm->assoc_resp_ies) {
  976. pos = sm->assoc_resp_ies;
  977. end = pos + sm->assoc_resp_ies_len;
  978. while (end - pos > 2) {
  979. if (2 + pos[1] > end - pos)
  980. break;
  981. switch (*pos) {
  982. case WLAN_EID_MOBILITY_DOMAIN:
  983. mdie = pos;
  984. break;
  985. case WLAN_EID_FAST_BSS_TRANSITION:
  986. ftie = pos;
  987. break;
  988. }
  989. pos += 2 + pos[1];
  990. }
  991. }
  992. if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
  993. ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
  994. ft_validate_rsnie(sm, src_addr, ie) < 0)
  995. return -1;
  996. return 0;
  997. }
  998. #endif /* CONFIG_IEEE80211R */
  999. static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
  1000. const unsigned char *src_addr,
  1001. struct wpa_eapol_ie_parse *ie)
  1002. {
  1003. if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
  1004. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1005. "WPA: No WPA/RSN IE for this AP known. "
  1006. "Trying to get from scan results");
  1007. if (wpa_sm_get_beacon_ie(sm) < 0) {
  1008. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1009. "WPA: Could not find AP from "
  1010. "the scan results");
  1011. } else {
  1012. wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
  1013. "WPA: Found the current AP from "
  1014. "updated scan results");
  1015. }
  1016. }
  1017. if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
  1018. (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
  1019. wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
  1020. "with IE in Beacon/ProbeResp (no IE?)",
  1021. src_addr, ie->wpa_ie, ie->wpa_ie_len,
  1022. ie->rsn_ie, ie->rsn_ie_len);
  1023. return -1;
  1024. }
  1025. if ((ie->wpa_ie && sm->ap_wpa_ie &&
  1026. (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
  1027. os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
  1028. (ie->rsn_ie && sm->ap_rsn_ie &&
  1029. wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
  1030. sm->ap_rsn_ie, sm->ap_rsn_ie_len,
  1031. ie->rsn_ie, ie->rsn_ie_len))) {
  1032. wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
  1033. "with IE in Beacon/ProbeResp",
  1034. src_addr, ie->wpa_ie, ie->wpa_ie_len,
  1035. ie->rsn_ie, ie->rsn_ie_len);
  1036. return -1;
  1037. }
  1038. if (sm->proto == WPA_PROTO_WPA &&
  1039. ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
  1040. wpa_report_ie_mismatch(sm, "Possible downgrade attack "
  1041. "detected - RSN was enabled and RSN IE "
  1042. "was in msg 3/4, but not in "
  1043. "Beacon/ProbeResp",
  1044. src_addr, ie->wpa_ie, ie->wpa_ie_len,
  1045. ie->rsn_ie, ie->rsn_ie_len);
  1046. return -1;
  1047. }
  1048. #ifdef CONFIG_IEEE80211R
  1049. if (wpa_key_mgmt_ft(sm->key_mgmt) &&
  1050. wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
  1051. return -1;
  1052. #endif /* CONFIG_IEEE80211R */
  1053. return 0;
  1054. }
  1055. /**
  1056. * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
  1057. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1058. * @dst: Destination address for the frame
  1059. * @key: Pointer to the EAPOL-Key frame header
  1060. * @ver: Version bits from EAPOL-Key Key Info
  1061. * @key_info: Key Info
  1062. * @ptk: PTK to use for keyed hash and encryption
  1063. * Returns: >= 0 on success, < 0 on failure
  1064. */
  1065. int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
  1066. const struct wpa_eapol_key *key,
  1067. u16 ver, u16 key_info,
  1068. struct wpa_ptk *ptk)
  1069. {
  1070. size_t mic_len, hdrlen, rlen;
  1071. struct wpa_eapol_key *reply;
  1072. u8 *rbuf, *key_mic;
  1073. mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
  1074. hdrlen = sizeof(*reply) + mic_len + 2;
  1075. rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
  1076. hdrlen, &rlen, (void *) &reply);
  1077. if (rbuf == NULL)
  1078. return -1;
  1079. reply->type = (sm->proto == WPA_PROTO_RSN ||
  1080. sm->proto == WPA_PROTO_OSEN) ?
  1081. EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
  1082. key_info &= WPA_KEY_INFO_SECURE;
  1083. key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
  1084. if (mic_len)
  1085. key_info |= WPA_KEY_INFO_MIC;
  1086. else
  1087. key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
  1088. WPA_PUT_BE16(reply->key_info, key_info);
  1089. if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
  1090. WPA_PUT_BE16(reply->key_length, 0);
  1091. else
  1092. os_memcpy(reply->key_length, key->key_length, 2);
  1093. os_memcpy(reply->replay_counter, key->replay_counter,
  1094. WPA_REPLAY_COUNTER_LEN);
  1095. key_mic = (u8 *) (reply + 1);
  1096. WPA_PUT_BE16(key_mic + mic_len, 0);
  1097. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
  1098. return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
  1099. key_mic);
  1100. }
  1101. static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
  1102. const struct wpa_eapol_key *key,
  1103. u16 ver, const u8 *key_data,
  1104. size_t key_data_len)
  1105. {
  1106. u16 key_info, keylen;
  1107. struct wpa_eapol_ie_parse ie;
  1108. wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
  1109. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
  1110. "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
  1111. key_info = WPA_GET_BE16(key->key_info);
  1112. wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
  1113. if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
  1114. goto failed;
  1115. if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  1116. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1117. "WPA: GTK IE in unencrypted key data");
  1118. goto failed;
  1119. }
  1120. #ifdef CONFIG_IEEE80211W
  1121. if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  1122. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1123. "WPA: IGTK KDE in unencrypted key data");
  1124. goto failed;
  1125. }
  1126. if (ie.igtk &&
  1127. wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
  1128. ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
  1129. (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
  1130. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1131. "WPA: Invalid IGTK KDE length %lu",
  1132. (unsigned long) ie.igtk_len);
  1133. goto failed;
  1134. }
  1135. #endif /* CONFIG_IEEE80211W */
  1136. if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
  1137. goto failed;
  1138. if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
  1139. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1140. "WPA: ANonce from message 1 of 4-Way Handshake "
  1141. "differs from 3 of 4-Way Handshake - drop packet (src="
  1142. MACSTR ")", MAC2STR(sm->bssid));
  1143. goto failed;
  1144. }
  1145. keylen = WPA_GET_BE16(key->key_length);
  1146. if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
  1147. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1148. "WPA: Invalid %s key length %d (src=" MACSTR
  1149. ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
  1150. MAC2STR(sm->bssid));
  1151. goto failed;
  1152. }
  1153. #ifdef CONFIG_P2P
  1154. if (ie.ip_addr_alloc) {
  1155. os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
  1156. wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
  1157. sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
  1158. }
  1159. #endif /* CONFIG_P2P */
  1160. if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
  1161. &sm->ptk) < 0) {
  1162. goto failed;
  1163. }
  1164. /* SNonce was successfully used in msg 3/4, so mark it to be renewed
  1165. * for the next 4-Way Handshake. If msg 3 is received again, the old
  1166. * SNonce will still be used to avoid changing PTK. */
  1167. sm->renew_snonce = 1;
  1168. if (key_info & WPA_KEY_INFO_INSTALL) {
  1169. if (wpa_supplicant_install_ptk(sm, key))
  1170. goto failed;
  1171. }
  1172. if (key_info & WPA_KEY_INFO_SECURE) {
  1173. wpa_sm_mlme_setprotection(
  1174. sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
  1175. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  1176. eapol_sm_notify_portValid(sm->eapol, TRUE);
  1177. }
  1178. wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
  1179. if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
  1180. wpa_supplicant_key_neg_complete(sm, sm->bssid,
  1181. key_info & WPA_KEY_INFO_SECURE);
  1182. } else if (ie.gtk &&
  1183. wpa_supplicant_pairwise_gtk(sm, key,
  1184. ie.gtk, ie.gtk_len, key_info) < 0) {
  1185. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1186. "RSN: Failed to configure GTK");
  1187. goto failed;
  1188. }
  1189. if (ieee80211w_set_keys(sm, &ie) < 0) {
  1190. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1191. "RSN: Failed to configure IGTK");
  1192. goto failed;
  1193. }
  1194. if (ie.gtk)
  1195. wpa_sm_set_rekey_offload(sm);
  1196. if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt)) {
  1197. struct rsn_pmksa_cache_entry *sa;
  1198. sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
  1199. sm->ptk.kck, sm->ptk.kck_len,
  1200. sm->bssid, sm->own_addr,
  1201. sm->network_ctx, sm->key_mgmt, NULL);
  1202. if (!sm->cur_pmksa)
  1203. sm->cur_pmksa = sa;
  1204. }
  1205. sm->msg_3_of_4_ok = 1;
  1206. return;
  1207. failed:
  1208. wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
  1209. }
  1210. static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
  1211. const u8 *keydata,
  1212. size_t keydatalen,
  1213. u16 key_info,
  1214. struct wpa_gtk_data *gd)
  1215. {
  1216. int maxkeylen;
  1217. struct wpa_eapol_ie_parse ie;
  1218. wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
  1219. keydata, keydatalen);
  1220. if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
  1221. return -1;
  1222. if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  1223. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1224. "WPA: GTK IE in unencrypted key data");
  1225. return -1;
  1226. }
  1227. if (ie.gtk == NULL) {
  1228. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1229. "WPA: No GTK IE in Group Key msg 1/2");
  1230. return -1;
  1231. }
  1232. maxkeylen = gd->gtk_len = ie.gtk_len - 2;
  1233. if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
  1234. gd->gtk_len, maxkeylen,
  1235. &gd->key_rsc_len, &gd->alg))
  1236. return -1;
  1237. wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
  1238. ie.gtk, ie.gtk_len);
  1239. gd->keyidx = ie.gtk[0] & 0x3;
  1240. gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
  1241. !!(ie.gtk[0] & BIT(2)));
  1242. if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
  1243. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1244. "RSN: Too long GTK in GTK IE (len=%lu)",
  1245. (unsigned long) ie.gtk_len - 2);
  1246. return -1;
  1247. }
  1248. os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
  1249. if (ieee80211w_set_keys(sm, &ie) < 0)
  1250. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1251. "RSN: Failed to configure IGTK");
  1252. return 0;
  1253. }
  1254. static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
  1255. const struct wpa_eapol_key *key,
  1256. const u8 *key_data,
  1257. size_t key_data_len, u16 key_info,
  1258. u16 ver, struct wpa_gtk_data *gd)
  1259. {
  1260. size_t maxkeylen;
  1261. u16 gtk_len;
  1262. gtk_len = WPA_GET_BE16(key->key_length);
  1263. maxkeylen = key_data_len;
  1264. if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
  1265. if (maxkeylen < 8) {
  1266. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1267. "WPA: Too short maxkeylen (%lu)",
  1268. (unsigned long) maxkeylen);
  1269. return -1;
  1270. }
  1271. maxkeylen -= 8;
  1272. }
  1273. if (gtk_len > maxkeylen ||
  1274. wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
  1275. gtk_len, maxkeylen,
  1276. &gd->key_rsc_len, &gd->alg))
  1277. return -1;
  1278. gd->gtk_len = gtk_len;
  1279. gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
  1280. WPA_KEY_INFO_KEY_INDEX_SHIFT;
  1281. if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
  1282. #ifdef CONFIG_NO_RC4
  1283. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1284. "WPA: RC4 not supported in the build");
  1285. return -1;
  1286. #else /* CONFIG_NO_RC4 */
  1287. u8 ek[32];
  1288. if (key_data_len > sizeof(gd->gtk)) {
  1289. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1290. "WPA: RC4 key data too long (%lu)",
  1291. (unsigned long) key_data_len);
  1292. return -1;
  1293. }
  1294. os_memcpy(ek, key->key_iv, 16);
  1295. os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
  1296. os_memcpy(gd->gtk, key_data, key_data_len);
  1297. if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
  1298. os_memset(ek, 0, sizeof(ek));
  1299. wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
  1300. "WPA: RC4 failed");
  1301. return -1;
  1302. }
  1303. os_memset(ek, 0, sizeof(ek));
  1304. #endif /* CONFIG_NO_RC4 */
  1305. } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
  1306. if (maxkeylen % 8) {
  1307. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1308. "WPA: Unsupported AES-WRAP len %lu",
  1309. (unsigned long) maxkeylen);
  1310. return -1;
  1311. }
  1312. if (maxkeylen > sizeof(gd->gtk)) {
  1313. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1314. "WPA: AES-WRAP key data "
  1315. "too long (keydatalen=%lu maxkeylen=%lu)",
  1316. (unsigned long) key_data_len,
  1317. (unsigned long) maxkeylen);
  1318. return -1;
  1319. }
  1320. if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
  1321. key_data, gd->gtk)) {
  1322. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1323. "WPA: AES unwrap failed - could not decrypt "
  1324. "GTK");
  1325. return -1;
  1326. }
  1327. } else {
  1328. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1329. "WPA: Unsupported key_info type %d", ver);
  1330. return -1;
  1331. }
  1332. gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
  1333. sm, !!(key_info & WPA_KEY_INFO_TXRX));
  1334. return 0;
  1335. }
  1336. static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
  1337. const struct wpa_eapol_key *key,
  1338. int ver, u16 key_info)
  1339. {
  1340. size_t mic_len, hdrlen, rlen;
  1341. struct wpa_eapol_key *reply;
  1342. u8 *rbuf, *key_mic;
  1343. mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
  1344. hdrlen = sizeof(*reply) + mic_len + 2;
  1345. rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
  1346. hdrlen, &rlen, (void *) &reply);
  1347. if (rbuf == NULL)
  1348. return -1;
  1349. reply->type = (sm->proto == WPA_PROTO_RSN ||
  1350. sm->proto == WPA_PROTO_OSEN) ?
  1351. EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
  1352. key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
  1353. key_info |= ver | WPA_KEY_INFO_SECURE;
  1354. if (mic_len)
  1355. key_info |= WPA_KEY_INFO_MIC;
  1356. else
  1357. key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
  1358. WPA_PUT_BE16(reply->key_info, key_info);
  1359. if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
  1360. WPA_PUT_BE16(reply->key_length, 0);
  1361. else
  1362. os_memcpy(reply->key_length, key->key_length, 2);
  1363. os_memcpy(reply->replay_counter, key->replay_counter,
  1364. WPA_REPLAY_COUNTER_LEN);
  1365. key_mic = (u8 *) (reply + 1);
  1366. WPA_PUT_BE16(key_mic + mic_len, 0);
  1367. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
  1368. return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL,
  1369. rbuf, rlen, key_mic);
  1370. }
  1371. static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
  1372. const unsigned char *src_addr,
  1373. const struct wpa_eapol_key *key,
  1374. const u8 *key_data,
  1375. size_t key_data_len, u16 ver)
  1376. {
  1377. u16 key_info;
  1378. int rekey, ret;
  1379. struct wpa_gtk_data gd;
  1380. const u8 *key_rsc;
  1381. if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
  1382. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1383. "WPA: Group Key Handshake started prior to completion of 4-way handshake");
  1384. goto failed;
  1385. }
  1386. os_memset(&gd, 0, sizeof(gd));
  1387. rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
  1388. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
  1389. "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
  1390. key_info = WPA_GET_BE16(key->key_info);
  1391. if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
  1392. ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
  1393. key_data_len, key_info,
  1394. &gd);
  1395. } else {
  1396. ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
  1397. key_data_len,
  1398. key_info, ver, &gd);
  1399. }
  1400. wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
  1401. if (ret)
  1402. goto failed;
  1403. key_rsc = key->key_rsc;
  1404. if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
  1405. key_rsc = null_rsc;
  1406. if (wpa_supplicant_install_gtk(sm, &gd, key_rsc) ||
  1407. wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
  1408. goto failed;
  1409. os_memset(&gd, 0, sizeof(gd));
  1410. if (rekey) {
  1411. wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
  1412. "completed with " MACSTR " [GTK=%s]",
  1413. MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
  1414. wpa_sm_cancel_auth_timeout(sm);
  1415. wpa_sm_set_state(sm, WPA_COMPLETED);
  1416. } else {
  1417. wpa_supplicant_key_neg_complete(sm, sm->bssid,
  1418. key_info &
  1419. WPA_KEY_INFO_SECURE);
  1420. }
  1421. wpa_sm_set_rekey_offload(sm);
  1422. return;
  1423. failed:
  1424. os_memset(&gd, 0, sizeof(gd));
  1425. wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
  1426. }
  1427. static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
  1428. struct wpa_eapol_key *key,
  1429. u16 ver,
  1430. const u8 *buf, size_t len)
  1431. {
  1432. u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
  1433. int ok = 0;
  1434. size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
  1435. os_memcpy(mic, key + 1, mic_len);
  1436. if (sm->tptk_set) {
  1437. os_memset(key + 1, 0, mic_len);
  1438. wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len, sm->key_mgmt,
  1439. ver, buf, len, (u8 *) (key + 1));
  1440. if (os_memcmp_const(mic, key + 1, mic_len) != 0) {
  1441. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1442. "WPA: Invalid EAPOL-Key MIC "
  1443. "when using TPTK - ignoring TPTK");
  1444. } else {
  1445. ok = 1;
  1446. sm->tptk_set = 0;
  1447. sm->ptk_set = 1;
  1448. os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
  1449. os_memset(&sm->tptk, 0, sizeof(sm->tptk));
  1450. }
  1451. }
  1452. if (!ok && sm->ptk_set) {
  1453. os_memset(key + 1, 0, mic_len);
  1454. wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len, sm->key_mgmt,
  1455. ver, buf, len, (u8 *) (key + 1));
  1456. if (os_memcmp_const(mic, key + 1, mic_len) != 0) {
  1457. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1458. "WPA: Invalid EAPOL-Key MIC - "
  1459. "dropping packet");
  1460. return -1;
  1461. }
  1462. ok = 1;
  1463. }
  1464. if (!ok) {
  1465. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1466. "WPA: Could not verify EAPOL-Key MIC - "
  1467. "dropping packet");
  1468. return -1;
  1469. }
  1470. os_memcpy(sm->rx_replay_counter, key->replay_counter,
  1471. WPA_REPLAY_COUNTER_LEN);
  1472. sm->rx_replay_counter_set = 1;
  1473. return 0;
  1474. }
  1475. /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
  1476. static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
  1477. struct wpa_eapol_key *key,
  1478. size_t mic_len, u16 ver,
  1479. u8 *key_data, size_t *key_data_len)
  1480. {
  1481. wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
  1482. key_data, *key_data_len);
  1483. if (!sm->ptk_set) {
  1484. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1485. "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
  1486. "Data");
  1487. return -1;
  1488. }
  1489. /* Decrypt key data here so that this operation does not need
  1490. * to be implemented separately for each message type. */
  1491. if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
  1492. #ifdef CONFIG_NO_RC4
  1493. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1494. "WPA: RC4 not supported in the build");
  1495. return -1;
  1496. #else /* CONFIG_NO_RC4 */
  1497. u8 ek[32];
  1498. wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
  1499. os_memcpy(ek, key->key_iv, 16);
  1500. os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
  1501. if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
  1502. os_memset(ek, 0, sizeof(ek));
  1503. wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
  1504. "WPA: RC4 failed");
  1505. return -1;
  1506. }
  1507. os_memset(ek, 0, sizeof(ek));
  1508. #endif /* CONFIG_NO_RC4 */
  1509. } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
  1510. ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
  1511. sm->key_mgmt == WPA_KEY_MGMT_OWE ||
  1512. sm->key_mgmt == WPA_KEY_MGMT_DPP ||
  1513. sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
  1514. wpa_key_mgmt_suite_b(sm->key_mgmt)) {
  1515. u8 *buf;
  1516. wpa_printf(MSG_DEBUG,
  1517. "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
  1518. (unsigned int) sm->ptk.kek_len);
  1519. if (*key_data_len < 8 || *key_data_len % 8) {
  1520. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1521. "WPA: Unsupported AES-WRAP len %u",
  1522. (unsigned int) *key_data_len);
  1523. return -1;
  1524. }
  1525. *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
  1526. buf = os_malloc(*key_data_len);
  1527. if (buf == NULL) {
  1528. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1529. "WPA: No memory for AES-UNWRAP buffer");
  1530. return -1;
  1531. }
  1532. if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
  1533. key_data, buf)) {
  1534. bin_clear_free(buf, *key_data_len);
  1535. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1536. "WPA: AES unwrap failed - "
  1537. "could not decrypt EAPOL-Key key data");
  1538. return -1;
  1539. }
  1540. os_memcpy(key_data, buf, *key_data_len);
  1541. bin_clear_free(buf, *key_data_len);
  1542. WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
  1543. } else {
  1544. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1545. "WPA: Unsupported key_info type %d", ver);
  1546. return -1;
  1547. }
  1548. wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
  1549. key_data, *key_data_len);
  1550. return 0;
  1551. }
  1552. /**
  1553. * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
  1554. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1555. */
  1556. void wpa_sm_aborted_cached(struct wpa_sm *sm)
  1557. {
  1558. if (sm && sm->cur_pmksa) {
  1559. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1560. "RSN: Cancelling PMKSA caching attempt");
  1561. sm->cur_pmksa = NULL;
  1562. }
  1563. }
  1564. static void wpa_eapol_key_dump(struct wpa_sm *sm,
  1565. const struct wpa_eapol_key *key,
  1566. unsigned int key_data_len,
  1567. const u8 *mic, unsigned int mic_len)
  1568. {
  1569. #ifndef CONFIG_NO_STDOUT_DEBUG
  1570. u16 key_info = WPA_GET_BE16(key->key_info);
  1571. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
  1572. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1573. " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
  1574. key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
  1575. (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
  1576. WPA_KEY_INFO_KEY_INDEX_SHIFT,
  1577. (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
  1578. key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
  1579. key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
  1580. key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
  1581. key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
  1582. key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
  1583. key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
  1584. key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
  1585. key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
  1586. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1587. " key_length=%u key_data_length=%u",
  1588. WPA_GET_BE16(key->key_length), key_data_len);
  1589. wpa_hexdump(MSG_DEBUG, " replay_counter",
  1590. key->replay_counter, WPA_REPLAY_COUNTER_LEN);
  1591. wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
  1592. wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
  1593. wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
  1594. wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
  1595. wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
  1596. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1597. }
  1598. #ifdef CONFIG_FILS
  1599. static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
  1600. size_t *key_data_len)
  1601. {
  1602. struct wpa_ptk *ptk;
  1603. struct ieee802_1x_hdr *hdr;
  1604. struct wpa_eapol_key *key;
  1605. u8 *pos, *tmp;
  1606. const u8 *aad[1];
  1607. size_t aad_len[1];
  1608. if (*key_data_len < AES_BLOCK_SIZE) {
  1609. wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
  1610. return -1;
  1611. }
  1612. if (sm->tptk_set)
  1613. ptk = &sm->tptk;
  1614. else if (sm->ptk_set)
  1615. ptk = &sm->ptk;
  1616. else
  1617. return -1;
  1618. hdr = (struct ieee802_1x_hdr *) buf;
  1619. key = (struct wpa_eapol_key *) (hdr + 1);
  1620. pos = (u8 *) (key + 1);
  1621. pos += 2; /* Pointing at the Encrypted Key Data field */
  1622. tmp = os_malloc(*key_data_len);
  1623. if (!tmp)
  1624. return -1;
  1625. /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
  1626. * to Key Data (exclusive). */
  1627. aad[0] = buf;
  1628. aad_len[0] = pos - buf;
  1629. if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
  1630. 1, aad, aad_len, tmp) < 0) {
  1631. wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
  1632. bin_clear_free(tmp, *key_data_len);
  1633. return -1;
  1634. }
  1635. /* AEAD decryption and validation completed successfully */
  1636. (*key_data_len) -= AES_BLOCK_SIZE;
  1637. wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
  1638. tmp, *key_data_len);
  1639. /* Replace Key Data field with the decrypted version */
  1640. os_memcpy(pos, tmp, *key_data_len);
  1641. pos -= 2; /* Key Data Length field */
  1642. WPA_PUT_BE16(pos, *key_data_len);
  1643. bin_clear_free(tmp, *key_data_len);
  1644. if (sm->tptk_set) {
  1645. sm->tptk_set = 0;
  1646. sm->ptk_set = 1;
  1647. os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
  1648. os_memset(&sm->tptk, 0, sizeof(sm->tptk));
  1649. }
  1650. os_memcpy(sm->rx_replay_counter, key->replay_counter,
  1651. WPA_REPLAY_COUNTER_LEN);
  1652. sm->rx_replay_counter_set = 1;
  1653. return 0;
  1654. }
  1655. #endif /* CONFIG_FILS */
  1656. /**
  1657. * wpa_sm_rx_eapol - Process received WPA EAPOL frames
  1658. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1659. * @src_addr: Source MAC address of the EAPOL packet
  1660. * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
  1661. * @len: Length of the EAPOL frame
  1662. * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
  1663. *
  1664. * This function is called for each received EAPOL frame. Other than EAPOL-Key
  1665. * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
  1666. * only processing WPA and WPA2 EAPOL-Key frames.
  1667. *
  1668. * The received EAPOL-Key packets are validated and valid packets are replied
  1669. * to. In addition, key material (PTK, GTK) is configured at the end of a
  1670. * successful key handshake.
  1671. */
  1672. int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
  1673. const u8 *buf, size_t len)
  1674. {
  1675. size_t plen, data_len, key_data_len;
  1676. const struct ieee802_1x_hdr *hdr;
  1677. struct wpa_eapol_key *key;
  1678. u16 key_info, ver;
  1679. u8 *tmp = NULL;
  1680. int ret = -1;
  1681. struct wpa_peerkey *peerkey = NULL;
  1682. u8 *mic, *key_data;
  1683. size_t mic_len, keyhdrlen;
  1684. #ifdef CONFIG_IEEE80211R
  1685. sm->ft_completed = 0;
  1686. #endif /* CONFIG_IEEE80211R */
  1687. mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
  1688. keyhdrlen = sizeof(*key) + mic_len + 2;
  1689. if (len < sizeof(*hdr) + keyhdrlen) {
  1690. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1691. "WPA: EAPOL frame too short to be a WPA "
  1692. "EAPOL-Key (len %lu, expecting at least %lu)",
  1693. (unsigned long) len,
  1694. (unsigned long) sizeof(*hdr) + keyhdrlen);
  1695. return 0;
  1696. }
  1697. hdr = (const struct ieee802_1x_hdr *) buf;
  1698. plen = be_to_host16(hdr->length);
  1699. data_len = plen + sizeof(*hdr);
  1700. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1701. "IEEE 802.1X RX: version=%d type=%d length=%lu",
  1702. hdr->version, hdr->type, (unsigned long) plen);
  1703. if (hdr->version < EAPOL_VERSION) {
  1704. /* TODO: backwards compatibility */
  1705. }
  1706. if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
  1707. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1708. "WPA: EAPOL frame (type %u) discarded, "
  1709. "not a Key frame", hdr->type);
  1710. ret = 0;
  1711. goto out;
  1712. }
  1713. wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
  1714. if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
  1715. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1716. "WPA: EAPOL frame payload size %lu "
  1717. "invalid (frame size %lu)",
  1718. (unsigned long) plen, (unsigned long) len);
  1719. ret = 0;
  1720. goto out;
  1721. }
  1722. if (data_len < len) {
  1723. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1724. "WPA: ignoring %lu bytes after the IEEE 802.1X data",
  1725. (unsigned long) len - data_len);
  1726. }
  1727. /*
  1728. * Make a copy of the frame since we need to modify the buffer during
  1729. * MAC validation and Key Data decryption.
  1730. */
  1731. tmp = os_memdup(buf, data_len);
  1732. if (tmp == NULL)
  1733. goto out;
  1734. key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
  1735. mic = (u8 *) (key + 1);
  1736. key_data = mic + mic_len + 2;
  1737. if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
  1738. {
  1739. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1740. "WPA: EAPOL-Key type (%d) unknown, discarded",
  1741. key->type);
  1742. ret = 0;
  1743. goto out;
  1744. }
  1745. key_data_len = WPA_GET_BE16(mic + mic_len);
  1746. wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
  1747. if (key_data_len > plen - keyhdrlen) {
  1748. wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
  1749. "frame - key_data overflow (%u > %u)",
  1750. (unsigned int) key_data_len,
  1751. (unsigned int) (plen - keyhdrlen));
  1752. goto out;
  1753. }
  1754. eapol_sm_notify_lower_layer_success(sm->eapol, 0);
  1755. key_info = WPA_GET_BE16(key->key_info);
  1756. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  1757. if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
  1758. #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
  1759. ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
  1760. #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
  1761. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
  1762. !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
  1763. !wpa_key_mgmt_fils(sm->key_mgmt) &&
  1764. sm->key_mgmt != WPA_KEY_MGMT_OWE &&
  1765. sm->key_mgmt != WPA_KEY_MGMT_DPP &&
  1766. sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
  1767. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1768. "WPA: Unsupported EAPOL-Key descriptor version %d",
  1769. ver);
  1770. goto out;
  1771. }
  1772. if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
  1773. ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
  1774. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1775. "OSEN: Unsupported EAPOL-Key descriptor version %d",
  1776. ver);
  1777. goto out;
  1778. }
  1779. if ((wpa_key_mgmt_suite_b(sm->key_mgmt) ||
  1780. wpa_key_mgmt_fils(sm->key_mgmt) ||
  1781. sm->key_mgmt == WPA_KEY_MGMT_DPP ||
  1782. sm->key_mgmt == WPA_KEY_MGMT_OWE) &&
  1783. ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
  1784. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1785. "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
  1786. ver);
  1787. goto out;
  1788. }
  1789. #ifdef CONFIG_IEEE80211R
  1790. if (wpa_key_mgmt_ft(sm->key_mgmt)) {
  1791. /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
  1792. if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  1793. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1794. "FT: AP did not use AES-128-CMAC");
  1795. goto out;
  1796. }
  1797. } else
  1798. #endif /* CONFIG_IEEE80211R */
  1799. #ifdef CONFIG_IEEE80211W
  1800. if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
  1801. if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
  1802. sm->key_mgmt != WPA_KEY_MGMT_OSEN &&
  1803. !wpa_key_mgmt_fils(sm->key_mgmt) &&
  1804. !wpa_key_mgmt_suite_b(sm->key_mgmt)) {
  1805. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1806. "WPA: AP did not use the "
  1807. "negotiated AES-128-CMAC");
  1808. goto out;
  1809. }
  1810. } else
  1811. #endif /* CONFIG_IEEE80211W */
  1812. if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
  1813. !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
  1814. !wpa_key_mgmt_fils(sm->key_mgmt) &&
  1815. sm->key_mgmt != WPA_KEY_MGMT_OWE &&
  1816. sm->key_mgmt != WPA_KEY_MGMT_DPP &&
  1817. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
  1818. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1819. "WPA: CCMP is used, but EAPOL-Key "
  1820. "descriptor version (%d) is not 2", ver);
  1821. if (sm->group_cipher != WPA_CIPHER_CCMP &&
  1822. !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
  1823. /* Earlier versions of IEEE 802.11i did not explicitly
  1824. * require version 2 descriptor for all EAPOL-Key
  1825. * packets, so allow group keys to use version 1 if
  1826. * CCMP is not used for them. */
  1827. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1828. "WPA: Backwards compatibility: allow invalid "
  1829. "version for non-CCMP group keys");
  1830. } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  1831. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1832. "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
  1833. } else
  1834. goto out;
  1835. } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
  1836. !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
  1837. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
  1838. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1839. "WPA: GCMP is used, but EAPOL-Key "
  1840. "descriptor version (%d) is not 2", ver);
  1841. goto out;
  1842. }
  1843. #ifdef CONFIG_PEERKEY
  1844. for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
  1845. if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
  1846. break;
  1847. }
  1848. if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
  1849. if (!peerkey->initiator && peerkey->replay_counter_set &&
  1850. os_memcmp(key->replay_counter, peerkey->replay_counter,
  1851. WPA_REPLAY_COUNTER_LEN) <= 0) {
  1852. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1853. "RSN: EAPOL-Key Replay Counter did not "
  1854. "increase (STK) - dropping packet");
  1855. goto out;
  1856. } else if (peerkey->initiator) {
  1857. u8 _tmp[WPA_REPLAY_COUNTER_LEN];
  1858. os_memcpy(_tmp, key->replay_counter,
  1859. WPA_REPLAY_COUNTER_LEN);
  1860. inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
  1861. if (os_memcmp(_tmp, peerkey->replay_counter,
  1862. WPA_REPLAY_COUNTER_LEN) != 0) {
  1863. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1864. "RSN: EAPOL-Key Replay "
  1865. "Counter did not match (STK) - "
  1866. "dropping packet");
  1867. goto out;
  1868. }
  1869. }
  1870. }
  1871. if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
  1872. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1873. "RSN: Ack bit in key_info from STK peer");
  1874. goto out;
  1875. }
  1876. #endif /* CONFIG_PEERKEY */
  1877. if (!peerkey && sm->rx_replay_counter_set &&
  1878. os_memcmp(key->replay_counter, sm->rx_replay_counter,
  1879. WPA_REPLAY_COUNTER_LEN) <= 0) {
  1880. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1881. "WPA: EAPOL-Key Replay Counter did not increase - "
  1882. "dropping packet");
  1883. goto out;
  1884. }
  1885. if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
  1886. #ifdef CONFIG_PEERKEY
  1887. && (peerkey == NULL || !peerkey->initiator)
  1888. #endif /* CONFIG_PEERKEY */
  1889. ) {
  1890. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1891. "WPA: No Ack bit in key_info");
  1892. goto out;
  1893. }
  1894. if (key_info & WPA_KEY_INFO_REQUEST) {
  1895. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1896. "WPA: EAPOL-Key with Request bit - dropped");
  1897. goto out;
  1898. }
  1899. if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
  1900. wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
  1901. goto out;
  1902. #ifdef CONFIG_PEERKEY
  1903. if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
  1904. peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp,
  1905. data_len))
  1906. goto out;
  1907. #endif /* CONFIG_PEERKEY */
  1908. #ifdef CONFIG_FILS
  1909. if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  1910. if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
  1911. goto out;
  1912. }
  1913. #endif /* CONFIG_FILS */
  1914. if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
  1915. (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
  1916. if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
  1917. ver, key_data,
  1918. &key_data_len))
  1919. goto out;
  1920. }
  1921. if (key_info & WPA_KEY_INFO_KEY_TYPE) {
  1922. if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
  1923. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1924. "WPA: Ignored EAPOL-Key (Pairwise) with "
  1925. "non-zero key index");
  1926. goto out;
  1927. }
  1928. if (peerkey) {
  1929. /* PeerKey 4-Way Handshake */
  1930. peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver,
  1931. key_data, key_data_len);
  1932. } else if (key_info & (WPA_KEY_INFO_MIC |
  1933. WPA_KEY_INFO_ENCR_KEY_DATA)) {
  1934. /* 3/4 4-Way Handshake */
  1935. wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
  1936. key_data_len);
  1937. } else {
  1938. /* 1/4 4-Way Handshake */
  1939. wpa_supplicant_process_1_of_4(sm, src_addr, key,
  1940. ver, key_data,
  1941. key_data_len);
  1942. }
  1943. } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
  1944. /* PeerKey SMK Handshake */
  1945. peerkey_rx_eapol_smk(sm, src_addr, key, key_data, key_data_len,
  1946. key_info, ver);
  1947. } else {
  1948. if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
  1949. (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
  1950. /* 1/2 Group Key Handshake */
  1951. wpa_supplicant_process_1_of_2(sm, src_addr, key,
  1952. key_data, key_data_len,
  1953. ver);
  1954. } else {
  1955. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1956. "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
  1957. "dropped");
  1958. }
  1959. }
  1960. ret = 1;
  1961. out:
  1962. bin_clear_free(tmp, data_len);
  1963. return ret;
  1964. }
  1965. #ifdef CONFIG_CTRL_IFACE
  1966. static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
  1967. {
  1968. switch (sm->key_mgmt) {
  1969. case WPA_KEY_MGMT_IEEE8021X:
  1970. return ((sm->proto == WPA_PROTO_RSN ||
  1971. sm->proto == WPA_PROTO_OSEN) ?
  1972. RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
  1973. WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
  1974. case WPA_KEY_MGMT_PSK:
  1975. return (sm->proto == WPA_PROTO_RSN ?
  1976. RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
  1977. WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
  1978. #ifdef CONFIG_IEEE80211R
  1979. case WPA_KEY_MGMT_FT_IEEE8021X:
  1980. return RSN_AUTH_KEY_MGMT_FT_802_1X;
  1981. case WPA_KEY_MGMT_FT_PSK:
  1982. return RSN_AUTH_KEY_MGMT_FT_PSK;
  1983. #endif /* CONFIG_IEEE80211R */
  1984. #ifdef CONFIG_IEEE80211W
  1985. case WPA_KEY_MGMT_IEEE8021X_SHA256:
  1986. return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
  1987. case WPA_KEY_MGMT_PSK_SHA256:
  1988. return RSN_AUTH_KEY_MGMT_PSK_SHA256;
  1989. #endif /* CONFIG_IEEE80211W */
  1990. case WPA_KEY_MGMT_CCKM:
  1991. return (sm->proto == WPA_PROTO_RSN ?
  1992. RSN_AUTH_KEY_MGMT_CCKM:
  1993. WPA_AUTH_KEY_MGMT_CCKM);
  1994. case WPA_KEY_MGMT_WPA_NONE:
  1995. return WPA_AUTH_KEY_MGMT_NONE;
  1996. case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
  1997. return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
  1998. case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
  1999. return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
  2000. default:
  2001. return 0;
  2002. }
  2003. }
  2004. #define RSN_SUITE "%02x-%02x-%02x-%d"
  2005. #define RSN_SUITE_ARG(s) \
  2006. ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
  2007. /**
  2008. * wpa_sm_get_mib - Dump text list of MIB entries
  2009. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2010. * @buf: Buffer for the list
  2011. * @buflen: Length of the buffer
  2012. * Returns: Number of bytes written to buffer
  2013. *
  2014. * This function is used fetch dot11 MIB variables.
  2015. */
  2016. int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
  2017. {
  2018. char pmkid_txt[PMKID_LEN * 2 + 1];
  2019. int rsna, ret;
  2020. size_t len;
  2021. if (sm->cur_pmksa) {
  2022. wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
  2023. sm->cur_pmksa->pmkid, PMKID_LEN);
  2024. } else
  2025. pmkid_txt[0] = '\0';
  2026. if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
  2027. wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
  2028. sm->proto == WPA_PROTO_RSN)
  2029. rsna = 1;
  2030. else
  2031. rsna = 0;
  2032. ret = os_snprintf(buf, buflen,
  2033. "dot11RSNAOptionImplemented=TRUE\n"
  2034. "dot11RSNAPreauthenticationImplemented=TRUE\n"
  2035. "dot11RSNAEnabled=%s\n"
  2036. "dot11RSNAPreauthenticationEnabled=%s\n"
  2037. "dot11RSNAConfigVersion=%d\n"
  2038. "dot11RSNAConfigPairwiseKeysSupported=5\n"
  2039. "dot11RSNAConfigGroupCipherSize=%d\n"
  2040. "dot11RSNAConfigPMKLifetime=%d\n"
  2041. "dot11RSNAConfigPMKReauthThreshold=%d\n"
  2042. "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
  2043. "dot11RSNAConfigSATimeout=%d\n",
  2044. rsna ? "TRUE" : "FALSE",
  2045. rsna ? "TRUE" : "FALSE",
  2046. RSN_VERSION,
  2047. wpa_cipher_key_len(sm->group_cipher) * 8,
  2048. sm->dot11RSNAConfigPMKLifetime,
  2049. sm->dot11RSNAConfigPMKReauthThreshold,
  2050. sm->dot11RSNAConfigSATimeout);
  2051. if (os_snprintf_error(buflen, ret))
  2052. return 0;
  2053. len = ret;
  2054. ret = os_snprintf(
  2055. buf + len, buflen - len,
  2056. "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
  2057. "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
  2058. "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
  2059. "dot11RSNAPMKIDUsed=%s\n"
  2060. "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
  2061. "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
  2062. "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
  2063. "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
  2064. "dot11RSNA4WayHandshakeFailures=%u\n",
  2065. RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
  2066. RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
  2067. sm->pairwise_cipher)),
  2068. RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
  2069. sm->group_cipher)),
  2070. pmkid_txt,
  2071. RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
  2072. RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
  2073. sm->pairwise_cipher)),
  2074. RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
  2075. sm->group_cipher)),
  2076. sm->dot11RSNA4WayHandshakeFailures);
  2077. if (!os_snprintf_error(buflen - len, ret))
  2078. len += ret;
  2079. return (int) len;
  2080. }
  2081. #endif /* CONFIG_CTRL_IFACE */
  2082. static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
  2083. void *ctx, enum pmksa_free_reason reason)
  2084. {
  2085. struct wpa_sm *sm = ctx;
  2086. int deauth = 0;
  2087. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
  2088. MACSTR " reason=%d", MAC2STR(entry->aa), reason);
  2089. if (sm->cur_pmksa == entry) {
  2090. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  2091. "RSN: %s current PMKSA entry",
  2092. reason == PMKSA_REPLACE ? "replaced" : "removed");
  2093. pmksa_cache_clear_current(sm);
  2094. /*
  2095. * If an entry is simply being replaced, there's no need to
  2096. * deauthenticate because it will be immediately re-added.
  2097. * This happens when EAP authentication is completed again
  2098. * (reauth or failed PMKSA caching attempt).
  2099. */
  2100. if (reason != PMKSA_REPLACE)
  2101. deauth = 1;
  2102. }
  2103. if (reason == PMKSA_EXPIRE &&
  2104. (sm->pmk_len == entry->pmk_len &&
  2105. os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
  2106. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  2107. "RSN: deauthenticating due to expired PMK");
  2108. pmksa_cache_clear_current(sm);
  2109. deauth = 1;
  2110. }
  2111. if (deauth) {
  2112. os_memset(sm->pmk, 0, sizeof(sm->pmk));
  2113. wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
  2114. }
  2115. }
  2116. /**
  2117. * wpa_sm_init - Initialize WPA state machine
  2118. * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
  2119. * Returns: Pointer to the allocated WPA state machine data
  2120. *
  2121. * This function is used to allocate a new WPA state machine and the returned
  2122. * value is passed to all WPA state machine calls.
  2123. */
  2124. struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
  2125. {
  2126. struct wpa_sm *sm;
  2127. sm = os_zalloc(sizeof(*sm));
  2128. if (sm == NULL)
  2129. return NULL;
  2130. dl_list_init(&sm->pmksa_candidates);
  2131. sm->renew_snonce = 1;
  2132. sm->ctx = ctx;
  2133. sm->dot11RSNAConfigPMKLifetime = 43200;
  2134. sm->dot11RSNAConfigPMKReauthThreshold = 70;
  2135. sm->dot11RSNAConfigSATimeout = 60;
  2136. sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
  2137. if (sm->pmksa == NULL) {
  2138. wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
  2139. "RSN: PMKSA cache initialization failed");
  2140. os_free(sm);
  2141. return NULL;
  2142. }
  2143. return sm;
  2144. }
  2145. /**
  2146. * wpa_sm_deinit - Deinitialize WPA state machine
  2147. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2148. */
  2149. void wpa_sm_deinit(struct wpa_sm *sm)
  2150. {
  2151. if (sm == NULL)
  2152. return;
  2153. pmksa_cache_deinit(sm->pmksa);
  2154. eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
  2155. eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
  2156. os_free(sm->assoc_wpa_ie);
  2157. os_free(sm->ap_wpa_ie);
  2158. os_free(sm->ap_rsn_ie);
  2159. wpa_sm_drop_sa(sm);
  2160. os_free(sm->ctx);
  2161. peerkey_deinit(sm);
  2162. #ifdef CONFIG_IEEE80211R
  2163. os_free(sm->assoc_resp_ies);
  2164. #endif /* CONFIG_IEEE80211R */
  2165. #ifdef CONFIG_TESTING_OPTIONS
  2166. wpabuf_free(sm->test_assoc_ie);
  2167. #endif /* CONFIG_TESTING_OPTIONS */
  2168. #ifdef CONFIG_FILS_SK_PFS
  2169. crypto_ecdh_deinit(sm->fils_ecdh);
  2170. #endif /* CONFIG_FILS_SK_PFS */
  2171. #ifdef CONFIG_FILS
  2172. wpabuf_free(sm->fils_ft_ies);
  2173. #endif /* CONFIG_FILS */
  2174. #ifdef CONFIG_OWE
  2175. crypto_ecdh_deinit(sm->owe_ecdh);
  2176. #endif /* CONFIG_OWE */
  2177. os_free(sm);
  2178. }
  2179. /**
  2180. * wpa_sm_notify_assoc - Notify WPA state machine about association
  2181. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2182. * @bssid: The BSSID of the new association
  2183. *
  2184. * This function is called to let WPA state machine know that the connection
  2185. * was established.
  2186. */
  2187. void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
  2188. {
  2189. int clear_ptk = 1;
  2190. if (sm == NULL)
  2191. return;
  2192. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  2193. "WPA: Association event - clear replay counter");
  2194. os_memcpy(sm->bssid, bssid, ETH_ALEN);
  2195. os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
  2196. sm->rx_replay_counter_set = 0;
  2197. sm->renew_snonce = 1;
  2198. if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
  2199. rsn_preauth_deinit(sm);
  2200. #ifdef CONFIG_IEEE80211R
  2201. if (wpa_ft_is_completed(sm)) {
  2202. /*
  2203. * Clear portValid to kick EAPOL state machine to re-enter
  2204. * AUTHENTICATED state to get the EAPOL port Authorized.
  2205. */
  2206. eapol_sm_notify_portValid(sm->eapol, FALSE);
  2207. wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
  2208. /* Prepare for the next transition */
  2209. wpa_ft_prepare_auth_request(sm, NULL);
  2210. clear_ptk = 0;
  2211. }
  2212. #endif /* CONFIG_IEEE80211R */
  2213. #ifdef CONFIG_FILS
  2214. if (sm->fils_completed) {
  2215. /*
  2216. * Clear portValid to kick EAPOL state machine to re-enter
  2217. * AUTHENTICATED state to get the EAPOL port Authorized.
  2218. */
  2219. wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
  2220. clear_ptk = 0;
  2221. }
  2222. #endif /* CONFIG_FILS */
  2223. if (clear_ptk) {
  2224. /*
  2225. * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
  2226. * this is not part of a Fast BSS Transition.
  2227. */
  2228. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
  2229. sm->ptk_set = 0;
  2230. os_memset(&sm->ptk, 0, sizeof(sm->ptk));
  2231. sm->tptk_set = 0;
  2232. os_memset(&sm->tptk, 0, sizeof(sm->tptk));
  2233. }
  2234. #ifdef CONFIG_TDLS
  2235. wpa_tdls_assoc(sm);
  2236. #endif /* CONFIG_TDLS */
  2237. #ifdef CONFIG_P2P
  2238. os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
  2239. #endif /* CONFIG_P2P */
  2240. }
  2241. /**
  2242. * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
  2243. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2244. *
  2245. * This function is called to let WPA state machine know that the connection
  2246. * was lost. This will abort any existing pre-authentication session.
  2247. */
  2248. void wpa_sm_notify_disassoc(struct wpa_sm *sm)
  2249. {
  2250. eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
  2251. eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
  2252. peerkey_deinit(sm);
  2253. rsn_preauth_deinit(sm);
  2254. pmksa_cache_clear_current(sm);
  2255. if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
  2256. sm->dot11RSNA4WayHandshakeFailures++;
  2257. #ifdef CONFIG_TDLS
  2258. wpa_tdls_disassoc(sm);
  2259. #endif /* CONFIG_TDLS */
  2260. #ifdef CONFIG_FILS
  2261. sm->fils_completed = 0;
  2262. #endif /* CONFIG_FILS */
  2263. /* Keys are not needed in the WPA state machine anymore */
  2264. wpa_sm_drop_sa(sm);
  2265. sm->msg_3_of_4_ok = 0;
  2266. }
  2267. /**
  2268. * wpa_sm_set_pmk - Set PMK
  2269. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2270. * @pmk: The new PMK
  2271. * @pmk_len: The length of the new PMK in bytes
  2272. * @pmkid: Calculated PMKID
  2273. * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
  2274. *
  2275. * Configure the PMK for WPA state machine.
  2276. */
  2277. void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
  2278. const u8 *pmkid, const u8 *bssid)
  2279. {
  2280. if (sm == NULL)
  2281. return;
  2282. sm->pmk_len = pmk_len;
  2283. os_memcpy(sm->pmk, pmk, pmk_len);
  2284. #ifdef CONFIG_IEEE80211R
  2285. /* Set XXKey to be PSK for FT key derivation */
  2286. sm->xxkey_len = pmk_len;
  2287. os_memcpy(sm->xxkey, pmk, pmk_len);
  2288. #endif /* CONFIG_IEEE80211R */
  2289. if (bssid) {
  2290. pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
  2291. bssid, sm->own_addr,
  2292. sm->network_ctx, sm->key_mgmt, NULL);
  2293. }
  2294. }
  2295. /**
  2296. * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
  2297. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2298. *
  2299. * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
  2300. * will be cleared.
  2301. */
  2302. void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
  2303. {
  2304. if (sm == NULL)
  2305. return;
  2306. if (sm->cur_pmksa) {
  2307. sm->pmk_len = sm->cur_pmksa->pmk_len;
  2308. os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
  2309. } else {
  2310. sm->pmk_len = PMK_LEN;
  2311. os_memset(sm->pmk, 0, PMK_LEN);
  2312. }
  2313. }
  2314. /**
  2315. * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
  2316. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2317. * @fast_reauth: Whether fast reauthentication (EAP) is allowed
  2318. */
  2319. void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
  2320. {
  2321. if (sm)
  2322. sm->fast_reauth = fast_reauth;
  2323. }
  2324. /**
  2325. * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
  2326. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2327. * @scard_ctx: Context pointer for smartcard related callback functions
  2328. */
  2329. void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
  2330. {
  2331. if (sm == NULL)
  2332. return;
  2333. sm->scard_ctx = scard_ctx;
  2334. if (sm->preauth_eapol)
  2335. eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
  2336. }
  2337. /**
  2338. * wpa_sm_set_config - Notification of current configration change
  2339. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2340. * @config: Pointer to current network configuration
  2341. *
  2342. * Notify WPA state machine that configuration has changed. config will be
  2343. * stored as a backpointer to network configuration. This can be %NULL to clear
  2344. * the stored pointed.
  2345. */
  2346. void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
  2347. {
  2348. if (!sm)
  2349. return;
  2350. if (config) {
  2351. sm->network_ctx = config->network_ctx;
  2352. sm->peerkey_enabled = config->peerkey_enabled;
  2353. sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
  2354. sm->proactive_key_caching = config->proactive_key_caching;
  2355. sm->eap_workaround = config->eap_workaround;
  2356. sm->eap_conf_ctx = config->eap_conf_ctx;
  2357. if (config->ssid) {
  2358. os_memcpy(sm->ssid, config->ssid, config->ssid_len);
  2359. sm->ssid_len = config->ssid_len;
  2360. } else
  2361. sm->ssid_len = 0;
  2362. sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
  2363. sm->p2p = config->p2p;
  2364. sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
  2365. #ifdef CONFIG_FILS
  2366. if (config->fils_cache_id) {
  2367. sm->fils_cache_id_set = 1;
  2368. os_memcpy(sm->fils_cache_id, config->fils_cache_id,
  2369. FILS_CACHE_ID_LEN);
  2370. } else {
  2371. sm->fils_cache_id_set = 0;
  2372. }
  2373. #endif /* CONFIG_FILS */
  2374. } else {
  2375. sm->network_ctx = NULL;
  2376. sm->peerkey_enabled = 0;
  2377. sm->allowed_pairwise_cipher = 0;
  2378. sm->proactive_key_caching = 0;
  2379. sm->eap_workaround = 0;
  2380. sm->eap_conf_ctx = NULL;
  2381. sm->ssid_len = 0;
  2382. sm->wpa_ptk_rekey = 0;
  2383. sm->p2p = 0;
  2384. sm->wpa_rsc_relaxation = 0;
  2385. }
  2386. }
  2387. /**
  2388. * wpa_sm_set_own_addr - Set own MAC address
  2389. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2390. * @addr: Own MAC address
  2391. */
  2392. void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
  2393. {
  2394. if (sm)
  2395. os_memcpy(sm->own_addr, addr, ETH_ALEN);
  2396. }
  2397. /**
  2398. * wpa_sm_set_ifname - Set network interface name
  2399. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2400. * @ifname: Interface name
  2401. * @bridge_ifname: Optional bridge interface name (for pre-auth)
  2402. */
  2403. void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
  2404. const char *bridge_ifname)
  2405. {
  2406. if (sm) {
  2407. sm->ifname = ifname;
  2408. sm->bridge_ifname = bridge_ifname;
  2409. }
  2410. }
  2411. /**
  2412. * wpa_sm_set_eapol - Set EAPOL state machine pointer
  2413. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2414. * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
  2415. */
  2416. void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
  2417. {
  2418. if (sm)
  2419. sm->eapol = eapol;
  2420. }
  2421. /**
  2422. * wpa_sm_set_param - Set WPA state machine parameters
  2423. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2424. * @param: Parameter field
  2425. * @value: Parameter value
  2426. * Returns: 0 on success, -1 on failure
  2427. */
  2428. int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
  2429. unsigned int value)
  2430. {
  2431. int ret = 0;
  2432. if (sm == NULL)
  2433. return -1;
  2434. switch (param) {
  2435. case RSNA_PMK_LIFETIME:
  2436. if (value > 0)
  2437. sm->dot11RSNAConfigPMKLifetime = value;
  2438. else
  2439. ret = -1;
  2440. break;
  2441. case RSNA_PMK_REAUTH_THRESHOLD:
  2442. if (value > 0 && value <= 100)
  2443. sm->dot11RSNAConfigPMKReauthThreshold = value;
  2444. else
  2445. ret = -1;
  2446. break;
  2447. case RSNA_SA_TIMEOUT:
  2448. if (value > 0)
  2449. sm->dot11RSNAConfigSATimeout = value;
  2450. else
  2451. ret = -1;
  2452. break;
  2453. case WPA_PARAM_PROTO:
  2454. sm->proto = value;
  2455. break;
  2456. case WPA_PARAM_PAIRWISE:
  2457. sm->pairwise_cipher = value;
  2458. break;
  2459. case WPA_PARAM_GROUP:
  2460. sm->group_cipher = value;
  2461. break;
  2462. case WPA_PARAM_KEY_MGMT:
  2463. sm->key_mgmt = value;
  2464. break;
  2465. #ifdef CONFIG_IEEE80211W
  2466. case WPA_PARAM_MGMT_GROUP:
  2467. sm->mgmt_group_cipher = value;
  2468. break;
  2469. #endif /* CONFIG_IEEE80211W */
  2470. case WPA_PARAM_RSN_ENABLED:
  2471. sm->rsn_enabled = value;
  2472. break;
  2473. case WPA_PARAM_MFP:
  2474. sm->mfp = value;
  2475. break;
  2476. default:
  2477. break;
  2478. }
  2479. return ret;
  2480. }
  2481. /**
  2482. * wpa_sm_get_status - Get WPA state machine
  2483. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2484. * @buf: Buffer for status information
  2485. * @buflen: Maximum buffer length
  2486. * @verbose: Whether to include verbose status information
  2487. * Returns: Number of bytes written to buf.
  2488. *
  2489. * Query WPA state machine for status information. This function fills in
  2490. * a text area with current status information. If the buffer (buf) is not
  2491. * large enough, status information will be truncated to fit the buffer.
  2492. */
  2493. int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
  2494. int verbose)
  2495. {
  2496. char *pos = buf, *end = buf + buflen;
  2497. int ret;
  2498. ret = os_snprintf(pos, end - pos,
  2499. "pairwise_cipher=%s\n"
  2500. "group_cipher=%s\n"
  2501. "key_mgmt=%s\n",
  2502. wpa_cipher_txt(sm->pairwise_cipher),
  2503. wpa_cipher_txt(sm->group_cipher),
  2504. wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
  2505. if (os_snprintf_error(end - pos, ret))
  2506. return pos - buf;
  2507. pos += ret;
  2508. if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
  2509. struct wpa_ie_data rsn;
  2510. if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
  2511. >= 0 &&
  2512. rsn.capabilities & (WPA_CAPABILITY_MFPR |
  2513. WPA_CAPABILITY_MFPC)) {
  2514. ret = os_snprintf(pos, end - pos, "pmf=%d\n",
  2515. (rsn.capabilities &
  2516. WPA_CAPABILITY_MFPR) ? 2 : 1);
  2517. if (os_snprintf_error(end - pos, ret))
  2518. return pos - buf;
  2519. pos += ret;
  2520. }
  2521. }
  2522. return pos - buf;
  2523. }
  2524. int wpa_sm_pmf_enabled(struct wpa_sm *sm)
  2525. {
  2526. struct wpa_ie_data rsn;
  2527. if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
  2528. return 0;
  2529. if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
  2530. rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
  2531. return 1;
  2532. return 0;
  2533. }
  2534. /**
  2535. * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
  2536. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2537. * @wpa_ie: Pointer to buffer for WPA/RSN IE
  2538. * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
  2539. * Returns: 0 on success, -1 on failure
  2540. */
  2541. int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
  2542. size_t *wpa_ie_len)
  2543. {
  2544. int res;
  2545. if (sm == NULL)
  2546. return -1;
  2547. #ifdef CONFIG_TESTING_OPTIONS
  2548. if (sm->test_assoc_ie) {
  2549. wpa_printf(MSG_DEBUG,
  2550. "TESTING: Replace association WPA/RSN IE");
  2551. if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
  2552. return -1;
  2553. os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
  2554. wpabuf_len(sm->test_assoc_ie));
  2555. res = wpabuf_len(sm->test_assoc_ie);
  2556. } else
  2557. #endif /* CONFIG_TESTING_OPTIONS */
  2558. res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
  2559. if (res < 0)
  2560. return -1;
  2561. *wpa_ie_len = res;
  2562. wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
  2563. wpa_ie, *wpa_ie_len);
  2564. if (sm->assoc_wpa_ie == NULL) {
  2565. /*
  2566. * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
  2567. * the correct version of the IE even if PMKSA caching is
  2568. * aborted (which would remove PMKID from IE generation).
  2569. */
  2570. sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
  2571. if (sm->assoc_wpa_ie == NULL)
  2572. return -1;
  2573. sm->assoc_wpa_ie_len = *wpa_ie_len;
  2574. } else {
  2575. wpa_hexdump(MSG_DEBUG,
  2576. "WPA: Leave previously set WPA IE default",
  2577. sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
  2578. }
  2579. return 0;
  2580. }
  2581. /**
  2582. * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
  2583. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2584. * @ie: Pointer to IE data (starting from id)
  2585. * @len: IE length
  2586. * Returns: 0 on success, -1 on failure
  2587. *
  2588. * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
  2589. * Request frame. The IE will be used to override the default value generated
  2590. * with wpa_sm_set_assoc_wpa_ie_default().
  2591. */
  2592. int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
  2593. {
  2594. if (sm == NULL)
  2595. return -1;
  2596. os_free(sm->assoc_wpa_ie);
  2597. if (ie == NULL || len == 0) {
  2598. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  2599. "WPA: clearing own WPA/RSN IE");
  2600. sm->assoc_wpa_ie = NULL;
  2601. sm->assoc_wpa_ie_len = 0;
  2602. } else {
  2603. wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
  2604. sm->assoc_wpa_ie = os_memdup(ie, len);
  2605. if (sm->assoc_wpa_ie == NULL)
  2606. return -1;
  2607. sm->assoc_wpa_ie_len = len;
  2608. }
  2609. return 0;
  2610. }
  2611. /**
  2612. * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
  2613. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2614. * @ie: Pointer to IE data (starting from id)
  2615. * @len: IE length
  2616. * Returns: 0 on success, -1 on failure
  2617. *
  2618. * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
  2619. * frame.
  2620. */
  2621. int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
  2622. {
  2623. if (sm == NULL)
  2624. return -1;
  2625. os_free(sm->ap_wpa_ie);
  2626. if (ie == NULL || len == 0) {
  2627. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  2628. "WPA: clearing AP WPA IE");
  2629. sm->ap_wpa_ie = NULL;
  2630. sm->ap_wpa_ie_len = 0;
  2631. } else {
  2632. wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
  2633. sm->ap_wpa_ie = os_memdup(ie, len);
  2634. if (sm->ap_wpa_ie == NULL)
  2635. return -1;
  2636. sm->ap_wpa_ie_len = len;
  2637. }
  2638. return 0;
  2639. }
  2640. /**
  2641. * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
  2642. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2643. * @ie: Pointer to IE data (starting from id)
  2644. * @len: IE length
  2645. * Returns: 0 on success, -1 on failure
  2646. *
  2647. * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
  2648. * frame.
  2649. */
  2650. int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
  2651. {
  2652. if (sm == NULL)
  2653. return -1;
  2654. os_free(sm->ap_rsn_ie);
  2655. if (ie == NULL || len == 0) {
  2656. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  2657. "WPA: clearing AP RSN IE");
  2658. sm->ap_rsn_ie = NULL;
  2659. sm->ap_rsn_ie_len = 0;
  2660. } else {
  2661. wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
  2662. sm->ap_rsn_ie = os_memdup(ie, len);
  2663. if (sm->ap_rsn_ie == NULL)
  2664. return -1;
  2665. sm->ap_rsn_ie_len = len;
  2666. }
  2667. return 0;
  2668. }
  2669. /**
  2670. * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
  2671. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2672. * @data: Pointer to data area for parsing results
  2673. * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
  2674. *
  2675. * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
  2676. * parsed data into data.
  2677. */
  2678. int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
  2679. {
  2680. if (sm == NULL)
  2681. return -1;
  2682. if (sm->assoc_wpa_ie == NULL) {
  2683. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  2684. "WPA: No WPA/RSN IE available from association info");
  2685. return -1;
  2686. }
  2687. if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
  2688. return -2;
  2689. return 0;
  2690. }
  2691. int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
  2692. {
  2693. return pmksa_cache_list(sm->pmksa, buf, len);
  2694. }
  2695. struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
  2696. {
  2697. return pmksa_cache_head(sm->pmksa);
  2698. }
  2699. struct rsn_pmksa_cache_entry *
  2700. wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
  2701. struct rsn_pmksa_cache_entry * entry)
  2702. {
  2703. return pmksa_cache_add_entry(sm->pmksa, entry);
  2704. }
  2705. void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
  2706. const u8 *pmkid, const u8 *bssid,
  2707. const u8 *fils_cache_id)
  2708. {
  2709. sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
  2710. bssid, sm->own_addr, sm->network_ctx,
  2711. sm->key_mgmt, fils_cache_id);
  2712. }
  2713. int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid,
  2714. const void *network_ctx)
  2715. {
  2716. return pmksa_cache_get(sm->pmksa, bssid, NULL, network_ctx) != NULL;
  2717. }
  2718. void wpa_sm_drop_sa(struct wpa_sm *sm)
  2719. {
  2720. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
  2721. sm->ptk_set = 0;
  2722. sm->tptk_set = 0;
  2723. os_memset(sm->pmk, 0, sizeof(sm->pmk));
  2724. os_memset(&sm->ptk, 0, sizeof(sm->ptk));
  2725. os_memset(&sm->tptk, 0, sizeof(sm->tptk));
  2726. #ifdef CONFIG_IEEE80211R
  2727. os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
  2728. os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
  2729. os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
  2730. #endif /* CONFIG_IEEE80211R */
  2731. }
  2732. int wpa_sm_has_ptk(struct wpa_sm *sm)
  2733. {
  2734. if (sm == NULL)
  2735. return 0;
  2736. return sm->ptk_set;
  2737. }
  2738. void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
  2739. {
  2740. os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
  2741. }
  2742. void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
  2743. {
  2744. pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
  2745. }
  2746. #ifdef CONFIG_WNM
  2747. int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
  2748. {
  2749. u16 keyinfo;
  2750. u8 keylen; /* plaintext key len */
  2751. u8 *key_rsc;
  2752. if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
  2753. struct wpa_gtk_data gd;
  2754. os_memset(&gd, 0, sizeof(gd));
  2755. keylen = wpa_cipher_key_len(sm->group_cipher);
  2756. gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
  2757. gd.alg = wpa_cipher_to_alg(sm->group_cipher);
  2758. if (gd.alg == WPA_ALG_NONE) {
  2759. wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
  2760. return -1;
  2761. }
  2762. key_rsc = buf + 5;
  2763. keyinfo = WPA_GET_LE16(buf + 2);
  2764. gd.gtk_len = keylen;
  2765. if (gd.gtk_len != buf[4]) {
  2766. wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
  2767. gd.gtk_len, buf[4]);
  2768. return -1;
  2769. }
  2770. gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
  2771. gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
  2772. sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
  2773. os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
  2774. wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
  2775. gd.gtk, gd.gtk_len);
  2776. if (wpa_supplicant_install_gtk(sm, &gd, key_rsc)) {
  2777. os_memset(&gd, 0, sizeof(gd));
  2778. wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
  2779. "WNM mode");
  2780. return -1;
  2781. }
  2782. os_memset(&gd, 0, sizeof(gd));
  2783. #ifdef CONFIG_IEEE80211W
  2784. } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
  2785. struct wpa_igtk_kde igd;
  2786. u16 keyidx;
  2787. os_memset(&igd, 0, sizeof(igd));
  2788. keylen = wpa_cipher_key_len(sm->mgmt_group_cipher);
  2789. os_memcpy(igd.keyid, buf + 2, 2);
  2790. os_memcpy(igd.pn, buf + 4, 6);
  2791. keyidx = WPA_GET_LE16(igd.keyid);
  2792. os_memcpy(igd.igtk, buf + 10, keylen);
  2793. wpa_hexdump_key(MSG_DEBUG, "Install IGTK (WNM SLEEP)",
  2794. igd.igtk, keylen);
  2795. if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
  2796. broadcast_ether_addr,
  2797. keyidx, 0, igd.pn, sizeof(igd.pn),
  2798. igd.igtk, keylen) < 0) {
  2799. wpa_printf(MSG_DEBUG, "Failed to install the IGTK in "
  2800. "WNM mode");
  2801. os_memset(&igd, 0, sizeof(igd));
  2802. return -1;
  2803. }
  2804. os_memset(&igd, 0, sizeof(igd));
  2805. #endif /* CONFIG_IEEE80211W */
  2806. } else {
  2807. wpa_printf(MSG_DEBUG, "Unknown element id");
  2808. return -1;
  2809. }
  2810. return 0;
  2811. }
  2812. #endif /* CONFIG_WNM */
  2813. #ifdef CONFIG_PEERKEY
  2814. int wpa_sm_rx_eapol_peerkey(struct wpa_sm *sm, const u8 *src_addr,
  2815. const u8 *buf, size_t len)
  2816. {
  2817. struct wpa_peerkey *peerkey;
  2818. for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
  2819. if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
  2820. break;
  2821. }
  2822. if (!peerkey)
  2823. return 0;
  2824. wpa_sm_rx_eapol(sm, src_addr, buf, len);
  2825. return 1;
  2826. }
  2827. #endif /* CONFIG_PEERKEY */
  2828. #ifdef CONFIG_P2P
  2829. int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
  2830. {
  2831. if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
  2832. return -1;
  2833. os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
  2834. return 0;
  2835. }
  2836. #endif /* CONFIG_P2P */
  2837. void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
  2838. {
  2839. if (rx_replay_counter == NULL)
  2840. return;
  2841. os_memcpy(sm->rx_replay_counter, rx_replay_counter,
  2842. WPA_REPLAY_COUNTER_LEN);
  2843. sm->rx_replay_counter_set = 1;
  2844. wpa_printf(MSG_DEBUG, "Updated key replay counter");
  2845. }
  2846. void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
  2847. const u8 *ptk_kck, size_t ptk_kck_len,
  2848. const u8 *ptk_kek, size_t ptk_kek_len)
  2849. {
  2850. if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
  2851. os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
  2852. sm->ptk.kck_len = ptk_kck_len;
  2853. wpa_printf(MSG_DEBUG, "Updated PTK KCK");
  2854. }
  2855. if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
  2856. os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
  2857. sm->ptk.kek_len = ptk_kek_len;
  2858. wpa_printf(MSG_DEBUG, "Updated PTK KEK");
  2859. }
  2860. sm->ptk_set = 1;
  2861. }
  2862. #ifdef CONFIG_TESTING_OPTIONS
  2863. void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
  2864. {
  2865. wpabuf_free(sm->test_assoc_ie);
  2866. sm->test_assoc_ie = buf;
  2867. }
  2868. #endif /* CONFIG_TESTING_OPTIONS */
  2869. #ifdef CONFIG_FILS
  2870. struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
  2871. {
  2872. struct wpabuf *buf = NULL;
  2873. struct wpabuf *erp_msg;
  2874. struct wpabuf *pub = NULL;
  2875. erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
  2876. if (!erp_msg && !sm->cur_pmksa) {
  2877. wpa_printf(MSG_DEBUG,
  2878. "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
  2879. goto fail;
  2880. }
  2881. wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
  2882. erp_msg != NULL, sm->cur_pmksa != NULL);
  2883. sm->fils_completed = 0;
  2884. if (!sm->assoc_wpa_ie) {
  2885. wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
  2886. goto fail;
  2887. }
  2888. if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
  2889. random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
  2890. goto fail;
  2891. wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
  2892. sm->fils_nonce, FILS_NONCE_LEN);
  2893. wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
  2894. sm->fils_session, FILS_SESSION_LEN);
  2895. #ifdef CONFIG_FILS_SK_PFS
  2896. sm->fils_dh_group = dh_group;
  2897. if (dh_group) {
  2898. crypto_ecdh_deinit(sm->fils_ecdh);
  2899. sm->fils_ecdh = crypto_ecdh_init(dh_group);
  2900. if (!sm->fils_ecdh) {
  2901. wpa_printf(MSG_INFO,
  2902. "FILS: Could not initialize ECDH with group %d",
  2903. dh_group);
  2904. goto fail;
  2905. }
  2906. pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
  2907. if (!pub)
  2908. goto fail;
  2909. wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
  2910. pub);
  2911. sm->fils_dh_elem_len = wpabuf_len(pub);
  2912. }
  2913. #endif /* CONFIG_FILS_SK_PFS */
  2914. buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
  2915. (pub ? wpabuf_len(pub) : 0));
  2916. if (!buf)
  2917. goto fail;
  2918. /* Fields following the Authentication algorithm number field */
  2919. /* Authentication Transaction seq# */
  2920. wpabuf_put_le16(buf, 1);
  2921. /* Status Code */
  2922. wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
  2923. /* TODO: FILS PK */
  2924. #ifdef CONFIG_FILS_SK_PFS
  2925. if (dh_group) {
  2926. /* Finite Cyclic Group */
  2927. wpabuf_put_le16(buf, dh_group);
  2928. /* Element */
  2929. wpabuf_put_buf(buf, pub);
  2930. }
  2931. #endif /* CONFIG_FILS_SK_PFS */
  2932. /* RSNE */
  2933. wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
  2934. sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
  2935. wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
  2936. if (md) {
  2937. /* MDE when using FILS for FT initial association */
  2938. struct rsn_mdie *mdie;
  2939. wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
  2940. wpabuf_put_u8(buf, sizeof(*mdie));
  2941. mdie = wpabuf_put(buf, sizeof(*mdie));
  2942. os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
  2943. mdie->ft_capab = 0;
  2944. }
  2945. /* FILS Nonce */
  2946. wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
  2947. wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
  2948. /* Element ID Extension */
  2949. wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
  2950. wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
  2951. /* FILS Session */
  2952. wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
  2953. wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
  2954. /* Element ID Extension */
  2955. wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
  2956. wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
  2957. /* FILS Wrapped Data */
  2958. sm->fils_erp_pmkid_set = 0;
  2959. if (erp_msg) {
  2960. wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
  2961. wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
  2962. /* Element ID Extension */
  2963. wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_WRAPPED_DATA);
  2964. wpabuf_put_buf(buf, erp_msg);
  2965. /* Calculate pending PMKID here so that we do not need to
  2966. * maintain a copy of the EAP-Initiate/Reauth message. */
  2967. if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
  2968. wpabuf_len(erp_msg),
  2969. sm->fils_erp_pmkid) == 0)
  2970. sm->fils_erp_pmkid_set = 1;
  2971. }
  2972. wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
  2973. buf);
  2974. fail:
  2975. wpabuf_free(erp_msg);
  2976. wpabuf_free(pub);
  2977. return buf;
  2978. }
  2979. int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
  2980. size_t len)
  2981. {
  2982. const u8 *pos, *end;
  2983. struct ieee802_11_elems elems;
  2984. struct wpa_ie_data rsn;
  2985. int pmkid_match = 0;
  2986. u8 ick[FILS_ICK_MAX_LEN];
  2987. size_t ick_len;
  2988. int res;
  2989. struct wpabuf *dh_ss = NULL;
  2990. const u8 *g_sta = NULL;
  2991. size_t g_sta_len = 0;
  2992. const u8 *g_ap = NULL;
  2993. size_t g_ap_len = 0;
  2994. struct wpabuf *pub = NULL;
  2995. os_memcpy(sm->bssid, bssid, ETH_ALEN);
  2996. wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
  2997. data, len);
  2998. pos = data;
  2999. end = data + len;
  3000. /* TODO: FILS PK */
  3001. #ifdef CONFIG_FILS_SK_PFS
  3002. if (sm->fils_dh_group) {
  3003. u16 group;
  3004. /* Using FILS PFS */
  3005. /* Finite Cyclic Group */
  3006. if (end - pos < 2) {
  3007. wpa_printf(MSG_DEBUG,
  3008. "FILS: No room for Finite Cyclic Group");
  3009. goto fail;
  3010. }
  3011. group = WPA_GET_LE16(pos);
  3012. pos += 2;
  3013. if (group != sm->fils_dh_group) {
  3014. wpa_printf(MSG_DEBUG,
  3015. "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
  3016. group, sm->fils_dh_group);
  3017. goto fail;
  3018. }
  3019. /* Element */
  3020. if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
  3021. wpa_printf(MSG_DEBUG, "FILS: No room for Element");
  3022. goto fail;
  3023. }
  3024. if (!sm->fils_ecdh) {
  3025. wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
  3026. goto fail;
  3027. }
  3028. dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
  3029. sm->fils_dh_elem_len);
  3030. if (!dh_ss) {
  3031. wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
  3032. goto fail;
  3033. }
  3034. wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
  3035. g_ap = pos;
  3036. g_ap_len = sm->fils_dh_elem_len;
  3037. pos += sm->fils_dh_elem_len;
  3038. }
  3039. #endif /* CONFIG_FILS_SK_PFS */
  3040. wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
  3041. if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
  3042. wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
  3043. goto fail;
  3044. }
  3045. /* RSNE */
  3046. wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
  3047. elems.rsn_ie_len);
  3048. if (!elems.rsn_ie ||
  3049. wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
  3050. &rsn) < 0) {
  3051. wpa_printf(MSG_DEBUG, "FILS: No RSN element");
  3052. goto fail;
  3053. }
  3054. if (!elems.fils_nonce) {
  3055. wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
  3056. goto fail;
  3057. }
  3058. os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
  3059. wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
  3060. if (wpa_key_mgmt_ft(sm->key_mgmt)) {
  3061. struct wpa_ft_ies parse;
  3062. if (!elems.mdie || !elems.ftie) {
  3063. wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
  3064. goto fail;
  3065. }
  3066. if (wpa_ft_parse_ies(pos, end - pos, &parse) < 0) {
  3067. wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
  3068. goto fail;
  3069. }
  3070. if (!parse.r0kh_id) {
  3071. wpa_printf(MSG_DEBUG,
  3072. "FILS+FT: No R0KH-ID subelem in FTE");
  3073. goto fail;
  3074. }
  3075. os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
  3076. sm->r0kh_id_len = parse.r0kh_id_len;
  3077. wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
  3078. sm->r0kh_id, sm->r0kh_id_len);
  3079. if (!parse.r1kh_id) {
  3080. wpa_printf(MSG_DEBUG,
  3081. "FILS+FT: No R1KH-ID subelem in FTE");
  3082. goto fail;
  3083. }
  3084. os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
  3085. wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
  3086. sm->r1kh_id, FT_R1KH_ID_LEN);
  3087. /* TODO: Check MDE and FTE payload */
  3088. wpabuf_free(sm->fils_ft_ies);
  3089. sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
  3090. 2 + elems.ftie_len);
  3091. if (!sm->fils_ft_ies)
  3092. goto fail;
  3093. wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
  3094. 2 + elems.mdie_len);
  3095. wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
  3096. 2 + elems.ftie_len);
  3097. } else {
  3098. wpabuf_free(sm->fils_ft_ies);
  3099. sm->fils_ft_ies = NULL;
  3100. }
  3101. /* PMKID List */
  3102. if (rsn.pmkid && rsn.num_pmkid > 0) {
  3103. wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
  3104. rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
  3105. if (rsn.num_pmkid != 1) {
  3106. wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
  3107. goto fail;
  3108. }
  3109. wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
  3110. if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
  3111. {
  3112. wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
  3113. wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
  3114. sm->cur_pmksa->pmkid, PMKID_LEN);
  3115. goto fail;
  3116. }
  3117. wpa_printf(MSG_DEBUG,
  3118. "FILS: Matching PMKID - continue using PMKSA caching");
  3119. pmkid_match = 1;
  3120. }
  3121. if (!pmkid_match && sm->cur_pmksa) {
  3122. wpa_printf(MSG_DEBUG,
  3123. "FILS: No PMKID match - cannot use cached PMKSA entry");
  3124. sm->cur_pmksa = NULL;
  3125. }
  3126. /* FILS Session */
  3127. if (!elems.fils_session) {
  3128. wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
  3129. goto fail;
  3130. }
  3131. wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
  3132. FILS_SESSION_LEN);
  3133. if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
  3134. != 0) {
  3135. wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
  3136. wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
  3137. sm->fils_session, FILS_SESSION_LEN);
  3138. goto fail;
  3139. }
  3140. /* FILS Wrapped Data */
  3141. if (!sm->cur_pmksa && elems.fils_wrapped_data) {
  3142. u8 rmsk[ERP_MAX_KEY_LEN];
  3143. size_t rmsk_len;
  3144. wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
  3145. elems.fils_wrapped_data,
  3146. elems.fils_wrapped_data_len);
  3147. eapol_sm_process_erp_finish(sm->eapol, elems.fils_wrapped_data,
  3148. elems.fils_wrapped_data_len);
  3149. if (eapol_sm_failed(sm->eapol))
  3150. goto fail;
  3151. rmsk_len = ERP_MAX_KEY_LEN;
  3152. res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
  3153. if (res == PMK_LEN) {
  3154. rmsk_len = PMK_LEN;
  3155. res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
  3156. }
  3157. if (res)
  3158. goto fail;
  3159. res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
  3160. sm->fils_nonce, sm->fils_anonce,
  3161. dh_ss ? wpabuf_head(dh_ss) : NULL,
  3162. dh_ss ? wpabuf_len(dh_ss) : 0,
  3163. sm->pmk, &sm->pmk_len);
  3164. os_memset(rmsk, 0, sizeof(rmsk));
  3165. wpabuf_clear_free(dh_ss);
  3166. dh_ss = NULL;
  3167. if (res)
  3168. goto fail;
  3169. if (!sm->fils_erp_pmkid_set) {
  3170. wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
  3171. goto fail;
  3172. }
  3173. wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
  3174. PMKID_LEN);
  3175. wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
  3176. sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
  3177. sm->fils_erp_pmkid, NULL, 0,
  3178. sm->bssid, sm->own_addr,
  3179. sm->network_ctx, sm->key_mgmt,
  3180. NULL);
  3181. }
  3182. if (!sm->cur_pmksa) {
  3183. wpa_printf(MSG_DEBUG,
  3184. "FILS: No remaining options to continue FILS authentication");
  3185. goto fail;
  3186. }
  3187. if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid,
  3188. sm->fils_nonce, sm->fils_anonce, &sm->ptk,
  3189. ick, &ick_len, sm->key_mgmt, sm->pairwise_cipher,
  3190. sm->fils_ft, &sm->fils_ft_len) < 0) {
  3191. wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
  3192. goto fail;
  3193. }
  3194. sm->ptk_set = 1;
  3195. sm->tptk_set = 0;
  3196. os_memset(&sm->tptk, 0, sizeof(sm->tptk));
  3197. #ifdef CONFIG_FILS_SK_PFS
  3198. if (sm->fils_dh_group) {
  3199. if (!sm->fils_ecdh) {
  3200. wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
  3201. goto fail;
  3202. }
  3203. pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
  3204. if (!pub)
  3205. goto fail;
  3206. wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
  3207. g_sta = wpabuf_head(pub);
  3208. g_sta_len = wpabuf_len(pub);
  3209. if (!g_ap) {
  3210. wpa_printf(MSG_INFO, "FILS: gAP not available");
  3211. goto fail;
  3212. }
  3213. wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
  3214. }
  3215. #endif /* CONFIG_FILS_SK_PFS */
  3216. res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
  3217. sm->fils_anonce, sm->own_addr, sm->bssid,
  3218. g_sta, g_sta_len, g_ap, g_ap_len,
  3219. sm->key_mgmt, sm->fils_key_auth_sta,
  3220. sm->fils_key_auth_ap,
  3221. &sm->fils_key_auth_len);
  3222. wpabuf_free(pub);
  3223. os_memset(ick, 0, sizeof(ick));
  3224. return res;
  3225. fail:
  3226. wpabuf_free(pub);
  3227. wpabuf_clear_free(dh_ss);
  3228. return -1;
  3229. }
  3230. #ifdef CONFIG_IEEE80211R
  3231. static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
  3232. {
  3233. struct rsn_ie_hdr *rsnie;
  3234. u16 capab;
  3235. u8 *pos;
  3236. /* RSNIE[PMKR0Name/PMKR1Name] */
  3237. rsnie = wpabuf_put(buf, sizeof(*rsnie));
  3238. rsnie->elem_id = WLAN_EID_RSN;
  3239. WPA_PUT_LE16(rsnie->version, RSN_VERSION);
  3240. /* Group Suite Selector */
  3241. if (!wpa_cipher_valid_group(sm->group_cipher)) {
  3242. wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
  3243. sm->group_cipher);
  3244. return -1;
  3245. }
  3246. pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
  3247. RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
  3248. sm->group_cipher));
  3249. /* Pairwise Suite Count */
  3250. wpabuf_put_le16(buf, 1);
  3251. /* Pairwise Suite List */
  3252. if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
  3253. wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
  3254. sm->pairwise_cipher);
  3255. return -1;
  3256. }
  3257. pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
  3258. RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
  3259. sm->pairwise_cipher));
  3260. /* Authenticated Key Management Suite Count */
  3261. wpabuf_put_le16(buf, 1);
  3262. /* Authenticated Key Management Suite List */
  3263. pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
  3264. if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
  3265. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
  3266. else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
  3267. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
  3268. else {
  3269. wpa_printf(MSG_WARNING,
  3270. "FILS+FT: Invalid key management type (%d)",
  3271. sm->key_mgmt);
  3272. return -1;
  3273. }
  3274. /* RSN Capabilities */
  3275. capab = 0;
  3276. #ifdef CONFIG_IEEE80211W
  3277. if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC)
  3278. capab |= WPA_CAPABILITY_MFPC;
  3279. #endif /* CONFIG_IEEE80211W */
  3280. wpabuf_put_le16(buf, capab);
  3281. /* PMKID Count */
  3282. wpabuf_put_le16(buf, 1);
  3283. /* PMKID List [PMKR1Name] */
  3284. wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
  3285. sm->fils_ft, sm->fils_ft_len);
  3286. wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
  3287. wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
  3288. sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
  3289. wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
  3290. sm->r0kh_id, sm->r0kh_id_len);
  3291. if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
  3292. sm->ssid_len, sm->mobility_domain,
  3293. sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
  3294. sm->pmk_r0, sm->pmk_r0_name) < 0) {
  3295. wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
  3296. return -1;
  3297. }
  3298. wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0", sm->pmk_r0, PMK_LEN);
  3299. wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name",
  3300. sm->pmk_r0_name, WPA_PMK_NAME_LEN);
  3301. wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
  3302. MAC2STR(sm->r1kh_id));
  3303. pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
  3304. if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
  3305. pos) < 0) {
  3306. wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
  3307. return -1;
  3308. }
  3309. wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", pos, WPA_PMK_NAME_LEN);
  3310. #ifdef CONFIG_IEEE80211W
  3311. if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
  3312. /* Management Group Cipher Suite */
  3313. pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
  3314. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
  3315. }
  3316. #endif /* CONFIG_IEEE80211W */
  3317. rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
  3318. return 0;
  3319. }
  3320. #endif /* CONFIG_IEEE80211R */
  3321. struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
  3322. size_t *kek_len, const u8 **snonce,
  3323. const u8 **anonce,
  3324. const struct wpabuf **hlp,
  3325. unsigned int num_hlp)
  3326. {
  3327. struct wpabuf *buf;
  3328. size_t len;
  3329. unsigned int i;
  3330. len = 1000;
  3331. #ifdef CONFIG_IEEE80211R
  3332. if (sm->fils_ft_ies)
  3333. len += wpabuf_len(sm->fils_ft_ies);
  3334. if (wpa_key_mgmt_ft(sm->key_mgmt))
  3335. len += 256;
  3336. #endif /* CONFIG_IEEE80211R */
  3337. for (i = 0; hlp && i < num_hlp; i++)
  3338. len += 10 + wpabuf_len(hlp[i]);
  3339. buf = wpabuf_alloc(len);
  3340. if (!buf)
  3341. return NULL;
  3342. #ifdef CONFIG_IEEE80211R
  3343. if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
  3344. /* MDE and FTE when using FILS+FT */
  3345. wpabuf_put_buf(buf, sm->fils_ft_ies);
  3346. /* RSNE with PMKR1Name in PMKID field */
  3347. if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
  3348. wpabuf_free(buf);
  3349. return NULL;
  3350. }
  3351. }
  3352. #endif /* CONFIG_IEEE80211R */
  3353. /* FILS Session */
  3354. wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
  3355. wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
  3356. /* Element ID Extension */
  3357. wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
  3358. wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
  3359. /* Everything after FILS Session element gets encrypted in the driver
  3360. * with KEK. The buffer returned from here is the plaintext version. */
  3361. /* TODO: FILS Public Key */
  3362. /* FILS Key Confirm */
  3363. wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
  3364. wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
  3365. /* Element ID Extension */
  3366. wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
  3367. wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
  3368. /* FILS HLP Container */
  3369. for (i = 0; hlp && i < num_hlp; i++) {
  3370. const u8 *pos = wpabuf_head(hlp[i]);
  3371. size_t left = wpabuf_len(hlp[i]);
  3372. wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
  3373. if (left <= 254)
  3374. len = 1 + left;
  3375. else
  3376. len = 255;
  3377. wpabuf_put_u8(buf, len); /* Length */
  3378. /* Element ID Extension */
  3379. wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
  3380. /* Destination MAC Address, Source MAC Address, HLP Packet.
  3381. * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
  3382. * header when LPD is used). */
  3383. wpabuf_put_data(buf, pos, len - 1);
  3384. pos += len - 1;
  3385. left -= len - 1;
  3386. while (left) {
  3387. wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
  3388. len = left > 255 ? 255 : left;
  3389. wpabuf_put_u8(buf, len);
  3390. wpabuf_put_data(buf, pos, len);
  3391. pos += len;
  3392. left -= len;
  3393. }
  3394. }
  3395. /* TODO: FILS IP Address Assignment */
  3396. wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
  3397. *kek = sm->ptk.kek;
  3398. *kek_len = sm->ptk.kek_len;
  3399. wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
  3400. *snonce = sm->fils_nonce;
  3401. wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
  3402. *snonce, FILS_NONCE_LEN);
  3403. *anonce = sm->fils_anonce;
  3404. wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
  3405. *anonce, FILS_NONCE_LEN);
  3406. return buf;
  3407. }
  3408. static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
  3409. {
  3410. const u8 *pos, *end;
  3411. wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
  3412. if (len < 2 * ETH_ALEN)
  3413. return;
  3414. pos = resp + 2 * ETH_ALEN;
  3415. end = resp + len;
  3416. if (end - pos >= 6 &&
  3417. os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
  3418. pos += 6; /* Remove SNAP/LLC header */
  3419. wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
  3420. }
  3421. static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
  3422. size_t len)
  3423. {
  3424. const u8 *end = pos + len;
  3425. u8 *tmp, *tmp_pos;
  3426. /* Check if there are any FILS HLP Container elements */
  3427. while (end - pos >= 2) {
  3428. if (2 + pos[1] > end - pos)
  3429. return;
  3430. if (pos[0] == WLAN_EID_EXTENSION &&
  3431. pos[1] >= 1 + 2 * ETH_ALEN &&
  3432. pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
  3433. break;
  3434. pos += 2 + pos[1];
  3435. }
  3436. if (end - pos < 2)
  3437. return; /* No FILS HLP Container elements */
  3438. tmp = os_malloc(end - pos);
  3439. if (!tmp)
  3440. return;
  3441. while (end - pos >= 2) {
  3442. if (2 + pos[1] > end - pos ||
  3443. pos[0] != WLAN_EID_EXTENSION ||
  3444. pos[1] < 1 + 2 * ETH_ALEN ||
  3445. pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
  3446. break;
  3447. tmp_pos = tmp;
  3448. os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
  3449. tmp_pos += pos[1] - 1;
  3450. pos += 2 + pos[1];
  3451. /* Add possible fragments */
  3452. while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
  3453. 2 + pos[1] <= end - pos) {
  3454. os_memcpy(tmp_pos, pos + 2, pos[1]);
  3455. tmp_pos += pos[1];
  3456. pos += 2 + pos[1];
  3457. }
  3458. fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
  3459. }
  3460. os_free(tmp);
  3461. }
  3462. int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
  3463. {
  3464. const struct ieee80211_mgmt *mgmt;
  3465. const u8 *end, *ie_start;
  3466. struct ieee802_11_elems elems;
  3467. int keylen, rsclen;
  3468. enum wpa_alg alg;
  3469. struct wpa_gtk_data gd;
  3470. int maxkeylen;
  3471. struct wpa_eapol_ie_parse kde;
  3472. if (!sm || !sm->ptk_set) {
  3473. wpa_printf(MSG_DEBUG, "FILS: No KEK available");
  3474. return -1;
  3475. }
  3476. if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
  3477. wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
  3478. return -1;
  3479. }
  3480. wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
  3481. resp, len);
  3482. mgmt = (const struct ieee80211_mgmt *) resp;
  3483. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
  3484. return -1;
  3485. end = resp + len;
  3486. /* Same offset for Association Response and Reassociation Response */
  3487. ie_start = mgmt->u.assoc_resp.variable;
  3488. if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
  3489. ParseFailed) {
  3490. wpa_printf(MSG_DEBUG,
  3491. "FILS: Failed to parse decrypted elements");
  3492. goto fail;
  3493. }
  3494. if (!elems.fils_session) {
  3495. wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
  3496. return -1;
  3497. }
  3498. if (os_memcmp(elems.fils_session, sm->fils_session,
  3499. FILS_SESSION_LEN) != 0) {
  3500. wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
  3501. wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
  3502. elems.fils_session, FILS_SESSION_LEN);
  3503. wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
  3504. sm->fils_session, FILS_SESSION_LEN);
  3505. }
  3506. /* TODO: FILS Public Key */
  3507. if (!elems.fils_key_confirm) {
  3508. wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
  3509. goto fail;
  3510. }
  3511. if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
  3512. wpa_printf(MSG_DEBUG,
  3513. "FILS: Unexpected Key-Auth length %d (expected %d)",
  3514. elems.fils_key_confirm_len,
  3515. (int) sm->fils_key_auth_len);
  3516. goto fail;
  3517. }
  3518. if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
  3519. sm->fils_key_auth_len) != 0) {
  3520. wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
  3521. wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
  3522. elems.fils_key_confirm,
  3523. elems.fils_key_confirm_len);
  3524. wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
  3525. sm->fils_key_auth_ap, sm->fils_key_auth_len);
  3526. goto fail;
  3527. }
  3528. /* Key Delivery */
  3529. if (!elems.key_delivery) {
  3530. wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
  3531. goto fail;
  3532. }
  3533. /* Parse GTK and set the key to the driver */
  3534. os_memset(&gd, 0, sizeof(gd));
  3535. if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
  3536. elems.key_delivery_len - WPA_KEY_RSC_LEN,
  3537. &kde) < 0) {
  3538. wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
  3539. goto fail;
  3540. }
  3541. if (!kde.gtk) {
  3542. wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
  3543. goto fail;
  3544. }
  3545. maxkeylen = gd.gtk_len = kde.gtk_len - 2;
  3546. if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
  3547. gd.gtk_len, maxkeylen,
  3548. &gd.key_rsc_len, &gd.alg))
  3549. goto fail;
  3550. wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
  3551. gd.keyidx = kde.gtk[0] & 0x3;
  3552. gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
  3553. !!(kde.gtk[0] & BIT(2)));
  3554. if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
  3555. wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
  3556. (unsigned long) kde.gtk_len - 2);
  3557. goto fail;
  3558. }
  3559. os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
  3560. wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
  3561. if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery) < 0) {
  3562. wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
  3563. goto fail;
  3564. }
  3565. if (ieee80211w_set_keys(sm, &kde) < 0) {
  3566. wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
  3567. goto fail;
  3568. }
  3569. alg = wpa_cipher_to_alg(sm->pairwise_cipher);
  3570. keylen = wpa_cipher_key_len(sm->pairwise_cipher);
  3571. rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
  3572. wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
  3573. sm->ptk.tk, keylen);
  3574. if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen,
  3575. sm->ptk.tk, keylen) < 0) {
  3576. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  3577. "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
  3578. MACSTR ")",
  3579. alg, keylen, MAC2STR(sm->bssid));
  3580. goto fail;
  3581. }
  3582. /* TODO: TK could be cleared after auth frame exchange now that driver
  3583. * takes care of association frame encryption/decryption. */
  3584. /* TK is not needed anymore in supplicant */
  3585. os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
  3586. /* FILS HLP Container */
  3587. fils_process_hlp_container(sm, ie_start, end - ie_start);
  3588. /* TODO: FILS IP Address Assignment */
  3589. wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
  3590. sm->fils_completed = 1;
  3591. return 0;
  3592. fail:
  3593. return -1;
  3594. }
  3595. void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
  3596. {
  3597. if (sm)
  3598. sm->fils_completed = !!set;
  3599. }
  3600. #endif /* CONFIG_FILS */
  3601. int wpa_fils_is_completed(struct wpa_sm *sm)
  3602. {
  3603. #ifdef CONFIG_FILS
  3604. return sm && sm->fils_completed;
  3605. #else /* CONFIG_FILS */
  3606. return 0;
  3607. #endif /* CONFIG_FILS */
  3608. }
  3609. #ifdef CONFIG_OWE
  3610. struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm)
  3611. {
  3612. struct wpabuf *ie = NULL, *pub = NULL;
  3613. crypto_ecdh_deinit(sm->owe_ecdh);
  3614. sm->owe_ecdh = crypto_ecdh_init(OWE_DH_GROUP);
  3615. if (!sm->owe_ecdh)
  3616. goto fail;
  3617. pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
  3618. if (!pub)
  3619. goto fail;
  3620. ie = wpabuf_alloc(5 + wpabuf_len(pub));
  3621. if (!ie)
  3622. goto fail;
  3623. wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
  3624. wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
  3625. wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
  3626. wpabuf_put_le16(ie, OWE_DH_GROUP);
  3627. wpabuf_put_buf(ie, pub);
  3628. wpabuf_free(pub);
  3629. wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
  3630. ie);
  3631. return ie;
  3632. fail:
  3633. wpabuf_free(pub);
  3634. crypto_ecdh_deinit(sm->owe_ecdh);
  3635. sm->owe_ecdh = NULL;
  3636. return NULL;
  3637. }
  3638. int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *resp_ies,
  3639. size_t resp_ies_len)
  3640. {
  3641. struct ieee802_11_elems elems;
  3642. u16 group;
  3643. struct wpabuf *secret, *pub, *hkey;
  3644. int res;
  3645. u8 prk[SHA256_MAC_LEN], pmkid[SHA256_MAC_LEN];
  3646. const char *info = "OWE Key Generation";
  3647. const u8 *addr[2];
  3648. size_t len[2];
  3649. if (!resp_ies ||
  3650. ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
  3651. ParseFailed ||
  3652. !elems.owe_dh) {
  3653. wpa_printf(MSG_INFO,
  3654. "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
  3655. return -1;
  3656. }
  3657. group = WPA_GET_LE16(elems.owe_dh);
  3658. if (group != OWE_DH_GROUP) {
  3659. wpa_printf(MSG_INFO,
  3660. "OWE: Unexpected Diffie-Hellman group in response: %u",
  3661. group);
  3662. return -1;
  3663. }
  3664. if (!sm->owe_ecdh) {
  3665. wpa_printf(MSG_INFO, "OWE: No ECDH state available");
  3666. return -1;
  3667. }
  3668. secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
  3669. elems.owe_dh + 2,
  3670. elems.owe_dh_len - 2);
  3671. if (!secret) {
  3672. wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
  3673. return -1;
  3674. }
  3675. wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
  3676. /* prk = HKDF-extract(C | A | group, z) */
  3677. pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
  3678. if (!pub) {
  3679. wpabuf_clear_free(secret);
  3680. return -1;
  3681. }
  3682. /* PMKID = Truncate-128(Hash(C | A)) */
  3683. addr[0] = wpabuf_head(pub);
  3684. len[0] = wpabuf_len(pub);
  3685. addr[1] = elems.owe_dh + 2;
  3686. len[1] = elems.owe_dh_len - 2;
  3687. res = sha256_vector(2, addr, len, pmkid);
  3688. if (res < 0) {
  3689. wpabuf_free(pub);
  3690. wpabuf_clear_free(secret);
  3691. return -1;
  3692. }
  3693. hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
  3694. if (!hkey) {
  3695. wpabuf_free(pub);
  3696. wpabuf_clear_free(secret);
  3697. return -1;
  3698. }
  3699. wpabuf_put_buf(hkey, pub); /* C */
  3700. wpabuf_free(pub);
  3701. wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
  3702. wpabuf_put_le16(hkey, OWE_DH_GROUP); /* group */
  3703. res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
  3704. wpabuf_head(secret), wpabuf_len(secret), prk);
  3705. wpabuf_clear_free(hkey);
  3706. wpabuf_clear_free(secret);
  3707. if (res < 0)
  3708. return -1;
  3709. wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, SHA256_MAC_LEN);
  3710. /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
  3711. res = hmac_sha256_kdf(prk, SHA256_MAC_LEN, NULL, (const u8 *) info,
  3712. os_strlen(info), sm->pmk, PMK_LEN);
  3713. os_memset(prk, 0, SHA256_MAC_LEN);
  3714. if (res < 0)
  3715. return -1;
  3716. wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, PMK_LEN);
  3717. wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
  3718. /* TODO: Add PMKSA cache entry */
  3719. return 0;
  3720. }
  3721. #endif /* CONFIG_OWE */
  3722. void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
  3723. {
  3724. #ifdef CONFIG_FILS
  3725. if (sm && fils_cache_id) {
  3726. sm->fils_cache_id_set = 1;
  3727. os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
  3728. }
  3729. #endif /* CONFIG_FILS */
  3730. }