wpa.c 105 KB

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