wpa.c 71 KB

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