wpa.c 69 KB

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