wpa.c 65 KB

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