wpa.c 73 KB

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