wpa.c 74 KB

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