wpa.c 83 KB

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