wpa.c 84 KB

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