wpa.c 68 KB

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