wpa.c 72 KB

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